Search in sources :

Example 11 with BLangUserDefinedType

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

the class BLangPackageBuilder method addTypeInitExpression.

public void addTypeInitExpression(DiagnosticPos pos, Set<Whitespace> ws, String initName, boolean typeAvailable, boolean exprAvailable) {
    BLangTypeInit objectInitNode = (BLangTypeInit) TreeBuilder.createObjectInitNode();
    objectInitNode.pos = pos;
    objectInitNode.addWS(ws);
    if (typeAvailable) {
        objectInitNode.userDefinedType = (BLangUserDefinedType) typeNodeStack.pop();
    }
    BLangInvocation invocationNode = (BLangInvocation) TreeBuilder.createInvocationNode();
    invocationNode.pos = pos;
    invocationNode.addWS(ws);
    if (exprAvailable) {
        List<ExpressionNode> exprNodes = exprNodeListStack.pop();
        Set<Whitespace> cws = commaWsStack.pop();
        exprNodes.forEach(exprNode -> {
            invocationNode.argExprs.add((BLangExpression) exprNode);
            objectInitNode.argsExpr.add((BLangExpression) exprNode);
        });
        invocationNode.addWS(cws);
        objectInitNode.addWS(cws);
    }
    // TODO check whether pkgName can be be empty
    IdentifierNode pkgNameNode = TreeBuilder.createIdentifierNode();
    IdentifierNode nameNode = createIdentifier(initName);
    BLangNameReference nameReference = new BLangNameReference(pos, ws, pkgNameNode, nameNode);
    invocationNode.name = (BLangIdentifier) nameReference.name;
    invocationNode.addWS(nameReference.ws);
    invocationNode.pkgAlias = (BLangIdentifier) nameReference.pkgAlias;
    objectInitNode.objectInitInvocation = invocationNode;
    this.addExpressionNode(objectInitNode);
}
Also used : BLangTypeInit(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit) BLangNameReference(org.wso2.ballerinalang.compiler.tree.BLangNameReference) ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) SelectExpressionNode(org.ballerinalang.model.tree.clauses.SelectExpressionNode) IdentifierNode(org.ballerinalang.model.tree.IdentifierNode) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) Whitespace(org.ballerinalang.model.Whitespace)

Example 12 with BLangUserDefinedType

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

the class Generator method generatePageForPrimitives.

/**
 * Generate the page for primitive types.
 * @param balPackage The ballerina.builtin package.
 * @param packages List of available packages.
 * @return A page model for the primitive types.
 */
