Search in sources :

Example 1 with BLangFunction

use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.

the class SignatureHelpUtil method getSignatureInfoModel.

/**
 * Get the required signature information filled model.
 *
 * @param bInvokableSymbol                  Invokable symbol
 * @param signatureContext                  Signature operation context
 * @return {@link SignatureInfoModel}       SignatureInfoModel containing signature information
 */
private static SignatureInfoModel getSignatureInfoModel(BInvokableSymbol bInvokableSymbol, TextDocumentServiceContext signatureContext) {
    Map<String, String> paramDescMap = new HashMap<>();
    SignatureInfoModel signatureInfoModel = new SignatureInfoModel();
    List<ParameterInfoModel> paramModels = new ArrayList<>();
    String functionName = signatureContext.get(SignatureKeys.CALLABLE_ITEM_NAME);
    CompilerContext compilerContext = signatureContext.get(DocumentServiceKeys.COMPILER_CONTEXT_KEY);
    BLangPackage bLangPackage = signatureContext.get(DocumentServiceKeys.LS_PACKAGE_CACHE_KEY).findPackage(compilerContext, bInvokableSymbol.pkgID);
    BLangFunction blangFunction = bLangPackage.getFunctions().stream().filter(bLangFunction -> bLangFunction.getName().getValue().equals(functionName)).findFirst().orElse(null);
    if (!blangFunction.getDocumentationAttachments().isEmpty()) {
        // Get the first documentation attachment
        BLangDocumentation bLangDocumentation = blangFunction.getDocumentationAttachments().get(0);
        signatureInfoModel.setSignatureDescription(bLangDocumentation.documentationText.trim());
        bLangDocumentation.attributes.forEach(attribute -> {
            if (attribute.docTag.equals(DocTag.PARAM)) {
                paramDescMap.put(attribute.documentationField.getValue(), attribute.documentationText.trim());
            }
        });
    } else {
        // TODO: Should be deprecated in due course
        // Iterate over the attachments list and extract the attachment Description Map
        blangFunction.getAnnotationAttachments().forEach(annotationAttachment -> {
            BLangExpression expr = annotationAttachment.expr;
            if (expr instanceof BLangRecordLiteral) {
                List<BLangRecordLiteral.BLangRecordKeyValue> recordKeyValues = ((BLangRecordLiteral) expr).keyValuePairs;
                for (BLangRecordLiteral.BLangRecordKeyValue recordKeyValue : recordKeyValues) {
                    BLangExpression key = recordKeyValue.key.expr;
                    BLangExpression value = recordKeyValue.getValue();
                    if (key instanceof BLangSimpleVarRef && ((BLangSimpleVarRef) key).getVariableName().getValue().equals("value") && value instanceof BLangLiteral) {
                        String annotationValue = ((BLangLiteral) value).getValue().toString();
                        if (annotationAttachment.getAnnotationName().getValue().equals("Param")) {
                            String paramName = annotationValue.substring(0, annotationValue.indexOf(UtilSymbolKeys.PKG_DELIMITER_KEYWORD));
                            String annotationDesc = annotationValue.substring(annotationValue.indexOf(UtilSymbolKeys.PKG_DELIMITER_KEYWORD) + 1).trim();
                            paramDescMap.put(paramName, annotationDesc);
                        } else if (annotationAttachment.getAnnotationName().getValue().equals("Description")) {
                            signatureInfoModel.setSignatureDescription(annotationValue);
                        }
                    }
                }
            }
        });
    }
    bInvokableSymbol.getParameters().forEach(bVarSymbol -> {
        ParameterInfoModel parameterInfoModel = new ParameterInfoModel();
        parameterInfoModel.setParamType(bVarSymbol.getType().toString());
        parameterInfoModel.setParamValue(bVarSymbol.getName().getValue());
        parameterInfoModel.setDescription(paramDescMap.get(bVarSymbol.getName().getValue()));
        paramModels.add(parameterInfoModel);
    });
    signatureInfoModel.setParameterInfoModels(paramModels);
    return signatureInfoModel;
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) HashMap(java.util.HashMap) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 2 with BLangFunction

use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.

the class SignatureTreeVisitor method visit.

