Search in sources :

Example 36 with Variable

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

the class SemanticAnalyzer method defineResourceEndpoint.

private void defineResourceEndpoint(BLangResource resourceNode, SymbolEnv resourceEnv) {
    if (!resourceNode.getParameters().isEmpty()) {
        final BLangVariable variable = resourceNode.getParameters().get(0);
        if (variable.type == symTable.endpointType) {
            String actualVarName = variable.name.value.substring(1);
            variable.name = new BLangIdentifier();
            variable.name.value = actualVarName;
            if (resourceEnv.enclService.endpointType != null) {
                variable.type = resourceEnv.enclService.endpointType;
                final BEndpointVarSymbol bEndpointVarSymbol = symbolEnter.defineEndpointVarSymbol(variable.pos, EnumSet.noneOf(Flag.class), variable.type, names.fromString(actualVarName), resourceEnv);
                variable.symbol = bEndpointVarSymbol;
                endpointSPIAnalyzer.populateEndpointSymbol((BStructSymbol) variable.type.tsymbol, bEndpointVarSymbol);
            } else {
                variable.type = symTable.errType;
                variable.symbol = symbolEnter.defineVarSymbol(variable.pos, EnumSet.noneOf(Flag.class), variable.type, names.fromString(actualVarName), resourceEnv);
            }
            // Replace old symbol with new one.
            resourceNode.symbol.params.remove(0);
            resourceNode.symbol.params.add(0, variable.symbol);
            ((BInvokableType) resourceNode.symbol.type).paramTypes.remove(0);
            ((BInvokableType) resourceNode.symbol.type).paramTypes.add(0, variable.type);
        }
    }
}
Also used : BEndpointVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BEndpointVarSymbol) BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier) Flag(org.ballerinalang.model.elements.Flag) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 37 with Variable

use of org.wso2.siddhi.query.api.expression.Variable 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 38 with Variable

use of org.wso2.siddhi.query.api.expression.Variable 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 39 with Variable

use of org.wso2.siddhi.query.api.expression.Variable 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 40 with Variable

use of org.wso2.siddhi.query.api.expression.Variable 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)

Aggregations

RestVariable (org.wso2.carbon.bpmn.rest.engine.variable.RestVariable)37 ArrayList (java.util.ArrayList)31 RestResponseFactory (org.wso2.carbon.bpmn.rest.common.RestResponseFactory)31 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)30 Test (org.testng.annotations.Test)28 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)26 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)26 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)26 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)23 BJSON (org.ballerinalang.model.values.BJSON)22 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)18 IOException (java.io.IOException)17 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)17 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)17 Variable (org.wso2.siddhi.query.api.expression.Variable)17 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)16 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)15 DataResponse (org.wso2.carbon.bpmn.rest.model.common.DataResponse)15 Response (javax.ws.rs.core.Response)14 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)13