blob: a407a16277473dbefa9fc5338074ec0b7bcc7402 [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"
John McCallad2b8042010-01-18 23:21:37 +000019#include "clang/Basic/Specifiers.h"
Chris Lattner49f28ca2009-03-05 08:00:35 +000020#include "llvm/Support/PrettyStackTrace.h"
Chris Lattneree219fd2009-03-29 06:06:59 +000021#include "llvm/ADT/PointerUnion.h"
Chris Lattner0ed844b2008-04-04 06:12:32 +000022
23namespace clang {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +000024class DeclContext;
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000025class TranslationUnitDecl;
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000026class NamespaceDecl;
Douglas Gregor2a3009a2009-02-03 19:21:40 +000027class UsingDirectiveDecl;
Douglas Gregor44b43212008-12-11 16:49:14 +000028class NamedDecl;
Chris Lattner0ed844b2008-04-04 06:12:32 +000029class FunctionDecl;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000030class CXXRecordDecl;
Chris Lattnerb048c982008-04-06 04:47:34 +000031class EnumDecl;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000032class ObjCMethodDecl;
Douglas Gregor64650af2009-02-02 23:39:07 +000033class ObjCContainerDecl;
Chris Lattner0ed844b2008-04-04 06:12:32 +000034class ObjCInterfaceDecl;
Steve Naroff0701bbb2009-01-08 17:28:14 +000035class ObjCCategoryDecl;
36class ObjCProtocolDecl;
37class ObjCImplementationDecl;
38class ObjCCategoryImplDecl;
Argyrios Kyrtzidisbfb498d2009-07-17 01:19:49 +000039class ObjCImplDecl;
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
Mike Stump1eb44332009-09-09 15:08:12 +000062/// Decl - This represents one declaration (or definition), e.g. a variable,
63/// typedef, function, struct, etc.
Chris Lattner0ed844b2008-04-04 06:12:32 +000064///
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) \
Mike Stump1eb44332009-09-09 15:08:12 +000071 CommonBase##First = Start, CommonBase##Last = End,
Douglas Gregor64650af2009-02-02 23:39:07 +000072#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
John McCall9488ea12009-11-17 05:59:44 +000081 /// use additional namespaces for Objective-C entities. We also put
82 /// C++ friend declarations (of previously-undeclared entities) in
83 /// shadow namespaces, and 'using' declarations (as opposed to their
84 /// implicit shadow declarations) can be found in their own
85 /// namespace.
Chris Lattner0ed844b2008-04-04 06:12:32 +000086 enum IdentifierNamespace {
Douglas Gregor2ce52f32008-04-13 21:07:44 +000087 IDNS_Label = 0x1,
88 IDNS_Tag = 0x2,
89 IDNS_Member = 0x4,
Douglas Gregor7dda67d2009-02-05 19:25:20 +000090 IDNS_Ordinary = 0x8,
Douglas Gregor8fc463a2009-04-24 00:11:27 +000091 IDNS_ObjCProtocol = 0x10,
92 IDNS_ObjCImplementation = 0x20,
Fariborz Jahanian737061f2009-12-11 00:26:36 +000093 IDNS_ObjCCategoryName = 0x40,
John McCall02cace72009-08-28 07:59:38 +000094 IDNS_OrdinaryFriend = 0x80,
John McCall9488ea12009-11-17 05:59:44 +000095 IDNS_TagFriend = 0x100,
96 IDNS_Using = 0x200
Chris Lattner0ed844b2008-04-04 06:12:32 +000097 };
Mike Stump1eb44332009-09-09 15:08:12 +000098
Chris Lattner0ed844b2008-04-04 06:12:32 +000099 /// ObjCDeclQualifier - Qualifier used on types in method declarations
100 /// for remote messaging. They are meant for the arguments though and
101 /// applied to the Decls (ObjCMethodDecl and ParmVarDecl).
102 enum ObjCDeclQualifier {
103 OBJC_TQ_None = 0x0,
104 OBJC_TQ_In = 0x1,
105 OBJC_TQ_Inout = 0x2,
106 OBJC_TQ_Out = 0x4,
107 OBJC_TQ_Bycopy = 0x8,
108 OBJC_TQ_Byref = 0x10,
109 OBJC_TQ_Oneway = 0x20
110 };
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Chris Lattner0ed844b2008-04-04 06:12:32 +0000112private:
Chris Lattner244a67d2009-03-28 06:04:26 +0000113 /// NextDeclInContext - The next declaration within the same lexical
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000114 /// DeclContext. These pointers form the linked list that is
115 /// traversed via DeclContext's decls_begin()/decls_end().
Chris Lattner244a67d2009-03-28 06:04:26 +0000116 Decl *NextDeclInContext;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000117
118 friend class DeclContext;
119
Chris Lattneree219fd2009-03-29 06:06:59 +0000120 struct MultipleDC {
121 DeclContext *SemanticDC;
122 DeclContext *LexicalDC;
123 };
Mike Stump1eb44332009-09-09 15:08:12 +0000124
125
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000126 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
127 /// For declarations that don't contain C++ scope specifiers, it contains
128 /// the DeclContext where the Decl was declared.
129 /// For declarations with C++ scope specifiers, it contains a MultipleDC*
130 /// with the context where it semantically belongs (SemanticDC) and the
131 /// context where it was lexically declared (LexicalDC).
132 /// e.g.:
133 ///
134 /// namespace A {
135 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
136 /// }
137 /// void A::f(); // SemanticDC == namespace 'A'
138 /// // LexicalDC == global namespace
Chris Lattneree219fd2009-03-29 06:06:59 +0000139 llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000140
Chris Lattneree219fd2009-03-29 06:06:59 +0000141 inline bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); }
142 inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000143 inline MultipleDC *getMultipleDC() const {
Chris Lattneree219fd2009-03-29 06:06:59 +0000144 return DeclCtx.get<MultipleDC*>();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000145 }
Chris Lattner10d83792009-03-27 18:46:15 +0000146 inline DeclContext *getSemanticDC() const {
Chris Lattneree219fd2009-03-29 06:06:59 +0000147 return DeclCtx.get<DeclContext*>();
Chris Lattner10d83792009-03-27 18:46:15 +0000148 }
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Daniel Dunbar39d76502009-03-04 02:26:41 +0000150 /// Loc - The location that this decl.
151 SourceLocation Loc;
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Chris Lattner0ed844b2008-04-04 06:12:32 +0000153 /// DeclKind - This indicates which class this is.
154 Kind DeclKind : 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Chris Lattner0ed844b2008-04-04 06:12:32 +0000156 /// InvalidDecl - This indicates a semantic error occurred.
157 unsigned int InvalidDecl : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Chris Lattner0ed844b2008-04-04 06:12:32 +0000159 /// HasAttrs - This indicates whether the decl has attributes or not.
160 unsigned int HasAttrs : 1;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000161
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000162 /// Implicit - Whether this declaration was implicitly generated by
163 /// the implementation rather than explicitly written by the user.
164 bool Implicit : 1;
165
Douglas Gregore0762c92009-06-19 23:52:42 +0000166 /// \brief Whether this declaration was "used", meaning that a definition is
167 /// required.
168 bool Used : 1;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000169
170protected:
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000171 /// Access - Used by C++ decls for the access specifier.
172 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
173 unsigned Access : 2;
174 friend class CXXClassMemberWrapper;
175
176 // PCHLevel - the "level" of precompiled header/AST file from which this
177 // declaration was built.
178 unsigned PCHLevel : 2;
179
Chris Lattner769dbdf2009-03-27 20:18:19 +0000180 /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in.
John McCall02cace72009-08-28 07:59:38 +0000181 unsigned IdentifierNamespace : 16;
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000183private:
Anders Carlsson1329c272009-03-25 23:38:06 +0000184#ifndef NDEBUG
185 void CheckAccessDeclContext() const;
186#else
187 void CheckAccessDeclContext() const { }
188#endif
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000190protected:
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000191
Mike Stump1eb44332009-09-09 15:08:12 +0000192 Decl(Kind DK, DeclContext *DC, SourceLocation L)
193 : NextDeclInContext(0), DeclCtx(DC),
Daniel Dunbar39d76502009-03-04 02:26:41 +0000194 Loc(L), DeclKind(DK), InvalidDecl(0),
Douglas Gregore0762c92009-06-19 23:52:42 +0000195 HasAttrs(false), Implicit(false), Used(false),
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000196 Access(AS_none), PCHLevel(0),
197 IdentifierNamespace(getIdentifierNamespaceForKind(DK)) {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000198 if (Decl::CollectingStats()) addDeclKind(DK);
199 }
Sam Bishop1bb19632008-04-11 18:04:39 +0000200
Chris Lattner0ed844b2008-04-04 06:12:32 +0000201 virtual ~Decl();
Sam Bishop1bb19632008-04-11 18:04:39 +0000202
203public:
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000204
205 /// \brief Source range that this declaration covers.
206 virtual SourceRange getSourceRange() const {
207 return SourceRange(getLocation(), getLocation());
208 }
209 SourceLocation getLocStart() const { return getSourceRange().getBegin(); }
210 SourceLocation getLocEnd() const { return getSourceRange().getEnd(); }
211
Chris Lattner0ed844b2008-04-04 06:12:32 +0000212 SourceLocation getLocation() const { return Loc; }
213 void setLocation(SourceLocation L) { Loc = L; }
214
215 Kind getKind() const { return DeclKind; }
216 const char *getDeclKindName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Chris Lattner244a67d2009-03-28 06:04:26 +0000218 Decl *getNextDeclInContext() { return NextDeclInContext; }
219 const Decl *getNextDeclInContext() const { return NextDeclInContext; }
Chris Lattner96f44682009-03-28 05:59:45 +0000220
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000221 DeclContext *getDeclContext() {
Chris Lattner10d83792009-03-27 18:46:15 +0000222 if (isInSemaDC())
223 return getSemanticDC();
224 return getMultipleDC()->SemanticDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000225 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000226 const DeclContext *getDeclContext() const {
227 return const_cast<Decl*>(this)->getDeclContext();
228 }
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000229
230 TranslationUnitDecl *getTranslationUnitDecl();
231 const TranslationUnitDecl *getTranslationUnitDecl() const {
232 return const_cast<Decl*>(this)->getTranslationUnitDecl();
233 }
234
John McCall9aeed322009-10-01 00:25:31 +0000235 bool isInAnonymousNamespace() const;
236
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000237 ASTContext &getASTContext() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Anders Carlsson1329c272009-03-25 23:38:06 +0000239 void setAccess(AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000240 Access = AS;
Anders Carlsson1329c272009-03-25 23:38:06 +0000241 CheckAccessDeclContext();
Anders Carlssonb8547e82009-03-25 20:19:57 +0000242 }
Mike Stump1eb44332009-09-09 15:08:12 +0000243
244 AccessSpecifier getAccess() const {
Anders Carlsson1329c272009-03-25 23:38:06 +0000245 CheckAccessDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000246 return AccessSpecifier(Access);
Anders Carlsson1329c272009-03-25 23:38:06 +0000247 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000248
Chris Lattner76a642f2009-02-15 22:43:40 +0000249 bool hasAttrs() const { return HasAttrs; }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000250 void addAttr(Attr *attr);
251 const Attr *getAttrs() const {
Chris Lattner81abbdd2009-03-21 06:27:31 +0000252 if (!HasAttrs) return 0; // common case, no attributes.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000253 return getAttrsImpl(); // Uncommon case, out of line hash lookup.
Chris Lattner81abbdd2009-03-21 06:27:31 +0000254 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000255 void swapAttrs(Decl *D);
256 void invalidateAttrs();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000257
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000258 template<typename T> const T *getAttr() const {
259 for (const Attr *attr = getAttrs(); attr; attr = attr->getNext())
Chris Lattner0ed844b2008-04-04 06:12:32 +0000260 if (const T *V = dyn_cast<T>(attr))
261 return V;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000262 return 0;
263 }
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000265 template<typename T> bool hasAttr() const {
266 return getAttr<T>() != 0;
Chris Lattner115cafc2009-04-12 20:07:59 +0000267 }
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Chris Lattner0ed844b2008-04-04 06:12:32 +0000269 /// setInvalidDecl - Indicates the Decl had a semantic error. This
270 /// allows for graceful error recovery.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000271 void setInvalidDecl(bool Invalid = true) { InvalidDecl = Invalid; }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000272 bool isInvalidDecl() const { return (bool) InvalidDecl; }
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000273
274 /// isImplicit - Indicates whether the declaration was implicitly
275 /// generated by the implementation. If false, this declaration
276 /// was written explicitly in the source code.
277 bool isImplicit() const { return Implicit; }
278 void setImplicit(bool I = true) { Implicit = I; }
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Douglas Gregore0762c92009-06-19 23:52:42 +0000280 /// \brief Whether this declaration was used, meaning that a definition
281 /// is required.
282 bool isUsed() const { return Used; }
283 void setUsed(bool U = true) { Used = U; }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000285 /// \brief Retrieve the level of precompiled header from which this
286 /// declaration was generated.
287 ///
288 /// The PCH level of a declaration describes where the declaration originated
289 /// from. A PCH level of 0 indicates that the declaration was not from a
290 /// precompiled header. A PCH level of 1 indicates that the declaration was
291 /// from a top-level precompiled header; 2 indicates that the declaration
292 /// comes from a precompiled header on which the top-level precompiled header
293 /// depends, and so on.
294 unsigned getPCHLevel() const { return PCHLevel; }
295
296 /// \brief The maximum PCH level that any declaration may have.
297 static const unsigned MaxPCHLevel = 3;
298
299 /// \brief Set the PCH level of this declaration.
300 void setPCHLevel(unsigned Level) {
301 assert(Level < MaxPCHLevel && "PCH level exceeds the maximum");
302 PCHLevel = Level;
303 }
304
Douglas Gregorcc636682009-02-17 23:15:12 +0000305 unsigned getIdentifierNamespace() const {
Chris Lattner769dbdf2009-03-27 20:18:19 +0000306 return IdentifierNamespace;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000307 }
Chris Lattnerd62fdc42009-01-06 07:16:40 +0000308 bool isInIdentifierNamespace(unsigned NS) const {
309 return getIdentifierNamespace() & NS;
310 }
Chris Lattner769dbdf2009-03-27 20:18:19 +0000311 static unsigned getIdentifierNamespaceForKind(Kind DK);
312
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000314 /// getLexicalDeclContext - The declaration context where this Decl was
315 /// lexically declared (LexicalDC). May be different from
316 /// getDeclContext() (SemanticDC).
317 /// e.g.:
318 ///
319 /// namespace A {
320 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
321 /// }
322 /// void A::f(); // SemanticDC == namespace 'A'
323 /// // LexicalDC == global namespace
Chris Lattner10d83792009-03-27 18:46:15 +0000324 DeclContext *getLexicalDeclContext() {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000325 if (isInSemaDC())
Chris Lattner10d83792009-03-27 18:46:15 +0000326 return getSemanticDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000327 return getMultipleDC()->LexicalDC;
328 }
Chris Lattner10d83792009-03-27 18:46:15 +0000329 const DeclContext *getLexicalDeclContext() const {
330 return const_cast<Decl*>(this)->getLexicalDeclContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000331 }
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000332
333 bool isOutOfLine() const {
334 return getLexicalDeclContext() != getDeclContext();
335 }
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Douglas Gregor6ab35242009-04-09 21:40:53 +0000337 /// setDeclContext - Set both the semantic and lexical DeclContext
338 /// to DC.
339 void setDeclContext(DeclContext *DC);
340
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000341 void setLexicalDeclContext(DeclContext *DC);
342
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000343 // isDefinedOutsideFunctionOrMethod - This predicate returns true if this
344 // scoped decl is defined outside the current function or method. This is
345 // roughly global variables and functions, but also handles enums (which could
346 // be defined inside or outside a function etc).
347 bool isDefinedOutsideFunctionOrMethod() const;
348
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000349 /// \brief Retrieves the "canonical" declaration of the given declaration.
350 virtual Decl *getCanonicalDecl() { return this; }
351 const Decl *getCanonicalDecl() const {
352 return const_cast<Decl*>(this)->getCanonicalDecl();
353 }
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000354
Argyrios Kyrtzidisac4e3792009-07-18 08:50:48 +0000355 /// \brief Whether this particular Decl is a canonical one.
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000356 bool isCanonicalDecl() const { return getCanonicalDecl() == this; }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000358protected:
359 /// \brief Returns the next redeclaration or itself if this is the only decl.
360 ///
361 /// Decl subclasses that can be redeclared should override this method so that
362 /// Decl::redecl_iterator can iterate over them.
363 virtual Decl *getNextRedeclaration() { return this; }
364
365public:
366 /// \brief Iterates through all the redeclarations of the same decl.
367 class redecl_iterator {
368 /// Current - The current declaration.
369 Decl *Current;
370 Decl *Starter;
371
372 public:
373 typedef Decl* value_type;
374 typedef Decl* reference;
375 typedef Decl* pointer;
376 typedef std::forward_iterator_tag iterator_category;
377 typedef std::ptrdiff_t difference_type;
378
379 redecl_iterator() : Current(0) { }
380 explicit redecl_iterator(Decl *C) : Current(C), Starter(C) { }
381
382 reference operator*() const { return Current; }
383 pointer operator->() const { return Current; }
384
385 redecl_iterator& operator++() {
386 assert(Current && "Advancing while iterator has reached end");
387 // Get either previous decl or latest decl.
388 Decl *Next = Current->getNextRedeclaration();
Argyrios Kyrtzidis22cbd2b2009-07-21 00:06:27 +0000389 assert(Next && "Should return next redeclaration or itself, never null!");
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000390 Current = (Next != Starter ? Next : 0);
391 return *this;
392 }
393
394 redecl_iterator operator++(int) {
395 redecl_iterator tmp(*this);
396 ++(*this);
397 return tmp;
398 }
399
Mike Stump1eb44332009-09-09 15:08:12 +0000400 friend bool operator==(redecl_iterator x, redecl_iterator y) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000401 return x.Current == y.Current;
402 }
Mike Stump1eb44332009-09-09 15:08:12 +0000403 friend bool operator!=(redecl_iterator x, redecl_iterator y) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000404 return x.Current != y.Current;
405 }
406 };
407
408 /// \brief Returns iterator for all the redeclarations of the same decl.
409 /// It will iterate at least once (when this decl is the only one).
410 redecl_iterator redecls_begin() const {
411 return redecl_iterator(const_cast<Decl*>(this));
412 }
413 redecl_iterator redecls_end() const { return redecl_iterator(); }
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000414
Sebastian Redld3a413d2009-04-26 20:35:05 +0000415 /// getBody - If this Decl represents a declaration for a body of code,
416 /// such as a function or method definition, this method returns the
417 /// top-level Stmt* of that body. Otherwise this method returns null.
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000418 virtual Stmt* getBody() const { return 0; }
Sebastian Redld3a413d2009-04-26 20:35:05 +0000419
420 /// getCompoundBody - Returns getBody(), dyn_casted to a CompoundStmt.
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000421 CompoundStmt* getCompoundBody() const;
Sebastian Redld3a413d2009-04-26 20:35:05 +0000422
423 /// getBodyRBrace - Gets the right brace of the body, if a body exists.
424 /// This works whether the body is a CompoundStmt or a CXXTryStmt.
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000425 SourceLocation getBodyRBrace() const;
Sebastian Redld3a413d2009-04-26 20:35:05 +0000426
Chris Lattner0ed844b2008-04-04 06:12:32 +0000427 // global temp stats (until we have a per-module visitor)
428 static void addDeclKind(Kind k);
429 static bool CollectingStats(bool Enable = false);
430 static void PrintStats();
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Anders Carlsson67e33202009-06-13 00:08:58 +0000432 /// isTemplateParameter - Determines whether this declaration is a
Douglas Gregorf57172b2008-12-08 18:40:42 +0000433 /// template parameter.
434 bool isTemplateParameter() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Anders Carlsson67e33202009-06-13 00:08:58 +0000436 /// isTemplateParameter - Determines whether this declaration is a
437 /// template parameter pack.
438 bool isTemplateParameterPack() const;
Douglas Gregorf57172b2008-12-08 18:40:42 +0000439
Douglas Gregore53060f2009-06-25 22:08:12 +0000440 /// \brief Whether this declaration is a function or function template.
441 bool isFunctionOrFunctionTemplate() const;
John McCall02cace72009-08-28 07:59:38 +0000442
443 /// \brief Changes the namespace of this declaration to reflect that it's
444 /// the object of a friend declaration.
445 ///
446 /// These declarations appear in the lexical context of the friending
447 /// class, but in the semantic context of the actual entity. This property
448 /// applies only to a specific decl object; other redeclarations of the
449 /// same entity may not (and probably don't) share this property.
450 void setObjectOfFriendDecl(bool PreviouslyDeclared) {
451 unsigned OldNS = IdentifierNamespace;
John McCall05b23ea2009-09-14 21:59:20 +0000452 assert((OldNS == IDNS_Tag || OldNS == IDNS_Ordinary ||
453 OldNS == (IDNS_Tag | IDNS_Ordinary))
John McCall02cace72009-08-28 07:59:38 +0000454 && "unsupported namespace for undeclared friend");
455 if (!PreviouslyDeclared) IdentifierNamespace = 0;
456
457 if (OldNS == IDNS_Tag)
458 IdentifierNamespace |= IDNS_TagFriend;
459 else
460 IdentifierNamespace |= IDNS_OrdinaryFriend;
461 }
462
463 enum FriendObjectKind {
464 FOK_None, // not a friend object
465 FOK_Declared, // a friend of a previously-declared entity
466 FOK_Undeclared // a friend of a previously-undeclared entity
467 };
468
469 /// \brief Determines whether this declaration is the object of a
470 /// friend declaration and, if so, what kind.
471 ///
472 /// There is currently no direct way to find the associated FriendDecl.
473 FriendObjectKind getFriendObjectKind() const {
474 unsigned mask
475 = (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend));
476 if (!mask) return FOK_None;
Anders Carlsson255d6412009-09-13 23:59:13 +0000477 return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ?
478 FOK_Declared : FOK_Undeclared);
John McCall02cace72009-08-28 07:59:38 +0000479 }
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Chris Lattner0ed844b2008-04-04 06:12:32 +0000481 // Implement isa/cast/dyncast/etc.
482 static bool classof(const Decl *) { return true; }
John McCall80cd64a2010-01-29 01:45:37 +0000483 static bool classofKind(Kind K) { return true; }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000484 static DeclContext *castToDeclContext(const Decl *);
485 static Decl *castFromDeclContext(const DeclContext *);
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Sam Bishopbb45c512008-04-11 15:01:25 +0000487 /// Destroy - Call destructors and release memory.
Ted Kremenek27f8a282008-05-20 00:43:19 +0000488 virtual void Destroy(ASTContext& C);
Sam Bishopbb45c512008-04-11 15:01:25 +0000489
Anders Carlssonf88df862009-09-26 21:58:53 +0000490 void print(llvm::raw_ostream &Out, unsigned Indentation = 0) const;
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000491 void print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Anders Carlssonf88df862009-09-26 21:58:53 +0000492 unsigned Indentation = 0) const;
Eli Friedman42f42c02009-05-30 04:20:30 +0000493 static void printGroup(Decl** Begin, unsigned NumDecls,
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000494 llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000495 unsigned Indentation = 0);
Anders Carlssonf88df862009-09-26 21:58:53 +0000496 void dump() const;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000497
Chris Lattner81abbdd2009-03-21 06:27:31 +0000498private:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000499 const Attr *getAttrsImpl() const;
Chris Lattner81abbdd2009-03-21 06:27:31 +0000500
Chris Lattner0ed844b2008-04-04 06:12:32 +0000501};
502
Chris Lattner49f28ca2009-03-05 08:00:35 +0000503/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when
504/// doing something to a specific decl.
505class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
Daniel Dunbarc5236562009-11-21 09:05:59 +0000506 const Decl *TheDecl;
Chris Lattner49f28ca2009-03-05 08:00:35 +0000507 SourceLocation Loc;
508 SourceManager &SM;
509 const char *Message;
510public:
Daniel Dunbarc5236562009-11-21 09:05:59 +0000511 PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L,
Chris Lattner49f28ca2009-03-05 08:00:35 +0000512 SourceManager &sm, const char *Msg)
513 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Chris Lattner49f28ca2009-03-05 08:00:35 +0000515 virtual void print(llvm::raw_ostream &OS) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000516};
517
Chris Lattner49f28ca2009-03-05 08:00:35 +0000518
Chris Lattnerb048c982008-04-06 04:47:34 +0000519/// DeclContext - This is used only as base class of specific decl types that
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000520/// can act as declaration contexts. These decls are (only the top classes
521/// that directly derive from DeclContext are mentioned, not their subclasses):
Chris Lattner0ed844b2008-04-04 06:12:32 +0000522///
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000523/// TranslationUnitDecl
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000524/// NamespaceDecl
Chris Lattner0ed844b2008-04-04 06:12:32 +0000525/// FunctionDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000526/// TagDecl
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000527/// ObjCMethodDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000528/// ObjCContainerDecl
Douglas Gregor074149e2009-01-05 19:45:36 +0000529/// LinkageSpecDecl
Steve Naroff56ee6892008-10-08 17:01:13 +0000530/// BlockDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000531///
Chris Lattnerb048c982008-04-06 04:47:34 +0000532class DeclContext {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000533 /// DeclKind - This indicates which class this is.
534 Decl::Kind DeclKind : 8;
535
Douglas Gregor2cf26342009-04-09 22:27:44 +0000536 /// \brief Whether this declaration context also has some external
537 /// storage that contains additional declarations that are lexically
538 /// part of this context.
539 mutable bool ExternalLexicalStorage : 1;
540
541 /// \brief Whether this declaration context also has some external
542 /// storage that contains additional declarations that are visible
543 /// in this context.
544 mutable bool ExternalVisibleStorage : 1;
545
Douglas Gregorc36c5402009-04-09 17:29:08 +0000546 /// \brief Pointer to the data structure used to lookup declarations
547 /// within this context, which is a DenseMap<DeclarationName,
548 /// StoredDeclsList>.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000549 mutable void* LookupPtr;
Douglas Gregor44b43212008-12-11 16:49:14 +0000550
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000551 /// FirstDecl - The first declaration stored within this declaration
552 /// context.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000553 mutable Decl *FirstDecl;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000554
555 /// LastDecl - The last declaration stored within this declaration
556 /// context. FIXME: We could probably cache this value somewhere
557 /// outside of the DeclContext, to reduce the size of DeclContext by
558 /// another pointer.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000559 mutable Decl *LastDecl;
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000560
Chris Lattner0ed844b2008-04-04 06:12:32 +0000561protected:
Mike Stump1eb44332009-09-09 15:08:12 +0000562 DeclContext(Decl::Kind K)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000563 : DeclKind(K), ExternalLexicalStorage(false),
Mike Stump1eb44332009-09-09 15:08:12 +0000564 ExternalVisibleStorage(false), LookupPtr(0), FirstDecl(0),
Douglas Gregor2cf26342009-04-09 22:27:44 +0000565 LastDecl(0) { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000566
567 void DestroyDecls(ASTContext &C);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000568
569public:
Douglas Gregor44b43212008-12-11 16:49:14 +0000570 ~DeclContext();
571
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000572 Decl::Kind getDeclKind() const {
573 return DeclKind;
574 }
Steve Naroff0a473932009-01-20 19:53:53 +0000575 const char *getDeclKindName() const;
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000576
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +0000577 /// getParent - Returns the containing DeclContext.
Chris Lattner0cf2b192009-03-27 19:19:59 +0000578 DeclContext *getParent() {
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +0000579 return cast<Decl>(this)->getDeclContext();
580 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000581 const DeclContext *getParent() const {
582 return const_cast<DeclContext*>(this)->getParent();
Argyrios Kyrtzidisd2595ec2008-10-12 18:40:01 +0000583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000585 /// getLexicalParent - Returns the containing lexical DeclContext. May be
Douglas Gregor44b43212008-12-11 16:49:14 +0000586 /// different from getParent, e.g.:
587 ///
588 /// namespace A {
589 /// struct S;
590 /// }
591 /// struct A::S {}; // getParent() == namespace 'A'
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000592 /// // getLexicalParent() == translation unit
593 ///
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000594 DeclContext *getLexicalParent() {
Chris Lattner0cf2b192009-03-27 19:19:59 +0000595 return cast<Decl>(this)->getLexicalDeclContext();
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000596 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000597 const DeclContext *getLexicalParent() const {
598 return const_cast<DeclContext*>(this)->getLexicalParent();
Mike Stump1eb44332009-09-09 15:08:12 +0000599 }
Argyrios Kyrtzidis048f30a2009-06-30 02:35:38 +0000600
Douglas Gregore942bbe2009-09-10 16:57:35 +0000601 DeclContext *getLookupParent();
602
603 const DeclContext *getLookupParent() const {
604 return const_cast<DeclContext*>(this)->getLookupParent();
605 }
606
Argyrios Kyrtzidis048f30a2009-06-30 02:35:38 +0000607 ASTContext &getParentASTContext() const {
608 return cast<Decl>(this)->getASTContext();
609 }
610
Chris Lattner0ed844b2008-04-04 06:12:32 +0000611 bool isFunctionOrMethod() const {
612 switch (DeclKind) {
Chris Lattner0cf2b192009-03-27 19:19:59 +0000613 case Decl::Block:
614 case Decl::ObjCMethod:
615 return true;
616 default:
617 return DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000618 }
619 }
620
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000621 bool isFileContext() const {
622 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
623 }
624
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000625 bool isTranslationUnit() const {
626 return DeclKind == Decl::TranslationUnit;
627 }
628
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000629 bool isRecord() const {
Douglas Gregor65100792009-02-26 00:02:51 +0000630 return DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast;
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +0000631 }
632
Douglas Gregor44b43212008-12-11 16:49:14 +0000633 bool isNamespace() const {
634 return DeclKind == Decl::Namespace;
635 }
636
Douglas Gregorbc221632009-05-28 16:34:51 +0000637 /// \brief Determines whether this context is dependent on a
638 /// template parameter.
639 bool isDependentContext() const;
640
Douglas Gregor074149e2009-01-05 19:45:36 +0000641 /// isTransparentContext - Determines whether this context is a
642 /// "transparent" context, meaning that the members declared in this
643 /// context are semantically declared in the nearest enclosing
644 /// non-transparent (opaque) context but are lexically declared in
645 /// this context. For example, consider the enumerators of an
Mike Stump1eb44332009-09-09 15:08:12 +0000646 /// enumeration type:
Douglas Gregor074149e2009-01-05 19:45:36 +0000647 /// @code
648 /// enum E {
Mike Stump1eb44332009-09-09 15:08:12 +0000649 /// Val1
Douglas Gregor074149e2009-01-05 19:45:36 +0000650 /// };
651 /// @endcode
652 /// Here, E is a transparent context, so its enumerator (Val1) will
653 /// appear (semantically) that it is in the same context of E.
654 /// Examples of transparent contexts include: enumerations (except for
655 /// C++0x scoped enums), C++ linkage specifications, and C++0x
656 /// inline namespaces.
657 bool isTransparentContext() const;
658
Douglas Gregor61481da2009-09-01 17:22:34 +0000659 /// \brief Determine whether this declaration context is equivalent
660 /// to the declaration context DC.
661 bool Equals(DeclContext *DC) {
662 return this->getPrimaryContext() == DC->getPrimaryContext();
663 }
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000665 /// \brief Determine whether this declaration context encloses the
666 /// declaration context DC.
667 bool Encloses(DeclContext *DC);
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000668
Douglas Gregor44b43212008-12-11 16:49:14 +0000669 /// getPrimaryContext - There may be many different
670 /// declarations of the same entity (including forward declarations
671 /// of classes, multiple definitions of namespaces, etc.), each with
672 /// a different set of declarations. This routine returns the
673 /// "primary" DeclContext structure, which will contain the
674 /// information needed to perform name lookup into this context.
Steve Naroff0701bbb2009-01-08 17:28:14 +0000675 DeclContext *getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000676
Douglas Gregorce356072009-01-06 23:51:29 +0000677 /// getLookupContext - Retrieve the innermost non-transparent
678 /// context of this context, which corresponds to the innermost
679 /// location from which name lookup can find the entities in this
680 /// context.
Chris Lattner0cf2b192009-03-27 19:19:59 +0000681 DeclContext *getLookupContext();
682 const DeclContext *getLookupContext() const {
683 return const_cast<DeclContext *>(this)->getLookupContext();
Douglas Gregor17a9b9e2009-01-07 02:48:43 +0000684 }
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Douglas Gregor88b70942009-02-25 22:02:03 +0000686 /// \brief Retrieve the nearest enclosing namespace context.
687 DeclContext *getEnclosingNamespaceContext();
688 const DeclContext *getEnclosingNamespaceContext() const {
689 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
690 }
691
Douglas Gregor44b43212008-12-11 16:49:14 +0000692 /// getNextContext - If this is a DeclContext that may have other
693 /// DeclContexts that are semantically connected but syntactically
694 /// different, such as C++ namespaces, this routine retrieves the
695 /// next DeclContext in the link. Iteration through the chain of
696 /// DeclContexts should begin at the primary DeclContext and
697 /// continue until this function returns NULL. For example, given:
698 /// @code
699 /// namespace N {
700 /// int x;
701 /// }
702 /// namespace N {
703 /// int y;
704 /// }
705 /// @endcode
706 /// The first occurrence of namespace N will be the primary
707 /// DeclContext. Its getNextContext will return the second
708 /// occurrence of namespace N.
709 DeclContext *getNextContext();
710
711 /// decl_iterator - Iterates through the declarations stored
712 /// within this context.
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000713 class decl_iterator {
714 /// Current - The current declaration.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000715 Decl *Current;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000716
717 public:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000718 typedef Decl* value_type;
719 typedef Decl* reference;
720 typedef Decl* pointer;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000721 typedef std::forward_iterator_tag iterator_category;
722 typedef std::ptrdiff_t difference_type;
723
724 decl_iterator() : Current(0) { }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000725 explicit decl_iterator(Decl *C) : Current(C) { }
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000726
727 reference operator*() const { return Current; }
728 pointer operator->() const { return Current; }
729
Chris Lattner96f44682009-03-28 05:59:45 +0000730 decl_iterator& operator++() {
Chris Lattner244a67d2009-03-28 06:04:26 +0000731 Current = Current->getNextDeclInContext();
Chris Lattner96f44682009-03-28 05:59:45 +0000732 return *this;
733 }
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000734
735 decl_iterator operator++(int) {
736 decl_iterator tmp(*this);
737 ++(*this);
738 return tmp;
739 }
740
Mike Stump1eb44332009-09-09 15:08:12 +0000741 friend bool operator==(decl_iterator x, decl_iterator y) {
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000742 return x.Current == y.Current;
743 }
Mike Stump1eb44332009-09-09 15:08:12 +0000744 friend bool operator!=(decl_iterator x, decl_iterator y) {
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000745 return x.Current != y.Current;
746 }
747 };
Douglas Gregor44b43212008-12-11 16:49:14 +0000748
Douglas Gregor44b43212008-12-11 16:49:14 +0000749 /// decls_begin/decls_end - Iterate over the declarations stored in
Mike Stump1eb44332009-09-09 15:08:12 +0000750 /// this context.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000751 decl_iterator decls_begin() const;
752 decl_iterator decls_end() const;
753 bool decls_empty() const;
Douglas Gregor44b43212008-12-11 16:49:14 +0000754
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000755 /// specific_decl_iterator - Iterates over a subrange of
756 /// declarations stored in a DeclContext, providing only those that
Douglas Gregor669c9a22009-02-02 18:25:48 +0000757 /// are of type SpecificDecl (or a class derived from it). This
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000758 /// iterator is used, for example, to provide iteration over just
Douglas Gregor669c9a22009-02-02 18:25:48 +0000759 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000760 template<typename SpecificDecl>
761 class specific_decl_iterator {
762 /// Current - The current, underlying declaration iterator, which
Douglas Gregord6f0b4e2009-02-02 17:56:05 +0000763 /// will either be NULL or will point to a declaration of
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000764 /// type SpecificDecl.
765 DeclContext::decl_iterator Current;
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000767 /// SkipToNextDecl - Advances the current position up to the next
768 /// declaration of type SpecificDecl that also meets the criteria
769 /// required by Acceptable.
770 void SkipToNextDecl() {
Douglas Gregor669c9a22009-02-02 18:25:48 +0000771 while (*Current && !isa<SpecificDecl>(*Current))
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000772 ++Current;
773 }
774
775 public:
776 typedef SpecificDecl* value_type;
777 typedef SpecificDecl* reference;
778 typedef SpecificDecl* pointer;
779 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
780 difference_type;
781 typedef std::forward_iterator_tag iterator_category;
782
Douglas Gregor669c9a22009-02-02 18:25:48 +0000783 specific_decl_iterator() : Current() { }
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000784
785 /// specific_decl_iterator - Construct a new iterator over a
Douglas Gregord6f0b4e2009-02-02 17:56:05 +0000786 /// subset of the declarations the range [C,
787 /// end-of-declarations). If A is non-NULL, it is a pointer to a
788 /// member function of SpecificDecl that should return true for
789 /// all of the SpecificDecl instances that will be in the subset
790 /// of iterators. For example, if you want Objective-C instance
791 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
792 /// &ObjCMethodDecl::isInstanceMethod.
Douglas Gregor669c9a22009-02-02 18:25:48 +0000793 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000794 SkipToNextDecl();
795 }
796
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000797 reference operator*() const { return cast<SpecificDecl>(*Current); }
798 pointer operator->() const { return cast<SpecificDecl>(*Current); }
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000799
800 specific_decl_iterator& operator++() {
801 ++Current;
802 SkipToNextDecl();
803 return *this;
804 }
805
806 specific_decl_iterator operator++(int) {
807 specific_decl_iterator tmp(*this);
808 ++(*this);
809 return tmp;
810 }
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000812 friend bool
813 operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) {
814 return x.Current == y.Current;
815 }
Mike Stump1eb44332009-09-09 15:08:12 +0000816
817 friend bool
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000818 operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) {
819 return x.Current != y.Current;
820 }
821 };
822
Douglas Gregor669c9a22009-02-02 18:25:48 +0000823 /// \brief Iterates over a filtered subrange of declarations stored
824 /// in a DeclContext.
825 ///
826 /// This iterator visits only those declarations that are of type
827 /// SpecificDecl (or a class derived from it) and that meet some
828 /// additional run-time criteria. This iterator is used, for
829 /// example, to provide access to the instance methods within an
830 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
831 /// Acceptable = ObjCMethodDecl::isInstanceMethod).
832 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
833 class filtered_decl_iterator {
834 /// Current - The current, underlying declaration iterator, which
835 /// will either be NULL or will point to a declaration of
836 /// type SpecificDecl.
837 DeclContext::decl_iterator Current;
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Douglas Gregor669c9a22009-02-02 18:25:48 +0000839 /// SkipToNextDecl - Advances the current position up to the next
840 /// declaration of type SpecificDecl that also meets the criteria
841 /// required by Acceptable.
842 void SkipToNextDecl() {
843 while (*Current &&
844 (!isa<SpecificDecl>(*Current) ||
845 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
846 ++Current;
847 }
848
849 public:
850 typedef SpecificDecl* value_type;
851 typedef SpecificDecl* reference;
852 typedef SpecificDecl* pointer;
853 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
854 difference_type;
855 typedef std::forward_iterator_tag iterator_category;
856
857 filtered_decl_iterator() : Current() { }
858
859 /// specific_decl_iterator - Construct a new iterator over a
860 /// subset of the declarations the range [C,
861 /// end-of-declarations). If A is non-NULL, it is a pointer to a
862 /// member function of SpecificDecl that should return true for
863 /// all of the SpecificDecl instances that will be in the subset
864 /// of iterators. For example, if you want Objective-C instance
865 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
866 /// &ObjCMethodDecl::isInstanceMethod.
867 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
868 SkipToNextDecl();
869 }
870
871 reference operator*() const { return cast<SpecificDecl>(*Current); }
872 pointer operator->() const { return cast<SpecificDecl>(*Current); }
873
874 filtered_decl_iterator& operator++() {
875 ++Current;
876 SkipToNextDecl();
877 return *this;
878 }
879
880 filtered_decl_iterator operator++(int) {
881 filtered_decl_iterator tmp(*this);
882 ++(*this);
883 return tmp;
884 }
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Douglas Gregor669c9a22009-02-02 18:25:48 +0000886 friend bool
887 operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
888 return x.Current == y.Current;
889 }
Mike Stump1eb44332009-09-09 15:08:12 +0000890
891 friend bool
Douglas Gregor669c9a22009-02-02 18:25:48 +0000892 operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
893 return x.Current != y.Current;
894 }
895 };
896
Douglas Gregor40f4e692009-01-20 16:54:50 +0000897 /// @brief Add the declaration D into this context.
898 ///
899 /// This routine should be invoked when the declaration D has first
900 /// been declared, to place D into the context where it was
901 /// (lexically) defined. Every declaration must be added to one
902 /// (and only one!) context, where it can be visited via
903 /// [decls_begin(), decls_end()). Once a declaration has been added
904 /// to its lexical context, the corresponding DeclContext owns the
905 /// declaration.
906 ///
907 /// If D is also a NamedDecl, it will be made visible within its
908 /// semantic context via makeDeclVisibleInContext.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000909 void addDecl(Decl *D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000910
John McCall3f9a8a62009-08-11 06:59:38 +0000911 /// @brief Add the declaration D to this context without modifying
912 /// any lookup tables.
913 ///
914 /// This is useful for some operations in dependent contexts where
915 /// the semantic context might not be dependent; this basically
916 /// only happens with friends.
917 void addHiddenDecl(Decl *D);
918
John McCall9f54ad42009-12-10 09:41:52 +0000919 /// @brief Removes a declaration from this context.
920 void removeDecl(Decl *D);
921
Douglas Gregor44b43212008-12-11 16:49:14 +0000922 /// lookup_iterator - An iterator that provides access to the results
923 /// of looking up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000924 typedef NamedDecl **lookup_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +0000925
926 /// lookup_const_iterator - An iterator that provides non-mutable
927 /// access to the results of lookup up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000928 typedef NamedDecl * const * lookup_const_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +0000929
930 typedef std::pair<lookup_iterator, lookup_iterator> lookup_result;
931 typedef std::pair<lookup_const_iterator, lookup_const_iterator>
932 lookup_const_result;
933
934 /// lookup - Find the declarations (if any) with the given Name in
935 /// this context. Returns a range of iterators that contains all of
Douglas Gregor40f4e692009-01-20 16:54:50 +0000936 /// the declarations with this name, with object, function, member,
937 /// and enumerator names preceding any tag name. Note that this
938 /// routine will not look into parent contexts.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000939 lookup_result lookup(DeclarationName Name);
940 lookup_const_result lookup(DeclarationName Name) const;
Douglas Gregor44b43212008-12-11 16:49:14 +0000941
Douglas Gregor40f4e692009-01-20 16:54:50 +0000942 /// @brief Makes a declaration visible within this context.
943 ///
944 /// This routine makes the declaration D visible to name lookup
945 /// within this context and, if this is a transparent context,
946 /// within its parent contexts up to the first enclosing
947 /// non-transparent context. Making a declaration visible within a
948 /// context does not transfer ownership of a declaration, and a
949 /// declaration can be visible in many contexts that aren't its
950 /// lexical context.
951 ///
952 /// If D is a redeclaration of an existing declaration that is
953 /// visible from this context, as determined by
954 /// NamedDecl::declarationReplaces, the previous declaration will be
955 /// replaced with D.
John McCallab88d972009-08-31 22:39:49 +0000956 ///
957 /// @param Recoverable true if it's okay to not add this decl to
958 /// the lookup tables because it can be easily recovered by walking
959 /// the declaration chains.
960 void makeDeclVisibleInContext(NamedDecl *D, bool Recoverable = true);
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000961
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000962 /// udir_iterator - Iterates through the using-directives stored
963 /// within this context.
964 typedef UsingDirectiveDecl * const * udir_iterator;
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000966 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range;
967
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000968 udir_iterator_range getUsingDirectives() const;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000969
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000970 udir_iterator using_directives_begin() const {
971 return getUsingDirectives().first;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000972 }
973
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000974 udir_iterator using_directives_end() const {
975 return getUsingDirectives().second;
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000976 }
977
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000978 // Low-level accessors
979
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000980 /// \brief Retrieve the internal representation of the lookup structure.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000981 void* getLookupPtr() const { return LookupPtr; }
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000982
Douglas Gregor2cf26342009-04-09 22:27:44 +0000983 /// \brief Whether this DeclContext has external storage containing
984 /// additional declarations that are lexically in this context.
985 bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; }
986
987 /// \brief State whether this DeclContext has external storage for
988 /// declarations lexically in this context.
Mike Stump1eb44332009-09-09 15:08:12 +0000989 void setHasExternalLexicalStorage(bool ES = true) {
990 ExternalLexicalStorage = ES;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000991 }
992
993 /// \brief Whether this DeclContext has external storage containing
994 /// additional declarations that are visible in this context.
995 bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; }
996
997 /// \brief State whether this DeclContext has external storage for
998 /// declarations visible in this context.
Mike Stump1eb44332009-09-09 15:08:12 +0000999 void setHasExternalVisibleStorage(bool ES = true) {
1000 ExternalVisibleStorage = ES;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001001 }
1002
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +00001003 static bool classof(const Decl *D);
Chris Lattnerb048c982008-04-06 04:47:34 +00001004 static bool classof(const DeclContext *D) { return true; }
Douglas Gregor64650af2009-02-02 23:39:07 +00001005#define DECL_CONTEXT(Name) \
1006 static bool classof(const Name##Decl *D) { return true; }
1007#include "clang/AST/DeclNodes.def"
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +00001008
Anders Carlsson84834432009-12-14 00:51:04 +00001009 void dumpDeclContext() const;
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +00001010
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +00001011private:
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001012 void LoadLexicalDeclsFromExternalStorage() const;
1013 void LoadVisibleDeclsFromExternalStorage() const;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001014
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001015 void buildLookup(DeclContext *DCtx);
1016 void makeDeclVisibleInContextImpl(NamedDecl *D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001017};
1018
Douglas Gregorf57172b2008-12-08 18:40:42 +00001019inline bool Decl::isTemplateParameter() const {
Douglas Gregor79c22782010-01-16 20:21:20 +00001020 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm ||
1021 getKind() == TemplateTemplateParm;
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001022}
1023
John McCall80cd64a2010-01-29 01:45:37 +00001024
1025// Specialization selected when ToTy is not a known subclass of DeclContext.
1026template <class ToTy,
1027 bool IsKnownSubtype = ::llvm::is_base_of< DeclContext, ToTy>::value>
1028struct cast_convert_decl_context {
1029 static const ToTy *doit(const DeclContext *Val) {
1030 return static_cast<const ToTy*>(Decl::castFromDeclContext(Val));
1031 }
1032
1033 static ToTy *doit(DeclContext *Val) {
1034 return static_cast<ToTy*>(Decl::castFromDeclContext(Val));
1035 }
1036};
1037
1038// Specialization selected when ToTy is a known subclass of DeclContext.
1039template <class ToTy>
1040struct cast_convert_decl_context<ToTy, true> {
1041 static const ToTy *doit(const DeclContext *Val) {
1042 return static_cast<const ToTy*>(Val);
1043 }
1044
1045 static ToTy *doit(DeclContext *Val) {
1046 return static_cast<ToTy*>(Val);
1047 }
1048};
1049
1050
Chris Lattner0ed844b2008-04-04 06:12:32 +00001051} // end clang.
1052
1053namespace llvm {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001054
John McCall80cd64a2010-01-29 01:45:37 +00001055/// isa<T>(DeclContext*)
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001056template<class ToTy>
1057struct isa_impl_wrap<ToTy,
1058 const ::clang::DeclContext,const ::clang::DeclContext> {
1059 static bool doit(const ::clang::DeclContext &Val) {
John McCall80cd64a2010-01-29 01:45:37 +00001060 return ToTy::classofKind(Val.getDeclKind());
Chris Lattner0ed844b2008-04-04 06:12:32 +00001061 }
1062};
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001063template<class ToTy>
1064struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext>
1065 : public isa_impl_wrap<ToTy,
1066 const ::clang::DeclContext,const ::clang::DeclContext> {};
Chris Lattner0ed844b2008-04-04 06:12:32 +00001067
John McCall80cd64a2010-01-29 01:45:37 +00001068/// cast<T>(DeclContext*)
1069template<class ToTy>
1070struct cast_convert_val<ToTy,
1071 const ::clang::DeclContext,const ::clang::DeclContext> {
1072 static const ToTy &doit(const ::clang::DeclContext &Val) {
1073 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1074 }
1075};
1076template<class ToTy>
1077struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> {
1078 static ToTy &doit(::clang::DeclContext &Val) {
1079 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1080 }
1081};
1082template<class ToTy>
1083struct cast_convert_val<ToTy,
1084 const ::clang::DeclContext*, const ::clang::DeclContext*> {
1085 static const ToTy *doit(const ::clang::DeclContext *Val) {
1086 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1087 }
1088};
1089template<class ToTy>
1090struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> {
1091 static ToTy *doit(::clang::DeclContext *Val) {
1092 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1093 }
1094};
1095
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001096/// Implement cast_convert_val for Decl -> DeclContext conversions.
Chris Lattner0ed844b2008-04-04 06:12:32 +00001097template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001098struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
Chris Lattnerb048c982008-04-06 04:47:34 +00001099 static ::clang::DeclContext &doit(const FromTy &Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001100 return *FromTy::castToDeclContext(&Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001101 }
1102};
Chris Lattner0ed844b2008-04-04 06:12:32 +00001103
1104template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001105struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
Chris Lattnerb048c982008-04-06 04:47:34 +00001106 static ::clang::DeclContext *doit(const FromTy *Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001107 return FromTy::castToDeclContext(Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001108 }
1109};
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001110
Douglas Gregor44b43212008-12-11 16:49:14 +00001111template<class FromTy>
1112struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
1113 static const ::clang::DeclContext &doit(const FromTy &Val) {
1114 return *FromTy::castToDeclContext(&Val);
1115 }
1116};
1117
1118template<class FromTy>
1119struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
1120 static const ::clang::DeclContext *doit(const FromTy *Val) {
1121 return FromTy::castToDeclContext(Val);
1122 }
1123};
1124
Chris Lattner0ed844b2008-04-04 06:12:32 +00001125} // end namespace llvm
1126
1127#endif