Search in sources :

Example 1 with IASTNamedTypeSpecifier

use of org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier in project Cpp4CIA by thanhminhmr.

the class AstBuilder method createFromDeclSpecifier.

@Nonnull
private CppNode createFromDeclSpecifier(@Nonnull CppNode parentNode, @Nonnull IASTDeclSpecifier declSpecifier) {
    final String signature = ASTStringUtil.getSignatureString(declSpecifier, null);
    if (declSpecifier instanceof ICPPASTEnumerationSpecifier) {
        // region Enumeration
        final ICPPASTEnumerationSpecifier enumerationSpecifier = (ICPPASTEnumerationSpecifier) declSpecifier;
        final IASTName enumerationName = enumerationSpecifier.getName();
        final IBinding enumerationBinding = enumerationName.resolveBinding();
        final CppNode enumNode = createNode(enumerationBinding, enumerationName, signature, new EnumNode(), parentNode);
        if (enumNode instanceof EnumNode) {
            final ICPPASTDeclSpecifier enumBaseType = enumerationSpecifier.getBaseType();
            final CppNode baseType = enumBaseType != null ? createFromDeclSpecifier(parentNode, enumBaseType) : null;
            final CppNode nodeType = enumerationSpecifier.isScoped() ? enumNode : baseType;
            if (baseType != null) {
                ((EnumNode) enumNode).setType(baseType);
            // enumNode.addDependencyTo(baseType, DependencyType.USE);
            }
            final StringBuilder bodyBuilder = enumNode.getName().isBlank() ? new StringBuilder() : null;
            for (final IASTEnumerationSpecifier.IASTEnumerator enumerator : enumerationSpecifier.getEnumerators()) {
                final IASTName enumeratorName = enumerator.getName();
                final IBinding enumeratorBinding = enumeratorName.resolveBinding();
                final CppNode enumeratorNode = createNode(enumeratorBinding, enumeratorName, null, new VariableNode(), enumNode);
                if (enumeratorNode.getParent() == null) {
                    enumNode.addChild(enumeratorNode);
                    enumNode.addDependencyTo(enumeratorNode, DependencyType.MEMBER);
                } else {
                    parentNode.addDependencyTo(enumNode, DependencyType.USE);
                }
                if (enumeratorNode instanceof VariableNode) {
                    if (nodeType != null) {
                        ((VariableNode) enumeratorNode).setType(nodeType);
                    // enumeratorNode.addDependencyTo(nodeType, DependencyType.USE);
                    }
                    final IASTExpression expression = enumerator.getValue();
                    if (expression != null) {
                        ((VariableNode) enumeratorNode).setBody(expression.getRawSignature());
                        childrenCreationQueue.add(Pair.mutableOf(enumeratorNode, expression));
                    }
                }
                if (bodyBuilder != null) {
                    bodyBuilder.append(bodyBuilder.length() > 0 ? ',' : "enum{").append(enumeratorNode.getName());
                }
            }
            if (bodyBuilder != null) {
                enumNode.setName(bodyBuilder.append('}').toString());
            }
        }
        // endregion
        return enumNode;
    } else if (declSpecifier instanceof ICPPASTCompositeTypeSpecifier) {
        // region Class, Struct, Union
        final ICPPASTCompositeTypeSpecifier classSpecifier = (ICPPASTCompositeTypeSpecifier) declSpecifier;
        final IASTName className = classSpecifier.getName();
        final CppNode classNode = createNode(className.resolveBinding(), className, signature, new ClassNode(), parentNode);
        if (classNode instanceof ClassNode) {
            for (final ICPPASTBaseSpecifier classBaseSpecifier : classSpecifier.getBaseSpecifiers()) {
                final ICPPASTNameSpecifier classBaseNameSpecifier = classBaseSpecifier.getNameSpecifier();
                final IBinding classBaseNameBinding = classBaseNameSpecifier.resolveBinding();
                final CppNode classBaseNode = createUnknownNode(parentNode, classBaseNameBinding, classBaseNameBinding.getName(), true);
                ((ClassNode) classNode).addBase(classBaseNode);
            // classNode.addDependencyTo(classBaseNode, DependencyType.INHERITANCE);
            }
            final StringBuilder bodyBuilder = classNode.getName().isBlank() ? new StringBuilder().append(classNode.getSignature()).append('{') : null;
            for (final IASTDeclaration classChildDeclaration : classSpecifier.getDeclarations(false)) {
                final List<CppNode> nodeList = createChildrenFromDeclaration(classNode, classChildDeclaration);
                if (bodyBuilder != null) {
                    for (final CppNode node : nodeList) bodyBuilder.append(node.getName()).append(';');
                }
            }
            if (bodyBuilder != null)
                classNode.setName(bodyBuilder.append('}').toString());
        }
        // endregion
        return classNode;
    } else if (declSpecifier instanceof ICPPASTNamedTypeSpecifier) {
        // region Typename Type
        final IASTName namedName = ((IASTNamedTypeSpecifier) declSpecifier).getName();
        return createUnknownNode(parentNode, namedName.resolveBinding(), namedName.toString(), true);
    // endregion
    } else if (declSpecifier instanceof ICPPASTElaboratedTypeSpecifier) {
        // region Forward Declaration
        final IASTName elaboratedName = ((ICPPASTElaboratedTypeSpecifier) declSpecifier).getName();
        return createUnknownNode(parentNode, elaboratedName.resolveBinding(), elaboratedName.toString(), true);
    // endregion
    } else if (declSpecifier instanceof ICPPASTSimpleDeclSpecifier) {
        // region Integral Type
        return createIntegralNode(signature);
    // endregion
    } else {
        // todo: debug?
        throw new IllegalArgumentException("createFromDeclSpecifier(declSpecifier = (" + Utilities.objectIdentifyString(declSpecifier) + "))");
    }
}
Also used : ClassNode(mrmathami.cia.cpp.ast.ClassNode) IASTNamedTypeSpecifier(org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier) IASTExpression(org.eclipse.cdt.core.dom.ast.IASTExpression) ICPPASTEnumerationSpecifier(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTEnumerationSpecifier) IASTDeclaration(org.eclipse.cdt.core.dom.ast.IASTDeclaration) IBinding(org.eclipse.cdt.core.dom.ast.IBinding) EnumNode(mrmathami.cia.cpp.ast.EnumNode) IASTEnumerationSpecifier(org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier) ICPPASTNameSpecifier(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNameSpecifier) CppNode(mrmathami.cia.cpp.ast.CppNode) ICPPASTCompositeTypeSpecifier(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier) ICPPASTNamedTypeSpecifier(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamedTypeSpecifier) ICPPASTSimpleDeclSpecifier(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier) ICPPASTDeclSpecifier(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclSpecifier) VariableNode(mrmathami.cia.cpp.ast.VariableNode) IASTName(org.eclipse.cdt.core.dom.ast.IASTName) ICPPASTElaboratedTypeSpecifier(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTElaboratedTypeSpecifier) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ICPPASTBaseSpecifier(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier) Nonnull(mrmathami.annotations.Nonnull)

