blob: b7941506b0bd0d42a8283790d164ad698e7bb69b [file] [log] [blame]
Eli Friedman7dbab8a2008-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 Gregor8bd3c2e2009-02-02 23:39:07 +000015#include "clang/AST/Decl.h"
Douglas Gregorb1fe2c92009-04-07 17:20:56 +000016#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidis2951e142008-06-09 21:05:31 +000017#include "clang/AST/DeclCXX.h"
John McCallbbbbe4e2010-03-11 07:50:04 +000018#include "clang/AST/DeclFriend.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000019#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.h"
John McCallc62bb642010-03-24 05:22:00 +000021#include "clang/AST/DependentDiagnostic.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000022#include "clang/AST/ExternalASTSource.h"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000023#include "clang/AST/ASTContext.h"
Douglas Gregor91f84212008-12-11 16:49:14 +000024#include "clang/AST/Type.h"
Sebastian Redla7b98a72009-04-26 20:35:05 +000025#include "clang/AST/Stmt.h"
26#include "clang/AST/StmtCXX.h"
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +000027#include "clang/AST/ASTMutationListener.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000028#include "clang/Basic/TargetInfo.h"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000029#include "llvm/ADT/DenseMap.h"
Chris Lattnereae6cb62009-03-05 08:00:35 +000030#include "llvm/Support/raw_ostream.h"
Douglas Gregor8b9ccca2008-12-23 21:05:05 +000031#include <algorithm>
Eli Friedman7dbab8a2008-06-07 16:52:53 +000032using namespace clang;
33
34//===----------------------------------------------------------------------===//
35// Statistics
36//===----------------------------------------------------------------------===//
37
Alexis Hunted053252010-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 Friedman7dbab8a2008-06-07 16:52:53 +000041
42static bool StatSwitch = false;
43
Eli Friedman7dbab8a2008-06-07 16:52:53 +000044const char *Decl::getDeclKindName() const {
45 switch (DeclKind) {
David Blaikie83d382b2011-09-23 05:06:16 +000046 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Alexis Hunted053252010-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 Friedman7dbab8a2008-06-07 16:52:53 +000050 }
51}
52
Douglas Gregor90d47172010-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 Naroff5faaef72009-01-20 19:53:53 +000063const char *DeclContext::getDeclKindName() const {
64 switch (DeclKind) {
David Blaikie83d382b2011-09-23 05:06:16 +000065 default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
Alexis Hunted053252010-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 Naroff5faaef72009-01-20 19:53:53 +000069 }
70}
71
Eli Friedman7dbab8a2008-06-07 16:52:53 +000072bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam130f7f92009-11-29 14:54:35 +000073 if (Enable) StatSwitch = true;
Eli Friedman7dbab8a2008-06-07 16:52:53 +000074 return StatSwitch;
75}
76
77void Decl::PrintStats() {
Chandler Carruthbfb154a2011-07-04 06:13:27 +000078 llvm::errs() << "\n*** Decl Stats:\n";
Mike Stump11289f42009-09-09 15:08:12 +000079
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000080 int totalDecls = 0;
Alexis Hunted053252010-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 Carruthbfb154a2011-07-04 06:13:27 +000084 llvm::errs() << " " << totalDecls << " decls total.\n";
Mike Stump11289f42009-09-09 15:08:12 +000085
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000086 int totalBytes = 0;
Alexis Hunted053252010-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 Carruthbfb154a2011-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 Gregor8bd3c2e2009-02-02 23:39:07 +000094 }
Alexis Hunted053252010-05-30 07:21:58 +000095#define ABSTRACT_DECL(DECL)
96#include "clang/AST/DeclNodes.inc"
Mike Stump11289f42009-09-09 15:08:12 +000097
Chandler Carruthbfb154a2011-07-04 06:13:27 +000098 llvm::errs() << "Total bytes = " << totalBytes << "\n";
Eli Friedman7dbab8a2008-06-07 16:52:53 +000099}
100
Alexis Hunted053252010-05-30 07:21:58 +0000101void Decl::add(Kind k) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000102 switch (k) {
David Blaikie83d382b2011-09-23 05:06:16 +0000103 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Alexis Hunted053252010-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 Friedman7dbab8a2008-06-07 16:52:53 +0000107 }
108}
109
Anders Carlssonaa73b912009-06-13 00:08:58 +0000110bool Decl::isTemplateParameterPack() const {
111 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
112 return TTP->isParameterPack();
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000113 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregorf5500772011-01-05 15:48:55 +0000114 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000115 return NTTP->isParameterPack();
Douglas Gregorf5500772011-01-05 15:48:55 +0000116 if (const TemplateTemplateParmDecl *TTP
117 = dyn_cast<TemplateTemplateParmDecl>(this))
118 return TTP->isParameterPack();
Anders Carlssonaa73b912009-06-13 00:08:58 +0000119 return false;
120}
121
Douglas Gregor3c6bd2a2011-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 Gregorad3f2fc2009-06-25 22:08:12 +0000129bool Decl::isFunctionOrFunctionTemplate() const {
John McCall3f746822009-11-17 05:59:44 +0000130 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlssonf057cb22009-06-26 05:26:50 +0000131 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump11289f42009-09-09 15:08:12 +0000132
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000133 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
134}
135
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000136bool Decl::isTemplateDecl() const {
137 return isa<TemplateDecl>(this);
138}
139
Argyrios Kyrtzidis0ce4c9a2011-09-28 02:45:33 +0000140const DeclContext *Decl::getParentFunctionOrMethod() const {
141 for (const DeclContext *DC = getDeclContext();
142 DC && !DC->isTranslationUnit() && !DC->isNamespace();
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000143 DC = DC->getParent())
144 if (DC->isFunctionOrMethod())
Argyrios Kyrtzidis0ce4c9a2011-09-28 02:45:33 +0000145 return DC;
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000146
Argyrios Kyrtzidis0ce4c9a2011-09-28 02:45:33 +0000147 return 0;
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000148}
149
Douglas Gregor133eddd2011-02-17 08:47:29 +0000150
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000151//===----------------------------------------------------------------------===//
Chris Lattnereae6cb62009-03-05 08:00:35 +0000152// PrettyStackTraceDecl Implementation
153//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000154
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000155void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Chris Lattnereae6cb62009-03-05 08:00:35 +0000156 SourceLocation TheLoc = Loc;
157 if (TheLoc.isInvalid() && TheDecl)
158 TheLoc = TheDecl->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000159
Chris Lattnereae6cb62009-03-05 08:00:35 +0000160 if (TheLoc.isValid()) {
161 TheLoc.print(OS, SM);
162 OS << ": ";
163 }
164
165 OS << Message;
166
Daniel Dunbar4f1054e2009-11-21 09:05:59 +0000167 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattnereae6cb62009-03-05 08:00:35 +0000168 OS << " '" << DN->getQualifiedNameAsString() << '\'';
169 OS << '\n';
170}
Mike Stump11289f42009-09-09 15:08:12 +0000171
Chris Lattnereae6cb62009-03-05 08:00:35 +0000172//===----------------------------------------------------------------------===//
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000173// Decl Implementation
174//===----------------------------------------------------------------------===//
175
Douglas Gregorb11aad82011-02-19 18:51:44 +0000176// Out-of-line virtual method providing a home for Decl.
177Decl::~Decl() { }
Douglas Gregora43942a2011-02-17 07:02:32 +0000178
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000179void Decl::setDeclContext(DeclContext *DC) {
Chris Lattnerb81eb052009-03-29 06:06:59 +0000180 DeclCtx = DC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000181}
182
183void Decl::setLexicalDeclContext(DeclContext *DC) {
184 if (DC == getLexicalDeclContext())
185 return;
186
187 if (isInSemaDC()) {
Ted Kremenekf8c12a32009-12-01 00:07:10 +0000188 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000189 MDC->SemanticDC = getDeclContext();
190 MDC->LexicalDC = DC;
Chris Lattnerb81eb052009-03-29 06:06:59 +0000191 DeclCtx = MDC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000192 } else {
193 getMultipleDC()->LexicalDC = DC;
194 }
195}
196
John McCall4fa53422009-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 Kyrtzidis743e7db2009-06-29 17:38:40 +0000208TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis4e1a72b2009-06-30 02:34:53 +0000209 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
210 return TUD;
211
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000212 DeclContext *DC = getDeclContext();
213 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump11289f42009-09-09 15:08:12 +0000214
Argyrios Kyrtzidis743e7db2009-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 Stump11289f42009-09-09 15:08:12 +0000219
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000220 return cast<TranslationUnitDecl>(DC);
221}
222
223ASTContext &Decl::getASTContext() const {
Mike Stump11289f42009-09-09 15:08:12 +0000224 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000225}
226
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000227ASTMutationListener *Decl::getASTMutationListener() const {
228 return getASTContext().getASTMutationListener();
229}
230
Douglas Gregorebada0772010-06-17 23:14:26 +0000231bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000232 if (Used)
233 return true;
234
235 // Check for used attribute.
Douglas Gregorebada0772010-06-17 23:14:26 +0000236 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner8aefcbe2010-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 Gregorebada0772010-06-17 23:14:26 +0000241 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000242 return true;
243 }
244
245 return false;
246}
247
Argyrios Kyrtzidis16180232011-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 Gregor20b2ebd2011-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 Gregore8bbc122011-09-02 00:18:52 +0000272 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000273 StringRef PrettyPlatformName
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000274 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
275 if (PrettyPlatformName.empty())
276 PrettyPlatformName = TargetPlatform;
277
Douglas Gregore8bbc122011-09-02 00:18:52 +0000278 VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
Douglas Gregor20b2ebd2011-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 Jahanian88d510d2011-12-10 00:28:41 +0000285
286 std::string HintMessage;
287 if (!A->getMessage().empty()) {
288 HintMessage = " - ";
289 HintMessage += A->getMessage();
290 }
291
Douglas Gregor7ab142b2011-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 Jahanian88d510d2011-12-10 00:28:41 +0000297 Out << "not available on " << PrettyPlatformName
298 << HintMessage;
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000299 }
300
301 return AR_Unavailable;
302 }
303
Douglas Gregor20b2ebd2011-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 Jahanian88d510d2011-12-10 00:28:41 +0000311 << A->getIntroduced() << HintMessage;
Douglas Gregor20b2ebd2011-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 Jahanian88d510d2011-12-10 00:28:41 +0000323 << A->getObsoleted() << HintMessage;
Douglas Gregor20b2ebd2011-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 Jahanian88d510d2011-12-10 00:28:41 +0000335 << A->getDeprecated() << HintMessage;
Douglas Gregor20b2ebd2011-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 Gregor20b2ebd2011-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 Lattner8aefcbe2010-02-17 02:17:21 +0000426
Chris Lattner8e097192009-03-27 20:18:19 +0000427unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
428 switch (DeclKind) {
John McCall3f746822009-11-17 05:59:44 +0000429 case Function:
430 case CXXMethod:
431 case CXXConstructor:
432 case CXXDestructor:
433 case CXXConversion:
Chris Lattner8e097192009-03-27 20:18:19 +0000434 case EnumConstant:
435 case Var:
436 case ImplicitParam:
437 case ParmVar:
Chris Lattner8e097192009-03-27 20:18:19 +0000438 case NonTypeTemplateParm:
439 case ObjCMethod:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000440 case ObjCProperty:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000441 return IDNS_Ordinary;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000442 case Label:
443 return IDNS_Label;
Francois Pichet783dd6e2010-11-21 06:08:52 +0000444 case IndirectField:
445 return IDNS_Ordinary | IDNS_Member;
446
John McCalle87beb22010-04-23 18:46:30 +0000447 case ObjCCompatibleAlias:
448 case ObjCInterface:
449 return IDNS_Ordinary | IDNS_Type;
450
451 case Typedef:
Richard Smithdda56e42011-04-15 14:24:37 +0000452 case TypeAlias:
Richard Smith3f1b5d02011-05-05 21:57:07 +0000453 case TypeAliasTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000454 case UnresolvedUsingTypename:
455 case TemplateTypeParm:
456 return IDNS_Ordinary | IDNS_Type;
457
John McCall3f746822009-11-17 05:59:44 +0000458 case UsingShadow:
459 return 0; // we'll actually overwrite this later
460
John McCalle61f2ba2009-11-18 02:36:19 +0000461 case UnresolvedUsingValue:
John McCalle61f2ba2009-11-18 02:36:19 +0000462 return IDNS_Ordinary | IDNS_Using;
John McCall3f746822009-11-17 05:59:44 +0000463
464 case Using:
465 return IDNS_Using;
466
Chris Lattner8e097192009-03-27 20:18:19 +0000467 case ObjCProtocol:
Douglas Gregor79947a22009-04-24 00:11:27 +0000468 return IDNS_ObjCProtocol;
Mike Stump11289f42009-09-09 15:08:12 +0000469
Chris Lattner8e097192009-03-27 20:18:19 +0000470 case Field:
471 case ObjCAtDefsField:
472 case ObjCIvar:
473 return IDNS_Member;
Mike Stump11289f42009-09-09 15:08:12 +0000474
Chris Lattner8e097192009-03-27 20:18:19 +0000475 case Record:
476 case CXXRecord:
477 case Enum:
John McCalle87beb22010-04-23 18:46:30 +0000478 return IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000479
Chris Lattner8e097192009-03-27 20:18:19 +0000480 case Namespace:
John McCalle87beb22010-04-23 18:46:30 +0000481 case NamespaceAlias:
482 return IDNS_Namespace;
483
Chris Lattner8e097192009-03-27 20:18:19 +0000484 case FunctionTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000485 return IDNS_Ordinary;
486
Chris Lattner8e097192009-03-27 20:18:19 +0000487 case ClassTemplate:
488 case TemplateTemplateParm:
John McCalle87beb22010-04-23 18:46:30 +0000489 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000490
Chris Lattner8e097192009-03-27 20:18:19 +0000491 // Never have names.
John McCallaa74a0c2009-08-28 07:59:38 +0000492 case Friend:
John McCall11083da2009-09-16 22:47:08 +0000493 case FriendTemplate:
Abramo Bagnarad7340582010-06-05 05:09:32 +0000494 case AccessSpec:
Chris Lattner8e097192009-03-27 20:18:19 +0000495 case LinkageSpec:
496 case FileScopeAsm:
497 case StaticAssert:
498 case ObjCClass:
Chris Lattner8e097192009-03-27 20:18:19 +0000499 case ObjCPropertyImpl:
500 case ObjCForwardProtocol:
501 case Block:
502 case TranslationUnit:
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000503
Chris Lattner8e097192009-03-27 20:18:19 +0000504 case UsingDirective:
505 case ClassTemplateSpecialization:
Douglas Gregor2373c592009-05-31 09:31:02 +0000506 case ClassTemplatePartialSpecialization:
Francois Pichet00c7e6c2011-08-14 03:52:19 +0000507 case ClassScopeFunctionSpecialization:
Douglas Gregore93525e2010-04-22 23:19:50 +0000508 case ObjCImplementation:
509 case ObjCCategory:
510 case ObjCCategoryImpl:
Douglas Gregorba345522011-12-02 23:23:56 +0000511 case Import:
Douglas Gregore93525e2010-04-22 23:19:50 +0000512 // Never looked up by name.
Chris Lattner8e097192009-03-27 20:18:19 +0000513 return 0;
514 }
John McCall3f746822009-11-17 05:59:44 +0000515
516 return 0;
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000517}
518
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000519void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000520 assert(!HasAttrs && "Decl already contains attrs.");
521
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000522 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
523 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000524
525 AttrBlank = attrs;
526 HasAttrs = true;
527}
528
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000529void Decl::dropAttrs() {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000530 if (!HasAttrs) return;
Mike Stump11289f42009-09-09 15:08:12 +0000531
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000532 HasAttrs = false;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000533 getASTContext().eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000534}
535
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000536const AttrVec &Decl::getAttrs() const {
537 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000538 return getASTContext().getDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000539}
540
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000541void Decl::swapAttrs(Decl *RHS) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000542 bool HasLHSAttr = this->HasAttrs;
543 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump11289f42009-09-09 15:08:12 +0000544
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000545 // Usually, neither decl has attrs, nothing to do.
546 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump11289f42009-09-09 15:08:12 +0000547
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000548 // If 'this' has no attrs, swap the other way.
549 if (!HasLHSAttr)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000550 return RHS->swapAttrs(this);
Mike Stump11289f42009-09-09 15:08:12 +0000551
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000552 ASTContext &Context = getASTContext();
Mike Stump11289f42009-09-09 15:08:12 +0000553
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000554 // Handle the case when both decls have attrs.
555 if (HasRHSAttr) {
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000556 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000557 return;
558 }
Mike Stump11289f42009-09-09 15:08:12 +0000559
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000560 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000561 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
562 Context.eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000563 this->HasAttrs = false;
564 RHS->HasAttrs = true;
565}
566
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000567Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000568 Decl::Kind DK = D->getDeclKind();
569 switch(DK) {
Alexis Hunted053252010-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 Kyrtzidisafe24c82009-02-16 14:29:28 +0000576 default:
Alexis Hunted053252010-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 Blaikie83d382b2011-09-23 05:06:16 +0000582 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000583 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000584}
585
586DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000587 Decl::Kind DK = D->getKind();
588 switch(DK) {
Alexis Hunted053252010-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 Kyrtzidisafe24c82009-02-16 14:29:28 +0000595 default:
Alexis Hunted053252010-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 Blaikie83d382b2011-09-23 05:06:16 +0000601 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000602 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000603}
604
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000605SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis36ea3222010-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 Kyrtzidis6fbc8fa2010-07-07 11:31:27 +0000615 if (Stmt *Body = getBody())
616 return Body->getSourceRange().getEnd();
617
618 return SourceLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +0000619}
620
Anders Carlssona28908d2009-03-25 23:38:06 +0000621void Decl::CheckAccessDeclContext() const {
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000622#ifndef NDEBUG
John McCall401982f2010-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 Kyrtzidise1778632010-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 Carlssonadf36b22009-08-29 20:47:47 +0000630 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidisa45855f2010-07-02 11:55:44 +0000631 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000632 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregor2b76dd92010-02-22 17:53:38 +0000633 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis260b4a82010-09-08 21:32:35 +0000634 isInvalidDecl() ||
635 isa<StaticAssertDecl>(this) ||
636 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
637 // as DeclContext (?).
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000638 isa<ParmVarDecl>(this) ||
639 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
640 // AS_none as access specifier.
Francois Pichet09af8c32011-08-17 01:06:54 +0000641 isa<CXXRecordDecl>(this) ||
642 isa<ClassScopeFunctionSpecializationDecl>(this))
Anders Carlssonadf36b22009-08-29 20:47:47 +0000643 return;
Mike Stump11289f42009-09-09 15:08:12 +0000644
645 assert(Access != AS_none &&
Anders Carlssona28908d2009-03-25 23:38:06 +0000646 "Access specifier is AS_none inside a record decl");
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000647#endif
Anders Carlssona28908d2009-03-25 23:38:06 +0000648}
649
John McCallb67608f2011-02-22 22:25:23 +0000650DeclContext *Decl::getNonClosureContext() {
John McCallfe96e0b2011-11-06 09:01:30 +0000651 return getDeclContext()->getNonClosureAncestor();
652}
653
654DeclContext *DeclContext::getNonClosureAncestor() {
655 DeclContext *DC = this;
John McCallb67608f2011-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 McCall63b45fe2011-06-23 21:18:31 +0000660 while (isa<BlockDecl>(DC))
661 DC = cast<BlockDecl>(DC)->getDeclContext();
John McCallb67608f2011-02-22 22:25:23 +0000662
663 assert(!DC->isClosure());
664 return DC;
665}
Anders Carlssona28908d2009-03-25 23:38:06 +0000666
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000667//===----------------------------------------------------------------------===//
668// DeclContext Implementation
669//===----------------------------------------------------------------------===//
670
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000671bool DeclContext::classof(const Decl *D) {
672 switch (D->getKind()) {
Alexis Hunted053252010-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 Kyrtzidisafe24c82009-02-16 14:29:28 +0000677 return true;
678 default:
Alexis Hunted053252010-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 Kyrtzidisafe24c82009-02-16 14:29:28 +0000683 return true;
Alexis Hunted053252010-05-30 07:21:58 +0000684#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000685 return false;
686 }
687}
688
Douglas Gregor9c832f72010-07-25 18:38:02 +0000689DeclContext::~DeclContext() { }
Douglas Gregor91f84212008-12-11 16:49:14 +0000690
Douglas Gregor7f737c02009-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 Redl50c68252010-08-31 00:36:30 +0000700 if (getParent()->getRedeclContext()->isFileContext() &&
701 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregor7f737c02009-09-10 16:57:35 +0000702 return getLexicalParent();
703
704 return getParent();
705}
706
Sebastian Redlbd595762010-08-31 20:53:31 +0000707bool DeclContext::isInlineNamespace() const {
708 return isNamespace() &&
709 cast<NamespaceDecl>(this)->isInline();
710}
711
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000712bool DeclContext::isDependentContext() const {
713 if (isFileContext())
714 return false;
715
Douglas Gregor2373c592009-05-31 09:31:02 +0000716 if (isa<ClassTemplatePartialSpecializationDecl>(this))
717 return true;
718
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000719 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
720 if (Record->getDescribedClassTemplate())
721 return true;
722
John McCallc62bb642010-03-24 05:22:00 +0000723 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000724 if (Function->getDescribedFunctionTemplate())
725 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000726
John McCallc62bb642010-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 Gregor9e927ab2009-05-28 16:34:51 +0000733 return getParent() && getParent()->isDependentContext();
734}
735
Douglas Gregor07665a62009-01-05 19:45:36 +0000736bool DeclContext::isTransparentContext() const {
737 if (DeclKind == Decl::Enum)
Douglas Gregor0bf31402010-10-08 23:50:27 +0000738 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor07665a62009-01-05 19:45:36 +0000739 else if (DeclKind == Decl::LinkageSpec)
740 return true;
Douglas Gregor07665a62009-01-05 19:45:36 +0000741
742 return false;
743}
744
John McCall5fe84122010-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 Redl50c68252010-08-31 00:36:30 +0000756bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregore985a3b2009-08-27 06:03:53 +0000757 if (getPrimaryContext() != this)
758 return getPrimaryContext()->Encloses(DC);
Mike Stump11289f42009-09-09 15:08:12 +0000759
Douglas Gregore985a3b2009-08-27 06:03:53 +0000760 for (; DC; DC = DC->getParent())
761 if (DC->getPrimaryContext() == this)
762 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000763 return false;
Douglas Gregore985a3b2009-08-27 06:03:53 +0000764}
765
Steve Naroff35c62ae2009-01-08 17:28:14 +0000766DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor91f84212008-12-11 16:49:14 +0000767 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000768 case Decl::TranslationUnit:
Douglas Gregor07665a62009-01-05 19:45:36 +0000769 case Decl::LinkageSpec:
Mike Stump11289f42009-09-09 15:08:12 +0000770 case Decl::Block:
Douglas Gregor91f84212008-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 Gregor91f84212008-12-11 16:49:14 +0000778 case Decl::ObjCMethod:
779 return this;
780
781 case Decl::ObjCInterface:
Steve Naroff35c62ae2009-01-08 17:28:14 +0000782 case Decl::ObjCProtocol:
783 case Decl::ObjCCategory:
Douglas Gregor91f84212008-12-11 16:49:14 +0000784 // FIXME: Can Objective-C interfaces be forward-declared?
785 return this;
786
Steve Naroff35c62ae2009-01-08 17:28:14 +0000787 case Decl::ObjCImplementation:
788 case Decl::ObjCCategoryImpl:
789 return this;
790
Douglas Gregor91f84212008-12-11 16:49:14 +0000791 default:
Alexis Hunted053252010-05-30 07:21:58 +0000792 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000793 // If this is a tag type that has a definition or is currently
794 // being defined, that definition is our primary context.
John McCalle78aac42010-03-10 03:28:59 +0000795 TagDecl *Tag = cast<TagDecl>(this);
796 assert(isa<TagType>(Tag->TypeForDecl) ||
797 isa<InjectedClassNameType>(Tag->TypeForDecl));
798
799 if (TagDecl *Def = Tag->getDefinition())
800 return Def;
801
802 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
803 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
804 if (TagTy->isBeingDefined())
805 // FIXME: is it necessarily being defined in the decl
806 // that owns the type?
807 return TagTy->getDecl();
808 }
809
810 return Tag;
Douglas Gregor67a65642009-02-17 23:15:12 +0000811 }
812
Alexis Hunted053252010-05-30 07:21:58 +0000813 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor91f84212008-12-11 16:49:14 +0000814 "Unknown DeclContext kind");
815 return this;
816 }
817}
818
819DeclContext *DeclContext::getNextContext() {
820 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000821 case Decl::Namespace:
822 // Return the next namespace
823 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
824
825 default:
Douglas Gregor91f84212008-12-11 16:49:14 +0000826 return 0;
827 }
828}
829
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000830std::pair<Decl *, Decl *>
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000831DeclContext::BuildDeclChain(const SmallVectorImpl<Decl*> &Decls,
832 bool FieldsAlreadyLoaded) {
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000833 // Build up a chain of declarations via the Decl::NextDeclInContext field.
834 Decl *FirstNewDecl = 0;
835 Decl *PrevDecl = 0;
836 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000837 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
838 continue;
839
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000840 Decl *D = Decls[I];
841 if (PrevDecl)
842 PrevDecl->NextDeclInContext = D;
843 else
844 FirstNewDecl = D;
845
846 PrevDecl = D;
847 }
848
849 return std::make_pair(FirstNewDecl, PrevDecl);
850}
851
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000852/// \brief Load the declarations within this lexical storage from an
853/// external source.
Mike Stump11289f42009-09-09 15:08:12 +0000854void
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000855DeclContext::LoadLexicalDeclsFromExternalStorage() const {
856 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000857 assert(hasExternalLexicalStorage() && Source && "No external storage?");
858
Argyrios Kyrtzidis98d045e2010-07-30 10:03:23 +0000859 // Notify that we have a DeclContext that is initializing.
860 ExternalASTSource::Deserializing ADeclContext(Source);
Douglas Gregorf337ae92011-08-26 21:23:06 +0000861
Douglas Gregor3d0adb32011-07-15 21:46:17 +0000862 // Load the external declarations, if any.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000863 SmallVector<Decl*, 64> Decls;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000864 ExternalLexicalStorage = false;
Douglas Gregor3d0adb32011-07-15 21:46:17 +0000865 switch (Source->FindExternalLexicalDecls(this, Decls)) {
866 case ELR_Success:
867 break;
868
869 case ELR_Failure:
870 case ELR_AlreadyLoaded:
871 return;
872 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000873
874 if (Decls.empty())
875 return;
876
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000877 // We may have already loaded just the fields of this record, in which case
878 // we need to ignore them.
879 bool FieldsAlreadyLoaded = false;
880 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
881 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
882
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000883 // Splice the newly-read declarations into the beginning of the list
884 // of declarations.
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000885 Decl *ExternalFirst, *ExternalLast;
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000886 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls,
887 FieldsAlreadyLoaded);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000888 ExternalLast->NextDeclInContext = FirstDecl;
889 FirstDecl = ExternalFirst;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000890 if (!LastDecl)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000891 LastDecl = ExternalLast;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000892}
893
John McCall75b960e2010-06-01 09:23:16 +0000894DeclContext::lookup_result
895ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
896 DeclarationName Name) {
897 ASTContext &Context = DC->getParentASTContext();
898 StoredDeclsMap *Map;
899 if (!(Map = DC->LookupPtr))
900 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000901
John McCall75b960e2010-06-01 09:23:16 +0000902 StoredDeclsList &List = (*Map)[Name];
903 assert(List.isNull());
904 (void) List;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000905
John McCall75b960e2010-06-01 09:23:16 +0000906 return DeclContext::lookup_result();
907}
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000908
John McCall75b960e2010-06-01 09:23:16 +0000909DeclContext::lookup_result
910ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +0000911 DeclarationName Name,
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000912 ArrayRef<NamedDecl*> Decls) {
John McCall75b960e2010-06-01 09:23:16 +0000913 ASTContext &Context = DC->getParentASTContext();;
914
915 StoredDeclsMap *Map;
916 if (!(Map = DC->LookupPtr))
917 Map = DC->CreateStoredDeclsMap(Context);
918
919 StoredDeclsList &List = (*Map)[Name];
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000920 for (ArrayRef<NamedDecl*>::iterator
921 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
John McCall75b960e2010-06-01 09:23:16 +0000922 if (List.isNull())
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000923 List.setOnlyValue(*I);
John McCall75b960e2010-06-01 09:23:16 +0000924 else
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000925 List.AddSubsequentDecl(*I);
John McCall75b960e2010-06-01 09:23:16 +0000926 }
927
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000928 return List.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +0000929}
930
Sebastian Redl66c5eef2010-07-27 00:17:23 +0000931DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
932 return decl_iterator(FirstDecl);
933}
934
935DeclContext::decl_iterator DeclContext::noload_decls_end() const {
936 return decl_iterator();
937}
938
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000939DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000940 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000941 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000942
Mike Stump11289f42009-09-09 15:08:12 +0000943 return decl_iterator(FirstDecl);
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000944}
945
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000946DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000947 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000948 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000949
Mike Stump11289f42009-09-09 15:08:12 +0000950 return decl_iterator();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000951}
952
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000953bool DeclContext::decls_empty() const {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000954 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000955 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000956
957 return !FirstDecl;
958}
959
John McCall84d87672009-12-10 09:41:52 +0000960void DeclContext::removeDecl(Decl *D) {
961 assert(D->getLexicalDeclContext() == this &&
962 "decl being removed from non-lexical context");
963 assert((D->NextDeclInContext || D == LastDecl) &&
964 "decl is not in decls list");
965
966 // Remove D from the decl chain. This is O(n) but hopefully rare.
967 if (D == FirstDecl) {
968 if (D == LastDecl)
969 FirstDecl = LastDecl = 0;
970 else
971 FirstDecl = D->NextDeclInContext;
972 } else {
973 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
974 assert(I && "decl not found in linked list");
975 if (I->NextDeclInContext == D) {
976 I->NextDeclInContext = D->NextDeclInContext;
977 if (D == LastDecl) LastDecl = I;
978 break;
979 }
980 }
981 }
982
983 // Mark that D is no longer in the decl chain.
984 D->NextDeclInContext = 0;
985
986 // Remove D from the lookup table if necessary.
987 if (isa<NamedDecl>(D)) {
988 NamedDecl *ND = cast<NamedDecl>(D);
989
Axel Naumanncb2c52f2011-08-26 14:06:12 +0000990 // Remove only decls that have a name
991 if (!ND->getDeclName()) return;
992
John McCallc62bb642010-03-24 05:22:00 +0000993 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
994 if (!Map) return;
John McCall84d87672009-12-10 09:41:52 +0000995
John McCall84d87672009-12-10 09:41:52 +0000996 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
997 assert(Pos != Map->end() && "no lookup entry for decl");
Axel Naumannfbc7b982011-11-08 18:21:06 +0000998 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
999 Pos->second.remove(ND);
John McCall84d87672009-12-10 09:41:52 +00001000 }
1001}
1002
John McCalld1e9d832009-08-11 06:59:38 +00001003void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner33f219d2009-02-20 00:56:18 +00001004 assert(D->getLexicalDeclContext() == this &&
1005 "Decl inserted into wrong lexical context");
Mike Stump11289f42009-09-09 15:08:12 +00001006 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor020713e2009-01-09 19:42:16 +00001007 "Decl already inserted into a DeclContext");
1008
1009 if (FirstDecl) {
Chris Lattnerfcd33a62009-03-28 06:04:26 +00001010 LastDecl->NextDeclInContext = D;
Douglas Gregor020713e2009-01-09 19:42:16 +00001011 LastDecl = D;
1012 } else {
1013 FirstDecl = LastDecl = D;
1014 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +00001015
1016 // Notify a C++ record declaration that we've added a member, so it can
1017 // update it's class-specific state.
1018 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1019 Record->addedMember(D);
Douglas Gregor0f2a3602011-12-03 00:30:27 +00001020
1021 // If this is a newly-created (not de-serialized) import declaration, wire
1022 // it in to the list of local import declarations.
1023 if (!D->isFromASTFile()) {
1024 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1025 D->getASTContext().addedLocalImportDecl(Import);
1026 }
John McCalld1e9d832009-08-11 06:59:38 +00001027}
1028
1029void DeclContext::addDecl(Decl *D) {
1030 addHiddenDecl(D);
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001031
1032 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001033 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor91f84212008-12-11 16:49:14 +00001034}
1035
Sean Callanan95e74be2011-10-21 02:57:43 +00001036void DeclContext::addDeclInternal(Decl *D) {
1037 addHiddenDecl(D);
1038
1039 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1040 ND->getDeclContext()->makeDeclVisibleInContextInternal(ND);
1041}
1042
Douglas Gregor07665a62009-01-05 19:45:36 +00001043/// buildLookup - Build the lookup data structure with all of the
1044/// declarations in DCtx (and any other contexts linked to it or
1045/// transparent contexts nested within it).
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001046void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001047 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump11289f42009-09-09 15:08:12 +00001048 for (decl_iterator D = DCtx->decls_begin(),
1049 DEnd = DCtx->decls_end();
Douglas Gregord05cb412009-01-06 07:17:58 +00001050 D != DEnd; ++D) {
John McCalld1e9d832009-08-11 06:59:38 +00001051 // Insert this declaration into the lookup structure, but only
1052 // if it's semantically in its decl context. During non-lazy
1053 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001054 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCalld1e9d832009-08-11 06:59:38 +00001055 if (D->getDeclContext() == DCtx)
Sean Callanan95e74be2011-10-21 02:57:43 +00001056 makeDeclVisibleInContextImpl(ND, false);
Douglas Gregor07665a62009-01-05 19:45:36 +00001057
Fariborz Jahanian3a039e32011-08-27 20:50:59 +00001058 // Insert any forward-declared Objective-C interface into the lookup
Ted Kremenek707ece62009-11-17 22:58:30 +00001059 // data structure.
1060 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
Sean Callanan95e74be2011-10-21 02:57:43 +00001061 makeDeclVisibleInContextImpl(Class->getForwardInterfaceDecl(), false);
Ted Kremenek707ece62009-11-17 22:58:30 +00001062
Sebastian Redlbd595762010-08-31 20:53:31 +00001063 // If this declaration is itself a transparent declaration context or
1064 // inline namespace, add its members (recursively).
Douglas Gregor07665a62009-01-05 19:45:36 +00001065 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redlbd595762010-08-31 20:53:31 +00001066 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001067 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor07665a62009-01-05 19:45:36 +00001068 }
1069 }
1070}
1071
Mike Stump11289f42009-09-09 15:08:12 +00001072DeclContext::lookup_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001073DeclContext::lookup(DeclarationName Name) {
Steve Naroff35c62ae2009-01-08 17:28:14 +00001074 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +00001075 if (PrimaryContext != this)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001076 return PrimaryContext->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +00001077
John McCall75b960e2010-06-01 09:23:16 +00001078 if (hasExternalVisibleStorage()) {
1079 // Check to see if we've already cached the lookup results.
1080 if (LookupPtr) {
1081 StoredDeclsMap::iterator I = LookupPtr->find(Name);
1082 if (I != LookupPtr->end())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001083 return I->second.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +00001084 }
1085
1086 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1087 return Source->FindExternalVisibleDeclsByName(this, Name);
1088 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001089
Douglas Gregor55297ac2008-12-23 00:26:44 +00001090 /// If there is no lookup data structure, build one now by walking
Douglas Gregor91f84212008-12-11 16:49:14 +00001091 /// all of the linked DeclContexts (in declaration order!) and
1092 /// inserting their values.
Douglas Gregor9615ec22009-04-09 17:29:08 +00001093 if (!LookupPtr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001094 buildLookup(this);
Douglas Gregor91f84212008-12-11 16:49:14 +00001095
Douglas Gregor9615ec22009-04-09 17:29:08 +00001096 if (!LookupPtr)
Douglas Gregor10dc8aa2010-05-11 06:18:17 +00001097 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregor9615ec22009-04-09 17:29:08 +00001098 }
Douglas Gregor91f84212008-12-11 16:49:14 +00001099
John McCallc62bb642010-03-24 05:22:00 +00001100 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1101 if (Pos == LookupPtr->end())
Douglas Gregor10dc8aa2010-05-11 06:18:17 +00001102 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001103 return Pos->second.getLookupResult();
Douglas Gregor91f84212008-12-11 16:49:14 +00001104}
1105
Mike Stump11289f42009-09-09 15:08:12 +00001106DeclContext::lookup_const_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001107DeclContext::lookup(DeclarationName Name) const {
1108 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +00001109}
1110
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001111void DeclContext::localUncachedLookup(DeclarationName Name,
1112 llvm::SmallVectorImpl<NamedDecl *> &Results) {
1113 Results.clear();
1114
1115 // If there's no external storage, just perform a normal lookup and copy
1116 // the results.
1117 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage()) {
1118 lookup_result LookupResults = lookup(Name);
1119 Results.insert(Results.end(), LookupResults.first, LookupResults.second);
1120 return;
1121 }
1122
1123 // If we have a lookup table, check there first. Maybe we'll get lucky.
1124 if (LookupPtr) {
1125 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1126 if (Pos != LookupPtr->end()) {
1127 Results.insert(Results.end(),
1128 Pos->second.getLookupResult().first,
1129 Pos->second.getLookupResult().second);
1130 return;
1131 }
1132 }
1133
1134 // Slow case: grovel through the declarations in our chain looking for
1135 // matches.
1136 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1137 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1138 if (ND->getDeclName() == Name)
1139 Results.push_back(ND);
1140 }
1141}
1142
Sebastian Redl50c68252010-08-31 00:36:30 +00001143DeclContext *DeclContext::getRedeclContext() {
Chris Lattner17a1bfa2009-03-27 19:19:59 +00001144 DeclContext *Ctx = this;
Sebastian Redlbd595762010-08-31 20:53:31 +00001145 // Skip through transparent contexts.
1146 while (Ctx->isTransparentContext())
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00001147 Ctx = Ctx->getParent();
1148 return Ctx;
1149}
1150
Douglas Gregorf47b9112009-02-25 22:02:03 +00001151DeclContext *DeclContext::getEnclosingNamespaceContext() {
1152 DeclContext *Ctx = this;
1153 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl4f08c962010-08-31 00:36:23 +00001154 while (!Ctx->isFileContext())
Douglas Gregorf47b9112009-02-25 22:02:03 +00001155 Ctx = Ctx->getParent();
1156 return Ctx->getPrimaryContext();
1157}
1158
Sebastian Redl50c68252010-08-31 00:36:30 +00001159bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1160 // For non-file contexts, this is equivalent to Equals.
1161 if (!isFileContext())
1162 return O->Equals(this);
1163
1164 do {
1165 if (O->Equals(this))
1166 return true;
1167
1168 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1169 if (!NS || !NS->isInline())
1170 break;
1171 O = NS->getParent();
1172 } while (O);
1173
1174 return false;
1175}
1176
Sean Callanan95e74be2011-10-21 02:57:43 +00001177void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable)
1178{
1179 makeDeclVisibleInContextWithFlags(D, false, Recoverable);
1180}
1181
1182void DeclContext::makeDeclVisibleInContextInternal(NamedDecl *D, bool Recoverable)
1183{
1184 makeDeclVisibleInContextWithFlags(D, true, Recoverable);
1185}
1186
1187void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal, bool Recoverable) {
Douglas Gregor67a65642009-02-17 23:15:12 +00001188 // FIXME: This feels like a hack. Should DeclarationName support
1189 // template-ids, or is there a better way to keep specializations
1190 // from being visible?
Douglas Gregorfd7c2252011-03-04 17:52:15 +00001191 if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
Douglas Gregor67a65642009-02-17 23:15:12 +00001192 return;
Eli Friedman73168192009-12-08 05:40:03 +00001193 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1194 if (FD->isFunctionTemplateSpecialization())
1195 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001196
Steve Naroff35c62ae2009-01-08 17:28:14 +00001197 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +00001198 if (PrimaryContext != this) {
Sean Callanan95e74be2011-10-21 02:57:43 +00001199 PrimaryContext->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
Douglas Gregor91f84212008-12-11 16:49:14 +00001200 return;
1201 }
1202
1203 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001204 // into it. If we haven't deserialized externally stored decls, deserialize
1205 // them so we can add the decl. Otherwise, be lazy and don't build that
1206 // structure until someone asks for it.
1207 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Sean Callanan95e74be2011-10-21 02:57:43 +00001208 makeDeclVisibleInContextImpl(D, Internal);
Douglas Gregor07665a62009-01-05 19:45:36 +00001209
Sebastian Redlbd595762010-08-31 20:53:31 +00001210 // If we are a transparent context or inline namespace, insert into our
1211 // parent context, too. This operation is recursive.
1212 if (isTransparentContext() || isInlineNamespace())
Sean Callanan95e74be2011-10-21 02:57:43 +00001213 getParent()->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00001214
1215 Decl *DCAsDecl = cast<Decl>(this);
1216 // Notify that a decl was made visible unless it's a Tag being defined.
1217 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1218 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1219 L->AddedVisibleDecl(this, D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001220}
1221
Sean Callanan95e74be2011-10-21 02:57:43 +00001222void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001223 // Skip unnamed declarations.
1224 if (!D->getDeclName())
1225 return;
1226
Douglas Gregor0de016d2011-05-06 23:32:38 +00001227 // Skip entities that can't be found by name lookup into a particular
1228 // context.
1229 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1230 D->isTemplateParameter())
Douglas Gregor67a65642009-02-17 23:15:12 +00001231 return;
1232
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001233 ASTContext *C = 0;
1234 if (!LookupPtr) {
1235 C = &getParentASTContext();
1236 CreateStoredDeclsMap(*C);
1237 }
1238
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001239 // If there is an external AST source, load any declarations it knows about
1240 // with this declaration's name.
1241 // If the lookup table contains an entry about this name it means that we
1242 // have already checked the external source.
Sean Callanan95e74be2011-10-21 02:57:43 +00001243 if (!Internal)
1244 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1245 if (hasExternalVisibleStorage() &&
1246 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1247 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001248
Douglas Gregor91f84212008-12-11 16:49:14 +00001249 // Insert this declaration into the map.
John McCallc62bb642010-03-24 05:22:00 +00001250 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattnercaae7162009-02-20 01:44:05 +00001251 if (DeclNameEntries.isNull()) {
1252 DeclNameEntries.setOnlyValue(D);
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +00001253 return;
Douglas Gregor91f84212008-12-11 16:49:14 +00001254 }
Chris Lattner24e24d52009-02-20 00:55:03 +00001255
Chris Lattner29578f32009-02-20 01:10:07 +00001256 // If it is possible that this is a redeclaration, check to see if there is
1257 // already a decl for which declarationReplaces returns true. If there is
1258 // one, just replace it and return.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001259 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattnercaae7162009-02-20 01:44:05 +00001260 return;
Mike Stump11289f42009-09-09 15:08:12 +00001261
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +00001262 // Put this declaration into the appropriate slot.
Chris Lattnercaae7162009-02-20 01:44:05 +00001263 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001264}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001265
1266/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1267/// this context.
Mike Stump11289f42009-09-09 15:08:12 +00001268DeclContext::udir_iterator_range
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001269DeclContext::getUsingDirectives() const {
1270 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor889ceb72009-02-03 19:21:40 +00001271 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1272 reinterpret_cast<udir_iterator>(Result.second));
1273}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001274
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001275//===----------------------------------------------------------------------===//
1276// Creation and Destruction of StoredDeclsMaps. //
1277//===----------------------------------------------------------------------===//
1278
John McCallc62bb642010-03-24 05:22:00 +00001279StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1280 assert(!LookupPtr && "context already has a decls map");
1281 assert(getPrimaryContext() == this &&
1282 "creating decls map on non-primary context");
1283
1284 StoredDeclsMap *M;
1285 bool Dependent = isDependentContext();
1286 if (Dependent)
1287 M = new DependentStoredDeclsMap();
1288 else
1289 M = new StoredDeclsMap();
1290 M->Previous = C.LastSDM;
1291 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1292 LookupPtr = M;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001293 return M;
1294}
1295
1296void ASTContext::ReleaseDeclContextMaps() {
John McCallc62bb642010-03-24 05:22:00 +00001297 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1298 // pointer because the subclass doesn't add anything that needs to
1299 // be deleted.
John McCallc62bb642010-03-24 05:22:00 +00001300 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1301}
1302
1303void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1304 while (Map) {
1305 // Advance the iteration before we invalidate memory.
1306 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1307
1308 if (Dependent)
1309 delete static_cast<DependentStoredDeclsMap*>(Map);
1310 else
1311 delete Map;
1312
1313 Map = Next.getPointer();
1314 Dependent = Next.getInt();
1315 }
1316}
1317
John McCallc62bb642010-03-24 05:22:00 +00001318DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1319 DeclContext *Parent,
1320 const PartialDiagnostic &PDiag) {
1321 assert(Parent->isDependentContext()
1322 && "cannot iterate dependent diagnostics of non-dependent context");
1323 Parent = Parent->getPrimaryContext();
1324 if (!Parent->LookupPtr)
1325 Parent->CreateStoredDeclsMap(C);
1326
1327 DependentStoredDeclsMap *Map
1328 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1329
Douglas Gregora55530e2010-03-29 23:56:53 +00001330 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregor89336232010-03-29 23:34:08 +00001331 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregora55530e2010-03-29 23:56:53 +00001332 PartialDiagnostic::Storage *DiagStorage = 0;
1333 if (PDiag.hasStorage())
1334 DiagStorage = new (C) PartialDiagnostic::Storage;
1335
1336 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCallc62bb642010-03-24 05:22:00 +00001337
1338 // TODO: Maybe we shouldn't reverse the order during insertion.
1339 DD->NextDiagnostic = Map->FirstDiagnostic;
1340 Map->FirstDiagnostic = DD;
1341
1342 return DD;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001343}