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