diff options
author | Douglas Gregor <dgregor@apple.com> | 2009-08-25 22:51:20 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2009-08-25 22:51:20 +0000 |
commit | 495c35d291da48c4f5655bbb54d15128ddde0d4d (patch) | |
tree | c7374a4cef2354d3548b4273ab3a067c385888ff /lib/Sema | |
parent | ce3ff2bd3a3386dbc209d3cba4b8769173b274c1 (diff) |
Improve support for out-of-line definitions of nested templates and
their members, including member class template, member function
templates, and member classes and functions of member templates.
To actually parse the nested-name-specifiers that qualify the name of
an out-of-line definition of a member template, e.g.,
template<typename X> template<typename Y>
X Outer<X>::Inner1<Y>::foo(Y) {
return X();
}
we need to look for the template names (e.g., "Inner1") as a member of
the current instantiation (Outer<X>), even before we have entered the
scope of the current instantiation. Since we can't do this in general
(i.e., we should not be looking into all dependent
nested-name-specifiers as if they were the current instantiation), we
rely on the parser to tell us when it is parsing a declaration
specifier sequence, and, therefore, when we should consider the
current scope specifier to be a current instantiation.
Printing of complicated, dependent nested-name-specifiers may be
somewhat broken by this commit; I'll add tests for this issue and fix
the problem (if it still exists) in a subsequent commit.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@80044 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema')
-rw-r--r-- | lib/Sema/Sema.h | 11 | ||||
-rw-r--r-- | lib/Sema/SemaCXXScopeSpec.cpp | 30 | ||||
-rw-r--r-- | lib/Sema/SemaDecl.cpp | 2 | ||||
-rw-r--r-- | lib/Sema/SemaLookup.cpp | 73 | ||||
-rw-r--r-- | lib/Sema/SemaTemplate.cpp | 19 | ||||
-rw-r--r-- | lib/Sema/TreeTransform.h | 5 |
6 files changed, 82 insertions, 58 deletions
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index 03e641780d..9db3fae036 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -1265,7 +1265,8 @@ public: LookupNameKind NameKind, bool RedeclarationOnly = false, bool AllowBuiltinCreation = false, - SourceLocation Loc = SourceLocation()); + SourceLocation Loc = SourceLocation(), + bool EnteringContext = false); ObjCProtocolDecl *LookupProtocol(IdentifierInfo *II); ObjCCategoryImplDecl *LookupObjCCategoryImpl(IdentifierInfo *II); @@ -1951,7 +1952,8 @@ public: const CXXScopeSpec &SS, SourceLocation IdLoc, SourceLocation CCLoc, - IdentifierInfo &II); + IdentifierInfo &II, + bool EnteringContext); /// ActOnCXXNestedNameSpecifier - Called during parsing of a /// nested-name-specifier that involves a template-id, e.g., @@ -2205,8 +2207,9 @@ public: // C++ Templates [C++ 14] // virtual TemplateNameKind isTemplateName(const IdentifierInfo &II, Scope *S, - TemplateTy &Template, - const CXXScopeSpec *SS = 0); + const CXXScopeSpec *SS, + bool EnteringContext, + TemplateTy &Template); bool DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl); TemplateDecl *AdjustDeclIfTemplate(DeclPtrTy &Decl); diff --git a/lib/Sema/SemaCXXScopeSpec.cpp b/lib/Sema/SemaCXXScopeSpec.cpp index 188957176b..9c9bff8a08 100644 --- a/lib/Sema/SemaCXXScopeSpec.cpp +++ b/lib/Sema/SemaCXXScopeSpec.cpp @@ -48,11 +48,11 @@ DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS, return Record; if (EnteringContext) { - // We are entering the context of the nested name specifier, so try to - // match the nested name specifier to either a primary class template - // or a class template partial specialization. if (const TemplateSpecializationType *SpecType = dyn_cast_or_null<TemplateSpecializationType>(NNS->getAsType())) { + // We are entering the context of the nested name specifier, so try to + // match the nested name specifier to either a primary class template + // or a class template partial specialization. if (ClassTemplateDecl *ClassTemplate = dyn_cast_or_null<ClassTemplateDecl>( SpecType->getTemplateName().getAsTemplateDecl())) { @@ -74,6 +74,10 @@ DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS, = ClassTemplate->findPartialSpecialization(ContextType)) return PartialSpec; } + } else if (const RecordType *RecordT + = dyn_cast_or_null<RecordType>(NNS->getAsType())) { + // The nested name specifier refers to a member of a class template. + return RecordT->getDecl(); } std::string NNSString; @@ -260,17 +264,14 @@ Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S, const CXXScopeSpec &SS, SourceLocation IdLoc, SourceLocation CCLoc, - IdentifierInfo &II) { + IdentifierInfo &II, + bool EnteringContext) { NestedNameSpecifier *Prefix = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); - // If the prefix already refers to an unknown specialization, there - // is no name lookup to perform. Just build the resulting - // nested-name-specifier. - if (Prefix && isUnknownSpecialization(SS)) - return NestedNameSpecifier::Create(Context, Prefix, &II); - - NamedDecl *SD = LookupParsedName(S, &SS, &II, LookupNestedNameSpecifierName); + NamedDecl *SD = LookupParsedName(S, &SS, &II, LookupNestedNameSpecifierName, + false, false, SourceLocation(), + EnteringContext); if (SD) { if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) @@ -303,13 +304,16 @@ Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S, // Fall through to produce an error: we found something that isn't // a class or a namespace. - } + } else if (SS.isSet() && isDependentScopeSpecifier(SS)) + return NestedNameSpecifier::Create(Context, Prefix, &II); // If we didn't find anything during our lookup, try again with // ordinary name lookup, which can help us produce better error // messages. if (!SD) - SD = LookupParsedName(S, &SS, &II, LookupOrdinaryName); + SD = LookupParsedName(S, &SS, &II, LookupOrdinaryName, + false, false, SourceLocation(), + EnteringContext); unsigned DiagID; if (SD) DiagID = diag::err_expected_class_or_namespace; diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 61f0ce808a..d478c048b8 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -72,7 +72,7 @@ Sema::TypeTy *Sema::getTypeName(IdentifierInfo &II, SourceLocation NameLoc, // refer to a member of an unknown specialization. if (SS && isUnknownSpecialization(*SS)) return 0; - + LookupResult Result = LookupParsedName(S, SS, &II, LookupOrdinaryName, false, false); diff --git a/lib/Sema/SemaLookup.cpp b/lib/Sema/SemaLookup.cpp index 1d583cc391..6627499d12 100644 --- a/lib/Sema/SemaLookup.cpp +++ b/lib/Sema/SemaLookup.cpp @@ -1116,7 +1116,7 @@ Sema::LookupQualifiedName(DeclContext *LookupCtx, DeclarationName Name, /// @param S The scope from which unqualified name lookup will /// begin. /// -/// @param SS An optional C++ scope-specified, e.g., "::N::M". +/// @param SS An optional C++ scope-specifier, e.g., "::N::M". /// /// @param Name The name of the entity that name lookup will /// search for. @@ -1125,49 +1125,56 @@ Sema::LookupQualifiedName(DeclContext *LookupCtx, DeclarationName Name, /// name lookup. At present, this is only used to produce diagnostics when /// C library functions (like "malloc") are implicitly declared. /// +/// @param EnteringContext Indicates whether we are going to enter the +/// context of the scope-specifier SS (if present). +/// /// @returns The result of qualified or unqualified name lookup. Sema::LookupResult Sema::LookupParsedName(Scope *S, const CXXScopeSpec *SS, DeclarationName Name, LookupNameKind NameKind, bool RedeclarationOnly, bool AllowBuiltinCreation, - SourceLocation Loc) { - if (SS && (SS->isSet() || SS->isInvalid())) { - // If the scope specifier is invalid, don't even look for + SourceLocation Loc, + bool EnteringContext) { + if (SS && SS->isInvalid()) { + // When the scope specifier is invalid, don't even look for // anything. - if (SS->isInvalid()) - return LookupResult::CreateLookupResult(Context, 0); - - assert(!isUnknownSpecialization(*SS) && "Can't lookup dependent types"); - - if (isDependentScopeSpecifier(*SS)) { - // Determine whether we are looking into the current - // instantiation. - NestedNameSpecifier *NNS - = static_cast<NestedNameSpecifier *>(SS->getScopeRep()); - CXXRecordDecl *Current = getCurrentInstantiationOf(NNS); - assert(Current && "Bad dependent scope specifier"); + return LookupResult::CreateLookupResult(Context, 0); + } + + if (SS && SS->isSet()) { + if (DeclContext *DC = computeDeclContext(*SS, EnteringContext)) { + // We have resolved the scope specifier to a particular declaration + // contex, and will perform name lookup in that context. - // We nested name specifier refers to the current instantiation, - // so now we will look for a member of the current instantiation - // (C++0x [temp.dep.type]). - unsigned IDNS = getIdentifierNamespacesFromLookupNameKind(NameKind, true); - DeclContext::lookup_iterator I, E; - for (llvm::tie(I, E) = Current->lookup(Name); I != E; ++I) - if (isAcceptableLookupResult(*I, NameKind, IDNS)) - return LookupResult::CreateLookupResult(Context, I, E); + if (DC->isDependentContext()) { + // If this is a dependent context, then we are looking for a member of + // the current instantiation. This is a narrow search that looks into + // just the described declaration context (C++0x [temp.dep.type]). + unsigned IDNS = getIdentifierNamespacesFromLookupNameKind(NameKind, + true); + DeclContext::lookup_iterator I, E; + for (llvm::tie(I, E) = DC->lookup(Name); I != E; ++I) + if (isAcceptableLookupResult(*I, NameKind, IDNS)) + return LookupResult::CreateLookupResult(Context, I, E); + } + + // Qualified name lookup into the named declaration context. + // The declaration context must be complete. + if (RequireCompleteDeclContext(*SS)) + return LookupResult::CreateLookupResult(Context, 0); + + return LookupQualifiedName(DC, Name, NameKind, RedeclarationOnly); } - if (RequireCompleteDeclContext(*SS)) - return LookupResult::CreateLookupResult(Context, 0); - - return LookupQualifiedName(computeDeclContext(*SS), - Name, NameKind, RedeclarationOnly); + // We could not resolve the scope specified to a specific declaration + // context, which means that SS refers to an unknown specialization. + // Name lookup can't find anything in this case. + return LookupResult::CreateLookupResult(Context, 0); } - LookupResult result(LookupName(S, Name, NameKind, RedeclarationOnly, - AllowBuiltinCreation, Loc)); - - return(result); + // Perform unqualified name lookup starting in the given scope. + return LookupName(S, Name, NameKind, RedeclarationOnly, AllowBuiltinCreation, + Loc); } diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp index 9dfa96288c..e0798f685b 100644 --- a/lib/Sema/SemaTemplate.cpp +++ b/lib/Sema/SemaTemplate.cpp @@ -27,10 +27,19 @@ using namespace clang; /// passed to indicate the C++ scope in which the identifier will be /// found. TemplateNameKind Sema::isTemplateName(const IdentifierInfo &II, Scope *S, - TemplateTy &TemplateResult, - const CXXScopeSpec *SS) { - NamedDecl *IIDecl = LookupParsedName(S, SS, &II, LookupOrdinaryName); - + const CXXScopeSpec *SS, + bool EnteringContext, + TemplateTy &TemplateResult) { + LookupResult Found = LookupParsedName(S, SS, &II, LookupOrdinaryName, + false, false, SourceLocation(), + EnteringContext); + + // FIXME: Cope with ambiguous name-lookup results. + assert(!Found.isAmbiguous() && + "Cannot handle template name-lookup ambiguities"); + + NamedDecl *IIDecl = Found; + TemplateNameKind TNK = TNK_Non_template; TemplateDecl *Template = 0; @@ -1116,7 +1125,7 @@ Sema::ActOnDependentTemplateName(SourceLocation TemplateKWLoc, // "template" keyword is now permitted). We follow the C++0x // rules, even in C++03 mode, retroactively applying the DR. TemplateTy Template; - TemplateNameKind TNK = isTemplateName(Name, 0, Template, &SS); + TemplateNameKind TNK = isTemplateName(Name, 0, &SS, false, Template); if (TNK == TNK_Non_template) { Diag(NameLoc, diag::err_template_kw_refers_to_non_template) << &Name; diff --git a/lib/Sema/TreeTransform.h b/lib/Sema/TreeTransform.h index 8220ec33c2..5d34e07303 100644 --- a/lib/Sema/TreeTransform.h +++ b/lib/Sema/TreeTransform.h @@ -4378,7 +4378,8 @@ TreeTransform<Derived>::RebuildNestedNameSpecifier(NestedNameSpecifier *Prefix, SS.setScopeRep(Prefix); return static_cast<NestedNameSpecifier *>( SemaRef.ActOnCXXNestedNameSpecifier(0, SS, Range.getEnd(), - Range.getEnd(), II)); + Range.getEnd(), II, + false)); } template<typename Derived> @@ -4435,7 +4436,7 @@ TreeTransform<Derived>::RebuildTemplateName(NestedNameSpecifier *Qualifier, SS.setRange(SourceRange(getDerived().getBaseLocation())); SS.setScopeRep(Qualifier); Sema::TemplateTy Template; - TemplateNameKind TNK = SemaRef.isTemplateName(II, 0, Template, &SS); + TemplateNameKind TNK = SemaRef.isTemplateName(II, 0, &SS, false, Template); if (TNK == TNK_Non_template) { SemaRef.Diag(getDerived().getBaseLocation(), diag::err_template_kw_refers_to_non_template) |