@Override
public void visit(BLangFunction funcNode) {
    BSymbol funcSymbol = funcNode.symbol;
    if (Symbols.isNative(funcSymbol)) {
        return;
    }
    SymbolEnv funcEnv = SymbolEnv.createFunctionEnv(funcNode, funcSymbol.scope, symbolEnv);
    blockOwnerStack.push(funcNode);
    this.acceptNode(funcNode.body, funcEnv);
    blockOwnerStack.pop();
    // Process workers
    if (terminateVisitor && !funcNode.workers.isEmpty()) {
        terminateVisitor = false;
    }
    funcNode.workers.forEach(e -> this.symbolEnter.defineNode(e, funcEnv));
    funcNode.workers.forEach(e -> this.acceptNode(e, funcEnv));
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Example 3 with BLangFunction

use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.

the class CommandExecutor method getDocumentEditForNode.

/**
 * Get the document edit attachment info for a given particular node.
 * @param node      Node given
 * @return          Doc Attachment Info
 */
private static CommandUtil.DocAttachmentInfo getDocumentEditForNode(Node node) {
    CommandUtil.DocAttachmentInfo docAttachmentInfo = null;
    int replaceFrom;
    switch(node.getKind()) {
        case FUNCTION:
            if (((BLangFunction) node).docAttachments.isEmpty()) {
                replaceFrom = CommonUtil.toZeroBasedPosition(((BLangFunction) node).getPosition()).getStartLine();
                docAttachmentInfo = CommandUtil.getFunctionNodeDocumentation((BLangFunction) node, replaceFrom);
            }
            break;
        case STRUCT:
            if (((BLangStruct) node).docAttachments.isEmpty()) {
                replaceFrom = CommonUtil.toZeroBasedPosition(((BLangStruct) node).getPosition()).getStartLine();
                docAttachmentInfo = CommandUtil.getStructNodeDocumentation((BLangStruct) node, replaceFrom);
            }
            break;
        case ENUM:
            if (((BLangEnum) node).docAttachments.isEmpty()) {
                replaceFrom = CommonUtil.toZeroBasedPosition(((BLangEnum) node).getPosition()).getStartLine();
                docAttachmentInfo = CommandUtil.getEnumNodeDocumentation((BLangEnum) node, replaceFrom);
            }
            break;
        case TRANSFORMER:
            if (((BLangTransformer) node).docAttachments.isEmpty()) {
                replaceFrom = CommonUtil.toZeroBasedPosition(((BLangTransformer) node).getPosition()).getStartLine();
                docAttachmentInfo = CommandUtil.getTransformerNodeDocumentation((BLangTransformer) node, replaceFrom);
            }
            break;
        case RESOURCE:
            if (((BLangResource) node).docAttachments.isEmpty()) {
                BLangResource bLangResource = (BLangResource) node;
                replaceFrom = getReplaceFromForServiceOrResource(bLangResource, bLangResource.getAnnotationAttachments());
                docAttachmentInfo = CommandUtil.getResourceNodeDocumentation(bLangResource, replaceFrom);
            }
            break;
        case SERVICE:
            if (((BLangService) node).docAttachments.isEmpty()) {
                BLangService bLangService = (BLangService) node;
                replaceFrom = getReplaceFromForServiceOrResource(bLangService, bLangService.getAnnotationAttachments());
                docAttachmentInfo = CommandUtil.getServiceNodeDocumentation(bLangService, replaceFrom);
            }
            break;
        default:
            break;
    }
    return docAttachmentInfo;
}
Also used : BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum)

Example 4 with BLangFunction

use of org.wso2.ballerinalang.compiler.tree.BLangFunction 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 5 with BLangFunction

use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.

the class SymbolEnter method defineObjectInitFunction.

