Search in sources :

Example 41 with BType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.

the class TypeChecker method checkActionInvocationExpr.

private void checkActionInvocationExpr(BLangInvocation iExpr, BType conType) {
    List<BType> actualTypes = getListWithErrorTypes(expTypes.size());
    if (conType == symTable.errType || conType.tag != TypeTags.STRUCT || iExpr.expr.symbol.tag != SymTag.ENDPOINT) {
        dlog.error(iExpr.pos, DiagnosticCode.INVALID_ACTION_INVOCATION);
        resultTypes = actualTypes;
        return;
    }
    final BEndpointVarSymbol epSymbol = (BEndpointVarSymbol) iExpr.expr.symbol;
    if (!epSymbol.interactable) {
        dlog.error(iExpr.pos, DiagnosticCode.ENDPOINT_NOT_SUPPORT_INTERACTIONS, epSymbol.name);
        resultTypes = actualTypes;
        return;
    }
    BSymbol conSymbol = epSymbol.clientSymbol;
    if (conSymbol == null || conSymbol == symTable.notFoundSymbol || conSymbol == symTable.errSymbol || conSymbol.tag != SymTag.STRUCT) {
        dlog.error(iExpr.pos, DiagnosticCode.INVALID_ACTION_INVOCATION);
        resultTypes = actualTypes;
        return;
    }
    Name actionName = names.fromIdNode(iExpr.name);
    Name uniqueFuncName = names.fromString(Symbols.getAttachedFuncSymbolName(conSymbol.name.value, actionName.value));
    BPackageSymbol packageSymbol = (BPackageSymbol) conSymbol.owner;
    BSymbol actionSym = symResolver.lookupMemberSymbol(iExpr.pos, packageSymbol.scope, this.env, uniqueFuncName, SymTag.FUNCTION);
    if (actionSym == symTable.errSymbol || actionSym == symTable.notFoundSymbol) {
        dlog.error(iExpr.pos, DiagnosticCode.UNDEFINED_ACTION, actionName, epSymbol.name, conSymbol.type);
        resultTypes = actualTypes;
        return;
    }
    iExpr.symbol = actionSym;
    checkInvocationParamAndReturnType(iExpr);
}
Also used : BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BEndpointVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BEndpointVarSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 42 with BType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.

the class TypeChecker method checkFunctionInvocationExpr.

private void checkFunctionInvocationExpr(BLangInvocation iExpr, BType bType) {
    Name funcName = names.fromString(Symbols.getAttachedFuncSymbolName(bType.toString(), iExpr.name.value));
    BPackageSymbol packageSymbol = (BPackageSymbol) bType.tsymbol.owner;
    BSymbol funcSymbol = symResolver.lookupMemberSymbol(iExpr.pos, packageSymbol.scope, this.env, funcName, SymTag.FUNCTION);
    if (funcSymbol == symTable.notFoundSymbol) {
        dlog.error(iExpr.pos, DiagnosticCode.UNDEFINED_FUNCTION, funcName);
        resultTypes = getListWithErrorTypes(expTypes.size());
        return;
    }
    iExpr.symbol = funcSymbol;
    checkInvocationParamAndReturnType(iExpr);
}
Also used : BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 43 with BType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.

the class TypeChecker method visit.

public void visit(BLangSimpleVarRef varRefExpr) {
    // Set error type as the actual type.
    BType actualType = symTable.errType;
    Name varName = names.fromIdNode(varRefExpr.variableName);
    if (varName == Names.IGNORE) {
        if (varRefExpr.lhsVar) {
            varRefExpr.type = this.symTable.noType;
        } else {
            varRefExpr.type = this.symTable.errType;
            dlog.error(varRefExpr.pos, DiagnosticCode.UNDERSCORE_NOT_ALLOWED);
        }
        varRefExpr.symbol = new BVarSymbol(0, varName, env.enclPkg.symbol.pkgID, actualType, env.scope.owner);
        resultTypes = Lists.of(varRefExpr.type);
        return;
    }
    varRefExpr.pkgSymbol = symResolver.resolveImportSymbol(varRefExpr.pos, env, names.fromIdNode(varRefExpr.pkgAlias));
    if (varRefExpr.pkgSymbol.tag == SymTag.XMLNS) {
        actualType = symTable.stringType;
    } else if (varRefExpr.pkgSymbol != symTable.notFoundSymbol) {
        BSymbol symbol = symResolver.lookupSymbolInPackage(varRefExpr.pos, env, names.fromIdNode(varRefExpr.pkgAlias), varName, SymTag.VARIABLE_NAME);
        if ((symbol.tag & SymTag.VARIABLE) == SymTag.VARIABLE) {
            BVarSymbol varSym = (BVarSymbol) symbol;
            checkSefReferences(varRefExpr.pos, env, varSym);
            varRefExpr.symbol = varSym;
            actualType = varSym.type;
        } else {
            dlog.error(varRefExpr.pos, DiagnosticCode.UNDEFINED_SYMBOL, varName.toString());
        }
    }
    // Check type compatibility
    resultTypes = types.checkTypes(varRefExpr, Lists.of(actualType), expTypes);
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 44 with BType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BType 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)

Example 45 with BType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BType in project ballerina by ballerina-lang.

the class TypeChecker method checkStructFieldAccess.

private BType checkStructFieldAccess(BLangVariableReference varReferExpr, Name fieldName, BType structType) {
    BSymbol fieldSymbol = symResolver.resolveStructField(varReferExpr.pos, this.env, fieldName, structType.tsymbol);
    if (fieldSymbol == symTable.notFoundSymbol) {
        dlog.error(varReferExpr.pos, DiagnosticCode.UNDEFINED_STRUCT_FIELD, fieldName, structType.tsymbol);
        return symTable.errType;
    }
    // Setting the field symbol. This is used during the code generation phase
    varReferExpr.symbol = (BVarSymbol) fieldSymbol;
    return fieldSymbol.type;
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)

Aggregations

BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)97 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)34 ArrayList (java.util.ArrayList)30 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)30 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)25 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)23 Name (org.wso2.ballerinalang.compiler.util.Name)20 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)18 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)17 BArrayType (org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType)17 BUnionType (org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType)17 List (java.util.List)16 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)16 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)16 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)15 Collectors (java.util.stream.Collectors)14 BConversionOperatorSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol)13 BStructType (org.wso2.ballerinalang.compiler.semantics.model.types.BStructType)13 Set (java.util.Set)12 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)12