Search in sources :

Example 1 with BLangRecordKeyValue

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

@Override
public void visit(BLangJSONLiteral jsonLiteral) {
    jsonLiteral.regIndex = calcAndGetExprRegIndex(jsonLiteral);
    Operand typeCPIndex = getTypeCPIndex(jsonLiteral.type);
    emit(InstructionCodes.NEWJSON, jsonLiteral.regIndex, typeCPIndex);
    for (BLangRecordKeyValue keyValue : jsonLiteral.keyValuePairs) {
        BLangExpression keyExpr = keyValue.key.expr;
        genNode(keyExpr, this.env);
        BLangExpression valueExpr = keyValue.valueExpr;
        genNode(valueExpr, this.env);
        emit(InstructionCodes.JSONSTORE, jsonLiteral.regIndex, keyExpr.regIndex, valueExpr.regIndex);
    }
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) BLangRecordKeyValue(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 2 with BLangRecordKeyValue

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue in project ballerina by ballerina-lang.

the class BLangPackageBuilder method addKeyValueRecord.

public void addKeyValueRecord(Set<Whitespace> ws) {
    BLangRecordKeyValue keyValue = (BLangRecordKeyValue) TreeBuilder.createRecordKeyValue();
    keyValue.addWS(ws);
    keyValue.valueExpr = (BLangExpression) exprNodeStack.pop();
    keyValue.key = new BLangRecordKey((BLangExpression) exprNodeStack.pop());
    recordLiteralNodes.peek().keyValuePairs.add(keyValue);
}
Also used : BLangRecordKey(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKey) BLangRecordKeyValue(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 3 with BLangRecordKeyValue

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue in project ballerina by ballerina-lang.

the class TypeChecker method checkRecLiteralKeyValue.

private void checkRecLiteralKeyValue(BLangRecordKeyValue keyValuePair, BType recType) {
    BType fieldType = symTable.errType;
    BLangExpression valueExpr = keyValuePair.valueExpr;
    switch(recType.tag) {
        case TypeTags.STRUCT:
            fieldType = checkStructLiteralKeyExpr(keyValuePair.key, recType, RecordKind.STRUCT);
            break;
        case TypeTags.MAP:
            fieldType = checkMapLiteralKeyExpr(keyValuePair.key.expr, recType, RecordKind.MAP);
            break;
        case TypeTags.JSON:
            fieldType = checkJSONLiteralKeyExpr(keyValuePair.key, recType, RecordKind.JSON);
            // If the field is again a struct, treat that literal expression as another constraint JSON.
            if (fieldType.tag == TypeTags.STRUCT) {
                fieldType = new BJSONType(TypeTags.JSON, fieldType, symTable.jsonType.tsymbol);
            }
            // First visit the expression having field type, as the expected type.
            checkExpr(valueExpr, this.env, Lists.of(fieldType));
            // Again check the type compatibility with JSON
            if (valueExpr.impConversionExpr == null) {
                types.checkTypes(valueExpr, Lists.of(valueExpr.type), Lists.of(symTable.jsonType));
            } else {
                BType valueType = valueExpr.type;
                types.checkTypes(valueExpr, valueExpr.impConversionExpr.types, Lists.of(symTable.jsonType));
                valueExpr.type = valueType;
            }
            resultTypes = Lists.of(valueExpr.type);
            return;
    }
    checkExpr(valueExpr, this.env, Lists.of(fieldType));
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 4 with BLangRecordKeyValue

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

@Override
public void visit(BLangMapLiteral mapLiteral) {
    Operand mapVarRegIndex = calcAndGetExprRegIndex(mapLiteral);
    Operand typeCPIndex = getTypeCPIndex(mapLiteral.type);
    emit(InstructionCodes.NEWMAP, mapVarRegIndex, typeCPIndex);
    // Handle Map init stuff
    for (BLangRecordKeyValue keyValue : mapLiteral.keyValuePairs) {
        BLangExpression keyExpr = keyValue.key.expr;
        genNode(keyExpr, this.env);
        BLangExpression valueExpr = keyValue.valueExpr;
        genNode(valueExpr, this.env);
        BMapType mapType = (BMapType) mapLiteral.type;
        int opcode = getValueToRefTypeCastOpcode(mapType.constraint.tag);
        if (opcode == InstructionCodes.NOP) {
            emit(InstructionCodes.MAPSTORE, mapVarRegIndex, keyExpr.regIndex, valueExpr.regIndex);
        } else {
            RegIndex refRegMapValue = getRegIndex(TypeTags.ANY);
            emit(opcode, valueExpr.regIndex, refRegMapValue);
            emit(InstructionCodes.MAPSTORE, mapVarRegIndex, keyExpr.regIndex, refRegMapValue);
        }
    }
}
Also used : BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) BLangRecordKeyValue(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 5 with BLangRecordKeyValue

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

@Override
public void visit(BLangStructLiteral structLiteral) {
    BStructSymbol structSymbol = (BStructSymbol) structLiteral.type.tsymbol;
    int pkgCPIndex = addPackageRefCPEntry(currentPkgInfo, structSymbol.pkgID);
    int structNameCPIndex = addUTF8CPEntry(currentPkgInfo, structSymbol.name.value);
    StructureRefCPEntry structureRefCPEntry = new StructureRefCPEntry(pkgCPIndex, structNameCPIndex);
    Operand structCPIndex = getOperand(currentPkgInfo.addCPEntry(structureRefCPEntry));
    // Emit an instruction to create a new struct.
    RegIndex structRegIndex = calcAndGetExprRegIndex(structLiteral);
    emit(InstructionCodes.NEWSTRUCT, structCPIndex, structRegIndex);
    // Invoke the struct default values init function here.
    if (structSymbol.defaultsValuesInitFunc != null) {
        int funcRefCPIndex = getFuncRefCPIndex(structSymbol.defaultsValuesInitFunc.symbol);
        // call funcRefCPIndex 1 structRegIndex 0
        Operand[] operands = new Operand[5];
        operands[0] = getOperand(funcRefCPIndex);
        operands[1] = getOperand(false);
        operands[2] = getOperand(1);
        operands[3] = structRegIndex;
        operands[4] = getOperand(0);
        emit(InstructionCodes.CALL, operands);
    }
    // Invoke the struct initializer here.
    if (structLiteral.initializer != null) {
        int funcRefCPIndex = getFuncRefCPIndex(structLiteral.initializer.symbol);
        // call funcRefCPIndex 1 structRegIndex 0
        Operand[] operands = new Operand[5];
        operands[0] = getOperand(funcRefCPIndex);
        operands[1] = getOperand(false);
        operands[2] = getOperand(1);
        operands[3] = structRegIndex;
        operands[4] = getOperand(0);
        emit(InstructionCodes.CALL, operands);
    }
    // Generate code the struct literal.
    for (BLangRecordKeyValue keyValue : structLiteral.keyValuePairs) {
        BLangRecordKey key = keyValue.key;
        Operand fieldIndex = key.fieldSymbol.varIndex;
        genNode(keyValue.valueExpr, this.env);
        int opcode = getOpcode(key.fieldSymbol.type.tag, InstructionCodes.IFIELDSTORE);
        emit(opcode, structRegIndex, fieldIndex, keyValue.valueExpr.regIndex);
    }
}
Also used : BLangRecordKey(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKey) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) StructureRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry) BLangRecordKeyValue(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Aggregations

BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)4 BLangRecordKeyValue (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue)4 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)3 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)2 BLangRecordKey (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKey)2 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)2 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)1 BJSONType (org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType)1 BMapType (org.wso2.ballerinalang.compiler.semantics.model.types.BMapType)1 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)1 StructureRefCPEntry (org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry)1