blob: 024f370c56d3288f7509f69ac277733236083c38 [file] [log] [blame]
Eli Friedman56d29372008-06-07 16:52:53 +00001//===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===//
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 the Decl and DeclContext classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclBase.h"
Douglas Gregor64650af2009-02-02 23:39:07 +000015#include "clang/AST/Decl.h"
Douglas Gregorc2ee10d2009-04-07 17:20:56 +000016#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000017#include "clang/AST/DeclCXX.h"
John McCall92b7f702010-03-11 07:50:04 +000018#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.h"
John McCall0c01d182010-03-24 05:22:00 +000021#include "clang/AST/DependentDiagnostic.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/ExternalASTSource.h"
Eli Friedman56d29372008-06-07 16:52:53 +000023#include "clang/AST/ASTContext.h"
Douglas Gregor44b43212008-12-11 16:49:14 +000024#include "clang/AST/Type.h"
Sebastian Redld3a413d2009-04-26 20:35:05 +000025#include "clang/AST/Stmt.h"
26#include "clang/AST/StmtCXX.h"
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +000027#include "clang/AST/ASTMutationListener.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000028#include "clang/Basic/TargetInfo.h"
Eli Friedman56d29372008-06-07 16:52:53 +000029#include "llvm/ADT/DenseMap.h"
Chris Lattner49f28ca2009-03-05 08:00:35 +000030#include "llvm/Support/raw_ostream.h"
Douglas Gregor6ed40e32008-12-23 21:05:05 +000031#include <algorithm>
Eli Friedman56d29372008-06-07 16:52:53 +000032using namespace clang;
33
34//===----------------------------------------------------------------------===//
35// Statistics
36//===----------------------------------------------------------------------===//
37
Sean Hunt9a555912010-05-30 07:21:58 +000038#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
39#define ABSTRACT_DECL(DECL)
40#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000041
42static bool StatSwitch = false;
43
Eli Friedman56d29372008-06-07 16:52:53 +000044const char *Decl::getDeclKindName() const {
45 switch (DeclKind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000046 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Sean Hunt9a555912010-05-30 07:21:58 +000047#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
48#define ABSTRACT_DECL(DECL)
49#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000050 }
51}
52
Douglas Gregor42738572010-03-05 00:26:45 +000053void Decl::setInvalidDecl(bool Invalid) {
54 InvalidDecl = Invalid;
55 if (Invalid) {
56 // Defensive maneuver for ill-formed code: we're likely not to make it to
57 // a point where we set the access specifier, so default it to "public"
58 // to avoid triggering asserts elsewhere in the front end.
59 setAccess(AS_public);
60 }
61}
62
Steve Naroff0a473932009-01-20 19:53:53 +000063const char *DeclContext::getDeclKindName() const {
64 switch (DeclKind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000065 default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
Sean Hunt9a555912010-05-30 07:21:58 +000066#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
67#define ABSTRACT_DECL(DECL)
68#include "clang/AST/DeclNodes.inc"
Steve Naroff0a473932009-01-20 19:53:53 +000069 }
70}
71
Eli Friedman56d29372008-06-07 16:52:53 +000072bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000073 if (Enable) StatSwitch = true;
Eli Friedman56d29372008-06-07 16:52:53 +000074 return StatSwitch;
75}
76
77void Decl::PrintStats() {
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000078 llvm::errs() << "\n*** Decl Stats:\n";
Mike Stump1eb44332009-09-09 15:08:12 +000079
Douglas Gregor64650af2009-02-02 23:39:07 +000080 int totalDecls = 0;
Sean Hunt9a555912010-05-30 07:21:58 +000081#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
82#define ABSTRACT_DECL(DECL)
83#include "clang/AST/DeclNodes.inc"
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000084 llvm::errs() << " " << totalDecls << " decls total.\n";
Mike Stump1eb44332009-09-09 15:08:12 +000085
Douglas Gregor64650af2009-02-02 23:39:07 +000086 int totalBytes = 0;
Sean Hunt9a555912010-05-30 07:21:58 +000087#define DECL(DERIVED, BASE) \
88 if (n##DERIVED##s > 0) { \
89 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000090 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
91 << sizeof(DERIVED##Decl) << " each (" \
92 << n##DERIVED##s * sizeof(DERIVED##Decl) \
93 << " bytes)\n"; \
Douglas Gregor64650af2009-02-02 23:39:07 +000094 }
Sean Hunt9a555912010-05-30 07:21:58 +000095#define ABSTRACT_DECL(DECL)
96#include "clang/AST/DeclNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +000097
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000098 llvm::errs() << "Total bytes = " << totalBytes << "\n";
Eli Friedman56d29372008-06-07 16:52:53 +000099}
100
Sean Hunt9a555912010-05-30 07:21:58 +0000101void Decl::add(Kind k) {
Eli Friedman56d29372008-06-07 16:52:53 +0000102 switch (k) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000103 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Sean Hunt9a555912010-05-30 07:21:58 +0000104#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
105#define ABSTRACT_DECL(DECL)
106#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +0000107 }
108}
109
Anders Carlsson67e33202009-06-13 00:08:58 +0000110bool Decl::isTemplateParameterPack() const {
111 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
112 return TTP->isParameterPack();
Douglas Gregor10738d32010-12-23 23:51:58 +0000113 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregor61c4d282011-01-05 15:48:55 +0000114 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregor10738d32010-12-23 23:51:58 +0000115 return NTTP->isParameterPack();
Douglas Gregor61c4d282011-01-05 15:48:55 +0000116 if (const TemplateTemplateParmDecl *TTP
117 = dyn_cast<TemplateTemplateParmDecl>(this))
118 return TTP->isParameterPack();
Anders Carlsson67e33202009-06-13 00:08:58 +0000119 return false;
120}
121
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000122bool Decl::isParameterPack() const {
123 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
124 return Parm->isParameterPack();
125
126 return isTemplateParameterPack();
127}
128
Douglas Gregore53060f2009-06-25 22:08:12 +0000129bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000130 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000131 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Douglas Gregore53060f2009-06-25 22:08:12 +0000133 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
134}
135
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000136bool Decl::isTemplateDecl() const {
137 return isa<TemplateDecl>(this);
138}
139
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000140const DeclContext *Decl::getParentFunctionOrMethod() const {
141 for (const DeclContext *DC = getDeclContext();
142 DC && !DC->isTranslationUnit() && !DC->isNamespace();
Douglas Gregor79c22782010-01-16 20:21:20 +0000143 DC = DC->getParent())
144 if (DC->isFunctionOrMethod())
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000145 return DC;
Douglas Gregor79c22782010-01-16 20:21:20 +0000146
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000147 return 0;
Douglas Gregor79c22782010-01-16 20:21:20 +0000148}
149
Douglas Gregor4c3e0ee2011-02-17 08:47:29 +0000150
Eli Friedman56d29372008-06-07 16:52:53 +0000151//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000152// PrettyStackTraceDecl Implementation
153//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Chris Lattner5f9e2722011-07-23 10:55:15 +0000155void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000156 SourceLocation TheLoc = Loc;
157 if (TheLoc.isInvalid() && TheDecl)
158 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Chris Lattner49f28ca2009-03-05 08:00:35 +0000160 if (TheLoc.isValid()) {
161 TheLoc.print(OS, SM);
162 OS << ": ";
163 }
164
165 OS << Message;
166
Daniel Dunbarc5236562009-11-21 09:05:59 +0000167 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000168 OS << " '" << DN->getQualifiedNameAsString() << '\'';
169 OS << '\n';
170}
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Chris Lattner49f28ca2009-03-05 08:00:35 +0000172//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000173// Decl Implementation
174//===----------------------------------------------------------------------===//
175
Douglas Gregorda2142f2011-02-19 18:51:44 +0000176// Out-of-line virtual method providing a home for Decl.
177Decl::~Decl() { }
Douglas Gregorf4a03cc2011-02-17 07:02:32 +0000178
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000179void Decl::setDeclContext(DeclContext *DC) {
Chris Lattneree219fd2009-03-29 06:06:59 +0000180 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000181}
182
183void Decl::setLexicalDeclContext(DeclContext *DC) {
184 if (DC == getLexicalDeclContext())
185 return;
186
187 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000188 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000189 MDC->SemanticDC = getDeclContext();
190 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000191 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000192 } else {
193 getMultipleDC()->LexicalDC = DC;
194 }
195}
196
John McCall9aeed322009-10-01 00:25:31 +0000197bool Decl::isInAnonymousNamespace() const {
198 const DeclContext *DC = getDeclContext();
199 do {
200 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
201 if (ND->isAnonymousNamespace())
202 return true;
203 } while ((DC = DC->getParent()));
204
205 return false;
206}
207
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000208TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000209 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
210 return TUD;
211
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000212 DeclContext *DC = getDeclContext();
213 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000215 while (!DC->isTranslationUnit()) {
216 DC = DC->getParent();
217 assert(DC && "This decl is not contained in a translation unit!");
218 }
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000220 return cast<TranslationUnitDecl>(DC);
221}
222
223ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000224 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000225}
226
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000227ASTMutationListener *Decl::getASTMutationListener() const {
228 return getASTContext().getASTMutationListener();
229}
230
Douglas Gregorc070cc62010-06-17 23:14:26 +0000231bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000232 if (Used)
233 return true;
234
235 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000236 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000237 return true;
238
239 // Check redeclarations for used attribute.
240 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorc070cc62010-06-17 23:14:26 +0000241 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner12ead492010-02-17 02:17:21 +0000242 return true;
243 }
244
245 return false;
246}
247
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000248bool Decl::isReferenced() const {
249 if (Referenced)
250 return true;
251
252 // Check redeclarations.
253 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
254 if (I->Referenced)
255 return true;
256
257 return false;
258}
259
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000260/// \brief Determine the availability of the given declaration based on
261/// the target platform.
262///
263/// When it returns an availability result other than \c AR_Available,
264/// if the \p Message parameter is non-NULL, it will be set to a
265/// string describing why the entity is unavailable.
266///
267/// FIXME: Make these strings localizable, since they end up in
268/// diagnostics.
269static AvailabilityResult CheckAvailability(ASTContext &Context,
270 const AvailabilityAttr *A,
271 std::string *Message) {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000272 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000273 StringRef PrettyPlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000274 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
275 if (PrettyPlatformName.empty())
276 PrettyPlatformName = TargetPlatform;
277
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000278 VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000279 if (TargetMinVersion.empty())
280 return AR_Available;
281
282 // Match the platform name.
283 if (A->getPlatform()->getName() != TargetPlatform)
284 return AR_Available;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000285
286 std::string HintMessage;
287 if (!A->getMessage().empty()) {
288 HintMessage = " - ";
289 HintMessage += A->getMessage();
290 }
291
Douglas Gregorb53e4172011-03-26 03:35:55 +0000292 // Make sure that this declaration has not been marked 'unavailable'.
293 if (A->getUnavailable()) {
294 if (Message) {
295 Message->clear();
296 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000297 Out << "not available on " << PrettyPlatformName
298 << HintMessage;
Douglas Gregorb53e4172011-03-26 03:35:55 +0000299 }
300
301 return AR_Unavailable;
302 }
303
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000304 // Make sure that this declaration has already been introduced.
305 if (!A->getIntroduced().empty() &&
306 TargetMinVersion < A->getIntroduced()) {
307 if (Message) {
308 Message->clear();
309 llvm::raw_string_ostream Out(*Message);
310 Out << "introduced in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000311 << A->getIntroduced() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000312 }
313
314 return AR_NotYetIntroduced;
315 }
316
317 // Make sure that this declaration hasn't been obsoleted.
318 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
319 if (Message) {
320 Message->clear();
321 llvm::raw_string_ostream Out(*Message);
322 Out << "obsoleted in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000323 << A->getObsoleted() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000324 }
325
326 return AR_Unavailable;
327 }
328
329 // Make sure that this declaration hasn't been deprecated.
330 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
331 if (Message) {
332 Message->clear();
333 llvm::raw_string_ostream Out(*Message);
334 Out << "first deprecated in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000335 << A->getDeprecated() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000336 }
337
338 return AR_Deprecated;
339 }
340
341 return AR_Available;
342}
343
344AvailabilityResult Decl::getAvailability(std::string *Message) const {
345 AvailabilityResult Result = AR_Available;
346 std::string ResultMessage;
347
348 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
349 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
350 if (Result >= AR_Deprecated)
351 continue;
352
353 if (Message)
354 ResultMessage = Deprecated->getMessage();
355
356 Result = AR_Deprecated;
357 continue;
358 }
359
360 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
361 if (Message)
362 *Message = Unavailable->getMessage();
363 return AR_Unavailable;
364 }
365
366 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
367 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
368 Message);
369
370 if (AR == AR_Unavailable)
371 return AR_Unavailable;
372
373 if (AR > Result) {
374 Result = AR;
375 if (Message)
376 ResultMessage.swap(*Message);
377 }
378 continue;
379 }
380 }
381
382 if (Message)
383 Message->swap(ResultMessage);
384 return Result;
385}
386
387bool Decl::canBeWeakImported(bool &IsDefinition) const {
388 IsDefinition = false;
389 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
390 if (!Var->hasExternalStorage() || Var->getInit()) {
391 IsDefinition = true;
392 return false;
393 }
394 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
395 if (FD->hasBody()) {
396 IsDefinition = true;
397 return false;
398 }
399 } else if (isa<ObjCPropertyDecl>(this) || isa<ObjCMethodDecl>(this))
400 return false;
401 else if (!(getASTContext().getLangOptions().ObjCNonFragileABI &&
402 isa<ObjCInterfaceDecl>(this)))
403 return false;
404
405 return true;
406}
407
408bool Decl::isWeakImported() const {
409 bool IsDefinition;
410 if (!canBeWeakImported(IsDefinition))
411 return false;
412
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000413 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
414 if (isa<WeakImportAttr>(*A))
415 return true;
416
417 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
418 if (CheckAvailability(getASTContext(), Availability, 0)
419 == AR_NotYetIntroduced)
420 return true;
421 }
422 }
423
424 return false;
425}
Tanya Lattner12ead492010-02-17 02:17:21 +0000426
Chris Lattner769dbdf2009-03-27 20:18:19 +0000427unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
428 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000429 case Function:
430 case CXXMethod:
431 case CXXConstructor:
432 case CXXDestructor:
433 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000434 case EnumConstant:
435 case Var:
436 case ImplicitParam:
437 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000438 case NonTypeTemplateParm:
439 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000440 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000441 return IDNS_Ordinary;
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000442 case Label:
443 return IDNS_Label;
Francois Pichet87c2e122010-11-21 06:08:52 +0000444 case IndirectField:
445 return IDNS_Ordinary | IDNS_Member;
446
John McCall0d6b1642010-04-23 18:46:30 +0000447 case ObjCCompatibleAlias:
448 case ObjCInterface:
449 return IDNS_Ordinary | IDNS_Type;
450
451 case Typedef:
Richard Smith162e1c12011-04-15 14:24:37 +0000452 case TypeAlias:
Richard Smith3e4c6c42011-05-05 21:57:07 +0000453 case TypeAliasTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000454 case UnresolvedUsingTypename:
455 case TemplateTypeParm:
456 return IDNS_Ordinary | IDNS_Type;
457
John McCall9488ea12009-11-17 05:59:44 +0000458 case UsingShadow:
459 return 0; // we'll actually overwrite this later
460
John McCall7ba107a2009-11-18 02:36:19 +0000461 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000462 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000463
464 case Using:
465 return IDNS_Using;
466
Chris Lattner769dbdf2009-03-27 20:18:19 +0000467 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000468 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Chris Lattner769dbdf2009-03-27 20:18:19 +0000470 case Field:
471 case ObjCAtDefsField:
472 case ObjCIvar:
473 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Chris Lattner769dbdf2009-03-27 20:18:19 +0000475 case Record:
476 case CXXRecord:
477 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000478 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Chris Lattner769dbdf2009-03-27 20:18:19 +0000480 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000481 case NamespaceAlias:
482 return IDNS_Namespace;
483
Chris Lattner769dbdf2009-03-27 20:18:19 +0000484 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000485 return IDNS_Ordinary;
486
Chris Lattner769dbdf2009-03-27 20:18:19 +0000487 case ClassTemplate:
488 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000489 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Chris Lattner769dbdf2009-03-27 20:18:19 +0000491 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000492 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000493 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000494 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000495 case LinkageSpec:
496 case FileScopeAsm:
497 case StaticAssert:
498 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000499 case ObjCPropertyImpl:
500 case ObjCForwardProtocol:
501 case Block:
502 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000503
Chris Lattner769dbdf2009-03-27 20:18:19 +0000504 case UsingDirective:
505 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000506 case ClassTemplatePartialSpecialization:
Francois Pichetaf0f4d02011-08-14 03:52:19 +0000507 case ClassScopeFunctionSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000508 case ObjCImplementation:
509 case ObjCCategory:
510 case ObjCCategoryImpl:
Douglas Gregor15de72c2011-12-02 23:23:56 +0000511 case Import:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000512 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000513 return 0;
514 }
John McCall9488ea12009-11-17 05:59:44 +0000515
516 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000517}
518
Sean Huntcf807c42010-08-18 23:23:40 +0000519void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000520 assert(!HasAttrs && "Decl already contains attrs.");
521
Sean Huntcf807c42010-08-18 23:23:40 +0000522 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
523 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000524
525 AttrBlank = attrs;
526 HasAttrs = true;
527}
528
Sean Huntcf807c42010-08-18 23:23:40 +0000529void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000530 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Eli Friedman56d29372008-06-07 16:52:53 +0000532 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000533 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000534}
535
Sean Huntcf807c42010-08-18 23:23:40 +0000536const AttrVec &Decl::getAttrs() const {
537 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000538 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000539}
540
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000541void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000542 bool HasLHSAttr = this->HasAttrs;
543 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Eli Friedman56d29372008-06-07 16:52:53 +0000545 // Usually, neither decl has attrs, nothing to do.
546 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Eli Friedman56d29372008-06-07 16:52:53 +0000548 // If 'this' has no attrs, swap the other way.
549 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000550 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000552 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Eli Friedman56d29372008-06-07 16:52:53 +0000554 // Handle the case when both decls have attrs.
555 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000556 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000557 return;
558 }
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Eli Friedman56d29372008-06-07 16:52:53 +0000560 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000561 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
562 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000563 this->HasAttrs = false;
564 RHS->HasAttrs = true;
565}
566
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000567Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000568 Decl::Kind DK = D->getDeclKind();
569 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000570#define DECL(NAME, BASE)
571#define DECL_CONTEXT(NAME) \
572 case Decl::NAME: \
573 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
574#define DECL_CONTEXT_BASE(NAME)
575#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000576 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000577#define DECL(NAME, BASE)
578#define DECL_CONTEXT_BASE(NAME) \
579 if (DK >= first##NAME && DK <= last##NAME) \
580 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
581#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000582 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000583 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000584}
585
586DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000587 Decl::Kind DK = D->getKind();
588 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000589#define DECL(NAME, BASE)
590#define DECL_CONTEXT(NAME) \
591 case Decl::NAME: \
592 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
593#define DECL_CONTEXT_BASE(NAME)
594#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000595 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000596#define DECL(NAME, BASE)
597#define DECL_CONTEXT_BASE(NAME) \
598 if (DK >= first##NAME && DK <= last##NAME) \
599 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
600#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000601 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000602 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000603}
604
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000605SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000606 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
607 // FunctionDecl stores EndRangeLoc for this purpose.
608 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
609 const FunctionDecl *Definition;
610 if (FD->hasBody(Definition))
611 return Definition->getSourceRange().getEnd();
612 return SourceLocation();
613 }
614
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000615 if (Stmt *Body = getBody())
616 return Body->getSourceRange().getEnd();
617
618 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000619}
620
Anders Carlsson1329c272009-03-25 23:38:06 +0000621void Decl::CheckAccessDeclContext() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000622#ifndef NDEBUG
John McCall46460a62010-01-20 21:53:11 +0000623 // Suppress this check if any of the following hold:
624 // 1. this is the translation unit (and thus has no parent)
625 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000626 // 3. this is a non-type template parameter
627 // 4. the context is not a record
628 // 5. it's invalid
629 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000630 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000631 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000632 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000633 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000634 isInvalidDecl() ||
635 isa<StaticAssertDecl>(this) ||
636 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
637 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000638 isa<ParmVarDecl>(this) ||
639 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
640 // AS_none as access specifier.
Francois Pichetbc845322011-08-17 01:06:54 +0000641 isa<CXXRecordDecl>(this) ||
642 isa<ClassScopeFunctionSpecializationDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000643 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000644
645 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000646 "Access specifier is AS_none inside a record decl");
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000647#endif
Anders Carlsson1329c272009-03-25 23:38:06 +0000648}
649
John McCallaab9e312011-02-22 22:25:23 +0000650DeclContext *Decl::getNonClosureContext() {
John McCall4b9c2d22011-11-06 09:01:30 +0000651 return getDeclContext()->getNonClosureAncestor();
652}
653
654DeclContext *DeclContext::getNonClosureAncestor() {
655 DeclContext *DC = this;
John McCallaab9e312011-02-22 22:25:23 +0000656
657 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
658 // except that it's significantly more efficient to cast to a known
659 // decl type and call getDeclContext() than to call getParent().
John McCall7b3f8532011-06-23 21:18:31 +0000660 while (isa<BlockDecl>(DC))
661 DC = cast<BlockDecl>(DC)->getDeclContext();
John McCallaab9e312011-02-22 22:25:23 +0000662
663 assert(!DC->isClosure());
664 return DC;
665}
Anders Carlsson1329c272009-03-25 23:38:06 +0000666
Eli Friedman56d29372008-06-07 16:52:53 +0000667//===----------------------------------------------------------------------===//
668// DeclContext Implementation
669//===----------------------------------------------------------------------===//
670
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000671bool DeclContext::classof(const Decl *D) {
672 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000673#define DECL(NAME, BASE)
674#define DECL_CONTEXT(NAME) case Decl::NAME:
675#define DECL_CONTEXT_BASE(NAME)
676#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000677 return true;
678 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000679#define DECL(NAME, BASE)
680#define DECL_CONTEXT_BASE(NAME) \
681 if (D->getKind() >= Decl::first##NAME && \
682 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000683 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000684#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000685 return false;
686 }
687}
688
Douglas Gregora2da7802010-07-25 18:38:02 +0000689DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000690
Douglas Gregore942bbe2009-09-10 16:57:35 +0000691/// \brief Find the parent context of this context that will be
692/// used for unqualified name lookup.
693///
694/// Generally, the parent lookup context is the semantic context. However, for
695/// a friend function the parent lookup context is the lexical context, which
696/// is the class in which the friend is declared.
697DeclContext *DeclContext::getLookupParent() {
698 // FIXME: Find a better way to identify friends
699 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000700 if (getParent()->getRedeclContext()->isFileContext() &&
701 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000702 return getLexicalParent();
703
704 return getParent();
705}
706
Sebastian Redl410c4f22010-08-31 20:53:31 +0000707bool DeclContext::isInlineNamespace() const {
708 return isNamespace() &&
709 cast<NamespaceDecl>(this)->isInline();
710}
711
Douglas Gregorbc221632009-05-28 16:34:51 +0000712bool DeclContext::isDependentContext() const {
713 if (isFileContext())
714 return false;
715
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000716 if (isa<ClassTemplatePartialSpecializationDecl>(this))
717 return true;
718
Douglas Gregorbc221632009-05-28 16:34:51 +0000719 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
720 if (Record->getDescribedClassTemplate())
721 return true;
722
John McCall0c01d182010-03-24 05:22:00 +0000723 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000724 if (Function->getDescribedFunctionTemplate())
725 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000726
John McCall0c01d182010-03-24 05:22:00 +0000727 // Friend function declarations are dependent if their *lexical*
728 // context is dependent.
729 if (cast<Decl>(this)->getFriendObjectKind())
730 return getLexicalParent()->isDependentContext();
731 }
732
Douglas Gregorbc221632009-05-28 16:34:51 +0000733 return getParent() && getParent()->isDependentContext();
734}
735
Douglas Gregor074149e2009-01-05 19:45:36 +0000736bool DeclContext::isTransparentContext() const {
737 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000738 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000739 else if (DeclKind == Decl::LinkageSpec)
740 return true;
Douglas Gregor074149e2009-01-05 19:45:36 +0000741
742 return false;
743}
744
John McCallac65c622010-10-26 04:59:26 +0000745bool DeclContext::isExternCContext() const {
746 const DeclContext *DC = this;
747 while (DC->DeclKind != Decl::TranslationUnit) {
748 if (DC->DeclKind == Decl::LinkageSpec)
749 return cast<LinkageSpecDecl>(DC)->getLanguage()
750 == LinkageSpecDecl::lang_c;
751 DC = DC->getParent();
752 }
753 return false;
754}
755
Sebastian Redl7a126a42010-08-31 00:36:30 +0000756bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000757 if (getPrimaryContext() != this)
758 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000760 for (; DC; DC = DC->getParent())
761 if (DC->getPrimaryContext() == this)
762 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000763 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000764}
765
Steve Naroff0701bbb2009-01-08 17:28:14 +0000766DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000767 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000768 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000769 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000770 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000771 // There is only one DeclContext for these entities.
772 return this;
773
774 case Decl::Namespace:
775 // The original namespace is our primary context.
776 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
777
Douglas Gregor44b43212008-12-11 16:49:14 +0000778 case Decl::ObjCMethod:
779 return this;
780
781 case Decl::ObjCInterface:
Douglas Gregor53df7a12011-12-15 18:03:09 +0000782 if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
783 return Def;
784
785 return this;
786
Steve Naroff0701bbb2009-01-08 17:28:14 +0000787 case Decl::ObjCProtocol:
Douglas Gregor53df7a12011-12-15 18:03:09 +0000788 // FIXME: Update when protocols properly model forward declarations.
789 // For now, it's fine to fall through
790
Steve Naroff0701bbb2009-01-08 17:28:14 +0000791 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000792 return this;
793
Steve Naroff0701bbb2009-01-08 17:28:14 +0000794 case Decl::ObjCImplementation:
795 case Decl::ObjCCategoryImpl:
796 return this;
797
Douglas Gregor44b43212008-12-11 16:49:14 +0000798 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000799 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000800 // If this is a tag type that has a definition or is currently
801 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000802 TagDecl *Tag = cast<TagDecl>(this);
803 assert(isa<TagType>(Tag->TypeForDecl) ||
804 isa<InjectedClassNameType>(Tag->TypeForDecl));
805
806 if (TagDecl *Def = Tag->getDefinition())
807 return Def;
808
809 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
810 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
811 if (TagTy->isBeingDefined())
812 // FIXME: is it necessarily being defined in the decl
813 // that owns the type?
814 return TagTy->getDecl();
815 }
816
817 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000818 }
819
Sean Hunt9a555912010-05-30 07:21:58 +0000820 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000821 "Unknown DeclContext kind");
822 return this;
823 }
824}
825
826DeclContext *DeclContext::getNextContext() {
827 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000828 case Decl::Namespace:
829 // Return the next namespace
830 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
831
832 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000833 return 0;
834 }
835}
836
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000837std::pair<Decl *, Decl *>
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000838DeclContext::BuildDeclChain(const SmallVectorImpl<Decl*> &Decls,
839 bool FieldsAlreadyLoaded) {
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000840 // Build up a chain of declarations via the Decl::NextDeclInContext field.
841 Decl *FirstNewDecl = 0;
842 Decl *PrevDecl = 0;
843 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000844 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
845 continue;
846
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000847 Decl *D = Decls[I];
848 if (PrevDecl)
849 PrevDecl->NextDeclInContext = D;
850 else
851 FirstNewDecl = D;
852
853 PrevDecl = D;
854 }
855
856 return std::make_pair(FirstNewDecl, PrevDecl);
857}
858
Douglas Gregor2cf26342009-04-09 22:27:44 +0000859/// \brief Load the declarations within this lexical storage from an
860/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000861void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000862DeclContext::LoadLexicalDeclsFromExternalStorage() const {
863 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000864 assert(hasExternalLexicalStorage() && Source && "No external storage?");
865
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000866 // Notify that we have a DeclContext that is initializing.
867 ExternalASTSource::Deserializing ADeclContext(Source);
Douglas Gregor9fc18c92011-08-26 21:23:06 +0000868
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000869 // Load the external declarations, if any.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000870 SmallVector<Decl*, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000871 ExternalLexicalStorage = false;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000872 switch (Source->FindExternalLexicalDecls(this, Decls)) {
873 case ELR_Success:
874 break;
875
876 case ELR_Failure:
877 case ELR_AlreadyLoaded:
878 return;
879 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000880
881 if (Decls.empty())
882 return;
883
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000884 // We may have already loaded just the fields of this record, in which case
885 // we need to ignore them.
886 bool FieldsAlreadyLoaded = false;
887 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
888 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
889
Douglas Gregor2cf26342009-04-09 22:27:44 +0000890 // Splice the newly-read declarations into the beginning of the list
891 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000892 Decl *ExternalFirst, *ExternalLast;
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000893 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls,
894 FieldsAlreadyLoaded);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000895 ExternalLast->NextDeclInContext = FirstDecl;
896 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000897 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000898 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000899}
900
John McCall76bd1f32010-06-01 09:23:16 +0000901DeclContext::lookup_result
902ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
903 DeclarationName Name) {
904 ASTContext &Context = DC->getParentASTContext();
905 StoredDeclsMap *Map;
906 if (!(Map = DC->LookupPtr))
907 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000908
John McCall76bd1f32010-06-01 09:23:16 +0000909 StoredDeclsList &List = (*Map)[Name];
910 assert(List.isNull());
911 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000912
John McCall76bd1f32010-06-01 09:23:16 +0000913 return DeclContext::lookup_result();
914}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000915
John McCall76bd1f32010-06-01 09:23:16 +0000916DeclContext::lookup_result
917ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +0000918 DeclarationName Name,
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000919 ArrayRef<NamedDecl*> Decls) {
John McCall76bd1f32010-06-01 09:23:16 +0000920 ASTContext &Context = DC->getParentASTContext();;
921
922 StoredDeclsMap *Map;
923 if (!(Map = DC->LookupPtr))
924 Map = DC->CreateStoredDeclsMap(Context);
925
926 StoredDeclsList &List = (*Map)[Name];
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000927 for (ArrayRef<NamedDecl*>::iterator
928 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
John McCall76bd1f32010-06-01 09:23:16 +0000929 if (List.isNull())
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000930 List.setOnlyValue(*I);
John McCall76bd1f32010-06-01 09:23:16 +0000931 else
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000932 List.AddSubsequentDecl(*I);
John McCall76bd1f32010-06-01 09:23:16 +0000933 }
934
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000935 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000936}
937
Sebastian Redl681d7232010-07-27 00:17:23 +0000938DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
939 return decl_iterator(FirstDecl);
940}
941
942DeclContext::decl_iterator DeclContext::noload_decls_end() const {
943 return decl_iterator();
944}
945
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000946DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000947 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000948 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000949
Mike Stump1eb44332009-09-09 15:08:12 +0000950 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000951}
952
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000953DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000954 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000955 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000956
Mike Stump1eb44332009-09-09 15:08:12 +0000957 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000958}
959
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000960bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000961 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000962 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000963
964 return !FirstDecl;
965}
966
John McCall9f54ad42009-12-10 09:41:52 +0000967void DeclContext::removeDecl(Decl *D) {
968 assert(D->getLexicalDeclContext() == this &&
969 "decl being removed from non-lexical context");
970 assert((D->NextDeclInContext || D == LastDecl) &&
971 "decl is not in decls list");
972
973 // Remove D from the decl chain. This is O(n) but hopefully rare.
974 if (D == FirstDecl) {
975 if (D == LastDecl)
976 FirstDecl = LastDecl = 0;
977 else
978 FirstDecl = D->NextDeclInContext;
979 } else {
980 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
981 assert(I && "decl not found in linked list");
982 if (I->NextDeclInContext == D) {
983 I->NextDeclInContext = D->NextDeclInContext;
984 if (D == LastDecl) LastDecl = I;
985 break;
986 }
987 }
988 }
989
990 // Mark that D is no longer in the decl chain.
991 D->NextDeclInContext = 0;
992
993 // Remove D from the lookup table if necessary.
994 if (isa<NamedDecl>(D)) {
995 NamedDecl *ND = cast<NamedDecl>(D);
996
Axel Naumann02368d02011-08-26 14:06:12 +0000997 // Remove only decls that have a name
998 if (!ND->getDeclName()) return;
999
John McCall0c01d182010-03-24 05:22:00 +00001000 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
1001 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +00001002
John McCall9f54ad42009-12-10 09:41:52 +00001003 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1004 assert(Pos != Map->end() && "no lookup entry for decl");
Axel Naumannd9d137e2011-11-08 18:21:06 +00001005 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1006 Pos->second.remove(ND);
John McCall9f54ad42009-12-10 09:41:52 +00001007 }
1008}
1009
John McCall3f9a8a62009-08-11 06:59:38 +00001010void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +00001011 assert(D->getLexicalDeclContext() == this &&
1012 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +00001013 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001014 "Decl already inserted into a DeclContext");
1015
1016 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +00001017 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001018 LastDecl = D;
1019 } else {
1020 FirstDecl = LastDecl = D;
1021 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +00001022
1023 // Notify a C++ record declaration that we've added a member, so it can
1024 // update it's class-specific state.
1025 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1026 Record->addedMember(D);
Douglas Gregore6649772011-12-03 00:30:27 +00001027
1028 // If this is a newly-created (not de-serialized) import declaration, wire
1029 // it in to the list of local import declarations.
1030 if (!D->isFromASTFile()) {
1031 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1032 D->getASTContext().addedLocalImportDecl(Import);
1033 }
John McCall3f9a8a62009-08-11 06:59:38 +00001034}
1035
1036void DeclContext::addDecl(Decl *D) {
1037 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001038
1039 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001040 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +00001041}
1042
Sean Callanan9faf8102011-10-21 02:57:43 +00001043void DeclContext::addDeclInternal(Decl *D) {
1044 addHiddenDecl(D);
1045
1046 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1047 ND->getDeclContext()->makeDeclVisibleInContextInternal(ND);
1048}
1049
Douglas Gregor074149e2009-01-05 19:45:36 +00001050/// buildLookup - Build the lookup data structure with all of the
1051/// declarations in DCtx (and any other contexts linked to it or
1052/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001053void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001054 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001055 for (decl_iterator D = DCtx->decls_begin(),
1056 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +00001057 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +00001058 // Insert this declaration into the lookup structure, but only
1059 // if it's semantically in its decl context. During non-lazy
1060 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001061 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +00001062 if (D->getDeclContext() == DCtx)
Sean Callanan9faf8102011-10-21 02:57:43 +00001063 makeDeclVisibleInContextImpl(ND, false);
Douglas Gregor074149e2009-01-05 19:45:36 +00001064
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001065 // Insert any forward-declared Objective-C interface into the lookup
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001066 // data structure.
1067 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
Sean Callanan9faf8102011-10-21 02:57:43 +00001068 makeDeclVisibleInContextImpl(Class->getForwardInterfaceDecl(), false);
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001069
Sebastian Redl410c4f22010-08-31 20:53:31 +00001070 // If this declaration is itself a transparent declaration context or
1071 // inline namespace, add its members (recursively).
Douglas Gregor074149e2009-01-05 19:45:36 +00001072 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redl410c4f22010-08-31 20:53:31 +00001073 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001074 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +00001075 }
1076 }
1077}
1078
Mike Stump1eb44332009-09-09 15:08:12 +00001079DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001080DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +00001081 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001082 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001083 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001084
John McCall76bd1f32010-06-01 09:23:16 +00001085 if (hasExternalVisibleStorage()) {
1086 // Check to see if we've already cached the lookup results.
1087 if (LookupPtr) {
1088 StoredDeclsMap::iterator I = LookupPtr->find(Name);
1089 if (I != LookupPtr->end())
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001090 return I->second.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +00001091 }
1092
1093 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1094 return Source->FindExternalVisibleDeclsByName(this, Name);
1095 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001096
Douglas Gregor3fc749d2008-12-23 00:26:44 +00001097 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +00001098 /// all of the linked DeclContexts (in declaration order!) and
1099 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +00001100 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001101 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +00001102
Douglas Gregorc36c5402009-04-09 17:29:08 +00001103 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001104 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +00001105 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001106
John McCall0c01d182010-03-24 05:22:00 +00001107 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1108 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001109 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001110 return Pos->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +00001111}
1112
Mike Stump1eb44332009-09-09 15:08:12 +00001113DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001114DeclContext::lookup(DeclarationName Name) const {
1115 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001116}
1117
Douglas Gregorb75a3452011-10-15 00:10:27 +00001118void DeclContext::localUncachedLookup(DeclarationName Name,
1119 llvm::SmallVectorImpl<NamedDecl *> &Results) {
1120 Results.clear();
1121
1122 // If there's no external storage, just perform a normal lookup and copy
1123 // the results.
1124 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage()) {
1125 lookup_result LookupResults = lookup(Name);
1126 Results.insert(Results.end(), LookupResults.first, LookupResults.second);
1127 return;
1128 }
1129
1130 // If we have a lookup table, check there first. Maybe we'll get lucky.
1131 if (LookupPtr) {
1132 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1133 if (Pos != LookupPtr->end()) {
1134 Results.insert(Results.end(),
1135 Pos->second.getLookupResult().first,
1136 Pos->second.getLookupResult().second);
1137 return;
1138 }
1139 }
1140
1141 // Slow case: grovel through the declarations in our chain looking for
1142 // matches.
1143 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1144 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1145 if (ND->getDeclName() == Name)
1146 Results.push_back(ND);
1147 }
1148}
1149
Sebastian Redl7a126a42010-08-31 00:36:30 +00001150DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001151 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +00001152 // Skip through transparent contexts.
1153 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +00001154 Ctx = Ctx->getParent();
1155 return Ctx;
1156}
1157
Douglas Gregor88b70942009-02-25 22:02:03 +00001158DeclContext *DeclContext::getEnclosingNamespaceContext() {
1159 DeclContext *Ctx = this;
1160 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +00001161 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +00001162 Ctx = Ctx->getParent();
1163 return Ctx->getPrimaryContext();
1164}
1165
Sebastian Redl7a126a42010-08-31 00:36:30 +00001166bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1167 // For non-file contexts, this is equivalent to Equals.
1168 if (!isFileContext())
1169 return O->Equals(this);
1170
1171 do {
1172 if (O->Equals(this))
1173 return true;
1174
1175 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1176 if (!NS || !NS->isInline())
1177 break;
1178 O = NS->getParent();
1179 } while (O);
1180
1181 return false;
1182}
1183
Sean Callanan9faf8102011-10-21 02:57:43 +00001184void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable)
1185{
1186 makeDeclVisibleInContextWithFlags(D, false, Recoverable);
1187}
1188
1189void DeclContext::makeDeclVisibleInContextInternal(NamedDecl *D, bool Recoverable)
1190{
1191 makeDeclVisibleInContextWithFlags(D, true, Recoverable);
1192}
1193
1194void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +00001195 // FIXME: This feels like a hack. Should DeclarationName support
1196 // template-ids, or is there a better way to keep specializations
1197 // from being visible?
Douglas Gregor9a299e02011-03-04 17:52:15 +00001198 if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
Douglas Gregorcc636682009-02-17 23:15:12 +00001199 return;
Eli Friedman6bc20132009-12-08 05:40:03 +00001200 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1201 if (FD->isFunctionTemplateSpecialization())
1202 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001203
Steve Naroff0701bbb2009-01-08 17:28:14 +00001204 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001205 if (PrimaryContext != this) {
Sean Callanan9faf8102011-10-21 02:57:43 +00001206 PrimaryContext->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +00001207 return;
1208 }
1209
1210 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001211 // into it. If we haven't deserialized externally stored decls, deserialize
1212 // them so we can add the decl. Otherwise, be lazy and don't build that
1213 // structure until someone asks for it.
1214 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Sean Callanan9faf8102011-10-21 02:57:43 +00001215 makeDeclVisibleInContextImpl(D, Internal);
Douglas Gregor074149e2009-01-05 19:45:36 +00001216
Sebastian Redl410c4f22010-08-31 20:53:31 +00001217 // If we are a transparent context or inline namespace, insert into our
1218 // parent context, too. This operation is recursive.
1219 if (isTransparentContext() || isInlineNamespace())
Sean Callanan9faf8102011-10-21 02:57:43 +00001220 getParent()->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00001221
1222 Decl *DCAsDecl = cast<Decl>(this);
1223 // Notify that a decl was made visible unless it's a Tag being defined.
1224 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1225 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1226 L->AddedVisibleDecl(this, D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001227}
1228
Sean Callanan9faf8102011-10-21 02:57:43 +00001229void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001230 // Skip unnamed declarations.
1231 if (!D->getDeclName())
1232 return;
1233
Douglas Gregor5cb0ef42011-05-06 23:32:38 +00001234 // Skip entities that can't be found by name lookup into a particular
1235 // context.
1236 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1237 D->isTemplateParameter())
Douglas Gregorcc636682009-02-17 23:15:12 +00001238 return;
1239
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001240 ASTContext *C = 0;
1241 if (!LookupPtr) {
1242 C = &getParentASTContext();
1243 CreateStoredDeclsMap(*C);
1244 }
1245
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001246 // If there is an external AST source, load any declarations it knows about
1247 // with this declaration's name.
1248 // If the lookup table contains an entry about this name it means that we
1249 // have already checked the external source.
Sean Callanan9faf8102011-10-21 02:57:43 +00001250 if (!Internal)
1251 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1252 if (hasExternalVisibleStorage() &&
1253 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1254 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001255
Douglas Gregor44b43212008-12-11 16:49:14 +00001256 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +00001257 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +00001258 if (DeclNameEntries.isNull()) {
1259 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001260 return;
Douglas Gregor44b43212008-12-11 16:49:14 +00001261 }
Chris Lattner91942502009-02-20 00:55:03 +00001262
Chris Lattnerbdc3d002009-02-20 01:10:07 +00001263 // If it is possible that this is a redeclaration, check to see if there is
1264 // already a decl for which declarationReplaces returns true. If there is
1265 // one, just replace it and return.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001266 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattner67762a32009-02-20 01:44:05 +00001267 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001269 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +00001270 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001271}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001272
1273/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1274/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001275DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001276DeclContext::getUsingDirectives() const {
1277 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001278 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1279 reinterpret_cast<udir_iterator>(Result.second));
1280}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001281
Ted Kremenek3478eb62010-02-11 07:12:28 +00001282//===----------------------------------------------------------------------===//
1283// Creation and Destruction of StoredDeclsMaps. //
1284//===----------------------------------------------------------------------===//
1285
John McCall0c01d182010-03-24 05:22:00 +00001286StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1287 assert(!LookupPtr && "context already has a decls map");
1288 assert(getPrimaryContext() == this &&
1289 "creating decls map on non-primary context");
1290
1291 StoredDeclsMap *M;
1292 bool Dependent = isDependentContext();
1293 if (Dependent)
1294 M = new DependentStoredDeclsMap();
1295 else
1296 M = new StoredDeclsMap();
1297 M->Previous = C.LastSDM;
1298 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1299 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001300 return M;
1301}
1302
1303void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001304 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1305 // pointer because the subclass doesn't add anything that needs to
1306 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001307 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1308}
1309
1310void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1311 while (Map) {
1312 // Advance the iteration before we invalidate memory.
1313 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1314
1315 if (Dependent)
1316 delete static_cast<DependentStoredDeclsMap*>(Map);
1317 else
1318 delete Map;
1319
1320 Map = Next.getPointer();
1321 Dependent = Next.getInt();
1322 }
1323}
1324
John McCall0c01d182010-03-24 05:22:00 +00001325DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1326 DeclContext *Parent,
1327 const PartialDiagnostic &PDiag) {
1328 assert(Parent->isDependentContext()
1329 && "cannot iterate dependent diagnostics of non-dependent context");
1330 Parent = Parent->getPrimaryContext();
1331 if (!Parent->LookupPtr)
1332 Parent->CreateStoredDeclsMap(C);
1333
1334 DependentStoredDeclsMap *Map
1335 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1336
Douglas Gregorb8365182010-03-29 23:56:53 +00001337 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001338 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001339 PartialDiagnostic::Storage *DiagStorage = 0;
1340 if (PDiag.hasStorage())
1341 DiagStorage = new (C) PartialDiagnostic::Storage;
1342
1343 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001344
1345 // TODO: Maybe we shouldn't reverse the order during insertion.
1346 DD->NextDiagnostic = Map->FirstDiagnostic;
1347 Map->FirstDiagnostic = DD;
1348
1349 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001350}