Search in sources :

Example 1 with Desugar

use of org.wso2.ballerinalang.compiler.desugar.Desugar 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 2 with Desugar

use of org.wso2.ballerinalang.compiler.desugar.Desugar 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 3 with Desugar

use of org.wso2.ballerinalang.compiler.desugar.Desugar in project ballerina by ballerina-lang.

the class IterableCodeDesugar method desugar.

public void desugar(IterableContext ctx) {
    // Gather required data for code generation.
    processIterableContext(ctx);
    // Generate Iterable Iteration.
    generateIteratorFunction(ctx);
    // Create invocation expression to invoke iterable operation.
    final BLangInvocation iExpr = ASTBuilderUtil.createInvocationExpr(ctx.collectionExpr.pos, ctx.iteratorFuncSymbol, Collections.emptyList(), symResolver);
    iExpr.requiredArgs.add(ctx.collectionExpr);
    if (ctx.getLastOperation().expectedType == symTable.noType || ctx.getLastOperation().expectedType == symTable.voidType) {
        ctx.iteratorCaller = iExpr;
    } else {
        ctx.iteratorCaller = ASTBuilderUtil.wrapToConversionExpr(ctx.getLastOperation().expectedType, iExpr, symTable, types);
    }
}
Also used : BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)

Example 4 with Desugar

use of org.wso2.ballerinalang.compiler.desugar.Desugar in project ballerina by ballerina-lang.

the class CompilerDriver method compile.

// Private methods
private BLangPackage compile(BLangPackage bLangPackage) {
    BLangPackage pkgNode = bLangPackage;
    // "ballerina/built-in" packages is only the pre-known package by the Ballerina compiler. So load it first.
    BLangPackage builtInPackage = loadBuiltInPackage();
    if (this.stopCompilation(pkgNode, CompilerPhase.DEFINE)) {
        return pkgNode;
    }
    pkgNode = define(pkgNode);
    if (this.stopCompilation(pkgNode, CompilerPhase.TYPE_CHECK)) {
        return pkgNode;
    }
    pkgNode = typeCheck(pkgNode);
    if (this.stopCompilation(pkgNode, CompilerPhase.CODE_ANALYZE)) {
        return pkgNode;
    }
    pkgNode = codeAnalyze(pkgNode);
    if (this.stopCompilation(pkgNode, CompilerPhase.TAINT_ANALYZE)) {
        return pkgNode;
    }
    pkgNode = taintAnalyze(pkgNode);
    if (this.stopCompilation(pkgNode, CompilerPhase.COMPILER_PLUGIN)) {
        return pkgNode;
    }
    pkgNode = annotationProcess(pkgNode);
    if (this.stopCompilation(pkgNode, CompilerPhase.DESUGAR)) {
        return pkgNode;
    }
    // TODO : Improve this.
    desugar(builtInPackage);
    return desugar(pkgNode);
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage)

Example 5 with Desugar

use of org.wso2.ballerinalang.compiler.desugar.Desugar in project ballerina by ballerina-lang.

the class TypeChecker method checkStructLiteralKeyExpr.

private BType checkStructLiteralKeyExpr(BLangRecordKey key, BType recordType, RecordKind recKind) {
    Name fieldName;
    BLangExpression keyExpr = key.expr;
    if (keyExpr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
        BLangSimpleVarRef varRef = (BLangSimpleVarRef) keyExpr;
        fieldName = names.fromIdNode(varRef.variableName);
    } else {
        // keys of the struct literal can only be a varRef (identifier)
        dlog.error(keyExpr.pos, DiagnosticCode.INVALID_STRUCT_LITERAL_KEY);
        return symTable.errType;
    }
    // Check weather the struct field exists
    BSymbol fieldSymbol = symResolver.resolveStructField(keyExpr.pos, this.env, fieldName, recordType.tsymbol);
    if (fieldSymbol == symTable.notFoundSymbol) {
        dlog.error(keyExpr.pos, DiagnosticCode.UNDEFINED_STRUCT_FIELD, fieldName, recordType.tsymbol);
        return symTable.errType;
    }
    // Setting the struct field symbol for future use in Desugar and code generator.
    key.fieldSymbol = (BVarSymbol) fieldSymbol;
    return fieldSymbol.type;
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name)

Aggregations

BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)3 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)3 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)3 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)2 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)2 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)2 BLangMatch (org.wso2.ballerinalang.compiler.tree.statements.BLangMatch)2 BLangMatchStmtPatternClause (org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause)2 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)2 LSPackageLoader (org.ballerinalang.langserver.LSPackageLoader)1 PackageLoader (org.wso2.ballerinalang.compiler.PackageLoader)1 Desugar (org.wso2.ballerinalang.compiler.desugar.Desugar)1 CodeAnalyzer (org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer)1 SemanticAnalyzer (org.wso2.ballerinalang.compiler.semantics.analyzer.SemanticAnalyzer)1 SymbolTable (org.wso2.ballerinalang.compiler.semantics.model.SymbolTable)1 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)1 BArrayType (org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType)1 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)1 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)1 BLangIndexBasedAccess (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess)1