use of org.wso2.ballerinalang.compiler.semantics.analyzer.Types in project ballerina by ballerina-lang.
the class SemanticAnalyzer method handleSafeAssignment.
private void handleSafeAssignment(DiagnosticPos lhsPos, BType lhsType, BLangExpression rhsExpr, SymbolEnv env) {
// Collect all the lhs types
Set<BType> lhsTypes = lhsType.tag == TypeTags.UNION ? ((BUnionType) lhsType).memberTypes : new HashSet<BType>() {
{
add(lhsType);
}
};
// If there is at least one lhs type which assignable to the error type, then report an error.
for (BType type : lhsTypes) {
if (types.isAssignable(symTable.errStructType, type)) {
dlog.error(lhsPos, DiagnosticCode.SAFE_ASSIGN_STMT_INVALID_USAGE);
typeChecker.checkExpr(rhsExpr, env, Lists.of(symTable.errType));
return;
}
}
// Create a new union type with the error type and continue the type checking process.
lhsTypes.add(symTable.errStructType);
BUnionType lhsUnionType = new BUnionType(null, lhsTypes, lhsTypes.contains(symTable.nullType));
typeChecker.checkExpr(rhsExpr, env, Lists.of(lhsUnionType));
if (rhsExpr.type.tag == TypeTags.UNION) {
BUnionType rhsUnionType = (BUnionType) rhsExpr.type;
for (BType type : rhsUnionType.memberTypes) {
if (types.isAssignable(symTable.errStructType, type)) {
return;
}
}
} else if (rhsExpr.type.tag != TypeTags.ERROR) {
dlog.error(rhsExpr.pos, DiagnosticCode.SAFE_ASSIGN_STMT_INVALID_USAGE);
}
}
use of org.wso2.ballerinalang.compiler.semantics.analyzer.Types 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.analyzer.Types 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.analyzer.Types in project ballerina by ballerina-lang.
the class Types method isArrayTypesAssignable.
public boolean isArrayTypesAssignable(BType source, BType target) {
if (target.tag == TypeTags.ARRAY && source.tag == TypeTags.ARRAY) {
// Both types are array types
BArrayType lhsArrayType = (BArrayType) target;
BArrayType rhsArrayType = (BArrayType) source;
return isArrayTypesAssignable(rhsArrayType.eType, lhsArrayType.eType);
} else if (source.tag == TypeTags.ARRAY) {
// to JSON.
if (target.tag == TypeTags.JSON) {
return getElementType(source).tag == TypeTags.JSON;
}
// Then lhs type should 'any' type
return target.tag == TypeTags.ANY;
} else if (target.tag == TypeTags.ARRAY) {
// Only the left-hand side is an array type
return false;
}
// Now both types are not array types and they have to be equal
if (target == source) {
// TODO Figure out this.
return true;
}
// In this case, lhs type should be of type 'any' and the rhs type cannot be a value type
return target.tag == TypeTags.ANY && !isValueType(source);
}
use of org.wso2.ballerinalang.compiler.semantics.analyzer.Types in project ballerina by ballerina-lang.
the class Symbols method createTypeofOperatorSymbol.
public static BOperatorSymbol createTypeofOperatorSymbol(BType exprType, Types types, SymbolTable symTable, Names names) {
List<BType> paramTypes = Lists.of(exprType);
List<BType> retTypes = Lists.of(symTable.typeDesc);
BInvokableType opType = new BInvokableType(paramTypes, retTypes, null);
if (types.isValueType(exprType)) {
return new BOperatorSymbol(names.fromString(OperatorKind.TYPEOF.value()), symTable.rootPkgSymbol.pkgID, opType, symTable.rootPkgSymbol, InstructionCodes.TYPELOAD);
} else {
return new BOperatorSymbol(names.fromString(OperatorKind.TYPEOF.value()), symTable.rootPkgSymbol.pkgID, opType, symTable.rootPkgSymbol, InstructionCodes.TYPEOF);
}
}
Aggregations