blob: 5e6c27b15d57d3be88080a8c6c479a88e7d5cb89 [file] [log] [blame]
Cedric Venet084381332009-02-14 20:20:19 +00001//===--- SemaCXXScopeSpec.cpp - Semantic Analysis for C++ scope specifiers-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements C++ semantic analysis for scope specifiers.
11//
12//===----------------------------------------------------------------------===//
13
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000015#include "clang/Sema/Lookup.h"
Richard Smith4b38ded2012-03-14 23:13:10 +000016#include "clang/Sema/Template.h"
Cedric Venet084381332009-02-14 20:20:19 +000017#include "clang/AST/ASTContext.h"
Douglas Gregorc9f9b862009-05-11 19:58:34 +000018#include "clang/AST/DeclTemplate.h"
Douglas Gregord8061562009-08-06 03:17:00 +000019#include "clang/AST/ExprCXX.h"
Douglas Gregor52537682009-03-19 00:18:19 +000020#include "clang/AST/NestedNameSpecifier.h"
Anders Carlssond624e162009-08-26 23:45:07 +000021#include "clang/Basic/PartialDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/DeclSpec.h"
Douglas Gregor90c99722011-02-24 00:17:56 +000023#include "TypeLocBuilder.h"
Cedric Venet084381332009-02-14 20:20:19 +000024#include "llvm/ADT/STLExtras.h"
Douglas Gregor168190d2009-07-22 00:28:09 +000025#include "llvm/Support/raw_ostream.h"
Cedric Venet084381332009-02-14 20:20:19 +000026using namespace clang;
27
Douglas Gregor41127182009-11-04 22:49:18 +000028/// \brief Find the current instantiation that associated with the given type.
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000029static CXXRecordDecl *getCurrentInstantiationOf(QualType T,
30 DeclContext *CurContext) {
Douglas Gregor41127182009-11-04 22:49:18 +000031 if (T.isNull())
32 return 0;
John McCall2408e322010-04-27 00:57:59 +000033
34 const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000035 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
36 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
37 if (!T->isDependentType())
38 return Record;
39
40 // This may be a member of a class template or class template partial
41 // specialization. If it's part of the current semantic context, then it's
42 // an injected-class-name;
43 for (; !CurContext->isFileContext(); CurContext = CurContext->getParent())
44 if (CurContext->Equals(Record))
45 return Record;
46
47 return 0;
48 } else if (isa<InjectedClassNameType>(Ty))
John McCall2408e322010-04-27 00:57:59 +000049 return cast<InjectedClassNameType>(Ty)->getDecl();
50 else
51 return 0;
Douglas Gregor41127182009-11-04 22:49:18 +000052}
53
Douglas Gregorb7bfe792009-09-02 22:59:36 +000054/// \brief Compute the DeclContext that is associated with the given type.
55///
56/// \param T the type for which we are attempting to find a DeclContext.
57///
Mike Stump11289f42009-09-09 15:08:12 +000058/// \returns the declaration context represented by the type T,
Douglas Gregorb7bfe792009-09-02 22:59:36 +000059/// or NULL if the declaration context cannot be computed (e.g., because it is
60/// dependent and not the current instantiation).
61DeclContext *Sema::computeDeclContext(QualType T) {
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000062 if (!T->isDependentType())
63 if (const TagType *Tag = T->getAs<TagType>())
64 return Tag->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +000065
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000066 return ::getCurrentInstantiationOf(T, CurContext);
Douglas Gregorb7bfe792009-09-02 22:59:36 +000067}
68
Douglas Gregor52537682009-03-19 00:18:19 +000069/// \brief Compute the DeclContext that is associated with the given
70/// scope specifier.
Douglas Gregord8d297c2009-07-21 23:53:31 +000071///
72/// \param SS the C++ scope specifier as it appears in the source
73///
74/// \param EnteringContext when true, we will be entering the context of
75/// this scope specifier, so we can retrieve the declaration context of a
76/// class template or class template partial specialization even if it is
77/// not the current instantiation.
78///
79/// \returns the declaration context represented by the scope specifier @p SS,
80/// or NULL if the declaration context cannot be computed (e.g., because it is
81/// dependent and not the current instantiation).
82DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS,
83 bool EnteringContext) {
Douglas Gregor52537682009-03-19 00:18:19 +000084 if (!SS.isSet() || SS.isInvalid())
Douglas Gregor6bfde492009-03-18 00:36:05 +000085 return 0;
Douglas Gregor6bfde492009-03-18 00:36:05 +000086
Mike Stump11289f42009-09-09 15:08:12 +000087 NestedNameSpecifier *NNS
Douglas Gregorc23500e2009-03-26 23:56:24 +000088 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorc9f9b862009-05-11 19:58:34 +000089 if (NNS->isDependent()) {
90 // If this nested-name-specifier refers to the current
91 // instantiation, return its DeclContext.
92 if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS))
93 return Record;
Mike Stump11289f42009-09-09 15:08:12 +000094
Douglas Gregord8d297c2009-07-21 23:53:31 +000095 if (EnteringContext) {
John McCalle78aac42010-03-10 03:28:59 +000096 const Type *NNSType = NNS->getAsType();
97 if (!NNSType) {
Richard Smith3f1b5d02011-05-05 21:57:07 +000098 return 0;
99 }
100
101 // Look through type alias templates, per C++0x [temp.dep.type]p1.
102 NNSType = Context.getCanonicalType(NNSType);
103 if (const TemplateSpecializationType *SpecType
104 = NNSType->getAs<TemplateSpecializationType>()) {
Douglas Gregore861bac2009-08-25 22:51:20 +0000105 // We are entering the context of the nested name specifier, so try to
106 // match the nested name specifier to either a primary class template
107 // or a class template partial specialization.
Mike Stump11289f42009-09-09 15:08:12 +0000108 if (ClassTemplateDecl *ClassTemplate
Douglas Gregord8d297c2009-07-21 23:53:31 +0000109 = dyn_cast_or_null<ClassTemplateDecl>(
110 SpecType->getTemplateName().getAsTemplateDecl())) {
Douglas Gregor15301382009-07-30 17:40:51 +0000111 QualType ContextType
112 = Context.getCanonicalType(QualType(SpecType, 0));
113
Douglas Gregord8d297c2009-07-21 23:53:31 +0000114 // If the type of the nested name specifier is the same as the
115 // injected class name of the named class template, we're entering
116 // into that class template definition.
John McCalle78aac42010-03-10 03:28:59 +0000117 QualType Injected
Douglas Gregor9961ce92010-07-08 18:37:38 +0000118 = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregor15301382009-07-30 17:40:51 +0000119 if (Context.hasSameType(Injected, ContextType))
Douglas Gregord8d297c2009-07-21 23:53:31 +0000120 return ClassTemplate->getTemplatedDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000121
Douglas Gregor15301382009-07-30 17:40:51 +0000122 // If the type of the nested name specifier is the same as the
123 // type of one of the class template's class template partial
124 // specializations, we're entering into the definition of that
125 // class template partial specialization.
126 if (ClassTemplatePartialSpecializationDecl *PartialSpec
127 = ClassTemplate->findPartialSpecialization(ContextType))
128 return PartialSpec;
Douglas Gregord8d297c2009-07-21 23:53:31 +0000129 }
John McCalle78aac42010-03-10 03:28:59 +0000130 } else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) {
Douglas Gregore861bac2009-08-25 22:51:20 +0000131 // The nested name specifier refers to a member of a class template.
132 return RecordT->getDecl();
Douglas Gregord8d297c2009-07-21 23:53:31 +0000133 }
134 }
Mike Stump11289f42009-09-09 15:08:12 +0000135
Douglas Gregord8d297c2009-07-21 23:53:31 +0000136 return 0;
Douglas Gregorc9f9b862009-05-11 19:58:34 +0000137 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000138
139 switch (NNS->getKind()) {
140 case NestedNameSpecifier::Identifier:
David Blaikie83d382b2011-09-23 05:06:16 +0000141 llvm_unreachable("Dependent nested-name-specifier has no DeclContext");
Douglas Gregorf21eb492009-03-26 23:50:42 +0000142
143 case NestedNameSpecifier::Namespace:
144 return NNS->getAsNamespace();
145
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000146 case NestedNameSpecifier::NamespaceAlias:
147 return NNS->getAsNamespaceAlias()->getNamespace();
148
Douglas Gregorf21eb492009-03-26 23:50:42 +0000149 case NestedNameSpecifier::TypeSpec:
150 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000151 const TagType *Tag = NNS->getAsType()->getAs<TagType>();
152 assert(Tag && "Non-tag type in nested-name-specifier");
153 return Tag->getDecl();
David Blaikie8a40f702012-01-17 06:56:22 +0000154 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000155
156 case NestedNameSpecifier::Global:
157 return Context.getTranslationUnitDecl();
158 }
159
David Blaikie8a40f702012-01-17 06:56:22 +0000160 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
Douglas Gregor6bfde492009-03-18 00:36:05 +0000161}
162
Douglas Gregor90a1a652009-03-19 17:26:29 +0000163bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) {
164 if (!SS.isSet() || SS.isInvalid())
165 return false;
166
Mike Stump11289f42009-09-09 15:08:12 +0000167 NestedNameSpecifier *NNS
Douglas Gregorc23500e2009-03-26 23:56:24 +0000168 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorf21eb492009-03-26 23:50:42 +0000169 return NNS->isDependent();
Douglas Gregor90a1a652009-03-19 17:26:29 +0000170}
171
Douglas Gregorc9f9b862009-05-11 19:58:34 +0000172// \brief Determine whether this C++ scope specifier refers to an
173// unknown specialization, i.e., a dependent type that is not the
174// current instantiation.
175bool Sema::isUnknownSpecialization(const CXXScopeSpec &SS) {
176 if (!isDependentScopeSpecifier(SS))
177 return false;
178
Mike Stump11289f42009-09-09 15:08:12 +0000179 NestedNameSpecifier *NNS
Douglas Gregorc9f9b862009-05-11 19:58:34 +0000180 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
181 return getCurrentInstantiationOf(NNS) == 0;
182}
183
184/// \brief If the given nested name specifier refers to the current
185/// instantiation, return the declaration that corresponds to that
186/// current instantiation (C++0x [temp.dep.type]p1).
187///
188/// \param NNS a dependent nested name specifier.
189CXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000190 assert(getLangOpts().CPlusPlus && "Only callable in C++");
Douglas Gregorc9f9b862009-05-11 19:58:34 +0000191 assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed");
192
Douglas Gregord8d297c2009-07-21 23:53:31 +0000193 if (!NNS->getAsType())
194 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000195
Douglas Gregorb9a955d2009-07-31 18:32:42 +0000196 QualType T = QualType(NNS->getAsType(), 0);
Douglas Gregorbf2b26d2011-02-19 19:24:40 +0000197 return ::getCurrentInstantiationOf(T, CurContext);
Douglas Gregorc9f9b862009-05-11 19:58:34 +0000198}
199
Douglas Gregor26897462009-03-11 16:48:53 +0000200/// \brief Require that the context specified by SS be complete.
201///
202/// If SS refers to a type, this routine checks whether the type is
203/// complete enough (or can be made complete enough) for name lookup
204/// into the DeclContext. A type that is not yet completed can be
205/// considered "complete enough" if it is a class/struct/union/enum
206/// that is currently being defined. Or, if we have a type that names
207/// a class template specialization that is not a complete type, we
208/// will attempt to instantiate that class template.
John McCall0b66eb32010-05-01 00:40:08 +0000209bool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS,
210 DeclContext *DC) {
211 assert(DC != 0 && "given null context");
Mike Stump11289f42009-09-09 15:08:12 +0000212
Richard Smith4b38ded2012-03-14 23:13:10 +0000213 TagDecl *tag = dyn_cast<TagDecl>(DC);
Douglas Gregor8a6d15d2010-02-05 04:39:02 +0000214
Richard Smith4b38ded2012-03-14 23:13:10 +0000215 // If this is a dependent type, then we consider it complete.
216 if (!tag || tag->isDependentContext())
217 return false;
Douglas Gregor26897462009-03-11 16:48:53 +0000218
Richard Smith4b38ded2012-03-14 23:13:10 +0000219 // If we're currently defining this type, then lookup into the
220 // type is okay: don't complain that it isn't complete yet.
221 QualType type = Context.getTypeDeclType(tag);
222 const TagType *tagType = type->getAs<TagType>();
223 if (tagType && tagType->isBeingDefined())
224 return false;
John McCall21878762011-07-06 06:57:57 +0000225
Richard Smith4b38ded2012-03-14 23:13:10 +0000226 SourceLocation loc = SS.getLastQualifierNameLoc();
227 if (loc.isInvalid()) loc = SS.getRange().getBegin();
John McCall21878762011-07-06 06:57:57 +0000228
Richard Smith4b38ded2012-03-14 23:13:10 +0000229 // The type must be complete.
230 if (RequireCompleteType(loc, type,
231 PDiag(diag::err_incomplete_nested_name_spec)
232 << SS.getRange())) {
233 SS.SetInvalid(SS.getRange());
234 return true;
Douglas Gregor26897462009-03-11 16:48:53 +0000235 }
236
Richard Smith4b38ded2012-03-14 23:13:10 +0000237 // Fixed enum types are complete, but they aren't valid as scopes
238 // until we see a definition, so awkwardly pull out this special
239 // case.
240 const EnumType *enumType = dyn_cast_or_null<EnumType>(tagType);
241 if (!enumType || enumType->getDecl()->isCompleteDefinition())
242 return false;
243
244 // Try to instantiate the definition, if this is a specialization of an
245 // enumeration temploid.
246 EnumDecl *ED = enumType->getDecl();
247 if (EnumDecl *Pattern = ED->getInstantiatedFromMemberEnum()) {
248 MemberSpecializationInfo *MSI = ED->getMemberSpecializationInfo();
249 if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
250 return InstantiateEnum(loc, ED, Pattern,
251 getTemplateInstantiationArgs(ED),
252 TSK_ImplicitInstantiation);
253 }
254
255 Diag(loc, diag::err_incomplete_nested_name_spec)
256 << type << SS.getRange();
257 SS.SetInvalid(SS.getRange());
258 return true;
Douglas Gregor26897462009-03-11 16:48:53 +0000259}
Cedric Venet084381332009-02-14 20:20:19 +0000260
Douglas Gregor90c99722011-02-24 00:17:56 +0000261bool Sema::ActOnCXXGlobalScopeSpecifier(Scope *S, SourceLocation CCLoc,
262 CXXScopeSpec &SS) {
263 SS.MakeGlobal(Context, CCLoc);
264 return false;
Cedric Venet084381332009-02-14 20:20:19 +0000265}
266
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000267/// \brief Determines whether the given declaration is an valid acceptable
268/// result for name lookup of a nested-name-specifier.
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000269bool Sema::isAcceptableNestedNameSpecifier(NamedDecl *SD) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000270 if (!SD)
271 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000272
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000273 // Namespace and namespace aliases are fine.
274 if (isa<NamespaceDecl>(SD) || isa<NamespaceAliasDecl>(SD))
275 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000276
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000277 if (!isa<TypeDecl>(SD))
278 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000279
Richard Smithc8239732011-10-18 21:39:00 +0000280 // Determine whether we have a class (or, in C++11, an enum) or
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000281 // a typedef thereof. If so, build the nested-name-specifier.
282 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
283 if (T->isDependentType())
284 return true;
Richard Smithdda56e42011-04-15 14:24:37 +0000285 else if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000286 if (TD->getUnderlyingType()->isRecordType() ||
David Blaikiebbafb8a2012-03-11 07:00:24 +0000287 (Context.getLangOpts().CPlusPlus0x &&
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000288 TD->getUnderlyingType()->isEnumeralType()))
289 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000290 } else if (isa<RecordDecl>(SD) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +0000291 (Context.getLangOpts().CPlusPlus0x && isa<EnumDecl>(SD)))
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000292 return true;
293
294 return false;
295}
296
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000297/// \brief If the given nested-name-specifier begins with a bare identifier
Mike Stump11289f42009-09-09 15:08:12 +0000298/// (e.g., Base::), perform name lookup for that identifier as a
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000299/// nested-name-specifier within the given scope, and return the result of that
300/// name lookup.
301NamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) {
302 if (!S || !NNS)
303 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000304
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000305 while (NNS->getPrefix())
306 NNS = NNS->getPrefix();
Mike Stump11289f42009-09-09 15:08:12 +0000307
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000308 if (NNS->getKind() != NestedNameSpecifier::Identifier)
309 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000310
John McCall27b18f82009-11-17 02:14:36 +0000311 LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(),
312 LookupNestedNameSpecifierName);
313 LookupName(Found, S);
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000314 assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet");
315
John McCall67c00872009-12-02 08:25:40 +0000316 if (!Found.isSingleResult())
317 return 0;
318
319 NamedDecl *Result = Found.getFoundDecl();
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000320 if (isAcceptableNestedNameSpecifier(Result))
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000321 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000322
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000323 return 0;
324}
325
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +0000326bool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS,
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000327 SourceLocation IdLoc,
328 IdentifierInfo &II,
John McCallba7bf592010-08-24 05:47:05 +0000329 ParsedType ObjectTypePtr) {
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000330 QualType ObjectType = GetTypeFromParser(ObjectTypePtr);
331 LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName);
332
333 // Determine where to perform name lookup
334 DeclContext *LookupCtx = 0;
335 bool isDependent = false;
336 if (!ObjectType.isNull()) {
337 // This nested-name-specifier occurs in a member access expression, e.g.,
338 // x->B::f, and we are looking into the type of the object.
339 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
340 LookupCtx = computeDeclContext(ObjectType);
341 isDependent = ObjectType->isDependentType();
342 } else if (SS.isSet()) {
343 // This nested-name-specifier occurs after another nested-name-specifier,
344 // so long into the context associated with the prior nested-name-specifier.
345 LookupCtx = computeDeclContext(SS, false);
346 isDependent = isDependentScopeSpecifier(SS);
347 Found.setContextRange(SS.getRange());
348 }
349
350 if (LookupCtx) {
351 // Perform "qualified" name lookup into the declaration context we
352 // computed, which is either the type of the base of a member access
353 // expression or the declaration context associated with a prior
354 // nested-name-specifier.
355
356 // The declaration context must be complete.
John McCall0b66eb32010-05-01 00:40:08 +0000357 if (!LookupCtx->isDependentContext() &&
358 RequireCompleteDeclContext(SS, LookupCtx))
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000359 return false;
360
361 LookupQualifiedName(Found, LookupCtx);
362 } else if (isDependent) {
363 return false;
364 } else {
365 LookupName(Found, S);
366 }
367 Found.suppressDiagnostics();
368
369 if (NamedDecl *ND = Found.getAsSingle<NamedDecl>())
370 return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND);
371
372 return false;
373}
374
Kaelyn Uhrainfb96ec72012-01-12 22:32:39 +0000375namespace {
376
377// Callback to only accept typo corrections that can be a valid C++ member
378// intializer: either a non-static field member or a base class.
379class NestedNameSpecifierValidatorCCC : public CorrectionCandidateCallback {
380 public:
381 explicit NestedNameSpecifierValidatorCCC(Sema &SRef)
382 : SRef(SRef) {}
383
384 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
385 return SRef.isAcceptableNestedNameSpecifier(candidate.getCorrectionDecl());
386 }
387
388 private:
389 Sema &SRef;
390};
391
392}
393
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000394/// \brief Build a new nested-name-specifier for "identifier::", as described
395/// by ActOnCXXNestedNameSpecifier.
396///
397/// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in
398/// that it contains an extra parameter \p ScopeLookupResult, which provides
399/// the result of name lookup within the scope of the nested-name-specifier
Douglas Gregorad183ac2009-12-30 16:01:52 +0000400/// that was computed at template definition time.
Chris Lattner1c428032009-12-07 01:36:53 +0000401///
402/// If ErrorRecoveryLookup is true, then this call is used to improve error
403/// recovery. This means that it should not emit diagnostics, it should
Douglas Gregor90c99722011-02-24 00:17:56 +0000404/// just return true on failure. It also means it should only return a valid
Chris Lattner1c428032009-12-07 01:36:53 +0000405/// scope if it *knows* that the result is correct. It should not return in a
Douglas Gregor90c99722011-02-24 00:17:56 +0000406/// dependent context, for example. Nor will it extend \p SS with the scope
407/// specifier.
408bool Sema::BuildCXXNestedNameSpecifier(Scope *S,
409 IdentifierInfo &Identifier,
410 SourceLocation IdentifierLoc,
411 SourceLocation CCLoc,
412 QualType ObjectType,
413 bool EnteringContext,
414 CXXScopeSpec &SS,
415 NamedDecl *ScopeLookupResult,
416 bool ErrorRecoveryLookup) {
417 LookupResult Found(*this, &Identifier, IdentifierLoc,
418 LookupNestedNameSpecifierName);
John McCall27b18f82009-11-17 02:14:36 +0000419
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000420 // Determine where to perform name lookup
421 DeclContext *LookupCtx = 0;
422 bool isDependent = false;
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000423 if (!ObjectType.isNull()) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000424 // This nested-name-specifier occurs in a member access expression, e.g.,
425 // x->B::f, and we are looking into the type of the object.
426 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000427 LookupCtx = computeDeclContext(ObjectType);
428 isDependent = ObjectType->isDependentType();
429 } else if (SS.isSet()) {
430 // This nested-name-specifier occurs after another nested-name-specifier,
Richard Smith3f1b5d02011-05-05 21:57:07 +0000431 // so look into the context associated with the prior nested-name-specifier.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000432 LookupCtx = computeDeclContext(SS, EnteringContext);
433 isDependent = isDependentScopeSpecifier(SS);
John McCall27b18f82009-11-17 02:14:36 +0000434 Found.setContextRange(SS.getRange());
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000435 }
Mike Stump11289f42009-09-09 15:08:12 +0000436
John McCall27b18f82009-11-17 02:14:36 +0000437
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000438 bool ObjectTypeSearchedInScope = false;
439 if (LookupCtx) {
Mike Stump11289f42009-09-09 15:08:12 +0000440 // Perform "qualified" name lookup into the declaration context we
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000441 // computed, which is either the type of the base of a member access
Mike Stump11289f42009-09-09 15:08:12 +0000442 // expression or the declaration context associated with a prior
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000443 // nested-name-specifier.
Mike Stump11289f42009-09-09 15:08:12 +0000444
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000445 // The declaration context must be complete.
John McCall0b66eb32010-05-01 00:40:08 +0000446 if (!LookupCtx->isDependentContext() &&
447 RequireCompleteDeclContext(SS, LookupCtx))
Douglas Gregor90c99722011-02-24 00:17:56 +0000448 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000449
John McCall27b18f82009-11-17 02:14:36 +0000450 LookupQualifiedName(Found, LookupCtx);
Mike Stump11289f42009-09-09 15:08:12 +0000451
John McCall27b18f82009-11-17 02:14:36 +0000452 if (!ObjectType.isNull() && Found.empty()) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000453 // C++ [basic.lookup.classref]p4:
454 // If the id-expression in a class member access is a qualified-id of
Mike Stump11289f42009-09-09 15:08:12 +0000455 // the form
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000456 //
457 // class-name-or-namespace-name::...
458 //
Mike Stump11289f42009-09-09 15:08:12 +0000459 // the class-name-or-namespace-name following the . or -> operator is
460 // looked up both in the context of the entire postfix-expression and in
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000461 // the scope of the class of the object expression. If the name is found
Mike Stump11289f42009-09-09 15:08:12 +0000462 // only in the scope of the class of the object expression, the name
463 // shall refer to a class-name. If the name is found only in the
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000464 // context of the entire postfix-expression, the name shall refer to a
465 // class-name or namespace-name. [...]
466 //
467 // Qualified name lookup into a class will not find a namespace-name,
Douglas Gregor9d07dfa2011-05-15 17:27:27 +0000468 // so we do not need to diagnose that case specifically. However,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000469 // this qualified name lookup may find nothing. In that case, perform
Mike Stump11289f42009-09-09 15:08:12 +0000470 // unqualified name lookup in the given scope (if available) or
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000471 // reconstruct the result from when name lookup was performed at template
472 // definition time.
473 if (S)
John McCall27b18f82009-11-17 02:14:36 +0000474 LookupName(Found, S);
John McCall9f3059a2009-10-09 21:13:30 +0000475 else if (ScopeLookupResult)
476 Found.addDecl(ScopeLookupResult);
Mike Stump11289f42009-09-09 15:08:12 +0000477
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000478 ObjectTypeSearchedInScope = true;
479 }
Douglas Gregordf65c8ed2010-07-28 14:49:07 +0000480 } else if (!isDependent) {
481 // Perform unqualified name lookup in the current scope.
482 LookupName(Found, S);
483 }
484
485 // If we performed lookup into a dependent context and did not find anything,
486 // that's fine: just build a dependent nested-name-specifier.
487 if (Found.empty() && isDependent &&
488 !(LookupCtx && LookupCtx->isRecord() &&
489 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
490 !cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()))) {
Chris Lattner1c428032009-12-07 01:36:53 +0000491 // Don't speculate if we're just trying to improve error recovery.
492 if (ErrorRecoveryLookup)
Douglas Gregor90c99722011-02-24 00:17:56 +0000493 return true;
Chris Lattner1c428032009-12-07 01:36:53 +0000494
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000495 // We were not able to compute the declaration context for a dependent
Mike Stump11289f42009-09-09 15:08:12 +0000496 // base object type or prior nested-name-specifier, so this
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000497 // nested-name-specifier refers to an unknown specialization. Just build
498 // a dependent nested-name-specifier.
Douglas Gregor90c99722011-02-24 00:17:56 +0000499 SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc);
500 return false;
Douglas Gregordf65c8ed2010-07-28 14:49:07 +0000501 }
502
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000503 // FIXME: Deal with ambiguities cleanly.
Douglas Gregor532e68f2009-12-31 08:26:35 +0000504
505 if (Found.empty() && !ErrorRecoveryLookup) {
506 // We haven't found anything, and we're not recovering from a
507 // different kind of error, so look for typos.
508 DeclarationName Name = Found.getLookupName();
Kaelyn Uhrainfb96ec72012-01-12 22:32:39 +0000509 NestedNameSpecifierValidatorCCC Validator(*this);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000510 TypoCorrection Corrected;
511 Found.clear();
512 if ((Corrected = CorrectTypo(Found.getLookupNameInfo(),
Kaelyn Uhrain4e8942c2012-01-31 23:49:25 +0000513 Found.getLookupKind(), S, &SS, Validator,
Kaelyn Uhrainfb96ec72012-01-12 22:32:39 +0000514 LookupCtx, EnteringContext))) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000515 std::string CorrectedStr(Corrected.getAsString(getLangOpts()));
516 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOpts()));
Douglas Gregor532e68f2009-12-31 08:26:35 +0000517 if (LookupCtx)
518 Diag(Found.getNameLoc(), diag::err_no_member_suggest)
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000519 << Name << LookupCtx << CorrectedQuotedStr << SS.getRange()
520 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregor532e68f2009-12-31 08:26:35 +0000521 else
522 Diag(Found.getNameLoc(), diag::err_undeclared_var_use_suggest)
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000523 << Name << CorrectedQuotedStr
524 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregor6da83622010-01-07 00:17:44 +0000525
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000526 if (NamedDecl *ND = Corrected.getCorrectionDecl()) {
527 Diag(ND->getLocation(), diag::note_previous_decl) << CorrectedQuotedStr;
528 Found.addDecl(ND);
529 }
530 Found.setLookupName(Corrected.getCorrection());
Douglas Gregorc048c522010-06-29 19:27:42 +0000531 } else {
Douglas Gregor90c99722011-02-24 00:17:56 +0000532 Found.setLookupName(&Identifier);
Douglas Gregorc048c522010-06-29 19:27:42 +0000533 }
Douglas Gregor532e68f2009-12-31 08:26:35 +0000534 }
535
John McCall67c00872009-12-02 08:25:40 +0000536 NamedDecl *SD = Found.getAsSingle<NamedDecl>();
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000537 if (isAcceptableNestedNameSpecifier(SD)) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000538 if (!ObjectType.isNull() && !ObjectTypeSearchedInScope) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000539 // C++ [basic.lookup.classref]p4:
Mike Stump11289f42009-09-09 15:08:12 +0000540 // [...] If the name is found in both contexts, the
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000541 // class-name-or-namespace-name shall refer to the same entity.
542 //
543 // We already found the name in the scope of the object. Now, look
544 // into the current scope (the scope of the postfix-expression) to
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000545 // see if we can find the same name there. As above, if there is no
546 // scope, reconstruct the result from the template instantiation itself.
John McCall9f3059a2009-10-09 21:13:30 +0000547 NamedDecl *OuterDecl;
548 if (S) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000549 LookupResult FoundOuter(*this, &Identifier, IdentifierLoc,
550 LookupNestedNameSpecifierName);
John McCall27b18f82009-11-17 02:14:36 +0000551 LookupName(FoundOuter, S);
John McCall67c00872009-12-02 08:25:40 +0000552 OuterDecl = FoundOuter.getAsSingle<NamedDecl>();
John McCall9f3059a2009-10-09 21:13:30 +0000553 } else
554 OuterDecl = ScopeLookupResult;
Mike Stump11289f42009-09-09 15:08:12 +0000555
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000556 if (isAcceptableNestedNameSpecifier(OuterDecl) &&
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000557 OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() &&
558 (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) ||
559 !Context.hasSameType(
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000560 Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)),
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000561 Context.getTypeDeclType(cast<TypeDecl>(SD))))) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000562 if (ErrorRecoveryLookup)
563 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000564
Douglas Gregor90c99722011-02-24 00:17:56 +0000565 Diag(IdentifierLoc,
566 diag::err_nested_name_member_ref_lookup_ambiguous)
567 << &Identifier;
568 Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type)
569 << ObjectType;
570 Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope);
571
572 // Fall through so that we'll pick the name we found in the object
573 // type, since that's probably what the user wanted anyway.
574 }
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000575 }
Mike Stump11289f42009-09-09 15:08:12 +0000576
Douglas Gregor90c99722011-02-24 00:17:56 +0000577 // If we're just performing this lookup for error-recovery purposes,
578 // don't extend the nested-name-specifier. Just return now.
579 if (ErrorRecoveryLookup)
580 return false;
581
582 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) {
583 SS.Extend(Context, Namespace, IdentifierLoc, CCLoc);
584 return false;
585 }
Mike Stump11289f42009-09-09 15:08:12 +0000586
Douglas Gregor90c99722011-02-24 00:17:56 +0000587 if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) {
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000588 SS.Extend(Context, Alias, IdentifierLoc, CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000589 return false;
590 }
Mike Stump11289f42009-09-09 15:08:12 +0000591
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000592 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
Douglas Gregor90c99722011-02-24 00:17:56 +0000593 TypeLocBuilder TLB;
594 if (isa<InjectedClassNameType>(T)) {
595 InjectedClassNameTypeLoc InjectedTL
596 = TLB.push<InjectedClassNameTypeLoc>(T);
597 InjectedTL.setNameLoc(IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000598 } else if (isa<RecordType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000599 RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T);
600 RecordTL.setNameLoc(IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000601 } else if (isa<TypedefType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000602 TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T);
603 TypedefTL.setNameLoc(IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000604 } else if (isa<EnumType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000605 EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T);
606 EnumTL.setNameLoc(IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000607 } else if (isa<TemplateTypeParmType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000608 TemplateTypeParmTypeLoc TemplateTypeTL
609 = TLB.push<TemplateTypeParmTypeLoc>(T);
610 TemplateTypeTL.setNameLoc(IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000611 } else if (isa<UnresolvedUsingType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000612 UnresolvedUsingTypeLoc UnresolvedTL
613 = TLB.push<UnresolvedUsingTypeLoc>(T);
614 UnresolvedTL.setNameLoc(IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000615 } else if (isa<SubstTemplateTypeParmType>(T)) {
616 SubstTemplateTypeParmTypeLoc TL
617 = TLB.push<SubstTemplateTypeParmTypeLoc>(T);
618 TL.setNameLoc(IdentifierLoc);
619 } else if (isa<SubstTemplateTypeParmPackType>(T)) {
620 SubstTemplateTypeParmPackTypeLoc TL
621 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(T);
622 TL.setNameLoc(IdentifierLoc);
623 } else {
624 llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier");
Douglas Gregor90c99722011-02-24 00:17:56 +0000625 }
626
Richard Smith91c7bbd2011-10-20 03:28:47 +0000627 if (T->isEnumeralType())
628 Diag(IdentifierLoc, diag::warn_cxx98_compat_enum_nested_name_spec);
629
Douglas Gregor90c99722011-02-24 00:17:56 +0000630 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
631 CCLoc);
632 return false;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000633 }
Mike Stump11289f42009-09-09 15:08:12 +0000634
Chris Lattner1c428032009-12-07 01:36:53 +0000635 // Otherwise, we have an error case. If we don't want diagnostics, just
636 // return an error now.
637 if (ErrorRecoveryLookup)
Douglas Gregor90c99722011-02-24 00:17:56 +0000638 return true;
Chris Lattner1c428032009-12-07 01:36:53 +0000639
Cedric Venet084381332009-02-14 20:20:19 +0000640 // If we didn't find anything during our lookup, try again with
641 // ordinary name lookup, which can help us produce better error
642 // messages.
John McCall67c00872009-12-02 08:25:40 +0000643 if (Found.empty()) {
John McCall27b18f82009-11-17 02:14:36 +0000644 Found.clear(LookupOrdinaryName);
645 LookupName(Found, S);
John McCall9f3059a2009-10-09 21:13:30 +0000646 }
Mike Stump11289f42009-09-09 15:08:12 +0000647
Francois Pichetb23dc092011-07-27 01:05:24 +0000648 // In Microsoft mode, if we are within a templated function and we can't
649 // resolve Identifier, then extend the SS with Identifier. This will have
650 // the effect of resolving Identifier during template instantiation.
651 // The goal is to be able to resolve a function call whose
652 // nested-name-specifier is located inside a dependent base class.
653 // Example:
654 //
655 // class C {
656 // public:
657 // static void foo2() { }
658 // };
659 // template <class T> class A { public: typedef C D; };
660 //
661 // template <class T> class B : public A<T> {
662 // public:
663 // void foo() { D::foo2(); }
664 // };
David Blaikiebbafb8a2012-03-11 07:00:24 +0000665 if (getLangOpts().MicrosoftExt) {
Francois Pichetb23dc092011-07-27 01:05:24 +0000666 DeclContext *DC = LookupCtx ? LookupCtx : CurContext;
667 if (DC->isDependentContext() && DC->isFunctionOrMethod()) {
668 SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc);
669 return false;
670 }
671 }
672
Cedric Venet084381332009-02-14 20:20:19 +0000673 unsigned DiagID;
John McCall67c00872009-12-02 08:25:40 +0000674 if (!Found.empty())
Cedric Venet084381332009-02-14 20:20:19 +0000675 DiagID = diag::err_expected_class_or_namespace;
Anders Carlssonb533df02009-08-30 07:09:50 +0000676 else if (SS.isSet()) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000677 Diag(IdentifierLoc, diag::err_no_member)
678 << &Identifier << LookupCtx << SS.getRange();
679 return true;
Anders Carlssonb533df02009-08-30 07:09:50 +0000680 } else
Cedric Venet084381332009-02-14 20:20:19 +0000681 DiagID = diag::err_undeclared_var_use;
Mike Stump11289f42009-09-09 15:08:12 +0000682
Cedric Venet084381332009-02-14 20:20:19 +0000683 if (SS.isSet())
Douglas Gregor90c99722011-02-24 00:17:56 +0000684 Diag(IdentifierLoc, DiagID) << &Identifier << SS.getRange();
Cedric Venet084381332009-02-14 20:20:19 +0000685 else
Douglas Gregor90c99722011-02-24 00:17:56 +0000686 Diag(IdentifierLoc, DiagID) << &Identifier;
Mike Stump11289f42009-09-09 15:08:12 +0000687
Douglas Gregor90c99722011-02-24 00:17:56 +0000688 return true;
Cedric Venet084381332009-02-14 20:20:19 +0000689}
690
Douglas Gregor90c99722011-02-24 00:17:56 +0000691bool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
692 IdentifierInfo &Identifier,
693 SourceLocation IdentifierLoc,
694 SourceLocation CCLoc,
695 ParsedType ObjectType,
696 bool EnteringContext,
697 CXXScopeSpec &SS) {
698 if (SS.isInvalid())
699 return true;
700
701 return BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, CCLoc,
702 GetTypeFromParser(ObjectType),
703 EnteringContext, SS,
704 /*ScopeLookupResult=*/0, false);
Chris Lattner1c428032009-12-07 01:36:53 +0000705}
706
David Blaikie15a430a2011-12-04 05:04:18 +0000707bool Sema::ActOnCXXNestedNameSpecifierDecltype(CXXScopeSpec &SS,
708 const DeclSpec &DS,
709 SourceLocation ColonColonLoc) {
710 if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error)
711 return true;
712
713 assert(DS.getTypeSpecType() == DeclSpec::TST_decltype);
714
715 QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc());
716 if (!T->isDependentType() && !T->getAs<TagType>()) {
717 Diag(DS.getTypeSpecTypeLoc(), diag::err_expected_class)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000718 << T << getLangOpts().CPlusPlus;
David Blaikie15a430a2011-12-04 05:04:18 +0000719 return true;
720 }
721
722 TypeLocBuilder TLB;
723 DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T);
724 DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc());
725 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
726 ColonColonLoc);
727 return false;
728}
729
Chris Lattner1c428032009-12-07 01:36:53 +0000730/// IsInvalidUnlessNestedName - This method is used for error recovery
731/// purposes to determine whether the specified identifier is only valid as
732/// a nested name specifier, for example a namespace name. It is
733/// conservatively correct to always return false from this method.
734///
735/// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +0000736bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS,
Douglas Gregor90c99722011-02-24 00:17:56 +0000737 IdentifierInfo &Identifier,
738 SourceLocation IdentifierLoc,
739 SourceLocation ColonLoc,
740 ParsedType ObjectType,
Chris Lattner1c428032009-12-07 01:36:53 +0000741 bool EnteringContext) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000742 if (SS.isInvalid())
743 return false;
744
745 return !BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, ColonLoc,
746 GetTypeFromParser(ObjectType),
747 EnteringContext, SS,
748 /*ScopeLookupResult=*/0, true);
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000749}
750
Douglas Gregor90c99722011-02-24 00:17:56 +0000751bool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000752 CXXScopeSpec &SS,
753 SourceLocation TemplateKWLoc,
Douglas Gregor6e068012011-02-28 00:04:36 +0000754 TemplateTy Template,
755 SourceLocation TemplateNameLoc,
756 SourceLocation LAngleLoc,
757 ASTTemplateArgsPtr TemplateArgsIn,
758 SourceLocation RAngleLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +0000759 SourceLocation CCLoc,
Douglas Gregor6e068012011-02-28 00:04:36 +0000760 bool EnteringContext) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000761 if (SS.isInvalid())
762 return true;
763
Douglas Gregor6e068012011-02-28 00:04:36 +0000764 // Translate the parser's template argument list in our AST format.
765 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
766 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
767
768 if (DependentTemplateName *DTN = Template.get().getAsDependentTemplateName()){
769 // Handle a dependent template specialization for which we cannot resolve
770 // the template name.
771 assert(DTN->getQualifier()
772 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
773 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
Douglas Gregora7a795b2011-03-01 20:11:18 +0000774 DTN->getQualifier(),
775 DTN->getIdentifier(),
Douglas Gregor6e068012011-02-28 00:04:36 +0000776 TemplateArgs);
777
778 // Create source-location information for this type.
779 TypeLocBuilder Builder;
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000780 DependentTemplateSpecializationTypeLoc SpecTL
Douglas Gregor6e068012011-02-28 00:04:36 +0000781 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000782 SpecTL.setElaboratedKeywordLoc(SourceLocation());
783 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000784 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000785 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000786 SpecTL.setLAngleLoc(LAngleLoc);
787 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000788 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
789 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
790
Abramo Bagnara7945c982012-01-27 09:46:47 +0000791 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
Douglas Gregor6e068012011-02-28 00:04:36 +0000792 CCLoc);
793 return false;
794 }
795
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000796
797 if (Template.get().getAsOverloadedTemplate() ||
798 isa<FunctionTemplateDecl>(Template.get().getAsTemplateDecl())) {
799 SourceRange R(TemplateNameLoc, RAngleLoc);
800 if (SS.getRange().isValid())
801 R.setBegin(SS.getRange().getBegin());
802
803 Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier)
804 << Template.get() << R;
805 NoteAllFoundTemplates(Template.get());
806 return true;
807 }
808
Douglas Gregor6e068012011-02-28 00:04:36 +0000809 // We were able to resolve the template name to an actual template.
810 // Build an appropriate nested-name-specifier.
811 QualType T = CheckTemplateIdType(Template.get(), TemplateNameLoc,
812 TemplateArgs);
Douglas Gregor90c99722011-02-24 00:17:56 +0000813 if (T.isNull())
814 return true;
815
Richard Smith3f1b5d02011-05-05 21:57:07 +0000816 // Alias template specializations can produce types which are not valid
817 // nested name specifiers.
818 if (!T->isDependentType() && !T->getAs<TagType>()) {
819 Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T;
820 NoteAllFoundTemplates(Template.get());
821 return true;
822 }
Douglas Gregor6e068012011-02-28 00:04:36 +0000823
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000824 // Provide source-location information for the template specialization type.
Douglas Gregor6e068012011-02-28 00:04:36 +0000825 TypeLocBuilder Builder;
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000826 TemplateSpecializationTypeLoc SpecTL
Douglas Gregor6e068012011-02-28 00:04:36 +0000827 = Builder.push<TemplateSpecializationTypeLoc>(T);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000828 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
829 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000830 SpecTL.setLAngleLoc(LAngleLoc);
831 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000832 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
833 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
834
835
Abramo Bagnara7945c982012-01-27 09:46:47 +0000836 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
Douglas Gregor6e068012011-02-28 00:04:36 +0000837 CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000838 return false;
Douglas Gregor7f741122009-02-25 19:37:18 +0000839}
840
Douglas Gregor869ad452011-02-24 17:54:50 +0000841namespace {
842 /// \brief A structure that stores a nested-name-specifier annotation,
843 /// including both the nested-name-specifier
844 struct NestedNameSpecifierAnnotation {
845 NestedNameSpecifier *NNS;
846 };
847}
848
849void *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) {
850 if (SS.isEmpty() || SS.isInvalid())
851 return 0;
852
853 void *Mem = Context.Allocate((sizeof(NestedNameSpecifierAnnotation) +
854 SS.location_size()),
855 llvm::alignOf<NestedNameSpecifierAnnotation>());
856 NestedNameSpecifierAnnotation *Annotation
857 = new (Mem) NestedNameSpecifierAnnotation;
858 Annotation->NNS = SS.getScopeRep();
859 memcpy(Annotation + 1, SS.location_data(), SS.location_size());
860 return Annotation;
861}
862
863void Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr,
864 SourceRange AnnotationRange,
865 CXXScopeSpec &SS) {
866 if (!AnnotationPtr) {
867 SS.SetInvalid(AnnotationRange);
868 return;
869 }
870
871 NestedNameSpecifierAnnotation *Annotation
872 = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr);
873 SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1));
874}
875
John McCall2b058ef2009-12-11 20:04:54 +0000876bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
877 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
878
879 NestedNameSpecifier *Qualifier =
880 static_cast<NestedNameSpecifier*>(SS.getScopeRep());
881
882 // There are only two places a well-formed program may qualify a
883 // declarator: first, when defining a namespace or class member
884 // out-of-line, and second, when naming an explicitly-qualified
885 // friend function. The latter case is governed by
886 // C++03 [basic.lookup.unqual]p10:
887 // In a friend declaration naming a member function, a name used
888 // in the function declarator and not part of a template-argument
889 // in a template-id is first looked up in the scope of the member
890 // function's class. If it is not found, or if the name is part of
891 // a template-argument in a template-id, the look up is as
892 // described for unqualified names in the definition of the class
893 // granting friendship.
894 // i.e. we don't push a scope unless it's a class member.
895
896 switch (Qualifier->getKind()) {
897 case NestedNameSpecifier::Global:
898 case NestedNameSpecifier::Namespace:
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000899 case NestedNameSpecifier::NamespaceAlias:
John McCall2b058ef2009-12-11 20:04:54 +0000900 // These are always namespace scopes. We never want to enter a
901 // namespace scope from anything but a file context.
Sebastian Redl50c68252010-08-31 00:36:30 +0000902 return CurContext->getRedeclContext()->isFileContext();
John McCall2b058ef2009-12-11 20:04:54 +0000903
904 case NestedNameSpecifier::Identifier:
905 case NestedNameSpecifier::TypeSpec:
906 case NestedNameSpecifier::TypeSpecWithTemplate:
907 // These are never namespace scopes.
908 return true;
909 }
910
David Blaikie8a40f702012-01-17 06:56:22 +0000911 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
John McCall2b058ef2009-12-11 20:04:54 +0000912}
913
Cedric Venet084381332009-02-14 20:20:19 +0000914/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
915/// scope or nested-name-specifier) is parsed, part of a declarator-id.
916/// After this method is called, according to [C++ 3.4.3p3], names should be
917/// looked up in the declarator-id's scope, until the declarator is parsed and
918/// ActOnCXXExitDeclaratorScope is called.
919/// The 'SS' should be a non-empty valid CXXScopeSpec.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +0000920bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) {
Cedric Venet084381332009-02-14 20:20:19 +0000921 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
John McCall6df5fef2009-12-19 10:49:29 +0000922
923 if (SS.isInvalid()) return true;
924
925 DeclContext *DC = computeDeclContext(SS, true);
926 if (!DC) return true;
927
928 // Before we enter a declarator's context, we need to make sure that
929 // it is a complete declaration context.
John McCall0b66eb32010-05-01 00:40:08 +0000930 if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC))
John McCall6df5fef2009-12-19 10:49:29 +0000931 return true;
932
933 EnterDeclaratorContext(S, DC);
John McCall2408e322010-04-27 00:57:59 +0000934
935 // Rebuild the nested name specifier for the new scope.
936 if (DC->isDependentContext())
937 RebuildNestedNameSpecifierInCurrentInstantiation(SS);
938
Douglas Gregor5013a7e2009-09-24 23:39:01 +0000939 return false;
Cedric Venet084381332009-02-14 20:20:19 +0000940}
941
942/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
943/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
944/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
945/// Used to indicate that names should revert to being looked up in the
946/// defining scope.
947void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
948 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
Douglas Gregor053f6912009-08-26 00:04:55 +0000949 if (SS.isInvalid())
950 return;
John McCall6df5fef2009-12-19 10:49:29 +0000951 assert(!SS.isInvalid() && computeDeclContext(SS, true) &&
952 "exiting declarator scope we never really entered");
953 ExitDeclaratorContext(S);
Cedric Venet084381332009-02-14 20:20:19 +0000954}