Search in sources :

Example 41 with Expression

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

the class SemanticAnalyzer method visit.

@Override
public void visit(BLangEndpoint endpointNode) {
    endpointNode.annAttachments.forEach(annotationAttachment -> {
        annotationAttachment.attachmentPoint = new BLangAnnotationAttachmentPoint(BLangAnnotationAttachmentPoint.AttachmentPoint.ENDPOINT);
        this.analyzeDef(annotationAttachment, env);
    });
    if (endpointNode.configurationExpr == null) {
        return;
    }
    BType configType = symTable.errType;
    if (endpointNode.symbol != null && endpointNode.symbol.type.tag == TypeTags.STRUCT) {
        if (endpointNode.configurationExpr.getKind() == NodeKind.RECORD_LITERAL_EXPR) {
            // Init expression.
            configType = endpointSPIAnalyzer.getEndpointConfigType((BStructSymbol) endpointNode.symbol.type.tsymbol);
        } else {
            // assign Expression.
            configType = endpointNode.symbol.type;
        }
    }
    this.typeChecker.checkExpr(endpointNode.configurationExpr, env, Lists.of(configType));
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangAnnotationAttachmentPoint(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)

Example 42 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(BLangAssignment assignNode) {
    if (assignNode.isDeclaredWithVar()) {
        handleAssignNodeWithVar(assignNode);
        return;
    }
    // Check each LHS expression.
    List<BType> expTypes = new ArrayList<>();
    for (BLangExpression expr : assignNode.varRefs) {
        // In assignment, lhs supports only simpleVarRef, indexBasedAccess, filedBasedAccess expressions.
        if (expr.getKind() != NodeKind.SIMPLE_VARIABLE_REF && expr.getKind() != NodeKind.INDEX_BASED_ACCESS_EXPR && expr.getKind() != NodeKind.FIELD_BASED_ACCESS_EXPR && expr.getKind() != NodeKind.XML_ATTRIBUTE_ACCESS_EXPR) {
            dlog.error(expr.pos, DiagnosticCode.INVALID_VARIABLE_ASSIGNMENT, expr);
            expTypes.add(symTable.errType);
            continue;
        }
        // Evaluate the variable reference expression.
        BLangVariableReference varRef = (BLangVariableReference) expr;
        varRef.lhsVar = true;
        typeChecker.checkExpr(varRef, env);
        // Check whether we've got an enumerator access expression here.
        if (varRef.getKind() == NodeKind.FIELD_BASED_ACCESS_EXPR && ((BLangFieldBasedAccess) varRef).expr.type.tag == TypeTags.ENUM) {
            dlog.error(varRef.pos, DiagnosticCode.INVALID_VARIABLE_ASSIGNMENT, varRef);
            expTypes.add(symTable.errType);
            continue;
        }
        expTypes.add(varRef.type);
        checkConstantAssignment(varRef);
    }
    if (assignNode.getKind() == NodeKind.TUPLE_DESTRUCTURE) {
        BTupleType tupleType = new BTupleType(expTypes);
        expTypes = Lists.of(tupleType);
    }
    if (!assignNode.safeAssignment) {
        typeChecker.checkExpr(assignNode.expr, this.env, expTypes);
        return;
    }
    // Assume that there is only one variable reference in LHS
    // Continue the validate if this is a safe assignment operator
    handleSafeAssignment(assignNode.pos, assignNode.varRefs.get(0).type, assignNode.expr, this.env);
}
Also used : BLangFieldBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangFieldBasedAccess) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangVariableReference(org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference) ArrayList(java.util.ArrayList) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 43 with Expression

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

the class TypeChecker method visit.

