blob: 271e59aadc3df4e2a91503fad54812c8a21c4cf1 [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
Chris Lattner769dbdf2009-03-27 20:18:19 +0000159 /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in.
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000160 unsigned IdentifierNamespace : 8;
Chris Lattner769dbdf2009-03-27 20:18:19 +0000161
Anders Carlsson1329c272009-03-25 23:38:06 +0000162#ifndef NDEBUG
163 void CheckAccessDeclContext() const;
164#else
165 void CheckAccessDeclContext() const { }
166#endif
167
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000168protected:
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000169 /// Access - Used by C++ decls for the access specifier.
170 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
171 unsigned Access : 2;
172 friend class CXXClassMemberWrapper;
173
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000174 Decl(Kind DK, DeclContext *DC, SourceLocation L)
Chris Lattner682bf922009-03-29 16:50:03 +0000175 : NextDeclInContext(0), DeclCtx(DC),
Daniel Dunbar39d76502009-03-04 02:26:41 +0000176 Loc(L), DeclKind(DK), InvalidDecl(0),
Chris Lattner769dbdf2009-03-27 20:18:19 +0000177 HasAttrs(false), Implicit(false),
178 IdentifierNamespace(getIdentifierNamespaceForKind(DK)), Access(AS_none) {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000179 if (Decl::CollectingStats()) addDeclKind(DK);
180 }
Sam Bishop1bb19632008-04-11 18:04:39 +0000181
Chris Lattner0ed844b2008-04-04 06:12:32 +0000182 virtual ~Decl();
Sam Bishop1bb19632008-04-11 18:04:39 +0000183
184public:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000185 SourceLocation getLocation() const { return Loc; }
186 void setLocation(SourceLocation L) { Loc = L; }
187
188 Kind getKind() const { return DeclKind; }
189 const char *getDeclKindName() const;
190
Chris Lattner244a67d2009-03-28 06:04:26 +0000191 Decl *getNextDeclInContext() { return NextDeclInContext; }
192 const Decl *getNextDeclInContext() const { return NextDeclInContext; }
Chris Lattner96f44682009-03-28 05:59:45 +0000193
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000194 DeclContext *getDeclContext() {
Chris Lattner10d83792009-03-27 18:46:15 +0000195 if (isInSemaDC())
196 return getSemanticDC();
197 return getMultipleDC()->SemanticDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000198 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000199 const DeclContext *getDeclContext() const {
200 return const_cast<Decl*>(this)->getDeclContext();
201 }
202
Anders Carlsson1329c272009-03-25 23:38:06 +0000203 void setAccess(AccessSpecifier AS) {
Anders Carlssonb8547e82009-03-25 20:19:57 +0000204 Access = AS;
Anders Carlsson1329c272009-03-25 23:38:06 +0000205 CheckAccessDeclContext();
Anders Carlssonb8547e82009-03-25 20:19:57 +0000206 }
Anders Carlsson1329c272009-03-25 23:38:06 +0000207
208 AccessSpecifier getAccess() const {
209 CheckAccessDeclContext();
210 return AccessSpecifier(Access);
211 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000212
Chris Lattner76a642f2009-02-15 22:43:40 +0000213 bool hasAttrs() const { return HasAttrs; }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000214 void addAttr(Attr *attr);
Chris Lattner81abbdd2009-03-21 06:27:31 +0000215 const Attr *getAttrs() const {
216 if (!HasAttrs) return 0; // common case, no attributes.
217 return getAttrsImpl(); // Uncommon case, out of line hash lookup.
218 }
Chris Lattnera212c562008-05-04 02:29:49 +0000219 void swapAttrs(Decl *D);
Nuno Lopes9141bee2008-06-01 22:53:53 +0000220 void invalidateAttrs();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000221
222 template<typename T> const T *getAttr() const {
223 for (const Attr *attr = getAttrs(); attr; attr = attr->getNext())
224 if (const T *V = dyn_cast<T>(attr))
225 return V;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000226 return 0;
227 }
228
Chris Lattner115cafc2009-04-12 20:07:59 +0000229 template<typename T> bool hasAttr() const {
230 return getAttr<T>() != 0;
231 }
232
Chris Lattner0ed844b2008-04-04 06:12:32 +0000233 /// setInvalidDecl - Indicates the Decl had a semantic error. This
234 /// allows for graceful error recovery.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000235 void setInvalidDecl(bool Invalid = true) { InvalidDecl = Invalid; }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000236 bool isInvalidDecl() const { return (bool) InvalidDecl; }
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000237
238 /// isImplicit - Indicates whether the declaration was implicitly
239 /// generated by the implementation. If false, this declaration
240 /// was written explicitly in the source code.
241 bool isImplicit() const { return Implicit; }
242 void setImplicit(bool I = true) { Implicit = I; }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000243
Douglas Gregorcc636682009-02-17 23:15:12 +0000244 unsigned getIdentifierNamespace() const {
Chris Lattner769dbdf2009-03-27 20:18:19 +0000245 return IdentifierNamespace;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000246 }
Chris Lattnerd62fdc42009-01-06 07:16:40 +0000247 bool isInIdentifierNamespace(unsigned NS) const {
248 return getIdentifierNamespace() & NS;
249 }
Chris Lattner769dbdf2009-03-27 20:18:19 +0000250 static unsigned getIdentifierNamespaceForKind(Kind DK);
251
Chris Lattnerd62fdc42009-01-06 07:16:40 +0000252
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000253 /// getLexicalDeclContext - The declaration context where this Decl was
254 /// lexically declared (LexicalDC). May be different from
255 /// getDeclContext() (SemanticDC).
256 /// e.g.:
257 ///
258 /// namespace A {
259 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
260 /// }
261 /// void A::f(); // SemanticDC == namespace 'A'
262 /// // LexicalDC == global namespace
Chris Lattner10d83792009-03-27 18:46:15 +0000263 DeclContext *getLexicalDeclContext() {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000264 if (isInSemaDC())
Chris Lattner10d83792009-03-27 18:46:15 +0000265 return getSemanticDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000266 return getMultipleDC()->LexicalDC;
267 }
Chris Lattner10d83792009-03-27 18:46:15 +0000268 const DeclContext *getLexicalDeclContext() const {
269 return const_cast<Decl*>(this)->getLexicalDeclContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000270 }
Chris Lattner10d83792009-03-27 18:46:15 +0000271
Douglas Gregor6ab35242009-04-09 21:40:53 +0000272 /// setDeclContext - Set both the semantic and lexical DeclContext
273 /// to DC.
274 void setDeclContext(DeclContext *DC);
275
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000276 void setLexicalDeclContext(DeclContext *DC);
277
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000278 // isDefinedOutsideFunctionOrMethod - This predicate returns true if this
279 // scoped decl is defined outside the current function or method. This is
280 // roughly global variables and functions, but also handles enums (which could
281 // be defined inside or outside a function etc).
282 bool isDefinedOutsideFunctionOrMethod() const;
283
Sebastian Redld3a413d2009-04-26 20:35:05 +0000284 /// getBody - If this Decl represents a declaration for a body of code,
285 /// such as a function or method definition, this method returns the
286 /// top-level Stmt* of that body. Otherwise this method returns null.
287 virtual Stmt* getBody(ASTContext &Context) const { return 0; }
288
289 /// getCompoundBody - Returns getBody(), dyn_casted to a CompoundStmt.
290 CompoundStmt* getCompoundBody(ASTContext &Context) const;
291
292 /// getBodyRBrace - Gets the right brace of the body, if a body exists.
293 /// This works whether the body is a CompoundStmt or a CXXTryStmt.
294 SourceLocation getBodyRBrace(ASTContext &Context) const;
295
Chris Lattner0ed844b2008-04-04 06:12:32 +0000296 // global temp stats (until we have a per-module visitor)
297 static void addDeclKind(Kind k);
298 static bool CollectingStats(bool Enable = false);
299 static void PrintStats();
300
Douglas Gregorf57172b2008-12-08 18:40:42 +0000301 /// isTemplateParameter - Determines whether this declartion is a
302 /// template parameter.
303 bool isTemplateParameter() const;
304
Chris Lattner0ed844b2008-04-04 06:12:32 +0000305 // Implement isa/cast/dyncast/etc.
306 static bool classof(const Decl *) { return true; }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000307 static DeclContext *castToDeclContext(const Decl *);
308 static Decl *castFromDeclContext(const DeclContext *);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000309
Sam Bishopbb45c512008-04-11 15:01:25 +0000310 /// Destroy - Call destructors and release memory.
Ted Kremenek27f8a282008-05-20 00:43:19 +0000311 virtual void Destroy(ASTContext& C);
Sam Bishopbb45c512008-04-11 15:01:25 +0000312
Chris Lattner81abbdd2009-03-21 06:27:31 +0000313private:
314 const Attr *getAttrsImpl() const;
315
Chris Lattner0ed844b2008-04-04 06:12:32 +0000316};
317
Chris Lattner49f28ca2009-03-05 08:00:35 +0000318/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when
319/// doing something to a specific decl.
320class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
321 Decl *TheDecl;
322 SourceLocation Loc;
323 SourceManager &SM;
324 const char *Message;
325public:
326 PrettyStackTraceDecl(Decl *theDecl, SourceLocation L,
327 SourceManager &sm, const char *Msg)
328 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {}
329
330 virtual void print(llvm::raw_ostream &OS) const;
331};
332
333
Chris Lattnerb048c982008-04-06 04:47:34 +0000334/// DeclContext - This is used only as base class of specific decl types that
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000335/// can act as declaration contexts. These decls are (only the top classes
336/// that directly derive from DeclContext are mentioned, not their subclasses):
Chris Lattner0ed844b2008-04-04 06:12:32 +0000337///
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000338/// TranslationUnitDecl
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000339/// NamespaceDecl
Chris Lattner0ed844b2008-04-04 06:12:32 +0000340/// FunctionDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000341/// TagDecl
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000342/// ObjCMethodDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000343/// ObjCContainerDecl
344/// ObjCCategoryImplDecl
345/// ObjCImplementationDecl
Douglas Gregor074149e2009-01-05 19:45:36 +0000346/// LinkageSpecDecl
Steve Naroff56ee6892008-10-08 17:01:13 +0000347/// BlockDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000348///
Chris Lattnerb048c982008-04-06 04:47:34 +0000349class DeclContext {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000350 /// DeclKind - This indicates which class this is.
351 Decl::Kind DeclKind : 8;
352
Douglas Gregor2cf26342009-04-09 22:27:44 +0000353 /// \brief Whether this declaration context also has some external
354 /// storage that contains additional declarations that are lexically
355 /// part of this context.
356 mutable bool ExternalLexicalStorage : 1;
357
358 /// \brief Whether this declaration context also has some external
359 /// storage that contains additional declarations that are visible
360 /// in this context.
361 mutable bool ExternalVisibleStorage : 1;
362
Douglas Gregorc36c5402009-04-09 17:29:08 +0000363 /// \brief Pointer to the data structure used to lookup declarations
364 /// within this context, which is a DenseMap<DeclarationName,
365 /// StoredDeclsList>.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000366 mutable void* LookupPtr;
Douglas Gregor44b43212008-12-11 16:49:14 +0000367
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000368 /// FirstDecl - The first declaration stored within this declaration
369 /// context.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000370 mutable Decl *FirstDecl;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000371
372 /// LastDecl - The last declaration stored within this declaration
373 /// context. FIXME: We could probably cache this value somewhere
374 /// outside of the DeclContext, to reduce the size of DeclContext by
375 /// another pointer.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000376 mutable Decl *LastDecl;
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000377
Chris Lattner0ed844b2008-04-04 06:12:32 +0000378protected:
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000379 DeclContext(Decl::Kind K)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000380 : DeclKind(K), ExternalLexicalStorage(false),
381 ExternalVisibleStorage(false), LookupPtr(0), FirstDecl(0),
382 LastDecl(0) { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000383
384 void DestroyDecls(ASTContext &C);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000385
386public:
Douglas Gregor44b43212008-12-11 16:49:14 +0000387 ~DeclContext();
388
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000389 Decl::Kind getDeclKind() const {
390 return DeclKind;
391 }
Steve Naroff0a473932009-01-20 19:53:53 +0000392 const char *getDeclKindName() const;
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000393
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +0000394 /// getParent - Returns the containing DeclContext.
Chris Lattner0cf2b192009-03-27 19:19:59 +0000395 DeclContext *getParent() {
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +0000396 return cast<Decl>(this)->getDeclContext();
397 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000398 const DeclContext *getParent() const {
399 return const_cast<DeclContext*>(this)->getParent();
Argyrios Kyrtzidisd2595ec2008-10-12 18:40:01 +0000400 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000401
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000402 /// getLexicalParent - Returns the containing lexical DeclContext. May be
Douglas Gregor44b43212008-12-11 16:49:14 +0000403 /// different from getParent, e.g.:
404 ///
405 /// namespace A {
406 /// struct S;
407 /// }
408 /// struct A::S {}; // getParent() == namespace 'A'
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000409 /// // getLexicalParent() == translation unit
410 ///
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000411 DeclContext *getLexicalParent() {
Chris Lattner0cf2b192009-03-27 19:19:59 +0000412 return cast<Decl>(this)->getLexicalDeclContext();
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000413 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000414 const DeclContext *getLexicalParent() const {
415 return const_cast<DeclContext*>(this)->getLexicalParent();
416 }
417
Chris Lattner0ed844b2008-04-04 06:12:32 +0000418 bool isFunctionOrMethod() const {
419 switch (DeclKind) {
Chris Lattner0cf2b192009-03-27 19:19:59 +0000420 case Decl::Block:
421 case Decl::ObjCMethod:
422 return true;
423 default:
424 return DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000425 }
426 }
427
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000428 bool isFileContext() const {
429 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
430 }
431
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000432 bool isTranslationUnit() const {
433 return DeclKind == Decl::TranslationUnit;
434 }
435
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000436 bool isRecord() const {
Douglas Gregor65100792009-02-26 00:02:51 +0000437 return DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast;
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +0000438 }
439
Douglas Gregor44b43212008-12-11 16:49:14 +0000440 bool isNamespace() const {
441 return DeclKind == Decl::Namespace;
442 }
443
Douglas Gregorbc221632009-05-28 16:34:51 +0000444 /// \brief Determines whether this context is dependent on a
445 /// template parameter.
446 bool isDependentContext() const;
447
Douglas Gregor074149e2009-01-05 19:45:36 +0000448 /// isTransparentContext - Determines whether this context is a
449 /// "transparent" context, meaning that the members declared in this
450 /// context are semantically declared in the nearest enclosing
451 /// non-transparent (opaque) context but are lexically declared in
452 /// this context. For example, consider the enumerators of an
453 /// enumeration type:
454 /// @code
455 /// enum E {
456 /// Val1
457 /// };
458 /// @endcode
459 /// Here, E is a transparent context, so its enumerator (Val1) will
460 /// appear (semantically) that it is in the same context of E.
461 /// Examples of transparent contexts include: enumerations (except for
462 /// C++0x scoped enums), C++ linkage specifications, and C++0x
463 /// inline namespaces.
464 bool isTransparentContext() const;
465
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000466 bool Encloses(DeclContext *DC) const {
467 for (; DC; DC = DC->getParent())
468 if (DC == this)
469 return true;
470 return false;
471 }
472
Douglas Gregor44b43212008-12-11 16:49:14 +0000473 /// getPrimaryContext - There may be many different
474 /// declarations of the same entity (including forward declarations
475 /// of classes, multiple definitions of namespaces, etc.), each with
476 /// a different set of declarations. This routine returns the
477 /// "primary" DeclContext structure, which will contain the
478 /// information needed to perform name lookup into this context.
Steve Naroff0701bbb2009-01-08 17:28:14 +0000479 DeclContext *getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000480
Douglas Gregorce356072009-01-06 23:51:29 +0000481 /// getLookupContext - Retrieve the innermost non-transparent
482 /// context of this context, which corresponds to the innermost
483 /// location from which name lookup can find the entities in this
484 /// context.
Chris Lattner0cf2b192009-03-27 19:19:59 +0000485 DeclContext *getLookupContext();
486 const DeclContext *getLookupContext() const {
487 return const_cast<DeclContext *>(this)->getLookupContext();
Douglas Gregor17a9b9e2009-01-07 02:48:43 +0000488 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000489
Douglas Gregor88b70942009-02-25 22:02:03 +0000490 /// \brief Retrieve the nearest enclosing namespace context.
491 DeclContext *getEnclosingNamespaceContext();
492 const DeclContext *getEnclosingNamespaceContext() const {
493 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
494 }
495
Douglas Gregor44b43212008-12-11 16:49:14 +0000496 /// getNextContext - If this is a DeclContext that may have other
497 /// DeclContexts that are semantically connected but syntactically
498 /// different, such as C++ namespaces, this routine retrieves the
499 /// next DeclContext in the link. Iteration through the chain of
500 /// DeclContexts should begin at the primary DeclContext and
501 /// continue until this function returns NULL. For example, given:
502 /// @code
503 /// namespace N {
504 /// int x;
505 /// }
506 /// namespace N {
507 /// int y;
508 /// }
509 /// @endcode
510 /// The first occurrence of namespace N will be the primary
511 /// DeclContext. Its getNextContext will return the second
512 /// occurrence of namespace N.
513 DeclContext *getNextContext();
514
515 /// decl_iterator - Iterates through the declarations stored
516 /// within this context.
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000517 class decl_iterator {
518 /// Current - The current declaration.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000519 Decl *Current;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000520
521 public:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000522 typedef Decl* value_type;
523 typedef Decl* reference;
524 typedef Decl* pointer;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000525 typedef std::forward_iterator_tag iterator_category;
526 typedef std::ptrdiff_t difference_type;
527
528 decl_iterator() : Current(0) { }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000529 explicit decl_iterator(Decl *C) : Current(C) { }
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000530
531 reference operator*() const { return Current; }
532 pointer operator->() const { return Current; }
533
Chris Lattner96f44682009-03-28 05:59:45 +0000534 decl_iterator& operator++() {
Chris Lattner244a67d2009-03-28 06:04:26 +0000535 Current = Current->getNextDeclInContext();
Chris Lattner96f44682009-03-28 05:59:45 +0000536 return *this;
537 }
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000538
539 decl_iterator operator++(int) {
540 decl_iterator tmp(*this);
541 ++(*this);
542 return tmp;
543 }
544
545 friend bool operator==(decl_iterator x, decl_iterator y) {
546 return x.Current == y.Current;
547 }
548 friend bool operator!=(decl_iterator x, decl_iterator y) {
549 return x.Current != y.Current;
550 }
551 };
Douglas Gregor44b43212008-12-11 16:49:14 +0000552
Douglas Gregor44b43212008-12-11 16:49:14 +0000553 /// decls_begin/decls_end - Iterate over the declarations stored in
554 /// this context.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000555 decl_iterator decls_begin(ASTContext &Context) const;
556 decl_iterator decls_end(ASTContext &Context) const;
Douglas Gregor8038d512009-04-10 17:25:41 +0000557 bool decls_empty(ASTContext &Context) const;
Douglas Gregor44b43212008-12-11 16:49:14 +0000558
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000559 /// specific_decl_iterator - Iterates over a subrange of
560 /// declarations stored in a DeclContext, providing only those that
Douglas Gregor669c9a22009-02-02 18:25:48 +0000561 /// are of type SpecificDecl (or a class derived from it). This
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000562 /// iterator is used, for example, to provide iteration over just
Douglas Gregor669c9a22009-02-02 18:25:48 +0000563 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000564 template<typename SpecificDecl>
565 class specific_decl_iterator {
566 /// Current - The current, underlying declaration iterator, which
Douglas Gregord6f0b4e2009-02-02 17:56:05 +0000567 /// will either be NULL or will point to a declaration of
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000568 /// type SpecificDecl.
569 DeclContext::decl_iterator Current;
570
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000571 /// SkipToNextDecl - Advances the current position up to the next
572 /// declaration of type SpecificDecl that also meets the criteria
573 /// required by Acceptable.
574 void SkipToNextDecl() {
Douglas Gregor669c9a22009-02-02 18:25:48 +0000575 while (*Current && !isa<SpecificDecl>(*Current))
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000576 ++Current;
577 }
578
579 public:
580 typedef SpecificDecl* value_type;
581 typedef SpecificDecl* reference;
582 typedef SpecificDecl* pointer;
583 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
584 difference_type;
585 typedef std::forward_iterator_tag iterator_category;
586
Douglas Gregor669c9a22009-02-02 18:25:48 +0000587 specific_decl_iterator() : Current() { }
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000588
589 /// specific_decl_iterator - Construct a new iterator over a
Douglas Gregord6f0b4e2009-02-02 17:56:05 +0000590 /// subset of the declarations the range [C,
591 /// end-of-declarations). If A is non-NULL, it is a pointer to a
592 /// member function of SpecificDecl that should return true for
593 /// all of the SpecificDecl instances that will be in the subset
594 /// of iterators. For example, if you want Objective-C instance
595 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
596 /// &ObjCMethodDecl::isInstanceMethod.
Douglas Gregor669c9a22009-02-02 18:25:48 +0000597 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000598 SkipToNextDecl();
599 }
600
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000601 reference operator*() const { return cast<SpecificDecl>(*Current); }
602 pointer operator->() const { return cast<SpecificDecl>(*Current); }
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000603
604 specific_decl_iterator& operator++() {
605 ++Current;
606 SkipToNextDecl();
607 return *this;
608 }
609
610 specific_decl_iterator operator++(int) {
611 specific_decl_iterator tmp(*this);
612 ++(*this);
613 return tmp;
614 }
615
616 friend bool
617 operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) {
618 return x.Current == y.Current;
619 }
620
621 friend bool
622 operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) {
623 return x.Current != y.Current;
624 }
625 };
626
Douglas Gregor669c9a22009-02-02 18:25:48 +0000627 /// \brief Iterates over a filtered subrange of declarations stored
628 /// in a DeclContext.
629 ///
630 /// This iterator visits only those declarations that are of type
631 /// SpecificDecl (or a class derived from it) and that meet some
632 /// additional run-time criteria. This iterator is used, for
633 /// example, to provide access to the instance methods within an
634 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
635 /// Acceptable = ObjCMethodDecl::isInstanceMethod).
636 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
637 class filtered_decl_iterator {
638 /// Current - The current, underlying declaration iterator, which
639 /// will either be NULL or will point to a declaration of
640 /// type SpecificDecl.
641 DeclContext::decl_iterator Current;
642
643 /// SkipToNextDecl - Advances the current position up to the next
644 /// declaration of type SpecificDecl that also meets the criteria
645 /// required by Acceptable.
646 void SkipToNextDecl() {
647 while (*Current &&
648 (!isa<SpecificDecl>(*Current) ||
649 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
650 ++Current;
651 }
652
653 public:
654 typedef SpecificDecl* value_type;
655 typedef SpecificDecl* reference;
656 typedef SpecificDecl* pointer;
657 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
658 difference_type;
659 typedef std::forward_iterator_tag iterator_category;
660
661 filtered_decl_iterator() : Current() { }
662
663 /// specific_decl_iterator - Construct a new iterator over a
664 /// subset of the declarations the range [C,
665 /// end-of-declarations). If A is non-NULL, it is a pointer to a
666 /// member function of SpecificDecl that should return true for
667 /// all of the SpecificDecl instances that will be in the subset
668 /// of iterators. For example, if you want Objective-C instance
669 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
670 /// &ObjCMethodDecl::isInstanceMethod.
671 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
672 SkipToNextDecl();
673 }
674
675 reference operator*() const { return cast<SpecificDecl>(*Current); }
676 pointer operator->() const { return cast<SpecificDecl>(*Current); }
677
678 filtered_decl_iterator& operator++() {
679 ++Current;
680 SkipToNextDecl();
681 return *this;
682 }
683
684 filtered_decl_iterator operator++(int) {
685 filtered_decl_iterator tmp(*this);
686 ++(*this);
687 return tmp;
688 }
689
690 friend bool
691 operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
692 return x.Current == y.Current;
693 }
694
695 friend bool
696 operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
697 return x.Current != y.Current;
698 }
699 };
700
Douglas Gregor40f4e692009-01-20 16:54:50 +0000701 /// @brief Add the declaration D into this context.
702 ///
703 /// This routine should be invoked when the declaration D has first
704 /// been declared, to place D into the context where it was
705 /// (lexically) defined. Every declaration must be added to one
706 /// (and only one!) context, where it can be visited via
707 /// [decls_begin(), decls_end()). Once a declaration has been added
708 /// to its lexical context, the corresponding DeclContext owns the
709 /// declaration.
710 ///
711 /// If D is also a NamedDecl, it will be made visible within its
712 /// semantic context via makeDeclVisibleInContext.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000713 void addDecl(ASTContext &Context, Decl *D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000714
Douglas Gregor44b43212008-12-11 16:49:14 +0000715 /// lookup_iterator - An iterator that provides access to the results
716 /// of looking up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000717 typedef NamedDecl **lookup_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +0000718
719 /// lookup_const_iterator - An iterator that provides non-mutable
720 /// access to the results of lookup up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000721 typedef NamedDecl * const * lookup_const_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +0000722
723 typedef std::pair<lookup_iterator, lookup_iterator> lookup_result;
724 typedef std::pair<lookup_const_iterator, lookup_const_iterator>
725 lookup_const_result;
726
727 /// lookup - Find the declarations (if any) with the given Name in
728 /// this context. Returns a range of iterators that contains all of
Douglas Gregor40f4e692009-01-20 16:54:50 +0000729 /// the declarations with this name, with object, function, member,
730 /// and enumerator names preceding any tag name. Note that this
731 /// routine will not look into parent contexts.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000732 lookup_result lookup(ASTContext &Context, DeclarationName Name);
733 lookup_const_result lookup(ASTContext &Context, DeclarationName Name) const;
Douglas Gregor44b43212008-12-11 16:49:14 +0000734
Douglas Gregor40f4e692009-01-20 16:54:50 +0000735 /// @brief Makes a declaration visible within this context.
736 ///
737 /// This routine makes the declaration D visible to name lookup
738 /// within this context and, if this is a transparent context,
739 /// within its parent contexts up to the first enclosing
740 /// non-transparent context. Making a declaration visible within a
741 /// context does not transfer ownership of a declaration, and a
742 /// declaration can be visible in many contexts that aren't its
743 /// lexical context.
744 ///
745 /// If D is a redeclaration of an existing declaration that is
746 /// visible from this context, as determined by
747 /// NamedDecl::declarationReplaces, the previous declaration will be
748 /// replaced with D.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000749 void makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D);
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000750
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000751 /// udir_iterator - Iterates through the using-directives stored
752 /// within this context.
753 typedef UsingDirectiveDecl * const * udir_iterator;
754
755 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range;
756
Douglas Gregor6ab35242009-04-09 21:40:53 +0000757 udir_iterator_range getUsingDirectives(ASTContext &Context) const;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000758
Douglas Gregor6ab35242009-04-09 21:40:53 +0000759 udir_iterator using_directives_begin(ASTContext &Context) const {
760 return getUsingDirectives(Context).first;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000761 }
762
Douglas Gregor6ab35242009-04-09 21:40:53 +0000763 udir_iterator using_directives_end(ASTContext &Context) const {
764 return getUsingDirectives(Context).second;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000765 }
766
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000767 // Low-level accessors
768
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000769 /// \brief Retrieve the internal representation of the lookup structure.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000770 void* getLookupPtr() const { return LookupPtr; }
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000771
Douglas Gregor2cf26342009-04-09 22:27:44 +0000772 /// \brief Whether this DeclContext has external storage containing
773 /// additional declarations that are lexically in this context.
774 bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; }
775
776 /// \brief State whether this DeclContext has external storage for
777 /// declarations lexically in this context.
778 void setHasExternalLexicalStorage(bool ES = true) {
779 ExternalLexicalStorage = ES;
780 }
781
782 /// \brief Whether this DeclContext has external storage containing
783 /// additional declarations that are visible in this context.
784 bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; }
785
786 /// \brief State whether this DeclContext has external storage for
787 /// declarations visible in this context.
788 void setHasExternalVisibleStorage(bool ES = true) {
789 ExternalVisibleStorage = ES;
790 }
791
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000792 static bool classof(const Decl *D);
Chris Lattnerb048c982008-04-06 04:47:34 +0000793 static bool classof(const DeclContext *D) { return true; }
Douglas Gregor64650af2009-02-02 23:39:07 +0000794#define DECL_CONTEXT(Name) \
795 static bool classof(const Name##Decl *D) { return true; }
796#include "clang/AST/DeclNodes.def"
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000797
798private:
Douglas Gregor2cf26342009-04-09 22:27:44 +0000799 void LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const;
800 void LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const;
801
Douglas Gregor6ab35242009-04-09 21:40:53 +0000802 void buildLookup(ASTContext &Context, DeclContext *DCtx);
803 void makeDeclVisibleInContextImpl(ASTContext &Context, NamedDecl *D);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000804};
805
Douglas Gregorf57172b2008-12-08 18:40:42 +0000806inline bool Decl::isTemplateParameter() const {
807 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm;
808}
809
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000810inline bool Decl::isDefinedOutsideFunctionOrMethod() const {
811 if (getDeclContext())
812 return !getDeclContext()->getLookupContext()->isFunctionOrMethod();
Chris Lattner96f44682009-03-28 05:59:45 +0000813 return true;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000814}
815
Chris Lattner0ed844b2008-04-04 06:12:32 +0000816} // end clang.
817
818namespace llvm {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000819
820/// Implement a isa_impl_wrap specialization to check whether a DeclContext is
821/// a specific Decl.
822template<class ToTy>
823struct isa_impl_wrap<ToTy,
824 const ::clang::DeclContext,const ::clang::DeclContext> {
825 static bool doit(const ::clang::DeclContext &Val) {
826 return ToTy::classof(::clang::Decl::castFromDeclContext(&Val));
Chris Lattner0ed844b2008-04-04 06:12:32 +0000827 }
828};
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000829template<class ToTy>
830struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext>
831 : public isa_impl_wrap<ToTy,
832 const ::clang::DeclContext,const ::clang::DeclContext> {};
Chris Lattner0ed844b2008-04-04 06:12:32 +0000833
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000834/// Implement cast_convert_val for Decl -> DeclContext conversions.
Chris Lattner0ed844b2008-04-04 06:12:32 +0000835template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000836struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
Chris Lattnerb048c982008-04-06 04:47:34 +0000837 static ::clang::DeclContext &doit(const FromTy &Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000838 return *FromTy::castToDeclContext(&Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000839 }
840};
Chris Lattner0ed844b2008-04-04 06:12:32 +0000841
842template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000843struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
Chris Lattnerb048c982008-04-06 04:47:34 +0000844 static ::clang::DeclContext *doit(const FromTy *Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000845 return FromTy::castToDeclContext(Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000846 }
847};
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000848
Douglas Gregor44b43212008-12-11 16:49:14 +0000849template<class FromTy>
850struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
851 static const ::clang::DeclContext &doit(const FromTy &Val) {
852 return *FromTy::castToDeclContext(&Val);
853 }
854};
855
856template<class FromTy>
857struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
858 static const ::clang::DeclContext *doit(const FromTy *Val) {
859 return FromTy::castToDeclContext(Val);
860 }
861};
862
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000863/// Implement cast_convert_val for DeclContext -> Decl conversions.
864template<class ToTy>
865struct cast_convert_val<ToTy,
866 const ::clang::DeclContext,const ::clang::DeclContext> {
867 static ToTy &doit(const ::clang::DeclContext &Val) {
868 return *reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(&Val));
869 }
870};
871template<class ToTy>
872struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext>
873 : public cast_convert_val<ToTy,
874 const ::clang::DeclContext,const ::clang::DeclContext> {};
875
876template<class ToTy>
877struct cast_convert_val<ToTy,
878 const ::clang::DeclContext*, const ::clang::DeclContext*> {
879 static ToTy *doit(const ::clang::DeclContext *Val) {
880 return reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(Val));
881 }
882};
883template<class ToTy>
884struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*>
885 : public cast_convert_val<ToTy,
886 const ::clang::DeclContext*,const ::clang::DeclContext*> {};
Chris Lattner0ed844b2008-04-04 06:12:32 +0000887
888} // end namespace llvm
889
890#endif