blob: 6f1b8d2e96cd5f4e8daad47b064a4d4c8de8318f [file] [log] [blame]
Cedric Venet084381332009-02-14 20:20:19 +00001//===--- SemaCXXScopeSpec.cpp - Semantic Analysis for C++ scope specifiers-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements C++ semantic analysis for scope specifiers.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "TypeLocBuilder.h"
Cedric Venet084381332009-02-14 20:20:19 +000015#include "clang/AST/ASTContext.h"
Douglas Gregorc9f9b862009-05-11 19:58:34 +000016#include "clang/AST/DeclTemplate.h"
Douglas Gregord8061562009-08-06 03:17:00 +000017#include "clang/AST/ExprCXX.h"
Douglas Gregor52537682009-03-19 00:18:19 +000018#include "clang/AST/NestedNameSpecifier.h"
Anders Carlssond624e162009-08-26 23:45:07 +000019#include "clang/Basic/PartialDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000020#include "clang/Sema/DeclSpec.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Sema/Lookup.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000022#include "clang/Sema/SemaInternal.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "clang/Sema/Template.h"
Cedric Venet084381332009-02-14 20:20:19 +000024#include "llvm/ADT/STLExtras.h"
25using namespace clang;
26
Douglas Gregor41127182009-11-04 22:49:18 +000027/// \brief Find the current instantiation that associated with the given type.
Richard Smithd80b2d52012-11-22 00:24:47 +000028static CXXRecordDecl *getCurrentInstantiationOf(QualType T,
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000029 DeclContext *CurContext) {
Douglas Gregor41127182009-11-04 22:49:18 +000030 if (T.isNull())
Craig Topperc3ec1492014-05-26 06:22:03 +000031 return nullptr;
John McCall2408e322010-04-27 00:57:59 +000032
33 const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000034 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
35 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
Richard Smithd80b2d52012-11-22 00:24:47 +000036 if (!Record->isDependentContext() ||
37 Record->isCurrentInstantiation(CurContext))
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000038 return Record;
39
Craig Topperc3ec1492014-05-26 06:22:03 +000040 return nullptr;
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000041 } else if (isa<InjectedClassNameType>(Ty))
John McCall2408e322010-04-27 00:57:59 +000042 return cast<InjectedClassNameType>(Ty)->getDecl();
43 else
Craig Topperc3ec1492014-05-26 06:22:03 +000044 return nullptr;
Douglas Gregor41127182009-11-04 22:49:18 +000045}
46
Douglas Gregorb7bfe792009-09-02 22:59:36 +000047/// \brief Compute the DeclContext that is associated with the given type.
48///
49/// \param T the type for which we are attempting to find a DeclContext.
50///
Mike Stump11289f42009-09-09 15:08:12 +000051/// \returns the declaration context represented by the type T,
Douglas Gregorb7bfe792009-09-02 22:59:36 +000052/// or NULL if the declaration context cannot be computed (e.g., because it is
53/// dependent and not the current instantiation).
54DeclContext *Sema::computeDeclContext(QualType T) {
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000055 if (!T->isDependentType())
56 if (const TagType *Tag = T->getAs<TagType>())
57 return Tag->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +000058
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000059 return ::getCurrentInstantiationOf(T, CurContext);
Douglas Gregorb7bfe792009-09-02 22:59:36 +000060}
61
Douglas Gregor52537682009-03-19 00:18:19 +000062/// \brief Compute the DeclContext that is associated with the given
63/// scope specifier.
Douglas Gregord8d297c2009-07-21 23:53:31 +000064///
65/// \param SS the C++ scope specifier as it appears in the source
66///
67/// \param EnteringContext when true, we will be entering the context of
68/// this scope specifier, so we can retrieve the declaration context of a
69/// class template or class template partial specialization even if it is
70/// not the current instantiation.
71///
72/// \returns the declaration context represented by the scope specifier @p SS,
73/// or NULL if the declaration context cannot be computed (e.g., because it is
74/// dependent and not the current instantiation).
75DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS,
76 bool EnteringContext) {
Douglas Gregor52537682009-03-19 00:18:19 +000077 if (!SS.isSet() || SS.isInvalid())
Craig Topperc3ec1492014-05-26 06:22:03 +000078 return nullptr;
Douglas Gregor6bfde492009-03-18 00:36:05 +000079
Richard Smith74bb2d22013-03-26 00:54:11 +000080 NestedNameSpecifier *NNS = SS.getScopeRep();
Douglas Gregorc9f9b862009-05-11 19:58:34 +000081 if (NNS->isDependent()) {
82 // If this nested-name-specifier refers to the current
83 // instantiation, return its DeclContext.
84 if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS))
85 return Record;
Mike Stump11289f42009-09-09 15:08:12 +000086
Douglas Gregord8d297c2009-07-21 23:53:31 +000087 if (EnteringContext) {
John McCalle78aac42010-03-10 03:28:59 +000088 const Type *NNSType = NNS->getAsType();
89 if (!NNSType) {
Craig Topperc3ec1492014-05-26 06:22:03 +000090 return nullptr;
Richard Smith3f1b5d02011-05-05 21:57:07 +000091 }
92
93 // Look through type alias templates, per C++0x [temp.dep.type]p1.
94 NNSType = Context.getCanonicalType(NNSType);
95 if (const TemplateSpecializationType *SpecType
96 = NNSType->getAs<TemplateSpecializationType>()) {
Douglas Gregore861bac2009-08-25 22:51:20 +000097 // We are entering the context of the nested name specifier, so try to
98 // match the nested name specifier to either a primary class template
99 // or a class template partial specialization.
Mike Stump11289f42009-09-09 15:08:12 +0000100 if (ClassTemplateDecl *ClassTemplate
Douglas Gregord8d297c2009-07-21 23:53:31 +0000101 = dyn_cast_or_null<ClassTemplateDecl>(
102 SpecType->getTemplateName().getAsTemplateDecl())) {
Douglas Gregor15301382009-07-30 17:40:51 +0000103 QualType ContextType
104 = Context.getCanonicalType(QualType(SpecType, 0));
105
Douglas Gregord8d297c2009-07-21 23:53:31 +0000106 // If the type of the nested name specifier is the same as the
107 // injected class name of the named class template, we're entering
108 // into that class template definition.
John McCalle78aac42010-03-10 03:28:59 +0000109 QualType Injected
Douglas Gregor9961ce92010-07-08 18:37:38 +0000110 = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregor15301382009-07-30 17:40:51 +0000111 if (Context.hasSameType(Injected, ContextType))
Douglas Gregord8d297c2009-07-21 23:53:31 +0000112 return ClassTemplate->getTemplatedDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000113
Douglas Gregor15301382009-07-30 17:40:51 +0000114 // If the type of the nested name specifier is the same as the
115 // type of one of the class template's class template partial
116 // specializations, we're entering into the definition of that
117 // class template partial specialization.
118 if (ClassTemplatePartialSpecializationDecl *PartialSpec
Richard Smith6739a102016-05-05 00:56:12 +0000119 = ClassTemplate->findPartialSpecialization(ContextType)) {
120 // A declaration of the partial specialization must be visible.
121 // We can always recover here, because this only happens when we're
122 // entering the context, and that can't happen in a SFINAE context.
123 assert(!isSFINAEContext() &&
124 "partial specialization scope specifier in SFINAE context?");
125 if (!hasVisibleDeclaration(PartialSpec))
126 diagnoseMissingImport(SS.getLastQualifierNameLoc(), PartialSpec,
127 MissingImportKind::PartialSpecialization,
128 /*Recover*/true);
Douglas Gregor15301382009-07-30 17:40:51 +0000129 return PartialSpec;
Richard Smith6739a102016-05-05 00:56:12 +0000130 }
Douglas Gregord8d297c2009-07-21 23:53:31 +0000131 }
John McCalle78aac42010-03-10 03:28:59 +0000132 } else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) {
Douglas Gregore861bac2009-08-25 22:51:20 +0000133 // The nested name specifier refers to a member of a class template.
134 return RecordT->getDecl();
Douglas Gregord8d297c2009-07-21 23:53:31 +0000135 }
136 }
Mike Stump11289f42009-09-09 15:08:12 +0000137
Craig Topperc3ec1492014-05-26 06:22:03 +0000138 return nullptr;
Douglas Gregorc9f9b862009-05-11 19:58:34 +0000139 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000140
141 switch (NNS->getKind()) {
142 case NestedNameSpecifier::Identifier:
David Blaikie83d382b2011-09-23 05:06:16 +0000143 llvm_unreachable("Dependent nested-name-specifier has no DeclContext");
Douglas Gregorf21eb492009-03-26 23:50:42 +0000144
145 case NestedNameSpecifier::Namespace:
146 return NNS->getAsNamespace();
147
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000148 case NestedNameSpecifier::NamespaceAlias:
149 return NNS->getAsNamespaceAlias()->getNamespace();
150
Douglas Gregorf21eb492009-03-26 23:50:42 +0000151 case NestedNameSpecifier::TypeSpec:
152 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000153 const TagType *Tag = NNS->getAsType()->getAs<TagType>();
154 assert(Tag && "Non-tag type in nested-name-specifier");
155 return Tag->getDecl();
David Blaikie8a40f702012-01-17 06:56:22 +0000156 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000157
158 case NestedNameSpecifier::Global:
159 return Context.getTranslationUnitDecl();
Nikola Smiljanic67860242014-09-26 00:28:20 +0000160
161 case NestedNameSpecifier::Super:
162 return NNS->getAsRecordDecl();
Douglas Gregorf21eb492009-03-26 23:50:42 +0000163 }
164
David Blaikie8a40f702012-01-17 06:56:22 +0000165 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
Douglas Gregor6bfde492009-03-18 00:36:05 +0000166}
167
Douglas Gregor90a1a652009-03-19 17:26:29 +0000168bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) {
169 if (!SS.isSet() || SS.isInvalid())
170 return false;
171
Richard Smith74bb2d22013-03-26 00:54:11 +0000172 return SS.getScopeRep()->isDependent();
Douglas Gregor90a1a652009-03-19 17:26:29 +0000173}
174
Douglas Gregorc9f9b862009-05-11 19:58:34 +0000175/// \brief If the given nested name specifier refers to the current
176/// instantiation, return the declaration that corresponds to that
177/// current instantiation (C++0x [temp.dep.type]p1).
178///
179/// \param NNS a dependent nested name specifier.
180CXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000181 assert(getLangOpts().CPlusPlus && "Only callable in C++");
Douglas Gregorc9f9b862009-05-11 19:58:34 +0000182 assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed");
183
Douglas Gregord8d297c2009-07-21 23:53:31 +0000184 if (!NNS->getAsType())
Craig Topperc3ec1492014-05-26 06:22:03 +0000185 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000186
Douglas Gregorb9a955d2009-07-31 18:32:42 +0000187 QualType T = QualType(NNS->getAsType(), 0);
Douglas Gregorbf2b26d2011-02-19 19:24:40 +0000188 return ::getCurrentInstantiationOf(T, CurContext);
Douglas Gregorc9f9b862009-05-11 19:58:34 +0000189}
190
Douglas Gregor26897462009-03-11 16:48:53 +0000191/// \brief Require that the context specified by SS be complete.
192///
193/// If SS refers to a type, this routine checks whether the type is
194/// complete enough (or can be made complete enough) for name lookup
195/// into the DeclContext. A type that is not yet completed can be
196/// considered "complete enough" if it is a class/struct/union/enum
197/// that is currently being defined. Or, if we have a type that names
198/// a class template specialization that is not a complete type, we
199/// will attempt to instantiate that class template.
John McCall0b66eb32010-05-01 00:40:08 +0000200bool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS,
201 DeclContext *DC) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000202 assert(DC && "given null context");
Mike Stump11289f42009-09-09 15:08:12 +0000203
Richard Smith4b38ded2012-03-14 23:13:10 +0000204 TagDecl *tag = dyn_cast<TagDecl>(DC);
Douglas Gregor8a6d15d2010-02-05 04:39:02 +0000205
Richard Smith4b38ded2012-03-14 23:13:10 +0000206 // If this is a dependent type, then we consider it complete.
Richard Smith6739a102016-05-05 00:56:12 +0000207 // FIXME: This is wrong; we should require a (visible) definition to
208 // exist in this case too.
Richard Smith4b38ded2012-03-14 23:13:10 +0000209 if (!tag || tag->isDependentContext())
210 return false;
Douglas Gregor26897462009-03-11 16:48:53 +0000211
Richard Smith4b38ded2012-03-14 23:13:10 +0000212 // If we're currently defining this type, then lookup into the
213 // type is okay: don't complain that it isn't complete yet.
214 QualType type = Context.getTypeDeclType(tag);
215 const TagType *tagType = type->getAs<TagType>();
216 if (tagType && tagType->isBeingDefined())
217 return false;
John McCall21878762011-07-06 06:57:57 +0000218
Richard Smith4b38ded2012-03-14 23:13:10 +0000219 SourceLocation loc = SS.getLastQualifierNameLoc();
220 if (loc.isInvalid()) loc = SS.getRange().getBegin();
John McCall21878762011-07-06 06:57:57 +0000221
Richard Smith4b38ded2012-03-14 23:13:10 +0000222 // The type must be complete.
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000223 if (RequireCompleteType(loc, type, diag::err_incomplete_nested_name_spec,
224 SS.getRange())) {
Richard Smith4b38ded2012-03-14 23:13:10 +0000225 SS.SetInvalid(SS.getRange());
226 return true;
Douglas Gregor26897462009-03-11 16:48:53 +0000227 }
228
Richard Smith4b38ded2012-03-14 23:13:10 +0000229 // Fixed enum types are complete, but they aren't valid as scopes
230 // until we see a definition, so awkwardly pull out this special
231 // case.
232 const EnumType *enumType = dyn_cast_or_null<EnumType>(tagType);
Richard Smith6739a102016-05-05 00:56:12 +0000233 if (!enumType)
Richard Smith4b38ded2012-03-14 23:13:10 +0000234 return false;
Richard Smith6739a102016-05-05 00:56:12 +0000235 if (enumType->getDecl()->isCompleteDefinition()) {
236 // If we know about the definition but it is not visible, complain.
237 NamedDecl *SuggestedDef = nullptr;
238 if (!hasVisibleDefinition(enumType->getDecl(), &SuggestedDef,
239 /*OnlyNeedComplete*/false)) {
240 // If the user is going to see an error here, recover by making the
241 // definition visible.
242 bool TreatAsComplete = !isSFINAEContext();
243 diagnoseMissingImport(loc, SuggestedDef, MissingImportKind::Definition,
244 /*Recover*/TreatAsComplete);
245 return !TreatAsComplete;
246 }
247 return false;
248 }
Richard Smith4b38ded2012-03-14 23:13:10 +0000249
250 // Try to instantiate the definition, if this is a specialization of an
251 // enumeration temploid.
252 EnumDecl *ED = enumType->getDecl();
253 if (EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) {
254 MemberSpecializationInfo *MSI = ED->getMemberSpecializationInfo();
Richard Smith7d137e32012-03-23 03:33:32 +0000255 if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) {
256 if (InstantiateEnum(loc, ED, Pattern, getTemplateInstantiationArgs(ED),
257 TSK_ImplicitInstantiation)) {
258 SS.SetInvalid(SS.getRange());
259 return true;
260 }
261 return false;
262 }
Richard Smith4b38ded2012-03-14 23:13:10 +0000263 }
264
265 Diag(loc, diag::err_incomplete_nested_name_spec)
266 << type << SS.getRange();
267 SS.SetInvalid(SS.getRange());
268 return true;
Douglas Gregor26897462009-03-11 16:48:53 +0000269}
Cedric Venet084381332009-02-14 20:20:19 +0000270
Nikola Smiljanic67860242014-09-26 00:28:20 +0000271bool Sema::ActOnCXXGlobalScopeSpecifier(SourceLocation CCLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +0000272 CXXScopeSpec &SS) {
273 SS.MakeGlobal(Context, CCLoc);
274 return false;
Cedric Venet084381332009-02-14 20:20:19 +0000275}
276
Nikola Smiljanic67860242014-09-26 00:28:20 +0000277bool Sema::ActOnSuperScopeSpecifier(SourceLocation SuperLoc,
278 SourceLocation ColonColonLoc,
279 CXXScopeSpec &SS) {
280 CXXRecordDecl *RD = nullptr;
281 for (Scope *S = getCurScope(); S; S = S->getParent()) {
282 if (S->isFunctionScope()) {
283 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(S->getEntity()))
284 RD = MD->getParent();
285 break;
286 }
287 if (S->isClassScope()) {
288 RD = cast<CXXRecordDecl>(S->getEntity());
289 break;
290 }
291 }
292
293 if (!RD) {
294 Diag(SuperLoc, diag::err_invalid_super_scope);
295 return true;
296 } else if (RD->isLambda()) {
297 Diag(SuperLoc, diag::err_super_in_lambda_unsupported);
298 return true;
299 } else if (RD->getNumBases() == 0) {
300 Diag(SuperLoc, diag::err_no_base_classes) << RD->getName();
301 return true;
302 }
303
304 SS.MakeSuper(Context, RD, SuperLoc, ColonColonLoc);
305 return false;
306}
307
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000308/// \brief Determines whether the given declaration is an valid acceptable
309/// result for name lookup of a nested-name-specifier.
Serge Pavlov25a8afa2015-01-18 20:04:35 +0000310/// \param SD Declaration checked for nested-name-specifier.
311/// \param IsExtension If not null and the declaration is accepted as an
312/// extension, the pointed variable is assigned true.
313bool Sema::isAcceptableNestedNameSpecifier(const NamedDecl *SD,
314 bool *IsExtension) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000315 if (!SD)
316 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000317
Richard Smithf2005d32015-12-29 23:34:32 +0000318 SD = SD->getUnderlyingDecl();
319
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000320 // Namespace and namespace aliases are fine.
Richard Smithf2005d32015-12-29 23:34:32 +0000321 if (isa<NamespaceDecl>(SD))
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000322 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000323
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000324 if (!isa<TypeDecl>(SD))
325 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000326
Richard Smithc8239732011-10-18 21:39:00 +0000327 // Determine whether we have a class (or, in C++11, an enum) or
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000328 // a typedef thereof. If so, build the nested-name-specifier.
329 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
330 if (T->isDependentType())
331 return true;
Serge Pavlov25a8afa2015-01-18 20:04:35 +0000332 if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) {
333 if (TD->getUnderlyingType()->isRecordType())
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000334 return true;
Serge Pavlov25a8afa2015-01-18 20:04:35 +0000335 if (TD->getUnderlyingType()->isEnumeralType()) {
336 if (Context.getLangOpts().CPlusPlus11)
337 return true;
338 if (IsExtension)
339 *IsExtension = true;
340 }
341 } else if (isa<RecordDecl>(SD)) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000342 return true;
Serge Pavlov25a8afa2015-01-18 20:04:35 +0000343 } else if (isa<EnumDecl>(SD)) {
344 if (Context.getLangOpts().CPlusPlus11)
345 return true;
346 if (IsExtension)
347 *IsExtension = true;
348 }
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000349
350 return false;
351}
352
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000353/// \brief If the given nested-name-specifier begins with a bare identifier
Mike Stump11289f42009-09-09 15:08:12 +0000354/// (e.g., Base::), perform name lookup for that identifier as a
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000355/// nested-name-specifier within the given scope, and return the result of that
356/// name lookup.
357NamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) {
358 if (!S || !NNS)
Craig Topperc3ec1492014-05-26 06:22:03 +0000359 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000360
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000361 while (NNS->getPrefix())
362 NNS = NNS->getPrefix();
Mike Stump11289f42009-09-09 15:08:12 +0000363
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000364 if (NNS->getKind() != NestedNameSpecifier::Identifier)
Craig Topperc3ec1492014-05-26 06:22:03 +0000365 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000366
John McCall27b18f82009-11-17 02:14:36 +0000367 LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(),
368 LookupNestedNameSpecifierName);
369 LookupName(Found, S);
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000370 assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet");
371
John McCall67c00872009-12-02 08:25:40 +0000372 if (!Found.isSingleResult())
Craig Topperc3ec1492014-05-26 06:22:03 +0000373 return nullptr;
John McCall67c00872009-12-02 08:25:40 +0000374
375 NamedDecl *Result = Found.getFoundDecl();
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000376 if (isAcceptableNestedNameSpecifier(Result))
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000377 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000378
Craig Topperc3ec1492014-05-26 06:22:03 +0000379 return nullptr;
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000380}
381
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +0000382bool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS,
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000383 NestedNameSpecInfo &IdInfo) {
384 QualType ObjectType = GetTypeFromParser(IdInfo.ObjectType);
385 LookupResult Found(*this, IdInfo.Identifier, IdInfo.IdentifierLoc,
386 LookupNestedNameSpecifierName);
387
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000388 // Determine where to perform name lookup
Craig Topperc3ec1492014-05-26 06:22:03 +0000389 DeclContext *LookupCtx = nullptr;
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000390 bool isDependent = false;
391 if (!ObjectType.isNull()) {
392 // This nested-name-specifier occurs in a member access expression, e.g.,
393 // x->B::f, and we are looking into the type of the object.
394 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
395 LookupCtx = computeDeclContext(ObjectType);
396 isDependent = ObjectType->isDependentType();
397 } else if (SS.isSet()) {
398 // This nested-name-specifier occurs after another nested-name-specifier,
399 // so long into the context associated with the prior nested-name-specifier.
400 LookupCtx = computeDeclContext(SS, false);
401 isDependent = isDependentScopeSpecifier(SS);
402 Found.setContextRange(SS.getRange());
403 }
404
405 if (LookupCtx) {
406 // Perform "qualified" name lookup into the declaration context we
407 // computed, which is either the type of the base of a member access
408 // expression or the declaration context associated with a prior
409 // nested-name-specifier.
410
411 // The declaration context must be complete.
John McCall0b66eb32010-05-01 00:40:08 +0000412 if (!LookupCtx->isDependentContext() &&
413 RequireCompleteDeclContext(SS, LookupCtx))
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000414 return false;
415
416 LookupQualifiedName(Found, LookupCtx);
417 } else if (isDependent) {
418 return false;
419 } else {
420 LookupName(Found, S);
421 }
422 Found.suppressDiagnostics();
423
Richard Smithf2005d32015-12-29 23:34:32 +0000424 return Found.getAsSingle<NamespaceDecl>();
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000425}
426
Kaelyn Uhrainfb96ec72012-01-12 22:32:39 +0000427namespace {
428
429// Callback to only accept typo corrections that can be a valid C++ member
430// intializer: either a non-static field member or a base class.
431class NestedNameSpecifierValidatorCCC : public CorrectionCandidateCallback {
432 public:
433 explicit NestedNameSpecifierValidatorCCC(Sema &SRef)
434 : SRef(SRef) {}
435
Craig Toppere14c0f82014-03-12 04:55:44 +0000436 bool ValidateCandidate(const TypoCorrection &candidate) override {
Kaelyn Uhrainfb96ec72012-01-12 22:32:39 +0000437 return SRef.isAcceptableNestedNameSpecifier(candidate.getCorrectionDecl());
438 }
439
440 private:
441 Sema &SRef;
442};
443
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000444}
Kaelyn Uhrainfb96ec72012-01-12 22:32:39 +0000445
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000446/// \brief Build a new nested-name-specifier for "identifier::", as described
447/// by ActOnCXXNestedNameSpecifier.
448///
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000449/// \param S Scope in which the nested-name-specifier occurs.
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000450/// \param IdInfo Parser information about an identifier in the
451/// nested-name-spec.
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000452/// \param EnteringContext If true, enter the context specified by the
453/// nested-name-specifier.
454/// \param SS Optional nested name specifier preceding the identifier.
455/// \param ScopeLookupResult Provides the result of name lookup within the
456/// scope of the nested-name-specifier that was computed at template
457/// definition time.
458/// \param ErrorRecoveryLookup Specifies if the method is called to improve
459/// error recovery and what kind of recovery is performed.
460/// \param IsCorrectedToColon If not null, suggestion of replace '::' -> ':'
461/// are allowed. The bool value pointed by this parameter is set to
462/// 'true' if the identifier is treated as if it was followed by ':',
463/// not '::'.
Matthias Gehredc01bb42017-03-17 21:41:20 +0000464/// \param OnlyNamespace If true, only considers namespaces in lookup.
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000465///
466/// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000467/// that it contains an extra parameter \p ScopeLookupResult, which provides
468/// the result of name lookup within the scope of the nested-name-specifier
Douglas Gregorad183ac2009-12-30 16:01:52 +0000469/// that was computed at template definition time.
Chris Lattner1c428032009-12-07 01:36:53 +0000470///
471/// If ErrorRecoveryLookup is true, then this call is used to improve error
472/// recovery. This means that it should not emit diagnostics, it should
Douglas Gregor90c99722011-02-24 00:17:56 +0000473/// just return true on failure. It also means it should only return a valid
Chris Lattner1c428032009-12-07 01:36:53 +0000474/// scope if it *knows* that the result is correct. It should not return in a
Douglas Gregor90c99722011-02-24 00:17:56 +0000475/// dependent context, for example. Nor will it extend \p SS with the scope
476/// specifier.
Matthias Gehredc01bb42017-03-17 21:41:20 +0000477bool Sema::BuildCXXNestedNameSpecifier(Scope *S, NestedNameSpecInfo &IdInfo,
478 bool EnteringContext, CXXScopeSpec &SS,
Douglas Gregor90c99722011-02-24 00:17:56 +0000479 NamedDecl *ScopeLookupResult,
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000480 bool ErrorRecoveryLookup,
Matthias Gehredc01bb42017-03-17 21:41:20 +0000481 bool *IsCorrectedToColon,
482 bool OnlyNamespace) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000483 LookupResult Found(*this, IdInfo.Identifier, IdInfo.IdentifierLoc,
Matthias Gehredc01bb42017-03-17 21:41:20 +0000484 OnlyNamespace ? LookupNamespaceName
485 : LookupNestedNameSpecifierName);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000486 QualType ObjectType = GetTypeFromParser(IdInfo.ObjectType);
John McCall27b18f82009-11-17 02:14:36 +0000487
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000488 // Determine where to perform name lookup
Craig Topperc3ec1492014-05-26 06:22:03 +0000489 DeclContext *LookupCtx = nullptr;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000490 bool isDependent = false;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000491 if (IsCorrectedToColon)
492 *IsCorrectedToColon = false;
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000493 if (!ObjectType.isNull()) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000494 // This nested-name-specifier occurs in a member access expression, e.g.,
495 // x->B::f, and we are looking into the type of the object.
496 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000497 LookupCtx = computeDeclContext(ObjectType);
498 isDependent = ObjectType->isDependentType();
499 } else if (SS.isSet()) {
500 // This nested-name-specifier occurs after another nested-name-specifier,
Richard Smith3f1b5d02011-05-05 21:57:07 +0000501 // so look into the context associated with the prior nested-name-specifier.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000502 LookupCtx = computeDeclContext(SS, EnteringContext);
503 isDependent = isDependentScopeSpecifier(SS);
John McCall27b18f82009-11-17 02:14:36 +0000504 Found.setContextRange(SS.getRange());
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000505 }
Mike Stump11289f42009-09-09 15:08:12 +0000506
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000507 bool ObjectTypeSearchedInScope = false;
508 if (LookupCtx) {
Mike Stump11289f42009-09-09 15:08:12 +0000509 // Perform "qualified" name lookup into the declaration context we
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000510 // computed, which is either the type of the base of a member access
Mike Stump11289f42009-09-09 15:08:12 +0000511 // expression or the declaration context associated with a prior
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000512 // nested-name-specifier.
Mike Stump11289f42009-09-09 15:08:12 +0000513
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000514 // The declaration context must be complete.
John McCall0b66eb32010-05-01 00:40:08 +0000515 if (!LookupCtx->isDependentContext() &&
516 RequireCompleteDeclContext(SS, LookupCtx))
Douglas Gregor90c99722011-02-24 00:17:56 +0000517 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000518
John McCall27b18f82009-11-17 02:14:36 +0000519 LookupQualifiedName(Found, LookupCtx);
Mike Stump11289f42009-09-09 15:08:12 +0000520
John McCall27b18f82009-11-17 02:14:36 +0000521 if (!ObjectType.isNull() && Found.empty()) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000522 // C++ [basic.lookup.classref]p4:
523 // If the id-expression in a class member access is a qualified-id of
Mike Stump11289f42009-09-09 15:08:12 +0000524 // the form
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000525 //
526 // class-name-or-namespace-name::...
527 //
Mike Stump11289f42009-09-09 15:08:12 +0000528 // the class-name-or-namespace-name following the . or -> operator is
529 // looked up both in the context of the entire postfix-expression and in
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000530 // the scope of the class of the object expression. If the name is found
Mike Stump11289f42009-09-09 15:08:12 +0000531 // only in the scope of the class of the object expression, the name
532 // shall refer to a class-name. If the name is found only in the
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000533 // context of the entire postfix-expression, the name shall refer to a
534 // class-name or namespace-name. [...]
535 //
536 // Qualified name lookup into a class will not find a namespace-name,
Douglas Gregor9d07dfa2011-05-15 17:27:27 +0000537 // so we do not need to diagnose that case specifically. However,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000538 // this qualified name lookup may find nothing. In that case, perform
Mike Stump11289f42009-09-09 15:08:12 +0000539 // unqualified name lookup in the given scope (if available) or
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000540 // reconstruct the result from when name lookup was performed at template
541 // definition time.
542 if (S)
John McCall27b18f82009-11-17 02:14:36 +0000543 LookupName(Found, S);
John McCall9f3059a2009-10-09 21:13:30 +0000544 else if (ScopeLookupResult)
545 Found.addDecl(ScopeLookupResult);
Mike Stump11289f42009-09-09 15:08:12 +0000546
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000547 ObjectTypeSearchedInScope = true;
548 }
Douglas Gregordf65c8ed2010-07-28 14:49:07 +0000549 } else if (!isDependent) {
550 // Perform unqualified name lookup in the current scope.
551 LookupName(Found, S);
552 }
553
Richard Smith4b213d72015-11-12 22:40:09 +0000554 if (Found.isAmbiguous())
555 return true;
556
Douglas Gregordf65c8ed2010-07-28 14:49:07 +0000557 // If we performed lookup into a dependent context and did not find anything,
558 // that's fine: just build a dependent nested-name-specifier.
559 if (Found.empty() && isDependent &&
560 !(LookupCtx && LookupCtx->isRecord() &&
561 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
562 !cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()))) {
Chris Lattner1c428032009-12-07 01:36:53 +0000563 // Don't speculate if we're just trying to improve error recovery.
564 if (ErrorRecoveryLookup)
Douglas Gregor90c99722011-02-24 00:17:56 +0000565 return true;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000566
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000567 // We were not able to compute the declaration context for a dependent
Mike Stump11289f42009-09-09 15:08:12 +0000568 // base object type or prior nested-name-specifier, so this
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000569 // nested-name-specifier refers to an unknown specialization. Just build
570 // a dependent nested-name-specifier.
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000571 SS.Extend(Context, IdInfo.Identifier, IdInfo.IdentifierLoc, IdInfo.CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000572 return false;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000573 }
574
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000575 if (Found.empty() && !ErrorRecoveryLookup) {
576 // If identifier is not found as class-name-or-namespace-name, but is found
577 // as other entity, don't look for typos.
578 LookupResult R(*this, Found.getLookupNameInfo(), LookupOrdinaryName);
579 if (LookupCtx)
580 LookupQualifiedName(R, LookupCtx);
581 else if (S && !isDependent)
582 LookupName(R, S);
583 if (!R.empty()) {
Richard Smith4b213d72015-11-12 22:40:09 +0000584 // Don't diagnose problems with this speculative lookup.
585 R.suppressDiagnostics();
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000586 // The identifier is found in ordinary lookup. If correction to colon is
587 // allowed, suggest replacement to ':'.
588 if (IsCorrectedToColon) {
589 *IsCorrectedToColon = true;
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000590 Diag(IdInfo.CCLoc, diag::err_nested_name_spec_is_not_class)
591 << IdInfo.Identifier << getLangOpts().CPlusPlus
592 << FixItHint::CreateReplacement(IdInfo.CCLoc, ":");
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000593 if (NamedDecl *ND = R.getAsSingle<NamedDecl>())
594 Diag(ND->getLocation(), diag::note_declared_at);
595 return true;
596 }
597 // Replacement '::' -> ':' is not allowed, just issue respective error.
Matthias Gehredc01bb42017-03-17 21:41:20 +0000598 Diag(R.getNameLoc(), OnlyNamespace
599 ? diag::err_expected_namespace_name
600 : diag::err_expected_class_or_namespace)
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000601 << IdInfo.Identifier << getLangOpts().CPlusPlus;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000602 if (NamedDecl *ND = R.getAsSingle<NamedDecl>())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000603 Diag(ND->getLocation(), diag::note_entity_declared_at)
604 << IdInfo.Identifier;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000605 return true;
606 }
607 }
608
Alp Tokerbfa39342014-01-14 12:51:41 +0000609 if (Found.empty() && !ErrorRecoveryLookup && !getLangOpts().MSVCCompat) {
Douglas Gregor532e68f2009-12-31 08:26:35 +0000610 // We haven't found anything, and we're not recovering from a
611 // different kind of error, so look for typos.
612 DeclarationName Name = Found.getLookupName();
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000613 Found.clear();
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000614 if (TypoCorrection Corrected = CorrectTypo(
615 Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS,
616 llvm::make_unique<NestedNameSpecifierValidatorCCC>(*this),
617 CTK_ErrorRecovery, LookupCtx, EnteringContext)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000618 if (LookupCtx) {
619 bool DroppedSpecifier =
620 Corrected.WillReplaceSpecifier() &&
621 Name.getAsString() == Corrected.getAsString(getLangOpts());
Kaelyn Uhraina6c78fe2013-12-16 19:19:18 +0000622 if (DroppedSpecifier)
623 SS.clear();
Richard Smithf9b15102013-08-17 00:46:16 +0000624 diagnoseTypo(Corrected, PDiag(diag::err_no_member_suggest)
625 << Name << LookupCtx << DroppedSpecifier
626 << SS.getRange());
627 } else
628 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
629 << Name);
Kaelyn Uhrain10413a42013-07-02 23:47:44 +0000630
Reid Kleckner4ce625c2016-02-16 19:16:20 +0000631 if (Corrected.getCorrectionSpecifier())
632 SS.MakeTrivial(Context, Corrected.getCorrectionSpecifier(),
633 SourceRange(Found.getNameLoc()));
634
Richard Smithde6d6c42015-12-29 19:43:10 +0000635 if (NamedDecl *ND = Corrected.getFoundDecl())
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000636 Found.addDecl(ND);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000637 Found.setLookupName(Corrected.getCorrection());
Douglas Gregorc048c522010-06-29 19:27:42 +0000638 } else {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000639 Found.setLookupName(IdInfo.Identifier);
Douglas Gregorc048c522010-06-29 19:27:42 +0000640 }
Douglas Gregor532e68f2009-12-31 08:26:35 +0000641 }
642
Richard Smithf2005d32015-12-29 23:34:32 +0000643 NamedDecl *SD =
644 Found.isSingleResult() ? Found.getRepresentativeDecl() : nullptr;
Serge Pavlov25a8afa2015-01-18 20:04:35 +0000645 bool IsExtension = false;
646 bool AcceptSpec = isAcceptableNestedNameSpecifier(SD, &IsExtension);
647 if (!AcceptSpec && IsExtension) {
648 AcceptSpec = true;
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000649 Diag(IdInfo.IdentifierLoc, diag::ext_nested_name_spec_is_enum);
Serge Pavlov25a8afa2015-01-18 20:04:35 +0000650 }
651 if (AcceptSpec) {
Douglas Gregor1b02e4a2012-05-01 20:23:02 +0000652 if (!ObjectType.isNull() && !ObjectTypeSearchedInScope &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000653 !getLangOpts().CPlusPlus11) {
Douglas Gregor1b02e4a2012-05-01 20:23:02 +0000654 // C++03 [basic.lookup.classref]p4:
Mike Stump11289f42009-09-09 15:08:12 +0000655 // [...] If the name is found in both contexts, the
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000656 // class-name-or-namespace-name shall refer to the same entity.
657 //
658 // We already found the name in the scope of the object. Now, look
659 // into the current scope (the scope of the postfix-expression) to
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000660 // see if we can find the same name there. As above, if there is no
661 // scope, reconstruct the result from the template instantiation itself.
Douglas Gregor1b02e4a2012-05-01 20:23:02 +0000662 //
663 // Note that C++11 does *not* perform this redundant lookup.
John McCall9f3059a2009-10-09 21:13:30 +0000664 NamedDecl *OuterDecl;
665 if (S) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000666 LookupResult FoundOuter(*this, IdInfo.Identifier, IdInfo.IdentifierLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +0000667 LookupNestedNameSpecifierName);
John McCall27b18f82009-11-17 02:14:36 +0000668 LookupName(FoundOuter, S);
John McCall67c00872009-12-02 08:25:40 +0000669 OuterDecl = FoundOuter.getAsSingle<NamedDecl>();
John McCall9f3059a2009-10-09 21:13:30 +0000670 } else
671 OuterDecl = ScopeLookupResult;
Mike Stump11289f42009-09-09 15:08:12 +0000672
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000673 if (isAcceptableNestedNameSpecifier(OuterDecl) &&
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000674 OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() &&
675 (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) ||
676 !Context.hasSameType(
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000677 Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)),
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000678 Context.getTypeDeclType(cast<TypeDecl>(SD))))) {
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000679 if (ErrorRecoveryLookup)
680 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000681
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000682 Diag(IdInfo.IdentifierLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +0000683 diag::err_nested_name_member_ref_lookup_ambiguous)
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000684 << IdInfo.Identifier;
Douglas Gregor90c99722011-02-24 00:17:56 +0000685 Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type)
686 << ObjectType;
687 Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope);
688
689 // Fall through so that we'll pick the name we found in the object
690 // type, since that's probably what the user wanted anyway.
691 }
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000692 }
Mike Stump11289f42009-09-09 15:08:12 +0000693
Nico Weber72889432014-09-06 01:25:55 +0000694 if (auto *TD = dyn_cast_or_null<TypedefNameDecl>(SD))
695 MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false);
696
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000697 // If we're just performing this lookup for error-recovery purposes,
Douglas Gregor90c99722011-02-24 00:17:56 +0000698 // don't extend the nested-name-specifier. Just return now.
699 if (ErrorRecoveryLookup)
700 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +0000701
702 // The use of a nested name specifier may trigger deprecation warnings.
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000703 DiagnoseUseOfDecl(SD, IdInfo.CCLoc);
Aaron Ballman43f40102014-11-14 22:34:56 +0000704
Douglas Gregor90c99722011-02-24 00:17:56 +0000705 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000706 SS.Extend(Context, Namespace, IdInfo.IdentifierLoc, IdInfo.CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000707 return false;
708 }
Mike Stump11289f42009-09-09 15:08:12 +0000709
Douglas Gregor90c99722011-02-24 00:17:56 +0000710 if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000711 SS.Extend(Context, Alias, IdInfo.IdentifierLoc, IdInfo.CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000712 return false;
713 }
Mike Stump11289f42009-09-09 15:08:12 +0000714
Richard Smithf2005d32015-12-29 23:34:32 +0000715 QualType T =
716 Context.getTypeDeclType(cast<TypeDecl>(SD->getUnderlyingDecl()));
Douglas Gregor90c99722011-02-24 00:17:56 +0000717 TypeLocBuilder TLB;
718 if (isa<InjectedClassNameType>(T)) {
719 InjectedClassNameTypeLoc InjectedTL
720 = TLB.push<InjectedClassNameTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000721 InjectedTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000722 } else if (isa<RecordType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000723 RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000724 RecordTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000725 } else if (isa<TypedefType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000726 TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000727 TypedefTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000728 } else if (isa<EnumType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000729 EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000730 EnumTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000731 } else if (isa<TemplateTypeParmType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000732 TemplateTypeParmTypeLoc TemplateTypeTL
733 = TLB.push<TemplateTypeParmTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000734 TemplateTypeTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000735 } else if (isa<UnresolvedUsingType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000736 UnresolvedUsingTypeLoc UnresolvedTL
737 = TLB.push<UnresolvedUsingTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000738 UnresolvedTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000739 } else if (isa<SubstTemplateTypeParmType>(T)) {
740 SubstTemplateTypeParmTypeLoc TL
741 = TLB.push<SubstTemplateTypeParmTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000742 TL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000743 } else if (isa<SubstTemplateTypeParmPackType>(T)) {
744 SubstTemplateTypeParmPackTypeLoc TL
745 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000746 TL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000747 } else {
748 llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier");
Douglas Gregor90c99722011-02-24 00:17:56 +0000749 }
750
Richard Smith91c7bbd2011-10-20 03:28:47 +0000751 if (T->isEnumeralType())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000752 Diag(IdInfo.IdentifierLoc, diag::warn_cxx98_compat_enum_nested_name_spec);
Richard Smith91c7bbd2011-10-20 03:28:47 +0000753
Douglas Gregor90c99722011-02-24 00:17:56 +0000754 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000755 IdInfo.CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000756 return false;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000757 }
Mike Stump11289f42009-09-09 15:08:12 +0000758
Chris Lattner1c428032009-12-07 01:36:53 +0000759 // Otherwise, we have an error case. If we don't want diagnostics, just
760 // return an error now.
761 if (ErrorRecoveryLookup)
Douglas Gregor90c99722011-02-24 00:17:56 +0000762 return true;
Chris Lattner1c428032009-12-07 01:36:53 +0000763
Cedric Venet084381332009-02-14 20:20:19 +0000764 // If we didn't find anything during our lookup, try again with
765 // ordinary name lookup, which can help us produce better error
766 // messages.
John McCall67c00872009-12-02 08:25:40 +0000767 if (Found.empty()) {
John McCall27b18f82009-11-17 02:14:36 +0000768 Found.clear(LookupOrdinaryName);
769 LookupName(Found, S);
John McCall9f3059a2009-10-09 21:13:30 +0000770 }
Mike Stump11289f42009-09-09 15:08:12 +0000771
Francois Pichetb23dc092011-07-27 01:05:24 +0000772 // In Microsoft mode, if we are within a templated function and we can't
773 // resolve Identifier, then extend the SS with Identifier. This will have
774 // the effect of resolving Identifier during template instantiation.
775 // The goal is to be able to resolve a function call whose
776 // nested-name-specifier is located inside a dependent base class.
777 // Example:
778 //
779 // class C {
780 // public:
781 // static void foo2() { }
782 // };
783 // template <class T> class A { public: typedef C D; };
784 //
785 // template <class T> class B : public A<T> {
786 // public:
787 // void foo() { D::foo2(); }
788 // };
Alp Tokerbfa39342014-01-14 12:51:41 +0000789 if (getLangOpts().MSVCCompat) {
Francois Pichetb23dc092011-07-27 01:05:24 +0000790 DeclContext *DC = LookupCtx ? LookupCtx : CurContext;
791 if (DC->isDependentContext() && DC->isFunctionOrMethod()) {
Reid Kleckner062be332014-08-14 23:34:52 +0000792 CXXRecordDecl *ContainingClass = dyn_cast<CXXRecordDecl>(DC->getParent());
793 if (ContainingClass && ContainingClass->hasAnyDependentBases()) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000794 Diag(IdInfo.IdentifierLoc,
795 diag::ext_undeclared_unqual_id_with_dependent_base)
796 << IdInfo.Identifier << ContainingClass;
797 SS.Extend(Context, IdInfo.Identifier, IdInfo.IdentifierLoc,
798 IdInfo.CCLoc);
Reid Kleckner062be332014-08-14 23:34:52 +0000799 return false;
800 }
Francois Pichetb23dc092011-07-27 01:05:24 +0000801 }
802 }
803
David Blaikie6cab5962014-02-09 06:54:23 +0000804 if (!Found.empty()) {
805 if (TypeDecl *TD = Found.getAsSingle<TypeDecl>())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000806 Diag(IdInfo.IdentifierLoc, diag::err_expected_class_or_namespace)
Richard Trieu42b98e22016-10-28 00:15:24 +0000807 << Context.getTypeDeclType(TD) << getLangOpts().CPlusPlus;
David Blaikie6cab5962014-02-09 06:54:23 +0000808 else {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000809 Diag(IdInfo.IdentifierLoc, diag::err_expected_class_or_namespace)
810 << IdInfo.Identifier << getLangOpts().CPlusPlus;
David Blaikie6cab5962014-02-09 06:54:23 +0000811 if (NamedDecl *ND = Found.getAsSingle<NamedDecl>())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000812 Diag(ND->getLocation(), diag::note_entity_declared_at)
813 << IdInfo.Identifier;
David Blaikie6cab5962014-02-09 06:54:23 +0000814 }
815 } else if (SS.isSet())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000816 Diag(IdInfo.IdentifierLoc, diag::err_no_member) << IdInfo.Identifier
817 << LookupCtx << SS.getRange();
Cedric Venet084381332009-02-14 20:20:19 +0000818 else
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000819 Diag(IdInfo.IdentifierLoc, diag::err_undeclared_var_use)
820 << IdInfo.Identifier;
Mike Stump11289f42009-09-09 15:08:12 +0000821
Douglas Gregor90c99722011-02-24 00:17:56 +0000822 return true;
Cedric Venet084381332009-02-14 20:20:19 +0000823}
824
Matthias Gehredc01bb42017-03-17 21:41:20 +0000825bool Sema::ActOnCXXNestedNameSpecifier(Scope *S, NestedNameSpecInfo &IdInfo,
826 bool EnteringContext, CXXScopeSpec &SS,
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000827 bool ErrorRecoveryLookup,
Matthias Gehredc01bb42017-03-17 21:41:20 +0000828 bool *IsCorrectedToColon,
829 bool OnlyNamespace) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000830 if (SS.isInvalid())
831 return true;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000832
Matthias Gehredc01bb42017-03-17 21:41:20 +0000833 return BuildCXXNestedNameSpecifier(S, IdInfo, EnteringContext, SS,
Craig Topperc3ec1492014-05-26 06:22:03 +0000834 /*ScopeLookupResult=*/nullptr, false,
Matthias Gehredc01bb42017-03-17 21:41:20 +0000835 IsCorrectedToColon, OnlyNamespace);
Chris Lattner1c428032009-12-07 01:36:53 +0000836}
837
David Blaikie15a430a2011-12-04 05:04:18 +0000838bool Sema::ActOnCXXNestedNameSpecifierDecltype(CXXScopeSpec &SS,
839 const DeclSpec &DS,
840 SourceLocation ColonColonLoc) {
841 if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error)
842 return true;
843
844 assert(DS.getTypeSpecType() == DeclSpec::TST_decltype);
845
846 QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc());
847 if (!T->isDependentType() && !T->getAs<TagType>()) {
David Blaikie6cab5962014-02-09 06:54:23 +0000848 Diag(DS.getTypeSpecTypeLoc(), diag::err_expected_class_or_namespace)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000849 << T << getLangOpts().CPlusPlus;
David Blaikie15a430a2011-12-04 05:04:18 +0000850 return true;
851 }
852
853 TypeLocBuilder TLB;
854 DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T);
855 DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc());
856 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
857 ColonColonLoc);
858 return false;
859}
860
Chris Lattner1c428032009-12-07 01:36:53 +0000861/// IsInvalidUnlessNestedName - This method is used for error recovery
862/// purposes to determine whether the specified identifier is only valid as
863/// a nested name specifier, for example a namespace name. It is
864/// conservatively correct to always return false from this method.
865///
866/// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +0000867bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS,
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000868 NestedNameSpecInfo &IdInfo,
Chris Lattner1c428032009-12-07 01:36:53 +0000869 bool EnteringContext) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000870 if (SS.isInvalid())
871 return false;
Craig Topperc3ec1492014-05-26 06:22:03 +0000872
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000873 return !BuildCXXNestedNameSpecifier(S, IdInfo, EnteringContext, SS,
Craig Topperc3ec1492014-05-26 06:22:03 +0000874 /*ScopeLookupResult=*/nullptr, true);
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000875}
876
Douglas Gregor90c99722011-02-24 00:17:56 +0000877bool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000878 CXXScopeSpec &SS,
879 SourceLocation TemplateKWLoc,
Douglas Gregor6e068012011-02-28 00:04:36 +0000880 TemplateTy Template,
881 SourceLocation TemplateNameLoc,
882 SourceLocation LAngleLoc,
883 ASTTemplateArgsPtr TemplateArgsIn,
884 SourceLocation RAngleLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +0000885 SourceLocation CCLoc,
Douglas Gregor6e068012011-02-28 00:04:36 +0000886 bool EnteringContext) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000887 if (SS.isInvalid())
888 return true;
889
Douglas Gregor6e068012011-02-28 00:04:36 +0000890 // Translate the parser's template argument list in our AST format.
891 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
892 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
893
Richard Smith72bfbd82013-12-04 00:28:23 +0000894 DependentTemplateName *DTN = Template.get().getAsDependentTemplateName();
895 if (DTN && DTN->isIdentifier()) {
Douglas Gregor6e068012011-02-28 00:04:36 +0000896 // Handle a dependent template specialization for which we cannot resolve
897 // the template name.
Richard Smith74bb2d22013-03-26 00:54:11 +0000898 assert(DTN->getQualifier() == SS.getScopeRep());
Douglas Gregor6e068012011-02-28 00:04:36 +0000899 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
Douglas Gregora7a795b2011-03-01 20:11:18 +0000900 DTN->getQualifier(),
901 DTN->getIdentifier(),
Douglas Gregor6e068012011-02-28 00:04:36 +0000902 TemplateArgs);
903
904 // Create source-location information for this type.
905 TypeLocBuilder Builder;
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000906 DependentTemplateSpecializationTypeLoc SpecTL
Douglas Gregor6e068012011-02-28 00:04:36 +0000907 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000908 SpecTL.setElaboratedKeywordLoc(SourceLocation());
909 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000910 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000911 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000912 SpecTL.setLAngleLoc(LAngleLoc);
913 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000914 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
915 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
916
Abramo Bagnara7945c982012-01-27 09:46:47 +0000917 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
Douglas Gregor6e068012011-02-28 00:04:36 +0000918 CCLoc);
919 return false;
920 }
Richard Smith72bfbd82013-12-04 00:28:23 +0000921
Richard Smithf95fe9b2013-12-04 00:47:45 +0000922 TemplateDecl *TD = Template.get().getAsTemplateDecl();
Richard Smith72bfbd82013-12-04 00:28:23 +0000923 if (Template.get().getAsOverloadedTemplate() || DTN ||
Richard Smithf95fe9b2013-12-04 00:47:45 +0000924 isa<FunctionTemplateDecl>(TD) || isa<VarTemplateDecl>(TD)) {
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000925 SourceRange R(TemplateNameLoc, RAngleLoc);
926 if (SS.getRange().isValid())
927 R.setBegin(SS.getRange().getBegin());
Richard Smith72bfbd82013-12-04 00:28:23 +0000928
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000929 Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier)
Richard Smithf95fe9b2013-12-04 00:47:45 +0000930 << (TD && isa<VarTemplateDecl>(TD)) << Template.get() << R;
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000931 NoteAllFoundTemplates(Template.get());
932 return true;
933 }
Richard Smith72bfbd82013-12-04 00:28:23 +0000934
Douglas Gregor6e068012011-02-28 00:04:36 +0000935 // We were able to resolve the template name to an actual template.
936 // Build an appropriate nested-name-specifier.
Richard Smith74f02342017-01-19 21:00:13 +0000937 QualType T =
938 CheckTemplateIdType(Template.get(), TemplateNameLoc, TemplateArgs);
Douglas Gregor90c99722011-02-24 00:17:56 +0000939 if (T.isNull())
940 return true;
941
Richard Smith3f1b5d02011-05-05 21:57:07 +0000942 // Alias template specializations can produce types which are not valid
943 // nested name specifiers.
944 if (!T->isDependentType() && !T->getAs<TagType>()) {
945 Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T;
946 NoteAllFoundTemplates(Template.get());
947 return true;
948 }
Douglas Gregor6e068012011-02-28 00:04:36 +0000949
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000950 // Provide source-location information for the template specialization type.
Douglas Gregor6e068012011-02-28 00:04:36 +0000951 TypeLocBuilder Builder;
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000952 TemplateSpecializationTypeLoc SpecTL
Douglas Gregor6e068012011-02-28 00:04:36 +0000953 = Builder.push<TemplateSpecializationTypeLoc>(T);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000954 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
955 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000956 SpecTL.setLAngleLoc(LAngleLoc);
957 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000958 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
959 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
960
961
Abramo Bagnara7945c982012-01-27 09:46:47 +0000962 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
Douglas Gregor6e068012011-02-28 00:04:36 +0000963 CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000964 return false;
Douglas Gregor7f741122009-02-25 19:37:18 +0000965}
966
Douglas Gregor869ad452011-02-24 17:54:50 +0000967namespace {
968 /// \brief A structure that stores a nested-name-specifier annotation,
969 /// including both the nested-name-specifier
970 struct NestedNameSpecifierAnnotation {
971 NestedNameSpecifier *NNS;
972 };
973}
974
975void *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) {
976 if (SS.isEmpty() || SS.isInvalid())
Craig Topperc3ec1492014-05-26 06:22:03 +0000977 return nullptr;
978
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000979 void *Mem = Context.Allocate(
980 (sizeof(NestedNameSpecifierAnnotation) + SS.location_size()),
981 alignof(NestedNameSpecifierAnnotation));
Douglas Gregor869ad452011-02-24 17:54:50 +0000982 NestedNameSpecifierAnnotation *Annotation
983 = new (Mem) NestedNameSpecifierAnnotation;
984 Annotation->NNS = SS.getScopeRep();
985 memcpy(Annotation + 1, SS.location_data(), SS.location_size());
986 return Annotation;
987}
988
989void Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr,
990 SourceRange AnnotationRange,
991 CXXScopeSpec &SS) {
992 if (!AnnotationPtr) {
993 SS.SetInvalid(AnnotationRange);
994 return;
995 }
996
997 NestedNameSpecifierAnnotation *Annotation
998 = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr);
999 SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1));
1000}
1001
John McCall2b058ef2009-12-11 20:04:54 +00001002bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
1003 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
1004
Alex Lorenze151f0102016-12-07 10:24:44 +00001005 // Don't enter a declarator context when the current context is an Objective-C
1006 // declaration.
1007 if (isa<ObjCContainerDecl>(CurContext) || isa<ObjCMethodDecl>(CurContext))
1008 return false;
1009
Richard Smith74bb2d22013-03-26 00:54:11 +00001010 NestedNameSpecifier *Qualifier = SS.getScopeRep();
John McCall2b058ef2009-12-11 20:04:54 +00001011
1012 // There are only two places a well-formed program may qualify a
1013 // declarator: first, when defining a namespace or class member
1014 // out-of-line, and second, when naming an explicitly-qualified
1015 // friend function. The latter case is governed by
1016 // C++03 [basic.lookup.unqual]p10:
1017 // In a friend declaration naming a member function, a name used
1018 // in the function declarator and not part of a template-argument
1019 // in a template-id is first looked up in the scope of the member
1020 // function's class. If it is not found, or if the name is part of
1021 // a template-argument in a template-id, the look up is as
1022 // described for unqualified names in the definition of the class
1023 // granting friendship.
1024 // i.e. we don't push a scope unless it's a class member.
1025
1026 switch (Qualifier->getKind()) {
1027 case NestedNameSpecifier::Global:
1028 case NestedNameSpecifier::Namespace:
Douglas Gregor7b26ff92011-02-24 02:36:08 +00001029 case NestedNameSpecifier::NamespaceAlias:
John McCall2b058ef2009-12-11 20:04:54 +00001030 // These are always namespace scopes. We never want to enter a
1031 // namespace scope from anything but a file context.
Sebastian Redl50c68252010-08-31 00:36:30 +00001032 return CurContext->getRedeclContext()->isFileContext();
John McCall2b058ef2009-12-11 20:04:54 +00001033
1034 case NestedNameSpecifier::Identifier:
1035 case NestedNameSpecifier::TypeSpec:
1036 case NestedNameSpecifier::TypeSpecWithTemplate:
Nikola Smiljanic67860242014-09-26 00:28:20 +00001037 case NestedNameSpecifier::Super:
John McCall2b058ef2009-12-11 20:04:54 +00001038 // These are never namespace scopes.
1039 return true;
1040 }
1041
David Blaikie8a40f702012-01-17 06:56:22 +00001042 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
John McCall2b058ef2009-12-11 20:04:54 +00001043}
1044
Cedric Venet084381332009-02-14 20:20:19 +00001045/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
1046/// scope or nested-name-specifier) is parsed, part of a declarator-id.
1047/// After this method is called, according to [C++ 3.4.3p3], names should be
1048/// looked up in the declarator-id's scope, until the declarator is parsed and
1049/// ActOnCXXExitDeclaratorScope is called.
1050/// The 'SS' should be a non-empty valid CXXScopeSpec.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00001051bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) {
Cedric Venet084381332009-02-14 20:20:19 +00001052 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
John McCall6df5fef2009-12-19 10:49:29 +00001053
1054 if (SS.isInvalid()) return true;
1055
1056 DeclContext *DC = computeDeclContext(SS, true);
1057 if (!DC) return true;
1058
1059 // Before we enter a declarator's context, we need to make sure that
1060 // it is a complete declaration context.
John McCall0b66eb32010-05-01 00:40:08 +00001061 if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC))
John McCall6df5fef2009-12-19 10:49:29 +00001062 return true;
1063
1064 EnterDeclaratorContext(S, DC);
John McCall2408e322010-04-27 00:57:59 +00001065
1066 // Rebuild the nested name specifier for the new scope.
1067 if (DC->isDependentContext())
1068 RebuildNestedNameSpecifierInCurrentInstantiation(SS);
1069
Douglas Gregor5013a7e2009-09-24 23:39:01 +00001070 return false;
Cedric Venet084381332009-02-14 20:20:19 +00001071}
1072
1073/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
1074/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
1075/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
1076/// Used to indicate that names should revert to being looked up in the
1077/// defining scope.
1078void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
1079 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
Douglas Gregor053f6912009-08-26 00:04:55 +00001080 if (SS.isInvalid())
1081 return;
John McCall6df5fef2009-12-19 10:49:29 +00001082 assert(!SS.isInvalid() && computeDeclContext(SS, true) &&
1083 "exiting declarator scope we never really entered");
1084 ExitDeclaratorContext(S);
Cedric Venet084381332009-02-14 20:20:19 +00001085}