blob: ff8a1c1323b2d6c78e0d8ae0fe76b8e1886ad991 [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
22namespace clang {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +000023class DeclContext;
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000024class TranslationUnitDecl;
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000025class NamespaceDecl;
Douglas Gregor2a3009a2009-02-03 19:21:40 +000026class UsingDirectiveDecl;
Douglas Gregor44b43212008-12-11 16:49:14 +000027class NamedDecl;
Chris Lattner0ed844b2008-04-04 06:12:32 +000028class FunctionDecl;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000029class CXXRecordDecl;
Chris Lattnerb048c982008-04-06 04:47:34 +000030class EnumDecl;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000031class ObjCMethodDecl;
Douglas Gregor64650af2009-02-02 23:39:07 +000032class ObjCContainerDecl;
Chris Lattner0ed844b2008-04-04 06:12:32 +000033class ObjCInterfaceDecl;
Steve Naroff0701bbb2009-01-08 17:28:14 +000034class ObjCCategoryDecl;
35class ObjCProtocolDecl;
36class ObjCImplementationDecl;
37class ObjCCategoryImplDecl;
Douglas Gregor074149e2009-01-05 19:45:36 +000038class LinkageSpecDecl;
Steve Naroff56ee6892008-10-08 17:01:13 +000039class BlockDecl;
Douglas Gregor44b43212008-12-11 16:49:14 +000040class DeclarationName;
Chris Lattner0ed844b2008-04-04 06:12:32 +000041
42/// Decl - This represents one declaration (or definition), e.g. a variable,
43/// typedef, function, struct, etc.
44///
45class Decl {
46public:
Douglas Gregor64650af2009-02-02 23:39:07 +000047 /// \brief Lists the kind of concrete classes of Decl.
Chris Lattner0ed844b2008-04-04 06:12:32 +000048 enum Kind {
Douglas Gregor64650af2009-02-02 23:39:07 +000049#define DECL(Derived, Base) Derived,
50#define DECL_RANGE(CommonBase, Start, End) \
51 CommonBase##First = Start, CommonBase##Last = End,
52#define LAST_DECL_RANGE(CommonBase, Start, End) \
53 CommonBase##First = Start, CommonBase##Last = End
54#include "clang/AST/DeclNodes.def"
Chris Lattner0ed844b2008-04-04 06:12:32 +000055 };
56
57 /// IdentifierNamespace - According to C99 6.2.3, there are four namespaces,
Douglas Gregor2ce52f32008-04-13 21:07:44 +000058 /// labels, tags, members and ordinary identifiers. These are meant
59 /// as bitmasks, so that searches in C++ can look into the "tag" namespace
60 /// during ordinary lookup.
Chris Lattner0ed844b2008-04-04 06:12:32 +000061 enum IdentifierNamespace {
Douglas Gregor2ce52f32008-04-13 21:07:44 +000062 IDNS_Label = 0x1,
63 IDNS_Tag = 0x2,
64 IDNS_Member = 0x4,
Douglas Gregor7dda67d2009-02-05 19:25:20 +000065 IDNS_Ordinary = 0x8,
66 IDNS_Protocol = 0x10
Chris Lattner0ed844b2008-04-04 06:12:32 +000067 };
68
69 /// ObjCDeclQualifier - Qualifier used on types in method declarations
70 /// for remote messaging. They are meant for the arguments though and
71 /// applied to the Decls (ObjCMethodDecl and ParmVarDecl).
72 enum ObjCDeclQualifier {
73 OBJC_TQ_None = 0x0,
74 OBJC_TQ_In = 0x1,
75 OBJC_TQ_Inout = 0x2,
76 OBJC_TQ_Out = 0x4,
77 OBJC_TQ_Bycopy = 0x8,
78 OBJC_TQ_Byref = 0x10,
79 OBJC_TQ_Oneway = 0x20
80 };
81
82private:
83 /// Loc - The location that this decl.
84 SourceLocation Loc;
85
Douglas Gregor4afa39d2009-01-20 01:17:11 +000086 /// NextDeclarator - If this decl was part of a multi-declarator declaration,
87 /// such as "int X, Y, *Z;" this indicates Decl for the next declarator.
88 Decl *NextDeclarator;
89
90 /// NextDeclInScope - The next declaration within the same lexical
91 /// DeclContext. These pointers form the linked list that is
92 /// traversed via DeclContext's decls_begin()/decls_end().
93 /// FIXME: If NextDeclarator is non-NULL, will it always be the same
94 /// as NextDeclInScope? If so, we can use a
95 /// PointerIntPair<Decl*, 1> to make Decl smaller.
96 Decl *NextDeclInScope;
97
98 friend class DeclContext;
99
100 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
101 /// For declarations that don't contain C++ scope specifiers, it contains
102 /// the DeclContext where the Decl was declared.
103 /// For declarations with C++ scope specifiers, it contains a MultipleDC*
104 /// with the context where it semantically belongs (SemanticDC) and the
105 /// context where it was lexically declared (LexicalDC).
106 /// e.g.:
107 ///
108 /// namespace A {
109 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
110 /// }
111 /// void A::f(); // SemanticDC == namespace 'A'
112 /// // LexicalDC == global namespace
113 uintptr_t DeclCtx;
114
115 struct MultipleDC {
116 DeclContext *SemanticDC;
117 DeclContext *LexicalDC;
118 };
119
120 inline bool isInSemaDC() const { return (DeclCtx & 0x1) == 0; }
121 inline bool isOutOfSemaDC() const { return (DeclCtx & 0x1) != 0; }
122 inline MultipleDC *getMultipleDC() const {
123 return reinterpret_cast<MultipleDC*>(DeclCtx & ~0x1);
124 }
125
Chris Lattner0ed844b2008-04-04 06:12:32 +0000126 /// DeclKind - This indicates which class this is.
127 Kind DeclKind : 8;
128
129 /// InvalidDecl - This indicates a semantic error occurred.
130 unsigned int InvalidDecl : 1;
131
132 /// HasAttrs - This indicates whether the decl has attributes or not.
133 unsigned int HasAttrs : 1;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000134
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000135 /// Implicit - Whether this declaration was implicitly generated by
136 /// the implementation rather than explicitly written by the user.
137 bool Implicit : 1;
138
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000139protected:
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000140 /// Access - Used by C++ decls for the access specifier.
141 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
142 unsigned Access : 2;
143 friend class CXXClassMemberWrapper;
144
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000145 Decl(Kind DK, DeclContext *DC, SourceLocation L)
146 : Loc(L), NextDeclarator(0), NextDeclInScope(0),
147 DeclCtx(reinterpret_cast<uintptr_t>(DC)), DeclKind(DK), InvalidDecl(0),
148 HasAttrs(false), Implicit(false) {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000149 if (Decl::CollectingStats()) addDeclKind(DK);
150 }
Sam Bishop1bb19632008-04-11 18:04:39 +0000151
Chris Lattner0ed844b2008-04-04 06:12:32 +0000152 virtual ~Decl();
Sam Bishop1bb19632008-04-11 18:04:39 +0000153
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000154 /// setDeclContext - Set both the semantic and lexical DeclContext
155 /// to DC.
156 void setDeclContext(DeclContext *DC);
157
Sam Bishop1bb19632008-04-11 18:04:39 +0000158public:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000159 SourceLocation getLocation() const { return Loc; }
160 void setLocation(SourceLocation L) { Loc = L; }
161
162 Kind getKind() const { return DeclKind; }
163 const char *getDeclKindName() const;
164
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000165 const DeclContext *getDeclContext() const {
166 if (isInSemaDC())
167 return reinterpret_cast<DeclContext*>(DeclCtx);
168 return getMultipleDC()->SemanticDC;
169 }
170 DeclContext *getDeclContext() {
171 return const_cast<DeclContext*>(
172 const_cast<const Decl*>(this)->getDeclContext());
173 }
174
175 void setAccess(AccessSpecifier AS) { Access = AS; }
176 AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
177
Chris Lattner76a642f2009-02-15 22:43:40 +0000178 bool hasAttrs() const { return HasAttrs; }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000179 void addAttr(Attr *attr);
180 const Attr *getAttrs() const;
Chris Lattnera212c562008-05-04 02:29:49 +0000181 void swapAttrs(Decl *D);
Nuno Lopes9141bee2008-06-01 22:53:53 +0000182 void invalidateAttrs();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000183
184 template<typename T> const T *getAttr() const {
185 for (const Attr *attr = getAttrs(); attr; attr = attr->getNext())
186 if (const T *V = dyn_cast<T>(attr))
187 return V;
188
189 return 0;
190 }
191
192 /// setInvalidDecl - Indicates the Decl had a semantic error. This
193 /// allows for graceful error recovery.
194 void setInvalidDecl() { InvalidDecl = 1; }
195 bool isInvalidDecl() const { return (bool) InvalidDecl; }
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000196
197 /// isImplicit - Indicates whether the declaration was implicitly
198 /// generated by the implementation. If false, this declaration
199 /// was written explicitly in the source code.
200 bool isImplicit() const { return Implicit; }
201 void setImplicit(bool I = true) { Implicit = I; }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000202
Douglas Gregorcc636682009-02-17 23:15:12 +0000203 unsigned getIdentifierNamespace() const {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000204 switch (DeclKind) {
Douglas Gregor9d350972008-12-12 08:25:50 +0000205 default:
206 if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast)
207 return IDNS_Ordinary;
208 assert(0 && "Unknown decl kind!");
Douglas Gregor3fd56d72009-01-23 21:30:56 +0000209 case OverloadedFunction:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000210 case Typedef:
Douglas Gregor3fd56d72009-01-23 21:30:56 +0000211 case EnumConstant:
Steve Naroff248a7532008-04-15 22:42:06 +0000212 case Var:
Douglas Gregor3fd56d72009-01-23 21:30:56 +0000213 case CXXClassVar:
214 case ImplicitParam:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000215 case ParmVar:
Fariborz Jahanian73da9e42008-12-20 20:56:12 +0000216 case OriginalParmVar:
Douglas Gregor72c3f312008-12-05 18:15:24 +0000217 case NonTypeTemplateParm:
Douglas Gregor3fd56d72009-01-23 21:30:56 +0000218 case ObjCMethod:
219 case ObjCContainer:
220 case ObjCCategory:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000221 case ObjCInterface:
Douglas Gregor3fd56d72009-01-23 21:30:56 +0000222 case ObjCCategoryImpl:
223 case ObjCProperty:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000224 case ObjCCompatibleAlias:
225 return IDNS_Ordinary;
Douglas Gregor72de6672009-01-08 20:45:30 +0000226
Douglas Gregor7dda67d2009-02-05 19:25:20 +0000227 case ObjCProtocol:
228 return IDNS_Protocol;
229
Douglas Gregor72de6672009-01-08 20:45:30 +0000230 case Field:
231 case ObjCAtDefsField:
232 case ObjCIvar:
233 return IDNS_Member;
234
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +0000235 case Record:
236 case CXXRecord:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000237 case Enum:
Douglas Gregorefe38bd2009-01-23 22:28:29 +0000238 case TemplateTypeParm:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000239 return IDNS_Tag;
Douglas Gregor3fd56d72009-01-23 21:30:56 +0000240
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000241 case Namespace:
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000242 case Template:
243 case FunctionTemplate:
244 case ClassTemplate:
245 case TemplateTemplateParm:
Douglas Gregorcc636682009-02-17 23:15:12 +0000246 return IDNS_Tag | IDNS_Ordinary;
247
248 case ClassTemplateSpecialization:
249 return 0;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000250 }
251 }
Ted Kremenek792481e2008-06-20 21:39:47 +0000252
Chris Lattnerd62fdc42009-01-06 07:16:40 +0000253 bool isInIdentifierNamespace(unsigned NS) const {
254 return getIdentifierNamespace() & NS;
255 }
256
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000257 /// getLexicalDeclContext - The declaration context where this Decl was
258 /// lexically declared (LexicalDC). May be different from
259 /// getDeclContext() (SemanticDC).
260 /// e.g.:
261 ///
262 /// namespace A {
263 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
264 /// }
265 /// void A::f(); // SemanticDC == namespace 'A'
266 /// // LexicalDC == global namespace
267 const DeclContext *getLexicalDeclContext() const {
268 if (isInSemaDC())
269 return reinterpret_cast<DeclContext*>(DeclCtx);
270 return getMultipleDC()->LexicalDC;
271 }
272 DeclContext *getLexicalDeclContext() {
273 return const_cast<DeclContext*>(
274 const_cast<const Decl*>(this)->getLexicalDeclContext());
275 }
276
277 void setLexicalDeclContext(DeclContext *DC);
278
279 /// getNextDeclarator - If this decl was part of a multi-declarator
280 /// declaration, such as "int X, Y, *Z;" this returns the decl for the next
281 /// declarator. Otherwise it returns null.
282 Decl *getNextDeclarator() { return NextDeclarator; }
283 const Decl *getNextDeclarator() const { return NextDeclarator; }
284 void setNextDeclarator(Decl *N) { NextDeclarator = N; }
285
286 // isDefinedOutsideFunctionOrMethod - This predicate returns true if this
287 // scoped decl is defined outside the current function or method. This is
288 // roughly global variables and functions, but also handles enums (which could
289 // be defined inside or outside a function etc).
290 bool isDefinedOutsideFunctionOrMethod() const;
291
Ted Kremenek69c8f0a2008-07-31 17:32:12 +0000292 // getBody - If this Decl represents a declaration for a body of code,
Ted Kremenek792481e2008-06-20 21:39:47 +0000293 // such as a function or method definition, this method returns the top-level
Ted Kremenek69c8f0a2008-07-31 17:32:12 +0000294 // Stmt* of that body. Otherwise this method returns null.
295 virtual Stmt* getBody() const { return 0; }
Ted Kremenek792481e2008-06-20 21:39:47 +0000296
Chris Lattner0ed844b2008-04-04 06:12:32 +0000297 // global temp stats (until we have a per-module visitor)
298 static void addDeclKind(Kind k);
299 static bool CollectingStats(bool Enable = false);
300 static void PrintStats();
301
Douglas Gregorf57172b2008-12-08 18:40:42 +0000302 /// isTemplateParameter - Determines whether this declartion is a
303 /// template parameter.
304 bool isTemplateParameter() const;
305
Chris Lattner0ed844b2008-04-04 06:12:32 +0000306 // Implement isa/cast/dyncast/etc.
307 static bool classof(const Decl *) { return true; }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000308 static DeclContext *castToDeclContext(const Decl *);
309 static Decl *castFromDeclContext(const DeclContext *);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000310
311 /// Emit - Serialize this Decl to Bitcode.
312 void Emit(llvm::Serializer& S) const;
313
314 /// Create - Deserialize a Decl from Bitcode.
Sam Bishope2563ca2008-04-07 21:55:54 +0000315 static Decl* Create(llvm::Deserializer& D, ASTContext& C);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000316
Sam Bishopbb45c512008-04-11 15:01:25 +0000317 /// Destroy - Call destructors and release memory.
Ted Kremenek27f8a282008-05-20 00:43:19 +0000318 virtual void Destroy(ASTContext& C);
Sam Bishopbb45c512008-04-11 15:01:25 +0000319
Chris Lattner0ed844b2008-04-04 06:12:32 +0000320protected:
321 /// EmitImpl - Provides the subclass-specific serialization logic for
322 /// serializing out a decl.
323 virtual void EmitImpl(llvm::Serializer& S) const {
324 // FIXME: This will eventually be a pure virtual function.
325 assert (false && "Not implemented.");
326 }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000327};
328
Chris Lattnerb048c982008-04-06 04:47:34 +0000329/// DeclContext - This is used only as base class of specific decl types that
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000330/// can act as declaration contexts. These decls are (only the top classes
331/// that directly derive from DeclContext are mentioned, not their subclasses):
Chris Lattner0ed844b2008-04-04 06:12:32 +0000332///
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000333/// TranslationUnitDecl
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000334/// NamespaceDecl
Chris Lattner0ed844b2008-04-04 06:12:32 +0000335/// FunctionDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000336/// TagDecl
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000337/// ObjCMethodDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000338/// ObjCContainerDecl
339/// ObjCCategoryImplDecl
340/// ObjCImplementationDecl
Douglas Gregor074149e2009-01-05 19:45:36 +0000341/// LinkageSpecDecl
Steve Naroff56ee6892008-10-08 17:01:13 +0000342/// BlockDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000343///
Chris Lattnerb048c982008-04-06 04:47:34 +0000344class DeclContext {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000345 /// DeclKind - This indicates which class this is.
346 Decl::Kind DeclKind : 8;
347
Douglas Gregor44b43212008-12-11 16:49:14 +0000348 /// LookupPtrKind - Describes what kind of pointer LookupPtr
349 /// actually is.
350 enum LookupPtrKind {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000351 /// LookupIsMap - Indicates that LookupPtr is actually a map.
Douglas Gregor44b43212008-12-11 16:49:14 +0000352 LookupIsMap = 7
353 };
354
355 /// LookupPtr - Pointer to a data structure used to lookup
356 /// declarations within this context. If the context contains fewer
357 /// than seven declarations, the number of declarations is provided
358 /// in the 3 lowest-order bits and the upper bits are treated as a
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000359 /// pointer to an array of NamedDecl pointers. If the context
Douglas Gregor44b43212008-12-11 16:49:14 +0000360 /// contains seven or more declarations, the upper bits are treated
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000361 /// as a pointer to a DenseMap<DeclarationName, std::vector<NamedDecl*>>.
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000362 /// FIXME: We need a better data structure for this.
Douglas Gregor44b43212008-12-11 16:49:14 +0000363 llvm::PointerIntPair<void*, 3> LookupPtr;
364
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000365 /// FirstDecl - The first declaration stored within this declaration
366 /// context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000367 Decl *FirstDecl;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000368
369 /// LastDecl - The last declaration stored within this declaration
370 /// context. FIXME: We could probably cache this value somewhere
371 /// outside of the DeclContext, to reduce the size of DeclContext by
372 /// another pointer.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000373 Decl *LastDecl;
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000374
Douglas Gregor44b43212008-12-11 16:49:14 +0000375 /// isLookupMap - Determine if the lookup structure is a
376 /// DenseMap. Othewise, it is an array.
377 bool isLookupMap() const { return LookupPtr.getInt() == LookupIsMap; }
378
Douglas Gregor0cba8552009-01-20 04:04:17 +0000379 static Decl *getNextDeclInScope(Decl *D) { return D->NextDeclInScope; }
380
Chris Lattner0ed844b2008-04-04 06:12:32 +0000381protected:
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000382 DeclContext(Decl::Kind K)
383 : DeclKind(K), LookupPtr(), FirstDecl(0), LastDecl(0) { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000384
385 void DestroyDecls(ASTContext &C);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000386
387public:
Douglas Gregor44b43212008-12-11 16:49:14 +0000388 ~DeclContext();
389
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000390 Decl::Kind getDeclKind() const {
391 return DeclKind;
392 }
Steve Naroff0a473932009-01-20 19:53:53 +0000393 const char *getDeclKindName() const;
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000394
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +0000395 /// getParent - Returns the containing DeclContext.
396 const DeclContext *getParent() const {
397 return cast<Decl>(this)->getDeclContext();
398 }
Argyrios Kyrtzidis20bc6762008-11-19 17:36:39 +0000399 DeclContext *getParent() {
400 return const_cast<DeclContext*>(
401 const_cast<const DeclContext*>(this)->getParent());
Argyrios Kyrtzidisd2595ec2008-10-12 18:40:01 +0000402 }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000403
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000404 /// getLexicalParent - Returns the containing lexical DeclContext. May be
Douglas Gregor44b43212008-12-11 16:49:14 +0000405 /// different from getParent, e.g.:
406 ///
407 /// namespace A {
408 /// struct S;
409 /// }
410 /// struct A::S {}; // getParent() == namespace 'A'
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000411 /// // getLexicalParent() == translation unit
412 ///
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +0000413 const DeclContext *getLexicalParent() const {
414 return cast<Decl>(this)->getLexicalDeclContext();
415 }
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000416 DeclContext *getLexicalParent() {
417 return const_cast<DeclContext*>(
418 const_cast<const DeclContext*>(this)->getLexicalParent());
419 }
420
Chris Lattner0ed844b2008-04-04 06:12:32 +0000421 bool isFunctionOrMethod() const {
422 switch (DeclKind) {
Steve Naroff56ee6892008-10-08 17:01:13 +0000423 case Decl::Block:
Chris Lattner0ed844b2008-04-04 06:12:32 +0000424 case Decl::ObjCMethod:
425 return true;
Douglas Gregor44b43212008-12-11 16:49:14 +0000426
Chris Lattner0ed844b2008-04-04 06:12:32 +0000427 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000428 if (DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast)
429 return true;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000430 return false;
431 }
432 }
433
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000434 bool isFileContext() const {
435 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
436 }
437
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000438 bool isTranslationUnit() const {
439 return DeclKind == Decl::TranslationUnit;
440 }
441
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000442 bool isRecord() const {
Douglas Gregor65100792009-02-26 00:02:51 +0000443 return DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast;
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +0000444 }
445
Douglas Gregor44b43212008-12-11 16:49:14 +0000446 bool isNamespace() const {
447 return DeclKind == Decl::Namespace;
448 }
449
Douglas Gregor074149e2009-01-05 19:45:36 +0000450 /// isTransparentContext - Determines whether this context is a
451 /// "transparent" context, meaning that the members declared in this
452 /// context are semantically declared in the nearest enclosing
453 /// non-transparent (opaque) context but are lexically declared in
454 /// this context. For example, consider the enumerators of an
455 /// enumeration type:
456 /// @code
457 /// enum E {
458 /// Val1
459 /// };
460 /// @endcode
461 /// Here, E is a transparent context, so its enumerator (Val1) will
462 /// appear (semantically) that it is in the same context of E.
463 /// Examples of transparent contexts include: enumerations (except for
464 /// C++0x scoped enums), C++ linkage specifications, and C++0x
465 /// inline namespaces.
466 bool isTransparentContext() const;
467
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000468 bool Encloses(DeclContext *DC) const {
469 for (; DC; DC = DC->getParent())
470 if (DC == this)
471 return true;
472 return false;
473 }
474
Douglas Gregor44b43212008-12-11 16:49:14 +0000475 /// getPrimaryContext - There may be many different
476 /// declarations of the same entity (including forward declarations
477 /// of classes, multiple definitions of namespaces, etc.), each with
478 /// a different set of declarations. This routine returns the
479 /// "primary" DeclContext structure, which will contain the
480 /// information needed to perform name lookup into this context.
Steve Naroff0701bbb2009-01-08 17:28:14 +0000481 DeclContext *getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000482
Douglas Gregorce356072009-01-06 23:51:29 +0000483 /// getLookupContext - Retrieve the innermost non-transparent
484 /// context of this context, which corresponds to the innermost
485 /// location from which name lookup can find the entities in this
486 /// context.
Douglas Gregor17a9b9e2009-01-07 02:48:43 +0000487 DeclContext *getLookupContext() {
488 return const_cast<DeclContext *>(
489 const_cast<const DeclContext *>(this)->getLookupContext());
490 }
491 const DeclContext *getLookupContext() const;
Douglas Gregorce356072009-01-06 23:51:29 +0000492
Douglas Gregor88b70942009-02-25 22:02:03 +0000493 /// \brief Retrieve the nearest enclosing namespace context.
494 DeclContext *getEnclosingNamespaceContext();
495 const DeclContext *getEnclosingNamespaceContext() const {
496 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
497 }
498
Douglas Gregor44b43212008-12-11 16:49:14 +0000499 /// getNextContext - If this is a DeclContext that may have other
500 /// DeclContexts that are semantically connected but syntactically
501 /// different, such as C++ namespaces, this routine retrieves the
502 /// next DeclContext in the link. Iteration through the chain of
503 /// DeclContexts should begin at the primary DeclContext and
504 /// continue until this function returns NULL. For example, given:
505 /// @code
506 /// namespace N {
507 /// int x;
508 /// }
509 /// namespace N {
510 /// int y;
511 /// }
512 /// @endcode
513 /// The first occurrence of namespace N will be the primary
514 /// DeclContext. Its getNextContext will return the second
515 /// occurrence of namespace N.
516 DeclContext *getNextContext();
517
518 /// decl_iterator - Iterates through the declarations stored
519 /// within this context.
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000520 class decl_iterator {
521 /// Current - The current declaration.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000522 Decl *Current;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000523
524 public:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000525 typedef Decl* value_type;
526 typedef Decl* reference;
527 typedef Decl* pointer;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000528 typedef std::forward_iterator_tag iterator_category;
529 typedef std::ptrdiff_t difference_type;
530
531 decl_iterator() : Current(0) { }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000532 explicit decl_iterator(Decl *C) : Current(C) { }
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000533
534 reference operator*() const { return Current; }
535 pointer operator->() const { return Current; }
536
537 decl_iterator& operator++();
538
539 decl_iterator operator++(int) {
540 decl_iterator tmp(*this);
541 ++(*this);
542 return tmp;
543 }
544
545 friend bool operator==(decl_iterator x, decl_iterator y) {
546 return x.Current == y.Current;
547 }
548 friend bool operator!=(decl_iterator x, decl_iterator y) {
549 return x.Current != y.Current;
550 }
551 };
Douglas Gregor44b43212008-12-11 16:49:14 +0000552
Douglas Gregor44b43212008-12-11 16:49:14 +0000553 /// decls_begin/decls_end - Iterate over the declarations stored in
554 /// this context.
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000555 decl_iterator decls_begin() const { return decl_iterator(FirstDecl); }
556 decl_iterator decls_end() const { return decl_iterator(); }
Douglas Gregor44b43212008-12-11 16:49:14 +0000557
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000558 /// specific_decl_iterator - Iterates over a subrange of
559 /// declarations stored in a DeclContext, providing only those that
Douglas Gregor669c9a22009-02-02 18:25:48 +0000560 /// are of type SpecificDecl (or a class derived from it). This
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000561 /// iterator is used, for example, to provide iteration over just
Douglas Gregor669c9a22009-02-02 18:25:48 +0000562 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000563 template<typename SpecificDecl>
564 class specific_decl_iterator {
565 /// Current - The current, underlying declaration iterator, which
Douglas Gregord6f0b4e2009-02-02 17:56:05 +0000566 /// will either be NULL or will point to a declaration of
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000567 /// type SpecificDecl.
568 DeclContext::decl_iterator Current;
569
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000570 /// SkipToNextDecl - Advances the current position up to the next
571 /// declaration of type SpecificDecl that also meets the criteria
572 /// required by Acceptable.
573 void SkipToNextDecl() {
Douglas Gregor669c9a22009-02-02 18:25:48 +0000574 while (*Current && !isa<SpecificDecl>(*Current))
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000575 ++Current;
576 }
577
578 public:
579 typedef SpecificDecl* value_type;
580 typedef SpecificDecl* reference;
581 typedef SpecificDecl* pointer;
582 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
583 difference_type;
584 typedef std::forward_iterator_tag iterator_category;
585
Douglas Gregor669c9a22009-02-02 18:25:48 +0000586 specific_decl_iterator() : Current() { }
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000587
588 /// specific_decl_iterator - Construct a new iterator over a
Douglas Gregord6f0b4e2009-02-02 17:56:05 +0000589 /// subset of the declarations the range [C,
590 /// end-of-declarations). If A is non-NULL, it is a pointer to a
591 /// member function of SpecificDecl that should return true for
592 /// all of the SpecificDecl instances that will be in the subset
593 /// of iterators. For example, if you want Objective-C instance
594 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
595 /// &ObjCMethodDecl::isInstanceMethod.
Douglas Gregor669c9a22009-02-02 18:25:48 +0000596 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000597 SkipToNextDecl();
598 }
599
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000600 reference operator*() const { return cast<SpecificDecl>(*Current); }
601 pointer operator->() const { return cast<SpecificDecl>(*Current); }
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000602
603 specific_decl_iterator& operator++() {
604 ++Current;
605 SkipToNextDecl();
606 return *this;
607 }
608
609 specific_decl_iterator operator++(int) {
610 specific_decl_iterator tmp(*this);
611 ++(*this);
612 return tmp;
613 }
614
615 friend bool
616 operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) {
617 return x.Current == y.Current;
618 }
619
620 friend bool
621 operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) {
622 return x.Current != y.Current;
623 }
624 };
625
Douglas Gregor669c9a22009-02-02 18:25:48 +0000626 /// \brief Iterates over a filtered subrange of declarations stored
627 /// in a DeclContext.
628 ///
629 /// This iterator visits only those declarations that are of type
630 /// SpecificDecl (or a class derived from it) and that meet some
631 /// additional run-time criteria. This iterator is used, for
632 /// example, to provide access to the instance methods within an
633 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
634 /// Acceptable = ObjCMethodDecl::isInstanceMethod).
635 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
636 class filtered_decl_iterator {
637 /// Current - The current, underlying declaration iterator, which
638 /// will either be NULL or will point to a declaration of
639 /// type SpecificDecl.
640 DeclContext::decl_iterator Current;
641
642 /// SkipToNextDecl - Advances the current position up to the next
643 /// declaration of type SpecificDecl that also meets the criteria
644 /// required by Acceptable.
645 void SkipToNextDecl() {
646 while (*Current &&
647 (!isa<SpecificDecl>(*Current) ||
648 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
649 ++Current;
650 }
651
652 public:
653 typedef SpecificDecl* value_type;
654 typedef SpecificDecl* reference;
655 typedef SpecificDecl* pointer;
656 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
657 difference_type;
658 typedef std::forward_iterator_tag iterator_category;
659
660 filtered_decl_iterator() : Current() { }
661
662 /// specific_decl_iterator - Construct a new iterator over a
663 /// subset of the declarations the range [C,
664 /// end-of-declarations). If A is non-NULL, it is a pointer to a
665 /// member function of SpecificDecl that should return true for
666 /// all of the SpecificDecl instances that will be in the subset
667 /// of iterators. For example, if you want Objective-C instance
668 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
669 /// &ObjCMethodDecl::isInstanceMethod.
670 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
671 SkipToNextDecl();
672 }
673
674 reference operator*() const { return cast<SpecificDecl>(*Current); }
675 pointer operator->() const { return cast<SpecificDecl>(*Current); }
676
677 filtered_decl_iterator& operator++() {
678 ++Current;
679 SkipToNextDecl();
680 return *this;
681 }
682
683 filtered_decl_iterator operator++(int) {
684 filtered_decl_iterator tmp(*this);
685 ++(*this);
686 return tmp;
687 }
688
689 friend bool
690 operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
691 return x.Current == y.Current;
692 }
693
694 friend bool
695 operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
696 return x.Current != y.Current;
697 }
698 };
699
Douglas Gregor40f4e692009-01-20 16:54:50 +0000700 /// @brief Add the declaration D into this context.
701 ///
702 /// This routine should be invoked when the declaration D has first
703 /// been declared, to place D into the context where it was
704 /// (lexically) defined. Every declaration must be added to one
705 /// (and only one!) context, where it can be visited via
706 /// [decls_begin(), decls_end()). Once a declaration has been added
707 /// to its lexical context, the corresponding DeclContext owns the
708 /// declaration.
709 ///
710 /// If D is also a NamedDecl, it will be made visible within its
711 /// semantic context via makeDeclVisibleInContext.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000712 void addDecl(Decl *D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000713
Douglas Gregor44b43212008-12-11 16:49:14 +0000714 /// lookup_iterator - An iterator that provides access to the results
715 /// of looking up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000716 typedef NamedDecl **lookup_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +0000717
718 /// lookup_const_iterator - An iterator that provides non-mutable
719 /// access to the results of lookup up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000720 typedef NamedDecl * const * lookup_const_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +0000721
722 typedef std::pair<lookup_iterator, lookup_iterator> lookup_result;
723 typedef std::pair<lookup_const_iterator, lookup_const_iterator>
724 lookup_const_result;
725
726 /// lookup - Find the declarations (if any) with the given Name in
727 /// this context. Returns a range of iterators that contains all of
Douglas Gregor40f4e692009-01-20 16:54:50 +0000728 /// the declarations with this name, with object, function, member,
729 /// and enumerator names preceding any tag name. Note that this
730 /// routine will not look into parent contexts.
Steve Naroff0701bbb2009-01-08 17:28:14 +0000731 lookup_result lookup(DeclarationName Name);
732 lookup_const_result lookup(DeclarationName Name) const;
Douglas Gregor44b43212008-12-11 16:49:14 +0000733
Douglas Gregor40f4e692009-01-20 16:54:50 +0000734 /// @brief Makes a declaration visible within this context.
735 ///
736 /// This routine makes the declaration D visible to name lookup
737 /// within this context and, if this is a transparent context,
738 /// within its parent contexts up to the first enclosing
739 /// non-transparent context. Making a declaration visible within a
740 /// context does not transfer ownership of a declaration, and a
741 /// declaration can be visible in many contexts that aren't its
742 /// lexical context.
743 ///
744 /// If D is a redeclaration of an existing declaration that is
745 /// visible from this context, as determined by
746 /// NamedDecl::declarationReplaces, the previous declaration will be
747 /// replaced with D.
748 void makeDeclVisibleInContext(NamedDecl *D);
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000749
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000750 /// udir_iterator - Iterates through the using-directives stored
751 /// within this context.
752 typedef UsingDirectiveDecl * const * udir_iterator;
753
754 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range;
755
756 udir_iterator_range getUsingDirectives() const;
757
758 udir_iterator using_directives_begin() const {
759 return getUsingDirectives().first;
760 }
761
762 udir_iterator using_directives_end() const {
763 return getUsingDirectives().second;
764 }
765
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000766 static bool classof(const Decl *D);
Chris Lattnerb048c982008-04-06 04:47:34 +0000767 static bool classof(const DeclContext *D) { return true; }
Douglas Gregor64650af2009-02-02 23:39:07 +0000768#define DECL_CONTEXT(Name) \
769 static bool classof(const Name##Decl *D) { return true; }
770#include "clang/AST/DeclNodes.def"
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000771
772private:
Douglas Gregor40f4e692009-01-20 16:54:50 +0000773 void buildLookup(DeclContext *DCtx);
774 void makeDeclVisibleInContextImpl(NamedDecl *D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000775
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000776 void EmitOutRec(llvm::Serializer& S) const;
777 void ReadOutRec(llvm::Deserializer& D, ASTContext& C);
778
779 friend class Decl;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000780};
781
Douglas Gregorf57172b2008-12-08 18:40:42 +0000782inline bool Decl::isTemplateParameter() const {
783 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm;
784}
785
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000786inline bool Decl::isDefinedOutsideFunctionOrMethod() const {
787 if (getDeclContext())
788 return !getDeclContext()->getLookupContext()->isFunctionOrMethod();
789 else
790 return true;
791}
792
793inline DeclContext::decl_iterator& DeclContext::decl_iterator::operator++() {
Douglas Gregor0cba8552009-01-20 04:04:17 +0000794 Current = getNextDeclInScope(Current);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000795 return *this;
796}
797
Chris Lattner0ed844b2008-04-04 06:12:32 +0000798} // end clang.
799
800namespace llvm {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000801
802/// Implement a isa_impl_wrap specialization to check whether a DeclContext is
803/// a specific Decl.
804template<class ToTy>
805struct isa_impl_wrap<ToTy,
806 const ::clang::DeclContext,const ::clang::DeclContext> {
807 static bool doit(const ::clang::DeclContext &Val) {
808 return ToTy::classof(::clang::Decl::castFromDeclContext(&Val));
Chris Lattner0ed844b2008-04-04 06:12:32 +0000809 }
810};
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000811template<class ToTy>
812struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext>
813 : public isa_impl_wrap<ToTy,
814 const ::clang::DeclContext,const ::clang::DeclContext> {};
Chris Lattner0ed844b2008-04-04 06:12:32 +0000815
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000816/// Implement cast_convert_val for Decl -> DeclContext conversions.
Chris Lattner0ed844b2008-04-04 06:12:32 +0000817template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000818struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
Chris Lattnerb048c982008-04-06 04:47:34 +0000819 static ::clang::DeclContext &doit(const FromTy &Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000820 return *FromTy::castToDeclContext(&Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000821 }
822};
Chris Lattner0ed844b2008-04-04 06:12:32 +0000823
824template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000825struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
Chris Lattnerb048c982008-04-06 04:47:34 +0000826 static ::clang::DeclContext *doit(const FromTy *Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000827 return FromTy::castToDeclContext(Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000828 }
829};
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000830
Douglas Gregor44b43212008-12-11 16:49:14 +0000831template<class FromTy>
832struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
833 static const ::clang::DeclContext &doit(const FromTy &Val) {
834 return *FromTy::castToDeclContext(&Val);
835 }
836};
837
838template<class FromTy>
839struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
840 static const ::clang::DeclContext *doit(const FromTy *Val) {
841 return FromTy::castToDeclContext(Val);
842 }
843};
844
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000845/// Implement cast_convert_val for DeclContext -> Decl conversions.
846template<class ToTy>
847struct cast_convert_val<ToTy,
848 const ::clang::DeclContext,const ::clang::DeclContext> {
849 static ToTy &doit(const ::clang::DeclContext &Val) {
850 return *reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(&Val));
851 }
852};
853template<class ToTy>
854struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext>
855 : public cast_convert_val<ToTy,
856 const ::clang::DeclContext,const ::clang::DeclContext> {};
857
858template<class ToTy>
859struct cast_convert_val<ToTy,
860 const ::clang::DeclContext*, const ::clang::DeclContext*> {
861 static ToTy *doit(const ::clang::DeclContext *Val) {
862 return reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(Val));
863 }
864};
865template<class ToTy>
866struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*>
867 : public cast_convert_val<ToTy,
868 const ::clang::DeclContext*,const ::clang::DeclContext*> {};
Chris Lattner0ed844b2008-04-04 06:12:32 +0000869
870} // end namespace llvm
871
872#endif