Search in sources :

Example 16 with BLangVariable

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

the class ASTBuilderUtil method generateArgExprs.

static List<BLangExpression> generateArgExprs(DiagnosticPos pos, List<BLangVariable> args, List<BVarSymbol> formalParams, SymbolResolver symResolver) {
    List<BLangExpression> argsExpr = new ArrayList<>();
    final List<BLangSimpleVarRef> variableRefList = createVariableRefList(pos, args);
    for (int i = 0; i < variableRefList.size(); i++) {
        BLangSimpleVarRef varRef = variableRefList.get(i);
        BType target = formalParams.get(i).type;
        BType source = varRef.symbol.type;
        if (source != target) {
            argsExpr.add(generateConversionExpr(varRef, target, symResolver));
            continue;
        }
        argsExpr.add(varRef);
    }
    return argsExpr;
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) ArrayList(java.util.ArrayList) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 17 with BLangVariable

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

the class Desugar method visit.

@Override
public void visit(BLangReturn returnNode) {
    if (returnNode.namedReturnVariables != null) {
        // Handled named returns.
        for (BLangVariable variable : returnNode.namedReturnVariables) {
            BLangSimpleVarRef varRef = (BLangSimpleVarRef) TreeBuilder.createSimpleVariableReferenceNode();
            varRef.variableName = variable.name;
            varRef.symbol = variable.symbol;
            varRef.type = variable.type;
            varRef.pos = returnNode.pos;
            returnNode.exprs.add(varRef);
        }
    }
    returnNode.exprs = rewriteExprs(returnNode.exprs);
    result = returnNode;
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 18 with BLangVariable

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

the class Desugar method visit.

@Override
public void visit(BLangTupleDestructure stmt) {
    // var (a, b) = (tuple)
    // 
    // desugar once
    // any[] x = (tuple);
    // a = x[0];
    final BLangBlockStmt blockStmt = ASTBuilderUtil.createBlockStmt(stmt.pos);
    BType runTimeType = new BArrayType(symTable.anyType);
    final BLangVariable tuple = ASTBuilderUtil.createVariable(stmt.pos, "", runTimeType, null, new BVarSymbol(0, names.fromString("tuple"), this.env.scope.owner.pkgID, runTimeType, this.env.scope.owner));
    tuple.expr = stmt.expr;
    final BLangVariableDef variableDef = ASTBuilderUtil.createVariableDefStmt(stmt.pos, blockStmt);
    variableDef.var = tuple;
    for (int index = 0; index < stmt.varRefs.size(); index++) {
        BLangExpression varRef = stmt.varRefs.get(index);
        BLangLiteral indexExpr = ASTBuilderUtil.createLiteral(stmt.pos, symTable.intType, (long) index);
        BLangIndexBasedAccess arrayAccess = ASTBuilderUtil.createIndexBasesAccessExpr(stmt.pos, symTable.anyType, tuple.symbol, indexExpr);
        final BLangExpression assignmentExpr;
        if (types.isValueType(varRef.type)) {
            BLangTypeConversionExpr castExpr = (BLangTypeConversionExpr) TreeBuilder.createTypeConversionNode();
            castExpr.expr = arrayAccess;
            castExpr.conversionSymbol = Symbols.createUnboxValueTypeOpSymbol(symTable.anyType, varRef.type);
            castExpr.type = varRef.type;
            assignmentExpr = castExpr;
        } else {
            assignmentExpr = arrayAccess;
        }
        final BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(stmt.pos, blockStmt);
        assignmentStmt.declaredWithVar = stmt.declaredWithVar;
        assignmentStmt.varRefs = Lists.of(varRef);
        assignmentStmt.expr = assignmentExpr;
    }
    result = rewrite(blockStmt, env);
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) BLangTypeConversionExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) 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) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 19 with BLangVariable

use of org.wso2.ballerinalang.compiler.tree.BLangVariable 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 20 with BLangVariable

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

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