Search in sources :

Example 1 with VariableNode

use of mrmathami.cia.cpp.ast.VariableNode in project Cpp4CIA by thanhminhmr.

the class AstBuilder method cleanUp.

private void cleanUp() {
    bindingNodeMap.clear();
    // remove all children of variable and function node
    for (final CppNode node : rootNode) {
        if (node instanceof FunctionNode || node instanceof VariableNode) {
            node.collapse();
        }
    }
    // clean up nodes
    for (final CppNode node : integralNodeMap.values()) {
        node.removeChildren();
        node.removeAllDependency();
    }
    // replace unknown node with integral node
    for (final IntegralNode unknownNode : unknownNodes) {
        if (unknownNode.getRoot() != rootNode)
            continue;
        final CppNode parent = unknownNode.getParent();
        assert parent != null;
        if (unknownNode.getAllDependencyFrom().isEmpty() && unknownNode.getAllDependencyTo().isEmpty()) {
            unknownNode.collapseToParent();
        } else {
            unknownNode.collapse();
            final CppNode integralNode = integralNodeMap.get(unknownNode.getName());
            if (integralNode != null) {
                unknownNode.transfer(integralNode);
                integralNode.transferAllDependency(parent);
            } else {
                unknownNode.move(rootNode);
                unknownNode.transferAllDependency(parent);
                integralNodeMap.put(unknownNode.getName(), unknownNode);
            }
        }
    }
    unknownNodes.clear();
    // merge duplicates
    {
        final CppNode.Matcher matcher = new CppNode.Matcher();
        mergeDuplicate(matcher, rootNode);
        for (final CppNode node : rootNode) {
            mergeDuplicate(matcher, node);
        }
    }
}
Also used : VariableNode(mrmathami.cia.cpp.ast.VariableNode) FunctionNode(mrmathami.cia.cpp.ast.FunctionNode) CppNode(mrmathami.cia.cpp.ast.CppNode) IntegralNode(mrmathami.cia.cpp.ast.IntegralNode)

Example 2 with VariableNode

use of mrmathami.cia.cpp.ast.VariableNode 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)

Example 3 with VariableNode

use of mrmathami.cia.cpp.ast.VariableNode in project Cpp4CIA by thanhminhmr.

the class AstBuilder method createFromDeclarator.

@Nonnull
private CppNode createFromDeclarator(@Nonnull CppNode parentNode, @Nullable CppNode typeNode, @Nonnull IASTDeclarator declarator, boolean isTypedef) {
    final IASTName declaratorName = declarator.getName();
    final IBinding declaratorBinding = declaratorName.resolveBinding();
    final String signature = ASTStringUtil.getSignatureString(declarator);
    if (declarator instanceof IASTAmbiguousDeclarator) {
        return createUnknownNode(parentNode, declaratorBinding, declaratorName.toString(), isTypedef);
    } else if (declarator instanceof ICPPASTFunctionDeclarator) {
        // region Function
        final ICPPASTFunctionDeclarator functionDeclarator = (ICPPASTFunctionDeclarator) declarator;
        final CppNode functionNode = createNode(declaratorBinding, declaratorName, signature, new FunctionNode(), parentNode);
        if (functionNode instanceof FunctionNode) {
            if (!(typeNode instanceof IntegralNode) || !typeNode.getName().isEmpty()) {
                ((FunctionNode) functionNode).setType(typeNode);
            // functionNode.addDependencyTo(typeNode, DependencyType.USE);
            }
            for (final ICPPASTParameterDeclaration functionParameter : functionDeclarator.getParameters()) {
                final CppNode parameterType = createFromDeclSpecifier(functionNode, functionParameter.getDeclSpecifier());
                if (!(parameterType instanceof IntegralNode) || !parameterType.getName().equals("void")) {
                    createFromDeclarator(functionNode, parameterType, functionParameter.getDeclarator(), true);
                    ((FunctionNode) functionNode).addParameter(parameterType);
                // functionNode.addDependencyTo(parameterType, DependencyType.USE);
                }
            }
            final IASTInitializer initializer = declarator.getInitializer();
            if (initializer != null && initializer.getChildren().length > 0) {
                ((FunctionNode) functionNode).setBody(initializer.getRawSignature());
                childrenCreationQueue.add(Pair.mutableOf(functionNode, initializer));
            }
        }
        // endregion
        return functionNode;
    } else if (declarator instanceof ICPPASTDeclarator) {
        if (isTypedef) {
            // region Typedef
            final CppNode typedefNode = createNode(declaratorBinding, declaratorName, signature, new TypedefNode(), parentNode);
            if (typedefNode instanceof TypedefNode) {
                ((TypedefNode) typedefNode).setType(typeNode);
            // typedefNode.addDependencyTo(typeNode, DependencyType.USE);
            }
            // endregion
            return typedefNode;
        } else {
            // region Variable
            final CppNode variableNode = createNode(declaratorBinding, declaratorName, signature, new VariableNode(), parentNode);
            if (variableNode instanceof VariableNode) {
                ((VariableNode) variableNode).setType(typeNode);
                // variableNode.addDependencyTo(typeNode, DependencyType.USE);
                final IASTInitializer initializer = declarator.getInitializer();
                if (initializer != null) {
                    ((VariableNode) variableNode).setBody(initializer.getRawSignature());
                    childrenCreationQueue.add(Pair.mutableOf(variableNode, initializer));
                }
            }
            // endregion
            return variableNode;
        }
    } else {
        // todo: debug?
        throw new IllegalArgumentException("createFromDeclarator(declarator = (" + Utilities.objectIdentifyString(declarator) + "))");
    }
}
Also used : ICPPASTFunctionDeclarator(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator) IBinding(org.eclipse.cdt.core.dom.ast.IBinding) FunctionNode(mrmathami.cia.cpp.ast.FunctionNode) CppNode(mrmathami.cia.cpp.ast.CppNode) ICPPASTDeclarator(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclarator) VariableNode(mrmathami.cia.cpp.ast.VariableNode) IASTName(org.eclipse.cdt.core.dom.ast.IASTName) IASTInitializer(org.eclipse.cdt.core.dom.ast.IASTInitializer) IASTAmbiguousDeclarator(org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousDeclarator) IntegralNode(mrmathami.cia.cpp.ast.IntegralNode) ICPPASTParameterDeclaration(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTParameterDeclaration) TypedefNode(mrmathami.cia.cpp.ast.TypedefNode) Nonnull(mrmathami.annotations.Nonnull)

Aggregations

CppNode (mrmathami.cia.cpp.ast.CppNode)3 VariableNode (mrmathami.cia.cpp.ast.VariableNode)3 Nonnull (mrmathami.annotations.Nonnull)2 FunctionNode (mrmathami.cia.cpp.ast.FunctionNode)2 IntegralNode (mrmathami.cia.cpp.ast.IntegralNode)2 IASTName (org.eclipse.cdt.core.dom.ast.IASTName)2 IBinding (org.eclipse.cdt.core.dom.ast.IBinding)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ClassNode (mrmathami.cia.cpp.ast.ClassNode)1 EnumNode (mrmathami.cia.cpp.ast.EnumNode)1 TypedefNode (mrmathami.cia.cpp.ast.TypedefNode)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 IASTInitializer (org.eclipse.cdt.core.dom.ast.IASTInitializer)1 IASTNamedTypeSpecifier (org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier)1 ICPPASTCompositeTypeSpecifier (org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier)1 ICPPASTBaseSpecifier (org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier)1