Search in sources :

Example 11 with BLangAssignment

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;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)

Example 12 with BLangAssignment

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;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangTernaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr) BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)

Example 13 with BLangAssignment

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;
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)

Example 14 with BLangAssignment

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;
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)

Example 15 with BLangAssignment

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);
    }
}
Also used : Arrays(java.util.Arrays) BLangWithinClause(org.wso2.ballerinalang.compiler.tree.clauses.BLangWithinClause) BLangWhere(org.wso2.ballerinalang.compiler.tree.clauses.BLangWhere) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn) BLangStreamingQueryStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangStreamingQueryStatement) BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier) BLangBreak(org.wso2.ballerinalang.compiler.tree.statements.BLangBreak) BLangXMLTextLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLTextLiteral) BLangTryCatchFinally(org.wso2.ballerinalang.compiler.tree.statements.BLangTryCatchFinally) BLangTupleDestructure(org.wso2.ballerinalang.compiler.tree.statements.BLangTupleDestructure) Map(java.util.Map) SymbolKind(org.ballerinalang.model.symbols.SymbolKind) BLangVariableReference(org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference) BLangWhile(org.wso2.ballerinalang.compiler.tree.statements.BLangWhile) BLangAnnotAttachmentAttribute(org.wso2.ballerinalang.compiler.tree.expressions.BLangAnnotAttachmentAttribute) BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr) CompilerPhase(org.ballerinalang.compiler.CompilerPhase) BLangCompoundAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangCompoundAssignment) PackageID(org.ballerinalang.model.elements.PackageID) Set(java.util.Set) BLangXMLNS(org.wso2.ballerinalang.compiler.tree.BLangXMLNS) BLangAnnotAttribute(org.wso2.ballerinalang.compiler.tree.BLangAnnotAttribute) BLangPackageDeclaration(org.wso2.ballerinalang.compiler.tree.BLangPackageDeclaration) BLangLambdaFunction(org.wso2.ballerinalang.compiler.tree.expressions.BLangLambdaFunction) BLangXMLNSStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangXMLNSStatement) BLangSelectExpression(org.wso2.ballerinalang.compiler.tree.clauses.BLangSelectExpression) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf) BLangInvokableNode(org.wso2.ballerinalang.compiler.tree.BLangInvokableNode) BLangForeach(org.wso2.ballerinalang.compiler.tree.statements.BLangForeach) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) BLangXMLAttribute(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttribute) BLangNamedArgsExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangNamedArgsExpression) BLangAbort(org.wso2.ballerinalang.compiler.tree.statements.BLangAbort) BLangTypeInit(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit) BLangAnnotAttachmentAttributeValue(org.wso2.ballerinalang.compiler.tree.expressions.BLangAnnotAttachmentAttributeValue) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) BLangXMLCommentLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLCommentLiteral) BLangDeprecatedNode(org.wso2.ballerinalang.compiler.tree.BLangDeprecatedNode) BLangPostIncrement(org.wso2.ballerinalang.compiler.tree.statements.BLangPostIncrement) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) BLangFunctionClause(org.wso2.ballerinalang.compiler.tree.clauses.BLangFunctionClause) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) BLangUnaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) LinkedHashSet(java.util.LinkedHashSet) TaintRecord(org.wso2.ballerinalang.compiler.semantics.model.symbols.TaintRecord) BLangForkJoin(org.wso2.ballerinalang.compiler.tree.statements.BLangForkJoin) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangXMLProcInsLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLProcInsLiteral) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) BLangForever(org.wso2.ballerinalang.compiler.tree.statements.BLangForever) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) BLangThrow(org.wso2.ballerinalang.compiler.tree.statements.BLangThrow) BLangOrderBy(org.wso2.ballerinalang.compiler.tree.clauses.BLangOrderBy) BLangSelectClause(org.wso2.ballerinalang.compiler.tree.clauses.BLangSelectClause) SymTag(org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangDocumentationAttribute(org.wso2.ballerinalang.compiler.tree.expressions.BLangDocumentationAttribute) BLangTableQuery(org.wso2.ballerinalang.compiler.tree.clauses.BLangTableQuery) BLangPatternStreamingEdgeInput(org.wso2.ballerinalang.compiler.tree.clauses.BLangPatternStreamingEdgeInput) BLangBracedOrTupleExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBracedOrTupleExpr) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BLangTypeConversionExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr) BLangXMLAttributeAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttributeAccess) BLangWorkerReceive(org.wso2.ballerinalang.compiler.tree.statements.BLangWorkerReceive) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) BLangPatternStreamingInput(org.wso2.ballerinalang.compiler.tree.clauses.BLangPatternStreamingInput) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) BLangHaving(org.wso2.ballerinalang.compiler.tree.clauses.BLangHaving) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) Names(org.wso2.ballerinalang.compiler.util.Names) BLangFail(org.wso2.ballerinalang.compiler.tree.statements.BLangFail) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangDiagnosticLog(org.wso2.ballerinalang.compiler.util.diagnotic.BLangDiagnosticLog) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangLock(org.wso2.ballerinalang.compiler.tree.statements.BLangLock) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangWindow(org.wso2.ballerinalang.compiler.tree.clauses.BLangWindow) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) BLangTypeCastExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeCastExpr) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) BLangCatch(org.wso2.ballerinalang.compiler.tree.statements.BLangCatch) BLangFieldBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangFieldBasedAccess) List(java.util.List) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) NodeKind(org.ballerinalang.model.tree.NodeKind) BLangWorker(org.wso2.ballerinalang.compiler.tree.BLangWorker) BLangBind(org.wso2.ballerinalang.compiler.tree.statements.BLangBind) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) BLangStreamAction(org.wso2.ballerinalang.compiler.tree.clauses.BLangStreamAction) BLangTableQueryExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangTableQueryExpression) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangExpressionStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt) BLangStreamingInput(org.wso2.ballerinalang.compiler.tree.clauses.BLangStreamingInput) BLangStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangStatement) HashMap(java.util.HashMap) BLangNodeVisitor(org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor) BLangTypeofExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeofExpr) BLangTransaction(org.wso2.ballerinalang.compiler.tree.statements.BLangTransaction) TypeTags(org.wso2.ballerinalang.compiler.util.TypeTags) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangMatch(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch) BLangStringTemplateLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangStringTemplateLiteral) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BLangTernaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr) BLangIntRangeExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangIntRangeExpression) LinkedList(java.util.LinkedList) XMLConstants(javax.xml.XMLConstants) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangPatternClause(org.wso2.ballerinalang.compiler.tree.clauses.BLangPatternClause) BLangNext(org.wso2.ballerinalang.compiler.tree.statements.BLangNext) Symbols(org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangGroupBy(org.wso2.ballerinalang.compiler.tree.clauses.BLangGroupBy) Name(org.wso2.ballerinalang.compiler.util.Name) BLangJoinStreamingInput(org.wso2.ballerinalang.compiler.tree.clauses.BLangJoinStreamingInput) BLangAwaitExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangAwaitExpr) BLangRestArgsExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangRestArgsExpression) BLangXMLElementLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral) DiagnosticCode(org.ballerinalang.util.diagnostic.DiagnosticCode) BLangSetAssignment(org.wso2.ballerinalang.compiler.tree.clauses.BLangSetAssignment) BLangWorkerSend(org.wso2.ballerinalang.compiler.tree.statements.BLangWorkerSend) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) Comparator(java.util.Comparator) Collections(java.util.Collections) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Aggregations

BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)29 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)13 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)11 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)10 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)9 BLangBinaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)7 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)7 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)7 ArrayList (java.util.ArrayList)6 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)6 BLangVariableReference (org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference)6 BLangIndexBasedAccess (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess)5 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)4 BLangForeach (org.wso2.ballerinalang.compiler.tree.statements.BLangForeach)4 BLangIf (org.wso2.ballerinalang.compiler.tree.statements.BLangIf)4 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)3 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)3 BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)3 BLangStatement (org.wso2.ballerinalang.compiler.tree.statements.BLangStatement)3 Arrays (java.util.Arrays)2