Search in sources :

Example 16 with BLangVariableDef

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

the class CodeAnalyzer method visit.

public void visit(BLangTransformer transformerNode) {
    List<BVarSymbol> inputs = new ArrayList<>();
    inputs.add(transformerNode.source.symbol);
    transformerNode.requiredParams.forEach(param -> inputs.add(param.symbol));
    List<BVarSymbol> outputs = new ArrayList<>();
    transformerNode.retParams.forEach(param -> outputs.add(param.symbol));
    for (BLangStatement stmt : transformerNode.body.stmts) {
        switch(stmt.getKind()) {
            case VARIABLE_DEF:
                BLangVariableDef variableDefStmt = (BLangVariableDef) stmt;
                variableDefStmt.var.expr.accept(new TransformerVarRefValidator(outputs, DiagnosticCode.TRANSFORMER_INVALID_OUTPUT_USAGE));
                inputs.add(variableDefStmt.var.symbol);
                break;
            case ASSIGNMENT:
                BLangAssignment assignStmt = (BLangAssignment) stmt;
                assignStmt.varRefs.forEach(varRef -> {
                    varRef.accept(new TransformerVarRefValidator(inputs, DiagnosticCode.TRANSFORMER_INVALID_INPUT_UPDATE));
                    // If the stmt is declared using var, all the variable refs on lhs should be treated as inputs
                    if (assignStmt.declaredWithVar && varRef.getKind() == NodeKind.SIMPLE_VARIABLE_REF && !inputs.contains(((BLangSimpleVarRef) varRef).symbol)) {
                        inputs.add(((BLangSimpleVarRef) varRef).symbol);
                    }
                });
                assignStmt.expr.accept(new TransformerVarRefValidator(outputs, DiagnosticCode.TRANSFORMER_INVALID_OUTPUT_USAGE));
                break;
            case EXPRESSION_STATEMENT:
                // Here we have assumed that the invocation expression is the only expression-statement available.
                // TODO: support other types, once they are implemented.
                dlog.error(stmt.pos, DiagnosticCode.INVALID_STATEMENT_IN_TRANSFORMER, "invocation");
                break;
            default:
                dlog.error(stmt.pos, DiagnosticCode.INVALID_STATEMENT_IN_TRANSFORMER, stmt.getKind().name().toLowerCase().replace('_', ' '));
                break;
        }
    }
}
Also used : BLangStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangStatement) ArrayList(java.util.ArrayList) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)

Example 17 with BLangVariableDef

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

the class ASTBuilderUtil method createVariableDefStmt.

static BLangVariableDef createVariableDefStmt(DiagnosticPos pos, BLangBlockStmt target) {
    final BLangVariableDef variableDef = createVariableDef(pos);
    target.addStatement(variableDef);
    return variableDef;
}
Also used : BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)

