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