public void visit(BLangFieldBasedAccess fieldAccessExpr) {
    // First analyze the variable reference expression.
    BType actualType = symTable.errType;
    BType varRefType = getTypeOfExprInFieldAccess(fieldAccessExpr.expr);
    if (fieldAccessExpr.fieldType == FieldType.ALL && varRefType.tag != TypeTags.XML) {
        dlog.error(fieldAccessExpr.pos, DiagnosticCode.CANNOT_GET_ALL_FIELDS, varRefType);
    }
    Name fieldName = names.fromIdNode(fieldAccessExpr.field);
    switch(varRefType.tag) {
        case TypeTags.STRUCT:
            actualType = checkStructFieldAccess(fieldAccessExpr, fieldName, varRefType);
            break;
        case TypeTags.MAP:
            actualType = ((BMapType) varRefType).getConstraint();
            break;
        case TypeTags.JSON:
            BType constraintType = ((BJSONType) varRefType).constraint;
            if (constraintType.tag == TypeTags.STRUCT) {
                BType fieldType = checkStructFieldAccess(fieldAccessExpr, fieldName, constraintType);
                // If the type of the field is struct, treat it as constraint JSON type.
                if (fieldType.tag == TypeTags.STRUCT) {
                    actualType = new BJSONType(TypeTags.JSON, fieldType, symTable.jsonType.tsymbol);
                    break;
                }
            }
            actualType = symTable.jsonType;
            break;
        case TypeTags.ENUM:
            // Enumerator access expressions only allow enum type name as the first part e.g state.INSTALLED,
            BEnumType enumType = (BEnumType) varRefType;
            if (fieldAccessExpr.expr.getKind() != NodeKind.SIMPLE_VARIABLE_REF || !((BLangSimpleVarRef) fieldAccessExpr.expr).variableName.value.equals(enumType.tsymbol.name.value)) {
                dlog.error(fieldAccessExpr.pos, DiagnosticCode.INVALID_ENUM_EXPR, enumType.tsymbol.name.value);
                break;
            }
            BSymbol symbol = symResolver.lookupMemberSymbol(fieldAccessExpr.pos, enumType.tsymbol.scope, this.env, fieldName, SymTag.VARIABLE);
            if (symbol == symTable.notFoundSymbol) {
                dlog.error(fieldAccessExpr.pos, DiagnosticCode.UNDEFINED_SYMBOL, fieldName.value);
                break;
            }
            fieldAccessExpr.symbol = (BVarSymbol) symbol;
            actualType = fieldAccessExpr.expr.type;
            break;
        case TypeTags.XML:
            if (fieldAccessExpr.lhsVar) {
                dlog.error(fieldAccessExpr.pos, DiagnosticCode.CANNOT_UPDATE_XML_SEQUENCE);
                break;
            }
            actualType = symTable.xmlType;
            break;
        case TypeTags.ERROR:
            // Do nothing
            break;
        default:
            dlog.error(fieldAccessExpr.pos, DiagnosticCode.OPERATION_DOES_NOT_SUPPORT_FIELD_ACCESS, varRefType);
    }
    resultTypes = types.checkTypes(fieldAccessExpr, Lists.of(actualType), this.expTypes);
}
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) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name) BEnumType(org.wso2.ballerinalang.compiler.semantics.model.types.BEnumType)

Example 44 with Expression

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

the class TypeChecker method visit.

