blob: 45c5494d4cbf53f5df41f2a3d252167aa5fcdf05 [file] [log] [blame]
Eli Friedmana8a09ac2008-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 Gregor469fc9a2009-02-02 23:39:07 +000015#include "clang/AST/Decl.h"
Douglas Gregord2b6edc2009-04-07 17:20:56 +000016#include "clang/AST/DeclContextInternals.h"
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/DeclTemplate.h"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000020#include "clang/AST/ASTContext.h"
Douglas Gregor8acb7272008-12-11 16:49:14 +000021#include "clang/AST/Type.h"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000022#include "llvm/ADT/DenseMap.h"
Chris Lattnerc309ade2009-03-05 08:00:35 +000023#include "llvm/Support/raw_ostream.h"
Douglas Gregor6e71edc2008-12-23 21:05:05 +000024#include <algorithm>
Chris Lattner7d6220c2009-03-02 22:20:04 +000025#include <cstdio>
Douglas Gregorddfd9d52008-12-23 00:26:44 +000026#include <vector>
Eli Friedmana8a09ac2008-06-07 16:52:53 +000027using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// Statistics
31//===----------------------------------------------------------------------===//
32
Douglas Gregor469fc9a2009-02-02 23:39:07 +000033#define DECL(Derived, Base) static int n##Derived##s = 0;
34#include "clang/AST/DeclNodes.def"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000035
36static bool StatSwitch = false;
37
38// This keeps track of all decl attributes. Since so few decls have attrs, we
39// keep them in a hash map instead of wasting space in the Decl class.
40typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
41
42static DeclAttrMapTy *DeclAttrs = 0;
43
44const char *Decl::getDeclKindName() const {
45 switch (DeclKind) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000046 default: assert(0 && "Declaration not in DeclNodes.def!");
47#define DECL(Derived, Base) case Derived: return #Derived;
48#include "clang/AST/DeclNodes.def"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000049 }
50}
51
Steve Naroffe5f128a2009-01-20 19:53:53 +000052const char *DeclContext::getDeclKindName() const {
53 switch (DeclKind) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000054 default: assert(0 && "Declaration context not in DeclNodes.def!");
Argiris Kirtzidis2d9b7612009-02-16 14:28:33 +000055#define DECL(Derived, Base) case Decl::Derived: return #Derived;
Douglas Gregor469fc9a2009-02-02 23:39:07 +000056#include "clang/AST/DeclNodes.def"
Steve Naroffe5f128a2009-01-20 19:53:53 +000057 }
58}
59
Eli Friedmana8a09ac2008-06-07 16:52:53 +000060bool Decl::CollectingStats(bool Enable) {
61 if (Enable)
62 StatSwitch = true;
63 return StatSwitch;
64}
65
66void Decl::PrintStats() {
67 fprintf(stderr, "*** Decl Stats:\n");
Eli Friedmana8a09ac2008-06-07 16:52:53 +000068
Douglas Gregor469fc9a2009-02-02 23:39:07 +000069 int totalDecls = 0;
70#define DECL(Derived, Base) totalDecls += n##Derived##s;
71#include "clang/AST/DeclNodes.def"
72 fprintf(stderr, " %d decls total.\n", totalDecls);
73
74 int totalBytes = 0;
75#define DECL(Derived, Base) \
76 if (n##Derived##s > 0) { \
77 totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \
78 fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \
79 n##Derived##s, (int)sizeof(Derived##Decl), \
80 (int)(n##Derived##s * sizeof(Derived##Decl))); \
81 }
82#include "clang/AST/DeclNodes.def"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000083
Douglas Gregor469fc9a2009-02-02 23:39:07 +000084 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedmana8a09ac2008-06-07 16:52:53 +000085}
86
87void Decl::addDeclKind(Kind k) {
88 switch (k) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000089 default: assert(0 && "Declaration not in DeclNodes.def!");
90#define DECL(Derived, Base) case Derived: ++n##Derived##s; break;
91#include "clang/AST/DeclNodes.def"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000092 }
93}
94
95//===----------------------------------------------------------------------===//
Chris Lattnerc309ade2009-03-05 08:00:35 +000096// PrettyStackTraceDecl Implementation
97//===----------------------------------------------------------------------===//
98
99void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
100 SourceLocation TheLoc = Loc;
101 if (TheLoc.isInvalid() && TheDecl)
102 TheLoc = TheDecl->getLocation();
103
104 if (TheLoc.isValid()) {
105 TheLoc.print(OS, SM);
106 OS << ": ";
107 }
108
109 OS << Message;
110
111 if (NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
112 OS << " '" << DN->getQualifiedNameAsString() << '\'';
113 OS << '\n';
114}
115
116//===----------------------------------------------------------------------===//
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000117// Decl Implementation
118//===----------------------------------------------------------------------===//
119
Chris Lattner8752fe02009-03-27 20:18:19 +0000120// Out-of-line virtual method providing a home for Decl.
121Decl::~Decl() {
122 if (isOutOfSemaDC())
123 delete getMultipleDC();
124
125 assert(!HasAttrs && "attributes should have been freed by Destroy");
126}
127
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000128void Decl::setDeclContext(DeclContext *DC) {
129 if (isOutOfSemaDC())
130 delete getMultipleDC();
131
Chris Lattner5ce4f6d2009-03-29 06:06:59 +0000132 DeclCtx = DC;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000133}
134
135void Decl::setLexicalDeclContext(DeclContext *DC) {
136 if (DC == getLexicalDeclContext())
137 return;
138
139 if (isInSemaDC()) {
140 MultipleDC *MDC = new MultipleDC();
141 MDC->SemanticDC = getDeclContext();
142 MDC->LexicalDC = DC;
Chris Lattner5ce4f6d2009-03-29 06:06:59 +0000143 DeclCtx = MDC;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000144 } else {
145 getMultipleDC()->LexicalDC = DC;
146 }
147}
148
Chris Lattner8752fe02009-03-27 20:18:19 +0000149unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
150 switch (DeclKind) {
151 default:
152 if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast)
153 return IDNS_Ordinary;
154 assert(0 && "Unknown decl kind!");
155 case OverloadedFunction:
156 case Typedef:
157 case EnumConstant:
158 case Var:
159 case ImplicitParam:
160 case ParmVar:
161 case OriginalParmVar:
162 case NonTypeTemplateParm:
163 case ObjCMethod:
164 case ObjCContainer:
165 case ObjCCategory:
166 case ObjCInterface:
167 case ObjCCategoryImpl:
168 case ObjCProperty:
169 case ObjCCompatibleAlias:
170 return IDNS_Ordinary;
171
172 case ObjCProtocol:
173 return IDNS_Protocol;
174
175 case Field:
176 case ObjCAtDefsField:
177 case ObjCIvar:
178 return IDNS_Member;
179
180 case Record:
181 case CXXRecord:
182 case Enum:
183 case TemplateTypeParm:
184 return IDNS_Tag;
185
186 case Namespace:
187 case Template:
188 case FunctionTemplate:
189 case ClassTemplate:
190 case TemplateTemplateParm:
Anders Carlsson1bf91fb2009-03-28 23:02:53 +0000191 case NamespaceAlias:
Chris Lattner8752fe02009-03-27 20:18:19 +0000192 return IDNS_Tag | IDNS_Ordinary;
193
194 // Never have names.
195 case LinkageSpec:
196 case FileScopeAsm:
197 case StaticAssert:
198 case ObjCClass:
199 case ObjCImplementation:
200 case ObjCPropertyImpl:
201 case ObjCForwardProtocol:
202 case Block:
203 case TranslationUnit:
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000204
Chris Lattner8752fe02009-03-27 20:18:19 +0000205 // Aren't looked up?
206 case UsingDirective:
207 case ClassTemplateSpecialization:
208 return 0;
209 }
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000210}
211
212void Decl::addAttr(Attr *NewAttr) {
213 if (!DeclAttrs)
214 DeclAttrs = new DeclAttrMapTy();
215
216 Attr *&ExistingAttr = (*DeclAttrs)[this];
217
218 NewAttr->setNext(ExistingAttr);
219 ExistingAttr = NewAttr;
220
221 HasAttrs = true;
222}
223
224void Decl::invalidateAttrs() {
225 if (!HasAttrs) return;
226
227 HasAttrs = false;
228 (*DeclAttrs)[this] = 0;
229 DeclAttrs->erase(this);
230
231 if (DeclAttrs->empty()) {
232 delete DeclAttrs;
233 DeclAttrs = 0;
234 }
235}
236
Chris Lattner004a9bb2009-03-21 06:27:31 +0000237const Attr *Decl::getAttrsImpl() const {
238 assert(HasAttrs && "getAttrs() should verify this!");
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000239 return (*DeclAttrs)[this];
240}
241
242void Decl::swapAttrs(Decl *RHS) {
243 bool HasLHSAttr = this->HasAttrs;
244 bool HasRHSAttr = RHS->HasAttrs;
245
246 // Usually, neither decl has attrs, nothing to do.
247 if (!HasLHSAttr && !HasRHSAttr) return;
248
249 // If 'this' has no attrs, swap the other way.
250 if (!HasLHSAttr)
251 return RHS->swapAttrs(this);
252
253 // Handle the case when both decls have attrs.
254 if (HasRHSAttr) {
255 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
256 return;
257 }
258
259 // Otherwise, LHS has an attr and RHS doesn't.
260 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
261 (*DeclAttrs).erase(this);
262 this->HasAttrs = false;
263 RHS->HasAttrs = true;
264}
265
266
Chris Lattner0fc6d642009-03-04 06:05:19 +0000267void Decl::Destroy(ASTContext &C) {
268 // Free attributes for this decl.
269 if (HasAttrs) {
270 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
271 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
272
273 // release attributes.
274 it->second->Destroy(C);
275 invalidateAttrs();
276 HasAttrs = false;
277 }
278
Douglas Gregor8314d742009-01-13 19:47:12 +0000279#if 0
Douglas Gregorf4006be2009-01-20 04:25:11 +0000280 // FIXME: Once ownership is fully understood, we can enable this code
281 if (DeclContext *DC = dyn_cast<DeclContext>(this))
282 DC->decls_begin()->Destroy(C);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000283
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000284 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000285 // within the loop, only the Destroy method for the first Decl
286 // will deallocate all of the Decls in a chain.
287
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000288 Decl* N = getNextDeclInContext();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000289
290 while (N) {
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000291 Decl* Tmp = N->getNextDeclInContext();
292 N->NextDeclInContext = 0;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000293 N->Destroy(C);
294 N = Tmp;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000295 }
Douglas Gregor8314d742009-01-13 19:47:12 +0000296
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000297 this->~Decl();
Steve Naroff5abb0282009-01-27 21:25:57 +0000298 C.Deallocate((void *)this);
Douglas Gregorf4006be2009-01-20 04:25:11 +0000299#endif
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000300}
301
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000302Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000303 Decl::Kind DK = D->getDeclKind();
304 switch(DK) {
305#define DECL_CONTEXT(Name) \
306 case Decl::Name: \
307 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
308#define DECL_CONTEXT_BASE(Name)
309#include "clang/AST/DeclNodes.def"
310 default:
311#define DECL_CONTEXT_BASE(Name) \
312 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
313 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
314#include "clang/AST/DeclNodes.def"
315 assert(false && "a decl that inherits DeclContext isn't handled");
316 return 0;
317 }
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000318}
319
320DeclContext *Decl::castToDeclContext(const Decl *D) {
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000321 Decl::Kind DK = D->getKind();
322 switch(DK) {
323#define DECL_CONTEXT(Name) \
324 case Decl::Name: \
325 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
326#define DECL_CONTEXT_BASE(Name)
327#include "clang/AST/DeclNodes.def"
328 default:
329#define DECL_CONTEXT_BASE(Name) \
330 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
331 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
332#include "clang/AST/DeclNodes.def"
333 assert(false && "a decl that inherits DeclContext isn't handled");
334 return 0;
335 }
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000336}
337
Anders Carlsson8ea6a322009-03-25 23:38:06 +0000338#ifndef NDEBUG
339void Decl::CheckAccessDeclContext() const {
Douglas Gregora470cea2009-04-07 20:58:25 +0000340 assert((Access != AS_none || isa<TranslationUnitDecl>(this) ||
341 !isa<CXXRecordDecl>(getDeclContext())) &&
Anders Carlsson8ea6a322009-03-25 23:38:06 +0000342 "Access specifier is AS_none inside a record decl");
343}
344
345#endif
346
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000347//===----------------------------------------------------------------------===//
348// DeclContext Implementation
349//===----------------------------------------------------------------------===//
350
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000351bool DeclContext::classof(const Decl *D) {
352 switch (D->getKind()) {
353#define DECL_CONTEXT(Name) case Decl::Name:
354#define DECL_CONTEXT_BASE(Name)
355#include "clang/AST/DeclNodes.def"
356 return true;
357 default:
358#define DECL_CONTEXT_BASE(Name) \
359 if (D->getKind() >= Decl::Name##First && \
360 D->getKind() <= Decl::Name##Last) \
361 return true;
362#include "clang/AST/DeclNodes.def"
363 return false;
364 }
365}
366
Douglas Gregor8acb7272008-12-11 16:49:14 +0000367DeclContext::~DeclContext() {
Chris Lattner8752fe02009-03-27 20:18:19 +0000368 if (isLookupMap())
Chris Lattner265f01d2009-02-20 00:55:03 +0000369 delete static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
370 else
371 delete [] static_cast<NamedDecl**>(LookupPtr.getPointer());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000372}
373
374void DeclContext::DestroyDecls(ASTContext &C) {
Douglas Gregorf4006be2009-01-20 04:25:11 +0000375 for (decl_iterator D = decls_begin(); D != decls_end(); )
376 (*D++)->Destroy(C);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000377}
378
Douglas Gregord8028382009-01-05 19:45:36 +0000379bool DeclContext::isTransparentContext() const {
380 if (DeclKind == Decl::Enum)
381 return true; // FIXME: Check for C++0x scoped enums
382 else if (DeclKind == Decl::LinkageSpec)
383 return true;
Douglas Gregor1db12932009-02-26 00:02:51 +0000384 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregor723d3332009-01-07 00:43:41 +0000385 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregord8028382009-01-05 19:45:36 +0000386 else if (DeclKind == Decl::Namespace)
387 return false; // FIXME: Check for C++0x inline namespaces
388
389 return false;
390}
391
Steve Naroffab63fd62009-01-08 17:28:14 +0000392DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000393 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000394 case Decl::TranslationUnit:
Douglas Gregord8028382009-01-05 19:45:36 +0000395 case Decl::LinkageSpec:
396 case Decl::Block:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000397 // There is only one DeclContext for these entities.
398 return this;
399
400 case Decl::Namespace:
401 // The original namespace is our primary context.
402 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
403
Douglas Gregor8acb7272008-12-11 16:49:14 +0000404 case Decl::ObjCMethod:
405 return this;
406
407 case Decl::ObjCInterface:
Steve Naroffab63fd62009-01-08 17:28:14 +0000408 case Decl::ObjCProtocol:
409 case Decl::ObjCCategory:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000410 // FIXME: Can Objective-C interfaces be forward-declared?
411 return this;
412
Steve Naroffab63fd62009-01-08 17:28:14 +0000413 case Decl::ObjCImplementation:
414 case Decl::ObjCCategoryImpl:
415 return this;
416
Douglas Gregor8acb7272008-12-11 16:49:14 +0000417 default:
Douglas Gregora08b6c72009-02-17 23:15:12 +0000418 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
419 // If this is a tag type that has a definition or is currently
420 // being defined, that definition is our primary context.
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000421 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAsTagType())
Douglas Gregora08b6c72009-02-17 23:15:12 +0000422 if (TagT->isBeingDefined() ||
423 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
424 return TagT->getDecl();
425 return this;
426 }
427
Douglas Gregor8acb7272008-12-11 16:49:14 +0000428 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
429 "Unknown DeclContext kind");
430 return this;
431 }
432}
433
434DeclContext *DeclContext::getNextContext() {
435 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000436 case Decl::Namespace:
437 // Return the next namespace
438 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
439
440 default:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000441 return 0;
442 }
443}
444
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000445void DeclContext::addDecl(Decl *D) {
Chris Lattner53602072009-02-20 00:56:18 +0000446 assert(D->getLexicalDeclContext() == this &&
447 "Decl inserted into wrong lexical context");
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000448 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregord1675382009-01-09 19:42:16 +0000449 "Decl already inserted into a DeclContext");
450
451 if (FirstDecl) {
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000452 LastDecl->NextDeclInContext = D;
Douglas Gregord1675382009-01-09 19:42:16 +0000453 LastDecl = D;
454 } else {
455 FirstDecl = LastDecl = D;
456 }
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000457
458 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000459 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000460}
461
Douglas Gregord8028382009-01-05 19:45:36 +0000462/// buildLookup - Build the lookup data structure with all of the
463/// declarations in DCtx (and any other contexts linked to it or
464/// transparent contexts nested within it).
Steve Naroffab63fd62009-01-08 17:28:14 +0000465void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregord8028382009-01-05 19:45:36 +0000466 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregorf0464ec2009-01-06 07:17:58 +0000467 for (decl_iterator D = DCtx->decls_begin(), DEnd = DCtx->decls_end();
468 D != DEnd; ++D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000469 // Insert this declaration into the lookup structure
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000470 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000471 makeDeclVisibleInContextImpl(ND);
Douglas Gregord8028382009-01-05 19:45:36 +0000472
473 // If this declaration is itself a transparent declaration context,
474 // add its members (recursively).
475 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
476 if (InnerCtx->isTransparentContext())
Steve Naroffab63fd62009-01-08 17:28:14 +0000477 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregord8028382009-01-05 19:45:36 +0000478 }
479 }
480}
481
Douglas Gregor8acb7272008-12-11 16:49:14 +0000482DeclContext::lookup_result
Steve Naroffab63fd62009-01-08 17:28:14 +0000483DeclContext::lookup(DeclarationName Name) {
484 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000485 if (PrimaryContext != this)
Steve Naroffab63fd62009-01-08 17:28:14 +0000486 return PrimaryContext->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000487
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000488 /// If there is no lookup data structure, build one now by walking
Douglas Gregor8acb7272008-12-11 16:49:14 +0000489 /// all of the linked DeclContexts (in declaration order!) and
490 /// inserting their values.
Douglas Gregord8028382009-01-05 19:45:36 +0000491 if (LookupPtr.getPointer() == 0)
Steve Naroffab63fd62009-01-08 17:28:14 +0000492 buildLookup(this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000493
Douglas Gregor8acb7272008-12-11 16:49:14 +0000494 if (isLookupMap()) {
495 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
496 StoredDeclsMap::iterator Pos = Map->find(Name);
Chris Lattner265f01d2009-02-20 00:55:03 +0000497 if (Pos == Map->end())
498 return lookup_result(0, 0);
Chris Lattner77820d52009-02-20 01:44:05 +0000499 return Pos->second.getLookupResult();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000500 }
501
502 // We have a small array. Look into it.
503 unsigned Size = LookupPtr.getInt();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000504 NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer());
Douglas Gregor39677622008-12-11 20:41:00 +0000505 for (unsigned Idx = 0; Idx != Size; ++Idx)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000506 if (Array[Idx]->getDeclName() == Name) {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000507 unsigned Last = Idx + 1;
508 while (Last != Size && Array[Last]->getDeclName() == Name)
509 ++Last;
510 return lookup_result(&Array[Idx], &Array[Last]);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000511 }
512
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000513 return lookup_result(0, 0);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000514}
515
516DeclContext::lookup_const_result
Steve Naroffab63fd62009-01-08 17:28:14 +0000517DeclContext::lookup(DeclarationName Name) const {
518 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000519}
520
Chris Lattner9a502bd2009-03-27 19:19:59 +0000521DeclContext *DeclContext::getLookupContext() {
522 DeclContext *Ctx = this;
Douglas Gregordb568cf2009-01-08 20:45:30 +0000523 // Skip through transparent contexts.
Douglas Gregor69e781f2009-01-06 23:51:29 +0000524 while (Ctx->isTransparentContext())
525 Ctx = Ctx->getParent();
526 return Ctx;
527}
528
Douglas Gregor0d93f692009-02-25 22:02:03 +0000529DeclContext *DeclContext::getEnclosingNamespaceContext() {
530 DeclContext *Ctx = this;
531 // Skip through non-namespace, non-translation-unit contexts.
532 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
533 Ctx = Ctx->getParent();
534 return Ctx->getPrimaryContext();
535}
536
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000537void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
Douglas Gregora08b6c72009-02-17 23:15:12 +0000538 // FIXME: This feels like a hack. Should DeclarationName support
539 // template-ids, or is there a better way to keep specializations
540 // from being visible?
541 if (isa<ClassTemplateSpecializationDecl>(D))
542 return;
543
Steve Naroffab63fd62009-01-08 17:28:14 +0000544 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000545 if (PrimaryContext != this) {
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000546 PrimaryContext->makeDeclVisibleInContext(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000547 return;
548 }
549
550 // If we already have a lookup data structure, perform the insertion
551 // into it. Otherwise, be lazy and don't build that structure until
552 // someone asks for it.
553 if (LookupPtr.getPointer())
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000554 makeDeclVisibleInContextImpl(D);
Douglas Gregord8028382009-01-05 19:45:36 +0000555
Douglas Gregord8028382009-01-05 19:45:36 +0000556 // If we are a transparent context, insert into our parent context,
557 // too. This operation is recursive.
558 if (isTransparentContext())
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000559 getParent()->makeDeclVisibleInContext(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000560}
561
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000562void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000563 // Skip unnamed declarations.
564 if (!D->getDeclName())
565 return;
566
Douglas Gregora08b6c72009-02-17 23:15:12 +0000567 // FIXME: This feels like a hack. Should DeclarationName support
568 // template-ids, or is there a better way to keep specializations
569 // from being visible?
570 if (isa<ClassTemplateSpecializationDecl>(D))
571 return;
572
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000573 bool MayBeRedeclaration = true;
574
Douglas Gregor8acb7272008-12-11 16:49:14 +0000575 if (!isLookupMap()) {
576 unsigned Size = LookupPtr.getInt();
577
578 // The lookup data is stored as an array. Search through the array
579 // to find the insertion location.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000580 NamedDecl **Array;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000581 if (Size == 0) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000582 Array = new NamedDecl*[LookupIsMap - 1];
Douglas Gregor8acb7272008-12-11 16:49:14 +0000583 LookupPtr.setPointer(Array);
584 } else {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000585 Array = static_cast<NamedDecl **>(LookupPtr.getPointer());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000586 }
587
588 // We always keep declarations of the same name next to each other
589 // in the array, so that it is easy to return multiple results
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000590 // from lookup().
591 unsigned FirstMatch;
592 for (FirstMatch = 0; FirstMatch != Size; ++FirstMatch)
593 if (Array[FirstMatch]->getDeclName() == D->getDeclName())
Douglas Gregor39677622008-12-11 20:41:00 +0000594 break;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000595
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000596 unsigned InsertPos = FirstMatch;
597 if (FirstMatch != Size) {
598 // We found another declaration with the same name. First
599 // determine whether this is a redeclaration of an existing
600 // declaration in this scope, in which case we will replace the
601 // existing declaration.
602 unsigned LastMatch = FirstMatch;
603 for (; LastMatch != Size; ++LastMatch) {
604 if (Array[LastMatch]->getDeclName() != D->getDeclName())
605 break;
606
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000607 if (D->declarationReplaces(Array[LastMatch])) {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000608 // D is a redeclaration of an existing element in the
609 // array. Replace that element with D.
610 Array[LastMatch] = D;
611 return;
612 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000613 }
614
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000615 // [FirstMatch, LastMatch) contains the set of declarations that
616 // have the same name as this declaration. Determine where the
Douglas Gregor7a7be652009-02-03 19:21:40 +0000617 // declaration D will be inserted into this range.
618 if (D->getKind() == Decl::UsingDirective ||
619 D->getIdentifierNamespace() == Decl::IDNS_Tag)
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000620 InsertPos = LastMatch;
621 else if (Array[LastMatch-1]->getIdentifierNamespace() == Decl::IDNS_Tag)
622 InsertPos = LastMatch - 1;
623 else
624 InsertPos = LastMatch;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000625 }
626
627 if (Size < LookupIsMap - 1) {
628 // The new declaration will fit in the array. Insert the new
629 // declaration at the position Match in the array.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000630 for (unsigned Idx = Size; Idx > InsertPos; --Idx)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000631 Array[Idx] = Array[Idx-1];
632
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000633 Array[InsertPos] = D;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000634 LookupPtr.setInt(Size + 1);
635 return;
636 }
637
638 // We've reached capacity in this array. Create a map and copy in
639 // all of the declarations that were stored in the array.
640 StoredDeclsMap *Map = new StoredDeclsMap(16);
641 LookupPtr.setPointer(Map);
642 LookupPtr.setInt(LookupIsMap);
Douglas Gregor39677622008-12-11 20:41:00 +0000643 for (unsigned Idx = 0; Idx != LookupIsMap - 1; ++Idx)
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000644 makeDeclVisibleInContextImpl(Array[Idx]);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000645 delete [] Array;
646
647 // Fall through to perform insertion into the map.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000648 MayBeRedeclaration = false;
649 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000650
651 // Insert this declaration into the map.
Chris Lattner77820d52009-02-20 01:44:05 +0000652 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
653 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
654 if (DeclNameEntries.isNull()) {
655 DeclNameEntries.setOnlyValue(D);
Chris Lattner6719b1f2009-02-19 07:00:44 +0000656 return;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000657 }
Chris Lattner265f01d2009-02-20 00:55:03 +0000658
Chris Lattnerba589d42009-02-20 01:10:07 +0000659 // If it is possible that this is a redeclaration, check to see if there is
660 // already a decl for which declarationReplaces returns true. If there is
661 // one, just replace it and return.
Chris Lattner77820d52009-02-20 01:44:05 +0000662 if (MayBeRedeclaration && DeclNameEntries.HandleRedeclaration(D))
663 return;
Chris Lattner265f01d2009-02-20 00:55:03 +0000664
Chris Lattner6719b1f2009-02-19 07:00:44 +0000665 // Put this declaration into the appropriate slot.
Chris Lattner77820d52009-02-20 01:44:05 +0000666 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000667}
Douglas Gregor7a7be652009-02-03 19:21:40 +0000668
669/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
670/// this context.
671DeclContext::udir_iterator_range DeclContext::getUsingDirectives() const {
672 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
673 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
674 reinterpret_cast<udir_iterator>(Result.second));
675}
676