Search in sources :

Example 71 with BLangVariable

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

the class AnnotationDesugar method createAnnotationMapEntryVar.

private BLangVariable createAnnotationMapEntryVar(String key, BLangVariable annotationMapVar, BLangBlockStmt target, BSymbol parentSymbol) {
    // create: map key = {};
    final BLangRecordLiteral recordLiteralNode = ASTBuilderUtil.createEmptyRecordLiteral(target.pos, symTable.mapType);
    BLangVariable entryVariable = ASTBuilderUtil.createVariable(target.pos, key, recordLiteralNode.type);
    entryVariable.expr = recordLiteralNode;
    ASTBuilderUtil.defineVariable(entryVariable, parentSymbol, names);
    ASTBuilderUtil.createVariableDefStmt(target.pos, target).var = entryVariable;
    // create: annotationMapVar["key"] = key;
    BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(target.pos, target);
    assignmentStmt.expr = ASTBuilderUtil.createVariableRef(target.pos, entryVariable.symbol);
    BLangIndexBasedAccess indexAccessNode = (BLangIndexBasedAccess) TreeBuilder.createIndexBasedAccessNode();
    indexAccessNode.pos = target.pos;
    indexAccessNode.indexExpr = ASTBuilderUtil.createLiteral(target.pos, symTable.stringType, key);
    indexAccessNode.expr = ASTBuilderUtil.createVariableRef(target.pos, annotationMapVar.symbol);
    indexAccessNode.type = recordLiteralNode.type;
    assignmentStmt.varRefs.add(indexAccessNode);
    return entryVariable;
}
Also used : BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 72 with BLangVariable

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

the class AnnotationDesugar method rewritePackageAnnotations.

protected void rewritePackageAnnotations(BLangPackage pkgNode) {
    BLangFunction initFunction = pkgNode.initFunction;
    // Remove last return statement. we will add it later. TODO : Fix this properly.
    initFunction.body.stmts.remove(initFunction.body.stmts.size() - 1);
    // This is the variable which store all package level annotations.
    BLangVariable annotationMap = createGlobalAnnotationMapVar(pkgNode);
    // handle Service Annotations.
    for (BLangService service : pkgNode.services) {
        generateAnnotations(service, service.name.value, initFunction, annotationMap);
        for (BLangResource resource : service.resources) {
            String key = service.name.value + DOT + resource.name.value;
            generateAnnotations(resource, key, initFunction, annotationMap);
        }
    }
    // Handle Function Annotations.
    for (BLangFunction function : pkgNode.functions) {
        generateAnnotations(function, function.symbol.name.value, initFunction, annotationMap);
    }
    // Handle Connector Annotations.
    for (BLangConnector connector : pkgNode.connectors) {
        generateAnnotations(connector, connector.name.value, initFunction, annotationMap);
        for (BLangAction action : connector.actions) {
            String key = connector.name.value + DOT + action.name.value;
            generateAnnotations(connector, key, initFunction, annotationMap);
        }
    }
    // Handle Struct Annotations.
    for (BLangStruct struct : pkgNode.structs) {
        generateAnnotations(struct, struct.name.value, initFunction, annotationMap);
        for (BLangVariable field : struct.fields) {
            String key = struct.name.value + DOT + field.name.value;
            generateAnnotations(field, key, initFunction, annotationMap);
        }
    }
    for (BLangEndpoint globalEndpoint : pkgNode.globalEndpoints) {
        generateAnnotations(globalEndpoint, globalEndpoint.name.value, initFunction, annotationMap);
    }
    ASTBuilderUtil.createReturnStmt(pkgNode.pos, initFunction.body);
}
Also used : BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction)

Example 73 with BLangVariable

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

the class AnnotationDesugar method createGlobalAnnotationMapVar.

private BLangVariable createGlobalAnnotationMapVar(BLangPackage pkgNode) {
    DiagnosticPos pos = pkgNode.pos;
    BLangVariable annotationMap = ASTBuilderUtil.createVariable(pkgNode.pos, ANNOTATION_DATA, symTable.mapType);
    ASTBuilderUtil.defineVariable(annotationMap, pkgNode.symbol, names);
    pkgNode.addGlobalVariable(annotationMap);
    final BLangRecordLiteral recordLiteralNode = ASTBuilderUtil.createEmptyRecordLiteral(pos, symTable.mapType);
    final BLangAssignment annMapAssignment = ASTBuilderUtil.createAssignmentStmt(pos, pkgNode.initFunction.body);
    annMapAssignment.expr = recordLiteralNode;
    annMapAssignment.addVariable(ASTBuilderUtil.createVariableRef(pos, annotationMap.symbol));
    return annotationMap;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 74 with BLangVariable

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

the class AnnotationDesugar method generateAnnotations.

private void generateAnnotations(AnnotatableNode node, String key, BLangFunction target, BLangVariable annMapVar) {
    if (node.getAnnotationAttachments().size() == 0) {
        return;
    }
    BLangVariable entryVar = createAnnotationMapEntryVar(key, annMapVar, target.body, target.symbol);
    int annCount = 0;
    for (AnnotationAttachmentNode attachment : node.getAnnotationAttachments()) {
        initAnnotation((BLangAnnotationAttachment) attachment, entryVar, target.body, target.symbol, annCount++);
    }
}
Also used : BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 75 with BLangVariable

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

the class AnnotationDesugar method initAnnotation.

private void initAnnotation(BLangAnnotationAttachment attachment, BLangVariable annotationMapEntryVar, BLangBlockStmt target, BSymbol parentSymbol, int index) {
    BLangVariable annotationVar = null;
    if (attachment.annotationSymbol.attachedType != null) {
        // create: AttachedType annotationVar = { annotation-expression }
        annotationVar = ASTBuilderUtil.createVariable(attachment.pos, attachment.annotationName.value, attachment.annotationSymbol.attachedType.type);
        annotationVar.expr = attachment.expr;
        ASTBuilderUtil.defineVariable(annotationVar, parentSymbol, names);
        ASTBuilderUtil.createVariableDefStmt(attachment.pos, target).var = annotationVar;
    }
    // create: annotationMapEntryVar["name$index"] = annotationVar;
    BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(target.pos, target);
    if (annotationVar != null) {
        assignmentStmt.expr = ASTBuilderUtil.createVariableRef(target.pos, annotationVar.symbol);
    } else {
        assignmentStmt.expr = ASTBuilderUtil.createLiteral(target.pos, symTable.nullType, null);
    }
    BLangIndexBasedAccess indexAccessNode = (BLangIndexBasedAccess) TreeBuilder.createIndexBasedAccessNode();
    indexAccessNode.pos = target.pos;
    indexAccessNode.indexExpr = ASTBuilderUtil.createLiteral(target.pos, symTable.stringType, attachment.annotationSymbol.bvmAlias() + "$" + index);
    indexAccessNode.expr = ASTBuilderUtil.createVariableRef(target.pos, annotationMapEntryVar.symbol);
    indexAccessNode.type = annotationMapEntryVar.symbol.type;
    assignmentStmt.varRefs.add(indexAccessNode);
}
Also used : BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Aggregations

BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)77 ArrayList (java.util.ArrayList)21 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)20 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)18 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)18 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)16 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)15 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)14 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)11 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)10 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)10 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)10 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)9 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)8 Whitespace (org.ballerinalang.model.Whitespace)7 BLangObject (org.wso2.ballerinalang.compiler.tree.BLangObject)7 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)7 Name (org.wso2.ballerinalang.compiler.util.Name)7 HashMap (java.util.HashMap)6 BLangExpressionStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)6