From bc520fcaffb0a059c0a6c36e354090af027cbbe0 Mon Sep 17 00:00:00 2001 From: Doug Schaefer Date: Wed, 31 May 2006 17:56:43 +0000 Subject: [PATCH] Bug 144095 - Added hooks to look up things in PDOMCPPNamespace when they aren't found during the regular scope lookup. --- .../core/dom/parser/cpp/CPPScope.java | 19 +++++++- .../core/dom/parser/cpp/CPPSemantics.java | 7 ++- .../core/pdom/dom/cpp/PDOMCPPLinkage.java | 3 ++ .../core/pdom/dom/cpp/PDOMCPPNamespace.java | 48 +++++++++++++++---- .../actions/OpenDeclarationsAction.java | 3 +- 5 files changed, 66 insertions(+), 14 deletions(-) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPScope.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPScope.java index 49455b26a77..c381648c887 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPScope.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPScope.java @@ -13,11 +13,13 @@ */ package org.eclipse.cdt.internal.core.dom.parser.cpp; +import org.eclipse.cdt.core.dom.IPDOM; 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.IScope; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName; import org.eclipse.cdt.core.dom.ast.cpp.ICPPScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPUsingDeclaration; @@ -25,6 +27,7 @@ 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.ProblemBinding; +import org.eclipse.cdt.internal.core.pdom.PDOM; /** * @author aniefer @@ -80,10 +83,10 @@ abstract public class CPPScope implements ICPPScope{ public IBinding getBinding(IASTName name, boolean forceResolve) throws DOMException { char [] c = name.toCharArray(); //can't look up bindings that don't have a name - if( c.length == 0 || bindings == null ) + if( c.length == 0 ) return null; - Object obj = bindings.get( c ); + Object obj = bindings != null ? bindings.get( c ) : null; if( obj != null ){ if( obj instanceof ObjectSet ) { ObjectSet os = (ObjectSet) obj; @@ -121,6 +124,18 @@ abstract public class CPPScope implements ICPPScope{ return binding; } return (IBinding) obj; + } else { + IPDOM pdom = name.getTranslationUnit().getIndex(); + if (pdom != null) { + // Try looking this up in the PDOM + if (physicalNode instanceof ICPPASTNamespaceDefinition) { + ICPPASTNamespaceDefinition nsdef = (ICPPASTNamespaceDefinition)physicalNode; + IASTName nsname = nsdef.getName(); + IBinding nsbinding = ((PDOM)pdom).resolveBinding(nsname); + if (nsbinding instanceof ICPPScope) + return ((ICPPScope)nsbinding).getBinding(name, forceResolve); + } + } } return null; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPSemantics.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPSemantics.java index 61fbe3d5720..0453ff6785b 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPSemantics.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPSemantics.java @@ -126,6 +126,7 @@ import org.eclipse.cdt.core.parser.util.ArrayUtil.ArrayWrapper; import org.eclipse.cdt.internal.core.dom.parser.ASTNode; import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer; import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding; +import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding; /** * @author aniefer @@ -746,7 +747,7 @@ public class CPPSemantics { addDefinition( binding, data.astName ); } } - if( binding == null ){ + if( binding == null || binding instanceof IProblemBinding ){ // Let's try the pdom IPDOM pdom = name.getTranslationUnit().getIndex(); if (pdom != null) { @@ -1025,7 +1026,9 @@ public class CPPSemantics { } else { if (!data.prefixLookup && data.astName != null ) { IBinding b = scope.getBinding( data.astName, false ); - if (b instanceof CPPImplicitFunction || b instanceof CPPImplicitTypedef) + if (b instanceof CPPImplicitFunction || b instanceof CPPImplicitTypedef + || b instanceof PDOMBinding) + // TODO the PDOMBinding thing is a kludge mergeResults( data, b, true ); } mergeResults( data, lookupInScope( data, scope, blockItem ), true ); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPLinkage.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPLinkage.java index c574f92b94f..6828088d2a1 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPLinkage.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPLinkage.java @@ -232,6 +232,9 @@ public class PDOMCPPLinkage extends PDOMLinkage { } public IBinding resolveBinding(IASTName name) throws CoreException { + IBinding origBinding = name.getBinding(); + if (origBinding != null) + return adaptBinding(origBinding); if (name instanceof ICPPASTQualifiedName) { IASTName[] names = ((ICPPASTQualifiedName)name).getNames(); if (names.length == 1) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPNamespace.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPNamespace.java index c3415a4b4f8..db89d085724 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPNamespace.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPNamespace.java @@ -132,8 +132,11 @@ public class PDOMCPPNamespace extends PDOMBinding private static final class FindBinding extends PDOMNamedNode.NodeFinder { PDOMBinding pdomBinding; - final int desiredType; + final int[] desiredType; public FindBinding(PDOM pdom, char[] name, int desiredType) { + this(pdom, name, new int[] { desiredType }); + } + public FindBinding(PDOM pdom, char[] name, int[] desiredType) { super(pdom, name); this.desiredType = desiredType; } @@ -144,13 +147,16 @@ public class PDOMCPPNamespace extends PDOMBinding if (!tBinding.hasName(name)) // no more bindings with our desired name return false; - if (tBinding.getNodeType() != desiredType) - // wrong type, try again - return true; + int nodeType = tBinding.getNodeType(); + for (int i = 0; i < desiredType.length; ++i) + if (nodeType == desiredType[i]) { + // got it + pdomBinding = tBinding; + return false; + } - // got it - pdomBinding = tBinding; - return false; + // wrong type, try again + return true; } } @@ -161,8 +167,32 @@ public class PDOMCPPNamespace extends PDOMBinding return lastName != null ? lastName.resolveBinding() : null; } IASTNode parent = name.getParent(); - if (parent instanceof ICPPASTQualifiedName) - parent = parent.getParent(); + if (parent instanceof ICPPASTQualifiedName) { + IASTName[] names = ((ICPPASTQualifiedName)parent).getNames(); + if (name == names[names.length - 1]) { + parent = parent.getParent(); + } else { + IASTName nsname = null; + for (int i = 0; i < names.length - 2; ++i) { + if (name != names[i]) + nsname = names[i]; + } + // make sure we're the namespace they're talking about + if (nsname != null && !equals(pdom.resolveBinding(nsname))) + return null; + + // Look up the name + FindBinding visitor = new FindBinding(pdom, name.toCharArray(), + new int[] { + PDOMCPPLinkage.CPPCLASSTYPE, + PDOMCPPLinkage.CPPNAMESPACE, + PDOMCPPLinkage.CPPFUNCTION, + PDOMCPPLinkage.CPPVARIABLE + }); + getIndex().accept(visitor); + return visitor.pdomBinding; + } + } if (parent instanceof IASTIdExpression) { // reference IASTNode eParent = parent.getParent(); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsAction.java index 4c39b9dac9e..bb4bcf1f9d5 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsAction.java @@ -16,6 +16,7 @@ import org.eclipse.cdt.core.dom.IPDOM; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IBinding; +import org.eclipse.cdt.core.dom.ast.IProblemBinding; import org.eclipse.cdt.core.model.ILanguage; import org.eclipse.cdt.core.model.IWorkingCopy; import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding; @@ -69,7 +70,7 @@ public class OpenDeclarationsAction extends SelectionParseAction { IASTName searchName = selectedNames[0]; IBinding binding = searchName.resolveBinding(); - if (binding != null) { + if (binding != null && !(binding instanceof IProblemBinding)) { final IASTName[] declNames = ast.getDeclarations(binding); if (declNames.length > 0) { Display.getDefault().asyncExec(new Runnable() {