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