blob: 064fa310d36a52963018be99405a9e1be0f630c7 [file] [log] [blame]
Chris Lattner0ed844b2008-04-04 06:12:32 +00001//===-- DeclBase.h - Base Classes for representing declarations *- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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 Lattner0ed844b2008-04-04 06:12:32 +000021#include "clang/Basic/SourceLocation.h"
Douglas Gregor44b43212008-12-11 16:49:14 +000022#include "llvm/ADT/PointerIntPair.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;
Chris Lattner0ed844b2008-04-04 06:12:32 +000043
44/// Decl - This represents one declaration (or definition), e.g. a variable,
45/// typedef, function, struct, etc.
46///
47class Decl {
48public:
Douglas Gregor64650af2009-02-02 23:39:07 +000049 /// \brief Lists the kind of concrete classes of Decl.
Chris Lattner0ed844b2008-04-04 06:12:32 +000050 enum Kind {
Douglas Gregor64650af2009-02-02 23:39:07 +000051#define DECL(Derived, Base) Derived,
52#define DECL_RANGE(CommonBase, Start, End) \
53 CommonBase##First = Start, CommonBase##Last = End,
54#define LAST_DECL_RANGE(CommonBase, Start, End) \
55 CommonBase##First = Start, CommonBase##Last = End
56#include "clang/AST/DeclNodes.def"
Chris Lattner0ed844b2008-04-04 06:12:32 +000057 };
58
59 /// IdentifierNamespace - According to C99 6.2.3, there are four namespaces,
Douglas Gregor2ce52f32008-04-13 21:07:44 +000060 /// labels, tags, members and ordinary identifiers. These are meant
61 /// as bitmasks, so that searches in C++ can look into the "tag" namespace
62 /// during ordinary lookup.
Chris Lattner0ed844b2008-04-04 06:12:32 +000063 enum IdentifierNamespace {
Douglas Gregor2ce52f32008-04-13 21:07:44 +000064 IDNS_Label = 0x1,
65 IDNS_Tag = 0x2,
66 IDNS_Member = 0x4,
Douglas Gregor7dda67d2009-02-05 19:25:20 +000067 IDNS_Ordinary = 0x8,
68 IDNS_Protocol = 0x10
Chris Lattner0ed844b2008-04-04 06:12:32 +000069 };
70
71 /// ObjCDeclQualifier - Qualifier used on types in method declarations
72 /// for remote messaging. They are meant for the arguments though and
73 /// applied to the Decls (ObjCMethodDecl and ParmVarDecl).
74 enum ObjCDeclQualifier {
75 OBJC_TQ_None = 0x0,
76 OBJC_TQ_In = 0x1,
77 OBJC_TQ_Inout = 0x2,
78 OBJC_TQ_Out = 0x4,
79 OBJC_TQ_Bycopy = 0x8,
80 OBJC_TQ_Byref = 0x10,
81 OBJC_TQ_Oneway = 0x20
82 };
83
84private:
85 /// Loc - The location that this decl.
86 SourceLocation Loc;
87
Douglas Gregor4afa39d2009-01-20 01:17:11 +000088 /// NextDeclarator - If this decl was part of a multi-declarator declaration,
89 /// such as "int X, Y, *Z;" this indicates Decl for the next declarator.
90 Decl *NextDeclarator;
91
92 /// NextDeclInScope - The next declaration within the same lexical
93 /// DeclContext. These pointers form the linked list that is
94 /// traversed via DeclContext's decls_begin()/decls_end().
95 /// FIXME: If NextDeclarator is non-NULL, will it always be the same
96 /// as NextDeclInScope? If so, we can use a
97 /// PointerIntPair<Decl*, 1> to make Decl smaller.
98 Decl *NextDeclInScope;
99
100 friend class DeclContext;
101
102 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
103 /// For declarations that don't contain C++ scope specifiers, it contains
104 /// the DeclContext where the Decl was declared.
105 /// For declarations with C++ scope specifiers, it contains a MultipleDC*
106 /// with the context where it semantically belongs (SemanticDC) and the
107 /// context where it was lexically declared (LexicalDC).
108 /// e.g.:
109 ///
110 /// namespace A {
111 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
112 /// }
113 /// void A::f(); // SemanticDC == namespace 'A'
114 /// // LexicalDC == global namespace
115 uintptr_t DeclCtx;
116
117 struct MultipleDC {
118 DeclContext *SemanticDC;
119 DeclContext *LexicalDC;
120 };
121
122 inline bool isInSemaDC() const { return (DeclCtx & 0x1) == 0; }
123 inline bool isOutOfSemaDC() const { return (DeclCtx & 0x1) != 0; }
124 inline MultipleDC *getMultipleDC() const {
125 return reinterpret_cast<MultipleDC*>(DeclCtx & ~0x1);
126 }
127
Chris Lattner0ed844b2008-04-04 06:12:32 +0000128 /// DeclKind - This indicates which class this is.
129 Kind DeclKind : 8;
130
131 /// InvalidDecl - This indicates a semantic error occurred.
132 unsigned int InvalidDecl : 1;
133
134 /// HasAttrs - This indicates whether the decl has attributes or not.
135 unsigned int HasAttrs : 1;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000136
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000137 /// Implicit - Whether this declaration was implicitly generated by
138 /// the implementation rather than explicitly written by the user.
139 bool Implicit : 1;
140
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000141protected:
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000142 /// Access - Used by C++ decls for the access specifier.
143 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
144 unsigned Access : 2;
145 friend class CXXClassMemberWrapper;
146
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000147 Decl(Kind DK, DeclContext *DC, SourceLocation L)
148 : Loc(L), NextDeclarator(0), NextDeclInScope(0),
149 DeclCtx(reinterpret_cast<uintptr_t>(DC)), DeclKind(DK), InvalidDecl(0),
150 HasAttrs(false), Implicit(false) {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000151 if (Decl::CollectingStats()) addDeclKind(DK);
152 }
Sam Bishop1bb19632008-04-11 18:04:39 +0000153
Chris Lattner0ed844b2008-04-04 06:12:32 +0000154 virtual ~Decl();
Sam Bishop1bb19632008-04-11 18:04:39 +0000155
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000156 /// setDeclContext - Set both the semantic and lexical DeclContext
157 /// to DC.
158 void setDeclContext(DeclContext *DC);
159
Sam Bishop1bb19632008-04-11 18:04:39 +0000160public:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000161 SourceLocation getLocation() const { return Loc; }
162 void setLocation(SourceLocation L) { Loc = L; }
163
164 Kind getKind() const { return DeclKind; }
165 const char *getDeclKindName() const;
166
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000167 const DeclContext *getDeclContext() const {
168 if (isInSemaDC())
169 return reinterpret_cast<DeclContext*>(DeclCtx);
170 return getMultipleDC()->SemanticDC;
171 }
172 DeclContext *getDeclContext() {
173 return const_cast<DeclContext*>(
174 const_cast<const Decl*>(this)->getDeclContext());
175 }
176
177 void setAccess(AccessSpecifier AS) { Access = AS; }
178 AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
179
Chris Lattner76a642f2009-02-15 22:43:40 +0000180 bool hasAttrs() const { return HasAttrs; }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000181 void addAttr(Attr *attr);
182 const Attr *getAttrs() const;
Chris Lattnera212c562008-05-04 02:29:49 +0000183 void swapAttrs(Decl *D);
Nuno Lopes9141bee2008-06-01 22:53:53 +0000184 void invalidateAttrs();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000185
186 template<typename T> const T *getAttr() const {
187 for (const Attr *attr = getAttrs(); attr; attr = attr->getNext())
188 if (const T *V = dyn_cast<T>(attr))
189 return V;
190
191 return 0;
192 }
193
194 /// setInvalidDecl - Indicates the Decl had a semantic error. This
195 /// allows for graceful error recovery.
196 void setInvalidDecl() { InvalidDecl = 1; }
197 bool isInvalidDecl() const { return (bool) InvalidDecl; }
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000198
199 /// isImplicit - Indicates whether the declaration was implicitly
200 /// generated by the implementation. If false, this declaration
201 /// was written explicitly in the source code.
202 bool isImplicit() const { return Implicit; }
203 void setImplicit(bool I = true) { Implicit = I; }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000204
205 IdentifierNamespace getIdentifierNamespace() const {
206 switch (DeclKind) {
Douglas Gregor9d350972008-12-12 08:25:50 +0000207 default:
208 if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast)
209 return IDNS_Ordinary;
210 assert(0 && "Unknown decl kind!");
Douglas Gregor3fd56d72009-01-23 21:30:56 +0000211 case OverloadedFunction:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000212 case Typedef:
Douglas Gregor3fd56d72009-01-23 21:30:56 +0000213 case EnumConstant:
Steve Naroff248a7532008-04-15 22:42:06 +0000214 case Var:
Douglas Gregor3fd56d72009-01-23 21:30:56 +0000215 case CXXClassVar:
216 case ImplicitParam:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000217 case ParmVar:
Fariborz Jahanian73da9e42008-12-20 20:56:12 +0000218 case OriginalParmVar:
Douglas Gregor72c3f312008-12-05 18:15:24 +0000219 case NonTypeTemplateParm:
Douglas Gregor3fd56d72009-01-23 21:30:56 +0000220 case ObjCMethod:
221 case ObjCContainer:
222 case ObjCCategory:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000223 case ObjCInterface:
Douglas Gregor3fd56d72009-01-23 21:30:56 +0000224 case ObjCCategoryImpl:
225 case ObjCProperty:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000226 case ObjCCompatibleAlias:
227 return IDNS_Ordinary;
Douglas Gregor72de6672009-01-08 20:45:30 +0000228
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000229 case ObjCProtocol:
230 return IDNS_Protocol;
231
Douglas Gregor72de6672009-01-08 20:45:30 +0000232 case Field:
233 case ObjCAtDefsField:
234 case ObjCIvar:
235 return IDNS_Member;
236
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000237 case Record:
238 case CXXRecord:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000239 case Enum:
Douglas Gregorefe38bd2009-01-23 22:28:29 +0000240 case TemplateTypeParm:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000241 return IDNS_Tag;
Douglas Gregor3fd56d72009-01-23 21:30:56 +0000242
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000243 case Namespace:
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000244 case Template:
245 case FunctionTemplate:
246 case ClassTemplate:
247 case TemplateTemplateParm:
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000248 return IdentifierNamespace(IDNS_Tag | IDNS_Ordinary);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000249 }
250 }
Ted Kremenek792481e2008-06-20 21:39:47 +0000251
Chris Lattnerd62fdc42009-01-06 07:16:40 +0000252 bool isInIdentifierNamespace(unsigned NS) const {
253 return getIdentifierNamespace() & NS;
254 }
255
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000256 /// getLexicalDeclContext - The declaration context where this Decl was
257 /// lexically declared (LexicalDC). May be different from
258 /// getDeclContext() (SemanticDC).
259 /// e.g.:
260 ///
261 /// namespace A {
262 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
263 /// }
264 /// void A::f(); // SemanticDC == namespace 'A'
265 /// // LexicalDC == global namespace
266 const DeclContext *getLexicalDeclContext() const {
267 if (isInSemaDC())
268 return reinterpret_cast<DeclContext*>(DeclCtx);
269 return getMultipleDC()->LexicalDC;
270 }
271 DeclContext *getLexicalDeclContext() {
272 return const_cast<DeclContext*>(
273 const_cast<const Decl*>(this)->getLexicalDeclContext());
274 }
275
276 void setLexicalDeclContext(DeclContext *DC);
277
278 /// getNextDeclarator - If this decl was part of a multi-declarator
279 /// declaration, such as "int X, Y, *Z;" this returns the decl for the next
280 /// declarator. Otherwise it returns null.
281 Decl *getNextDeclarator() { return NextDeclarator; }
282 const Decl *getNextDeclarator() const { return NextDeclarator; }
283 void setNextDeclarator(Decl *N) { NextDeclarator = N; }
284
285 // isDefinedOutsideFunctionOrMethod - This predicate returns true if this
286 // scoped decl is defined outside the current function or method. This is
287 // roughly global variables and functions, but also handles enums (which could
288 // be defined inside or outside a function etc).
289 bool isDefinedOutsideFunctionOrMethod() const;
290
Ted Kremenek69c8f0a2008-07-31 17:32:12 +0000291 // getBody - If this Decl represents a declaration for a body of code,
Ted Kremenek792481e2008-06-20 21:39:47 +0000292 // such as a function or method definition, this method returns the top-level
Ted Kremenek69c8f0a2008-07-31 17:32:12 +0000293 // Stmt* of that body. Otherwise this method returns null.
294 virtual Stmt* getBody() const { return 0; }
Ted Kremenek792481e2008-06-20 21:39:47 +0000295
Chris Lattner0ed844b2008-04-04 06:12:32 +0000296 // global temp stats (until we have a per-module visitor)
297 static void addDeclKind(Kind k);
298 static bool CollectingStats(bool Enable = false);
299 static void PrintStats();
300
Douglas Gregorf57172b2008-12-08 18:40:42 +0000301 /// isTemplateParameter - Determines whether this declartion is a
302 /// template parameter.
303 bool isTemplateParameter() const;
304
Chris Lattner0ed844b2008-04-04 06:12:32 +0000305 // Implement isa/cast/dyncast/etc.
306 static bool classof(const Decl *) { return true; }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000307 static DeclContext *castToDeclContext(const Decl *);
308 static Decl *castFromDeclContext(const DeclContext *);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000309
310 /// Emit - Serialize this Decl to Bitcode.
311 void Emit(llvm::Serializer& S) const;
312
313 /// Create - Deserialize a Decl from Bitcode.
Sam Bishope2563ca2008-04-07 21:55:54 +0000314 static Decl* Create(llvm::Deserializer& D, ASTContext& C);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000315
Sam Bishopbb45c512008-04-11 15:01:25 +0000316 /// Destroy - Call destructors and release memory.
Ted Kremenek27f8a282008-05-20 00:43:19 +0000317 virtual void Destroy(ASTContext& C);
Sam Bishopbb45c512008-04-11 15:01:25 +0000318
Chris Lattner0ed844b2008-04-04 06:12:32 +0000319protected:
320 /// EmitImpl - Provides the subclass-specific serialization logic for
321 /// serializing out a decl.
322 virtual void EmitImpl(llvm::Serializer& S) const {
323 // FIXME: This will eventually be a pure virtual function.
324 assert (false && "Not implemented.");
325 }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000326};
327
Chris Lattnerb048c982008-04-06 04:47:34 +0000328/// DeclContext - This is used only as base class of specific decl types that
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000329/// can act as declaration contexts. These decls are (only the top classes
330/// that directly derive from DeclContext are mentioned, not their subclasses):
Chris Lattner0ed844b2008-04-04 06:12:32 +0000331///
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000332/// TranslationUnitDecl
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000333/// NamespaceDecl
Chris Lattner0ed844b2008-04-04 06:12:32 +0000334/// FunctionDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000335/// TagDecl
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000336/// ObjCMethodDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000337/// ObjCContainerDecl
338/// ObjCCategoryImplDecl
339/// ObjCImplementationDecl
Douglas Gregor074149e2009-01-05 19:45:36 +0000340/// LinkageSpecDecl
Steve Naroff56ee6892008-10-08 17:01:13 +0000341/// BlockDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000342///
Chris Lattnerb048c982008-04-06 04:47:34 +0000343class DeclContext {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000344 /// DeclKind - This indicates which class this is.
345 Decl::Kind DeclKind : 8;
346
Douglas Gregor44b43212008-12-11 16:49:14 +0000347 /// LookupPtrKind - Describes what kind of pointer LookupPtr
348 /// actually is.
349 enum LookupPtrKind {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000350 /// LookupIsMap - Indicates that LookupPtr is actually a map.
Douglas Gregor44b43212008-12-11 16:49:14 +0000351 LookupIsMap = 7
352 };
353
354 /// LookupPtr - Pointer to a data structure used to lookup
355 /// declarations within this context. If the context contains fewer
356 /// than seven declarations, the number of declarations is provided
357 /// in the 3 lowest-order bits and the upper bits are treated as a
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000358 /// pointer to an array of NamedDecl pointers. If the context
Douglas Gregor44b43212008-12-11 16:49:14 +0000359 /// contains seven or more declarations, the upper bits are treated
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000360 /// as a pointer to a DenseMap<DeclarationName, std::vector<NamedDecl*>>.
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000361 /// FIXME: We need a better data structure for this.
Douglas Gregor44b43212008-12-11 16:49:14 +0000362 llvm::PointerIntPair<void*, 3> LookupPtr;
363
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000364 /// FirstDecl - The first declaration stored within this declaration
365 /// context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000366 Decl *FirstDecl;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000367
368 /// LastDecl - The last declaration stored within this declaration
369 /// context. FIXME: We could probably cache this value somewhere
370 /// outside of the DeclContext, to reduce the size of DeclContext by
371 /// another pointer.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000372 Decl *LastDecl;
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000373
Douglas Gregor44b43212008-12-11 16:49:14 +0000374 /// isLookupMap - Determine if the lookup structure is a
375 /// DenseMap. Othewise, it is an array.
376 bool isLookupMap() const { return LookupPtr.getInt() == LookupIsMap; }
377
Douglas Gregor0cba8552009-01-20 04:04:17 +0000378 static Decl *getNextDeclInScope(Decl *D) { return D->NextDeclInScope; }
379
Chris Lattner0ed844b2008-04-04 06:12:32 +0000380protected:
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000381 DeclContext(Decl::Kind K)
382 : DeclKind(K), LookupPtr(), FirstDecl(0), LastDecl(0) { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000383
384 void DestroyDecls(ASTContext &C);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000385
386public:
Douglas Gregor44b43212008-12-11 16:49:14 +0000387 ~DeclContext();
388
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000389 Decl::Kind getDeclKind() const {
390 return DeclKind;
391 }
Steve Naroff0a473932009-01-20 19:53:53 +0000392 const char *getDeclKindName() const;
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000393
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000394 /// getParent - Returns the containing DeclContext if this is a Decl,
Chris Lattner0ed844b2008-04-04 06:12:32 +0000395 /// else returns NULL.
Argyrios Kyrtzidis20bc6762008-11-19 17:36:39 +0000396 const DeclContext *getParent() const;
397 DeclContext *getParent() {
398 return const_cast<DeclContext*>(
399 const_cast<const DeclContext*>(this)->getParent());
Argyrios Kyrtzidisd2595ec2008-10-12 18:40:01 +0000400 }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000401
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000402 /// getLexicalParent - Returns the containing lexical DeclContext. May be
Douglas Gregor44b43212008-12-11 16:49:14 +0000403 /// different from getParent, e.g.:
404 ///
405 /// namespace A {
406 /// struct S;
407 /// }
408 /// struct A::S {}; // getParent() == namespace 'A'
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000409 /// // getLexicalParent() == translation unit
410 ///
411 const DeclContext *getLexicalParent() const;
412 DeclContext *getLexicalParent() {
413 return const_cast<DeclContext*>(
414 const_cast<const DeclContext*>(this)->getLexicalParent());
415 }
416
Chris Lattner0ed844b2008-04-04 06:12:32 +0000417 bool isFunctionOrMethod() const {
418 switch (DeclKind) {
Steve Naroff56ee6892008-10-08 17:01:13 +0000419 case Decl::Block:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000420 case Decl::ObjCMethod:
421 return true;
Douglas Gregor44b43212008-12-11 16:49:14 +0000422
Chris Lattner0ed844b2008-04-04 06:12:32 +0000423 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000424 if (DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast)
425 return true;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000426 return false;
427 }
428 }
429
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000430 bool isFileContext() const {
431 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
432 }
433
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000434 bool isTranslationUnit() const {
435 return DeclKind == Decl::TranslationUnit;
436 }
437
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000438 bool isRecord() const {
439 return DeclKind == Decl::Record || DeclKind == Decl::CXXRecord;
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +0000440 }
441
Douglas Gregor44b43212008-12-11 16:49:14 +0000442 bool isNamespace() const {
443 return DeclKind == Decl::Namespace;
444 }
445
Douglas Gregor074149e2009-01-05 19:45:36 +0000446 /// isTransparentContext - Determines whether this context is a
447 /// "transparent" context, meaning that the members declared in this
448 /// context are semantically declared in the nearest enclosing
449 /// non-transparent (opaque) context but are lexically declared in
450 /// this context. For example, consider the enumerators of an
451 /// enumeration type:
452 /// @code
453 /// enum E {
454 /// Val1
455 /// };
456 /// @endcode
457 /// Here, E is a transparent context, so its enumerator (Val1) will
458 /// appear (semantically) that it is in the same context of E.
459 /// Examples of transparent contexts include: enumerations (except for
460 /// C++0x scoped enums), C++ linkage specifications, and C++0x
461 /// inline namespaces.
462 bool isTransparentContext() const;
463
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000464 bool Encloses(DeclContext *DC) const {
465 for (; DC; DC = DC->getParent())
466 if (DC == this)
467 return true;
468 return false;
469 }
470
Douglas Gregor44b43212008-12-11 16:49:14 +0000471 /// getPrimaryContext - There may be many different
472 /// declarations of the same entity (including forward declarations
473 /// of classes, multiple definitions of namespaces, etc.), each with
474 /// a different set of declarations. This routine returns the
475 /// "primary" DeclContext structure, which will contain the
476 /// information needed to perform name lookup into this context.
Steve Naroff0701bbb2009-01-08 17:28:14 +0000477 DeclContext *getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000478
Douglas Gregorce356072009-01-06 23:51:29 +0000479 /// getLookupContext - Retrieve the innermost non-transparent
480 /// context of this context, which corresponds to the innermost
481 /// location from which name lookup can find the entities in this
482 /// context.
Douglas Gregor17a9b9e2009-01-07 02:48:43 +0000483 DeclContext *getLookupContext() {
484 return const_cast<DeclContext *>(
485 const_cast<const DeclContext *>(this)->getLookupContext());
486 }
487 const DeclContext *getLookupContext() const;
Douglas Gregorce356072009-01-06 23:51:29 +0000488
Douglas Gregor44b43212008-12-11 16:49:14 +0000489 /// getNextContext - If this is a DeclContext that may have other
490 /// DeclContexts that are semantically connected but syntactically
491 /// different, such as C++ namespaces, this routine retrieves the
492 /// next DeclContext in the link. Iteration through the chain of
493 /// DeclContexts should begin at the primary DeclContext and
494 /// continue until this function returns NULL. For example, given:
495 /// @code
496 /// namespace N {
497 /// int x;
498 /// }
499 /// namespace N {
500 /// int y;
501 /// }
502 /// @endcode
503 /// The first occurrence of namespace N will be the primary
504 /// DeclContext. Its getNextContext will return the second
505 /// occurrence of namespace N.
506 DeclContext *getNextContext();
507
508 /// decl_iterator - Iterates through the declarations stored
509 /// within this context.
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000510 class decl_iterator {
511 /// Current - The current declaration.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000512 Decl *Current;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000513
514 public:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000515 typedef Decl* value_type;
516 typedef Decl* reference;
517 typedef Decl* pointer;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000518 typedef std::forward_iterator_tag iterator_category;
519 typedef std::ptrdiff_t difference_type;
520
521 decl_iterator() : Current(0) { }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000522 explicit decl_iterator(Decl *C) : Current(C) { }
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000523
524 reference operator*() const { return Current; }
525 pointer operator->() const { return Current; }
526
527 decl_iterator& operator++();
528
529 decl_iterator operator++(int) {
530 decl_iterator tmp(*this);
531 ++(*this);
532 return tmp;
533 }
534
535 friend bool operator==(decl_iterator x, decl_iterator y) {
536 return x.Current == y.Current;
537 }
538 friend bool operator!=(decl_iterator x, decl_iterator y) {
539 return x.Current != y.Current;
540 }
541 };
Douglas Gregor44b43212008-12-11 16:49:14 +0000542
Douglas Gregor44b43212008-12-11 16:49:14 +0000543 /// decls_begin/decls_end - Iterate over the declarations stored in
544 /// this context.
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000545 decl_iterator decls_begin() const { return decl_iterator(FirstDecl); }
546 decl_iterator decls_end() const { return decl_iterator(); }
Douglas Gregor44b43212008-12-11 16:49:14 +0000547
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000548 /// specific_decl_iterator - Iterates over a subrange of
549 /// declarations stored in a DeclContext, providing only those that
Douglas Gregor669c9a22009-02-02 18:25:48 +0000550 /// are of type SpecificDecl (or a class derived from it). This
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000551 /// iterator is used, for example, to provide iteration over just
Douglas Gregor669c9a22009-02-02 18:25:48 +0000552 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000553 template<typename SpecificDecl>
554 class specific_decl_iterator {
555 /// Current - The current, underlying declaration iterator, which
Douglas Gregord6f0b4e2009-02-02 17:56:05 +0000556 /// will either be NULL or will point to a declaration of
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000557 /// type SpecificDecl.
558 DeclContext::decl_iterator Current;
559
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000560 /// SkipToNextDecl - Advances the current position up to the next
561 /// declaration of type SpecificDecl that also meets the criteria
562 /// required by Acceptable.
563 void SkipToNextDecl() {
Douglas Gregor669c9a22009-02-02 18:25:48 +0000564 while (*Current && !isa<SpecificDecl>(*Current))
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000565 ++Current;
566 }
567
568 public:
569 typedef SpecificDecl* value_type;
570 typedef SpecificDecl* reference;
571 typedef SpecificDecl* pointer;
572 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
573 difference_type;
574 typedef std::forward_iterator_tag iterator_category;
575
Douglas Gregor669c9a22009-02-02 18:25:48 +0000576 specific_decl_iterator() : Current() { }
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000577
578 /// specific_decl_iterator - Construct a new iterator over a
Douglas Gregord6f0b4e2009-02-02 17:56:05 +0000579 /// subset of the declarations the range [C,
580 /// end-of-declarations). If A is non-NULL, it is a pointer to a
581 /// member function of SpecificDecl that should return true for
582 /// all of the SpecificDecl instances that will be in the subset
583 /// of iterators. For example, if you want Objective-C instance
584 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
585 /// &ObjCMethodDecl::isInstanceMethod.
Douglas Gregor669c9a22009-02-02 18:25:48 +0000586 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000587 SkipToNextDecl();
588 }
589
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000590 reference operator*() const { return cast<SpecificDecl>(*Current); }
591 pointer operator->() const { return cast<SpecificDecl>(*Current); }
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000592
593 specific_decl_iterator& operator++() {
594 ++Current;
595 SkipToNextDecl();
596 return *this;
597 }
598
599 specific_decl_iterator operator++(int) {
600 specific_decl_iterator tmp(*this);
601 ++(*this);
602 return tmp;
603 }
604
605 friend bool
606 operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) {
607 return x.Current == y.Current;
608 }
609
610 friend bool
611 operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) {
612 return x.Current != y.Current;
613 }
614 };
615
Douglas Gregor669c9a22009-02-02 18:25:48 +0000616 /// \brief Iterates over a filtered subrange of declarations stored
617 /// in a DeclContext.
618 ///
619 /// This iterator visits only those declarations that are of type
620 /// SpecificDecl (or a class derived from it) and that meet some
621 /// additional run-time criteria. This iterator is used, for
622 /// example, to provide access to the instance methods within an
623 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
624 /// Acceptable = ObjCMethodDecl::isInstanceMethod).
625 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
626 class filtered_decl_iterator {
627 /// Current - The current, underlying declaration iterator, which
628 /// will either be NULL or will point to a declaration of
629 /// type SpecificDecl.
630 DeclContext::decl_iterator Current;
631
632 /// SkipToNextDecl - Advances the current position up to the next
633 /// declaration of type SpecificDecl that also meets the criteria
634 /// required by Acceptable.
635 void SkipToNextDecl() {
636 while (*Current &&
637 (!isa<SpecificDecl>(*Current) ||
638 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
639 ++Current;
640 }
641
642 public:
643 typedef SpecificDecl* value_type;
644 typedef SpecificDecl* reference;
645 typedef SpecificDecl* pointer;
646 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
647 difference_type;
648 typedef std::forward_iterator_tag iterator_category;
649
650 filtered_decl_iterator() : Current() { }
651
652 /// specific_decl_iterator - Construct a new iterator over a
653 /// subset of the declarations the range [C,
654 /// end-of-declarations). If A is non-NULL, it is a pointer to a
655 /// member function of SpecificDecl that should return true for
656 /// all of the SpecificDecl instances that will be in the subset
657 /// of iterators. For example, if you want Objective-C instance
658 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
659 /// &ObjCMethodDecl::isInstanceMethod.
660 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
661 SkipToNextDecl();
662 }
663
664 reference operator*() const { return cast<SpecificDecl>(*Current); }
665 pointer operator->() const { return cast<SpecificDecl>(*Current); }
666
667 filtered_decl_iterator& operator++() {
668 ++Current;
669 SkipToNextDecl();
670 return *this;
671 }
672
673 filtered_decl_iterator operator++(int) {
674 filtered_decl_iterator tmp(*this);
675 ++(*this);
676 return tmp;
677 }
678
679 friend bool
680 operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
681 return x.Current == y.Current;
682 }
683
684 friend bool
685 operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
686 return x.Current != y.Current;
687 }
688 };
689
Douglas Gregor40f4e692009-01-20 16:54:50 +0000690 /// @brief Add the declaration D into this context.
691 ///
692 /// This routine should be invoked when the declaration D has first
693 /// been declared, to place D into the context where it was
694 /// (lexically) defined. Every declaration must be added to one
695 /// (and only one!) context, where it can be visited via
696 /// [decls_begin(), decls_end()). Once a declaration has been added
697 /// to its lexical context, the corresponding DeclContext owns the
698 /// declaration.
699 ///
700 /// If D is also a NamedDecl, it will be made visible within its
701 /// semantic context via makeDeclVisibleInContext.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000702 void addDecl(Decl *D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000703
Douglas Gregor44b43212008-12-11 16:49:14 +0000704 /// lookup_iterator - An iterator that provides access to the results
705 /// of looking up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000706 typedef NamedDecl **lookup_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +0000707
708 /// lookup_const_iterator - An iterator that provides non-mutable
709 /// access to the results of lookup up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000710 typedef NamedDecl * const * lookup_const_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +0000711
712 typedef std::pair<lookup_iterator, lookup_iterator> lookup_result;
713 typedef std::pair<lookup_const_iterator, lookup_const_iterator>
714 lookup_const_result;
715
716 /// lookup - Find the declarations (if any) with the given Name in
717 /// this context. Returns a range of iterators that contains all of
Douglas Gregor40f4e692009-01-20 16:54:50 +0000718 /// the declarations with this name, with object, function, member,
719 /// and enumerator names preceding any tag name. Note that this
720 /// routine will not look into parent contexts.
Steve Naroff0701bbb2009-01-08 17:28:14 +0000721 lookup_result lookup(DeclarationName Name);
722 lookup_const_result lookup(DeclarationName Name) const;
Douglas Gregor44b43212008-12-11 16:49:14 +0000723
Douglas Gregor40f4e692009-01-20 16:54:50 +0000724 /// @brief Makes a declaration visible within this context.
725 ///
726 /// This routine makes the declaration D visible to name lookup
727 /// within this context and, if this is a transparent context,
728 /// within its parent contexts up to the first enclosing
729 /// non-transparent context. Making a declaration visible within a
730 /// context does not transfer ownership of a declaration, and a
731 /// declaration can be visible in many contexts that aren't its
732 /// lexical context.
733 ///
734 /// If D is a redeclaration of an existing declaration that is
735 /// visible from this context, as determined by
736 /// NamedDecl::declarationReplaces, the previous declaration will be
737 /// replaced with D.
738 void makeDeclVisibleInContext(NamedDecl *D);
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000739
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000740 /// udir_iterator - Iterates through the using-directives stored
741 /// within this context.
742 typedef UsingDirectiveDecl * const * udir_iterator;
743
744 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range;
745
746 udir_iterator_range getUsingDirectives() const;
747
748 udir_iterator using_directives_begin() const {
749 return getUsingDirectives().first;
750 }
751
752 udir_iterator using_directives_end() const {
753 return getUsingDirectives().second;
754 }
755
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000756 static bool classof(const Decl *D);
Chris Lattnerb048c982008-04-06 04:47:34 +0000757 static bool classof(const DeclContext *D) { return true; }
Douglas Gregor64650af2009-02-02 23:39:07 +0000758#define DECL_CONTEXT(Name) \
759 static bool classof(const Name##Decl *D) { return true; }
760#include "clang/AST/DeclNodes.def"
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000761
762private:
Douglas Gregor40f4e692009-01-20 16:54:50 +0000763 void buildLookup(DeclContext *DCtx);
764 void makeDeclVisibleInContextImpl(NamedDecl *D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000765
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000766 void EmitOutRec(llvm::Serializer& S) const;
767 void ReadOutRec(llvm::Deserializer& D, ASTContext& C);
768
769 friend class Decl;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000770};
771
Douglas Gregorf57172b2008-12-08 18:40:42 +0000772inline bool Decl::isTemplateParameter() const {
773 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm;
774}
775
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000776inline bool Decl::isDefinedOutsideFunctionOrMethod() const {
777 if (getDeclContext())
778 return !getDeclContext()->getLookupContext()->isFunctionOrMethod();
779 else
780 return true;
781}
782
783inline DeclContext::decl_iterator& DeclContext::decl_iterator::operator++() {
Douglas Gregor0cba8552009-01-20 04:04:17 +0000784 Current = getNextDeclInScope(Current);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000785 return *this;
786}
787
Chris Lattner0ed844b2008-04-04 06:12:32 +0000788} // end clang.
789
790namespace llvm {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000791
792/// Implement a isa_impl_wrap specialization to check whether a DeclContext is
793/// a specific Decl.
794template<class ToTy>
795struct isa_impl_wrap<ToTy,
796 const ::clang::DeclContext,const ::clang::DeclContext> {
797 static bool doit(const ::clang::DeclContext &Val) {
798 return ToTy::classof(::clang::Decl::castFromDeclContext(&Val));
Chris Lattner0ed844b2008-04-04 06:12:32 +0000799 }
800};
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000801template<class ToTy>
802struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext>
803 : public isa_impl_wrap<ToTy,
804 const ::clang::DeclContext,const ::clang::DeclContext> {};
Chris Lattner0ed844b2008-04-04 06:12:32 +0000805
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000806/// Implement cast_convert_val for Decl -> DeclContext conversions.
Chris Lattner0ed844b2008-04-04 06:12:32 +0000807template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000808struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
Chris Lattnerb048c982008-04-06 04:47:34 +0000809 static ::clang::DeclContext &doit(const FromTy &Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000810 return *FromTy::castToDeclContext(&Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000811 }
812};
Chris Lattner0ed844b2008-04-04 06:12:32 +0000813
814template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000815struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
Chris Lattnerb048c982008-04-06 04:47:34 +0000816 static ::clang::DeclContext *doit(const FromTy *Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000817 return FromTy::castToDeclContext(Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000818 }
819};
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000820
Douglas Gregor44b43212008-12-11 16:49:14 +0000821template<class FromTy>
822struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
823 static const ::clang::DeclContext &doit(const FromTy &Val) {
824 return *FromTy::castToDeclContext(&Val);
825 }
826};
827
828template<class FromTy>
829struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
830 static const ::clang::DeclContext *doit(const FromTy *Val) {
831 return FromTy::castToDeclContext(Val);
832 }
833};
834
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000835/// Implement cast_convert_val for DeclContext -> Decl conversions.
836template<class ToTy>
837struct cast_convert_val<ToTy,
838 const ::clang::DeclContext,const ::clang::DeclContext> {
839 static ToTy &doit(const ::clang::DeclContext &Val) {
840 return *reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(&Val));
841 }
842};
843template<class ToTy>
844struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext>
845 : public cast_convert_val<ToTy,
846 const ::clang::DeclContext,const ::clang::DeclContext> {};
847
848template<class ToTy>
849struct cast_convert_val<ToTy,
850 const ::clang::DeclContext*, const ::clang::DeclContext*> {
851 static ToTy *doit(const ::clang::DeclContext *Val) {
852 return reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(Val));
853 }
854};
855template<class ToTy>
856struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*>
857 : public cast_convert_val<ToTy,
858 const ::clang::DeclContext*,const ::clang::DeclContext*> {};
Chris Lattner0ed844b2008-04-04 06:12:32 +0000859
860} // end namespace llvm
861
862#endif