Search in sources :

Example 71 with SymbolEnv

use of org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv in project ballerina by ballerina-lang.

the class Desugar method visit.

// visitors
@Override
public void visit(BLangPackage pkgNode) {
    if (pkgNode.completedPhases.contains(CompilerPhase.DESUGAR)) {
        result = pkgNode;
        return;
    }
    SymbolEnv env = this.symTable.pkgEnvMap.get(pkgNode.symbol);
    // Adding object functions to package level.
    pkgNode.objects.forEach(o -> {
        o.functions.forEach(f -> {
            pkgNode.functions.add(f);
            pkgNode.topLevelNodes.add(f);
        });
    });
    // Rewriting Object to struct
    pkgNode.objects.forEach(o -> pkgNode.structs.add(rewriteObjectToStruct(o, env)));
    pkgNode.structs = rewrite(pkgNode.structs, env);
    // Adding struct init functions to package level.
    pkgNode.structs.forEach(struct -> {
        pkgNode.functions.add(struct.initFunction);
        pkgNode.topLevelNodes.add(struct.initFunction);
    });
    pkgNode.imports = rewrite(pkgNode.imports, env);
    pkgNode.xmlnsList = rewrite(pkgNode.xmlnsList, env);
    pkgNode.globalVars = rewrite(pkgNode.globalVars, env);
    pkgNode.globalEndpoints = rewrite(pkgNode.globalEndpoints, env);
    pkgNode.globalEndpoints.forEach(endpoint -> endpointDesugar.defineGlobalEndpoint(endpoint, env));
    annotationDesugar.rewritePackageAnnotations(pkgNode);
    endpointDesugar.rewriteAllEndpointsInPkg(pkgNode, env);
    endpointDesugar.rewriteServiceBoundToEndpointInPkg(pkgNode, env);
    pkgNode.transformers = rewrite(pkgNode.transformers, env);
    pkgNode.functions = rewrite(pkgNode.functions, env);
    pkgNode.connectors = rewrite(pkgNode.connectors, env);
    pkgNode.services = rewrite(pkgNode.services, env);
    pkgNode.initFunction = rewrite(pkgNode.initFunction, env);
    pkgNode.startFunction = rewrite(pkgNode.startFunction, env);
    pkgNode.stopFunction = rewrite(pkgNode.stopFunction, env);
    pkgNode.completedPhases.add(CompilerPhase.DESUGAR);
    result = pkgNode;
}
Also used : SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Example 72 with SymbolEnv

use of org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv in project ballerina by ballerina-lang.

the class EndpointDesugar method defineGlobalEndpoint.

void defineGlobalEndpoint(BLangEndpoint ep, SymbolEnv env) {
    final BLangVariable epVariable = ASTBuilderUtil.createVariable(ep.pos, ep.name.value, ep.symbol.type);
    epVariable.symbol = (BVarSymbol) symResolver.lookupMemberSymbol(ep.pos, env.enclPkg.symbol.scope, env, names.fromIdNode(ep.name), SymTag.VARIABLE);
    env.enclPkg.globalVars.add(epVariable);
}
Also used : BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 73 with SymbolEnv

use of org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv in project ballerina by ballerina-lang.

the class EndpointDesugar method generateEndpointInit.

private BLangBlockStmt generateEndpointInit(BLangEndpoint endpoint, SymbolEnv env, BSymbol encSymbol) {
    final String epName = endpoint.name.value;
    final DiagnosticPos pos = endpoint.pos;
    BLangBlockStmt temp = new BLangBlockStmt();
    final BLangVariable epVariable = ASTBuilderUtil.createVariable(pos, epName, endpoint.symbol.type);
    epVariable.symbol = (BVarSymbol) symResolver.lookupMemberSymbol(pos, encSymbol.scope, env, names.fromString(epName), SymTag.VARIABLE);
    final BLangExpression newExpr;
    if (endpoint.configurationExpr != null && endpoint.configurationExpr.getKind() != NodeKind.RECORD_LITERAL_EXPR) {
        // Handle Endpoint Assignment.
        newExpr = endpoint.configurationExpr;
    } else if (endpoint.configurationExpr != null && endpoint.configurationExpr.getKind() == NodeKind.RECORD_LITERAL_EXPR) {
        // Handle Endpoint initialization.
        newExpr = ASTBuilderUtil.createEmptyRecordLiteral(pos, endpoint.symbol.type);
    } else {
        newExpr = null;
    }
    // EPType ep_name = {};
    if (env.enclInvokable != null) {
        // In callable unit, endpoint is same scope variable.
        final BLangVariableDef epNewStmt = ASTBuilderUtil.createVariableDefStmt(pos, temp);
        epNewStmt.var = epVariable;
        epNewStmt.var.expr = newExpr;
    } else {
        // This is an init function. ep variable is defined in outside.
        if (env.enclService != null) {
            // Add to endpoint variable to relevant location
            final BLangVariableDef epVarDef = ASTBuilderUtil.createVariableDef(pos);
            epVarDef.var = epVariable;
            env.enclService.vars.add(epVarDef);
        }
        final BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(pos, temp);
        assignmentStmt.varRefs.add(ASTBuilderUtil.createVariableRef(pos, epVariable.symbol));
        assignmentStmt.expr = newExpr;
    }
    return temp;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 74 with SymbolEnv

use of org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv in project ballerina by ballerina-lang.

the class EndpointDesugar method generateEndpointStartOrStop.

private BLangBlockStmt generateEndpointStartOrStop(BLangEndpoint endpoint, BInvokableSymbol funSymbol, SymbolEnv env, BSymbol encSymbol) {
    BLangBlockStmt temp = new BLangBlockStmt();
    if (funSymbol == null || endpoint.configurationExpr == null || endpoint.configurationExpr.getKind() != NodeKind.RECORD_LITERAL_EXPR) {
        // 2: If endpoint in initialized
        return temp;
    }
    final DiagnosticPos pos = endpoint.pos;
    final String epName = endpoint.name.value;
    final BLangVariable epVariable = ASTBuilderUtil.createVariable(pos, epName, endpoint.symbol.type);
    final Name name = names.fromIdNode(endpoint.name);
    epVariable.symbol = (BVarSymbol) symResolver.lookupMemberSymbol(pos, encSymbol.scope, env, name, SymTag.VARIABLE);
    List<BLangVariable> args = new ArrayList<>();
    if (funSymbol.params.size() == 1) {
        // Endpoint is already desugared. Fix this correctly.
        args.add(0, epVariable);
    }
    final BLangExpressionStmt expressionStmt = ASTBuilderUtil.createExpressionStmt(pos, temp);
    final BLangInvocation iExpr = ASTBuilderUtil.createInvocationExpr(pos, funSymbol, args, symResolver);
    if (funSymbol.params.size() != 1) {
        iExpr.expr = ASTBuilderUtil.createVariableRef(epVariable.pos, epVariable.symbol);
    }
    expressionStmt.expr = iExpr;
    return temp;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) ArrayList(java.util.ArrayList) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BLangExpressionStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 75 with SymbolEnv

use of org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv in project ballerina by ballerina-lang.

the class EndpointDesugar method rewriteService.

private void rewriteService(BLangService service, SymbolEnv pkgEnv) {
    final BServiceSymbol serviceSymbol = (BServiceSymbol) service.symbol;
    if (serviceSymbol.boundEndpoints.isEmpty()) {
        return;
    }
    final BSymbol enclosingSymbol = pkgEnv.enclPkg.symbol;
    final BSymbol varSymbol = pkgEnv.enclPkg.startFunction.symbol;
    final BLangBlockStmt startBlock = pkgEnv.enclPkg.startFunction.body;
    serviceSymbol.boundEndpoints.forEach(endpointVarSymbol -> {
        final BLangBlockStmt generateCode = generateServiceRegistered(endpointVarSymbol, service, pkgEnv, enclosingSymbol, varSymbol);
        ASTBuilderUtil.prependStatements(generateCode, startBlock);
    });
}
Also used : BServiceSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BServiceSymbol) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)

Aggregations

SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)125 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)47 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)30 Name (org.wso2.ballerinalang.compiler.util.Name)26 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)25 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)25 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)24 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)22 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)20 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)20 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)20 BLangExpressionStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)19 Scope (org.wso2.ballerinalang.compiler.semantics.model.Scope)18 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)18 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)18 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)17 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)17 BLangAnnotation (org.wso2.ballerinalang.compiler.tree.BLangAnnotation)17 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)17 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)17