blob: f2fad825c3e78fca936e25196c8b49f41270843e [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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000027/// 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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000047/// Compute the DeclContext that is associated with the given type.
Douglas Gregorb7bfe792009-09-02 22:59:36 +000048///
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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000062/// Compute the DeclContext that is associated with the given
Douglas Gregor52537682009-03-19 00:18:19 +000063/// 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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000175/// If the given nested name specifier refers to the current
Douglas Gregorc9f9b862009-05-11 19:58:34 +0000176/// 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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000191/// Require that the context specified by SS be complete.
Douglas Gregor26897462009-03-11 16:48:53 +0000192///
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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000308/// Determines whether the given declaration is an valid acceptable
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000309/// 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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000353/// 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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000446/// Build a new nested-name-specifier for "identifier::", as described
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000447/// 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) {
Alex Lorenz1be800c52017-04-19 08:58:56 +0000483 if (IdInfo.Identifier->isEditorPlaceholder())
484 return true;
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000485 LookupResult Found(*this, IdInfo.Identifier, IdInfo.IdentifierLoc,
Matthias Gehredc01bb42017-03-17 21:41:20 +0000486 OnlyNamespace ? LookupNamespaceName
487 : LookupNestedNameSpecifierName);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000488 QualType ObjectType = GetTypeFromParser(IdInfo.ObjectType);
John McCall27b18f82009-11-17 02:14:36 +0000489
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000490 // Determine where to perform name lookup
Craig Topperc3ec1492014-05-26 06:22:03 +0000491 DeclContext *LookupCtx = nullptr;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000492 bool isDependent = false;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000493 if (IsCorrectedToColon)
494 *IsCorrectedToColon = false;
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000495 if (!ObjectType.isNull()) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000496 // This nested-name-specifier occurs in a member access expression, e.g.,
497 // x->B::f, and we are looking into the type of the object.
498 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000499 LookupCtx = computeDeclContext(ObjectType);
500 isDependent = ObjectType->isDependentType();
501 } else if (SS.isSet()) {
502 // This nested-name-specifier occurs after another nested-name-specifier,
Richard Smith3f1b5d02011-05-05 21:57:07 +0000503 // so look into the context associated with the prior nested-name-specifier.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000504 LookupCtx = computeDeclContext(SS, EnteringContext);
505 isDependent = isDependentScopeSpecifier(SS);
John McCall27b18f82009-11-17 02:14:36 +0000506 Found.setContextRange(SS.getRange());
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000507 }
Mike Stump11289f42009-09-09 15:08:12 +0000508
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000509 bool ObjectTypeSearchedInScope = false;
510 if (LookupCtx) {
Mike Stump11289f42009-09-09 15:08:12 +0000511 // Perform "qualified" name lookup into the declaration context we
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000512 // computed, which is either the type of the base of a member access
Mike Stump11289f42009-09-09 15:08:12 +0000513 // expression or the declaration context associated with a prior
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000514 // nested-name-specifier.
Mike Stump11289f42009-09-09 15:08:12 +0000515
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000516 // The declaration context must be complete.
John McCall0b66eb32010-05-01 00:40:08 +0000517 if (!LookupCtx->isDependentContext() &&
518 RequireCompleteDeclContext(SS, LookupCtx))
Douglas Gregor90c99722011-02-24 00:17:56 +0000519 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000520
John McCall27b18f82009-11-17 02:14:36 +0000521 LookupQualifiedName(Found, LookupCtx);
Mike Stump11289f42009-09-09 15:08:12 +0000522
John McCall27b18f82009-11-17 02:14:36 +0000523 if (!ObjectType.isNull() && Found.empty()) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000524 // C++ [basic.lookup.classref]p4:
525 // If the id-expression in a class member access is a qualified-id of
Mike Stump11289f42009-09-09 15:08:12 +0000526 // the form
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000527 //
528 // class-name-or-namespace-name::...
529 //
Mike Stump11289f42009-09-09 15:08:12 +0000530 // the class-name-or-namespace-name following the . or -> operator is
531 // looked up both in the context of the entire postfix-expression and in
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000532 // the scope of the class of the object expression. If the name is found
Mike Stump11289f42009-09-09 15:08:12 +0000533 // only in the scope of the class of the object expression, the name
534 // shall refer to a class-name. If the name is found only in the
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000535 // context of the entire postfix-expression, the name shall refer to a
536 // class-name or namespace-name. [...]
537 //
538 // Qualified name lookup into a class will not find a namespace-name,
Douglas Gregor9d07dfa2011-05-15 17:27:27 +0000539 // so we do not need to diagnose that case specifically. However,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000540 // this qualified name lookup may find nothing. In that case, perform
Mike Stump11289f42009-09-09 15:08:12 +0000541 // unqualified name lookup in the given scope (if available) or
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000542 // reconstruct the result from when name lookup was performed at template
543 // definition time.
544 if (S)
John McCall27b18f82009-11-17 02:14:36 +0000545 LookupName(Found, S);
John McCall9f3059a2009-10-09 21:13:30 +0000546 else if (ScopeLookupResult)
547 Found.addDecl(ScopeLookupResult);
Mike Stump11289f42009-09-09 15:08:12 +0000548
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000549 ObjectTypeSearchedInScope = true;
550 }
Douglas Gregordf65c8ed2010-07-28 14:49:07 +0000551 } else if (!isDependent) {
552 // Perform unqualified name lookup in the current scope.
553 LookupName(Found, S);
554 }
555
Richard Smith4b213d72015-11-12 22:40:09 +0000556 if (Found.isAmbiguous())
557 return true;
558
Douglas Gregordf65c8ed2010-07-28 14:49:07 +0000559 // If we performed lookup into a dependent context and did not find anything,
560 // that's fine: just build a dependent nested-name-specifier.
561 if (Found.empty() && isDependent &&
562 !(LookupCtx && LookupCtx->isRecord() &&
563 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
564 !cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()))) {
Chris Lattner1c428032009-12-07 01:36:53 +0000565 // Don't speculate if we're just trying to improve error recovery.
566 if (ErrorRecoveryLookup)
Douglas Gregor90c99722011-02-24 00:17:56 +0000567 return true;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000568
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000569 // We were not able to compute the declaration context for a dependent
Mike Stump11289f42009-09-09 15:08:12 +0000570 // base object type or prior nested-name-specifier, so this
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000571 // nested-name-specifier refers to an unknown specialization. Just build
572 // a dependent nested-name-specifier.
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000573 SS.Extend(Context, IdInfo.Identifier, IdInfo.IdentifierLoc, IdInfo.CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000574 return false;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000575 }
576
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000577 if (Found.empty() && !ErrorRecoveryLookup) {
578 // If identifier is not found as class-name-or-namespace-name, but is found
579 // as other entity, don't look for typos.
580 LookupResult R(*this, Found.getLookupNameInfo(), LookupOrdinaryName);
581 if (LookupCtx)
582 LookupQualifiedName(R, LookupCtx);
583 else if (S && !isDependent)
584 LookupName(R, S);
585 if (!R.empty()) {
Richard Smith4b213d72015-11-12 22:40:09 +0000586 // Don't diagnose problems with this speculative lookup.
587 R.suppressDiagnostics();
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000588 // The identifier is found in ordinary lookup. If correction to colon is
589 // allowed, suggest replacement to ':'.
590 if (IsCorrectedToColon) {
591 *IsCorrectedToColon = true;
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000592 Diag(IdInfo.CCLoc, diag::err_nested_name_spec_is_not_class)
593 << IdInfo.Identifier << getLangOpts().CPlusPlus
594 << FixItHint::CreateReplacement(IdInfo.CCLoc, ":");
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000595 if (NamedDecl *ND = R.getAsSingle<NamedDecl>())
596 Diag(ND->getLocation(), diag::note_declared_at);
597 return true;
598 }
599 // Replacement '::' -> ':' is not allowed, just issue respective error.
Matthias Gehredc01bb42017-03-17 21:41:20 +0000600 Diag(R.getNameLoc(), OnlyNamespace
Davide Italiano3c6e5ea2017-03-17 22:19:20 +0000601 ? unsigned(diag::err_expected_namespace_name)
602 : unsigned(diag::err_expected_class_or_namespace))
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000603 << IdInfo.Identifier << getLangOpts().CPlusPlus;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000604 if (NamedDecl *ND = R.getAsSingle<NamedDecl>())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000605 Diag(ND->getLocation(), diag::note_entity_declared_at)
606 << IdInfo.Identifier;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000607 return true;
608 }
609 }
610
Alp Tokerbfa39342014-01-14 12:51:41 +0000611 if (Found.empty() && !ErrorRecoveryLookup && !getLangOpts().MSVCCompat) {
Douglas Gregor532e68f2009-12-31 08:26:35 +0000612 // We haven't found anything, and we're not recovering from a
613 // different kind of error, so look for typos.
614 DeclarationName Name = Found.getLookupName();
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000615 Found.clear();
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000616 if (TypoCorrection Corrected = CorrectTypo(
617 Found.getLookupNameInfo(), Found.getLookupKind(), S, &SS,
618 llvm::make_unique<NestedNameSpecifierValidatorCCC>(*this),
619 CTK_ErrorRecovery, LookupCtx, EnteringContext)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000620 if (LookupCtx) {
621 bool DroppedSpecifier =
622 Corrected.WillReplaceSpecifier() &&
623 Name.getAsString() == Corrected.getAsString(getLangOpts());
Kaelyn Uhraina6c78fe2013-12-16 19:19:18 +0000624 if (DroppedSpecifier)
625 SS.clear();
Richard Smithf9b15102013-08-17 00:46:16 +0000626 diagnoseTypo(Corrected, PDiag(diag::err_no_member_suggest)
627 << Name << LookupCtx << DroppedSpecifier
628 << SS.getRange());
629 } else
630 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
631 << Name);
Kaelyn Uhrain10413a42013-07-02 23:47:44 +0000632
Reid Kleckner4ce625c2016-02-16 19:16:20 +0000633 if (Corrected.getCorrectionSpecifier())
634 SS.MakeTrivial(Context, Corrected.getCorrectionSpecifier(),
635 SourceRange(Found.getNameLoc()));
636
Richard Smithde6d6c42015-12-29 19:43:10 +0000637 if (NamedDecl *ND = Corrected.getFoundDecl())
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000638 Found.addDecl(ND);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000639 Found.setLookupName(Corrected.getCorrection());
Douglas Gregorc048c522010-06-29 19:27:42 +0000640 } else {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000641 Found.setLookupName(IdInfo.Identifier);
Douglas Gregorc048c522010-06-29 19:27:42 +0000642 }
Douglas Gregor532e68f2009-12-31 08:26:35 +0000643 }
644
Richard Smithf2005d32015-12-29 23:34:32 +0000645 NamedDecl *SD =
646 Found.isSingleResult() ? Found.getRepresentativeDecl() : nullptr;
Serge Pavlov25a8afa2015-01-18 20:04:35 +0000647 bool IsExtension = false;
648 bool AcceptSpec = isAcceptableNestedNameSpecifier(SD, &IsExtension);
649 if (!AcceptSpec && IsExtension) {
650 AcceptSpec = true;
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000651 Diag(IdInfo.IdentifierLoc, diag::ext_nested_name_spec_is_enum);
Serge Pavlov25a8afa2015-01-18 20:04:35 +0000652 }
653 if (AcceptSpec) {
Douglas Gregor1b02e4a2012-05-01 20:23:02 +0000654 if (!ObjectType.isNull() && !ObjectTypeSearchedInScope &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000655 !getLangOpts().CPlusPlus11) {
Douglas Gregor1b02e4a2012-05-01 20:23:02 +0000656 // C++03 [basic.lookup.classref]p4:
Mike Stump11289f42009-09-09 15:08:12 +0000657 // [...] If the name is found in both contexts, the
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000658 // class-name-or-namespace-name shall refer to the same entity.
659 //
660 // We already found the name in the scope of the object. Now, look
661 // into the current scope (the scope of the postfix-expression) to
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000662 // see if we can find the same name there. As above, if there is no
663 // scope, reconstruct the result from the template instantiation itself.
Douglas Gregor1b02e4a2012-05-01 20:23:02 +0000664 //
665 // Note that C++11 does *not* perform this redundant lookup.
John McCall9f3059a2009-10-09 21:13:30 +0000666 NamedDecl *OuterDecl;
667 if (S) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000668 LookupResult FoundOuter(*this, IdInfo.Identifier, IdInfo.IdentifierLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +0000669 LookupNestedNameSpecifierName);
John McCall27b18f82009-11-17 02:14:36 +0000670 LookupName(FoundOuter, S);
John McCall67c00872009-12-02 08:25:40 +0000671 OuterDecl = FoundOuter.getAsSingle<NamedDecl>();
John McCall9f3059a2009-10-09 21:13:30 +0000672 } else
673 OuterDecl = ScopeLookupResult;
Mike Stump11289f42009-09-09 15:08:12 +0000674
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000675 if (isAcceptableNestedNameSpecifier(OuterDecl) &&
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000676 OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() &&
677 (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) ||
678 !Context.hasSameType(
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000679 Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)),
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000680 Context.getTypeDeclType(cast<TypeDecl>(SD))))) {
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000681 if (ErrorRecoveryLookup)
682 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000683
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000684 Diag(IdInfo.IdentifierLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +0000685 diag::err_nested_name_member_ref_lookup_ambiguous)
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000686 << IdInfo.Identifier;
Douglas Gregor90c99722011-02-24 00:17:56 +0000687 Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type)
688 << ObjectType;
689 Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope);
690
691 // Fall through so that we'll pick the name we found in the object
692 // type, since that's probably what the user wanted anyway.
693 }
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000694 }
Mike Stump11289f42009-09-09 15:08:12 +0000695
Nico Weber72889432014-09-06 01:25:55 +0000696 if (auto *TD = dyn_cast_or_null<TypedefNameDecl>(SD))
697 MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false);
698
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000699 // If we're just performing this lookup for error-recovery purposes,
Douglas Gregor90c99722011-02-24 00:17:56 +0000700 // don't extend the nested-name-specifier. Just return now.
701 if (ErrorRecoveryLookup)
702 return false;
Aaron Ballman43f40102014-11-14 22:34:56 +0000703
704 // The use of a nested name specifier may trigger deprecation warnings.
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000705 DiagnoseUseOfDecl(SD, IdInfo.CCLoc);
Aaron Ballman43f40102014-11-14 22:34:56 +0000706
Douglas Gregor90c99722011-02-24 00:17:56 +0000707 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000708 SS.Extend(Context, Namespace, IdInfo.IdentifierLoc, IdInfo.CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000709 return false;
710 }
Mike Stump11289f42009-09-09 15:08:12 +0000711
Douglas Gregor90c99722011-02-24 00:17:56 +0000712 if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000713 SS.Extend(Context, Alias, IdInfo.IdentifierLoc, IdInfo.CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000714 return false;
715 }
Mike Stump11289f42009-09-09 15:08:12 +0000716
Richard Smithf2005d32015-12-29 23:34:32 +0000717 QualType T =
718 Context.getTypeDeclType(cast<TypeDecl>(SD->getUnderlyingDecl()));
Douglas Gregor90c99722011-02-24 00:17:56 +0000719 TypeLocBuilder TLB;
720 if (isa<InjectedClassNameType>(T)) {
721 InjectedClassNameTypeLoc InjectedTL
722 = TLB.push<InjectedClassNameTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000723 InjectedTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000724 } else if (isa<RecordType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000725 RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000726 RecordTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000727 } else if (isa<TypedefType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000728 TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000729 TypedefTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000730 } else if (isa<EnumType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000731 EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000732 EnumTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000733 } else if (isa<TemplateTypeParmType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000734 TemplateTypeParmTypeLoc TemplateTypeTL
735 = TLB.push<TemplateTypeParmTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000736 TemplateTypeTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000737 } else if (isa<UnresolvedUsingType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000738 UnresolvedUsingTypeLoc UnresolvedTL
739 = TLB.push<UnresolvedUsingTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000740 UnresolvedTL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000741 } else if (isa<SubstTemplateTypeParmType>(T)) {
742 SubstTemplateTypeParmTypeLoc TL
743 = TLB.push<SubstTemplateTypeParmTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000744 TL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000745 } else if (isa<SubstTemplateTypeParmPackType>(T)) {
746 SubstTemplateTypeParmPackTypeLoc TL
747 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(T);
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000748 TL.setNameLoc(IdInfo.IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000749 } else {
750 llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier");
Douglas Gregor90c99722011-02-24 00:17:56 +0000751 }
752
Richard Smith91c7bbd2011-10-20 03:28:47 +0000753 if (T->isEnumeralType())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000754 Diag(IdInfo.IdentifierLoc, diag::warn_cxx98_compat_enum_nested_name_spec);
Richard Smith91c7bbd2011-10-20 03:28:47 +0000755
Douglas Gregor90c99722011-02-24 00:17:56 +0000756 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000757 IdInfo.CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000758 return false;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000759 }
Mike Stump11289f42009-09-09 15:08:12 +0000760
Chris Lattner1c428032009-12-07 01:36:53 +0000761 // Otherwise, we have an error case. If we don't want diagnostics, just
762 // return an error now.
763 if (ErrorRecoveryLookup)
Douglas Gregor90c99722011-02-24 00:17:56 +0000764 return true;
Chris Lattner1c428032009-12-07 01:36:53 +0000765
Cedric Venet084381332009-02-14 20:20:19 +0000766 // If we didn't find anything during our lookup, try again with
767 // ordinary name lookup, which can help us produce better error
768 // messages.
John McCall67c00872009-12-02 08:25:40 +0000769 if (Found.empty()) {
John McCall27b18f82009-11-17 02:14:36 +0000770 Found.clear(LookupOrdinaryName);
771 LookupName(Found, S);
John McCall9f3059a2009-10-09 21:13:30 +0000772 }
Mike Stump11289f42009-09-09 15:08:12 +0000773
Francois Pichetb23dc092011-07-27 01:05:24 +0000774 // In Microsoft mode, if we are within a templated function and we can't
775 // resolve Identifier, then extend the SS with Identifier. This will have
776 // the effect of resolving Identifier during template instantiation.
777 // The goal is to be able to resolve a function call whose
778 // nested-name-specifier is located inside a dependent base class.
779 // Example:
780 //
781 // class C {
782 // public:
783 // static void foo2() { }
784 // };
785 // template <class T> class A { public: typedef C D; };
786 //
787 // template <class T> class B : public A<T> {
788 // public:
789 // void foo() { D::foo2(); }
790 // };
Alp Tokerbfa39342014-01-14 12:51:41 +0000791 if (getLangOpts().MSVCCompat) {
Francois Pichetb23dc092011-07-27 01:05:24 +0000792 DeclContext *DC = LookupCtx ? LookupCtx : CurContext;
793 if (DC->isDependentContext() && DC->isFunctionOrMethod()) {
Reid Kleckner062be332014-08-14 23:34:52 +0000794 CXXRecordDecl *ContainingClass = dyn_cast<CXXRecordDecl>(DC->getParent());
795 if (ContainingClass && ContainingClass->hasAnyDependentBases()) {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000796 Diag(IdInfo.IdentifierLoc,
797 diag::ext_undeclared_unqual_id_with_dependent_base)
798 << IdInfo.Identifier << ContainingClass;
799 SS.Extend(Context, IdInfo.Identifier, IdInfo.IdentifierLoc,
800 IdInfo.CCLoc);
Reid Kleckner062be332014-08-14 23:34:52 +0000801 return false;
802 }
Francois Pichetb23dc092011-07-27 01:05:24 +0000803 }
804 }
805
David Blaikie6cab5962014-02-09 06:54:23 +0000806 if (!Found.empty()) {
807 if (TypeDecl *TD = Found.getAsSingle<TypeDecl>())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000808 Diag(IdInfo.IdentifierLoc, diag::err_expected_class_or_namespace)
Richard Trieu42b98e22016-10-28 00:15:24 +0000809 << Context.getTypeDeclType(TD) << getLangOpts().CPlusPlus;
David Blaikie6cab5962014-02-09 06:54:23 +0000810 else {
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000811 Diag(IdInfo.IdentifierLoc, diag::err_expected_class_or_namespace)
812 << IdInfo.Identifier << getLangOpts().CPlusPlus;
David Blaikie6cab5962014-02-09 06:54:23 +0000813 if (NamedDecl *ND = Found.getAsSingle<NamedDecl>())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000814 Diag(ND->getLocation(), diag::note_entity_declared_at)
815 << IdInfo.Identifier;
David Blaikie6cab5962014-02-09 06:54:23 +0000816 }
817 } else if (SS.isSet())
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000818 Diag(IdInfo.IdentifierLoc, diag::err_no_member) << IdInfo.Identifier
819 << LookupCtx << SS.getRange();
Cedric Venet084381332009-02-14 20:20:19 +0000820 else
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000821 Diag(IdInfo.IdentifierLoc, diag::err_undeclared_var_use)
822 << IdInfo.Identifier;
Mike Stump11289f42009-09-09 15:08:12 +0000823
Douglas Gregor90c99722011-02-24 00:17:56 +0000824 return true;
Cedric Venet084381332009-02-14 20:20:19 +0000825}
826
Matthias Gehredc01bb42017-03-17 21:41:20 +0000827bool Sema::ActOnCXXNestedNameSpecifier(Scope *S, NestedNameSpecInfo &IdInfo,
828 bool EnteringContext, CXXScopeSpec &SS,
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000829 bool ErrorRecoveryLookup,
Matthias Gehredc01bb42017-03-17 21:41:20 +0000830 bool *IsCorrectedToColon,
831 bool OnlyNamespace) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000832 if (SS.isInvalid())
833 return true;
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000834
Matthias Gehredc01bb42017-03-17 21:41:20 +0000835 return BuildCXXNestedNameSpecifier(S, IdInfo, EnteringContext, SS,
Craig Topperc3ec1492014-05-26 06:22:03 +0000836 /*ScopeLookupResult=*/nullptr, false,
Matthias Gehredc01bb42017-03-17 21:41:20 +0000837 IsCorrectedToColon, OnlyNamespace);
Chris Lattner1c428032009-12-07 01:36:53 +0000838}
839
David Blaikie15a430a2011-12-04 05:04:18 +0000840bool Sema::ActOnCXXNestedNameSpecifierDecltype(CXXScopeSpec &SS,
841 const DeclSpec &DS,
842 SourceLocation ColonColonLoc) {
843 if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error)
844 return true;
845
846 assert(DS.getTypeSpecType() == DeclSpec::TST_decltype);
847
848 QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc());
Richard Trieu02826552018-07-07 00:17:25 +0000849 if (T.isNull())
850 return true;
851
David Blaikie15a430a2011-12-04 05:04:18 +0000852 if (!T->isDependentType() && !T->getAs<TagType>()) {
David Blaikie6cab5962014-02-09 06:54:23 +0000853 Diag(DS.getTypeSpecTypeLoc(), diag::err_expected_class_or_namespace)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000854 << T << getLangOpts().CPlusPlus;
David Blaikie15a430a2011-12-04 05:04:18 +0000855 return true;
856 }
857
858 TypeLocBuilder TLB;
859 DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T);
860 DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc());
861 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
862 ColonColonLoc);
863 return false;
864}
865
Chris Lattner1c428032009-12-07 01:36:53 +0000866/// IsInvalidUnlessNestedName - This method is used for error recovery
867/// purposes to determine whether the specified identifier is only valid as
868/// a nested name specifier, for example a namespace name. It is
869/// conservatively correct to always return false from this method.
870///
871/// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +0000872bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS,
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000873 NestedNameSpecInfo &IdInfo,
Chris Lattner1c428032009-12-07 01:36:53 +0000874 bool EnteringContext) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000875 if (SS.isInvalid())
876 return false;
Craig Topperc3ec1492014-05-26 06:22:03 +0000877
Serge Pavlovd931b9f2016-08-08 04:02:15 +0000878 return !BuildCXXNestedNameSpecifier(S, IdInfo, EnteringContext, SS,
Craig Topperc3ec1492014-05-26 06:22:03 +0000879 /*ScopeLookupResult=*/nullptr, true);
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000880}
881
Douglas Gregor90c99722011-02-24 00:17:56 +0000882bool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000883 CXXScopeSpec &SS,
884 SourceLocation TemplateKWLoc,
Douglas Gregor6e068012011-02-28 00:04:36 +0000885 TemplateTy Template,
886 SourceLocation TemplateNameLoc,
887 SourceLocation LAngleLoc,
888 ASTTemplateArgsPtr TemplateArgsIn,
889 SourceLocation RAngleLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +0000890 SourceLocation CCLoc,
Douglas Gregor6e068012011-02-28 00:04:36 +0000891 bool EnteringContext) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000892 if (SS.isInvalid())
893 return true;
894
Douglas Gregor6e068012011-02-28 00:04:36 +0000895 // Translate the parser's template argument list in our AST format.
896 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
897 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
898
Richard Smith72bfbd82013-12-04 00:28:23 +0000899 DependentTemplateName *DTN = Template.get().getAsDependentTemplateName();
900 if (DTN && DTN->isIdentifier()) {
Douglas Gregor6e068012011-02-28 00:04:36 +0000901 // Handle a dependent template specialization for which we cannot resolve
902 // the template name.
Richard Smith74bb2d22013-03-26 00:54:11 +0000903 assert(DTN->getQualifier() == SS.getScopeRep());
Douglas Gregor6e068012011-02-28 00:04:36 +0000904 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
Douglas Gregora7a795b2011-03-01 20:11:18 +0000905 DTN->getQualifier(),
906 DTN->getIdentifier(),
Douglas Gregor6e068012011-02-28 00:04:36 +0000907 TemplateArgs);
908
909 // Create source-location information for this type.
910 TypeLocBuilder Builder;
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000911 DependentTemplateSpecializationTypeLoc SpecTL
Douglas Gregor6e068012011-02-28 00:04:36 +0000912 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000913 SpecTL.setElaboratedKeywordLoc(SourceLocation());
914 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000915 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000916 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000917 SpecTL.setLAngleLoc(LAngleLoc);
918 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000919 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
920 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
921
Abramo Bagnara7945c982012-01-27 09:46:47 +0000922 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
Douglas Gregor6e068012011-02-28 00:04:36 +0000923 CCLoc);
924 return false;
925 }
Richard Smith72bfbd82013-12-04 00:28:23 +0000926
Richard Smithf95fe9b2013-12-04 00:47:45 +0000927 TemplateDecl *TD = Template.get().getAsTemplateDecl();
Richard Smith72bfbd82013-12-04 00:28:23 +0000928 if (Template.get().getAsOverloadedTemplate() || DTN ||
Richard Smithf95fe9b2013-12-04 00:47:45 +0000929 isa<FunctionTemplateDecl>(TD) || isa<VarTemplateDecl>(TD)) {
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000930 SourceRange R(TemplateNameLoc, RAngleLoc);
931 if (SS.getRange().isValid())
932 R.setBegin(SS.getRange().getBegin());
Richard Smith72bfbd82013-12-04 00:28:23 +0000933
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000934 Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier)
Richard Smithf95fe9b2013-12-04 00:47:45 +0000935 << (TD && isa<VarTemplateDecl>(TD)) << Template.get() << R;
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000936 NoteAllFoundTemplates(Template.get());
937 return true;
938 }
Richard Smith72bfbd82013-12-04 00:28:23 +0000939
Douglas Gregor6e068012011-02-28 00:04:36 +0000940 // We were able to resolve the template name to an actual template.
941 // Build an appropriate nested-name-specifier.
Richard Smith74f02342017-01-19 21:00:13 +0000942 QualType T =
943 CheckTemplateIdType(Template.get(), TemplateNameLoc, TemplateArgs);
Douglas Gregor90c99722011-02-24 00:17:56 +0000944 if (T.isNull())
945 return true;
946
Richard Smith3f1b5d02011-05-05 21:57:07 +0000947 // Alias template specializations can produce types which are not valid
948 // nested name specifiers.
949 if (!T->isDependentType() && !T->getAs<TagType>()) {
950 Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T;
951 NoteAllFoundTemplates(Template.get());
952 return true;
953 }
Douglas Gregor6e068012011-02-28 00:04:36 +0000954
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000955 // Provide source-location information for the template specialization type.
Douglas Gregor6e068012011-02-28 00:04:36 +0000956 TypeLocBuilder Builder;
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000957 TemplateSpecializationTypeLoc SpecTL
Douglas Gregor6e068012011-02-28 00:04:36 +0000958 = Builder.push<TemplateSpecializationTypeLoc>(T);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000959 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
960 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000961 SpecTL.setLAngleLoc(LAngleLoc);
962 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000963 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
964 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
965
966
Abramo Bagnara7945c982012-01-27 09:46:47 +0000967 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
Douglas Gregor6e068012011-02-28 00:04:36 +0000968 CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000969 return false;
Douglas Gregor7f741122009-02-25 19:37:18 +0000970}
971
Douglas Gregor869ad452011-02-24 17:54:50 +0000972namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000973 /// A structure that stores a nested-name-specifier annotation,
Douglas Gregor869ad452011-02-24 17:54:50 +0000974 /// including both the nested-name-specifier
975 struct NestedNameSpecifierAnnotation {
976 NestedNameSpecifier *NNS;
977 };
978}
979
980void *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) {
981 if (SS.isEmpty() || SS.isInvalid())
Craig Topperc3ec1492014-05-26 06:22:03 +0000982 return nullptr;
983
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000984 void *Mem = Context.Allocate(
985 (sizeof(NestedNameSpecifierAnnotation) + SS.location_size()),
986 alignof(NestedNameSpecifierAnnotation));
Douglas Gregor869ad452011-02-24 17:54:50 +0000987 NestedNameSpecifierAnnotation *Annotation
988 = new (Mem) NestedNameSpecifierAnnotation;
989 Annotation->NNS = SS.getScopeRep();
990 memcpy(Annotation + 1, SS.location_data(), SS.location_size());
991 return Annotation;
992}
993
994void Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr,
995 SourceRange AnnotationRange,
996 CXXScopeSpec &SS) {
997 if (!AnnotationPtr) {
998 SS.SetInvalid(AnnotationRange);
999 return;
1000 }
1001
1002 NestedNameSpecifierAnnotation *Annotation
1003 = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr);
1004 SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1));
1005}
1006
John McCall2b058ef2009-12-11 20:04:54 +00001007bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
1008 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
1009
Alex Lorenze151f0102016-12-07 10:24:44 +00001010 // Don't enter a declarator context when the current context is an Objective-C
1011 // declaration.
1012 if (isa<ObjCContainerDecl>(CurContext) || isa<ObjCMethodDecl>(CurContext))
1013 return false;
1014
Richard Smith74bb2d22013-03-26 00:54:11 +00001015 NestedNameSpecifier *Qualifier = SS.getScopeRep();
John McCall2b058ef2009-12-11 20:04:54 +00001016
1017 // There are only two places a well-formed program may qualify a
1018 // declarator: first, when defining a namespace or class member
1019 // out-of-line, and second, when naming an explicitly-qualified
1020 // friend function. The latter case is governed by
1021 // C++03 [basic.lookup.unqual]p10:
1022 // In a friend declaration naming a member function, a name used
1023 // in the function declarator and not part of a template-argument
1024 // in a template-id is first looked up in the scope of the member
1025 // function's class. If it is not found, or if the name is part of
1026 // a template-argument in a template-id, the look up is as
1027 // described for unqualified names in the definition of the class
1028 // granting friendship.
1029 // i.e. we don't push a scope unless it's a class member.
1030
1031 switch (Qualifier->getKind()) {
1032 case NestedNameSpecifier::Global:
1033 case NestedNameSpecifier::Namespace:
Douglas Gregor7b26ff92011-02-24 02:36:08 +00001034 case NestedNameSpecifier::NamespaceAlias:
John McCall2b058ef2009-12-11 20:04:54 +00001035 // These are always namespace scopes. We never want to enter a
1036 // namespace scope from anything but a file context.
Sebastian Redl50c68252010-08-31 00:36:30 +00001037 return CurContext->getRedeclContext()->isFileContext();
John McCall2b058ef2009-12-11 20:04:54 +00001038
1039 case NestedNameSpecifier::Identifier:
1040 case NestedNameSpecifier::TypeSpec:
1041 case NestedNameSpecifier::TypeSpecWithTemplate:
Nikola Smiljanic67860242014-09-26 00:28:20 +00001042 case NestedNameSpecifier::Super:
John McCall2b058ef2009-12-11 20:04:54 +00001043 // These are never namespace scopes.
1044 return true;
1045 }
1046
David Blaikie8a40f702012-01-17 06:56:22 +00001047 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
John McCall2b058ef2009-12-11 20:04:54 +00001048}
1049
Cedric Venet084381332009-02-14 20:20:19 +00001050/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
1051/// scope or nested-name-specifier) is parsed, part of a declarator-id.
1052/// After this method is called, according to [C++ 3.4.3p3], names should be
1053/// looked up in the declarator-id's scope, until the declarator is parsed and
1054/// ActOnCXXExitDeclaratorScope is called.
1055/// The 'SS' should be a non-empty valid CXXScopeSpec.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00001056bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) {
Cedric Venet084381332009-02-14 20:20:19 +00001057 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
John McCall6df5fef2009-12-19 10:49:29 +00001058
1059 if (SS.isInvalid()) return true;
1060
1061 DeclContext *DC = computeDeclContext(SS, true);
1062 if (!DC) return true;
1063
1064 // Before we enter a declarator's context, we need to make sure that
1065 // it is a complete declaration context.
John McCall0b66eb32010-05-01 00:40:08 +00001066 if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC))
John McCall6df5fef2009-12-19 10:49:29 +00001067 return true;
1068
1069 EnterDeclaratorContext(S, DC);
John McCall2408e322010-04-27 00:57:59 +00001070
1071 // Rebuild the nested name specifier for the new scope.
1072 if (DC->isDependentContext())
1073 RebuildNestedNameSpecifierInCurrentInstantiation(SS);
1074
Douglas Gregor5013a7e2009-09-24 23:39:01 +00001075 return false;
Cedric Venet084381332009-02-14 20:20:19 +00001076}
1077
1078/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
1079/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
1080/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
1081/// Used to indicate that names should revert to being looked up in the
1082/// defining scope.
1083void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
1084 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
Douglas Gregor053f6912009-08-26 00:04:55 +00001085 if (SS.isInvalid())
1086 return;
John McCall6df5fef2009-12-19 10:49:29 +00001087 assert(!SS.isInvalid() && computeDeclContext(SS, true) &&
1088 "exiting declarator scope we never really entered");
1089 ExitDeclaratorContext(S);
Cedric Venet084381332009-02-14 20:20:19 +00001090}