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));
}
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;
}
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);
}
}
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);
}
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;
}
Aggregations