public static Page generatePageForPrimitives(BLangPackage balPackage, List<Link> packages) {
    ArrayList<Documentable> primitiveTypes = new ArrayList<>();
    // Check for functions in the package
    if (balPackage.getFunctions().size() > 0) {
        for (BLangFunction function : balPackage.getFunctions()) {
            if (function.getFlags().contains(Flag.PUBLIC) && function.getReceiver() != null) {
                TypeNode langType = function.getReceiver().getTypeNode();
                if (!(langType instanceof BLangUserDefinedType)) {
                    // Check for primitives in ballerina.builtin
                    Optional<PrimitiveTypeDoc> existingPrimitiveType = primitiveTypes.stream().filter((doc) -> doc instanceof PrimitiveTypeDoc && (((PrimitiveTypeDoc) doc)).name.equals(langType.toString())).map(doc -> (PrimitiveTypeDoc) doc).findFirst();
                    PrimitiveTypeDoc primitiveTypeDoc;
                    if (existingPrimitiveType.isPresent()) {
                        primitiveTypeDoc = existingPrimitiveType.get();
                    } else {
                        primitiveTypeDoc = new PrimitiveTypeDoc(langType.toString(), new ArrayList<>());
                        primitiveTypes.add(primitiveTypeDoc);
                    }
                    primitiveTypeDoc.children.add(createDocForNode(function));
                }
            }
        }
    }
    // Create the links to select which page or package is active
    List<Link> links = new ArrayList<>();
    for (Link pkgLink : packages) {
        if (BallerinaDocConstants.PRIMITIVE_TYPES_PAGE_NAME.equals(pkgLink.caption.value)) {
            links.add(new Link(pkgLink.caption, pkgLink.href, true));
        } else {
            links.add(new Link(pkgLink.caption, pkgLink.href, false));
        }
    }
    StaticCaption primitivesPageHeading = new StaticCaption(BallerinaDocConstants.PRIMITIVE_TYPES_PAGE_NAME);
    return new Page(primitivesPageHeading, primitiveTypes, links);
}
Also used : AnnotationDoc(org.ballerinalang.docgen.model.AnnotationDoc) AnnotatableNode(org.ballerinalang.model.tree.AnnotatableNode) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType) Link(org.ballerinalang.docgen.model.Link) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) Field(org.ballerinalang.docgen.model.Field) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) ArrayList(java.util.ArrayList) Page(org.ballerinalang.docgen.model.Page) Flag(org.ballerinalang.model.elements.Flag) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) EnumNode(org.ballerinalang.model.tree.EnumNode) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) FunctionDoc(org.ballerinalang.docgen.model.FunctionDoc) ConnectorDoc(org.ballerinalang.docgen.model.ConnectorDoc) EnumDoc(org.ballerinalang.docgen.model.EnumDoc) TypeNode(org.ballerinalang.model.tree.types.TypeNode) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangAnnotAttribute(org.wso2.ballerinalang.compiler.tree.BLangAnnotAttribute) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) ActionDoc(org.ballerinalang.docgen.model.ActionDoc) StaticCaption(org.ballerinalang.docgen.model.StaticCaption) List(java.util.List) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) Variable(org.ballerinalang.docgen.model.Variable) NodeKind(org.ballerinalang.model.tree.NodeKind) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) PackageName(org.ballerinalang.docgen.model.PackageName) Optional(java.util.Optional) Documentable(org.ballerinalang.docgen.model.Documentable) GlobalVariableDoc(org.ballerinalang.docgen.model.GlobalVariableDoc) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BallerinaDocConstants(org.ballerinalang.docgen.docs.BallerinaDocConstants) PrimitiveTypeDoc(org.ballerinalang.docgen.model.PrimitiveTypeDoc) StructDoc(org.ballerinalang.docgen.model.StructDoc) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) BLangType(org.wso2.ballerinalang.compiler.tree.types.BLangType) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) ArrayList(java.util.ArrayList) Page(org.ballerinalang.docgen.model.Page) StaticCaption(org.ballerinalang.docgen.model.StaticCaption) Documentable(org.ballerinalang.docgen.model.Documentable) TypeNode(org.ballerinalang.model.tree.types.TypeNode) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType) PrimitiveTypeDoc(org.ballerinalang.docgen.model.PrimitiveTypeDoc) Link(org.ballerinalang.docgen.model.Link)

Example 13 with BLangUserDefinedType

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

the class Generator method generatePage.

/**
 * Generate the page when the bal package is passed.
 * @param balPackage The current package that is being viewed.
 * @param packages List of available packages.
 * @return A page model for the current package.
 */
