blob: 85efe2d4dfec8fee24cbc43271595c846c201354 [file] [log] [blame]
Cedric Venet3d658642009-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
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "TypeLocBuilder.h"
Cedric Venet3d658642009-02-14 20:20:19 +000016#include "clang/AST/ASTContext.h"
Douglas Gregor42af25f2009-05-11 19:58:34 +000017#include "clang/AST/DeclTemplate.h"
Douglas Gregorfe85ced2009-08-06 03:17:00 +000018#include "clang/AST/ExprCXX.h"
Douglas Gregore4e5b052009-03-19 00:18:19 +000019#include "clang/AST/NestedNameSpecifier.h"
Anders Carlssonb7906612009-08-26 23:45:07 +000020#include "clang/Basic/PartialDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000021#include "clang/Sema/DeclSpec.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Sema/Lookup.h"
23#include "clang/Sema/Template.h"
Cedric Venet3d658642009-02-14 20:20:19 +000024#include "llvm/ADT/STLExtras.h"
Douglas Gregor7551c182009-07-22 00:28:09 +000025#include "llvm/Support/raw_ostream.h"
Cedric Venet3d658642009-02-14 20:20:19 +000026using namespace clang;
27
Douglas Gregor43d88632009-11-04 22:49:18 +000028/// \brief Find the current instantiation that associated with the given type.
Richard Smithf62c6902012-11-22 00:24:47 +000029static CXXRecordDecl *getCurrentInstantiationOf(QualType T,
Douglas Gregord9ea1802011-02-19 19:24:40 +000030 DeclContext *CurContext) {
Douglas Gregor43d88632009-11-04 22:49:18 +000031 if (T.isNull())
32 return 0;
John McCall31f17ec2010-04-27 00:57:59 +000033
34 const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
Douglas Gregord9ea1802011-02-19 19:24:40 +000035 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
36 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
Richard Smithf62c6902012-11-22 00:24:47 +000037 if (!Record->isDependentContext() ||
38 Record->isCurrentInstantiation(CurContext))
Douglas Gregord9ea1802011-02-19 19:24:40 +000039 return Record;
40
Douglas Gregord9ea1802011-02-19 19:24:40 +000041 return 0;
42 } else if (isa<InjectedClassNameType>(Ty))
John McCall31f17ec2010-04-27 00:57:59 +000043 return cast<InjectedClassNameType>(Ty)->getDecl();
44 else
45 return 0;
Douglas Gregor43d88632009-11-04 22:49:18 +000046}
47
Douglas Gregor2dd078a2009-09-02 22:59:36 +000048/// \brief Compute the DeclContext that is associated with the given type.
49///
50/// \param T the type for which we are attempting to find a DeclContext.
51///
Mike Stump1eb44332009-09-09 15:08:12 +000052/// \returns the declaration context represented by the type T,
Douglas Gregor2dd078a2009-09-02 22:59:36 +000053/// or NULL if the declaration context cannot be computed (e.g., because it is
54/// dependent and not the current instantiation).
55DeclContext *Sema::computeDeclContext(QualType T) {
Douglas Gregord9ea1802011-02-19 19:24:40 +000056 if (!T->isDependentType())
57 if (const TagType *Tag = T->getAs<TagType>())
58 return Tag->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000059
Douglas Gregord9ea1802011-02-19 19:24:40 +000060 return ::getCurrentInstantiationOf(T, CurContext);
Douglas Gregor2dd078a2009-09-02 22:59:36 +000061}
62
Douglas Gregore4e5b052009-03-19 00:18:19 +000063/// \brief Compute the DeclContext that is associated with the given
64/// scope specifier.
Douglas Gregorf59a56e2009-07-21 23:53:31 +000065///
66/// \param SS the C++ scope specifier as it appears in the source
67///
68/// \param EnteringContext when true, we will be entering the context of
69/// this scope specifier, so we can retrieve the declaration context of a
70/// class template or class template partial specialization even if it is
71/// not the current instantiation.
72///
73/// \returns the declaration context represented by the scope specifier @p SS,
74/// or NULL if the declaration context cannot be computed (e.g., because it is
75/// dependent and not the current instantiation).
76DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS,
77 bool EnteringContext) {
Douglas Gregore4e5b052009-03-19 00:18:19 +000078 if (!SS.isSet() || SS.isInvalid())
Douglas Gregorca5e77f2009-03-18 00:36:05 +000079 return 0;
Douglas Gregorca5e77f2009-03-18 00:36:05 +000080
Mike Stump1eb44332009-09-09 15:08:12 +000081 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +000082 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregor42af25f2009-05-11 19:58:34 +000083 if (NNS->isDependent()) {
84 // If this nested-name-specifier refers to the current
85 // instantiation, return its DeclContext.
86 if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS))
87 return Record;
Mike Stump1eb44332009-09-09 15:08:12 +000088
Douglas Gregorf59a56e2009-07-21 23:53:31 +000089 if (EnteringContext) {
John McCall3cb0ebd2010-03-10 03:28:59 +000090 const Type *NNSType = NNS->getAsType();
91 if (!NNSType) {
Richard Smith3e4c6c42011-05-05 21:57:07 +000092 return 0;
93 }
94
95 // Look through type alias templates, per C++0x [temp.dep.type]p1.
96 NNSType = Context.getCanonicalType(NNSType);
97 if (const TemplateSpecializationType *SpecType
98 = NNSType->getAs<TemplateSpecializationType>()) {
Douglas Gregor495c35d2009-08-25 22:51:20 +000099 // We are entering the context of the nested name specifier, so try to
100 // match the nested name specifier to either a primary class template
101 // or a class template partial specialization.
Mike Stump1eb44332009-09-09 15:08:12 +0000102 if (ClassTemplateDecl *ClassTemplate
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000103 = dyn_cast_or_null<ClassTemplateDecl>(
104 SpecType->getTemplateName().getAsTemplateDecl())) {
Douglas Gregorb88e8882009-07-30 17:40:51 +0000105 QualType ContextType
106 = Context.getCanonicalType(QualType(SpecType, 0));
107
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000108 // If the type of the nested name specifier is the same as the
109 // injected class name of the named class template, we're entering
110 // into that class template definition.
John McCall3cb0ebd2010-03-10 03:28:59 +0000111 QualType Injected
Douglas Gregor24bae922010-07-08 18:37:38 +0000112 = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregorb88e8882009-07-30 17:40:51 +0000113 if (Context.hasSameType(Injected, ContextType))
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000114 return ClassTemplate->getTemplatedDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Douglas Gregorb88e8882009-07-30 17:40:51 +0000116 // If the type of the nested name specifier is the same as the
117 // type of one of the class template's class template partial
118 // specializations, we're entering into the definition of that
119 // class template partial specialization.
120 if (ClassTemplatePartialSpecializationDecl *PartialSpec
121 = ClassTemplate->findPartialSpecialization(ContextType))
122 return PartialSpec;
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000123 }
John McCall3cb0ebd2010-03-10 03:28:59 +0000124 } else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) {
Douglas Gregor495c35d2009-08-25 22:51:20 +0000125 // The nested name specifier refers to a member of a class template.
126 return RecordT->getDecl();
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000127 }
128 }
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000130 return 0;
Douglas Gregor42af25f2009-05-11 19:58:34 +0000131 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000132
133 switch (NNS->getKind()) {
134 case NestedNameSpecifier::Identifier:
David Blaikieb219cfc2011-09-23 05:06:16 +0000135 llvm_unreachable("Dependent nested-name-specifier has no DeclContext");
Douglas Gregorab452ba2009-03-26 23:50:42 +0000136
137 case NestedNameSpecifier::Namespace:
138 return NNS->getAsNamespace();
139
Douglas Gregor14aba762011-02-24 02:36:08 +0000140 case NestedNameSpecifier::NamespaceAlias:
141 return NNS->getAsNamespaceAlias()->getNamespace();
142
Douglas Gregorab452ba2009-03-26 23:50:42 +0000143 case NestedNameSpecifier::TypeSpec:
144 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregoredc90502010-02-25 04:46:04 +0000145 const TagType *Tag = NNS->getAsType()->getAs<TagType>();
146 assert(Tag && "Non-tag type in nested-name-specifier");
147 return Tag->getDecl();
David Blaikie7530c032012-01-17 06:56:22 +0000148 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000149
150 case NestedNameSpecifier::Global:
151 return Context.getTranslationUnitDecl();
152 }
153
David Blaikie7530c032012-01-17 06:56:22 +0000154 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
Douglas Gregorca5e77f2009-03-18 00:36:05 +0000155}
156
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000157bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) {
158 if (!SS.isSet() || SS.isInvalid())
159 return false;
160
Mike Stump1eb44332009-09-09 15:08:12 +0000161 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +0000162 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +0000163 return NNS->isDependent();
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000164}
165
Douglas Gregor42af25f2009-05-11 19:58:34 +0000166// \brief Determine whether this C++ scope specifier refers to an
167// unknown specialization, i.e., a dependent type that is not the
168// current instantiation.
169bool Sema::isUnknownSpecialization(const CXXScopeSpec &SS) {
170 if (!isDependentScopeSpecifier(SS))
171 return false;
172
Mike Stump1eb44332009-09-09 15:08:12 +0000173 NestedNameSpecifier *NNS
Douglas Gregor42af25f2009-05-11 19:58:34 +0000174 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
175 return getCurrentInstantiationOf(NNS) == 0;
176}
177
178/// \brief If the given nested name specifier refers to the current
179/// instantiation, return the declaration that corresponds to that
180/// current instantiation (C++0x [temp.dep.type]p1).
181///
182/// \param NNS a dependent nested name specifier.
183CXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000184 assert(getLangOpts().CPlusPlus && "Only callable in C++");
Douglas Gregor42af25f2009-05-11 19:58:34 +0000185 assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed");
186
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000187 if (!NNS->getAsType())
188 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Douglas Gregor1560def2009-07-31 18:32:42 +0000190 QualType T = QualType(NNS->getAsType(), 0);
Douglas Gregord9ea1802011-02-19 19:24:40 +0000191 return ::getCurrentInstantiationOf(T, CurContext);
Douglas Gregor42af25f2009-05-11 19:58:34 +0000192}
193
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000194/// \brief Require that the context specified by SS be complete.
195///
196/// If SS refers to a type, this routine checks whether the type is
197/// complete enough (or can be made complete enough) for name lookup
198/// into the DeclContext. A type that is not yet completed can be
199/// considered "complete enough" if it is a class/struct/union/enum
200/// that is currently being defined. Or, if we have a type that names
201/// a class template specialization that is not a complete type, we
202/// will attempt to instantiate that class template.
John McCall77bb1aa2010-05-01 00:40:08 +0000203bool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS,
204 DeclContext *DC) {
205 assert(DC != 0 && "given null context");
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Richard Smithf1c66b42012-03-14 23:13:10 +0000207 TagDecl *tag = dyn_cast<TagDecl>(DC);
Douglas Gregora4e8c2a2010-02-05 04:39:02 +0000208
Richard Smithf1c66b42012-03-14 23:13:10 +0000209 // If this is a dependent type, then we consider it complete.
210 if (!tag || tag->isDependentContext())
211 return false;
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000212
Richard Smithf1c66b42012-03-14 23:13:10 +0000213 // If we're currently defining this type, then lookup into the
214 // type is okay: don't complain that it isn't complete yet.
215 QualType type = Context.getTypeDeclType(tag);
216 const TagType *tagType = type->getAs<TagType>();
217 if (tagType && tagType->isBeingDefined())
218 return false;
John McCall9dc71d22011-07-06 06:57:57 +0000219
Richard Smithf1c66b42012-03-14 23:13:10 +0000220 SourceLocation loc = SS.getLastQualifierNameLoc();
221 if (loc.isInvalid()) loc = SS.getRange().getBegin();
John McCall9dc71d22011-07-06 06:57:57 +0000222
Richard Smithf1c66b42012-03-14 23:13:10 +0000223 // The type must be complete.
Douglas Gregord10099e2012-05-04 16:32:21 +0000224 if (RequireCompleteType(loc, type, diag::err_incomplete_nested_name_spec,
225 SS.getRange())) {
Richard Smithf1c66b42012-03-14 23:13:10 +0000226 SS.SetInvalid(SS.getRange());
227 return true;
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000228 }
229
Richard Smithf1c66b42012-03-14 23:13:10 +0000230 // Fixed enum types are complete, but they aren't valid as scopes
231 // until we see a definition, so awkwardly pull out this special
232 // case.
233 const EnumType *enumType = dyn_cast_or_null<EnumType>(tagType);
234 if (!enumType || enumType->getDecl()->isCompleteDefinition())
235 return false;
236
237 // Try to instantiate the definition, if this is a specialization of an
238 // enumeration temploid.
239 EnumDecl *ED = enumType->getDecl();
240 if (EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) {
241 MemberSpecializationInfo *MSI = ED->getMemberSpecializationInfo();
Richard Smith1af83c42012-03-23 03:33:32 +0000242 if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) {
243 if (InstantiateEnum(loc, ED, Pattern, getTemplateInstantiationArgs(ED),
244 TSK_ImplicitInstantiation)) {
245 SS.SetInvalid(SS.getRange());
246 return true;
247 }
248 return false;
249 }
Richard Smithf1c66b42012-03-14 23:13:10 +0000250 }
251
252 Diag(loc, diag::err_incomplete_nested_name_spec)
253 << type << SS.getRange();
254 SS.SetInvalid(SS.getRange());
255 return true;
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000256}
Cedric Venet3d658642009-02-14 20:20:19 +0000257
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000258bool Sema::ActOnCXXGlobalScopeSpecifier(Scope *S, SourceLocation CCLoc,
259 CXXScopeSpec &SS) {
260 SS.MakeGlobal(Context, CCLoc);
261 return false;
Cedric Venet3d658642009-02-14 20:20:19 +0000262}
263
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000264/// \brief Determines whether the given declaration is an valid acceptable
265/// result for name lookup of a nested-name-specifier.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000266bool Sema::isAcceptableNestedNameSpecifier(const NamedDecl *SD) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000267 if (!SD)
268 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000270 // Namespace and namespace aliases are fine.
271 if (isa<NamespaceDecl>(SD) || isa<NamespaceAliasDecl>(SD))
272 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000274 if (!isa<TypeDecl>(SD))
275 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Richard Smith6b130222011-10-18 21:39:00 +0000277 // Determine whether we have a class (or, in C++11, an enum) or
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000278 // a typedef thereof. If so, build the nested-name-specifier.
279 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
280 if (T->isDependentType())
281 return true;
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000282 else if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000283 if (TD->getUnderlyingType()->isRecordType() ||
Richard Smith80ad52f2013-01-02 11:42:31 +0000284 (Context.getLangOpts().CPlusPlus11 &&
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000285 TD->getUnderlyingType()->isEnumeralType()))
286 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000287 } else if (isa<RecordDecl>(SD) ||
Richard Smith80ad52f2013-01-02 11:42:31 +0000288 (Context.getLangOpts().CPlusPlus11 && isa<EnumDecl>(SD)))
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000289 return true;
290
291 return false;
292}
293
Douglas Gregorc68afe22009-09-03 21:38:09 +0000294/// \brief If the given nested-name-specifier begins with a bare identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000295/// (e.g., Base::), perform name lookup for that identifier as a
Douglas Gregorc68afe22009-09-03 21:38:09 +0000296/// nested-name-specifier within the given scope, and return the result of that
297/// name lookup.
298NamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) {
299 if (!S || !NNS)
300 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Douglas Gregorc68afe22009-09-03 21:38:09 +0000302 while (NNS->getPrefix())
303 NNS = NNS->getPrefix();
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Douglas Gregorc68afe22009-09-03 21:38:09 +0000305 if (NNS->getKind() != NestedNameSpecifier::Identifier)
306 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000307
John McCalla24dc2e2009-11-17 02:14:36 +0000308 LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(),
309 LookupNestedNameSpecifierName);
310 LookupName(Found, S);
Douglas Gregorc68afe22009-09-03 21:38:09 +0000311 assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet");
312
John McCall1bcee0a2009-12-02 08:25:40 +0000313 if (!Found.isSingleResult())
314 return 0;
315
316 NamedDecl *Result = Found.getFoundDecl();
Douglas Gregoredc90502010-02-25 04:46:04 +0000317 if (isAcceptableNestedNameSpecifier(Result))
Douglas Gregorc68afe22009-09-03 21:38:09 +0000318 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Douglas Gregorc68afe22009-09-03 21:38:09 +0000320 return 0;
321}
322
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000323bool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS,
Douglas Gregor77549082010-02-24 21:29:12 +0000324 SourceLocation IdLoc,
325 IdentifierInfo &II,
John McCallb3d87482010-08-24 05:47:05 +0000326 ParsedType ObjectTypePtr) {
Douglas Gregor77549082010-02-24 21:29:12 +0000327 QualType ObjectType = GetTypeFromParser(ObjectTypePtr);
328 LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName);
329
330 // Determine where to perform name lookup
331 DeclContext *LookupCtx = 0;
332 bool isDependent = false;
333 if (!ObjectType.isNull()) {
334 // This nested-name-specifier occurs in a member access expression, e.g.,
335 // x->B::f, and we are looking into the type of the object.
336 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
337 LookupCtx = computeDeclContext(ObjectType);
338 isDependent = ObjectType->isDependentType();
339 } else if (SS.isSet()) {
340 // This nested-name-specifier occurs after another nested-name-specifier,
341 // so long into the context associated with the prior nested-name-specifier.
342 LookupCtx = computeDeclContext(SS, false);
343 isDependent = isDependentScopeSpecifier(SS);
344 Found.setContextRange(SS.getRange());
345 }
346
347 if (LookupCtx) {
348 // Perform "qualified" name lookup into the declaration context we
349 // computed, which is either the type of the base of a member access
350 // expression or the declaration context associated with a prior
351 // nested-name-specifier.
352
353 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000354 if (!LookupCtx->isDependentContext() &&
355 RequireCompleteDeclContext(SS, LookupCtx))
Douglas Gregor77549082010-02-24 21:29:12 +0000356 return false;
357
358 LookupQualifiedName(Found, LookupCtx);
359 } else if (isDependent) {
360 return false;
361 } else {
362 LookupName(Found, S);
363 }
364 Found.suppressDiagnostics();
365
366 if (NamedDecl *ND = Found.getAsSingle<NamedDecl>())
367 return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND);
368
369 return false;
370}
371
Kaelyn Uhrain3b4b0472012-01-12 22:32:39 +0000372namespace {
373
374// Callback to only accept typo corrections that can be a valid C++ member
375// intializer: either a non-static field member or a base class.
376class NestedNameSpecifierValidatorCCC : public CorrectionCandidateCallback {
377 public:
378 explicit NestedNameSpecifierValidatorCCC(Sema &SRef)
379 : SRef(SRef) {}
380
381 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
382 return SRef.isAcceptableNestedNameSpecifier(candidate.getCorrectionDecl());
383 }
384
385 private:
386 Sema &SRef;
387};
388
389}
390
Douglas Gregorc68afe22009-09-03 21:38:09 +0000391/// \brief Build a new nested-name-specifier for "identifier::", as described
392/// by ActOnCXXNestedNameSpecifier.
393///
394/// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in
395/// that it contains an extra parameter \p ScopeLookupResult, which provides
396/// the result of name lookup within the scope of the nested-name-specifier
Douglas Gregora6e51992009-12-30 16:01:52 +0000397/// that was computed at template definition time.
Chris Lattner46646492009-12-07 01:36:53 +0000398///
399/// If ErrorRecoveryLookup is true, then this call is used to improve error
400/// recovery. This means that it should not emit diagnostics, it should
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000401/// just return true on failure. It also means it should only return a valid
Chris Lattner46646492009-12-07 01:36:53 +0000402/// scope if it *knows* that the result is correct. It should not return in a
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000403/// dependent context, for example. Nor will it extend \p SS with the scope
404/// specifier.
405bool Sema::BuildCXXNestedNameSpecifier(Scope *S,
406 IdentifierInfo &Identifier,
407 SourceLocation IdentifierLoc,
408 SourceLocation CCLoc,
409 QualType ObjectType,
410 bool EnteringContext,
411 CXXScopeSpec &SS,
412 NamedDecl *ScopeLookupResult,
413 bool ErrorRecoveryLookup) {
414 LookupResult Found(*this, &Identifier, IdentifierLoc,
415 LookupNestedNameSpecifierName);
John McCalla24dc2e2009-11-17 02:14:36 +0000416
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000417 // Determine where to perform name lookup
418 DeclContext *LookupCtx = 0;
419 bool isDependent = false;
Douglas Gregorc68afe22009-09-03 21:38:09 +0000420 if (!ObjectType.isNull()) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000421 // This nested-name-specifier occurs in a member access expression, e.g.,
422 // x->B::f, and we are looking into the type of the object.
423 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000424 LookupCtx = computeDeclContext(ObjectType);
425 isDependent = ObjectType->isDependentType();
426 } else if (SS.isSet()) {
427 // This nested-name-specifier occurs after another nested-name-specifier,
Richard Smith3e4c6c42011-05-05 21:57:07 +0000428 // so look into the context associated with the prior nested-name-specifier.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000429 LookupCtx = computeDeclContext(SS, EnteringContext);
430 isDependent = isDependentScopeSpecifier(SS);
John McCalla24dc2e2009-11-17 02:14:36 +0000431 Found.setContextRange(SS.getRange());
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
John McCalla24dc2e2009-11-17 02:14:36 +0000434
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000435 bool ObjectTypeSearchedInScope = false;
436 if (LookupCtx) {
Mike Stump1eb44332009-09-09 15:08:12 +0000437 // Perform "qualified" name lookup into the declaration context we
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000438 // computed, which is either the type of the base of a member access
Mike Stump1eb44332009-09-09 15:08:12 +0000439 // expression or the declaration context associated with a prior
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000440 // nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000442 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000443 if (!LookupCtx->isDependentContext() &&
444 RequireCompleteDeclContext(SS, LookupCtx))
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000445 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000446
John McCalla24dc2e2009-11-17 02:14:36 +0000447 LookupQualifiedName(Found, LookupCtx);
Mike Stump1eb44332009-09-09 15:08:12 +0000448
John McCalla24dc2e2009-11-17 02:14:36 +0000449 if (!ObjectType.isNull() && Found.empty()) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000450 // C++ [basic.lookup.classref]p4:
451 // If the id-expression in a class member access is a qualified-id of
Mike Stump1eb44332009-09-09 15:08:12 +0000452 // the form
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000453 //
454 // class-name-or-namespace-name::...
455 //
Mike Stump1eb44332009-09-09 15:08:12 +0000456 // the class-name-or-namespace-name following the . or -> operator is
457 // looked up both in the context of the entire postfix-expression and in
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000458 // the scope of the class of the object expression. If the name is found
Mike Stump1eb44332009-09-09 15:08:12 +0000459 // only in the scope of the class of the object expression, the name
460 // shall refer to a class-name. If the name is found only in the
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000461 // context of the entire postfix-expression, the name shall refer to a
462 // class-name or namespace-name. [...]
463 //
464 // Qualified name lookup into a class will not find a namespace-name,
Douglas Gregor714c9922011-05-15 17:27:27 +0000465 // so we do not need to diagnose that case specifically. However,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000466 // this qualified name lookup may find nothing. In that case, perform
Mike Stump1eb44332009-09-09 15:08:12 +0000467 // unqualified name lookup in the given scope (if available) or
Douglas Gregorc68afe22009-09-03 21:38:09 +0000468 // reconstruct the result from when name lookup was performed at template
469 // definition time.
470 if (S)
John McCalla24dc2e2009-11-17 02:14:36 +0000471 LookupName(Found, S);
John McCallf36e02d2009-10-09 21:13:30 +0000472 else if (ScopeLookupResult)
473 Found.addDecl(ScopeLookupResult);
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000475 ObjectTypeSearchedInScope = true;
476 }
Douglas Gregorac7cbd82010-07-28 14:49:07 +0000477 } else if (!isDependent) {
478 // Perform unqualified name lookup in the current scope.
479 LookupName(Found, S);
480 }
481
482 // If we performed lookup into a dependent context and did not find anything,
483 // that's fine: just build a dependent nested-name-specifier.
484 if (Found.empty() && isDependent &&
485 !(LookupCtx && LookupCtx->isRecord() &&
486 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
487 !cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()))) {
Chris Lattner46646492009-12-07 01:36:53 +0000488 // Don't speculate if we're just trying to improve error recovery.
489 if (ErrorRecoveryLookup)
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000490 return true;
Chris Lattner46646492009-12-07 01:36:53 +0000491
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000492 // We were not able to compute the declaration context for a dependent
Mike Stump1eb44332009-09-09 15:08:12 +0000493 // base object type or prior nested-name-specifier, so this
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000494 // nested-name-specifier refers to an unknown specialization. Just build
495 // a dependent nested-name-specifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000496 SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc);
497 return false;
Douglas Gregorac7cbd82010-07-28 14:49:07 +0000498 }
499
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000500 // FIXME: Deal with ambiguities cleanly.
Douglas Gregor175a6562009-12-31 08:26:35 +0000501
502 if (Found.empty() && !ErrorRecoveryLookup) {
503 // We haven't found anything, and we're not recovering from a
504 // different kind of error, so look for typos.
505 DeclarationName Name = Found.getLookupName();
Kaelyn Uhrain3b4b0472012-01-12 22:32:39 +0000506 NestedNameSpecifierValidatorCCC Validator(*this);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000507 TypoCorrection Corrected;
508 Found.clear();
509 if ((Corrected = CorrectTypo(Found.getLookupNameInfo(),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000510 Found.getLookupKind(), S, &SS, Validator,
Kaelyn Uhrain3b4b0472012-01-12 22:32:39 +0000511 LookupCtx, EnteringContext))) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000512 std::string CorrectedStr(Corrected.getAsString(getLangOpts()));
513 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOpts()));
Douglas Gregor175a6562009-12-31 08:26:35 +0000514 if (LookupCtx)
515 Diag(Found.getNameLoc(), diag::err_no_member_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000516 << Name << LookupCtx << CorrectedQuotedStr << SS.getRange()
David Blaikie6952c012012-10-12 20:00:44 +0000517 << FixItHint::CreateReplacement(Corrected.getCorrectionRange(),
518 CorrectedStr);
Douglas Gregor175a6562009-12-31 08:26:35 +0000519 else
520 Diag(Found.getNameLoc(), diag::err_undeclared_var_use_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000521 << Name << CorrectedQuotedStr
522 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000523
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000524 if (NamedDecl *ND = Corrected.getCorrectionDecl()) {
525 Diag(ND->getLocation(), diag::note_previous_decl) << CorrectedQuotedStr;
526 Found.addDecl(ND);
527 }
528 Found.setLookupName(Corrected.getCorrection());
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000529 } else {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000530 Found.setLookupName(&Identifier);
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000531 }
Douglas Gregor175a6562009-12-31 08:26:35 +0000532 }
533
John McCall1bcee0a2009-12-02 08:25:40 +0000534 NamedDecl *SD = Found.getAsSingle<NamedDecl>();
Douglas Gregoredc90502010-02-25 04:46:04 +0000535 if (isAcceptableNestedNameSpecifier(SD)) {
Douglas Gregor05e60762012-05-01 20:23:02 +0000536 if (!ObjectType.isNull() && !ObjectTypeSearchedInScope &&
Richard Smith80ad52f2013-01-02 11:42:31 +0000537 !getLangOpts().CPlusPlus11) {
Douglas Gregor05e60762012-05-01 20:23:02 +0000538 // C++03 [basic.lookup.classref]p4:
Mike Stump1eb44332009-09-09 15:08:12 +0000539 // [...] If the name is found in both contexts, the
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000540 // class-name-or-namespace-name shall refer to the same entity.
541 //
542 // We already found the name in the scope of the object. Now, look
543 // into the current scope (the scope of the postfix-expression) to
Douglas Gregorc68afe22009-09-03 21:38:09 +0000544 // see if we can find the same name there. As above, if there is no
545 // scope, reconstruct the result from the template instantiation itself.
Douglas Gregor05e60762012-05-01 20:23:02 +0000546 //
547 // Note that C++11 does *not* perform this redundant lookup.
John McCallf36e02d2009-10-09 21:13:30 +0000548 NamedDecl *OuterDecl;
549 if (S) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000550 LookupResult FoundOuter(*this, &Identifier, IdentifierLoc,
551 LookupNestedNameSpecifierName);
John McCalla24dc2e2009-11-17 02:14:36 +0000552 LookupName(FoundOuter, S);
John McCall1bcee0a2009-12-02 08:25:40 +0000553 OuterDecl = FoundOuter.getAsSingle<NamedDecl>();
John McCallf36e02d2009-10-09 21:13:30 +0000554 } else
555 OuterDecl = ScopeLookupResult;
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Douglas Gregoredc90502010-02-25 04:46:04 +0000557 if (isAcceptableNestedNameSpecifier(OuterDecl) &&
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000558 OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() &&
559 (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) ||
560 !Context.hasSameType(
Douglas Gregorc68afe22009-09-03 21:38:09 +0000561 Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)),
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000562 Context.getTypeDeclType(cast<TypeDecl>(SD))))) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000563 if (ErrorRecoveryLookup)
564 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000566 Diag(IdentifierLoc,
567 diag::err_nested_name_member_ref_lookup_ambiguous)
568 << &Identifier;
569 Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type)
570 << ObjectType;
571 Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope);
572
573 // Fall through so that we'll pick the name we found in the object
574 // type, since that's probably what the user wanted anyway.
575 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000576 }
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000578 // If we're just performing this lookup for error-recovery purposes,
579 // don't extend the nested-name-specifier. Just return now.
580 if (ErrorRecoveryLookup)
581 return false;
582
583 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) {
584 SS.Extend(Context, Namespace, IdentifierLoc, CCLoc);
585 return false;
586 }
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000588 if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) {
Douglas Gregor14aba762011-02-24 02:36:08 +0000589 SS.Extend(Context, Alias, IdentifierLoc, CCLoc);
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000590 return false;
591 }
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000593 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000594 TypeLocBuilder TLB;
595 if (isa<InjectedClassNameType>(T)) {
596 InjectedClassNameTypeLoc InjectedTL
597 = TLB.push<InjectedClassNameTypeLoc>(T);
598 InjectedTL.setNameLoc(IdentifierLoc);
Douglas Gregorbd61e342011-05-04 23:05:40 +0000599 } else if (isa<RecordType>(T)) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000600 RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T);
601 RecordTL.setNameLoc(IdentifierLoc);
Douglas Gregorbd61e342011-05-04 23:05:40 +0000602 } else if (isa<TypedefType>(T)) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000603 TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T);
604 TypedefTL.setNameLoc(IdentifierLoc);
Douglas Gregorbd61e342011-05-04 23:05:40 +0000605 } else if (isa<EnumType>(T)) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000606 EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T);
607 EnumTL.setNameLoc(IdentifierLoc);
Douglas Gregorbd61e342011-05-04 23:05:40 +0000608 } else if (isa<TemplateTypeParmType>(T)) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000609 TemplateTypeParmTypeLoc TemplateTypeTL
610 = TLB.push<TemplateTypeParmTypeLoc>(T);
611 TemplateTypeTL.setNameLoc(IdentifierLoc);
Douglas Gregorbd61e342011-05-04 23:05:40 +0000612 } else if (isa<UnresolvedUsingType>(T)) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000613 UnresolvedUsingTypeLoc UnresolvedTL
614 = TLB.push<UnresolvedUsingTypeLoc>(T);
615 UnresolvedTL.setNameLoc(IdentifierLoc);
Douglas Gregorbd61e342011-05-04 23:05:40 +0000616 } else if (isa<SubstTemplateTypeParmType>(T)) {
617 SubstTemplateTypeParmTypeLoc TL
618 = TLB.push<SubstTemplateTypeParmTypeLoc>(T);
619 TL.setNameLoc(IdentifierLoc);
620 } else if (isa<SubstTemplateTypeParmPackType>(T)) {
621 SubstTemplateTypeParmPackTypeLoc TL
622 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(T);
623 TL.setNameLoc(IdentifierLoc);
624 } else {
625 llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier");
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000626 }
627
Richard Smith95aafb22011-10-20 03:28:47 +0000628 if (T->isEnumeralType())
629 Diag(IdentifierLoc, diag::warn_cxx98_compat_enum_nested_name_spec);
630
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000631 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
632 CCLoc);
633 return false;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000634 }
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Chris Lattner46646492009-12-07 01:36:53 +0000636 // Otherwise, we have an error case. If we don't want diagnostics, just
637 // return an error now.
638 if (ErrorRecoveryLookup)
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000639 return true;
Chris Lattner46646492009-12-07 01:36:53 +0000640
Cedric Venet3d658642009-02-14 20:20:19 +0000641 // If we didn't find anything during our lookup, try again with
642 // ordinary name lookup, which can help us produce better error
643 // messages.
John McCall1bcee0a2009-12-02 08:25:40 +0000644 if (Found.empty()) {
John McCalla24dc2e2009-11-17 02:14:36 +0000645 Found.clear(LookupOrdinaryName);
646 LookupName(Found, S);
John McCallf36e02d2009-10-09 21:13:30 +0000647 }
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Francois Pichetdfb6ae12011-07-27 01:05:24 +0000649 // In Microsoft mode, if we are within a templated function and we can't
650 // resolve Identifier, then extend the SS with Identifier. This will have
651 // the effect of resolving Identifier during template instantiation.
652 // The goal is to be able to resolve a function call whose
653 // nested-name-specifier is located inside a dependent base class.
654 // Example:
655 //
656 // class C {
657 // public:
658 // static void foo2() { }
659 // };
660 // template <class T> class A { public: typedef C D; };
661 //
662 // template <class T> class B : public A<T> {
663 // public:
664 // void foo() { D::foo2(); }
665 // };
David Blaikie4e4d0842012-03-11 07:00:24 +0000666 if (getLangOpts().MicrosoftExt) {
Francois Pichetdfb6ae12011-07-27 01:05:24 +0000667 DeclContext *DC = LookupCtx ? LookupCtx : CurContext;
668 if (DC->isDependentContext() && DC->isFunctionOrMethod()) {
669 SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc);
670 return false;
671 }
672 }
673
Cedric Venet3d658642009-02-14 20:20:19 +0000674 unsigned DiagID;
John McCall1bcee0a2009-12-02 08:25:40 +0000675 if (!Found.empty())
Cedric Venet3d658642009-02-14 20:20:19 +0000676 DiagID = diag::err_expected_class_or_namespace;
Anders Carlssona31d5f72009-08-30 07:09:50 +0000677 else if (SS.isSet()) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000678 Diag(IdentifierLoc, diag::err_no_member)
679 << &Identifier << LookupCtx << SS.getRange();
680 return true;
Anders Carlssona31d5f72009-08-30 07:09:50 +0000681 } else
Cedric Venet3d658642009-02-14 20:20:19 +0000682 DiagID = diag::err_undeclared_var_use;
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Cedric Venet3d658642009-02-14 20:20:19 +0000684 if (SS.isSet())
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000685 Diag(IdentifierLoc, DiagID) << &Identifier << SS.getRange();
Cedric Venet3d658642009-02-14 20:20:19 +0000686 else
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000687 Diag(IdentifierLoc, DiagID) << &Identifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000689 return true;
Cedric Venet3d658642009-02-14 20:20:19 +0000690}
691
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000692bool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
693 IdentifierInfo &Identifier,
694 SourceLocation IdentifierLoc,
695 SourceLocation CCLoc,
696 ParsedType ObjectType,
697 bool EnteringContext,
698 CXXScopeSpec &SS) {
699 if (SS.isInvalid())
700 return true;
701
702 return BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, CCLoc,
703 GetTypeFromParser(ObjectType),
704 EnteringContext, SS,
705 /*ScopeLookupResult=*/0, false);
Chris Lattner46646492009-12-07 01:36:53 +0000706}
707
David Blaikie42d6d0c2011-12-04 05:04:18 +0000708bool Sema::ActOnCXXNestedNameSpecifierDecltype(CXXScopeSpec &SS,
709 const DeclSpec &DS,
710 SourceLocation ColonColonLoc) {
711 if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error)
712 return true;
713
714 assert(DS.getTypeSpecType() == DeclSpec::TST_decltype);
715
716 QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc());
717 if (!T->isDependentType() && !T->getAs<TagType>()) {
718 Diag(DS.getTypeSpecTypeLoc(), diag::err_expected_class)
David Blaikie4e4d0842012-03-11 07:00:24 +0000719 << T << getLangOpts().CPlusPlus;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000720 return true;
721 }
722
723 TypeLocBuilder TLB;
724 DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T);
725 DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc());
726 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
727 ColonColonLoc);
728 return false;
729}
730
Chris Lattner46646492009-12-07 01:36:53 +0000731/// IsInvalidUnlessNestedName - This method is used for error recovery
732/// purposes to determine whether the specified identifier is only valid as
733/// a nested name specifier, for example a namespace name. It is
734/// conservatively correct to always return false from this method.
735///
736/// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000737bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS,
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000738 IdentifierInfo &Identifier,
739 SourceLocation IdentifierLoc,
740 SourceLocation ColonLoc,
741 ParsedType ObjectType,
Chris Lattner46646492009-12-07 01:36:53 +0000742 bool EnteringContext) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000743 if (SS.isInvalid())
744 return false;
745
746 return !BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, ColonLoc,
747 GetTypeFromParser(ObjectType),
748 EnteringContext, SS,
749 /*ScopeLookupResult=*/0, true);
Douglas Gregorc68afe22009-09-03 21:38:09 +0000750}
751
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000752bool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000753 CXXScopeSpec &SS,
754 SourceLocation TemplateKWLoc,
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000755 TemplateTy Template,
756 SourceLocation TemplateNameLoc,
757 SourceLocation LAngleLoc,
758 ASTTemplateArgsPtr TemplateArgsIn,
759 SourceLocation RAngleLoc,
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000760 SourceLocation CCLoc,
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000761 bool EnteringContext) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000762 if (SS.isInvalid())
763 return true;
764
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000765 // Translate the parser's template argument list in our AST format.
766 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
767 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
768
769 if (DependentTemplateName *DTN = Template.get().getAsDependentTemplateName()){
770 // Handle a dependent template specialization for which we cannot resolve
771 // the template name.
772 assert(DTN->getQualifier()
773 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
774 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000775 DTN->getQualifier(),
776 DTN->getIdentifier(),
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000777 TemplateArgs);
778
779 // Create source-location information for this type.
780 TypeLocBuilder Builder;
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000781 DependentTemplateSpecializationTypeLoc SpecTL
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000782 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000783 SpecTL.setElaboratedKeywordLoc(SourceLocation());
784 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara66581d42012-02-06 22:45:07 +0000785 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000786 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000787 SpecTL.setLAngleLoc(LAngleLoc);
788 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000789 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
790 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
791
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000792 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000793 CCLoc);
794 return false;
795 }
796
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000797
798 if (Template.get().getAsOverloadedTemplate() ||
799 isa<FunctionTemplateDecl>(Template.get().getAsTemplateDecl())) {
800 SourceRange R(TemplateNameLoc, RAngleLoc);
801 if (SS.getRange().isValid())
802 R.setBegin(SS.getRange().getBegin());
803
804 Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier)
805 << Template.get() << R;
806 NoteAllFoundTemplates(Template.get());
807 return true;
808 }
809
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000810 // We were able to resolve the template name to an actual template.
811 // Build an appropriate nested-name-specifier.
812 QualType T = CheckTemplateIdType(Template.get(), TemplateNameLoc,
813 TemplateArgs);
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000814 if (T.isNull())
815 return true;
816
Richard Smith3e4c6c42011-05-05 21:57:07 +0000817 // Alias template specializations can produce types which are not valid
818 // nested name specifiers.
819 if (!T->isDependentType() && !T->getAs<TagType>()) {
820 Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T;
821 NoteAllFoundTemplates(Template.get());
822 return true;
823 }
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000824
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000825 // Provide source-location information for the template specialization type.
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000826 TypeLocBuilder Builder;
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000827 TemplateSpecializationTypeLoc SpecTL
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000828 = Builder.push<TemplateSpecializationTypeLoc>(T);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000829 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
830 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000831 SpecTL.setLAngleLoc(LAngleLoc);
832 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000833 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
834 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
835
836
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000837 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000838 CCLoc);
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000839 return false;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000840}
841
Douglas Gregorc34348a2011-02-24 17:54:50 +0000842namespace {
843 /// \brief A structure that stores a nested-name-specifier annotation,
844 /// including both the nested-name-specifier
845 struct NestedNameSpecifierAnnotation {
846 NestedNameSpecifier *NNS;
847 };
848}
849
850void *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) {
851 if (SS.isEmpty() || SS.isInvalid())
852 return 0;
853
854 void *Mem = Context.Allocate((sizeof(NestedNameSpecifierAnnotation) +
855 SS.location_size()),
856 llvm::alignOf<NestedNameSpecifierAnnotation>());
857 NestedNameSpecifierAnnotation *Annotation
858 = new (Mem) NestedNameSpecifierAnnotation;
859 Annotation->NNS = SS.getScopeRep();
860 memcpy(Annotation + 1, SS.location_data(), SS.location_size());
861 return Annotation;
862}
863
864void Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr,
865 SourceRange AnnotationRange,
866 CXXScopeSpec &SS) {
867 if (!AnnotationPtr) {
868 SS.SetInvalid(AnnotationRange);
869 return;
870 }
871
872 NestedNameSpecifierAnnotation *Annotation
873 = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr);
874 SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1));
875}
876
John McCalle7e278b2009-12-11 20:04:54 +0000877bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
878 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
879
880 NestedNameSpecifier *Qualifier =
881 static_cast<NestedNameSpecifier*>(SS.getScopeRep());
882
883 // There are only two places a well-formed program may qualify a
884 // declarator: first, when defining a namespace or class member
885 // out-of-line, and second, when naming an explicitly-qualified
886 // friend function. The latter case is governed by
887 // C++03 [basic.lookup.unqual]p10:
888 // In a friend declaration naming a member function, a name used
889 // in the function declarator and not part of a template-argument
890 // in a template-id is first looked up in the scope of the member
891 // function's class. If it is not found, or if the name is part of
892 // a template-argument in a template-id, the look up is as
893 // described for unqualified names in the definition of the class
894 // granting friendship.
895 // i.e. we don't push a scope unless it's a class member.
896
897 switch (Qualifier->getKind()) {
898 case NestedNameSpecifier::Global:
899 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +0000900 case NestedNameSpecifier::NamespaceAlias:
John McCalle7e278b2009-12-11 20:04:54 +0000901 // These are always namespace scopes. We never want to enter a
902 // namespace scope from anything but a file context.
Sebastian Redl7a126a42010-08-31 00:36:30 +0000903 return CurContext->getRedeclContext()->isFileContext();
John McCalle7e278b2009-12-11 20:04:54 +0000904
905 case NestedNameSpecifier::Identifier:
906 case NestedNameSpecifier::TypeSpec:
907 case NestedNameSpecifier::TypeSpecWithTemplate:
908 // These are never namespace scopes.
909 return true;
910 }
911
David Blaikie7530c032012-01-17 06:56:22 +0000912 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
John McCalle7e278b2009-12-11 20:04:54 +0000913}
914
Cedric Venet3d658642009-02-14 20:20:19 +0000915/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
916/// scope or nested-name-specifier) is parsed, part of a declarator-id.
917/// After this method is called, according to [C++ 3.4.3p3], names should be
918/// looked up in the declarator-id's scope, until the declarator is parsed and
919/// ActOnCXXExitDeclaratorScope is called.
920/// The 'SS' should be a non-empty valid CXXScopeSpec.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000921bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) {
Cedric Venet3d658642009-02-14 20:20:19 +0000922 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
John McCall7a1dc562009-12-19 10:49:29 +0000923
924 if (SS.isInvalid()) return true;
925
926 DeclContext *DC = computeDeclContext(SS, true);
927 if (!DC) return true;
928
929 // Before we enter a declarator's context, we need to make sure that
930 // it is a complete declaration context.
John McCall77bb1aa2010-05-01 00:40:08 +0000931 if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC))
John McCall7a1dc562009-12-19 10:49:29 +0000932 return true;
933
934 EnterDeclaratorContext(S, DC);
John McCall31f17ec2010-04-27 00:57:59 +0000935
936 // Rebuild the nested name specifier for the new scope.
937 if (DC->isDependentContext())
938 RebuildNestedNameSpecifierInCurrentInstantiation(SS);
939
Douglas Gregor7dfd0fb2009-09-24 23:39:01 +0000940 return false;
Cedric Venet3d658642009-02-14 20:20:19 +0000941}
942
943/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
944/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
945/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
946/// Used to indicate that names should revert to being looked up in the
947/// defining scope.
948void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
949 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
Douglas Gregordacd4342009-08-26 00:04:55 +0000950 if (SS.isInvalid())
951 return;
John McCall7a1dc562009-12-19 10:49:29 +0000952 assert(!SS.isInvalid() && computeDeclContext(SS, true) &&
953 "exiting declarator scope we never really entered");
954 ExitDeclaratorContext(S);
Cedric Venet3d658642009-02-14 20:20:19 +0000955}