Cedric Venet | 08438133 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 1 | //===--- SemaCXXScopeSpec.cpp - Semantic Analysis for C++ scope specifiers-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements C++ semantic analysis for scope specifiers. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Lookup.h" |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Template.h" |
Cedric Venet | 08438133 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 20 | #include "clang/AST/NestedNameSpecifier.h" |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 21 | #include "clang/Basic/PartialDiagnostic.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 22 | #include "clang/Sema/DeclSpec.h" |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 23 | #include "TypeLocBuilder.h" |
Cedric Venet | 08438133 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 168190d | 2009-07-22 00:28:09 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Cedric Venet | 08438133 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
Douglas Gregor | 4112718 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 28 | /// \brief Find the current instantiation that associated with the given type. |
Douglas Gregor | bf2b26d | 2011-02-19 19:24:40 +0000 | [diff] [blame] | 29 | static CXXRecordDecl *getCurrentInstantiationOf(QualType T, |
| 30 | DeclContext *CurContext) { |
Douglas Gregor | 4112718 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 31 | if (T.isNull()) |
| 32 | return 0; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 33 | |
| 34 | const Type *Ty = T->getCanonicalTypeInternal().getTypePtr(); |
Douglas Gregor | bf2b26d | 2011-02-19 19:24:40 +0000 | [diff] [blame] | 35 | if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { |
| 36 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 37 | if (!T->isDependentType()) |
| 38 | return Record; |
| 39 | |
| 40 | // This may be a member of a class template or class template partial |
| 41 | // specialization. If it's part of the current semantic context, then it's |
| 42 | // an injected-class-name; |
| 43 | for (; !CurContext->isFileContext(); CurContext = CurContext->getParent()) |
| 44 | if (CurContext->Equals(Record)) |
| 45 | return Record; |
| 46 | |
| 47 | return 0; |
| 48 | } else if (isa<InjectedClassNameType>(Ty)) |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 49 | return cast<InjectedClassNameType>(Ty)->getDecl(); |
| 50 | else |
| 51 | return 0; |
Douglas Gregor | 4112718 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 54 | /// \brief Compute the DeclContext that is associated with the given type. |
| 55 | /// |
| 56 | /// \param T the type for which we are attempting to find a DeclContext. |
| 57 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | /// \returns the declaration context represented by the type T, |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 59 | /// or NULL if the declaration context cannot be computed (e.g., because it is |
| 60 | /// dependent and not the current instantiation). |
| 61 | DeclContext *Sema::computeDeclContext(QualType T) { |
Douglas Gregor | bf2b26d | 2011-02-19 19:24:40 +0000 | [diff] [blame] | 62 | if (!T->isDependentType()) |
| 63 | if (const TagType *Tag = T->getAs<TagType>()) |
| 64 | return Tag->getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Douglas Gregor | bf2b26d | 2011-02-19 19:24:40 +0000 | [diff] [blame] | 66 | return ::getCurrentInstantiationOf(T, CurContext); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 69 | /// \brief Compute the DeclContext that is associated with the given |
| 70 | /// scope specifier. |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 71 | /// |
| 72 | /// \param SS the C++ scope specifier as it appears in the source |
| 73 | /// |
| 74 | /// \param EnteringContext when true, we will be entering the context of |
| 75 | /// this scope specifier, so we can retrieve the declaration context of a |
| 76 | /// class template or class template partial specialization even if it is |
| 77 | /// not the current instantiation. |
| 78 | /// |
| 79 | /// \returns the declaration context represented by the scope specifier @p SS, |
| 80 | /// or NULL if the declaration context cannot be computed (e.g., because it is |
| 81 | /// dependent and not the current instantiation). |
| 82 | DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS, |
| 83 | bool EnteringContext) { |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 84 | if (!SS.isSet() || SS.isInvalid()) |
Douglas Gregor | 6bfde49 | 2009-03-18 00:36:05 +0000 | [diff] [blame] | 85 | return 0; |
Douglas Gregor | 6bfde49 | 2009-03-18 00:36:05 +0000 | [diff] [blame] | 86 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | NestedNameSpecifier *NNS |
Douglas Gregor | c23500e | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 88 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 89 | if (NNS->isDependent()) { |
| 90 | // If this nested-name-specifier refers to the current |
| 91 | // instantiation, return its DeclContext. |
| 92 | if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS)) |
| 93 | return Record; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 95 | if (EnteringContext) { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 96 | const Type *NNSType = NNS->getAsType(); |
| 97 | if (!NNSType) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | // Look through type alias templates, per C++0x [temp.dep.type]p1. |
| 102 | NNSType = Context.getCanonicalType(NNSType); |
| 103 | if (const TemplateSpecializationType *SpecType |
| 104 | = NNSType->getAs<TemplateSpecializationType>()) { |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 105 | // We are entering the context of the nested name specifier, so try to |
| 106 | // match the nested name specifier to either a primary class template |
| 107 | // or a class template partial specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 109 | = dyn_cast_or_null<ClassTemplateDecl>( |
| 110 | SpecType->getTemplateName().getAsTemplateDecl())) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 111 | QualType ContextType |
| 112 | = Context.getCanonicalType(QualType(SpecType, 0)); |
| 113 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 114 | // If the type of the nested name specifier is the same as the |
| 115 | // injected class name of the named class template, we're entering |
| 116 | // into that class template definition. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 117 | QualType Injected |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 118 | = ClassTemplate->getInjectedClassNameSpecialization(); |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 119 | if (Context.hasSameType(Injected, ContextType)) |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 120 | return ClassTemplate->getTemplatedDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 122 | // If the type of the nested name specifier is the same as the |
| 123 | // type of one of the class template's class template partial |
| 124 | // specializations, we're entering into the definition of that |
| 125 | // class template partial specialization. |
| 126 | if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 127 | = ClassTemplate->findPartialSpecialization(ContextType)) |
| 128 | return PartialSpec; |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 129 | } |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 130 | } else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) { |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 131 | // The nested name specifier refers to a member of a class template. |
| 132 | return RecordT->getDecl(); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 136 | return 0; |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 137 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 138 | |
| 139 | switch (NNS->getKind()) { |
| 140 | case NestedNameSpecifier::Identifier: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 141 | llvm_unreachable("Dependent nested-name-specifier has no DeclContext"); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 142 | |
| 143 | case NestedNameSpecifier::Namespace: |
| 144 | return NNS->getAsNamespace(); |
| 145 | |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 146 | case NestedNameSpecifier::NamespaceAlias: |
| 147 | return NNS->getAsNamespaceAlias()->getNamespace(); |
| 148 | |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 149 | case NestedNameSpecifier::TypeSpec: |
| 150 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
Douglas Gregor | cd3f49f | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 151 | const TagType *Tag = NNS->getAsType()->getAs<TagType>(); |
| 152 | assert(Tag && "Non-tag type in nested-name-specifier"); |
| 153 | return Tag->getDecl(); |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 154 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 155 | |
| 156 | case NestedNameSpecifier::Global: |
| 157 | return Context.getTranslationUnitDecl(); |
| 158 | } |
| 159 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 160 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 6bfde49 | 2009-03-18 00:36:05 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 163 | bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) { |
| 164 | if (!SS.isSet() || SS.isInvalid()) |
| 165 | return false; |
| 166 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | NestedNameSpecifier *NNS |
Douglas Gregor | c23500e | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 168 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 169 | return NNS->isDependent(); |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 172 | // \brief Determine whether this C++ scope specifier refers to an |
| 173 | // unknown specialization, i.e., a dependent type that is not the |
| 174 | // current instantiation. |
| 175 | bool Sema::isUnknownSpecialization(const CXXScopeSpec &SS) { |
| 176 | if (!isDependentScopeSpecifier(SS)) |
| 177 | return false; |
| 178 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | NestedNameSpecifier *NNS |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 180 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 181 | return getCurrentInstantiationOf(NNS) == 0; |
| 182 | } |
| 183 | |
| 184 | /// \brief If the given nested name specifier refers to the current |
| 185 | /// instantiation, return the declaration that corresponds to that |
| 186 | /// current instantiation (C++0x [temp.dep.type]p1). |
| 187 | /// |
| 188 | /// \param NNS a dependent nested name specifier. |
| 189 | CXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 190 | assert(getLangOpts().CPlusPlus && "Only callable in C++"); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 191 | assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed"); |
| 192 | |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 193 | if (!NNS->getAsType()) |
| 194 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Douglas Gregor | b9a955d | 2009-07-31 18:32:42 +0000 | [diff] [blame] | 196 | QualType T = QualType(NNS->getAsType(), 0); |
Douglas Gregor | bf2b26d | 2011-02-19 19:24:40 +0000 | [diff] [blame] | 197 | return ::getCurrentInstantiationOf(T, CurContext); |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Douglas Gregor | 2689746 | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 200 | /// \brief Require that the context specified by SS be complete. |
| 201 | /// |
| 202 | /// If SS refers to a type, this routine checks whether the type is |
| 203 | /// complete enough (or can be made complete enough) for name lookup |
| 204 | /// into the DeclContext. A type that is not yet completed can be |
| 205 | /// considered "complete enough" if it is a class/struct/union/enum |
| 206 | /// that is currently being defined. Or, if we have a type that names |
| 207 | /// a class template specialization that is not a complete type, we |
| 208 | /// will attempt to instantiate that class template. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 209 | bool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS, |
| 210 | DeclContext *DC) { |
| 211 | assert(DC != 0 && "given null context"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 213 | TagDecl *tag = dyn_cast<TagDecl>(DC); |
Douglas Gregor | 8a6d15d | 2010-02-05 04:39:02 +0000 | [diff] [blame] | 214 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 215 | // If this is a dependent type, then we consider it complete. |
| 216 | if (!tag || tag->isDependentContext()) |
| 217 | return false; |
Douglas Gregor | 2689746 | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 218 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 219 | // If we're currently defining this type, then lookup into the |
| 220 | // type is okay: don't complain that it isn't complete yet. |
| 221 | QualType type = Context.getTypeDeclType(tag); |
| 222 | const TagType *tagType = type->getAs<TagType>(); |
| 223 | if (tagType && tagType->isBeingDefined()) |
| 224 | return false; |
John McCall | 2187876 | 2011-07-06 06:57:57 +0000 | [diff] [blame] | 225 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 226 | SourceLocation loc = SS.getLastQualifierNameLoc(); |
| 227 | if (loc.isInvalid()) loc = SS.getRange().getBegin(); |
John McCall | 2187876 | 2011-07-06 06:57:57 +0000 | [diff] [blame] | 228 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 229 | // The type must be complete. |
| 230 | if (RequireCompleteType(loc, type, |
| 231 | PDiag(diag::err_incomplete_nested_name_spec) |
| 232 | << SS.getRange())) { |
| 233 | SS.SetInvalid(SS.getRange()); |
| 234 | return true; |
Douglas Gregor | 2689746 | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 237 | // Fixed enum types are complete, but they aren't valid as scopes |
| 238 | // until we see a definition, so awkwardly pull out this special |
| 239 | // case. |
| 240 | const EnumType *enumType = dyn_cast_or_null<EnumType>(tagType); |
| 241 | if (!enumType || enumType->getDecl()->isCompleteDefinition()) |
| 242 | return false; |
| 243 | |
| 244 | // Try to instantiate the definition, if this is a specialization of an |
| 245 | // enumeration temploid. |
| 246 | EnumDecl *ED = enumType->getDecl(); |
| 247 | if (EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) { |
| 248 | MemberSpecializationInfo *MSI = ED->getMemberSpecializationInfo(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame^] | 249 | if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) { |
| 250 | if (InstantiateEnum(loc, ED, Pattern, getTemplateInstantiationArgs(ED), |
| 251 | TSK_ImplicitInstantiation)) { |
| 252 | SS.SetInvalid(SS.getRange()); |
| 253 | return true; |
| 254 | } |
| 255 | return false; |
| 256 | } |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | Diag(loc, diag::err_incomplete_nested_name_spec) |
| 260 | << type << SS.getRange(); |
| 261 | SS.SetInvalid(SS.getRange()); |
| 262 | return true; |
Douglas Gregor | 2689746 | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 263 | } |
Cedric Venet | 08438133 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 264 | |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 265 | bool Sema::ActOnCXXGlobalScopeSpecifier(Scope *S, SourceLocation CCLoc, |
| 266 | CXXScopeSpec &SS) { |
| 267 | SS.MakeGlobal(Context, CCLoc); |
| 268 | return false; |
Cedric Venet | 08438133 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 271 | /// \brief Determines whether the given declaration is an valid acceptable |
| 272 | /// result for name lookup of a nested-name-specifier. |
Douglas Gregor | cd3f49f | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 273 | bool Sema::isAcceptableNestedNameSpecifier(NamedDecl *SD) { |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 274 | if (!SD) |
| 275 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 277 | // Namespace and namespace aliases are fine. |
| 278 | if (isa<NamespaceDecl>(SD) || isa<NamespaceAliasDecl>(SD)) |
| 279 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 281 | if (!isa<TypeDecl>(SD)) |
| 282 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 284 | // Determine whether we have a class (or, in C++11, an enum) or |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 285 | // a typedef thereof. If so, build the nested-name-specifier. |
| 286 | QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD)); |
| 287 | if (T->isDependentType()) |
| 288 | return true; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 289 | else if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) { |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 290 | if (TD->getUnderlyingType()->isRecordType() || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 291 | (Context.getLangOpts().CPlusPlus0x && |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 292 | TD->getUnderlyingType()->isEnumeralType())) |
| 293 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | } else if (isa<RecordDecl>(SD) || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 295 | (Context.getLangOpts().CPlusPlus0x && isa<EnumDecl>(SD))) |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 296 | return true; |
| 297 | |
| 298 | return false; |
| 299 | } |
| 300 | |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 301 | /// \brief If the given nested-name-specifier begins with a bare identifier |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | /// (e.g., Base::), perform name lookup for that identifier as a |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 303 | /// nested-name-specifier within the given scope, and return the result of that |
| 304 | /// name lookup. |
| 305 | NamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) { |
| 306 | if (!S || !NNS) |
| 307 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 309 | while (NNS->getPrefix()) |
| 310 | NNS = NNS->getPrefix(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 312 | if (NNS->getKind() != NestedNameSpecifier::Identifier) |
| 313 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 315 | LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(), |
| 316 | LookupNestedNameSpecifierName); |
| 317 | LookupName(Found, S); |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 318 | assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet"); |
| 319 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 320 | if (!Found.isSingleResult()) |
| 321 | return 0; |
| 322 | |
| 323 | NamedDecl *Result = Found.getFoundDecl(); |
Douglas Gregor | cd3f49f | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 324 | if (isAcceptableNestedNameSpecifier(Result)) |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 325 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 327 | return 0; |
| 328 | } |
| 329 | |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 330 | bool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS, |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 331 | SourceLocation IdLoc, |
| 332 | IdentifierInfo &II, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 333 | ParsedType ObjectTypePtr) { |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 334 | QualType ObjectType = GetTypeFromParser(ObjectTypePtr); |
| 335 | LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName); |
| 336 | |
| 337 | // Determine where to perform name lookup |
| 338 | DeclContext *LookupCtx = 0; |
| 339 | bool isDependent = false; |
| 340 | if (!ObjectType.isNull()) { |
| 341 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 342 | // x->B::f, and we are looking into the type of the object. |
| 343 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 344 | LookupCtx = computeDeclContext(ObjectType); |
| 345 | isDependent = ObjectType->isDependentType(); |
| 346 | } else if (SS.isSet()) { |
| 347 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 348 | // so long into the context associated with the prior nested-name-specifier. |
| 349 | LookupCtx = computeDeclContext(SS, false); |
| 350 | isDependent = isDependentScopeSpecifier(SS); |
| 351 | Found.setContextRange(SS.getRange()); |
| 352 | } |
| 353 | |
| 354 | if (LookupCtx) { |
| 355 | // Perform "qualified" name lookup into the declaration context we |
| 356 | // computed, which is either the type of the base of a member access |
| 357 | // expression or the declaration context associated with a prior |
| 358 | // nested-name-specifier. |
| 359 | |
| 360 | // The declaration context must be complete. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 361 | if (!LookupCtx->isDependentContext() && |
| 362 | RequireCompleteDeclContext(SS, LookupCtx)) |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 363 | return false; |
| 364 | |
| 365 | LookupQualifiedName(Found, LookupCtx); |
| 366 | } else if (isDependent) { |
| 367 | return false; |
| 368 | } else { |
| 369 | LookupName(Found, S); |
| 370 | } |
| 371 | Found.suppressDiagnostics(); |
| 372 | |
| 373 | if (NamedDecl *ND = Found.getAsSingle<NamedDecl>()) |
| 374 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
| 375 | |
| 376 | return false; |
| 377 | } |
| 378 | |
Kaelyn Uhrain | fb96ec7 | 2012-01-12 22:32:39 +0000 | [diff] [blame] | 379 | namespace { |
| 380 | |
| 381 | // Callback to only accept typo corrections that can be a valid C++ member |
| 382 | // intializer: either a non-static field member or a base class. |
| 383 | class NestedNameSpecifierValidatorCCC : public CorrectionCandidateCallback { |
| 384 | public: |
| 385 | explicit NestedNameSpecifierValidatorCCC(Sema &SRef) |
| 386 | : SRef(SRef) {} |
| 387 | |
| 388 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 389 | return SRef.isAcceptableNestedNameSpecifier(candidate.getCorrectionDecl()); |
| 390 | } |
| 391 | |
| 392 | private: |
| 393 | Sema &SRef; |
| 394 | }; |
| 395 | |
| 396 | } |
| 397 | |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 398 | /// \brief Build a new nested-name-specifier for "identifier::", as described |
| 399 | /// by ActOnCXXNestedNameSpecifier. |
| 400 | /// |
| 401 | /// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in |
| 402 | /// that it contains an extra parameter \p ScopeLookupResult, which provides |
| 403 | /// the result of name lookup within the scope of the nested-name-specifier |
Douglas Gregor | ad183ac | 2009-12-30 16:01:52 +0000 | [diff] [blame] | 404 | /// that was computed at template definition time. |
Chris Lattner | 1c42803 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 405 | /// |
| 406 | /// If ErrorRecoveryLookup is true, then this call is used to improve error |
| 407 | /// recovery. This means that it should not emit diagnostics, it should |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 408 | /// just return true on failure. It also means it should only return a valid |
Chris Lattner | 1c42803 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 409 | /// scope if it *knows* that the result is correct. It should not return in a |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 410 | /// dependent context, for example. Nor will it extend \p SS with the scope |
| 411 | /// specifier. |
| 412 | bool Sema::BuildCXXNestedNameSpecifier(Scope *S, |
| 413 | IdentifierInfo &Identifier, |
| 414 | SourceLocation IdentifierLoc, |
| 415 | SourceLocation CCLoc, |
| 416 | QualType ObjectType, |
| 417 | bool EnteringContext, |
| 418 | CXXScopeSpec &SS, |
| 419 | NamedDecl *ScopeLookupResult, |
| 420 | bool ErrorRecoveryLookup) { |
| 421 | LookupResult Found(*this, &Identifier, IdentifierLoc, |
| 422 | LookupNestedNameSpecifierName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 423 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 424 | // Determine where to perform name lookup |
| 425 | DeclContext *LookupCtx = 0; |
| 426 | bool isDependent = false; |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 427 | if (!ObjectType.isNull()) { |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 428 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 429 | // x->B::f, and we are looking into the type of the object. |
| 430 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 431 | LookupCtx = computeDeclContext(ObjectType); |
| 432 | isDependent = ObjectType->isDependentType(); |
| 433 | } else if (SS.isSet()) { |
| 434 | // This nested-name-specifier occurs after another nested-name-specifier, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 435 | // so look into the context associated with the prior nested-name-specifier. |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 436 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 437 | isDependent = isDependentScopeSpecifier(SS); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 438 | Found.setContextRange(SS.getRange()); |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 439 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 440 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 441 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 442 | bool ObjectTypeSearchedInScope = false; |
| 443 | if (LookupCtx) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | // Perform "qualified" name lookup into the declaration context we |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 445 | // computed, which is either the type of the base of a member access |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 446 | // expression or the declaration context associated with a prior |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 447 | // nested-name-specifier. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 448 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 449 | // The declaration context must be complete. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 450 | if (!LookupCtx->isDependentContext() && |
| 451 | RequireCompleteDeclContext(SS, LookupCtx)) |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 452 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 454 | LookupQualifiedName(Found, LookupCtx); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 456 | if (!ObjectType.isNull() && Found.empty()) { |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 457 | // C++ [basic.lookup.classref]p4: |
| 458 | // If the id-expression in a class member access is a qualified-id of |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | // the form |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 460 | // |
| 461 | // class-name-or-namespace-name::... |
| 462 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | // the class-name-or-namespace-name following the . or -> operator is |
| 464 | // looked up both in the context of the entire postfix-expression and in |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 465 | // the scope of the class of the object expression. If the name is found |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 466 | // only in the scope of the class of the object expression, the name |
| 467 | // shall refer to a class-name. If the name is found only in the |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 468 | // context of the entire postfix-expression, the name shall refer to a |
| 469 | // class-name or namespace-name. [...] |
| 470 | // |
| 471 | // Qualified name lookup into a class will not find a namespace-name, |
Douglas Gregor | 9d07dfa | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 472 | // so we do not need to diagnose that case specifically. However, |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 473 | // this qualified name lookup may find nothing. In that case, perform |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | // unqualified name lookup in the given scope (if available) or |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 475 | // reconstruct the result from when name lookup was performed at template |
| 476 | // definition time. |
| 477 | if (S) |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 478 | LookupName(Found, S); |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 479 | else if (ScopeLookupResult) |
| 480 | Found.addDecl(ScopeLookupResult); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 481 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 482 | ObjectTypeSearchedInScope = true; |
| 483 | } |
Douglas Gregor | df65c8ed | 2010-07-28 14:49:07 +0000 | [diff] [blame] | 484 | } else if (!isDependent) { |
| 485 | // Perform unqualified name lookup in the current scope. |
| 486 | LookupName(Found, S); |
| 487 | } |
| 488 | |
| 489 | // If we performed lookup into a dependent context and did not find anything, |
| 490 | // that's fine: just build a dependent nested-name-specifier. |
| 491 | if (Found.empty() && isDependent && |
| 492 | !(LookupCtx && LookupCtx->isRecord() && |
| 493 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 494 | !cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()))) { |
Chris Lattner | 1c42803 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 495 | // Don't speculate if we're just trying to improve error recovery. |
| 496 | if (ErrorRecoveryLookup) |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 497 | return true; |
Chris Lattner | 1c42803 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 498 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 499 | // We were not able to compute the declaration context for a dependent |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 500 | // base object type or prior nested-name-specifier, so this |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 501 | // nested-name-specifier refers to an unknown specialization. Just build |
| 502 | // a dependent nested-name-specifier. |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 503 | SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc); |
| 504 | return false; |
Douglas Gregor | df65c8ed | 2010-07-28 14:49:07 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 507 | // FIXME: Deal with ambiguities cleanly. |
Douglas Gregor | 532e68f | 2009-12-31 08:26:35 +0000 | [diff] [blame] | 508 | |
| 509 | if (Found.empty() && !ErrorRecoveryLookup) { |
| 510 | // We haven't found anything, and we're not recovering from a |
| 511 | // different kind of error, so look for typos. |
| 512 | DeclarationName Name = Found.getLookupName(); |
Kaelyn Uhrain | fb96ec7 | 2012-01-12 22:32:39 +0000 | [diff] [blame] | 513 | NestedNameSpecifierValidatorCCC Validator(*this); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 514 | TypoCorrection Corrected; |
| 515 | Found.clear(); |
| 516 | if ((Corrected = CorrectTypo(Found.getLookupNameInfo(), |
Kaelyn Uhrain | 4e8942c | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 517 | Found.getLookupKind(), S, &SS, Validator, |
Kaelyn Uhrain | fb96ec7 | 2012-01-12 22:32:39 +0000 | [diff] [blame] | 518 | LookupCtx, EnteringContext))) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 519 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 520 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOpts())); |
Douglas Gregor | 532e68f | 2009-12-31 08:26:35 +0000 | [diff] [blame] | 521 | if (LookupCtx) |
| 522 | Diag(Found.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 523 | << Name << LookupCtx << CorrectedQuotedStr << SS.getRange() |
| 524 | << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr); |
Douglas Gregor | 532e68f | 2009-12-31 08:26:35 +0000 | [diff] [blame] | 525 | else |
| 526 | Diag(Found.getNameLoc(), diag::err_undeclared_var_use_suggest) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 527 | << Name << CorrectedQuotedStr |
| 528 | << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr); |
Douglas Gregor | 6da8362 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 529 | |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 530 | if (NamedDecl *ND = Corrected.getCorrectionDecl()) { |
| 531 | Diag(ND->getLocation(), diag::note_previous_decl) << CorrectedQuotedStr; |
| 532 | Found.addDecl(ND); |
| 533 | } |
| 534 | Found.setLookupName(Corrected.getCorrection()); |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 535 | } else { |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 536 | Found.setLookupName(&Identifier); |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 537 | } |
Douglas Gregor | 532e68f | 2009-12-31 08:26:35 +0000 | [diff] [blame] | 538 | } |
| 539 | |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 540 | NamedDecl *SD = Found.getAsSingle<NamedDecl>(); |
Douglas Gregor | cd3f49f | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 541 | if (isAcceptableNestedNameSpecifier(SD)) { |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 542 | if (!ObjectType.isNull() && !ObjectTypeSearchedInScope) { |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 543 | // C++ [basic.lookup.classref]p4: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | // [...] If the name is found in both contexts, the |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 545 | // class-name-or-namespace-name shall refer to the same entity. |
| 546 | // |
| 547 | // We already found the name in the scope of the object. Now, look |
| 548 | // into the current scope (the scope of the postfix-expression) to |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 549 | // see if we can find the same name there. As above, if there is no |
| 550 | // scope, reconstruct the result from the template instantiation itself. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 551 | NamedDecl *OuterDecl; |
| 552 | if (S) { |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 553 | LookupResult FoundOuter(*this, &Identifier, IdentifierLoc, |
| 554 | LookupNestedNameSpecifierName); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 555 | LookupName(FoundOuter, S); |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 556 | OuterDecl = FoundOuter.getAsSingle<NamedDecl>(); |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 557 | } else |
| 558 | OuterDecl = ScopeLookupResult; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 559 | |
Douglas Gregor | cd3f49f | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 560 | if (isAcceptableNestedNameSpecifier(OuterDecl) && |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 561 | OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() && |
| 562 | (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) || |
| 563 | !Context.hasSameType( |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 564 | Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)), |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 565 | Context.getTypeDeclType(cast<TypeDecl>(SD))))) { |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 566 | if (ErrorRecoveryLookup) |
| 567 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 568 | |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 569 | Diag(IdentifierLoc, |
| 570 | diag::err_nested_name_member_ref_lookup_ambiguous) |
| 571 | << &Identifier; |
| 572 | Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type) |
| 573 | << ObjectType; |
| 574 | Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope); |
| 575 | |
| 576 | // Fall through so that we'll pick the name we found in the object |
| 577 | // type, since that's probably what the user wanted anyway. |
| 578 | } |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 579 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 581 | // If we're just performing this lookup for error-recovery purposes, |
| 582 | // don't extend the nested-name-specifier. Just return now. |
| 583 | if (ErrorRecoveryLookup) |
| 584 | return false; |
| 585 | |
| 586 | if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) { |
| 587 | SS.Extend(Context, Namespace, IdentifierLoc, CCLoc); |
| 588 | return false; |
| 589 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 590 | |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 591 | if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) { |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 592 | SS.Extend(Context, Alias, IdentifierLoc, CCLoc); |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 593 | return false; |
| 594 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 595 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 596 | QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD)); |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 597 | TypeLocBuilder TLB; |
| 598 | if (isa<InjectedClassNameType>(T)) { |
| 599 | InjectedClassNameTypeLoc InjectedTL |
| 600 | = TLB.push<InjectedClassNameTypeLoc>(T); |
| 601 | InjectedTL.setNameLoc(IdentifierLoc); |
Douglas Gregor | dfd4b74 | 2011-05-04 23:05:40 +0000 | [diff] [blame] | 602 | } else if (isa<RecordType>(T)) { |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 603 | RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T); |
| 604 | RecordTL.setNameLoc(IdentifierLoc); |
Douglas Gregor | dfd4b74 | 2011-05-04 23:05:40 +0000 | [diff] [blame] | 605 | } else if (isa<TypedefType>(T)) { |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 606 | TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T); |
| 607 | TypedefTL.setNameLoc(IdentifierLoc); |
Douglas Gregor | dfd4b74 | 2011-05-04 23:05:40 +0000 | [diff] [blame] | 608 | } else if (isa<EnumType>(T)) { |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 609 | EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T); |
| 610 | EnumTL.setNameLoc(IdentifierLoc); |
Douglas Gregor | dfd4b74 | 2011-05-04 23:05:40 +0000 | [diff] [blame] | 611 | } else if (isa<TemplateTypeParmType>(T)) { |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 612 | TemplateTypeParmTypeLoc TemplateTypeTL |
| 613 | = TLB.push<TemplateTypeParmTypeLoc>(T); |
| 614 | TemplateTypeTL.setNameLoc(IdentifierLoc); |
Douglas Gregor | dfd4b74 | 2011-05-04 23:05:40 +0000 | [diff] [blame] | 615 | } else if (isa<UnresolvedUsingType>(T)) { |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 616 | UnresolvedUsingTypeLoc UnresolvedTL |
| 617 | = TLB.push<UnresolvedUsingTypeLoc>(T); |
| 618 | UnresolvedTL.setNameLoc(IdentifierLoc); |
Douglas Gregor | dfd4b74 | 2011-05-04 23:05:40 +0000 | [diff] [blame] | 619 | } else if (isa<SubstTemplateTypeParmType>(T)) { |
| 620 | SubstTemplateTypeParmTypeLoc TL |
| 621 | = TLB.push<SubstTemplateTypeParmTypeLoc>(T); |
| 622 | TL.setNameLoc(IdentifierLoc); |
| 623 | } else if (isa<SubstTemplateTypeParmPackType>(T)) { |
| 624 | SubstTemplateTypeParmPackTypeLoc TL |
| 625 | = TLB.push<SubstTemplateTypeParmPackTypeLoc>(T); |
| 626 | TL.setNameLoc(IdentifierLoc); |
| 627 | } else { |
| 628 | llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier"); |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Richard Smith | 91c7bbd | 2011-10-20 03:28:47 +0000 | [diff] [blame] | 631 | if (T->isEnumeralType()) |
| 632 | Diag(IdentifierLoc, diag::warn_cxx98_compat_enum_nested_name_spec); |
| 633 | |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 634 | SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T), |
| 635 | CCLoc); |
| 636 | return false; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 637 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
Chris Lattner | 1c42803 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 639 | // Otherwise, we have an error case. If we don't want diagnostics, just |
| 640 | // return an error now. |
| 641 | if (ErrorRecoveryLookup) |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 642 | return true; |
Chris Lattner | 1c42803 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 643 | |
Cedric Venet | 08438133 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 644 | // If we didn't find anything during our lookup, try again with |
| 645 | // ordinary name lookup, which can help us produce better error |
| 646 | // messages. |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 647 | if (Found.empty()) { |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 648 | Found.clear(LookupOrdinaryName); |
| 649 | LookupName(Found, S); |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 650 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 651 | |
Francois Pichet | b23dc09 | 2011-07-27 01:05:24 +0000 | [diff] [blame] | 652 | // In Microsoft mode, if we are within a templated function and we can't |
| 653 | // resolve Identifier, then extend the SS with Identifier. This will have |
| 654 | // the effect of resolving Identifier during template instantiation. |
| 655 | // The goal is to be able to resolve a function call whose |
| 656 | // nested-name-specifier is located inside a dependent base class. |
| 657 | // Example: |
| 658 | // |
| 659 | // class C { |
| 660 | // public: |
| 661 | // static void foo2() { } |
| 662 | // }; |
| 663 | // template <class T> class A { public: typedef C D; }; |
| 664 | // |
| 665 | // template <class T> class B : public A<T> { |
| 666 | // public: |
| 667 | // void foo() { D::foo2(); } |
| 668 | // }; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 669 | if (getLangOpts().MicrosoftExt) { |
Francois Pichet | b23dc09 | 2011-07-27 01:05:24 +0000 | [diff] [blame] | 670 | DeclContext *DC = LookupCtx ? LookupCtx : CurContext; |
| 671 | if (DC->isDependentContext() && DC->isFunctionOrMethod()) { |
| 672 | SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc); |
| 673 | return false; |
| 674 | } |
| 675 | } |
| 676 | |
Cedric Venet | 08438133 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 677 | unsigned DiagID; |
John McCall | 67c0087 | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 678 | if (!Found.empty()) |
Cedric Venet | 08438133 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 679 | DiagID = diag::err_expected_class_or_namespace; |
Anders Carlsson | b533df0 | 2009-08-30 07:09:50 +0000 | [diff] [blame] | 680 | else if (SS.isSet()) { |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 681 | Diag(IdentifierLoc, diag::err_no_member) |
| 682 | << &Identifier << LookupCtx << SS.getRange(); |
| 683 | return true; |
Anders Carlsson | b533df0 | 2009-08-30 07:09:50 +0000 | [diff] [blame] | 684 | } else |
Cedric Venet | 08438133 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 685 | DiagID = diag::err_undeclared_var_use; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 686 | |
Cedric Venet | 08438133 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 687 | if (SS.isSet()) |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 688 | Diag(IdentifierLoc, DiagID) << &Identifier << SS.getRange(); |
Cedric Venet | 08438133 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 689 | else |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 690 | Diag(IdentifierLoc, DiagID) << &Identifier; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 691 | |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 692 | return true; |
Cedric Venet | 08438133 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 695 | bool Sema::ActOnCXXNestedNameSpecifier(Scope *S, |
| 696 | IdentifierInfo &Identifier, |
| 697 | SourceLocation IdentifierLoc, |
| 698 | SourceLocation CCLoc, |
| 699 | ParsedType ObjectType, |
| 700 | bool EnteringContext, |
| 701 | CXXScopeSpec &SS) { |
| 702 | if (SS.isInvalid()) |
| 703 | return true; |
| 704 | |
| 705 | return BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, CCLoc, |
| 706 | GetTypeFromParser(ObjectType), |
| 707 | EnteringContext, SS, |
| 708 | /*ScopeLookupResult=*/0, false); |
Chris Lattner | 1c42803 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 709 | } |
| 710 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 711 | bool Sema::ActOnCXXNestedNameSpecifierDecltype(CXXScopeSpec &SS, |
| 712 | const DeclSpec &DS, |
| 713 | SourceLocation ColonColonLoc) { |
| 714 | if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error) |
| 715 | return true; |
| 716 | |
| 717 | assert(DS.getTypeSpecType() == DeclSpec::TST_decltype); |
| 718 | |
| 719 | QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
| 720 | if (!T->isDependentType() && !T->getAs<TagType>()) { |
| 721 | Diag(DS.getTypeSpecTypeLoc(), diag::err_expected_class) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 722 | << T << getLangOpts().CPlusPlus; |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 723 | return true; |
| 724 | } |
| 725 | |
| 726 | TypeLocBuilder TLB; |
| 727 | DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T); |
| 728 | DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc()); |
| 729 | SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T), |
| 730 | ColonColonLoc); |
| 731 | return false; |
| 732 | } |
| 733 | |
Chris Lattner | 1c42803 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 734 | /// IsInvalidUnlessNestedName - This method is used for error recovery |
| 735 | /// purposes to determine whether the specified identifier is only valid as |
| 736 | /// a nested name specifier, for example a namespace name. It is |
| 737 | /// conservatively correct to always return false from this method. |
| 738 | /// |
| 739 | /// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier. |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 740 | bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS, |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 741 | IdentifierInfo &Identifier, |
| 742 | SourceLocation IdentifierLoc, |
| 743 | SourceLocation ColonLoc, |
| 744 | ParsedType ObjectType, |
Chris Lattner | 1c42803 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 745 | bool EnteringContext) { |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 746 | if (SS.isInvalid()) |
| 747 | return false; |
| 748 | |
| 749 | return !BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, ColonLoc, |
| 750 | GetTypeFromParser(ObjectType), |
| 751 | EnteringContext, SS, |
| 752 | /*ScopeLookupResult=*/0, true); |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 755 | bool Sema::ActOnCXXNestedNameSpecifier(Scope *S, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 756 | CXXScopeSpec &SS, |
| 757 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 758 | TemplateTy Template, |
| 759 | SourceLocation TemplateNameLoc, |
| 760 | SourceLocation LAngleLoc, |
| 761 | ASTTemplateArgsPtr TemplateArgsIn, |
| 762 | SourceLocation RAngleLoc, |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 763 | SourceLocation CCLoc, |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 764 | bool EnteringContext) { |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 765 | if (SS.isInvalid()) |
| 766 | return true; |
| 767 | |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 768 | // Translate the parser's template argument list in our AST format. |
| 769 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 770 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 771 | |
| 772 | if (DependentTemplateName *DTN = Template.get().getAsDependentTemplateName()){ |
| 773 | // Handle a dependent template specialization for which we cannot resolve |
| 774 | // the template name. |
| 775 | assert(DTN->getQualifier() |
| 776 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 777 | QualType T = Context.getDependentTemplateSpecializationType(ETK_None, |
Douglas Gregor | a7a795b | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 778 | DTN->getQualifier(), |
| 779 | DTN->getIdentifier(), |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 780 | TemplateArgs); |
| 781 | |
| 782 | // Create source-location information for this type. |
| 783 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 784 | DependentTemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 785 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 786 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 787 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e0a70b2 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 788 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 789 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 790 | SpecTL.setLAngleLoc(LAngleLoc); |
| 791 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 792 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 793 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 794 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 795 | SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T), |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 796 | CCLoc); |
| 797 | return false; |
| 798 | } |
| 799 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 800 | |
| 801 | if (Template.get().getAsOverloadedTemplate() || |
| 802 | isa<FunctionTemplateDecl>(Template.get().getAsTemplateDecl())) { |
| 803 | SourceRange R(TemplateNameLoc, RAngleLoc); |
| 804 | if (SS.getRange().isValid()) |
| 805 | R.setBegin(SS.getRange().getBegin()); |
| 806 | |
| 807 | Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier) |
| 808 | << Template.get() << R; |
| 809 | NoteAllFoundTemplates(Template.get()); |
| 810 | return true; |
| 811 | } |
| 812 | |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 813 | // We were able to resolve the template name to an actual template. |
| 814 | // Build an appropriate nested-name-specifier. |
| 815 | QualType T = CheckTemplateIdType(Template.get(), TemplateNameLoc, |
| 816 | TemplateArgs); |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 817 | if (T.isNull()) |
| 818 | return true; |
| 819 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 820 | // Alias template specializations can produce types which are not valid |
| 821 | // nested name specifiers. |
| 822 | if (!T->isDependentType() && !T->getAs<TagType>()) { |
| 823 | Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T; |
| 824 | NoteAllFoundTemplates(Template.get()); |
| 825 | return true; |
| 826 | } |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 827 | |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 828 | // Provide source-location information for the template specialization type. |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 829 | TypeLocBuilder Builder; |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 830 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 831 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 832 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 833 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 834 | SpecTL.setLAngleLoc(LAngleLoc); |
| 835 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 836 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 837 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 838 | |
| 839 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 840 | SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T), |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 841 | CCLoc); |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 842 | return false; |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 845 | namespace { |
| 846 | /// \brief A structure that stores a nested-name-specifier annotation, |
| 847 | /// including both the nested-name-specifier |
| 848 | struct NestedNameSpecifierAnnotation { |
| 849 | NestedNameSpecifier *NNS; |
| 850 | }; |
| 851 | } |
| 852 | |
| 853 | void *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) { |
| 854 | if (SS.isEmpty() || SS.isInvalid()) |
| 855 | return 0; |
| 856 | |
| 857 | void *Mem = Context.Allocate((sizeof(NestedNameSpecifierAnnotation) + |
| 858 | SS.location_size()), |
| 859 | llvm::alignOf<NestedNameSpecifierAnnotation>()); |
| 860 | NestedNameSpecifierAnnotation *Annotation |
| 861 | = new (Mem) NestedNameSpecifierAnnotation; |
| 862 | Annotation->NNS = SS.getScopeRep(); |
| 863 | memcpy(Annotation + 1, SS.location_data(), SS.location_size()); |
| 864 | return Annotation; |
| 865 | } |
| 866 | |
| 867 | void Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr, |
| 868 | SourceRange AnnotationRange, |
| 869 | CXXScopeSpec &SS) { |
| 870 | if (!AnnotationPtr) { |
| 871 | SS.SetInvalid(AnnotationRange); |
| 872 | return; |
| 873 | } |
| 874 | |
| 875 | NestedNameSpecifierAnnotation *Annotation |
| 876 | = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr); |
| 877 | SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1)); |
| 878 | } |
| 879 | |
John McCall | 2b058ef | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 880 | bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { |
| 881 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); |
| 882 | |
| 883 | NestedNameSpecifier *Qualifier = |
| 884 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 885 | |
| 886 | // There are only two places a well-formed program may qualify a |
| 887 | // declarator: first, when defining a namespace or class member |
| 888 | // out-of-line, and second, when naming an explicitly-qualified |
| 889 | // friend function. The latter case is governed by |
| 890 | // C++03 [basic.lookup.unqual]p10: |
| 891 | // In a friend declaration naming a member function, a name used |
| 892 | // in the function declarator and not part of a template-argument |
| 893 | // in a template-id is first looked up in the scope of the member |
| 894 | // function's class. If it is not found, or if the name is part of |
| 895 | // a template-argument in a template-id, the look up is as |
| 896 | // described for unqualified names in the definition of the class |
| 897 | // granting friendship. |
| 898 | // i.e. we don't push a scope unless it's a class member. |
| 899 | |
| 900 | switch (Qualifier->getKind()) { |
| 901 | case NestedNameSpecifier::Global: |
| 902 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 903 | case NestedNameSpecifier::NamespaceAlias: |
John McCall | 2b058ef | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 904 | // These are always namespace scopes. We never want to enter a |
| 905 | // namespace scope from anything but a file context. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 906 | return CurContext->getRedeclContext()->isFileContext(); |
John McCall | 2b058ef | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 907 | |
| 908 | case NestedNameSpecifier::Identifier: |
| 909 | case NestedNameSpecifier::TypeSpec: |
| 910 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 911 | // These are never namespace scopes. |
| 912 | return true; |
| 913 | } |
| 914 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 915 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
John McCall | 2b058ef | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Cedric Venet | 08438133 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 918 | /// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global |
| 919 | /// scope or nested-name-specifier) is parsed, part of a declarator-id. |
| 920 | /// After this method is called, according to [C++ 3.4.3p3], names should be |
| 921 | /// looked up in the declarator-id's scope, until the declarator is parsed and |
| 922 | /// ActOnCXXExitDeclaratorScope is called. |
| 923 | /// The 'SS' should be a non-empty valid CXXScopeSpec. |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 924 | bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) { |
Cedric Venet | 08438133 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 925 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 926 | |
| 927 | if (SS.isInvalid()) return true; |
| 928 | |
| 929 | DeclContext *DC = computeDeclContext(SS, true); |
| 930 | if (!DC) return true; |
| 931 | |
| 932 | // Before we enter a declarator's context, we need to make sure that |
| 933 | // it is a complete declaration context. |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 934 | if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC)) |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 935 | return true; |
| 936 | |
| 937 | EnterDeclaratorContext(S, DC); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 938 | |
| 939 | // Rebuild the nested name specifier for the new scope. |
| 940 | if (DC->isDependentContext()) |
| 941 | RebuildNestedNameSpecifierInCurrentInstantiation(SS); |
| 942 | |
Douglas Gregor | 5013a7e | 2009-09-24 23:39:01 +0000 | [diff] [blame] | 943 | return false; |
Cedric Venet | 08438133 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | /// ActOnCXXExitDeclaratorScope - Called when a declarator that previously |
| 947 | /// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same |
| 948 | /// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well. |
| 949 | /// Used to indicate that names should revert to being looked up in the |
| 950 | /// defining scope. |
| 951 | void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) { |
| 952 | assert(SS.isSet() && "Parser passed invalid CXXScopeSpec."); |
Douglas Gregor | 053f691 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 953 | if (SS.isInvalid()) |
| 954 | return; |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 955 | assert(!SS.isInvalid() && computeDeclContext(SS, true) && |
| 956 | "exiting declarator scope we never really entered"); |
| 957 | ExitDeclaratorContext(S); |
Cedric Venet | 08438133 | 2009-02-14 20:20:19 +0000 | [diff] [blame] | 958 | } |