Search in sources :

Example 1 with BUnionType

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;
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) ArrayList(java.util.ArrayList) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) CompletionItem(org.eclipse.lsp4j.CompletionItem) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangMatch(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with BUnionType

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));
}
Also used : BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) BLangTupleTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangTupleTypeNode) Lists(org.wso2.ballerinalang.util.Lists) BLangBuiltInRefTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangBuiltInRefTypeNode) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) BLangValueType(org.wso2.ballerinalang.compiler.tree.types.BLangValueType) OperatorKind(org.ballerinalang.model.tree.OperatorKind) Flag(org.ballerinalang.model.elements.Flag) Map(java.util.Map) SymbolKind(org.ballerinalang.model.symbols.SymbolKind) BOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol) Names(org.wso2.ballerinalang.compiler.util.Names) EnumSet(java.util.EnumSet) BLangDiagnosticLog(org.wso2.ballerinalang.compiler.util.diagnotic.BLangDiagnosticLog) BXMLNSSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol) BLangArrayType(org.wso2.ballerinalang.compiler.tree.types.BLangArrayType) BLangUnionTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangUnionTypeNode) TypeKind(org.ballerinalang.model.types.TypeKind) BConversionOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) NOT_FOUND_ENTRY(org.wso2.ballerinalang.compiler.semantics.model.Scope.NOT_FOUND_ENTRY) Scope(org.wso2.ballerinalang.compiler.semantics.model.Scope) Stream(java.util.stream.Stream) InstructionCodes(org.wso2.ballerinalang.programfile.InstructionCodes) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType) BStreamType(org.wso2.ballerinalang.compiler.semantics.model.types.BStreamType) HashMap(java.util.HashMap) BLangNodeVisitor(org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor) ArrayList(java.util.ArrayList) TypeTags(org.wso2.ballerinalang.compiler.util.TypeTags) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) BLangConstrainedType(org.wso2.ballerinalang.compiler.tree.types.BLangConstrainedType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BFutureType(org.wso2.ballerinalang.compiler.semantics.model.types.BFutureType) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) Symbols(org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols) BLangFunctionTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangFunctionTypeNode) Name(org.wso2.ballerinalang.compiler.util.Name) ScopeEntry(org.wso2.ballerinalang.compiler.semantics.model.Scope.ScopeEntry) SymTag(org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag) DiagnosticCode(org.ballerinalang.util.diagnostic.DiagnosticCode) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType) BLangType(org.wso2.ballerinalang.compiler.tree.types.BLangType) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType)

Example 3 with BUnionType

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));
}
Also used : BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) HashSet(java.util.HashSet)

Example 4 with BUnionType

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;
}
Also used : BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BStructField(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType.BStructField) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BTupleType(org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType) BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) BLangTypeConversionExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr) BAttachedFunction(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction) BStreamType(org.wso2.ballerinalang.compiler.semantics.model.types.BStreamType) BBuiltInRefType(org.wso2.ballerinalang.compiler.semantics.model.types.BBuiltInRefType) Lists(org.wso2.ballerinalang.util.Lists) BConnectorType(org.wso2.ballerinalang.compiler.semantics.model.types.BConnectorType) ArrayList(java.util.ArrayList) BXMLType(org.wso2.ballerinalang.compiler.semantics.model.types.BXMLType) TypeTags(org.wso2.ballerinalang.compiler.util.TypeTags) HashSet(java.util.HashSet) Flags(org.wso2.ballerinalang.util.Flags) TreeBuilder(org.ballerinalang.model.TreeBuilder) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) Names(org.wso2.ballerinalang.compiler.util.Names) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BErrorType(org.wso2.ballerinalang.compiler.semantics.model.types.BErrorType) BFutureType(org.wso2.ballerinalang.compiler.semantics.model.types.BFutureType) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) BEnumType(org.wso2.ballerinalang.compiler.semantics.model.types.BEnumType) BLangDiagnosticLog(org.wso2.ballerinalang.compiler.util.diagnotic.BLangDiagnosticLog) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BConversionOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) Set(java.util.Set) BTypeVisitor(org.wso2.ballerinalang.compiler.semantics.model.types.BTypeVisitor) Symbols(org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols) Collectors(java.util.stream.Collectors) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) List(java.util.List) DiagnosticCode(org.ballerinalang.util.diagnostic.DiagnosticCode) InstructionCodes(org.wso2.ballerinalang.programfile.InstructionCodes) BAnyType(org.wso2.ballerinalang.compiler.semantics.model.types.BAnyType) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) Collections(java.util.Collections) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) HashSet(java.util.HashSet)

Example 5 with BUnionType

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;
}
Also used : BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) Set(java.util.Set) HashSet(java.util.HashSet) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) ArrayList(java.util.ArrayList) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)

Aggregations

BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)13 BUnionType (org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType)13 ArrayList (java.util.ArrayList)7 HashSet (java.util.HashSet)6 Set (java.util.Set)6 List (java.util.List)5 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)5 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)5 Collectors (java.util.stream.Collectors)4 BConversionOperatorSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BConversionOperatorSymbol)4 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)4 BStructType (org.wso2.ballerinalang.compiler.semantics.model.types.BStructType)4 TypeTags (org.wso2.ballerinalang.compiler.util.TypeTags)4 DiagnosticCode (org.ballerinalang.util.diagnostic.DiagnosticCode)3 BJSONType (org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType)3 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 EnumSet (java.util.EnumSet)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2