diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CCompositeTypeScope.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CCompositeTypeScope.java index df6d4f2d6d6..f14f17cd4c3 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CCompositeTypeScope.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CCompositeTypeScope.java @@ -14,13 +14,19 @@ */ 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.IASTTranslationUnit; +import org.eclipse.cdt.core.dom.ast.IASTVisitor; import org.eclipse.cdt.core.dom.ast.IBinding; 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.ICCompositeTypeScope; 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.internal.core.dom.parser.c.CScope.CollectNamesAction; /** * @author aniefer @@ -68,8 +74,26 @@ public class CCompositeTypeScope implements ICCompositeTypeScope { * @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String) */ public IBinding[] find( String name ) { - // TODO Auto-generated method stub - return null; + IASTNode node = getPhysicalNode(); + 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) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CScope.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CScope.java index c131d809cfd..9a5cfccdda8 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CScope.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CScope.java @@ -14,13 +14,26 @@ */ 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.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.ICompositeType; import org.eclipse.cdt.core.dom.ast.IEnumeration; 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.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.CharArrayObjectMap; +import org.eclipse.cdt.core.parser.util.CharArrayUtils; /** * @author aniefer @@ -41,12 +54,61 @@ public class CScope implements ICScope { 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) * @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String) */ public IBinding[] find( String name ) { - // TODO Auto-generated method stub - return null; + IASTNode node = getPhysicalNode(); + 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 ) { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassScope.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassScope.java index 466a3c75ead..33fa3745224 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassScope.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClassScope.java @@ -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.CharArrayObjectMap; 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; /** @@ -189,8 +190,29 @@ public class CPPClassScope extends CPPScope implements ICPPClassScope { * @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String) */ public IBinding[] find(String name) { - // TODO Auto-generated method stub - return null; + char [] n = name.toCharArray(); + 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 ){ diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunctionScope.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunctionScope.java index 79592ffd8f0..65e458aaa38 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunctionScope.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunctionScope.java @@ -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.ICPPFunctionScope; 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.ObjectSet; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPSemantics.LookupData; /** * @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) */ public IBinding[] find(String name) { - // TODO Auto-generated method stub - return null; + char [] n = name.toCharArray(); + 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 { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPNamespaceScope.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPNamespaceScope.java index 2c083dd5a37..eaaca75529e 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPNamespaceScope.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPNamespaceScope.java @@ -13,12 +13,15 @@ */ 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.IASTNode; import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespaceScope; import org.eclipse.cdt.core.parser.util.ArrayUtil; 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 @@ -49,7 +52,6 @@ public class CPPNamespaceScope extends CPPScope implements ICPPNamespaceScope{ } } private ScopeMap bindings = null; - //private boolean checkForAdditionalBindings = true; public CPPNamespaceScope( IASTNode physicalNode ) { super( physicalNode ); @@ -70,22 +72,6 @@ public class CPPNamespaceScope extends CPPScope implements ICPPNamespaceScope{ } } else { 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) */ public IBinding[] find(String name) { - // TODO Auto-generated method stub - return null; + char [] n = name.toCharArray(); + 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 ){