blob: 4c31bbd3d7620b40a2ef5323baf02b4d4fa69455 [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;
285
Douglas Gregorb53e4172011-03-26 03:35:55 +0000286 // Make sure that this declaration has not been marked 'unavailable'.
287 if (A->getUnavailable()) {
288 if (Message) {
289 Message->clear();
290 llvm::raw_string_ostream Out(*Message);
291 Out << "not available on " << PrettyPlatformName;
292 }
293
294 return AR_Unavailable;
295 }
296
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000297 // Make sure that this declaration has already been introduced.
298 if (!A->getIntroduced().empty() &&
299 TargetMinVersion < A->getIntroduced()) {
300 if (Message) {
301 Message->clear();
302 llvm::raw_string_ostream Out(*Message);
303 Out << "introduced in " << PrettyPlatformName << ' '
304 << A->getIntroduced();
305 }
306
307 return AR_NotYetIntroduced;
308 }
309
310 // Make sure that this declaration hasn't been obsoleted.
311 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
312 if (Message) {
313 Message->clear();
314 llvm::raw_string_ostream Out(*Message);
315 Out << "obsoleted in " << PrettyPlatformName << ' '
316 << A->getObsoleted();
317 }
318
319 return AR_Unavailable;
320 }
321
322 // Make sure that this declaration hasn't been deprecated.
323 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
324 if (Message) {
325 Message->clear();
326 llvm::raw_string_ostream Out(*Message);
327 Out << "first deprecated in " << PrettyPlatformName << ' '
328 << A->getDeprecated();
329 }
330
331 return AR_Deprecated;
332 }
333
334 return AR_Available;
335}
336
337AvailabilityResult Decl::getAvailability(std::string *Message) const {
338 AvailabilityResult Result = AR_Available;
339 std::string ResultMessage;
340
341 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
342 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
343 if (Result >= AR_Deprecated)
344 continue;
345
346 if (Message)
347 ResultMessage = Deprecated->getMessage();
348
349 Result = AR_Deprecated;
350 continue;
351 }
352
353 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
354 if (Message)
355 *Message = Unavailable->getMessage();
356 return AR_Unavailable;
357 }
358
359 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
360 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
361 Message);
362
363 if (AR == AR_Unavailable)
364 return AR_Unavailable;
365
366 if (AR > Result) {
367 Result = AR;
368 if (Message)
369 ResultMessage.swap(*Message);
370 }
371 continue;
372 }
373 }
374
375 if (Message)
376 Message->swap(ResultMessage);
377 return Result;
378}
379
380bool Decl::canBeWeakImported(bool &IsDefinition) const {
381 IsDefinition = false;
382 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
383 if (!Var->hasExternalStorage() || Var->getInit()) {
384 IsDefinition = true;
385 return false;
386 }
387 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
388 if (FD->hasBody()) {
389 IsDefinition = true;
390 return false;
391 }
392 } else if (isa<ObjCPropertyDecl>(this) || isa<ObjCMethodDecl>(this))
393 return false;
394 else if (!(getASTContext().getLangOptions().ObjCNonFragileABI &&
395 isa<ObjCInterfaceDecl>(this)))
396 return false;
397
398 return true;
399}
400
401bool Decl::isWeakImported() const {
402 bool IsDefinition;
403 if (!canBeWeakImported(IsDefinition))
404 return false;
405
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000406 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
407 if (isa<WeakImportAttr>(*A))
408 return true;
409
410 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
411 if (CheckAvailability(getASTContext(), Availability, 0)
412 == AR_NotYetIntroduced)
413 return true;
414 }
415 }
416
417 return false;
418}
Tanya Lattner12ead492010-02-17 02:17:21 +0000419
Chris Lattner769dbdf2009-03-27 20:18:19 +0000420unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
421 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000422 case Function:
423 case CXXMethod:
424 case CXXConstructor:
425 case CXXDestructor:
426 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000427 case EnumConstant:
428 case Var:
429 case ImplicitParam:
430 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000431 case NonTypeTemplateParm:
432 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000433 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000434 return IDNS_Ordinary;
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000435 case Label:
436 return IDNS_Label;
Francois Pichet87c2e122010-11-21 06:08:52 +0000437 case IndirectField:
438 return IDNS_Ordinary | IDNS_Member;
439
John McCall0d6b1642010-04-23 18:46:30 +0000440 case ObjCCompatibleAlias:
441 case ObjCInterface:
442 return IDNS_Ordinary | IDNS_Type;
443
444 case Typedef:
Richard Smith162e1c12011-04-15 14:24:37 +0000445 case TypeAlias:
Richard Smith3e4c6c42011-05-05 21:57:07 +0000446 case TypeAliasTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000447 case UnresolvedUsingTypename:
448 case TemplateTypeParm:
449 return IDNS_Ordinary | IDNS_Type;
450
John McCall9488ea12009-11-17 05:59:44 +0000451 case UsingShadow:
452 return 0; // we'll actually overwrite this later
453
John McCall7ba107a2009-11-18 02:36:19 +0000454 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000455 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000456
457 case Using:
458 return IDNS_Using;
459
Chris Lattner769dbdf2009-03-27 20:18:19 +0000460 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000461 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Chris Lattner769dbdf2009-03-27 20:18:19 +0000463 case Field:
464 case ObjCAtDefsField:
465 case ObjCIvar:
466 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Chris Lattner769dbdf2009-03-27 20:18:19 +0000468 case Record:
469 case CXXRecord:
470 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000471 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Chris Lattner769dbdf2009-03-27 20:18:19 +0000473 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000474 case NamespaceAlias:
475 return IDNS_Namespace;
476
Chris Lattner769dbdf2009-03-27 20:18:19 +0000477 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000478 return IDNS_Ordinary;
479
Chris Lattner769dbdf2009-03-27 20:18:19 +0000480 case ClassTemplate:
481 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000482 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Chris Lattner769dbdf2009-03-27 20:18:19 +0000484 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000485 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000486 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000487 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000488 case LinkageSpec:
489 case FileScopeAsm:
490 case StaticAssert:
491 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000492 case ObjCPropertyImpl:
493 case ObjCForwardProtocol:
494 case Block:
495 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000496
Chris Lattner769dbdf2009-03-27 20:18:19 +0000497 case UsingDirective:
498 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000499 case ClassTemplatePartialSpecialization:
Francois Pichetaf0f4d02011-08-14 03:52:19 +0000500 case ClassScopeFunctionSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000501 case ObjCImplementation:
502 case ObjCCategory:
503 case ObjCCategoryImpl:
504 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000505 return 0;
506 }
John McCall9488ea12009-11-17 05:59:44 +0000507
508 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000509}
510
Sean Huntcf807c42010-08-18 23:23:40 +0000511void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000512 assert(!HasAttrs && "Decl already contains attrs.");
513
Sean Huntcf807c42010-08-18 23:23:40 +0000514 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
515 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000516
517 AttrBlank = attrs;
518 HasAttrs = true;
519}
520
Sean Huntcf807c42010-08-18 23:23:40 +0000521void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000522 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Eli Friedman56d29372008-06-07 16:52:53 +0000524 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000525 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000526}
527
Sean Huntcf807c42010-08-18 23:23:40 +0000528const AttrVec &Decl::getAttrs() const {
529 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000530 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000531}
532
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000533void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000534 bool HasLHSAttr = this->HasAttrs;
535 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Eli Friedman56d29372008-06-07 16:52:53 +0000537 // Usually, neither decl has attrs, nothing to do.
538 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Eli Friedman56d29372008-06-07 16:52:53 +0000540 // If 'this' has no attrs, swap the other way.
541 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000542 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000544 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Eli Friedman56d29372008-06-07 16:52:53 +0000546 // Handle the case when both decls have attrs.
547 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000548 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000549 return;
550 }
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Eli Friedman56d29372008-06-07 16:52:53 +0000552 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000553 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
554 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000555 this->HasAttrs = false;
556 RHS->HasAttrs = true;
557}
558
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000559Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000560 Decl::Kind DK = D->getDeclKind();
561 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000562#define DECL(NAME, BASE)
563#define DECL_CONTEXT(NAME) \
564 case Decl::NAME: \
565 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
566#define DECL_CONTEXT_BASE(NAME)
567#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000568 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000569#define DECL(NAME, BASE)
570#define DECL_CONTEXT_BASE(NAME) \
571 if (DK >= first##NAME && DK <= last##NAME) \
572 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
573#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000574 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000575 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000576}
577
578DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000579 Decl::Kind DK = D->getKind();
580 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000581#define DECL(NAME, BASE)
582#define DECL_CONTEXT(NAME) \
583 case Decl::NAME: \
584 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
585#define DECL_CONTEXT_BASE(NAME)
586#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000587 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000588#define DECL(NAME, BASE)
589#define DECL_CONTEXT_BASE(NAME) \
590 if (DK >= first##NAME && DK <= last##NAME) \
591 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
592#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000593 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000594 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000595}
596
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000597SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000598 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
599 // FunctionDecl stores EndRangeLoc for this purpose.
600 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
601 const FunctionDecl *Definition;
602 if (FD->hasBody(Definition))
603 return Definition->getSourceRange().getEnd();
604 return SourceLocation();
605 }
606
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000607 if (Stmt *Body = getBody())
608 return Body->getSourceRange().getEnd();
609
610 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000611}
612
Anders Carlsson1329c272009-03-25 23:38:06 +0000613void Decl::CheckAccessDeclContext() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000614#ifndef NDEBUG
John McCall46460a62010-01-20 21:53:11 +0000615 // Suppress this check if any of the following hold:
616 // 1. this is the translation unit (and thus has no parent)
617 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000618 // 3. this is a non-type template parameter
619 // 4. the context is not a record
620 // 5. it's invalid
621 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000622 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000623 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000624 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000625 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000626 isInvalidDecl() ||
627 isa<StaticAssertDecl>(this) ||
628 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
629 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000630 isa<ParmVarDecl>(this) ||
631 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
632 // AS_none as access specifier.
Francois Pichetbc845322011-08-17 01:06:54 +0000633 isa<CXXRecordDecl>(this) ||
634 isa<ClassScopeFunctionSpecializationDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000635 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000636
637 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000638 "Access specifier is AS_none inside a record decl");
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000639#endif
Anders Carlsson1329c272009-03-25 23:38:06 +0000640}
641
John McCallaab9e312011-02-22 22:25:23 +0000642DeclContext *Decl::getNonClosureContext() {
John McCall4b9c2d22011-11-06 09:01:30 +0000643 return getDeclContext()->getNonClosureAncestor();
644}
645
646DeclContext *DeclContext::getNonClosureAncestor() {
647 DeclContext *DC = this;
John McCallaab9e312011-02-22 22:25:23 +0000648
649 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
650 // except that it's significantly more efficient to cast to a known
651 // decl type and call getDeclContext() than to call getParent().
John McCall7b3f8532011-06-23 21:18:31 +0000652 while (isa<BlockDecl>(DC))
653 DC = cast<BlockDecl>(DC)->getDeclContext();
John McCallaab9e312011-02-22 22:25:23 +0000654
655 assert(!DC->isClosure());
656 return DC;
657}
Anders Carlsson1329c272009-03-25 23:38:06 +0000658
Eli Friedman56d29372008-06-07 16:52:53 +0000659//===----------------------------------------------------------------------===//
660// DeclContext Implementation
661//===----------------------------------------------------------------------===//
662
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000663bool DeclContext::classof(const Decl *D) {
664 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000665#define DECL(NAME, BASE)
666#define DECL_CONTEXT(NAME) case Decl::NAME:
667#define DECL_CONTEXT_BASE(NAME)
668#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000669 return true;
670 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000671#define DECL(NAME, BASE)
672#define DECL_CONTEXT_BASE(NAME) \
673 if (D->getKind() >= Decl::first##NAME && \
674 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000675 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000676#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000677 return false;
678 }
679}
680
Douglas Gregora2da7802010-07-25 18:38:02 +0000681DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000682
Douglas Gregore942bbe2009-09-10 16:57:35 +0000683/// \brief Find the parent context of this context that will be
684/// used for unqualified name lookup.
685///
686/// Generally, the parent lookup context is the semantic context. However, for
687/// a friend function the parent lookup context is the lexical context, which
688/// is the class in which the friend is declared.
689DeclContext *DeclContext::getLookupParent() {
690 // FIXME: Find a better way to identify friends
691 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000692 if (getParent()->getRedeclContext()->isFileContext() &&
693 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000694 return getLexicalParent();
695
696 return getParent();
697}
698
Sebastian Redl410c4f22010-08-31 20:53:31 +0000699bool DeclContext::isInlineNamespace() const {
700 return isNamespace() &&
701 cast<NamespaceDecl>(this)->isInline();
702}
703
Douglas Gregorbc221632009-05-28 16:34:51 +0000704bool DeclContext::isDependentContext() const {
705 if (isFileContext())
706 return false;
707
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000708 if (isa<ClassTemplatePartialSpecializationDecl>(this))
709 return true;
710
Douglas Gregorbc221632009-05-28 16:34:51 +0000711 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
712 if (Record->getDescribedClassTemplate())
713 return true;
714
John McCall0c01d182010-03-24 05:22:00 +0000715 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000716 if (Function->getDescribedFunctionTemplate())
717 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000718
John McCall0c01d182010-03-24 05:22:00 +0000719 // Friend function declarations are dependent if their *lexical*
720 // context is dependent.
721 if (cast<Decl>(this)->getFriendObjectKind())
722 return getLexicalParent()->isDependentContext();
723 }
724
Douglas Gregorbc221632009-05-28 16:34:51 +0000725 return getParent() && getParent()->isDependentContext();
726}
727
Douglas Gregor074149e2009-01-05 19:45:36 +0000728bool DeclContext::isTransparentContext() const {
729 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000730 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000731 else if (DeclKind == Decl::LinkageSpec)
732 return true;
Douglas Gregor074149e2009-01-05 19:45:36 +0000733
734 return false;
735}
736
John McCallac65c622010-10-26 04:59:26 +0000737bool DeclContext::isExternCContext() const {
738 const DeclContext *DC = this;
739 while (DC->DeclKind != Decl::TranslationUnit) {
740 if (DC->DeclKind == Decl::LinkageSpec)
741 return cast<LinkageSpecDecl>(DC)->getLanguage()
742 == LinkageSpecDecl::lang_c;
743 DC = DC->getParent();
744 }
745 return false;
746}
747
Sebastian Redl7a126a42010-08-31 00:36:30 +0000748bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000749 if (getPrimaryContext() != this)
750 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000752 for (; DC; DC = DC->getParent())
753 if (DC->getPrimaryContext() == this)
754 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000755 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000756}
757
Steve Naroff0701bbb2009-01-08 17:28:14 +0000758DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000759 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000760 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000761 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000762 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000763 // There is only one DeclContext for these entities.
764 return this;
765
766 case Decl::Namespace:
767 // The original namespace is our primary context.
768 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
769
Douglas Gregor44b43212008-12-11 16:49:14 +0000770 case Decl::ObjCMethod:
771 return this;
772
773 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000774 case Decl::ObjCProtocol:
775 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000776 // FIXME: Can Objective-C interfaces be forward-declared?
777 return this;
778
Steve Naroff0701bbb2009-01-08 17:28:14 +0000779 case Decl::ObjCImplementation:
780 case Decl::ObjCCategoryImpl:
781 return this;
782
Douglas Gregor44b43212008-12-11 16:49:14 +0000783 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000784 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000785 // If this is a tag type that has a definition or is currently
786 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000787 TagDecl *Tag = cast<TagDecl>(this);
788 assert(isa<TagType>(Tag->TypeForDecl) ||
789 isa<InjectedClassNameType>(Tag->TypeForDecl));
790
791 if (TagDecl *Def = Tag->getDefinition())
792 return Def;
793
794 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
795 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
796 if (TagTy->isBeingDefined())
797 // FIXME: is it necessarily being defined in the decl
798 // that owns the type?
799 return TagTy->getDecl();
800 }
801
802 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000803 }
804
Sean Hunt9a555912010-05-30 07:21:58 +0000805 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000806 "Unknown DeclContext kind");
807 return this;
808 }
809}
810
811DeclContext *DeclContext::getNextContext() {
812 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000813 case Decl::Namespace:
814 // Return the next namespace
815 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
816
817 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000818 return 0;
819 }
820}
821
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000822std::pair<Decl *, Decl *>
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000823DeclContext::BuildDeclChain(const SmallVectorImpl<Decl*> &Decls,
824 bool FieldsAlreadyLoaded) {
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000825 // Build up a chain of declarations via the Decl::NextDeclInContext field.
826 Decl *FirstNewDecl = 0;
827 Decl *PrevDecl = 0;
828 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000829 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
830 continue;
831
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000832 Decl *D = Decls[I];
833 if (PrevDecl)
834 PrevDecl->NextDeclInContext = D;
835 else
836 FirstNewDecl = D;
837
838 PrevDecl = D;
839 }
840
841 return std::make_pair(FirstNewDecl, PrevDecl);
842}
843
Douglas Gregor2cf26342009-04-09 22:27:44 +0000844/// \brief Load the declarations within this lexical storage from an
845/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000846void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000847DeclContext::LoadLexicalDeclsFromExternalStorage() const {
848 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000849 assert(hasExternalLexicalStorage() && Source && "No external storage?");
850
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000851 // Notify that we have a DeclContext that is initializing.
852 ExternalASTSource::Deserializing ADeclContext(Source);
Douglas Gregor9fc18c92011-08-26 21:23:06 +0000853
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000854 // Load the external declarations, if any.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000855 SmallVector<Decl*, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000856 ExternalLexicalStorage = false;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000857 switch (Source->FindExternalLexicalDecls(this, Decls)) {
858 case ELR_Success:
859 break;
860
861 case ELR_Failure:
862 case ELR_AlreadyLoaded:
863 return;
864 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000865
866 if (Decls.empty())
867 return;
868
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000869 // We may have already loaded just the fields of this record, in which case
870 // we need to ignore them.
871 bool FieldsAlreadyLoaded = false;
872 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
873 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
874
Douglas Gregor2cf26342009-04-09 22:27:44 +0000875 // Splice the newly-read declarations into the beginning of the list
876 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000877 Decl *ExternalFirst, *ExternalLast;
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000878 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls,
879 FieldsAlreadyLoaded);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000880 ExternalLast->NextDeclInContext = FirstDecl;
881 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000882 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000883 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000884}
885
John McCall76bd1f32010-06-01 09:23:16 +0000886DeclContext::lookup_result
887ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
888 DeclarationName Name) {
889 ASTContext &Context = DC->getParentASTContext();
890 StoredDeclsMap *Map;
891 if (!(Map = DC->LookupPtr))
892 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000893
John McCall76bd1f32010-06-01 09:23:16 +0000894 StoredDeclsList &List = (*Map)[Name];
895 assert(List.isNull());
896 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000897
John McCall76bd1f32010-06-01 09:23:16 +0000898 return DeclContext::lookup_result();
899}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000900
John McCall76bd1f32010-06-01 09:23:16 +0000901DeclContext::lookup_result
902ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +0000903 DeclarationName Name,
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000904 ArrayRef<NamedDecl*> Decls) {
John McCall76bd1f32010-06-01 09:23:16 +0000905 ASTContext &Context = DC->getParentASTContext();;
906
907 StoredDeclsMap *Map;
908 if (!(Map = DC->LookupPtr))
909 Map = DC->CreateStoredDeclsMap(Context);
910
911 StoredDeclsList &List = (*Map)[Name];
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000912 for (ArrayRef<NamedDecl*>::iterator
913 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
John McCall76bd1f32010-06-01 09:23:16 +0000914 if (List.isNull())
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000915 List.setOnlyValue(*I);
John McCall76bd1f32010-06-01 09:23:16 +0000916 else
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000917 List.AddSubsequentDecl(*I);
John McCall76bd1f32010-06-01 09:23:16 +0000918 }
919
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000920 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000921}
922
Sebastian Redl681d7232010-07-27 00:17:23 +0000923DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
924 return decl_iterator(FirstDecl);
925}
926
927DeclContext::decl_iterator DeclContext::noload_decls_end() const {
928 return decl_iterator();
929}
930
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000931DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000932 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000933 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000934
Mike Stump1eb44332009-09-09 15:08:12 +0000935 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000936}
937
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000938DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000939 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000940 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000941
Mike Stump1eb44332009-09-09 15:08:12 +0000942 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000943}
944
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000945bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000946 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000947 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000948
949 return !FirstDecl;
950}
951
John McCall9f54ad42009-12-10 09:41:52 +0000952void DeclContext::removeDecl(Decl *D) {
953 assert(D->getLexicalDeclContext() == this &&
954 "decl being removed from non-lexical context");
955 assert((D->NextDeclInContext || D == LastDecl) &&
956 "decl is not in decls list");
957
958 // Remove D from the decl chain. This is O(n) but hopefully rare.
959 if (D == FirstDecl) {
960 if (D == LastDecl)
961 FirstDecl = LastDecl = 0;
962 else
963 FirstDecl = D->NextDeclInContext;
964 } else {
965 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
966 assert(I && "decl not found in linked list");
967 if (I->NextDeclInContext == D) {
968 I->NextDeclInContext = D->NextDeclInContext;
969 if (D == LastDecl) LastDecl = I;
970 break;
971 }
972 }
973 }
974
975 // Mark that D is no longer in the decl chain.
976 D->NextDeclInContext = 0;
977
978 // Remove D from the lookup table if necessary.
979 if (isa<NamedDecl>(D)) {
980 NamedDecl *ND = cast<NamedDecl>(D);
981
Axel Naumann02368d02011-08-26 14:06:12 +0000982 // Remove only decls that have a name
983 if (!ND->getDeclName()) return;
984
John McCall0c01d182010-03-24 05:22:00 +0000985 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
986 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +0000987
John McCall9f54ad42009-12-10 09:41:52 +0000988 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
989 assert(Pos != Map->end() && "no lookup entry for decl");
990 Pos->second.remove(ND);
991 }
992}
993
John McCall3f9a8a62009-08-11 06:59:38 +0000994void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000995 assert(D->getLexicalDeclContext() == this &&
996 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000997 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000998 "Decl already inserted into a DeclContext");
999
1000 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +00001001 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001002 LastDecl = D;
1003 } else {
1004 FirstDecl = LastDecl = D;
1005 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +00001006
1007 // Notify a C++ record declaration that we've added a member, so it can
1008 // update it's class-specific state.
1009 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1010 Record->addedMember(D);
John McCall3f9a8a62009-08-11 06:59:38 +00001011}
1012
1013void DeclContext::addDecl(Decl *D) {
1014 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001015
1016 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001017 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +00001018}
1019
Sean Callanan9faf8102011-10-21 02:57:43 +00001020void DeclContext::addDeclInternal(Decl *D) {
1021 addHiddenDecl(D);
1022
1023 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1024 ND->getDeclContext()->makeDeclVisibleInContextInternal(ND);
1025}
1026
Douglas Gregor074149e2009-01-05 19:45:36 +00001027/// buildLookup - Build the lookup data structure with all of the
1028/// declarations in DCtx (and any other contexts linked to it or
1029/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001030void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001031 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001032 for (decl_iterator D = DCtx->decls_begin(),
1033 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +00001034 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +00001035 // Insert this declaration into the lookup structure, but only
1036 // if it's semantically in its decl context. During non-lazy
1037 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001038 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +00001039 if (D->getDeclContext() == DCtx)
Sean Callanan9faf8102011-10-21 02:57:43 +00001040 makeDeclVisibleInContextImpl(ND, false);
Douglas Gregor074149e2009-01-05 19:45:36 +00001041
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001042 // Insert any forward-declared Objective-C interface into the lookup
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001043 // data structure.
1044 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
Sean Callanan9faf8102011-10-21 02:57:43 +00001045 makeDeclVisibleInContextImpl(Class->getForwardInterfaceDecl(), false);
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001046
Sebastian Redl410c4f22010-08-31 20:53:31 +00001047 // If this declaration is itself a transparent declaration context or
1048 // inline namespace, add its members (recursively).
Douglas Gregor074149e2009-01-05 19:45:36 +00001049 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redl410c4f22010-08-31 20:53:31 +00001050 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001051 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +00001052 }
1053 }
1054}
1055
Mike Stump1eb44332009-09-09 15:08:12 +00001056DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001057DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +00001058 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001059 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001060 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001061
John McCall76bd1f32010-06-01 09:23:16 +00001062 if (hasExternalVisibleStorage()) {
1063 // Check to see if we've already cached the lookup results.
1064 if (LookupPtr) {
1065 StoredDeclsMap::iterator I = LookupPtr->find(Name);
1066 if (I != LookupPtr->end())
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001067 return I->second.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +00001068 }
1069
1070 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1071 return Source->FindExternalVisibleDeclsByName(this, Name);
1072 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001073
Douglas Gregor3fc749d2008-12-23 00:26:44 +00001074 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +00001075 /// all of the linked DeclContexts (in declaration order!) and
1076 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +00001077 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001078 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +00001079
Douglas Gregorc36c5402009-04-09 17:29:08 +00001080 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001081 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +00001082 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001083
John McCall0c01d182010-03-24 05:22:00 +00001084 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1085 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001086 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001087 return Pos->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +00001088}
1089
Mike Stump1eb44332009-09-09 15:08:12 +00001090DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001091DeclContext::lookup(DeclarationName Name) const {
1092 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001093}
1094
Douglas Gregorb75a3452011-10-15 00:10:27 +00001095void DeclContext::localUncachedLookup(DeclarationName Name,
1096 llvm::SmallVectorImpl<NamedDecl *> &Results) {
1097 Results.clear();
1098
1099 // If there's no external storage, just perform a normal lookup and copy
1100 // the results.
1101 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage()) {
1102 lookup_result LookupResults = lookup(Name);
1103 Results.insert(Results.end(), LookupResults.first, LookupResults.second);
1104 return;
1105 }
1106
1107 // If we have a lookup table, check there first. Maybe we'll get lucky.
1108 if (LookupPtr) {
1109 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1110 if (Pos != LookupPtr->end()) {
1111 Results.insert(Results.end(),
1112 Pos->second.getLookupResult().first,
1113 Pos->second.getLookupResult().second);
1114 return;
1115 }
1116 }
1117
1118 // Slow case: grovel through the declarations in our chain looking for
1119 // matches.
1120 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1121 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1122 if (ND->getDeclName() == Name)
1123 Results.push_back(ND);
1124 }
1125}
1126
Sebastian Redl7a126a42010-08-31 00:36:30 +00001127DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001128 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +00001129 // Skip through transparent contexts.
1130 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +00001131 Ctx = Ctx->getParent();
1132 return Ctx;
1133}
1134
Douglas Gregor88b70942009-02-25 22:02:03 +00001135DeclContext *DeclContext::getEnclosingNamespaceContext() {
1136 DeclContext *Ctx = this;
1137 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +00001138 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +00001139 Ctx = Ctx->getParent();
1140 return Ctx->getPrimaryContext();
1141}
1142
Sebastian Redl7a126a42010-08-31 00:36:30 +00001143bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1144 // For non-file contexts, this is equivalent to Equals.
1145 if (!isFileContext())
1146 return O->Equals(this);
1147
1148 do {
1149 if (O->Equals(this))
1150 return true;
1151
1152 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1153 if (!NS || !NS->isInline())
1154 break;
1155 O = NS->getParent();
1156 } while (O);
1157
1158 return false;
1159}
1160
Sean Callanan9faf8102011-10-21 02:57:43 +00001161void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable)
1162{
1163 makeDeclVisibleInContextWithFlags(D, false, Recoverable);
1164}
1165
1166void DeclContext::makeDeclVisibleInContextInternal(NamedDecl *D, bool Recoverable)
1167{
1168 makeDeclVisibleInContextWithFlags(D, true, Recoverable);
1169}
1170
1171void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +00001172 // FIXME: This feels like a hack. Should DeclarationName support
1173 // template-ids, or is there a better way to keep specializations
1174 // from being visible?
Douglas Gregor9a299e02011-03-04 17:52:15 +00001175 if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
Douglas Gregorcc636682009-02-17 23:15:12 +00001176 return;
Eli Friedman6bc20132009-12-08 05:40:03 +00001177 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1178 if (FD->isFunctionTemplateSpecialization())
1179 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001180
Steve Naroff0701bbb2009-01-08 17:28:14 +00001181 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001182 if (PrimaryContext != this) {
Sean Callanan9faf8102011-10-21 02:57:43 +00001183 PrimaryContext->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +00001184 return;
1185 }
1186
1187 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001188 // into it. If we haven't deserialized externally stored decls, deserialize
1189 // them so we can add the decl. Otherwise, be lazy and don't build that
1190 // structure until someone asks for it.
1191 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Sean Callanan9faf8102011-10-21 02:57:43 +00001192 makeDeclVisibleInContextImpl(D, Internal);
Douglas Gregor074149e2009-01-05 19:45:36 +00001193
Sebastian Redl410c4f22010-08-31 20:53:31 +00001194 // If we are a transparent context or inline namespace, insert into our
1195 // parent context, too. This operation is recursive.
1196 if (isTransparentContext() || isInlineNamespace())
Sean Callanan9faf8102011-10-21 02:57:43 +00001197 getParent()->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00001198
1199 Decl *DCAsDecl = cast<Decl>(this);
1200 // Notify that a decl was made visible unless it's a Tag being defined.
1201 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1202 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1203 L->AddedVisibleDecl(this, D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001204}
1205
Sean Callanan9faf8102011-10-21 02:57:43 +00001206void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001207 // Skip unnamed declarations.
1208 if (!D->getDeclName())
1209 return;
1210
Douglas Gregor5cb0ef42011-05-06 23:32:38 +00001211 // Skip entities that can't be found by name lookup into a particular
1212 // context.
1213 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1214 D->isTemplateParameter())
Douglas Gregorcc636682009-02-17 23:15:12 +00001215 return;
1216
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001217 ASTContext *C = 0;
1218 if (!LookupPtr) {
1219 C = &getParentASTContext();
1220 CreateStoredDeclsMap(*C);
1221 }
1222
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001223 // If there is an external AST source, load any declarations it knows about
1224 // with this declaration's name.
1225 // If the lookup table contains an entry about this name it means that we
1226 // have already checked the external source.
Sean Callanan9faf8102011-10-21 02:57:43 +00001227 if (!Internal)
1228 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1229 if (hasExternalVisibleStorage() &&
1230 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1231 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001232
Douglas Gregor44b43212008-12-11 16:49:14 +00001233 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +00001234 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +00001235 if (DeclNameEntries.isNull()) {
1236 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001237 return;
Douglas Gregor44b43212008-12-11 16:49:14 +00001238 }
Chris Lattner91942502009-02-20 00:55:03 +00001239
Chris Lattnerbdc3d002009-02-20 01:10:07 +00001240 // If it is possible that this is a redeclaration, check to see if there is
1241 // already a decl for which declarationReplaces returns true. If there is
1242 // one, just replace it and return.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001243 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattner67762a32009-02-20 01:44:05 +00001244 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001246 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +00001247 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001248}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001249
1250/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1251/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001252DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001253DeclContext::getUsingDirectives() const {
1254 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001255 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1256 reinterpret_cast<udir_iterator>(Result.second));
1257}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001258
Ted Kremenek3478eb62010-02-11 07:12:28 +00001259//===----------------------------------------------------------------------===//
1260// Creation and Destruction of StoredDeclsMaps. //
1261//===----------------------------------------------------------------------===//
1262
John McCall0c01d182010-03-24 05:22:00 +00001263StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1264 assert(!LookupPtr && "context already has a decls map");
1265 assert(getPrimaryContext() == this &&
1266 "creating decls map on non-primary context");
1267
1268 StoredDeclsMap *M;
1269 bool Dependent = isDependentContext();
1270 if (Dependent)
1271 M = new DependentStoredDeclsMap();
1272 else
1273 M = new StoredDeclsMap();
1274 M->Previous = C.LastSDM;
1275 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1276 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001277 return M;
1278}
1279
1280void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001281 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1282 // pointer because the subclass doesn't add anything that needs to
1283 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001284 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1285}
1286
1287void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1288 while (Map) {
1289 // Advance the iteration before we invalidate memory.
1290 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1291
1292 if (Dependent)
1293 delete static_cast<DependentStoredDeclsMap*>(Map);
1294 else
1295 delete Map;
1296
1297 Map = Next.getPointer();
1298 Dependent = Next.getInt();
1299 }
1300}
1301
John McCall0c01d182010-03-24 05:22:00 +00001302DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1303 DeclContext *Parent,
1304 const PartialDiagnostic &PDiag) {
1305 assert(Parent->isDependentContext()
1306 && "cannot iterate dependent diagnostics of non-dependent context");
1307 Parent = Parent->getPrimaryContext();
1308 if (!Parent->LookupPtr)
1309 Parent->CreateStoredDeclsMap(C);
1310
1311 DependentStoredDeclsMap *Map
1312 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1313
Douglas Gregorb8365182010-03-29 23:56:53 +00001314 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001315 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001316 PartialDiagnostic::Storage *DiagStorage = 0;
1317 if (PDiag.hasStorage())
1318 DiagStorage = new (C) PartialDiagnostic::Storage;
1319
1320 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001321
1322 // TODO: Maybe we shouldn't reverse the order during insertion.
1323 DD->NextDiagnostic = Map->FirstDiagnostic;
1324 Map->FirstDiagnostic = DD;
1325
1326 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001327}