blob: d8971c0d37ebad7ffc84dd291a66e6b483bc8722 [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 '::'.
464///
465/// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000466/// that it contains an extra parameter \p ScopeLookupResult, which provides
467/// the result of name lookup within the scope of the nested-name-specifier
Douglas Gregorad183ac2009-12-30 16:01:52 +0000468/// that was computed at template definition time.
Chris Lattner1c428032009-12-07 01:36:53 +0000469///
470/// If ErrorRecoveryLookup is true, then this call is used to improve error
471/// recovery. This means that it should not emit diagnostics, it should
Douglas Gregor90c99722011-02-24 00:17:56 +0000472/// just return true on failure. It also means it should only return a valid
Chris Lattner1c428032009-12-07 01:36:53 +0000473/// scope if it *knows* that the result is correct. It should not return in a
Douglas Gregor90c99722011-02-24 00:17:56 +0000474/// dependent context, for example. Nor will it extend \p SS with the scope
475/// specifier.
476bool Sema::BuildCXXNestedNameSpecifier(Scope *S,
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000477 NestedNameSpecInfo &IdInfo,
Douglas Gregor90c99722011-02-24 00:17:56 +0000478 bool EnteringContext,
479 CXXScopeSpec &SS,
480 NamedDecl *ScopeLookupResult,
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000481 bool ErrorRecoveryLookup,
482 bool *IsCorrectedToColon) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000483 LookupResult Found(*this, IdInfo.Identifier, IdInfo.IdentifierLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +0000484 LookupNestedNameSpecifierName);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000485 QualType ObjectType = GetTypeFromParser(IdInfo.ObjectType);
John McCall27b18f82009-11-17 02:14:36 +0000486
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000487 // Determine where to perform name lookup
Craig Topperc3ec1492014-05-26 06:22:03 +0000488 DeclContext *LookupCtx = nullptr;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000489 bool isDependent = false;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000490 if (IsCorrectedToColon)
491 *IsCorrectedToColon = false;
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000492 if (!ObjectType.isNull()) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000493 // This nested-name-specifier occurs in a member access expression, e.g.,
494 // x->B::f, and we are looking into the type of the object.
495 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000496 LookupCtx = computeDeclContext(ObjectType);
497 isDependent = ObjectType->isDependentType();
498 } else if (SS.isSet()) {
499 // This nested-name-specifier occurs after another nested-name-specifier,
Richard Smith3f1b5d02011-05-05 21:57:07 +0000500 // so look into the context associated with the prior nested-name-specifier.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000501 LookupCtx = computeDeclContext(SS, EnteringContext);
502 isDependent = isDependentScopeSpecifier(SS);
John McCall27b18f82009-11-17 02:14:36 +0000503 Found.setContextRange(SS.getRange());
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000504 }
Mike Stump11289f42009-09-09 15:08:12 +0000505
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000506 bool ObjectTypeSearchedInScope = false;
507 if (LookupCtx) {
Mike Stump11289f42009-09-09 15:08:12 +0000508 // Perform "qualified" name lookup into the declaration context we
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000509 // computed, which is either the type of the base of a member access
Mike Stump11289f42009-09-09 15:08:12 +0000510 // expression or the declaration context associated with a prior
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000511 // nested-name-specifier.
Mike Stump11289f42009-09-09 15:08:12 +0000512
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000513 // The declaration context must be complete.
John McCall0b66eb32010-05-01 00:40:08 +0000514 if (!LookupCtx->isDependentContext() &&
515 RequireCompleteDeclContext(SS, LookupCtx))
Douglas Gregor90c99722011-02-24 00:17:56 +0000516 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000517
John McCall27b18f82009-11-17 02:14:36 +0000518 LookupQualifiedName(Found, LookupCtx);
Mike Stump11289f42009-09-09 15:08:12 +0000519
John McCall27b18f82009-11-17 02:14:36 +0000520 if (!ObjectType.isNull() && Found.empty()) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000521 // C++ [basic.lookup.classref]p4:
522 // If the id-expression in a class member access is a qualified-id of
Mike Stump11289f42009-09-09 15:08:12 +0000523 // the form
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000524 //
525 // class-name-or-namespace-name::...
526 //
Mike Stump11289f42009-09-09 15:08:12 +0000527 // the class-name-or-namespace-name following the . or -> operator is
528 // looked up both in the context of the entire postfix-expression and in
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000529 // the scope of the class of the object expression. If the name is found
Mike Stump11289f42009-09-09 15:08:12 +0000530 // only in the scope of the class of the object expression, the name
531 // shall refer to a class-name. If the name is found only in the
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000532 // context of the entire postfix-expression, the name shall refer to a
533 // class-name or namespace-name. [...]
534 //
535 // Qualified name lookup into a class will not find a namespace-name,
Douglas Gregor9d07dfa2011-05-15 17:27:27 +0000536 // so we do not need to diagnose that case specifically. However,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000537 // this qualified name lookup may find nothing. In that case, perform
Mike Stump11289f42009-09-09 15:08:12 +0000538 // unqualified name lookup in the given scope (if available) or
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000539 // reconstruct the result from when name lookup was performed at template
540 // definition time.
541 if (S)
John McCall27b18f82009-11-17 02:14:36 +0000542 LookupName(Found, S);
John McCall9f3059a2009-10-09 21:13:30 +0000543 else if (ScopeLookupResult)
544 Found.addDecl(ScopeLookupResult);
Mike Stump11289f42009-09-09 15:08:12 +0000545
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000546 ObjectTypeSearchedInScope = true;
547 }
Douglas Gregordf65c8ed2010-07-28 14:49:07 +0000548 } else if (!isDependent) {
549 // Perform unqualified name lookup in the current scope.
550 LookupName(Found, S);
551 }
552
Richard Smith4b213d72015-11-12 22:40:09 +0000553 if (Found.isAmbiguous())
554 return true;
555
Douglas Gregordf65c8ed2010-07-28 14:49:07 +0000556 // If we performed lookup into a dependent context and did not find anything,
557 // that's fine: just build a dependent nested-name-specifier.
558 if (Found.empty() && isDependent &&
559 !(LookupCtx && LookupCtx->isRecord() &&
560 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
561 !cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()))) {
Chris Lattner1c428032009-12-07 01:36:53 +0000562 // Don't speculate if we're just trying to improve error recovery.
563 if (ErrorRecoveryLookup)
Douglas Gregor90c99722011-02-24 00:17:56 +0000564 return true;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000565
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000566 // We were not able to compute the declaration context for a dependent
Mike Stump11289f42009-09-09 15:08:12 +0000567 // base object type or prior nested-name-specifier, so this
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000568 // nested-name-specifier refers to an unknown specialization. Just build
569 // a dependent nested-name-specifier.
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000570 SS.Extend(Context, IdInfo.Identifier, IdInfo.IdentifierLoc, IdInfo.CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000571 return false;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000572 }
573
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000574 if (Found.empty() && !ErrorRecoveryLookup) {
575 // If identifier is not found as class-name-or-namespace-name, but is found
576 // as other entity, don't look for typos.
577 LookupResult R(*this, Found.getLookupNameInfo(), LookupOrdinaryName);
578 if (LookupCtx)
579 LookupQualifiedName(R, LookupCtx);
580 else if (S && !isDependent)
581 LookupName(R, S);
582 if (!R.empty()) {
Richard Smith4b213d72015-11-12 22:40:09 +0000583 // Don't diagnose problems with this speculative lookup.
584 R.suppressDiagnostics();
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000585 // The identifier is found in ordinary lookup. If correction to colon is
586 // allowed, suggest replacement to ':'.
587 if (IsCorrectedToColon) {
588 *IsCorrectedToColon = true;
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000589 Diag(IdInfo.CCLoc, diag::err_nested_name_spec_is_not_class)
590 << IdInfo.Identifier << getLangOpts().CPlusPlus
591 << FixItHint::CreateReplacement(IdInfo.CCLoc, ":");
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000592 if (NamedDecl *ND = R.getAsSingle<NamedDecl>())
593 Diag(ND->getLocation(), diag::note_declared_at);
594 return true;
595 }
596 // Replacement '::' -> ':' is not allowed, just issue respective error.
597 Diag(R.getNameLoc(), diag::err_expected_class_or_namespace)
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000598 << IdInfo.Identifier << getLangOpts().CPlusPlus;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000599 if (NamedDecl *ND = R.getAsSingle<NamedDecl>())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000600 Diag(ND->getLocation(), diag::note_entity_declared_at)
601 << IdInfo.Identifier;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000602 return true;
603 }
604 }
605
Alp Tokerbfa39342014-01-14 12:51:41 +0000606 if (Found.empty() && !ErrorRecoveryLookup && !getLangOpts().MSVCCompat) {
Douglas Gregor532e68f2009-12-31 08:26:35 +0000607 // We haven't found anything, and we're not recovering from a
608 // different kind of error, so look for typos.
609 DeclarationName Name = Found.getLookupName();
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000610 Found.clear();
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000611 if (TypoCorrection Corrected = CorrectTypo(
612 Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS,
613 llvm::make_unique<NestedNameSpecifierValidatorCCC>(*this),
614 CTK_ErrorRecovery, LookupCtx, EnteringContext)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000615 if (LookupCtx) {
616 bool DroppedSpecifier =
617 Corrected.WillReplaceSpecifier() &&
618 Name.getAsString() == Corrected.getAsString(getLangOpts());
Kaelyn Uhraina6c78fe2013-12-16 19:19:18 +0000619 if (DroppedSpecifier)
620 SS.clear();
Richard Smithf9b15102013-08-17 00:46:16 +0000621 diagnoseTypo(Corrected, PDiag(diag::err_no_member_suggest)
622 << Name << LookupCtx << DroppedSpecifier
623 << SS.getRange());
624 } else
625 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
626 << Name);
Kaelyn Uhrain10413a42013-07-02 23:47:44 +0000627
Reid Kleckner4ce625c2016-02-16 19:16:20 +0000628 if (Corrected.getCorrectionSpecifier())
629 SS.MakeTrivial(Context, Corrected.getCorrectionSpecifier(),
630 SourceRange(Found.getNameLoc()));
631
Richard Smithde6d6c42015-12-29 19:43:10 +0000632 if (NamedDecl *ND = Corrected.getFoundDecl())
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000633 Found.addDecl(ND);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000634 Found.setLookupName(Corrected.getCorrection());
Douglas Gregorc048c522010-06-29 19:27:42 +0000635 } else {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000636 Found.setLookupName(IdInfo.Identifier);
Douglas Gregorc048c522010-06-29 19:27:42 +0000637 }
Douglas Gregor532e68f2009-12-31 08:26:35 +0000638 }
639
Richard Smithf2005d32015-12-29 23:34:32 +0000640 NamedDecl *SD =
641 Found.isSingleResult() ? Found.getRepresentativeDecl() : nullptr;
Serge Pavlov25a8afa2015-01-18 20:04:35 +0000642 bool IsExtension = false;
643 bool AcceptSpec = isAcceptableNestedNameSpecifier(SD, &IsExtension);
644 if (!AcceptSpec && IsExtension) {
645 AcceptSpec = true;
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000646 Diag(IdInfo.IdentifierLoc, diag::ext_nested_name_spec_is_enum);
Serge Pavlov25a8afa2015-01-18 20:04:35 +0000647 }
648 if (AcceptSpec) {
Douglas Gregor1b02e4a2012-05-01 20:23:02 +0000649 if (!ObjectType.isNull() && !ObjectTypeSearchedInScope &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000650 !getLangOpts().CPlusPlus11) {
Douglas Gregor1b02e4a2012-05-01 20:23:02 +0000651 // C++03 [basic.lookup.classref]p4:
Mike Stump11289f42009-09-09 15:08:12 +0000652 // [...] If the name is found in both contexts, the
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000653 // class-name-or-namespace-name shall refer to the same entity.
654 //
655 // We already found the name in the scope of the object. Now, look
656 // into the current scope (the scope of the postfix-expression) to
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000657 // see if we can find the same name there. As above, if there is no
658 // scope, reconstruct the result from the template instantiation itself.
Douglas Gregor1b02e4a2012-05-01 20:23:02 +0000659 //
660 // Note that C++11 does *not* perform this redundant lookup.
John McCall9f3059a2009-10-09 21:13:30 +0000661 NamedDecl *OuterDecl;
662 if (S) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000663 LookupResult FoundOuter(*this, IdInfo.Identifier, IdInfo.IdentifierLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +0000664 LookupNestedNameSpecifierName);
John McCall27b18f82009-11-17 02:14:36 +0000665 LookupName(FoundOuter, S);
John McCall67c00872009-12-02 08:25:40 +0000666 OuterDecl = FoundOuter.getAsSingle<NamedDecl>();
John McCall9f3059a2009-10-09 21:13:30 +0000667 } else
668 OuterDecl = ScopeLookupResult;
Mike Stump11289f42009-09-09 15:08:12 +0000669
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000670 if (isAcceptableNestedNameSpecifier(OuterDecl) &&
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000671 OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() &&
672 (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) ||
673 !Context.hasSameType(
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000674 Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)),
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000675 Context.getTypeDeclType(cast<TypeDecl>(SD))))) {
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000676 if (ErrorRecoveryLookup)
677 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000678
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000679 Diag(IdInfo.IdentifierLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +0000680 diag::err_nested_name_member_ref_lookup_ambiguous)
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000681 << IdInfo.Identifier;
Douglas Gregor90c99722011-02-24 00:17:56 +0000682 Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type)
683 << ObjectType;
684 Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope);
685
686 // Fall through so that we'll pick the name we found in the object
687 // type, since that's probably what the user wanted anyway.
688 }
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000689 }
Mike Stump11289f42009-09-09 15:08:12 +0000690
Nico Weber72889432014-09-06 01:25:55 +0000691 if (auto *TD = dyn_cast_or_null<TypedefNameDecl>(SD))
692 MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false);
693
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000694 // If we're just performing this lookup for error-recovery purposes,
Douglas Gregor90c99722011-02-24 00:17:56 +0000695 // don't extend the nested-name-specifier. Just return now.
696 if (ErrorRecoveryLookup)
697 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +0000698
699 // The use of a nested name specifier may trigger deprecation warnings.
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000700 DiagnoseUseOfDecl(SD, IdInfo.CCLoc);
Aaron Ballman43f40102014-11-14 22:34:56 +0000701
Douglas Gregor90c99722011-02-24 00:17:56 +0000702 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000703 SS.Extend(Context, Namespace, IdInfo.IdentifierLoc, IdInfo.CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000704 return false;
705 }
Mike Stump11289f42009-09-09 15:08:12 +0000706
Douglas Gregor90c99722011-02-24 00:17:56 +0000707 if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000708 SS.Extend(Context, Alias, IdInfo.IdentifierLoc, IdInfo.CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000709 return false;
710 }
Mike Stump11289f42009-09-09 15:08:12 +0000711
Richard Smithf2005d32015-12-29 23:34:32 +0000712 QualType T =
713 Context.getTypeDeclType(cast<TypeDecl>(SD->getUnderlyingDecl()));
Douglas Gregor90c99722011-02-24 00:17:56 +0000714 TypeLocBuilder TLB;
715 if (isa<InjectedClassNameType>(T)) {
716 InjectedClassNameTypeLoc InjectedTL
717 = TLB.push<InjectedClassNameTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000718 InjectedTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000719 } else if (isa<RecordType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000720 RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000721 RecordTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000722 } else if (isa<TypedefType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000723 TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000724 TypedefTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000725 } else if (isa<EnumType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000726 EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000727 EnumTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000728 } else if (isa<TemplateTypeParmType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000729 TemplateTypeParmTypeLoc TemplateTypeTL
730 = TLB.push<TemplateTypeParmTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000731 TemplateTypeTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000732 } else if (isa<UnresolvedUsingType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000733 UnresolvedUsingTypeLoc UnresolvedTL
734 = TLB.push<UnresolvedUsingTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000735 UnresolvedTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000736 } else if (isa<SubstTemplateTypeParmType>(T)) {
737 SubstTemplateTypeParmTypeLoc TL
738 = TLB.push<SubstTemplateTypeParmTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000739 TL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000740 } else if (isa<SubstTemplateTypeParmPackType>(T)) {
741 SubstTemplateTypeParmPackTypeLoc TL
742 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000743 TL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000744 } else {
745 llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier");
Douglas Gregor90c99722011-02-24 00:17:56 +0000746 }
747
Richard Smith91c7bbd2011-10-20 03:28:47 +0000748 if (T->isEnumeralType())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000749 Diag(IdInfo.IdentifierLoc, diag::warn_cxx98_compat_enum_nested_name_spec);
Richard Smith91c7bbd2011-10-20 03:28:47 +0000750
Douglas Gregor90c99722011-02-24 00:17:56 +0000751 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000752 IdInfo.CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000753 return false;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000754 }
Mike Stump11289f42009-09-09 15:08:12 +0000755
Chris Lattner1c428032009-12-07 01:36:53 +0000756 // Otherwise, we have an error case. If we don't want diagnostics, just
757 // return an error now.
758 if (ErrorRecoveryLookup)
Douglas Gregor90c99722011-02-24 00:17:56 +0000759 return true;
Chris Lattner1c428032009-12-07 01:36:53 +0000760
Cedric Venet084381332009-02-14 20:20:19 +0000761 // If we didn't find anything during our lookup, try again with
762 // ordinary name lookup, which can help us produce better error
763 // messages.
John McCall67c00872009-12-02 08:25:40 +0000764 if (Found.empty()) {
John McCall27b18f82009-11-17 02:14:36 +0000765 Found.clear(LookupOrdinaryName);
766 LookupName(Found, S);
John McCall9f3059a2009-10-09 21:13:30 +0000767 }
Mike Stump11289f42009-09-09 15:08:12 +0000768
Francois Pichetb23dc092011-07-27 01:05:24 +0000769 // In Microsoft mode, if we are within a templated function and we can't
770 // resolve Identifier, then extend the SS with Identifier. This will have
771 // the effect of resolving Identifier during template instantiation.
772 // The goal is to be able to resolve a function call whose
773 // nested-name-specifier is located inside a dependent base class.
774 // Example:
775 //
776 // class C {
777 // public:
778 // static void foo2() { }
779 // };
780 // template <class T> class A { public: typedef C D; };
781 //
782 // template <class T> class B : public A<T> {
783 // public:
784 // void foo() { D::foo2(); }
785 // };
Alp Tokerbfa39342014-01-14 12:51:41 +0000786 if (getLangOpts().MSVCCompat) {
Francois Pichetb23dc092011-07-27 01:05:24 +0000787 DeclContext *DC = LookupCtx ? LookupCtx : CurContext;
788 if (DC->isDependentContext() && DC->isFunctionOrMethod()) {
Reid Kleckner062be332014-08-14 23:34:52 +0000789 CXXRecordDecl *ContainingClass = dyn_cast<CXXRecordDecl>(DC->getParent());
790 if (ContainingClass && ContainingClass->hasAnyDependentBases()) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000791 Diag(IdInfo.IdentifierLoc,
792 diag::ext_undeclared_unqual_id_with_dependent_base)
793 << IdInfo.Identifier << ContainingClass;
794 SS.Extend(Context, IdInfo.Identifier, IdInfo.IdentifierLoc,
795 IdInfo.CCLoc);
Reid Kleckner062be332014-08-14 23:34:52 +0000796 return false;
797 }
Francois Pichetb23dc092011-07-27 01:05:24 +0000798 }
799 }
800
David Blaikie6cab5962014-02-09 06:54:23 +0000801 if (!Found.empty()) {
802 if (TypeDecl *TD = Found.getAsSingle<TypeDecl>())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000803 Diag(IdInfo.IdentifierLoc, diag::err_expected_class_or_namespace)
Richard Trieu42b98e22016-10-28 00:15:24 +0000804 << Context.getTypeDeclType(TD) << getLangOpts().CPlusPlus;
David Blaikie6cab5962014-02-09 06:54:23 +0000805 else {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000806 Diag(IdInfo.IdentifierLoc, diag::err_expected_class_or_namespace)
807 << IdInfo.Identifier << getLangOpts().CPlusPlus;
David Blaikie6cab5962014-02-09 06:54:23 +0000808 if (NamedDecl *ND = Found.getAsSingle<NamedDecl>())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000809 Diag(ND->getLocation(), diag::note_entity_declared_at)
810 << IdInfo.Identifier;
David Blaikie6cab5962014-02-09 06:54:23 +0000811 }
812 } else if (SS.isSet())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000813 Diag(IdInfo.IdentifierLoc, diag::err_no_member) << IdInfo.Identifier
814 << LookupCtx << SS.getRange();
Cedric Venet084381332009-02-14 20:20:19 +0000815 else
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000816 Diag(IdInfo.IdentifierLoc, diag::err_undeclared_var_use)
817 << IdInfo.Identifier;
Mike Stump11289f42009-09-09 15:08:12 +0000818
Douglas Gregor90c99722011-02-24 00:17:56 +0000819 return true;
Cedric Venet084381332009-02-14 20:20:19 +0000820}
821
Douglas Gregor90c99722011-02-24 00:17:56 +0000822bool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000823 NestedNameSpecInfo &IdInfo,
Douglas Gregor90c99722011-02-24 00:17:56 +0000824 bool EnteringContext,
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000825 CXXScopeSpec &SS,
826 bool ErrorRecoveryLookup,
827 bool *IsCorrectedToColon) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000828 if (SS.isInvalid())
829 return true;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000830
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000831 return BuildCXXNestedNameSpecifier(S, IdInfo,
832 EnteringContext, SS,
Craig Topperc3ec1492014-05-26 06:22:03 +0000833 /*ScopeLookupResult=*/nullptr, false,
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000834 IsCorrectedToColon);
Chris Lattner1c428032009-12-07 01:36:53 +0000835}
836
David Blaikie15a430a2011-12-04 05:04:18 +0000837bool Sema::ActOnCXXNestedNameSpecifierDecltype(CXXScopeSpec &SS,
838 const DeclSpec &DS,
839 SourceLocation ColonColonLoc) {
840 if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error)
841 return true;
842
843 assert(DS.getTypeSpecType() == DeclSpec::TST_decltype);
844
845 QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc());
846 if (!T->isDependentType() && !T->getAs<TagType>()) {
David Blaikie6cab5962014-02-09 06:54:23 +0000847 Diag(DS.getTypeSpecTypeLoc(), diag::err_expected_class_or_namespace)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000848 << T << getLangOpts().CPlusPlus;
David Blaikie15a430a2011-12-04 05:04:18 +0000849 return true;
850 }
851
852 TypeLocBuilder TLB;
853 DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T);
854 DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc());
855 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
856 ColonColonLoc);
857 return false;
858}
859
Chris Lattner1c428032009-12-07 01:36:53 +0000860/// IsInvalidUnlessNestedName - This method is used for error recovery
861/// purposes to determine whether the specified identifier is only valid as
862/// a nested name specifier, for example a namespace name. It is
863/// conservatively correct to always return false from this method.
864///
865/// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +0000866bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS,
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000867 NestedNameSpecInfo &IdInfo,
Chris Lattner1c428032009-12-07 01:36:53 +0000868 bool EnteringContext) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000869 if (SS.isInvalid())
870 return false;
Craig Topperc3ec1492014-05-26 06:22:03 +0000871
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000872 return !BuildCXXNestedNameSpecifier(S, IdInfo, EnteringContext, SS,
Craig Topperc3ec1492014-05-26 06:22:03 +0000873 /*ScopeLookupResult=*/nullptr, true);
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000874}
875
Douglas Gregor90c99722011-02-24 00:17:56 +0000876bool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000877 CXXScopeSpec &SS,
878 SourceLocation TemplateKWLoc,
Douglas Gregor6e068012011-02-28 00:04:36 +0000879 TemplateTy Template,
880 SourceLocation TemplateNameLoc,
881 SourceLocation LAngleLoc,
882 ASTTemplateArgsPtr TemplateArgsIn,
883 SourceLocation RAngleLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +0000884 SourceLocation CCLoc,
Douglas Gregor6e068012011-02-28 00:04:36 +0000885 bool EnteringContext) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000886 if (SS.isInvalid())
887 return true;
888
Douglas Gregor6e068012011-02-28 00:04:36 +0000889 // Translate the parser's template argument list in our AST format.
890 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
891 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
892
Richard Smith72bfbd82013-12-04 00:28:23 +0000893 DependentTemplateName *DTN = Template.get().getAsDependentTemplateName();
894 if (DTN && DTN->isIdentifier()) {
Douglas Gregor6e068012011-02-28 00:04:36 +0000895 // Handle a dependent template specialization for which we cannot resolve
896 // the template name.
Richard Smith74bb2d22013-03-26 00:54:11 +0000897 assert(DTN->getQualifier() == SS.getScopeRep());
Douglas Gregor6e068012011-02-28 00:04:36 +0000898 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
Douglas Gregora7a795b2011-03-01 20:11:18 +0000899 DTN->getQualifier(),
900 DTN->getIdentifier(),
Douglas Gregor6e068012011-02-28 00:04:36 +0000901 TemplateArgs);
902
903 // Create source-location information for this type.
904 TypeLocBuilder Builder;
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000905 DependentTemplateSpecializationTypeLoc SpecTL
Douglas Gregor6e068012011-02-28 00:04:36 +0000906 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000907 SpecTL.setElaboratedKeywordLoc(SourceLocation());
908 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000909 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000910 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000911 SpecTL.setLAngleLoc(LAngleLoc);
912 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000913 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
914 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
915
Abramo Bagnara7945c982012-01-27 09:46:47 +0000916 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
Douglas Gregor6e068012011-02-28 00:04:36 +0000917 CCLoc);
918 return false;
919 }
Richard Smith72bfbd82013-12-04 00:28:23 +0000920
Richard Smithf95fe9b2013-12-04 00:47:45 +0000921 TemplateDecl *TD = Template.get().getAsTemplateDecl();
Richard Smith72bfbd82013-12-04 00:28:23 +0000922 if (Template.get().getAsOverloadedTemplate() || DTN ||
Richard Smithf95fe9b2013-12-04 00:47:45 +0000923 isa<FunctionTemplateDecl>(TD) || isa<VarTemplateDecl>(TD)) {
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000924 SourceRange R(TemplateNameLoc, RAngleLoc);
925 if (SS.getRange().isValid())
926 R.setBegin(SS.getRange().getBegin());
Richard Smith72bfbd82013-12-04 00:28:23 +0000927
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000928 Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier)
Richard Smithf95fe9b2013-12-04 00:47:45 +0000929 << (TD && isa<VarTemplateDecl>(TD)) << Template.get() << R;
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000930 NoteAllFoundTemplates(Template.get());
931 return true;
932 }
Richard Smith72bfbd82013-12-04 00:28:23 +0000933
Douglas Gregor6e068012011-02-28 00:04:36 +0000934 // We were able to resolve the template name to an actual template.
935 // Build an appropriate nested-name-specifier.
936 QualType T = CheckTemplateIdType(Template.get(), TemplateNameLoc,
937 TemplateArgs);
Douglas Gregor90c99722011-02-24 00:17:56 +0000938 if (T.isNull())
939 return true;
940
Richard Smith3f1b5d02011-05-05 21:57:07 +0000941 // Alias template specializations can produce types which are not valid
942 // nested name specifiers.
943 if (!T->isDependentType() && !T->getAs<TagType>()) {
944 Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T;
945 NoteAllFoundTemplates(Template.get());
946 return true;
947 }
Douglas Gregor6e068012011-02-28 00:04:36 +0000948
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000949 // Provide source-location information for the template specialization type.
Douglas Gregor6e068012011-02-28 00:04:36 +0000950 TypeLocBuilder Builder;
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000951 TemplateSpecializationTypeLoc SpecTL
Douglas Gregor6e068012011-02-28 00:04:36 +0000952 = Builder.push<TemplateSpecializationTypeLoc>(T);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000953 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
954 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000955 SpecTL.setLAngleLoc(LAngleLoc);
956 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000957 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
958 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
959
960
Abramo Bagnara7945c982012-01-27 09:46:47 +0000961 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
Douglas Gregor6e068012011-02-28 00:04:36 +0000962 CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000963 return false;
Douglas Gregor7f741122009-02-25 19:37:18 +0000964}
965
Douglas Gregor869ad452011-02-24 17:54:50 +0000966namespace {
967 /// \brief A structure that stores a nested-name-specifier annotation,
968 /// including both the nested-name-specifier
969 struct NestedNameSpecifierAnnotation {
970 NestedNameSpecifier *NNS;
971 };
972}
973
974void *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) {
975 if (SS.isEmpty() || SS.isInvalid())
Craig Topperc3ec1492014-05-26 06:22:03 +0000976 return nullptr;
977
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000978 void *Mem = Context.Allocate(
979 (sizeof(NestedNameSpecifierAnnotation) + SS.location_size()),
980 alignof(NestedNameSpecifierAnnotation));
Douglas Gregor869ad452011-02-24 17:54:50 +0000981 NestedNameSpecifierAnnotation *Annotation
982 = new (Mem) NestedNameSpecifierAnnotation;
983 Annotation->NNS = SS.getScopeRep();
984 memcpy(Annotation + 1, SS.location_data(), SS.location_size());
985 return Annotation;
986}
987
988void Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr,
989 SourceRange AnnotationRange,
990 CXXScopeSpec &SS) {
991 if (!AnnotationPtr) {
992 SS.SetInvalid(AnnotationRange);
993 return;
994 }
995
996 NestedNameSpecifierAnnotation *Annotation
997 = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr);
998 SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1));
999}
1000
John McCall2b058ef2009-12-11 20:04:54 +00001001bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
1002 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
1003
Alex Lorenze151f0102016-12-07 10:24:44 +00001004 // Don't enter a declarator context when the current context is an Objective-C
1005 // declaration.
1006 if (isa<ObjCContainerDecl>(CurContext) || isa<ObjCMethodDecl>(CurContext))
1007 return false;
1008
Richard Smith74bb2d22013-03-26 00:54:11 +00001009 NestedNameSpecifier *Qualifier = SS.getScopeRep();
John McCall2b058ef2009-12-11 20:04:54 +00001010
1011 // There are only two places a well-formed program may qualify a
1012 // declarator: first, when defining a namespace or class member
1013 // out-of-line, and second, when naming an explicitly-qualified
1014 // friend function. The latter case is governed by
1015 // C++03 [basic.lookup.unqual]p10:
1016 // In a friend declaration naming a member function, a name used
1017 // in the function declarator and not part of a template-argument
1018 // in a template-id is first looked up in the scope of the member
1019 // function's class. If it is not found, or if the name is part of
1020 // a template-argument in a template-id, the look up is as
1021 // described for unqualified names in the definition of the class
1022 // granting friendship.
1023 // i.e. we don't push a scope unless it's a class member.
1024
1025 switch (Qualifier->getKind()) {
1026 case NestedNameSpecifier::Global:
1027 case NestedNameSpecifier::Namespace:
Douglas Gregor7b26ff92011-02-24 02:36:08 +00001028 case NestedNameSpecifier::NamespaceAlias:
John McCall2b058ef2009-12-11 20:04:54 +00001029 // These are always namespace scopes. We never want to enter a
1030 // namespace scope from anything but a file context.
Sebastian Redl50c68252010-08-31 00:36:30 +00001031 return CurContext->getRedeclContext()->isFileContext();
John McCall2b058ef2009-12-11 20:04:54 +00001032
1033 case NestedNameSpecifier::Identifier:
1034 case NestedNameSpecifier::TypeSpec:
1035 case NestedNameSpecifier::TypeSpecWithTemplate:
Nikola Smiljanic67860242014-09-26 00:28:20 +00001036 case NestedNameSpecifier::Super:
John McCall2b058ef2009-12-11 20:04:54 +00001037 // These are never namespace scopes.
1038 return true;
1039 }
1040
David Blaikie8a40f702012-01-17 06:56:22 +00001041 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
John McCall2b058ef2009-12-11 20:04:54 +00001042}
1043
Cedric Venet084381332009-02-14 20:20:19 +00001044/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
1045/// scope or nested-name-specifier) is parsed, part of a declarator-id.
1046/// After this method is called, according to [C++ 3.4.3p3], names should be
1047/// looked up in the declarator-id's scope, until the declarator is parsed and
1048/// ActOnCXXExitDeclaratorScope is called.
1049/// The 'SS' should be a non-empty valid CXXScopeSpec.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00001050bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) {
Cedric Venet084381332009-02-14 20:20:19 +00001051 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
John McCall6df5fef2009-12-19 10:49:29 +00001052
1053 if (SS.isInvalid()) return true;
1054
1055 DeclContext *DC = computeDeclContext(SS, true);
1056 if (!DC) return true;
1057
1058 // Before we enter a declarator's context, we need to make sure that
1059 // it is a complete declaration context.
John McCall0b66eb32010-05-01 00:40:08 +00001060 if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC))
John McCall6df5fef2009-12-19 10:49:29 +00001061 return true;
1062
1063 EnterDeclaratorContext(S, DC);
John McCall2408e322010-04-27 00:57:59 +00001064
1065 // Rebuild the nested name specifier for the new scope.
1066 if (DC->isDependentContext())
1067 RebuildNestedNameSpecifierInCurrentInstantiation(SS);
1068
Douglas Gregor5013a7e2009-09-24 23:39:01 +00001069 return false;
Cedric Venet084381332009-02-14 20:20:19 +00001070}
1071
1072/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
1073/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
1074/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
1075/// Used to indicate that names should revert to being looked up in the
1076/// defining scope.
1077void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
1078 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
Douglas Gregor053f6912009-08-26 00:04:55 +00001079 if (SS.isInvalid())
1080 return;
John McCall6df5fef2009-12-19 10:49:29 +00001081 assert(!SS.isInvalid() && computeDeclContext(SS, true) &&
1082 "exiting declarator scope we never really entered");
1083 ExitDeclaratorContext(S);
Cedric Venet084381332009-02-14 20:20:19 +00001084}