public static Page generatePage(BLangPackage balPackage, List<Link> packages) {
    ArrayList<Documentable> documentables = new ArrayList<>();
    String currentPackageName = (balPackage.symbol).pkgID.name.value;
    // Check for structs in the package
    if (balPackage.getStructs().size() > 0) {
        for (BLangStruct struct : balPackage.getStructs()) {
            if (struct.getFlags().contains(Flag.PUBLIC)) {
                documentables.add(createDocForNode(struct));
            }
        }
    }
    // Check for functions in the package
    if (balPackage.getFunctions().size() > 0) {
        for (BLangFunction function : balPackage.getFunctions()) {
            if (function.getFlags().contains(Flag.PUBLIC)) {
                if (function.getReceiver() != null) {
                    if (documentables.size() > 0) {
                        for (Documentable parentDocumentable : documentables) {
                            TypeNode langType = function.getReceiver().getTypeNode();
                            String typeName = (langType instanceof BLangUserDefinedType ? ((BLangUserDefinedType) langType).typeName.value : langType.toString());
                            if (typeName.equals(parentDocumentable.name)) {
                                parentDocumentable.children.add(createDocForNode(function));
                            }
                        }
                    }
                } else {
                    // If there's no receiver type i.e. no struct binding to the function
                    documentables.add(createDocForNode(function));
                }
            }
        }
    }
    // Check for connectors in the package
    for (BLangConnector connector : balPackage.getConnectors()) {
        if (connector.getFlags().contains(Flag.PUBLIC)) {
            documentables.add(createDocForNode(connector));
        }
    }
    // Check for connectors in the package
    for (EnumNode enumNode : balPackage.getEnums()) {
        if (enumNode.getFlags().contains(Flag.PUBLIC)) {
            documentables.add(createDocForNode(enumNode));
        }
    }
    // Check for annotations
    for (BLangAnnotation annotation : balPackage.getAnnotations()) {
        if (annotation.getFlags().contains(Flag.PUBLIC)) {
            documentables.add(createDocForNode(annotation));
        }
    }
    // Check for global variables
    for (BLangVariable var : balPackage.getGlobalVariables()) {
        if (var.getFlags().contains(Flag.PUBLIC)) {
            documentables.add(createDocForNode(var));
        }
    }
    // Create the links to select which page or package is active
    List<Link> links = new ArrayList<>();
    PackageName packageNameHeading = null;
    for (Link pkgLink : packages) {
        if (pkgLink.caption.value.equals(currentPackageName)) {
            packageNameHeading = (PackageName) pkgLink.caption;
            links.add(new Link(pkgLink.caption, pkgLink.href, true));
        } else {
            links.add(new Link(pkgLink.caption, pkgLink.href, false));
        }
    }
    return new Page(packageNameHeading, documentables, links);
}
Also used : BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) PackageName(org.ballerinalang.docgen.model.PackageName) ArrayList(java.util.ArrayList) EnumNode(org.ballerinalang.model.tree.EnumNode) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) Page(org.ballerinalang.docgen.model.Page) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) Documentable(org.ballerinalang.docgen.model.Documentable) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) TypeNode(org.ballerinalang.model.tree.types.TypeNode) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) Link(org.ballerinalang.docgen.model.Link)

Example 14 with BLangUserDefinedType

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

the class ParameterContextHolder method buildContext.

/**
 * Build a readable parameter model from a Ballerina <code>VariableNode</code>.
 *
 * @param parameter {@code VariableNode} with parameter definition
 * @return built Parameter context model
 */
public static ParameterContextHolder buildContext(VariableNode parameter) {
    ParameterContextHolder context = new ParameterContextHolder();
    TypeNode type = parameter.getTypeNode();
    if (type instanceof BLangValueType) {
        context.type = ((BLangValueType) parameter.getTypeNode()).getTypeKind().typeName();
    } else if (type instanceof BLangUserDefinedType) {
        context.type = ((BLangUserDefinedType) parameter.getTypeNode()).getTypeName().getValue();
    }
    // Ignore Connection and InRequest parameters
    if (context.isIgnoredType(context.type)) {
        return null;
    }
    context.name = parameter.getName().toString();
    context.defaultValue = context.getDefaultValue(context.type, context.name);
    // examples are not yet supported
    context.example = "";
    return context;
}
Also used : BLangValueType(org.wso2.ballerinalang.compiler.tree.types.BLangValueType) TypeNode(org.ballerinalang.model.tree.types.TypeNode) BLangUserDefinedType(org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType)

Example 15 with BLangUserDefinedType

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

the class PositionTreeVisitor method visit.

