Search in sources :

Example 1 with BLangReturn

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

the class SymbolEnter method addInitReturnStatement.

private void addInitReturnStatement(BLangBlockStmt bLangBlockStmt) {
    // Add return statement to the init function
    BLangReturn returnStmt = (BLangReturn) TreeBuilder.createReturnNode();
    returnStmt.pos = bLangBlockStmt.pos;
    bLangBlockStmt.addStatement(returnStmt);
}
Also used : BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)

Example 2 with BLangReturn

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

the class TaintAnalyzer method visit.

public void visit(BLangReturn returnNode) {
    List<Boolean> returnTaintedStatus = new ArrayList<>();
    if (returnNode.namedReturnVariables == null) {
        // If named returns are not used, evaluate each expression to identify the tainted status.
        for (BLangExpression expr : returnNode.exprs) {
            expr.accept(this);
            returnTaintedStatus.addAll(taintedStatusList);
        }
    } else {
        // If named returns are used, report back the tainted status of each variable.
        for (BLangVariable var : returnNode.namedReturnVariables) {
            returnTaintedStatus.add(var.symbol.tainted);
        }
    }
    if (returnTaintedStatusList == null) {
        returnTaintedStatusList = returnTaintedStatus;
    } else {
        // collective tainted status of returns.
        for (int i = 0; i < returnTaintedStatusList.size(); i++) {
            if (returnTaintedStatus.size() > i && returnTaintedStatus.get(i)) {
                returnTaintedStatusList.set(i, true);
            }
        }
    }
    taintedStatusList = returnTaintedStatusList;
}
Also used : ArrayList(java.util.ArrayList) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 3 with BLangReturn

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

the class SemanticAnalyzer method checkReturnValueCounts.

private boolean checkReturnValueCounts(BLangReturn returnNode) {
    boolean success = false;
    int expRetCount = this.env.enclInvokable.getReturnParameters().size();
    int actualRetCount = returnNode.exprs.size();
    if (expRetCount > 1 && actualRetCount <= 1) {
        this.dlog.error(returnNode.pos, DiagnosticCode.MULTI_VALUE_RETURN_EXPECTED);
    } else if (expRetCount == 1 && actualRetCount > 1) {
        this.dlog.error(returnNode.pos, DiagnosticCode.SINGLE_VALUE_RETURN_EXPECTED);
    } else if (expRetCount == 0 && actualRetCount >= 1) {
        this.dlog.error(returnNode.pos, DiagnosticCode.RETURN_VALUE_NOT_EXPECTED);
    } else if (expRetCount > actualRetCount) {
        this.dlog.error(returnNode.pos, DiagnosticCode.NOT_ENOUGH_RETURN_VALUES);
    } else if (expRetCount < actualRetCount) {
        this.dlog.error(returnNode.pos, DiagnosticCode.TOO_MANY_RETURN_VALUES);
    } else {
        success = true;
    }
    return success;
}
Also used : BLangAnnotationAttachmentPoint(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 4 with BLangReturn

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

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

the class Desugar method addReturnIfNotPresent.

private void addReturnIfNotPresent(BLangInvokableNode invokableNode) {
    if (Symbols.isNative(invokableNode.symbol)) {
        return;
    }
    // This will only check whether last statement is a return and just add a return statement.
    // This won't analyse if else blocks etc to see whether return statements are present
    BLangBlockStmt blockStmt = invokableNode.body;
    if (invokableNode.workers.size() == 0 && invokableNode.retParams.isEmpty() && (blockStmt.stmts.size() < 1 || blockStmt.stmts.get(blockStmt.stmts.size() - 1).getKind() != NodeKind.RETURN)) {
        BLangReturn returnStmt = (BLangReturn) TreeBuilder.createReturnNode();
        DiagnosticPos invPos = invokableNode.pos;
        DiagnosticPos pos = new DiagnosticPos(invPos.src, invPos.eLine, invPos.eLine, invPos.sCol, invPos.sCol);
        returnStmt.pos = pos;
        blockStmt.addStatement(returnStmt);
    }
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)

Aggregations

BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)9 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)6 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)5 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)4 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)3 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)3 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)3 BLangForeach (org.wso2.ballerinalang.compiler.tree.statements.BLangForeach)3 ArrayList (java.util.ArrayList)2 Operation (org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation)2 BLangBinaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)2 BLangBracedOrTupleExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBracedOrTupleExpr)2 BLangIf (org.wso2.ballerinalang.compiler.tree.statements.BLangIf)2 BLangTupleDestructure (org.wso2.ballerinalang.compiler.tree.statements.BLangTupleDestructure)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1