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