Search in sources :

Example 16 with BLangExpression

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.

the class SemanticAnalyzer method handleAssignNodeWithVar.

private void handleAssignNodeWithVar(BLangAssignment assignNode) {
    int ignoredCount = 0;
    int createdSymbolCount = 0;
    List<Name> newVariables = new ArrayList<Name>();
    List<BType> expTypes = new ArrayList<>();
    // Check each LHS expression.
    for (int i = 0; i < assignNode.varRefs.size(); i++) {
        BLangExpression varRef = assignNode.varRefs.get(i);
        // If the assignment is declared with "var", then lhs supports only simpleVarRef expressions only.
        if (varRef.getKind() != NodeKind.SIMPLE_VARIABLE_REF) {
            dlog.error(varRef.pos, DiagnosticCode.INVALID_VARIABLE_ASSIGNMENT, varRef);
            expTypes.add(symTable.errType);
            continue;
        }
        // Check variable symbol if exists.
        BLangSimpleVarRef simpleVarRef = (BLangSimpleVarRef) varRef;
        ((BLangVariableReference) varRef).lhsVar = true;
        Name varName = names.fromIdNode(simpleVarRef.variableName);
        if (varName == Names.IGNORE) {
            ignoredCount++;
            simpleVarRef.type = this.symTable.noType;
            expTypes.add(symTable.noType);
            typeChecker.checkExpr(simpleVarRef, env);
            continue;
        }
        BSymbol symbol = symResolver.lookupSymbol(env, varName, SymTag.VARIABLE);
        if (symbol == symTable.notFoundSymbol) {
            createdSymbolCount++;
            newVariables.add(varName);
            expTypes.add(symTable.noType);
        } else {
            expTypes.add(symbol.type);
        }
    }
    if (ignoredCount == assignNode.varRefs.size() || createdSymbolCount == 0) {
        dlog.error(assignNode.pos, DiagnosticCode.NO_NEW_VARIABLES_VAR_ASSIGNMENT);
    }
    // Check RHS expressions with expected type list.
    if (assignNode.getKind() == NodeKind.TUPLE_DESTRUCTURE) {
        expTypes = Lists.of(symTable.noType);
    }
    List<BType> rhsTypes = typeChecker.checkExpr(assignNode.expr, this.env, expTypes);
    if (assignNode.safeAssignment) {
        rhsTypes = Lists.of(handleSafeAssignmentWithVarDeclaration(assignNode.pos, rhsTypes.get(0)));
    }
    if (assignNode.getKind() == NodeKind.TUPLE_DESTRUCTURE) {
        if (rhsTypes.get(0) != symTable.errType && rhsTypes.get(0).tag == TypeTags.TUPLE) {
            BTupleType tupleType = (BTupleType) rhsTypes.get(0);
            rhsTypes = tupleType.tupleTypes;
        } else if (rhsTypes.get(0) != symTable.errType && rhsTypes.get(0).tag != TypeTags.TUPLE) {
            dlog.error(assignNode.pos, DiagnosticCode.INCOMPATIBLE_TYPES_EXP_TUPLE, rhsTypes.get(0));
            rhsTypes = typeChecker.getListWithErrorTypes(assignNode.varRefs.size());
        }
    }
    // visit all lhs expressions
    for (int i = 0; i < assignNode.varRefs.size(); i++) {
        BLangExpression varRef = assignNode.varRefs.get(i);
        if (varRef.getKind() != NodeKind.SIMPLE_VARIABLE_REF) {
            continue;
        }
        BType actualType = rhsTypes.get(i);
        BLangSimpleVarRef simpleVarRef = (BLangSimpleVarRef) varRef;
        Name varName = names.fromIdNode(simpleVarRef.variableName);
        if (newVariables.contains(varName)) {
            // define new variables
            this.symbolEnter.defineVarSymbol(simpleVarRef.pos, Collections.emptySet(), actualType, varName, env);
        }
        typeChecker.checkExpr(simpleVarRef, env);
    }
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) ArrayList(java.util.ArrayList) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) 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)

