use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateSumAggregator.
/**
* Generates following.
*
* result = result + value
*
* @param blockStmt target
* @param ctx current context
*/
private void generateSumAggregator(BLangBlockStmt blockStmt, IterableContext ctx) {
final DiagnosticPos pos = blockStmt.pos;
final BLangBinaryExpr add = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
add.pos = pos;
add.type = ctx.resultVar.symbol.type;
add.opKind = OperatorKind.ADD;
add.lhsExpr = ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol);
add.rhsExpr = ASTBuilderUtil.createVariableRef(pos, ctx.iteratorResultVariables.get(0).symbol);
add.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.ADD, add.type, add.type);
final BLangAssignment countAdd = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
countAdd.varRefs.add(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
countAdd.expr = add;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateCompareAggregator.
/**
* Generates following.
*
* result = result (Operator) value ? result : value
*
* @param blockStmt target
* @param ctx current context
* @param operator compare operator
*/
private void generateCompareAggregator(BLangBlockStmt blockStmt, IterableContext ctx, OperatorKind operator) {
final DiagnosticPos pos = blockStmt.pos;
final BLangSimpleVarRef resultVar = ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol);
final BLangSimpleVarRef valueVar = ASTBuilderUtil.createVariableRef(pos, ctx.iteratorResultVariables.get(0).symbol);
final BLangBinaryExpr compare = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
compare.pos = pos;
compare.type = symTable.booleanType;
compare.opKind = operator;
compare.lhsExpr = resultVar;
compare.rhsExpr = valueVar;
compare.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(operator, resultVar.symbol.type, valueVar.symbol.type);
final BLangTernaryExpr ternaryExpr = (BLangTernaryExpr) TreeBuilder.createTernaryExpressionNode();
ternaryExpr.pos = pos;
ternaryExpr.expr = compare;
ternaryExpr.thenExpr = resultVar;
ternaryExpr.elseExpr = valueVar;
ternaryExpr.type = compare.type;
final BLangAssignment countAdd = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
countAdd.varRefs.add(resultVar);
countAdd.expr = ternaryExpr;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment in project ballerina by ballerina-lang.
the class SymbolEnter method createAssignmentStmt.
private StatementNode createAssignmentStmt(BLangVariable variable, BVarSymbol varSym, BVarSymbol fieldVar) {
// Create LHS reference variable
BLangSimpleVarRef varRef = (BLangSimpleVarRef) TreeBuilder.createSimpleVariableReferenceNode();
varRef.pos = variable.pos;
varRef.variableName = (BLangIdentifier) createIdentifier(fieldVar.name.getValue());
varRef.pkgAlias = (BLangIdentifier) TreeBuilder.createIdentifierNode();
// Create RHS variable reference
BLangSimpleVarRef exprVar = (BLangSimpleVarRef) TreeBuilder.createSimpleVariableReferenceNode();
exprVar.pos = variable.pos;
exprVar.variableName = (BLangIdentifier) createIdentifier(varSym.name.getValue());
exprVar.pkgAlias = (BLangIdentifier) TreeBuilder.createIdentifierNode();
// Create assignment statement
BLangAssignment assignmentStmt = (BLangAssignment) TreeBuilder.createAssignmentNode();
assignmentStmt.expr = exprVar;
assignmentStmt.pos = variable.pos;
assignmentStmt.addVariable(varRef);
return assignmentStmt;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment in project ballerina by ballerina-lang.
the class SymbolEnter method createAssignmentStmt.
private StatementNode createAssignmentStmt(BLangVariable variable) {
BLangSimpleVarRef varRef = (BLangSimpleVarRef) TreeBuilder.createSimpleVariableReferenceNode();
varRef.pos = variable.pos;
varRef.variableName = variable.name;
varRef.pkgAlias = (BLangIdentifier) TreeBuilder.createIdentifierNode();
BLangAssignment assignmentStmt = (BLangAssignment) TreeBuilder.createAssignmentNode();
assignmentStmt.expr = variable.expr;
assignmentStmt.pos = variable.pos;
assignmentStmt.addVariable(varRef);
return assignmentStmt;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment in project ballerina by ballerina-lang.
the class TaintAnalyzer method visit.
public void visit(BLangAssignment assignNode) {
assignNode.expr.accept(this);
boolean multiReturnsHandledProperly = taintedStatusList.size() == assignNode.varRefs.size();
boolean combinedTaintedStatus = false;
if (!multiReturnsHandledProperly) {
combinedTaintedStatus = taintedStatusList.stream().filter(status -> status == true).count() > 0;
}
// Propagate tainted status of each variable separately (when multi returns are used).
for (int varIndex = 0; varIndex < assignNode.varRefs.size(); varIndex++) {
BLangExpression varRefExpr = assignNode.varRefs.get(varIndex);
boolean varTaintedStatus;
if (multiReturnsHandledProperly) {
varTaintedStatus = taintedStatusList.get(varIndex);
} else {
varTaintedStatus = combinedTaintedStatus;
}
visitAssignment(varRefExpr, varTaintedStatus, assignNode.pos);
}
}
Aggregations