mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 14:42:11 +02:00
get declarations for C++
This commit is contained in:
parent
8564247303
commit
ebdeb763c8
3 changed files with 155 additions and 13 deletions
|
@ -118,8 +118,7 @@ public class CPPASTTranslationUnit extends CPPASTNode implements
|
|||
* @see org.eclipse.cdt.core.dom.ast.IASTTranslationUnit#getDeclarations(org.eclipse.cdt.core.dom.ast.IBinding)
|
||||
*/
|
||||
public IASTName[] getDeclarations(IBinding b) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
return CPPVisitor.getDeclarations( this, b );
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -23,6 +23,7 @@ import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
|
|||
import org.eclipse.cdt.core.dom.ast.IScope;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVisiblityLabel;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
|
||||
|
@ -113,4 +114,31 @@ public class CPPMethod extends CPPFunction implements ICPPMethod {
|
|||
public IScope getScope() {
|
||||
return CPPVisitor.getContainingScope( declarations != null ? declarations[0] : definition );
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
if( definition != null ){
|
||||
IASTName n = definition.getName();
|
||||
if( n instanceof ICPPASTQualifiedName ){
|
||||
IASTName [] ns = ((ICPPASTQualifiedName)n).getNames();
|
||||
return ns[ ns.length - 1 ].toString();
|
||||
}
|
||||
return n.toString();
|
||||
}
|
||||
return declarations[0].getName().toString();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.cdt.core.dom.ast.IBinding#getNameCharArray()
|
||||
*/
|
||||
public char[] getNameCharArray() {
|
||||
if( definition != null ){
|
||||
IASTName n = definition.getName();
|
||||
if( n instanceof ICPPASTQualifiedName ){
|
||||
IASTName [] ns = ((ICPPASTQualifiedName)n).getNames();
|
||||
return ns[ ns.length - 1 ].toCharArray();
|
||||
}
|
||||
return n.toCharArray();
|
||||
}
|
||||
return declarations[0].getName().toCharArray();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -740,6 +740,110 @@ public class CPPVisitor {
|
|||
}
|
||||
}
|
||||
|
||||
public static class CollectDeclarationsAction extends CPPBaseVisitorAction {
|
||||
private static final int DEFAULT_LIST_SIZE = 8;
|
||||
private IASTName [] decls;
|
||||
private IBinding binding;
|
||||
private int idx = 0;
|
||||
private int kind;
|
||||
|
||||
private static final int KIND_LABEL = 1;
|
||||
private static final int KIND_OBJ_FN = 2;
|
||||
private static final int KIND_TYPE = 3;
|
||||
private static final int KIND_NAMEPSACE = 4;
|
||||
|
||||
|
||||
public CollectDeclarationsAction( IBinding binding ){
|
||||
this.binding = binding;
|
||||
this.decls = new IASTName[ DEFAULT_LIST_SIZE ];
|
||||
|
||||
processNames = true;
|
||||
if( binding instanceof ILabel )
|
||||
kind = KIND_LABEL;
|
||||
else if( binding instanceof ICompositeType ||
|
||||
binding instanceof ITypedef ||
|
||||
binding instanceof IEnumeration)
|
||||
{
|
||||
kind = KIND_TYPE;
|
||||
}
|
||||
else if( binding instanceof ICPPNamespace) {
|
||||
kind = KIND_NAMEPSACE;
|
||||
} else
|
||||
kind = KIND_OBJ_FN;
|
||||
}
|
||||
|
||||
public int processName( IASTName name ){
|
||||
if( name instanceof ICPPASTQualifiedName ) return PROCESS_CONTINUE;
|
||||
|
||||
ASTNodeProperty prop = name.getPropertyInParent();
|
||||
if( prop == ICPPASTQualifiedName.SEGMENT_NAME )
|
||||
prop = name.getParent().getPropertyInParent();
|
||||
|
||||
switch( kind ){
|
||||
case KIND_LABEL:
|
||||
if( prop == IASTLabelStatement.NAME )
|
||||
break;
|
||||
return PROCESS_CONTINUE;
|
||||
case KIND_TYPE:
|
||||
if( prop == IASTCompositeTypeSpecifier.TYPE_NAME ||
|
||||
prop == IASTEnumerationSpecifier.ENUMERATION_NAME )
|
||||
{
|
||||
break;
|
||||
} else if( prop == IASTElaboratedTypeSpecifier.TYPE_NAME ){
|
||||
IASTNode p = name.getParent().getParent();
|
||||
if( p instanceof IASTSimpleDeclaration &&
|
||||
((IASTSimpleDeclaration)p).getDeclarators().length == 0 )
|
||||
{
|
||||
break;
|
||||
}
|
||||
} else if( prop == IASTDeclarator.DECLARATOR_NAME ){
|
||||
IASTNode p = name.getParent().getParent();
|
||||
if( p instanceof IASTSimpleDeclaration ){
|
||||
IASTDeclSpecifier declSpec = ((IASTSimpleDeclaration)p).getDeclSpecifier();
|
||||
if( declSpec.getStorageClass() == IASTDeclSpecifier.sc_typedef )
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return PROCESS_CONTINUE;
|
||||
case KIND_OBJ_FN:
|
||||
if( prop == IASTDeclarator.DECLARATOR_NAME )
|
||||
{
|
||||
break;
|
||||
}
|
||||
return PROCESS_CONTINUE;
|
||||
case KIND_NAMEPSACE:
|
||||
if( prop == ICPPASTNamespaceDefinition.NAMESPACE_NAME )
|
||||
{
|
||||
break;
|
||||
}
|
||||
return PROCESS_CONTINUE;
|
||||
}
|
||||
|
||||
if( binding != null &&
|
||||
CharArrayUtils.equals(name.toCharArray(), binding.getNameCharArray()) &&
|
||||
name.resolveBinding() == binding )
|
||||
{
|
||||
if( decls.length == idx ){
|
||||
IASTName [] temp = new IASTName[ decls.length * 2 ];
|
||||
System.arraycopy( decls, 0, temp, 0, decls.length );
|
||||
decls = temp;
|
||||
}
|
||||
decls[idx++] = name;
|
||||
}
|
||||
return PROCESS_CONTINUE;
|
||||
}
|
||||
public IASTName[] getDeclarations(){
|
||||
if( idx < decls.length ){
|
||||
IASTName [] temp = new IASTName[ idx ];
|
||||
System.arraycopy( decls, 0, temp, 0, idx );
|
||||
decls = temp;
|
||||
}
|
||||
return decls;
|
||||
}
|
||||
|
||||
}
|
||||
public static class CollectReferencesAction extends CPPBaseVisitorAction {
|
||||
private static final int DEFAULT_LIST_SIZE = 8;
|
||||
private IASTName [] refs;
|
||||
|
@ -776,8 +880,11 @@ public class CPPVisitor {
|
|||
if( name instanceof ICPPASTQualifiedName ) return PROCESS_CONTINUE;
|
||||
|
||||
ASTNodeProperty prop = name.getPropertyInParent();
|
||||
if( prop == ICPPASTQualifiedName.SEGMENT_NAME )
|
||||
ASTNodeProperty p2 = null;
|
||||
if( prop == ICPPASTQualifiedName.SEGMENT_NAME ){
|
||||
p2 = prop;
|
||||
prop = name.getParent().getPropertyInParent();
|
||||
}
|
||||
|
||||
switch( kind ){
|
||||
case KIND_LABEL:
|
||||
|
@ -786,13 +893,15 @@ public class CPPVisitor {
|
|||
return PROCESS_CONTINUE;
|
||||
case KIND_TYPE:
|
||||
if( prop == IASTNamedTypeSpecifier.NAME ||
|
||||
prop == ICPPASTPointerToMember.NAME ||
|
||||
prop == ICPPASTTypenameExpression.TYPENAME ||
|
||||
prop == ICPPASTUsingDeclaration.NAME ||
|
||||
prop == ICPPASTQualifiedName.SEGMENT_NAME ||
|
||||
prop == ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier.NAME)
|
||||
prop == ICPPASTPointerToMember.NAME ||
|
||||
prop == ICPPASTTypenameExpression.TYPENAME ||
|
||||
prop == ICPPASTUsingDeclaration.NAME ||
|
||||
prop == ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier.NAME)
|
||||
{
|
||||
break;
|
||||
else if( prop == IASTElaboratedTypeSpecifier.TYPE_NAME ){
|
||||
}
|
||||
else if( prop == IASTElaboratedTypeSpecifier.TYPE_NAME )
|
||||
{
|
||||
IASTNode p = name.getParent().getParent();
|
||||
if( !(p instanceof IASTSimpleDeclaration) ||
|
||||
((IASTSimpleDeclaration)p).getDeclarators().length > 0 )
|
||||
|
@ -809,7 +918,6 @@ public class CPPVisitor {
|
|||
prop == ICPPASTUsingDeclaration.NAME ||
|
||||
prop == IASTFunctionCallExpression.FUNCTION_NAME ||
|
||||
prop == ICPPASTUsingDeclaration.NAME ||
|
||||
prop == ICPPASTQualifiedName.SEGMENT_NAME ||
|
||||
prop == IASTNamedTypeSpecifier.NAME)
|
||||
{
|
||||
break;
|
||||
|
@ -817,9 +925,10 @@ public class CPPVisitor {
|
|||
return PROCESS_CONTINUE;
|
||||
case KIND_NAMEPSACE:
|
||||
if( prop == ICPPASTUsingDirective.QUALIFIED_NAME ||
|
||||
prop == ICPPASTNamespaceAlias.MAPPING_NAME ||
|
||||
prop == ICPPASTUsingDeclaration.NAME ||
|
||||
prop == ICPPASTQualifiedName.SEGMENT_NAME) {
|
||||
prop == ICPPASTNamespaceAlias.MAPPING_NAME ||
|
||||
prop == ICPPASTUsingDeclaration.NAME ||
|
||||
p2 == ICPPASTQualifiedName.SEGMENT_NAME )
|
||||
{
|
||||
break;
|
||||
}
|
||||
return PROCESS_CONTINUE;
|
||||
|
@ -1678,4 +1787,10 @@ public class CPPVisitor {
|
|||
visitTranslationUnit( tu, action );
|
||||
return action.getReferences();
|
||||
}
|
||||
|
||||
public static IASTName[] getDeclarations( IASTTranslationUnit tu, IBinding binding ){
|
||||
CollectDeclarationsAction action = new CollectDeclarationsAction( binding );
|
||||
visitTranslationUnit( tu, action );
|
||||
return action.getDeclarations();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue