use of org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNameSpecifier 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) + "))");
}
}
Aggregations