Search in sources :

Example 1 with IntegralNode

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

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

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

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

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

the class AstBuilder method createUnknownNode.

@Nonnull
private CppNode createUnknownNode(@Nonnull CppNode parentNode, @Nonnull IBinding binding, @Nonnull String name, boolean createUseDependency) {
    if (binding instanceof IProblemBinding)
        return createIntegralNode(name);
    final IBinding topBinding = binding instanceof ICPPSpecialization ? Objects.requireNonNullElse(((ICPPSpecialization) binding).getSpecializedBinding(), binding) : binding;
    final CppNode existNode = bindingNodeMap.get(topBinding);
    if (existNode != null) {
        if (createUseDependency)
            parentNode.addDependencyTo(existNode, DependencyType.USE);
        return existNode;
    }
    final IntegralNode newNode = new IntegralNode(name);
    parentNode.addChild(newNode);
    if (createUseDependency)
        parentNode.addDependencyTo(newNode, DependencyType.USE);
    bindingNodeMap.put(topBinding, newNode);
    unknownNodes.add(newNode);
    return newNode;
}
Also used : IProblemBinding(org.eclipse.cdt.core.dom.ast.IProblemBinding) 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) Nonnull(mrmathami.annotations.Nonnull)

Aggregations

CppNode (mrmathami.cia.cpp.ast.CppNode)7 IntegralNode (mrmathami.cia.cpp.ast.IntegralNode)7 Nonnull (mrmathami.annotations.Nonnull)5 IBinding (org.eclipse.cdt.core.dom.ast.IBinding)3 HashMap (java.util.HashMap)2 FunctionNode (mrmathami.cia.cpp.ast.FunctionNode)2 VariableNode (mrmathami.cia.cpp.ast.VariableNode)2 ICPPSpecialization (org.eclipse.cdt.core.dom.ast.cpp.ICPPSpecialization)2 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 DependencyType (mrmathami.cia.cpp.ast.DependencyType)1 IBodyContainer (mrmathami.cia.cpp.ast.IBodyContainer)1 RootNode (mrmathami.cia.cpp.ast.RootNode)1 TypedefNode (mrmathami.cia.cpp.ast.TypedefNode)1 Pair (mrmathami.utils.Pair)1 IASTInitializer (org.eclipse.cdt.core.dom.ast.IASTInitializer)1 IASTName (org.eclipse.cdt.core.dom.ast.IASTName)1 IProblemBinding (org.eclipse.cdt.core.dom.ast.IProblemBinding)1 ICPPASTDeclarator (org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclarator)1 ICPPASTFunctionDeclarator (org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator)1