blob: a4421d2b68af370e2390cde30ada56889473004b [file] [log] [blame]
Cedric Venet084381332009-02-14 20:20:19 +00001//===--- SemaCXXScopeSpec.cpp - Semantic Analysis for C++ scope specifiers-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Venet084381332009-02-14 20:20:19 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements C++ semantic analysis for scope specifiers.
10//
11//===----------------------------------------------------------------------===//
12
Chandler Carruth3a022472012-12-04 09:13:33 +000013#include "TypeLocBuilder.h"
Cedric Venet084381332009-02-14 20:20:19 +000014#include "clang/AST/ASTContext.h"
Douglas Gregorc9f9b862009-05-11 19:58:34 +000015#include "clang/AST/DeclTemplate.h"
Douglas Gregord8061562009-08-06 03:17:00 +000016#include "clang/AST/ExprCXX.h"
Douglas Gregor52537682009-03-19 00:18:19 +000017#include "clang/AST/NestedNameSpecifier.h"
Anders Carlssond624e162009-08-26 23:45:07 +000018#include "clang/Basic/PartialDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Sema/Lookup.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000021#include "clang/Sema/SemaInternal.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Sema/Template.h"
Cedric Venet084381332009-02-14 20:20:19 +000023#include "llvm/ADT/STLExtras.h"
24using namespace clang;
25
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000026/// Find the current instantiation that associated with the given type.
Richard Smithd80b2d52012-11-22 00:24:47 +000027static CXXRecordDecl *getCurrentInstantiationOf(QualType T,
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000028 DeclContext *CurContext) {
Douglas Gregor41127182009-11-04 22:49:18 +000029 if (T.isNull())
Craig Topperc3ec1492014-05-26 06:22:03 +000030 return nullptr;
John McCall2408e322010-04-27 00:57:59 +000031
32 const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000033 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
34 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
Richard Smithd80b2d52012-11-22 00:24:47 +000035 if (!Record->isDependentContext() ||
36 Record->isCurrentInstantiation(CurContext))
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000037 return Record;
38
Craig Topperc3ec1492014-05-26 06:22:03 +000039 return nullptr;
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000040 } else if (isa<InjectedClassNameType>(Ty))
John McCall2408e322010-04-27 00:57:59 +000041 return cast<InjectedClassNameType>(Ty)->getDecl();
42 else
Craig Topperc3ec1492014-05-26 06:22:03 +000043 return nullptr;
Douglas Gregor41127182009-11-04 22:49:18 +000044}
45
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000046/// Compute the DeclContext that is associated with the given type.
Douglas Gregorb7bfe792009-09-02 22:59:36 +000047///
48/// \param T the type for which we are attempting to find a DeclContext.
49///
Mike Stump11289f42009-09-09 15:08:12 +000050/// \returns the declaration context represented by the type T,
Douglas Gregorb7bfe792009-09-02 22:59:36 +000051/// or NULL if the declaration context cannot be computed (e.g., because it is
52/// dependent and not the current instantiation).
53DeclContext *Sema::computeDeclContext(QualType T) {
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000054 if (!T->isDependentType())
55 if (const TagType *Tag = T->getAs<TagType>())
56 return Tag->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +000057
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000058 return ::getCurrentInstantiationOf(T, CurContext);
Douglas Gregorb7bfe792009-09-02 22:59:36 +000059}
60
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000061/// Compute the DeclContext that is associated with the given
Douglas Gregor52537682009-03-19 00:18:19 +000062/// scope specifier.
Douglas Gregord8d297c2009-07-21 23:53:31 +000063///
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).
74DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS,
75 bool EnteringContext) {
Douglas Gregor52537682009-03-19 00:18:19 +000076 if (!SS.isSet() || SS.isInvalid())
Craig Topperc3ec1492014-05-26 06:22:03 +000077 return nullptr;
Douglas Gregor6bfde492009-03-18 00:36:05 +000078
Richard Smith74bb2d22013-03-26 00:54:11 +000079 NestedNameSpecifier *NNS = SS.getScopeRep();
Douglas Gregorc9f9b862009-05-11 19:58:34 +000080 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 Stump11289f42009-09-09 15:08:12 +000085
Douglas Gregord8d297c2009-07-21 23:53:31 +000086 if (EnteringContext) {
John McCalle78aac42010-03-10 03:28:59 +000087 const Type *NNSType = NNS->getAsType();
88 if (!NNSType) {
Craig Topperc3ec1492014-05-26 06:22:03 +000089 return nullptr;
Richard Smith3f1b5d02011-05-05 21:57:07 +000090 }
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 Gregore861bac2009-08-25 22:51:20 +000096 // 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 Stump11289f42009-09-09 15:08:12 +000099 if (ClassTemplateDecl *ClassTemplate
Douglas Gregord8d297c2009-07-21 23:53:31 +0000100 = dyn_cast_or_null<ClassTemplateDecl>(
101 SpecType->getTemplateName().getAsTemplateDecl())) {
Douglas Gregor15301382009-07-30 17:40:51 +0000102 QualType ContextType
103 = Context.getCanonicalType(QualType(SpecType, 0));
104
Douglas Gregord8d297c2009-07-21 23:53:31 +0000105 // 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 McCalle78aac42010-03-10 03:28:59 +0000108 QualType Injected
Douglas Gregor9961ce92010-07-08 18:37:38 +0000109 = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregor15301382009-07-30 17:40:51 +0000110 if (Context.hasSameType(Injected, ContextType))
Douglas Gregord8d297c2009-07-21 23:53:31 +0000111 return ClassTemplate->getTemplatedDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000112
Douglas Gregor15301382009-07-30 17:40:51 +0000113 // 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 Smith6739a102016-05-05 00:56:12 +0000118 = 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 Gregor15301382009-07-30 17:40:51 +0000128 return PartialSpec;
Richard Smith6739a102016-05-05 00:56:12 +0000129 }
Douglas Gregord8d297c2009-07-21 23:53:31 +0000130 }
John McCalle78aac42010-03-10 03:28:59 +0000131 } else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) {
Douglas Gregore861bac2009-08-25 22:51:20 +0000132 // The nested name specifier refers to a member of a class template.
133 return RecordT->getDecl();
Douglas Gregord8d297c2009-07-21 23:53:31 +0000134 }
135 }
Mike Stump11289f42009-09-09 15:08:12 +0000136
Craig Topperc3ec1492014-05-26 06:22:03 +0000137 return nullptr;
Douglas Gregorc9f9b862009-05-11 19:58:34 +0000138 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000139
140 switch (NNS->getKind()) {
141 case NestedNameSpecifier::Identifier:
David Blaikie83d382b2011-09-23 05:06:16 +0000142 llvm_unreachable("Dependent nested-name-specifier has no DeclContext");
Douglas Gregorf21eb492009-03-26 23:50:42 +0000143
144 case NestedNameSpecifier::Namespace:
145 return NNS->getAsNamespace();
146
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000147 case NestedNameSpecifier::NamespaceAlias:
148 return NNS->getAsNamespaceAlias()->getNamespace();
149
Douglas Gregorf21eb492009-03-26 23:50:42 +0000150 case NestedNameSpecifier::TypeSpec:
151 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000152 const TagType *Tag = NNS->getAsType()->getAs<TagType>();
153 assert(Tag && "Non-tag type in nested-name-specifier");
154 return Tag->getDecl();
David Blaikie8a40f702012-01-17 06:56:22 +0000155 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000156
157 case NestedNameSpecifier::Global:
158 return Context.getTranslationUnitDecl();
Nikola Smiljanic67860242014-09-26 00:28:20 +0000159
160 case NestedNameSpecifier::Super:
161 return NNS->getAsRecordDecl();
Douglas Gregorf21eb492009-03-26 23:50:42 +0000162 }
163
David Blaikie8a40f702012-01-17 06:56:22 +0000164 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
Douglas Gregor6bfde492009-03-18 00:36:05 +0000165}
166
Douglas Gregor90a1a652009-03-19 17:26:29 +0000167bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) {
168 if (!SS.isSet() || SS.isInvalid())
169 return false;
170
Richard Smith74bb2d22013-03-26 00:54:11 +0000171 return SS.getScopeRep()->isDependent();
Douglas Gregor90a1a652009-03-19 17:26:29 +0000172}
173
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000174/// If the given nested name specifier refers to the current
Douglas Gregorc9f9b862009-05-11 19:58:34 +0000175/// 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.
179CXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000180 assert(getLangOpts().CPlusPlus && "Only callable in C++");
Douglas Gregorc9f9b862009-05-11 19:58:34 +0000181 assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed");
182
Douglas Gregord8d297c2009-07-21 23:53:31 +0000183 if (!NNS->getAsType())
Craig Topperc3ec1492014-05-26 06:22:03 +0000184 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000185
Douglas Gregorb9a955d2009-07-31 18:32:42 +0000186 QualType T = QualType(NNS->getAsType(), 0);
Douglas Gregorbf2b26d2011-02-19 19:24:40 +0000187 return ::getCurrentInstantiationOf(T, CurContext);
Douglas Gregorc9f9b862009-05-11 19:58:34 +0000188}
189
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000190/// Require that the context specified by SS be complete.
Douglas Gregor26897462009-03-11 16:48:53 +0000191///
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 McCall0b66eb32010-05-01 00:40:08 +0000199bool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS,
200 DeclContext *DC) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000201 assert(DC && "given null context");
Mike Stump11289f42009-09-09 15:08:12 +0000202
Richard Smith4b38ded2012-03-14 23:13:10 +0000203 TagDecl *tag = dyn_cast<TagDecl>(DC);
Douglas Gregor8a6d15d2010-02-05 04:39:02 +0000204
Richard Smith4b38ded2012-03-14 23:13:10 +0000205 // If this is a dependent type, then we consider it complete.
Richard Smith6739a102016-05-05 00:56:12 +0000206 // FIXME: This is wrong; we should require a (visible) definition to
207 // exist in this case too.
Richard Smith4b38ded2012-03-14 23:13:10 +0000208 if (!tag || tag->isDependentContext())
209 return false;
Douglas Gregor26897462009-03-11 16:48:53 +0000210
Richard Smith96cb3562018-08-07 21:35:41 +0000211 // Grab the tag definition, if there is one.
212 QualType type = Context.getTypeDeclType(tag);
213 tag = type->getAsTagDecl();
214
Richard Smith4b38ded2012-03-14 23:13:10 +0000215 // 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 Smith96cb3562018-08-07 21:35:41 +0000217 if (tag->isBeingDefined())
Richard Smith4b38ded2012-03-14 23:13:10 +0000218 return false;
John McCall21878762011-07-06 06:57:57 +0000219
Richard Smith4b38ded2012-03-14 23:13:10 +0000220 SourceLocation loc = SS.getLastQualifierNameLoc();
221 if (loc.isInvalid()) loc = SS.getRange().getBegin();
John McCall21878762011-07-06 06:57:57 +0000222
Richard Smith4b38ded2012-03-14 23:13:10 +0000223 // The type must be complete.
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000224 if (RequireCompleteType(loc, type, diag::err_incomplete_nested_name_spec,
225 SS.getRange())) {
Richard Smith4b38ded2012-03-14 23:13:10 +0000226 SS.SetInvalid(SS.getRange());
227 return true;
Douglas Gregor26897462009-03-11 16:48:53 +0000228 }
229
Richard Smith4b38ded2012-03-14 23:13:10 +0000230 // 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 Smith96cb3562018-08-07 21:35:41 +0000233 auto *EnumD = dyn_cast<EnumDecl>(tag);
234 if (!EnumD)
Richard Smith4b38ded2012-03-14 23:13:10 +0000235 return false;
Richard Smith96cb3562018-08-07 21:35:41 +0000236 if (EnumD->isCompleteDefinition()) {
Richard Smith6739a102016-05-05 00:56:12 +0000237 // If we know about the definition but it is not visible, complain.
238 NamedDecl *SuggestedDef = nullptr;
Richard Smith96cb3562018-08-07 21:35:41 +0000239 if (!hasVisibleDefinition(EnumD, &SuggestedDef,
Richard Smith6739a102016-05-05 00:56:12 +0000240 /*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 Smith4b38ded2012-03-14 23:13:10 +0000250
251 // Try to instantiate the definition, if this is a specialization of an
252 // enumeration temploid.
Richard Smith96cb3562018-08-07 21:35:41 +0000253 if (EnumDecl *Pattern = EnumD->getInstantiatedFromMemberEnum()) {
254 MemberSpecializationInfo *MSI = EnumD->getMemberSpecializationInfo();
Richard Smith7d137e32012-03-23 03:33:32 +0000255 if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) {
Richard Smith96cb3562018-08-07 21:35:41 +0000256 if (InstantiateEnum(loc, EnumD, Pattern,
257 getTemplateInstantiationArgs(EnumD),
Richard Smith7d137e32012-03-23 03:33:32 +0000258 TSK_ImplicitInstantiation)) {
259 SS.SetInvalid(SS.getRange());
260 return true;
261 }
262 return false;
263 }
Richard Smith4b38ded2012-03-14 23:13:10 +0000264 }
265
266 Diag(loc, diag::err_incomplete_nested_name_spec)
267 << type << SS.getRange();
268 SS.SetInvalid(SS.getRange());
269 return true;
Douglas Gregor26897462009-03-11 16:48:53 +0000270}
Cedric Venet084381332009-02-14 20:20:19 +0000271
Nikola Smiljanic67860242014-09-26 00:28:20 +0000272bool Sema::ActOnCXXGlobalScopeSpecifier(SourceLocation CCLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +0000273 CXXScopeSpec &SS) {
274 SS.MakeGlobal(Context, CCLoc);
275 return false;
Cedric Venet084381332009-02-14 20:20:19 +0000276}
277
Nikola Smiljanic67860242014-09-26 00:28:20 +0000278bool 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 Prantl9fc8faf2018-05-09 01:00:01 +0000309/// Determines whether the given declaration is an valid acceptable
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000310/// result for name lookup of a nested-name-specifier.
Serge Pavlov25a8afa2015-01-18 20:04:35 +0000311/// \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.
314bool Sema::isAcceptableNestedNameSpecifier(const NamedDecl *SD,
315 bool *IsExtension) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000316 if (!SD)
317 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000318
Richard Smithf2005d32015-12-29 23:34:32 +0000319 SD = SD->getUnderlyingDecl();
320
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000321 // Namespace and namespace aliases are fine.
Richard Smithf2005d32015-12-29 23:34:32 +0000322 if (isa<NamespaceDecl>(SD))
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000323 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000324
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000325 if (!isa<TypeDecl>(SD))
326 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000327
Richard Smithc8239732011-10-18 21:39:00 +0000328 // Determine whether we have a class (or, in C++11, an enum) or
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000329 // 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 Pavlov25a8afa2015-01-18 20:04:35 +0000333 if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) {
334 if (TD->getUnderlyingType()->isRecordType())
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000335 return true;
Serge Pavlov25a8afa2015-01-18 20:04:35 +0000336 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 Gregorb7bfe792009-09-02 22:59:36 +0000343 return true;
Serge Pavlov25a8afa2015-01-18 20:04:35 +0000344 } else if (isa<EnumDecl>(SD)) {
345 if (Context.getLangOpts().CPlusPlus11)
346 return true;
347 if (IsExtension)
348 *IsExtension = true;
349 }
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000350
351 return false;
352}
353
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000354/// If the given nested-name-specifier begins with a bare identifier
Mike Stump11289f42009-09-09 15:08:12 +0000355/// (e.g., Base::), perform name lookup for that identifier as a
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000356/// nested-name-specifier within the given scope, and return the result of that
357/// name lookup.
358NamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) {
359 if (!S || !NNS)
Craig Topperc3ec1492014-05-26 06:22:03 +0000360 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000361
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000362 while (NNS->getPrefix())
363 NNS = NNS->getPrefix();
Mike Stump11289f42009-09-09 15:08:12 +0000364
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000365 if (NNS->getKind() != NestedNameSpecifier::Identifier)
Craig Topperc3ec1492014-05-26 06:22:03 +0000366 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000367
John McCall27b18f82009-11-17 02:14:36 +0000368 LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(),
369 LookupNestedNameSpecifierName);
370 LookupName(Found, S);
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000371 assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet");
372
John McCall67c00872009-12-02 08:25:40 +0000373 if (!Found.isSingleResult())
Craig Topperc3ec1492014-05-26 06:22:03 +0000374 return nullptr;
John McCall67c00872009-12-02 08:25:40 +0000375
376 NamedDecl *Result = Found.getFoundDecl();
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000377 if (isAcceptableNestedNameSpecifier(Result))
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000378 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000379
Craig Topperc3ec1492014-05-26 06:22:03 +0000380 return nullptr;
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000381}
382
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +0000383bool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS,
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000384 NestedNameSpecInfo &IdInfo) {
385 QualType ObjectType = GetTypeFromParser(IdInfo.ObjectType);
386 LookupResult Found(*this, IdInfo.Identifier, IdInfo.IdentifierLoc,
387 LookupNestedNameSpecifierName);
388
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000389 // Determine where to perform name lookup
Craig Topperc3ec1492014-05-26 06:22:03 +0000390 DeclContext *LookupCtx = nullptr;
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000391 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 Song6907ce22018-07-30 19:24:48 +0000405
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000406 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 Song6907ce22018-07-30 19:24:48 +0000411
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000412 // The declaration context must be complete.
John McCall0b66eb32010-05-01 00:40:08 +0000413 if (!LookupCtx->isDependentContext() &&
414 RequireCompleteDeclContext(SS, LookupCtx))
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000415 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000416
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000417 LookupQualifiedName(Found, LookupCtx);
418 } else if (isDependent) {
419 return false;
420 } else {
421 LookupName(Found, S);
422 }
423 Found.suppressDiagnostics();
Fangrui Song6907ce22018-07-30 19:24:48 +0000424
Richard Smithf2005d32015-12-29 23:34:32 +0000425 return Found.getAsSingle<NamespaceDecl>();
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000426}
427
Kaelyn Uhrainfb96ec72012-01-12 22:32:39 +0000428namespace {
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 Ricci70ad3962019-03-25 17:08:51 +0000432class NestedNameSpecifierValidatorCCC final
433 : public CorrectionCandidateCallback {
434public:
Kaelyn Uhrainfb96ec72012-01-12 22:32:39 +0000435 explicit NestedNameSpecifierValidatorCCC(Sema &SRef)
436 : SRef(SRef) {}
437
Craig Toppere14c0f82014-03-12 04:55:44 +0000438 bool ValidateCandidate(const TypoCorrection &candidate) override {
Kaelyn Uhrainfb96ec72012-01-12 22:32:39 +0000439 return SRef.isAcceptableNestedNameSpecifier(candidate.getCorrectionDecl());
440 }
441
Bruno Ricci70ad3962019-03-25 17:08:51 +0000442 std::unique_ptr<CorrectionCandidateCallback> clone() override {
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +0000443 return std::make_unique<NestedNameSpecifierValidatorCCC>(*this);
Bruno Ricci70ad3962019-03-25 17:08:51 +0000444 }
445
Kaelyn Uhrainfb96ec72012-01-12 22:32:39 +0000446 private:
447 Sema &SRef;
448};
449
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000450}
Kaelyn Uhrainfb96ec72012-01-12 22:32:39 +0000451
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000452/// Build a new nested-name-specifier for "identifier::", as described
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000453/// by ActOnCXXNestedNameSpecifier.
454///
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000455/// \param S Scope in which the nested-name-specifier occurs.
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000456/// \param IdInfo Parser information about an identifier in the
457/// nested-name-spec.
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000458/// \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 Gehredc01bb42017-03-17 21:41:20 +0000470/// \param OnlyNamespace If true, only considers namespaces in lookup.
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000471///
472/// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000473/// 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 Gregorad183ac2009-12-30 16:01:52 +0000475/// that was computed at template definition time.
Chris Lattner1c428032009-12-07 01:36:53 +0000476///
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 Gregor90c99722011-02-24 00:17:56 +0000479/// just return true on failure. It also means it should only return a valid
Chris Lattner1c428032009-12-07 01:36:53 +0000480/// scope if it *knows* that the result is correct. It should not return in a
Douglas Gregor90c99722011-02-24 00:17:56 +0000481/// dependent context, for example. Nor will it extend \p SS with the scope
482/// specifier.
Matthias Gehredc01bb42017-03-17 21:41:20 +0000483bool Sema::BuildCXXNestedNameSpecifier(Scope *S, NestedNameSpecInfo &IdInfo,
484 bool EnteringContext, CXXScopeSpec &SS,
Douglas Gregor90c99722011-02-24 00:17:56 +0000485 NamedDecl *ScopeLookupResult,
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000486 bool ErrorRecoveryLookup,
Matthias Gehredc01bb42017-03-17 21:41:20 +0000487 bool *IsCorrectedToColon,
488 bool OnlyNamespace) {
Alex Lorenz1be800c52017-04-19 08:58:56 +0000489 if (IdInfo.Identifier->isEditorPlaceholder())
490 return true;
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000491 LookupResult Found(*this, IdInfo.Identifier, IdInfo.IdentifierLoc,
Matthias Gehredc01bb42017-03-17 21:41:20 +0000492 OnlyNamespace ? LookupNamespaceName
493 : LookupNestedNameSpecifierName);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000494 QualType ObjectType = GetTypeFromParser(IdInfo.ObjectType);
John McCall27b18f82009-11-17 02:14:36 +0000495
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000496 // Determine where to perform name lookup
Craig Topperc3ec1492014-05-26 06:22:03 +0000497 DeclContext *LookupCtx = nullptr;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000498 bool isDependent = false;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000499 if (IsCorrectedToColon)
500 *IsCorrectedToColon = false;
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000501 if (!ObjectType.isNull()) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000502 // 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 Gregorb7bfe792009-09-02 22:59:36 +0000505 LookupCtx = computeDeclContext(ObjectType);
506 isDependent = ObjectType->isDependentType();
507 } else if (SS.isSet()) {
508 // This nested-name-specifier occurs after another nested-name-specifier,
Richard Smith3f1b5d02011-05-05 21:57:07 +0000509 // so look into the context associated with the prior nested-name-specifier.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000510 LookupCtx = computeDeclContext(SS, EnteringContext);
511 isDependent = isDependentScopeSpecifier(SS);
John McCall27b18f82009-11-17 02:14:36 +0000512 Found.setContextRange(SS.getRange());
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000513 }
Mike Stump11289f42009-09-09 15:08:12 +0000514
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000515 bool ObjectTypeSearchedInScope = false;
516 if (LookupCtx) {
Mike Stump11289f42009-09-09 15:08:12 +0000517 // Perform "qualified" name lookup into the declaration context we
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000518 // computed, which is either the type of the base of a member access
Mike Stump11289f42009-09-09 15:08:12 +0000519 // expression or the declaration context associated with a prior
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000520 // nested-name-specifier.
Mike Stump11289f42009-09-09 15:08:12 +0000521
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000522 // The declaration context must be complete.
John McCall0b66eb32010-05-01 00:40:08 +0000523 if (!LookupCtx->isDependentContext() &&
524 RequireCompleteDeclContext(SS, LookupCtx))
Douglas Gregor90c99722011-02-24 00:17:56 +0000525 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000526
John McCall27b18f82009-11-17 02:14:36 +0000527 LookupQualifiedName(Found, LookupCtx);
Mike Stump11289f42009-09-09 15:08:12 +0000528
John McCall27b18f82009-11-17 02:14:36 +0000529 if (!ObjectType.isNull() && Found.empty()) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000530 // C++ [basic.lookup.classref]p4:
531 // If the id-expression in a class member access is a qualified-id of
Mike Stump11289f42009-09-09 15:08:12 +0000532 // the form
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000533 //
534 // class-name-or-namespace-name::...
535 //
Mike Stump11289f42009-09-09 15:08:12 +0000536 // 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 Gregorb7bfe792009-09-02 22:59:36 +0000538 // the scope of the class of the object expression. If the name is found
Mike Stump11289f42009-09-09 15:08:12 +0000539 // 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 Gregorb7bfe792009-09-02 22:59:36 +0000541 // 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 Gregor9d07dfa2011-05-15 17:27:27 +0000545 // so we do not need to diagnose that case specifically. However,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000546 // this qualified name lookup may find nothing. In that case, perform
Mike Stump11289f42009-09-09 15:08:12 +0000547 // unqualified name lookup in the given scope (if available) or
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000548 // reconstruct the result from when name lookup was performed at template
549 // definition time.
550 if (S)
John McCall27b18f82009-11-17 02:14:36 +0000551 LookupName(Found, S);
John McCall9f3059a2009-10-09 21:13:30 +0000552 else if (ScopeLookupResult)
553 Found.addDecl(ScopeLookupResult);
Mike Stump11289f42009-09-09 15:08:12 +0000554
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000555 ObjectTypeSearchedInScope = true;
556 }
Douglas Gregordf65c8ed2010-07-28 14:49:07 +0000557 } else if (!isDependent) {
558 // Perform unqualified name lookup in the current scope.
559 LookupName(Found, S);
560 }
561
Richard Smith4b213d72015-11-12 22:40:09 +0000562 if (Found.isAmbiguous())
563 return true;
564
Douglas Gregordf65c8ed2010-07-28 14:49:07 +0000565 // 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 Lattner1c428032009-12-07 01:36:53 +0000571 // Don't speculate if we're just trying to improve error recovery.
572 if (ErrorRecoveryLookup)
Douglas Gregor90c99722011-02-24 00:17:56 +0000573 return true;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000574
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000575 // We were not able to compute the declaration context for a dependent
Mike Stump11289f42009-09-09 15:08:12 +0000576 // base object type or prior nested-name-specifier, so this
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000577 // nested-name-specifier refers to an unknown specialization. Just build
578 // a dependent nested-name-specifier.
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000579 SS.Extend(Context, IdInfo.Identifier, IdInfo.IdentifierLoc, IdInfo.CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000580 return false;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000581 }
582
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000583 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 Smith4b213d72015-11-12 22:40:09 +0000592 // Don't diagnose problems with this speculative lookup.
593 R.suppressDiagnostics();
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000594 // The identifier is found in ordinary lookup. If correction to colon is
595 // allowed, suggest replacement to ':'.
596 if (IsCorrectedToColon) {
597 *IsCorrectedToColon = true;
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000598 Diag(IdInfo.CCLoc, diag::err_nested_name_spec_is_not_class)
599 << IdInfo.Identifier << getLangOpts().CPlusPlus
600 << FixItHint::CreateReplacement(IdInfo.CCLoc, ":");
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000601 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 Gehredc01bb42017-03-17 21:41:20 +0000606 Diag(R.getNameLoc(), OnlyNamespace
Davide Italiano3c6e5ea2017-03-17 22:19:20 +0000607 ? unsigned(diag::err_expected_namespace_name)
608 : unsigned(diag::err_expected_class_or_namespace))
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000609 << IdInfo.Identifier << getLangOpts().CPlusPlus;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000610 if (NamedDecl *ND = R.getAsSingle<NamedDecl>())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000611 Diag(ND->getLocation(), diag::note_entity_declared_at)
612 << IdInfo.Identifier;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000613 return true;
614 }
615 }
616
Alp Tokerbfa39342014-01-14 12:51:41 +0000617 if (Found.empty() && !ErrorRecoveryLookup && !getLangOpts().MSVCCompat) {
Douglas Gregor532e68f2009-12-31 08:26:35 +0000618 // 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 Gregorc2fa1692011-06-28 16:20:02 +0000621 Found.clear();
Bruno Ricci70ad3962019-03-25 17:08:51 +0000622 NestedNameSpecifierValidatorCCC CCC(*this);
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000623 if (TypoCorrection Corrected = CorrectTypo(
Bruno Ricci70ad3962019-03-25 17:08:51 +0000624 Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS, CCC,
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000625 CTK_ErrorRecovery, LookupCtx, EnteringContext)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000626 if (LookupCtx) {
627 bool DroppedSpecifier =
628 Corrected.WillReplaceSpecifier() &&
629 Name.getAsString() == Corrected.getAsString(getLangOpts());
Kaelyn Uhraina6c78fe2013-12-16 19:19:18 +0000630 if (DroppedSpecifier)
631 SS.clear();
Richard Smithf9b15102013-08-17 00:46:16 +0000632 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 Uhrain10413a42013-07-02 23:47:44 +0000638
Reid Kleckner4ce625c2016-02-16 19:16:20 +0000639 if (Corrected.getCorrectionSpecifier())
640 SS.MakeTrivial(Context, Corrected.getCorrectionSpecifier(),
641 SourceRange(Found.getNameLoc()));
642
Richard Smithde6d6c42015-12-29 19:43:10 +0000643 if (NamedDecl *ND = Corrected.getFoundDecl())
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000644 Found.addDecl(ND);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000645 Found.setLookupName(Corrected.getCorrection());
Douglas Gregorc048c522010-06-29 19:27:42 +0000646 } else {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000647 Found.setLookupName(IdInfo.Identifier);
Douglas Gregorc048c522010-06-29 19:27:42 +0000648 }
Douglas Gregor532e68f2009-12-31 08:26:35 +0000649 }
650
Richard Smithf2005d32015-12-29 23:34:32 +0000651 NamedDecl *SD =
652 Found.isSingleResult() ? Found.getRepresentativeDecl() : nullptr;
Serge Pavlov25a8afa2015-01-18 20:04:35 +0000653 bool IsExtension = false;
654 bool AcceptSpec = isAcceptableNestedNameSpecifier(SD, &IsExtension);
655 if (!AcceptSpec && IsExtension) {
656 AcceptSpec = true;
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000657 Diag(IdInfo.IdentifierLoc, diag::ext_nested_name_spec_is_enum);
Serge Pavlov25a8afa2015-01-18 20:04:35 +0000658 }
659 if (AcceptSpec) {
Douglas Gregor1b02e4a2012-05-01 20:23:02 +0000660 if (!ObjectType.isNull() && !ObjectTypeSearchedInScope &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000661 !getLangOpts().CPlusPlus11) {
Douglas Gregor1b02e4a2012-05-01 20:23:02 +0000662 // C++03 [basic.lookup.classref]p4:
Mike Stump11289f42009-09-09 15:08:12 +0000663 // [...] If the name is found in both contexts, the
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000664 // 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 Gregor2b6ca462009-09-03 21:38:09 +0000668 // 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 Gregor1b02e4a2012-05-01 20:23:02 +0000670 //
671 // Note that C++11 does *not* perform this redundant lookup.
John McCall9f3059a2009-10-09 21:13:30 +0000672 NamedDecl *OuterDecl;
673 if (S) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000674 LookupResult FoundOuter(*this, IdInfo.Identifier, IdInfo.IdentifierLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +0000675 LookupNestedNameSpecifierName);
John McCall27b18f82009-11-17 02:14:36 +0000676 LookupName(FoundOuter, S);
John McCall67c00872009-12-02 08:25:40 +0000677 OuterDecl = FoundOuter.getAsSingle<NamedDecl>();
John McCall9f3059a2009-10-09 21:13:30 +0000678 } else
679 OuterDecl = ScopeLookupResult;
Mike Stump11289f42009-09-09 15:08:12 +0000680
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000681 if (isAcceptableNestedNameSpecifier(OuterDecl) &&
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000682 OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() &&
683 (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) ||
684 !Context.hasSameType(
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000685 Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)),
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000686 Context.getTypeDeclType(cast<TypeDecl>(SD))))) {
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000687 if (ErrorRecoveryLookup)
688 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000689
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000690 Diag(IdInfo.IdentifierLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +0000691 diag::err_nested_name_member_ref_lookup_ambiguous)
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000692 << IdInfo.Identifier;
Douglas Gregor90c99722011-02-24 00:17:56 +0000693 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 Gregorb7bfe792009-09-02 22:59:36 +0000700 }
Mike Stump11289f42009-09-09 15:08:12 +0000701
Nico Weber72889432014-09-06 01:25:55 +0000702 if (auto *TD = dyn_cast_or_null<TypedefNameDecl>(SD))
703 MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false);
704
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000705 // If we're just performing this lookup for error-recovery purposes,
Douglas Gregor90c99722011-02-24 00:17:56 +0000706 // don't extend the nested-name-specifier. Just return now.
707 if (ErrorRecoveryLookup)
708 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +0000709
710 // The use of a nested name specifier may trigger deprecation warnings.
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000711 DiagnoseUseOfDecl(SD, IdInfo.CCLoc);
Aaron Ballman43f40102014-11-14 22:34:56 +0000712
Douglas Gregor90c99722011-02-24 00:17:56 +0000713 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000714 SS.Extend(Context, Namespace, IdInfo.IdentifierLoc, IdInfo.CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000715 return false;
716 }
Mike Stump11289f42009-09-09 15:08:12 +0000717
Douglas Gregor90c99722011-02-24 00:17:56 +0000718 if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000719 SS.Extend(Context, Alias, IdInfo.IdentifierLoc, IdInfo.CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000720 return false;
721 }
Mike Stump11289f42009-09-09 15:08:12 +0000722
Richard Smithf2005d32015-12-29 23:34:32 +0000723 QualType T =
724 Context.getTypeDeclType(cast<TypeDecl>(SD->getUnderlyingDecl()));
Douglas Gregor90c99722011-02-24 00:17:56 +0000725 TypeLocBuilder TLB;
726 if (isa<InjectedClassNameType>(T)) {
727 InjectedClassNameTypeLoc InjectedTL
728 = TLB.push<InjectedClassNameTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000729 InjectedTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000730 } else if (isa<RecordType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000731 RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000732 RecordTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000733 } else if (isa<TypedefType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000734 TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000735 TypedefTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000736 } else if (isa<EnumType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000737 EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000738 EnumTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000739 } else if (isa<TemplateTypeParmType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000740 TemplateTypeParmTypeLoc TemplateTypeTL
741 = TLB.push<TemplateTypeParmTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000742 TemplateTypeTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000743 } else if (isa<UnresolvedUsingType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000744 UnresolvedUsingTypeLoc UnresolvedTL
745 = TLB.push<UnresolvedUsingTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000746 UnresolvedTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000747 } else if (isa<SubstTemplateTypeParmType>(T)) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000748 SubstTemplateTypeParmTypeLoc TL
Douglas Gregordfd4b742011-05-04 23:05:40 +0000749 = TLB.push<SubstTemplateTypeParmTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000750 TL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000751 } else if (isa<SubstTemplateTypeParmPackType>(T)) {
752 SubstTemplateTypeParmPackTypeLoc TL
753 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000754 TL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000755 } else {
756 llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier");
Douglas Gregor90c99722011-02-24 00:17:56 +0000757 }
758
Richard Smith91c7bbd2011-10-20 03:28:47 +0000759 if (T->isEnumeralType())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000760 Diag(IdInfo.IdentifierLoc, diag::warn_cxx98_compat_enum_nested_name_spec);
Richard Smith91c7bbd2011-10-20 03:28:47 +0000761
Douglas Gregor90c99722011-02-24 00:17:56 +0000762 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000763 IdInfo.CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000764 return false;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000765 }
Mike Stump11289f42009-09-09 15:08:12 +0000766
Chris Lattner1c428032009-12-07 01:36:53 +0000767 // Otherwise, we have an error case. If we don't want diagnostics, just
768 // return an error now.
769 if (ErrorRecoveryLookup)
Douglas Gregor90c99722011-02-24 00:17:56 +0000770 return true;
Chris Lattner1c428032009-12-07 01:36:53 +0000771
Cedric Venet084381332009-02-14 20:20:19 +0000772 // 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 McCall67c00872009-12-02 08:25:40 +0000775 if (Found.empty()) {
John McCall27b18f82009-11-17 02:14:36 +0000776 Found.clear(LookupOrdinaryName);
777 LookupName(Found, S);
John McCall9f3059a2009-10-09 21:13:30 +0000778 }
Mike Stump11289f42009-09-09 15:08:12 +0000779
Francois Pichetb23dc092011-07-27 01:05:24 +0000780 // In Microsoft mode, if we are within a templated function and we can't
Fangrui Song6907ce22018-07-30 19:24:48 +0000781 // resolve Identifier, then extend the SS with Identifier. This will have
782 // the effect of resolving Identifier during template instantiation.
Francois Pichetb23dc092011-07-27 01:05:24 +0000783 // The goal is to be able to resolve a function call whose
784 // nested-name-specifier is located inside a dependent base class.
Fangrui Song6907ce22018-07-30 19:24:48 +0000785 // Example:
Francois Pichetb23dc092011-07-27 01:05:24 +0000786 //
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 Tokerbfa39342014-01-14 12:51:41 +0000797 if (getLangOpts().MSVCCompat) {
Francois Pichetb23dc092011-07-27 01:05:24 +0000798 DeclContext *DC = LookupCtx ? LookupCtx : CurContext;
799 if (DC->isDependentContext() && DC->isFunctionOrMethod()) {
Reid Kleckner062be332014-08-14 23:34:52 +0000800 CXXRecordDecl *ContainingClass = dyn_cast<CXXRecordDecl>(DC->getParent());
801 if (ContainingClass && ContainingClass->hasAnyDependentBases()) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000802 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 Kleckner062be332014-08-14 23:34:52 +0000807 return false;
808 }
Francois Pichetb23dc092011-07-27 01:05:24 +0000809 }
810 }
811
David Blaikie6cab5962014-02-09 06:54:23 +0000812 if (!Found.empty()) {
813 if (TypeDecl *TD = Found.getAsSingle<TypeDecl>())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000814 Diag(IdInfo.IdentifierLoc, diag::err_expected_class_or_namespace)
Richard Trieu42b98e22016-10-28 00:15:24 +0000815 << Context.getTypeDeclType(TD) << getLangOpts().CPlusPlus;
David Blaikie6cab5962014-02-09 06:54:23 +0000816 else {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000817 Diag(IdInfo.IdentifierLoc, diag::err_expected_class_or_namespace)
818 << IdInfo.Identifier << getLangOpts().CPlusPlus;
David Blaikie6cab5962014-02-09 06:54:23 +0000819 if (NamedDecl *ND = Found.getAsSingle<NamedDecl>())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000820 Diag(ND->getLocation(), diag::note_entity_declared_at)
821 << IdInfo.Identifier;
David Blaikie6cab5962014-02-09 06:54:23 +0000822 }
823 } else if (SS.isSet())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000824 Diag(IdInfo.IdentifierLoc, diag::err_no_member) << IdInfo.Identifier
825 << LookupCtx << SS.getRange();
Cedric Venet084381332009-02-14 20:20:19 +0000826 else
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000827 Diag(IdInfo.IdentifierLoc, diag::err_undeclared_var_use)
828 << IdInfo.Identifier;
Mike Stump11289f42009-09-09 15:08:12 +0000829
Douglas Gregor90c99722011-02-24 00:17:56 +0000830 return true;
Cedric Venet084381332009-02-14 20:20:19 +0000831}
832
Matthias Gehredc01bb42017-03-17 21:41:20 +0000833bool Sema::ActOnCXXNestedNameSpecifier(Scope *S, NestedNameSpecInfo &IdInfo,
834 bool EnteringContext, CXXScopeSpec &SS,
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000835 bool ErrorRecoveryLookup,
Matthias Gehredc01bb42017-03-17 21:41:20 +0000836 bool *IsCorrectedToColon,
837 bool OnlyNamespace) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000838 if (SS.isInvalid())
839 return true;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000840
Matthias Gehredc01bb42017-03-17 21:41:20 +0000841 return BuildCXXNestedNameSpecifier(S, IdInfo, EnteringContext, SS,
Craig Topperc3ec1492014-05-26 06:22:03 +0000842 /*ScopeLookupResult=*/nullptr, false,
Matthias Gehredc01bb42017-03-17 21:41:20 +0000843 IsCorrectedToColon, OnlyNamespace);
Chris Lattner1c428032009-12-07 01:36:53 +0000844}
845
David Blaikie15a430a2011-12-04 05:04:18 +0000846bool 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 Trieu02826552018-07-07 00:17:25 +0000855 if (T.isNull())
856 return true;
857
David Blaikie15a430a2011-12-04 05:04:18 +0000858 if (!T->isDependentType() && !T->getAs<TagType>()) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000859 Diag(DS.getTypeSpecTypeLoc(), diag::err_expected_class_or_namespace)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000860 << T << getLangOpts().CPlusPlus;
David Blaikie15a430a2011-12-04 05:04:18 +0000861 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 Lattner1c428032009-12-07 01:36:53 +0000872/// 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 Yasskinc76498d2010-04-08 16:38:48 +0000878bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS,
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000879 NestedNameSpecInfo &IdInfo,
Chris Lattner1c428032009-12-07 01:36:53 +0000880 bool EnteringContext) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000881 if (SS.isInvalid())
882 return false;
Craig Topperc3ec1492014-05-26 06:22:03 +0000883
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000884 return !BuildCXXNestedNameSpecifier(S, IdInfo, EnteringContext, SS,
Craig Topperc3ec1492014-05-26 06:22:03 +0000885 /*ScopeLookupResult=*/nullptr, true);
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000886}
887
Douglas Gregor90c99722011-02-24 00:17:56 +0000888bool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000889 CXXScopeSpec &SS,
890 SourceLocation TemplateKWLoc,
Richard Smithb23c5e82019-05-09 03:31:27 +0000891 TemplateTy OpaqueTemplate,
Douglas Gregor6e068012011-02-28 00:04:36 +0000892 SourceLocation TemplateNameLoc,
893 SourceLocation LAngleLoc,
894 ASTTemplateArgsPtr TemplateArgsIn,
895 SourceLocation RAngleLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +0000896 SourceLocation CCLoc,
Douglas Gregor6e068012011-02-28 00:04:36 +0000897 bool EnteringContext) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000898 if (SS.isInvalid())
899 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000900
Richard Smithb23c5e82019-05-09 03:31:27 +0000901 TemplateName Template = OpaqueTemplate.get();
902
Douglas Gregor6e068012011-02-28 00:04:36 +0000903 // Translate the parser's template argument list in our AST format.
904 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
905 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
906
Richard Smithb23c5e82019-05-09 03:31:27 +0000907 DependentTemplateName *DTN = Template.getAsDependentTemplateName();
Richard Smith72bfbd82013-12-04 00:28:23 +0000908 if (DTN && DTN->isIdentifier()) {
Douglas Gregor6e068012011-02-28 00:04:36 +0000909 // Handle a dependent template specialization for which we cannot resolve
910 // the template name.
Richard Smith74bb2d22013-03-26 00:54:11 +0000911 assert(DTN->getQualifier() == SS.getScopeRep());
Douglas Gregor6e068012011-02-28 00:04:36 +0000912 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
Douglas Gregora7a795b2011-03-01 20:11:18 +0000913 DTN->getQualifier(),
914 DTN->getIdentifier(),
Douglas Gregor6e068012011-02-28 00:04:36 +0000915 TemplateArgs);
Fangrui Song6907ce22018-07-30 19:24:48 +0000916
Douglas Gregor6e068012011-02-28 00:04:36 +0000917 // Create source-location information for this type.
918 TypeLocBuilder Builder;
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000919 DependentTemplateSpecializationTypeLoc SpecTL
Douglas Gregor6e068012011-02-28 00:04:36 +0000920 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000921 SpecTL.setElaboratedKeywordLoc(SourceLocation());
922 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000923 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000924 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000925 SpecTL.setLAngleLoc(LAngleLoc);
926 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000927 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
928 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Fangrui Song6907ce22018-07-30 19:24:48 +0000929
Abramo Bagnara7945c982012-01-27 09:46:47 +0000930 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
Douglas Gregor6e068012011-02-28 00:04:36 +0000931 CCLoc);
932 return false;
933 }
Richard Smith72bfbd82013-12-04 00:28:23 +0000934
Richard Smithb23c5e82019-05-09 03:31:27 +0000935 // If we assumed an undeclared identifier was a template name, try to
936 // typo-correct it now.
937 if (Template.getAsAssumedTemplateName() &&
938 resolveAssumedTemplateNameAsType(S, Template, TemplateNameLoc))
939 return true;
940
941 TemplateDecl *TD = Template.getAsTemplateDecl();
942 if (Template.getAsOverloadedTemplate() || DTN ||
Richard Smithf95fe9b2013-12-04 00:47:45 +0000943 isa<FunctionTemplateDecl>(TD) || isa<VarTemplateDecl>(TD)) {
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000944 SourceRange R(TemplateNameLoc, RAngleLoc);
945 if (SS.getRange().isValid())
946 R.setBegin(SS.getRange().getBegin());
Richard Smith72bfbd82013-12-04 00:28:23 +0000947
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000948 Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier)
Richard Smithb23c5e82019-05-09 03:31:27 +0000949 << (TD && isa<VarTemplateDecl>(TD)) << Template << R;
950 NoteAllFoundTemplates(Template);
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000951 return true;
952 }
Richard Smith72bfbd82013-12-04 00:28:23 +0000953
Fangrui Song6907ce22018-07-30 19:24:48 +0000954 // We were able to resolve the template name to an actual template.
Douglas Gregor6e068012011-02-28 00:04:36 +0000955 // Build an appropriate nested-name-specifier.
Richard Smithb23c5e82019-05-09 03:31:27 +0000956 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
Douglas Gregor90c99722011-02-24 00:17:56 +0000957 if (T.isNull())
958 return true;
959
Richard Smith3f1b5d02011-05-05 21:57:07 +0000960 // Alias template specializations can produce types which are not valid
961 // nested name specifiers.
962 if (!T->isDependentType() && !T->getAs<TagType>()) {
963 Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T;
Richard Smithb23c5e82019-05-09 03:31:27 +0000964 NoteAllFoundTemplates(Template);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000965 return true;
966 }
Douglas Gregor6e068012011-02-28 00:04:36 +0000967
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000968 // Provide source-location information for the template specialization type.
Douglas Gregor6e068012011-02-28 00:04:36 +0000969 TypeLocBuilder Builder;
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000970 TemplateSpecializationTypeLoc SpecTL
Douglas Gregor6e068012011-02-28 00:04:36 +0000971 = Builder.push<TemplateSpecializationTypeLoc>(T);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000972 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
973 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000974 SpecTL.setLAngleLoc(LAngleLoc);
975 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000976 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
977 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
978
979
Abramo Bagnara7945c982012-01-27 09:46:47 +0000980 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
Douglas Gregor6e068012011-02-28 00:04:36 +0000981 CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000982 return false;
Douglas Gregor7f741122009-02-25 19:37:18 +0000983}
984
Douglas Gregor869ad452011-02-24 17:54:50 +0000985namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000986 /// A structure that stores a nested-name-specifier annotation,
Fangrui Song6907ce22018-07-30 19:24:48 +0000987 /// including both the nested-name-specifier
Douglas Gregor869ad452011-02-24 17:54:50 +0000988 struct NestedNameSpecifierAnnotation {
989 NestedNameSpecifier *NNS;
990 };
991}
992
993void *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) {
994 if (SS.isEmpty() || SS.isInvalid())
Craig Topperc3ec1492014-05-26 06:22:03 +0000995 return nullptr;
996
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000997 void *Mem = Context.Allocate(
998 (sizeof(NestedNameSpecifierAnnotation) + SS.location_size()),
999 alignof(NestedNameSpecifierAnnotation));
Douglas Gregor869ad452011-02-24 17:54:50 +00001000 NestedNameSpecifierAnnotation *Annotation
1001 = new (Mem) NestedNameSpecifierAnnotation;
1002 Annotation->NNS = SS.getScopeRep();
1003 memcpy(Annotation + 1, SS.location_data(), SS.location_size());
1004 return Annotation;
1005}
1006
Fangrui Song6907ce22018-07-30 19:24:48 +00001007void Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr,
Douglas Gregor869ad452011-02-24 17:54:50 +00001008 SourceRange AnnotationRange,
1009 CXXScopeSpec &SS) {
1010 if (!AnnotationPtr) {
1011 SS.SetInvalid(AnnotationRange);
1012 return;
1013 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001014
Douglas Gregor869ad452011-02-24 17:54:50 +00001015 NestedNameSpecifierAnnotation *Annotation
1016 = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr);
1017 SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1));
1018}
1019
John McCall2b058ef2009-12-11 20:04:54 +00001020bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
1021 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
1022
Alex Lorenze151f0102016-12-07 10:24:44 +00001023 // Don't enter a declarator context when the current context is an Objective-C
1024 // declaration.
1025 if (isa<ObjCContainerDecl>(CurContext) || isa<ObjCMethodDecl>(CurContext))
1026 return false;
1027
Richard Smith74bb2d22013-03-26 00:54:11 +00001028 NestedNameSpecifier *Qualifier = SS.getScopeRep();
John McCall2b058ef2009-12-11 20:04:54 +00001029
1030 // There are only two places a well-formed program may qualify a
1031 // declarator: first, when defining a namespace or class member
1032 // out-of-line, and second, when naming an explicitly-qualified
1033 // friend function. The latter case is governed by
1034 // C++03 [basic.lookup.unqual]p10:
1035 // In a friend declaration naming a member function, a name used
1036 // in the function declarator and not part of a template-argument
1037 // in a template-id is first looked up in the scope of the member
1038 // function's class. If it is not found, or if the name is part of
1039 // a template-argument in a template-id, the look up is as
1040 // described for unqualified names in the definition of the class
1041 // granting friendship.
1042 // i.e. we don't push a scope unless it's a class member.
1043
1044 switch (Qualifier->getKind()) {
1045 case NestedNameSpecifier::Global:
1046 case NestedNameSpecifier::Namespace:
Douglas Gregor7b26ff92011-02-24 02:36:08 +00001047 case NestedNameSpecifier::NamespaceAlias:
John McCall2b058ef2009-12-11 20:04:54 +00001048 // These are always namespace scopes. We never want to enter a
1049 // namespace scope from anything but a file context.
Sebastian Redl50c68252010-08-31 00:36:30 +00001050 return CurContext->getRedeclContext()->isFileContext();
John McCall2b058ef2009-12-11 20:04:54 +00001051
1052 case NestedNameSpecifier::Identifier:
1053 case NestedNameSpecifier::TypeSpec:
1054 case NestedNameSpecifier::TypeSpecWithTemplate:
Nikola Smiljanic67860242014-09-26 00:28:20 +00001055 case NestedNameSpecifier::Super:
John McCall2b058ef2009-12-11 20:04:54 +00001056 // These are never namespace scopes.
1057 return true;
1058 }
1059
David Blaikie8a40f702012-01-17 06:56:22 +00001060 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
John McCall2b058ef2009-12-11 20:04:54 +00001061}
1062
Cedric Venet084381332009-02-14 20:20:19 +00001063/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
1064/// scope or nested-name-specifier) is parsed, part of a declarator-id.
1065/// After this method is called, according to [C++ 3.4.3p3], names should be
1066/// looked up in the declarator-id's scope, until the declarator is parsed and
1067/// ActOnCXXExitDeclaratorScope is called.
1068/// The 'SS' should be a non-empty valid CXXScopeSpec.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00001069bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) {
Cedric Venet084381332009-02-14 20:20:19 +00001070 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
John McCall6df5fef2009-12-19 10:49:29 +00001071
1072 if (SS.isInvalid()) return true;
1073
1074 DeclContext *DC = computeDeclContext(SS, true);
1075 if (!DC) return true;
1076
1077 // Before we enter a declarator's context, we need to make sure that
1078 // it is a complete declaration context.
John McCall0b66eb32010-05-01 00:40:08 +00001079 if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC))
John McCall6df5fef2009-12-19 10:49:29 +00001080 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001081
John McCall6df5fef2009-12-19 10:49:29 +00001082 EnterDeclaratorContext(S, DC);
John McCall2408e322010-04-27 00:57:59 +00001083
1084 // Rebuild the nested name specifier for the new scope.
1085 if (DC->isDependentContext())
1086 RebuildNestedNameSpecifierInCurrentInstantiation(SS);
1087
Douglas Gregor5013a7e2009-09-24 23:39:01 +00001088 return false;
Cedric Venet084381332009-02-14 20:20:19 +00001089}
1090
1091/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
1092/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
1093/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
1094/// Used to indicate that names should revert to being looked up in the
1095/// defining scope.
1096void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
1097 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
Douglas Gregor053f6912009-08-26 00:04:55 +00001098 if (SS.isInvalid())
1099 return;
John McCall6df5fef2009-12-19 10:49:29 +00001100 assert(!SS.isInvalid() && computeDeclContext(SS, true) &&
1101 "exiting declarator scope we never really entered");
1102 ExitDeclaratorContext(S);
Cedric Venet084381332009-02-14 20:20:19 +00001103}