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