blob: 01ac8f7fb62d7ecfaf5f3a96e472d61bd46b1222 [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
Richard Smithc2e935f2013-03-26 00:54:11 +000081 NestedNameSpecifier *NNS = SS.getScopeRep();
Douglas Gregor42af25f2009-05-11 19:58:34 +000082 if (NNS->isDependent()) {
83 // If this nested-name-specifier refers to the current
84 // instantiation, return its DeclContext.
85 if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS))
86 return Record;
Mike Stump1eb44332009-09-09 15:08:12 +000087
Douglas Gregorf59a56e2009-07-21 23:53:31 +000088 if (EnteringContext) {
John McCall3cb0ebd2010-03-10 03:28:59 +000089 const Type *NNSType = NNS->getAsType();
90 if (!NNSType) {
Richard Smith3e4c6c42011-05-05 21:57:07 +000091 return 0;
92 }
93
94 // Look through type alias templates, per C++0x [temp.dep.type]p1.
95 NNSType = Context.getCanonicalType(NNSType);
96 if (const TemplateSpecializationType *SpecType
97 = NNSType->getAs<TemplateSpecializationType>()) {
Douglas Gregor495c35d2009-08-25 22:51:20 +000098 // We are entering the context of the nested name specifier, so try to
99 // match the nested name specifier to either a primary class template
100 // or a class template partial specialization.
Mike Stump1eb44332009-09-09 15:08:12 +0000101 if (ClassTemplateDecl *ClassTemplate
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000102 = dyn_cast_or_null<ClassTemplateDecl>(
103 SpecType->getTemplateName().getAsTemplateDecl())) {
Douglas Gregorb88e8882009-07-30 17:40:51 +0000104 QualType ContextType
105 = Context.getCanonicalType(QualType(SpecType, 0));
106
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000107 // If the type of the nested name specifier is the same as the
108 // injected class name of the named class template, we're entering
109 // into that class template definition.
John McCall3cb0ebd2010-03-10 03:28:59 +0000110 QualType Injected
Douglas Gregor24bae922010-07-08 18:37:38 +0000111 = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregorb88e8882009-07-30 17:40:51 +0000112 if (Context.hasSameType(Injected, ContextType))
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000113 return ClassTemplate->getTemplatedDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Douglas Gregorb88e8882009-07-30 17:40:51 +0000115 // If the type of the nested name specifier is the same as the
116 // type of one of the class template's class template partial
117 // specializations, we're entering into the definition of that
118 // class template partial specialization.
119 if (ClassTemplatePartialSpecializationDecl *PartialSpec
120 = ClassTemplate->findPartialSpecialization(ContextType))
121 return PartialSpec;
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000122 }
John McCall3cb0ebd2010-03-10 03:28:59 +0000123 } else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) {
Douglas Gregor495c35d2009-08-25 22:51:20 +0000124 // The nested name specifier refers to a member of a class template.
125 return RecordT->getDecl();
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000126 }
127 }
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000129 return 0;
Douglas Gregor42af25f2009-05-11 19:58:34 +0000130 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000131
132 switch (NNS->getKind()) {
133 case NestedNameSpecifier::Identifier:
David Blaikieb219cfc2011-09-23 05:06:16 +0000134 llvm_unreachable("Dependent nested-name-specifier has no DeclContext");
Douglas Gregorab452ba2009-03-26 23:50:42 +0000135
136 case NestedNameSpecifier::Namespace:
137 return NNS->getAsNamespace();
138
Douglas Gregor14aba762011-02-24 02:36:08 +0000139 case NestedNameSpecifier::NamespaceAlias:
140 return NNS->getAsNamespaceAlias()->getNamespace();
141
Douglas Gregorab452ba2009-03-26 23:50:42 +0000142 case NestedNameSpecifier::TypeSpec:
143 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregoredc90502010-02-25 04:46:04 +0000144 const TagType *Tag = NNS->getAsType()->getAs<TagType>();
145 assert(Tag && "Non-tag type in nested-name-specifier");
146 return Tag->getDecl();
David Blaikie7530c032012-01-17 06:56:22 +0000147 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000148
149 case NestedNameSpecifier::Global:
150 return Context.getTranslationUnitDecl();
151 }
152
David Blaikie7530c032012-01-17 06:56:22 +0000153 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
Douglas Gregorca5e77f2009-03-18 00:36:05 +0000154}
155
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000156bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) {
157 if (!SS.isSet() || SS.isInvalid())
158 return false;
159
Richard Smithc2e935f2013-03-26 00:54:11 +0000160 return SS.getScopeRep()->isDependent();
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000161}
162
Douglas Gregor42af25f2009-05-11 19:58:34 +0000163// \brief Determine whether this C++ scope specifier refers to an
164// unknown specialization, i.e., a dependent type that is not the
165// current instantiation.
166bool Sema::isUnknownSpecialization(const CXXScopeSpec &SS) {
167 if (!isDependentScopeSpecifier(SS))
168 return false;
169
Richard Smithc2e935f2013-03-26 00:54:11 +0000170 return getCurrentInstantiationOf(SS.getScopeRep()) == 0;
Douglas Gregor42af25f2009-05-11 19:58:34 +0000171}
172
173/// \brief If the given nested name specifier refers to the current
174/// instantiation, return the declaration that corresponds to that
175/// current instantiation (C++0x [temp.dep.type]p1).
176///
177/// \param NNS a dependent nested name specifier.
178CXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000179 assert(getLangOpts().CPlusPlus && "Only callable in C++");
Douglas Gregor42af25f2009-05-11 19:58:34 +0000180 assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed");
181
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000182 if (!NNS->getAsType())
183 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Douglas Gregor1560def2009-07-31 18:32:42 +0000185 QualType T = QualType(NNS->getAsType(), 0);
Douglas Gregord9ea1802011-02-19 19:24:40 +0000186 return ::getCurrentInstantiationOf(T, CurContext);
Douglas Gregor42af25f2009-05-11 19:58:34 +0000187}
188
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000189/// \brief Require that the context specified by SS be complete.
190///
191/// If SS refers to a type, this routine checks whether the type is
192/// complete enough (or can be made complete enough) for name lookup
193/// into the DeclContext. A type that is not yet completed can be
194/// considered "complete enough" if it is a class/struct/union/enum
195/// that is currently being defined. Or, if we have a type that names
196/// a class template specialization that is not a complete type, we
197/// will attempt to instantiate that class template.
John McCall77bb1aa2010-05-01 00:40:08 +0000198bool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS,
199 DeclContext *DC) {
200 assert(DC != 0 && "given null context");
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Richard Smithf1c66b42012-03-14 23:13:10 +0000202 TagDecl *tag = dyn_cast<TagDecl>(DC);
Douglas Gregora4e8c2a2010-02-05 04:39:02 +0000203
Richard Smithf1c66b42012-03-14 23:13:10 +0000204 // If this is a dependent type, then we consider it complete.
205 if (!tag || tag->isDependentContext())
206 return false;
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000207
Richard Smithf1c66b42012-03-14 23:13:10 +0000208 // If we're currently defining this type, then lookup into the
209 // type is okay: don't complain that it isn't complete yet.
210 QualType type = Context.getTypeDeclType(tag);
211 const TagType *tagType = type->getAs<TagType>();
212 if (tagType && tagType->isBeingDefined())
213 return false;
John McCall9dc71d22011-07-06 06:57:57 +0000214
Richard Smithf1c66b42012-03-14 23:13:10 +0000215 SourceLocation loc = SS.getLastQualifierNameLoc();
216 if (loc.isInvalid()) loc = SS.getRange().getBegin();
John McCall9dc71d22011-07-06 06:57:57 +0000217
Richard Smithf1c66b42012-03-14 23:13:10 +0000218 // The type must be complete.
Douglas Gregord10099e2012-05-04 16:32:21 +0000219 if (RequireCompleteType(loc, type, diag::err_incomplete_nested_name_spec,
220 SS.getRange())) {
Richard Smithf1c66b42012-03-14 23:13:10 +0000221 SS.SetInvalid(SS.getRange());
222 return true;
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000223 }
224
Richard Smithf1c66b42012-03-14 23:13:10 +0000225 // Fixed enum types are complete, but they aren't valid as scopes
226 // until we see a definition, so awkwardly pull out this special
227 // case.
228 const EnumType *enumType = dyn_cast_or_null<EnumType>(tagType);
229 if (!enumType || enumType->getDecl()->isCompleteDefinition())
230 return false;
231
232 // Try to instantiate the definition, if this is a specialization of an
233 // enumeration temploid.
234 EnumDecl *ED = enumType->getDecl();
235 if (EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) {
236 MemberSpecializationInfo *MSI = ED->getMemberSpecializationInfo();
Richard Smith1af83c42012-03-23 03:33:32 +0000237 if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) {
238 if (InstantiateEnum(loc, ED, Pattern, getTemplateInstantiationArgs(ED),
239 TSK_ImplicitInstantiation)) {
240 SS.SetInvalid(SS.getRange());
241 return true;
242 }
243 return false;
244 }
Richard Smithf1c66b42012-03-14 23:13:10 +0000245 }
246
247 Diag(loc, diag::err_incomplete_nested_name_spec)
248 << type << SS.getRange();
249 SS.SetInvalid(SS.getRange());
250 return true;
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000251}
Cedric Venet3d658642009-02-14 20:20:19 +0000252
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000253bool Sema::ActOnCXXGlobalScopeSpecifier(Scope *S, SourceLocation CCLoc,
254 CXXScopeSpec &SS) {
255 SS.MakeGlobal(Context, CCLoc);
256 return false;
Cedric Venet3d658642009-02-14 20:20:19 +0000257}
258
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000259/// \brief Determines whether the given declaration is an valid acceptable
260/// result for name lookup of a nested-name-specifier.
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000261bool Sema::isAcceptableNestedNameSpecifier(const NamedDecl *SD) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000262 if (!SD)
263 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000265 // Namespace and namespace aliases are fine.
266 if (isa<NamespaceDecl>(SD) || isa<NamespaceAliasDecl>(SD))
267 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000269 if (!isa<TypeDecl>(SD))
270 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Richard Smith6b130222011-10-18 21:39:00 +0000272 // Determine whether we have a class (or, in C++11, an enum) or
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000273 // a typedef thereof. If so, build the nested-name-specifier.
274 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
275 if (T->isDependentType())
276 return true;
Dmitri Gribenko89cf4252013-01-23 17:21:11 +0000277 else if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000278 if (TD->getUnderlyingType()->isRecordType() ||
Richard Smith80ad52f2013-01-02 11:42:31 +0000279 (Context.getLangOpts().CPlusPlus11 &&
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000280 TD->getUnderlyingType()->isEnumeralType()))
281 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000282 } else if (isa<RecordDecl>(SD) ||
Richard Smith80ad52f2013-01-02 11:42:31 +0000283 (Context.getLangOpts().CPlusPlus11 && isa<EnumDecl>(SD)))
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000284 return true;
285
286 return false;
287}
288
Douglas Gregorc68afe22009-09-03 21:38:09 +0000289/// \brief If the given nested-name-specifier begins with a bare identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000290/// (e.g., Base::), perform name lookup for that identifier as a
Douglas Gregorc68afe22009-09-03 21:38:09 +0000291/// nested-name-specifier within the given scope, and return the result of that
292/// name lookup.
293NamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) {
294 if (!S || !NNS)
295 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Douglas Gregorc68afe22009-09-03 21:38:09 +0000297 while (NNS->getPrefix())
298 NNS = NNS->getPrefix();
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Douglas Gregorc68afe22009-09-03 21:38:09 +0000300 if (NNS->getKind() != NestedNameSpecifier::Identifier)
301 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000302
John McCalla24dc2e2009-11-17 02:14:36 +0000303 LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(),
304 LookupNestedNameSpecifierName);
305 LookupName(Found, S);
Douglas Gregorc68afe22009-09-03 21:38:09 +0000306 assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet");
307
John McCall1bcee0a2009-12-02 08:25:40 +0000308 if (!Found.isSingleResult())
309 return 0;
310
311 NamedDecl *Result = Found.getFoundDecl();
Douglas Gregoredc90502010-02-25 04:46:04 +0000312 if (isAcceptableNestedNameSpecifier(Result))
Douglas Gregorc68afe22009-09-03 21:38:09 +0000313 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Douglas Gregorc68afe22009-09-03 21:38:09 +0000315 return 0;
316}
317
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000318bool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS,
Douglas Gregor77549082010-02-24 21:29:12 +0000319 SourceLocation IdLoc,
320 IdentifierInfo &II,
John McCallb3d87482010-08-24 05:47:05 +0000321 ParsedType ObjectTypePtr) {
Douglas Gregor77549082010-02-24 21:29:12 +0000322 QualType ObjectType = GetTypeFromParser(ObjectTypePtr);
323 LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName);
324
325 // Determine where to perform name lookup
326 DeclContext *LookupCtx = 0;
327 bool isDependent = false;
328 if (!ObjectType.isNull()) {
329 // This nested-name-specifier occurs in a member access expression, e.g.,
330 // x->B::f, and we are looking into the type of the object.
331 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
332 LookupCtx = computeDeclContext(ObjectType);
333 isDependent = ObjectType->isDependentType();
334 } else if (SS.isSet()) {
335 // This nested-name-specifier occurs after another nested-name-specifier,
336 // so long into the context associated with the prior nested-name-specifier.
337 LookupCtx = computeDeclContext(SS, false);
338 isDependent = isDependentScopeSpecifier(SS);
339 Found.setContextRange(SS.getRange());
340 }
341
342 if (LookupCtx) {
343 // Perform "qualified" name lookup into the declaration context we
344 // computed, which is either the type of the base of a member access
345 // expression or the declaration context associated with a prior
346 // nested-name-specifier.
347
348 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000349 if (!LookupCtx->isDependentContext() &&
350 RequireCompleteDeclContext(SS, LookupCtx))
Douglas Gregor77549082010-02-24 21:29:12 +0000351 return false;
352
353 LookupQualifiedName(Found, LookupCtx);
354 } else if (isDependent) {
355 return false;
356 } else {
357 LookupName(Found, S);
358 }
359 Found.suppressDiagnostics();
360
361 if (NamedDecl *ND = Found.getAsSingle<NamedDecl>())
362 return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND);
363
364 return false;
365}
366
Kaelyn Uhrain3b4b0472012-01-12 22:32:39 +0000367namespace {
368
369// Callback to only accept typo corrections that can be a valid C++ member
370// intializer: either a non-static field member or a base class.
371class NestedNameSpecifierValidatorCCC : public CorrectionCandidateCallback {
372 public:
373 explicit NestedNameSpecifierValidatorCCC(Sema &SRef)
374 : SRef(SRef) {}
375
376 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
377 return SRef.isAcceptableNestedNameSpecifier(candidate.getCorrectionDecl());
378 }
379
380 private:
381 Sema &SRef;
382};
383
384}
385
Douglas Gregorc68afe22009-09-03 21:38:09 +0000386/// \brief Build a new nested-name-specifier for "identifier::", as described
387/// by ActOnCXXNestedNameSpecifier.
388///
389/// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in
390/// that it contains an extra parameter \p ScopeLookupResult, which provides
391/// the result of name lookup within the scope of the nested-name-specifier
Douglas Gregora6e51992009-12-30 16:01:52 +0000392/// that was computed at template definition time.
Chris Lattner46646492009-12-07 01:36:53 +0000393///
394/// If ErrorRecoveryLookup is true, then this call is used to improve error
395/// recovery. This means that it should not emit diagnostics, it should
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000396/// just return true on failure. It also means it should only return a valid
Chris Lattner46646492009-12-07 01:36:53 +0000397/// scope if it *knows* that the result is correct. It should not return in a
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000398/// dependent context, for example. Nor will it extend \p SS with the scope
399/// specifier.
400bool Sema::BuildCXXNestedNameSpecifier(Scope *S,
401 IdentifierInfo &Identifier,
402 SourceLocation IdentifierLoc,
403 SourceLocation CCLoc,
404 QualType ObjectType,
405 bool EnteringContext,
406 CXXScopeSpec &SS,
407 NamedDecl *ScopeLookupResult,
408 bool ErrorRecoveryLookup) {
409 LookupResult Found(*this, &Identifier, IdentifierLoc,
410 LookupNestedNameSpecifierName);
John McCalla24dc2e2009-11-17 02:14:36 +0000411
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000412 // Determine where to perform name lookup
413 DeclContext *LookupCtx = 0;
414 bool isDependent = false;
Douglas Gregorc68afe22009-09-03 21:38:09 +0000415 if (!ObjectType.isNull()) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000416 // This nested-name-specifier occurs in a member access expression, e.g.,
417 // x->B::f, and we are looking into the type of the object.
418 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000419 LookupCtx = computeDeclContext(ObjectType);
420 isDependent = ObjectType->isDependentType();
421 } else if (SS.isSet()) {
422 // This nested-name-specifier occurs after another nested-name-specifier,
Richard Smith3e4c6c42011-05-05 21:57:07 +0000423 // so look into the context associated with the prior nested-name-specifier.
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000424 LookupCtx = computeDeclContext(SS, EnteringContext);
425 isDependent = isDependentScopeSpecifier(SS);
John McCalla24dc2e2009-11-17 02:14:36 +0000426 Found.setContextRange(SS.getRange());
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000427 }
Mike Stump1eb44332009-09-09 15:08:12 +0000428
John McCalla24dc2e2009-11-17 02:14:36 +0000429
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000430 bool ObjectTypeSearchedInScope = false;
431 if (LookupCtx) {
Mike Stump1eb44332009-09-09 15:08:12 +0000432 // Perform "qualified" name lookup into the declaration context we
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000433 // computed, which is either the type of the base of a member access
Mike Stump1eb44332009-09-09 15:08:12 +0000434 // expression or the declaration context associated with a prior
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000435 // nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000437 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000438 if (!LookupCtx->isDependentContext() &&
439 RequireCompleteDeclContext(SS, LookupCtx))
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000440 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000441
John McCalla24dc2e2009-11-17 02:14:36 +0000442 LookupQualifiedName(Found, LookupCtx);
Mike Stump1eb44332009-09-09 15:08:12 +0000443
John McCalla24dc2e2009-11-17 02:14:36 +0000444 if (!ObjectType.isNull() && Found.empty()) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000445 // C++ [basic.lookup.classref]p4:
446 // If the id-expression in a class member access is a qualified-id of
Mike Stump1eb44332009-09-09 15:08:12 +0000447 // the form
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000448 //
449 // class-name-or-namespace-name::...
450 //
Mike Stump1eb44332009-09-09 15:08:12 +0000451 // the class-name-or-namespace-name following the . or -> operator is
452 // looked up both in the context of the entire postfix-expression and in
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000453 // the scope of the class of the object expression. If the name is found
Mike Stump1eb44332009-09-09 15:08:12 +0000454 // only in the scope of the class of the object expression, the name
455 // shall refer to a class-name. If the name is found only in the
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000456 // context of the entire postfix-expression, the name shall refer to a
457 // class-name or namespace-name. [...]
458 //
459 // Qualified name lookup into a class will not find a namespace-name,
Douglas Gregor714c9922011-05-15 17:27:27 +0000460 // so we do not need to diagnose that case specifically. However,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000461 // this qualified name lookup may find nothing. In that case, perform
Mike Stump1eb44332009-09-09 15:08:12 +0000462 // unqualified name lookup in the given scope (if available) or
Douglas Gregorc68afe22009-09-03 21:38:09 +0000463 // reconstruct the result from when name lookup was performed at template
464 // definition time.
465 if (S)
John McCalla24dc2e2009-11-17 02:14:36 +0000466 LookupName(Found, S);
John McCallf36e02d2009-10-09 21:13:30 +0000467 else if (ScopeLookupResult)
468 Found.addDecl(ScopeLookupResult);
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000470 ObjectTypeSearchedInScope = true;
471 }
Douglas Gregorac7cbd82010-07-28 14:49:07 +0000472 } else if (!isDependent) {
473 // Perform unqualified name lookup in the current scope.
474 LookupName(Found, S);
475 }
476
477 // If we performed lookup into a dependent context and did not find anything,
478 // that's fine: just build a dependent nested-name-specifier.
479 if (Found.empty() && isDependent &&
480 !(LookupCtx && LookupCtx->isRecord() &&
481 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
482 !cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()))) {
Chris Lattner46646492009-12-07 01:36:53 +0000483 // Don't speculate if we're just trying to improve error recovery.
484 if (ErrorRecoveryLookup)
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000485 return true;
Chris Lattner46646492009-12-07 01:36:53 +0000486
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000487 // We were not able to compute the declaration context for a dependent
Mike Stump1eb44332009-09-09 15:08:12 +0000488 // base object type or prior nested-name-specifier, so this
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000489 // nested-name-specifier refers to an unknown specialization. Just build
490 // a dependent nested-name-specifier.
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000491 SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc);
492 return false;
Douglas Gregorac7cbd82010-07-28 14:49:07 +0000493 }
494
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000495 // FIXME: Deal with ambiguities cleanly.
Douglas Gregor175a6562009-12-31 08:26:35 +0000496
497 if (Found.empty() && !ErrorRecoveryLookup) {
498 // We haven't found anything, and we're not recovering from a
499 // different kind of error, so look for typos.
500 DeclarationName Name = Found.getLookupName();
Kaelyn Uhrain3b4b0472012-01-12 22:32:39 +0000501 NestedNameSpecifierValidatorCCC Validator(*this);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000502 TypoCorrection Corrected;
503 Found.clear();
504 if ((Corrected = CorrectTypo(Found.getLookupNameInfo(),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000505 Found.getLookupKind(), S, &SS, Validator,
Kaelyn Uhrain3b4b0472012-01-12 22:32:39 +0000506 LookupCtx, EnteringContext))) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000507 std::string CorrectedStr(Corrected.getAsString(getLangOpts()));
508 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOpts()));
Douglas Gregor175a6562009-12-31 08:26:35 +0000509 if (LookupCtx)
510 Diag(Found.getNameLoc(), diag::err_no_member_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000511 << Name << LookupCtx << CorrectedQuotedStr << SS.getRange()
David Blaikie6952c012012-10-12 20:00:44 +0000512 << FixItHint::CreateReplacement(Corrected.getCorrectionRange(),
513 CorrectedStr);
Douglas Gregor175a6562009-12-31 08:26:35 +0000514 else
515 Diag(Found.getNameLoc(), diag::err_undeclared_var_use_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000516 << Name << CorrectedQuotedStr
517 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000518
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000519 if (NamedDecl *ND = Corrected.getCorrectionDecl()) {
520 Diag(ND->getLocation(), diag::note_previous_decl) << CorrectedQuotedStr;
521 Found.addDecl(ND);
522 }
523 Found.setLookupName(Corrected.getCorrection());
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000524 } else {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000525 Found.setLookupName(&Identifier);
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000526 }
Douglas Gregor175a6562009-12-31 08:26:35 +0000527 }
528
John McCall1bcee0a2009-12-02 08:25:40 +0000529 NamedDecl *SD = Found.getAsSingle<NamedDecl>();
Douglas Gregoredc90502010-02-25 04:46:04 +0000530 if (isAcceptableNestedNameSpecifier(SD)) {
Douglas Gregor05e60762012-05-01 20:23:02 +0000531 if (!ObjectType.isNull() && !ObjectTypeSearchedInScope &&
Richard Smith80ad52f2013-01-02 11:42:31 +0000532 !getLangOpts().CPlusPlus11) {
Douglas Gregor05e60762012-05-01 20:23:02 +0000533 // C++03 [basic.lookup.classref]p4:
Mike Stump1eb44332009-09-09 15:08:12 +0000534 // [...] If the name is found in both contexts, the
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000535 // class-name-or-namespace-name shall refer to the same entity.
536 //
537 // We already found the name in the scope of the object. Now, look
538 // into the current scope (the scope of the postfix-expression) to
Douglas Gregorc68afe22009-09-03 21:38:09 +0000539 // see if we can find the same name there. As above, if there is no
540 // scope, reconstruct the result from the template instantiation itself.
Douglas Gregor05e60762012-05-01 20:23:02 +0000541 //
542 // Note that C++11 does *not* perform this redundant lookup.
John McCallf36e02d2009-10-09 21:13:30 +0000543 NamedDecl *OuterDecl;
544 if (S) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000545 LookupResult FoundOuter(*this, &Identifier, IdentifierLoc,
546 LookupNestedNameSpecifierName);
John McCalla24dc2e2009-11-17 02:14:36 +0000547 LookupName(FoundOuter, S);
John McCall1bcee0a2009-12-02 08:25:40 +0000548 OuterDecl = FoundOuter.getAsSingle<NamedDecl>();
John McCallf36e02d2009-10-09 21:13:30 +0000549 } else
550 OuterDecl = ScopeLookupResult;
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Douglas Gregoredc90502010-02-25 04:46:04 +0000552 if (isAcceptableNestedNameSpecifier(OuterDecl) &&
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000553 OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() &&
554 (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) ||
555 !Context.hasSameType(
Douglas Gregorc68afe22009-09-03 21:38:09 +0000556 Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)),
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000557 Context.getTypeDeclType(cast<TypeDecl>(SD))))) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000558 if (ErrorRecoveryLookup)
559 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000561 Diag(IdentifierLoc,
562 diag::err_nested_name_member_ref_lookup_ambiguous)
563 << &Identifier;
564 Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type)
565 << ObjectType;
566 Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope);
567
568 // Fall through so that we'll pick the name we found in the object
569 // type, since that's probably what the user wanted anyway.
570 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000571 }
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000573 // If we're just performing this lookup for error-recovery purposes,
574 // don't extend the nested-name-specifier. Just return now.
575 if (ErrorRecoveryLookup)
576 return false;
577
578 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) {
579 SS.Extend(Context, Namespace, IdentifierLoc, CCLoc);
580 return false;
581 }
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000583 if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) {
Douglas Gregor14aba762011-02-24 02:36:08 +0000584 SS.Extend(Context, Alias, IdentifierLoc, CCLoc);
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000585 return false;
586 }
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000588 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000589 TypeLocBuilder TLB;
590 if (isa<InjectedClassNameType>(T)) {
591 InjectedClassNameTypeLoc InjectedTL
592 = TLB.push<InjectedClassNameTypeLoc>(T);
593 InjectedTL.setNameLoc(IdentifierLoc);
Douglas Gregorbd61e342011-05-04 23:05:40 +0000594 } else if (isa<RecordType>(T)) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000595 RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T);
596 RecordTL.setNameLoc(IdentifierLoc);
Douglas Gregorbd61e342011-05-04 23:05:40 +0000597 } else if (isa<TypedefType>(T)) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000598 TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T);
599 TypedefTL.setNameLoc(IdentifierLoc);
Douglas Gregorbd61e342011-05-04 23:05:40 +0000600 } else if (isa<EnumType>(T)) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000601 EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T);
602 EnumTL.setNameLoc(IdentifierLoc);
Douglas Gregorbd61e342011-05-04 23:05:40 +0000603 } else if (isa<TemplateTypeParmType>(T)) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000604 TemplateTypeParmTypeLoc TemplateTypeTL
605 = TLB.push<TemplateTypeParmTypeLoc>(T);
606 TemplateTypeTL.setNameLoc(IdentifierLoc);
Douglas Gregorbd61e342011-05-04 23:05:40 +0000607 } else if (isa<UnresolvedUsingType>(T)) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000608 UnresolvedUsingTypeLoc UnresolvedTL
609 = TLB.push<UnresolvedUsingTypeLoc>(T);
610 UnresolvedTL.setNameLoc(IdentifierLoc);
Douglas Gregorbd61e342011-05-04 23:05:40 +0000611 } else if (isa<SubstTemplateTypeParmType>(T)) {
612 SubstTemplateTypeParmTypeLoc TL
613 = TLB.push<SubstTemplateTypeParmTypeLoc>(T);
614 TL.setNameLoc(IdentifierLoc);
615 } else if (isa<SubstTemplateTypeParmPackType>(T)) {
616 SubstTemplateTypeParmPackTypeLoc TL
617 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(T);
618 TL.setNameLoc(IdentifierLoc);
619 } else {
620 llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier");
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000621 }
622
Richard Smith95aafb22011-10-20 03:28:47 +0000623 if (T->isEnumeralType())
624 Diag(IdentifierLoc, diag::warn_cxx98_compat_enum_nested_name_spec);
625
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000626 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
627 CCLoc);
628 return false;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000629 }
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Chris Lattner46646492009-12-07 01:36:53 +0000631 // Otherwise, we have an error case. If we don't want diagnostics, just
632 // return an error now.
633 if (ErrorRecoveryLookup)
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000634 return true;
Chris Lattner46646492009-12-07 01:36:53 +0000635
Cedric Venet3d658642009-02-14 20:20:19 +0000636 // If we didn't find anything during our lookup, try again with
637 // ordinary name lookup, which can help us produce better error
638 // messages.
John McCall1bcee0a2009-12-02 08:25:40 +0000639 if (Found.empty()) {
John McCalla24dc2e2009-11-17 02:14:36 +0000640 Found.clear(LookupOrdinaryName);
641 LookupName(Found, S);
John McCallf36e02d2009-10-09 21:13:30 +0000642 }
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Francois Pichetdfb6ae12011-07-27 01:05:24 +0000644 // In Microsoft mode, if we are within a templated function and we can't
645 // resolve Identifier, then extend the SS with Identifier. This will have
646 // the effect of resolving Identifier during template instantiation.
647 // The goal is to be able to resolve a function call whose
648 // nested-name-specifier is located inside a dependent base class.
649 // Example:
650 //
651 // class C {
652 // public:
653 // static void foo2() { }
654 // };
655 // template <class T> class A { public: typedef C D; };
656 //
657 // template <class T> class B : public A<T> {
658 // public:
659 // void foo() { D::foo2(); }
660 // };
David Blaikie4e4d0842012-03-11 07:00:24 +0000661 if (getLangOpts().MicrosoftExt) {
Francois Pichetdfb6ae12011-07-27 01:05:24 +0000662 DeclContext *DC = LookupCtx ? LookupCtx : CurContext;
663 if (DC->isDependentContext() && DC->isFunctionOrMethod()) {
664 SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc);
665 return false;
666 }
667 }
668
Cedric Venet3d658642009-02-14 20:20:19 +0000669 unsigned DiagID;
John McCall1bcee0a2009-12-02 08:25:40 +0000670 if (!Found.empty())
Cedric Venet3d658642009-02-14 20:20:19 +0000671 DiagID = diag::err_expected_class_or_namespace;
Anders Carlssona31d5f72009-08-30 07:09:50 +0000672 else if (SS.isSet()) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000673 Diag(IdentifierLoc, diag::err_no_member)
674 << &Identifier << LookupCtx << SS.getRange();
675 return true;
Anders Carlssona31d5f72009-08-30 07:09:50 +0000676 } else
Cedric Venet3d658642009-02-14 20:20:19 +0000677 DiagID = diag::err_undeclared_var_use;
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Cedric Venet3d658642009-02-14 20:20:19 +0000679 if (SS.isSet())
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000680 Diag(IdentifierLoc, DiagID) << &Identifier << SS.getRange();
Cedric Venet3d658642009-02-14 20:20:19 +0000681 else
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000682 Diag(IdentifierLoc, DiagID) << &Identifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000684 return true;
Cedric Venet3d658642009-02-14 20:20:19 +0000685}
686
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000687bool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
688 IdentifierInfo &Identifier,
689 SourceLocation IdentifierLoc,
690 SourceLocation CCLoc,
691 ParsedType ObjectType,
692 bool EnteringContext,
693 CXXScopeSpec &SS) {
694 if (SS.isInvalid())
695 return true;
696
697 return BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, CCLoc,
698 GetTypeFromParser(ObjectType),
699 EnteringContext, SS,
700 /*ScopeLookupResult=*/0, false);
Chris Lattner46646492009-12-07 01:36:53 +0000701}
702
David Blaikie42d6d0c2011-12-04 05:04:18 +0000703bool Sema::ActOnCXXNestedNameSpecifierDecltype(CXXScopeSpec &SS,
704 const DeclSpec &DS,
705 SourceLocation ColonColonLoc) {
706 if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error)
707 return true;
708
709 assert(DS.getTypeSpecType() == DeclSpec::TST_decltype);
710
711 QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc());
712 if (!T->isDependentType() && !T->getAs<TagType>()) {
713 Diag(DS.getTypeSpecTypeLoc(), diag::err_expected_class)
David Blaikie4e4d0842012-03-11 07:00:24 +0000714 << T << getLangOpts().CPlusPlus;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000715 return true;
716 }
717
718 TypeLocBuilder TLB;
719 DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T);
720 DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc());
721 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
722 ColonColonLoc);
723 return false;
724}
725
Chris Lattner46646492009-12-07 01:36:53 +0000726/// IsInvalidUnlessNestedName - This method is used for error recovery
727/// purposes to determine whether the specified identifier is only valid as
728/// a nested name specifier, for example a namespace name. It is
729/// conservatively correct to always return false from this method.
730///
731/// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000732bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS,
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000733 IdentifierInfo &Identifier,
734 SourceLocation IdentifierLoc,
735 SourceLocation ColonLoc,
736 ParsedType ObjectType,
Chris Lattner46646492009-12-07 01:36:53 +0000737 bool EnteringContext) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000738 if (SS.isInvalid())
739 return false;
740
741 return !BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, ColonLoc,
742 GetTypeFromParser(ObjectType),
743 EnteringContext, SS,
744 /*ScopeLookupResult=*/0, true);
Douglas Gregorc68afe22009-09-03 21:38:09 +0000745}
746
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000747bool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000748 CXXScopeSpec &SS,
749 SourceLocation TemplateKWLoc,
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000750 TemplateTy Template,
751 SourceLocation TemplateNameLoc,
752 SourceLocation LAngleLoc,
753 ASTTemplateArgsPtr TemplateArgsIn,
754 SourceLocation RAngleLoc,
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000755 SourceLocation CCLoc,
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000756 bool EnteringContext) {
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000757 if (SS.isInvalid())
758 return true;
759
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000760 // Translate the parser's template argument list in our AST format.
761 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
762 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
763
764 if (DependentTemplateName *DTN = Template.get().getAsDependentTemplateName()){
765 // Handle a dependent template specialization for which we cannot resolve
766 // the template name.
Richard Smithc2e935f2013-03-26 00:54:11 +0000767 assert(DTN->getQualifier() == SS.getScopeRep());
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000768 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000769 DTN->getQualifier(),
770 DTN->getIdentifier(),
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000771 TemplateArgs);
772
773 // Create source-location information for this type.
774 TypeLocBuilder Builder;
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000775 DependentTemplateSpecializationTypeLoc SpecTL
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000776 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000777 SpecTL.setElaboratedKeywordLoc(SourceLocation());
778 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara66581d42012-02-06 22:45:07 +0000779 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000780 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000781 SpecTL.setLAngleLoc(LAngleLoc);
782 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000783 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
784 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
785
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000786 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000787 CCLoc);
788 return false;
789 }
790
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +0000791
792 if (Template.get().getAsOverloadedTemplate() ||
793 isa<FunctionTemplateDecl>(Template.get().getAsTemplateDecl())) {
794 SourceRange R(TemplateNameLoc, RAngleLoc);
795 if (SS.getRange().isValid())
796 R.setBegin(SS.getRange().getBegin());
797
798 Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier)
799 << Template.get() << R;
800 NoteAllFoundTemplates(Template.get());
801 return true;
802 }
803
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000804 // We were able to resolve the template name to an actual template.
805 // Build an appropriate nested-name-specifier.
806 QualType T = CheckTemplateIdType(Template.get(), TemplateNameLoc,
807 TemplateArgs);
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000808 if (T.isNull())
809 return true;
810
Richard Smith3e4c6c42011-05-05 21:57:07 +0000811 // Alias template specializations can produce types which are not valid
812 // nested name specifiers.
813 if (!T->isDependentType() && !T->getAs<TagType>()) {
814 Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T;
815 NoteAllFoundTemplates(Template.get());
816 return true;
817 }
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000818
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000819 // Provide source-location information for the template specialization type.
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000820 TypeLocBuilder Builder;
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000821 TemplateSpecializationTypeLoc SpecTL
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000822 = Builder.push<TemplateSpecializationTypeLoc>(T);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000823 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
824 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000825 SpecTL.setLAngleLoc(LAngleLoc);
826 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000827 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
828 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
829
830
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000831 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
Douglas Gregoraa2187d2011-02-28 00:04:36 +0000832 CCLoc);
Douglas Gregor2e4c34a2011-02-24 00:17:56 +0000833 return false;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000834}
835
Douglas Gregorc34348a2011-02-24 17:54:50 +0000836namespace {
837 /// \brief A structure that stores a nested-name-specifier annotation,
838 /// including both the nested-name-specifier
839 struct NestedNameSpecifierAnnotation {
840 NestedNameSpecifier *NNS;
841 };
842}
843
844void *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) {
845 if (SS.isEmpty() || SS.isInvalid())
846 return 0;
847
848 void *Mem = Context.Allocate((sizeof(NestedNameSpecifierAnnotation) +
849 SS.location_size()),
850 llvm::alignOf<NestedNameSpecifierAnnotation>());
851 NestedNameSpecifierAnnotation *Annotation
852 = new (Mem) NestedNameSpecifierAnnotation;
853 Annotation->NNS = SS.getScopeRep();
854 memcpy(Annotation + 1, SS.location_data(), SS.location_size());
855 return Annotation;
856}
857
858void Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr,
859 SourceRange AnnotationRange,
860 CXXScopeSpec &SS) {
861 if (!AnnotationPtr) {
862 SS.SetInvalid(AnnotationRange);
863 return;
864 }
865
866 NestedNameSpecifierAnnotation *Annotation
867 = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr);
868 SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1));
869}
870
John McCalle7e278b2009-12-11 20:04:54 +0000871bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
872 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
873
Richard Smithc2e935f2013-03-26 00:54:11 +0000874 NestedNameSpecifier *Qualifier = SS.getScopeRep();
John McCalle7e278b2009-12-11 20:04:54 +0000875
876 // There are only two places a well-formed program may qualify a
877 // declarator: first, when defining a namespace or class member
878 // out-of-line, and second, when naming an explicitly-qualified
879 // friend function. The latter case is governed by
880 // C++03 [basic.lookup.unqual]p10:
881 // In a friend declaration naming a member function, a name used
882 // in the function declarator and not part of a template-argument
883 // in a template-id is first looked up in the scope of the member
884 // function's class. If it is not found, or if the name is part of
885 // a template-argument in a template-id, the look up is as
886 // described for unqualified names in the definition of the class
887 // granting friendship.
888 // i.e. we don't push a scope unless it's a class member.
889
890 switch (Qualifier->getKind()) {
891 case NestedNameSpecifier::Global:
892 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +0000893 case NestedNameSpecifier::NamespaceAlias:
John McCalle7e278b2009-12-11 20:04:54 +0000894 // These are always namespace scopes. We never want to enter a
895 // namespace scope from anything but a file context.
Sebastian Redl7a126a42010-08-31 00:36:30 +0000896 return CurContext->getRedeclContext()->isFileContext();
John McCalle7e278b2009-12-11 20:04:54 +0000897
898 case NestedNameSpecifier::Identifier:
899 case NestedNameSpecifier::TypeSpec:
900 case NestedNameSpecifier::TypeSpecWithTemplate:
901 // These are never namespace scopes.
902 return true;
903 }
904
David Blaikie7530c032012-01-17 06:56:22 +0000905 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
John McCalle7e278b2009-12-11 20:04:54 +0000906}
907
Cedric Venet3d658642009-02-14 20:20:19 +0000908/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
909/// scope or nested-name-specifier) is parsed, part of a declarator-id.
910/// After this method is called, according to [C++ 3.4.3p3], names should be
911/// looked up in the declarator-id's scope, until the declarator is parsed and
912/// ActOnCXXExitDeclaratorScope is called.
913/// The 'SS' should be a non-empty valid CXXScopeSpec.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000914bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) {
Cedric Venet3d658642009-02-14 20:20:19 +0000915 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
John McCall7a1dc562009-12-19 10:49:29 +0000916
917 if (SS.isInvalid()) return true;
918
919 DeclContext *DC = computeDeclContext(SS, true);
920 if (!DC) return true;
921
922 // Before we enter a declarator's context, we need to make sure that
923 // it is a complete declaration context.
John McCall77bb1aa2010-05-01 00:40:08 +0000924 if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC))
John McCall7a1dc562009-12-19 10:49:29 +0000925 return true;
926
927 EnterDeclaratorContext(S, DC);
John McCall31f17ec2010-04-27 00:57:59 +0000928
929 // Rebuild the nested name specifier for the new scope.
930 if (DC->isDependentContext())
931 RebuildNestedNameSpecifierInCurrentInstantiation(SS);
932
Douglas Gregor7dfd0fb2009-09-24 23:39:01 +0000933 return false;
Cedric Venet3d658642009-02-14 20:20:19 +0000934}
935
936/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
937/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
938/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
939/// Used to indicate that names should revert to being looked up in the
940/// defining scope.
941void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
942 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
Douglas Gregordacd4342009-08-26 00:04:55 +0000943 if (SS.isInvalid())
944 return;
John McCall7a1dc562009-12-19 10:49:29 +0000945 assert(!SS.isInvalid() && computeDeclContext(SS, true) &&
946 "exiting declarator scope we never really entered");
947 ExitDeclaratorContext(S);
Cedric Venet3d658642009-02-14 20:20:19 +0000948}