blob: e54857196a3cb889e9a5d37112f419f055f00738 [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"
Cedric Venet084381332009-02-14 20:20:19 +000016#include "clang/AST/ASTContext.h"
Douglas Gregorc9f9b862009-05-11 19:58:34 +000017#include "clang/AST/DeclTemplate.h"
Douglas Gregord8061562009-08-06 03:17:00 +000018#include "clang/AST/ExprCXX.h"
Douglas Gregor52537682009-03-19 00:18:19 +000019#include "clang/AST/NestedNameSpecifier.h"
Anders Carlssond624e162009-08-26 23:45:07 +000020#include "clang/Basic/PartialDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000021#include "clang/Sema/DeclSpec.h"
Douglas Gregor90c99722011-02-24 00:17:56 +000022#include "TypeLocBuilder.h"
Cedric Venet084381332009-02-14 20:20:19 +000023#include "llvm/ADT/STLExtras.h"
Douglas Gregor168190d2009-07-22 00:28:09 +000024#include "llvm/Support/raw_ostream.h"
Cedric Venet084381332009-02-14 20:20:19 +000025using namespace clang;
26
Douglas Gregor41127182009-11-04 22:49:18 +000027/// \brief Find the current instantiation that associated with the given type.
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000028static CXXRecordDecl *getCurrentInstantiationOf(QualType T,
29 DeclContext *CurContext) {
Douglas Gregor41127182009-11-04 22:49:18 +000030 if (T.isNull())
31 return 0;
John McCall2408e322010-04-27 00:57:59 +000032
33 const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000034 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
35 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
36 if (!T->isDependentType())
37 return Record;
38
39 // This may be a member of a class template or class template partial
40 // specialization. If it's part of the current semantic context, then it's
41 // an injected-class-name;
42 for (; !CurContext->isFileContext(); CurContext = CurContext->getParent())
43 if (CurContext->Equals(Record))
44 return Record;
45
46 return 0;
47 } else if (isa<InjectedClassNameType>(Ty))
John McCall2408e322010-04-27 00:57:59 +000048 return cast<InjectedClassNameType>(Ty)->getDecl();
49 else
50 return 0;
Douglas Gregor41127182009-11-04 22:49:18 +000051}
52
Douglas Gregorb7bfe792009-09-02 22:59:36 +000053/// \brief Compute the DeclContext that is associated with the given type.
54///
55/// \param T the type for which we are attempting to find a DeclContext.
56///
Mike Stump11289f42009-09-09 15:08:12 +000057/// \returns the declaration context represented by the type T,
Douglas Gregorb7bfe792009-09-02 22:59:36 +000058/// or NULL if the declaration context cannot be computed (e.g., because it is
59/// dependent and not the current instantiation).
60DeclContext *Sema::computeDeclContext(QualType T) {
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000061 if (!T->isDependentType())
62 if (const TagType *Tag = T->getAs<TagType>())
63 return Tag->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +000064
Douglas Gregorbf2b26d2011-02-19 19:24:40 +000065 return ::getCurrentInstantiationOf(T, CurContext);
Douglas Gregorb7bfe792009-09-02 22:59:36 +000066}
67
Douglas Gregor52537682009-03-19 00:18:19 +000068/// \brief Compute the DeclContext that is associated with the given
69/// scope specifier.
Douglas Gregord8d297c2009-07-21 23:53:31 +000070///
71/// \param SS the C++ scope specifier as it appears in the source
72///
73/// \param EnteringContext when true, we will be entering the context of
74/// this scope specifier, so we can retrieve the declaration context of a
75/// class template or class template partial specialization even if it is
76/// not the current instantiation.
77///
78/// \returns the declaration context represented by the scope specifier @p SS,
79/// or NULL if the declaration context cannot be computed (e.g., because it is
80/// dependent and not the current instantiation).
81DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS,
82 bool EnteringContext) {
Douglas Gregor52537682009-03-19 00:18:19 +000083 if (!SS.isSet() || SS.isInvalid())
Douglas Gregor6bfde492009-03-18 00:36:05 +000084 return 0;
Douglas Gregor6bfde492009-03-18 00:36:05 +000085
Mike Stump11289f42009-09-09 15:08:12 +000086 NestedNameSpecifier *NNS
Douglas Gregorc23500e2009-03-26 23:56:24 +000087 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorc9f9b862009-05-11 19:58:34 +000088 if (NNS->isDependent()) {
89 // If this nested-name-specifier refers to the current
90 // instantiation, return its DeclContext.
91 if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS))
92 return Record;
Mike Stump11289f42009-09-09 15:08:12 +000093
Douglas Gregord8d297c2009-07-21 23:53:31 +000094 if (EnteringContext) {
John McCalle78aac42010-03-10 03:28:59 +000095 const Type *NNSType = NNS->getAsType();
96 if (!NNSType) {
Richard Smith3f1b5d02011-05-05 21:57:07 +000097 return 0;
98 }
99
100 // Look through type alias templates, per C++0x [temp.dep.type]p1.
101 NNSType = Context.getCanonicalType(NNSType);
102 if (const TemplateSpecializationType *SpecType
103 = NNSType->getAs<TemplateSpecializationType>()) {
Douglas Gregore861bac2009-08-25 22:51:20 +0000104 // We are entering the context of the nested name specifier, so try to
105 // match the nested name specifier to either a primary class template
106 // or a class template partial specialization.
Mike Stump11289f42009-09-09 15:08:12 +0000107 if (ClassTemplateDecl *ClassTemplate
Douglas Gregord8d297c2009-07-21 23:53:31 +0000108 = dyn_cast_or_null<ClassTemplateDecl>(
109 SpecType->getTemplateName().getAsTemplateDecl())) {
Douglas Gregor15301382009-07-30 17:40:51 +0000110 QualType ContextType
111 = Context.getCanonicalType(QualType(SpecType, 0));
112
Douglas Gregord8d297c2009-07-21 23:53:31 +0000113 // If the type of the nested name specifier is the same as the
114 // injected class name of the named class template, we're entering
115 // into that class template definition.
John McCalle78aac42010-03-10 03:28:59 +0000116 QualType Injected
Douglas Gregor9961ce92010-07-08 18:37:38 +0000117 = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregor15301382009-07-30 17:40:51 +0000118 if (Context.hasSameType(Injected, ContextType))
Douglas Gregord8d297c2009-07-21 23:53:31 +0000119 return ClassTemplate->getTemplatedDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000120
Douglas Gregor15301382009-07-30 17:40:51 +0000121 // If the type of the nested name specifier is the same as the
122 // type of one of the class template's class template partial
123 // specializations, we're entering into the definition of that
124 // class template partial specialization.
125 if (ClassTemplatePartialSpecializationDecl *PartialSpec
126 = ClassTemplate->findPartialSpecialization(ContextType))
127 return PartialSpec;
Douglas Gregord8d297c2009-07-21 23:53:31 +0000128 }
John McCalle78aac42010-03-10 03:28:59 +0000129 } else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) {
Douglas Gregore861bac2009-08-25 22:51:20 +0000130 // The nested name specifier refers to a member of a class template.
131 return RecordT->getDecl();
Douglas Gregord8d297c2009-07-21 23:53:31 +0000132 }
133 }
Mike Stump11289f42009-09-09 15:08:12 +0000134
Douglas Gregord8d297c2009-07-21 23:53:31 +0000135 return 0;
Douglas Gregorc9f9b862009-05-11 19:58:34 +0000136 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000137
138 switch (NNS->getKind()) {
139 case NestedNameSpecifier::Identifier:
140 assert(false && "Dependent nested-name-specifier has no DeclContext");
141 break;
142
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();
154 } break;
Douglas Gregorf21eb492009-03-26 23:50:42 +0000155
156 case NestedNameSpecifier::Global:
157 return Context.getTranslationUnitDecl();
158 }
159
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000160 // Required to silence a GCC warning.
Douglas Gregorf21eb492009-03-26 23:50:42 +0000161 return 0;
Douglas Gregor6bfde492009-03-18 00:36:05 +0000162}
163
Douglas Gregor90a1a652009-03-19 17:26:29 +0000164bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) {
165 if (!SS.isSet() || SS.isInvalid())
166 return false;
167
Mike Stump11289f42009-09-09 15:08:12 +0000168 NestedNameSpecifier *NNS
Douglas Gregorc23500e2009-03-26 23:56:24 +0000169 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorf21eb492009-03-26 23:50:42 +0000170 return NNS->isDependent();
Douglas Gregor90a1a652009-03-19 17:26:29 +0000171}
172
Douglas Gregorc9f9b862009-05-11 19:58:34 +0000173// \brief Determine whether this C++ scope specifier refers to an
174// unknown specialization, i.e., a dependent type that is not the
175// current instantiation.
176bool Sema::isUnknownSpecialization(const CXXScopeSpec &SS) {
177 if (!isDependentScopeSpecifier(SS))
178 return false;
179
Mike Stump11289f42009-09-09 15:08:12 +0000180 NestedNameSpecifier *NNS
Douglas Gregorc9f9b862009-05-11 19:58:34 +0000181 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
182 return getCurrentInstantiationOf(NNS) == 0;
183}
184
185/// \brief If the given nested name specifier refers to the current
186/// instantiation, return the declaration that corresponds to that
187/// current instantiation (C++0x [temp.dep.type]p1).
188///
189/// \param NNS a dependent nested name specifier.
190CXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) {
191 assert(getLangOptions().CPlusPlus && "Only callable in C++");
192 assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed");
193
Douglas Gregord8d297c2009-07-21 23:53:31 +0000194 if (!NNS->getAsType())
195 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000196
Douglas Gregorb9a955d2009-07-31 18:32:42 +0000197 QualType T = QualType(NNS->getAsType(), 0);
Douglas Gregorbf2b26d2011-02-19 19:24:40 +0000198 return ::getCurrentInstantiationOf(T, CurContext);
Douglas Gregorc9f9b862009-05-11 19:58:34 +0000199}
200
Douglas Gregor26897462009-03-11 16:48:53 +0000201/// \brief Require that the context specified by SS be complete.
202///
203/// If SS refers to a type, this routine checks whether the type is
204/// complete enough (or can be made complete enough) for name lookup
205/// into the DeclContext. A type that is not yet completed can be
206/// considered "complete enough" if it is a class/struct/union/enum
207/// that is currently being defined. Or, if we have a type that names
208/// a class template specialization that is not a complete type, we
209/// will attempt to instantiate that class template.
John McCall0b66eb32010-05-01 00:40:08 +0000210bool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS,
211 DeclContext *DC) {
212 assert(DC != 0 && "given null context");
Mike Stump11289f42009-09-09 15:08:12 +0000213
Douglas Gregor26897462009-03-11 16:48:53 +0000214 if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
Douglas Gregor8a6d15d2010-02-05 04:39:02 +0000215 // If this is a dependent type, then we consider it complete.
216 if (Tag->isDependentContext())
217 return false;
218
Douglas Gregor26897462009-03-11 16:48:53 +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.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000221 const TagType *TagT = Context.getTypeDeclType(Tag)->getAs<TagType>();
John McCalle78aac42010-03-10 03:28:59 +0000222 if (TagT && TagT->isBeingDefined())
Douglas Gregor26897462009-03-11 16:48:53 +0000223 return false;
224
225 // The type must be complete.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +0000226 if (RequireCompleteType(SS.getRange().getBegin(),
227 Context.getTypeDeclType(Tag),
228 PDiag(diag::err_incomplete_nested_name_spec)
John McCall0b66eb32010-05-01 00:40:08 +0000229 << SS.getRange())) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000230 SS.SetInvalid(SS.getRange());
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +0000231 return true;
232 }
Douglas Gregor26897462009-03-11 16:48:53 +0000233 }
234
235 return false;
236}
Cedric Venet084381332009-02-14 20:20:19 +0000237
Douglas Gregor90c99722011-02-24 00:17:56 +0000238bool Sema::ActOnCXXGlobalScopeSpecifier(Scope *S, SourceLocation CCLoc,
239 CXXScopeSpec &SS) {
240 SS.MakeGlobal(Context, CCLoc);
241 return false;
Cedric Venet084381332009-02-14 20:20:19 +0000242}
243
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000244/// \brief Determines whether the given declaration is an valid acceptable
245/// result for name lookup of a nested-name-specifier.
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000246bool Sema::isAcceptableNestedNameSpecifier(NamedDecl *SD) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000247 if (!SD)
248 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000249
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000250 // Namespace and namespace aliases are fine.
251 if (isa<NamespaceDecl>(SD) || isa<NamespaceAliasDecl>(SD))
252 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000253
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000254 if (!isa<TypeDecl>(SD))
255 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000256
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000257 // Determine whether we have a class (or, in C++0x, an enum) or
258 // a typedef thereof. If so, build the nested-name-specifier.
259 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
260 if (T->isDependentType())
261 return true;
Richard Smithdda56e42011-04-15 14:24:37 +0000262 else if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(SD)) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000263 if (TD->getUnderlyingType()->isRecordType() ||
Mike Stump11289f42009-09-09 15:08:12 +0000264 (Context.getLangOptions().CPlusPlus0x &&
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000265 TD->getUnderlyingType()->isEnumeralType()))
266 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000267 } else if (isa<RecordDecl>(SD) ||
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000268 (Context.getLangOptions().CPlusPlus0x && isa<EnumDecl>(SD)))
269 return true;
270
271 return false;
272}
273
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000274/// \brief If the given nested-name-specifier begins with a bare identifier
Mike Stump11289f42009-09-09 15:08:12 +0000275/// (e.g., Base::), perform name lookup for that identifier as a
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000276/// nested-name-specifier within the given scope, and return the result of that
277/// name lookup.
278NamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) {
279 if (!S || !NNS)
280 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000281
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000282 while (NNS->getPrefix())
283 NNS = NNS->getPrefix();
Mike Stump11289f42009-09-09 15:08:12 +0000284
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000285 if (NNS->getKind() != NestedNameSpecifier::Identifier)
286 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000287
John McCall27b18f82009-11-17 02:14:36 +0000288 LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(),
289 LookupNestedNameSpecifierName);
290 LookupName(Found, S);
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000291 assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet");
292
John McCall67c00872009-12-02 08:25:40 +0000293 if (!Found.isSingleResult())
294 return 0;
295
296 NamedDecl *Result = Found.getFoundDecl();
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000297 if (isAcceptableNestedNameSpecifier(Result))
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000298 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000299
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000300 return 0;
301}
302
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +0000303bool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS,
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000304 SourceLocation IdLoc,
305 IdentifierInfo &II,
John McCallba7bf592010-08-24 05:47:05 +0000306 ParsedType ObjectTypePtr) {
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000307 QualType ObjectType = GetTypeFromParser(ObjectTypePtr);
308 LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName);
309
310 // Determine where to perform name lookup
311 DeclContext *LookupCtx = 0;
312 bool isDependent = false;
313 if (!ObjectType.isNull()) {
314 // This nested-name-specifier occurs in a member access expression, e.g.,
315 // x->B::f, and we are looking into the type of the object.
316 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
317 LookupCtx = computeDeclContext(ObjectType);
318 isDependent = ObjectType->isDependentType();
319 } else if (SS.isSet()) {
320 // This nested-name-specifier occurs after another nested-name-specifier,
321 // so long into the context associated with the prior nested-name-specifier.
322 LookupCtx = computeDeclContext(SS, false);
323 isDependent = isDependentScopeSpecifier(SS);
324 Found.setContextRange(SS.getRange());
325 }
326
327 if (LookupCtx) {
328 // Perform "qualified" name lookup into the declaration context we
329 // computed, which is either the type of the base of a member access
330 // expression or the declaration context associated with a prior
331 // nested-name-specifier.
332
333 // The declaration context must be complete.
John McCall0b66eb32010-05-01 00:40:08 +0000334 if (!LookupCtx->isDependentContext() &&
335 RequireCompleteDeclContext(SS, LookupCtx))
Douglas Gregor0d5b0a12010-02-24 21:29:12 +0000336 return false;
337
338 LookupQualifiedName(Found, LookupCtx);
339 } else if (isDependent) {
340 return false;
341 } else {
342 LookupName(Found, S);
343 }
344 Found.suppressDiagnostics();
345
346 if (NamedDecl *ND = Found.getAsSingle<NamedDecl>())
347 return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND);
348
349 return false;
350}
351
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000352/// \brief Build a new nested-name-specifier for "identifier::", as described
353/// by ActOnCXXNestedNameSpecifier.
354///
355/// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in
356/// that it contains an extra parameter \p ScopeLookupResult, which provides
357/// the result of name lookup within the scope of the nested-name-specifier
Douglas Gregorad183ac2009-12-30 16:01:52 +0000358/// that was computed at template definition time.
Chris Lattner1c428032009-12-07 01:36:53 +0000359///
360/// If ErrorRecoveryLookup is true, then this call is used to improve error
361/// recovery. This means that it should not emit diagnostics, it should
Douglas Gregor90c99722011-02-24 00:17:56 +0000362/// just return true on failure. It also means it should only return a valid
Chris Lattner1c428032009-12-07 01:36:53 +0000363/// scope if it *knows* that the result is correct. It should not return in a
Douglas Gregor90c99722011-02-24 00:17:56 +0000364/// dependent context, for example. Nor will it extend \p SS with the scope
365/// specifier.
366bool Sema::BuildCXXNestedNameSpecifier(Scope *S,
367 IdentifierInfo &Identifier,
368 SourceLocation IdentifierLoc,
369 SourceLocation CCLoc,
370 QualType ObjectType,
371 bool EnteringContext,
372 CXXScopeSpec &SS,
373 NamedDecl *ScopeLookupResult,
374 bool ErrorRecoveryLookup) {
375 LookupResult Found(*this, &Identifier, IdentifierLoc,
376 LookupNestedNameSpecifierName);
John McCall27b18f82009-11-17 02:14:36 +0000377
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000378 // Determine where to perform name lookup
379 DeclContext *LookupCtx = 0;
380 bool isDependent = false;
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000381 if (!ObjectType.isNull()) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000382 // This nested-name-specifier occurs in a member access expression, e.g.,
383 // x->B::f, and we are looking into the type of the object.
384 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000385 LookupCtx = computeDeclContext(ObjectType);
386 isDependent = ObjectType->isDependentType();
387 } else if (SS.isSet()) {
388 // This nested-name-specifier occurs after another nested-name-specifier,
Richard Smith3f1b5d02011-05-05 21:57:07 +0000389 // so look into the context associated with the prior nested-name-specifier.
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000390 LookupCtx = computeDeclContext(SS, EnteringContext);
391 isDependent = isDependentScopeSpecifier(SS);
John McCall27b18f82009-11-17 02:14:36 +0000392 Found.setContextRange(SS.getRange());
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000393 }
Mike Stump11289f42009-09-09 15:08:12 +0000394
John McCall27b18f82009-11-17 02:14:36 +0000395
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000396 bool ObjectTypeSearchedInScope = false;
397 if (LookupCtx) {
Mike Stump11289f42009-09-09 15:08:12 +0000398 // Perform "qualified" name lookup into the declaration context we
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000399 // computed, which is either the type of the base of a member access
Mike Stump11289f42009-09-09 15:08:12 +0000400 // expression or the declaration context associated with a prior
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000401 // nested-name-specifier.
Mike Stump11289f42009-09-09 15:08:12 +0000402
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000403 // The declaration context must be complete.
John McCall0b66eb32010-05-01 00:40:08 +0000404 if (!LookupCtx->isDependentContext() &&
405 RequireCompleteDeclContext(SS, LookupCtx))
Douglas Gregor90c99722011-02-24 00:17:56 +0000406 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000407
John McCall27b18f82009-11-17 02:14:36 +0000408 LookupQualifiedName(Found, LookupCtx);
Mike Stump11289f42009-09-09 15:08:12 +0000409
John McCall27b18f82009-11-17 02:14:36 +0000410 if (!ObjectType.isNull() && Found.empty()) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000411 // C++ [basic.lookup.classref]p4:
412 // If the id-expression in a class member access is a qualified-id of
Mike Stump11289f42009-09-09 15:08:12 +0000413 // the form
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000414 //
415 // class-name-or-namespace-name::...
416 //
Mike Stump11289f42009-09-09 15:08:12 +0000417 // the class-name-or-namespace-name following the . or -> operator is
418 // looked up both in the context of the entire postfix-expression and in
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000419 // the scope of the class of the object expression. If the name is found
Mike Stump11289f42009-09-09 15:08:12 +0000420 // only in the scope of the class of the object expression, the name
421 // shall refer to a class-name. If the name is found only in the
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000422 // context of the entire postfix-expression, the name shall refer to a
423 // class-name or namespace-name. [...]
424 //
425 // Qualified name lookup into a class will not find a namespace-name,
Douglas Gregor9d07dfa2011-05-15 17:27:27 +0000426 // so we do not need to diagnose that case specifically. However,
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000427 // this qualified name lookup may find nothing. In that case, perform
Mike Stump11289f42009-09-09 15:08:12 +0000428 // unqualified name lookup in the given scope (if available) or
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000429 // reconstruct the result from when name lookup was performed at template
430 // definition time.
431 if (S)
John McCall27b18f82009-11-17 02:14:36 +0000432 LookupName(Found, S);
John McCall9f3059a2009-10-09 21:13:30 +0000433 else if (ScopeLookupResult)
434 Found.addDecl(ScopeLookupResult);
Mike Stump11289f42009-09-09 15:08:12 +0000435
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000436 ObjectTypeSearchedInScope = true;
437 }
Douglas Gregordf65c8ed2010-07-28 14:49:07 +0000438 } else if (!isDependent) {
439 // Perform unqualified name lookup in the current scope.
440 LookupName(Found, S);
441 }
442
443 // If we performed lookup into a dependent context and did not find anything,
444 // that's fine: just build a dependent nested-name-specifier.
445 if (Found.empty() && isDependent &&
446 !(LookupCtx && LookupCtx->isRecord() &&
447 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
448 !cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()))) {
Chris Lattner1c428032009-12-07 01:36:53 +0000449 // Don't speculate if we're just trying to improve error recovery.
450 if (ErrorRecoveryLookup)
Douglas Gregor90c99722011-02-24 00:17:56 +0000451 return true;
Chris Lattner1c428032009-12-07 01:36:53 +0000452
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000453 // We were not able to compute the declaration context for a dependent
Mike Stump11289f42009-09-09 15:08:12 +0000454 // base object type or prior nested-name-specifier, so this
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000455 // nested-name-specifier refers to an unknown specialization. Just build
456 // a dependent nested-name-specifier.
Douglas Gregor90c99722011-02-24 00:17:56 +0000457 SS.Extend(Context, &Identifier, IdentifierLoc, CCLoc);
458 return false;
Douglas Gregordf65c8ed2010-07-28 14:49:07 +0000459 }
460
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000461 // FIXME: Deal with ambiguities cleanly.
Douglas Gregor532e68f2009-12-31 08:26:35 +0000462
463 if (Found.empty() && !ErrorRecoveryLookup) {
464 // We haven't found anything, and we're not recovering from a
465 // different kind of error, so look for typos.
466 DeclarationName Name = Found.getLookupName();
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000467 TypoCorrection Corrected;
468 Found.clear();
469 if ((Corrected = CorrectTypo(Found.getLookupNameInfo(),
470 Found.getLookupKind(), S, &SS, LookupCtx,
471 EnteringContext, CTC_NoKeywords)) &&
472 isAcceptableNestedNameSpecifier(Corrected.getCorrectionDecl())) {
473 std::string CorrectedStr(Corrected.getAsString(getLangOptions()));
474 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions()));
Douglas Gregor532e68f2009-12-31 08:26:35 +0000475 if (LookupCtx)
476 Diag(Found.getNameLoc(), diag::err_no_member_suggest)
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000477 << Name << LookupCtx << CorrectedQuotedStr << SS.getRange()
478 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregor532e68f2009-12-31 08:26:35 +0000479 else
480 Diag(Found.getNameLoc(), diag::err_undeclared_var_use_suggest)
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000481 << Name << CorrectedQuotedStr
482 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregor6da83622010-01-07 00:17:44 +0000483
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000484 if (NamedDecl *ND = Corrected.getCorrectionDecl()) {
485 Diag(ND->getLocation(), diag::note_previous_decl) << CorrectedQuotedStr;
486 Found.addDecl(ND);
487 }
488 Found.setLookupName(Corrected.getCorrection());
Douglas Gregorc048c522010-06-29 19:27:42 +0000489 } else {
Douglas Gregor90c99722011-02-24 00:17:56 +0000490 Found.setLookupName(&Identifier);
Douglas Gregorc048c522010-06-29 19:27:42 +0000491 }
Douglas Gregor532e68f2009-12-31 08:26:35 +0000492 }
493
John McCall67c00872009-12-02 08:25:40 +0000494 NamedDecl *SD = Found.getAsSingle<NamedDecl>();
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000495 if (isAcceptableNestedNameSpecifier(SD)) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000496 if (!ObjectType.isNull() && !ObjectTypeSearchedInScope) {
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000497 // C++ [basic.lookup.classref]p4:
Mike Stump11289f42009-09-09 15:08:12 +0000498 // [...] If the name is found in both contexts, the
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000499 // class-name-or-namespace-name shall refer to the same entity.
500 //
501 // We already found the name in the scope of the object. Now, look
502 // into the current scope (the scope of the postfix-expression) to
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000503 // see if we can find the same name there. As above, if there is no
504 // scope, reconstruct the result from the template instantiation itself.
John McCall9f3059a2009-10-09 21:13:30 +0000505 NamedDecl *OuterDecl;
506 if (S) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000507 LookupResult FoundOuter(*this, &Identifier, IdentifierLoc,
508 LookupNestedNameSpecifierName);
John McCall27b18f82009-11-17 02:14:36 +0000509 LookupName(FoundOuter, S);
John McCall67c00872009-12-02 08:25:40 +0000510 OuterDecl = FoundOuter.getAsSingle<NamedDecl>();
John McCall9f3059a2009-10-09 21:13:30 +0000511 } else
512 OuterDecl = ScopeLookupResult;
Mike Stump11289f42009-09-09 15:08:12 +0000513
Douglas Gregorcd3f49f2010-02-25 04:46:04 +0000514 if (isAcceptableNestedNameSpecifier(OuterDecl) &&
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000515 OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() &&
516 (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) ||
517 !Context.hasSameType(
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000518 Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)),
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000519 Context.getTypeDeclType(cast<TypeDecl>(SD))))) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000520 if (ErrorRecoveryLookup)
521 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000522
Douglas Gregor90c99722011-02-24 00:17:56 +0000523 Diag(IdentifierLoc,
524 diag::err_nested_name_member_ref_lookup_ambiguous)
525 << &Identifier;
526 Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type)
527 << ObjectType;
528 Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope);
529
530 // Fall through so that we'll pick the name we found in the object
531 // type, since that's probably what the user wanted anyway.
532 }
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000533 }
Mike Stump11289f42009-09-09 15:08:12 +0000534
Douglas Gregor90c99722011-02-24 00:17:56 +0000535 // If we're just performing this lookup for error-recovery purposes,
536 // don't extend the nested-name-specifier. Just return now.
537 if (ErrorRecoveryLookup)
538 return false;
539
540 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD)) {
541 SS.Extend(Context, Namespace, IdentifierLoc, CCLoc);
542 return false;
543 }
Mike Stump11289f42009-09-09 15:08:12 +0000544
Douglas Gregor90c99722011-02-24 00:17:56 +0000545 if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD)) {
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000546 SS.Extend(Context, Alias, IdentifierLoc, CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000547 return false;
548 }
Mike Stump11289f42009-09-09 15:08:12 +0000549
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000550 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
Douglas Gregor90c99722011-02-24 00:17:56 +0000551 TypeLocBuilder TLB;
552 if (isa<InjectedClassNameType>(T)) {
553 InjectedClassNameTypeLoc InjectedTL
554 = TLB.push<InjectedClassNameTypeLoc>(T);
555 InjectedTL.setNameLoc(IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000556 } else if (isa<RecordType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000557 RecordTypeLoc RecordTL = TLB.push<RecordTypeLoc>(T);
558 RecordTL.setNameLoc(IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000559 } else if (isa<TypedefType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000560 TypedefTypeLoc TypedefTL = TLB.push<TypedefTypeLoc>(T);
561 TypedefTL.setNameLoc(IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000562 } else if (isa<EnumType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000563 EnumTypeLoc EnumTL = TLB.push<EnumTypeLoc>(T);
564 EnumTL.setNameLoc(IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000565 } else if (isa<TemplateTypeParmType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000566 TemplateTypeParmTypeLoc TemplateTypeTL
567 = TLB.push<TemplateTypeParmTypeLoc>(T);
568 TemplateTypeTL.setNameLoc(IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000569 } else if (isa<UnresolvedUsingType>(T)) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000570 UnresolvedUsingTypeLoc UnresolvedTL
571 = TLB.push<UnresolvedUsingTypeLoc>(T);
572 UnresolvedTL.setNameLoc(IdentifierLoc);
Douglas Gregordfd4b742011-05-04 23:05:40 +0000573 } else if (isa<SubstTemplateTypeParmType>(T)) {
574 SubstTemplateTypeParmTypeLoc TL
575 = TLB.push<SubstTemplateTypeParmTypeLoc>(T);
576 TL.setNameLoc(IdentifierLoc);
577 } else if (isa<SubstTemplateTypeParmPackType>(T)) {
578 SubstTemplateTypeParmPackTypeLoc TL
579 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(T);
580 TL.setNameLoc(IdentifierLoc);
581 } else {
582 llvm_unreachable("Unhandled TypeDecl node in nested-name-specifier");
Douglas Gregor90c99722011-02-24 00:17:56 +0000583 }
584
585 SS.Extend(Context, SourceLocation(), TLB.getTypeLocInContext(Context, T),
586 CCLoc);
587 return false;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000588 }
Mike Stump11289f42009-09-09 15:08:12 +0000589
Chris Lattner1c428032009-12-07 01:36:53 +0000590 // Otherwise, we have an error case. If we don't want diagnostics, just
591 // return an error now.
592 if (ErrorRecoveryLookup)
Douglas Gregor90c99722011-02-24 00:17:56 +0000593 return true;
Chris Lattner1c428032009-12-07 01:36:53 +0000594
Cedric Venet084381332009-02-14 20:20:19 +0000595 // If we didn't find anything during our lookup, try again with
596 // ordinary name lookup, which can help us produce better error
597 // messages.
John McCall67c00872009-12-02 08:25:40 +0000598 if (Found.empty()) {
John McCall27b18f82009-11-17 02:14:36 +0000599 Found.clear(LookupOrdinaryName);
600 LookupName(Found, S);
John McCall9f3059a2009-10-09 21:13:30 +0000601 }
Mike Stump11289f42009-09-09 15:08:12 +0000602
Cedric Venet084381332009-02-14 20:20:19 +0000603 unsigned DiagID;
John McCall67c00872009-12-02 08:25:40 +0000604 if (!Found.empty())
Cedric Venet084381332009-02-14 20:20:19 +0000605 DiagID = diag::err_expected_class_or_namespace;
Anders Carlssonb533df02009-08-30 07:09:50 +0000606 else if (SS.isSet()) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000607 Diag(IdentifierLoc, diag::err_no_member)
608 << &Identifier << LookupCtx << SS.getRange();
609 return true;
Anders Carlssonb533df02009-08-30 07:09:50 +0000610 } else
Cedric Venet084381332009-02-14 20:20:19 +0000611 DiagID = diag::err_undeclared_var_use;
Mike Stump11289f42009-09-09 15:08:12 +0000612
Cedric Venet084381332009-02-14 20:20:19 +0000613 if (SS.isSet())
Douglas Gregor90c99722011-02-24 00:17:56 +0000614 Diag(IdentifierLoc, DiagID) << &Identifier << SS.getRange();
Cedric Venet084381332009-02-14 20:20:19 +0000615 else
Douglas Gregor90c99722011-02-24 00:17:56 +0000616 Diag(IdentifierLoc, DiagID) << &Identifier;
Mike Stump11289f42009-09-09 15:08:12 +0000617
Douglas Gregor90c99722011-02-24 00:17:56 +0000618 return true;
Cedric Venet084381332009-02-14 20:20:19 +0000619}
620
Douglas Gregor90c99722011-02-24 00:17:56 +0000621bool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
622 IdentifierInfo &Identifier,
623 SourceLocation IdentifierLoc,
624 SourceLocation CCLoc,
625 ParsedType ObjectType,
626 bool EnteringContext,
627 CXXScopeSpec &SS) {
628 if (SS.isInvalid())
629 return true;
630
631 return BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, CCLoc,
632 GetTypeFromParser(ObjectType),
633 EnteringContext, SS,
634 /*ScopeLookupResult=*/0, false);
Chris Lattner1c428032009-12-07 01:36:53 +0000635}
636
637/// IsInvalidUnlessNestedName - This method is used for error recovery
638/// purposes to determine whether the specified identifier is only valid as
639/// a nested name specifier, for example a namespace name. It is
640/// conservatively correct to always return false from this method.
641///
642/// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +0000643bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS,
Douglas Gregor90c99722011-02-24 00:17:56 +0000644 IdentifierInfo &Identifier,
645 SourceLocation IdentifierLoc,
646 SourceLocation ColonLoc,
647 ParsedType ObjectType,
Chris Lattner1c428032009-12-07 01:36:53 +0000648 bool EnteringContext) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000649 if (SS.isInvalid())
650 return false;
651
652 return !BuildCXXNestedNameSpecifier(S, Identifier, IdentifierLoc, ColonLoc,
653 GetTypeFromParser(ObjectType),
654 EnteringContext, SS,
655 /*ScopeLookupResult=*/0, true);
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000656}
657
Douglas Gregor90c99722011-02-24 00:17:56 +0000658bool Sema::ActOnCXXNestedNameSpecifier(Scope *S,
Douglas Gregor6e068012011-02-28 00:04:36 +0000659 SourceLocation TemplateLoc,
660 CXXScopeSpec &SS,
661 TemplateTy Template,
662 SourceLocation TemplateNameLoc,
663 SourceLocation LAngleLoc,
664 ASTTemplateArgsPtr TemplateArgsIn,
665 SourceLocation RAngleLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +0000666 SourceLocation CCLoc,
Douglas Gregor6e068012011-02-28 00:04:36 +0000667 bool EnteringContext) {
Douglas Gregor90c99722011-02-24 00:17:56 +0000668 if (SS.isInvalid())
669 return true;
670
Douglas Gregor6e068012011-02-28 00:04:36 +0000671 // Translate the parser's template argument list in our AST format.
672 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
673 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
674
675 if (DependentTemplateName *DTN = Template.get().getAsDependentTemplateName()){
676 // Handle a dependent template specialization for which we cannot resolve
677 // the template name.
678 assert(DTN->getQualifier()
679 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
680 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
Douglas Gregora7a795b2011-03-01 20:11:18 +0000681 DTN->getQualifier(),
682 DTN->getIdentifier(),
Douglas Gregor6e068012011-02-28 00:04:36 +0000683 TemplateArgs);
684
685 // Create source-location information for this type.
686 TypeLocBuilder Builder;
687 DependentTemplateSpecializationTypeLoc SpecTL
688 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
689 SpecTL.setLAngleLoc(LAngleLoc);
690 SpecTL.setRAngleLoc(RAngleLoc);
691 SpecTL.setKeywordLoc(SourceLocation());
692 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000693 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregor6e068012011-02-28 00:04:36 +0000694 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
695 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
696
697 SS.Extend(Context, TemplateLoc, Builder.getTypeLocInContext(Context, T),
698 CCLoc);
699 return false;
700 }
701
Douglas Gregor8b6070b2011-03-04 21:37:14 +0000702
703 if (Template.get().getAsOverloadedTemplate() ||
704 isa<FunctionTemplateDecl>(Template.get().getAsTemplateDecl())) {
705 SourceRange R(TemplateNameLoc, RAngleLoc);
706 if (SS.getRange().isValid())
707 R.setBegin(SS.getRange().getBegin());
708
709 Diag(CCLoc, diag::err_non_type_template_in_nested_name_specifier)
710 << Template.get() << R;
711 NoteAllFoundTemplates(Template.get());
712 return true;
713 }
714
Douglas Gregor6e068012011-02-28 00:04:36 +0000715 // We were able to resolve the template name to an actual template.
716 // Build an appropriate nested-name-specifier.
717 QualType T = CheckTemplateIdType(Template.get(), TemplateNameLoc,
718 TemplateArgs);
Douglas Gregor90c99722011-02-24 00:17:56 +0000719 if (T.isNull())
720 return true;
721
Richard Smith3f1b5d02011-05-05 21:57:07 +0000722 // Alias template specializations can produce types which are not valid
723 // nested name specifiers.
724 if (!T->isDependentType() && !T->getAs<TagType>()) {
725 Diag(TemplateNameLoc, diag::err_nested_name_spec_non_tag) << T;
726 NoteAllFoundTemplates(Template.get());
727 return true;
728 }
Douglas Gregor6e068012011-02-28 00:04:36 +0000729
730 // Provide source-location information for the template specialization
731 // type.
732 TypeLocBuilder Builder;
733 TemplateSpecializationTypeLoc SpecTL
734 = Builder.push<TemplateSpecializationTypeLoc>(T);
735
736 SpecTL.setLAngleLoc(LAngleLoc);
737 SpecTL.setRAngleLoc(RAngleLoc);
738 SpecTL.setTemplateNameLoc(TemplateNameLoc);
739 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
740 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
741
742
743 SS.Extend(Context, TemplateLoc, Builder.getTypeLocInContext(Context, T),
744 CCLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +0000745 return false;
Douglas Gregor7f741122009-02-25 19:37:18 +0000746}
747
Douglas Gregor869ad452011-02-24 17:54:50 +0000748namespace {
749 /// \brief A structure that stores a nested-name-specifier annotation,
750 /// including both the nested-name-specifier
751 struct NestedNameSpecifierAnnotation {
752 NestedNameSpecifier *NNS;
753 };
754}
755
756void *Sema::SaveNestedNameSpecifierAnnotation(CXXScopeSpec &SS) {
757 if (SS.isEmpty() || SS.isInvalid())
758 return 0;
759
760 void *Mem = Context.Allocate((sizeof(NestedNameSpecifierAnnotation) +
761 SS.location_size()),
762 llvm::alignOf<NestedNameSpecifierAnnotation>());
763 NestedNameSpecifierAnnotation *Annotation
764 = new (Mem) NestedNameSpecifierAnnotation;
765 Annotation->NNS = SS.getScopeRep();
766 memcpy(Annotation + 1, SS.location_data(), SS.location_size());
767 return Annotation;
768}
769
770void Sema::RestoreNestedNameSpecifierAnnotation(void *AnnotationPtr,
771 SourceRange AnnotationRange,
772 CXXScopeSpec &SS) {
773 if (!AnnotationPtr) {
774 SS.SetInvalid(AnnotationRange);
775 return;
776 }
777
778 NestedNameSpecifierAnnotation *Annotation
779 = static_cast<NestedNameSpecifierAnnotation *>(AnnotationPtr);
780 SS.Adopt(NestedNameSpecifierLoc(Annotation->NNS, Annotation + 1));
781}
782
John McCall2b058ef2009-12-11 20:04:54 +0000783bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
784 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
785
786 NestedNameSpecifier *Qualifier =
787 static_cast<NestedNameSpecifier*>(SS.getScopeRep());
788
789 // There are only two places a well-formed program may qualify a
790 // declarator: first, when defining a namespace or class member
791 // out-of-line, and second, when naming an explicitly-qualified
792 // friend function. The latter case is governed by
793 // C++03 [basic.lookup.unqual]p10:
794 // In a friend declaration naming a member function, a name used
795 // in the function declarator and not part of a template-argument
796 // in a template-id is first looked up in the scope of the member
797 // function's class. If it is not found, or if the name is part of
798 // a template-argument in a template-id, the look up is as
799 // described for unqualified names in the definition of the class
800 // granting friendship.
801 // i.e. we don't push a scope unless it's a class member.
802
803 switch (Qualifier->getKind()) {
804 case NestedNameSpecifier::Global:
805 case NestedNameSpecifier::Namespace:
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000806 case NestedNameSpecifier::NamespaceAlias:
John McCall2b058ef2009-12-11 20:04:54 +0000807 // These are always namespace scopes. We never want to enter a
808 // namespace scope from anything but a file context.
Sebastian Redl50c68252010-08-31 00:36:30 +0000809 return CurContext->getRedeclContext()->isFileContext();
John McCall2b058ef2009-12-11 20:04:54 +0000810
811 case NestedNameSpecifier::Identifier:
812 case NestedNameSpecifier::TypeSpec:
813 case NestedNameSpecifier::TypeSpecWithTemplate:
814 // These are never namespace scopes.
815 return true;
816 }
817
818 // Silence bogus warning.
819 return false;
820}
821
Cedric Venet084381332009-02-14 20:20:19 +0000822/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
823/// scope or nested-name-specifier) is parsed, part of a declarator-id.
824/// After this method is called, according to [C++ 3.4.3p3], names should be
825/// looked up in the declarator-id's scope, until the declarator is parsed and
826/// ActOnCXXExitDeclaratorScope is called.
827/// The 'SS' should be a non-empty valid CXXScopeSpec.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +0000828bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) {
Cedric Venet084381332009-02-14 20:20:19 +0000829 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
John McCall6df5fef2009-12-19 10:49:29 +0000830
831 if (SS.isInvalid()) return true;
832
833 DeclContext *DC = computeDeclContext(SS, true);
834 if (!DC) return true;
835
836 // Before we enter a declarator's context, we need to make sure that
837 // it is a complete declaration context.
John McCall0b66eb32010-05-01 00:40:08 +0000838 if (!DC->isDependentContext() && RequireCompleteDeclContext(SS, DC))
John McCall6df5fef2009-12-19 10:49:29 +0000839 return true;
840
841 EnterDeclaratorContext(S, DC);
John McCall2408e322010-04-27 00:57:59 +0000842
843 // Rebuild the nested name specifier for the new scope.
844 if (DC->isDependentContext())
845 RebuildNestedNameSpecifierInCurrentInstantiation(SS);
846
Douglas Gregor5013a7e2009-09-24 23:39:01 +0000847 return false;
Cedric Venet084381332009-02-14 20:20:19 +0000848}
849
850/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
851/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
852/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
853/// Used to indicate that names should revert to being looked up in the
854/// defining scope.
855void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
856 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
Douglas Gregor053f6912009-08-26 00:04:55 +0000857 if (SS.isInvalid())
858 return;
John McCall6df5fef2009-12-19 10:49:29 +0000859 assert(!SS.isInvalid() && computeDeclContext(SS, true) &&
860 "exiting declarator scope we never really entered");
861 ExitDeclaratorContext(S);
Cedric Venet084381332009-02-14 20:20:19 +0000862}