Example 17 with BLangExpression

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.

the class SemanticAnalyzer method visit.

public void visit(BLangJoinStreamingInput joinStreamingInput) {
    StreamingInput streamingInput = joinStreamingInput.getStreamingInput();
    if (streamingInput != null) {
        ((BLangStreamingInput) streamingInput).accept(this);
    }
    ExpressionNode expressionNode = joinStreamingInput.getOnExpression();
    if (expressionNode != null) {
        ((BLangExpression) expressionNode).accept(this);
    }
}
Also used : JoinStreamingInput(org.ballerinalang.model.tree.clauses.JoinStreamingInput) BLangPatternStreamingInput(org.wso2.ballerinalang.compiler.tree.clauses.BLangPatternStreamingInput) BLangStreamingInput(org.wso2.ballerinalang.compiler.tree.clauses.BLangStreamingInput) BLangJoinStreamingInput(org.wso2.ballerinalang.compiler.tree.clauses.BLangJoinStreamingInput) StreamingInput(org.ballerinalang.model.tree.clauses.StreamingInput) SelectExpressionNode(org.ballerinalang.model.tree.clauses.SelectExpressionNode) ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangStreamingInput(org.wso2.ballerinalang.compiler.tree.clauses.BLangStreamingInput)

Example 18 with BLangExpression

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.

the class SemanticAnalyzer method visit.

public void visit(BLangSelectExpression selectExpression) {
    ExpressionNode expressionNode = selectExpression.getExpression();
    ((BLangExpression) expressionNode).accept(this);
}
Also used : SelectExpressionNode(org.ballerinalang.model.tree.clauses.SelectExpressionNode) ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 19 with BLangExpression

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.

the class SemanticAnalyzer method checkRetryStmtValidity.

private void checkRetryStmtValidity(BLangExpression retryCountExpr) {
    boolean error = true;
    NodeKind retryKind = retryCountExpr.getKind();
    if (retryKind == NodeKind.LITERAL) {
        if (retryCountExpr.type.tag == TypeTags.INT) {
            int retryCount = Integer.parseInt(((BLangLiteral) retryCountExpr).getValue().toString());
            if (retryCount >= 0) {
                error = false;
            }
        }
    } else if (retryKind == NodeKind.SIMPLE_VARIABLE_REF) {
        if (((BLangSimpleVarRef) retryCountExpr).symbol.flags == Flags.CONST) {
            if (((BLangSimpleVarRef) retryCountExpr).symbol.type.tag == TypeTags.INT) {
                error = false;
            }
        }
    }
    if (error) {
        this.dlog.error(retryCountExpr.pos, DiagnosticCode.INVALID_RETRY_COUNT);
    }
}
Also used : NodeKind(org.ballerinalang.model.tree.NodeKind) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangAnnotationAttachmentPoint(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 20 with BLangExpression

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.

the class SemanticAnalyzer method visit.

public void visit(BLangSetAssignment setAssignmentClause) {
    ExpressionNode expressionNode = setAssignmentClause.getExpressionNode();
    ((BLangExpression) expressionNode).accept(this);
    ExpressionNode variableReference = setAssignmentClause.getVariableReference();
    ((BLangExpression) variableReference).accept(this);
}
Also used : SelectExpressionNode(org.ballerinalang.model.tree.clauses.SelectExpressionNode) ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Aggregations

BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)101 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)27 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)26 ArrayList (java.util.ArrayList)22 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)19 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)15 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)15 SelectExpressionNode (org.ballerinalang.model.tree.clauses.SelectExpressionNode)14 ExpressionNode (org.ballerinalang.model.tree.expressions.ExpressionNode)14 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)14 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)13 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)11 BLangArrayLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral)11 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)11 BLangTypeConversionExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr)11 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)10 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)10 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)10 BLangXMLQuotedString (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)10 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)10