blob: 5a0fceca20da8e9ca0010b0ea382d5e44e2f2144 [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();
Richard Smith7d137e32012-03-23 03:33:32 +0000249 if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) {
250 if (InstantiateEnum(loc, ED, Pattern, getTemplateInstantiationArgs(ED),
251 TSK_ImplicitInstantiation)) {
252 SS.SetInvalid(SS.getRange());
253 return true;
254 }
255 return false;
256 }
Richard Smith4b38ded2012-03-14 23:13:10 +0000257 }
258
259 Diag(loc, diag::err_incomplete_nested_name_spec)
260 << type << SS.getRange();
261 SS.SetInvalid(SS.getRange());
262 return true;
Douglas Gregor26897462009-03-11 16:48:53 +0000263}
Cedric Venet084381332009-02-14 20:20:19 +0000264
Douglas Gregor90c99722011-02-24 00:17:56 +0000265bool Sema::ActOnCXXGlobalScopeSpecifier(Scope *S, SourceLocation CCLoc,
266 CXXScopeSpec &SS) {
267 SS.MakeGlobal(Context, CCLoc);
268 return false;
Cedric Venet084381332009-02-14 20:20:19 +0000269}
270
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000271/// \brief Determines whether the given declaration is an valid acceptable
272/// result for name lookup of a nested-name-specifier.
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000273bool Sema::isAcceptableNestedNameSpecifier(NamedDecl *SD) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000274 if (!SD)
275 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000276
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000277 // Namespace and namespace aliases are fine.
278 if (isa<NamespaceDecl>(SD) || isa<NamespaceAliasDecl>(SD))
279 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000280
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000281 if (!isa<TypeDecl>(SD))
282 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000283
Richard Smithc8239732011-10-18 21:39:00 +0000284 // Determine whether we have a class (or, in C++11, an enum) or
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000285 // a typedef thereof. If so, build the nested-name-specifier.
286 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
287 if (T->isDependentType())
288 return true;
Richard Smithdda56e42011-04-15 14:24:37 +0000289 else if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000290 if (TD->getUnderlyingType()->isRecordType() ||
David Blaikiebbafb8a2012-03-11 07:00:24 +0000291 (Context.getLangOpts().CPlusPlus0x &&
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000292 TD->getUnderlyingType()->isEnumeralType()))
293 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000294 } else if (isa<RecordDecl>(SD) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +0000295 (Context.getLangOpts().CPlusPlus0x && isa<EnumDecl>(SD)))
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000296 return true;
297
298 return false;
299}
300
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000301/// \brief If the given nested-name-specifier begins with a bare identifier
Mike Stump11289f42009-09-09 15:08:12 +0000302/// (e.g., Base::), perform name lookup for that identifier as a
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000303/// nested-name-specifier within the given scope, and return the result of that
304/// name lookup.
305NamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) {
306 if (!S || !NNS)
307 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000308
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000309 while (NNS->getPrefix())
310 NNS = NNS->getPrefix();
Mike Stump11289f42009-09-09 15:08:12 +0000311
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000312 if (NNS->getKind() != NestedNameSpecifier::Identifier)
313 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000314
John McCall27b18f82009-11-17 02:14:36 +0000315 LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(),
316 LookupNestedNameSpecifierName);
317 LookupName(Found, S);
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000318 assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet");
319
John McCall67c00872009-12-02 08:25:40 +0000320 if (!Found.isSingleResult())
321 return 0;
322
323 NamedDecl *Result = Found.getFoundDecl();
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000324 if (isAcceptableNestedNameSpecifier(Result))
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000325 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000326
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000327 return 0;
328}
329
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +0000330bool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS,
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000331 SourceLocation IdLoc,
332 IdentifierInfo &II,
John McCallba7bf592010-08-24 05:47:05 +0000333 ParsedType ObjectTypePtr) {
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000334 QualType ObjectType = GetTypeFromParser(ObjectTypePtr);
335 LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName);
336
337 // Determine where to perform name lookup
338 DeclContext *LookupCtx = 0;
339 bool isDependent = false;
340 if (!ObjectType.isNull()) {
341 // This nested-name-specifier occurs in a member access expression, e.g.,
342 // x->B::f, and we are looking into the type of the object.
343 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
344 LookupCtx = computeDeclContext(ObjectType);
345 isDependent = ObjectType->isDependentType();
346 } else if (SS.isSet()) {
347 // This nested-name-specifier occurs after another nested-name-specifier,
348 // so long into the context associated with the prior nested-name-specifier.
349 LookupCtx = computeDeclContext(SS, false);
350 isDependent = isDependentScopeSpecifier(SS);
351 Found.setContextRange(SS.getRange());
352 }
353
354 if (LookupCtx) {
355 // Perform "qualified" name lookup into the declaration context we
356 // computed, which is either the type of the base of a member access
357 // expression or the declaration context associated with a prior
358 // nested-name-specifier.
359
360 // The declaration context must be complete.
John McCall0b66eb32010-05-01 00:40:08 +0000361 if (!LookupCtx->isDependentContext() &&
362 RequireCompleteDeclContext(SS, LookupCtx))
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000363 return false;
364
365 LookupQualifiedName(Found, LookupCtx);
366 } else if (isDependent) {
367 return false;
368 } else {
369 LookupName(Found, S);
370 }
371 Found.suppressDiagnostics();
372
373 if (NamedDecl *ND = Found.getAsSingle<NamedDecl>())
374 return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND);
375
376 return false;
377}
378
Kaelyn Uhrainfb96ec72012-01-12 22:32:39 +0000379namespace {
380
381// Callback to only accept typo corrections that can be a valid C++ member
382// intializer: either a non-static field member or a base class.
383class NestedNameSpecifierValidatorCCC : public CorrectionCandidateCallback {
384 public:
385 explicit NestedNameSpecifierValidatorCCC(Sema &SRef)
386 : SRef(SRef) {}
387
388 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
389 return SRef.isAcceptableNestedNameSpecifier(candidate.getCorrectionDecl());
390 }
391
392 private:
393 Sema &SRef;
394};
395
396}
397
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000398/// \brief Build a new nested-name-specifier for "identifier::", as described
399/// by ActOnCXXNestedNameSpecifier.
400///
401/// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in
402/// that it contains an extra parameter \p ScopeLookupResult, which provides
403/// the result of name lookup within the scope of the nested-name-specifier
Douglas Gregorad183ac2009-12-30 16:01:52 +0000404/// that was computed at template definition time.
Chris Lattner1c428032009-12-07 01:36:53 +0000405///
406/// If ErrorRecoveryLookup is true, then this call is used to improve error
407/// recovery. This means that it should not emit diagnostics, it should
Douglas Gregor90c99722011-02-24 00:17:56 +0000408/// just return true on failure. It also means it should only return a valid
Chris Lattner1c428032009-12-07 01:36:53 +0000409/// scope if it *knows* that the result is correct. It should not return in a
Douglas Gregor90c99722011-02-24 00:17:56 +0000410/// dependent context, for example. Nor will it extend \p SS with the scope
411/// specifier.
412bool Sema::BuildCXXNestedNameSpecifier(Scope *S,
413 IdentifierInfo &Identifier,
414 SourceLocation IdentifierLoc,
415 SourceLocation CCLoc,
416 QualType ObjectType,
417 bool EnteringContext,
418 CXXScopeSpec &SS,
419 NamedDecl *ScopeLookupResult,
420 bool ErrorRecoveryLookup) {
421 LookupResult Found(*this, &Identifier, IdentifierLoc,
422 LookupNestedNameSpecifierName);
John McCall27b18f82009-11-17 02:14:36 +0000423
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000424 // Determine where to perform name lookup
425 DeclContext *LookupCtx = 0;
426 bool isDependent = false;
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000427 if (!ObjectType.isNull()) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000428 // This nested-name-specifier occurs in a member access expression, e.g.,
429 // x->B::f, and we are looking into the type of the object.
430 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000431 LookupCtx = computeDeclContext(ObjectType);
432 isDependent = ObjectType->isDependentType();
433 } else if (SS.isSet()) {
434 // This nested-name-specifier occurs after another nested-name-specifier,
Richard Smith3f1b5d02011-05-05 21:57:07 +0000435 // so look into the context associated with the prior nested-name-specifier.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000436 LookupCtx = computeDeclContext(SS, EnteringContext);
437 isDependent = isDependentScopeSpecifier(SS);
John McCall27b18f82009-11-17 02:14:36 +0000438 Found.setContextRange(SS.getRange());
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000439 }
Mike Stump11289f42009-09-09 15:08:12 +0000440
John McCall27b18f82009-11-17 02:14:36 +0000441
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000442 bool ObjectTypeSearchedInScope = false;
443 if (LookupCtx) {
Mike Stump11289f42009-09-09 15:08:12 +0000444 // Perform "qualified" name lookup into the declaration context we
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000445 // computed, which is either the type of the base of a member access
Mike Stump11289f42009-09-09 15:08:12 +0000446 // expression or the declaration context associated with a prior
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000447 // nested-name-specifier.
Mike Stump11289f42009-09-09 15:08:12 +0000448
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000449 // The declaration context must be complete.
John McCall0b66eb32010-05-01 00:40:08 +0000450 if (!LookupCtx->isDependentContext() &&
451 RequireCompleteDeclContext(SS, LookupCtx))
Douglas Gregor90c99722011-02-24 00:17:56 +0000452 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000453
John McCall27b18f82009-11-17 02:14:36 +0000454 LookupQualifiedName(Found, LookupCtx);
Mike Stump11289f42009-09-09 15:08:12 +0000455
John McCall27b18f82009-11-17 02:14:36 +0000456 if (!ObjectType.isNull() && Found.empty()) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000457 // C++ [basic.lookup.classref]p4:
458 // If the id-expression in a class member access is a qualified-id of
Mike Stump11289f42009-09-09 15:08:12 +0000459 // the form
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000460 //
461 // class-name-or-namespace-name::...
462 //
Mike Stump11289f42009-09-09 15:08:12 +0000463 // the class-name-or-namespace-name following the . or -> operator is
464 // looked up both in the context of the entire postfix-expression and in
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000465 // the scope of the class of the object expression. If the name is found
Mike Stump11289f42009-09-09 15:08:12 +0000466 // only in the scope of the class of the object expression, the name
467 // shall refer to a class-name. If the name is found only in the
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000468 // context of the entire postfix-expression, the name shall refer to a
469 // class-name or namespace-name. [...]
470 //
471 // Qualified name lookup into a class will not find a namespace-name,
Douglas Gregor9d07dfa2011-05-15 17:27:27 +0000472 // so we do not need to diagnose that case specifically. However,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000473 // this qualified name lookup may find nothing. In that case, perform
Mike Stump11289f42009-09-09 15:08:12 +0000474 // unqualified name lookup in the given scope (if available) or
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000475 // reconstruct the result from when name lookup was performed at template
476 // definition time.
477 if (S)
John McCall27b18f82009-11-17 02:14:36 +0000478 LookupName(Found, S);
John McCall9f3059a2009-10-09 21:13:30 +0000479 else if (ScopeLookupResult)
480 Found.addDecl(ScopeLookupResult);
Mike Stump11289f42009-09-09 15:08:12 +0000481
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000482 ObjectTypeSearchedInScope = true;
483 }
Douglas Gregordf65c8ed2010-07-28 14:49:07 +0000484 } else if (!isDependent) {
485 // Perform unqualified name lookup in the current scope.
486 LookupName(Found, S);
487 }
488
489 // If we performed lookup into a dependent context and did not find anything,
490 // that's fine: just build a dependent nested-name-specifier.
491 if (Found.empty() && isDependent &&
492 !(LookupCtx && LookupCtx->isRecord() &&
493 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
494 !cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()))) {
Chris Lattner1c428032009-12-07 01:36:53 +0000495 // Don't speculate if we're just trying to improve error recovery.
496 if (ErrorRecoveryLookup)
Douglas Gregor90c99722011-02-24 00:17:56 +0000497 return true;
Chris Lattner1c428032009-12-07 01:36:53 +0000498
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000499 // We were not able to compute the declaration context for a dependent
Mike Stump11289f42009-09-09 15:08:12 +0000500 // base object type or prior nested-name-specifier, so this
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000501 // nested-name-specifier refers to an unknown specialization. Just build
502 // a dependent nested-name-specifier.
Douglas Gregor90c99722011-02-24 00:17:56 +0000503 SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc);
504 return false;
Douglas Gregordf65c8ed2010-07-28 14:49:07 +0000505 }
506
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000507 // FIXME: Deal with ambiguities cleanly.
Douglas Gregor532e68f2009-12-31 08:26:35 +0000508
509 if (Found.empty() && !ErrorRecoveryLookup) {
510 // We haven't found anything, and we're not recovering from a
511 // different kind of error, so look for typos.
512 DeclarationName Name = Found.getLookupName();
Kaelyn Uhrainfb96ec72012-01-12 22:32:39 +0000513 NestedNameSpecifierValidatorCCC Validator(*this);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000514 TypoCorrection Corrected;
515 Found.clear();
516 if ((Corrected = CorrectTypo(Found.getLookupNameInfo(),
Kaelyn Uhrain4e8942c2012-01-31 23:49:25 +0000517 Found.getLookupKind(), S, &SS, Validator,
Kaelyn Uhrainfb96ec72012-01-12 22:32:39 +0000518 LookupCtx, EnteringContext))) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000519 std::string CorrectedStr(Corrected.getAsString(getLangOpts()));
520 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOpts()));
Douglas Gregor532e68f2009-12-31 08:26:35 +0000521 if (LookupCtx)
522 Diag(Found.getNameLoc(), diag::err_no_member_suggest)
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000523 << Name << LookupCtx << CorrectedQuotedStr << SS.getRange()
524 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregor532e68f2009-12-31 08:26:35 +0000525 else
526 Diag(Found.getNameLoc(), diag::err_undeclared_var_use_suggest)
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000527 << Name << CorrectedQuotedStr
528 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregor6da83622010-01-07 00:17:44 +0000529
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000530 if (NamedDecl *ND = Corrected.getCorrectionDecl()) {
531 Diag(ND->getLocation(), diag::note_previous_decl) << CorrectedQuotedStr;
532 Found.addDecl(ND);
533 }
534 Found.setLookupName(Corrected.getCorrection());
Douglas Gregorc048c522010-06-29 19:27:42 +0000535 } else {
Douglas Gregor90c99722011-02-24 00:17:56 +0000536 Found.setLookupName(&Identifier);
Douglas Gregorc048c522010-06-29 19:27:42 +0000537 }
Douglas Gregor532e68f2009-12-31 08:26:35 +0000538 }
539
John McCall67c00872009-12-02 08:25:40 +0000540 NamedDecl *SD = Found.getAsSingle<NamedDecl>();
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000541 if (isAcceptableNestedNameSpecifier(SD)) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000542 if (!ObjectType.isNull() && !ObjectTypeSearchedInScope) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000543 // C++ [basic.lookup.classref]p4:
Mike Stump11289f42009-09-09 15:08:12 +0000544 // [...] If the name is found in both contexts, the
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000545 // class-name-or-namespace-name shall refer to the same entity.
546 //
547 // We already found the name in the scope of the object. Now, look
548 // into the current scope (the scope of the postfix-expression) to
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000549 // see if we can find the same name there. As above, if there is no
550 // scope, reconstruct the result from the template instantiation itself.
John McCall9f3059a2009-10-09 21:13:30 +0000551 NamedDecl *OuterDecl;
552 if (S) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000553 LookupResult FoundOuter(*this, &Identifier, IdentifierLoc,
554 LookupNestedNameSpecifierName);
John McCall27b18f82009-11-17 02:14:36 +0000555 LookupName(FoundOuter, S);
John McCall67c00872009-12-02 08:25:40 +0000556 OuterDecl = FoundOuter.getAsSingle<NamedDecl>();
John McCall9f3059a2009-10-09 21:13:30 +0000557 } else
558 OuterDecl = ScopeLookupResult;
Mike Stump11289f42009-09-09 15:08:12 +0000559
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000560 if (isAcceptableNestedNameSpecifier(OuterDecl) &&
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000561 OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() &&
562 (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) ||
563 !Context.hasSameType(
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000564 Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)),
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000565 Context.getTypeDeclType(cast<TypeDecl>(SD))))) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000566 if (ErrorRecoveryLookup)
567 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000568
Douglas Gregor90c99722011-02-24 00:17:56 +0000569 Diag(IdentifierLoc,
570 diag::err_nested_name_member_ref_lookup_ambiguous)
571 << &Identifier;
572 Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type)
573 << ObjectType;
574 Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope);
575
576 // Fall through so that we'll pick the name we found in the object
577 // type, since that's probably what the user wanted anyway.
578 }
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000579 }
Mike Stump11289f42009-09-09 15:08:12 +0000580
Douglas Gregor90c99722011-02-24 00:17:56 +0000581 // If we're just performing this lookup for error-recovery purposes,
582 // don't extend the nested-name-specifier. Just return now.
583 if (ErrorRecoveryLookup)
584 return false;
585
586 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) {
587 SS.Extend(Context, Namespace, IdentifierLoc, CCLoc);
588 return false;
589 }
Mike Stump11289f42009-09-09 15:08:12 +0000590
Douglas Gregor90c99722011-02-24 00:17:56 +0000591 if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) {
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000592 SS.Extend(Context, Alias, IdentifierLoc, CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000593 return false;
594 }
Mike Stump11289f42009-09-09 15:08:12 +0000595
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000596 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
Douglas Gregor90c99722011-02-24 00:17:56 +0000597 TypeLocBuilder TLB;
598 if (isa<InjectedClassNameType>(T)) {
599 InjectedClassNameTypeLoc InjectedTL
600 = TLB.push<InjectedClassNameTypeLoc>(T);
601 InjectedTL.setNameLoc(IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000602 } else if (isa<RecordType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000603 RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T);
604 RecordTL.setNameLoc(IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000605 } else if (isa<TypedefType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000606 TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T);
607 TypedefTL.setNameLoc(IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000608 } else if (isa<EnumType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000609 EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T);
610 EnumTL.setNameLoc(IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000611 } else if (isa<TemplateTypeParmType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000612 TemplateTypeParmTypeLoc TemplateTypeTL
613 = TLB.push<TemplateTypeParmTypeLoc>(T);
614 TemplateTypeTL.setNameLoc(IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000615 } else if (isa<UnresolvedUsingType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000616 UnresolvedUsingTypeLoc UnresolvedTL
617 = TLB.push<UnresolvedUsingTypeLoc>(T);
618 UnresolvedTL.setNameLoc(IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000619 } else if (isa<SubstTemplateTypeParmType>(T)) {
620 SubstTemplateTypeParmTypeLoc TL
621 = TLB.push<SubstTemplateTypeParmTypeLoc>(T);
622 TL.setNameLoc(IdentifierLoc);
623 } else if (isa<SubstTemplateTypeParmPackType>(T)) {
624 SubstTemplateTypeParmPackTypeLoc TL
625 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(T);
626 TL.setNameLoc(IdentifierLoc);
627 } else {
628 llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier");
Douglas Gregor90c99722011-02-24 00:17:56 +0000629 }
630
Richard Smith91c7bbd2011-10-20 03:28:47 +0000631 if (T->isEnumeralType())
632 Diag(IdentifierLoc, diag::warn_cxx98_compat_enum_nested_name_spec);
633
Douglas Gregor90c99722011-02-24 00:17:56 +0000634 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
635 CCLoc);
636 return false;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000637 }
Mike Stump11289f42009-09-09 15:08:12 +0000638
Chris Lattner1c428032009-12-07 01:36:53 +0000639 // Otherwise, we have an error case. If we don't want diagnostics, just
640 // return an error now.
641 if (ErrorRecoveryLookup)
Douglas Gregor90c99722011-02-24 00:17:56 +0000642 return true;
Chris Lattner1c428032009-12-07 01:36:53 +0000643
Cedric Venet084381332009-02-14 20:20:19 +0000644 // If we didn't find anything during our lookup, try again with
645 // ordinary name lookup, which can help us produce better error
646 // messages.
John McCall67c00872009-12-02 08:25:40 +0000647 if (Found.empty()) {
John McCall27b18f82009-11-17 02:14:36 +0000648 Found.clear(LookupOrdinaryName);
649 LookupName(Found, S);
John McCall9f3059a2009-10-09 21:13:30 +0000650 }
Mike Stump11289f42009-09-09 15:08:12 +0000651
Francois Pichetb23dc092011-07-27 01:05:24 +0000652 // In Microsoft mode, if we are within a templated function and we can't
653 // resolve Identifier, then extend the SS with Identifier. This will have
654 // the effect of resolving Identifier during template instantiation.
655 // The goal is to be able to resolve a function call whose
656 // nested-name-specifier is located inside a dependent base class.
657 // Example:
658 //
659 // class C {
660 // public:
661 // static void foo2() { }
662 // };
663 // template <class T> class A { public: typedef C D; };
664 //
665 // template <class T> class B : public A<T> {
666 // public:
667 // void foo() { D::foo2(); }
668 // };
David Blaikiebbafb8a2012-03-11 07:00:24 +0000669 if (getLangOpts().MicrosoftExt) {
Francois Pichetb23dc092011-07-27 01:05:24 +0000670 DeclContext *DC = LookupCtx ? LookupCtx : CurContext;
671 if (DC->isDependentContext() && DC->isFunctionOrMethod()) {
672 SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc);
673 return false;
674 }
675 }
676
Cedric Venet084381332009-02-14 20:20:19 +0000677 unsigned DiagID;
John McCall67c00872009-12-02 08:25:40 +0000678 if (!Found.empty())
Cedric Venet084381332009-02-14 20:20:19 +0000679 DiagID = diag::err_expected_class_or_namespace;
Anders Carlssonb533df02009-08-30 07:09:50 +0000680 else if (SS.isSet()) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000681 Diag(IdentifierLoc, diag::err_no_member)
682 << &Identifier << LookupCtx << SS.getRange();
683 return true;
Anders Carlssonb533df02009-08-30 07:09:50 +0000684 } else
Cedric Venet084381332009-02-14 20:20:19 +0000685 DiagID = diag::err_undeclared_var_use;
Mike Stump11289f42009-09-09 15:08:12 +0000686
Cedric Venet084381332009-02-14 20:20:19 +0000687 if (SS.isSet())
Douglas Gregor90c99722011-02-24 00:17:56 +0000688 Diag(IdentifierLoc, DiagID) << &Identifier << SS.getRange();
Cedric Venet084381332009-02-14 20:20:19 +0000689 else
Douglas Gregor90c99722011-02-24 00:17:56 +0000690 Diag(IdentifierLoc, DiagID) << &Identifier;
Mike Stump11289f42009-09-09 15:08:12 +0000691
Douglas Gregor90c99722011-02-24 00:17:56 +0000692 return true;
Cedric Venet084381332009-02-14 20:20:19 +0000693}
694
Douglas Gregor90c99722011-02-24 00:17:56 +0000695bool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
696 IdentifierInfo &Identifier,
697 SourceLocation IdentifierLoc,
698 SourceLocation CCLoc,
699 ParsedType ObjectType,
700 bool EnteringContext,
701 CXXScopeSpec &SS) {
702 if (SS.isInvalid())
703 return true;
704
705 return BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, CCLoc,
706 GetTypeFromParser(ObjectType),
707 EnteringContext, SS,
708 /*ScopeLookupResult=*/0, false);
Chris Lattner1c428032009-12-07 01:36:53 +0000709}
710
David Blaikie15a430a2011-12-04 05:04:18 +0000711bool Sema::ActOnCXXNestedNameSpecifierDecltype(CXXScopeSpec &SS,
712 const DeclSpec &DS,
713 SourceLocation ColonColonLoc) {
714 if (SS.isInvalid() || DS.getTypeSpecType() == DeclSpec::TST_error)
715 return true;
716
717 assert(DS.getTypeSpecType() == DeclSpec::TST_decltype);
718
719 QualType T = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc());
720 if (!T->isDependentType() && !T->getAs<TagType>()) {
721 Diag(DS.getTypeSpecTypeLoc(), diag::err_expected_class)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000722 << T << getLangOpts().CPlusPlus;
David Blaikie15a430a2011-12-04 05:04:18 +0000723 return true;
724 }
725
726 TypeLocBuilder TLB;
727 DecltypeTypeLoc DecltypeTL = TLB.push<DecltypeTypeLoc>(T);
728 DecltypeTL.setNameLoc(DS.getTypeSpecTypeLoc());
729 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
730 ColonColonLoc);
731 return false;
732}
733
Chris Lattner1c428032009-12-07 01:36:53 +0000734/// IsInvalidUnlessNestedName - This method is used for error recovery
735/// purposes to determine whether the specified identifier is only valid as
736/// a nested name specifier, for example a namespace name. It is
737/// conservatively correct to always return false from this method.
738///
739/// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +0000740bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS,
Douglas Gregor90c99722011-02-24 00:17:56 +0000741 IdentifierInfo &Identifier,
742 SourceLocation IdentifierLoc,
743 SourceLocation ColonLoc,
744 ParsedType ObjectType,
Chris Lattner1c428032009-12-07 01:36:53 +0000745 bool EnteringContext) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000746 if (SS.isInvalid())
747 return false;
748
749 return !BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, ColonLoc,
750 GetTypeFromParser(ObjectType),
751 EnteringContext, SS,
752 /*ScopeLookupResult=*/0, true);
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000753}
754
Douglas Gregor90c99722011-02-24 00:17:56 +0000755bool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000756 CXXScopeSpec &SS,
757 SourceLocation TemplateKWLoc,
Douglas Gregor6e068012011-02-28 00:04:36 +0000758 TemplateTy Template,
759 SourceLocation TemplateNameLoc,
760 SourceLocation LAngleLoc,
761 ASTTemplateArgsPtr TemplateArgsIn,
762 SourceLocation RAngleLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +0000763 SourceLocation CCLoc,
Douglas Gregor6e068012011-02-28 00:04:36 +0000764 bool EnteringContext) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000765 if (SS.isInvalid())
766 return true;
767
Douglas Gregor6e068012011-02-28 00:04:36 +0000768 // Translate the parser's template argument list in our AST format.
769 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
770 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
771
772 if (DependentTemplateName *DTN = Template.get().getAsDependentTemplateName()){
773 // Handle a dependent template specialization for which we cannot resolve
774 // the template name.
775 assert(DTN->getQualifier()
776 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
777 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
Douglas Gregora7a795b2011-03-01 20:11:18 +0000778 DTN->getQualifier(),
779 DTN->getIdentifier(),
Douglas Gregor6e068012011-02-28 00:04:36 +0000780 TemplateArgs);
781
782 // Create source-location information for this type.
783 TypeLocBuilder Builder;
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000784 DependentTemplateSpecializationTypeLoc SpecTL
Douglas Gregor6e068012011-02-28 00:04:36 +0000785 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000786 SpecTL.setElaboratedKeywordLoc(SourceLocation());
787 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000788 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000789 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000790 SpecTL.setLAngleLoc(LAngleLoc);
791 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000792 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
793 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
794
Abramo Bagnara7945c982012-01-27 09:46:47 +0000795 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
Douglas Gregor6e068012011-02-28 00:04:36 +0000796 CCLoc);
797 return false;
798 }
799
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000800
801 if (Template.get().getAsOverloadedTemplate() ||
802 isa<FunctionTemplateDecl>(Template.get().getAsTemplateDecl())) {
803 SourceRange R(TemplateNameLoc, RAngleLoc);
804 if (SS.getRange().isValid())
805 R.setBegin(SS.getRange().getBegin());
806
807 Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier)
808 << Template.get() << R;
809 NoteAllFoundTemplates(Template.get());
810 return true;
811 }
812
Douglas Gregor6e068012011-02-28 00:04:36 +0000813 // We were able to resolve the template name to an actual template.
814 // Build an appropriate nested-name-specifier.
815 QualType T = CheckTemplateIdType(Template.get(), TemplateNameLoc,
816 TemplateArgs);
Douglas Gregor90c99722011-02-24 00:17:56 +0000817 if (T.isNull())
818 return true;
819
Richard Smith3f1b5d02011-05-05 21:57:07 +0000820 // Alias template specializations can produce types which are not valid
821 // nested name specifiers.
822 if (!T->isDependentType() && !T->getAs<TagType>()) {
823 Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T;
824 NoteAllFoundTemplates(Template.get());
825 return true;
826 }
Douglas Gregor6e068012011-02-28 00:04:36 +0000827
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000828 // Provide source-location information for the template specialization type.
Douglas Gregor6e068012011-02-28 00:04:36 +0000829 TypeLocBuilder Builder;
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000830 TemplateSpecializationTypeLoc SpecTL
Douglas Gregor6e068012011-02-28 00:04:36 +0000831 = Builder.push<TemplateSpecializationTypeLoc>(T);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000832 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
833 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000834 SpecTL.setLAngleLoc(LAngleLoc);
835 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor6e068012011-02-28 00:04:36 +0000836 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
837 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
838
839
Abramo Bagnara7945c982012-01-27 09:46:47 +0000840 SS.Extend(Context, TemplateKWLoc, Builder.getTypeLocInContext(Context, T),
Douglas Gregor6e068012011-02-28 00:04:36 +0000841 CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000842 return false;
Douglas Gregor7f741122009-02-25 19:37:18 +0000843}
844
Douglas Gregor869ad452011-02-24 17:54:50 +0000845namespace {
846 /// \brief A structure that stores a nested-name-specifier annotation,
847 /// including both the nested-name-specifier
848 struct NestedNameSpecifierAnnotation {
849 NestedNameSpecifier *NNS;
850 };
851}
852
853void *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) {
854 if (SS.isEmpty() || SS.isInvalid())
855 return 0;
856
857 void *Mem = Context.Allocate((sizeof(NestedNameSpecifierAnnotation) +
858 SS.location_size()),
859 llvm::alignOf<NestedNameSpecifierAnnotation>());
860 NestedNameSpecifierAnnotation *Annotation
861 = new (Mem) NestedNameSpecifierAnnotation;
862 Annotation->NNS = SS.getScopeRep();
863 memcpy(Annotation + 1, SS.location_data(), SS.location_size());
864 return Annotation;
865}
866
867void Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr,
868 SourceRange AnnotationRange,
869 CXXScopeSpec &SS) {
870 if (!AnnotationPtr) {
871 SS.SetInvalid(AnnotationRange);
872 return;
873 }
874
875 NestedNameSpecifierAnnotation *Annotation
876 = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr);
877 SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1));
878}
879
John McCall2b058ef2009-12-11 20:04:54 +0000880bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
881 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
882
883 NestedNameSpecifier *Qualifier =
884 static_cast<NestedNameSpecifier*>(SS.getScopeRep());
885
886 // There are only two places a well-formed program may qualify a
887 // declarator: first, when defining a namespace or class member
888 // out-of-line, and second, when naming an explicitly-qualified
889 // friend function. The latter case is governed by
890 // C++03 [basic.lookup.unqual]p10:
891 // In a friend declaration naming a member function, a name used
892 // in the function declarator and not part of a template-argument
893 // in a template-id is first looked up in the scope of the member
894 // function's class. If it is not found, or if the name is part of
895 // a template-argument in a template-id, the look up is as
896 // described for unqualified names in the definition of the class
897 // granting friendship.
898 // i.e. we don't push a scope unless it's a class member.
899
900 switch (Qualifier->getKind()) {
901 case NestedNameSpecifier::Global:
902 case NestedNameSpecifier::Namespace:
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000903 case NestedNameSpecifier::NamespaceAlias:
John McCall2b058ef2009-12-11 20:04:54 +0000904 // These are always namespace scopes. We never want to enter a
905 // namespace scope from anything but a file context.
Sebastian Redl50c68252010-08-31 00:36:30 +0000906 return CurContext->getRedeclContext()->isFileContext();
John McCall2b058ef2009-12-11 20:04:54 +0000907
908 case NestedNameSpecifier::Identifier:
909 case NestedNameSpecifier::TypeSpec:
910 case NestedNameSpecifier::TypeSpecWithTemplate:
911 // These are never namespace scopes.
912 return true;
913 }
914
David Blaikie8a40f702012-01-17 06:56:22 +0000915 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
John McCall2b058ef2009-12-11 20:04:54 +0000916}
917
Cedric Venet084381332009-02-14 20:20:19 +0000918/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
919/// scope or nested-name-specifier) is parsed, part of a declarator-id.
920/// After this method is called, according to [C++ 3.4.3p3], names should be
921/// looked up in the declarator-id's scope, until the declarator is parsed and
922/// ActOnCXXExitDeclaratorScope is called.
923/// The 'SS' should be a non-empty valid CXXScopeSpec.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +0000924bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) {
Cedric Venet084381332009-02-14 20:20:19 +0000925 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
John McCall6df5fef2009-12-19 10:49:29 +0000926
927 if (SS.isInvalid()) return true;
928
929 DeclContext *DC = computeDeclContext(SS, true);
930 if (!DC) return true;
931
932 // Before we enter a declarator's context, we need to make sure that
933 // it is a complete declaration context.
John McCall0b66eb32010-05-01 00:40:08 +0000934 if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC))
John McCall6df5fef2009-12-19 10:49:29 +0000935 return true;
936
937 EnterDeclaratorContext(S, DC);
John McCall2408e322010-04-27 00:57:59 +0000938
939 // Rebuild the nested name specifier for the new scope.
940 if (DC->isDependentContext())
941 RebuildNestedNameSpecifierInCurrentInstantiation(SS);
942
Douglas Gregor5013a7e2009-09-24 23:39:01 +0000943 return false;
Cedric Venet084381332009-02-14 20:20:19 +0000944}
945
946/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
947/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
948/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
949/// Used to indicate that names should revert to being looked up in the
950/// defining scope.
951void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
952 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
Douglas Gregor053f6912009-08-26 00:04:55 +0000953 if (SS.isInvalid())
954 return;
John McCall6df5fef2009-12-19 10:49:29 +0000955 assert(!SS.isInvalid() && computeDeclContext(SS, true) &&
956 "exiting declarator scope we never really entered");
957 ExitDeclaratorContext(S);
Cedric Venet084381332009-02-14 20:20:19 +0000958}