blob: 10adc6762f0bc5c5f7cd9eb5949439f7c21f0000 [file] [log] [blame]
Cedric Venet3d658642009-02-14 20:20:19 +00001//===--- SemaCXXScopeSpec.cpp - Semantic Analysis for C++ scope specifiers-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements C++ semantic analysis for scope specifiers.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
John McCall7d384dd2009-11-18 07:57:50 +000015#include "Lookup.h"
Cedric Venet3d658642009-02-14 20:20:19 +000016#include "clang/AST/ASTContext.h"
Douglas Gregor42af25f2009-05-11 19:58:34 +000017#include "clang/AST/DeclTemplate.h"
Douglas Gregorfe85ced2009-08-06 03:17:00 +000018#include "clang/AST/ExprCXX.h"
Douglas Gregore4e5b052009-03-19 00:18:19 +000019#include "clang/AST/NestedNameSpecifier.h"
Anders Carlssonb7906612009-08-26 23:45:07 +000020#include "clang/Basic/PartialDiagnostic.h"
Cedric Venet3d658642009-02-14 20:20:19 +000021#include "clang/Parse/DeclSpec.h"
22#include "llvm/ADT/STLExtras.h"
Douglas Gregor7551c182009-07-22 00:28:09 +000023#include "llvm/Support/raw_ostream.h"
Cedric Venet3d658642009-02-14 20:20:19 +000024using namespace clang;
25
Douglas Gregor43d88632009-11-04 22:49:18 +000026/// \brief Find the current instantiation that associated with the given type.
John McCall31f17ec2010-04-27 00:57:59 +000027static CXXRecordDecl *getCurrentInstantiationOf(QualType T) {
Douglas Gregor43d88632009-11-04 22:49:18 +000028 if (T.isNull())
29 return 0;
John McCall31f17ec2010-04-27 00:57:59 +000030
31 const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
32 if (isa<RecordType>(Ty))
33 return cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
34 else if (isa<InjectedClassNameType>(Ty))
35 return cast<InjectedClassNameType>(Ty)->getDecl();
36 else
37 return 0;
Douglas Gregor43d88632009-11-04 22:49:18 +000038}
39
Douglas Gregor2dd078a2009-09-02 22:59:36 +000040/// \brief Compute the DeclContext that is associated with the given type.
41///
42/// \param T the type for which we are attempting to find a DeclContext.
43///
Mike Stump1eb44332009-09-09 15:08:12 +000044/// \returns the declaration context represented by the type T,
Douglas Gregor2dd078a2009-09-02 22:59:36 +000045/// or NULL if the declaration context cannot be computed (e.g., because it is
46/// dependent and not the current instantiation).
47DeclContext *Sema::computeDeclContext(QualType T) {
48 if (const TagType *Tag = T->getAs<TagType>())
49 return Tag->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000050
John McCall31f17ec2010-04-27 00:57:59 +000051 return ::getCurrentInstantiationOf(T);
Douglas Gregor2dd078a2009-09-02 22:59:36 +000052}
53
Douglas Gregore4e5b052009-03-19 00:18:19 +000054/// \brief Compute the DeclContext that is associated with the given
55/// scope specifier.
Douglas Gregorf59a56e2009-07-21 23:53:31 +000056///
57/// \param SS the C++ scope specifier as it appears in the source
58///
59/// \param EnteringContext when true, we will be entering the context of
60/// this scope specifier, so we can retrieve the declaration context of a
61/// class template or class template partial specialization even if it is
62/// not the current instantiation.
63///
64/// \returns the declaration context represented by the scope specifier @p SS,
65/// or NULL if the declaration context cannot be computed (e.g., because it is
66/// dependent and not the current instantiation).
67DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS,
68 bool EnteringContext) {
Douglas Gregore4e5b052009-03-19 00:18:19 +000069 if (!SS.isSet() || SS.isInvalid())
Douglas Gregorca5e77f2009-03-18 00:36:05 +000070 return 0;
Douglas Gregorca5e77f2009-03-18 00:36:05 +000071
Mike Stump1eb44332009-09-09 15:08:12 +000072 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +000073 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregor42af25f2009-05-11 19:58:34 +000074 if (NNS->isDependent()) {
75 // If this nested-name-specifier refers to the current
76 // instantiation, return its DeclContext.
77 if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS))
78 return Record;
Mike Stump1eb44332009-09-09 15:08:12 +000079
Douglas Gregorf59a56e2009-07-21 23:53:31 +000080 if (EnteringContext) {
John McCall3cb0ebd2010-03-10 03:28:59 +000081 const Type *NNSType = NNS->getAsType();
82 if (!NNSType) {
83 // do nothing, fall out
84 } else if (const TemplateSpecializationType *SpecType
85 = NNSType->getAs<TemplateSpecializationType>()) {
Douglas Gregor495c35d2009-08-25 22:51:20 +000086 // We are entering the context of the nested name specifier, so try to
87 // match the nested name specifier to either a primary class template
88 // or a class template partial specialization.
Mike Stump1eb44332009-09-09 15:08:12 +000089 if (ClassTemplateDecl *ClassTemplate
Douglas Gregorf59a56e2009-07-21 23:53:31 +000090 = dyn_cast_or_null<ClassTemplateDecl>(
91 SpecType->getTemplateName().getAsTemplateDecl())) {
Douglas Gregorb88e8882009-07-30 17:40:51 +000092 QualType ContextType
93 = Context.getCanonicalType(QualType(SpecType, 0));
94
Douglas Gregorf59a56e2009-07-21 23:53:31 +000095 // If the type of the nested name specifier is the same as the
96 // injected class name of the named class template, we're entering
97 // into that class template definition.
John McCall3cb0ebd2010-03-10 03:28:59 +000098 QualType Injected
99 = ClassTemplate->getInjectedClassNameSpecialization(Context);
Douglas Gregorb88e8882009-07-30 17:40:51 +0000100 if (Context.hasSameType(Injected, ContextType))
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000101 return ClassTemplate->getTemplatedDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Douglas Gregorb88e8882009-07-30 17:40:51 +0000103 // If the type of the nested name specifier is the same as the
104 // type of one of the class template's class template partial
105 // specializations, we're entering into the definition of that
106 // class template partial specialization.
107 if (ClassTemplatePartialSpecializationDecl *PartialSpec
108 = ClassTemplate->findPartialSpecialization(ContextType))
109 return PartialSpec;
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000110 }
John McCall3cb0ebd2010-03-10 03:28:59 +0000111 } else if (const RecordType *RecordT = NNSType->getAs<RecordType>()) {
Douglas Gregor495c35d2009-08-25 22:51:20 +0000112 // The nested name specifier refers to a member of a class template.
113 return RecordT->getDecl();
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000114 }
115 }
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000117 return 0;
Douglas Gregor42af25f2009-05-11 19:58:34 +0000118 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000119
120 switch (NNS->getKind()) {
121 case NestedNameSpecifier::Identifier:
122 assert(false && "Dependent nested-name-specifier has no DeclContext");
123 break;
124
125 case NestedNameSpecifier::Namespace:
126 return NNS->getAsNamespace();
127
128 case NestedNameSpecifier::TypeSpec:
129 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregoredc90502010-02-25 04:46:04 +0000130 const TagType *Tag = NNS->getAsType()->getAs<TagType>();
131 assert(Tag && "Non-tag type in nested-name-specifier");
132 return Tag->getDecl();
133 } break;
Douglas Gregorab452ba2009-03-26 23:50:42 +0000134
135 case NestedNameSpecifier::Global:
136 return Context.getTranslationUnitDecl();
137 }
138
Douglas Gregoredc90502010-02-25 04:46:04 +0000139 // Required to silence a GCC warning.
Douglas Gregorab452ba2009-03-26 23:50:42 +0000140 return 0;
Douglas Gregorca5e77f2009-03-18 00:36:05 +0000141}
142
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000143bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) {
144 if (!SS.isSet() || SS.isInvalid())
145 return false;
146
Mike Stump1eb44332009-09-09 15:08:12 +0000147 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +0000148 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +0000149 return NNS->isDependent();
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000150}
151
Douglas Gregor42af25f2009-05-11 19:58:34 +0000152// \brief Determine whether this C++ scope specifier refers to an
153// unknown specialization, i.e., a dependent type that is not the
154// current instantiation.
155bool Sema::isUnknownSpecialization(const CXXScopeSpec &SS) {
156 if (!isDependentScopeSpecifier(SS))
157 return false;
158
Mike Stump1eb44332009-09-09 15:08:12 +0000159 NestedNameSpecifier *NNS
Douglas Gregor42af25f2009-05-11 19:58:34 +0000160 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
161 return getCurrentInstantiationOf(NNS) == 0;
162}
163
164/// \brief If the given nested name specifier refers to the current
165/// instantiation, return the declaration that corresponds to that
166/// current instantiation (C++0x [temp.dep.type]p1).
167///
168/// \param NNS a dependent nested name specifier.
169CXXRecordDecl *Sema::getCurrentInstantiationOf(NestedNameSpecifier *NNS) {
170 assert(getLangOptions().CPlusPlus && "Only callable in C++");
171 assert(NNS->isDependent() && "Only dependent nested-name-specifier allowed");
172
Douglas Gregorf59a56e2009-07-21 23:53:31 +0000173 if (!NNS->getAsType())
174 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Douglas Gregor1560def2009-07-31 18:32:42 +0000176 QualType T = QualType(NNS->getAsType(), 0);
John McCall31f17ec2010-04-27 00:57:59 +0000177 return ::getCurrentInstantiationOf(T);
Douglas Gregor42af25f2009-05-11 19:58:34 +0000178}
179
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000180/// \brief Require that the context specified by SS be complete.
181///
182/// If SS refers to a type, this routine checks whether the type is
183/// complete enough (or can be made complete enough) for name lookup
184/// into the DeclContext. A type that is not yet completed can be
185/// considered "complete enough" if it is a class/struct/union/enum
186/// that is currently being defined. Or, if we have a type that names
187/// a class template specialization that is not a complete type, we
188/// will attempt to instantiate that class template.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000189bool Sema::RequireCompleteDeclContext(CXXScopeSpec &SS) {
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000190 if (!SS.isSet() || SS.isInvalid())
191 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000193 DeclContext *DC = computeDeclContext(SS, true);
Douglas Gregor48c89f42010-04-24 16:38:41 +0000194 if (!DC) {
195 // It's dependent.
196 assert(isDependentScopeSpecifier(SS) &&
197 "No context for non-dependent scope specifier?");
198 Diag(SS.getRange().getBegin(), diag::err_dependent_nested_name_spec)
199 << SS.getRange();
200 SS.setScopeRep(0);
201 return true;
202 }
203
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000204 if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) {
Douglas Gregora4e8c2a2010-02-05 04:39:02 +0000205 // If this is a dependent type, then we consider it complete.
206 if (Tag->isDependentContext())
207 return false;
208
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000209 // If we're currently defining this type, then lookup into the
210 // type is okay: don't complain that it isn't complete yet.
Ted Kremenek6217b802009-07-29 21:53:49 +0000211 const TagType *TagT = Context.getTypeDeclType(Tag)->getAs<TagType>();
John McCall3cb0ebd2010-03-10 03:28:59 +0000212 if (TagT && TagT->isBeingDefined())
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000213 return false;
214
215 // The type must be complete.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000216 if (RequireCompleteType(SS.getRange().getBegin(),
217 Context.getTypeDeclType(Tag),
218 PDiag(diag::err_incomplete_nested_name_spec)
219 << SS.getRange())) {
220 SS.setScopeRep(0); // Mark the ScopeSpec invalid.
221 return true;
222 }
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000223 }
224
225 return false;
226}
Cedric Venet3d658642009-02-14 20:20:19 +0000227
228/// ActOnCXXGlobalScopeSpecifier - Return the object that represents the
229/// global scope ('::').
230Sema::CXXScopeTy *Sema::ActOnCXXGlobalScopeSpecifier(Scope *S,
231 SourceLocation CCLoc) {
Douglas Gregorab452ba2009-03-26 23:50:42 +0000232 return NestedNameSpecifier::GlobalSpecifier(Context);
Cedric Venet3d658642009-02-14 20:20:19 +0000233}
234
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000235/// \brief Determines whether the given declaration is an valid acceptable
236/// result for name lookup of a nested-name-specifier.
Douglas Gregoredc90502010-02-25 04:46:04 +0000237bool Sema::isAcceptableNestedNameSpecifier(NamedDecl *SD) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000238 if (!SD)
239 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000241 // Namespace and namespace aliases are fine.
242 if (isa<NamespaceDecl>(SD) || isa<NamespaceAliasDecl>(SD))
243 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000245 if (!isa<TypeDecl>(SD))
246 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000248 // Determine whether we have a class (or, in C++0x, an enum) or
249 // a typedef thereof. If so, build the nested-name-specifier.
250 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
251 if (T->isDependentType())
252 return true;
253 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) {
254 if (TD->getUnderlyingType()->isRecordType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000255 (Context.getLangOptions().CPlusPlus0x &&
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000256 TD->getUnderlyingType()->isEnumeralType()))
257 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000258 } else if (isa<RecordDecl>(SD) ||
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000259 (Context.getLangOptions().CPlusPlus0x && isa<EnumDecl>(SD)))
260 return true;
261
262 return false;
263}
264
Douglas Gregorc68afe22009-09-03 21:38:09 +0000265/// \brief If the given nested-name-specifier begins with a bare identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000266/// (e.g., Base::), perform name lookup for that identifier as a
Douglas Gregorc68afe22009-09-03 21:38:09 +0000267/// nested-name-specifier within the given scope, and return the result of that
268/// name lookup.
269NamedDecl *Sema::FindFirstQualifierInScope(Scope *S, NestedNameSpecifier *NNS) {
270 if (!S || !NNS)
271 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Douglas Gregorc68afe22009-09-03 21:38:09 +0000273 while (NNS->getPrefix())
274 NNS = NNS->getPrefix();
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Douglas Gregorc68afe22009-09-03 21:38:09 +0000276 if (NNS->getKind() != NestedNameSpecifier::Identifier)
277 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000278
John McCalla24dc2e2009-11-17 02:14:36 +0000279 LookupResult Found(*this, NNS->getAsIdentifier(), SourceLocation(),
280 LookupNestedNameSpecifierName);
281 LookupName(Found, S);
Douglas Gregorc68afe22009-09-03 21:38:09 +0000282 assert(!Found.isAmbiguous() && "Cannot handle ambiguities here yet");
283
John McCall1bcee0a2009-12-02 08:25:40 +0000284 if (!Found.isSingleResult())
285 return 0;
286
287 NamedDecl *Result = Found.getFoundDecl();
Douglas Gregoredc90502010-02-25 04:46:04 +0000288 if (isAcceptableNestedNameSpecifier(Result))
Douglas Gregorc68afe22009-09-03 21:38:09 +0000289 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Douglas Gregorc68afe22009-09-03 21:38:09 +0000291 return 0;
292}
293
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000294bool Sema::isNonTypeNestedNameSpecifier(Scope *S, CXXScopeSpec &SS,
Douglas Gregor77549082010-02-24 21:29:12 +0000295 SourceLocation IdLoc,
296 IdentifierInfo &II,
297 TypeTy *ObjectTypePtr) {
298 QualType ObjectType = GetTypeFromParser(ObjectTypePtr);
299 LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName);
300
301 // Determine where to perform name lookup
302 DeclContext *LookupCtx = 0;
303 bool isDependent = false;
304 if (!ObjectType.isNull()) {
305 // This nested-name-specifier occurs in a member access expression, e.g.,
306 // x->B::f, and we are looking into the type of the object.
307 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
308 LookupCtx = computeDeclContext(ObjectType);
309 isDependent = ObjectType->isDependentType();
310 } else if (SS.isSet()) {
311 // This nested-name-specifier occurs after another nested-name-specifier,
312 // so long into the context associated with the prior nested-name-specifier.
313 LookupCtx = computeDeclContext(SS, false);
314 isDependent = isDependentScopeSpecifier(SS);
315 Found.setContextRange(SS.getRange());
316 }
317
318 if (LookupCtx) {
319 // Perform "qualified" name lookup into the declaration context we
320 // computed, which is either the type of the base of a member access
321 // expression or the declaration context associated with a prior
322 // nested-name-specifier.
323
324 // The declaration context must be complete.
325 if (!LookupCtx->isDependentContext() && RequireCompleteDeclContext(SS))
326 return false;
327
328 LookupQualifiedName(Found, LookupCtx);
329 } else if (isDependent) {
330 return false;
331 } else {
332 LookupName(Found, S);
333 }
334 Found.suppressDiagnostics();
335
336 if (NamedDecl *ND = Found.getAsSingle<NamedDecl>())
337 return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND);
338
339 return false;
340}
341
Douglas Gregorc68afe22009-09-03 21:38:09 +0000342/// \brief Build a new nested-name-specifier for "identifier::", as described
343/// by ActOnCXXNestedNameSpecifier.
344///
345/// This routine differs only slightly from ActOnCXXNestedNameSpecifier, in
346/// that it contains an extra parameter \p ScopeLookupResult, which provides
347/// the result of name lookup within the scope of the nested-name-specifier
Douglas Gregora6e51992009-12-30 16:01:52 +0000348/// that was computed at template definition time.
Chris Lattner46646492009-12-07 01:36:53 +0000349///
350/// If ErrorRecoveryLookup is true, then this call is used to improve error
351/// recovery. This means that it should not emit diagnostics, it should
352/// just return null on failure. It also means it should only return a valid
353/// scope if it *knows* that the result is correct. It should not return in a
354/// dependent context, for example.
Douglas Gregorc68afe22009-09-03 21:38:09 +0000355Sema::CXXScopeTy *Sema::BuildCXXNestedNameSpecifier(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000356 CXXScopeSpec &SS,
Cedric Venet3d658642009-02-14 20:20:19 +0000357 SourceLocation IdLoc,
358 SourceLocation CCLoc,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000359 IdentifierInfo &II,
Douglas Gregorc68afe22009-09-03 21:38:09 +0000360 QualType ObjectType,
361 NamedDecl *ScopeLookupResult,
Chris Lattner46646492009-12-07 01:36:53 +0000362 bool EnteringContext,
363 bool ErrorRecoveryLookup) {
Mike Stump1eb44332009-09-09 15:08:12 +0000364 NestedNameSpecifier *Prefix
Douglas Gregor35073692009-03-26 23:56:24 +0000365 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Mike Stump1eb44332009-09-09 15:08:12 +0000366
John McCalla24dc2e2009-11-17 02:14:36 +0000367 LookupResult Found(*this, &II, IdLoc, LookupNestedNameSpecifierName);
368
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000369 // Determine where to perform name lookup
370 DeclContext *LookupCtx = 0;
371 bool isDependent = false;
Douglas Gregorc68afe22009-09-03 21:38:09 +0000372 if (!ObjectType.isNull()) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000373 // This nested-name-specifier occurs in a member access expression, e.g.,
374 // x->B::f, and we are looking into the type of the object.
375 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000376 LookupCtx = computeDeclContext(ObjectType);
377 isDependent = ObjectType->isDependentType();
378 } else if (SS.isSet()) {
379 // This nested-name-specifier occurs after another nested-name-specifier,
380 // so long into the context associated with the prior nested-name-specifier.
381 LookupCtx = computeDeclContext(SS, EnteringContext);
382 isDependent = isDependentScopeSpecifier(SS);
John McCalla24dc2e2009-11-17 02:14:36 +0000383 Found.setContextRange(SS.getRange());
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000384 }
Mike Stump1eb44332009-09-09 15:08:12 +0000385
John McCalla24dc2e2009-11-17 02:14:36 +0000386
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000387 bool ObjectTypeSearchedInScope = false;
388 if (LookupCtx) {
Mike Stump1eb44332009-09-09 15:08:12 +0000389 // Perform "qualified" name lookup into the declaration context we
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000390 // computed, which is either the type of the base of a member access
Mike Stump1eb44332009-09-09 15:08:12 +0000391 // expression or the declaration context associated with a prior
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000392 // nested-name-specifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000394 // The declaration context must be complete.
395 if (!LookupCtx->isDependentContext() && RequireCompleteDeclContext(SS))
396 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000397
John McCalla24dc2e2009-11-17 02:14:36 +0000398 LookupQualifiedName(Found, LookupCtx);
Mike Stump1eb44332009-09-09 15:08:12 +0000399
John McCalla24dc2e2009-11-17 02:14:36 +0000400 if (!ObjectType.isNull() && Found.empty()) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000401 // C++ [basic.lookup.classref]p4:
402 // If the id-expression in a class member access is a qualified-id of
Mike Stump1eb44332009-09-09 15:08:12 +0000403 // the form
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000404 //
405 // class-name-or-namespace-name::...
406 //
Mike Stump1eb44332009-09-09 15:08:12 +0000407 // the class-name-or-namespace-name following the . or -> operator is
408 // looked up both in the context of the entire postfix-expression and in
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000409 // the scope of the class of the object expression. If the name is found
Mike Stump1eb44332009-09-09 15:08:12 +0000410 // only in the scope of the class of the object expression, the name
411 // shall refer to a class-name. If the name is found only in the
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000412 // context of the entire postfix-expression, the name shall refer to a
413 // class-name or namespace-name. [...]
414 //
415 // Qualified name lookup into a class will not find a namespace-name,
Mike Stump1eb44332009-09-09 15:08:12 +0000416 // so we do not need to diagnoste that case specifically. However,
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000417 // this qualified name lookup may find nothing. In that case, perform
Mike Stump1eb44332009-09-09 15:08:12 +0000418 // unqualified name lookup in the given scope (if available) or
Douglas Gregorc68afe22009-09-03 21:38:09 +0000419 // reconstruct the result from when name lookup was performed at template
420 // definition time.
421 if (S)
John McCalla24dc2e2009-11-17 02:14:36 +0000422 LookupName(Found, S);
John McCallf36e02d2009-10-09 21:13:30 +0000423 else if (ScopeLookupResult)
424 Found.addDecl(ScopeLookupResult);
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000426 ObjectTypeSearchedInScope = true;
427 }
428 } else if (isDependent) {
Chris Lattner46646492009-12-07 01:36:53 +0000429 // Don't speculate if we're just trying to improve error recovery.
430 if (ErrorRecoveryLookup)
431 return 0;
432
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000433 // We were not able to compute the declaration context for a dependent
Mike Stump1eb44332009-09-09 15:08:12 +0000434 // base object type or prior nested-name-specifier, so this
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000435 // nested-name-specifier refers to an unknown specialization. Just build
436 // a dependent nested-name-specifier.
Douglas Gregor2700dcd2009-09-02 23:58:38 +0000437 if (!Prefix)
438 return NestedNameSpecifier::Create(Context, &II);
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000440 return NestedNameSpecifier::Create(Context, Prefix, &II);
441 } else {
442 // Perform unqualified name lookup in the current scope.
John McCalla24dc2e2009-11-17 02:14:36 +0000443 LookupName(Found, S);
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000444 }
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000446 // FIXME: Deal with ambiguities cleanly.
Douglas Gregor175a6562009-12-31 08:26:35 +0000447
448 if (Found.empty() && !ErrorRecoveryLookup) {
449 // We haven't found anything, and we're not recovering from a
450 // different kind of error, so look for typos.
451 DeclarationName Name = Found.getLookupName();
Douglas Gregoraaf87162010-04-14 20:04:41 +0000452 if (CorrectTypo(Found, S, &SS, LookupCtx, EnteringContext,
453 CTC_NoKeywords) &&
Douglas Gregor175a6562009-12-31 08:26:35 +0000454 Found.isSingleResult() &&
Douglas Gregoredc90502010-02-25 04:46:04 +0000455 isAcceptableNestedNameSpecifier(Found.getAsSingle<NamedDecl>())) {
Douglas Gregor175a6562009-12-31 08:26:35 +0000456 if (LookupCtx)
457 Diag(Found.getNameLoc(), diag::err_no_member_suggest)
458 << Name << LookupCtx << Found.getLookupName() << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +0000459 << FixItHint::CreateReplacement(Found.getNameLoc(),
460 Found.getLookupName().getAsString());
Douglas Gregor175a6562009-12-31 08:26:35 +0000461 else
462 Diag(Found.getNameLoc(), diag::err_undeclared_var_use_suggest)
463 << Name << Found.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000464 << FixItHint::CreateReplacement(Found.getNameLoc(),
465 Found.getLookupName().getAsString());
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000466
467 if (NamedDecl *ND = Found.getAsSingle<NamedDecl>())
468 Diag(ND->getLocation(), diag::note_previous_decl)
469 << ND->getDeclName();
Douglas Gregor175a6562009-12-31 08:26:35 +0000470 } else
471 Found.clear();
472 }
473
John McCall1bcee0a2009-12-02 08:25:40 +0000474 NamedDecl *SD = Found.getAsSingle<NamedDecl>();
Douglas Gregoredc90502010-02-25 04:46:04 +0000475 if (isAcceptableNestedNameSpecifier(SD)) {
Douglas Gregorc68afe22009-09-03 21:38:09 +0000476 if (!ObjectType.isNull() && !ObjectTypeSearchedInScope) {
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000477 // C++ [basic.lookup.classref]p4:
Mike Stump1eb44332009-09-09 15:08:12 +0000478 // [...] If the name is found in both contexts, the
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000479 // class-name-or-namespace-name shall refer to the same entity.
480 //
481 // We already found the name in the scope of the object. Now, look
482 // into the current scope (the scope of the postfix-expression) to
Douglas Gregorc68afe22009-09-03 21:38:09 +0000483 // see if we can find the same name there. As above, if there is no
484 // scope, reconstruct the result from the template instantiation itself.
John McCallf36e02d2009-10-09 21:13:30 +0000485 NamedDecl *OuterDecl;
486 if (S) {
Douglas Gregoredc90502010-02-25 04:46:04 +0000487 LookupResult FoundOuter(*this, &II, IdLoc, LookupNestedNameSpecifierName);
John McCalla24dc2e2009-11-17 02:14:36 +0000488 LookupName(FoundOuter, S);
John McCall1bcee0a2009-12-02 08:25:40 +0000489 OuterDecl = FoundOuter.getAsSingle<NamedDecl>();
John McCallf36e02d2009-10-09 21:13:30 +0000490 } else
491 OuterDecl = ScopeLookupResult;
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Douglas Gregoredc90502010-02-25 04:46:04 +0000493 if (isAcceptableNestedNameSpecifier(OuterDecl) &&
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000494 OuterDecl->getCanonicalDecl() != SD->getCanonicalDecl() &&
495 (!isa<TypeDecl>(OuterDecl) || !isa<TypeDecl>(SD) ||
496 !Context.hasSameType(
Douglas Gregorc68afe22009-09-03 21:38:09 +0000497 Context.getTypeDeclType(cast<TypeDecl>(OuterDecl)),
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000498 Context.getTypeDeclType(cast<TypeDecl>(SD))))) {
Chris Lattner46646492009-12-07 01:36:53 +0000499 if (ErrorRecoveryLookup)
500 return 0;
501
Douglas Gregorc68afe22009-09-03 21:38:09 +0000502 Diag(IdLoc, diag::err_nested_name_member_ref_lookup_ambiguous)
503 << &II;
504 Diag(SD->getLocation(), diag::note_ambig_member_ref_object_type)
505 << ObjectType;
506 Diag(OuterDecl->getLocation(), diag::note_ambig_member_ref_scope);
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Chris Lattner46646492009-12-07 01:36:53 +0000508 // Fall through so that we'll pick the name we found in the object
509 // type, since that's probably what the user wanted anyway.
Douglas Gregorc68afe22009-09-03 21:38:09 +0000510 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000511 }
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Douglas Gregorab452ba2009-03-26 23:50:42 +0000513 if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(SD))
514 return NestedNameSpecifier::Create(Context, Prefix, Namespace);
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Douglas Gregordacd4342009-08-26 00:04:55 +0000516 // FIXME: It would be nice to maintain the namespace alias name, then
517 // see through that alias when resolving the nested-name-specifier down to
518 // a declaration context.
Anders Carlsson81c85c42009-03-28 23:53:49 +0000519 if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD))
520 return NestedNameSpecifier::Create(Context, Prefix,
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000522 Alias->getNamespace());
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000524 QualType T = Context.getTypeDeclType(cast<TypeDecl>(SD));
525 return NestedNameSpecifier::Create(Context, Prefix, false,
526 T.getTypePtr());
527 }
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Chris Lattner46646492009-12-07 01:36:53 +0000529 // Otherwise, we have an error case. If we don't want diagnostics, just
530 // return an error now.
531 if (ErrorRecoveryLookup)
532 return 0;
533
Cedric Venet3d658642009-02-14 20:20:19 +0000534 // If we didn't find anything during our lookup, try again with
535 // ordinary name lookup, which can help us produce better error
536 // messages.
John McCall1bcee0a2009-12-02 08:25:40 +0000537 if (Found.empty()) {
John McCalla24dc2e2009-11-17 02:14:36 +0000538 Found.clear(LookupOrdinaryName);
539 LookupName(Found, S);
John McCallf36e02d2009-10-09 21:13:30 +0000540 }
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Cedric Venet3d658642009-02-14 20:20:19 +0000542 unsigned DiagID;
John McCall1bcee0a2009-12-02 08:25:40 +0000543 if (!Found.empty())
Cedric Venet3d658642009-02-14 20:20:19 +0000544 DiagID = diag::err_expected_class_or_namespace;
Anders Carlssona31d5f72009-08-30 07:09:50 +0000545 else if (SS.isSet()) {
Douglas Gregor3f093272009-10-13 21:16:44 +0000546 Diag(IdLoc, diag::err_no_member) << &II << LookupCtx << SS.getRange();
Anders Carlssona31d5f72009-08-30 07:09:50 +0000547 return 0;
548 } else
Cedric Venet3d658642009-02-14 20:20:19 +0000549 DiagID = diag::err_undeclared_var_use;
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Cedric Venet3d658642009-02-14 20:20:19 +0000551 if (SS.isSet())
552 Diag(IdLoc, DiagID) << &II << SS.getRange();
553 else
554 Diag(IdLoc, DiagID) << &II;
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Cedric Venet3d658642009-02-14 20:20:19 +0000556 return 0;
557}
558
Douglas Gregorc68afe22009-09-03 21:38:09 +0000559/// ActOnCXXNestedNameSpecifier - Called during parsing of a
560/// nested-name-specifier. e.g. for "foo::bar::" we parsed "foo::" and now
561/// we want to resolve "bar::". 'SS' is empty or the previously parsed
562/// nested-name part ("foo::"), 'IdLoc' is the source location of 'bar',
563/// 'CCLoc' is the location of '::' and 'II' is the identifier for 'bar'.
564/// Returns a CXXScopeTy* object representing the C++ scope.
565Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000566 CXXScopeSpec &SS,
Douglas Gregorc68afe22009-09-03 21:38:09 +0000567 SourceLocation IdLoc,
568 SourceLocation CCLoc,
569 IdentifierInfo &II,
570 TypeTy *ObjectTypePtr,
571 bool EnteringContext) {
Mike Stump1eb44332009-09-09 15:08:12 +0000572 return BuildCXXNestedNameSpecifier(S, SS, IdLoc, CCLoc, II,
Douglas Gregorc68afe22009-09-03 21:38:09 +0000573 QualType::getFromOpaquePtr(ObjectTypePtr),
Chris Lattner46646492009-12-07 01:36:53 +0000574 /*ScopeLookupResult=*/0, EnteringContext,
575 false);
576}
577
578/// IsInvalidUnlessNestedName - This method is used for error recovery
579/// purposes to determine whether the specified identifier is only valid as
580/// a nested name specifier, for example a namespace name. It is
581/// conservatively correct to always return false from this method.
582///
583/// The arguments are the same as those passed to ActOnCXXNestedNameSpecifier.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000584bool Sema::IsInvalidUnlessNestedName(Scope *S, CXXScopeSpec &SS,
Douglas Gregoredc90502010-02-25 04:46:04 +0000585 IdentifierInfo &II, TypeTy *ObjectType,
Chris Lattner46646492009-12-07 01:36:53 +0000586 bool EnteringContext) {
587 return BuildCXXNestedNameSpecifier(S, SS, SourceLocation(), SourceLocation(),
Douglas Gregoredc90502010-02-25 04:46:04 +0000588 II, QualType::getFromOpaquePtr(ObjectType),
Chris Lattner46646492009-12-07 01:36:53 +0000589 /*ScopeLookupResult=*/0, EnteringContext,
590 true);
Douglas Gregorc68afe22009-09-03 21:38:09 +0000591}
592
Douglas Gregor39a8de12009-02-25 19:37:18 +0000593Sema::CXXScopeTy *Sema::ActOnCXXNestedNameSpecifier(Scope *S,
594 const CXXScopeSpec &SS,
595 TypeTy *Ty,
596 SourceRange TypeRange,
Douglas Gregoredc90502010-02-25 04:46:04 +0000597 SourceLocation CCLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000598 NestedNameSpecifier *Prefix
Douglas Gregor35073692009-03-26 23:56:24 +0000599 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000600 QualType T = GetTypeFromParser(Ty);
Douglas Gregorab452ba2009-03-26 23:50:42 +0000601 return NestedNameSpecifier::Create(Context, Prefix, /*FIXME:*/false,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000602 T.getTypePtr());
Douglas Gregor39a8de12009-02-25 19:37:18 +0000603}
604
John McCalle7e278b2009-12-11 20:04:54 +0000605bool Sema::ShouldEnterDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
606 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
607
608 NestedNameSpecifier *Qualifier =
609 static_cast<NestedNameSpecifier*>(SS.getScopeRep());
610
611 // There are only two places a well-formed program may qualify a
612 // declarator: first, when defining a namespace or class member
613 // out-of-line, and second, when naming an explicitly-qualified
614 // friend function. The latter case is governed by
615 // C++03 [basic.lookup.unqual]p10:
616 // In a friend declaration naming a member function, a name used
617 // in the function declarator and not part of a template-argument
618 // in a template-id is first looked up in the scope of the member
619 // function's class. If it is not found, or if the name is part of
620 // a template-argument in a template-id, the look up is as
621 // described for unqualified names in the definition of the class
622 // granting friendship.
623 // i.e. we don't push a scope unless it's a class member.
624
625 switch (Qualifier->getKind()) {
626 case NestedNameSpecifier::Global:
627 case NestedNameSpecifier::Namespace:
628 // These are always namespace scopes. We never want to enter a
629 // namespace scope from anything but a file context.
630 return CurContext->getLookupContext()->isFileContext();
631
632 case NestedNameSpecifier::Identifier:
633 case NestedNameSpecifier::TypeSpec:
634 case NestedNameSpecifier::TypeSpecWithTemplate:
635 // These are never namespace scopes.
636 return true;
637 }
638
639 // Silence bogus warning.
640 return false;
641}
642
Cedric Venet3d658642009-02-14 20:20:19 +0000643/// ActOnCXXEnterDeclaratorScope - Called when a C++ scope specifier (global
644/// scope or nested-name-specifier) is parsed, part of a declarator-id.
645/// After this method is called, according to [C++ 3.4.3p3], names should be
646/// looked up in the declarator-id's scope, until the declarator is parsed and
647/// ActOnCXXExitDeclaratorScope is called.
648/// The 'SS' should be a non-empty valid CXXScopeSpec.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000649bool Sema::ActOnCXXEnterDeclaratorScope(Scope *S, CXXScopeSpec &SS) {
Cedric Venet3d658642009-02-14 20:20:19 +0000650 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
John McCall7a1dc562009-12-19 10:49:29 +0000651
652 if (SS.isInvalid()) return true;
653
654 DeclContext *DC = computeDeclContext(SS, true);
655 if (!DC) return true;
656
657 // Before we enter a declarator's context, we need to make sure that
658 // it is a complete declaration context.
659 if (!DC->isDependentContext() && RequireCompleteDeclContext(SS))
660 return true;
661
662 EnterDeclaratorContext(S, DC);
John McCall31f17ec2010-04-27 00:57:59 +0000663
664 // Rebuild the nested name specifier for the new scope.
665 if (DC->isDependentContext())
666 RebuildNestedNameSpecifierInCurrentInstantiation(SS);
667
Douglas Gregor7dfd0fb2009-09-24 23:39:01 +0000668 return false;
Cedric Venet3d658642009-02-14 20:20:19 +0000669}
670
671/// ActOnCXXExitDeclaratorScope - Called when a declarator that previously
672/// invoked ActOnCXXEnterDeclaratorScope(), is finished. 'SS' is the same
673/// CXXScopeSpec that was passed to ActOnCXXEnterDeclaratorScope as well.
674/// Used to indicate that names should revert to being looked up in the
675/// defining scope.
676void Sema::ActOnCXXExitDeclaratorScope(Scope *S, const CXXScopeSpec &SS) {
677 assert(SS.isSet() && "Parser passed invalid CXXScopeSpec.");
Douglas Gregordacd4342009-08-26 00:04:55 +0000678 if (SS.isInvalid())
679 return;
John McCall7a1dc562009-12-19 10:49:29 +0000680 assert(!SS.isInvalid() && computeDeclContext(SS, true) &&
681 "exiting declarator scope we never really entered");
682 ExitDeclaratorContext(S);
Cedric Venet3d658642009-02-14 20:20:19 +0000683}