blob: 2a2f8100442527b9221552bdf5e6fd7f8e37c11b [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;
John McCall0c01d182010-03-24 05:22:00 +000044class StoredDeclsMap;
45class DependentDiagnostic;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000046class ASTMutationListener;
Chris Lattner0eda3b32009-03-29 04:32:54 +000047}
48
49namespace llvm {
50// DeclContext* is only 4-byte aligned on 32-bit systems.
51template<>
52 class PointerLikeTypeTraits<clang::DeclContext*> {
53 typedef clang::DeclContext* PT;
54public:
55 static inline void *getAsVoidPointer(PT P) { return P; }
56 static inline PT getFromVoidPointer(void *P) {
57 return static_cast<PT>(P);
58 }
59 enum { NumLowBitsAvailable = 2 };
60};
61}
62
63namespace clang {
Chris Lattner0ed844b2008-04-04 06:12:32 +000064
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000065 /// \brief Captures the result of checking the availability of a
66 /// declaration.
67 enum AvailabilityResult {
68 AR_Available = 0,
69 AR_NotYetIntroduced,
70 AR_Deprecated,
71 AR_Unavailable
72 };
73
Mike Stump1eb44332009-09-09 15:08:12 +000074/// Decl - This represents one declaration (or definition), e.g. a variable,
75/// typedef, function, struct, etc.
Chris Lattner0ed844b2008-04-04 06:12:32 +000076///
77class Decl {
78public:
Douglas Gregor64650af2009-02-02 23:39:07 +000079 /// \brief Lists the kind of concrete classes of Decl.
Chris Lattner0ed844b2008-04-04 06:12:32 +000080 enum Kind {
Sean Hunt9a555912010-05-30 07:21:58 +000081#define DECL(DERIVED, BASE) DERIVED,
82#define ABSTRACT_DECL(DECL)
83#define DECL_RANGE(BASE, START, END) \
84 first##BASE = START, last##BASE = END,
85#define LAST_DECL_RANGE(BASE, START, END) \
86 first##BASE = START, last##BASE = END
87#include "clang/AST/DeclNodes.inc"
Chris Lattner0ed844b2008-04-04 06:12:32 +000088 };
89
Chris Lattner6ad9ac02010-05-07 21:43:38 +000090 /// \brief A placeholder type used to construct an empty shell of a
91 /// decl-derived type that will be filled in later (e.g., by some
92 /// deserialization method).
93 struct EmptyShell { };
94
John McCall0d6b1642010-04-23 18:46:30 +000095 /// IdentifierNamespace - The different namespaces in which
96 /// declarations may appear. According to C99 6.2.3, there are
97 /// four namespaces, labels, tags, members and ordinary
98 /// identifiers. C++ describes lookup completely differently:
99 /// certain lookups merely "ignore" certain kinds of declarations,
100 /// usually based on whether the declaration is of a type, etc.
101 ///
102 /// These are meant as bitmasks, so that searches in
103 /// C++ can look into the "tag" namespace during ordinary lookup.
104 ///
Sebastian Redl07a353c2010-07-14 20:26:45 +0000105 /// Decl currently provides 15 bits of IDNS bits.
Chris Lattner0ed844b2008-04-04 06:12:32 +0000106 enum IdentifierNamespace {
John McCall0d6b1642010-04-23 18:46:30 +0000107 /// Labels, declared with 'x:' and referenced with 'goto x'.
108 IDNS_Label = 0x0001,
109
110 /// Tags, declared with 'struct foo;' and referenced with
111 /// 'struct foo'. All tags are also types. This is what
112 /// elaborated-type-specifiers look for in C.
113 IDNS_Tag = 0x0002,
114
115 /// Types, declared with 'struct foo', typedefs, etc.
116 /// This is what elaborated-type-specifiers look for in C++,
117 /// but note that it's ill-formed to find a non-tag.
118 IDNS_Type = 0x0004,
119
120 /// Members, declared with object declarations within tag
121 /// definitions. In C, these can only be found by "qualified"
122 /// lookup in member expressions. In C++, they're found by
123 /// normal lookup.
124 IDNS_Member = 0x0008,
125
126 /// Namespaces, declared with 'namespace foo {}'.
127 /// Lookup for nested-name-specifiers find these.
128 IDNS_Namespace = 0x0010,
129
130 /// Ordinary names. In C, everything that's not a label, tag,
131 /// or member ends up here.
132 IDNS_Ordinary = 0x0020,
133
134 /// Objective C @protocol.
135 IDNS_ObjCProtocol = 0x0040,
136
137 /// This declaration is a friend function. A friend function
138 /// declaration is always in this namespace but may also be in
139 /// IDNS_Ordinary if it was previously declared.
140 IDNS_OrdinaryFriend = 0x0080,
141
142 /// This declaration is a friend class. A friend class
143 /// declaration is always in this namespace but may also be in
144 /// IDNS_Tag|IDNS_Type if it was previously declared.
145 IDNS_TagFriend = 0x0100,
146
147 /// This declaration is a using declaration. A using declaration
148 /// *introduces* a number of other declarations into the current
149 /// scope, and those declarations use the IDNS of their targets,
150 /// but the actual using declarations go in this namespace.
John McCall76d32642010-04-24 01:30:58 +0000151 IDNS_Using = 0x0200,
152
153 /// This declaration is a C++ operator declared in a non-class
154 /// context. All such operators are also in IDNS_Ordinary.
155 /// C++ lexical operator lookup looks for these.
156 IDNS_NonMemberOperator = 0x0400
Chris Lattner0ed844b2008-04-04 06:12:32 +0000157 };
Mike Stump1eb44332009-09-09 15:08:12 +0000158
John McCall09e2c522011-05-01 03:04:29 +0000159 /// ObjCDeclQualifier - 'Qualifiers' written next to the return and
160 /// parameter types in method declarations. Other than remembering
161 /// them and mangling them into the method's signature string, these
162 /// are ignored by the compiler; they are consumed by certain
163 /// remote-messaging frameworks.
164 ///
165 /// in, inout, and out are mutually exclusive and apply only to
166 /// method parameters. bycopy and byref are mutually exclusive and
167 /// apply only to method parameters (?). oneway applies only to
168 /// results. All of these expect their corresponding parameter to
169 /// have a particular type. None of this is currently enforced by
170 /// clang.
171 ///
172 /// This should be kept in sync with ObjCDeclSpec::ObjCDeclQualifier.
Chris Lattner0ed844b2008-04-04 06:12:32 +0000173 enum ObjCDeclQualifier {
174 OBJC_TQ_None = 0x0,
175 OBJC_TQ_In = 0x1,
176 OBJC_TQ_Inout = 0x2,
177 OBJC_TQ_Out = 0x4,
178 OBJC_TQ_Bycopy = 0x8,
179 OBJC_TQ_Byref = 0x10,
180 OBJC_TQ_Oneway = 0x20
181 };
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Chris Lattner0ed844b2008-04-04 06:12:32 +0000183private:
Chris Lattner244a67d2009-03-28 06:04:26 +0000184 /// NextDeclInContext - The next declaration within the same lexical
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000185 /// DeclContext. These pointers form the linked list that is
186 /// traversed via DeclContext's decls_begin()/decls_end().
Chris Lattner244a67d2009-03-28 06:04:26 +0000187 Decl *NextDeclInContext;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000188
189 friend class DeclContext;
190
Chris Lattneree219fd2009-03-29 06:06:59 +0000191 struct MultipleDC {
192 DeclContext *SemanticDC;
193 DeclContext *LexicalDC;
194 };
Mike Stump1eb44332009-09-09 15:08:12 +0000195
196
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000197 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
198 /// For declarations that don't contain C++ scope specifiers, it contains
199 /// the DeclContext where the Decl was declared.
200 /// For declarations with C++ scope specifiers, it contains a MultipleDC*
201 /// with the context where it semantically belongs (SemanticDC) and the
202 /// context where it was lexically declared (LexicalDC).
203 /// e.g.:
204 ///
205 /// namespace A {
206 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
207 /// }
208 /// void A::f(); // SemanticDC == namespace 'A'
209 /// // LexicalDC == global namespace
Chris Lattneree219fd2009-03-29 06:06:59 +0000210 llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000211
Chris Lattneree219fd2009-03-29 06:06:59 +0000212 inline bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); }
213 inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000214 inline MultipleDC *getMultipleDC() const {
Chris Lattneree219fd2009-03-29 06:06:59 +0000215 return DeclCtx.get<MultipleDC*>();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000216 }
Chris Lattner10d83792009-03-27 18:46:15 +0000217 inline DeclContext *getSemanticDC() const {
Chris Lattneree219fd2009-03-29 06:06:59 +0000218 return DeclCtx.get<DeclContext*>();
Chris Lattner10d83792009-03-27 18:46:15 +0000219 }
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Douglas Gregor381d34e2010-12-06 18:36:25 +0000221 /// Loc - The location of this decl.
Daniel Dunbar39d76502009-03-04 02:26:41 +0000222 SourceLocation Loc;
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Chris Lattner0ed844b2008-04-04 06:12:32 +0000224 /// DeclKind - This indicates which class this is.
John McCall35043e52010-10-19 05:43:52 +0000225 unsigned DeclKind : 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Chris Lattner0ed844b2008-04-04 06:12:32 +0000227 /// InvalidDecl - This indicates a semantic error occurred.
John McCall35043e52010-10-19 05:43:52 +0000228 unsigned InvalidDecl : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Chris Lattner0ed844b2008-04-04 06:12:32 +0000230 /// HasAttrs - This indicates whether the decl has attributes or not.
John McCall35043e52010-10-19 05:43:52 +0000231 unsigned HasAttrs : 1;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000232
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000233 /// Implicit - Whether this declaration was implicitly generated by
234 /// the implementation rather than explicitly written by the user.
John McCall35043e52010-10-19 05:43:52 +0000235 unsigned Implicit : 1;
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000236
Douglas Gregore0762c92009-06-19 23:52:42 +0000237 /// \brief Whether this declaration was "used", meaning that a definition is
238 /// required.
John McCall35043e52010-10-19 05:43:52 +0000239 unsigned Used : 1;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000240
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000241 /// \brief Whether this declaration was "referenced".
242 /// The difference with 'Used' is whether the reference appears in a
243 /// evaluated context or not, e.g. functions used in uninstantiated templates
244 /// are regarded as "referenced" but not "used".
245 unsigned Referenced : 1;
Douglas Gregor8d267c52011-09-09 02:06:17 +0000246
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000247protected:
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000248 /// Access - Used by C++ decls for the access specifier.
249 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
250 unsigned Access : 2;
251 friend class CXXClassMemberWrapper;
Sebastian Redl0b17c612010-08-13 00:28:03 +0000252
Douglas Gregor08e0bc12011-09-10 00:09:20 +0000253 /// \brief Whether this declaration was loaded from an AST file.
254 unsigned FromASTFile : 1;
255
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000256 /// ChangedAfterLoad - if this declaration has changed since being loaded
John McCall35043e52010-10-19 05:43:52 +0000257 unsigned ChangedAfterLoad : 1;
Sebastian Redl0b17c612010-08-13 00:28:03 +0000258
Douglas Gregor8d267c52011-09-09 02:06:17 +0000259 /// \brief Whether this declaration is private to the module in which it was
260 /// defined.
261 unsigned ModulePrivate : 1;
262
Chris Lattner769dbdf2009-03-27 20:18:19 +0000263 /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in.
Douglas Gregor381d34e2010-12-06 18:36:25 +0000264 unsigned IdentifierNamespace : 12;
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Douglas Gregor381d34e2010-12-06 18:36:25 +0000266 /// \brief Whether the \c CachedLinkage field is active.
267 ///
268 /// This field is only valid for NamedDecls subclasses.
269 mutable unsigned HasCachedLinkage : 1;
270
271 /// \brief If \c HasCachedLinkage, the linkage of this declaration.
272 ///
273 /// This field is only valid for NamedDecls subclasses.
274 mutable unsigned CachedLinkage : 2;
275
Douglas Gregor8d267c52011-09-09 02:06:17 +0000276 friend class ASTDeclWriter;
277 friend class ASTDeclReader;
278
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000279private:
Anders Carlsson1329c272009-03-25 23:38:06 +0000280 void CheckAccessDeclContext() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000282protected:
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000283
Mike Stump1eb44332009-09-09 15:08:12 +0000284 Decl(Kind DK, DeclContext *DC, SourceLocation L)
285 : NextDeclInContext(0), DeclCtx(DC),
Daniel Dunbar39d76502009-03-04 02:26:41 +0000286 Loc(L), DeclKind(DK), InvalidDecl(0),
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000287 HasAttrs(false), Implicit(false), Used(false), Referenced(false),
Douglas Gregor08e0bc12011-09-10 00:09:20 +0000288 Access(AS_none), FromASTFile(0), ChangedAfterLoad(false),
Douglas Gregor8d267c52011-09-09 02:06:17 +0000289 ModulePrivate(0),
Douglas Gregor381d34e2010-12-06 18:36:25 +0000290 IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
291 HasCachedLinkage(0)
292 {
Sean Hunt9a555912010-05-30 07:21:58 +0000293 if (Decl::CollectingStats()) add(DK);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000294 }
Sam Bishop1bb19632008-04-11 18:04:39 +0000295
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +0000296 Decl(Kind DK, EmptyShell Empty)
297 : NextDeclInContext(0), DeclKind(DK), InvalidDecl(0),
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000298 HasAttrs(false), Implicit(false), Used(false), Referenced(false),
Douglas Gregor08e0bc12011-09-10 00:09:20 +0000299 Access(AS_none), FromASTFile(0), ChangedAfterLoad(false),
Douglas Gregor8d267c52011-09-09 02:06:17 +0000300 ModulePrivate(0),
Douglas Gregor381d34e2010-12-06 18:36:25 +0000301 IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
302 HasCachedLinkage(0)
303 {
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +0000304 if (Decl::CollectingStats()) add(DK);
305 }
306
Douglas Gregorda2142f2011-02-19 18:51:44 +0000307 virtual ~Decl();
308
Sam Bishop1bb19632008-04-11 18:04:39 +0000309public:
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000310
311 /// \brief Source range that this declaration covers.
Douglas Gregorda2142f2011-02-19 18:51:44 +0000312 virtual SourceRange getSourceRange() const {
313 return SourceRange(getLocation(), getLocation());
314 }
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000315 SourceLocation getLocStart() const { return getSourceRange().getBegin(); }
316 SourceLocation getLocEnd() const { return getSourceRange().getEnd(); }
317
Chris Lattner0ed844b2008-04-04 06:12:32 +0000318 SourceLocation getLocation() const { return Loc; }
319 void setLocation(SourceLocation L) { Loc = L; }
320
John McCall35043e52010-10-19 05:43:52 +0000321 Kind getKind() const { return static_cast<Kind>(DeclKind); }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000322 const char *getDeclKindName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Chris Lattner244a67d2009-03-28 06:04:26 +0000324 Decl *getNextDeclInContext() { return NextDeclInContext; }
325 const Decl *getNextDeclInContext() const { return NextDeclInContext; }
Chris Lattner96f44682009-03-28 05:59:45 +0000326
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000327 DeclContext *getDeclContext() {
Chris Lattner10d83792009-03-27 18:46:15 +0000328 if (isInSemaDC())
329 return getSemanticDC();
330 return getMultipleDC()->SemanticDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000331 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000332 const DeclContext *getDeclContext() const {
333 return const_cast<Decl*>(this)->getDeclContext();
334 }
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000335
John McCallaab9e312011-02-22 22:25:23 +0000336 /// Finds the innermost non-closure context of this declaration.
337 /// That is, walk out the DeclContext chain, skipping any blocks.
338 DeclContext *getNonClosureContext();
339 const DeclContext *getNonClosureContext() const {
340 return const_cast<Decl*>(this)->getNonClosureContext();
341 }
342
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000343 TranslationUnitDecl *getTranslationUnitDecl();
344 const TranslationUnitDecl *getTranslationUnitDecl() const {
345 return const_cast<Decl*>(this)->getTranslationUnitDecl();
346 }
347
John McCall9aeed322009-10-01 00:25:31 +0000348 bool isInAnonymousNamespace() const;
349
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000350 ASTContext &getASTContext() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Anders Carlsson1329c272009-03-25 23:38:06 +0000352 void setAccess(AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000353 Access = AS;
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000354#ifndef NDEBUG
Anders Carlsson1329c272009-03-25 23:38:06 +0000355 CheckAccessDeclContext();
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000356#endif
Anders Carlssonb8547e82009-03-25 20:19:57 +0000357 }
Mike Stump1eb44332009-09-09 15:08:12 +0000358
359 AccessSpecifier getAccess() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000360#ifndef NDEBUG
Anders Carlsson1329c272009-03-25 23:38:06 +0000361 CheckAccessDeclContext();
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000362#endif
Mike Stump1eb44332009-09-09 15:08:12 +0000363 return AccessSpecifier(Access);
Anders Carlsson1329c272009-03-25 23:38:06 +0000364 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000365
Chris Lattner76a642f2009-02-15 22:43:40 +0000366 bool hasAttrs() const { return HasAttrs; }
Sean Huntcf807c42010-08-18 23:23:40 +0000367 void setAttrs(const AttrVec& Attrs);
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000368 AttrVec &getAttrs() {
Sean Huntcf807c42010-08-18 23:23:40 +0000369 return const_cast<AttrVec&>(const_cast<const Decl*>(this)->getAttrs());
Chris Lattner81abbdd2009-03-21 06:27:31 +0000370 }
Sean Huntcf807c42010-08-18 23:23:40 +0000371 const AttrVec &getAttrs() const;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000372 void swapAttrs(Decl *D);
Sean Huntcf807c42010-08-18 23:23:40 +0000373 void dropAttrs();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000374
Douglas Gregorb5f35ba2010-12-06 17:49:01 +0000375 void addAttr(Attr *A) {
376 if (hasAttrs())
377 getAttrs().push_back(A);
378 else
379 setAttrs(AttrVec(1, A));
380 }
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Sean Huntcf807c42010-08-18 23:23:40 +0000382 typedef AttrVec::const_iterator attr_iterator;
383
384 // FIXME: Do not rely on iterators having comparable singular values.
385 // Note that this should error out if they do not.
386 attr_iterator attr_begin() const {
387 return hasAttrs() ? getAttrs().begin() : 0;
388 }
389 attr_iterator attr_end() const {
390 return hasAttrs() ? getAttrs().end() : 0;
391 }
Fariborz Jahanianc3ca14d2011-06-23 17:50:10 +0000392
393 template <typename T>
Fariborz Jahanian8cf0f522011-06-23 20:24:38 +0000394 void dropAttr() {
395 if (!HasAttrs) return;
396
397 AttrVec &Attrs = getAttrs();
398 for (unsigned i = 0, e = Attrs.size(); i != e; /* in loop */) {
399 if (isa<T>(Attrs[i])) {
400 Attrs.erase(Attrs.begin() + i);
401 --e;
402 }
403 else
404 ++i;
405 }
406 if (Attrs.empty())
407 HasAttrs = false;
408 }
409
Sean Huntcf807c42010-08-18 23:23:40 +0000410 template <typename T>
411 specific_attr_iterator<T> specific_attr_begin() const {
412 return specific_attr_iterator<T>(attr_begin());
413 }
414 template <typename T>
415 specific_attr_iterator<T> specific_attr_end() const {
416 return specific_attr_iterator<T>(attr_end());
417 }
418
419 template<typename T> T *getAttr() const {
420 return hasAttrs() ? getSpecificAttr<T>(getAttrs()) : 0;
421 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000422 template<typename T> bool hasAttr() const {
Sean Huntcf807c42010-08-18 23:23:40 +0000423 return hasAttrs() && hasSpecificAttr<T>(getAttrs());
424 }
425
426 /// getMaxAlignment - return the maximum alignment specified by attributes
427 /// on this decl, 0 if there are none.
428 unsigned getMaxAlignment() const {
429 return hasAttrs() ? getMaxAttrAlignment(getAttrs(), getASTContext()) : 0;
Chris Lattner115cafc2009-04-12 20:07:59 +0000430 }
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Chris Lattner0ed844b2008-04-04 06:12:32 +0000432 /// setInvalidDecl - Indicates the Decl had a semantic error. This
433 /// allows for graceful error recovery.
Douglas Gregor42738572010-03-05 00:26:45 +0000434 void setInvalidDecl(bool Invalid = true);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000435 bool isInvalidDecl() const { return (bool) InvalidDecl; }
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000436
437 /// isImplicit - Indicates whether the declaration was implicitly
438 /// generated by the implementation. If false, this declaration
439 /// was written explicitly in the source code.
440 bool isImplicit() const { return Implicit; }
441 void setImplicit(bool I = true) { Implicit = I; }
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Douglas Gregore0762c92009-06-19 23:52:42 +0000443 /// \brief Whether this declaration was used, meaning that a definition
444 /// is required.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000445 ///
446 /// \param CheckUsedAttr When true, also consider the "used" attribute
447 /// (in addition to the "used" bit set by \c setUsed()) when determining
448 /// whether the function is used.
449 bool isUsed(bool CheckUsedAttr = true) const;
Sean Huntcf807c42010-08-18 23:23:40 +0000450
Douglas Gregore0762c92009-06-19 23:52:42 +0000451 void setUsed(bool U = true) { Used = U; }
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000453 /// \brief Whether this declaration was referenced.
454 bool isReferenced() const;
455
456 void setReferenced(bool R = true) { Referenced = R; }
457
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000458 /// \brief Determine the availability of the given declaration.
459 ///
460 /// This routine will determine the most restrictive availability of
461 /// the given declaration (e.g., preferring 'unavailable' to
462 /// 'deprecated').
463 ///
464 /// \param Message If non-NULL and the result is not \c
465 /// AR_Available, will be set to a (possibly empty) message
466 /// describing why the declaration has not been introduced, is
467 /// deprecated, or is unavailable.
468 AvailabilityResult getAvailability(std::string *Message = 0) const;
469
470 /// \brief Determine whether this declaration is marked 'deprecated'.
471 ///
472 /// \param Message If non-NULL and the declaration is deprecated,
473 /// this will be set to the message describing why the declaration
474 /// was deprecated (which may be empty).
475 bool isDeprecated(std::string *Message = 0) const {
476 return getAvailability(Message) == AR_Deprecated;
477 }
478
479 /// \brief Determine whether this declaration is marked 'unavailable'.
480 ///
481 /// \param Message If non-NULL and the declaration is unavailable,
482 /// this will be set to the message describing why the declaration
483 /// was made unavailable (which may be empty).
484 bool isUnavailable(std::string *Message = 0) const {
485 return getAvailability(Message) == AR_Unavailable;
486 }
487
488 /// \brief Determine whether this is a weak-imported symbol.
489 ///
490 /// Weak-imported symbols are typically marked with the
Eli Friedman7f3ad232011-03-28 02:00:21 +0000491 /// 'weak_import' attribute, but may also be marked with an
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000492 /// 'availability' attribute where we're targing a platform prior to
493 /// the introduction of this feature.
494 bool isWeakImported() const;
495
496 /// \brief Determines whether this symbol can be weak-imported,
497 /// e.g., whether it would be well-formed to add the weak_import
498 /// attribute.
499 ///
500 /// \param IsDefinition Set to \c true to indicate that this
501 /// declaration cannot be weak-imported because it has a definition.
502 bool canBeWeakImported(bool &IsDefinition) const;
503
Douglas Gregor919814d2011-09-09 23:01:35 +0000504 /// \brief Determine whether this declaration came from an AST file (such as
505 /// a precompiled header or module) rather than having been parsed.
Douglas Gregor08e0bc12011-09-10 00:09:20 +0000506 bool isFromASTFile() const { return FromASTFile; }
Douglas Gregor919814d2011-09-09 23:01:35 +0000507
Sebastian Redl0b17c612010-08-13 00:28:03 +0000508 /// \brief Query whether this declaration was changed in a significant way
Sebastian Redl3397c552010-08-18 23:56:27 +0000509 /// since being loaded from an AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000510 ///
511 /// In an epic violation of layering, what is "significant" is entirely
Sebastian Redl3397c552010-08-18 23:56:27 +0000512 /// up to the serialization system, but implemented in AST and Sema.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000513 bool isChangedSinceDeserialization() const { return ChangedAfterLoad; }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000514
515 /// \brief Mark this declaration as having changed since deserialization, or
516 /// reset the flag.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000517 void setChangedSinceDeserialization(bool Changed) {
518 ChangedAfterLoad = Changed;
519 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000520
Douglas Gregorcc636682009-02-17 23:15:12 +0000521 unsigned getIdentifierNamespace() const {
Chris Lattner769dbdf2009-03-27 20:18:19 +0000522 return IdentifierNamespace;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000523 }
Chris Lattnerd62fdc42009-01-06 07:16:40 +0000524 bool isInIdentifierNamespace(unsigned NS) const {
525 return getIdentifierNamespace() & NS;
526 }
Chris Lattner769dbdf2009-03-27 20:18:19 +0000527 static unsigned getIdentifierNamespaceForKind(Kind DK);
528
John McCall0d6b1642010-04-23 18:46:30 +0000529 bool hasTagIdentifierNamespace() const {
530 return isTagIdentifierNamespace(getIdentifierNamespace());
531 }
532 static bool isTagIdentifierNamespace(unsigned NS) {
533 // TagDecls have Tag and Type set and may also have TagFriend.
534 return (NS & ~IDNS_TagFriend) == (IDNS_Tag | IDNS_Type);
535 }
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000537 /// getLexicalDeclContext - The declaration context where this Decl was
538 /// lexically declared (LexicalDC). May be different from
539 /// getDeclContext() (SemanticDC).
540 /// e.g.:
541 ///
542 /// namespace A {
543 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
544 /// }
545 /// void A::f(); // SemanticDC == namespace 'A'
546 /// // LexicalDC == global namespace
Chris Lattner10d83792009-03-27 18:46:15 +0000547 DeclContext *getLexicalDeclContext() {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000548 if (isInSemaDC())
Chris Lattner10d83792009-03-27 18:46:15 +0000549 return getSemanticDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000550 return getMultipleDC()->LexicalDC;
551 }
Chris Lattner10d83792009-03-27 18:46:15 +0000552 const DeclContext *getLexicalDeclContext() const {
553 return const_cast<Decl*>(this)->getLexicalDeclContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000554 }
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000555
Douglas Gregorda2142f2011-02-19 18:51:44 +0000556 virtual bool isOutOfLine() const {
557 return getLexicalDeclContext() != getDeclContext();
558 }
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Douglas Gregor6ab35242009-04-09 21:40:53 +0000560 /// setDeclContext - Set both the semantic and lexical DeclContext
561 /// to DC.
562 void setDeclContext(DeclContext *DC);
563
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000564 void setLexicalDeclContext(DeclContext *DC);
565
Sebastian Redl7a126a42010-08-31 00:36:30 +0000566 /// isDefinedOutsideFunctionOrMethod - This predicate returns true if this
567 /// scoped decl is defined outside the current function or method. This is
568 /// roughly global variables and functions, but also handles enums (which
569 /// could be defined inside or outside a function etc).
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000570 bool isDefinedOutsideFunctionOrMethod() const {
571 return getParentFunctionOrMethod() == 0;
572 }
573
574 /// \brief If this decl is defined inside a function/method/block it returns
575 /// the corresponding DeclContext, otherwise it returns null.
576 const DeclContext *getParentFunctionOrMethod() const;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000577
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000578 /// \brief Retrieves the "canonical" declaration of the given declaration.
Douglas Gregorda2142f2011-02-19 18:51:44 +0000579 virtual Decl *getCanonicalDecl() { return this; }
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000580 const Decl *getCanonicalDecl() const {
581 return const_cast<Decl*>(this)->getCanonicalDecl();
582 }
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000583
Argyrios Kyrtzidisac4e3792009-07-18 08:50:48 +0000584 /// \brief Whether this particular Decl is a canonical one.
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000585 bool isCanonicalDecl() const { return getCanonicalDecl() == this; }
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000587protected:
588 /// \brief Returns the next redeclaration or itself if this is the only decl.
589 ///
590 /// Decl subclasses that can be redeclared should override this method so that
591 /// Decl::redecl_iterator can iterate over them.
Douglas Gregorda2142f2011-02-19 18:51:44 +0000592 virtual Decl *getNextRedeclaration() { return this; }
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000593
594public:
595 /// \brief Iterates through all the redeclarations of the same decl.
596 class redecl_iterator {
597 /// Current - The current declaration.
598 Decl *Current;
599 Decl *Starter;
600
601 public:
602 typedef Decl* value_type;
603 typedef Decl* reference;
604 typedef Decl* pointer;
605 typedef std::forward_iterator_tag iterator_category;
606 typedef std::ptrdiff_t difference_type;
607
608 redecl_iterator() : Current(0) { }
609 explicit redecl_iterator(Decl *C) : Current(C), Starter(C) { }
610
611 reference operator*() const { return Current; }
612 pointer operator->() const { return Current; }
613
614 redecl_iterator& operator++() {
615 assert(Current && "Advancing while iterator has reached end");
616 // Get either previous decl or latest decl.
617 Decl *Next = Current->getNextRedeclaration();
Argyrios Kyrtzidis22cbd2b2009-07-21 00:06:27 +0000618 assert(Next && "Should return next redeclaration or itself, never null!");
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000619 Current = (Next != Starter ? Next : 0);
620 return *this;
621 }
622
623 redecl_iterator operator++(int) {
624 redecl_iterator tmp(*this);
625 ++(*this);
626 return tmp;
627 }
628
Mike Stump1eb44332009-09-09 15:08:12 +0000629 friend bool operator==(redecl_iterator x, redecl_iterator y) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000630 return x.Current == y.Current;
631 }
Mike Stump1eb44332009-09-09 15:08:12 +0000632 friend bool operator!=(redecl_iterator x, redecl_iterator y) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000633 return x.Current != y.Current;
634 }
635 };
636
637 /// \brief Returns iterator for all the redeclarations of the same decl.
638 /// It will iterate at least once (when this decl is the only one).
639 redecl_iterator redecls_begin() const {
640 return redecl_iterator(const_cast<Decl*>(this));
641 }
642 redecl_iterator redecls_end() const { return redecl_iterator(); }
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000643
Sebastian Redld3a413d2009-04-26 20:35:05 +0000644 /// getBody - If this Decl represents a declaration for a body of code,
645 /// such as a function or method definition, this method returns the
646 /// top-level Stmt* of that body. Otherwise this method returns null.
Douglas Gregorda2142f2011-02-19 18:51:44 +0000647 virtual Stmt* getBody() const { return 0; }
Sebastian Redld3a413d2009-04-26 20:35:05 +0000648
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000649 /// \brief Returns true if this Decl represents a declaration for a body of
650 /// code, such as a function or method definition.
Douglas Gregorda2142f2011-02-19 18:51:44 +0000651 virtual bool hasBody() const { return getBody() != 0; }
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000652
Sebastian Redld3a413d2009-04-26 20:35:05 +0000653 /// getBodyRBrace - Gets the right brace of the body, if a body exists.
654 /// This works whether the body is a CompoundStmt or a CXXTryStmt.
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000655 SourceLocation getBodyRBrace() const;
Sebastian Redld3a413d2009-04-26 20:35:05 +0000656
Chris Lattner0ed844b2008-04-04 06:12:32 +0000657 // global temp stats (until we have a per-module visitor)
Sean Hunt9a555912010-05-30 07:21:58 +0000658 static void add(Kind k);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000659 static bool CollectingStats(bool Enable = false);
660 static void PrintStats();
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Anders Carlsson67e33202009-06-13 00:08:58 +0000662 /// isTemplateParameter - Determines whether this declaration is a
Douglas Gregorf57172b2008-12-08 18:40:42 +0000663 /// template parameter.
664 bool isTemplateParameter() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Anders Carlsson67e33202009-06-13 00:08:58 +0000666 /// isTemplateParameter - Determines whether this declaration is a
667 /// template parameter pack.
668 bool isTemplateParameterPack() const;
Douglas Gregorf57172b2008-12-08 18:40:42 +0000669
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000670 /// \brief Whether this declaration is a parameter pack.
671 bool isParameterPack() const;
672
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000673 /// \brief returns true if this declaration is a template
674 bool isTemplateDecl() const;
675
Douglas Gregore53060f2009-06-25 22:08:12 +0000676 /// \brief Whether this declaration is a function or function template.
677 bool isFunctionOrFunctionTemplate() const;
John McCall02cace72009-08-28 07:59:38 +0000678
679 /// \brief Changes the namespace of this declaration to reflect that it's
680 /// the object of a friend declaration.
681 ///
682 /// These declarations appear in the lexical context of the friending
683 /// class, but in the semantic context of the actual entity. This property
684 /// applies only to a specific decl object; other redeclarations of the
685 /// same entity may not (and probably don't) share this property.
686 void setObjectOfFriendDecl(bool PreviouslyDeclared) {
687 unsigned OldNS = IdentifierNamespace;
John McCallb0cb0222010-03-27 05:57:59 +0000688 assert((OldNS & (IDNS_Tag | IDNS_Ordinary |
689 IDNS_TagFriend | IDNS_OrdinaryFriend)) &&
690 "namespace includes neither ordinary nor tag");
John McCall0d6b1642010-04-23 18:46:30 +0000691 assert(!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type |
John McCallb0cb0222010-03-27 05:57:59 +0000692 IDNS_TagFriend | IDNS_OrdinaryFriend)) &&
693 "namespace includes other than ordinary or tag");
John McCall02cace72009-08-28 07:59:38 +0000694
John McCallb0cb0222010-03-27 05:57:59 +0000695 IdentifierNamespace = 0;
696 if (OldNS & (IDNS_Tag | IDNS_TagFriend)) {
John McCall02cace72009-08-28 07:59:38 +0000697 IdentifierNamespace |= IDNS_TagFriend;
John McCall0d6b1642010-04-23 18:46:30 +0000698 if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Tag | IDNS_Type;
John McCallb0cb0222010-03-27 05:57:59 +0000699 }
700
701 if (OldNS & (IDNS_Ordinary | IDNS_OrdinaryFriend)) {
John McCall02cace72009-08-28 07:59:38 +0000702 IdentifierNamespace |= IDNS_OrdinaryFriend;
John McCallb0cb0222010-03-27 05:57:59 +0000703 if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Ordinary;
704 }
John McCall02cace72009-08-28 07:59:38 +0000705 }
706
707 enum FriendObjectKind {
708 FOK_None, // not a friend object
709 FOK_Declared, // a friend of a previously-declared entity
710 FOK_Undeclared // a friend of a previously-undeclared entity
711 };
712
713 /// \brief Determines whether this declaration is the object of a
714 /// friend declaration and, if so, what kind.
715 ///
716 /// There is currently no direct way to find the associated FriendDecl.
717 FriendObjectKind getFriendObjectKind() const {
718 unsigned mask
719 = (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend));
720 if (!mask) return FOK_None;
Anders Carlsson255d6412009-09-13 23:59:13 +0000721 return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ?
722 FOK_Declared : FOK_Undeclared);
John McCall02cace72009-08-28 07:59:38 +0000723 }
Mike Stump1eb44332009-09-09 15:08:12 +0000724
John McCall76d32642010-04-24 01:30:58 +0000725 /// Specifies that this declaration is a C++ overloaded non-member.
726 void setNonMemberOperator() {
727 assert(getKind() == Function || getKind() == FunctionTemplate);
728 assert((IdentifierNamespace & IDNS_Ordinary) &&
729 "visible non-member operators should be in ordinary namespace");
730 IdentifierNamespace |= IDNS_NonMemberOperator;
731 }
732
Chris Lattner0ed844b2008-04-04 06:12:32 +0000733 // Implement isa/cast/dyncast/etc.
734 static bool classof(const Decl *) { return true; }
John McCall80cd64a2010-01-29 01:45:37 +0000735 static bool classofKind(Kind K) { return true; }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000736 static DeclContext *castToDeclContext(const Decl *);
737 static Decl *castFromDeclContext(const DeclContext *);
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Richard Trieu5cb3d692011-07-28 00:19:05 +0000739 void print(raw_ostream &Out, unsigned Indentation = 0,
740 bool PrintInstantiation = false) const;
Chris Lattner8cc488f2011-07-20 07:06:53 +0000741 void print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu5cb3d692011-07-28 00:19:05 +0000742 unsigned Indentation = 0, bool PrintInstantiation = false) const;
Eli Friedman42f42c02009-05-30 04:20:30 +0000743 static void printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner8cc488f2011-07-20 07:06:53 +0000744 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000745 unsigned Indentation = 0);
Anders Carlssonf88df862009-09-26 21:58:53 +0000746 void dump() const;
John McCallf3514242010-11-24 11:21:45 +0000747 void dumpXML() const;
Chris Lattner8cc488f2011-07-20 07:06:53 +0000748 void dumpXML(raw_ostream &OS) const;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000749
Chris Lattner81abbdd2009-03-21 06:27:31 +0000750private:
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000751 const Attr *getAttrsImpl() const;
Chris Lattner81abbdd2009-03-21 06:27:31 +0000752
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000753protected:
754 ASTMutationListener *getASTMutationListener() const;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000755};
756
Chris Lattner49f28ca2009-03-05 08:00:35 +0000757/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when
758/// doing something to a specific decl.
759class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
Daniel Dunbarc5236562009-11-21 09:05:59 +0000760 const Decl *TheDecl;
Chris Lattner49f28ca2009-03-05 08:00:35 +0000761 SourceLocation Loc;
762 SourceManager &SM;
763 const char *Message;
764public:
Daniel Dunbarc5236562009-11-21 09:05:59 +0000765 PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L,
Chris Lattner49f28ca2009-03-05 08:00:35 +0000766 SourceManager &sm, const char *Msg)
767 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Chris Lattner8cc488f2011-07-20 07:06:53 +0000769 virtual void print(raw_ostream &OS) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000770};
771
John McCall6d9496d2010-08-26 09:52:08 +0000772class DeclContextLookupResult
773 : public std::pair<NamedDecl**,NamedDecl**> {
John McCallea318642010-08-26 09:15:37 +0000774public:
John McCall6d9496d2010-08-26 09:52:08 +0000775 DeclContextLookupResult(NamedDecl **I, NamedDecl **E)
776 : std::pair<NamedDecl**,NamedDecl**>(I, E) {}
777 DeclContextLookupResult()
778 : std::pair<NamedDecl**,NamedDecl**>() {}
John McCallea318642010-08-26 09:15:37 +0000779
John McCallf29815a2010-08-26 10:20:09 +0000780 using std::pair<NamedDecl**,NamedDecl**>::operator=;
John McCallea318642010-08-26 09:15:37 +0000781};
782
783class DeclContextLookupConstResult
784 : public std::pair<NamedDecl*const*, NamedDecl*const*> {
785public:
786 DeclContextLookupConstResult(std::pair<NamedDecl**,NamedDecl**> R)
John McCall6d9496d2010-08-26 09:52:08 +0000787 : std::pair<NamedDecl*const*, NamedDecl*const*>(R) {}
John McCallea318642010-08-26 09:15:37 +0000788 DeclContextLookupConstResult(NamedDecl * const *I, NamedDecl * const *E)
John McCall6d9496d2010-08-26 09:52:08 +0000789 : std::pair<NamedDecl*const*, NamedDecl*const*>(I, E) {}
790 DeclContextLookupConstResult()
791 : std::pair<NamedDecl*const*, NamedDecl*const*>() {}
John McCallea318642010-08-26 09:15:37 +0000792
John McCallf29815a2010-08-26 10:20:09 +0000793 using std::pair<NamedDecl*const*,NamedDecl*const*>::operator=;
John McCallea318642010-08-26 09:15:37 +0000794};
Chris Lattner49f28ca2009-03-05 08:00:35 +0000795
Chris Lattnerb048c982008-04-06 04:47:34 +0000796/// DeclContext - This is used only as base class of specific decl types that
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000797/// can act as declaration contexts. These decls are (only the top classes
798/// that directly derive from DeclContext are mentioned, not their subclasses):
Chris Lattner0ed844b2008-04-04 06:12:32 +0000799///
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000800/// TranslationUnitDecl
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000801/// NamespaceDecl
Chris Lattner0ed844b2008-04-04 06:12:32 +0000802/// FunctionDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000803/// TagDecl
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000804/// ObjCMethodDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000805/// ObjCContainerDecl
Douglas Gregor074149e2009-01-05 19:45:36 +0000806/// LinkageSpecDecl
Steve Naroff56ee6892008-10-08 17:01:13 +0000807/// BlockDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000808///
Chris Lattnerb048c982008-04-06 04:47:34 +0000809class DeclContext {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000810 /// DeclKind - This indicates which class this is.
John McCall35043e52010-10-19 05:43:52 +0000811 unsigned DeclKind : 8;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000812
Douglas Gregor2cf26342009-04-09 22:27:44 +0000813 /// \brief Whether this declaration context also has some external
814 /// storage that contains additional declarations that are lexically
815 /// part of this context.
John McCall35043e52010-10-19 05:43:52 +0000816 mutable unsigned ExternalLexicalStorage : 1;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000817
818 /// \brief Whether this declaration context also has some external
819 /// storage that contains additional declarations that are visible
820 /// in this context.
John McCall35043e52010-10-19 05:43:52 +0000821 mutable unsigned ExternalVisibleStorage : 1;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000822
Douglas Gregorc36c5402009-04-09 17:29:08 +0000823 /// \brief Pointer to the data structure used to lookup declarations
John McCall0c01d182010-03-24 05:22:00 +0000824 /// within this context (or a DependentStoredDeclsMap if this is a
825 /// dependent context).
826 mutable StoredDeclsMap *LookupPtr;
Douglas Gregor44b43212008-12-11 16:49:14 +0000827
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000828protected:
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000829 /// FirstDecl - The first declaration stored within this declaration
830 /// context.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000831 mutable Decl *FirstDecl;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000832
833 /// LastDecl - The last declaration stored within this declaration
834 /// context. FIXME: We could probably cache this value somewhere
835 /// outside of the DeclContext, to reduce the size of DeclContext by
836 /// another pointer.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000837 mutable Decl *LastDecl;
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000838
John McCall76bd1f32010-06-01 09:23:16 +0000839 friend class ExternalASTSource;
840
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000841 /// \brief Build up a chain of declarations.
842 ///
843 /// \returns the first/last pair of declarations.
844 static std::pair<Decl *, Decl *>
Chris Lattner686775d2011-07-20 06:58:45 +0000845 BuildDeclChain(const SmallVectorImpl<Decl*> &Decls);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000846
Mike Stump1eb44332009-09-09 15:08:12 +0000847 DeclContext(Decl::Kind K)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000848 : DeclKind(K), ExternalLexicalStorage(false),
Mike Stump1eb44332009-09-09 15:08:12 +0000849 ExternalVisibleStorage(false), LookupPtr(0), FirstDecl(0),
Douglas Gregor2cf26342009-04-09 22:27:44 +0000850 LastDecl(0) { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000851
Chris Lattner0ed844b2008-04-04 06:12:32 +0000852public:
Douglas Gregor44b43212008-12-11 16:49:14 +0000853 ~DeclContext();
854
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000855 Decl::Kind getDeclKind() const {
John McCall35043e52010-10-19 05:43:52 +0000856 return static_cast<Decl::Kind>(DeclKind);
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000857 }
Steve Naroff0a473932009-01-20 19:53:53 +0000858 const char *getDeclKindName() const;
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000859
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +0000860 /// getParent - Returns the containing DeclContext.
Chris Lattner0cf2b192009-03-27 19:19:59 +0000861 DeclContext *getParent() {
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +0000862 return cast<Decl>(this)->getDeclContext();
863 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000864 const DeclContext *getParent() const {
865 return const_cast<DeclContext*>(this)->getParent();
Argyrios Kyrtzidisd2595ec2008-10-12 18:40:01 +0000866 }
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000868 /// getLexicalParent - Returns the containing lexical DeclContext. May be
Douglas Gregor44b43212008-12-11 16:49:14 +0000869 /// different from getParent, e.g.:
870 ///
871 /// namespace A {
872 /// struct S;
873 /// }
874 /// struct A::S {}; // getParent() == namespace 'A'
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000875 /// // getLexicalParent() == translation unit
876 ///
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000877 DeclContext *getLexicalParent() {
Chris Lattner0cf2b192009-03-27 19:19:59 +0000878 return cast<Decl>(this)->getLexicalDeclContext();
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000879 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000880 const DeclContext *getLexicalParent() const {
881 return const_cast<DeclContext*>(this)->getLexicalParent();
Mike Stump1eb44332009-09-09 15:08:12 +0000882 }
Argyrios Kyrtzidis048f30a2009-06-30 02:35:38 +0000883
Douglas Gregore942bbe2009-09-10 16:57:35 +0000884 DeclContext *getLookupParent();
885
886 const DeclContext *getLookupParent() const {
887 return const_cast<DeclContext*>(this)->getLookupParent();
888 }
889
Argyrios Kyrtzidis048f30a2009-06-30 02:35:38 +0000890 ASTContext &getParentASTContext() const {
891 return cast<Decl>(this)->getASTContext();
892 }
893
John McCallaab9e312011-02-22 22:25:23 +0000894 bool isClosure() const {
895 return DeclKind == Decl::Block;
896 }
897
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000898 bool isObjCContainer() const {
Fariborz Jahanian80f77532011-08-22 17:13:51 +0000899 switch (DeclKind) {
900 case Decl::ObjCCategory:
901 case Decl::ObjCCategoryImpl:
902 case Decl::ObjCImplementation:
903 case Decl::ObjCInterface:
904 case Decl::ObjCProtocol:
905 return true;
906 }
907 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000908 }
909
Chris Lattner0ed844b2008-04-04 06:12:32 +0000910 bool isFunctionOrMethod() const {
911 switch (DeclKind) {
Chris Lattner0cf2b192009-03-27 19:19:59 +0000912 case Decl::Block:
913 case Decl::ObjCMethod:
914 return true;
915 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000916 return DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000917 }
918 }
919
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000920 bool isFileContext() const {
921 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
922 }
923
Douglas Gregor5f2bfd42009-02-13 00:10:09 +0000924 bool isTranslationUnit() const {
925 return DeclKind == Decl::TranslationUnit;
926 }
927
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000928 bool isRecord() const {
Sean Hunt9a555912010-05-30 07:21:58 +0000929 return DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord;
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +0000930 }
931
Douglas Gregor44b43212008-12-11 16:49:14 +0000932 bool isNamespace() const {
933 return DeclKind == Decl::Namespace;
934 }
935
Sebastian Redl410c4f22010-08-31 20:53:31 +0000936 bool isInlineNamespace() const;
937
Douglas Gregorbc221632009-05-28 16:34:51 +0000938 /// \brief Determines whether this context is dependent on a
939 /// template parameter.
940 bool isDependentContext() const;
941
Douglas Gregor074149e2009-01-05 19:45:36 +0000942 /// isTransparentContext - Determines whether this context is a
943 /// "transparent" context, meaning that the members declared in this
944 /// context are semantically declared in the nearest enclosing
945 /// non-transparent (opaque) context but are lexically declared in
946 /// this context. For example, consider the enumerators of an
Mike Stump1eb44332009-09-09 15:08:12 +0000947 /// enumeration type:
Douglas Gregor074149e2009-01-05 19:45:36 +0000948 /// @code
949 /// enum E {
Mike Stump1eb44332009-09-09 15:08:12 +0000950 /// Val1
Douglas Gregor074149e2009-01-05 19:45:36 +0000951 /// };
952 /// @endcode
953 /// Here, E is a transparent context, so its enumerator (Val1) will
954 /// appear (semantically) that it is in the same context of E.
955 /// Examples of transparent contexts include: enumerations (except for
Sebastian Redl410c4f22010-08-31 20:53:31 +0000956 /// C++0x scoped enums), and C++ linkage specifications.
Douglas Gregor074149e2009-01-05 19:45:36 +0000957 bool isTransparentContext() const;
958
John McCallac65c622010-10-26 04:59:26 +0000959 /// \brief Determines whether this context is, or is nested within,
960 /// a C++ extern "C" linkage spec.
961 bool isExternCContext() const;
962
Douglas Gregor61481da2009-09-01 17:22:34 +0000963 /// \brief Determine whether this declaration context is equivalent
964 /// to the declaration context DC.
Sebastian Redl7a126a42010-08-31 00:36:30 +0000965 bool Equals(const DeclContext *DC) const {
Douglas Gregordbdf5e72010-03-15 15:26:48 +0000966 return DC && this->getPrimaryContext() == DC->getPrimaryContext();
Douglas Gregor61481da2009-09-01 17:22:34 +0000967 }
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000969 /// \brief Determine whether this declaration context encloses the
970 /// declaration context DC.
Sebastian Redl7a126a42010-08-31 00:36:30 +0000971 bool Encloses(const DeclContext *DC) const;
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +0000972
Douglas Gregor44b43212008-12-11 16:49:14 +0000973 /// getPrimaryContext - There may be many different
974 /// declarations of the same entity (including forward declarations
975 /// of classes, multiple definitions of namespaces, etc.), each with
976 /// a different set of declarations. This routine returns the
977 /// "primary" DeclContext structure, which will contain the
978 /// information needed to perform name lookup into this context.
Steve Naroff0701bbb2009-01-08 17:28:14 +0000979 DeclContext *getPrimaryContext();
John McCall0c01d182010-03-24 05:22:00 +0000980 const DeclContext *getPrimaryContext() const {
981 return const_cast<DeclContext*>(this)->getPrimaryContext();
982 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000983
Sebastian Redl7a126a42010-08-31 00:36:30 +0000984 /// getRedeclContext - Retrieve the context in which an entity conflicts with
985 /// other entities of the same name, or where it is a redeclaration if the
Sebastian Redl410c4f22010-08-31 20:53:31 +0000986 /// two entities are compatible. This skips through transparent contexts.
Sebastian Redl7a126a42010-08-31 00:36:30 +0000987 DeclContext *getRedeclContext();
988 const DeclContext *getRedeclContext() const {
989 return const_cast<DeclContext *>(this)->getRedeclContext();
Douglas Gregor17a9b9e2009-01-07 02:48:43 +0000990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Douglas Gregor88b70942009-02-25 22:02:03 +0000992 /// \brief Retrieve the nearest enclosing namespace context.
993 DeclContext *getEnclosingNamespaceContext();
994 const DeclContext *getEnclosingNamespaceContext() const {
995 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
996 }
997
Sebastian Redl7a126a42010-08-31 00:36:30 +0000998 /// \brief Test if this context is part of the enclosing namespace set of
999 /// the context NS, as defined in C++0x [namespace.def]p9. If either context
1000 /// isn't a namespace, this is equivalent to Equals().
1001 ///
1002 /// The enclosing namespace set of a namespace is the namespace and, if it is
1003 /// inline, its enclosing namespace, recursively.
1004 bool InEnclosingNamespaceSetOf(const DeclContext *NS) const;
1005
Douglas Gregor44b43212008-12-11 16:49:14 +00001006 /// getNextContext - If this is a DeclContext that may have other
1007 /// DeclContexts that are semantically connected but syntactically
1008 /// different, such as C++ namespaces, this routine retrieves the
1009 /// next DeclContext in the link. Iteration through the chain of
1010 /// DeclContexts should begin at the primary DeclContext and
1011 /// continue until this function returns NULL. For example, given:
1012 /// @code
1013 /// namespace N {
1014 /// int x;
1015 /// }
1016 /// namespace N {
1017 /// int y;
1018 /// }
1019 /// @endcode
1020 /// The first occurrence of namespace N will be the primary
1021 /// DeclContext. Its getNextContext will return the second
1022 /// occurrence of namespace N.
1023 DeclContext *getNextContext();
1024
1025 /// decl_iterator - Iterates through the declarations stored
1026 /// within this context.
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001027 class decl_iterator {
1028 /// Current - The current declaration.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001029 Decl *Current;
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001030
1031 public:
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001032 typedef Decl* value_type;
1033 typedef Decl* reference;
1034 typedef Decl* pointer;
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001035 typedef std::forward_iterator_tag iterator_category;
1036 typedef std::ptrdiff_t difference_type;
1037
1038 decl_iterator() : Current(0) { }
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001039 explicit decl_iterator(Decl *C) : Current(C) { }
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001040
1041 reference operator*() const { return Current; }
1042 pointer operator->() const { return Current; }
1043
Chris Lattner96f44682009-03-28 05:59:45 +00001044 decl_iterator& operator++() {
Chris Lattner244a67d2009-03-28 06:04:26 +00001045 Current = Current->getNextDeclInContext();
Chris Lattner96f44682009-03-28 05:59:45 +00001046 return *this;
1047 }
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001048
1049 decl_iterator operator++(int) {
1050 decl_iterator tmp(*this);
1051 ++(*this);
1052 return tmp;
1053 }
1054
Mike Stump1eb44332009-09-09 15:08:12 +00001055 friend bool operator==(decl_iterator x, decl_iterator y) {
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001056 return x.Current == y.Current;
1057 }
Mike Stump1eb44332009-09-09 15:08:12 +00001058 friend bool operator!=(decl_iterator x, decl_iterator y) {
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001059 return x.Current != y.Current;
1060 }
1061 };
Douglas Gregor44b43212008-12-11 16:49:14 +00001062
Douglas Gregor44b43212008-12-11 16:49:14 +00001063 /// decls_begin/decls_end - Iterate over the declarations stored in
Mike Stump1eb44332009-09-09 15:08:12 +00001064 /// this context.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001065 decl_iterator decls_begin() const;
1066 decl_iterator decls_end() const;
1067 bool decls_empty() const;
Douglas Gregor44b43212008-12-11 16:49:14 +00001068
Sebastian Redl681d7232010-07-27 00:17:23 +00001069 /// noload_decls_begin/end - Iterate over the declarations stored in this
1070 /// context that are currently loaded; don't attempt to retrieve anything
1071 /// from an external source.
1072 decl_iterator noload_decls_begin() const;
1073 decl_iterator noload_decls_end() const;
1074
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001075 /// specific_decl_iterator - Iterates over a subrange of
1076 /// declarations stored in a DeclContext, providing only those that
Douglas Gregor669c9a22009-02-02 18:25:48 +00001077 /// are of type SpecificDecl (or a class derived from it). This
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001078 /// iterator is used, for example, to provide iteration over just
Douglas Gregor669c9a22009-02-02 18:25:48 +00001079 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001080 template<typename SpecificDecl>
1081 class specific_decl_iterator {
1082 /// Current - The current, underlying declaration iterator, which
Douglas Gregord6f0b4e2009-02-02 17:56:05 +00001083 /// will either be NULL or will point to a declaration of
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001084 /// type SpecificDecl.
1085 DeclContext::decl_iterator Current;
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001087 /// SkipToNextDecl - Advances the current position up to the next
1088 /// declaration of type SpecificDecl that also meets the criteria
1089 /// required by Acceptable.
1090 void SkipToNextDecl() {
Douglas Gregor669c9a22009-02-02 18:25:48 +00001091 while (*Current && !isa<SpecificDecl>(*Current))
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001092 ++Current;
1093 }
1094
1095 public:
1096 typedef SpecificDecl* value_type;
1097 typedef SpecificDecl* reference;
1098 typedef SpecificDecl* pointer;
1099 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1100 difference_type;
1101 typedef std::forward_iterator_tag iterator_category;
1102
Douglas Gregor669c9a22009-02-02 18:25:48 +00001103 specific_decl_iterator() : Current() { }
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001104
1105 /// specific_decl_iterator - Construct a new iterator over a
Douglas Gregord6f0b4e2009-02-02 17:56:05 +00001106 /// subset of the declarations the range [C,
1107 /// end-of-declarations). If A is non-NULL, it is a pointer to a
1108 /// member function of SpecificDecl that should return true for
1109 /// all of the SpecificDecl instances that will be in the subset
1110 /// of iterators. For example, if you want Objective-C instance
1111 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1112 /// &ObjCMethodDecl::isInstanceMethod.
Douglas Gregor669c9a22009-02-02 18:25:48 +00001113 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001114 SkipToNextDecl();
1115 }
1116
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001117 reference operator*() const { return cast<SpecificDecl>(*Current); }
1118 pointer operator->() const { return cast<SpecificDecl>(*Current); }
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001119
1120 specific_decl_iterator& operator++() {
1121 ++Current;
1122 SkipToNextDecl();
1123 return *this;
1124 }
1125
1126 specific_decl_iterator operator++(int) {
1127 specific_decl_iterator tmp(*this);
1128 ++(*this);
1129 return tmp;
1130 }
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001132 friend bool
1133 operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) {
1134 return x.Current == y.Current;
1135 }
Mike Stump1eb44332009-09-09 15:08:12 +00001136
1137 friend bool
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001138 operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) {
1139 return x.Current != y.Current;
1140 }
1141 };
1142
Douglas Gregor669c9a22009-02-02 18:25:48 +00001143 /// \brief Iterates over a filtered subrange of declarations stored
1144 /// in a DeclContext.
1145 ///
1146 /// This iterator visits only those declarations that are of type
1147 /// SpecificDecl (or a class derived from it) and that meet some
1148 /// additional run-time criteria. This iterator is used, for
1149 /// example, to provide access to the instance methods within an
1150 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
1151 /// Acceptable = ObjCMethodDecl::isInstanceMethod).
1152 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
1153 class filtered_decl_iterator {
1154 /// Current - The current, underlying declaration iterator, which
1155 /// will either be NULL or will point to a declaration of
1156 /// type SpecificDecl.
1157 DeclContext::decl_iterator Current;
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Douglas Gregor669c9a22009-02-02 18:25:48 +00001159 /// SkipToNextDecl - Advances the current position up to the next
1160 /// declaration of type SpecificDecl that also meets the criteria
1161 /// required by Acceptable.
1162 void SkipToNextDecl() {
1163 while (*Current &&
1164 (!isa<SpecificDecl>(*Current) ||
1165 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
1166 ++Current;
1167 }
1168
1169 public:
1170 typedef SpecificDecl* value_type;
1171 typedef SpecificDecl* reference;
1172 typedef SpecificDecl* pointer;
1173 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1174 difference_type;
1175 typedef std::forward_iterator_tag iterator_category;
1176
1177 filtered_decl_iterator() : Current() { }
1178
1179 /// specific_decl_iterator - Construct a new iterator over a
1180 /// subset of the declarations the range [C,
1181 /// end-of-declarations). If A is non-NULL, it is a pointer to a
1182 /// member function of SpecificDecl that should return true for
1183 /// all of the SpecificDecl instances that will be in the subset
1184 /// of iterators. For example, if you want Objective-C instance
1185 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1186 /// &ObjCMethodDecl::isInstanceMethod.
1187 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
1188 SkipToNextDecl();
1189 }
1190
1191 reference operator*() const { return cast<SpecificDecl>(*Current); }
1192 pointer operator->() const { return cast<SpecificDecl>(*Current); }
1193
1194 filtered_decl_iterator& operator++() {
1195 ++Current;
1196 SkipToNextDecl();
1197 return *this;
1198 }
1199
1200 filtered_decl_iterator operator++(int) {
1201 filtered_decl_iterator tmp(*this);
1202 ++(*this);
1203 return tmp;
1204 }
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Douglas Gregor669c9a22009-02-02 18:25:48 +00001206 friend bool
1207 operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
1208 return x.Current == y.Current;
1209 }
Mike Stump1eb44332009-09-09 15:08:12 +00001210
1211 friend bool
Douglas Gregor669c9a22009-02-02 18:25:48 +00001212 operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
1213 return x.Current != y.Current;
1214 }
1215 };
1216
Douglas Gregor40f4e692009-01-20 16:54:50 +00001217 /// @brief Add the declaration D into this context.
1218 ///
1219 /// This routine should be invoked when the declaration D has first
1220 /// been declared, to place D into the context where it was
1221 /// (lexically) defined. Every declaration must be added to one
1222 /// (and only one!) context, where it can be visited via
1223 /// [decls_begin(), decls_end()). Once a declaration has been added
1224 /// to its lexical context, the corresponding DeclContext owns the
1225 /// declaration.
1226 ///
1227 /// If D is also a NamedDecl, it will be made visible within its
1228 /// semantic context via makeDeclVisibleInContext.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001229 void addDecl(Decl *D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001230
John McCall3f9a8a62009-08-11 06:59:38 +00001231 /// @brief Add the declaration D to this context without modifying
1232 /// any lookup tables.
1233 ///
1234 /// This is useful for some operations in dependent contexts where
1235 /// the semantic context might not be dependent; this basically
1236 /// only happens with friends.
1237 void addHiddenDecl(Decl *D);
1238
John McCall9f54ad42009-12-10 09:41:52 +00001239 /// @brief Removes a declaration from this context.
1240 void removeDecl(Decl *D);
1241
Douglas Gregor44b43212008-12-11 16:49:14 +00001242 /// lookup_iterator - An iterator that provides access to the results
1243 /// of looking up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001244 typedef NamedDecl **lookup_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +00001245
1246 /// lookup_const_iterator - An iterator that provides non-mutable
1247 /// access to the results of lookup up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001248 typedef NamedDecl * const * lookup_const_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +00001249
John McCallea318642010-08-26 09:15:37 +00001250 typedef DeclContextLookupResult lookup_result;
1251 typedef DeclContextLookupConstResult lookup_const_result;
Douglas Gregor44b43212008-12-11 16:49:14 +00001252
1253 /// lookup - Find the declarations (if any) with the given Name in
1254 /// this context. Returns a range of iterators that contains all of
Douglas Gregor40f4e692009-01-20 16:54:50 +00001255 /// the declarations with this name, with object, function, member,
1256 /// and enumerator names preceding any tag name. Note that this
1257 /// routine will not look into parent contexts.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001258 lookup_result lookup(DeclarationName Name);
1259 lookup_const_result lookup(DeclarationName Name) const;
Douglas Gregor44b43212008-12-11 16:49:14 +00001260
Douglas Gregor40f4e692009-01-20 16:54:50 +00001261 /// @brief Makes a declaration visible within this context.
1262 ///
1263 /// This routine makes the declaration D visible to name lookup
1264 /// within this context and, if this is a transparent context,
1265 /// within its parent contexts up to the first enclosing
1266 /// non-transparent context. Making a declaration visible within a
1267 /// context does not transfer ownership of a declaration, and a
1268 /// declaration can be visible in many contexts that aren't its
1269 /// lexical context.
1270 ///
1271 /// If D is a redeclaration of an existing declaration that is
1272 /// visible from this context, as determined by
1273 /// NamedDecl::declarationReplaces, the previous declaration will be
1274 /// replaced with D.
John McCallab88d972009-08-31 22:39:49 +00001275 ///
1276 /// @param Recoverable true if it's okay to not add this decl to
1277 /// the lookup tables because it can be easily recovered by walking
1278 /// the declaration chains.
1279 void makeDeclVisibleInContext(NamedDecl *D, bool Recoverable = true);
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +00001280
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001281 /// udir_iterator - Iterates through the using-directives stored
1282 /// within this context.
1283 typedef UsingDirectiveDecl * const * udir_iterator;
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001285 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range;
1286
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001287 udir_iterator_range getUsingDirectives() const;
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001288
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001289 udir_iterator using_directives_begin() const {
1290 return getUsingDirectives().first;
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001291 }
1292
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001293 udir_iterator using_directives_end() const {
1294 return getUsingDirectives().second;
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001295 }
1296
John McCall0c01d182010-03-24 05:22:00 +00001297 // These are all defined in DependentDiagnostic.h.
1298 class ddiag_iterator;
1299 inline ddiag_iterator ddiag_begin() const;
1300 inline ddiag_iterator ddiag_end() const;
1301
Douglas Gregorc2ee10d2009-04-07 17:20:56 +00001302 // Low-level accessors
1303
Douglas Gregorc2ee10d2009-04-07 17:20:56 +00001304 /// \brief Retrieve the internal representation of the lookup structure.
John McCall0c01d182010-03-24 05:22:00 +00001305 StoredDeclsMap* getLookupPtr() const { return LookupPtr; }
Douglas Gregorc2ee10d2009-04-07 17:20:56 +00001306
Douglas Gregor2cf26342009-04-09 22:27:44 +00001307 /// \brief Whether this DeclContext has external storage containing
1308 /// additional declarations that are lexically in this context.
1309 bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; }
1310
1311 /// \brief State whether this DeclContext has external storage for
1312 /// declarations lexically in this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001313 void setHasExternalLexicalStorage(bool ES = true) {
1314 ExternalLexicalStorage = ES;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001315 }
1316
1317 /// \brief Whether this DeclContext has external storage containing
1318 /// additional declarations that are visible in this context.
1319 bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; }
1320
1321 /// \brief State whether this DeclContext has external storage for
1322 /// declarations visible in this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001323 void setHasExternalVisibleStorage(bool ES = true) {
1324 ExternalVisibleStorage = ES;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001325 }
1326
Douglas Gregor2ea054f2011-08-26 22:04:51 +00001327 /// \brief Determine whether the given declaration is stored in the list of
1328 /// declarations lexically within this context.
1329 bool isDeclInLexicalTraversal(const Decl *D) const {
1330 return D && (D->NextDeclInContext || D == FirstDecl || D == LastDecl);
1331 }
1332
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +00001333 static bool classof(const Decl *D);
Chris Lattnerb048c982008-04-06 04:47:34 +00001334 static bool classof(const DeclContext *D) { return true; }
Sean Hunt9a555912010-05-30 07:21:58 +00001335#define DECL(NAME, BASE)
1336#define DECL_CONTEXT(NAME) \
1337 static bool classof(const NAME##Decl *D) { return true; }
1338#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +00001339
Anders Carlsson84834432009-12-14 00:51:04 +00001340 void dumpDeclContext() const;
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +00001341
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +00001342private:
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001343 void LoadLexicalDeclsFromExternalStorage() const;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001344
John McCall0c01d182010-03-24 05:22:00 +00001345 friend class DependentDiagnostic;
1346 StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const;
1347
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001348 void buildLookup(DeclContext *DCtx);
1349 void makeDeclVisibleInContextImpl(NamedDecl *D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001350};
1351
Douglas Gregorf57172b2008-12-08 18:40:42 +00001352inline bool Decl::isTemplateParameter() const {
Douglas Gregor79c22782010-01-16 20:21:20 +00001353 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm ||
1354 getKind() == TemplateTemplateParm;
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001355}
1356
John McCall80cd64a2010-01-29 01:45:37 +00001357// Specialization selected when ToTy is not a known subclass of DeclContext.
1358template <class ToTy,
1359 bool IsKnownSubtype = ::llvm::is_base_of< DeclContext, ToTy>::value>
1360struct cast_convert_decl_context {
1361 static const ToTy *doit(const DeclContext *Val) {
1362 return static_cast<const ToTy*>(Decl::castFromDeclContext(Val));
1363 }
1364
1365 static ToTy *doit(DeclContext *Val) {
1366 return static_cast<ToTy*>(Decl::castFromDeclContext(Val));
1367 }
1368};
1369
1370// Specialization selected when ToTy is a known subclass of DeclContext.
1371template <class ToTy>
1372struct cast_convert_decl_context<ToTy, true> {
1373 static const ToTy *doit(const DeclContext *Val) {
1374 return static_cast<const ToTy*>(Val);
1375 }
1376
1377 static ToTy *doit(DeclContext *Val) {
1378 return static_cast<ToTy*>(Val);
1379 }
1380};
1381
1382
Chris Lattner0ed844b2008-04-04 06:12:32 +00001383} // end clang.
1384
1385namespace llvm {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001386
John McCall80cd64a2010-01-29 01:45:37 +00001387/// isa<T>(DeclContext*)
Eli Friedman4d509342011-05-21 19:15:39 +00001388template <typename To>
1389struct isa_impl<To, ::clang::DeclContext> {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001390 static bool doit(const ::clang::DeclContext &Val) {
Eli Friedman4d509342011-05-21 19:15:39 +00001391 return To::classofKind(Val.getDeclKind());
Chris Lattner0ed844b2008-04-04 06:12:32 +00001392 }
1393};
Chris Lattner0ed844b2008-04-04 06:12:32 +00001394
John McCall80cd64a2010-01-29 01:45:37 +00001395/// cast<T>(DeclContext*)
1396template<class ToTy>
1397struct cast_convert_val<ToTy,
1398 const ::clang::DeclContext,const ::clang::DeclContext> {
1399 static const ToTy &doit(const ::clang::DeclContext &Val) {
1400 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1401 }
1402};
1403template<class ToTy>
1404struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> {
1405 static ToTy &doit(::clang::DeclContext &Val) {
1406 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1407 }
1408};
1409template<class ToTy>
1410struct cast_convert_val<ToTy,
1411 const ::clang::DeclContext*, const ::clang::DeclContext*> {
1412 static const ToTy *doit(const ::clang::DeclContext *Val) {
1413 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1414 }
1415};
1416template<class ToTy>
1417struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> {
1418 static ToTy *doit(::clang::DeclContext *Val) {
1419 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1420 }
1421};
1422
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001423/// Implement cast_convert_val for Decl -> DeclContext conversions.
Chris Lattner0ed844b2008-04-04 06:12:32 +00001424template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001425struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
Chris Lattnerb048c982008-04-06 04:47:34 +00001426 static ::clang::DeclContext &doit(const FromTy &Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001427 return *FromTy::castToDeclContext(&Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001428 }
1429};
Chris Lattner0ed844b2008-04-04 06:12:32 +00001430
1431template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001432struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
Chris Lattnerb048c982008-04-06 04:47:34 +00001433 static ::clang::DeclContext *doit(const FromTy *Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001434 return FromTy::castToDeclContext(Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001435 }
1436};
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001437
Douglas Gregor44b43212008-12-11 16:49:14 +00001438template<class FromTy>
1439struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
1440 static const ::clang::DeclContext &doit(const FromTy &Val) {
1441 return *FromTy::castToDeclContext(&Val);
1442 }
1443};
1444
1445template<class FromTy>
1446struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
1447 static const ::clang::DeclContext *doit(const FromTy *Val) {
1448 return FromTy::castToDeclContext(Val);
1449 }
1450};
1451
Chris Lattner0ed844b2008-04-04 06:12:32 +00001452} // end namespace llvm
1453
1454#endif