1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-04 14:55:41 +02:00

IScope.find( name )

This commit is contained in:
Andrew Niefer 2005-03-01 22:29:21 +00:00
parent 361aa3e14e
commit b7f7126ca2
5 changed files with 163 additions and 27 deletions

View file

@ -14,13 +14,19 @@
*/ */
package org.eclipse.cdt.internal.core.dom.parser.c; package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTVisitor;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier; import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICCompositeTypeScope; import org.eclipse.cdt.core.dom.ast.c.ICCompositeTypeScope;
import org.eclipse.cdt.core.dom.ast.c.ICScope; import org.eclipse.cdt.core.dom.ast.c.ICScope;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.CharArrayObjectMap; import org.eclipse.cdt.core.parser.util.CharArrayObjectMap;
import org.eclipse.cdt.internal.core.dom.parser.c.CScope.CollectNamesAction;
/** /**
* @author aniefer * @author aniefer
@ -68,8 +74,26 @@ public class CCompositeTypeScope implements ICCompositeTypeScope {
* @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String) * @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String)
*/ */
public IBinding[] find( String name ) { public IBinding[] find( String name ) {
// TODO Auto-generated method stub IASTNode node = getPhysicalNode();
return null; IASTTranslationUnit tu = node.getTranslationUnit();
IASTVisitor visitor = tu.getVisitor();
CollectNamesAction action = new CollectNamesAction( name.toCharArray() );
visitor.visitDeclSpecifier( compositeTypeSpec, action );
IASTName [] names = action.getNames();
IBinding [] result = null;
for( int i = 0; i < names.length; i++ ){
IBinding b = names[i].resolveBinding();
if( b == null ) continue;
try {
if( b.getScope() == this )
result = (IBinding[]) ArrayUtil.append( IBinding.class, result, b );
} catch ( DOMException e ) {
}
}
return (IBinding[]) ArrayUtil.trim( IBinding.class, result );
} }
/* (non-Javadoc) /* (non-Javadoc)

View file

@ -14,13 +14,26 @@
*/ */
package org.eclipse.cdt.internal.core.dom.parser.c; package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.ASTNodeProperty;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTVisitor;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType; import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IEnumeration; import org.eclipse.cdt.core.dom.ast.IEnumeration;
import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.c.ICASTVisitor;
import org.eclipse.cdt.core.dom.ast.c.ICScope; import org.eclipse.cdt.core.dom.ast.c.ICScope;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.CharArrayObjectMap; import org.eclipse.cdt.core.parser.util.CharArrayObjectMap;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
/** /**
* @author aniefer * @author aniefer
@ -41,12 +54,61 @@ public class CScope implements ICScope {
return CVisitor.getContainingScope( physicalNode ); return CVisitor.getContainingScope( physicalNode );
} }
protected static class CollectNamesAction extends ICASTVisitor.CBaseVisitorAction {
private char [] name;
private IASTName [] result = null;
CollectNamesAction( char [] n ){
name = n;
processNames = true;
}
public int processName( IASTName n ){
ASTNodeProperty prop = n.getPropertyInParent();
if( prop == IASTElaboratedTypeSpecifier.TYPE_NAME ||
prop == IASTCompositeTypeSpecifier.TYPE_NAME ||
prop == IASTDeclarator.DECLARATOR_NAME )
{
if( CharArrayUtils.equals( n.toCharArray(), name ) )
result = (IASTName[]) ArrayUtil.append( IASTName.class, result, name );
}
return PROCESS_CONTINUE;
}
public int processStatement( IASTStatement statement ){
if( statement instanceof IASTDeclarationStatement )
return PROCESS_CONTINUE;
return PROCESS_SKIP;
}
public IASTName [] getNames(){
return (IASTName[]) ArrayUtil.trim( IASTName.class, result );
}
}
/* (non-Javadoc) /* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String) * @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String)
*/ */
public IBinding[] find( String name ) { public IBinding[] find( String name ) {
// TODO Auto-generated method stub IASTNode node = getPhysicalNode();
return null; IASTTranslationUnit tu = node.getTranslationUnit();
IASTVisitor visitor = tu.getVisitor();
CollectNamesAction action = new CollectNamesAction( name.toCharArray() );
if( node instanceof IASTTranslationUnit )
visitor.visitTranslationUnit( action );
else if( node instanceof IASTStatement )
visitor.visitStatement( (IASTStatement) node, action );
IASTName [] names = action.getNames();
IBinding [] result = null;
for( int i = 0; i < names.length; i++ ){
IBinding b = names[i].resolveBinding();
if( b == null ) continue;
try {
if( b.getScope() == this )
result = (IBinding[]) ArrayUtil.append( IBinding.class, result, b );
} catch ( DOMException e ) {
}
}
return (IBinding[]) ArrayUtil.trim( IBinding.class, result );
} }
public void addBinding( IBinding binding ) { public void addBinding( IBinding binding ) {

View file

@ -31,6 +31,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.CharArrayObjectMap; import org.eclipse.cdt.core.parser.util.CharArrayObjectMap;
import org.eclipse.cdt.core.parser.util.CharArrayUtils; import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.core.parser.util.ObjectSet;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPSemantics.LookupData; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPSemantics.LookupData;
/** /**
@ -189,8 +190,29 @@ public class CPPClassScope extends CPPScope implements ICPPClassScope {
* @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String) * @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String)
*/ */
public IBinding[] find(String name) { public IBinding[] find(String name) {
// TODO Auto-generated method stub char [] n = name.toCharArray();
return null; if( bindings.containsKey( n ) ){
Object o = bindings.get( n );
if( o instanceof IBinding[] )
return (IBinding[]) ArrayUtil.trim( IBinding.class, (Object[]) o );
return new IBinding[] { (IBinding) o };
}
LookupData data = new LookupData( n );
try {
data.foundItems = CPPSemantics.lookupInScope( data, this, null, null );
} catch ( DOMException e ) {
}
if( data.foundItems != null ){
IASTName [] ns = (IASTName[]) data.foundItems;
ObjectSet set = new ObjectSet( ns.length );
for( int i = 0; i < ns.length && ns[i] != null; i++ ){
set.put( ns[i].resolveBinding() );
}
return (IBinding[]) ArrayUtil.trim( IBinding.class, set.keyArray(), true );
}
return new IBinding[0];
} }
private boolean isConstructorReference( IASTName name ){ private boolean isConstructorReference( IASTName name ){

View file

@ -23,7 +23,10 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace; import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.CharArrayObjectMap; import org.eclipse.cdt.core.parser.util.CharArrayObjectMap;
import org.eclipse.cdt.core.parser.util.ObjectSet;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPSemantics.LookupData;
/** /**
* @author aniefer * @author aniefer
@ -65,8 +68,26 @@ public class CPPFunctionScope extends CPPScope implements ICPPFunctionScope {
* @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String) * @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String)
*/ */
public IBinding[] find(String name) { public IBinding[] find(String name) {
// TODO Auto-generated method stub char [] n = name.toCharArray();
return null; if( labels.containsKey( n ) )
return new IBinding[] { (IBinding) labels.get( n ) };
LookupData data = new LookupData( n );
try {
data.foundItems = CPPSemantics.lookupInScope( data, this, null, null );
} catch ( DOMException e ) {
}
if( data.foundItems != null ){
IASTName [] ns = (IASTName[]) data.foundItems;
ObjectSet set = new ObjectSet( ns.length );
for( int i = 0; i < ns.length && ns[i] != null; i++ ){
set.put( ns[i].resolveBinding() );
}
return (IBinding[]) ArrayUtil.trim( IBinding.class, set.keyArray(), true );
}
return new IBinding[0];
} }
public IScope getParent() throws DOMException { public IScope getParent() throws DOMException {

View file

@ -13,12 +13,15 @@
*/ */
package org.eclipse.cdt.internal.core.dom.parser.cpp; package org.eclipse.cdt.internal.core.dom.parser.cpp;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespaceScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespaceScope;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.CharArrayObjectMap; import org.eclipse.cdt.core.parser.util.CharArrayObjectMap;
import org.eclipse.cdt.core.parser.util.ObjectSet;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPSemantics.LookupData;
/** /**
* @author aniefer * @author aniefer
@ -49,7 +52,6 @@ public class CPPNamespaceScope extends CPPScope implements ICPPNamespaceScope{
} }
} }
private ScopeMap bindings = null; private ScopeMap bindings = null;
//private boolean checkForAdditionalBindings = true;
public CPPNamespaceScope( IASTNode physicalNode ) { public CPPNamespaceScope( IASTNode physicalNode ) {
super( physicalNode ); super( physicalNode );
@ -70,22 +72,6 @@ public class CPPNamespaceScope extends CPPScope implements ICPPNamespaceScope{
} }
} else { } else {
bindings.put( c, binding ); bindings.put( c, binding );
// if( checkForAdditionalBindings ){
// //need to ensure we have all bindings that correspond to this char[]
// checkForAdditionalBindings = false;
// LookupData data = new LookupData( c );
// try {
// data.foundItems = CPPSemantics.lookupInScope( data, this, null, null );
// } catch ( DOMException e ) {
// }
// if( data.foundItems != null ){
// IASTName [] ns = (IASTName[]) data.foundItems;
// for( int i = 0; i < ns.length && ns[i] != null; i++ ){
// ns[i].resolveBinding();
// }
// }
// checkForAdditionalBindings = true;
// }
} }
} }
@ -110,8 +96,29 @@ public class CPPNamespaceScope extends CPPScope implements ICPPNamespaceScope{
* @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String) * @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String)
*/ */
public IBinding[] find(String name) { public IBinding[] find(String name) {
// TODO Auto-generated method stub char [] n = name.toCharArray();
return null; if( bindings.isFullyResolved( n ) ){
Object o = bindings.get( n );
if( o instanceof IBinding[] )
return (IBinding[]) ArrayUtil.trim( IBinding.class, (Object[]) o );
return new IBinding[] { (IBinding) o };
}
LookupData data = new LookupData( n );
try {
data.foundItems = CPPSemantics.lookupInScope( data, this, null, null );
} catch ( DOMException e ) {
}
if( data.foundItems != null ){
IASTName [] ns = (IASTName[]) data.foundItems;
ObjectSet set = new ObjectSet( ns.length );
for( int i = 0; i < ns.length && ns[i] != null; i++ ){
set.put( ns[i].resolveBinding() );
}
return (IBinding[]) ArrayUtil.trim( IBinding.class, set.keyArray(), true );
}
return new IBinding[0];
} }
public void setFullyResolved( IASTName name ){ public void setFullyResolved( IASTName name ){