Search in sources :

Example 16 with BLangBlockStmt

use of org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt 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 17 with BLangBlockStmt

use of org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt 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 18 with BLangBlockStmt

use of org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt 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)

Example 19 with BLangBlockStmt

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

the class EndpointDesugar method generateEndpointInitFunctionCall.

private BLangBlockStmt generateEndpointInitFunctionCall(BLangEndpoint endpoint, SymbolEnv env, BSymbol encSymbol, BSymbol varEncSymbol) {
    BLangBlockStmt temp = new BLangBlockStmt();
    if (endpoint.configurationExpr == null || endpoint.configurationExpr.getKind() != NodeKind.RECORD_LITERAL_EXPR) {
        return temp;
    }
    final String epName = endpoint.name.value;
    final DiagnosticPos pos = endpoint.pos;
    final BLangVariable epVariable = ASTBuilderUtil.createVariable(pos, epName, endpoint.symbol.type);
    epVariable.symbol = (BVarSymbol) symResolver.lookupMemberSymbol(pos, encSymbol.scope, env, names.fromString(epName), SymTag.VARIABLE);
    // EPConfigType ep_nameConf = { ep-config-expr };
    final BLangVariableDef epConfigNewStmt = ASTBuilderUtil.createVariableDefStmt(pos, temp);
    epConfigNewStmt.var = ASTBuilderUtil.createVariable(pos, epName + "Conf", endpoint.configurationExpr.type);
    epConfigNewStmt.var.expr = endpoint.configurationExpr;
    ASTBuilderUtil.defineVariable(epConfigNewStmt.var, varEncSymbol, names);
    List<BLangVariable> args = Lists.of(epConfigNewStmt.var);
    if (endpoint.symbol.initFunction != null && endpoint.symbol.initFunction.params.size() == 2) {
        // Endpoint is already desugared. Fix this correctly.
        args.add(0, epVariable);
    }
    // epName.init(ep_nameConf);
    final BLangExpressionStmt expressionStmt = ASTBuilderUtil.createExpressionStmt(pos, temp);
    final BLangInvocation iExpr = ASTBuilderUtil.createInvocationExpr(pos, endpoint.symbol.initFunction, args, symResolver);
    if (endpoint.symbol.initFunction != null && endpoint.symbol.initFunction.params.size() != 2) {
        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) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BLangExpressionStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 20 with BLangBlockStmt

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

the class IterableCodeDesugar method generateTableAggregator.

private void generateTableAggregator(BLangBlockStmt blockStmt, IterableContext ctx) {
    final DiagnosticPos pos = blockStmt.pos;
    List<BLangVariable> variables = new ArrayList<>(1);
    variables.add(ctx.resultVar);
    variables.add(ctx.iteratorResultVariables.get(0));
    BInvokableSymbol addSymbol = (BInvokableSymbol) symTable.rootScope.lookup(names.fromString(TABLE_ADD_FUNCTION)).symbol;
    BLangInvocation addFunctionInvocation = ASTBuilderUtil.createInvocationExpr(pos, addSymbol, variables, symResolver);
    BLangExpressionStmt expressionStmt = ASTBuilderUtil.createExpressionStmt(pos, blockStmt);
    expressionStmt.expr = addFunctionInvocation;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) ArrayList(java.util.ArrayList) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BLangExpressionStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Aggregations

BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)33 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)22 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)19 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)16 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)12 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)9 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)8 BLangIf (org.wso2.ballerinalang.compiler.tree.statements.BLangIf)8 BLangExpressionStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)7 BLangBinaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)6 BLangIndexBasedAccess (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess)6 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)6 BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)6 Name (org.wso2.ballerinalang.compiler.util.Name)6 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)5 BLangMatchStmtPatternClause (org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause)5 ArrayList (java.util.ArrayList)4 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)4 BLangForeach (org.wso2.ballerinalang.compiler.tree.statements.BLangForeach)4 Whitespace (org.ballerinalang.model.Whitespace)3