blob: 7865566bec7c31942be9b7c136d36def71c6e303 [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"
Chris Lattner0ed844b2008-04-04 06:12:32 +000019#include "clang/AST/Type.h"
John McCallad2b8042010-01-18 23:21:37 +000020#include "clang/Basic/Specifiers.h"
Chris Lattneree219fd2009-03-29 06:06:59 +000021#include "llvm/ADT/PointerUnion.h"
Daniel Dunbaraa49a7d2012-03-09 19:35:29 +000022#include "llvm/Support/Compiler.h"
23#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner0ed844b2008-04-04 06:12:32 +000024
25namespace clang {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +000026class DeclContext;
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000027class TranslationUnitDecl;
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000028class NamespaceDecl;
Douglas Gregor2a3009a2009-02-03 19:21:40 +000029class UsingDirectiveDecl;
Douglas Gregor44b43212008-12-11 16:49:14 +000030class NamedDecl;
Chris Lattner0ed844b2008-04-04 06:12:32 +000031class FunctionDecl;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000032class CXXRecordDecl;
Chris Lattnerb048c982008-04-06 04:47:34 +000033class EnumDecl;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000034class ObjCMethodDecl;
Douglas Gregor64650af2009-02-02 23:39:07 +000035class ObjCContainerDecl;
Chris Lattner0ed844b2008-04-04 06:12:32 +000036class ObjCInterfaceDecl;
Steve Naroff0701bbb2009-01-08 17:28:14 +000037class ObjCCategoryDecl;
38class ObjCProtocolDecl;
39class ObjCImplementationDecl;
40class ObjCCategoryImplDecl;
Argyrios Kyrtzidisbfb498d2009-07-17 01:19:49 +000041class ObjCImplDecl;
Douglas Gregor074149e2009-01-05 19:45:36 +000042class LinkageSpecDecl;
Steve Naroff56ee6892008-10-08 17:01:13 +000043class BlockDecl;
Douglas Gregor44b43212008-12-11 16:49:14 +000044class DeclarationName;
Ted Kremenekeaab2062009-03-12 18:33:24 +000045class CompoundStmt;
John McCall0c01d182010-03-24 05:22:00 +000046class StoredDeclsMap;
47class DependentDiagnostic;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000048class ASTMutationListener;
Chris Lattner0eda3b32009-03-29 04:32:54 +000049}
50
51namespace llvm {
52// DeclContext* is only 4-byte aligned on 32-bit systems.
53template<>
54 class PointerLikeTypeTraits<clang::DeclContext*> {
55 typedef clang::DeclContext* PT;
56public:
57 static inline void *getAsVoidPointer(PT P) { return P; }
58 static inline PT getFromVoidPointer(void *P) {
59 return static_cast<PT>(P);
60 }
61 enum { NumLowBitsAvailable = 2 };
62};
63}
64
65namespace clang {
Chris Lattner0ed844b2008-04-04 06:12:32 +000066
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000067 /// \brief Captures the result of checking the availability of a
68 /// declaration.
69 enum AvailabilityResult {
70 AR_Available = 0,
71 AR_NotYetIntroduced,
72 AR_Deprecated,
73 AR_Unavailable
74 };
75
Mike Stump1eb44332009-09-09 15:08:12 +000076/// Decl - This represents one declaration (or definition), e.g. a variable,
77/// typedef, function, struct, etc.
Chris Lattner0ed844b2008-04-04 06:12:32 +000078///
79class Decl {
80public:
Douglas Gregor64650af2009-02-02 23:39:07 +000081 /// \brief Lists the kind of concrete classes of Decl.
Chris Lattner0ed844b2008-04-04 06:12:32 +000082 enum Kind {
Sean Hunt9a555912010-05-30 07:21:58 +000083#define DECL(DERIVED, BASE) DERIVED,
84#define ABSTRACT_DECL(DECL)
85#define DECL_RANGE(BASE, START, END) \
86 first##BASE = START, last##BASE = END,
87#define LAST_DECL_RANGE(BASE, START, END) \
88 first##BASE = START, last##BASE = END
89#include "clang/AST/DeclNodes.inc"
Chris Lattner0ed844b2008-04-04 06:12:32 +000090 };
91
Chris Lattner6ad9ac02010-05-07 21:43:38 +000092 /// \brief A placeholder type used to construct an empty shell of a
93 /// decl-derived type that will be filled in later (e.g., by some
94 /// deserialization method).
95 struct EmptyShell { };
96
John McCall0d6b1642010-04-23 18:46:30 +000097 /// IdentifierNamespace - The different namespaces in which
98 /// declarations may appear. According to C99 6.2.3, there are
99 /// four namespaces, labels, tags, members and ordinary
100 /// identifiers. C++ describes lookup completely differently:
101 /// certain lookups merely "ignore" certain kinds of declarations,
102 /// usually based on whether the declaration is of a type, etc.
David Blaikieba243b52011-11-09 06:07:30 +0000103 ///
John McCall0d6b1642010-04-23 18:46:30 +0000104 /// These are meant as bitmasks, so that searches in
105 /// C++ can look into the "tag" namespace during ordinary lookup.
106 ///
Sebastian Redl07a353c2010-07-14 20:26:45 +0000107 /// Decl currently provides 15 bits of IDNS bits.
Chris Lattner0ed844b2008-04-04 06:12:32 +0000108 enum IdentifierNamespace {
John McCall0d6b1642010-04-23 18:46:30 +0000109 /// Labels, declared with 'x:' and referenced with 'goto x'.
110 IDNS_Label = 0x0001,
111
112 /// Tags, declared with 'struct foo;' and referenced with
113 /// 'struct foo'. All tags are also types. This is what
114 /// elaborated-type-specifiers look for in C.
115 IDNS_Tag = 0x0002,
116
117 /// Types, declared with 'struct foo', typedefs, etc.
118 /// This is what elaborated-type-specifiers look for in C++,
119 /// but note that it's ill-formed to find a non-tag.
120 IDNS_Type = 0x0004,
121
122 /// Members, declared with object declarations within tag
123 /// definitions. In C, these can only be found by "qualified"
124 /// lookup in member expressions. In C++, they're found by
125 /// normal lookup.
126 IDNS_Member = 0x0008,
127
128 /// Namespaces, declared with 'namespace foo {}'.
129 /// Lookup for nested-name-specifiers find these.
130 IDNS_Namespace = 0x0010,
131
132 /// Ordinary names. In C, everything that's not a label, tag,
133 /// or member ends up here.
134 IDNS_Ordinary = 0x0020,
135
136 /// Objective C @protocol.
137 IDNS_ObjCProtocol = 0x0040,
138
139 /// This declaration is a friend function. A friend function
140 /// declaration is always in this namespace but may also be in
141 /// IDNS_Ordinary if it was previously declared.
142 IDNS_OrdinaryFriend = 0x0080,
143
144 /// This declaration is a friend class. A friend class
145 /// declaration is always in this namespace but may also be in
146 /// IDNS_Tag|IDNS_Type if it was previously declared.
147 IDNS_TagFriend = 0x0100,
148
149 /// This declaration is a using declaration. A using declaration
150 /// *introduces* a number of other declarations into the current
151 /// scope, and those declarations use the IDNS of their targets,
152 /// but the actual using declarations go in this namespace.
John McCall76d32642010-04-24 01:30:58 +0000153 IDNS_Using = 0x0200,
154
155 /// This declaration is a C++ operator declared in a non-class
156 /// context. All such operators are also in IDNS_Ordinary.
157 /// C++ lexical operator lookup looks for these.
158 IDNS_NonMemberOperator = 0x0400
Chris Lattner0ed844b2008-04-04 06:12:32 +0000159 };
Mike Stump1eb44332009-09-09 15:08:12 +0000160
John McCall09e2c522011-05-01 03:04:29 +0000161 /// ObjCDeclQualifier - 'Qualifiers' written next to the return and
162 /// parameter types in method declarations. Other than remembering
163 /// them and mangling them into the method's signature string, these
164 /// are ignored by the compiler; they are consumed by certain
165 /// remote-messaging frameworks.
166 ///
167 /// in, inout, and out are mutually exclusive and apply only to
168 /// method parameters. bycopy and byref are mutually exclusive and
169 /// apply only to method parameters (?). oneway applies only to
170 /// results. All of these expect their corresponding parameter to
171 /// have a particular type. None of this is currently enforced by
172 /// clang.
173 ///
174 /// This should be kept in sync with ObjCDeclSpec::ObjCDeclQualifier.
Chris Lattner0ed844b2008-04-04 06:12:32 +0000175 enum ObjCDeclQualifier {
176 OBJC_TQ_None = 0x0,
177 OBJC_TQ_In = 0x1,
178 OBJC_TQ_Inout = 0x2,
179 OBJC_TQ_Out = 0x4,
180 OBJC_TQ_Bycopy = 0x8,
181 OBJC_TQ_Byref = 0x10,
182 OBJC_TQ_Oneway = 0x20
183 };
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Douglas Gregor46cd2182012-01-06 16:59:53 +0000185protected:
186 // Enumeration values used in the bits stored in NextInContextAndBits.
187 enum {
188 /// \brief Whether this declaration is a top-level declaration (function,
189 /// global variable, etc.) that is lexically inside an objc container
190 /// definition.
191 TopLevelDeclInObjCContainerFlag = 0x01,
192
193 /// \brief Whether this declaration is private to the module in which it was
194 /// defined.
195 ModulePrivateFlag = 0x02
196 };
197
198 /// \brief The next declaration within the same lexical
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000199 /// DeclContext. These pointers form the linked list that is
200 /// traversed via DeclContext's decls_begin()/decls_end().
Douglas Gregor46cd2182012-01-06 16:59:53 +0000201 ///
202 /// The extra two bits are used for the TopLevelDeclInObjCContainer and
203 /// ModulePrivate bits.
204 llvm::PointerIntPair<Decl *, 2, unsigned> NextInContextAndBits;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000205
Douglas Gregor46cd2182012-01-06 16:59:53 +0000206private:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000207 friend class DeclContext;
208
Chris Lattneree219fd2009-03-29 06:06:59 +0000209 struct MultipleDC {
210 DeclContext *SemanticDC;
211 DeclContext *LexicalDC;
212 };
Mike Stump1eb44332009-09-09 15:08:12 +0000213
214
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000215 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
216 /// For declarations that don't contain C++ scope specifiers, it contains
217 /// the DeclContext where the Decl was declared.
218 /// For declarations with C++ scope specifiers, it contains a MultipleDC*
219 /// with the context where it semantically belongs (SemanticDC) and the
220 /// context where it was lexically declared (LexicalDC).
221 /// e.g.:
222 ///
223 /// namespace A {
224 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
225 /// }
226 /// void A::f(); // SemanticDC == namespace 'A'
227 /// // LexicalDC == global namespace
Chris Lattneree219fd2009-03-29 06:06:59 +0000228 llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000229
Chris Lattneree219fd2009-03-29 06:06:59 +0000230 inline bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); }
231 inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000232 inline MultipleDC *getMultipleDC() const {
Chris Lattneree219fd2009-03-29 06:06:59 +0000233 return DeclCtx.get<MultipleDC*>();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000234 }
Chris Lattner10d83792009-03-27 18:46:15 +0000235 inline DeclContext *getSemanticDC() const {
Chris Lattneree219fd2009-03-29 06:06:59 +0000236 return DeclCtx.get<DeclContext*>();
Chris Lattner10d83792009-03-27 18:46:15 +0000237 }
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Douglas Gregor381d34e2010-12-06 18:36:25 +0000239 /// Loc - The location of this decl.
Daniel Dunbar39d76502009-03-04 02:26:41 +0000240 SourceLocation Loc;
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Chris Lattner0ed844b2008-04-04 06:12:32 +0000242 /// DeclKind - This indicates which class this is.
John McCall35043e52010-10-19 05:43:52 +0000243 unsigned DeclKind : 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Chris Lattner0ed844b2008-04-04 06:12:32 +0000245 /// InvalidDecl - This indicates a semantic error occurred.
John McCall35043e52010-10-19 05:43:52 +0000246 unsigned InvalidDecl : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Chris Lattner0ed844b2008-04-04 06:12:32 +0000248 /// HasAttrs - This indicates whether the decl has attributes or not.
John McCall35043e52010-10-19 05:43:52 +0000249 unsigned HasAttrs : 1;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000250
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000251 /// Implicit - Whether this declaration was implicitly generated by
252 /// the implementation rather than explicitly written by the user.
John McCall35043e52010-10-19 05:43:52 +0000253 unsigned Implicit : 1;
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000254
Douglas Gregore0762c92009-06-19 23:52:42 +0000255 /// \brief Whether this declaration was "used", meaning that a definition is
256 /// required.
John McCall35043e52010-10-19 05:43:52 +0000257 unsigned Used : 1;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000258
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000259 /// \brief Whether this declaration was "referenced".
260 /// The difference with 'Used' is whether the reference appears in a
261 /// evaluated context or not, e.g. functions used in uninstantiated templates
262 /// are regarded as "referenced" but not "used".
263 unsigned Referenced : 1;
David Blaikieba243b52011-11-09 06:07:30 +0000264
Daniel Dunbar02892a62012-03-05 21:42:49 +0000265 /// \brief Whether statistic collection is enabled.
266 static bool StatisticsEnabled;
267
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000268protected:
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000269 /// Access - Used by C++ decls for the access specifier.
270 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
271 unsigned Access : 2;
272 friend class CXXClassMemberWrapper;
Sebastian Redl0b17c612010-08-13 00:28:03 +0000273
Douglas Gregor08e0bc12011-09-10 00:09:20 +0000274 /// \brief Whether this declaration was loaded from an AST file.
275 unsigned FromASTFile : 1;
David Blaikieba243b52011-11-09 06:07:30 +0000276
Douglas Gregorf143ffc2012-01-06 16:22:39 +0000277 /// \brief Whether this declaration is hidden from normal name lookup, e.g.,
278 /// because it is was loaded from an AST file is either module-private or
279 /// because its submodule has not been made visible.
280 unsigned Hidden : 1;
281
Chris Lattner769dbdf2009-03-27 20:18:19 +0000282 /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in.
Douglas Gregor381d34e2010-12-06 18:36:25 +0000283 unsigned IdentifierNamespace : 12;
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Douglas Gregor381d34e2010-12-06 18:36:25 +0000285 /// \brief Whether the \c CachedLinkage field is active.
286 ///
287 /// This field is only valid for NamedDecls subclasses.
288 mutable unsigned HasCachedLinkage : 1;
David Blaikieba243b52011-11-09 06:07:30 +0000289
Douglas Gregor381d34e2010-12-06 18:36:25 +0000290 /// \brief If \c HasCachedLinkage, the linkage of this declaration.
291 ///
292 /// This field is only valid for NamedDecls subclasses.
293 mutable unsigned CachedLinkage : 2;
David Blaikieba243b52011-11-09 06:07:30 +0000294
Douglas Gregor8d267c52011-09-09 02:06:17 +0000295 friend class ASTDeclWriter;
296 friend class ASTDeclReader;
Douglas Gregorecc2c092011-12-01 22:20:10 +0000297 friend class ASTReader;
Douglas Gregor8d267c52011-09-09 02:06:17 +0000298
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000299private:
Anders Carlsson1329c272009-03-25 23:38:06 +0000300 void CheckAccessDeclContext() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000302protected:
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000303
Mike Stump1eb44332009-09-09 15:08:12 +0000304 Decl(Kind DK, DeclContext *DC, SourceLocation L)
Douglas Gregor46cd2182012-01-06 16:59:53 +0000305 : NextInContextAndBits(), DeclCtx(DC),
Daniel Dunbar39d76502009-03-04 02:26:41 +0000306 Loc(L), DeclKind(DK), InvalidDecl(0),
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000307 HasAttrs(false), Implicit(false), Used(false), Referenced(false),
Douglas Gregor46cd2182012-01-06 16:59:53 +0000308 Access(AS_none), FromASTFile(0), Hidden(0),
Douglas Gregor381d34e2010-12-06 18:36:25 +0000309 IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
David Blaikieba243b52011-11-09 06:07:30 +0000310 HasCachedLinkage(0)
Douglas Gregor381d34e2010-12-06 18:36:25 +0000311 {
Daniel Dunbar02892a62012-03-05 21:42:49 +0000312 if (StatisticsEnabled) add(DK);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000313 }
Sam Bishop1bb19632008-04-11 18:04:39 +0000314
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +0000315 Decl(Kind DK, EmptyShell Empty)
Douglas Gregor46cd2182012-01-06 16:59:53 +0000316 : NextInContextAndBits(), DeclKind(DK), InvalidDecl(0),
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000317 HasAttrs(false), Implicit(false), Used(false), Referenced(false),
Douglas Gregor46cd2182012-01-06 16:59:53 +0000318 Access(AS_none), FromASTFile(0), Hidden(0),
Douglas Gregor381d34e2010-12-06 18:36:25 +0000319 IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
320 HasCachedLinkage(0)
321 {
Daniel Dunbar02892a62012-03-05 21:42:49 +0000322 if (StatisticsEnabled) add(DK);
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +0000323 }
324
Douglas Gregorda2142f2011-02-19 18:51:44 +0000325 virtual ~Decl();
326
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000327 /// \brief Allocate memory for a deserialized declaration.
328 ///
329 /// This routine must be used to allocate memory for any declaration that is
330 /// deserialized from a module file.
331 ///
332 /// \param Context The context in which we will allocate memory.
333 /// \param ID The global ID of the deserialized declaration.
334 /// \param Size The size of the allocated object.
335 static void *AllocateDeserializedDecl(const ASTContext &Context,
336 unsigned ID,
337 unsigned Size);
338
Sam Bishop1bb19632008-04-11 18:04:39 +0000339public:
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000340
341 /// \brief Source range that this declaration covers.
Daniel Dunbaraa49a7d2012-03-09 19:35:29 +0000342 virtual SourceRange getSourceRange() const LLVM_READONLY {
Douglas Gregorda2142f2011-02-19 18:51:44 +0000343 return SourceRange(getLocation(), getLocation());
344 }
Daniel Dunbaraa49a7d2012-03-09 19:35:29 +0000345 SourceLocation getLocStart() const LLVM_READONLY {
346 return getSourceRange().getBegin();
347 }
348 SourceLocation getLocEnd() const LLVM_READONLY {
349 return getSourceRange().getEnd();
350 }
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000351
Chris Lattner0ed844b2008-04-04 06:12:32 +0000352 SourceLocation getLocation() const { return Loc; }
353 void setLocation(SourceLocation L) { Loc = L; }
354
John McCall35043e52010-10-19 05:43:52 +0000355 Kind getKind() const { return static_cast<Kind>(DeclKind); }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000356 const char *getDeclKindName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Douglas Gregor46cd2182012-01-06 16:59:53 +0000358 Decl *getNextDeclInContext() { return NextInContextAndBits.getPointer(); }
359 const Decl *getNextDeclInContext() const {return NextInContextAndBits.getPointer();}
Chris Lattner96f44682009-03-28 05:59:45 +0000360
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000361 DeclContext *getDeclContext() {
Chris Lattner10d83792009-03-27 18:46:15 +0000362 if (isInSemaDC())
363 return getSemanticDC();
364 return getMultipleDC()->SemanticDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000365 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000366 const DeclContext *getDeclContext() const {
367 return const_cast<Decl*>(this)->getDeclContext();
368 }
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000369
John McCallaab9e312011-02-22 22:25:23 +0000370 /// Finds the innermost non-closure context of this declaration.
371 /// That is, walk out the DeclContext chain, skipping any blocks.
372 DeclContext *getNonClosureContext();
373 const DeclContext *getNonClosureContext() const {
374 return const_cast<Decl*>(this)->getNonClosureContext();
375 }
376
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000377 TranslationUnitDecl *getTranslationUnitDecl();
378 const TranslationUnitDecl *getTranslationUnitDecl() const {
379 return const_cast<Decl*>(this)->getTranslationUnitDecl();
380 }
381
John McCall9aeed322009-10-01 00:25:31 +0000382 bool isInAnonymousNamespace() const;
383
Daniel Dunbaraa49a7d2012-03-09 19:35:29 +0000384 ASTContext &getASTContext() const LLVM_READONLY;
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Anders Carlsson1329c272009-03-25 23:38:06 +0000386 void setAccess(AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000387 Access = AS;
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000388#ifndef NDEBUG
Anders Carlsson1329c272009-03-25 23:38:06 +0000389 CheckAccessDeclContext();
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000390#endif
Anders Carlssonb8547e82009-03-25 20:19:57 +0000391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
393 AccessSpecifier getAccess() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000394#ifndef NDEBUG
Anders Carlsson1329c272009-03-25 23:38:06 +0000395 CheckAccessDeclContext();
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000396#endif
Mike Stump1eb44332009-09-09 15:08:12 +0000397 return AccessSpecifier(Access);
Anders Carlsson1329c272009-03-25 23:38:06 +0000398 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000399
Chris Lattner76a642f2009-02-15 22:43:40 +0000400 bool hasAttrs() const { return HasAttrs; }
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000401 void setAttrs(const AttrVec& Attrs) {
402 return setAttrsImpl(Attrs, getASTContext());
403 }
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000404 AttrVec &getAttrs() {
Sean Huntcf807c42010-08-18 23:23:40 +0000405 return const_cast<AttrVec&>(const_cast<const Decl*>(this)->getAttrs());
Chris Lattner81abbdd2009-03-21 06:27:31 +0000406 }
Sean Huntcf807c42010-08-18 23:23:40 +0000407 const AttrVec &getAttrs() const;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000408 void swapAttrs(Decl *D);
Sean Huntcf807c42010-08-18 23:23:40 +0000409 void dropAttrs();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000410
Douglas Gregorb5f35ba2010-12-06 17:49:01 +0000411 void addAttr(Attr *A) {
412 if (hasAttrs())
413 getAttrs().push_back(A);
414 else
415 setAttrs(AttrVec(1, A));
416 }
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Sean Huntcf807c42010-08-18 23:23:40 +0000418 typedef AttrVec::const_iterator attr_iterator;
419
420 // FIXME: Do not rely on iterators having comparable singular values.
421 // Note that this should error out if they do not.
422 attr_iterator attr_begin() const {
423 return hasAttrs() ? getAttrs().begin() : 0;
424 }
425 attr_iterator attr_end() const {
426 return hasAttrs() ? getAttrs().end() : 0;
427 }
David Blaikieba243b52011-11-09 06:07:30 +0000428
Fariborz Jahanianc3ca14d2011-06-23 17:50:10 +0000429 template <typename T>
Fariborz Jahanian8cf0f522011-06-23 20:24:38 +0000430 void dropAttr() {
431 if (!HasAttrs) return;
David Blaikieba243b52011-11-09 06:07:30 +0000432
Benjamin Kramer10be5ba2012-10-14 11:50:50 +0000433 AttrVec &Vec = getAttrs();
434 Vec.erase(std::remove_if(Vec.begin(), Vec.end(), isa<T, Attr*>), Vec.end());
435
436 if (Vec.empty())
Fariborz Jahanian8cf0f522011-06-23 20:24:38 +0000437 HasAttrs = false;
438 }
David Blaikieba243b52011-11-09 06:07:30 +0000439
Sean Huntcf807c42010-08-18 23:23:40 +0000440 template <typename T>
441 specific_attr_iterator<T> specific_attr_begin() const {
442 return specific_attr_iterator<T>(attr_begin());
443 }
444 template <typename T>
445 specific_attr_iterator<T> specific_attr_end() const {
446 return specific_attr_iterator<T>(attr_end());
447 }
448
449 template<typename T> T *getAttr() const {
450 return hasAttrs() ? getSpecificAttr<T>(getAttrs()) : 0;
451 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000452 template<typename T> bool hasAttr() const {
Sean Huntcf807c42010-08-18 23:23:40 +0000453 return hasAttrs() && hasSpecificAttr<T>(getAttrs());
454 }
455
456 /// getMaxAlignment - return the maximum alignment specified by attributes
457 /// on this decl, 0 if there are none.
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +0000458 unsigned getMaxAlignment() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Chris Lattner0ed844b2008-04-04 06:12:32 +0000460 /// setInvalidDecl - Indicates the Decl had a semantic error. This
461 /// allows for graceful error recovery.
Douglas Gregor42738572010-03-05 00:26:45 +0000462 void setInvalidDecl(bool Invalid = true);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000463 bool isInvalidDecl() const { return (bool) InvalidDecl; }
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000464
465 /// isImplicit - Indicates whether the declaration was implicitly
466 /// generated by the implementation. If false, this declaration
467 /// was written explicitly in the source code.
468 bool isImplicit() const { return Implicit; }
469 void setImplicit(bool I = true) { Implicit = I; }
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Douglas Gregore0762c92009-06-19 23:52:42 +0000471 /// \brief Whether this declaration was used, meaning that a definition
472 /// is required.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000473 ///
474 /// \param CheckUsedAttr When true, also consider the "used" attribute
475 /// (in addition to the "used" bit set by \c setUsed()) when determining
476 /// whether the function is used.
477 bool isUsed(bool CheckUsedAttr = true) const;
Sean Huntcf807c42010-08-18 23:23:40 +0000478
Douglas Gregore0762c92009-06-19 23:52:42 +0000479 void setUsed(bool U = true) { Used = U; }
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000481 /// \brief Whether this declaration was referenced.
482 bool isReferenced() const;
483
484 void setReferenced(bool R = true) { Referenced = R; }
485
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +0000486 /// \brief Whether this declaration is a top-level declaration (function,
487 /// global variable, etc.) that is lexically inside an objc container
488 /// definition.
489 bool isTopLevelDeclInObjCContainer() const {
Douglas Gregor46cd2182012-01-06 16:59:53 +0000490 return NextInContextAndBits.getInt() & TopLevelDeclInObjCContainerFlag;
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +0000491 }
492
493 void setTopLevelDeclInObjCContainer(bool V = true) {
Douglas Gregor46cd2182012-01-06 16:59:53 +0000494 unsigned Bits = NextInContextAndBits.getInt();
495 if (V)
496 Bits |= TopLevelDeclInObjCContainerFlag;
497 else
498 Bits &= ~TopLevelDeclInObjCContainerFlag;
499 NextInContextAndBits.setInt(Bits);
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +0000500 }
501
Douglas Gregor46cd2182012-01-06 16:59:53 +0000502protected:
503 /// \brief Whether this declaration was marked as being private to the
504 /// module in which it was defined.
505 bool isModulePrivate() const {
506 return NextInContextAndBits.getInt() & ModulePrivateFlag;
507 }
508
509 /// \brief Specify whether this declaration was marked as being private
510 /// to the module in which it was defined.
511 void setModulePrivate(bool MP = true) {
512 unsigned Bits = NextInContextAndBits.getInt();
513 if (MP)
514 Bits |= ModulePrivateFlag;
515 else
516 Bits &= ~ModulePrivateFlag;
517 NextInContextAndBits.setInt(Bits);
518 }
519
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +0000520 /// \brief Set the owning module ID.
521 void setOwningModuleID(unsigned ID) {
522 assert(isFromASTFile() && "Only works on a deserialized declaration");
523 *((unsigned*)this - 2) = ID;
524 }
525
Douglas Gregor46cd2182012-01-06 16:59:53 +0000526public:
527
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000528 /// \brief Determine the availability of the given declaration.
529 ///
530 /// This routine will determine the most restrictive availability of
531 /// the given declaration (e.g., preferring 'unavailable' to
532 /// 'deprecated').
533 ///
534 /// \param Message If non-NULL and the result is not \c
535 /// AR_Available, will be set to a (possibly empty) message
536 /// describing why the declaration has not been introduced, is
537 /// deprecated, or is unavailable.
538 AvailabilityResult getAvailability(std::string *Message = 0) const;
539
540 /// \brief Determine whether this declaration is marked 'deprecated'.
541 ///
542 /// \param Message If non-NULL and the declaration is deprecated,
543 /// this will be set to the message describing why the declaration
544 /// was deprecated (which may be empty).
545 bool isDeprecated(std::string *Message = 0) const {
546 return getAvailability(Message) == AR_Deprecated;
547 }
548
549 /// \brief Determine whether this declaration is marked 'unavailable'.
550 ///
551 /// \param Message If non-NULL and the declaration is unavailable,
552 /// this will be set to the message describing why the declaration
553 /// was made unavailable (which may be empty).
554 bool isUnavailable(std::string *Message = 0) const {
555 return getAvailability(Message) == AR_Unavailable;
556 }
557
558 /// \brief Determine whether this is a weak-imported symbol.
559 ///
560 /// Weak-imported symbols are typically marked with the
Eli Friedman7f3ad232011-03-28 02:00:21 +0000561 /// 'weak_import' attribute, but may also be marked with an
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000562 /// 'availability' attribute where we're targing a platform prior to
563 /// the introduction of this feature.
564 bool isWeakImported() const;
565
566 /// \brief Determines whether this symbol can be weak-imported,
567 /// e.g., whether it would be well-formed to add the weak_import
568 /// attribute.
569 ///
570 /// \param IsDefinition Set to \c true to indicate that this
571 /// declaration cannot be weak-imported because it has a definition.
572 bool canBeWeakImported(bool &IsDefinition) const;
573
Douglas Gregor919814d2011-09-09 23:01:35 +0000574 /// \brief Determine whether this declaration came from an AST file (such as
575 /// a precompiled header or module) rather than having been parsed.
Douglas Gregor08e0bc12011-09-10 00:09:20 +0000576 bool isFromASTFile() const { return FromASTFile; }
David Blaikieba243b52011-11-09 06:07:30 +0000577
Douglas Gregorb6b60c12012-01-05 22:27:05 +0000578 /// \brief Retrieve the global declaration ID associated with this
579 /// declaration, which specifies where in the
580 unsigned getGlobalID() const {
581 if (isFromASTFile())
582 return *((const unsigned*)this - 1);
583 return 0;
584 }
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +0000585
586 /// \brief Retrieve the global ID of the module that owns this particular
587 /// declaration.
588 unsigned getOwningModuleID() const {
589 if (isFromASTFile())
590 return *((const unsigned*)this - 2);
591
592 return 0;
593 }
594
Douglas Gregorcc636682009-02-17 23:15:12 +0000595 unsigned getIdentifierNamespace() const {
Chris Lattner769dbdf2009-03-27 20:18:19 +0000596 return IdentifierNamespace;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000597 }
Chris Lattnerd62fdc42009-01-06 07:16:40 +0000598 bool isInIdentifierNamespace(unsigned NS) const {
599 return getIdentifierNamespace() & NS;
600 }
Chris Lattner769dbdf2009-03-27 20:18:19 +0000601 static unsigned getIdentifierNamespaceForKind(Kind DK);
602
John McCall0d6b1642010-04-23 18:46:30 +0000603 bool hasTagIdentifierNamespace() const {
604 return isTagIdentifierNamespace(getIdentifierNamespace());
605 }
606 static bool isTagIdentifierNamespace(unsigned NS) {
607 // TagDecls have Tag and Type set and may also have TagFriend.
608 return (NS & ~IDNS_TagFriend) == (IDNS_Tag | IDNS_Type);
609 }
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000611 /// getLexicalDeclContext - The declaration context where this Decl was
612 /// lexically declared (LexicalDC). May be different from
613 /// getDeclContext() (SemanticDC).
614 /// e.g.:
615 ///
616 /// namespace A {
617 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
618 /// }
619 /// void A::f(); // SemanticDC == namespace 'A'
620 /// // LexicalDC == global namespace
Chris Lattner10d83792009-03-27 18:46:15 +0000621 DeclContext *getLexicalDeclContext() {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000622 if (isInSemaDC())
Chris Lattner10d83792009-03-27 18:46:15 +0000623 return getSemanticDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000624 return getMultipleDC()->LexicalDC;
625 }
Chris Lattner10d83792009-03-27 18:46:15 +0000626 const DeclContext *getLexicalDeclContext() const {
627 return const_cast<Decl*>(this)->getLexicalDeclContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000628 }
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000629
Douglas Gregorda2142f2011-02-19 18:51:44 +0000630 virtual bool isOutOfLine() const {
631 return getLexicalDeclContext() != getDeclContext();
632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Douglas Gregor6ab35242009-04-09 21:40:53 +0000634 /// setDeclContext - Set both the semantic and lexical DeclContext
635 /// to DC.
636 void setDeclContext(DeclContext *DC);
637
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000638 void setLexicalDeclContext(DeclContext *DC);
639
Sebastian Redl7a126a42010-08-31 00:36:30 +0000640 /// isDefinedOutsideFunctionOrMethod - This predicate returns true if this
641 /// scoped decl is defined outside the current function or method. This is
642 /// roughly global variables and functions, but also handles enums (which
643 /// could be defined inside or outside a function etc).
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000644 bool isDefinedOutsideFunctionOrMethod() const {
645 return getParentFunctionOrMethod() == 0;
646 }
647
648 /// \brief If this decl is defined inside a function/method/block it returns
649 /// the corresponding DeclContext, otherwise it returns null.
650 const DeclContext *getParentFunctionOrMethod() const;
Argyrios Kyrtzidisfcc1e502011-09-28 18:14:24 +0000651 DeclContext *getParentFunctionOrMethod() {
652 return const_cast<DeclContext*>(
653 const_cast<const Decl*>(this)->getParentFunctionOrMethod());
654 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000655
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000656 /// \brief Retrieves the "canonical" declaration of the given declaration.
Douglas Gregorda2142f2011-02-19 18:51:44 +0000657 virtual Decl *getCanonicalDecl() { return this; }
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000658 const Decl *getCanonicalDecl() const {
659 return const_cast<Decl*>(this)->getCanonicalDecl();
660 }
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000661
Argyrios Kyrtzidisac4e3792009-07-18 08:50:48 +0000662 /// \brief Whether this particular Decl is a canonical one.
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000663 bool isCanonicalDecl() const { return getCanonicalDecl() == this; }
Douglas Gregorc896ad02011-12-14 21:44:45 +0000664
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000665protected:
666 /// \brief Returns the next redeclaration or itself if this is the only decl.
667 ///
668 /// Decl subclasses that can be redeclared should override this method so that
669 /// Decl::redecl_iterator can iterate over them.
Douglas Gregorda2142f2011-02-19 18:51:44 +0000670 virtual Decl *getNextRedeclaration() { return this; }
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000671
Douglas Gregoref96ee02012-01-14 16:38:05 +0000672 /// \brief Implementation of getPreviousDecl(), to be overridden by any
673 /// subclass that has a redeclaration chain.
674 virtual Decl *getPreviousDeclImpl() { return 0; }
675
676 /// \brief Implementation of getMostRecentDecl(), to be overridden by any
677 /// subclass that has a redeclaration chain.
678 virtual Decl *getMostRecentDeclImpl() { return this; }
679
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000680public:
681 /// \brief Iterates through all the redeclarations of the same decl.
682 class redecl_iterator {
683 /// Current - The current declaration.
684 Decl *Current;
685 Decl *Starter;
686
687 public:
David Blaikie581deb32012-06-06 20:45:41 +0000688 typedef Decl *value_type;
689 typedef const value_type &reference;
690 typedef const value_type *pointer;
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000691 typedef std::forward_iterator_tag iterator_category;
David Blaikie581deb32012-06-06 20:45:41 +0000692 typedef std::ptrdiff_t difference_type;
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000693
694 redecl_iterator() : Current(0) { }
695 explicit redecl_iterator(Decl *C) : Current(C), Starter(C) { }
696
David Blaikie581deb32012-06-06 20:45:41 +0000697 reference operator*() const { return Current; }
698 value_type operator->() const { return Current; }
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000699
700 redecl_iterator& operator++() {
701 assert(Current && "Advancing while iterator has reached end");
702 // Get either previous decl or latest decl.
703 Decl *Next = Current->getNextRedeclaration();
Argyrios Kyrtzidis22cbd2b2009-07-21 00:06:27 +0000704 assert(Next && "Should return next redeclaration or itself, never null!");
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000705 Current = (Next != Starter ? Next : 0);
706 return *this;
707 }
708
709 redecl_iterator operator++(int) {
710 redecl_iterator tmp(*this);
711 ++(*this);
712 return tmp;
713 }
714
Mike Stump1eb44332009-09-09 15:08:12 +0000715 friend bool operator==(redecl_iterator x, redecl_iterator y) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000716 return x.Current == y.Current;
717 }
Mike Stump1eb44332009-09-09 15:08:12 +0000718 friend bool operator!=(redecl_iterator x, redecl_iterator y) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000719 return x.Current != y.Current;
720 }
721 };
722
723 /// \brief Returns iterator for all the redeclarations of the same decl.
724 /// It will iterate at least once (when this decl is the only one).
725 redecl_iterator redecls_begin() const {
726 return redecl_iterator(const_cast<Decl*>(this));
727 }
728 redecl_iterator redecls_end() const { return redecl_iterator(); }
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000729
Douglas Gregorf785a7d2012-01-14 15:55:47 +0000730 /// \brief Retrieve the previous declaration that declares the same entity
731 /// as this declaration, or NULL if there is no previous declaration.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000732 Decl *getPreviousDecl() { return getPreviousDeclImpl(); }
733
734 /// \brief Retrieve the most recent declaration that declares the same entity
735 /// as this declaration, or NULL if there is no previous declaration.
736 const Decl *getPreviousDecl() const {
737 return const_cast<Decl *>(this)->getPreviousDeclImpl();
738 }
Douglas Gregorf785a7d2012-01-14 15:55:47 +0000739
740 /// \brief Retrieve the most recent declaration that declares the same entity
741 /// as this declaration (which may be this declaration).
Douglas Gregoref96ee02012-01-14 16:38:05 +0000742 Decl *getMostRecentDecl() { return getMostRecentDeclImpl(); }
743
744 /// \brief Retrieve the most recent declaration that declares the same entity
745 /// as this declaration (which may be this declaration).
746 const Decl *getMostRecentDecl() const {
747 return const_cast<Decl *>(this)->getMostRecentDeclImpl();
748 }
749
Sebastian Redld3a413d2009-04-26 20:35:05 +0000750 /// getBody - If this Decl represents a declaration for a body of code,
751 /// such as a function or method definition, this method returns the
752 /// top-level Stmt* of that body. Otherwise this method returns null.
Douglas Gregorda2142f2011-02-19 18:51:44 +0000753 virtual Stmt* getBody() const { return 0; }
Sebastian Redld3a413d2009-04-26 20:35:05 +0000754
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000755 /// \brief Returns true if this Decl represents a declaration for a body of
756 /// code, such as a function or method definition.
Douglas Gregorda2142f2011-02-19 18:51:44 +0000757 virtual bool hasBody() const { return getBody() != 0; }
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000758
Sebastian Redld3a413d2009-04-26 20:35:05 +0000759 /// getBodyRBrace - Gets the right brace of the body, if a body exists.
760 /// This works whether the body is a CompoundStmt or a CXXTryStmt.
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000761 SourceLocation getBodyRBrace() const;
Sebastian Redld3a413d2009-04-26 20:35:05 +0000762
Chris Lattner0ed844b2008-04-04 06:12:32 +0000763 // global temp stats (until we have a per-module visitor)
Sean Hunt9a555912010-05-30 07:21:58 +0000764 static void add(Kind k);
Daniel Dunbar02892a62012-03-05 21:42:49 +0000765 static void EnableStatistics();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000766 static void PrintStats();
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Anders Carlsson67e33202009-06-13 00:08:58 +0000768 /// isTemplateParameter - Determines whether this declaration is a
Douglas Gregorf57172b2008-12-08 18:40:42 +0000769 /// template parameter.
770 bool isTemplateParameter() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Anders Carlsson67e33202009-06-13 00:08:58 +0000772 /// isTemplateParameter - Determines whether this declaration is a
773 /// template parameter pack.
774 bool isTemplateParameterPack() const;
Douglas Gregorf57172b2008-12-08 18:40:42 +0000775
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000776 /// \brief Whether this declaration is a parameter pack.
777 bool isParameterPack() const;
David Blaikieba243b52011-11-09 06:07:30 +0000778
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000779 /// \brief returns true if this declaration is a template
780 bool isTemplateDecl() const;
781
Douglas Gregore53060f2009-06-25 22:08:12 +0000782 /// \brief Whether this declaration is a function or function template.
783 bool isFunctionOrFunctionTemplate() const;
John McCall02cace72009-08-28 07:59:38 +0000784
785 /// \brief Changes the namespace of this declaration to reflect that it's
786 /// the object of a friend declaration.
787 ///
788 /// These declarations appear in the lexical context of the friending
789 /// class, but in the semantic context of the actual entity. This property
790 /// applies only to a specific decl object; other redeclarations of the
791 /// same entity may not (and probably don't) share this property.
792 void setObjectOfFriendDecl(bool PreviouslyDeclared) {
793 unsigned OldNS = IdentifierNamespace;
John McCallb0cb0222010-03-27 05:57:59 +0000794 assert((OldNS & (IDNS_Tag | IDNS_Ordinary |
795 IDNS_TagFriend | IDNS_OrdinaryFriend)) &&
796 "namespace includes neither ordinary nor tag");
John McCall0d6b1642010-04-23 18:46:30 +0000797 assert(!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type |
John McCallb0cb0222010-03-27 05:57:59 +0000798 IDNS_TagFriend | IDNS_OrdinaryFriend)) &&
799 "namespace includes other than ordinary or tag");
John McCall02cace72009-08-28 07:59:38 +0000800
John McCallb0cb0222010-03-27 05:57:59 +0000801 IdentifierNamespace = 0;
802 if (OldNS & (IDNS_Tag | IDNS_TagFriend)) {
John McCall02cace72009-08-28 07:59:38 +0000803 IdentifierNamespace |= IDNS_TagFriend;
John McCall0d6b1642010-04-23 18:46:30 +0000804 if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Tag | IDNS_Type;
John McCallb0cb0222010-03-27 05:57:59 +0000805 }
806
807 if (OldNS & (IDNS_Ordinary | IDNS_OrdinaryFriend)) {
John McCall02cace72009-08-28 07:59:38 +0000808 IdentifierNamespace |= IDNS_OrdinaryFriend;
John McCallb0cb0222010-03-27 05:57:59 +0000809 if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Ordinary;
810 }
John McCall02cace72009-08-28 07:59:38 +0000811 }
812
813 enum FriendObjectKind {
814 FOK_None, // not a friend object
815 FOK_Declared, // a friend of a previously-declared entity
816 FOK_Undeclared // a friend of a previously-undeclared entity
817 };
818
819 /// \brief Determines whether this declaration is the object of a
820 /// friend declaration and, if so, what kind.
821 ///
822 /// There is currently no direct way to find the associated FriendDecl.
823 FriendObjectKind getFriendObjectKind() const {
824 unsigned mask
825 = (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend));
826 if (!mask) return FOK_None;
David Blaikieba243b52011-11-09 06:07:30 +0000827 return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ?
Anders Carlsson255d6412009-09-13 23:59:13 +0000828 FOK_Declared : FOK_Undeclared);
John McCall02cace72009-08-28 07:59:38 +0000829 }
Mike Stump1eb44332009-09-09 15:08:12 +0000830
John McCall76d32642010-04-24 01:30:58 +0000831 /// Specifies that this declaration is a C++ overloaded non-member.
832 void setNonMemberOperator() {
833 assert(getKind() == Function || getKind() == FunctionTemplate);
834 assert((IdentifierNamespace & IDNS_Ordinary) &&
835 "visible non-member operators should be in ordinary namespace");
836 IdentifierNamespace |= IDNS_NonMemberOperator;
837 }
838
John McCall80cd64a2010-01-29 01:45:37 +0000839 static bool classofKind(Kind K) { return true; }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000840 static DeclContext *castToDeclContext(const Decl *);
841 static Decl *castFromDeclContext(const DeclContext *);
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Richard Trieu5cb3d692011-07-28 00:19:05 +0000843 void print(raw_ostream &Out, unsigned Indentation = 0,
844 bool PrintInstantiation = false) const;
Chris Lattner8cc488f2011-07-20 07:06:53 +0000845 void print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu5cb3d692011-07-28 00:19:05 +0000846 unsigned Indentation = 0, bool PrintInstantiation = false) const;
Eli Friedman42f42c02009-05-30 04:20:30 +0000847 static void printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner8cc488f2011-07-20 07:06:53 +0000848 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000849 unsigned Indentation = 0);
Alexander Kornienko559b9282012-07-26 17:11:45 +0000850 // Debuggers don't usually respect default arguments.
Benjamin Kramer42c72c22012-08-14 14:50:32 +0000851 LLVM_ATTRIBUTE_USED void dump() const;
Alexander Kornienko559b9282012-07-26 17:11:45 +0000852 void dump(raw_ostream &Out) const;
853 // Debuggers don't usually respect default arguments.
Benjamin Kramer42c72c22012-08-14 14:50:32 +0000854 LLVM_ATTRIBUTE_USED void dumpXML() const;
Alexander Kornienko559b9282012-07-26 17:11:45 +0000855 void dumpXML(raw_ostream &OS) const;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000856
Chris Lattner81abbdd2009-03-21 06:27:31 +0000857private:
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000858 void setAttrsImpl(const AttrVec& Attrs, ASTContext &Ctx);
859 void setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
860 ASTContext &Ctx);
Chris Lattner81abbdd2009-03-21 06:27:31 +0000861
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000862protected:
863 ASTMutationListener *getASTMutationListener() const;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000864};
865
Douglas Gregoreaa95112011-12-14 23:59:32 +0000866/// \brief Determine whether two declarations declare the same entity.
867inline bool declaresSameEntity(const Decl *D1, const Decl *D2) {
Douglas Gregoreaa95112011-12-14 23:59:32 +0000868 if (!D1 || !D2)
869 return false;
870
Douglas Gregordec1cc42011-12-15 17:15:07 +0000871 if (D1 == D2)
872 return true;
873
Douglas Gregoreaa95112011-12-14 23:59:32 +0000874 return D1->getCanonicalDecl() == D2->getCanonicalDecl();
875}
876
Chris Lattner49f28ca2009-03-05 08:00:35 +0000877/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when
878/// doing something to a specific decl.
879class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
Daniel Dunbarc5236562009-11-21 09:05:59 +0000880 const Decl *TheDecl;
Chris Lattner49f28ca2009-03-05 08:00:35 +0000881 SourceLocation Loc;
882 SourceManager &SM;
883 const char *Message;
884public:
Daniel Dunbarc5236562009-11-21 09:05:59 +0000885 PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L,
Chris Lattner49f28ca2009-03-05 08:00:35 +0000886 SourceManager &sm, const char *Msg)
887 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Chris Lattner8cc488f2011-07-20 07:06:53 +0000889 virtual void print(raw_ostream &OS) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000890};
891
John McCall6d9496d2010-08-26 09:52:08 +0000892class DeclContextLookupResult
893 : public std::pair<NamedDecl**,NamedDecl**> {
John McCallea318642010-08-26 09:15:37 +0000894public:
John McCall6d9496d2010-08-26 09:52:08 +0000895 DeclContextLookupResult(NamedDecl **I, NamedDecl **E)
896 : std::pair<NamedDecl**,NamedDecl**>(I, E) {}
897 DeclContextLookupResult()
898 : std::pair<NamedDecl**,NamedDecl**>() {}
John McCallea318642010-08-26 09:15:37 +0000899
John McCallf29815a2010-08-26 10:20:09 +0000900 using std::pair<NamedDecl**,NamedDecl**>::operator=;
John McCallea318642010-08-26 09:15:37 +0000901};
902
903class DeclContextLookupConstResult
904 : public std::pair<NamedDecl*const*, NamedDecl*const*> {
905public:
906 DeclContextLookupConstResult(std::pair<NamedDecl**,NamedDecl**> R)
John McCall6d9496d2010-08-26 09:52:08 +0000907 : std::pair<NamedDecl*const*, NamedDecl*const*>(R) {}
John McCallea318642010-08-26 09:15:37 +0000908 DeclContextLookupConstResult(NamedDecl * const *I, NamedDecl * const *E)
John McCall6d9496d2010-08-26 09:52:08 +0000909 : std::pair<NamedDecl*const*, NamedDecl*const*>(I, E) {}
910 DeclContextLookupConstResult()
911 : std::pair<NamedDecl*const*, NamedDecl*const*>() {}
John McCallea318642010-08-26 09:15:37 +0000912
John McCallf29815a2010-08-26 10:20:09 +0000913 using std::pair<NamedDecl*const*,NamedDecl*const*>::operator=;
John McCallea318642010-08-26 09:15:37 +0000914};
Chris Lattner49f28ca2009-03-05 08:00:35 +0000915
Chris Lattnerb048c982008-04-06 04:47:34 +0000916/// DeclContext - This is used only as base class of specific decl types that
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000917/// can act as declaration contexts. These decls are (only the top classes
918/// that directly derive from DeclContext are mentioned, not their subclasses):
Chris Lattner0ed844b2008-04-04 06:12:32 +0000919///
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000920/// TranslationUnitDecl
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000921/// NamespaceDecl
Chris Lattner0ed844b2008-04-04 06:12:32 +0000922/// FunctionDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000923/// TagDecl
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000924/// ObjCMethodDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000925/// ObjCContainerDecl
Douglas Gregor074149e2009-01-05 19:45:36 +0000926/// LinkageSpecDecl
Steve Naroff56ee6892008-10-08 17:01:13 +0000927/// BlockDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000928///
Chris Lattnerb048c982008-04-06 04:47:34 +0000929class DeclContext {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000930 /// DeclKind - This indicates which class this is.
John McCall35043e52010-10-19 05:43:52 +0000931 unsigned DeclKind : 8;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000932
Douglas Gregor2cf26342009-04-09 22:27:44 +0000933 /// \brief Whether this declaration context also has some external
934 /// storage that contains additional declarations that are lexically
935 /// part of this context.
John McCall35043e52010-10-19 05:43:52 +0000936 mutable unsigned ExternalLexicalStorage : 1;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000937
938 /// \brief Whether this declaration context also has some external
939 /// storage that contains additional declarations that are visible
940 /// in this context.
John McCall35043e52010-10-19 05:43:52 +0000941 mutable unsigned ExternalVisibleStorage : 1;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000942
Douglas Gregorc36c5402009-04-09 17:29:08 +0000943 /// \brief Pointer to the data structure used to lookup declarations
John McCall0c01d182010-03-24 05:22:00 +0000944 /// within this context (or a DependentStoredDeclsMap if this is a
Richard Smithc5d3e802012-03-16 06:12:59 +0000945 /// dependent context), and a bool indicating whether we have lazily
946 /// omitted any declarations from the map. We maintain the invariant
947 /// that, if the map contains an entry for a DeclarationName, then it
948 /// contains all relevant entries for that name.
949 mutable llvm::PointerIntPair<StoredDeclsMap*, 1, bool> LookupPtr;
Douglas Gregor44b43212008-12-11 16:49:14 +0000950
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000951protected:
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000952 /// FirstDecl - The first declaration stored within this declaration
953 /// context.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000954 mutable Decl *FirstDecl;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000955
956 /// LastDecl - The last declaration stored within this declaration
957 /// context. FIXME: We could probably cache this value somewhere
958 /// outside of the DeclContext, to reduce the size of DeclContext by
959 /// another pointer.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000960 mutable Decl *LastDecl;
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000961
John McCall76bd1f32010-06-01 09:23:16 +0000962 friend class ExternalASTSource;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +0000963 friend class ASTWriter;
John McCall76bd1f32010-06-01 09:23:16 +0000964
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000965 /// \brief Build up a chain of declarations.
966 ///
967 /// \returns the first/last pair of declarations.
968 static std::pair<Decl *, Decl *>
Bill Wendling341785e2012-02-22 09:51:33 +0000969 BuildDeclChain(ArrayRef<Decl*> Decls, bool FieldsAlreadyLoaded);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000970
Mike Stump1eb44332009-09-09 15:08:12 +0000971 DeclContext(Decl::Kind K)
Douglas Gregor2cf26342009-04-09 22:27:44 +0000972 : DeclKind(K), ExternalLexicalStorage(false),
Richard Smithc5d3e802012-03-16 06:12:59 +0000973 ExternalVisibleStorage(false), LookupPtr(0, false), FirstDecl(0),
Douglas Gregor2cf26342009-04-09 22:27:44 +0000974 LastDecl(0) { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000975
Chris Lattner0ed844b2008-04-04 06:12:32 +0000976public:
Douglas Gregor44b43212008-12-11 16:49:14 +0000977 ~DeclContext();
978
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000979 Decl::Kind getDeclKind() const {
John McCall35043e52010-10-19 05:43:52 +0000980 return static_cast<Decl::Kind>(DeclKind);
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000981 }
Steve Naroff0a473932009-01-20 19:53:53 +0000982 const char *getDeclKindName() const;
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +0000983
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +0000984 /// getParent - Returns the containing DeclContext.
Chris Lattner0cf2b192009-03-27 19:19:59 +0000985 DeclContext *getParent() {
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +0000986 return cast<Decl>(this)->getDeclContext();
987 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000988 const DeclContext *getParent() const {
989 return const_cast<DeclContext*>(this)->getParent();
Argyrios Kyrtzidisd2595ec2008-10-12 18:40:01 +0000990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000992 /// getLexicalParent - Returns the containing lexical DeclContext. May be
Douglas Gregor44b43212008-12-11 16:49:14 +0000993 /// different from getParent, e.g.:
994 ///
995 /// namespace A {
996 /// struct S;
997 /// }
998 /// struct A::S {}; // getParent() == namespace 'A'
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000999 /// // getLexicalParent() == translation unit
1000 ///
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +00001001 DeclContext *getLexicalParent() {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001002 return cast<Decl>(this)->getLexicalDeclContext();
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +00001003 }
Chris Lattner0cf2b192009-03-27 19:19:59 +00001004 const DeclContext *getLexicalParent() const {
1005 return const_cast<DeclContext*>(this)->getLexicalParent();
Mike Stump1eb44332009-09-09 15:08:12 +00001006 }
Argyrios Kyrtzidis048f30a2009-06-30 02:35:38 +00001007
Douglas Gregore942bbe2009-09-10 16:57:35 +00001008 DeclContext *getLookupParent();
David Blaikieba243b52011-11-09 06:07:30 +00001009
Douglas Gregore942bbe2009-09-10 16:57:35 +00001010 const DeclContext *getLookupParent() const {
1011 return const_cast<DeclContext*>(this)->getLookupParent();
1012 }
David Blaikieba243b52011-11-09 06:07:30 +00001013
Argyrios Kyrtzidis048f30a2009-06-30 02:35:38 +00001014 ASTContext &getParentASTContext() const {
1015 return cast<Decl>(this)->getASTContext();
1016 }
1017
John McCallaab9e312011-02-22 22:25:23 +00001018 bool isClosure() const {
1019 return DeclKind == Decl::Block;
1020 }
1021
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001022 bool isObjCContainer() const {
Fariborz Jahanian80f77532011-08-22 17:13:51 +00001023 switch (DeclKind) {
1024 case Decl::ObjCCategory:
1025 case Decl::ObjCCategoryImpl:
1026 case Decl::ObjCImplementation:
1027 case Decl::ObjCInterface:
1028 case Decl::ObjCProtocol:
1029 return true;
1030 }
1031 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001032 }
1033
Chris Lattner0ed844b2008-04-04 06:12:32 +00001034 bool isFunctionOrMethod() const {
1035 switch (DeclKind) {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001036 case Decl::Block:
1037 case Decl::ObjCMethod:
1038 return true;
1039 default:
Sean Hunt9a555912010-05-30 07:21:58 +00001040 return DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001041 }
1042 }
1043
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +00001044 bool isFileContext() const {
1045 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
1046 }
1047
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001048 bool isTranslationUnit() const {
1049 return DeclKind == Decl::TranslationUnit;
1050 }
1051
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001052 bool isRecord() const {
Sean Hunt9a555912010-05-30 07:21:58 +00001053 return DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord;
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001054 }
1055
Douglas Gregor44b43212008-12-11 16:49:14 +00001056 bool isNamespace() const {
1057 return DeclKind == Decl::Namespace;
1058 }
1059
Sebastian Redl410c4f22010-08-31 20:53:31 +00001060 bool isInlineNamespace() const;
1061
Douglas Gregorbc221632009-05-28 16:34:51 +00001062 /// \brief Determines whether this context is dependent on a
1063 /// template parameter.
1064 bool isDependentContext() const;
1065
Douglas Gregor074149e2009-01-05 19:45:36 +00001066 /// isTransparentContext - Determines whether this context is a
1067 /// "transparent" context, meaning that the members declared in this
1068 /// context are semantically declared in the nearest enclosing
1069 /// non-transparent (opaque) context but are lexically declared in
1070 /// this context. For example, consider the enumerators of an
Mike Stump1eb44332009-09-09 15:08:12 +00001071 /// enumeration type:
Douglas Gregor074149e2009-01-05 19:45:36 +00001072 /// @code
1073 /// enum E {
Mike Stump1eb44332009-09-09 15:08:12 +00001074 /// Val1
Douglas Gregor074149e2009-01-05 19:45:36 +00001075 /// };
1076 /// @endcode
1077 /// Here, E is a transparent context, so its enumerator (Val1) will
1078 /// appear (semantically) that it is in the same context of E.
1079 /// Examples of transparent contexts include: enumerations (except for
Sebastian Redl410c4f22010-08-31 20:53:31 +00001080 /// C++0x scoped enums), and C++ linkage specifications.
Douglas Gregor074149e2009-01-05 19:45:36 +00001081 bool isTransparentContext() const;
1082
John McCallac65c622010-10-26 04:59:26 +00001083 /// \brief Determines whether this context is, or is nested within,
1084 /// a C++ extern "C" linkage spec.
1085 bool isExternCContext() const;
1086
Douglas Gregor61481da2009-09-01 17:22:34 +00001087 /// \brief Determine whether this declaration context is equivalent
1088 /// to the declaration context DC.
Sebastian Redl7a126a42010-08-31 00:36:30 +00001089 bool Equals(const DeclContext *DC) const {
Douglas Gregordbdf5e72010-03-15 15:26:48 +00001090 return DC && this->getPrimaryContext() == DC->getPrimaryContext();
Douglas Gregor61481da2009-09-01 17:22:34 +00001091 }
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Douglas Gregor6dd38da2009-08-27 06:03:53 +00001093 /// \brief Determine whether this declaration context encloses the
1094 /// declaration context DC.
Sebastian Redl7a126a42010-08-31 00:36:30 +00001095 bool Encloses(const DeclContext *DC) const;
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +00001096
John McCall4b9c2d22011-11-06 09:01:30 +00001097 /// \brief Find the nearest non-closure ancestor of this context,
1098 /// i.e. the innermost semantic parent of this context which is not
1099 /// a closure. A context may be its own non-closure ancestor.
1100 DeclContext *getNonClosureAncestor();
1101 const DeclContext *getNonClosureAncestor() const {
1102 return const_cast<DeclContext*>(this)->getNonClosureAncestor();
1103 }
1104
Douglas Gregor44b43212008-12-11 16:49:14 +00001105 /// getPrimaryContext - There may be many different
1106 /// declarations of the same entity (including forward declarations
1107 /// of classes, multiple definitions of namespaces, etc.), each with
1108 /// a different set of declarations. This routine returns the
1109 /// "primary" DeclContext structure, which will contain the
1110 /// information needed to perform name lookup into this context.
Steve Naroff0701bbb2009-01-08 17:28:14 +00001111 DeclContext *getPrimaryContext();
John McCall0c01d182010-03-24 05:22:00 +00001112 const DeclContext *getPrimaryContext() const {
1113 return const_cast<DeclContext*>(this)->getPrimaryContext();
1114 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001115
Sebastian Redl7a126a42010-08-31 00:36:30 +00001116 /// getRedeclContext - Retrieve the context in which an entity conflicts with
1117 /// other entities of the same name, or where it is a redeclaration if the
Sebastian Redl410c4f22010-08-31 20:53:31 +00001118 /// two entities are compatible. This skips through transparent contexts.
Sebastian Redl7a126a42010-08-31 00:36:30 +00001119 DeclContext *getRedeclContext();
1120 const DeclContext *getRedeclContext() const {
1121 return const_cast<DeclContext *>(this)->getRedeclContext();
Douglas Gregor17a9b9e2009-01-07 02:48:43 +00001122 }
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Douglas Gregor88b70942009-02-25 22:02:03 +00001124 /// \brief Retrieve the nearest enclosing namespace context.
1125 DeclContext *getEnclosingNamespaceContext();
1126 const DeclContext *getEnclosingNamespaceContext() const {
1127 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
1128 }
1129
Sebastian Redl7a126a42010-08-31 00:36:30 +00001130 /// \brief Test if this context is part of the enclosing namespace set of
1131 /// the context NS, as defined in C++0x [namespace.def]p9. If either context
1132 /// isn't a namespace, this is equivalent to Equals().
1133 ///
1134 /// The enclosing namespace set of a namespace is the namespace and, if it is
1135 /// inline, its enclosing namespace, recursively.
1136 bool InEnclosingNamespaceSetOf(const DeclContext *NS) const;
1137
James Dennett18483f52012-06-15 08:37:50 +00001138 /// \brief Collects all of the declaration contexts that are semantically
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001139 /// connected to this declaration context.
1140 ///
1141 /// For declaration contexts that have multiple semantically connected but
1142 /// syntactically distinct contexts, such as C++ namespaces, this routine
1143 /// retrieves the complete set of such declaration contexts in source order.
1144 /// For example, given:
1145 ///
1146 /// \code
Douglas Gregor44b43212008-12-11 16:49:14 +00001147 /// namespace N {
1148 /// int x;
1149 /// }
1150 /// namespace N {
1151 /// int y;
1152 /// }
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001153 /// \endcode
1154 ///
1155 /// The \c Contexts parameter will contain both definitions of N.
1156 ///
1157 /// \param Contexts Will be cleared and set to the set of declaration
1158 /// contexts that are semanticaly connected to this declaration context,
1159 /// in source order, including this context (which may be the only result,
1160 /// for non-namespace contexts).
1161 void collectAllContexts(llvm::SmallVectorImpl<DeclContext *> &Contexts);
Douglas Gregor44b43212008-12-11 16:49:14 +00001162
1163 /// decl_iterator - Iterates through the declarations stored
1164 /// within this context.
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001165 class decl_iterator {
1166 /// Current - The current declaration.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001167 Decl *Current;
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001168
1169 public:
David Blaikie581deb32012-06-06 20:45:41 +00001170 typedef Decl *value_type;
1171 typedef const value_type &reference;
1172 typedef const value_type *pointer;
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001173 typedef std::forward_iterator_tag iterator_category;
1174 typedef std::ptrdiff_t difference_type;
1175
1176 decl_iterator() : Current(0) { }
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001177 explicit decl_iterator(Decl *C) : Current(C) { }
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001178
1179 reference operator*() const { return Current; }
David Blaikie581deb32012-06-06 20:45:41 +00001180 // This doesn't meet the iterator requirements, but it's convenient
1181 value_type operator->() const { return Current; }
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001182
Chris Lattner96f44682009-03-28 05:59:45 +00001183 decl_iterator& operator++() {
Chris Lattner244a67d2009-03-28 06:04:26 +00001184 Current = Current->getNextDeclInContext();
Chris Lattner96f44682009-03-28 05:59:45 +00001185 return *this;
1186 }
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001187
1188 decl_iterator operator++(int) {
1189 decl_iterator tmp(*this);
1190 ++(*this);
1191 return tmp;
1192 }
1193
Mike Stump1eb44332009-09-09 15:08:12 +00001194 friend bool operator==(decl_iterator x, decl_iterator y) {
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001195 return x.Current == y.Current;
1196 }
Mike Stump1eb44332009-09-09 15:08:12 +00001197 friend bool operator!=(decl_iterator x, decl_iterator y) {
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001198 return x.Current != y.Current;
1199 }
1200 };
Douglas Gregor44b43212008-12-11 16:49:14 +00001201
Douglas Gregor44b43212008-12-11 16:49:14 +00001202 /// decls_begin/decls_end - Iterate over the declarations stored in
Mike Stump1eb44332009-09-09 15:08:12 +00001203 /// this context.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001204 decl_iterator decls_begin() const;
David Blaikie581deb32012-06-06 20:45:41 +00001205 decl_iterator decls_end() const { return decl_iterator(); }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001206 bool decls_empty() const;
Douglas Gregor44b43212008-12-11 16:49:14 +00001207
Sebastian Redl681d7232010-07-27 00:17:23 +00001208 /// noload_decls_begin/end - Iterate over the declarations stored in this
1209 /// context that are currently loaded; don't attempt to retrieve anything
1210 /// from an external source.
1211 decl_iterator noload_decls_begin() const;
David Blaikie581deb32012-06-06 20:45:41 +00001212 decl_iterator noload_decls_end() const { return decl_iterator(); }
Sebastian Redl681d7232010-07-27 00:17:23 +00001213
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001214 /// specific_decl_iterator - Iterates over a subrange of
1215 /// declarations stored in a DeclContext, providing only those that
Douglas Gregor669c9a22009-02-02 18:25:48 +00001216 /// are of type SpecificDecl (or a class derived from it). This
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001217 /// iterator is used, for example, to provide iteration over just
Douglas Gregor669c9a22009-02-02 18:25:48 +00001218 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001219 template<typename SpecificDecl>
1220 class specific_decl_iterator {
1221 /// Current - The current, underlying declaration iterator, which
Douglas Gregord6f0b4e2009-02-02 17:56:05 +00001222 /// will either be NULL or will point to a declaration of
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001223 /// type SpecificDecl.
1224 DeclContext::decl_iterator Current;
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001226 /// SkipToNextDecl - Advances the current position up to the next
1227 /// declaration of type SpecificDecl that also meets the criteria
1228 /// required by Acceptable.
1229 void SkipToNextDecl() {
Douglas Gregor669c9a22009-02-02 18:25:48 +00001230 while (*Current && !isa<SpecificDecl>(*Current))
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001231 ++Current;
1232 }
1233
1234 public:
David Blaikie581deb32012-06-06 20:45:41 +00001235 typedef SpecificDecl *value_type;
1236 // TODO: Add reference and pointer typedefs (with some appropriate proxy
1237 // type) if we ever have a need for them.
1238 typedef void reference;
1239 typedef void pointer;
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001240 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1241 difference_type;
1242 typedef std::forward_iterator_tag iterator_category;
1243
Douglas Gregor669c9a22009-02-02 18:25:48 +00001244 specific_decl_iterator() : Current() { }
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001245
1246 /// specific_decl_iterator - Construct a new iterator over a
Douglas Gregord6f0b4e2009-02-02 17:56:05 +00001247 /// subset of the declarations the range [C,
1248 /// end-of-declarations). If A is non-NULL, it is a pointer to a
1249 /// member function of SpecificDecl that should return true for
1250 /// all of the SpecificDecl instances that will be in the subset
1251 /// of iterators. For example, if you want Objective-C instance
1252 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1253 /// &ObjCMethodDecl::isInstanceMethod.
Douglas Gregor669c9a22009-02-02 18:25:48 +00001254 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001255 SkipToNextDecl();
1256 }
1257
David Blaikie581deb32012-06-06 20:45:41 +00001258 value_type operator*() const { return cast<SpecificDecl>(*Current); }
1259 // This doesn't meet the iterator requirements, but it's convenient
1260 value_type operator->() const { return **this; }
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001261
1262 specific_decl_iterator& operator++() {
1263 ++Current;
1264 SkipToNextDecl();
1265 return *this;
1266 }
1267
1268 specific_decl_iterator operator++(int) {
1269 specific_decl_iterator tmp(*this);
1270 ++(*this);
1271 return tmp;
1272 }
Mike Stump1eb44332009-09-09 15:08:12 +00001273
David Blaikieba243b52011-11-09 06:07:30 +00001274 friend bool operator==(const specific_decl_iterator& x,
1275 const specific_decl_iterator& y) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001276 return x.Current == y.Current;
1277 }
Mike Stump1eb44332009-09-09 15:08:12 +00001278
David Blaikieba243b52011-11-09 06:07:30 +00001279 friend bool operator!=(const specific_decl_iterator& x,
1280 const specific_decl_iterator& y) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001281 return x.Current != y.Current;
1282 }
1283 };
1284
Douglas Gregor669c9a22009-02-02 18:25:48 +00001285 /// \brief Iterates over a filtered subrange of declarations stored
1286 /// in a DeclContext.
1287 ///
1288 /// This iterator visits only those declarations that are of type
1289 /// SpecificDecl (or a class derived from it) and that meet some
1290 /// additional run-time criteria. This iterator is used, for
1291 /// example, to provide access to the instance methods within an
1292 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
1293 /// Acceptable = ObjCMethodDecl::isInstanceMethod).
1294 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
1295 class filtered_decl_iterator {
1296 /// Current - The current, underlying declaration iterator, which
1297 /// will either be NULL or will point to a declaration of
1298 /// type SpecificDecl.
1299 DeclContext::decl_iterator Current;
Mike Stump1eb44332009-09-09 15:08:12 +00001300
Douglas Gregor669c9a22009-02-02 18:25:48 +00001301 /// SkipToNextDecl - Advances the current position up to the next
1302 /// declaration of type SpecificDecl that also meets the criteria
1303 /// required by Acceptable.
1304 void SkipToNextDecl() {
1305 while (*Current &&
1306 (!isa<SpecificDecl>(*Current) ||
1307 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
1308 ++Current;
1309 }
1310
1311 public:
David Blaikie581deb32012-06-06 20:45:41 +00001312 typedef SpecificDecl *value_type;
1313 // TODO: Add reference and pointer typedefs (with some appropriate proxy
1314 // type) if we ever have a need for them.
1315 typedef void reference;
1316 typedef void pointer;
Douglas Gregor669c9a22009-02-02 18:25:48 +00001317 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1318 difference_type;
1319 typedef std::forward_iterator_tag iterator_category;
1320
1321 filtered_decl_iterator() : Current() { }
1322
David Blaikiebd4fa452012-05-01 00:48:43 +00001323 /// filtered_decl_iterator - Construct a new iterator over a
Douglas Gregor669c9a22009-02-02 18:25:48 +00001324 /// subset of the declarations the range [C,
1325 /// end-of-declarations). If A is non-NULL, it is a pointer to a
1326 /// member function of SpecificDecl that should return true for
1327 /// all of the SpecificDecl instances that will be in the subset
1328 /// of iterators. For example, if you want Objective-C instance
1329 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1330 /// &ObjCMethodDecl::isInstanceMethod.
1331 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
1332 SkipToNextDecl();
1333 }
1334
David Blaikie581deb32012-06-06 20:45:41 +00001335 value_type operator*() const { return cast<SpecificDecl>(*Current); }
1336 value_type operator->() const { return cast<SpecificDecl>(*Current); }
Douglas Gregor669c9a22009-02-02 18:25:48 +00001337
1338 filtered_decl_iterator& operator++() {
1339 ++Current;
1340 SkipToNextDecl();
1341 return *this;
1342 }
1343
1344 filtered_decl_iterator operator++(int) {
1345 filtered_decl_iterator tmp(*this);
1346 ++(*this);
1347 return tmp;
1348 }
Mike Stump1eb44332009-09-09 15:08:12 +00001349
David Blaikieba243b52011-11-09 06:07:30 +00001350 friend bool operator==(const filtered_decl_iterator& x,
1351 const filtered_decl_iterator& y) {
Douglas Gregor669c9a22009-02-02 18:25:48 +00001352 return x.Current == y.Current;
1353 }
Mike Stump1eb44332009-09-09 15:08:12 +00001354
David Blaikieba243b52011-11-09 06:07:30 +00001355 friend bool operator!=(const filtered_decl_iterator& x,
1356 const filtered_decl_iterator& y) {
Douglas Gregor669c9a22009-02-02 18:25:48 +00001357 return x.Current != y.Current;
1358 }
1359 };
1360
Douglas Gregor40f4e692009-01-20 16:54:50 +00001361 /// @brief Add the declaration D into this context.
1362 ///
1363 /// This routine should be invoked when the declaration D has first
1364 /// been declared, to place D into the context where it was
1365 /// (lexically) defined. Every declaration must be added to one
1366 /// (and only one!) context, where it can be visited via
1367 /// [decls_begin(), decls_end()). Once a declaration has been added
1368 /// to its lexical context, the corresponding DeclContext owns the
1369 /// declaration.
1370 ///
1371 /// If D is also a NamedDecl, it will be made visible within its
1372 /// semantic context via makeDeclVisibleInContext.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001373 void addDecl(Decl *D);
Sean Callanan381509e2011-10-21 16:15:18 +00001374
1375 /// @brief Add the declaration D into this context, but suppress
1376 /// searches for external declarations with the same name.
1377 ///
1378 /// Although analogous in function to addDecl, this removes an
1379 /// important check. This is only useful if the Decl is being
1380 /// added in response to an external search; in all other cases,
1381 /// addDecl() is the right function to use.
1382 /// See the ASTImporter for use cases.
Sean Callanan9faf8102011-10-21 02:57:43 +00001383 void addDeclInternal(Decl *D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001384
John McCall3f9a8a62009-08-11 06:59:38 +00001385 /// @brief Add the declaration D to this context without modifying
1386 /// any lookup tables.
1387 ///
1388 /// This is useful for some operations in dependent contexts where
1389 /// the semantic context might not be dependent; this basically
1390 /// only happens with friends.
1391 void addHiddenDecl(Decl *D);
1392
John McCall9f54ad42009-12-10 09:41:52 +00001393 /// @brief Removes a declaration from this context.
1394 void removeDecl(Decl *D);
1395
Douglas Gregor44b43212008-12-11 16:49:14 +00001396 /// lookup_iterator - An iterator that provides access to the results
1397 /// of looking up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001398 typedef NamedDecl **lookup_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +00001399
1400 /// lookup_const_iterator - An iterator that provides non-mutable
1401 /// access to the results of lookup up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001402 typedef NamedDecl * const * lookup_const_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +00001403
John McCallea318642010-08-26 09:15:37 +00001404 typedef DeclContextLookupResult lookup_result;
1405 typedef DeclContextLookupConstResult lookup_const_result;
Douglas Gregor44b43212008-12-11 16:49:14 +00001406
1407 /// lookup - Find the declarations (if any) with the given Name in
1408 /// this context. Returns a range of iterators that contains all of
Douglas Gregor40f4e692009-01-20 16:54:50 +00001409 /// the declarations with this name, with object, function, member,
1410 /// and enumerator names preceding any tag name. Note that this
1411 /// routine will not look into parent contexts.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001412 lookup_result lookup(DeclarationName Name);
Douglas Gregor514d3b62012-05-03 23:18:44 +00001413 lookup_const_result lookup(DeclarationName Name) const {
1414 return const_cast<DeclContext*>(this)->lookup(Name);
1415 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001416
Douglas Gregorb75a3452011-10-15 00:10:27 +00001417 /// \brief A simplistic name lookup mechanism that performs name lookup
1418 /// into this declaration context without consulting the external source.
1419 ///
David Blaikieba243b52011-11-09 06:07:30 +00001420 /// This function should almost never be used, because it subverts the
Douglas Gregorb75a3452011-10-15 00:10:27 +00001421 /// usual relationship between a DeclContext and the external source.
1422 /// See the ASTImporter for the (few, but important) use cases.
David Blaikieba243b52011-11-09 06:07:30 +00001423 void localUncachedLookup(DeclarationName Name,
Douglas Gregorb75a3452011-10-15 00:10:27 +00001424 llvm::SmallVectorImpl<NamedDecl *> &Results);
David Blaikieba243b52011-11-09 06:07:30 +00001425
Douglas Gregor40f4e692009-01-20 16:54:50 +00001426 /// @brief Makes a declaration visible within this context.
1427 ///
1428 /// This routine makes the declaration D visible to name lookup
1429 /// within this context and, if this is a transparent context,
1430 /// within its parent contexts up to the first enclosing
1431 /// non-transparent context. Making a declaration visible within a
1432 /// context does not transfer ownership of a declaration, and a
1433 /// declaration can be visible in many contexts that aren't its
1434 /// lexical context.
1435 ///
1436 /// If D is a redeclaration of an existing declaration that is
1437 /// visible from this context, as determined by
1438 /// NamedDecl::declarationReplaces, the previous declaration will be
1439 /// replaced with D.
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001440 void makeDeclVisibleInContext(NamedDecl *D);
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +00001441
Nick Lewycky173a37a2012-04-03 21:44:08 +00001442 /// all_lookups_iterator - An iterator that provides a view over the results
1443 /// of looking up every possible name.
1444 class all_lookups_iterator;
1445
1446 all_lookups_iterator lookups_begin() const;
1447
1448 all_lookups_iterator lookups_end() const;
1449
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001450 /// udir_iterator - Iterates through the using-directives stored
1451 /// within this context.
1452 typedef UsingDirectiveDecl * const * udir_iterator;
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001454 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range;
1455
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001456 udir_iterator_range getUsingDirectives() const;
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001457
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001458 udir_iterator using_directives_begin() const {
1459 return getUsingDirectives().first;
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001460 }
1461
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001462 udir_iterator using_directives_end() const {
1463 return getUsingDirectives().second;
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001464 }
1465
John McCall0c01d182010-03-24 05:22:00 +00001466 // These are all defined in DependentDiagnostic.h.
1467 class ddiag_iterator;
1468 inline ddiag_iterator ddiag_begin() const;
1469 inline ddiag_iterator ddiag_end() const;
1470
Douglas Gregorc2ee10d2009-04-07 17:20:56 +00001471 // Low-level accessors
Sean Callanand5a20c12012-08-23 21:16:40 +00001472
1473 /// \brief Mark the lookup table as needing to be built. This should be
1474 /// used only if setHasExternalLexicalStorage() has been called.
1475 void setMustBuildLookupTable() {
1476 assert(ExternalLexicalStorage && "Requires external lexical storage");
1477 LookupPtr.setInt(true);
1478 }
Douglas Gregorc2ee10d2009-04-07 17:20:56 +00001479
Douglas Gregorc2ee10d2009-04-07 17:20:56 +00001480 /// \brief Retrieve the internal representation of the lookup structure.
Richard Smithc5d3e802012-03-16 06:12:59 +00001481 /// This may omit some names if we are lazily building the structure.
1482 StoredDeclsMap *getLookupPtr() const { return LookupPtr.getPointer(); }
1483
1484 /// \brief Ensure the lookup structure is fully-built and return it.
1485 StoredDeclsMap *buildLookup();
Douglas Gregorc2ee10d2009-04-07 17:20:56 +00001486
Douglas Gregor2cf26342009-04-09 22:27:44 +00001487 /// \brief Whether this DeclContext has external storage containing
1488 /// additional declarations that are lexically in this context.
1489 bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; }
1490
1491 /// \brief State whether this DeclContext has external storage for
1492 /// declarations lexically in this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001493 void setHasExternalLexicalStorage(bool ES = true) {
1494 ExternalLexicalStorage = ES;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001495 }
1496
1497 /// \brief Whether this DeclContext has external storage containing
1498 /// additional declarations that are visible in this context.
1499 bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; }
1500
1501 /// \brief State whether this DeclContext has external storage for
1502 /// declarations visible in this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001503 void setHasExternalVisibleStorage(bool ES = true) {
1504 ExternalVisibleStorage = ES;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001505 }
1506
Douglas Gregor2ea054f2011-08-26 22:04:51 +00001507 /// \brief Determine whether the given declaration is stored in the list of
1508 /// declarations lexically within this context.
1509 bool isDeclInLexicalTraversal(const Decl *D) const {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001510 return D && (D->NextInContextAndBits.getPointer() || D == FirstDecl ||
1511 D == LastDecl);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00001512 }
David Blaikieba243b52011-11-09 06:07:30 +00001513
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +00001514 static bool classof(const Decl *D);
Chris Lattnerb048c982008-04-06 04:47:34 +00001515 static bool classof(const DeclContext *D) { return true; }
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +00001516
Argyrios Kyrtzidis0ee7d942012-02-21 05:04:44 +00001517 LLVM_ATTRIBUTE_USED void dumpDeclContext() const;
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +00001518
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +00001519private:
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001520 void LoadLexicalDeclsFromExternalStorage() const;
David Blaikieba243b52011-11-09 06:07:30 +00001521
1522 /// @brief Makes a declaration visible within this context, but
Sean Callanan381509e2011-10-21 16:15:18 +00001523 /// suppresses searches for external declarations with the same
1524 /// name.
1525 ///
1526 /// Analogous to makeDeclVisibleInContext, but for the exclusive
1527 /// use of addDeclInternal().
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001528 void makeDeclVisibleInContextInternal(NamedDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001529
John McCall0c01d182010-03-24 05:22:00 +00001530 friend class DependentDiagnostic;
1531 StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const;
1532
Richard Smithc5d3e802012-03-16 06:12:59 +00001533 void buildLookupImpl(DeclContext *DCtx);
1534 void makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1535 bool Rediscoverable);
Sean Callanan9faf8102011-10-21 02:57:43 +00001536 void makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001537};
1538
Douglas Gregorf57172b2008-12-08 18:40:42 +00001539inline bool Decl::isTemplateParameter() const {
Douglas Gregor79c22782010-01-16 20:21:20 +00001540 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm ||
1541 getKind() == TemplateTemplateParm;
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001542}
1543
John McCall80cd64a2010-01-29 01:45:37 +00001544// Specialization selected when ToTy is not a known subclass of DeclContext.
1545template <class ToTy,
1546 bool IsKnownSubtype = ::llvm::is_base_of< DeclContext, ToTy>::value>
1547struct cast_convert_decl_context {
1548 static const ToTy *doit(const DeclContext *Val) {
1549 return static_cast<const ToTy*>(Decl::castFromDeclContext(Val));
1550 }
1551
1552 static ToTy *doit(DeclContext *Val) {
1553 return static_cast<ToTy*>(Decl::castFromDeclContext(Val));
1554 }
1555};
1556
1557// Specialization selected when ToTy is a known subclass of DeclContext.
1558template <class ToTy>
1559struct cast_convert_decl_context<ToTy, true> {
1560 static const ToTy *doit(const DeclContext *Val) {
1561 return static_cast<const ToTy*>(Val);
1562 }
1563
1564 static ToTy *doit(DeclContext *Val) {
1565 return static_cast<ToTy*>(Val);
1566 }
1567};
1568
1569
Chris Lattner0ed844b2008-04-04 06:12:32 +00001570} // end clang.
1571
1572namespace llvm {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001573
John McCall80cd64a2010-01-29 01:45:37 +00001574/// isa<T>(DeclContext*)
Eli Friedman4d509342011-05-21 19:15:39 +00001575template <typename To>
1576struct isa_impl<To, ::clang::DeclContext> {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001577 static bool doit(const ::clang::DeclContext &Val) {
Eli Friedman4d509342011-05-21 19:15:39 +00001578 return To::classofKind(Val.getDeclKind());
Chris Lattner0ed844b2008-04-04 06:12:32 +00001579 }
1580};
Chris Lattner0ed844b2008-04-04 06:12:32 +00001581
John McCall80cd64a2010-01-29 01:45:37 +00001582/// cast<T>(DeclContext*)
1583template<class ToTy>
1584struct cast_convert_val<ToTy,
1585 const ::clang::DeclContext,const ::clang::DeclContext> {
1586 static const ToTy &doit(const ::clang::DeclContext &Val) {
1587 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1588 }
1589};
1590template<class ToTy>
1591struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> {
1592 static ToTy &doit(::clang::DeclContext &Val) {
1593 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1594 }
1595};
1596template<class ToTy>
1597struct cast_convert_val<ToTy,
1598 const ::clang::DeclContext*, const ::clang::DeclContext*> {
1599 static const ToTy *doit(const ::clang::DeclContext *Val) {
1600 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1601 }
1602};
1603template<class ToTy>
1604struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> {
1605 static ToTy *doit(::clang::DeclContext *Val) {
1606 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1607 }
1608};
1609
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001610/// Implement cast_convert_val for Decl -> DeclContext conversions.
Chris Lattner0ed844b2008-04-04 06:12:32 +00001611template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001612struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
Chris Lattnerb048c982008-04-06 04:47:34 +00001613 static ::clang::DeclContext &doit(const FromTy &Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001614 return *FromTy::castToDeclContext(&Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001615 }
1616};
Chris Lattner0ed844b2008-04-04 06:12:32 +00001617
1618template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001619struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
Chris Lattnerb048c982008-04-06 04:47:34 +00001620 static ::clang::DeclContext *doit(const FromTy *Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001621 return FromTy::castToDeclContext(Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001622 }
1623};
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001624
Douglas Gregor44b43212008-12-11 16:49:14 +00001625template<class FromTy>
1626struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
1627 static const ::clang::DeclContext &doit(const FromTy &Val) {
1628 return *FromTy::castToDeclContext(&Val);
1629 }
1630};
1631
1632template<class FromTy>
1633struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
1634 static const ::clang::DeclContext *doit(const FromTy *Val) {
1635 return FromTy::castToDeclContext(Val);
1636 }
1637};
1638
Chris Lattner0ed844b2008-04-04 06:12:32 +00001639} // end namespace llvm
1640
1641#endif