Example 18 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(BLangVariableDef varDefNode) {
    if (varDefNode.var.expr instanceof BLangRecordLiteral && ((BLangRecordLiteral) varDefNode.var.expr).type.tag == TypeTags.STREAM) {
        ((BLangRecordLiteral) varDefNode.var.expr).name = varDefNode.var.name;
    }
    varDefNode.var = rewrite(varDefNode.var, env);
    BLangVariable varNode = varDefNode.var;
    // Generate default init expression, if rhs expr is null
    if (varNode.expr == null) {
        varNode.expr = getInitExpr(varNode.type);
    }
    if (!varNode.safeAssignment) {
        result = varDefNode;
        return;
    }
    // Desugar the =? operator with the match statement
    // 
    // e.g.
    // var f =? openFile("/tmp/foo.txt"); // openFile: () -> (File | error)
    // 
    // {
    // File f;
    // match openFile("/tmp/foo.txt") {
    // File _$_f1 => f = _$_f1;
    // error e => throw e | return e
    // }
    // }
    // Create the pattern to match the success case
    BLangMatchStmtPatternClause patternSuccessCase = getSafeAssignSuccessPattern(varNode.pos, varNode.symbol.type, true, varNode.symbol, null);
    BLangMatchStmtPatternClause patternErrorCase = getSafeAssignErrorPattern(varNode.pos, varNode.symbol.owner);
    // Create the match statement
    BLangMatch matchStmt = ASTBuilderUtil.createMatchStatement(varNode.expr.pos, varNode.expr, new ArrayList<BLangMatchStmtPatternClause>() {

        {
            add(patternSuccessCase);
            add(patternErrorCase);
        }
    });
    // var f =? foo() -> var f;
    varNode.expr = null;
    varNode.safeAssignment = false;
    BLangBlockStmt safeAssignmentBlock = ASTBuilderUtil.createBlockStmt(varDefNode.pos, new ArrayList<BLangStatement>() {

        {
            add(varDefNode);
            add(matchStmt);
        }
    });
    result = rewrite(safeAssignmentBlock, this.env);
}
Also used : BLangStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangStatement) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangMatch(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch) BLangMatchStmtPatternClause(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 19 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(BLangMatch matchStmt) {
    // Here we generate an if-else statement for the match statement
    // Here is an example match statement
    // 
    // match expr {
    // int k => io:println("int value: " + k);
    // string s => io:println("string value: " + s);
    // json j => io:println("json value: " + s);
    // 
    // }
    // 
    // Here is how we convert the match statement to an if-else statement. The last clause should always be the
    // else clause
    // 
    // string | int | json | any _$$_matchexpr = expr;
    // if ( _$$_matchexpr isassignable int ){
    // int k = (int) _$$_matchexpr; // unbox
    // io:println("int value: " + k);
    // 
    // } else if (_$$_matchexpr isassignable string ) {
    // string s = (string) _$$_matchexpr; // unbox
    // io:println("string value: " + s);
    // 
    // } else if ( _$$_matchexpr isassignable float ||    // should we consider json[] as well
    // _$$_matchexpr isassignable boolean ||
    // _$$_matchexpr isassignable json) {
    // 
    // } else {
    // // handle the last pattern
    // any case..
    // }
    // 
    // First create a block statement to hold generated statements
    BLangBlockStmt matchBlockStmt = (BLangBlockStmt) TreeBuilder.createBlockNode();
    matchBlockStmt.pos = matchStmt.pos;
    // Create a variable definition to store the value of the match expression
    String matchExprVarName = GEN_VAR_PREFIX.value;
    BLangVariable matchExprVar = ASTBuilderUtil.createVariable(matchStmt.expr.pos, matchExprVarName, matchStmt.expr.type, matchStmt.expr, new BVarSymbol(0, names.fromString(matchExprVarName), this.env.scope.owner.pkgID, matchStmt.expr.type, this.env.scope.owner));
    // Now create a variable definition node
    BLangVariableDef matchExprVarDef = ASTBuilderUtil.createVariableDef(matchBlockStmt.pos, matchExprVar);
    // Add the var def statement to the block statement
    // string | int _$$_matchexpr = expr;
    matchBlockStmt.stmts.add(matchExprVarDef);
    // Create if/else blocks with typeof binary expressions for each pattern
    matchBlockStmt.stmts.add(generateIfElseStmt(matchStmt, matchExprVar));
    rewrite(matchBlockStmt, this.env);
    result = matchBlockStmt;
}
Also used : BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)

Example 20 with BLangVariableDef

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

the class EndpointDesugar method generateServiceRegistered.

private BLangBlockStmt generateServiceRegistered(BEndpointVarSymbol endpoint, BLangService service, SymbolEnv env, BSymbol encSymbol, BSymbol varEncSymbol) {
    final DiagnosticPos pos = service.pos;
    final String epName = endpoint.name.value;
    BLangBlockStmt temp = new BLangBlockStmt();
    final BLangVariable epVariable = ASTBuilderUtil.createVariable(pos, epName, endpoint.type);
    final Name name = endpoint.name;
    epVariable.symbol = (BVarSymbol) symResolver.lookupMemberSymbol(pos, encSymbol.scope, env, name, SymTag.VARIABLE);
    final BLangVariableDef serviceTypeDef = ASTBuilderUtil.createVariableDefStmt(pos, temp);
    serviceTypeDef.var = ASTBuilderUtil.createVariable(pos, service.name + "type", symTable.typeDesc);
    ASTBuilderUtil.defineVariable(serviceTypeDef.var, varEncSymbol, names);
    final BLangUnaryExpr typeOfExpr = ASTBuilderUtil.createUnaryExpr(pos);
    serviceTypeDef.var.expr = typeOfExpr;
    typeOfExpr.operator = OperatorKind.TYPEOF;
    typeOfExpr.expr = getTypeAccessExpression(pos, service.symbol.type);
    typeOfExpr.type = symTable.typeDesc;
    List<BLangVariable> args = Lists.of(serviceTypeDef.var);
    if (endpoint.registerFunction != null && endpoint.registerFunction.params.size() == 2) {
        // Endpoint is already desugared. Fix this correctly.
        args.add(0, epVariable);
    }
    final BLangExpressionStmt expressionStmt = ASTBuilderUtil.createExpressionStmt(pos, temp);
    final BLangInvocation iExpr = ASTBuilderUtil.createInvocationExpr(pos, endpoint.registerFunction, args, symResolver);
    if (endpoint.registerFunction != null && endpoint.registerFunction.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) BLangUnaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr) 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)

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