Search in sources :

Example 6 with BLangVariableDef

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

the class Desugar method getMatchPatternBody.

private BLangBlockStmt getMatchPatternBody(BLangMatchStmtPatternClause patternClause, BLangVariable matchExprVar) {
    // Add the variable definition to the body of the pattern clause
    if (patternClause.variable.name.value.equals(Names.IGNORE.value)) {
        return patternClause.body;
    }
    // create TypeName i = <TypeName> _$$_
    // Create a variable reference for _$$_
    BLangSimpleVarRef matchExprVarRef = ASTBuilderUtil.createVariableRef(patternClause.pos, matchExprVar.symbol);
    BLangExpression patternVarExpr = addConversionExprIfRequired(matchExprVarRef, patternClause);
    // Add the variable def statement
    BLangVariable patternVar = ASTBuilderUtil.createVariable(patternClause.pos, "", patternClause.variable.type, patternVarExpr, patternClause.variable.symbol);
    BLangVariableDef patternVarDef = ASTBuilderUtil.createVariableDef(patternVar.pos, patternVar);
    patternClause.body.stmts.add(0, patternVarDef);
    return patternClause.body;
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 7 with BLangVariableDef

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

the class Desugar method visit.

@Override
public void visit(BLangAssignment assignNode) {
    if (assignNode.expr.type.tag == TypeTags.STREAM && assignNode.varRefs.get(0) instanceof BLangSimpleVarRef) {
        ((BLangRecordLiteral) assignNode.expr).name = ((BLangSimpleVarRef) assignNode.varRefs.get(0)).variableName;
    }
    assignNode.varRefs = rewriteExprs(assignNode.varRefs);
    assignNode.expr = rewriteExpr(assignNode.expr);
    result = assignNode;
    if (!assignNode.safeAssignment) {
        return;
    }
    // Desugar the =? operator with the match statement
    // 
    // e.g.
    // File f;
    // .....
    // f =? openFile("/tmp/foo.txt"); // openFile: () -> (File | error)
    // 
    // {
    // match openFile("/tmp/foo.txt") {
    // File _$_f1 => f = _$_f1;
    // error e => throw e | return e
    // }
    // }
    BLangBlockStmt safeAssignmentBlock = ASTBuilderUtil.createBlockStmt(assignNode.pos, new ArrayList<>());
    BLangExpression lhsExpr = assignNode.varRefs.get(0);
    BLangMatchStmtPatternClause patternSuccessCase;
    if (assignNode.declaredWithVar) {
        BVarSymbol varSymbol = ((BLangSimpleVarRef) lhsExpr).symbol;
        BLangVariable variable = ASTBuilderUtil.createVariable(assignNode.pos, "", lhsExpr.type, null, varSymbol);
        BLangVariableDef variableDef = ASTBuilderUtil.createVariableDef(assignNode.pos, variable);
        safeAssignmentBlock.stmts.add(variableDef);
        patternSuccessCase = getSafeAssignSuccessPattern(assignNode.pos, lhsExpr.type, true, varSymbol, null);
    } else {
        patternSuccessCase = getSafeAssignSuccessPattern(assignNode.pos, lhsExpr.type, false, null, lhsExpr);
    }
    // Create the pattern to match the success case
    BLangMatchStmtPatternClause patternErrorCase = getSafeAssignErrorPattern(assignNode.pos, this.env.enclInvokable.symbol);
    // Create the match statement
    BLangMatch matchStmt = ASTBuilderUtil.createMatchStatement(assignNode.pos, assignNode.expr, new ArrayList<BLangMatchStmtPatternClause>() {

        {
            add(patternSuccessCase);
            add(patternErrorCase);
        }
    });
    // var f =? foo() -> var f;
    assignNode.expr = null;
    assignNode.safeAssignment = false;
    safeAssignmentBlock.stmts.add(matchStmt);
    result = rewrite(safeAssignmentBlock, this.env);
}
Also used : BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangMatch(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch) BLangMatchStmtPatternClause(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 8 with BLangVariableDef

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

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

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

the class IterableCodeDesugar method createCounterVarDefStmt.

/**
 * Generates following.
 *
 * int count = 0;
 *
 * @param funcNode functionNode
 * @param ctx      current context
 */
private void createCounterVarDefStmt(BLangFunction funcNode, IterableContext ctx) {
    BLangBlockStmt blockStmt = funcNode.body;
    final DiagnosticPos pos = blockStmt.pos;
    ctx.countVar = ASTBuilderUtil.createVariable(pos, VAR_COUNT, symTable.intType);
    ctx.countVar.expr = ASTBuilderUtil.createLiteral(pos, symTable.intType, 0L);
    defineVariable(ctx.countVar, funcNode.symbol.pkgID, funcNode);
    final BLangVariableDef variableDefStmt = ASTBuilderUtil.createVariableDefStmt(pos, blockStmt);
    variableDefStmt.var = ctx.countVar;
}
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)

Aggregations

BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)27 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)14 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)10 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)5 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)5 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)4 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)4 CompletionItem (org.eclipse.lsp4j.CompletionItem)3 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)3 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)3 InvokableNode (org.ballerinalang.model.tree.InvokableNode)2 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)2 BLangAction (org.wso2.ballerinalang.compiler.tree.BLangAction)2 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)2 BLangResource (org.wso2.ballerinalang.compiler.tree.BLangResource)2 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)2 BLangExpressionStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)2 BLangMatch (org.wso2.ballerinalang.compiler.tree.statements.BLangMatch)2 BLangMatchStmtPatternClause (org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause)2 BLangStatement (org.wso2.ballerinalang.compiler.tree.statements.BLangStatement)2