blob: e3fa41ef314219ebcd9cdafcdd815c051751666c [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
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000017#include "clang/AST/AttrIterator.h"
Douglas Gregor514d3b62012-05-03 23:18:44 +000018#include "clang/AST/DeclarationName.h"
John McCallad2b8042010-01-18 23:21:37 +000019#include "clang/Basic/Specifiers.h"
Chris Lattneree219fd2009-03-29 06:06:59 +000020#include "llvm/ADT/PointerUnion.h"
Daniel Dunbaraa49a7d2012-03-09 19:35:29 +000021#include "llvm/Support/Compiler.h"
22#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner0ed844b2008-04-04 06:12:32 +000023
24namespace clang {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000025class ASTMutationListener;
Benjamin Kramer9852f582012-12-01 16:35:25 +000026class BlockDecl;
27class CXXRecordDecl;
28class CompoundStmt;
29class DeclContext;
30class DeclarationName;
31class DependentDiagnostic;
32class EnumDecl;
33class FunctionDecl;
34class LinkageSpecDecl;
35class NamedDecl;
36class NamespaceDecl;
37class ObjCCategoryDecl;
38class ObjCCategoryImplDecl;
39class ObjCContainerDecl;
40class ObjCImplDecl;
41class ObjCImplementationDecl;
42class ObjCInterfaceDecl;
43class ObjCMethodDecl;
44class ObjCProtocolDecl;
45struct PrintingPolicy;
46class Stmt;
47class StoredDeclsMap;
48class TranslationUnitDecl;
49class UsingDirectiveDecl;
Chris Lattner0eda3b32009-03-29 04:32:54 +000050}
51
52namespace llvm {
53// DeclContext* is only 4-byte aligned on 32-bit systems.
54template<>
55 class PointerLikeTypeTraits<clang::DeclContext*> {
56 typedef clang::DeclContext* PT;
57public:
58 static inline void *getAsVoidPointer(PT P) { return P; }
59 static inline PT getFromVoidPointer(void *P) {
60 return static_cast<PT>(P);
61 }
62 enum { NumLowBitsAvailable = 2 };
63};
64}
65
66namespace clang {
Chris Lattner0ed844b2008-04-04 06:12:32 +000067
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000068 /// \brief Captures the result of checking the availability of a
69 /// declaration.
70 enum AvailabilityResult {
71 AR_Available = 0,
72 AR_NotYetIntroduced,
73 AR_Deprecated,
74 AR_Unavailable
75 };
76
Mike Stump1eb44332009-09-09 15:08:12 +000077/// Decl - This represents one declaration (or definition), e.g. a variable,
78/// typedef, function, struct, etc.
Chris Lattner0ed844b2008-04-04 06:12:32 +000079///
80class Decl {
81public:
Douglas Gregor64650af2009-02-02 23:39:07 +000082 /// \brief Lists the kind of concrete classes of Decl.
Chris Lattner0ed844b2008-04-04 06:12:32 +000083 enum Kind {
Sean Hunt9a555912010-05-30 07:21:58 +000084#define DECL(DERIVED, BASE) DERIVED,
85#define ABSTRACT_DECL(DECL)
86#define DECL_RANGE(BASE, START, END) \
87 first##BASE = START, last##BASE = END,
88#define LAST_DECL_RANGE(BASE, START, END) \
89 first##BASE = START, last##BASE = END
90#include "clang/AST/DeclNodes.inc"
Chris Lattner0ed844b2008-04-04 06:12:32 +000091 };
92
Chris Lattner6ad9ac02010-05-07 21:43:38 +000093 /// \brief A placeholder type used to construct an empty shell of a
94 /// decl-derived type that will be filled in later (e.g., by some
95 /// deserialization method).
96 struct EmptyShell { };
97
John McCall0d6b1642010-04-23 18:46:30 +000098 /// IdentifierNamespace - The different namespaces in which
99 /// declarations may appear. According to C99 6.2.3, there are
100 /// four namespaces, labels, tags, members and ordinary
101 /// identifiers. C++ describes lookup completely differently:
102 /// certain lookups merely "ignore" certain kinds of declarations,
103 /// usually based on whether the declaration is of a type, etc.
David Blaikieba243b52011-11-09 06:07:30 +0000104 ///
John McCall0d6b1642010-04-23 18:46:30 +0000105 /// These are meant as bitmasks, so that searches in
106 /// C++ can look into the "tag" namespace during ordinary lookup.
107 ///
Sebastian Redl07a353c2010-07-14 20:26:45 +0000108 /// Decl currently provides 15 bits of IDNS bits.
Chris Lattner0ed844b2008-04-04 06:12:32 +0000109 enum IdentifierNamespace {
John McCall0d6b1642010-04-23 18:46:30 +0000110 /// Labels, declared with 'x:' and referenced with 'goto x'.
111 IDNS_Label = 0x0001,
112
113 /// Tags, declared with 'struct foo;' and referenced with
114 /// 'struct foo'. All tags are also types. This is what
115 /// elaborated-type-specifiers look for in C.
116 IDNS_Tag = 0x0002,
117
118 /// Types, declared with 'struct foo', typedefs, etc.
119 /// This is what elaborated-type-specifiers look for in C++,
120 /// but note that it's ill-formed to find a non-tag.
121 IDNS_Type = 0x0004,
122
123 /// Members, declared with object declarations within tag
124 /// definitions. In C, these can only be found by "qualified"
125 /// lookup in member expressions. In C++, they're found by
126 /// normal lookup.
127 IDNS_Member = 0x0008,
128
129 /// Namespaces, declared with 'namespace foo {}'.
130 /// Lookup for nested-name-specifiers find these.
131 IDNS_Namespace = 0x0010,
132
133 /// Ordinary names. In C, everything that's not a label, tag,
134 /// or member ends up here.
135 IDNS_Ordinary = 0x0020,
136
137 /// Objective C @protocol.
138 IDNS_ObjCProtocol = 0x0040,
139
140 /// This declaration is a friend function. A friend function
141 /// declaration is always in this namespace but may also be in
142 /// IDNS_Ordinary if it was previously declared.
143 IDNS_OrdinaryFriend = 0x0080,
144
145 /// This declaration is a friend class. A friend class
146 /// declaration is always in this namespace but may also be in
147 /// IDNS_Tag|IDNS_Type if it was previously declared.
148 IDNS_TagFriend = 0x0100,
149
150 /// This declaration is a using declaration. A using declaration
151 /// *introduces* a number of other declarations into the current
152 /// scope, and those declarations use the IDNS of their targets,
153 /// but the actual using declarations go in this namespace.
John McCall76d32642010-04-24 01:30:58 +0000154 IDNS_Using = 0x0200,
155
156 /// This declaration is a C++ operator declared in a non-class
157 /// context. All such operators are also in IDNS_Ordinary.
158 /// C++ lexical operator lookup looks for these.
159 IDNS_NonMemberOperator = 0x0400
Chris Lattner0ed844b2008-04-04 06:12:32 +0000160 };
Mike Stump1eb44332009-09-09 15:08:12 +0000161
John McCall09e2c522011-05-01 03:04:29 +0000162 /// ObjCDeclQualifier - 'Qualifiers' written next to the return and
163 /// parameter types in method declarations. Other than remembering
164 /// them and mangling them into the method's signature string, these
165 /// are ignored by the compiler; they are consumed by certain
166 /// remote-messaging frameworks.
167 ///
168 /// in, inout, and out are mutually exclusive and apply only to
169 /// method parameters. bycopy and byref are mutually exclusive and
170 /// apply only to method parameters (?). oneway applies only to
171 /// results. All of these expect their corresponding parameter to
172 /// have a particular type. None of this is currently enforced by
173 /// clang.
174 ///
175 /// This should be kept in sync with ObjCDeclSpec::ObjCDeclQualifier.
Chris Lattner0ed844b2008-04-04 06:12:32 +0000176 enum ObjCDeclQualifier {
177 OBJC_TQ_None = 0x0,
178 OBJC_TQ_In = 0x1,
179 OBJC_TQ_Inout = 0x2,
180 OBJC_TQ_Out = 0x4,
181 OBJC_TQ_Bycopy = 0x8,
182 OBJC_TQ_Byref = 0x10,
183 OBJC_TQ_Oneway = 0x20
184 };
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Douglas Gregor46cd2182012-01-06 16:59:53 +0000186protected:
187 // Enumeration values used in the bits stored in NextInContextAndBits.
188 enum {
189 /// \brief Whether this declaration is a top-level declaration (function,
190 /// global variable, etc.) that is lexically inside an objc container
191 /// definition.
192 TopLevelDeclInObjCContainerFlag = 0x01,
193
194 /// \brief Whether this declaration is private to the module in which it was
195 /// defined.
196 ModulePrivateFlag = 0x02
197 };
198
199 /// \brief The next declaration within the same lexical
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000200 /// DeclContext. These pointers form the linked list that is
201 /// traversed via DeclContext's decls_begin()/decls_end().
Douglas Gregor46cd2182012-01-06 16:59:53 +0000202 ///
203 /// The extra two bits are used for the TopLevelDeclInObjCContainer and
204 /// ModulePrivate bits.
205 llvm::PointerIntPair<Decl *, 2, unsigned> NextInContextAndBits;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000206
Douglas Gregor46cd2182012-01-06 16:59:53 +0000207private:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000208 friend class DeclContext;
209
Chris Lattneree219fd2009-03-29 06:06:59 +0000210 struct MultipleDC {
211 DeclContext *SemanticDC;
212 DeclContext *LexicalDC;
213 };
Mike Stump1eb44332009-09-09 15:08:12 +0000214
215
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000216 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
217 /// For declarations that don't contain C++ scope specifiers, it contains
218 /// the DeclContext where the Decl was declared.
219 /// For declarations with C++ scope specifiers, it contains a MultipleDC*
220 /// with the context where it semantically belongs (SemanticDC) and the
221 /// context where it was lexically declared (LexicalDC).
222 /// e.g.:
223 ///
224 /// namespace A {
225 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
226 /// }
227 /// void A::f(); // SemanticDC == namespace 'A'
228 /// // LexicalDC == global namespace
Chris Lattneree219fd2009-03-29 06:06:59 +0000229 llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000230
Chris Lattneree219fd2009-03-29 06:06:59 +0000231 inline bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); }
232 inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000233 inline MultipleDC *getMultipleDC() const {
Chris Lattneree219fd2009-03-29 06:06:59 +0000234 return DeclCtx.get<MultipleDC*>();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000235 }
Chris Lattner10d83792009-03-27 18:46:15 +0000236 inline DeclContext *getSemanticDC() const {
Chris Lattneree219fd2009-03-29 06:06:59 +0000237 return DeclCtx.get<DeclContext*>();
Chris Lattner10d83792009-03-27 18:46:15 +0000238 }
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Douglas Gregor381d34e2010-12-06 18:36:25 +0000240 /// Loc - The location of this decl.
Daniel Dunbar39d76502009-03-04 02:26:41 +0000241 SourceLocation Loc;
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Chris Lattner0ed844b2008-04-04 06:12:32 +0000243 /// DeclKind - This indicates which class this is.
John McCall35043e52010-10-19 05:43:52 +0000244 unsigned DeclKind : 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Chris Lattner0ed844b2008-04-04 06:12:32 +0000246 /// InvalidDecl - This indicates a semantic error occurred.
John McCall35043e52010-10-19 05:43:52 +0000247 unsigned InvalidDecl : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Chris Lattner0ed844b2008-04-04 06:12:32 +0000249 /// HasAttrs - This indicates whether the decl has attributes or not.
John McCall35043e52010-10-19 05:43:52 +0000250 unsigned HasAttrs : 1;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000251
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000252 /// Implicit - Whether this declaration was implicitly generated by
253 /// the implementation rather than explicitly written by the user.
John McCall35043e52010-10-19 05:43:52 +0000254 unsigned Implicit : 1;
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000255
Douglas Gregore0762c92009-06-19 23:52:42 +0000256 /// \brief Whether this declaration was "used", meaning that a definition is
257 /// required.
John McCall35043e52010-10-19 05:43:52 +0000258 unsigned Used : 1;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000259
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000260 /// \brief Whether this declaration was "referenced".
261 /// The difference with 'Used' is whether the reference appears in a
262 /// evaluated context or not, e.g. functions used in uninstantiated templates
263 /// are regarded as "referenced" but not "used".
264 unsigned Referenced : 1;
David Blaikieba243b52011-11-09 06:07:30 +0000265
Daniel Dunbar02892a62012-03-05 21:42:49 +0000266 /// \brief Whether statistic collection is enabled.
267 static bool StatisticsEnabled;
268
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000269protected:
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000270 /// Access - Used by C++ decls for the access specifier.
271 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
272 unsigned Access : 2;
273 friend class CXXClassMemberWrapper;
Sebastian Redl0b17c612010-08-13 00:28:03 +0000274
Douglas Gregor08e0bc12011-09-10 00:09:20 +0000275 /// \brief Whether this declaration was loaded from an AST file.
276 unsigned FromASTFile : 1;
David Blaikieba243b52011-11-09 06:07:30 +0000277
Douglas Gregorf143ffc2012-01-06 16:22:39 +0000278 /// \brief Whether this declaration is hidden from normal name lookup, e.g.,
279 /// because it is was loaded from an AST file is either module-private or
280 /// because its submodule has not been made visible.
281 unsigned Hidden : 1;
282
Chris Lattner769dbdf2009-03-27 20:18:19 +0000283 /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in.
Douglas Gregor381d34e2010-12-06 18:36:25 +0000284 unsigned IdentifierNamespace : 12;
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Douglas Gregor381d34e2010-12-06 18:36:25 +0000286 /// \brief Whether the \c CachedLinkage field is active.
287 ///
288 /// This field is only valid for NamedDecls subclasses.
289 mutable unsigned HasCachedLinkage : 1;
David Blaikieba243b52011-11-09 06:07:30 +0000290
Douglas Gregor381d34e2010-12-06 18:36:25 +0000291 /// \brief If \c HasCachedLinkage, the linkage of this declaration.
292 ///
293 /// This field is only valid for NamedDecls subclasses.
294 mutable unsigned CachedLinkage : 2;
David Blaikieba243b52011-11-09 06:07:30 +0000295
Douglas Gregor8d267c52011-09-09 02:06:17 +0000296 friend class ASTDeclWriter;
297 friend class ASTDeclReader;
Douglas Gregorecc2c092011-12-01 22:20:10 +0000298 friend class ASTReader;
Douglas Gregor8d267c52011-09-09 02:06:17 +0000299
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000300private:
Anders Carlsson1329c272009-03-25 23:38:06 +0000301 void CheckAccessDeclContext() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000303protected:
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000304
Mike Stump1eb44332009-09-09 15:08:12 +0000305 Decl(Kind DK, DeclContext *DC, SourceLocation L)
Douglas Gregor46cd2182012-01-06 16:59:53 +0000306 : NextInContextAndBits(), DeclCtx(DC),
Daniel Dunbar39d76502009-03-04 02:26:41 +0000307 Loc(L), DeclKind(DK), InvalidDecl(0),
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000308 HasAttrs(false), Implicit(false), Used(false), Referenced(false),
Douglas Gregor46cd2182012-01-06 16:59:53 +0000309 Access(AS_none), FromASTFile(0), Hidden(0),
Douglas Gregor381d34e2010-12-06 18:36:25 +0000310 IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
David Blaikieba243b52011-11-09 06:07:30 +0000311 HasCachedLinkage(0)
Douglas Gregor381d34e2010-12-06 18:36:25 +0000312 {
Daniel Dunbar02892a62012-03-05 21:42:49 +0000313 if (StatisticsEnabled) add(DK);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000314 }
Sam Bishop1bb19632008-04-11 18:04:39 +0000315
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +0000316 Decl(Kind DK, EmptyShell Empty)
Douglas Gregor46cd2182012-01-06 16:59:53 +0000317 : NextInContextAndBits(), DeclKind(DK), InvalidDecl(0),
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000318 HasAttrs(false), Implicit(false), Used(false), Referenced(false),
Douglas Gregor46cd2182012-01-06 16:59:53 +0000319 Access(AS_none), FromASTFile(0), Hidden(0),
Douglas Gregor381d34e2010-12-06 18:36:25 +0000320 IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
321 HasCachedLinkage(0)
322 {
Daniel Dunbar02892a62012-03-05 21:42:49 +0000323 if (StatisticsEnabled) add(DK);
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +0000324 }
325
Douglas Gregorda2142f2011-02-19 18:51:44 +0000326 virtual ~Decl();
327
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000328 /// \brief Allocate memory for a deserialized declaration.
329 ///
330 /// This routine must be used to allocate memory for any declaration that is
331 /// deserialized from a module file.
332 ///
333 /// \param Context The context in which we will allocate memory.
334 /// \param ID The global ID of the deserialized declaration.
335 /// \param Size The size of the allocated object.
336 static void *AllocateDeserializedDecl(const ASTContext &Context,
337 unsigned ID,
338 unsigned Size);
339
Sam Bishop1bb19632008-04-11 18:04:39 +0000340public:
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000341
342 /// \brief Source range that this declaration covers.
Daniel Dunbaraa49a7d2012-03-09 19:35:29 +0000343 virtual SourceRange getSourceRange() const LLVM_READONLY {
Douglas Gregorda2142f2011-02-19 18:51:44 +0000344 return SourceRange(getLocation(), getLocation());
345 }
Daniel Dunbaraa49a7d2012-03-09 19:35:29 +0000346 SourceLocation getLocStart() const LLVM_READONLY {
347 return getSourceRange().getBegin();
348 }
349 SourceLocation getLocEnd() const LLVM_READONLY {
350 return getSourceRange().getEnd();
351 }
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000352
Chris Lattner0ed844b2008-04-04 06:12:32 +0000353 SourceLocation getLocation() const { return Loc; }
354 void setLocation(SourceLocation L) { Loc = L; }
355
John McCall35043e52010-10-19 05:43:52 +0000356 Kind getKind() const { return static_cast<Kind>(DeclKind); }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000357 const char *getDeclKindName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Douglas Gregor46cd2182012-01-06 16:59:53 +0000359 Decl *getNextDeclInContext() { return NextInContextAndBits.getPointer(); }
360 const Decl *getNextDeclInContext() const {return NextInContextAndBits.getPointer();}
Chris Lattner96f44682009-03-28 05:59:45 +0000361
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000362 DeclContext *getDeclContext() {
Chris Lattner10d83792009-03-27 18:46:15 +0000363 if (isInSemaDC())
364 return getSemanticDC();
365 return getMultipleDC()->SemanticDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000366 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000367 const DeclContext *getDeclContext() const {
368 return const_cast<Decl*>(this)->getDeclContext();
369 }
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000370
John McCallaab9e312011-02-22 22:25:23 +0000371 /// Finds the innermost non-closure context of this declaration.
372 /// That is, walk out the DeclContext chain, skipping any blocks.
373 DeclContext *getNonClosureContext();
374 const DeclContext *getNonClosureContext() const {
375 return const_cast<Decl*>(this)->getNonClosureContext();
376 }
377
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000378 TranslationUnitDecl *getTranslationUnitDecl();
379 const TranslationUnitDecl *getTranslationUnitDecl() const {
380 return const_cast<Decl*>(this)->getTranslationUnitDecl();
381 }
382
John McCall9aeed322009-10-01 00:25:31 +0000383 bool isInAnonymousNamespace() const;
384
Daniel Dunbaraa49a7d2012-03-09 19:35:29 +0000385 ASTContext &getASTContext() const LLVM_READONLY;
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Anders Carlsson1329c272009-03-25 23:38:06 +0000387 void setAccess(AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000388 Access = AS;
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000389#ifndef NDEBUG
Anders Carlsson1329c272009-03-25 23:38:06 +0000390 CheckAccessDeclContext();
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000391#endif
Anders Carlssonb8547e82009-03-25 20:19:57 +0000392 }
Mike Stump1eb44332009-09-09 15:08:12 +0000393
394 AccessSpecifier getAccess() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000395#ifndef NDEBUG
Anders Carlsson1329c272009-03-25 23:38:06 +0000396 CheckAccessDeclContext();
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000397#endif
Mike Stump1eb44332009-09-09 15:08:12 +0000398 return AccessSpecifier(Access);
Anders Carlsson1329c272009-03-25 23:38:06 +0000399 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000400
Chris Lattner76a642f2009-02-15 22:43:40 +0000401 bool hasAttrs() const { return HasAttrs; }
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000402 void setAttrs(const AttrVec& Attrs) {
403 return setAttrsImpl(Attrs, getASTContext());
404 }
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000405 AttrVec &getAttrs() {
Sean Huntcf807c42010-08-18 23:23:40 +0000406 return const_cast<AttrVec&>(const_cast<const Decl*>(this)->getAttrs());
Chris Lattner81abbdd2009-03-21 06:27:31 +0000407 }
Sean Huntcf807c42010-08-18 23:23:40 +0000408 const AttrVec &getAttrs() const;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000409 void swapAttrs(Decl *D);
Sean Huntcf807c42010-08-18 23:23:40 +0000410 void dropAttrs();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000411
Douglas Gregorb5f35ba2010-12-06 17:49:01 +0000412 void addAttr(Attr *A) {
413 if (hasAttrs())
414 getAttrs().push_back(A);
415 else
416 setAttrs(AttrVec(1, A));
417 }
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Sean Huntcf807c42010-08-18 23:23:40 +0000419 typedef AttrVec::const_iterator attr_iterator;
420
421 // FIXME: Do not rely on iterators having comparable singular values.
422 // Note that this should error out if they do not.
423 attr_iterator attr_begin() const {
424 return hasAttrs() ? getAttrs().begin() : 0;
425 }
426 attr_iterator attr_end() const {
427 return hasAttrs() ? getAttrs().end() : 0;
428 }
David Blaikieba243b52011-11-09 06:07:30 +0000429
Fariborz Jahanianc3ca14d2011-06-23 17:50:10 +0000430 template <typename T>
Fariborz Jahanian8cf0f522011-06-23 20:24:38 +0000431 void dropAttr() {
432 if (!HasAttrs) return;
David Blaikieba243b52011-11-09 06:07:30 +0000433
Benjamin Kramer10be5ba2012-10-14 11:50:50 +0000434 AttrVec &Vec = getAttrs();
435 Vec.erase(std::remove_if(Vec.begin(), Vec.end(), isa<T, Attr*>), Vec.end());
436
437 if (Vec.empty())
Fariborz Jahanian8cf0f522011-06-23 20:24:38 +0000438 HasAttrs = false;
439 }
David Blaikieba243b52011-11-09 06:07:30 +0000440
Sean Huntcf807c42010-08-18 23:23:40 +0000441 template <typename T>
442 specific_attr_iterator<T> specific_attr_begin() const {
443 return specific_attr_iterator<T>(attr_begin());
444 }
445 template <typename T>
446 specific_attr_iterator<T> specific_attr_end() const {
447 return specific_attr_iterator<T>(attr_end());
448 }
449
450 template<typename T> T *getAttr() const {
451 return hasAttrs() ? getSpecificAttr<T>(getAttrs()) : 0;
452 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000453 template<typename T> bool hasAttr() const {
Sean Huntcf807c42010-08-18 23:23:40 +0000454 return hasAttrs() && hasSpecificAttr<T>(getAttrs());
455 }
456
457 /// getMaxAlignment - return the maximum alignment specified by attributes
458 /// on this decl, 0 if there are none.
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +0000459 unsigned getMaxAlignment() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Chris Lattner0ed844b2008-04-04 06:12:32 +0000461 /// setInvalidDecl - Indicates the Decl had a semantic error. This
462 /// allows for graceful error recovery.
Douglas Gregor42738572010-03-05 00:26:45 +0000463 void setInvalidDecl(bool Invalid = true);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000464 bool isInvalidDecl() const { return (bool) InvalidDecl; }
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000465
466 /// isImplicit - Indicates whether the declaration was implicitly
467 /// generated by the implementation. If false, this declaration
468 /// was written explicitly in the source code.
469 bool isImplicit() const { return Implicit; }
470 void setImplicit(bool I = true) { Implicit = I; }
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Douglas Gregore0762c92009-06-19 23:52:42 +0000472 /// \brief Whether this declaration was used, meaning that a definition
473 /// is required.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000474 ///
475 /// \param CheckUsedAttr When true, also consider the "used" attribute
476 /// (in addition to the "used" bit set by \c setUsed()) when determining
477 /// whether the function is used.
478 bool isUsed(bool CheckUsedAttr = true) const;
Sean Huntcf807c42010-08-18 23:23:40 +0000479
Douglas Gregore0762c92009-06-19 23:52:42 +0000480 void setUsed(bool U = true) { Used = U; }
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000482 /// \brief Whether this declaration was referenced.
483 bool isReferenced() const;
484
485 void setReferenced(bool R = true) { Referenced = R; }
486
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +0000487 /// \brief Whether this declaration is a top-level declaration (function,
488 /// global variable, etc.) that is lexically inside an objc container
489 /// definition.
490 bool isTopLevelDeclInObjCContainer() const {
Douglas Gregor46cd2182012-01-06 16:59:53 +0000491 return NextInContextAndBits.getInt() & TopLevelDeclInObjCContainerFlag;
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +0000492 }
493
494 void setTopLevelDeclInObjCContainer(bool V = true) {
Douglas Gregor46cd2182012-01-06 16:59:53 +0000495 unsigned Bits = NextInContextAndBits.getInt();
496 if (V)
497 Bits |= TopLevelDeclInObjCContainerFlag;
498 else
499 Bits &= ~TopLevelDeclInObjCContainerFlag;
500 NextInContextAndBits.setInt(Bits);
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +0000501 }
502
Douglas Gregor46cd2182012-01-06 16:59:53 +0000503protected:
504 /// \brief Whether this declaration was marked as being private to the
505 /// module in which it was defined.
506 bool isModulePrivate() const {
507 return NextInContextAndBits.getInt() & ModulePrivateFlag;
508 }
509
510 /// \brief Specify whether this declaration was marked as being private
511 /// to the module in which it was defined.
512 void setModulePrivate(bool MP = true) {
513 unsigned Bits = NextInContextAndBits.getInt();
514 if (MP)
515 Bits |= ModulePrivateFlag;
516 else
517 Bits &= ~ModulePrivateFlag;
518 NextInContextAndBits.setInt(Bits);
519 }
520
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +0000521 /// \brief Set the owning module ID.
522 void setOwningModuleID(unsigned ID) {
523 assert(isFromASTFile() && "Only works on a deserialized declaration");
524 *((unsigned*)this - 2) = ID;
525 }
526
Douglas Gregor46cd2182012-01-06 16:59:53 +0000527public:
528
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000529 /// \brief Determine the availability of the given declaration.
530 ///
531 /// This routine will determine the most restrictive availability of
532 /// the given declaration (e.g., preferring 'unavailable' to
533 /// 'deprecated').
534 ///
535 /// \param Message If non-NULL and the result is not \c
536 /// AR_Available, will be set to a (possibly empty) message
537 /// describing why the declaration has not been introduced, is
538 /// deprecated, or is unavailable.
539 AvailabilityResult getAvailability(std::string *Message = 0) const;
540
541 /// \brief Determine whether this declaration is marked 'deprecated'.
542 ///
543 /// \param Message If non-NULL and the declaration is deprecated,
544 /// this will be set to the message describing why the declaration
545 /// was deprecated (which may be empty).
546 bool isDeprecated(std::string *Message = 0) const {
547 return getAvailability(Message) == AR_Deprecated;
548 }
549
550 /// \brief Determine whether this declaration is marked 'unavailable'.
551 ///
552 /// \param Message If non-NULL and the declaration is unavailable,
553 /// this will be set to the message describing why the declaration
554 /// was made unavailable (which may be empty).
555 bool isUnavailable(std::string *Message = 0) const {
556 return getAvailability(Message) == AR_Unavailable;
557 }
558
559 /// \brief Determine whether this is a weak-imported symbol.
560 ///
561 /// Weak-imported symbols are typically marked with the
Eli Friedman7f3ad232011-03-28 02:00:21 +0000562 /// 'weak_import' attribute, but may also be marked with an
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000563 /// 'availability' attribute where we're targing a platform prior to
564 /// the introduction of this feature.
565 bool isWeakImported() const;
566
567 /// \brief Determines whether this symbol can be weak-imported,
568 /// e.g., whether it would be well-formed to add the weak_import
569 /// attribute.
570 ///
571 /// \param IsDefinition Set to \c true to indicate that this
572 /// declaration cannot be weak-imported because it has a definition.
573 bool canBeWeakImported(bool &IsDefinition) const;
574
Douglas Gregor919814d2011-09-09 23:01:35 +0000575 /// \brief Determine whether this declaration came from an AST file (such as
576 /// a precompiled header or module) rather than having been parsed.
Douglas Gregor08e0bc12011-09-10 00:09:20 +0000577 bool isFromASTFile() const { return FromASTFile; }
David Blaikieba243b52011-11-09 06:07:30 +0000578
Douglas Gregorb6b60c12012-01-05 22:27:05 +0000579 /// \brief Retrieve the global declaration ID associated with this
580 /// declaration, which specifies where in the
581 unsigned getGlobalID() const {
582 if (isFromASTFile())
583 return *((const unsigned*)this - 1);
584 return 0;
585 }
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +0000586
587 /// \brief Retrieve the global ID of the module that owns this particular
588 /// declaration.
589 unsigned getOwningModuleID() const {
590 if (isFromASTFile())
591 return *((const unsigned*)this - 2);
592
593 return 0;
594 }
595
Douglas Gregorcc636682009-02-17 23:15:12 +0000596 unsigned getIdentifierNamespace() const {
Chris Lattner769dbdf2009-03-27 20:18:19 +0000597 return IdentifierNamespace;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000598 }
Chris Lattnerd62fdc42009-01-06 07:16:40 +0000599 bool isInIdentifierNamespace(unsigned NS) const {
600 return getIdentifierNamespace() & NS;
601 }
Chris Lattner769dbdf2009-03-27 20:18:19 +0000602 static unsigned getIdentifierNamespaceForKind(Kind DK);
603
John McCall0d6b1642010-04-23 18:46:30 +0000604 bool hasTagIdentifierNamespace() const {
605 return isTagIdentifierNamespace(getIdentifierNamespace());
606 }
607 static bool isTagIdentifierNamespace(unsigned NS) {
608 // TagDecls have Tag and Type set and may also have TagFriend.
609 return (NS & ~IDNS_TagFriend) == (IDNS_Tag | IDNS_Type);
610 }
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000612 /// getLexicalDeclContext - The declaration context where this Decl was
613 /// lexically declared (LexicalDC). May be different from
614 /// getDeclContext() (SemanticDC).
615 /// e.g.:
616 ///
617 /// namespace A {
618 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
619 /// }
620 /// void A::f(); // SemanticDC == namespace 'A'
621 /// // LexicalDC == global namespace
Chris Lattner10d83792009-03-27 18:46:15 +0000622 DeclContext *getLexicalDeclContext() {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000623 if (isInSemaDC())
Chris Lattner10d83792009-03-27 18:46:15 +0000624 return getSemanticDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000625 return getMultipleDC()->LexicalDC;
626 }
Chris Lattner10d83792009-03-27 18:46:15 +0000627 const DeclContext *getLexicalDeclContext() const {
628 return const_cast<Decl*>(this)->getLexicalDeclContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000629 }
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000630
Douglas Gregorda2142f2011-02-19 18:51:44 +0000631 virtual bool isOutOfLine() const {
632 return getLexicalDeclContext() != getDeclContext();
633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Douglas Gregor6ab35242009-04-09 21:40:53 +0000635 /// setDeclContext - Set both the semantic and lexical DeclContext
636 /// to DC.
637 void setDeclContext(DeclContext *DC);
638
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000639 void setLexicalDeclContext(DeclContext *DC);
640
Sebastian Redl7a126a42010-08-31 00:36:30 +0000641 /// isDefinedOutsideFunctionOrMethod - This predicate returns true if this
642 /// scoped decl is defined outside the current function or method. This is
643 /// roughly global variables and functions, but also handles enums (which
644 /// could be defined inside or outside a function etc).
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000645 bool isDefinedOutsideFunctionOrMethod() const {
646 return getParentFunctionOrMethod() == 0;
647 }
648
649 /// \brief If this decl is defined inside a function/method/block it returns
650 /// the corresponding DeclContext, otherwise it returns null.
651 const DeclContext *getParentFunctionOrMethod() const;
Argyrios Kyrtzidisfcc1e502011-09-28 18:14:24 +0000652 DeclContext *getParentFunctionOrMethod() {
653 return const_cast<DeclContext*>(
654 const_cast<const Decl*>(this)->getParentFunctionOrMethod());
655 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000656
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000657 /// \brief Retrieves the "canonical" declaration of the given declaration.
Douglas Gregorda2142f2011-02-19 18:51:44 +0000658 virtual Decl *getCanonicalDecl() { return this; }
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000659 const Decl *getCanonicalDecl() const {
660 return const_cast<Decl*>(this)->getCanonicalDecl();
661 }
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000662
Argyrios Kyrtzidisac4e3792009-07-18 08:50:48 +0000663 /// \brief Whether this particular Decl is a canonical one.
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000664 bool isCanonicalDecl() const { return getCanonicalDecl() == this; }
Douglas Gregorc896ad02011-12-14 21:44:45 +0000665
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000666protected:
667 /// \brief Returns the next redeclaration or itself if this is the only decl.
668 ///
669 /// Decl subclasses that can be redeclared should override this method so that
670 /// Decl::redecl_iterator can iterate over them.
Douglas Gregorda2142f2011-02-19 18:51:44 +0000671 virtual Decl *getNextRedeclaration() { return this; }
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000672
Douglas Gregoref96ee02012-01-14 16:38:05 +0000673 /// \brief Implementation of getPreviousDecl(), to be overridden by any
674 /// subclass that has a redeclaration chain.
675 virtual Decl *getPreviousDeclImpl() { return 0; }
676
677 /// \brief Implementation of getMostRecentDecl(), to be overridden by any
678 /// subclass that has a redeclaration chain.
679 virtual Decl *getMostRecentDeclImpl() { return this; }
680
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000681public:
682 /// \brief Iterates through all the redeclarations of the same decl.
683 class redecl_iterator {
684 /// Current - The current declaration.
685 Decl *Current;
686 Decl *Starter;
687
688 public:
David Blaikie581deb32012-06-06 20:45:41 +0000689 typedef Decl *value_type;
690 typedef const value_type &reference;
691 typedef const value_type *pointer;
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000692 typedef std::forward_iterator_tag iterator_category;
David Blaikie581deb32012-06-06 20:45:41 +0000693 typedef std::ptrdiff_t difference_type;
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000694
695 redecl_iterator() : Current(0) { }
696 explicit redecl_iterator(Decl *C) : Current(C), Starter(C) { }
697
David Blaikie581deb32012-06-06 20:45:41 +0000698 reference operator*() const { return Current; }
699 value_type operator->() const { return Current; }
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000700
701 redecl_iterator& operator++() {
702 assert(Current && "Advancing while iterator has reached end");
703 // Get either previous decl or latest decl.
704 Decl *Next = Current->getNextRedeclaration();
Argyrios Kyrtzidis22cbd2b2009-07-21 00:06:27 +0000705 assert(Next && "Should return next redeclaration or itself, never null!");
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000706 Current = (Next != Starter ? Next : 0);
707 return *this;
708 }
709
710 redecl_iterator operator++(int) {
711 redecl_iterator tmp(*this);
712 ++(*this);
713 return tmp;
714 }
715
Mike Stump1eb44332009-09-09 15:08:12 +0000716 friend bool operator==(redecl_iterator x, redecl_iterator y) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000717 return x.Current == y.Current;
718 }
Mike Stump1eb44332009-09-09 15:08:12 +0000719 friend bool operator!=(redecl_iterator x, redecl_iterator y) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000720 return x.Current != y.Current;
721 }
722 };
723
724 /// \brief Returns iterator for all the redeclarations of the same decl.
725 /// It will iterate at least once (when this decl is the only one).
726 redecl_iterator redecls_begin() const {
727 return redecl_iterator(const_cast<Decl*>(this));
728 }
729 redecl_iterator redecls_end() const { return redecl_iterator(); }
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000730
Douglas Gregorf785a7d2012-01-14 15:55:47 +0000731 /// \brief Retrieve the previous declaration that declares the same entity
732 /// as this declaration, or NULL if there is no previous declaration.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000733 Decl *getPreviousDecl() { return getPreviousDeclImpl(); }
734
735 /// \brief Retrieve the most recent declaration that declares the same entity
736 /// as this declaration, or NULL if there is no previous declaration.
737 const Decl *getPreviousDecl() const {
738 return const_cast<Decl *>(this)->getPreviousDeclImpl();
739 }
Douglas Gregorf785a7d2012-01-14 15:55:47 +0000740
741 /// \brief Retrieve the most recent declaration that declares the same entity
742 /// as this declaration (which may be this declaration).
Douglas Gregoref96ee02012-01-14 16:38:05 +0000743 Decl *getMostRecentDecl() { return getMostRecentDeclImpl(); }
744
745 /// \brief Retrieve the most recent declaration that declares the same entity
746 /// as this declaration (which may be this declaration).
747 const Decl *getMostRecentDecl() const {
748 return const_cast<Decl *>(this)->getMostRecentDeclImpl();
749 }
750
Sebastian Redld3a413d2009-04-26 20:35:05 +0000751 /// getBody - If this Decl represents a declaration for a body of code,
752 /// such as a function or method definition, this method returns the
753 /// top-level Stmt* of that body. Otherwise this method returns null.
Douglas Gregorda2142f2011-02-19 18:51:44 +0000754 virtual Stmt* getBody() const { return 0; }
Sebastian Redld3a413d2009-04-26 20:35:05 +0000755
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000756 /// \brief Returns true if this Decl represents a declaration for a body of
757 /// code, such as a function or method definition.
Douglas Gregorda2142f2011-02-19 18:51:44 +0000758 virtual bool hasBody() const { return getBody() != 0; }
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000759
Sebastian Redld3a413d2009-04-26 20:35:05 +0000760 /// getBodyRBrace - Gets the right brace of the body, if a body exists.
761 /// This works whether the body is a CompoundStmt or a CXXTryStmt.
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000762 SourceLocation getBodyRBrace() const;
Sebastian Redld3a413d2009-04-26 20:35:05 +0000763
Chris Lattner0ed844b2008-04-04 06:12:32 +0000764 // global temp stats (until we have a per-module visitor)
Sean Hunt9a555912010-05-30 07:21:58 +0000765 static void add(Kind k);
Daniel Dunbar02892a62012-03-05 21:42:49 +0000766 static void EnableStatistics();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000767 static void PrintStats();
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Anders Carlsson67e33202009-06-13 00:08:58 +0000769 /// isTemplateParameter - Determines whether this declaration is a
Douglas Gregorf57172b2008-12-08 18:40:42 +0000770 /// template parameter.
771 bool isTemplateParameter() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Anders Carlsson67e33202009-06-13 00:08:58 +0000773 /// isTemplateParameter - Determines whether this declaration is a
774 /// template parameter pack.
775 bool isTemplateParameterPack() const;
Douglas Gregorf57172b2008-12-08 18:40:42 +0000776
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000777 /// \brief Whether this declaration is a parameter pack.
778 bool isParameterPack() const;
David Blaikieba243b52011-11-09 06:07:30 +0000779
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000780 /// \brief returns true if this declaration is a template
781 bool isTemplateDecl() const;
782
Douglas Gregore53060f2009-06-25 22:08:12 +0000783 /// \brief Whether this declaration is a function or function template.
784 bool isFunctionOrFunctionTemplate() const;
John McCall02cace72009-08-28 07:59:38 +0000785
786 /// \brief Changes the namespace of this declaration to reflect that it's
787 /// the object of a friend declaration.
788 ///
789 /// These declarations appear in the lexical context of the friending
790 /// class, but in the semantic context of the actual entity. This property
791 /// applies only to a specific decl object; other redeclarations of the
792 /// same entity may not (and probably don't) share this property.
793 void setObjectOfFriendDecl(bool PreviouslyDeclared) {
794 unsigned OldNS = IdentifierNamespace;
John McCallb0cb0222010-03-27 05:57:59 +0000795 assert((OldNS & (IDNS_Tag | IDNS_Ordinary |
796 IDNS_TagFriend | IDNS_OrdinaryFriend)) &&
797 "namespace includes neither ordinary nor tag");
John McCall0d6b1642010-04-23 18:46:30 +0000798 assert(!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type |
John McCallb0cb0222010-03-27 05:57:59 +0000799 IDNS_TagFriend | IDNS_OrdinaryFriend)) &&
800 "namespace includes other than ordinary or tag");
John McCall02cace72009-08-28 07:59:38 +0000801
John McCallb0cb0222010-03-27 05:57:59 +0000802 IdentifierNamespace = 0;
803 if (OldNS & (IDNS_Tag | IDNS_TagFriend)) {
John McCall02cace72009-08-28 07:59:38 +0000804 IdentifierNamespace |= IDNS_TagFriend;
John McCall0d6b1642010-04-23 18:46:30 +0000805 if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Tag | IDNS_Type;
John McCallb0cb0222010-03-27 05:57:59 +0000806 }
807
808 if (OldNS & (IDNS_Ordinary | IDNS_OrdinaryFriend)) {
John McCall02cace72009-08-28 07:59:38 +0000809 IdentifierNamespace |= IDNS_OrdinaryFriend;
John McCallb0cb0222010-03-27 05:57:59 +0000810 if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Ordinary;
811 }
John McCall02cace72009-08-28 07:59:38 +0000812 }
813
814 enum FriendObjectKind {
815 FOK_None, // not a friend object
816 FOK_Declared, // a friend of a previously-declared entity
817 FOK_Undeclared // a friend of a previously-undeclared entity
818 };
819
820 /// \brief Determines whether this declaration is the object of a
821 /// friend declaration and, if so, what kind.
822 ///
823 /// There is currently no direct way to find the associated FriendDecl.
824 FriendObjectKind getFriendObjectKind() const {
825 unsigned mask
826 = (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend));
827 if (!mask) return FOK_None;
David Blaikieba243b52011-11-09 06:07:30 +0000828 return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ?
Anders Carlsson255d6412009-09-13 23:59:13 +0000829 FOK_Declared : FOK_Undeclared);
John McCall02cace72009-08-28 07:59:38 +0000830 }
Mike Stump1eb44332009-09-09 15:08:12 +0000831
John McCall76d32642010-04-24 01:30:58 +0000832 /// Specifies that this declaration is a C++ overloaded non-member.
833 void setNonMemberOperator() {
834 assert(getKind() == Function || getKind() == FunctionTemplate);
835 assert((IdentifierNamespace & IDNS_Ordinary) &&
836 "visible non-member operators should be in ordinary namespace");
837 IdentifierNamespace |= IDNS_NonMemberOperator;
838 }
839
John McCall80cd64a2010-01-29 01:45:37 +0000840 static bool classofKind(Kind K) { return true; }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000841 static DeclContext *castToDeclContext(const Decl *);
842 static Decl *castFromDeclContext(const DeclContext *);
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Richard Trieu5cb3d692011-07-28 00:19:05 +0000844 void print(raw_ostream &Out, unsigned Indentation = 0,
845 bool PrintInstantiation = false) const;
Chris Lattner8cc488f2011-07-20 07:06:53 +0000846 void print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu5cb3d692011-07-28 00:19:05 +0000847 unsigned Indentation = 0, bool PrintInstantiation = false) const;
Eli Friedman42f42c02009-05-30 04:20:30 +0000848 static void printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner8cc488f2011-07-20 07:06:53 +0000849 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000850 unsigned Indentation = 0);
Alexander Kornienko559b9282012-07-26 17:11:45 +0000851 // Debuggers don't usually respect default arguments.
Benjamin Kramer42c72c22012-08-14 14:50:32 +0000852 LLVM_ATTRIBUTE_USED void dump() const;
Alexander Kornienko559b9282012-07-26 17:11:45 +0000853 void dump(raw_ostream &Out) const;
854 // Debuggers don't usually respect default arguments.
Benjamin Kramer42c72c22012-08-14 14:50:32 +0000855 LLVM_ATTRIBUTE_USED void dumpXML() const;
Alexander Kornienko559b9282012-07-26 17:11:45 +0000856 void dumpXML(raw_ostream &OS) const;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000857
Chris Lattner81abbdd2009-03-21 06:27:31 +0000858private:
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000859 void setAttrsImpl(const AttrVec& Attrs, ASTContext &Ctx);
860 void setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
861 ASTContext &Ctx);
Chris Lattner81abbdd2009-03-21 06:27:31 +0000862
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000863protected:
864 ASTMutationListener *getASTMutationListener() const;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000865};
866
Douglas Gregoreaa95112011-12-14 23:59:32 +0000867/// \brief Determine whether two declarations declare the same entity.
868inline bool declaresSameEntity(const Decl *D1, const Decl *D2) {
Douglas Gregoreaa95112011-12-14 23:59:32 +0000869 if (!D1 || !D2)
870 return false;
871
Douglas Gregordec1cc42011-12-15 17:15:07 +0000872 if (D1 == D2)
873 return true;
874
Douglas Gregoreaa95112011-12-14 23:59:32 +0000875 return D1->getCanonicalDecl() == D2->getCanonicalDecl();
876}
877
Chris Lattner49f28ca2009-03-05 08:00:35 +0000878/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when
879/// doing something to a specific decl.
880class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
Daniel Dunbarc5236562009-11-21 09:05:59 +0000881 const Decl *TheDecl;
Chris Lattner49f28ca2009-03-05 08:00:35 +0000882 SourceLocation Loc;
883 SourceManager &SM;
884 const char *Message;
885public:
Daniel Dunbarc5236562009-11-21 09:05:59 +0000886 PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L,
Chris Lattner49f28ca2009-03-05 08:00:35 +0000887 SourceManager &sm, const char *Msg)
888 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Chris Lattner8cc488f2011-07-20 07:06:53 +0000890 virtual void print(raw_ostream &OS) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000891};
892
David Blaikie3bc93e32012-12-19 00:45:41 +0000893typedef llvm::MutableArrayRef<NamedDecl*> DeclContextLookupResult;
John McCallea318642010-08-26 09:15:37 +0000894
David Blaikie3bc93e32012-12-19 00:45:41 +0000895typedef llvm::ArrayRef<NamedDecl*> DeclContextLookupConstResult;
Chris Lattner49f28ca2009-03-05 08:00:35 +0000896
Chris Lattnerb048c982008-04-06 04:47:34 +0000897/// DeclContext - This is used only as base class of specific decl types that
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000898/// can act as declaration contexts. These decls are (only the top classes
899/// that directly derive from DeclContext are mentioned, not their subclasses):
Chris Lattner0ed844b2008-04-04 06:12:32 +0000900///
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000901/// TranslationUnitDecl
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000902/// NamespaceDecl
Chris Lattner0ed844b2008-04-04 06:12:32 +0000903/// FunctionDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000904/// TagDecl
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000905/// ObjCMethodDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000906/// ObjCContainerDecl
Douglas Gregor074149e2009-01-05 19:45:36 +0000907/// LinkageSpecDecl
Steve Naroff56ee6892008-10-08 17:01:13 +0000908/// BlockDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000909///
Chris Lattnerb048c982008-04-06 04:47:34 +0000910class DeclContext {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000911 /// DeclKind - This indicates which class this is.
John McCall35043e52010-10-19 05:43:52 +0000912 unsigned DeclKind : 8;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000913
Douglas Gregor2cf26342009-04-09 22:27:44 +0000914 /// \brief Whether this declaration context also has some external
915 /// storage that contains additional declarations that are lexically
916 /// part of this context.
John McCall35043e52010-10-19 05:43:52 +0000917 mutable unsigned ExternalLexicalStorage : 1;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000918
919 /// \brief Whether this declaration context also has some external
920 /// storage that contains additional declarations that are visible
921 /// in this context.
John McCall35043e52010-10-19 05:43:52 +0000922 mutable unsigned ExternalVisibleStorage : 1;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000923
Douglas Gregorc36c5402009-04-09 17:29:08 +0000924 /// \brief Pointer to the data structure used to lookup declarations
John McCall0c01d182010-03-24 05:22:00 +0000925 /// within this context (or a DependentStoredDeclsMap if this is a
Richard Smithc5d3e802012-03-16 06:12:59 +0000926 /// dependent context), and a bool indicating whether we have lazily
927 /// omitted any declarations from the map. We maintain the invariant
928 /// that, if the map contains an entry for a DeclarationName, then it
929 /// contains all relevant entries for that name.
930 mutable llvm::PointerIntPair<StoredDeclsMap*, 1, bool> LookupPtr;
Douglas Gregor44b43212008-12-11 16:49:14 +0000931
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000932protected:
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000933 /// FirstDecl - The first declaration stored within this declaration
934 /// context.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000935 mutable Decl *FirstDecl;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000936
937 /// LastDecl - The last declaration stored within this declaration
938 /// context. FIXME: We could probably cache this value somewhere
939 /// outside of the DeclContext, to reduce the size of DeclContext by
940 /// another pointer.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000941 mutable Decl *LastDecl;
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000942
John McCall76bd1f32010-06-01 09:23:16 +0000943 friend class ExternalASTSource;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +0000944 friend class ASTWriter;
John McCall76bd1f32010-06-01 09:23:16 +0000945
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000946 /// \brief Build up a chain of declarations.
947 ///
948 /// \returns the first/last pair of declarations.
949 static std::pair<Decl *, Decl *>
Bill Wendling341785e2012-02-22 09:51:33 +0000950 BuildDeclChain(ArrayRef<Decl*> Decls, bool FieldsAlreadyLoaded);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000951
Mike Stump1eb44332009-09-09 15:08:12 +0000952 DeclContext(Decl::Kind K)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000953 : DeclKind(K), ExternalLexicalStorage(false),
Richard Smithc5d3e802012-03-16 06:12:59 +0000954 ExternalVisibleStorage(false), LookupPtr(0, false), FirstDecl(0),
Douglas Gregor2cf26342009-04-09 22:27:44 +0000955 LastDecl(0) { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000956
Chris Lattner0ed844b2008-04-04 06:12:32 +0000957public:
Douglas Gregor44b43212008-12-11 16:49:14 +0000958 ~DeclContext();
959
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000960 Decl::Kind getDeclKind() const {
John McCall35043e52010-10-19 05:43:52 +0000961 return static_cast<Decl::Kind>(DeclKind);
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000962 }
Steve Naroff0a473932009-01-20 19:53:53 +0000963 const char *getDeclKindName() const;
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000964
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +0000965 /// getParent - Returns the containing DeclContext.
Chris Lattner0cf2b192009-03-27 19:19:59 +0000966 DeclContext *getParent() {
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +0000967 return cast<Decl>(this)->getDeclContext();
968 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000969 const DeclContext *getParent() const {
970 return const_cast<DeclContext*>(this)->getParent();
Argyrios Kyrtzidisd2595ec2008-10-12 18:40:01 +0000971 }
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000973 /// getLexicalParent - Returns the containing lexical DeclContext. May be
Douglas Gregor44b43212008-12-11 16:49:14 +0000974 /// different from getParent, e.g.:
975 ///
976 /// namespace A {
977 /// struct S;
978 /// }
979 /// struct A::S {}; // getParent() == namespace 'A'
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000980 /// // getLexicalParent() == translation unit
981 ///
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000982 DeclContext *getLexicalParent() {
Chris Lattner0cf2b192009-03-27 19:19:59 +0000983 return cast<Decl>(this)->getLexicalDeclContext();
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000984 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000985 const DeclContext *getLexicalParent() const {
986 return const_cast<DeclContext*>(this)->getLexicalParent();
Mike Stump1eb44332009-09-09 15:08:12 +0000987 }
Argyrios Kyrtzidis048f30a2009-06-30 02:35:38 +0000988
Douglas Gregore942bbe2009-09-10 16:57:35 +0000989 DeclContext *getLookupParent();
David Blaikieba243b52011-11-09 06:07:30 +0000990
Douglas Gregore942bbe2009-09-10 16:57:35 +0000991 const DeclContext *getLookupParent() const {
992 return const_cast<DeclContext*>(this)->getLookupParent();
993 }
David Blaikieba243b52011-11-09 06:07:30 +0000994
Argyrios Kyrtzidis048f30a2009-06-30 02:35:38 +0000995 ASTContext &getParentASTContext() const {
996 return cast<Decl>(this)->getASTContext();
997 }
998
John McCallaab9e312011-02-22 22:25:23 +0000999 bool isClosure() const {
1000 return DeclKind == Decl::Block;
1001 }
1002
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001003 bool isObjCContainer() const {
Fariborz Jahanian80f77532011-08-22 17:13:51 +00001004 switch (DeclKind) {
1005 case Decl::ObjCCategory:
1006 case Decl::ObjCCategoryImpl:
1007 case Decl::ObjCImplementation:
1008 case Decl::ObjCInterface:
1009 case Decl::ObjCProtocol:
1010 return true;
1011 }
1012 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001013 }
1014
Chris Lattner0ed844b2008-04-04 06:12:32 +00001015 bool isFunctionOrMethod() const {
1016 switch (DeclKind) {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001017 case Decl::Block:
1018 case Decl::ObjCMethod:
1019 return true;
1020 default:
Sean Hunt9a555912010-05-30 07:21:58 +00001021 return DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001022 }
1023 }
1024
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +00001025 bool isFileContext() const {
1026 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
1027 }
1028
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001029 bool isTranslationUnit() const {
1030 return DeclKind == Decl::TranslationUnit;
1031 }
1032
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001033 bool isRecord() const {
Sean Hunt9a555912010-05-30 07:21:58 +00001034 return DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord;
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001035 }
1036
Douglas Gregor44b43212008-12-11 16:49:14 +00001037 bool isNamespace() const {
1038 return DeclKind == Decl::Namespace;
1039 }
1040
Sebastian Redl410c4f22010-08-31 20:53:31 +00001041 bool isInlineNamespace() const;
1042
Douglas Gregorbc221632009-05-28 16:34:51 +00001043 /// \brief Determines whether this context is dependent on a
1044 /// template parameter.
1045 bool isDependentContext() const;
1046
Douglas Gregor074149e2009-01-05 19:45:36 +00001047 /// isTransparentContext - Determines whether this context is a
1048 /// "transparent" context, meaning that the members declared in this
1049 /// context are semantically declared in the nearest enclosing
1050 /// non-transparent (opaque) context but are lexically declared in
1051 /// this context. For example, consider the enumerators of an
Mike Stump1eb44332009-09-09 15:08:12 +00001052 /// enumeration type:
Douglas Gregor074149e2009-01-05 19:45:36 +00001053 /// @code
1054 /// enum E {
Mike Stump1eb44332009-09-09 15:08:12 +00001055 /// Val1
Douglas Gregor074149e2009-01-05 19:45:36 +00001056 /// };
1057 /// @endcode
1058 /// Here, E is a transparent context, so its enumerator (Val1) will
1059 /// appear (semantically) that it is in the same context of E.
1060 /// Examples of transparent contexts include: enumerations (except for
Sebastian Redl410c4f22010-08-31 20:53:31 +00001061 /// C++0x scoped enums), and C++ linkage specifications.
Douglas Gregor074149e2009-01-05 19:45:36 +00001062 bool isTransparentContext() const;
1063
John McCallac65c622010-10-26 04:59:26 +00001064 /// \brief Determines whether this context is, or is nested within,
1065 /// a C++ extern "C" linkage spec.
1066 bool isExternCContext() const;
1067
Douglas Gregor61481da2009-09-01 17:22:34 +00001068 /// \brief Determine whether this declaration context is equivalent
1069 /// to the declaration context DC.
Sebastian Redl7a126a42010-08-31 00:36:30 +00001070 bool Equals(const DeclContext *DC) const {
Douglas Gregordbdf5e72010-03-15 15:26:48 +00001071 return DC && this->getPrimaryContext() == DC->getPrimaryContext();
Douglas Gregor61481da2009-09-01 17:22:34 +00001072 }
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Douglas Gregor6dd38da2009-08-27 06:03:53 +00001074 /// \brief Determine whether this declaration context encloses the
1075 /// declaration context DC.
Sebastian Redl7a126a42010-08-31 00:36:30 +00001076 bool Encloses(const DeclContext *DC) const;
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +00001077
John McCall4b9c2d22011-11-06 09:01:30 +00001078 /// \brief Find the nearest non-closure ancestor of this context,
1079 /// i.e. the innermost semantic parent of this context which is not
1080 /// a closure. A context may be its own non-closure ancestor.
1081 DeclContext *getNonClosureAncestor();
1082 const DeclContext *getNonClosureAncestor() const {
1083 return const_cast<DeclContext*>(this)->getNonClosureAncestor();
1084 }
1085
Douglas Gregor44b43212008-12-11 16:49:14 +00001086 /// getPrimaryContext - There may be many different
1087 /// declarations of the same entity (including forward declarations
1088 /// of classes, multiple definitions of namespaces, etc.), each with
1089 /// a different set of declarations. This routine returns the
1090 /// "primary" DeclContext structure, which will contain the
1091 /// information needed to perform name lookup into this context.
Steve Naroff0701bbb2009-01-08 17:28:14 +00001092 DeclContext *getPrimaryContext();
John McCall0c01d182010-03-24 05:22:00 +00001093 const DeclContext *getPrimaryContext() const {
1094 return const_cast<DeclContext*>(this)->getPrimaryContext();
1095 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001096
Sebastian Redl7a126a42010-08-31 00:36:30 +00001097 /// getRedeclContext - Retrieve the context in which an entity conflicts with
1098 /// other entities of the same name, or where it is a redeclaration if the
Sebastian Redl410c4f22010-08-31 20:53:31 +00001099 /// two entities are compatible. This skips through transparent contexts.
Sebastian Redl7a126a42010-08-31 00:36:30 +00001100 DeclContext *getRedeclContext();
1101 const DeclContext *getRedeclContext() const {
1102 return const_cast<DeclContext *>(this)->getRedeclContext();
Douglas Gregor17a9b9e2009-01-07 02:48:43 +00001103 }
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Douglas Gregor88b70942009-02-25 22:02:03 +00001105 /// \brief Retrieve the nearest enclosing namespace context.
1106 DeclContext *getEnclosingNamespaceContext();
1107 const DeclContext *getEnclosingNamespaceContext() const {
1108 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
1109 }
1110
Sebastian Redl7a126a42010-08-31 00:36:30 +00001111 /// \brief Test if this context is part of the enclosing namespace set of
1112 /// the context NS, as defined in C++0x [namespace.def]p9. If either context
1113 /// isn't a namespace, this is equivalent to Equals().
1114 ///
1115 /// The enclosing namespace set of a namespace is the namespace and, if it is
1116 /// inline, its enclosing namespace, recursively.
1117 bool InEnclosingNamespaceSetOf(const DeclContext *NS) const;
1118
James Dennett18483f52012-06-15 08:37:50 +00001119 /// \brief Collects all of the declaration contexts that are semantically
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001120 /// connected to this declaration context.
1121 ///
1122 /// For declaration contexts that have multiple semantically connected but
1123 /// syntactically distinct contexts, such as C++ namespaces, this routine
1124 /// retrieves the complete set of such declaration contexts in source order.
1125 /// For example, given:
1126 ///
1127 /// \code
Douglas Gregor44b43212008-12-11 16:49:14 +00001128 /// namespace N {
1129 /// int x;
1130 /// }
1131 /// namespace N {
1132 /// int y;
1133 /// }
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001134 /// \endcode
1135 ///
1136 /// The \c Contexts parameter will contain both definitions of N.
1137 ///
1138 /// \param Contexts Will be cleared and set to the set of declaration
1139 /// contexts that are semanticaly connected to this declaration context,
1140 /// in source order, including this context (which may be the only result,
1141 /// for non-namespace contexts).
1142 void collectAllContexts(llvm::SmallVectorImpl<DeclContext *> &Contexts);
Douglas Gregor44b43212008-12-11 16:49:14 +00001143
1144 /// decl_iterator - Iterates through the declarations stored
1145 /// within this context.
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001146 class decl_iterator {
1147 /// Current - The current declaration.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001148 Decl *Current;
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001149
1150 public:
David Blaikie581deb32012-06-06 20:45:41 +00001151 typedef Decl *value_type;
1152 typedef const value_type &reference;
1153 typedef const value_type *pointer;
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001154 typedef std::forward_iterator_tag iterator_category;
1155 typedef std::ptrdiff_t difference_type;
1156
1157 decl_iterator() : Current(0) { }
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001158 explicit decl_iterator(Decl *C) : Current(C) { }
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001159
1160 reference operator*() const { return Current; }
David Blaikie581deb32012-06-06 20:45:41 +00001161 // This doesn't meet the iterator requirements, but it's convenient
1162 value_type operator->() const { return Current; }
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001163
Chris Lattner96f44682009-03-28 05:59:45 +00001164 decl_iterator& operator++() {
Chris Lattner244a67d2009-03-28 06:04:26 +00001165 Current = Current->getNextDeclInContext();
Chris Lattner96f44682009-03-28 05:59:45 +00001166 return *this;
1167 }
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001168
1169 decl_iterator operator++(int) {
1170 decl_iterator tmp(*this);
1171 ++(*this);
1172 return tmp;
1173 }
1174
Mike Stump1eb44332009-09-09 15:08:12 +00001175 friend bool operator==(decl_iterator x, decl_iterator y) {
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001176 return x.Current == y.Current;
1177 }
Mike Stump1eb44332009-09-09 15:08:12 +00001178 friend bool operator!=(decl_iterator x, decl_iterator y) {
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001179 return x.Current != y.Current;
1180 }
1181 };
Douglas Gregor44b43212008-12-11 16:49:14 +00001182
Douglas Gregor44b43212008-12-11 16:49:14 +00001183 /// decls_begin/decls_end - Iterate over the declarations stored in
Mike Stump1eb44332009-09-09 15:08:12 +00001184 /// this context.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001185 decl_iterator decls_begin() const;
David Blaikie581deb32012-06-06 20:45:41 +00001186 decl_iterator decls_end() const { return decl_iterator(); }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001187 bool decls_empty() const;
Douglas Gregor44b43212008-12-11 16:49:14 +00001188
Sebastian Redl681d7232010-07-27 00:17:23 +00001189 /// noload_decls_begin/end - Iterate over the declarations stored in this
1190 /// context that are currently loaded; don't attempt to retrieve anything
1191 /// from an external source.
1192 decl_iterator noload_decls_begin() const;
David Blaikie581deb32012-06-06 20:45:41 +00001193 decl_iterator noload_decls_end() const { return decl_iterator(); }
Sebastian Redl681d7232010-07-27 00:17:23 +00001194
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001195 /// specific_decl_iterator - Iterates over a subrange of
1196 /// declarations stored in a DeclContext, providing only those that
Douglas Gregor669c9a22009-02-02 18:25:48 +00001197 /// are of type SpecificDecl (or a class derived from it). This
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001198 /// iterator is used, for example, to provide iteration over just
Douglas Gregor669c9a22009-02-02 18:25:48 +00001199 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001200 template<typename SpecificDecl>
1201 class specific_decl_iterator {
1202 /// Current - The current, underlying declaration iterator, which
Douglas Gregord6f0b4e2009-02-02 17:56:05 +00001203 /// will either be NULL or will point to a declaration of
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001204 /// type SpecificDecl.
1205 DeclContext::decl_iterator Current;
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001207 /// SkipToNextDecl - Advances the current position up to the next
1208 /// declaration of type SpecificDecl that also meets the criteria
1209 /// required by Acceptable.
1210 void SkipToNextDecl() {
Douglas Gregor669c9a22009-02-02 18:25:48 +00001211 while (*Current && !isa<SpecificDecl>(*Current))
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001212 ++Current;
1213 }
1214
1215 public:
David Blaikie581deb32012-06-06 20:45:41 +00001216 typedef SpecificDecl *value_type;
1217 // TODO: Add reference and pointer typedefs (with some appropriate proxy
1218 // type) if we ever have a need for them.
1219 typedef void reference;
1220 typedef void pointer;
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001221 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1222 difference_type;
1223 typedef std::forward_iterator_tag iterator_category;
1224
Douglas Gregor669c9a22009-02-02 18:25:48 +00001225 specific_decl_iterator() : Current() { }
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001226
1227 /// specific_decl_iterator - Construct a new iterator over a
Douglas Gregord6f0b4e2009-02-02 17:56:05 +00001228 /// subset of the declarations the range [C,
1229 /// end-of-declarations). If A is non-NULL, it is a pointer to a
1230 /// member function of SpecificDecl that should return true for
1231 /// all of the SpecificDecl instances that will be in the subset
1232 /// of iterators. For example, if you want Objective-C instance
1233 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1234 /// &ObjCMethodDecl::isInstanceMethod.
Douglas Gregor669c9a22009-02-02 18:25:48 +00001235 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001236 SkipToNextDecl();
1237 }
1238
David Blaikie581deb32012-06-06 20:45:41 +00001239 value_type operator*() const { return cast<SpecificDecl>(*Current); }
1240 // This doesn't meet the iterator requirements, but it's convenient
1241 value_type operator->() const { return **this; }
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001242
1243 specific_decl_iterator& operator++() {
1244 ++Current;
1245 SkipToNextDecl();
1246 return *this;
1247 }
1248
1249 specific_decl_iterator operator++(int) {
1250 specific_decl_iterator tmp(*this);
1251 ++(*this);
1252 return tmp;
1253 }
Mike Stump1eb44332009-09-09 15:08:12 +00001254
David Blaikieba243b52011-11-09 06:07:30 +00001255 friend bool operator==(const specific_decl_iterator& x,
1256 const specific_decl_iterator& y) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001257 return x.Current == y.Current;
1258 }
Mike Stump1eb44332009-09-09 15:08:12 +00001259
David Blaikieba243b52011-11-09 06:07:30 +00001260 friend bool operator!=(const specific_decl_iterator& x,
1261 const specific_decl_iterator& y) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001262 return x.Current != y.Current;
1263 }
1264 };
1265
Douglas Gregor669c9a22009-02-02 18:25:48 +00001266 /// \brief Iterates over a filtered subrange of declarations stored
1267 /// in a DeclContext.
1268 ///
1269 /// This iterator visits only those declarations that are of type
1270 /// SpecificDecl (or a class derived from it) and that meet some
1271 /// additional run-time criteria. This iterator is used, for
1272 /// example, to provide access to the instance methods within an
1273 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
1274 /// Acceptable = ObjCMethodDecl::isInstanceMethod).
1275 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
1276 class filtered_decl_iterator {
1277 /// Current - The current, underlying declaration iterator, which
1278 /// will either be NULL or will point to a declaration of
1279 /// type SpecificDecl.
1280 DeclContext::decl_iterator Current;
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Douglas Gregor669c9a22009-02-02 18:25:48 +00001282 /// SkipToNextDecl - Advances the current position up to the next
1283 /// declaration of type SpecificDecl that also meets the criteria
1284 /// required by Acceptable.
1285 void SkipToNextDecl() {
1286 while (*Current &&
1287 (!isa<SpecificDecl>(*Current) ||
1288 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
1289 ++Current;
1290 }
1291
1292 public:
David Blaikie581deb32012-06-06 20:45:41 +00001293 typedef SpecificDecl *value_type;
1294 // TODO: Add reference and pointer typedefs (with some appropriate proxy
1295 // type) if we ever have a need for them.
1296 typedef void reference;
1297 typedef void pointer;
Douglas Gregor669c9a22009-02-02 18:25:48 +00001298 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1299 difference_type;
1300 typedef std::forward_iterator_tag iterator_category;
1301
1302 filtered_decl_iterator() : Current() { }
1303
David Blaikiebd4fa452012-05-01 00:48:43 +00001304 /// filtered_decl_iterator - Construct a new iterator over a
Douglas Gregor669c9a22009-02-02 18:25:48 +00001305 /// subset of the declarations the range [C,
1306 /// end-of-declarations). If A is non-NULL, it is a pointer to a
1307 /// member function of SpecificDecl that should return true for
1308 /// all of the SpecificDecl instances that will be in the subset
1309 /// of iterators. For example, if you want Objective-C instance
1310 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1311 /// &ObjCMethodDecl::isInstanceMethod.
1312 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
1313 SkipToNextDecl();
1314 }
1315
David Blaikie581deb32012-06-06 20:45:41 +00001316 value_type operator*() const { return cast<SpecificDecl>(*Current); }
1317 value_type operator->() const { return cast<SpecificDecl>(*Current); }
Douglas Gregor669c9a22009-02-02 18:25:48 +00001318
1319 filtered_decl_iterator& operator++() {
1320 ++Current;
1321 SkipToNextDecl();
1322 return *this;
1323 }
1324
1325 filtered_decl_iterator operator++(int) {
1326 filtered_decl_iterator tmp(*this);
1327 ++(*this);
1328 return tmp;
1329 }
Mike Stump1eb44332009-09-09 15:08:12 +00001330
David Blaikieba243b52011-11-09 06:07:30 +00001331 friend bool operator==(const filtered_decl_iterator& x,
1332 const filtered_decl_iterator& y) {
Douglas Gregor669c9a22009-02-02 18:25:48 +00001333 return x.Current == y.Current;
1334 }
Mike Stump1eb44332009-09-09 15:08:12 +00001335
David Blaikieba243b52011-11-09 06:07:30 +00001336 friend bool operator!=(const filtered_decl_iterator& x,
1337 const filtered_decl_iterator& y) {
Douglas Gregor669c9a22009-02-02 18:25:48 +00001338 return x.Current != y.Current;
1339 }
1340 };
1341
Douglas Gregor40f4e692009-01-20 16:54:50 +00001342 /// @brief Add the declaration D into this context.
1343 ///
1344 /// This routine should be invoked when the declaration D has first
1345 /// been declared, to place D into the context where it was
1346 /// (lexically) defined. Every declaration must be added to one
1347 /// (and only one!) context, where it can be visited via
1348 /// [decls_begin(), decls_end()). Once a declaration has been added
1349 /// to its lexical context, the corresponding DeclContext owns the
1350 /// declaration.
1351 ///
1352 /// If D is also a NamedDecl, it will be made visible within its
1353 /// semantic context via makeDeclVisibleInContext.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001354 void addDecl(Decl *D);
Sean Callanan381509e2011-10-21 16:15:18 +00001355
1356 /// @brief Add the declaration D into this context, but suppress
1357 /// searches for external declarations with the same name.
1358 ///
1359 /// Although analogous in function to addDecl, this removes an
1360 /// important check. This is only useful if the Decl is being
1361 /// added in response to an external search; in all other cases,
1362 /// addDecl() is the right function to use.
1363 /// See the ASTImporter for use cases.
Sean Callanan9faf8102011-10-21 02:57:43 +00001364 void addDeclInternal(Decl *D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001365
John McCall3f9a8a62009-08-11 06:59:38 +00001366 /// @brief Add the declaration D to this context without modifying
1367 /// any lookup tables.
1368 ///
1369 /// This is useful for some operations in dependent contexts where
1370 /// the semantic context might not be dependent; this basically
1371 /// only happens with friends.
1372 void addHiddenDecl(Decl *D);
1373
John McCall9f54ad42009-12-10 09:41:52 +00001374 /// @brief Removes a declaration from this context.
1375 void removeDecl(Decl *D);
1376
Douglas Gregor44b43212008-12-11 16:49:14 +00001377 /// lookup_iterator - An iterator that provides access to the results
1378 /// of looking up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001379 typedef NamedDecl **lookup_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +00001380
1381 /// lookup_const_iterator - An iterator that provides non-mutable
1382 /// access to the results of lookup up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001383 typedef NamedDecl * const * lookup_const_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +00001384
John McCallea318642010-08-26 09:15:37 +00001385 typedef DeclContextLookupResult lookup_result;
1386 typedef DeclContextLookupConstResult lookup_const_result;
Douglas Gregor44b43212008-12-11 16:49:14 +00001387
1388 /// lookup - Find the declarations (if any) with the given Name in
1389 /// this context. Returns a range of iterators that contains all of
Douglas Gregor40f4e692009-01-20 16:54:50 +00001390 /// the declarations with this name, with object, function, member,
1391 /// and enumerator names preceding any tag name. Note that this
1392 /// routine will not look into parent contexts.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001393 lookup_result lookup(DeclarationName Name);
Douglas Gregor514d3b62012-05-03 23:18:44 +00001394 lookup_const_result lookup(DeclarationName Name) const {
1395 return const_cast<DeclContext*>(this)->lookup(Name);
1396 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001397
Douglas Gregorb75a3452011-10-15 00:10:27 +00001398 /// \brief A simplistic name lookup mechanism that performs name lookup
1399 /// into this declaration context without consulting the external source.
1400 ///
David Blaikieba243b52011-11-09 06:07:30 +00001401 /// This function should almost never be used, because it subverts the
Douglas Gregorb75a3452011-10-15 00:10:27 +00001402 /// usual relationship between a DeclContext and the external source.
1403 /// See the ASTImporter for the (few, but important) use cases.
David Blaikieba243b52011-11-09 06:07:30 +00001404 void localUncachedLookup(DeclarationName Name,
Douglas Gregorb75a3452011-10-15 00:10:27 +00001405 llvm::SmallVectorImpl<NamedDecl *> &Results);
David Blaikieba243b52011-11-09 06:07:30 +00001406
Douglas Gregor40f4e692009-01-20 16:54:50 +00001407 /// @brief Makes a declaration visible within this context.
1408 ///
1409 /// This routine makes the declaration D visible to name lookup
1410 /// within this context and, if this is a transparent context,
1411 /// within its parent contexts up to the first enclosing
1412 /// non-transparent context. Making a declaration visible within a
1413 /// context does not transfer ownership of a declaration, and a
1414 /// declaration can be visible in many contexts that aren't its
1415 /// lexical context.
1416 ///
1417 /// If D is a redeclaration of an existing declaration that is
1418 /// visible from this context, as determined by
1419 /// NamedDecl::declarationReplaces, the previous declaration will be
1420 /// replaced with D.
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001421 void makeDeclVisibleInContext(NamedDecl *D);
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +00001422
Nick Lewycky173a37a2012-04-03 21:44:08 +00001423 /// all_lookups_iterator - An iterator that provides a view over the results
1424 /// of looking up every possible name.
1425 class all_lookups_iterator;
1426
1427 all_lookups_iterator lookups_begin() const;
1428
1429 all_lookups_iterator lookups_end() const;
1430
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001431 /// udir_iterator - Iterates through the using-directives stored
1432 /// within this context.
1433 typedef UsingDirectiveDecl * const * udir_iterator;
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001435 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range;
1436
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001437 udir_iterator_range getUsingDirectives() const;
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001438
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001439 udir_iterator using_directives_begin() const {
1440 return getUsingDirectives().first;
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001441 }
1442
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001443 udir_iterator using_directives_end() const {
1444 return getUsingDirectives().second;
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001445 }
1446
John McCall0c01d182010-03-24 05:22:00 +00001447 // These are all defined in DependentDiagnostic.h.
1448 class ddiag_iterator;
1449 inline ddiag_iterator ddiag_begin() const;
1450 inline ddiag_iterator ddiag_end() const;
1451
Douglas Gregorc2ee10d2009-04-07 17:20:56 +00001452 // Low-level accessors
Sean Callanand5a20c12012-08-23 21:16:40 +00001453
1454 /// \brief Mark the lookup table as needing to be built. This should be
1455 /// used only if setHasExternalLexicalStorage() has been called.
1456 void setMustBuildLookupTable() {
1457 assert(ExternalLexicalStorage && "Requires external lexical storage");
1458 LookupPtr.setInt(true);
1459 }
Douglas Gregorc2ee10d2009-04-07 17:20:56 +00001460
Douglas Gregorc2ee10d2009-04-07 17:20:56 +00001461 /// \brief Retrieve the internal representation of the lookup structure.
Richard Smithc5d3e802012-03-16 06:12:59 +00001462 /// This may omit some names if we are lazily building the structure.
1463 StoredDeclsMap *getLookupPtr() const { return LookupPtr.getPointer(); }
1464
1465 /// \brief Ensure the lookup structure is fully-built and return it.
1466 StoredDeclsMap *buildLookup();
Douglas Gregorc2ee10d2009-04-07 17:20:56 +00001467
Douglas Gregor2cf26342009-04-09 22:27:44 +00001468 /// \brief Whether this DeclContext has external storage containing
1469 /// additional declarations that are lexically in this context.
1470 bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; }
1471
1472 /// \brief State whether this DeclContext has external storage for
1473 /// declarations lexically in this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001474 void setHasExternalLexicalStorage(bool ES = true) {
1475 ExternalLexicalStorage = ES;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001476 }
1477
1478 /// \brief Whether this DeclContext has external storage containing
1479 /// additional declarations that are visible in this context.
1480 bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; }
1481
1482 /// \brief State whether this DeclContext has external storage for
1483 /// declarations visible in this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001484 void setHasExternalVisibleStorage(bool ES = true) {
1485 ExternalVisibleStorage = ES;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001486 }
1487
Douglas Gregor2ea054f2011-08-26 22:04:51 +00001488 /// \brief Determine whether the given declaration is stored in the list of
1489 /// declarations lexically within this context.
1490 bool isDeclInLexicalTraversal(const Decl *D) const {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001491 return D && (D->NextInContextAndBits.getPointer() || D == FirstDecl ||
1492 D == LastDecl);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00001493 }
David Blaikieba243b52011-11-09 06:07:30 +00001494
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +00001495 static bool classof(const Decl *D);
Chris Lattnerb048c982008-04-06 04:47:34 +00001496 static bool classof(const DeclContext *D) { return true; }
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +00001497
Argyrios Kyrtzidis0ee7d942012-02-21 05:04:44 +00001498 LLVM_ATTRIBUTE_USED void dumpDeclContext() const;
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +00001499
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +00001500private:
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001501 void LoadLexicalDeclsFromExternalStorage() const;
David Blaikieba243b52011-11-09 06:07:30 +00001502
1503 /// @brief Makes a declaration visible within this context, but
Sean Callanan381509e2011-10-21 16:15:18 +00001504 /// suppresses searches for external declarations with the same
1505 /// name.
1506 ///
1507 /// Analogous to makeDeclVisibleInContext, but for the exclusive
1508 /// use of addDeclInternal().
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001509 void makeDeclVisibleInContextInternal(NamedDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001510
John McCall0c01d182010-03-24 05:22:00 +00001511 friend class DependentDiagnostic;
1512 StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const;
1513
Richard Smithc5d3e802012-03-16 06:12:59 +00001514 void buildLookupImpl(DeclContext *DCtx);
1515 void makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1516 bool Rediscoverable);
Sean Callanan9faf8102011-10-21 02:57:43 +00001517 void makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001518};
1519
Douglas Gregorf57172b2008-12-08 18:40:42 +00001520inline bool Decl::isTemplateParameter() const {
Douglas Gregor79c22782010-01-16 20:21:20 +00001521 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm ||
1522 getKind() == TemplateTemplateParm;
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001523}
1524
John McCall80cd64a2010-01-29 01:45:37 +00001525// Specialization selected when ToTy is not a known subclass of DeclContext.
1526template <class ToTy,
1527 bool IsKnownSubtype = ::llvm::is_base_of< DeclContext, ToTy>::value>
1528struct cast_convert_decl_context {
1529 static const ToTy *doit(const DeclContext *Val) {
1530 return static_cast<const ToTy*>(Decl::castFromDeclContext(Val));
1531 }
1532
1533 static ToTy *doit(DeclContext *Val) {
1534 return static_cast<ToTy*>(Decl::castFromDeclContext(Val));
1535 }
1536};
1537
1538// Specialization selected when ToTy is a known subclass of DeclContext.
1539template <class ToTy>
1540struct cast_convert_decl_context<ToTy, true> {
1541 static const ToTy *doit(const DeclContext *Val) {
1542 return static_cast<const ToTy*>(Val);
1543 }
1544
1545 static ToTy *doit(DeclContext *Val) {
1546 return static_cast<ToTy*>(Val);
1547 }
1548};
1549
1550
Chris Lattner0ed844b2008-04-04 06:12:32 +00001551} // end clang.
1552
1553namespace llvm {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001554
John McCall80cd64a2010-01-29 01:45:37 +00001555/// isa<T>(DeclContext*)
Eli Friedman4d509342011-05-21 19:15:39 +00001556template <typename To>
1557struct isa_impl<To, ::clang::DeclContext> {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001558 static bool doit(const ::clang::DeclContext &Val) {
Eli Friedman4d509342011-05-21 19:15:39 +00001559 return To::classofKind(Val.getDeclKind());
Chris Lattner0ed844b2008-04-04 06:12:32 +00001560 }
1561};
Chris Lattner0ed844b2008-04-04 06:12:32 +00001562
John McCall80cd64a2010-01-29 01:45:37 +00001563/// cast<T>(DeclContext*)
1564template<class ToTy>
1565struct cast_convert_val<ToTy,
1566 const ::clang::DeclContext,const ::clang::DeclContext> {
1567 static const ToTy &doit(const ::clang::DeclContext &Val) {
1568 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1569 }
1570};
1571template<class ToTy>
1572struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> {
1573 static ToTy &doit(::clang::DeclContext &Val) {
1574 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1575 }
1576};
1577template<class ToTy>
1578struct cast_convert_val<ToTy,
1579 const ::clang::DeclContext*, const ::clang::DeclContext*> {
1580 static const ToTy *doit(const ::clang::DeclContext *Val) {
1581 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1582 }
1583};
1584template<class ToTy>
1585struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> {
1586 static ToTy *doit(::clang::DeclContext *Val) {
1587 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1588 }
1589};
1590
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001591/// Implement cast_convert_val for Decl -> DeclContext conversions.
Chris Lattner0ed844b2008-04-04 06:12:32 +00001592template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001593struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
Chris Lattnerb048c982008-04-06 04:47:34 +00001594 static ::clang::DeclContext &doit(const FromTy &Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001595 return *FromTy::castToDeclContext(&Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001596 }
1597};
Chris Lattner0ed844b2008-04-04 06:12:32 +00001598
1599template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001600struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
Chris Lattnerb048c982008-04-06 04:47:34 +00001601 static ::clang::DeclContext *doit(const FromTy *Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001602 return FromTy::castToDeclContext(Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001603 }
1604};
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001605
Douglas Gregor44b43212008-12-11 16:49:14 +00001606template<class FromTy>
1607struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
1608 static const ::clang::DeclContext &doit(const FromTy &Val) {
1609 return *FromTy::castToDeclContext(&Val);
1610 }
1611};
1612
1613template<class FromTy>
1614struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
1615 static const ::clang::DeclContext *doit(const FromTy *Val) {
1616 return FromTy::castToDeclContext(Val);
1617 }
1618};
1619
Chris Lattner0ed844b2008-04-04 06:12:32 +00001620} // end namespace llvm
1621
1622#endif