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());
}
}
}
}
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);
}
}
}
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;
}
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;
}
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;
}
Aggregations