Search in sources :

Example 1 with BLangStructLiteral

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

the class Desugar method getReturnType.

private BLangStructLiteral getReturnType(BLangTableQueryExpression tableQueryExpression) {
    // create a literal to represent the sql query.
    BTableType tableType = (BTableType) tableQueryExpression.type;
    BStructType structType = (BStructType) tableType.constraint;
    return new BLangStructLiteral(new ArrayList<>(), structType);
}
Also used : BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BLangStructLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStructLiteral) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType)

Example 2 with BLangStructLiteral

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

the class Desugar method getInitExpr.

private BLangExpression getInitExpr(BType type) {
    // Don't need to create an empty init expressions if the type allows null.
    if (type.isNullable()) {
        return null;
    }
    switch(type.tag) {
        case TypeTags.INT:
        case TypeTags.FLOAT:
        case TypeTags.BOOLEAN:
        case TypeTags.STRING:
        case TypeTags.BLOB:
            // Int, float, boolean, string, blob types will get default values from VM side.
            break;
        case TypeTags.JSON:
            return new BLangJSONLiteral(new ArrayList<>(), type);
        case TypeTags.XML:
            return new BLangXMLSequenceLiteral(type);
        case TypeTags.TABLE:
            return new BLangTableLiteral(type);
        case TypeTags.STREAM:
            return new BLangStreamLiteral(type, null);
        case TypeTags.MAP:
            return new BLangMapLiteral(new ArrayList<>(), type);
        case TypeTags.STRUCT:
            if (((BStructSymbol) type.tsymbol).isObject) {
                return new BLangTypeInit();
            }
            return new BLangStructLiteral(new ArrayList<>(), type);
        case TypeTags.ARRAY:
            BLangArrayLiteral array = new BLangArrayLiteral();
            array.exprs = new ArrayList<>();
            array.type = type;
            return rewriteExpr(array);
        default:
            break;
    }
    return null;
}
Also used : BLangTypeInit(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit) BLangStructLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStructLiteral) BLangXMLSequenceLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLSequenceLiteral) BLangStreamLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStreamLiteral) BLangMapLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangMapLiteral) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) BLangJSONLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangJSONLiteral) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) BLangTableLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangTableLiteral)

Example 3 with BLangStructLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStructLiteral 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)

Example 4 with BLangStructLiteral

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

the class Desugar method addArgInitExpr.

private void addArgInitExpr(BLangTransformer transformerNode, BLangVariable var) {
    BLangSimpleVarRef varRef = new BLangLocalVarRef(var.symbol);
    varRef.lhsVar = true;
    varRef.pos = var.pos;
    varRef.type = var.type;
    BLangExpression initExpr = null;
    switch(var.type.tag) {
        case TypeTags.MAP:
            initExpr = new BLangMapLiteral(new ArrayList<>(), var.type);
            break;
        case TypeTags.JSON:
            initExpr = new BLangJSONLiteral(new ArrayList<>(), var.type);
            break;
        case TypeTags.STRUCT:
            initExpr = new BLangStructLiteral(new ArrayList<>(), var.type);
            break;
        case TypeTags.INT:
        case TypeTags.FLOAT:
        case TypeTags.STRING:
        case TypeTags.BOOLEAN:
        case TypeTags.BLOB:
        case TypeTags.XML:
            return;
        case TypeTags.TABLE:
        case TypeTags.STREAM:
            // TODO: add this once the able initializing is supported.
            return;
        default:
            return;
    }
    initExpr.pos = var.pos;
    BLangAssignment assignStmt = (BLangAssignment) TreeBuilder.createAssignmentNode();
    assignStmt.pos = var.pos;
    assignStmt.addVariable(varRef);
    assignStmt.expr = initExpr;
    transformerNode.body.stmts.add(0, assignStmt);
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangLocalVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangLocalVarRef) BLangStructLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStructLiteral) ArrayList(java.util.ArrayList) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangMapLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangMapLiteral) BLangJSONLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangJSONLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 5 with BLangStructLiteral

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

the class Desugar method visit.

@Override
public void visit(BLangRecordLiteral recordLiteral) {
    recordLiteral.keyValuePairs.forEach(keyValue -> {
        BLangExpression keyExpr = keyValue.key.expr;
        if (keyExpr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
            BLangSimpleVarRef varRef = (BLangSimpleVarRef) keyExpr;
            keyValue.key.expr = createStringLiteral(varRef.pos, varRef.variableName.value);
        } else {
            keyValue.key.expr = rewriteExpr(keyValue.key.expr);
        }
        keyValue.valueExpr = rewriteExpr(keyValue.valueExpr);
    });
    BLangExpression expr;
    if (recordLiteral.type.tag == TypeTags.STRUCT) {
        expr = new BLangStructLiteral(recordLiteral.keyValuePairs, recordLiteral.type);
    } else if (recordLiteral.type.tag == TypeTags.MAP) {
        expr = new BLangMapLiteral(recordLiteral.keyValuePairs, recordLiteral.type);
    } else if (recordLiteral.type.tag == TypeTags.TABLE) {
        expr = new BLangTableLiteral(recordLiteral.type);
    } else if (recordLiteral.type.tag == TypeTags.STREAM) {
        expr = new BLangStreamLiteral(recordLiteral.type, recordLiteral.name);
    } else {
        expr = new BLangJSONLiteral(recordLiteral.keyValuePairs, recordLiteral.type);
    }
    result = rewriteExpr(expr);
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangStructLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStructLiteral) BLangMapLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangMapLiteral) BLangStreamLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStreamLiteral) BLangJSONLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangJSONLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangTableLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangTableLiteral)

Aggregations

BLangStructLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStructLiteral)4 BLangJSONLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangJSONLiteral)3 BLangMapLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangMapLiteral)3 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)2 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)2 BLangStreamLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStreamLiteral)2 BLangTableLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangTableLiteral)2 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)2 ArrayList (java.util.ArrayList)1 BStructType (org.wso2.ballerinalang.compiler.semantics.model.types.BStructType)1 BTableType (org.wso2.ballerinalang.compiler.semantics.model.types.BTableType)1 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)1 BLangArrayLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral)1 BLangRecordKey (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKey)1 BLangRecordKeyValue (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue)1 BLangLocalVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef.BLangLocalVarRef)1 BLangTypeInit (org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit)1 BLangXMLSequenceLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLSequenceLiteral)1 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)1 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)1