private void defineObjectInitFunction(BLangObject object, SymbolEnv conEnv) {
    BLangFunction initFunction = object.initFunction;
    if (initFunction == null) {
        initFunction = createInitFunction(object.pos, "", Names.OBJECT_INIT_SUFFIX);
    }
    initFunction.attachedFunction = true;
    // Set cached receiver to the init function
    initFunction.receiver = object.receiver;
    initFunction.flagSet.add(Flag.ATTACHED);
    // Add object level variables to the init function
    BLangFunction finalInitFunction = initFunction;
    object.fields.stream().filter(f -> f.expr != null).forEachOrdered(v -> finalInitFunction.initFunctionStmts.put(v.symbol, (BLangStatement) createAssignmentStmt(v)));
    object.initFunction = initFunction;
    defineNode(object.initFunction, conEnv);
}
Also used : BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn) BAttachedFunction(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction) BLangIdentifier(org.wso2.ballerinalang.compiler.tree.BLangIdentifier) BConnectorType(org.wso2.ballerinalang.compiler.semantics.model.types.BConnectorType) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) Flag(org.ballerinalang.model.elements.Flag) IdentifierNode(org.ballerinalang.model.tree.IdentifierNode) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) BAnnotationSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationSymbol) Names(org.wso2.ballerinalang.compiler.util.Names) BServiceSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BServiceSymbol) BEnumType(org.wso2.ballerinalang.compiler.semantics.model.types.BEnumType) EnumSet(java.util.EnumSet) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangDiagnosticLog(org.wso2.ballerinalang.compiler.util.diagnotic.BLangDiagnosticLog) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BXMLNSSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol) BConnectorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BConnectorSymbol) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) CompilerPhase(org.ballerinalang.compiler.CompilerPhase) BXMLAttributeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLAttributeSymbol) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) PackageID(org.ballerinalang.model.elements.PackageID) Set(java.util.Set) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) BLangXMLNS(org.wso2.ballerinalang.compiler.tree.BLangXMLNS) BLangAnnotAttribute(org.wso2.ballerinalang.compiler.tree.BLangAnnotAttribute) Collectors(java.util.stream.Collectors) BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) List(java.util.List) IMPORT(org.ballerinalang.model.tree.NodeKind.IMPORT) Scope(org.wso2.ballerinalang.compiler.semantics.model.Scope) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) NodeKind(org.ballerinalang.model.tree.NodeKind) BAnnotationAttributeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationAttributeSymbol) StatementNode(org.ballerinalang.model.tree.statements.StatementNode) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) BLangWorker(org.wso2.ballerinalang.compiler.tree.BLangWorker) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangXMLNSStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangXMLNSStatement) BLangInvokableNode(org.wso2.ballerinalang.compiler.tree.BLangInvokableNode) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) BStructField(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType.BStructField) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType) BAnnotationType(org.wso2.ballerinalang.compiler.semantics.model.types.BAnnotationType) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangExpressionStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) BLangXMLAttribute(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttribute) BLangStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangStatement) BLangNodeVisitor(org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor) PackageLoader(org.wso2.ballerinalang.compiler.PackageLoader) TypeTags(org.wso2.ballerinalang.compiler.util.TypeTags) BServiceType(org.wso2.ballerinalang.compiler.semantics.model.types.BServiceType) Flags(org.wso2.ballerinalang.util.Flags) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) DocTag(org.ballerinalang.model.elements.DocTag) TreeBuilder(org.ballerinalang.model.TreeBuilder) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) XMLConstants(javax.xml.XMLConstants) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BTransformerSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTransformerSymbol) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Symbols(org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) Name(org.wso2.ballerinalang.compiler.util.Name) SymTag(org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag) DiagnosticCode(org.ballerinalang.util.diagnostic.DiagnosticCode) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangEnumerator(org.wso2.ballerinalang.compiler.tree.BLangEnum.BLangEnumerator) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BEndpointVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BEndpointVarSymbol) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) BLangStatement(org.wso2.ballerinalang.compiler.tree.statements.BLangStatement) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction)

Aggregations

BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)37 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)24 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)16 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)16 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)15 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)15 ArrayList (java.util.ArrayList)14 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)14 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)13 List (java.util.List)11 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)10 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)10 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)10 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)9 BLangEnum (org.wso2.ballerinalang.compiler.tree.BLangEnum)9 BLangObject (org.wso2.ballerinalang.compiler.tree.BLangObject)9 BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)9 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)9 Collectors (java.util.stream.Collectors)8 NodeKind (org.ballerinalang.model.tree.NodeKind)8