@Override
public void visit(BLangUserDefinedType userDefinedType) {
    userDefinedType.getPosition().sCol += (previousNode instanceof BLangEndpointTypeNode ? "endpoint<".length() : 0);
    CommonUtil.calculateEndColumnOfGivenName(userDefinedType.getPosition(), userDefinedType.typeName.value, userDefinedType.pkgAlias.value);
    if (userDefinedType.type instanceof BUnionType && HoverUtil.isMatchingPosition(userDefinedType.getPosition(), this.position)) {
        try {
            BUnionType bUnionType = (BUnionType) userDefinedType.type;
            for (BType type : bUnionType.memberTypes) {
                if (type.tsymbol != null && type.tsymbol.getName().getValue().equals(userDefinedType.typeName.getValue())) {
                    this.context.put(NodeContextKeys.NODE_KEY, userDefinedType);
                    this.context.put(NodeContextKeys.PREVIOUSLY_VISITED_NODE_KEY, this.previousNode);
                    this.context.put(NodeContextKeys.NAME_OF_NODE_KEY, userDefinedType.typeName.getValue());
                    this.context.put(NodeContextKeys.PACKAGE_OF_NODE_KEY, type.tsymbol.pkgID);
                    this.context.put(NodeContextKeys.SYMBOL_KIND_OF_NODE_PARENT_KEY, type.tsymbol.kind.name());
                    this.context.put(NodeContextKeys.SYMBOL_KIND_OF_NODE_KEY, type.tsymbol.kind.name());
                    this.context.put(NodeContextKeys.NODE_OWNER_KEY, type.tsymbol.owner.name.getValue());
                    this.context.put(NodeContextKeys.NODE_OWNER_PACKAGE_KEY, type.tsymbol.owner.pkgID);
                    this.context.put(NodeContextKeys.VAR_NAME_OF_NODE_KEY, userDefinedType.typeName.getValue());
                    setTerminateVisitor(true);
                    break;
                }
            }
        } catch (ClassCastException e) {
        // Ignores
        }
    } else if (userDefinedType.type.tsymbol != null && HoverUtil.isMatchingPosition(userDefinedType.getPosition(), this.position)) {
        this.context.put(NodeContextKeys.NODE_KEY, userDefinedType);
        this.context.put(NodeContextKeys.PREVIOUSLY_VISITED_NODE_KEY, this.previousNode);
        this.context.put(NodeContextKeys.NAME_OF_NODE_KEY, userDefinedType.typeName.getValue());
        this.context.put(NodeContextKeys.PACKAGE_OF_NODE_KEY, userDefinedType.type.tsymbol.pkgID);
        this.context.put(NodeContextKeys.SYMBOL_KIND_OF_NODE_PARENT_KEY, userDefinedType.type.tsymbol.kind.name());
        this.context.put(NodeContextKeys.SYMBOL_KIND_OF_NODE_KEY, userDefinedType.type.tsymbol.kind.name());
        this.context.put(NodeContextKeys.NODE_OWNER_KEY, userDefinedType.type.tsymbol.owner.name.getValue());
        this.context.put(NodeContextKeys.NODE_OWNER_PACKAGE_KEY, userDefinedType.type.tsymbol.owner.pkgID);
        this.context.put(NodeContextKeys.VAR_NAME_OF_NODE_KEY, userDefinedType.typeName.getValue());
        setTerminateVisitor(true);
    }
}
Also used : BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangEndpointTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangEndpointTypeNode)

Aggregations

BLangUserDefinedType (org.wso2.ballerinalang.compiler.tree.types.BLangUserDefinedType)12 TypeNode (org.ballerinalang.model.tree.types.TypeNode)4 BLangNameReference (org.wso2.ballerinalang.compiler.tree.BLangNameReference)4 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)4 IdentifierNode (org.ballerinalang.model.tree.IdentifierNode)3 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)3 BLangAnnotation (org.wso2.ballerinalang.compiler.tree.BLangAnnotation)3 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)3 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)3 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Documentable (org.ballerinalang.docgen.model.Documentable)2 Link (org.ballerinalang.docgen.model.Link)2 PackageName (org.ballerinalang.docgen.model.PackageName)2 Page (org.ballerinalang.docgen.model.Page)2 Flag (org.ballerinalang.model.elements.Flag)2 EnumNode (org.ballerinalang.model.tree.EnumNode)2 NodeKind (org.ballerinalang.model.tree.NodeKind)2 BStructType (org.wso2.ballerinalang.compiler.semantics.model.types.BStructType)2