use of org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType in project ballerina by ballerina-lang.
the class BLangMatchContextResolver method resolveItems.
@Override
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
ArrayList<CompletionItem> completionItems = new ArrayList<>();
BLangNode symbolEnvNode = completionContext.get(CompletionKeys.SYMBOL_ENV_NODE_KEY);
List<SymbolInfo> visibleSymbols = completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY);
if (!(symbolEnvNode instanceof BLangMatch)) {
return completionItems;
}
BLangMatch bLangMatch = (BLangMatch) symbolEnvNode;
if (bLangMatch.expr.type instanceof BUnionType) {
Set<BType> memberTypes = ((BUnionType) ((BLangSimpleVarRef) bLangMatch.expr).type).getMemberTypes();
memberTypes.forEach(bType -> {
completionItems.add(this.populateCompletionItem(bType.toString(), ItemResolverConstants.B_TYPE, bType.toString()));
});
} else if (bLangMatch.expr.type instanceof BJSONType) {
ArrayList<Integer> typeTagsList = new ArrayList<>(Arrays.asList(TypeTags.INT, TypeTags.FLOAT, TypeTags.BOOLEAN, TypeTags.STRING, TypeTags.NULL, TypeTags.JSON));
List<SymbolInfo> filteredBasicTypes = visibleSymbols.stream().filter(symbolInfo -> {
BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
return bSymbol instanceof BTypeSymbol && typeTagsList.contains(bSymbol.getType().tag);
}).collect(Collectors.toList());
this.populateCompletionItemList(filteredBasicTypes, completionItems);
} else {
if (bLangMatch.expr.type instanceof BStructType) {
List<SymbolInfo> structSymbols = visibleSymbols.stream().filter(symbolInfo -> {
BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
return bSymbol instanceof BStructSymbol && !bSymbol.getName().getValue().startsWith(UtilSymbolKeys.ANON_STRUCT_CHECKER);
}).collect(Collectors.toList());
this.populateCompletionItemList(structSymbols, completionItems);
}
}
return completionItems;
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType in project ballerina by ballerina-lang.
the class SymbolResolver method visit.
public void visit(BLangUnionTypeNode unionTypeNode) {
Set<BType> memberTypes = unionTypeNode.memberTypeNodes.stream().map(memTypeNode -> resolveTypeNode(memTypeNode, env)).flatMap(memBType -> memBType.tag == TypeTags.UNION ? ((BUnionType) memBType).memberTypes.stream() : Stream.of(memBType)).collect(Collectors.toSet());
resultType = new BUnionType(null, memberTypes, memberTypes.contains(symTable.nullType));
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType in project ballerina by ballerina-lang.
the class SemanticAnalyzer method handleSafeAssignmentWithVarDeclaration.
private BType handleSafeAssignmentWithVarDeclaration(DiagnosticPos pos, BType rhsType) {
if (rhsType.tag != TypeTags.UNION && types.isAssignable(symTable.errStructType, rhsType)) {
dlog.error(pos, DiagnosticCode.SAFE_ASSIGN_STMT_INVALID_USAGE);
return symTable.errType;
} else if (rhsType.tag != TypeTags.UNION) {
return rhsType;
}
// Collect all the rhs types from the union type
boolean isErrorFound = false;
BUnionType unionType = (BUnionType) rhsType;
Set<BType> rhsTypeSet = new HashSet<>(unionType.memberTypes);
for (BType type : unionType.memberTypes) {
if (types.isAssignable(type, symTable.errStructType)) {
rhsTypeSet.remove(type);
isErrorFound = true;
}
}
if (rhsTypeSet.isEmpty() || !isErrorFound) {
dlog.error(pos, DiagnosticCode.SAFE_ASSIGN_STMT_INVALID_USAGE);
return symTable.noType;
} else if (rhsTypeSet.size() == 1) {
return rhsTypeSet.toArray(new BType[0])[0];
}
return new BUnionType(null, rhsTypeSet, rhsTypeSet.contains(symTable.nullType));
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType in project ballerina by ballerina-lang.
the class Types method isAssignableToUnionType.
private boolean isAssignableToUnionType(BType source, BType target) {
Set<BType> sourceTypes = new HashSet<>();
Set<BType> targetTypes = new HashSet<>();
if (source.tag == TypeTags.UNION) {
BUnionType sourceUnionType = (BUnionType) source;
sourceTypes.addAll(sourceUnionType.memberTypes);
} else {
sourceTypes.add(source);
}
if (target.tag == TypeTags.UNION) {
BUnionType targetUnionType = (BUnionType) target;
targetTypes.addAll(targetUnionType.memberTypes);
} else {
targetTypes.add(target);
}
boolean notAssignable = sourceTypes.stream().map(s -> targetTypes.stream().anyMatch(t -> isAssignable(s, t))).anyMatch(assignable -> !assignable);
return !notAssignable;
}
use of org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType in project ballerina by ballerina-lang.
the class Symbols method createConversionOperatorSymbol.
public static BConversionOperatorSymbol createConversionOperatorSymbol(final BType sourceType, final BType targetType, final BType errorType, boolean implicit, boolean safe, int opcode, PackageID pkgID, BSymbol owner) {
List<BType> paramTypes = Lists.of(sourceType, targetType);
List<BType> retTypes = new ArrayList<>(1);
if (safe) {
retTypes.add(targetType);
} else if (targetType.tag == TypeTags.UNION && targetType instanceof BUnionType) {
BUnionType unionType = (BUnionType) targetType;
unionType.memberTypes.add(errorType);
retTypes.add(unionType);
} else {
Set<BType> memberTypes = new HashSet<>(2);
memberTypes.add(targetType);
memberTypes.add(errorType);
BUnionType unionType = new BUnionType(null, memberTypes, false);
retTypes.add(unionType);
}
BInvokableType opType = new BInvokableType(paramTypes, retTypes, null);
BConversionOperatorSymbol symbol = new BConversionOperatorSymbol(pkgID, opType, owner, implicit, safe, opcode);
symbol.kind = SymbolKind.CONVERSION_OPERATOR;
return symbol;
}
Aggregations