Search in sources :

Example 1 with CppNode

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

the class AstBuilder method mergeDuplicate.

private void mergeDuplicate(@Nonnull CppNode.Matcher matcher, @Nonnull CppNode node) {
    final Map<CppNode.Wrapper, CppNode> nodeMap = new HashMap<>();
    for (final CppNode childNode : List.copyOf(node.getChildren())) {
        final CppNode.Wrapper wrapper = new CppNode.Wrapper(childNode, CppNode.MatchLevel.SIMILAR, matcher);
        final CppNode existingNode = nodeMap.putIfAbsent(wrapper, childNode);
        if (existingNode == null)
            continue;
        if (existingNode instanceof IBodyContainer) {
            if (((IBodyContainer) existingNode).getBody() == null) {
                existingNode.transfer(childNode);
                nodeMap.put(wrapper, childNode);
            } else {
                childNode.transfer(existingNode);
            }
        } else {
            childNode.transfer(existingNode);
            if (childNode instanceof IntegralNode) {
                integralNodeMap.remove(childNode.getName());
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) CppNode(mrmathami.cia.cpp.ast.CppNode) IBodyContainer(mrmathami.cia.cpp.ast.IBodyContainer) IntegralNode(mrmathami.cia.cpp.ast.IntegralNode)

Example 2 with CppNode

use of mrmathami.cia.cpp.ast.CppNode 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 3 with CppNode

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

the class AstBuilder method createIntegralNode.

@Nonnull
private CppNode createIntegralNode(@Nonnull String typeName) {
    final CppNode existNode = integralNodeMap.get(typeName);
    if (existNode != null)
        return existNode;
    final IntegralNode newNode = new IntegralNode(typeName);
    rootNode.addChild(newNode);
    integralNodeMap.put(typeName, newNode);
    return newNode;
}
Also used : CppNode(mrmathami.cia.cpp.ast.CppNode) IntegralNode(mrmathami.cia.cpp.ast.IntegralNode) Nonnull(mrmathami.annotations.Nonnull)

Example 4 with CppNode

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

the class AstBuilder method createNode.

@Nonnull
private CppNode createNode(@Nullable IBinding binding, @Nullable IASTName astName, @Nullable String signature, @Nonnull CppNode newNode, @Nonnull CppNode parentNode) {
    assert !(newNode instanceof IntegralNode);
    if (binding == null) {
        return createIntegralNode(astName != null ? astName.toString() : signature != null ? signature : "");
    }
    final IBinding topBinding = binding instanceof ICPPSpecialization ? Objects.requireNonNullElse(((ICPPSpecialization) binding).getSpecializedBinding(), binding) : binding;
    final CppNode existNode = bindingNodeMap.get(topBinding);
    if (existNode != null && !(existNode instanceof IntegralNode))
        return existNode;
    final String name = firstNonBlank(astName != null ? astName.toString() : null, topBinding.getName());
    final String uniqueName = firstNonBlank(topBinding instanceof ICPPBinding ? PATTERN.matcher(ASTTypeUtil.getQualifiedName((ICPPBinding) binding)).replaceAll("{ROOT}") : astName != null ? ASTStringUtil.getQualifiedName(astName) : null, name);
    newNode.setName(name);
    newNode.setUniqueName(uniqueName);
    newNode.setSignature(signature != null ? signature : uniqueName);
    parentNode.addChild(newNode);
    parentNode.addDependencyTo(newNode, DependencyType.MEMBER);
    bindingNodeMap.put(topBinding, newNode);
    if (existNode != null)
        replaceNode(existNode, newNode);
    return newNode;
}
Also used : IBinding(org.eclipse.cdt.core.dom.ast.IBinding) CppNode(mrmathami.cia.cpp.ast.CppNode) ICPPSpecialization(org.eclipse.cdt.core.dom.ast.cpp.ICPPSpecialization) IntegralNode(mrmathami.cia.cpp.ast.IntegralNode) ICPPBinding(org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding) Nonnull(mrmathami.annotations.Nonnull)

Example 5 with CppNode

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

the class AstBuilder method createFromTemplateParameter.

private void createFromTemplateParameter(@Nonnull CppNode parentNode, @Nonnull ICPPASTTemplateParameter templateParameter) {
    if (templateParameter instanceof ICPPASTParameterDeclaration) {
        // region Template Variable
        final ICPPASTParameterDeclaration parameterDeclaration = (ICPPASTParameterDeclaration) templateParameter;
        final CppNode parameterType = createFromDeclSpecifier(parentNode, parameterDeclaration.getDeclSpecifier());
        createFromDeclarator(parentNode, parameterType, parameterDeclaration.getDeclarator(), false);
    // endregion
    } else if (templateParameter instanceof ICPPASTSimpleTypeTemplateParameter) {
        // region Template Typename
        final ICPPASTSimpleTypeTemplateParameter simpleParameter = (ICPPASTSimpleTypeTemplateParameter) templateParameter;
        final IASTName simpleName = simpleParameter.getName();
        final IBinding simpleBinding = simpleName.resolveBinding();
        final CppNode typedefNode = createNode(simpleBinding, simpleName, null, new TypedefNode(), parentNode);
        if (typedefNode instanceof TypedefNode) {
            final IASTTypeId defaultType = simpleParameter.getDefaultType();
            if (defaultType != null) {
                final IASTDeclSpecifier elementSpecifier = defaultType.getDeclSpecifier();
                final IASTDeclarator elementDeclarator = defaultType.getAbstractDeclarator();
                final CppNode elementType = elementSpecifier != null ? createFromDeclSpecifier(typedefNode, elementSpecifier) : null;
                final CppNode element = elementDeclarator != null && elementType != null ? createFromDeclarator(typedefNode, elementType, elementDeclarator, false) : null;
                if (element != null || elementType != null) {
                    final CppNode typeNode = element != null ? element : elementType;
                    ((TypedefNode) typedefNode).setType(typeNode);
                // typedefNode.addDependencyTo(typeNode, DependencyType.USE);
                }
            }
        }
    // endregion
    } else if (templateParameter instanceof ICPPASTTemplatedTypeTemplateParameter) {
        // region Nested Template
        final ICPPASTTemplatedTypeTemplateParameter nestedParameter = (ICPPASTTemplatedTypeTemplateParameter) templateParameter;
        final IASTName nestedName = nestedParameter.getName();
        final CppNode nestedNode = createNode(nestedName.resolveBinding(), nestedName, null, new TypedefNode(), parentNode);
        for (final ICPPASTTemplateParameter innerParameter : nestedParameter.getTemplateParameters()) {
            createFromTemplateParameter(nestedNode, innerParameter);
        }
    // endregion
    } else {
        // todo: debug?
        throw new IllegalArgumentException("createFromTemplateParameter(parentNode = (" + Utilities.objectIdentifyString(parentNode) + "), templateParameter = (" + Utilities.objectIdentifyString(templateParameter) + "))");
    }
}
Also used : ICPPASTTemplateParameter(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter) IASTDeclarator(org.eclipse.cdt.core.dom.ast.IASTDeclarator) ICPPASTSimpleTypeTemplateParameter(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeTemplateParameter) IASTName(org.eclipse.cdt.core.dom.ast.IASTName) IBinding(org.eclipse.cdt.core.dom.ast.IBinding) CppNode(mrmathami.cia.cpp.ast.CppNode) ICPPASTTemplatedTypeTemplateParameter(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplatedTypeTemplateParameter) IASTTypeId(org.eclipse.cdt.core.dom.ast.IASTTypeId) ICPPASTParameterDeclaration(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTParameterDeclaration) TypedefNode(mrmathami.cia.cpp.ast.TypedefNode) IASTDeclSpecifier(org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier)

Aggregations

CppNode (mrmathami.cia.cpp.ast.CppNode)16 Nonnull (mrmathami.annotations.Nonnull)12 IntegralNode (mrmathami.cia.cpp.ast.IntegralNode)7 IBinding (org.eclipse.cdt.core.dom.ast.IBinding)6 FunctionNode (mrmathami.cia.cpp.ast.FunctionNode)4 IASTName (org.eclipse.cdt.core.dom.ast.IASTName)4 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 TypedefNode (mrmathami.cia.cpp.ast.TypedefNode)3 VariableNode (mrmathami.cia.cpp.ast.VariableNode)3 IASTDeclaration (org.eclipse.cdt.core.dom.ast.IASTDeclaration)3 ArrayList (java.util.ArrayList)2 IdentityHashMap (java.util.IdentityHashMap)2 List (java.util.List)2 ClassNode (mrmathami.cia.cpp.ast.ClassNode)2 DependencyType (mrmathami.cia.cpp.ast.DependencyType)2 RootNode (mrmathami.cia.cpp.ast.RootNode)2 IASTDeclSpecifier (org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier)2 IASTDeclarator (org.eclipse.cdt.core.dom.ast.IASTDeclarator)2 IASTInitializer (org.eclipse.cdt.core.dom.ast.IASTInitializer)2