Search in sources :

Example 31 with BType

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

the class TypeChecker method checkInvocationParam.

private List<BType> checkInvocationParam(BLangInvocation iExpr) {
    List<BType> paramTypes = ((BInvokableType) iExpr.symbol.type).getParameterTypes();
    int requiredParamsCount;
    if (iExpr.symbol.tag == SymTag.VARIABLE) {
        // Here we assume function pointers can have only required params.
        // And assume that named params and rest params are not supported.
        requiredParamsCount = paramTypes.size();
    } else {
        requiredParamsCount = ((BInvokableSymbol) iExpr.symbol).params.size();
    }
    // Split the different argument types: required args, named args and rest args
    int i = 0;
    BLangExpression vararg = null;
    for (BLangExpression expr : iExpr.argExprs) {
        switch(expr.getKind()) {
            case NAMED_ARGS_EXPR:
                iExpr.namedArgs.add((BLangNamedArgsExpression) expr);
                break;
            case REST_ARGS_EXPR:
                vararg = expr;
                break;
            default:
                if (i < requiredParamsCount) {
                    iExpr.requiredArgs.add(expr);
                } else {
                    iExpr.restArgs.add(expr);
                }
                i++;
                break;
        }
    }
    return checkInvocationArgs(iExpr, paramTypes, requiredParamsCount, vararg);
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 32 with BType

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

the class TypeChecker method checkExpr.

public List<BType> checkExpr(BLangExpression expr, SymbolEnv env, List<BType> expTypes, DiagnosticCode diagCode) {
    // TODO Check the possibility of using a try/finally here
    SymbolEnv prevEnv = this.env;
    List<BType> preExpTypes = this.expTypes;
    DiagnosticCode preDiagCode = this.diagCode;
    this.env = env;
    this.diagCode = diagCode;
    this.expTypes = expTypes;
    expr.accept(this);
    setExprType(expr, expTypes);
    this.env = prevEnv;
    this.expTypes = preExpTypes;
    this.diagCode = preDiagCode;
    return resultTypes;
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) DiagnosticCode(org.ballerinalang.util.diagnostic.DiagnosticCode)

Example 33 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(BLangBinaryExpr binaryExpr) {
    BType lhsType = checkExpr(binaryExpr.lhsExpr, env).get(0);
    BType rhsType = checkExpr(binaryExpr.rhsExpr, env).get(0);
    // Set error type as the actual type.
    BType actualType = symTable.errType;
    // Look up operator symbol if both rhs and lhs types are error types
    if (lhsType != symTable.errType && rhsType != symTable.errType) {
        BSymbol opSymbol = symResolver.resolveBinaryOperator(binaryExpr.opKind, lhsType, rhsType);
        if (opSymbol == symTable.notFoundSymbol) {
            dlog.error(binaryExpr.pos, DiagnosticCode.BINARY_OP_INCOMPATIBLE_TYPES, binaryExpr.opKind, lhsType, rhsType);
        } else {
            binaryExpr.opSymbol = (BOperatorSymbol) opSymbol;
            actualType = opSymbol.type.getReturnTypes().get(0);
        }
    }
    resultTypes = types.checkTypes(binaryExpr, Lists.of(actualType), expTypes);
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 34 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(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 35 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(BLangAwaitExpr awaitExpr) {
    BType expType = checkExpr(awaitExpr.expr, env, Lists.of(this.symTable.futureType)).get(0);
    if (expType == symTable.errType) {
        resultTypes = Lists.of(symTable.errType);
    } else {
        BType constraint = ((BFutureType) expType).constraint;
        resultTypes = Lists.of(constraint);
        if (constraint == symTable.noType) {
            resultTypes.clear();
        }
    }
    this.checkAsyncReturnTypes(awaitExpr, resultTypes);
}
Also used : BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BFutureType(org.wso2.ballerinalang.compiler.semantics.model.types.BFutureType)

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