public void visit(BLangXMLAttributeAccess xmlAttributeAccessExpr) {
    BType actualType = symTable.errType;
    // First analyze the variable reference expression.
    checkExpr(xmlAttributeAccessExpr.expr, env, Lists.of(symTable.xmlType));
    // Then analyze the index expression.
    BLangExpression indexExpr = xmlAttributeAccessExpr.indexExpr;
    if (indexExpr == null) {
        if (xmlAttributeAccessExpr.lhsVar) {
            dlog.error(xmlAttributeAccessExpr.pos, DiagnosticCode.XML_ATTRIBUTE_MAP_UPDATE_NOT_ALLOWED);
        } else {
            actualType = symTable.xmlAttributesType;
        }
        resultTypes = types.checkTypes(xmlAttributeAccessExpr, Lists.of(actualType), expTypes);
        return;
    }
    checkExpr(indexExpr, env, Lists.of(symTable.stringType)).get(0);
    if (indexExpr.getKind() == NodeKind.XML_QNAME) {
        ((BLangXMLQName) indexExpr).isUsedInXML = true;
    }
    if (indexExpr.type.tag == TypeTags.STRING) {
        actualType = symTable.stringType;
    }
    xmlAttributeAccessExpr.namespaces.putAll(symResolver.resolveAllNamespaces(env));
    resultTypes = types.checkTypes(xmlAttributeAccessExpr, Lists.of(actualType), expTypes);
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 45 with Expression

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

the class TypeChecker method visit.

public void visit(BLangIndexBasedAccess indexBasedAccessExpr) {
    BType actualType = symTable.errType;
    // First analyze the variable reference expression.
    checkExpr(indexBasedAccessExpr.expr, this.env, Lists.of(symTable.noType));
    BType indexExprType;
    BType varRefType = indexBasedAccessExpr.expr.type;
    BLangExpression indexExpr = indexBasedAccessExpr.indexExpr;
    switch(varRefType.tag) {
        case TypeTags.STRUCT:
            indexExprType = checkIndexExprForStructFieldAccess(indexExpr);
            if (indexExprType.tag == TypeTags.STRING) {
                String fieldName = (String) ((BLangLiteral) indexExpr).value;
                actualType = checkStructFieldAccess(indexBasedAccessExpr, names.fromString(fieldName), varRefType);
            }
            break;
        case TypeTags.MAP:
            indexExprType = checkExpr(indexExpr, this.env, Lists.of(symTable.stringType)).get(0);
            if (indexExprType.tag == TypeTags.STRING) {
                actualType = ((BMapType) varRefType).getConstraint();
            }
            break;
        case TypeTags.JSON:
            BType constraintType = ((BJSONType) varRefType).constraint;
            if (constraintType.tag == TypeTags.STRUCT) {
                indexExprType = checkIndexExprForStructFieldAccess(indexExpr);
                if (indexExprType.tag != TypeTags.STRING) {
                    break;
                }
                String fieldName = (String) ((BLangLiteral) indexExpr).value;
                BType fieldType = checkStructFieldAccess(indexBasedAccessExpr, names.fromString(fieldName), constraintType);
                // If the type of the field is struct, treat it as constraint JSON type.
                if (fieldType.tag == TypeTags.STRUCT) {
                    actualType = new BJSONType(TypeTags.JSON, fieldType, symTable.jsonType.tsymbol);
                    break;
                }
            } else {
                indexExprType = checkExpr(indexExpr, this.env, Lists.of(symTable.noType)).get(0);
                if (indexExprType.tag != TypeTags.STRING && indexExprType.tag != TypeTags.INT) {
                    dlog.error(indexExpr.pos, DiagnosticCode.INCOMPATIBLE_TYPES, symTable.stringType, indexExprType);
                    break;
                }
            }
            actualType = symTable.jsonType;
            break;
        case TypeTags.ARRAY:
            indexExprType = checkExpr(indexExpr, this.env, Lists.of(symTable.intType)).get(0);
            if (indexExprType.tag == TypeTags.INT) {
                actualType = ((BArrayType) varRefType).getElementType();
            }
            break;
        case TypeTags.XML:
            if (indexBasedAccessExpr.lhsVar) {
                dlog.error(indexBasedAccessExpr.pos, DiagnosticCode.CANNOT_UPDATE_XML_SEQUENCE);
                break;
            }
            checkExpr(indexExpr, this.env).get(0);
            actualType = symTable.xmlType;
            break;
        case TypeTags.ERROR:
            // Do nothing
            break;
        default:
            dlog.error(indexBasedAccessExpr.pos, DiagnosticCode.OPERATION_DOES_NOT_SUPPORT_INDEXING, indexBasedAccessExpr.expr.type);
    }
    resultTypes = types.checkTypes(indexBasedAccessExpr, Lists.of(actualType), this.expTypes);
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) 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