Search in sources :

Example 91 with Expression

use of org.wso2.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.

the class SemanticAnalyzer method visit.

public void visit(BLangVariable varNode) {
    int ownerSymTag = env.scope.owner.tag;
    if ((ownerSymTag & SymTag.INVOKABLE) == SymTag.INVOKABLE) {
        // If the variable is parameter then the variable symbol is already defined
        if (varNode.symbol == null) {
            symbolEnter.defineNode(varNode, env);
        }
    }
    BType lhsType = varNode.symbol.type;
    varNode.type = lhsType;
    // Here we validate annotation attachments for package level variables.
    varNode.annAttachments.forEach(a -> {
        a.attachmentPoint = new BLangAnnotationAttachmentPoint(BLangAnnotationAttachmentPoint.AttachmentPoint.CONST);
        a.accept(this);
    });
    // Here we validate document attachments for package level variables.
    varNode.docAttachments.forEach(doc -> {
        doc.accept(this);
    });
    // Analyze the init expression
    BLangExpression rhsExpr = varNode.expr;
    if (rhsExpr == null) {
        return;
    }
    // Here we create a new symbol environment to catch self references by keep the current
    // variable symbol in the symbol environment
    // e.g. int a = x + a;
    SymbolEnv varInitEnv = SymbolEnv.createVarInitEnv(varNode, env, varNode.symbol);
    // It will we done during the init-function of the respective construct is visited.
    if ((ownerSymTag & SymTag.PACKAGE) == SymTag.PACKAGE || (ownerSymTag & SymTag.SERVICE) == SymTag.SERVICE || (ownerSymTag & SymTag.CONNECTOR) == SymTag.CONNECTOR) {
        return;
    }
    // Return if this not a safe assignment
    if (!varNode.safeAssignment) {
        typeChecker.checkExpr(rhsExpr, varInitEnv, Lists.of(lhsType));
        return;
    }
    handleSafeAssignment(varNode.pos, lhsType, rhsExpr, varInitEnv);
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangAnnotationAttachmentPoint(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangAnnotationAttachmentPoint(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint)

Example 92 with Expression

use of org.wso2.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.

the class SemanticAnalyzer method visit.

public void visit(BLangBind bindNode) {
    List<BType> expTypes = new ArrayList<>();
    // Check each LHS expression.
    BLangExpression varRef = bindNode.varRef;
    ((BLangVariableReference) varRef).lhsVar = true;
    expTypes.add(typeChecker.checkExpr(varRef, env).get(0));
    checkConstantAssignment(varRef);
    typeChecker.checkExpr(bindNode.expr, this.env, expTypes);
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) ArrayList(java.util.ArrayList) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 93 with Expression

use of org.wso2.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.

the class TypeChecker method getTypeOfExprInFieldAccess.

private BType getTypeOfExprInFieldAccess(BLangExpression expr) {
    // First check whether variable expression is of type enum.
    if (expr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
        BLangSimpleVarRef varRef = (BLangSimpleVarRef) expr;
        BSymbol symbol = symResolver.lookupSymbolInPackage(varRef.pos, env, names.fromIdNode(varRef.pkgAlias), names.fromIdNode(varRef.variableName), SymTag.ENUM);
        if (symbol != symTable.notFoundSymbol) {
            expr.type = symbol.type;
            return symbol.type;
        }
    }
    checkExpr(expr, this.env, Lists.of(symTable.noType));
    return expr.type;
}
Also used : BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)

Example 94 with Expression

use of org.wso2.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.

the class TypeChecker method checkRecLiteralKeyValue.

private void checkRecLiteralKeyValue(BLangRecordKeyValue keyValuePair, BType recType) {
    BType fieldType = symTable.errType;
    BLangExpression valueExpr = keyValuePair.valueExpr;
    switch(recType.tag) {
        case TypeTags.STRUCT:
            fieldType = checkStructLiteralKeyExpr(keyValuePair.key, recType, RecordKind.STRUCT);
            break;
        case TypeTags.MAP:
            fieldType = checkMapLiteralKeyExpr(keyValuePair.key.expr, recType, RecordKind.MAP);
            break;
        case TypeTags.JSON:
            fieldType = checkJSONLiteralKeyExpr(keyValuePair.key, recType, RecordKind.JSON);
            // If the field is again a struct, treat that literal expression as another constraint JSON.
            if (fieldType.tag == TypeTags.STRUCT) {
                fieldType = new BJSONType(TypeTags.JSON, fieldType, symTable.jsonType.tsymbol);
            }
            // First visit the expression having field type, as the expected type.
            checkExpr(valueExpr, this.env, Lists.of(fieldType));
            // Again check the type compatibility with JSON
            if (valueExpr.impConversionExpr == null) {
                types.checkTypes(valueExpr, Lists.of(valueExpr.type), Lists.of(symTable.jsonType));
            } else {
                BType valueType = valueExpr.type;
                types.checkTypes(valueExpr, valueExpr.impConversionExpr.types, Lists.of(symTable.jsonType));
                valueExpr.type = valueType;
            }
            resultTypes = Lists.of(valueExpr.type);
            return;
    }
    checkExpr(valueExpr, this.env, Lists.of(fieldType));
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 95 with Expression

use of org.wso2.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.

the class TypeChecker method visit.

@Override
public void visit(BLangBracedOrTupleExpr bracedOrTupleExpr) {
    // Handle Tuple Expression.
    if (!expTypes.isEmpty() && expTypes.get(0).tag == TypeTags.TUPLE) {
        BTupleType tupleType = (BTupleType) this.expTypes.get(0);
        // Fix this.
        List<BType> expTypes = getListWithErrorTypes(bracedOrTupleExpr.expressions.size());
        if (tupleType.tupleTypes.size() != bracedOrTupleExpr.expressions.size()) {
            dlog.error(bracedOrTupleExpr.pos, DiagnosticCode.SYNTAX_ERROR, "tuple and expression size does not match");
        } else {
            expTypes = tupleType.tupleTypes;
        }
        List<BType> results = new ArrayList<>();
        for (int i = 0; i < bracedOrTupleExpr.expressions.size(); i++) {
            results.add(checkExpr(bracedOrTupleExpr.expressions.get(i), env, Lists.of(expTypes.get(i))).get(0));
        }
        resultTypes = Lists.of(new BTupleType(results));
    } else if (bracedOrTupleExpr.expressions.size() > 1) {
        // This is a tuple.
        List<BType> results = new ArrayList<>();
        for (int i = 0; i < bracedOrTupleExpr.expressions.size(); i++) {
            results.add(checkExpr(bracedOrTupleExpr.expressions.get(i), env, Lists.of(symTable.noType)).get(0));
        }
        resultTypes = Lists.of(new BTupleType(results));
    } else {
        // This is a braced expression.
        bracedOrTupleExpr.isBracedExpr = true;
        final BLangExpression expr = bracedOrTupleExpr.expressions.get(0);
        final BType actualType = checkExpr(expr, env, Lists.of(symTable.noType)).get(0);
        types.setImplicitCastExpr(expr, actualType, expTypes.get(0));
        resultTypes = Lists.of(actualType);
    }
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) ArrayList(java.util.ArrayList) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) ArrayList(java.util.ArrayList) List(java.util.List) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Aggregations

Expression (org.wso2.siddhi.query.api.expression.Expression)32 ArrayList (java.util.ArrayList)20 Attribute (org.wso2.siddhi.query.api.definition.Attribute)16 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)15 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)15 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)13 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)13 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)13 Variable (org.wso2.siddhi.query.api.expression.Variable)11 Test (org.testng.annotations.Test)10 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)10 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)10 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)10 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)9 OutputAttribute (org.wso2.siddhi.query.api.execution.query.selection.OutputAttribute)8 CompiledCondition (org.wso2.siddhi.core.util.collection.operator.CompiledCondition)7 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)6 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)6 MatchingMetaInfoHolder (org.wso2.siddhi.core.util.collection.operator.MatchingMetaInfoHolder)6 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)6