blob: c959b05473fee51fe883c412e8e0f72d0a4aa18f [file] [log] [blame]
Douglas Gregor6ab35242009-04-09 21:40:53 +00001//===-- DeclBase.h - Base Classes for representing declarations -*- C++ -*-===//
Chris Lattner0ed844b2008-04-04 06:12:32 +00002//
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//
Chris Lattnerb048c982008-04-06 04:47:34 +000010// This file defines the Decl and DeclContext interfaces.
Chris Lattner0ed844b2008-04-04 06:12:32 +000011//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_DECLBASE_H
15#define LLVM_CLANG_AST_DECLBASE_H
16
17#include "clang/AST/Attr.h"
18#include "clang/AST/Type.h"
Douglas Gregor4afa39d2009-01-20 01:17:11 +000019// FIXME: Layering violation
20#include "clang/Parse/AccessSpecifier.h"
Chris Lattner49f28ca2009-03-05 08:00:35 +000021#include "llvm/Support/PrettyStackTrace.h"
Chris Lattneree219fd2009-03-29 06:06:59 +000022#include "llvm/ADT/PointerUnion.h"
Chris Lattner0ed844b2008-04-04 06:12:32 +000023
24namespace clang {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +000025class DeclContext;
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000026class TranslationUnitDecl;
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000027class NamespaceDecl;
Douglas Gregor2a3009a2009-02-03 19:21:40 +000028class UsingDirectiveDecl;
Douglas Gregor44b43212008-12-11 16:49:14 +000029class NamedDecl;
Chris Lattner0ed844b2008-04-04 06:12:32 +000030class FunctionDecl;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000031class CXXRecordDecl;
Chris Lattnerb048c982008-04-06 04:47:34 +000032class EnumDecl;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000033class ObjCMethodDecl;
Douglas Gregor64650af2009-02-02 23:39:07 +000034class ObjCContainerDecl;
Chris Lattner0ed844b2008-04-04 06:12:32 +000035class ObjCInterfaceDecl;
Steve Naroff0701bbb2009-01-08 17:28:14 +000036class ObjCCategoryDecl;
37class ObjCProtocolDecl;
38class ObjCImplementationDecl;
39class ObjCCategoryImplDecl;
Douglas Gregor074149e2009-01-05 19:45:36 +000040class LinkageSpecDecl;
Steve Naroff56ee6892008-10-08 17:01:13 +000041class BlockDecl;
Douglas Gregor44b43212008-12-11 16:49:14 +000042class DeclarationName;
Ted Kremenekeaab2062009-03-12 18:33:24 +000043class CompoundStmt;
Chris Lattner0eda3b32009-03-29 04:32:54 +000044}
45
46namespace llvm {
47// DeclContext* is only 4-byte aligned on 32-bit systems.
48template<>
49 class PointerLikeTypeTraits<clang::DeclContext*> {
50 typedef clang::DeclContext* PT;
51public:
52 static inline void *getAsVoidPointer(PT P) { return P; }
53 static inline PT getFromVoidPointer(void *P) {
54 return static_cast<PT>(P);
55 }
56 enum { NumLowBitsAvailable = 2 };
57};
58}
59
60namespace clang {
Chris Lattner0ed844b2008-04-04 06:12:32 +000061
62/// Decl - This represents one declaration (or definition), e.g. a variable,
63/// typedef, function, struct, etc.
64///
65class Decl {
66public:
Douglas Gregor64650af2009-02-02 23:39:07 +000067 /// \brief Lists the kind of concrete classes of Decl.
Chris Lattner0ed844b2008-04-04 06:12:32 +000068 enum Kind {
Douglas Gregor64650af2009-02-02 23:39:07 +000069#define DECL(Derived, Base) Derived,
70#define DECL_RANGE(CommonBase, Start, End) \
71 CommonBase##First = Start, CommonBase##Last = End,
72#define LAST_DECL_RANGE(CommonBase, Start, End) \
73 CommonBase##First = Start, CommonBase##Last = End
74#include "clang/AST/DeclNodes.def"
Chris Lattner0ed844b2008-04-04 06:12:32 +000075 };
76
Douglas Gregor8fc463a2009-04-24 00:11:27 +000077 /// IdentifierNamespace - According to C99 6.2.3, there are four
78 /// namespaces, labels, tags, members and ordinary
79 /// identifiers. These are meant as bitmasks, so that searches in
80 /// C++ can look into the "tag" namespace during ordinary lookup. We
81 /// use additional namespaces for Objective-C entities.
Chris Lattner0ed844b2008-04-04 06:12:32 +000082 enum IdentifierNamespace {
Douglas Gregor2ce52f32008-04-13 21:07:44 +000083 IDNS_Label = 0x1,
84 IDNS_Tag = 0x2,
85 IDNS_Member = 0x4,
Douglas Gregor7dda67d2009-02-05 19:25:20 +000086 IDNS_Ordinary = 0x8,
Douglas Gregor8fc463a2009-04-24 00:11:27 +000087 IDNS_ObjCProtocol = 0x10,
88 IDNS_ObjCImplementation = 0x20,
89 IDNS_ObjCCategoryImpl = 0x40
Chris Lattner0ed844b2008-04-04 06:12:32 +000090 };
91
92 /// ObjCDeclQualifier - Qualifier used on types in method declarations
93 /// for remote messaging. They are meant for the arguments though and
94 /// applied to the Decls (ObjCMethodDecl and ParmVarDecl).
95 enum ObjCDeclQualifier {
96 OBJC_TQ_None = 0x0,
97 OBJC_TQ_In = 0x1,
98 OBJC_TQ_Inout = 0x2,
99 OBJC_TQ_Out = 0x4,
100 OBJC_TQ_Bycopy = 0x8,
101 OBJC_TQ_Byref = 0x10,
102 OBJC_TQ_Oneway = 0x20
103 };
104
105private:
Chris Lattner244a67d2009-03-28 06:04:26 +0000106 /// NextDeclInContext - The next declaration within the same lexical
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000107 /// DeclContext. These pointers form the linked list that is
108 /// traversed via DeclContext's decls_begin()/decls_end().
Chris Lattner244a67d2009-03-28 06:04:26 +0000109 Decl *NextDeclInContext;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000110
111 friend class DeclContext;
112
Chris Lattneree219fd2009-03-29 06:06:59 +0000113 struct MultipleDC {
114 DeclContext *SemanticDC;
115 DeclContext *LexicalDC;
116 };
117
118
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000119 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
120 /// For declarations that don't contain C++ scope specifiers, it contains
121 /// the DeclContext where the Decl was declared.
122 /// For declarations with C++ scope specifiers, it contains a MultipleDC*
123 /// with the context where it semantically belongs (SemanticDC) and the
124 /// context where it was lexically declared (LexicalDC).
125 /// e.g.:
126 ///
127 /// namespace A {
128 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
129 /// }
130 /// void A::f(); // SemanticDC == namespace 'A'
131 /// // LexicalDC == global namespace
Chris Lattneree219fd2009-03-29 06:06:59 +0000132 llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000133
Chris Lattneree219fd2009-03-29 06:06:59 +0000134 inline bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); }
135 inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000136 inline MultipleDC *getMultipleDC() const {
Chris Lattneree219fd2009-03-29 06:06:59 +0000137 return DeclCtx.get<MultipleDC*>();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000138 }
Chris Lattner10d83792009-03-27 18:46:15 +0000139 inline DeclContext *getSemanticDC() const {
Chris Lattneree219fd2009-03-29 06:06:59 +0000140 return DeclCtx.get<DeclContext*>();
Chris Lattner10d83792009-03-27 18:46:15 +0000141 }
142
Daniel Dunbar39d76502009-03-04 02:26:41 +0000143 /// Loc - The location that this decl.
144 SourceLocation Loc;
145
Chris Lattner0ed844b2008-04-04 06:12:32 +0000146 /// DeclKind - This indicates which class this is.
147 Kind DeclKind : 8;
148
149 /// InvalidDecl - This indicates a semantic error occurred.
150 unsigned int InvalidDecl : 1;
151
152 /// HasAttrs - This indicates whether the decl has attributes or not.
153 unsigned int HasAttrs : 1;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000154
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000155 /// Implicit - Whether this declaration was implicitly generated by
156 /// the implementation rather than explicitly written by the user.
157 bool Implicit : 1;
158
Douglas Gregore0762c92009-06-19 23:52:42 +0000159 /// \brief Whether this declaration was "used", meaning that a definition is
160 /// required.
161 bool Used : 1;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000162
163protected:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000164 /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in.
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000165 unsigned IdentifierNamespace : 8;
Chris Lattner769dbdf2009-03-27 20:18:19 +0000166
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000167private:
Anders Carlsson1329c272009-03-25 23:38:06 +0000168#ifndef NDEBUG
169 void CheckAccessDeclContext() const;
170#else
171 void CheckAccessDeclContext() const { }
172#endif
173
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000174protected:
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000175 /// Access - Used by C++ decls for the access specifier.
176 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
177 unsigned Access : 2;
178 friend class CXXClassMemberWrapper;
179
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000180 Decl(Kind DK, DeclContext *DC, SourceLocation L)
Chris Lattner682bf922009-03-29 16:50:03 +0000181 : NextDeclInContext(0), DeclCtx(DC),
Daniel Dunbar39d76502009-03-04 02:26:41 +0000182 Loc(L), DeclKind(DK), InvalidDecl(0),
Douglas Gregore0762c92009-06-19 23:52:42 +0000183 HasAttrs(false), Implicit(false), Used(false),
Chris Lattner769dbdf2009-03-27 20:18:19 +0000184 IdentifierNamespace(getIdentifierNamespaceForKind(DK)), Access(AS_none) {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000185 if (Decl::CollectingStats()) addDeclKind(DK);
186 }
Sam Bishop1bb19632008-04-11 18:04:39 +0000187
Chris Lattner0ed844b2008-04-04 06:12:32 +0000188 virtual ~Decl();
Sam Bishop1bb19632008-04-11 18:04:39 +0000189
190public:
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000191
192 /// \brief Source range that this declaration covers.
193 virtual SourceRange getSourceRange() const {
194 return SourceRange(getLocation(), getLocation());
195 }
196 SourceLocation getLocStart() const { return getSourceRange().getBegin(); }
197 SourceLocation getLocEnd() const { return getSourceRange().getEnd(); }
198
Chris Lattner0ed844b2008-04-04 06:12:32 +0000199 SourceLocation getLocation() const { return Loc; }
200 void setLocation(SourceLocation L) { Loc = L; }
201
202 Kind getKind() const { return DeclKind; }
203 const char *getDeclKindName() const;
204
Chris Lattner244a67d2009-03-28 06:04:26 +0000205 Decl *getNextDeclInContext() { return NextDeclInContext; }
206 const Decl *getNextDeclInContext() const { return NextDeclInContext; }
Chris Lattner96f44682009-03-28 05:59:45 +0000207
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000208 DeclContext *getDeclContext() {
Chris Lattner10d83792009-03-27 18:46:15 +0000209 if (isInSemaDC())
210 return getSemanticDC();
211 return getMultipleDC()->SemanticDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000212 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000213 const DeclContext *getDeclContext() const {
214 return const_cast<Decl*>(this)->getDeclContext();
215 }
216
Anders Carlsson1329c272009-03-25 23:38:06 +0000217 void setAccess(AccessSpecifier AS) {
Anders Carlssonb8547e82009-03-25 20:19:57 +0000218 Access = AS;
Anders Carlsson1329c272009-03-25 23:38:06 +0000219 CheckAccessDeclContext();
Anders Carlssonb8547e82009-03-25 20:19:57 +0000220 }
Anders Carlsson1329c272009-03-25 23:38:06 +0000221
222 AccessSpecifier getAccess() const {
223 CheckAccessDeclContext();
224 return AccessSpecifier(Access);
225 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000226
Chris Lattner76a642f2009-02-15 22:43:40 +0000227 bool hasAttrs() const { return HasAttrs; }
Douglas Gregor68584ed2009-06-18 16:11:24 +0000228 void addAttr(ASTContext &Context, Attr *attr);
229 const Attr *getAttrs(ASTContext &Context) const {
Chris Lattner81abbdd2009-03-21 06:27:31 +0000230 if (!HasAttrs) return 0; // common case, no attributes.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000231 return getAttrsImpl(Context); // Uncommon case, out of line hash lookup.
Chris Lattner81abbdd2009-03-21 06:27:31 +0000232 }
Douglas Gregor68584ed2009-06-18 16:11:24 +0000233 void swapAttrs(ASTContext &Context, Decl *D);
234 void invalidateAttrs(ASTContext &Context);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000235
Douglas Gregor68584ed2009-06-18 16:11:24 +0000236 template<typename T> const T *getAttr(ASTContext &Context) const {
237 for (const Attr *attr = getAttrs(Context); attr; attr = attr->getNext())
Chris Lattner0ed844b2008-04-04 06:12:32 +0000238 if (const T *V = dyn_cast<T>(attr))
239 return V;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000240 return 0;
241 }
242
Douglas Gregor68584ed2009-06-18 16:11:24 +0000243 template<typename T> bool hasAttr(ASTContext &Context) const {
244 return getAttr<T>(Context) != 0;
Chris Lattner115cafc2009-04-12 20:07:59 +0000245 }
246
Chris Lattner0ed844b2008-04-04 06:12:32 +0000247 /// setInvalidDecl - Indicates the Decl had a semantic error. This
248 /// allows for graceful error recovery.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000249 void setInvalidDecl(bool Invalid = true) { InvalidDecl = Invalid; }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000250 bool isInvalidDecl() const { return (bool) InvalidDecl; }
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000251
252 /// isImplicit - Indicates whether the declaration was implicitly
253 /// generated by the implementation. If false, this declaration
254 /// was written explicitly in the source code.
255 bool isImplicit() const { return Implicit; }
256 void setImplicit(bool I = true) { Implicit = I; }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000257
Douglas Gregore0762c92009-06-19 23:52:42 +0000258 /// \brief Whether this declaration was used, meaning that a definition
259 /// is required.
260 bool isUsed() const { return Used; }
261 void setUsed(bool U = true) { Used = U; }
262
Douglas Gregorcc636682009-02-17 23:15:12 +0000263 unsigned getIdentifierNamespace() const {
Chris Lattner769dbdf2009-03-27 20:18:19 +0000264 return IdentifierNamespace;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000265 }
Chris Lattnerd62fdc42009-01-06 07:16:40 +0000266 bool isInIdentifierNamespace(unsigned NS) const {
267 return getIdentifierNamespace() & NS;
268 }
Chris Lattner769dbdf2009-03-27 20:18:19 +0000269 static unsigned getIdentifierNamespaceForKind(Kind DK);
270
Chris Lattnerd62fdc42009-01-06 07:16:40 +0000271
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000272 /// getLexicalDeclContext - The declaration context where this Decl was
273 /// lexically declared (LexicalDC). May be different from
274 /// getDeclContext() (SemanticDC).
275 /// e.g.:
276 ///
277 /// namespace A {
278 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
279 /// }
280 /// void A::f(); // SemanticDC == namespace 'A'
281 /// // LexicalDC == global namespace
Chris Lattner10d83792009-03-27 18:46:15 +0000282 DeclContext *getLexicalDeclContext() {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000283 if (isInSemaDC())
Chris Lattner10d83792009-03-27 18:46:15 +0000284 return getSemanticDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000285 return getMultipleDC()->LexicalDC;
286 }
Chris Lattner10d83792009-03-27 18:46:15 +0000287 const DeclContext *getLexicalDeclContext() const {
288 return const_cast<Decl*>(this)->getLexicalDeclContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000289 }
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000290
291 bool isOutOfLine() const {
292 return getLexicalDeclContext() != getDeclContext();
293 }
Chris Lattner10d83792009-03-27 18:46:15 +0000294
Douglas Gregor6ab35242009-04-09 21:40:53 +0000295 /// setDeclContext - Set both the semantic and lexical DeclContext
296 /// to DC.
297 void setDeclContext(DeclContext *DC);
298
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000299 void setLexicalDeclContext(DeclContext *DC);
300
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000301 // isDefinedOutsideFunctionOrMethod - This predicate returns true if this
302 // scoped decl is defined outside the current function or method. This is
303 // roughly global variables and functions, but also handles enums (which could
304 // be defined inside or outside a function etc).
305 bool isDefinedOutsideFunctionOrMethod() const;
306
Sebastian Redld3a413d2009-04-26 20:35:05 +0000307 /// getBody - If this Decl represents a declaration for a body of code,
308 /// such as a function or method definition, this method returns the
309 /// top-level Stmt* of that body. Otherwise this method returns null.
310 virtual Stmt* getBody(ASTContext &Context) const { return 0; }
311
312 /// getCompoundBody - Returns getBody(), dyn_casted to a CompoundStmt.
313 CompoundStmt* getCompoundBody(ASTContext &Context) const;
314
315 /// getBodyRBrace - Gets the right brace of the body, if a body exists.
316 /// This works whether the body is a CompoundStmt or a CXXTryStmt.
317 SourceLocation getBodyRBrace(ASTContext &Context) const;
318
Chris Lattner0ed844b2008-04-04 06:12:32 +0000319 // global temp stats (until we have a per-module visitor)
320 static void addDeclKind(Kind k);
321 static bool CollectingStats(bool Enable = false);
322 static void PrintStats();
323
Anders Carlsson67e33202009-06-13 00:08:58 +0000324 /// isTemplateParameter - Determines whether this declaration is a
Douglas Gregorf57172b2008-12-08 18:40:42 +0000325 /// template parameter.
326 bool isTemplateParameter() const;
Anders Carlsson67e33202009-06-13 00:08:58 +0000327
328 /// isTemplateParameter - Determines whether this declaration is a
329 /// template parameter pack.
330 bool isTemplateParameterPack() const;
Douglas Gregorf57172b2008-12-08 18:40:42 +0000331
Douglas Gregore53060f2009-06-25 22:08:12 +0000332 /// \brief Whether this declaration is a function or function template.
333 bool isFunctionOrFunctionTemplate() const;
334
Chris Lattner0ed844b2008-04-04 06:12:32 +0000335 // Implement isa/cast/dyncast/etc.
336 static bool classof(const Decl *) { return true; }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000337 static DeclContext *castToDeclContext(const Decl *);
338 static Decl *castFromDeclContext(const DeclContext *);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000339
Sam Bishopbb45c512008-04-11 15:01:25 +0000340 /// Destroy - Call destructors and release memory.
Ted Kremenek27f8a282008-05-20 00:43:19 +0000341 virtual void Destroy(ASTContext& C);
Sam Bishopbb45c512008-04-11 15:01:25 +0000342
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000343 void print(llvm::raw_ostream &Out, ASTContext &Context,
344 unsigned Indentation = 0);
345 void print(llvm::raw_ostream &Out, ASTContext &Context,
346 const PrintingPolicy &Policy, unsigned Indentation = 0);
Eli Friedman42f42c02009-05-30 04:20:30 +0000347 static void printGroup(Decl** Begin, unsigned NumDecls,
348 llvm::raw_ostream &Out, ASTContext &Context,
349 const PrintingPolicy &Policy,
350 unsigned Indentation = 0);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000351 void dump(ASTContext &Context);
352
Chris Lattner81abbdd2009-03-21 06:27:31 +0000353private:
Douglas Gregor68584ed2009-06-18 16:11:24 +0000354 const Attr *getAttrsImpl(ASTContext &Context) const;
Chris Lattner81abbdd2009-03-21 06:27:31 +0000355
Chris Lattner0ed844b2008-04-04 06:12:32 +0000356};
357
Chris Lattner49f28ca2009-03-05 08:00:35 +0000358/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when
359/// doing something to a specific decl.
360class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
361 Decl *TheDecl;
362 SourceLocation Loc;
363 SourceManager &SM;
364 const char *Message;
365public:
366 PrettyStackTraceDecl(Decl *theDecl, SourceLocation L,
367 SourceManager &sm, const char *Msg)
368 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {}
369
370 virtual void print(llvm::raw_ostream &OS) const;
371};
372
373
Chris Lattnerb048c982008-04-06 04:47:34 +0000374/// DeclContext - This is used only as base class of specific decl types that
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000375/// can act as declaration contexts. These decls are (only the top classes
376/// that directly derive from DeclContext are mentioned, not their subclasses):
Chris Lattner0ed844b2008-04-04 06:12:32 +0000377///
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000378/// TranslationUnitDecl
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000379/// NamespaceDecl
Chris Lattner0ed844b2008-04-04 06:12:32 +0000380/// FunctionDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000381/// TagDecl
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000382/// ObjCMethodDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000383/// ObjCContainerDecl
384/// ObjCCategoryImplDecl
385/// ObjCImplementationDecl
Douglas Gregor074149e2009-01-05 19:45:36 +0000386/// LinkageSpecDecl
Steve Naroff56ee6892008-10-08 17:01:13 +0000387/// BlockDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000388///
Chris Lattnerb048c982008-04-06 04:47:34 +0000389class DeclContext {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000390 /// DeclKind - This indicates which class this is.
391 Decl::Kind DeclKind : 8;
392
Douglas Gregor2cf26342009-04-09 22:27:44 +0000393 /// \brief Whether this declaration context also has some external
394 /// storage that contains additional declarations that are lexically
395 /// part of this context.
396 mutable bool ExternalLexicalStorage : 1;
397
398 /// \brief Whether this declaration context also has some external
399 /// storage that contains additional declarations that are visible
400 /// in this context.
401 mutable bool ExternalVisibleStorage : 1;
402
Douglas Gregorc36c5402009-04-09 17:29:08 +0000403 /// \brief Pointer to the data structure used to lookup declarations
404 /// within this context, which is a DenseMap<DeclarationName,
405 /// StoredDeclsList>.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000406 mutable void* LookupPtr;
Douglas Gregor44b43212008-12-11 16:49:14 +0000407
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000408 /// FirstDecl - The first declaration stored within this declaration
409 /// context.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000410 mutable Decl *FirstDecl;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000411
412 /// LastDecl - The last declaration stored within this declaration
413 /// context. FIXME: We could probably cache this value somewhere
414 /// outside of the DeclContext, to reduce the size of DeclContext by
415 /// another pointer.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000416 mutable Decl *LastDecl;
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000417
Chris Lattner0ed844b2008-04-04 06:12:32 +0000418protected:
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000419 DeclContext(Decl::Kind K)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000420 : DeclKind(K), ExternalLexicalStorage(false),
421 ExternalVisibleStorage(false), LookupPtr(0), FirstDecl(0),
422 LastDecl(0) { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000423
424 void DestroyDecls(ASTContext &C);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000425
426public:
Douglas Gregor44b43212008-12-11 16:49:14 +0000427 ~DeclContext();
428
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000429 Decl::Kind getDeclKind() const {
430 return DeclKind;
431 }
Steve Naroff0a473932009-01-20 19:53:53 +0000432 const char *getDeclKindName() const;
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000433
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +0000434 /// getParent - Returns the containing DeclContext.
Chris Lattner0cf2b192009-03-27 19:19:59 +0000435 DeclContext *getParent() {
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +0000436 return cast<Decl>(this)->getDeclContext();
437 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000438 const DeclContext *getParent() const {
439 return const_cast<DeclContext*>(this)->getParent();
Argyrios Kyrtzidisd2595ec2008-10-12 18:40:01 +0000440 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000441
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000442 /// getLexicalParent - Returns the containing lexical DeclContext. May be
Douglas Gregor44b43212008-12-11 16:49:14 +0000443 /// different from getParent, e.g.:
444 ///
445 /// namespace A {
446 /// struct S;
447 /// }
448 /// struct A::S {}; // getParent() == namespace 'A'
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000449 /// // getLexicalParent() == translation unit
450 ///
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000451 DeclContext *getLexicalParent() {
Chris Lattner0cf2b192009-03-27 19:19:59 +0000452 return cast<Decl>(this)->getLexicalDeclContext();
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000453 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000454 const DeclContext *getLexicalParent() const {
455 return const_cast<DeclContext*>(this)->getLexicalParent();
456 }
457
Chris Lattner0ed844b2008-04-04 06:12:32 +0000458 bool isFunctionOrMethod() const {
459 switch (DeclKind) {
Chris Lattner0cf2b192009-03-27 19:19:59 +0000460 case Decl::Block:
461 case Decl::ObjCMethod:
462 return true;
463 default:
464 return DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000465 }
466 }
467
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000468 bool isFileContext() const {
469 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
470 }
471
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000472 bool isTranslationUnit() const {
473 return DeclKind == Decl::TranslationUnit;
474 }
475
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000476 bool isRecord() const {
Douglas Gregor65100792009-02-26 00:02:51 +0000477 return DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast;
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +0000478 }
479
Douglas Gregor44b43212008-12-11 16:49:14 +0000480 bool isNamespace() const {
481 return DeclKind == Decl::Namespace;
482 }
483
Douglas Gregorbc221632009-05-28 16:34:51 +0000484 /// \brief Determines whether this context is dependent on a
485 /// template parameter.
486 bool isDependentContext() const;
487
Douglas Gregor074149e2009-01-05 19:45:36 +0000488 /// isTransparentContext - Determines whether this context is a
489 /// "transparent" context, meaning that the members declared in this
490 /// context are semantically declared in the nearest enclosing
491 /// non-transparent (opaque) context but are lexically declared in
492 /// this context. For example, consider the enumerators of an
493 /// enumeration type:
494 /// @code
495 /// enum E {
496 /// Val1
497 /// };
498 /// @endcode
499 /// Here, E is a transparent context, so its enumerator (Val1) will
500 /// appear (semantically) that it is in the same context of E.
501 /// Examples of transparent contexts include: enumerations (except for
502 /// C++0x scoped enums), C++ linkage specifications, and C++0x
503 /// inline namespaces.
504 bool isTransparentContext() const;
505
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000506 bool Encloses(DeclContext *DC) const {
507 for (; DC; DC = DC->getParent())
508 if (DC == this)
509 return true;
510 return false;
511 }
512
Douglas Gregor44b43212008-12-11 16:49:14 +0000513 /// getPrimaryContext - There may be many different
514 /// declarations of the same entity (including forward declarations
515 /// of classes, multiple definitions of namespaces, etc.), each with
516 /// a different set of declarations. This routine returns the
517 /// "primary" DeclContext structure, which will contain the
518 /// information needed to perform name lookup into this context.
Steve Naroff0701bbb2009-01-08 17:28:14 +0000519 DeclContext *getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000520
Douglas Gregorce356072009-01-06 23:51:29 +0000521 /// getLookupContext - Retrieve the innermost non-transparent
522 /// context of this context, which corresponds to the innermost
523 /// location from which name lookup can find the entities in this
524 /// context.
Chris Lattner0cf2b192009-03-27 19:19:59 +0000525 DeclContext *getLookupContext();
526 const DeclContext *getLookupContext() const {
527 return const_cast<DeclContext *>(this)->getLookupContext();
Douglas Gregor17a9b9e2009-01-07 02:48:43 +0000528 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000529
Douglas Gregor88b70942009-02-25 22:02:03 +0000530 /// \brief Retrieve the nearest enclosing namespace context.
531 DeclContext *getEnclosingNamespaceContext();
532 const DeclContext *getEnclosingNamespaceContext() const {
533 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
534 }
535
Douglas Gregor44b43212008-12-11 16:49:14 +0000536 /// getNextContext - If this is a DeclContext that may have other
537 /// DeclContexts that are semantically connected but syntactically
538 /// different, such as C++ namespaces, this routine retrieves the
539 /// next DeclContext in the link. Iteration through the chain of
540 /// DeclContexts should begin at the primary DeclContext and
541 /// continue until this function returns NULL. For example, given:
542 /// @code
543 /// namespace N {
544 /// int x;
545 /// }
546 /// namespace N {
547 /// int y;
548 /// }
549 /// @endcode
550 /// The first occurrence of namespace N will be the primary
551 /// DeclContext. Its getNextContext will return the second
552 /// occurrence of namespace N.
553 DeclContext *getNextContext();
554
555 /// decl_iterator - Iterates through the declarations stored
556 /// within this context.
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000557 class decl_iterator {
558 /// Current - The current declaration.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000559 Decl *Current;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000560
561 public:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000562 typedef Decl* value_type;
563 typedef Decl* reference;
564 typedef Decl* pointer;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000565 typedef std::forward_iterator_tag iterator_category;
566 typedef std::ptrdiff_t difference_type;
567
568 decl_iterator() : Current(0) { }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000569 explicit decl_iterator(Decl *C) : Current(C) { }
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000570
571 reference operator*() const { return Current; }
572 pointer operator->() const { return Current; }
573
Chris Lattner96f44682009-03-28 05:59:45 +0000574 decl_iterator& operator++() {
Chris Lattner244a67d2009-03-28 06:04:26 +0000575 Current = Current->getNextDeclInContext();
Chris Lattner96f44682009-03-28 05:59:45 +0000576 return *this;
577 }
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000578
579 decl_iterator operator++(int) {
580 decl_iterator tmp(*this);
581 ++(*this);
582 return tmp;
583 }
584
585 friend bool operator==(decl_iterator x, decl_iterator y) {
586 return x.Current == y.Current;
587 }
588 friend bool operator!=(decl_iterator x, decl_iterator y) {
589 return x.Current != y.Current;
590 }
591 };
Douglas Gregor44b43212008-12-11 16:49:14 +0000592
Douglas Gregor44b43212008-12-11 16:49:14 +0000593 /// decls_begin/decls_end - Iterate over the declarations stored in
594 /// this context.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000595 decl_iterator decls_begin(ASTContext &Context) const;
596 decl_iterator decls_end(ASTContext &Context) const;
Douglas Gregor8038d512009-04-10 17:25:41 +0000597 bool decls_empty(ASTContext &Context) const;
Douglas Gregor44b43212008-12-11 16:49:14 +0000598
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000599 /// specific_decl_iterator - Iterates over a subrange of
600 /// declarations stored in a DeclContext, providing only those that
Douglas Gregor669c9a22009-02-02 18:25:48 +0000601 /// are of type SpecificDecl (or a class derived from it). This
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000602 /// iterator is used, for example, to provide iteration over just
Douglas Gregor669c9a22009-02-02 18:25:48 +0000603 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000604 template<typename SpecificDecl>
605 class specific_decl_iterator {
606 /// Current - The current, underlying declaration iterator, which
Douglas Gregord6f0b4e2009-02-02 17:56:05 +0000607 /// will either be NULL or will point to a declaration of
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000608 /// type SpecificDecl.
609 DeclContext::decl_iterator Current;
610
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000611 /// SkipToNextDecl - Advances the current position up to the next
612 /// declaration of type SpecificDecl that also meets the criteria
613 /// required by Acceptable.
614 void SkipToNextDecl() {
Douglas Gregor669c9a22009-02-02 18:25:48 +0000615 while (*Current && !isa<SpecificDecl>(*Current))
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000616 ++Current;
617 }
618
619 public:
620 typedef SpecificDecl* value_type;
621 typedef SpecificDecl* reference;
622 typedef SpecificDecl* pointer;
623 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
624 difference_type;
625 typedef std::forward_iterator_tag iterator_category;
626
Douglas Gregor669c9a22009-02-02 18:25:48 +0000627 specific_decl_iterator() : Current() { }
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000628
629 /// specific_decl_iterator - Construct a new iterator over a
Douglas Gregord6f0b4e2009-02-02 17:56:05 +0000630 /// subset of the declarations the range [C,
631 /// end-of-declarations). If A is non-NULL, it is a pointer to a
632 /// member function of SpecificDecl that should return true for
633 /// all of the SpecificDecl instances that will be in the subset
634 /// of iterators. For example, if you want Objective-C instance
635 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
636 /// &ObjCMethodDecl::isInstanceMethod.
Douglas Gregor669c9a22009-02-02 18:25:48 +0000637 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000638 SkipToNextDecl();
639 }
640
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000641 reference operator*() const { return cast<SpecificDecl>(*Current); }
642 pointer operator->() const { return cast<SpecificDecl>(*Current); }
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000643
644 specific_decl_iterator& operator++() {
645 ++Current;
646 SkipToNextDecl();
647 return *this;
648 }
649
650 specific_decl_iterator operator++(int) {
651 specific_decl_iterator tmp(*this);
652 ++(*this);
653 return tmp;
654 }
655
656 friend bool
657 operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) {
658 return x.Current == y.Current;
659 }
660
661 friend bool
662 operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) {
663 return x.Current != y.Current;
664 }
665 };
666
Douglas Gregor669c9a22009-02-02 18:25:48 +0000667 /// \brief Iterates over a filtered subrange of declarations stored
668 /// in a DeclContext.
669 ///
670 /// This iterator visits only those declarations that are of type
671 /// SpecificDecl (or a class derived from it) and that meet some
672 /// additional run-time criteria. This iterator is used, for
673 /// example, to provide access to the instance methods within an
674 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
675 /// Acceptable = ObjCMethodDecl::isInstanceMethod).
676 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
677 class filtered_decl_iterator {
678 /// Current - The current, underlying declaration iterator, which
679 /// will either be NULL or will point to a declaration of
680 /// type SpecificDecl.
681 DeclContext::decl_iterator Current;
682
683 /// SkipToNextDecl - Advances the current position up to the next
684 /// declaration of type SpecificDecl that also meets the criteria
685 /// required by Acceptable.
686 void SkipToNextDecl() {
687 while (*Current &&
688 (!isa<SpecificDecl>(*Current) ||
689 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
690 ++Current;
691 }
692
693 public:
694 typedef SpecificDecl* value_type;
695 typedef SpecificDecl* reference;
696 typedef SpecificDecl* pointer;
697 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
698 difference_type;
699 typedef std::forward_iterator_tag iterator_category;
700
701 filtered_decl_iterator() : Current() { }
702
703 /// specific_decl_iterator - Construct a new iterator over a
704 /// subset of the declarations the range [C,
705 /// end-of-declarations). If A is non-NULL, it is a pointer to a
706 /// member function of SpecificDecl that should return true for
707 /// all of the SpecificDecl instances that will be in the subset
708 /// of iterators. For example, if you want Objective-C instance
709 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
710 /// &ObjCMethodDecl::isInstanceMethod.
711 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
712 SkipToNextDecl();
713 }
714
715 reference operator*() const { return cast<SpecificDecl>(*Current); }
716 pointer operator->() const { return cast<SpecificDecl>(*Current); }
717
718 filtered_decl_iterator& operator++() {
719 ++Current;
720 SkipToNextDecl();
721 return *this;
722 }
723
724 filtered_decl_iterator operator++(int) {
725 filtered_decl_iterator tmp(*this);
726 ++(*this);
727 return tmp;
728 }
729
730 friend bool
731 operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
732 return x.Current == y.Current;
733 }
734
735 friend bool
736 operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
737 return x.Current != y.Current;
738 }
739 };
740
Douglas Gregor40f4e692009-01-20 16:54:50 +0000741 /// @brief Add the declaration D into this context.
742 ///
743 /// This routine should be invoked when the declaration D has first
744 /// been declared, to place D into the context where it was
745 /// (lexically) defined. Every declaration must be added to one
746 /// (and only one!) context, where it can be visited via
747 /// [decls_begin(), decls_end()). Once a declaration has been added
748 /// to its lexical context, the corresponding DeclContext owns the
749 /// declaration.
750 ///
751 /// If D is also a NamedDecl, it will be made visible within its
752 /// semantic context via makeDeclVisibleInContext.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000753 void addDecl(ASTContext &Context, Decl *D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000754
Douglas Gregor44b43212008-12-11 16:49:14 +0000755 /// lookup_iterator - An iterator that provides access to the results
756 /// of looking up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000757 typedef NamedDecl **lookup_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +0000758
759 /// lookup_const_iterator - An iterator that provides non-mutable
760 /// access to the results of lookup up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000761 typedef NamedDecl * const * lookup_const_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +0000762
763 typedef std::pair<lookup_iterator, lookup_iterator> lookup_result;
764 typedef std::pair<lookup_const_iterator, lookup_const_iterator>
765 lookup_const_result;
766
767 /// lookup - Find the declarations (if any) with the given Name in
768 /// this context. Returns a range of iterators that contains all of
Douglas Gregor40f4e692009-01-20 16:54:50 +0000769 /// the declarations with this name, with object, function, member,
770 /// and enumerator names preceding any tag name. Note that this
771 /// routine will not look into parent contexts.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000772 lookup_result lookup(ASTContext &Context, DeclarationName Name);
773 lookup_const_result lookup(ASTContext &Context, DeclarationName Name) const;
Douglas Gregor44b43212008-12-11 16:49:14 +0000774
Douglas Gregor40f4e692009-01-20 16:54:50 +0000775 /// @brief Makes a declaration visible within this context.
776 ///
777 /// This routine makes the declaration D visible to name lookup
778 /// within this context and, if this is a transparent context,
779 /// within its parent contexts up to the first enclosing
780 /// non-transparent context. Making a declaration visible within a
781 /// context does not transfer ownership of a declaration, and a
782 /// declaration can be visible in many contexts that aren't its
783 /// lexical context.
784 ///
785 /// If D is a redeclaration of an existing declaration that is
786 /// visible from this context, as determined by
787 /// NamedDecl::declarationReplaces, the previous declaration will be
788 /// replaced with D.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000789 void makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D);
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000790
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000791 /// udir_iterator - Iterates through the using-directives stored
792 /// within this context.
793 typedef UsingDirectiveDecl * const * udir_iterator;
794
795 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range;
796
Douglas Gregor6ab35242009-04-09 21:40:53 +0000797 udir_iterator_range getUsingDirectives(ASTContext &Context) const;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000798
Douglas Gregor6ab35242009-04-09 21:40:53 +0000799 udir_iterator using_directives_begin(ASTContext &Context) const {
800 return getUsingDirectives(Context).first;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000801 }
802
Douglas Gregor6ab35242009-04-09 21:40:53 +0000803 udir_iterator using_directives_end(ASTContext &Context) const {
804 return getUsingDirectives(Context).second;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000805 }
806
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000807 // Low-level accessors
808
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000809 /// \brief Retrieve the internal representation of the lookup structure.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000810 void* getLookupPtr() const { return LookupPtr; }
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000811
Douglas Gregor2cf26342009-04-09 22:27:44 +0000812 /// \brief Whether this DeclContext has external storage containing
813 /// additional declarations that are lexically in this context.
814 bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; }
815
816 /// \brief State whether this DeclContext has external storage for
817 /// declarations lexically in this context.
818 void setHasExternalLexicalStorage(bool ES = true) {
819 ExternalLexicalStorage = ES;
820 }
821
822 /// \brief Whether this DeclContext has external storage containing
823 /// additional declarations that are visible in this context.
824 bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; }
825
826 /// \brief State whether this DeclContext has external storage for
827 /// declarations visible in this context.
828 void setHasExternalVisibleStorage(bool ES = true) {
829 ExternalVisibleStorage = ES;
830 }
831
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000832 static bool classof(const Decl *D);
Chris Lattnerb048c982008-04-06 04:47:34 +0000833 static bool classof(const DeclContext *D) { return true; }
Douglas Gregor64650af2009-02-02 23:39:07 +0000834#define DECL_CONTEXT(Name) \
835 static bool classof(const Name##Decl *D) { return true; }
836#include "clang/AST/DeclNodes.def"
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000837
838private:
Douglas Gregor2cf26342009-04-09 22:27:44 +0000839 void LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const;
840 void LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const;
841
Douglas Gregor6ab35242009-04-09 21:40:53 +0000842 void buildLookup(ASTContext &Context, DeclContext *DCtx);
843 void makeDeclVisibleInContextImpl(ASTContext &Context, NamedDecl *D);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000844};
845
Douglas Gregorf57172b2008-12-08 18:40:42 +0000846inline bool Decl::isTemplateParameter() const {
847 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm;
848}
849
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000850inline bool Decl::isDefinedOutsideFunctionOrMethod() const {
851 if (getDeclContext())
852 return !getDeclContext()->getLookupContext()->isFunctionOrMethod();
Chris Lattner96f44682009-03-28 05:59:45 +0000853 return true;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000854}
855
Chris Lattner0ed844b2008-04-04 06:12:32 +0000856} // end clang.
857
858namespace llvm {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000859
860/// Implement a isa_impl_wrap specialization to check whether a DeclContext is
861/// a specific Decl.
862template<class ToTy>
863struct isa_impl_wrap<ToTy,
864 const ::clang::DeclContext,const ::clang::DeclContext> {
865 static bool doit(const ::clang::DeclContext &Val) {
866 return ToTy::classof(::clang::Decl::castFromDeclContext(&Val));
Chris Lattner0ed844b2008-04-04 06:12:32 +0000867 }
868};
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000869template<class ToTy>
870struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext>
871 : public isa_impl_wrap<ToTy,
872 const ::clang::DeclContext,const ::clang::DeclContext> {};
Chris Lattner0ed844b2008-04-04 06:12:32 +0000873
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000874/// Implement cast_convert_val for Decl -> DeclContext conversions.
Chris Lattner0ed844b2008-04-04 06:12:32 +0000875template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000876struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
Chris Lattnerb048c982008-04-06 04:47:34 +0000877 static ::clang::DeclContext &doit(const FromTy &Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000878 return *FromTy::castToDeclContext(&Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000879 }
880};
Chris Lattner0ed844b2008-04-04 06:12:32 +0000881
882template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000883struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
Chris Lattnerb048c982008-04-06 04:47:34 +0000884 static ::clang::DeclContext *doit(const FromTy *Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000885 return FromTy::castToDeclContext(Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000886 }
887};
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000888
Douglas Gregor44b43212008-12-11 16:49:14 +0000889template<class FromTy>
890struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
891 static const ::clang::DeclContext &doit(const FromTy &Val) {
892 return *FromTy::castToDeclContext(&Val);
893 }
894};
895
896template<class FromTy>
897struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
898 static const ::clang::DeclContext *doit(const FromTy *Val) {
899 return FromTy::castToDeclContext(Val);
900 }
901};
902
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000903/// Implement cast_convert_val for DeclContext -> Decl conversions.
904template<class ToTy>
905struct cast_convert_val<ToTy,
906 const ::clang::DeclContext,const ::clang::DeclContext> {
907 static ToTy &doit(const ::clang::DeclContext &Val) {
908 return *reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(&Val));
909 }
910};
911template<class ToTy>
912struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext>
913 : public cast_convert_val<ToTy,
914 const ::clang::DeclContext,const ::clang::DeclContext> {};
915
916template<class ToTy>
917struct cast_convert_val<ToTy,
918 const ::clang::DeclContext*, const ::clang::DeclContext*> {
919 static ToTy *doit(const ::clang::DeclContext *Val) {
920 return reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(Val));
921 }
922};
923template<class ToTy>
924struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*>
925 : public cast_convert_val<ToTy,
926 const ::clang::DeclContext*,const ::clang::DeclContext*> {};
Chris Lattner0ed844b2008-04-04 06:12:32 +0000927
928} // end namespace llvm
929
930#endif