Search in sources :

Example 1 with BStructSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol 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 BStructSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol in project ballerina by ballerina-lang.

the class SymbolEnter method validateFunctionsAttachedToStructs.

private void validateFunctionsAttachedToStructs(BLangFunction funcNode, BInvokableSymbol funcSymbol, SymbolEnv invokableEnv) {
    BInvokableType funcType = (BInvokableType) funcSymbol.type;
    BStructSymbol structSymbol = (BStructSymbol) funcNode.receiver.type.tsymbol;
    BSymbol symbol = symResolver.lookupMemberSymbol(funcNode.receiver.pos, structSymbol.scope, invokableEnv, names.fromIdNode(funcNode.name), SymTag.VARIABLE);
    if (symbol != symTable.notFoundSymbol) {
        dlog.error(funcNode.pos, DiagnosticCode.STRUCT_FIELD_AND_FUNC_WITH_SAME_NAME, funcNode.name.value, funcNode.receiver.type.toString());
        return;
    }
    BStructType structType = (BStructType) funcNode.receiver.type;
    BAttachedFunction attachedFunc = new BAttachedFunction(names.fromIdNode(funcNode.name), funcSymbol, funcType);
    structSymbol.attachedFuncs.add(attachedFunc);
    if (funcNode.name.value.equals(structType.tsymbol.name.value + Names.INIT_FUNCTION_SUFFIX.value)) {
        structSymbol.defaultsValuesInitFunc = attachedFunc;
        return;
    }
    // Check whether this attached function is a struct initializer.
    if (!structType.tsymbol.name.value.equals(funcNode.name.value)) {
        // Not a struct initializer.
        return;
    }
    if (!funcNode.requiredParams.isEmpty() || !funcNode.retParams.isEmpty()) {
        dlog.error(funcNode.pos, DiagnosticCode.INVALID_STRUCT_INITIALIZER_FUNCTION, funcNode.name.value, funcNode.receiver.type.toString());
    }
    structSymbol.initializerFunc = attachedFunc;
}
Also used : BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BAttachedFunction(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)

Example 3 with BStructSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol in project ballerina by ballerina-lang.

the class SymbolEnter method validateFunctionsAttachedToObject.

private void validateFunctionsAttachedToObject(BLangFunction funcNode, BInvokableSymbol funcSymbol, SymbolEnv invokableEnv) {
    BInvokableType funcType = (BInvokableType) funcSymbol.type;
    BStructSymbol objectSymbol = (BStructSymbol) funcNode.receiver.type.tsymbol;
    BSymbol symbol = symResolver.lookupMemberSymbol(funcNode.receiver.pos, objectSymbol.scope, invokableEnv, names.fromIdNode(funcNode.name), SymTag.VARIABLE);
    if (symbol != symTable.notFoundSymbol) {
        dlog.error(funcNode.pos, DiagnosticCode.STRUCT_FIELD_AND_FUNC_WITH_SAME_NAME, funcNode.name.value, funcNode.receiver.type.toString());
        return;
    }
    BAttachedFunction attachedFunc = new BAttachedFunction(names.fromIdNode(funcNode.name), funcSymbol, funcType);
    objectSymbol.attachedFuncs.add(attachedFunc);
    // Check whether this attached function is a object initializer.
    if (!Names.OBJECT_INIT_SUFFIX.value.equals(funcNode.name.value)) {
        // Not a object initializer.
        return;
    }
    if (!funcNode.retParams.isEmpty()) {
        // TODO change message
        dlog.error(funcNode.pos, DiagnosticCode.INVALID_STRUCT_INITIALIZER_FUNCTION, funcNode.name.value, funcNode.receiver.type.toString());
    }
    objectSymbol.initializerFunc = attachedFunc;
}
Also used : BAttachedFunction(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)

Example 4 with BStructSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol in project ballerina by ballerina-lang.

the class EndpointSPIAnalyzer method isValidEndpointSPI.

private boolean isValidEndpointSPI(DiagnosticPos pos, BStructSymbol structSymbol) {
    if (isProcessedValidEndpoint(structSymbol)) {
        return true;
    }
    if (isProcessedInvalidEndpoint(structSymbol)) {
        // Check for cached values, to avoid duplication of endpoint related error messages.
        dlog.error(pos, DiagnosticCode.ENDPOINT_INVALID_TYPE, structSymbol);
        return false;
    }
    Endpoint ep = new Endpoint(pos, structSymbol);
    processEndpointSPIFunctions(ep);
    // Check validity of endpoint interface.
    checkValidBaseEndpointSPI(ep);
    checkValidInteractableEndpoint(ep);
    checkValidStartableEndpoint(ep);
    checkValidStoppableEndpoint(ep);
    checkValidRegistrableEndpoint(ep);
    if (invalidSPIs.containsKey(ep.structSymbol)) {
        return false;
    }
    validSPIs.put(structSymbol, ep);
    return true;
}
Also used : BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 5 with BStructSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol in project ballerina by ballerina-lang.

the class EndpointSPIAnalyzer method getEndpointTypeFromServiceType.

public BStructType getEndpointTypeFromServiceType(DiagnosticPos pos, BType type) {
    if (type.tag != TypeTags.STRUCT) {
        dlog.error(pos, DiagnosticCode.ENDPOINT_STRUCT_TYPE_REQUIRED);
        return null;
    }
    final BStructSymbol serviceType = (BStructSymbol) type.tsymbol;
    for (BStructSymbol.BAttachedFunction attachedFunc : serviceType.attachedFuncs) {
        if (Names.EP_SERVICE_GET_ENDPOINT.equals(attachedFunc.funcName)) {
            if (attachedFunc.type.getParameterTypes().size() != 0 || attachedFunc.type.retTypes.size() != 1 || attachedFunc.type.retTypes.get(0).tag != TypeTags.STRUCT) {
                dlog.error(pos, DiagnosticCode.SERVICE_INVALID_STRUCT_TYPE, serviceType);
                return null;
            }
            final BStructSymbol endPointType = (BStructSymbol) attachedFunc.type.retTypes.get(0).tsymbol;
            if (isValidEndpointSPI(pos, endPointType)) {
                return (BStructType) attachedFunc.type.retTypes.get(0);
            }
            break;
        }
    }
    dlog.error(pos, DiagnosticCode.SERVICE_INVALID_STRUCT_TYPE, serviceType);
    return null;
}
Also used : BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)

Aggregations

BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)17 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)7 BStructType (org.wso2.ballerinalang.compiler.semantics.model.types.BStructType)6 BAttachedFunction (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction)5 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)5 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)5 ArrayList (java.util.ArrayList)3 SymbolInfo (org.ballerinalang.langserver.completions.SymbolInfo)3 CompletionItem (org.eclipse.lsp4j.CompletionItem)3 List (java.util.List)2 BEndpointVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BEndpointVarSymbol)2 BTypeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)2 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)2 BJSONType (org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType)2 BStructField (org.wso2.ballerinalang.compiler.semantics.model.types.BStructType.BStructField)2 BUnionType (org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType)2 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)2 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)2 Arrays (java.util.Arrays)1 Set (java.util.Set)1