Search in sources :

Example 1 with BLangForeach

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

the class ServiceProtoUtils method getInvocationExpression.

private static BLangInvocation getInvocationExpression(BlockNode body) {
    if (body == null) {
        return null;
    }
    for (StatementNode statementNode : body.getStatements()) {
        BLangExpression expression = null;
        // example : conn.send inside while block.
        if (statementNode instanceof BLangWhile) {
            BLangWhile langWhile = (BLangWhile) statementNode;
            BLangInvocation invocExp = getInvocationExpression(langWhile.getBody());
            if (invocExp != null) {
                return invocExp;
            }
        }
        // example : conn.send inside for block.
        if (statementNode instanceof BLangForeach) {
            BLangForeach langForeach = (BLangForeach) statementNode;
            BLangInvocation invocExp = getInvocationExpression(langForeach.getBody());
            if (invocExp != null) {
                return invocExp;
            }
        }
        // example : conn.send inside if block.
        if (statementNode instanceof BLangIf) {
            BLangIf langIf = (BLangIf) statementNode;
            BLangInvocation invocExp = getInvocationExpression(langIf.getBody());
            if (invocExp != null) {
                return invocExp;
            }
            invocExp = getInvocationExpression((BLangBlockStmt) langIf.getElseStatement());
            if (invocExp != null) {
                return invocExp;
            }
        }
        // example : _ = conn.send(msg);
        if (statementNode instanceof BLangAssignment) {
            BLangAssignment assignment = (BLangAssignment) statementNode;
            expression = assignment.getExpression();
        }
        // example : grpc:HttpConnectorError err = conn.send(msg);
        if (statementNode instanceof BLangVariableDef) {
            BLangVariableDef variableDef = (BLangVariableDef) statementNode;
            BLangVariable variable = variableDef.getVariable();
            expression = variable.getInitialExpression();
        }
        if (expression != null && expression instanceof BLangInvocation) {
            BLangInvocation invocation = (BLangInvocation) expression;
            if ("send".equals(invocation.getName().getValue())) {
                return invocation;
            }
        }
    }
    return null;
}
Also used : BLangForeach(org.wso2.ballerinalang.compiler.tree.statements.BLangForeach) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) StatementNode(org.ballerinalang.model.tree.statements.StatementNode) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf) BLangWhile(org.wso2.ballerinalang.compiler.tree.statements.BLangWhile) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 2 with BLangForeach

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

the class TaintAnalyzer method visit.

public void visit(BLangForeach foreach) {
    SymbolEnv blockEnv = SymbolEnv.createBlockEnv(foreach.body, env);
    // Propagate the tainted status of collection to foreach variables.
    foreach.collection.accept(this);
    if (getObservedTaintedStatus()) {
        foreach.varRefs.forEach(varRef -> setTaintedStatus((BLangVariableReference) varRef, getObservedTaintedStatus()));
    }
    analyzeNode(foreach.body, blockEnv);
}
Also used : BLangVariableReference(org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Example 3 with BLangForeach

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

the class SemanticAnalyzer method visit.

public void visit(BLangForeach foreach) {
    typeChecker.checkExpr(foreach.collection, env);
    foreach.varTypes = types.checkForeachTypes(foreach.collection, foreach.varRefs.size());
    SymbolEnv blockEnv = SymbolEnv.createBlockEnv(foreach.body, env);
    handleForeachVariables(foreach, foreach.varTypes, blockEnv);
    analyzeStmt(foreach.body, blockEnv);
}
Also used : SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Example 4 with BLangForeach

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

the class IterableCodeDesugar method generateSimpleIteratorBlock.

private void generateSimpleIteratorBlock(IterableContext ctx, BLangFunction funcNode) {
    final Operation firstOperation = ctx.getFirstOperation();
    final DiagnosticPos pos = firstOperation.pos;
    // return result;
    if (isReturningIteratorFunction(ctx)) {
        createCounterVarDefStmt(funcNode, ctx);
        createResultVarDefStmt(funcNode, ctx);
    }
    // Create variables required.
    final List<BLangVariable> foreachVariables = createForeachVariables(ctx, ctx.getFirstOperation().argVar, funcNode);
    ctx.iteratorResultVariables = foreachVariables;
    final BLangForeach foreachStmt = ASTBuilderUtil.createForeach(pos, funcNode.body, ASTBuilderUtil.createVariableRef(pos, ctx.collectionVar.symbol), ASTBuilderUtil.createVariableRefList(pos, foreachVariables), ctx.foreachTypes);
    if (isReturningIteratorFunction(ctx)) {
        generateAggregator(foreachStmt.body, ctx);
        generateFinalResult(funcNode.body, ctx);
    }
    final BLangReturn returnStmt = ASTBuilderUtil.createReturnStmt(firstOperation.pos, funcNode.body);
    if (isReturningIteratorFunction(ctx)) {
        returnStmt.addExpression(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
    }
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangForeach(org.wso2.ballerinalang.compiler.tree.statements.BLangForeach) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn) Operation(org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 5 with BLangForeach

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

the class SemanticAnalyzer method handleForeachVariables.

// Private methods
private void handleForeachVariables(BLangForeach foreachStmt, List<BType> varTypes, SymbolEnv env) {
    for (int i = 0; i < foreachStmt.varRefs.size(); i++) {
        BLangExpression varRef = foreachStmt.varRefs.get(i);
        // foreach variables supports only simpleVarRef expressions only.
        if (varRef.getKind() != NodeKind.SIMPLE_VARIABLE_REF) {
            dlog.error(varRef.pos, DiagnosticCode.INVALID_VARIABLE_ASSIGNMENT, varRef);
            continue;
        }
        BLangSimpleVarRef simpleVarRef = (BLangSimpleVarRef) varRef;
        simpleVarRef.lhsVar = true;
        Name varName = names.fromIdNode(simpleVarRef.variableName);
        if (varName == Names.IGNORE) {
            simpleVarRef.type = this.symTable.noType;
            typeChecker.checkExpr(simpleVarRef, env);
            continue;
        }
        // Check variable symbol for existence.
        BSymbol symbol = symResolver.lookupSymbol(env, varName, SymTag.VARIABLE);
        if (symbol == symTable.notFoundSymbol) {
            symbolEnter.defineVarSymbol(simpleVarRef.pos, Collections.emptySet(), varTypes.get(i), varName, env);
            typeChecker.checkExpr(simpleVarRef, env);
        } else {
            dlog.error(simpleVarRef.pos, DiagnosticCode.REDECLARED_SYMBOL, varName);
        }
    }
}
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) BLangAnnotationAttachmentPoint(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) Name(org.wso2.ballerinalang.compiler.util.Name)

Aggregations

BLangForeach (org.wso2.ballerinalang.compiler.tree.statements.BLangForeach)6 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)4 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)3 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)3 BLangVariableReference (org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference)3 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)3 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)3 BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)3 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)3 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)2 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)2 BLangBracedOrTupleExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBracedOrTupleExpr)2 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)2 Instruction (org.wso2.ballerinalang.programfile.Instruction)2 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1