use of org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration in project ch.hsr.ifs.cdttesting by IFS-HSR.
the class ILTISDummyRenameRefactoring method collectModifications.
@Override
protected void collectModifications(IProgressMonitor pm, ModificationCollector collector) throws CoreException, OperationCanceledException {
IASTTranslationUnit ast = refactoringContext.getAST(tu, pm);
IASTSimpleDeclaration funcDeclaration = (IASTSimpleDeclaration) ast.getDeclarations()[0];
IASTFunctionDeclarator declarator = (IASTFunctionDeclarator) funcDeclaration.getDeclarators()[0];
IASTFunctionDeclarator newDeclarator = declarator.copy();
IASTName newName = CPPNodeFactory.getDefault().newName("b".toCharArray());
newDeclarator.setName(newName);
ASTRewrite rewrite = collector.rewriterForTranslationUnit(ast);
rewrite.replace(declarator, newDeclarator, new TextEditGroup(""));
}
use of org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration in project ch.hsr.ifs.cdttesting by IFS-HSR.
the class DummyRenameRefactoring method collectModifications.
@Override
protected void collectModifications(IProgressMonitor pm, ModificationCollector collector) throws CoreException, OperationCanceledException {
IASTTranslationUnit ast = refactoringContext.getAST(tu, pm);
IASTSimpleDeclaration funcDeclaration = (IASTSimpleDeclaration) ast.getDeclarations()[0];
IASTFunctionDeclarator declarator = (IASTFunctionDeclarator) funcDeclaration.getDeclarators()[0];
IASTFunctionDeclarator newDeclarator = declarator.copy();
IASTName newName = CPPNodeFactory.getDefault().newName("b".toCharArray());
newDeclarator.setName(newName);
ASTRewrite rewrite = collector.rewriterForTranslationUnit(ast);
rewrite.replace(declarator, newDeclarator, new TextEditGroup(""));
}
use of org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration in project ch.hsr.ifs.cdttesting by IFS-HSR.
the class ASTComparison method typeSensitiveNodeEquals.
private static <T extends IASTNode> boolean typeSensitiveNodeEquals(final T expected, final T actual, final EnumSet<ComparisonArg> args) {
if (!expected.getClass().equals(actual.getClass())) {
return false;
}
if (expected instanceof IASTDeclaration) {
/* Terminal checks */
if (expected instanceof ICPPASTFunctionDefinition) {
final ICPPASTFunctionDefinition et = as(expected);
final ICPPASTFunctionDefinition at = as(actual);
return et.isDefaulted() == at.isDefaulted() && et.isDeleted() == at.isDeleted();
} else if (expected instanceof ICPPASTTemplateDeclaration) {
final ICPPASTTemplateDeclaration et = as(expected);
final ICPPASTTemplateDeclaration at = as(actual);
return et.isExported() == at.isExported();
} else if (expected instanceof ICPPASTNamespaceDefinition) {
final ICPPASTNamespaceDefinition et = as(expected);
final ICPPASTNamespaceDefinition at = as(actual);
return et.isInline() == at.isInline();
} else if (expected instanceof ICPPASTVisibilityLabel) {
final ICPPASTVisibilityLabel et = as(expected);
final ICPPASTVisibilityLabel at = as(actual);
return et.getVisibility() == at.getVisibility();
} else if (expected instanceof ICPPASTLinkageSpecification) {
final ICPPASTLinkageSpecification et = as(expected);
final ICPPASTLinkageSpecification at = as(actual);
return et.getLiteral().equals(at.getLiteral());
} else if (expected instanceof ICPPASTUsingDeclaration) {
final ICPPASTUsingDeclaration et = as(expected);
final ICPPASTUsingDeclaration at = as(actual);
return et.isTypename() == at.isTypename();
} else if (expected instanceof IASTSimpleDeclaration || expected instanceof ICPPASTAliasDeclaration || expected instanceof ICPPASTUsingDirective) {
/* All tokens are stored in the child-nodes */
return defaultHandler(expected, actual, args);
}
/* Continue comparing raw-signature */
} else if (expected instanceof ICPPASTTemplateParameter) {
/* Template Parameter */
final ICPPASTTemplateParameter e = as(expected);
final ICPPASTTemplateParameter a = as(actual);
if (e.isParameterPack() != a.isParameterPack()) {
return false;
}
if (expected instanceof ICPPASTSimpleTypeTemplateParameter) {
final ICPPASTSimpleTypeTemplateParameter et = as(expected);
final ICPPASTSimpleTypeTemplateParameter at = as(actual);
return et.getParameterType() == at.getParameterType();
} else if (expected instanceof ICPPASTParameterDeclaration || expected instanceof ICPPASTTemplatedTypeTemplateParameter) {
/* All tokens are stored in the child-nodes */
return defaultHandler(expected, actual, args);
}
} else if (expected instanceof ICPPASTDeclarator) {
/* Terminal checks */
if (expected instanceof ICPPASTFunctionDeclarator) {
final ICPPASTFunctionDeclarator e = as(expected);
final ICPPASTFunctionDeclarator a = as(actual);
return e.isConst() == a.isConst() && e.isFinal() == a.isFinal() && e.isPureVirtual() == a.isPureVirtual() && e.isVolatile() == a.isVolatile();
} else {
/* All tokens are stored in the child-nodes */
return defaultHandler(expected, actual, args);
}
/* Continue comparing raw-signature */
} else if (expected instanceof ICPPASTDeclSpecifier) {
/* DeclSpecifier */
final ICPPASTDeclSpecifier e = as(expected);
final ICPPASTDeclSpecifier a = as(actual);
if (e.isConst() != a.isConst() || e.isVirtual() != a.isVirtual() || e.isVolatile() != a.isVolatile() || e.isConstexpr() != a.isConstexpr() || e.isExplicit() != a.isExplicit() || e.isFriend() != a.isFriend() || e.isRestrict() != a.isRestrict() || e.isThreadLocal() != a.isThreadLocal() || e.getStorageClass() != a.getStorageClass()) {
return false;
}
/* Terminal checks */
if (expected instanceof ICPPASTNamedTypeSpecifier) {
final ICPPASTNamedTypeSpecifier et = as(expected);
final ICPPASTNamedTypeSpecifier at = as(actual);
return et.isTypename() == at.isTypename();
} else if (expected instanceof ICPPASTCompositeTypeSpecifier) {
final ICPPASTCompositeTypeSpecifier et = as(expected);
final ICPPASTCompositeTypeSpecifier at = as(actual);
return et.isFinal() == at.isFinal() && et.isVirtual() == at.isVirtual() && et.getKey() == at.getKey();
} else if (expected instanceof ICPPASTElaboratedTypeSpecifier) {
final ICPPASTElaboratedTypeSpecifier et = as(expected);
final ICPPASTElaboratedTypeSpecifier at = as(actual);
return et.getKind() == at.getKind();
} else if (expected instanceof ICPPASTEnumerationSpecifier) {
final ICPPASTEnumerationSpecifier et = as(expected);
final ICPPASTEnumerationSpecifier at = as(actual);
return et.isOpaque() == at.isOpaque() && et.isScoped() == at.isScoped();
} else {
return defaultHandler(expected, actual, args);
}
/* Continue comparing raw-signature */
} else if (expected instanceof IASTExpression) {
if (expected instanceof ICPPASTUnaryExpression) {
final ICPPASTUnaryExpression et = as(expected);
final ICPPASTUnaryExpression at = as(actual);
return et.getOperator() == at.getOperator();
} else if (expected instanceof ICPPASTBinaryExpression) {
final ICPPASTBinaryExpression et = as(expected);
final ICPPASTBinaryExpression at = as(actual);
return et.getOperator() == at.getOperator();
} else if (expected instanceof ICPPASTLiteralExpression) {
final ICPPASTLiteralExpression et = as(expected);
final ICPPASTLiteralExpression at = as(actual);
return et.getKind() == at.getKind() && equalsRaw(et, at, args);
} else if (expected instanceof ICPPASTCastExpression) {
final ICPPASTCastExpression et = as(expected);
final ICPPASTCastExpression at = as(actual);
return et.getOperator() == at.getOperator();
} else if (expected instanceof ICPPASTNewExpression) {
final ICPPASTNewExpression et = as(expected);
final ICPPASTNewExpression at = as(actual);
return et.isArrayAllocation() == at.isArrayAllocation() && et.isGlobal() == at.isGlobal() && et.isNewTypeId() == at.isNewTypeId();
} else if (expected instanceof ICPPASTDeleteExpression) {
final ICPPASTDeleteExpression et = as(expected);
final ICPPASTDeleteExpression at = as(actual);
return et.isVectored() == at.isVectored() && et.isGlobal() == at.isGlobal();
} else if (expected instanceof ICPPASTFieldReference) {
final ICPPASTFieldReference et = as(expected);
final ICPPASTFieldReference at = as(actual);
return et.isPointerDereference() == at.isPointerDereference() && et.isTemplate() == at.isTemplate();
} else if (expected instanceof ICPPASTLambdaExpression) {
final ICPPASTLambdaExpression et = as(expected);
final ICPPASTLambdaExpression at = as(actual);
return et.getCaptureDefault() == at.getCaptureDefault();
} else if (expected instanceof ICPPASTFunctionCallExpression || expected instanceof ICPPASTSimpleTypeConstructorExpression || expected instanceof ICPPASTLambdaExpression || expected instanceof ICPPASTPackExpansionExpression || expected instanceof IASTIdExpression || expected instanceof ICPPASTArraySubscriptExpression || expected instanceof ICPPASTExpressionList || expected instanceof IASTConditionalExpression) {
/* All tokens are stored in the child-nodes */
return defaultHandler(expected, actual, args);
}
/* Continue comparing raw-signature */
} else if (expected instanceof ICPPASTReferenceOperator) {
/* ReferenceOperator */
final ICPPASTReferenceOperator et = as(expected);
final ICPPASTReferenceOperator at = as(actual);
return et.isRValueReference() == at.isRValueReference();
} else if (expected instanceof IASTPointer) {
/* Pointers */
final IASTPointer et = as(expected);
final IASTPointer at = as(actual);
return et.isConst() == at.isConst() && et.isRestrict() == at.isRestrict() && et.isVolatile() == at.isVolatile();
} else if (expected instanceof IASTStatement) {
return defaultHandler(expected, actual, args);
} else if (expected instanceof ICPPASTPackExpandable) {
/* ICPPASTPackExpandable */
final ICPPASTPackExpandable e = as(expected);
final ICPPASTPackExpandable a = as(actual);
if (e.isPackExpansion() != a.isPackExpansion()) {
return false;
}
if (expected instanceof ICPPASTInitializerList) {
final ICPPASTInitializerList et = as(expected);
final ICPPASTInitializerList at = as(actual);
return et.getSize() == at.getSize();
} else if (expected instanceof ICPPASTBaseSpecifier) {
final ICPPASTBaseSpecifier et = as(expected);
final ICPPASTBaseSpecifier at = as(actual);
return et.isVirtual() == at.isVirtual() && et.getVisibility() == at.getVisibility();
} else if (expected instanceof ICPPASTCapture) {
final ICPPASTCapture et = as(expected);
final ICPPASTCapture at = as(actual);
return et.capturesThisPointer() == at.capturesThisPointer() && et.isByReference() == at.isByReference();
} else if (expected instanceof ICPPASTTypeId) {
return true;
} else {
return defaultHandler(expected, actual, args);
}
} else if (expected instanceof ICPPASTName) {
/* Names */
final ICPPASTName e = as(expected);
final ICPPASTName a = as(actual);
if (e.isQualified() != a.isQualified()) {
return false;
}
if (expected instanceof ICPPASTTemplateId) {
final ICPPASTTemplateId et = as(expected);
final ICPPASTTemplateId at = as(actual);
return et.isDeclaration() == at.isDeclaration();
} else if (expected instanceof ICPPASTQualifiedName) {
final ICPPASTQualifiedName et = as(expected);
final ICPPASTQualifiedName at = as(actual);
return et.isFullyQualified() == at.isFullyQualified();
} else if (expected instanceof ICPPASTTemplateId) {
/* Relevant information is contained in the children */
return defaultHandler(expected, actual, args);
}
} else {
/* OTHER */
if (expected instanceof IASTTranslationUnit || expected instanceof IASTArrayModifier || expected instanceof IASTInitializer) {
/* Relevant information is contained in the children */
return defaultHandler(expected, actual, args);
}
/* Continue comparing raw-signature */
}
/* Default case */
return equalsNormalizedRaw(expected, actual);
}
use of org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration in project Cpp4CIA by thanhminhmr.
the class AstBuilder method createChildrenFromDeclaration.
@Nonnull
private List<CppNode> createChildrenFromDeclaration(@Nonnull CppNode parentNode, @Nonnull IASTDeclaration declaration) {
if (declaration instanceof ICPPASTVisibilityLabel || declaration instanceof IASTASMDeclaration || declaration instanceof IASTProblemDeclaration || declaration instanceof ICPPASTStaticAssertDeclaration || declaration instanceof ICPPASTExplicitTemplateInstantiation || declaration instanceof ICPPASTInitCapture) {
// skipped
return List.of();
} else if (declaration instanceof ICPPASTUsingDeclaration) {
// region Using Declaration / Directive
final IASTName declarationName = ((ICPPASTUsingDeclaration) declaration).getName();
final IBinding declarationBinding = declarationName.resolveBinding();
final CppNode declarationNode = createNode(declarationBinding, declarationName, null, new TypedefNode(), parentNode);
if (declarationNode instanceof TypedefNode && declarationBinding instanceof ICPPUsingDeclaration) {
for (final IBinding delegateBinding : ((ICPPUsingDeclaration) declarationBinding).getDelegates()) {
createUnknownNode(declarationNode, delegateBinding, declarationName.toString(), true);
}
}
// endregion
return List.of(declarationNode);
} else if (declaration instanceof ICPPASTUsingDirective) {
// region Using Declaration / Directive
final IASTName usingName = ((ICPPASTUsingDirective) declaration).getQualifiedName();
final CppNode usingNode = createUnknownNode(parentNode, usingName.resolveBinding(), usingName.toString(), true);
// endregion
return List.of(usingNode);
} else if (declaration instanceof ICPPASTLinkageSpecification) {
final ICPPASTLinkageSpecification linkageSpecification = (ICPPASTLinkageSpecification) declaration;
// region extern "C"
final List<CppNode> childrenNode = new ArrayList<>();
for (final IASTDeclaration linkageDeclaration : linkageSpecification.getDeclarations(false)) {
childrenNode.addAll(createChildrenFromDeclaration(parentNode, linkageDeclaration));
}
// endregion
return childrenNode;
} else if (declaration instanceof ICPPASTNamespaceDefinition) {
final ICPPASTNamespaceDefinition namespaceDefinition = (ICPPASTNamespaceDefinition) declaration;
// region Namespace
final IASTName namespaceName = namespaceDefinition.getName();
final CppNode namespaceNode = createNode(namespaceName.resolveBinding(), namespaceName, null, new NamespaceNode(), parentNode);
for (final IASTDeclaration namespaceDeclaration : namespaceDefinition.getDeclarations(false)) {
createChildrenFromDeclaration(namespaceNode, namespaceDeclaration);
}
// endregion
return List.of(namespaceNode);
} else if (declaration instanceof IASTSimpleDeclaration) {
final IASTSimpleDeclaration simpleDeclaration = (IASTSimpleDeclaration) declaration;
// region Simple Declaration
final IASTDeclSpecifier simpleSpecifier = simpleDeclaration.getDeclSpecifier();
final CppNode simpleNodeType = createFromDeclSpecifier(parentNode, simpleSpecifier);
final List<CppNode> simpleNodeList = new ArrayList<>();
final boolean isTypedef = simpleSpecifier.getStorageClass() == IASTDeclSpecifier.sc_typedef;
for (final IASTDeclarator simpleDeclarator : simpleDeclaration.getDeclarators()) {
simpleNodeList.add(createFromDeclarator(parentNode, simpleNodeType, simpleDeclarator, isTypedef));
}
// endregion
return !simpleNodeList.isEmpty() ? simpleNodeList : List.of(simpleNodeType);
} else if (declaration instanceof ICPPASTFunctionDefinition) {
final ICPPASTFunctionDefinition functionDefinition = (ICPPASTFunctionDefinition) declaration;
// region Function
final CppNode functionReturnType = createFromDeclSpecifier(parentNode, functionDefinition.getDeclSpecifier());
final CppNode functionNode = createFromDeclarator(parentNode, functionReturnType, functionDefinition.getDeclarator(), false);
final StringBuilder functionBodyBuilder = new StringBuilder(functionNode instanceof FunctionNode ? Objects.requireNonNullElse(((FunctionNode) functionNode).getBody(), "") : "");
// function with constructor
for (final ICPPASTConstructorChainInitializer memberChainInitializer : functionDefinition.getMemberInitializers()) {
final IASTName memberName = memberChainInitializer.getMemberInitializerId();
createUnknownNode(functionNode, memberName.resolveBinding(), memberName.toString(), true);
functionBodyBuilder.append(memberName).append('(');
final IASTInitializer memberInitializer = memberChainInitializer.getInitializer();
if (memberInitializer != null) {
childrenCreationQueue.add(Pair.mutableOf(functionNode, memberInitializer));
functionBodyBuilder.append(memberInitializer.getRawSignature());
}
functionBodyBuilder.append(");");
}
// function with body
final IASTStatement functionBody = functionDefinition.getBody();
if (functionBody != null && functionBody.getChildren().length > 0) {
childrenCreationQueue.add(Pair.mutableOf(functionNode, functionBody));
functionBodyBuilder.append(functionBody.getRawSignature());
}
if (functionBodyBuilder.length() > 0 && functionNode instanceof FunctionNode) {
((FunctionNode) functionNode).setBody(functionBodyBuilder.toString());
}
// endregion
return List.of(functionNode);
} else if (declaration instanceof ICPPASTTemplateDeclaration) {
final ICPPASTTemplateDeclaration templateDeclaration = (ICPPASTTemplateDeclaration) declaration;
// region Template
final List<CppNode> innerNodeList = createChildrenFromDeclaration(parentNode, templateDeclaration.getDeclaration());
if (!innerNodeList.isEmpty()) {
final CppNode innerNode = innerNodeList.get(0);
for (final ICPPASTTemplateParameter templateParameter : templateDeclaration.getTemplateParameters()) {
createFromTemplateParameter(innerNode, templateParameter);
}
}
// endregion
return innerNodeList;
} else if (declaration instanceof ICPPASTNamespaceAlias) {
final ICPPASTNamespaceAlias namespaceAlias = (ICPPASTNamespaceAlias) declaration;
// region Namespace Alias
final IASTName aliasName = namespaceAlias.getAlias();
final CppNode aliasNode = createNode(aliasName.resolveBinding(), aliasName, null, new TypedefNode(), parentNode);
final IASTName mappingName = namespaceAlias.getMappingName();
createUnknownNode(aliasNode, mappingName.resolveBinding(), mappingName.toString(), true);
// endregion
return List.of(aliasNode);
} else if (declaration instanceof ICPPASTAliasDeclaration) {
final ICPPASTAliasDeclaration aliasDefinition = (ICPPASTAliasDeclaration) declaration;
// region Alias
final IASTName aliasName = aliasDefinition.getAlias();
final ICPPASTTypeId aliasTypeId = aliasDefinition.getMappingTypeId();
final IASTDeclSpecifier aliasDeclSpecifier = aliasTypeId.getDeclSpecifier();
final IASTDeclarator aliasDeclarator = aliasTypeId.getAbstractDeclarator();
final CppNode aliasNode = createNode(aliasName.resolveBinding(), aliasName, ASTStringUtil.getSignatureString(aliasDeclSpecifier, aliasDeclarator), new TypedefNode(), parentNode);
final CppNode aliasType = createFromDeclSpecifier(aliasNode, aliasDeclSpecifier);
createFromDeclarator(aliasNode, aliasType, aliasDeclarator, false);
// endregion
return List.of(aliasNode);
} else {
// todo: debug?
throw new IllegalArgumentException("createChildrenFromDeclaration(parentNode = (" + Utilities.objectIdentifyString(parentNode) + "), declaration = (" + Utilities.objectIdentifyString(declaration) + "))");
}
}
Aggregations