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;
}
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);
}
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);
}
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));
}
}
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);
}
}
}
Aggregations