Search in sources :

Example 96 with BType

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

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

the class TypeChecker method checkUnNamedTransformerInvocation.

private List<BType> checkUnNamedTransformerInvocation(BLangTypeConversionExpr conversionExpr, BType sourceType, BType targetType) {
    List<BType> actualTypes = getListWithErrorTypes(expTypes.size());
    // Check whether a transformer is available for the two types
    BSymbol symbol = symResolver.resolveTransformer(env, sourceType, targetType);
    if (symbol == symTable.notFoundSymbol) {
        // check whether a casting is possible, to provide user a hint.
        BSymbol castSymbol = symResolver.resolveConversionOperator(sourceType, targetType);
        if (castSymbol == symTable.notFoundSymbol) {
            dlog.error(conversionExpr.pos, DiagnosticCode.INCOMPATIBLE_TYPES_CONVERSION, sourceType, targetType);
        } else {
            dlog.error(conversionExpr.pos, DiagnosticCode.INCOMPATIBLE_TYPES_CONVERSION_WITH_SUGGESTION, sourceType, targetType);
        }
    } else {
        BTransformerSymbol transformerSymbol = (BTransformerSymbol) symbol;
        conversionExpr.conversionSymbol = transformerSymbol;
        if (conversionExpr.conversionSymbol.safe) {
            ((BInvokableType) transformerSymbol.type).retTypes.add(symTable.errStructType);
        }
        actualTypes = getActualTypesOfConversionExpr(conversionExpr, targetType, sourceType, transformerSymbol);
    }
    return actualTypes;
}
Also used : BTransformerSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTransformerSymbol) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 98 with BType

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

Example 99 with BType

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

the class Types method checkArrayEquality.

public boolean checkArrayEquality(BType source, BType target) {
    if (target.tag == TypeTags.ARRAY && source.tag == TypeTags.ARRAY) {
        // Both types are array types
        BArrayType lhrArrayType = (BArrayType) target;
        BArrayType rhsArrayType = (BArrayType) source;
        return checkArrayEquality(lhrArrayType.eType, rhsArrayType.eType);
    }
    // Now one or both types are not array types and they have to be equal
    return isSameType(source, target);
}
Also used : BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType)

Example 100 with BType

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

the class Types method checkForeachTypes.

List<BType> checkForeachTypes(BLangNode collection, int variableSize) {
    BType collectionType = collection.type;
    List<BType> errorTypes;
    int maxSupportedTypes;
    switch(collectionType.tag) {
        case TypeTags.ARRAY:
            BArrayType bArrayType = (BArrayType) collectionType;
            if (variableSize == 1) {
                return Lists.of(bArrayType.eType);
            } else if (variableSize == 2) {
                return Lists.of(symTable.intType, bArrayType.eType);
            } else {
                maxSupportedTypes = 2;
                errorTypes = Lists.of(symTable.intType, bArrayType.eType);
            }
            break;
        case TypeTags.MAP:
            BMapType bMapType = (BMapType) collectionType;
            if (variableSize == 1) {
                return Lists.of(bMapType.constraint);
            } else if (variableSize == 2) {
                return Lists.of(symTable.stringType, bMapType.constraint);
            } else {
                maxSupportedTypes = 2;
                errorTypes = Lists.of(symTable.stringType, bMapType.constraint);
            }
            break;
        case TypeTags.JSON:
            if (variableSize == 1) {
                return Lists.of(symTable.jsonType);
            } else {
                maxSupportedTypes = 1;
                errorTypes = Lists.of(symTable.jsonType);
            }
            break;
        case TypeTags.XML:
            if (variableSize == 1) {
                return Lists.of(symTable.xmlType);
            } else if (variableSize == 2) {
                return Lists.of(symTable.intType, symTable.xmlType);
            } else {
                maxSupportedTypes = 2;
                errorTypes = Lists.of(symTable.intType, symTable.xmlType);
            }
            break;
        case TypeTags.TABLE:
            BTableType tableType = (BTableType) collectionType;
            if (variableSize == 1) {
                return Lists.of(tableType.constraint);
            } else {
                maxSupportedTypes = 1;
                errorTypes = Lists.of(tableType.constraint);
            }
            break;
        case TypeTags.ERROR:
            return Collections.nCopies(variableSize, symTable.errType);
        default:
            dlog.error(collection.pos, DiagnosticCode.ITERABLE_NOT_SUPPORTED_COLLECTION, collectionType);
            return Collections.nCopies(variableSize, symTable.errType);
    }
    dlog.error(collection.pos, DiagnosticCode.ITERABLE_TOO_MANY_VARIABLES, collectionType);
    errorTypes.addAll(Collections.nCopies(variableSize - maxSupportedTypes, symTable.errType));
    return errorTypes;
}
Also used : BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType)

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