Search in sources :

Example 1 with BLangTypeInit

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

the class TypeChecker method visit.

public void visit(BLangTypeInit cIExpr) {
    BType actualType;
    if (cIExpr.userDefinedType != null) {
        actualType = symResolver.resolveTypeNode(cIExpr.userDefinedType, env);
    } else {
        actualType = expTypes.get(0);
    }
    if (actualType == symTable.errType) {
        // TODO dlog error?
        resultTypes = Lists.of(symTable.errType);
        return;
    }
    if (actualType.tag != TypeTags.STRUCT) {
        // TODO dlog error?
        resultTypes = Lists.of(symTable.errType);
        return;
    }
    cIExpr.objectInitInvocation.symbol = ((BStructSymbol) actualType.tsymbol).initializerFunc.symbol;
    checkInvocationParam(cIExpr.objectInitInvocation);
    resultTypes = types.checkTypes(cIExpr, Lists.of(actualType), expTypes);
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)

Example 2 with BLangTypeInit

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

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

the class CodeGenerator method visit.

public void visit(BLangTypeInit cIExpr) {
    BSymbol structSymbol = cIExpr.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(cIExpr);
    emit(InstructionCodes.NEWSTRUCT, structCPIndex, structRegIndex);
    // Invoke the struct initializer here.
    Operand[] operands = getFuncOperands(cIExpr.objectInitInvocation);
    Operand[] callOperands = new Operand[operands.length + 1];
    callOperands[0] = operands[0];
    callOperands[1] = operands[1];
    callOperands[2] = getOperand(operands[2].value + 1);
    callOperands[3] = structRegIndex;
    System.arraycopy(operands, 3, callOperands, 4, operands.length - 3);
    emit(InstructionCodes.CALL, callOperands);
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) StructureRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.StructureRefCPEntry) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 4 with BLangTypeInit

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

the class BLangPackageBuilder method addTypeInitExpression.

public void addTypeInitExpression(DiagnosticPos pos, Set<Whitespace> ws, String initName, boolean typeAvailable, boolean exprAvailable) {
    BLangTypeInit objectInitNode = (BLangTypeInit) TreeBuilder.createObjectInitNode();
    objectInitNode.pos = pos;
    objectInitNode.addWS(ws);
    if (typeAvailable) {
        objectInitNode.userDefinedType = (BLangUserDefinedType) typeNodeStack.pop();
    }
    BLangInvocation invocationNode = (BLangInvocation) TreeBuilder.createInvocationNode();
    invocationNode.pos = pos;
    invocationNode.addWS(ws);
    if (exprAvailable) {
        List<ExpressionNode> exprNodes = exprNodeListStack.pop();
        Set<Whitespace> cws = commaWsStack.pop();
        exprNodes.forEach(exprNode -> {
            invocationNode.argExprs.add((BLangExpression) exprNode);
            objectInitNode.argsExpr.add((BLangExpression) exprNode);
        });
        invocationNode.addWS(cws);
        objectInitNode.addWS(cws);
    }
    // TODO check whether pkgName can be be empty
    IdentifierNode pkgNameNode = TreeBuilder.createIdentifierNode();
    IdentifierNode nameNode = createIdentifier(initName);
    BLangNameReference nameReference = new BLangNameReference(pos, ws, pkgNameNode, nameNode);
    invocationNode.name = (BLangIdentifier) nameReference.name;
    invocationNode.addWS(nameReference.ws);
    invocationNode.pkgAlias = (BLangIdentifier) nameReference.pkgAlias;
    objectInitNode.objectInitInvocation = invocationNode;
    this.addExpressionNode(objectInitNode);
}
Also used : BLangTypeInit(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit) BLangNameReference(org.wso2.ballerinalang.compiler.tree.BLangNameReference) ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) SelectExpressionNode(org.ballerinalang.model.tree.clauses.SelectExpressionNode) IdentifierNode(org.ballerinalang.model.tree.IdentifierNode) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) Whitespace(org.ballerinalang.model.Whitespace)

Aggregations

BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)2 BLangTypeInit (org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit)2 Whitespace (org.ballerinalang.model.Whitespace)1 IdentifierNode (org.ballerinalang.model.tree.IdentifierNode)1 SelectExpressionNode (org.ballerinalang.model.tree.clauses.SelectExpressionNode)1 ExpressionNode (org.ballerinalang.model.tree.expressions.ExpressionNode)1 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)1 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)1 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)1 BLangNameReference (org.wso2.ballerinalang.compiler.tree.BLangNameReference)1 BLangArrayLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral)1 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)1 BLangJSONLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangJSONLiteral)1 BLangMapLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangMapLiteral)1 BLangStreamLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStreamLiteral)1 BLangStructLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangStructLiteral)1 BLangTableLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangTableLiteral)1 BLangXMLSequenceLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLSequenceLiteral)1 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)1 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)1