Aggregations

ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Nonnull (mrmathami.annotations.Nonnull)1 ClassNode (mrmathami.cia.cpp.ast.ClassNode)1 CppNode (mrmathami.cia.cpp.ast.CppNode)1 EnumNode (mrmathami.cia.cpp.ast.EnumNode)1 VariableNode (mrmathami.cia.cpp.ast.VariableNode)1 IASTDeclaration (org.eclipse.cdt.core.dom.ast.IASTDeclaration)1 IASTEnumerationSpecifier (org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier)1 IASTExpression (org.eclipse.cdt.core.dom.ast.IASTExpression)1 IASTName (org.eclipse.cdt.core.dom.ast.IASTName)1 IASTNamedTypeSpecifier (org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier)1 IBinding (org.eclipse.cdt.core.dom.ast.IBinding)1 ICPPASTCompositeTypeSpecifier (org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier)1 ICPPASTBaseSpecifier (org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier)1 ICPPASTDeclSpecifier (org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclSpecifier)1 ICPPASTElaboratedTypeSpecifier (org.eclipse.cdt.core.dom.ast.cpp.ICPPASTElaboratedTypeSpecifier)1 ICPPASTEnumerationSpecifier (org.eclipse.cdt.core.dom.ast.cpp.ICPPASTEnumerationSpecifier)1 ICPPASTNameSpecifier (org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNameSpecifier)1