blob: e2ca1a3298b049e29ea1c7db19058091bf00f9fc [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;
162
Chris Lattner769dbdf2009-03-27 20:18:19 +0000163 /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in.
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000164 unsigned IdentifierNamespace : 8;
Chris Lattner769dbdf2009-03-27 20:18:19 +0000165
Anders Carlsson1329c272009-03-25 23:38:06 +0000166#ifndef NDEBUG
167 void CheckAccessDeclContext() const;
168#else
169 void CheckAccessDeclContext() const { }
170#endif
171
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000172protected:
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000173 /// Access - Used by C++ decls for the access specifier.
174 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
175 unsigned Access : 2;
176 friend class CXXClassMemberWrapper;
177
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000178 Decl(Kind DK, DeclContext *DC, SourceLocation L)
Chris Lattner682bf922009-03-29 16:50:03 +0000179 : NextDeclInContext(0), DeclCtx(DC),
Daniel Dunbar39d76502009-03-04 02:26:41 +0000180 Loc(L), DeclKind(DK), InvalidDecl(0),
Douglas Gregore0762c92009-06-19 23:52:42 +0000181 HasAttrs(false), Implicit(false), Used(false),
Chris Lattner769dbdf2009-03-27 20:18:19 +0000182 IdentifierNamespace(getIdentifierNamespaceForKind(DK)), Access(AS_none) {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000183 if (Decl::CollectingStats()) addDeclKind(DK);
184 }
Sam Bishop1bb19632008-04-11 18:04:39 +0000185
Chris Lattner0ed844b2008-04-04 06:12:32 +0000186 virtual ~Decl();
Sam Bishop1bb19632008-04-11 18:04:39 +0000187
188public:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000189 SourceLocation getLocation() const { return Loc; }
190 void setLocation(SourceLocation L) { Loc = L; }
191
192 Kind getKind() const { return DeclKind; }
193 const char *getDeclKindName() const;
194
Chris Lattner244a67d2009-03-28 06:04:26 +0000195 Decl *getNextDeclInContext() { return NextDeclInContext; }
196 const Decl *getNextDeclInContext() const { return NextDeclInContext; }
Chris Lattner96f44682009-03-28 05:59:45 +0000197
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000198 DeclContext *getDeclContext() {
Chris Lattner10d83792009-03-27 18:46:15 +0000199 if (isInSemaDC())
200 return getSemanticDC();
201 return getMultipleDC()->SemanticDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000202 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000203 const DeclContext *getDeclContext() const {
204 return const_cast<Decl*>(this)->getDeclContext();
205 }
206
Anders Carlsson1329c272009-03-25 23:38:06 +0000207 void setAccess(AccessSpecifier AS) {
Anders Carlssonb8547e82009-03-25 20:19:57 +0000208 Access = AS;
Anders Carlsson1329c272009-03-25 23:38:06 +0000209 CheckAccessDeclContext();
Anders Carlssonb8547e82009-03-25 20:19:57 +0000210 }
Anders Carlsson1329c272009-03-25 23:38:06 +0000211
212 AccessSpecifier getAccess() const {
213 CheckAccessDeclContext();
214 return AccessSpecifier(Access);
215 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000216
Chris Lattner76a642f2009-02-15 22:43:40 +0000217 bool hasAttrs() const { return HasAttrs; }
Douglas Gregor68584ed2009-06-18 16:11:24 +0000218 void addAttr(ASTContext &Context, Attr *attr);
219 const Attr *getAttrs(ASTContext &Context) const {
Chris Lattner81abbdd2009-03-21 06:27:31 +0000220 if (!HasAttrs) return 0; // common case, no attributes.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000221 return getAttrsImpl(Context); // Uncommon case, out of line hash lookup.
Chris Lattner81abbdd2009-03-21 06:27:31 +0000222 }
Douglas Gregor68584ed2009-06-18 16:11:24 +0000223 void swapAttrs(ASTContext &Context, Decl *D);
224 void invalidateAttrs(ASTContext &Context);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000225
Douglas Gregor68584ed2009-06-18 16:11:24 +0000226 template<typename T> const T *getAttr(ASTContext &Context) const {
227 for (const Attr *attr = getAttrs(Context); attr; attr = attr->getNext())
Chris Lattner0ed844b2008-04-04 06:12:32 +0000228 if (const T *V = dyn_cast<T>(attr))
229 return V;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000230 return 0;
231 }
232
Douglas Gregor68584ed2009-06-18 16:11:24 +0000233 template<typename T> bool hasAttr(ASTContext &Context) const {
234 return getAttr<T>(Context) != 0;
Chris Lattner115cafc2009-04-12 20:07:59 +0000235 }
236
Chris Lattner0ed844b2008-04-04 06:12:32 +0000237 /// setInvalidDecl - Indicates the Decl had a semantic error. This
238 /// allows for graceful error recovery.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000239 void setInvalidDecl(bool Invalid = true) { InvalidDecl = Invalid; }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000240 bool isInvalidDecl() const { return (bool) InvalidDecl; }
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000241
242 /// isImplicit - Indicates whether the declaration was implicitly
243 /// generated by the implementation. If false, this declaration
244 /// was written explicitly in the source code.
245 bool isImplicit() const { return Implicit; }
246 void setImplicit(bool I = true) { Implicit = I; }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000247
Douglas Gregore0762c92009-06-19 23:52:42 +0000248 /// \brief Whether this declaration was used, meaning that a definition
249 /// is required.
250 bool isUsed() const { return Used; }
251 void setUsed(bool U = true) { Used = U; }
252
Douglas Gregorcc636682009-02-17 23:15:12 +0000253 unsigned getIdentifierNamespace() const {
Chris Lattner769dbdf2009-03-27 20:18:19 +0000254 return IdentifierNamespace;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000255 }
Chris Lattnerd62fdc42009-01-06 07:16:40 +0000256 bool isInIdentifierNamespace(unsigned NS) const {
257 return getIdentifierNamespace() & NS;
258 }
Chris Lattner769dbdf2009-03-27 20:18:19 +0000259 static unsigned getIdentifierNamespaceForKind(Kind DK);
260
Chris Lattnerd62fdc42009-01-06 07:16:40 +0000261
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000262 /// getLexicalDeclContext - The declaration context where this Decl was
263 /// lexically declared (LexicalDC). May be different from
264 /// getDeclContext() (SemanticDC).
265 /// e.g.:
266 ///
267 /// namespace A {
268 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
269 /// }
270 /// void A::f(); // SemanticDC == namespace 'A'
271 /// // LexicalDC == global namespace
Chris Lattner10d83792009-03-27 18:46:15 +0000272 DeclContext *getLexicalDeclContext() {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000273 if (isInSemaDC())
Chris Lattner10d83792009-03-27 18:46:15 +0000274 return getSemanticDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000275 return getMultipleDC()->LexicalDC;
276 }
Chris Lattner10d83792009-03-27 18:46:15 +0000277 const DeclContext *getLexicalDeclContext() const {
278 return const_cast<Decl*>(this)->getLexicalDeclContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000279 }
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000280
281 bool isOutOfLine() const {
282 return getLexicalDeclContext() != getDeclContext();
283 }
Chris Lattner10d83792009-03-27 18:46:15 +0000284
Douglas Gregor6ab35242009-04-09 21:40:53 +0000285 /// setDeclContext - Set both the semantic and lexical DeclContext
286 /// to DC.
287 void setDeclContext(DeclContext *DC);
288
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000289 void setLexicalDeclContext(DeclContext *DC);
290
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000291 // isDefinedOutsideFunctionOrMethod - This predicate returns true if this
292 // scoped decl is defined outside the current function or method. This is
293 // roughly global variables and functions, but also handles enums (which could
294 // be defined inside or outside a function etc).
295 bool isDefinedOutsideFunctionOrMethod() const;
296
Sebastian Redld3a413d2009-04-26 20:35:05 +0000297 /// getBody - If this Decl represents a declaration for a body of code,
298 /// such as a function or method definition, this method returns the
299 /// top-level Stmt* of that body. Otherwise this method returns null.
300 virtual Stmt* getBody(ASTContext &Context) const { return 0; }
301
302 /// getCompoundBody - Returns getBody(), dyn_casted to a CompoundStmt.
303 CompoundStmt* getCompoundBody(ASTContext &Context) const;
304
305 /// getBodyRBrace - Gets the right brace of the body, if a body exists.
306 /// This works whether the body is a CompoundStmt or a CXXTryStmt.
307 SourceLocation getBodyRBrace(ASTContext &Context) const;
308
Chris Lattner0ed844b2008-04-04 06:12:32 +0000309 // global temp stats (until we have a per-module visitor)
310 static void addDeclKind(Kind k);
311 static bool CollectingStats(bool Enable = false);
312 static void PrintStats();
313
Anders Carlsson67e33202009-06-13 00:08:58 +0000314 /// isTemplateParameter - Determines whether this declaration is a
Douglas Gregorf57172b2008-12-08 18:40:42 +0000315 /// template parameter.
316 bool isTemplateParameter() const;
Anders Carlsson67e33202009-06-13 00:08:58 +0000317
318 /// isTemplateParameter - Determines whether this declaration is a
319 /// template parameter pack.
320 bool isTemplateParameterPack() const;
Douglas Gregorf57172b2008-12-08 18:40:42 +0000321
Chris Lattner0ed844b2008-04-04 06:12:32 +0000322 // Implement isa/cast/dyncast/etc.
323 static bool classof(const Decl *) { return true; }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000324 static DeclContext *castToDeclContext(const Decl *);
325 static Decl *castFromDeclContext(const DeclContext *);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000326
Sam Bishopbb45c512008-04-11 15:01:25 +0000327 /// Destroy - Call destructors and release memory.
Ted Kremenek27f8a282008-05-20 00:43:19 +0000328 virtual void Destroy(ASTContext& C);
Sam Bishopbb45c512008-04-11 15:01:25 +0000329
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000330 void print(llvm::raw_ostream &Out, ASTContext &Context,
331 unsigned Indentation = 0);
332 void print(llvm::raw_ostream &Out, ASTContext &Context,
333 const PrintingPolicy &Policy, unsigned Indentation = 0);
Eli Friedman42f42c02009-05-30 04:20:30 +0000334 static void printGroup(Decl** Begin, unsigned NumDecls,
335 llvm::raw_ostream &Out, ASTContext &Context,
336 const PrintingPolicy &Policy,
337 unsigned Indentation = 0);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000338 void dump(ASTContext &Context);
339
Chris Lattner81abbdd2009-03-21 06:27:31 +0000340private:
Douglas Gregor68584ed2009-06-18 16:11:24 +0000341 const Attr *getAttrsImpl(ASTContext &Context) const;
Chris Lattner81abbdd2009-03-21 06:27:31 +0000342
Chris Lattner0ed844b2008-04-04 06:12:32 +0000343};
344
Chris Lattner49f28ca2009-03-05 08:00:35 +0000345/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when
346/// doing something to a specific decl.
347class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
348 Decl *TheDecl;
349 SourceLocation Loc;
350 SourceManager &SM;
351 const char *Message;
352public:
353 PrettyStackTraceDecl(Decl *theDecl, SourceLocation L,
354 SourceManager &sm, const char *Msg)
355 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {}
356
357 virtual void print(llvm::raw_ostream &OS) const;
358};
359
360
Chris Lattnerb048c982008-04-06 04:47:34 +0000361/// DeclContext - This is used only as base class of specific decl types that
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000362/// can act as declaration contexts. These decls are (only the top classes
363/// that directly derive from DeclContext are mentioned, not their subclasses):
Chris Lattner0ed844b2008-04-04 06:12:32 +0000364///
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000365/// TranslationUnitDecl
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000366/// NamespaceDecl
Chris Lattner0ed844b2008-04-04 06:12:32 +0000367/// FunctionDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000368/// TagDecl
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000369/// ObjCMethodDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000370/// ObjCContainerDecl
371/// ObjCCategoryImplDecl
372/// ObjCImplementationDecl
Douglas Gregor074149e2009-01-05 19:45:36 +0000373/// LinkageSpecDecl
Steve Naroff56ee6892008-10-08 17:01:13 +0000374/// BlockDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000375///
Chris Lattnerb048c982008-04-06 04:47:34 +0000376class DeclContext {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000377 /// DeclKind - This indicates which class this is.
378 Decl::Kind DeclKind : 8;
379
Douglas Gregor2cf26342009-04-09 22:27:44 +0000380 /// \brief Whether this declaration context also has some external
381 /// storage that contains additional declarations that are lexically
382 /// part of this context.
383 mutable bool ExternalLexicalStorage : 1;
384
385 /// \brief Whether this declaration context also has some external
386 /// storage that contains additional declarations that are visible
387 /// in this context.
388 mutable bool ExternalVisibleStorage : 1;
389
Douglas Gregorc36c5402009-04-09 17:29:08 +0000390 /// \brief Pointer to the data structure used to lookup declarations
391 /// within this context, which is a DenseMap<DeclarationName,
392 /// StoredDeclsList>.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000393 mutable void* LookupPtr;
Douglas Gregor44b43212008-12-11 16:49:14 +0000394
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000395 /// FirstDecl - The first declaration stored within this declaration
396 /// context.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000397 mutable Decl *FirstDecl;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000398
399 /// LastDecl - The last declaration stored within this declaration
400 /// context. FIXME: We could probably cache this value somewhere
401 /// outside of the DeclContext, to reduce the size of DeclContext by
402 /// another pointer.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000403 mutable Decl *LastDecl;
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000404
Chris Lattner0ed844b2008-04-04 06:12:32 +0000405protected:
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000406 DeclContext(Decl::Kind K)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000407 : DeclKind(K), ExternalLexicalStorage(false),
408 ExternalVisibleStorage(false), LookupPtr(0), FirstDecl(0),
409 LastDecl(0) { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000410
411 void DestroyDecls(ASTContext &C);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000412
413public:
Douglas Gregor44b43212008-12-11 16:49:14 +0000414 ~DeclContext();
415
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000416 Decl::Kind getDeclKind() const {
417 return DeclKind;
418 }
Steve Naroff0a473932009-01-20 19:53:53 +0000419 const char *getDeclKindName() const;
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000420
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +0000421 /// getParent - Returns the containing DeclContext.
Chris Lattner0cf2b192009-03-27 19:19:59 +0000422 DeclContext *getParent() {
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +0000423 return cast<Decl>(this)->getDeclContext();
424 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000425 const DeclContext *getParent() const {
426 return const_cast<DeclContext*>(this)->getParent();
Argyrios Kyrtzidisd2595ec2008-10-12 18:40:01 +0000427 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000428
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000429 /// getLexicalParent - Returns the containing lexical DeclContext. May be
Douglas Gregor44b43212008-12-11 16:49:14 +0000430 /// different from getParent, e.g.:
431 ///
432 /// namespace A {
433 /// struct S;
434 /// }
435 /// struct A::S {}; // getParent() == namespace 'A'
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000436 /// // getLexicalParent() == translation unit
437 ///
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000438 DeclContext *getLexicalParent() {
Chris Lattner0cf2b192009-03-27 19:19:59 +0000439 return cast<Decl>(this)->getLexicalDeclContext();
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000440 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000441 const DeclContext *getLexicalParent() const {
442 return const_cast<DeclContext*>(this)->getLexicalParent();
443 }
444
Chris Lattner0ed844b2008-04-04 06:12:32 +0000445 bool isFunctionOrMethod() const {
446 switch (DeclKind) {
Chris Lattner0cf2b192009-03-27 19:19:59 +0000447 case Decl::Block:
448 case Decl::ObjCMethod:
449 return true;
450 default:
451 return DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000452 }
453 }
454
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000455 bool isFileContext() const {
456 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
457 }
458
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000459 bool isTranslationUnit() const {
460 return DeclKind == Decl::TranslationUnit;
461 }
462
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000463 bool isRecord() const {
Douglas Gregor65100792009-02-26 00:02:51 +0000464 return DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast;
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +0000465 }
466
Douglas Gregor44b43212008-12-11 16:49:14 +0000467 bool isNamespace() const {
468 return DeclKind == Decl::Namespace;
469 }
470
Douglas Gregorbc221632009-05-28 16:34:51 +0000471 /// \brief Determines whether this context is dependent on a
472 /// template parameter.
473 bool isDependentContext() const;
474
Douglas Gregor074149e2009-01-05 19:45:36 +0000475 /// isTransparentContext - Determines whether this context is a
476 /// "transparent" context, meaning that the members declared in this
477 /// context are semantically declared in the nearest enclosing
478 /// non-transparent (opaque) context but are lexically declared in
479 /// this context. For example, consider the enumerators of an
480 /// enumeration type:
481 /// @code
482 /// enum E {
483 /// Val1
484 /// };
485 /// @endcode
486 /// Here, E is a transparent context, so its enumerator (Val1) will
487 /// appear (semantically) that it is in the same context of E.
488 /// Examples of transparent contexts include: enumerations (except for
489 /// C++0x scoped enums), C++ linkage specifications, and C++0x
490 /// inline namespaces.
491 bool isTransparentContext() const;
492
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000493 bool Encloses(DeclContext *DC) const {
494 for (; DC; DC = DC->getParent())
495 if (DC == this)
496 return true;
497 return false;
498 }
499
Douglas Gregor44b43212008-12-11 16:49:14 +0000500 /// getPrimaryContext - There may be many different
501 /// declarations of the same entity (including forward declarations
502 /// of classes, multiple definitions of namespaces, etc.), each with
503 /// a different set of declarations. This routine returns the
504 /// "primary" DeclContext structure, which will contain the
505 /// information needed to perform name lookup into this context.
Steve Naroff0701bbb2009-01-08 17:28:14 +0000506 DeclContext *getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000507
Douglas Gregorce356072009-01-06 23:51:29 +0000508 /// getLookupContext - Retrieve the innermost non-transparent
509 /// context of this context, which corresponds to the innermost
510 /// location from which name lookup can find the entities in this
511 /// context.
Chris Lattner0cf2b192009-03-27 19:19:59 +0000512 DeclContext *getLookupContext();
513 const DeclContext *getLookupContext() const {
514 return const_cast<DeclContext *>(this)->getLookupContext();
Douglas Gregor17a9b9e2009-01-07 02:48:43 +0000515 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000516
Douglas Gregor88b70942009-02-25 22:02:03 +0000517 /// \brief Retrieve the nearest enclosing namespace context.
518 DeclContext *getEnclosingNamespaceContext();
519 const DeclContext *getEnclosingNamespaceContext() const {
520 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
521 }
522
Douglas Gregor44b43212008-12-11 16:49:14 +0000523 /// getNextContext - If this is a DeclContext that may have other
524 /// DeclContexts that are semantically connected but syntactically
525 /// different, such as C++ namespaces, this routine retrieves the
526 /// next DeclContext in the link. Iteration through the chain of
527 /// DeclContexts should begin at the primary DeclContext and
528 /// continue until this function returns NULL. For example, given:
529 /// @code
530 /// namespace N {
531 /// int x;
532 /// }
533 /// namespace N {
534 /// int y;
535 /// }
536 /// @endcode
537 /// The first occurrence of namespace N will be the primary
538 /// DeclContext. Its getNextContext will return the second
539 /// occurrence of namespace N.
540 DeclContext *getNextContext();
541
542 /// decl_iterator - Iterates through the declarations stored
543 /// within this context.
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000544 class decl_iterator {
545 /// Current - The current declaration.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000546 Decl *Current;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000547
548 public:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000549 typedef Decl* value_type;
550 typedef Decl* reference;
551 typedef Decl* pointer;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000552 typedef std::forward_iterator_tag iterator_category;
553 typedef std::ptrdiff_t difference_type;
554
555 decl_iterator() : Current(0) { }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000556 explicit decl_iterator(Decl *C) : Current(C) { }
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000557
558 reference operator*() const { return Current; }
559 pointer operator->() const { return Current; }
560
Chris Lattner96f44682009-03-28 05:59:45 +0000561 decl_iterator& operator++() {
Chris Lattner244a67d2009-03-28 06:04:26 +0000562 Current = Current->getNextDeclInContext();
Chris Lattner96f44682009-03-28 05:59:45 +0000563 return *this;
564 }
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000565
566 decl_iterator operator++(int) {
567 decl_iterator tmp(*this);
568 ++(*this);
569 return tmp;
570 }
571
572 friend bool operator==(decl_iterator x, decl_iterator y) {
573 return x.Current == y.Current;
574 }
575 friend bool operator!=(decl_iterator x, decl_iterator y) {
576 return x.Current != y.Current;
577 }
578 };
Douglas Gregor44b43212008-12-11 16:49:14 +0000579
Douglas Gregor44b43212008-12-11 16:49:14 +0000580 /// decls_begin/decls_end - Iterate over the declarations stored in
581 /// this context.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000582 decl_iterator decls_begin(ASTContext &Context) const;
583 decl_iterator decls_end(ASTContext &Context) const;
Douglas Gregor8038d512009-04-10 17:25:41 +0000584 bool decls_empty(ASTContext &Context) const;
Douglas Gregor44b43212008-12-11 16:49:14 +0000585
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000586 /// specific_decl_iterator - Iterates over a subrange of
587 /// declarations stored in a DeclContext, providing only those that
Douglas Gregor669c9a22009-02-02 18:25:48 +0000588 /// are of type SpecificDecl (or a class derived from it). This
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000589 /// iterator is used, for example, to provide iteration over just
Douglas Gregor669c9a22009-02-02 18:25:48 +0000590 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000591 template<typename SpecificDecl>
592 class specific_decl_iterator {
593 /// Current - The current, underlying declaration iterator, which
Douglas Gregord6f0b4e2009-02-02 17:56:05 +0000594 /// will either be NULL or will point to a declaration of
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000595 /// type SpecificDecl.
596 DeclContext::decl_iterator Current;
597
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000598 /// SkipToNextDecl - Advances the current position up to the next
599 /// declaration of type SpecificDecl that also meets the criteria
600 /// required by Acceptable.
601 void SkipToNextDecl() {
Douglas Gregor669c9a22009-02-02 18:25:48 +0000602 while (*Current && !isa<SpecificDecl>(*Current))
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000603 ++Current;
604 }
605
606 public:
607 typedef SpecificDecl* value_type;
608 typedef SpecificDecl* reference;
609 typedef SpecificDecl* pointer;
610 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
611 difference_type;
612 typedef std::forward_iterator_tag iterator_category;
613
Douglas Gregor669c9a22009-02-02 18:25:48 +0000614 specific_decl_iterator() : Current() { }
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000615
616 /// specific_decl_iterator - Construct a new iterator over a
Douglas Gregord6f0b4e2009-02-02 17:56:05 +0000617 /// subset of the declarations the range [C,
618 /// end-of-declarations). If A is non-NULL, it is a pointer to a
619 /// member function of SpecificDecl that should return true for
620 /// all of the SpecificDecl instances that will be in the subset
621 /// of iterators. For example, if you want Objective-C instance
622 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
623 /// &ObjCMethodDecl::isInstanceMethod.
Douglas Gregor669c9a22009-02-02 18:25:48 +0000624 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000625 SkipToNextDecl();
626 }
627
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000628 reference operator*() const { return cast<SpecificDecl>(*Current); }
629 pointer operator->() const { return cast<SpecificDecl>(*Current); }
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000630
631 specific_decl_iterator& operator++() {
632 ++Current;
633 SkipToNextDecl();
634 return *this;
635 }
636
637 specific_decl_iterator operator++(int) {
638 specific_decl_iterator tmp(*this);
639 ++(*this);
640 return tmp;
641 }
642
643 friend bool
644 operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) {
645 return x.Current == y.Current;
646 }
647
648 friend bool
649 operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) {
650 return x.Current != y.Current;
651 }
652 };
653
Douglas Gregor669c9a22009-02-02 18:25:48 +0000654 /// \brief Iterates over a filtered subrange of declarations stored
655 /// in a DeclContext.
656 ///
657 /// This iterator visits only those declarations that are of type
658 /// SpecificDecl (or a class derived from it) and that meet some
659 /// additional run-time criteria. This iterator is used, for
660 /// example, to provide access to the instance methods within an
661 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
662 /// Acceptable = ObjCMethodDecl::isInstanceMethod).
663 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
664 class filtered_decl_iterator {
665 /// Current - The current, underlying declaration iterator, which
666 /// will either be NULL or will point to a declaration of
667 /// type SpecificDecl.
668 DeclContext::decl_iterator Current;
669
670 /// SkipToNextDecl - Advances the current position up to the next
671 /// declaration of type SpecificDecl that also meets the criteria
672 /// required by Acceptable.
673 void SkipToNextDecl() {
674 while (*Current &&
675 (!isa<SpecificDecl>(*Current) ||
676 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
677 ++Current;
678 }
679
680 public:
681 typedef SpecificDecl* value_type;
682 typedef SpecificDecl* reference;
683 typedef SpecificDecl* pointer;
684 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
685 difference_type;
686 typedef std::forward_iterator_tag iterator_category;
687
688 filtered_decl_iterator() : Current() { }
689
690 /// specific_decl_iterator - Construct a new iterator over a
691 /// subset of the declarations the range [C,
692 /// end-of-declarations). If A is non-NULL, it is a pointer to a
693 /// member function of SpecificDecl that should return true for
694 /// all of the SpecificDecl instances that will be in the subset
695 /// of iterators. For example, if you want Objective-C instance
696 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
697 /// &ObjCMethodDecl::isInstanceMethod.
698 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
699 SkipToNextDecl();
700 }
701
702 reference operator*() const { return cast<SpecificDecl>(*Current); }
703 pointer operator->() const { return cast<SpecificDecl>(*Current); }
704
705 filtered_decl_iterator& operator++() {
706 ++Current;
707 SkipToNextDecl();
708 return *this;
709 }
710
711 filtered_decl_iterator operator++(int) {
712 filtered_decl_iterator tmp(*this);
713 ++(*this);
714 return tmp;
715 }
716
717 friend bool
718 operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
719 return x.Current == y.Current;
720 }
721
722 friend bool
723 operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
724 return x.Current != y.Current;
725 }
726 };
727
Douglas Gregor40f4e692009-01-20 16:54:50 +0000728 /// @brief Add the declaration D into this context.
729 ///
730 /// This routine should be invoked when the declaration D has first
731 /// been declared, to place D into the context where it was
732 /// (lexically) defined. Every declaration must be added to one
733 /// (and only one!) context, where it can be visited via
734 /// [decls_begin(), decls_end()). Once a declaration has been added
735 /// to its lexical context, the corresponding DeclContext owns the
736 /// declaration.
737 ///
738 /// If D is also a NamedDecl, it will be made visible within its
739 /// semantic context via makeDeclVisibleInContext.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000740 void addDecl(ASTContext &Context, Decl *D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000741
Douglas Gregor44b43212008-12-11 16:49:14 +0000742 /// lookup_iterator - An iterator that provides access to the results
743 /// of looking up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000744 typedef NamedDecl **lookup_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +0000745
746 /// lookup_const_iterator - An iterator that provides non-mutable
747 /// access to the results of lookup up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000748 typedef NamedDecl * const * lookup_const_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +0000749
750 typedef std::pair<lookup_iterator, lookup_iterator> lookup_result;
751 typedef std::pair<lookup_const_iterator, lookup_const_iterator>
752 lookup_const_result;
753
754 /// lookup - Find the declarations (if any) with the given Name in
755 /// this context. Returns a range of iterators that contains all of
Douglas Gregor40f4e692009-01-20 16:54:50 +0000756 /// the declarations with this name, with object, function, member,
757 /// and enumerator names preceding any tag name. Note that this
758 /// routine will not look into parent contexts.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000759 lookup_result lookup(ASTContext &Context, DeclarationName Name);
760 lookup_const_result lookup(ASTContext &Context, DeclarationName Name) const;
Douglas Gregor44b43212008-12-11 16:49:14 +0000761
Douglas Gregor40f4e692009-01-20 16:54:50 +0000762 /// @brief Makes a declaration visible within this context.
763 ///
764 /// This routine makes the declaration D visible to name lookup
765 /// within this context and, if this is a transparent context,
766 /// within its parent contexts up to the first enclosing
767 /// non-transparent context. Making a declaration visible within a
768 /// context does not transfer ownership of a declaration, and a
769 /// declaration can be visible in many contexts that aren't its
770 /// lexical context.
771 ///
772 /// If D is a redeclaration of an existing declaration that is
773 /// visible from this context, as determined by
774 /// NamedDecl::declarationReplaces, the previous declaration will be
775 /// replaced with D.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000776 void makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D);
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000777
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000778 /// udir_iterator - Iterates through the using-directives stored
779 /// within this context.
780 typedef UsingDirectiveDecl * const * udir_iterator;
781
782 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range;
783
Douglas Gregor6ab35242009-04-09 21:40:53 +0000784 udir_iterator_range getUsingDirectives(ASTContext &Context) const;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000785
Douglas Gregor6ab35242009-04-09 21:40:53 +0000786 udir_iterator using_directives_begin(ASTContext &Context) const {
787 return getUsingDirectives(Context).first;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000788 }
789
Douglas Gregor6ab35242009-04-09 21:40:53 +0000790 udir_iterator using_directives_end(ASTContext &Context) const {
791 return getUsingDirectives(Context).second;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000792 }
793
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000794 // Low-level accessors
795
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000796 /// \brief Retrieve the internal representation of the lookup structure.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000797 void* getLookupPtr() const { return LookupPtr; }
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000798
Douglas Gregor2cf26342009-04-09 22:27:44 +0000799 /// \brief Whether this DeclContext has external storage containing
800 /// additional declarations that are lexically in this context.
801 bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; }
802
803 /// \brief State whether this DeclContext has external storage for
804 /// declarations lexically in this context.
805 void setHasExternalLexicalStorage(bool ES = true) {
806 ExternalLexicalStorage = ES;
807 }
808
809 /// \brief Whether this DeclContext has external storage containing
810 /// additional declarations that are visible in this context.
811 bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; }
812
813 /// \brief State whether this DeclContext has external storage for
814 /// declarations visible in this context.
815 void setHasExternalVisibleStorage(bool ES = true) {
816 ExternalVisibleStorage = ES;
817 }
818
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000819 static bool classof(const Decl *D);
Chris Lattnerb048c982008-04-06 04:47:34 +0000820 static bool classof(const DeclContext *D) { return true; }
Douglas Gregor64650af2009-02-02 23:39:07 +0000821#define DECL_CONTEXT(Name) \
822 static bool classof(const Name##Decl *D) { return true; }
823#include "clang/AST/DeclNodes.def"
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000824
825private:
Douglas Gregor2cf26342009-04-09 22:27:44 +0000826 void LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const;
827 void LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const;
828
Douglas Gregor6ab35242009-04-09 21:40:53 +0000829 void buildLookup(ASTContext &Context, DeclContext *DCtx);
830 void makeDeclVisibleInContextImpl(ASTContext &Context, NamedDecl *D);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000831};
832
Douglas Gregorf57172b2008-12-08 18:40:42 +0000833inline bool Decl::isTemplateParameter() const {
834 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm;
835}
836
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000837inline bool Decl::isDefinedOutsideFunctionOrMethod() const {
838 if (getDeclContext())
839 return !getDeclContext()->getLookupContext()->isFunctionOrMethod();
Chris Lattner96f44682009-03-28 05:59:45 +0000840 return true;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000841}
842
Chris Lattner0ed844b2008-04-04 06:12:32 +0000843} // end clang.
844
845namespace llvm {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000846
847/// Implement a isa_impl_wrap specialization to check whether a DeclContext is
848/// a specific Decl.
849template<class ToTy>
850struct isa_impl_wrap<ToTy,
851 const ::clang::DeclContext,const ::clang::DeclContext> {
852 static bool doit(const ::clang::DeclContext &Val) {
853 return ToTy::classof(::clang::Decl::castFromDeclContext(&Val));
Chris Lattner0ed844b2008-04-04 06:12:32 +0000854 }
855};
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000856template<class ToTy>
857struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext>
858 : public isa_impl_wrap<ToTy,
859 const ::clang::DeclContext,const ::clang::DeclContext> {};
Chris Lattner0ed844b2008-04-04 06:12:32 +0000860
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000861/// Implement cast_convert_val for Decl -> DeclContext conversions.
Chris Lattner0ed844b2008-04-04 06:12:32 +0000862template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000863struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
Chris Lattnerb048c982008-04-06 04:47:34 +0000864 static ::clang::DeclContext &doit(const FromTy &Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000865 return *FromTy::castToDeclContext(&Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000866 }
867};
Chris Lattner0ed844b2008-04-04 06:12:32 +0000868
869template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000870struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
Chris Lattnerb048c982008-04-06 04:47:34 +0000871 static ::clang::DeclContext *doit(const FromTy *Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000872 return FromTy::castToDeclContext(Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000873 }
874};
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000875
Douglas Gregor44b43212008-12-11 16:49:14 +0000876template<class FromTy>
877struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
878 static const ::clang::DeclContext &doit(const FromTy &Val) {
879 return *FromTy::castToDeclContext(&Val);
880 }
881};
882
883template<class FromTy>
884struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
885 static const ::clang::DeclContext *doit(const FromTy *Val) {
886 return FromTy::castToDeclContext(Val);
887 }
888};
889
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000890/// Implement cast_convert_val for DeclContext -> Decl conversions.
891template<class ToTy>
892struct cast_convert_val<ToTy,
893 const ::clang::DeclContext,const ::clang::DeclContext> {
894 static ToTy &doit(const ::clang::DeclContext &Val) {
895 return *reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(&Val));
896 }
897};
898template<class ToTy>
899struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext>
900 : public cast_convert_val<ToTy,
901 const ::clang::DeclContext,const ::clang::DeclContext> {};
902
903template<class ToTy>
904struct cast_convert_val<ToTy,
905 const ::clang::DeclContext*, const ::clang::DeclContext*> {
906 static ToTy *doit(const ::clang::DeclContext *Val) {
907 return reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(Val));
908 }
909};
910template<class ToTy>
911struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*>
912 : public cast_convert_val<ToTy,
913 const ::clang::DeclContext*,const ::clang::DeclContext*> {};
Chris Lattner0ed844b2008-04-04 06:12:32 +0000914
915} // end namespace llvm
916
917#endif