From 025ae5751da5caaa93766e3d18bc645cb75f1fbe Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Wed, 2 May 2012 11:26:06 -0700 Subject: [PATCH] Bug 377838. Moved the logic from CPPScope to CPPNamespaceScope in accordance with Markus' suggestion. --- .../dom/parser/cpp/CPPNamespaceScope.java | 48 ++++++++++++++++++- .../core/dom/parser/cpp/CPPScope.java | 43 ++--------------- 2 files changed, 51 insertions(+), 40 deletions(-) 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 b3940cd35bb..53afe1585d7 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 @@ -8,6 +8,7 @@ * Contributors: * Andrew Niefer (IBM Corporation) - initial API and implementation * Markus Schorn (Wind River Systems) + * Sergey Prigogin (Google) *******************************************************************************/ package org.eclipse.cdt.internal.core.dom.parser.cpp; @@ -22,17 +23,22 @@ import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.EScopeKind; import org.eclipse.cdt.core.dom.ast.IASTDeclaration; +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.IScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLinkageSpecification; 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.ICPPASTTemplateId; import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace; import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespaceScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPUsingDirective; import org.eclipse.cdt.core.index.IIndex; import org.eclipse.cdt.core.index.IIndexFileSet; import org.eclipse.cdt.core.index.IIndexName; +import org.eclipse.cdt.core.parser.util.CharArrayUtils; +import org.eclipse.cdt.internal.core.dom.parser.ASTInternal; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPScopeMapper.InlineNamespaceDirective; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor; import org.eclipse.cdt.internal.core.index.IIndexScope; @@ -59,7 +65,7 @@ public class CPPNamespaceScope extends CPPScope implements ICPPInternalNamespace @Override public EScopeKind getKind() { if (getPhysicalNode() instanceof IASTTranslationUnit) - return EScopeKind.eGlobal; + return EScopeKind.eGlobal; return EScopeKind.eNamespace; } @@ -100,18 +106,20 @@ public class CPPNamespaceScope extends CPPScope implements ICPPInternalNamespace public IScope findNamespaceScope(IIndexScope scope) { final String[] qname= CPPVisitor.getQualifiedName(scope.getScopeBinding()); - final IScope[] result= {null}; + final IScope[] result= { null }; final ASTVisitor visitor= new ASTVisitor() { private int depth= 0; { shouldVisitNamespaces= shouldVisitDeclarations= true; } + @Override public int visit( IASTDeclaration declaration ){ if (declaration instanceof ICPPASTLinkageSpecification) return PROCESS_CONTINUE; return PROCESS_SKIP; } + @Override public int visit(ICPPASTNamespaceDefinition namespace) { final String name = namespace.getName().toString(); @@ -127,6 +135,7 @@ public class CPPNamespaceScope extends CPPScope implements ICPPInternalNamespace } return PROCESS_SKIP; } + @Override public int leave(ICPPASTNamespaceDefinition namespace) { if (namespace.getName().getLookupKey().length > 0) { @@ -135,10 +144,45 @@ public class CPPNamespaceScope extends CPPScope implements ICPPInternalNamespace return PROCESS_CONTINUE; } }; + getPhysicalNode().accept(visitor); return result[0]; } + @Override + public void addName(IASTName name) { + if (name instanceof ICPPASTQualifiedName && !canDenoteNamespaceMember((ICPPASTQualifiedName) name)) + return; + super.addName(name); + } + + public boolean canDenoteNamespaceMember(ICPPASTQualifiedName name) { + IScope scope= this; + IASTName[] segments= name.getNames(); + try { + for (int i= segments.length - 1; --i >= 0;) { + if (scope == null) + return false; + IName scopeName = scope.getScopeName(); + if (scopeName == null) + return false; + + IASTName segmentName = segments[i]; + if (segmentName instanceof ICPPASTTemplateId || + !CharArrayUtils.equals(scopeName.getSimpleID(), segmentName.getSimpleID())) { + return false; + } + scope= scope.getParent(); + } + if (!name.isFullyQualified() || scope == null) { + return true; + } + return ASTInternal.getPhysicalNodeOfScope(scope) instanceof IASTTranslationUnit; + } catch (DOMException e) { + return false; + } + } + @Override public boolean isInlineNamepace() { if (!fIsInlineInitialized) { 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 df54026c78b..2340c7cc814 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 @@ -38,10 +38,8 @@ import org.eclipse.cdt.core.index.IIndexFileSet; import org.eclipse.cdt.core.index.IndexFilter; 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.IContentAssistMatcher; import org.eclipse.cdt.core.parser.util.ObjectSet; -import org.eclipse.cdt.internal.core.dom.parser.ASTInternal; import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPSemantics; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor; @@ -97,16 +95,12 @@ abstract public class CPPScope implements ICPPASTInternalScope { if (bindings == null) bindings = new CharArrayObjectMap(1); - if (name instanceof ICPPASTQualifiedName) { - if (!(physicalNode instanceof ICPPASTCompositeTypeSpecifier) && - !(physicalNode instanceof ICPPASTNamespaceDefinition)) { - return; - } - - // Name belongs to a different scope, don't add it here except if it names this scope. - if (!canDenoteScopeMember((ICPPASTQualifiedName) name)) - return; + if (name instanceof ICPPASTQualifiedName && + !(physicalNode instanceof ICPPASTCompositeTypeSpecifier) && + !(physicalNode instanceof ICPPASTNamespaceDefinition)) { + return; } + final char[] c= name.getLookupKey(); if (c.length == 0) return; @@ -125,33 +119,6 @@ abstract public class CPPScope implements ICPPASTInternalScope { } } - public boolean canDenoteScopeMember(ICPPASTQualifiedName name) { - IScope scope= this; - IASTName[] segments= name.getNames(); - try { - for (int i= segments.length - 1; --i >= 0;) { - if (scope == null) - return false; - IName scopeName = scope.getScopeName(); - if (scopeName == null) - return false; - - IASTName segmentName = segments[i]; - if ((scopeName instanceof ICPPASTTemplateId) != (segmentName instanceof ICPPASTTemplateId)) - return false; - if (!CharArrayUtils.equals(scopeName.getSimpleID(), segmentName.getSimpleID())) - return false; - scope= scope.getParent(); - } - if (!name.isFullyQualified() || scope == null) { - return true; - } - return ASTInternal.getPhysicalNodeOfScope(scope) instanceof IASTTranslationUnit; - } catch (DOMException e) { - return false; - } - } - @Override public IBinding getBinding(IASTName name, boolean forceResolve, IIndexFileSet fileSet) { IBinding binding= getBindingInAST(name, forceResolve);