blob: 121bd005353867537a741cbaf5821106a4f8dc9d [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"
Stephen Hines0e2c34f2015-03-23 12:09:02 -070021#include "llvm/ADT/iterator.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070022#include "llvm/ADT/iterator_range.h"
Daniel Dunbaraa49a7d2012-03-09 19:35:29 +000023#include "llvm/Support/Compiler.h"
24#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner0ed844b2008-04-04 06:12:32 +000025
26namespace clang {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000027class ASTMutationListener;
Benjamin Kramer9852f582012-12-01 16:35:25 +000028class BlockDecl;
29class CXXRecordDecl;
30class CompoundStmt;
31class DeclContext;
32class DeclarationName;
33class DependentDiagnostic;
34class EnumDecl;
35class FunctionDecl;
Stephen Hines651f13c2014-04-23 16:59:28 -070036class FunctionType;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070037enum Linkage : unsigned char;
Rafael Espindoladc070562013-05-28 19:43:11 +000038class LinkageComputer;
Benjamin Kramer9852f582012-12-01 16:35:25 +000039class LinkageSpecDecl;
Douglas Gregorca2ab452013-01-12 01:29:50 +000040class Module;
Benjamin Kramer9852f582012-12-01 16:35:25 +000041class NamedDecl;
42class NamespaceDecl;
43class ObjCCategoryDecl;
44class ObjCCategoryImplDecl;
45class ObjCContainerDecl;
46class ObjCImplDecl;
47class ObjCImplementationDecl;
48class ObjCInterfaceDecl;
49class ObjCMethodDecl;
50class ObjCProtocolDecl;
51struct PrintingPolicy;
Stephen Hines176edba2014-12-01 14:53:08 -080052class RecordDecl;
Benjamin Kramer9852f582012-12-01 16:35:25 +000053class Stmt;
54class StoredDeclsMap;
55class TranslationUnitDecl;
56class UsingDirectiveDecl;
Chris Lattner0eda3b32009-03-29 04:32:54 +000057}
58
Chris Lattner0eda3b32009-03-29 04:32:54 +000059namespace clang {
Chris Lattner0ed844b2008-04-04 06:12:32 +000060
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000061 /// \brief Captures the result of checking the availability of a
62 /// declaration.
63 enum AvailabilityResult {
64 AR_Available = 0,
65 AR_NotYetIntroduced,
66 AR_Deprecated,
67 AR_Unavailable
68 };
69
Mike Stump1eb44332009-09-09 15:08:12 +000070/// Decl - This represents one declaration (or definition), e.g. a variable,
71/// typedef, function, struct, etc.
Chris Lattner0ed844b2008-04-04 06:12:32 +000072///
73class Decl {
74public:
Douglas Gregor64650af2009-02-02 23:39:07 +000075 /// \brief Lists the kind of concrete classes of Decl.
Chris Lattner0ed844b2008-04-04 06:12:32 +000076 enum Kind {
Sean Hunt9a555912010-05-30 07:21:58 +000077#define DECL(DERIVED, BASE) DERIVED,
78#define ABSTRACT_DECL(DECL)
79#define DECL_RANGE(BASE, START, END) \
80 first##BASE = START, last##BASE = END,
81#define LAST_DECL_RANGE(BASE, START, END) \
82 first##BASE = START, last##BASE = END
83#include "clang/AST/DeclNodes.inc"
Chris Lattner0ed844b2008-04-04 06:12:32 +000084 };
85
Chris Lattner6ad9ac02010-05-07 21:43:38 +000086 /// \brief A placeholder type used to construct an empty shell of a
87 /// decl-derived type that will be filled in later (e.g., by some
88 /// deserialization method).
89 struct EmptyShell { };
90
John McCall0d6b1642010-04-23 18:46:30 +000091 /// IdentifierNamespace - The different namespaces in which
92 /// declarations may appear. According to C99 6.2.3, there are
93 /// four namespaces, labels, tags, members and ordinary
94 /// identifiers. C++ describes lookup completely differently:
95 /// certain lookups merely "ignore" certain kinds of declarations,
96 /// usually based on whether the declaration is of a type, etc.
David Blaikieba243b52011-11-09 06:07:30 +000097 ///
John McCall0d6b1642010-04-23 18:46:30 +000098 /// These are meant as bitmasks, so that searches in
99 /// C++ can look into the "tag" namespace during ordinary lookup.
100 ///
Sebastian Redl07a353c2010-07-14 20:26:45 +0000101 /// Decl currently provides 15 bits of IDNS bits.
Chris Lattner0ed844b2008-04-04 06:12:32 +0000102 enum IdentifierNamespace {
John McCall0d6b1642010-04-23 18:46:30 +0000103 /// Labels, declared with 'x:' and referenced with 'goto x'.
104 IDNS_Label = 0x0001,
105
106 /// Tags, declared with 'struct foo;' and referenced with
107 /// 'struct foo'. All tags are also types. This is what
108 /// elaborated-type-specifiers look for in C.
109 IDNS_Tag = 0x0002,
110
111 /// Types, declared with 'struct foo', typedefs, etc.
112 /// This is what elaborated-type-specifiers look for in C++,
113 /// but note that it's ill-formed to find a non-tag.
114 IDNS_Type = 0x0004,
115
116 /// Members, declared with object declarations within tag
117 /// definitions. In C, these can only be found by "qualified"
118 /// lookup in member expressions. In C++, they're found by
119 /// normal lookup.
120 IDNS_Member = 0x0008,
121
122 /// Namespaces, declared with 'namespace foo {}'.
123 /// Lookup for nested-name-specifiers find these.
124 IDNS_Namespace = 0x0010,
125
126 /// Ordinary names. In C, everything that's not a label, tag,
127 /// or member ends up here.
128 IDNS_Ordinary = 0x0020,
129
Jordan Rosed4582b82013-04-03 01:39:23 +0000130 /// Objective C \@protocol.
John McCall0d6b1642010-04-23 18:46:30 +0000131 IDNS_ObjCProtocol = 0x0040,
132
133 /// This declaration is a friend function. A friend function
134 /// declaration is always in this namespace but may also be in
135 /// IDNS_Ordinary if it was previously declared.
136 IDNS_OrdinaryFriend = 0x0080,
137
138 /// This declaration is a friend class. A friend class
139 /// declaration is always in this namespace but may also be in
140 /// IDNS_Tag|IDNS_Type if it was previously declared.
141 IDNS_TagFriend = 0x0100,
142
143 /// This declaration is a using declaration. A using declaration
144 /// *introduces* a number of other declarations into the current
145 /// scope, and those declarations use the IDNS of their targets,
146 /// but the actual using declarations go in this namespace.
John McCall76d32642010-04-24 01:30:58 +0000147 IDNS_Using = 0x0200,
148
149 /// This declaration is a C++ operator declared in a non-class
150 /// context. All such operators are also in IDNS_Ordinary.
151 /// C++ lexical operator lookup looks for these.
Richard Smitha41c97a2013-09-20 01:15:31 +0000152 IDNS_NonMemberOperator = 0x0400,
153
154 /// This declaration is a function-local extern declaration of a
155 /// variable or function. This may also be IDNS_Ordinary if it
156 /// has been declared outside any function.
157 IDNS_LocalExtern = 0x0800
Chris Lattner0ed844b2008-04-04 06:12:32 +0000158 };
Mike Stump1eb44332009-09-09 15:08:12 +0000159
John McCall09e2c522011-05-01 03:04:29 +0000160 /// ObjCDeclQualifier - 'Qualifiers' written next to the return and
161 /// parameter types in method declarations. Other than remembering
162 /// them and mangling them into the method's signature string, these
163 /// are ignored by the compiler; they are consumed by certain
164 /// remote-messaging frameworks.
165 ///
166 /// in, inout, and out are mutually exclusive and apply only to
167 /// method parameters. bycopy and byref are mutually exclusive and
168 /// apply only to method parameters (?). oneway applies only to
169 /// results. All of these expect their corresponding parameter to
170 /// have a particular type. None of this is currently enforced by
171 /// clang.
172 ///
173 /// This should be kept in sync with ObjCDeclSpec::ObjCDeclQualifier.
Chris Lattner0ed844b2008-04-04 06:12:32 +0000174 enum ObjCDeclQualifier {
175 OBJC_TQ_None = 0x0,
176 OBJC_TQ_In = 0x1,
177 OBJC_TQ_Inout = 0x2,
178 OBJC_TQ_Out = 0x4,
179 OBJC_TQ_Bycopy = 0x8,
180 OBJC_TQ_Byref = 0x10,
181 OBJC_TQ_Oneway = 0x20
182 };
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Douglas Gregor46cd2182012-01-06 16:59:53 +0000184protected:
185 // Enumeration values used in the bits stored in NextInContextAndBits.
186 enum {
187 /// \brief Whether this declaration is a top-level declaration (function,
188 /// global variable, etc.) that is lexically inside an objc container
189 /// definition.
190 TopLevelDeclInObjCContainerFlag = 0x01,
191
192 /// \brief Whether this declaration is private to the module in which it was
193 /// defined.
194 ModulePrivateFlag = 0x02
195 };
196
197 /// \brief The next declaration within the same lexical
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000198 /// DeclContext. These pointers form the linked list that is
199 /// traversed via DeclContext's decls_begin()/decls_end().
Douglas Gregor46cd2182012-01-06 16:59:53 +0000200 ///
201 /// The extra two bits are used for the TopLevelDeclInObjCContainer and
202 /// ModulePrivate bits.
203 llvm::PointerIntPair<Decl *, 2, unsigned> NextInContextAndBits;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000204
Douglas Gregor46cd2182012-01-06 16:59:53 +0000205private:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000206 friend class DeclContext;
207
Chris Lattneree219fd2009-03-29 06:06:59 +0000208 struct MultipleDC {
209 DeclContext *SemanticDC;
210 DeclContext *LexicalDC;
211 };
Mike Stump1eb44332009-09-09 15:08:12 +0000212
213
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000214 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
215 /// For declarations that don't contain C++ scope specifiers, it contains
216 /// the DeclContext where the Decl was declared.
217 /// For declarations with C++ scope specifiers, it contains a MultipleDC*
218 /// with the context where it semantically belongs (SemanticDC) and the
219 /// context where it was lexically declared (LexicalDC).
220 /// e.g.:
221 ///
222 /// namespace A {
223 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
224 /// }
225 /// void A::f(); // SemanticDC == namespace 'A'
226 /// // LexicalDC == global namespace
Chris Lattneree219fd2009-03-29 06:06:59 +0000227 llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000228
Chris Lattneree219fd2009-03-29 06:06:59 +0000229 inline bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); }
230 inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000231 inline MultipleDC *getMultipleDC() const {
Chris Lattneree219fd2009-03-29 06:06:59 +0000232 return DeclCtx.get<MultipleDC*>();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000233 }
Chris Lattner10d83792009-03-27 18:46:15 +0000234 inline DeclContext *getSemanticDC() const {
Chris Lattneree219fd2009-03-29 06:06:59 +0000235 return DeclCtx.get<DeclContext*>();
Chris Lattner10d83792009-03-27 18:46:15 +0000236 }
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Douglas Gregor381d34e2010-12-06 18:36:25 +0000238 /// Loc - The location of this decl.
Daniel Dunbar39d76502009-03-04 02:26:41 +0000239 SourceLocation Loc;
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Chris Lattner0ed844b2008-04-04 06:12:32 +0000241 /// DeclKind - This indicates which class this is.
Rafael Espindola838dc592013-01-12 06:42:30 +0000242 unsigned DeclKind : 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Chris Lattner0ed844b2008-04-04 06:12:32 +0000244 /// InvalidDecl - This indicates a semantic error occurred.
John McCall35043e52010-10-19 05:43:52 +0000245 unsigned InvalidDecl : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Chris Lattner0ed844b2008-04-04 06:12:32 +0000247 /// HasAttrs - This indicates whether the decl has attributes or not.
John McCall35043e52010-10-19 05:43:52 +0000248 unsigned HasAttrs : 1;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000249
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000250 /// Implicit - Whether this declaration was implicitly generated by
251 /// the implementation rather than explicitly written by the user.
John McCall35043e52010-10-19 05:43:52 +0000252 unsigned Implicit : 1;
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000253
Douglas Gregore0762c92009-06-19 23:52:42 +0000254 /// \brief Whether this declaration was "used", meaning that a definition is
255 /// required.
John McCall35043e52010-10-19 05:43:52 +0000256 unsigned Used : 1;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000257
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000258 /// \brief Whether this declaration was "referenced".
259 /// The difference with 'Used' is whether the reference appears in a
260 /// evaluated context or not, e.g. functions used in uninstantiated templates
261 /// are regarded as "referenced" but not "used".
262 unsigned Referenced : 1;
David Blaikieba243b52011-11-09 06:07:30 +0000263
Daniel Dunbar02892a62012-03-05 21:42:49 +0000264 /// \brief Whether statistic collection is enabled.
265 static bool StatisticsEnabled;
266
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000267protected:
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000268 /// Access - Used by C++ decls for the access specifier.
269 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
270 unsigned Access : 2;
271 friend class CXXClassMemberWrapper;
Sebastian Redl0b17c612010-08-13 00:28:03 +0000272
Douglas Gregor08e0bc12011-09-10 00:09:20 +0000273 /// \brief Whether this declaration was loaded from an AST file.
274 unsigned FromASTFile : 1;
David Blaikieba243b52011-11-09 06:07:30 +0000275
Douglas Gregorf143ffc2012-01-06 16:22:39 +0000276 /// \brief Whether this declaration is hidden from normal name lookup, e.g.,
277 /// because it is was loaded from an AST file is either module-private or
278 /// because its submodule has not been made visible.
279 unsigned Hidden : 1;
280
Chris Lattner769dbdf2009-03-27 20:18:19 +0000281 /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in.
Douglas Gregor381d34e2010-12-06 18:36:25 +0000282 unsigned IdentifierNamespace : 12;
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Rafael Espindolaa99ecbc2013-05-25 17:16:20 +0000284 /// \brief If 0, we have not computed the linkage of this declaration.
285 /// Otherwise, it is the linkage + 1.
286 mutable unsigned CacheValidAndLinkage : 3;
David Blaikieba243b52011-11-09 06:07:30 +0000287
Douglas Gregor8d267c52011-09-09 02:06:17 +0000288 friend class ASTDeclWriter;
289 friend class ASTDeclReader;
Douglas Gregorecc2c092011-12-01 22:20:10 +0000290 friend class ASTReader;
Rafael Espindoladc070562013-05-28 19:43:11 +0000291 friend class LinkageComputer;
Douglas Gregor8d267c52011-09-09 02:06:17 +0000292
Richard Smith22050f22013-07-17 23:53:16 +0000293 template<typename decl_type> friend class Redeclarable;
294
Stephen Hines651f13c2014-04-23 16:59:28 -0700295 /// \brief Allocate memory for a deserialized declaration.
296 ///
297 /// This routine must be used to allocate memory for any declaration that is
298 /// deserialized from a module file.
299 ///
300 /// \param Size The size of the allocated object.
301 /// \param Ctx The context in which we will allocate memory.
302 /// \param ID The global ID of the deserialized declaration.
303 /// \param Extra The amount of extra space to allocate after the object.
304 void *operator new(std::size_t Size, const ASTContext &Ctx, unsigned ID,
305 std::size_t Extra = 0);
306
307 /// \brief Allocate memory for a non-deserialized declaration.
308 void *operator new(std::size_t Size, const ASTContext &Ctx,
309 DeclContext *Parent, std::size_t Extra = 0);
310
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000311private:
Stephen Hines651f13c2014-04-23 16:59:28 -0700312 bool AccessDeclContextSanity() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000314protected:
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000315
Mike Stump1eb44332009-09-09 15:08:12 +0000316 Decl(Kind DK, DeclContext *DC, SourceLocation L)
Douglas Gregor46cd2182012-01-06 16:59:53 +0000317 : NextInContextAndBits(), DeclCtx(DC),
Daniel Dunbar39d76502009-03-04 02:26:41 +0000318 Loc(L), DeclKind(DK), InvalidDecl(0),
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000319 HasAttrs(false), Implicit(false), Used(false), Referenced(false),
Douglas Gregor46cd2182012-01-06 16:59:53 +0000320 Access(AS_none), FromASTFile(0), Hidden(0),
Douglas Gregor381d34e2010-12-06 18:36:25 +0000321 IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
Rafael Espindolaa99ecbc2013-05-25 17:16:20 +0000322 CacheValidAndLinkage(0)
Douglas Gregor381d34e2010-12-06 18:36:25 +0000323 {
Daniel Dunbar02892a62012-03-05 21:42:49 +0000324 if (StatisticsEnabled) add(DK);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000325 }
Sam Bishop1bb19632008-04-11 18:04:39 +0000326
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +0000327 Decl(Kind DK, EmptyShell Empty)
Douglas Gregor46cd2182012-01-06 16:59:53 +0000328 : NextInContextAndBits(), DeclKind(DK), InvalidDecl(0),
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000329 HasAttrs(false), Implicit(false), Used(false), Referenced(false),
Douglas Gregor46cd2182012-01-06 16:59:53 +0000330 Access(AS_none), FromASTFile(0), Hidden(0),
Douglas Gregor381d34e2010-12-06 18:36:25 +0000331 IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
Rafael Espindolaa99ecbc2013-05-25 17:16:20 +0000332 CacheValidAndLinkage(0)
Douglas Gregor381d34e2010-12-06 18:36:25 +0000333 {
Daniel Dunbar02892a62012-03-05 21:42:49 +0000334 if (StatisticsEnabled) add(DK);
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +0000335 }
336
Douglas Gregorda2142f2011-02-19 18:51:44 +0000337 virtual ~Decl();
338
Douglas Gregor6bd99292013-02-09 01:35:03 +0000339 /// \brief Update a potentially out-of-date declaration.
340 void updateOutOfDate(IdentifierInfo &II) const;
341
Rafael Espindolaa99ecbc2013-05-25 17:16:20 +0000342 Linkage getCachedLinkage() const {
343 return Linkage(CacheValidAndLinkage - 1);
344 }
345
346 void setCachedLinkage(Linkage L) const {
347 CacheValidAndLinkage = L + 1;
348 }
349
350 bool hasCachedLinkage() const {
351 return CacheValidAndLinkage;
352 }
353
Sam Bishop1bb19632008-04-11 18:04:39 +0000354public:
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000355
356 /// \brief Source range that this declaration covers.
Daniel Dunbaraa49a7d2012-03-09 19:35:29 +0000357 virtual SourceRange getSourceRange() const LLVM_READONLY {
Douglas Gregorda2142f2011-02-19 18:51:44 +0000358 return SourceRange(getLocation(), getLocation());
359 }
Daniel Dunbaraa49a7d2012-03-09 19:35:29 +0000360 SourceLocation getLocStart() const LLVM_READONLY {
361 return getSourceRange().getBegin();
362 }
363 SourceLocation getLocEnd() const LLVM_READONLY {
364 return getSourceRange().getEnd();
365 }
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000366
Chris Lattner0ed844b2008-04-04 06:12:32 +0000367 SourceLocation getLocation() const { return Loc; }
368 void setLocation(SourceLocation L) { Loc = L; }
369
John McCall35043e52010-10-19 05:43:52 +0000370 Kind getKind() const { return static_cast<Kind>(DeclKind); }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000371 const char *getDeclKindName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Douglas Gregor46cd2182012-01-06 16:59:53 +0000373 Decl *getNextDeclInContext() { return NextInContextAndBits.getPointer(); }
374 const Decl *getNextDeclInContext() const {return NextInContextAndBits.getPointer();}
Chris Lattner96f44682009-03-28 05:59:45 +0000375
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000376 DeclContext *getDeclContext() {
Chris Lattner10d83792009-03-27 18:46:15 +0000377 if (isInSemaDC())
378 return getSemanticDC();
379 return getMultipleDC()->SemanticDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000380 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000381 const DeclContext *getDeclContext() const {
382 return const_cast<Decl*>(this)->getDeclContext();
383 }
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000384
John McCallf5ebf9b2013-05-03 07:33:41 +0000385 /// Find the innermost non-closure ancestor of this declaration,
386 /// walking up through blocks, lambdas, etc. If that ancestor is
387 /// not a code context (!isFunctionOrMethod()), returns null.
388 ///
389 /// A declaration may be its own non-closure context.
390 Decl *getNonClosureContext();
391 const Decl *getNonClosureContext() const {
John McCallaab9e312011-02-22 22:25:23 +0000392 return const_cast<Decl*>(this)->getNonClosureContext();
393 }
394
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000395 TranslationUnitDecl *getTranslationUnitDecl();
396 const TranslationUnitDecl *getTranslationUnitDecl() const {
397 return const_cast<Decl*>(this)->getTranslationUnitDecl();
398 }
399
John McCall9aeed322009-10-01 00:25:31 +0000400 bool isInAnonymousNamespace() const;
401
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700402 bool isInStdNamespace() const;
403
Daniel Dunbaraa49a7d2012-03-09 19:35:29 +0000404 ASTContext &getASTContext() const LLVM_READONLY;
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Anders Carlsson1329c272009-03-25 23:38:06 +0000406 void setAccess(AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000407 Access = AS;
Stephen Hines651f13c2014-04-23 16:59:28 -0700408 assert(AccessDeclContextSanity());
Anders Carlssonb8547e82009-03-25 20:19:57 +0000409 }
Mike Stump1eb44332009-09-09 15:08:12 +0000410
411 AccessSpecifier getAccess() const {
Stephen Hines651f13c2014-04-23 16:59:28 -0700412 assert(AccessDeclContextSanity());
Mike Stump1eb44332009-09-09 15:08:12 +0000413 return AccessSpecifier(Access);
Anders Carlsson1329c272009-03-25 23:38:06 +0000414 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000415
Douglas Gregor109f5fc2013-04-08 17:12:58 +0000416 /// \brief Retrieve the access specifier for this declaration, even though
417 /// it may not yet have been properly set.
418 AccessSpecifier getAccessUnsafe() const {
419 return AccessSpecifier(Access);
420 }
421
Chris Lattner76a642f2009-02-15 22:43:40 +0000422 bool hasAttrs() const { return HasAttrs; }
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000423 void setAttrs(const AttrVec& Attrs) {
424 return setAttrsImpl(Attrs, getASTContext());
425 }
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000426 AttrVec &getAttrs() {
Sean Huntcf807c42010-08-18 23:23:40 +0000427 return const_cast<AttrVec&>(const_cast<const Decl*>(this)->getAttrs());
Chris Lattner81abbdd2009-03-21 06:27:31 +0000428 }
Sean Huntcf807c42010-08-18 23:23:40 +0000429 const AttrVec &getAttrs() const;
Sean Huntcf807c42010-08-18 23:23:40 +0000430 void dropAttrs();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000431
Douglas Gregorb5f35ba2010-12-06 17:49:01 +0000432 void addAttr(Attr *A) {
433 if (hasAttrs())
434 getAttrs().push_back(A);
435 else
436 setAttrs(AttrVec(1, A));
437 }
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Sean Huntcf807c42010-08-18 23:23:40 +0000439 typedef AttrVec::const_iterator attr_iterator;
Stephen Hines651f13c2014-04-23 16:59:28 -0700440 typedef llvm::iterator_range<attr_iterator> attr_range;
Sean Huntcf807c42010-08-18 23:23:40 +0000441
Stephen Hines651f13c2014-04-23 16:59:28 -0700442 attr_range attrs() const {
443 return attr_range(attr_begin(), attr_end());
444 }
445
Sean Huntcf807c42010-08-18 23:23:40 +0000446 attr_iterator attr_begin() const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700447 return hasAttrs() ? getAttrs().begin() : nullptr;
Sean Huntcf807c42010-08-18 23:23:40 +0000448 }
449 attr_iterator attr_end() const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700450 return hasAttrs() ? getAttrs().end() : nullptr;
Sean Huntcf807c42010-08-18 23:23:40 +0000451 }
David Blaikieba243b52011-11-09 06:07:30 +0000452
Fariborz Jahanianc3ca14d2011-06-23 17:50:10 +0000453 template <typename T>
Fariborz Jahanian8cf0f522011-06-23 20:24:38 +0000454 void dropAttr() {
455 if (!HasAttrs) return;
David Blaikieba243b52011-11-09 06:07:30 +0000456
Benjamin Kramer10be5ba2012-10-14 11:50:50 +0000457 AttrVec &Vec = getAttrs();
458 Vec.erase(std::remove_if(Vec.begin(), Vec.end(), isa<T, Attr*>), Vec.end());
459
460 if (Vec.empty())
Fariborz Jahanian8cf0f522011-06-23 20:24:38 +0000461 HasAttrs = false;
462 }
David Blaikieba243b52011-11-09 06:07:30 +0000463
Sean Huntcf807c42010-08-18 23:23:40 +0000464 template <typename T>
Stephen Hines651f13c2014-04-23 16:59:28 -0700465 llvm::iterator_range<specific_attr_iterator<T>> specific_attrs() const {
466 return llvm::iterator_range<specific_attr_iterator<T>>(
467 specific_attr_begin<T>(), specific_attr_end<T>());
468 }
469
470 template <typename T>
Sean Huntcf807c42010-08-18 23:23:40 +0000471 specific_attr_iterator<T> specific_attr_begin() const {
472 return specific_attr_iterator<T>(attr_begin());
473 }
474 template <typename T>
475 specific_attr_iterator<T> specific_attr_end() const {
476 return specific_attr_iterator<T>(attr_end());
477 }
478
479 template<typename T> T *getAttr() const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700480 return hasAttrs() ? getSpecificAttr<T>(getAttrs()) : nullptr;
Sean Huntcf807c42010-08-18 23:23:40 +0000481 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000482 template<typename T> bool hasAttr() const {
Sean Huntcf807c42010-08-18 23:23:40 +0000483 return hasAttrs() && hasSpecificAttr<T>(getAttrs());
484 }
485
486 /// getMaxAlignment - return the maximum alignment specified by attributes
487 /// on this decl, 0 if there are none.
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +0000488 unsigned getMaxAlignment() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Chris Lattner0ed844b2008-04-04 06:12:32 +0000490 /// setInvalidDecl - Indicates the Decl had a semantic error. This
491 /// allows for graceful error recovery.
Douglas Gregor42738572010-03-05 00:26:45 +0000492 void setInvalidDecl(bool Invalid = true);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000493 bool isInvalidDecl() const { return (bool) InvalidDecl; }
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000494
495 /// isImplicit - Indicates whether the declaration was implicitly
496 /// generated by the implementation. If false, this declaration
497 /// was written explicitly in the source code.
498 bool isImplicit() const { return Implicit; }
499 void setImplicit(bool I = true) { Implicit = I; }
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Douglas Gregore0762c92009-06-19 23:52:42 +0000501 /// \brief Whether this declaration was used, meaning that a definition
502 /// is required.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000503 ///
504 /// \param CheckUsedAttr When true, also consider the "used" attribute
505 /// (in addition to the "used" bit set by \c setUsed()) when determining
506 /// whether the function is used.
507 bool isUsed(bool CheckUsedAttr = true) const;
Sean Huntcf807c42010-08-18 23:23:40 +0000508
Eli Friedman86164e82013-09-05 00:02:25 +0000509 /// \brief Set whether the declaration is used, in the sense of odr-use.
510 ///
511 /// This should only be used immediately after creating a declaration.
Rafael Espindolae7bd89a2013-10-23 16:46:34 +0000512 void setIsUsed() { Used = true; }
Eli Friedman86164e82013-09-05 00:02:25 +0000513
514 /// \brief Mark the declaration used, in the sense of odr-use.
515 ///
516 /// This notifies any mutation listeners in addition to setting a bit
517 /// indicating the declaration is used.
518 void markUsed(ASTContext &C);
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Stephen Hines176edba2014-12-01 14:53:08 -0800520 /// \brief Whether any declaration of this entity was referenced.
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000521 bool isReferenced() const;
522
Stephen Hines176edba2014-12-01 14:53:08 -0800523 /// \brief Whether this declaration was referenced. This should not be relied
524 /// upon for anything other than debugging.
525 bool isThisDeclarationReferenced() const { return Referenced; }
526
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000527 void setReferenced(bool R = true) { Referenced = R; }
528
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +0000529 /// \brief Whether this declaration is a top-level declaration (function,
530 /// global variable, etc.) that is lexically inside an objc container
531 /// definition.
532 bool isTopLevelDeclInObjCContainer() const {
Douglas Gregor46cd2182012-01-06 16:59:53 +0000533 return NextInContextAndBits.getInt() & TopLevelDeclInObjCContainerFlag;
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +0000534 }
535
536 void setTopLevelDeclInObjCContainer(bool V = true) {
Douglas Gregor46cd2182012-01-06 16:59:53 +0000537 unsigned Bits = NextInContextAndBits.getInt();
538 if (V)
539 Bits |= TopLevelDeclInObjCContainerFlag;
540 else
541 Bits &= ~TopLevelDeclInObjCContainerFlag;
542 NextInContextAndBits.setInt(Bits);
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +0000543 }
544
Douglas Gregor46cd2182012-01-06 16:59:53 +0000545 /// \brief Whether this declaration was marked as being private to the
546 /// module in which it was defined.
Richard Smithb7751002013-07-25 23:08:39 +0000547 bool isModulePrivate() const {
Douglas Gregor46cd2182012-01-06 16:59:53 +0000548 return NextInContextAndBits.getInt() & ModulePrivateFlag;
549 }
Richard Smithb7751002013-07-25 23:08:39 +0000550
551protected:
Douglas Gregor46cd2182012-01-06 16:59:53 +0000552 /// \brief Specify whether this declaration was marked as being private
553 /// to the module in which it was defined.
554 void setModulePrivate(bool MP = true) {
555 unsigned Bits = NextInContextAndBits.getInt();
556 if (MP)
557 Bits |= ModulePrivateFlag;
558 else
559 Bits &= ~ModulePrivateFlag;
560 NextInContextAndBits.setInt(Bits);
561 }
562
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +0000563 /// \brief Set the owning module ID.
564 void setOwningModuleID(unsigned ID) {
565 assert(isFromASTFile() && "Only works on a deserialized declaration");
566 *((unsigned*)this - 2) = ID;
567 }
568
Douglas Gregor46cd2182012-01-06 16:59:53 +0000569public:
570
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000571 /// \brief Determine the availability of the given declaration.
572 ///
573 /// This routine will determine the most restrictive availability of
574 /// the given declaration (e.g., preferring 'unavailable' to
575 /// 'deprecated').
576 ///
577 /// \param Message If non-NULL and the result is not \c
578 /// AR_Available, will be set to a (possibly empty) message
579 /// describing why the declaration has not been introduced, is
580 /// deprecated, or is unavailable.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700581 AvailabilityResult getAvailability(std::string *Message = nullptr) const;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000582
583 /// \brief Determine whether this declaration is marked 'deprecated'.
584 ///
585 /// \param Message If non-NULL and the declaration is deprecated,
586 /// this will be set to the message describing why the declaration
587 /// was deprecated (which may be empty).
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700588 bool isDeprecated(std::string *Message = nullptr) const {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000589 return getAvailability(Message) == AR_Deprecated;
590 }
591
592 /// \brief Determine whether this declaration is marked 'unavailable'.
593 ///
594 /// \param Message If non-NULL and the declaration is unavailable,
595 /// this will be set to the message describing why the declaration
596 /// was made unavailable (which may be empty).
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700597 bool isUnavailable(std::string *Message = nullptr) const {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000598 return getAvailability(Message) == AR_Unavailable;
599 }
600
601 /// \brief Determine whether this is a weak-imported symbol.
602 ///
603 /// Weak-imported symbols are typically marked with the
Eli Friedman7f3ad232011-03-28 02:00:21 +0000604 /// 'weak_import' attribute, but may also be marked with an
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000605 /// 'availability' attribute where we're targing a platform prior to
606 /// the introduction of this feature.
607 bool isWeakImported() const;
608
609 /// \brief Determines whether this symbol can be weak-imported,
610 /// e.g., whether it would be well-formed to add the weak_import
611 /// attribute.
612 ///
613 /// \param IsDefinition Set to \c true to indicate that this
614 /// declaration cannot be weak-imported because it has a definition.
615 bool canBeWeakImported(bool &IsDefinition) const;
616
Douglas Gregor919814d2011-09-09 23:01:35 +0000617 /// \brief Determine whether this declaration came from an AST file (such as
618 /// a precompiled header or module) rather than having been parsed.
Douglas Gregor08e0bc12011-09-10 00:09:20 +0000619 bool isFromASTFile() const { return FromASTFile; }
David Blaikieba243b52011-11-09 06:07:30 +0000620
Douglas Gregorb6b60c12012-01-05 22:27:05 +0000621 /// \brief Retrieve the global declaration ID associated with this
622 /// declaration, which specifies where in the
623 unsigned getGlobalID() const {
624 if (isFromASTFile())
625 return *((const unsigned*)this - 1);
626 return 0;
627 }
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +0000628
629 /// \brief Retrieve the global ID of the module that owns this particular
630 /// declaration.
631 unsigned getOwningModuleID() const {
632 if (isFromASTFile())
633 return *((const unsigned*)this - 2);
634
635 return 0;
636 }
Douglas Gregorca2ab452013-01-12 01:29:50 +0000637
638private:
639 Module *getOwningModuleSlow() const;
640
641public:
642 Module *getOwningModule() const {
643 if (!isFromASTFile())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700644 return nullptr;
Douglas Gregorca2ab452013-01-12 01:29:50 +0000645
646 return getOwningModuleSlow();
647 }
648
Douglas Gregorcc636682009-02-17 23:15:12 +0000649 unsigned getIdentifierNamespace() const {
Chris Lattner769dbdf2009-03-27 20:18:19 +0000650 return IdentifierNamespace;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000651 }
Chris Lattnerd62fdc42009-01-06 07:16:40 +0000652 bool isInIdentifierNamespace(unsigned NS) const {
653 return getIdentifierNamespace() & NS;
654 }
Chris Lattner769dbdf2009-03-27 20:18:19 +0000655 static unsigned getIdentifierNamespaceForKind(Kind DK);
656
John McCall0d6b1642010-04-23 18:46:30 +0000657 bool hasTagIdentifierNamespace() const {
658 return isTagIdentifierNamespace(getIdentifierNamespace());
659 }
660 static bool isTagIdentifierNamespace(unsigned NS) {
661 // TagDecls have Tag and Type set and may also have TagFriend.
662 return (NS & ~IDNS_TagFriend) == (IDNS_Tag | IDNS_Type);
663 }
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000665 /// getLexicalDeclContext - The declaration context where this Decl was
666 /// lexically declared (LexicalDC). May be different from
667 /// getDeclContext() (SemanticDC).
668 /// e.g.:
669 ///
670 /// namespace A {
671 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
672 /// }
673 /// void A::f(); // SemanticDC == namespace 'A'
674 /// // LexicalDC == global namespace
Chris Lattner10d83792009-03-27 18:46:15 +0000675 DeclContext *getLexicalDeclContext() {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000676 if (isInSemaDC())
Chris Lattner10d83792009-03-27 18:46:15 +0000677 return getSemanticDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000678 return getMultipleDC()->LexicalDC;
679 }
Chris Lattner10d83792009-03-27 18:46:15 +0000680 const DeclContext *getLexicalDeclContext() const {
681 return const_cast<Decl*>(this)->getLexicalDeclContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000682 }
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000683
Stephen Hines176edba2014-12-01 14:53:08 -0800684 /// Determine whether this declaration is declared out of line (outside its
685 /// semantic context).
686 virtual bool isOutOfLine() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Douglas Gregor6ab35242009-04-09 21:40:53 +0000688 /// setDeclContext - Set both the semantic and lexical DeclContext
689 /// to DC.
690 void setDeclContext(DeclContext *DC);
691
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000692 void setLexicalDeclContext(DeclContext *DC);
693
Sebastian Redl7a126a42010-08-31 00:36:30 +0000694 /// isDefinedOutsideFunctionOrMethod - This predicate returns true if this
695 /// scoped decl is defined outside the current function or method. This is
696 /// roughly global variables and functions, but also handles enums (which
697 /// could be defined inside or outside a function etc).
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000698 bool isDefinedOutsideFunctionOrMethod() const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700699 return getParentFunctionOrMethod() == nullptr;
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000700 }
701
702 /// \brief If this decl is defined inside a function/method/block it returns
703 /// the corresponding DeclContext, otherwise it returns null.
704 const DeclContext *getParentFunctionOrMethod() const;
Argyrios Kyrtzidisfcc1e502011-09-28 18:14:24 +0000705 DeclContext *getParentFunctionOrMethod() {
706 return const_cast<DeclContext*>(
707 const_cast<const Decl*>(this)->getParentFunctionOrMethod());
708 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000709
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000710 /// \brief Retrieves the "canonical" declaration of the given declaration.
Douglas Gregorda2142f2011-02-19 18:51:44 +0000711 virtual Decl *getCanonicalDecl() { return this; }
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000712 const Decl *getCanonicalDecl() const {
713 return const_cast<Decl*>(this)->getCanonicalDecl();
714 }
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000715
Argyrios Kyrtzidisac4e3792009-07-18 08:50:48 +0000716 /// \brief Whether this particular Decl is a canonical one.
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000717 bool isCanonicalDecl() const { return getCanonicalDecl() == this; }
Douglas Gregorc896ad02011-12-14 21:44:45 +0000718
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000719protected:
720 /// \brief Returns the next redeclaration or itself if this is the only decl.
721 ///
722 /// Decl subclasses that can be redeclared should override this method so that
723 /// Decl::redecl_iterator can iterate over them.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700724 virtual Decl *getNextRedeclarationImpl() { return this; }
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000725
Douglas Gregoref96ee02012-01-14 16:38:05 +0000726 /// \brief Implementation of getPreviousDecl(), to be overridden by any
727 /// subclass that has a redeclaration chain.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700728 virtual Decl *getPreviousDeclImpl() { return nullptr; }
729
Douglas Gregoref96ee02012-01-14 16:38:05 +0000730 /// \brief Implementation of getMostRecentDecl(), to be overridden by any
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700731 /// subclass that has a redeclaration chain.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000732 virtual Decl *getMostRecentDeclImpl() { return this; }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700733
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000734public:
735 /// \brief Iterates through all the redeclarations of the same decl.
736 class redecl_iterator {
737 /// Current - The current declaration.
738 Decl *Current;
739 Decl *Starter;
740
741 public:
David Blaikie581deb32012-06-06 20:45:41 +0000742 typedef Decl *value_type;
743 typedef const value_type &reference;
744 typedef const value_type *pointer;
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000745 typedef std::forward_iterator_tag iterator_category;
David Blaikie581deb32012-06-06 20:45:41 +0000746 typedef std::ptrdiff_t difference_type;
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000747
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700748 redecl_iterator() : Current(nullptr) { }
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000749 explicit redecl_iterator(Decl *C) : Current(C), Starter(C) { }
750
David Blaikie581deb32012-06-06 20:45:41 +0000751 reference operator*() const { return Current; }
752 value_type operator->() const { return Current; }
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000753
754 redecl_iterator& operator++() {
755 assert(Current && "Advancing while iterator has reached end");
756 // Get either previous decl or latest decl.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700757 Decl *Next = Current->getNextRedeclarationImpl();
Argyrios Kyrtzidis22cbd2b2009-07-21 00:06:27 +0000758 assert(Next && "Should return next redeclaration or itself, never null!");
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700759 Current = (Next != Starter) ? Next : nullptr;
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000760 return *this;
761 }
762
763 redecl_iterator operator++(int) {
764 redecl_iterator tmp(*this);
765 ++(*this);
766 return tmp;
767 }
768
Mike Stump1eb44332009-09-09 15:08:12 +0000769 friend bool operator==(redecl_iterator x, redecl_iterator y) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000770 return x.Current == y.Current;
771 }
Mike Stump1eb44332009-09-09 15:08:12 +0000772 friend bool operator!=(redecl_iterator x, redecl_iterator y) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000773 return x.Current != y.Current;
774 }
775 };
776
Stephen Hines651f13c2014-04-23 16:59:28 -0700777 typedef llvm::iterator_range<redecl_iterator> redecl_range;
778
779 /// \brief Returns an iterator range for all the redeclarations of the same
780 /// decl. It will iterate at least once (when this decl is the only one).
781 redecl_range redecls() const {
782 return redecl_range(redecls_begin(), redecls_end());
783 }
784
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000785 redecl_iterator redecls_begin() const {
Stephen Hines651f13c2014-04-23 16:59:28 -0700786 return redecl_iterator(const_cast<Decl *>(this));
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000787 }
788 redecl_iterator redecls_end() const { return redecl_iterator(); }
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000789
Douglas Gregorf785a7d2012-01-14 15:55:47 +0000790 /// \brief Retrieve the previous declaration that declares the same entity
791 /// as this declaration, or NULL if there is no previous declaration.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000792 Decl *getPreviousDecl() { return getPreviousDeclImpl(); }
793
794 /// \brief Retrieve the most recent declaration that declares the same entity
795 /// as this declaration, or NULL if there is no previous declaration.
796 const Decl *getPreviousDecl() const {
797 return const_cast<Decl *>(this)->getPreviousDeclImpl();
798 }
Rafael Espindola7693b322013-10-19 02:13:21 +0000799
Rafael Espindolae6ddd7f2013-10-19 16:14:07 +0000800 /// \brief True if this is the first declaration in its redeclaration chain.
Rafael Espindola7693b322013-10-19 02:13:21 +0000801 bool isFirstDecl() const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700802 return getPreviousDecl() == nullptr;
Rafael Espindola7693b322013-10-19 02:13:21 +0000803 }
804
Douglas Gregorf785a7d2012-01-14 15:55:47 +0000805 /// \brief Retrieve the most recent declaration that declares the same entity
806 /// as this declaration (which may be this declaration).
Douglas Gregoref96ee02012-01-14 16:38:05 +0000807 Decl *getMostRecentDecl() { return getMostRecentDeclImpl(); }
808
809 /// \brief Retrieve the most recent declaration that declares the same entity
810 /// as this declaration (which may be this declaration).
811 const Decl *getMostRecentDecl() const {
812 return const_cast<Decl *>(this)->getMostRecentDeclImpl();
813 }
814
Sebastian Redld3a413d2009-04-26 20:35:05 +0000815 /// getBody - If this Decl represents a declaration for a body of code,
816 /// such as a function or method definition, this method returns the
817 /// top-level Stmt* of that body. Otherwise this method returns null.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700818 virtual Stmt* getBody() const { return nullptr; }
Sebastian Redld3a413d2009-04-26 20:35:05 +0000819
Manuel Klimeka438e682013-06-13 09:05:19 +0000820 /// \brief Returns true if this \c Decl represents a declaration for a body of
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000821 /// code, such as a function or method definition.
Manuel Klimeka438e682013-06-13 09:05:19 +0000822 /// Note that \c hasBody can also return true if any redeclaration of this
823 /// \c Decl represents a declaration for a body of code.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700824 virtual bool hasBody() const { return getBody() != nullptr; }
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000825
Sebastian Redld3a413d2009-04-26 20:35:05 +0000826 /// getBodyRBrace - Gets the right brace of the body, if a body exists.
827 /// This works whether the body is a CompoundStmt or a CXXTryStmt.
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000828 SourceLocation getBodyRBrace() const;
Sebastian Redld3a413d2009-04-26 20:35:05 +0000829
Chris Lattner0ed844b2008-04-04 06:12:32 +0000830 // global temp stats (until we have a per-module visitor)
Sean Hunt9a555912010-05-30 07:21:58 +0000831 static void add(Kind k);
Daniel Dunbar02892a62012-03-05 21:42:49 +0000832 static void EnableStatistics();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000833 static void PrintStats();
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Anders Carlsson67e33202009-06-13 00:08:58 +0000835 /// isTemplateParameter - Determines whether this declaration is a
Douglas Gregorf57172b2008-12-08 18:40:42 +0000836 /// template parameter.
837 bool isTemplateParameter() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Anders Carlsson67e33202009-06-13 00:08:58 +0000839 /// isTemplateParameter - Determines whether this declaration is a
840 /// template parameter pack.
841 bool isTemplateParameterPack() const;
Douglas Gregorf57172b2008-12-08 18:40:42 +0000842
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000843 /// \brief Whether this declaration is a parameter pack.
844 bool isParameterPack() const;
David Blaikieba243b52011-11-09 06:07:30 +0000845
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000846 /// \brief returns true if this declaration is a template
847 bool isTemplateDecl() const;
848
Douglas Gregore53060f2009-06-25 22:08:12 +0000849 /// \brief Whether this declaration is a function or function template.
Stephen Hines651f13c2014-04-23 16:59:28 -0700850 bool isFunctionOrFunctionTemplate() const {
851 return (DeclKind >= Decl::firstFunction &&
852 DeclKind <= Decl::lastFunction) ||
853 DeclKind == FunctionTemplate;
854 }
855
856 /// \brief Returns the function itself, or the templated function if this is a
857 /// function template.
858 FunctionDecl *getAsFunction() LLVM_READONLY;
859
860 const FunctionDecl *getAsFunction() const {
861 return const_cast<Decl *>(this)->getAsFunction();
862 }
John McCall02cace72009-08-28 07:59:38 +0000863
864 /// \brief Changes the namespace of this declaration to reflect that it's
Richard Smitha41c97a2013-09-20 01:15:31 +0000865 /// a function-local extern declaration.
866 ///
867 /// These declarations appear in the lexical context of the extern
868 /// declaration, but in the semantic context of the enclosing namespace
869 /// scope.
870 void setLocalExternDecl() {
871 assert((IdentifierNamespace == IDNS_Ordinary ||
872 IdentifierNamespace == IDNS_OrdinaryFriend) &&
873 "namespace is not ordinary");
874
875 Decl *Prev = getPreviousDecl();
876 IdentifierNamespace &= ~IDNS_Ordinary;
877
878 IdentifierNamespace |= IDNS_LocalExtern;
879 if (Prev && Prev->getIdentifierNamespace() & IDNS_Ordinary)
880 IdentifierNamespace |= IDNS_Ordinary;
881 }
882
883 /// \brief Determine whether this is a block-scope declaration with linkage.
884 /// This will either be a local variable declaration declared 'extern', or a
885 /// local function declaration.
886 bool isLocalExternDecl() {
887 return IdentifierNamespace & IDNS_LocalExtern;
888 }
889
890 /// \brief Changes the namespace of this declaration to reflect that it's
John McCall02cace72009-08-28 07:59:38 +0000891 /// the object of a friend declaration.
892 ///
893 /// These declarations appear in the lexical context of the friending
894 /// class, but in the semantic context of the actual entity. This property
895 /// applies only to a specific decl object; other redeclarations of the
896 /// same entity may not (and probably don't) share this property.
Richard Smith22050f22013-07-17 23:53:16 +0000897 void setObjectOfFriendDecl(bool PerformFriendInjection = false) {
John McCall02cace72009-08-28 07:59:38 +0000898 unsigned OldNS = IdentifierNamespace;
John McCallb0cb0222010-03-27 05:57:59 +0000899 assert((OldNS & (IDNS_Tag | IDNS_Ordinary |
Richard Smitha41c97a2013-09-20 01:15:31 +0000900 IDNS_TagFriend | IDNS_OrdinaryFriend |
901 IDNS_LocalExtern)) &&
John McCallb0cb0222010-03-27 05:57:59 +0000902 "namespace includes neither ordinary nor tag");
John McCall0d6b1642010-04-23 18:46:30 +0000903 assert(!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type |
Richard Smitha41c97a2013-09-20 01:15:31 +0000904 IDNS_TagFriend | IDNS_OrdinaryFriend |
905 IDNS_LocalExtern)) &&
John McCallb0cb0222010-03-27 05:57:59 +0000906 "namespace includes other than ordinary or tag");
John McCall02cace72009-08-28 07:59:38 +0000907
Richard Smith22050f22013-07-17 23:53:16 +0000908 Decl *Prev = getPreviousDecl();
Richard Smitha41c97a2013-09-20 01:15:31 +0000909 IdentifierNamespace &= ~(IDNS_Ordinary | IDNS_Tag | IDNS_Type);
910
John McCallb0cb0222010-03-27 05:57:59 +0000911 if (OldNS & (IDNS_Tag | IDNS_TagFriend)) {
John McCall02cace72009-08-28 07:59:38 +0000912 IdentifierNamespace |= IDNS_TagFriend;
Richard Smitha41c97a2013-09-20 01:15:31 +0000913 if (PerformFriendInjection ||
Richard Smith22050f22013-07-17 23:53:16 +0000914 (Prev && Prev->getIdentifierNamespace() & IDNS_Tag))
915 IdentifierNamespace |= IDNS_Tag | IDNS_Type;
John McCallb0cb0222010-03-27 05:57:59 +0000916 }
917
Richard Smitha41c97a2013-09-20 01:15:31 +0000918 if (OldNS & (IDNS_Ordinary | IDNS_OrdinaryFriend | IDNS_LocalExtern)) {
John McCall02cace72009-08-28 07:59:38 +0000919 IdentifierNamespace |= IDNS_OrdinaryFriend;
Richard Smith22050f22013-07-17 23:53:16 +0000920 if (PerformFriendInjection ||
921 (Prev && Prev->getIdentifierNamespace() & IDNS_Ordinary))
922 IdentifierNamespace |= IDNS_Ordinary;
John McCallb0cb0222010-03-27 05:57:59 +0000923 }
John McCall02cace72009-08-28 07:59:38 +0000924 }
925
926 enum FriendObjectKind {
David Majnemer7841abf2013-06-26 21:28:41 +0000927 FOK_None, ///< Not a friend object.
928 FOK_Declared, ///< A friend of a previously-declared entity.
929 FOK_Undeclared ///< A friend of a previously-undeclared entity.
John McCall02cace72009-08-28 07:59:38 +0000930 };
931
932 /// \brief Determines whether this declaration is the object of a
933 /// friend declaration and, if so, what kind.
934 ///
935 /// There is currently no direct way to find the associated FriendDecl.
936 FriendObjectKind getFriendObjectKind() const {
David Majnemer2946ceb2013-06-23 03:38:10 +0000937 unsigned mask =
938 (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend));
John McCall02cace72009-08-28 07:59:38 +0000939 if (!mask) return FOK_None;
David Majnemer2946ceb2013-06-23 03:38:10 +0000940 return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ? FOK_Declared
941 : FOK_Undeclared);
John McCall02cace72009-08-28 07:59:38 +0000942 }
Mike Stump1eb44332009-09-09 15:08:12 +0000943
John McCall76d32642010-04-24 01:30:58 +0000944 /// Specifies that this declaration is a C++ overloaded non-member.
945 void setNonMemberOperator() {
946 assert(getKind() == Function || getKind() == FunctionTemplate);
947 assert((IdentifierNamespace & IDNS_Ordinary) &&
948 "visible non-member operators should be in ordinary namespace");
949 IdentifierNamespace |= IDNS_NonMemberOperator;
950 }
951
John McCall80cd64a2010-01-29 01:45:37 +0000952 static bool classofKind(Kind K) { return true; }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000953 static DeclContext *castToDeclContext(const Decl *);
954 static Decl *castFromDeclContext(const DeclContext *);
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Richard Trieu5cb3d692011-07-28 00:19:05 +0000956 void print(raw_ostream &Out, unsigned Indentation = 0,
957 bool PrintInstantiation = false) const;
Chris Lattner8cc488f2011-07-20 07:06:53 +0000958 void print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu5cb3d692011-07-28 00:19:05 +0000959 unsigned Indentation = 0, bool PrintInstantiation = false) const;
Eli Friedman42f42c02009-05-30 04:20:30 +0000960 static void printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner8cc488f2011-07-20 07:06:53 +0000961 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000962 unsigned Indentation = 0);
Alexander Kornienko559b9282012-07-26 17:11:45 +0000963 // Debuggers don't usually respect default arguments.
Stephen Hines651f13c2014-04-23 16:59:28 -0700964 void dump() const;
Richard Trieu7ba443a2013-01-26 01:31:20 +0000965 // Same as dump(), but forces color printing.
Stephen Hines651f13c2014-04-23 16:59:28 -0700966 void dumpColor() const;
Alexander Kornienko559b9282012-07-26 17:11:45 +0000967 void dump(raw_ostream &Out) const;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000968
Stephen Hines651f13c2014-04-23 16:59:28 -0700969 /// \brief Looks through the Decl's underlying type to extract a FunctionType
970 /// when possible. Will return null if the type underlying the Decl does not
971 /// have a FunctionType.
972 const FunctionType *getFunctionType(bool BlocksToo = true) const;
973
Chris Lattner81abbdd2009-03-21 06:27:31 +0000974private:
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000975 void setAttrsImpl(const AttrVec& Attrs, ASTContext &Ctx);
976 void setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
977 ASTContext &Ctx);
Chris Lattner81abbdd2009-03-21 06:27:31 +0000978
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000979protected:
980 ASTMutationListener *getASTMutationListener() const;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000981};
982
Douglas Gregoreaa95112011-12-14 23:59:32 +0000983/// \brief Determine whether two declarations declare the same entity.
984inline bool declaresSameEntity(const Decl *D1, const Decl *D2) {
Douglas Gregoreaa95112011-12-14 23:59:32 +0000985 if (!D1 || !D2)
986 return false;
987
Douglas Gregordec1cc42011-12-15 17:15:07 +0000988 if (D1 == D2)
989 return true;
990
Douglas Gregoreaa95112011-12-14 23:59:32 +0000991 return D1->getCanonicalDecl() == D2->getCanonicalDecl();
992}
993
Chris Lattner49f28ca2009-03-05 08:00:35 +0000994/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when
995/// doing something to a specific decl.
996class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
Daniel Dunbarc5236562009-11-21 09:05:59 +0000997 const Decl *TheDecl;
Chris Lattner49f28ca2009-03-05 08:00:35 +0000998 SourceLocation Loc;
999 SourceManager &SM;
1000 const char *Message;
1001public:
Daniel Dunbarc5236562009-11-21 09:05:59 +00001002 PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L,
Chris Lattner49f28ca2009-03-05 08:00:35 +00001003 SourceManager &sm, const char *Msg)
1004 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Stephen Hines651f13c2014-04-23 16:59:28 -07001006 void print(raw_ostream &OS) const override;
Mike Stump1eb44332009-09-09 15:08:12 +00001007};
1008
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001009/// \brief The results of name lookup within a DeclContext. This is either a
1010/// single result (with no stable storage) or a collection of results (with
1011/// stable storage provided by the lookup table).
1012class DeclContextLookupResult {
1013 typedef ArrayRef<NamedDecl *> ResultTy;
1014 ResultTy Result;
1015 // If there is only one lookup result, it would be invalidated by
1016 // reallocations of the name table, so store it separately.
1017 NamedDecl *Single;
John McCallea318642010-08-26 09:15:37 +00001018
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001019 static NamedDecl *const SingleElementDummyList;
1020
1021public:
1022 DeclContextLookupResult() : Result(), Single() {}
1023 DeclContextLookupResult(ArrayRef<NamedDecl *> Result)
1024 : Result(Result), Single() {}
1025 DeclContextLookupResult(NamedDecl *Single)
1026 : Result(SingleElementDummyList), Single(Single) {}
1027
1028 class iterator;
1029 typedef llvm::iterator_adaptor_base<iterator, ResultTy::iterator,
1030 std::random_access_iterator_tag,
1031 NamedDecl *const> IteratorBase;
1032 class iterator : public IteratorBase {
1033 value_type SingleElement;
1034
1035 public:
1036 iterator() : IteratorBase(), SingleElement() {}
1037 explicit iterator(pointer Pos, value_type Single = nullptr)
1038 : IteratorBase(Pos), SingleElement(Single) {}
1039
1040 reference operator*() const {
1041 return SingleElement ? SingleElement : IteratorBase::operator*();
1042 }
1043 };
1044 typedef iterator const_iterator;
1045 typedef iterator::pointer pointer;
1046 typedef iterator::reference reference;
1047
1048 iterator begin() const { return iterator(Result.begin(), Single); }
1049 iterator end() const { return iterator(Result.end(), Single); }
1050
1051 bool empty() const { return Result.empty(); }
1052 pointer data() const { return Single ? &Single : Result.data(); }
1053 size_t size() const { return Single ? 1 : Result.size(); }
1054 reference front() const { return Single ? Single : Result.front(); }
1055 reference back() const { return Single ? Single : Result.back(); }
1056 reference operator[](size_t N) const { return Single ? Single : Result[N]; }
1057
1058 // FIXME: Remove this from the interface
1059 DeclContextLookupResult slice(size_t N) const {
1060 DeclContextLookupResult Sliced = Result.slice(N);
1061 Sliced.Single = Single;
1062 return Sliced;
1063 }
1064};
Chris Lattner49f28ca2009-03-05 08:00:35 +00001065
Chris Lattnerb048c982008-04-06 04:47:34 +00001066/// DeclContext - This is used only as base class of specific decl types that
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +00001067/// can act as declaration contexts. These decls are (only the top classes
1068/// that directly derive from DeclContext are mentioned, not their subclasses):
Chris Lattner0ed844b2008-04-04 06:12:32 +00001069///
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00001070/// TranslationUnitDecl
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00001071/// NamespaceDecl
Chris Lattner0ed844b2008-04-04 06:12:32 +00001072/// FunctionDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +00001073/// TagDecl
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +00001074/// ObjCMethodDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +00001075/// ObjCContainerDecl
Douglas Gregor074149e2009-01-05 19:45:36 +00001076/// LinkageSpecDecl
Steve Naroff56ee6892008-10-08 17:01:13 +00001077/// BlockDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +00001078///
Chris Lattnerb048c982008-04-06 04:47:34 +00001079class DeclContext {
Chris Lattner0ed844b2008-04-04 06:12:32 +00001080 /// DeclKind - This indicates which class this is.
John McCall35043e52010-10-19 05:43:52 +00001081 unsigned DeclKind : 8;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001082
Douglas Gregor2cf26342009-04-09 22:27:44 +00001083 /// \brief Whether this declaration context also has some external
1084 /// storage that contains additional declarations that are lexically
1085 /// part of this context.
Richard Smithbbcd0f32013-02-07 03:37:08 +00001086 mutable bool ExternalLexicalStorage : 1;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001087
1088 /// \brief Whether this declaration context also has some external
1089 /// storage that contains additional declarations that are visible
1090 /// in this context.
Richard Smithbbcd0f32013-02-07 03:37:08 +00001091 mutable bool ExternalVisibleStorage : 1;
1092
1093 /// \brief Whether this declaration context has had external visible
1094 /// storage added since the last lookup. In this case, \c LookupPtr's
1095 /// invariant may not hold and needs to be fixed before we perform
1096 /// another lookup.
1097 mutable bool NeedToReconcileExternalVisibleStorage : 1;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001098
Douglas Gregorc36c5402009-04-09 17:29:08 +00001099 /// \brief Pointer to the data structure used to lookup declarations
John McCall0c01d182010-03-24 05:22:00 +00001100 /// within this context (or a DependentStoredDeclsMap if this is a
Richard Smithc5d3e802012-03-16 06:12:59 +00001101 /// dependent context), and a bool indicating whether we have lazily
1102 /// omitted any declarations from the map. We maintain the invariant
Richard Smithbbcd0f32013-02-07 03:37:08 +00001103 /// that, if the map contains an entry for a DeclarationName (and we
1104 /// haven't lazily omitted anything), then it contains all relevant
1105 /// entries for that name.
Richard Smithc5d3e802012-03-16 06:12:59 +00001106 mutable llvm::PointerIntPair<StoredDeclsMap*, 1, bool> LookupPtr;
Douglas Gregor44b43212008-12-11 16:49:14 +00001107
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001108protected:
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001109 /// FirstDecl - The first declaration stored within this declaration
1110 /// context.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001111 mutable Decl *FirstDecl;
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001112
1113 /// LastDecl - The last declaration stored within this declaration
1114 /// context. FIXME: We could probably cache this value somewhere
1115 /// outside of the DeclContext, to reduce the size of DeclContext by
1116 /// another pointer.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001117 mutable Decl *LastDecl;
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +00001118
John McCall76bd1f32010-06-01 09:23:16 +00001119 friend class ExternalASTSource;
Richard Smithe7bae152013-07-13 02:00:19 +00001120 friend class ASTDeclReader;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00001121 friend class ASTWriter;
John McCall76bd1f32010-06-01 09:23:16 +00001122
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001123 /// \brief Build up a chain of declarations.
1124 ///
1125 /// \returns the first/last pair of declarations.
1126 static std::pair<Decl *, Decl *>
Bill Wendling341785e2012-02-22 09:51:33 +00001127 BuildDeclChain(ArrayRef<Decl*> Decls, bool FieldsAlreadyLoaded);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001128
Richard Smithbbcd0f32013-02-07 03:37:08 +00001129 DeclContext(Decl::Kind K)
1130 : DeclKind(K), ExternalLexicalStorage(false),
1131 ExternalVisibleStorage(false),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001132 NeedToReconcileExternalVisibleStorage(false), LookupPtr(nullptr, false),
1133 FirstDecl(nullptr), LastDecl(nullptr) {}
Douglas Gregor44b43212008-12-11 16:49:14 +00001134
Chris Lattner0ed844b2008-04-04 06:12:32 +00001135public:
Douglas Gregor44b43212008-12-11 16:49:14 +00001136 ~DeclContext();
1137
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +00001138 Decl::Kind getDeclKind() const {
John McCall35043e52010-10-19 05:43:52 +00001139 return static_cast<Decl::Kind>(DeclKind);
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +00001140 }
Steve Naroff0a473932009-01-20 19:53:53 +00001141 const char *getDeclKindName() const;
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +00001142
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +00001143 /// getParent - Returns the containing DeclContext.
Chris Lattner0cf2b192009-03-27 19:19:59 +00001144 DeclContext *getParent() {
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +00001145 return cast<Decl>(this)->getDeclContext();
1146 }
Chris Lattner0cf2b192009-03-27 19:19:59 +00001147 const DeclContext *getParent() const {
1148 return const_cast<DeclContext*>(this)->getParent();
Argyrios Kyrtzidisd2595ec2008-10-12 18:40:01 +00001149 }
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +00001151 /// getLexicalParent - Returns the containing lexical DeclContext. May be
Douglas Gregor44b43212008-12-11 16:49:14 +00001152 /// different from getParent, e.g.:
1153 ///
1154 /// namespace A {
1155 /// struct S;
1156 /// }
1157 /// struct A::S {}; // getParent() == namespace 'A'
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +00001158 /// // getLexicalParent() == translation unit
1159 ///
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +00001160 DeclContext *getLexicalParent() {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001161 return cast<Decl>(this)->getLexicalDeclContext();
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +00001162 }
Chris Lattner0cf2b192009-03-27 19:19:59 +00001163 const DeclContext *getLexicalParent() const {
1164 return const_cast<DeclContext*>(this)->getLexicalParent();
Mike Stump1eb44332009-09-09 15:08:12 +00001165 }
Argyrios Kyrtzidis048f30a2009-06-30 02:35:38 +00001166
Douglas Gregore942bbe2009-09-10 16:57:35 +00001167 DeclContext *getLookupParent();
David Blaikieba243b52011-11-09 06:07:30 +00001168
Douglas Gregore942bbe2009-09-10 16:57:35 +00001169 const DeclContext *getLookupParent() const {
1170 return const_cast<DeclContext*>(this)->getLookupParent();
1171 }
David Blaikieba243b52011-11-09 06:07:30 +00001172
Argyrios Kyrtzidis048f30a2009-06-30 02:35:38 +00001173 ASTContext &getParentASTContext() const {
1174 return cast<Decl>(this)->getASTContext();
1175 }
1176
John McCallaab9e312011-02-22 22:25:23 +00001177 bool isClosure() const {
1178 return DeclKind == Decl::Block;
1179 }
1180
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001181 bool isObjCContainer() const {
Fariborz Jahanian80f77532011-08-22 17:13:51 +00001182 switch (DeclKind) {
1183 case Decl::ObjCCategory:
1184 case Decl::ObjCCategoryImpl:
1185 case Decl::ObjCImplementation:
1186 case Decl::ObjCInterface:
1187 case Decl::ObjCProtocol:
1188 return true;
1189 }
1190 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001191 }
1192
Chris Lattner0ed844b2008-04-04 06:12:32 +00001193 bool isFunctionOrMethod() const {
1194 switch (DeclKind) {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001195 case Decl::Block:
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001196 case Decl::Captured:
Chris Lattner0cf2b192009-03-27 19:19:59 +00001197 case Decl::ObjCMethod:
1198 return true;
1199 default:
Sean Hunt9a555912010-05-30 07:21:58 +00001200 return DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001201 }
1202 }
1203
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +00001204 bool isFileContext() const {
1205 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
1206 }
1207
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001208 bool isTranslationUnit() const {
1209 return DeclKind == Decl::TranslationUnit;
1210 }
1211
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001212 bool isRecord() const {
Sean Hunt9a555912010-05-30 07:21:58 +00001213 return DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord;
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001214 }
1215
Douglas Gregor44b43212008-12-11 16:49:14 +00001216 bool isNamespace() const {
1217 return DeclKind == Decl::Namespace;
1218 }
1219
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001220 bool isStdNamespace() const;
1221
Sebastian Redl410c4f22010-08-31 20:53:31 +00001222 bool isInlineNamespace() const;
1223
Douglas Gregorbc221632009-05-28 16:34:51 +00001224 /// \brief Determines whether this context is dependent on a
1225 /// template parameter.
1226 bool isDependentContext() const;
1227
Douglas Gregor074149e2009-01-05 19:45:36 +00001228 /// isTransparentContext - Determines whether this context is a
1229 /// "transparent" context, meaning that the members declared in this
1230 /// context are semantically declared in the nearest enclosing
1231 /// non-transparent (opaque) context but are lexically declared in
1232 /// this context. For example, consider the enumerators of an
Mike Stump1eb44332009-09-09 15:08:12 +00001233 /// enumeration type:
Douglas Gregor074149e2009-01-05 19:45:36 +00001234 /// @code
1235 /// enum E {
Mike Stump1eb44332009-09-09 15:08:12 +00001236 /// Val1
Douglas Gregor074149e2009-01-05 19:45:36 +00001237 /// };
1238 /// @endcode
1239 /// Here, E is a transparent context, so its enumerator (Val1) will
1240 /// appear (semantically) that it is in the same context of E.
1241 /// Examples of transparent contexts include: enumerations (except for
Sebastian Redl410c4f22010-08-31 20:53:31 +00001242 /// C++0x scoped enums), and C++ linkage specifications.
Douglas Gregor074149e2009-01-05 19:45:36 +00001243 bool isTransparentContext() const;
1244
Serge Pavlov142ab062013-11-14 02:13:03 +00001245 /// \brief Determines whether this context or some of its ancestors is a
1246 /// linkage specification context that specifies C linkage.
1247 bool isExternCContext() const;
1248
1249 /// \brief Determines whether this context or some of its ancestors is a
1250 /// linkage specification context that specifies C++ linkage.
1251 bool isExternCXXContext() const;
1252
Douglas Gregor61481da2009-09-01 17:22:34 +00001253 /// \brief Determine whether this declaration context is equivalent
1254 /// to the declaration context DC.
Sebastian Redl7a126a42010-08-31 00:36:30 +00001255 bool Equals(const DeclContext *DC) const {
Douglas Gregordbdf5e72010-03-15 15:26:48 +00001256 return DC && this->getPrimaryContext() == DC->getPrimaryContext();
Douglas Gregor61481da2009-09-01 17:22:34 +00001257 }
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Douglas Gregor6dd38da2009-08-27 06:03:53 +00001259 /// \brief Determine whether this declaration context encloses the
1260 /// declaration context DC.
Sebastian Redl7a126a42010-08-31 00:36:30 +00001261 bool Encloses(const DeclContext *DC) const;
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +00001262
John McCall4b9c2d22011-11-06 09:01:30 +00001263 /// \brief Find the nearest non-closure ancestor of this context,
1264 /// i.e. the innermost semantic parent of this context which is not
1265 /// a closure. A context may be its own non-closure ancestor.
John McCallf5ebf9b2013-05-03 07:33:41 +00001266 Decl *getNonClosureAncestor();
1267 const Decl *getNonClosureAncestor() const {
John McCall4b9c2d22011-11-06 09:01:30 +00001268 return const_cast<DeclContext*>(this)->getNonClosureAncestor();
1269 }
1270
Douglas Gregor44b43212008-12-11 16:49:14 +00001271 /// getPrimaryContext - There may be many different
1272 /// declarations of the same entity (including forward declarations
1273 /// of classes, multiple definitions of namespaces, etc.), each with
1274 /// a different set of declarations. This routine returns the
1275 /// "primary" DeclContext structure, which will contain the
1276 /// information needed to perform name lookup into this context.
Steve Naroff0701bbb2009-01-08 17:28:14 +00001277 DeclContext *getPrimaryContext();
John McCall0c01d182010-03-24 05:22:00 +00001278 const DeclContext *getPrimaryContext() const {
1279 return const_cast<DeclContext*>(this)->getPrimaryContext();
1280 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001281
Sebastian Redl7a126a42010-08-31 00:36:30 +00001282 /// getRedeclContext - Retrieve the context in which an entity conflicts with
1283 /// other entities of the same name, or where it is a redeclaration if the
Sebastian Redl410c4f22010-08-31 20:53:31 +00001284 /// two entities are compatible. This skips through transparent contexts.
Sebastian Redl7a126a42010-08-31 00:36:30 +00001285 DeclContext *getRedeclContext();
1286 const DeclContext *getRedeclContext() const {
1287 return const_cast<DeclContext *>(this)->getRedeclContext();
Douglas Gregor17a9b9e2009-01-07 02:48:43 +00001288 }
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Douglas Gregor88b70942009-02-25 22:02:03 +00001290 /// \brief Retrieve the nearest enclosing namespace context.
1291 DeclContext *getEnclosingNamespaceContext();
1292 const DeclContext *getEnclosingNamespaceContext() const {
1293 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
1294 }
1295
Stephen Hines176edba2014-12-01 14:53:08 -08001296 /// \brief Retrieve the outermost lexically enclosing record context.
1297 RecordDecl *getOuterLexicalRecordContext();
1298 const RecordDecl *getOuterLexicalRecordContext() const {
1299 return const_cast<DeclContext *>(this)->getOuterLexicalRecordContext();
1300 }
1301
Sebastian Redl7a126a42010-08-31 00:36:30 +00001302 /// \brief Test if this context is part of the enclosing namespace set of
1303 /// the context NS, as defined in C++0x [namespace.def]p9. If either context
1304 /// isn't a namespace, this is equivalent to Equals().
1305 ///
1306 /// The enclosing namespace set of a namespace is the namespace and, if it is
1307 /// inline, its enclosing namespace, recursively.
1308 bool InEnclosingNamespaceSetOf(const DeclContext *NS) const;
1309
James Dennett18483f52012-06-15 08:37:50 +00001310 /// \brief Collects all of the declaration contexts that are semantically
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001311 /// connected to this declaration context.
1312 ///
1313 /// For declaration contexts that have multiple semantically connected but
1314 /// syntactically distinct contexts, such as C++ namespaces, this routine
1315 /// retrieves the complete set of such declaration contexts in source order.
1316 /// For example, given:
1317 ///
1318 /// \code
Douglas Gregor44b43212008-12-11 16:49:14 +00001319 /// namespace N {
1320 /// int x;
1321 /// }
1322 /// namespace N {
1323 /// int y;
1324 /// }
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001325 /// \endcode
1326 ///
1327 /// The \c Contexts parameter will contain both definitions of N.
1328 ///
1329 /// \param Contexts Will be cleared and set to the set of declaration
1330 /// contexts that are semanticaly connected to this declaration context,
1331 /// in source order, including this context (which may be the only result,
1332 /// for non-namespace contexts).
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001333 void collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts);
Douglas Gregor44b43212008-12-11 16:49:14 +00001334
1335 /// decl_iterator - Iterates through the declarations stored
1336 /// within this context.
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001337 class decl_iterator {
1338 /// Current - The current declaration.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001339 Decl *Current;
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001340
1341 public:
David Blaikie581deb32012-06-06 20:45:41 +00001342 typedef Decl *value_type;
1343 typedef const value_type &reference;
1344 typedef const value_type *pointer;
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001345 typedef std::forward_iterator_tag iterator_category;
1346 typedef std::ptrdiff_t difference_type;
1347
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001348 decl_iterator() : Current(nullptr) { }
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001349 explicit decl_iterator(Decl *C) : Current(C) { }
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001350
1351 reference operator*() const { return Current; }
David Blaikie581deb32012-06-06 20:45:41 +00001352 // This doesn't meet the iterator requirements, but it's convenient
1353 value_type operator->() const { return Current; }
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001354
Chris Lattner96f44682009-03-28 05:59:45 +00001355 decl_iterator& operator++() {
Chris Lattner244a67d2009-03-28 06:04:26 +00001356 Current = Current->getNextDeclInContext();
Chris Lattner96f44682009-03-28 05:59:45 +00001357 return *this;
1358 }
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001359
1360 decl_iterator operator++(int) {
1361 decl_iterator tmp(*this);
1362 ++(*this);
1363 return tmp;
1364 }
1365
Mike Stump1eb44332009-09-09 15:08:12 +00001366 friend bool operator==(decl_iterator x, decl_iterator y) {
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001367 return x.Current == y.Current;
1368 }
Mike Stump1eb44332009-09-09 15:08:12 +00001369 friend bool operator!=(decl_iterator x, decl_iterator y) {
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001370 return x.Current != y.Current;
1371 }
1372 };
Douglas Gregor44b43212008-12-11 16:49:14 +00001373
Stephen Hines651f13c2014-04-23 16:59:28 -07001374 typedef llvm::iterator_range<decl_iterator> decl_range;
1375
Douglas Gregor44b43212008-12-11 16:49:14 +00001376 /// decls_begin/decls_end - Iterate over the declarations stored in
Mike Stump1eb44332009-09-09 15:08:12 +00001377 /// this context.
Stephen Hines651f13c2014-04-23 16:59:28 -07001378 decl_range decls() const { return decl_range(decls_begin(), decls_end()); }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001379 decl_iterator decls_begin() const;
David Blaikie581deb32012-06-06 20:45:41 +00001380 decl_iterator decls_end() const { return decl_iterator(); }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001381 bool decls_empty() const;
Douglas Gregor44b43212008-12-11 16:49:14 +00001382
Sebastian Redl681d7232010-07-27 00:17:23 +00001383 /// noload_decls_begin/end - Iterate over the declarations stored in this
1384 /// context that are currently loaded; don't attempt to retrieve anything
1385 /// from an external source.
Stephen Hines651f13c2014-04-23 16:59:28 -07001386 decl_range noload_decls() const {
1387 return decl_range(noload_decls_begin(), noload_decls_end());
1388 }
1389 decl_iterator noload_decls_begin() const { return decl_iterator(FirstDecl); }
David Blaikie581deb32012-06-06 20:45:41 +00001390 decl_iterator noload_decls_end() const { return decl_iterator(); }
Sebastian Redl681d7232010-07-27 00:17:23 +00001391
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001392 /// specific_decl_iterator - Iterates over a subrange of
1393 /// declarations stored in a DeclContext, providing only those that
Douglas Gregor669c9a22009-02-02 18:25:48 +00001394 /// are of type SpecificDecl (or a class derived from it). This
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001395 /// iterator is used, for example, to provide iteration over just
Douglas Gregor669c9a22009-02-02 18:25:48 +00001396 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001397 template<typename SpecificDecl>
1398 class specific_decl_iterator {
1399 /// Current - The current, underlying declaration iterator, which
Douglas Gregord6f0b4e2009-02-02 17:56:05 +00001400 /// will either be NULL or will point to a declaration of
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001401 /// type SpecificDecl.
1402 DeclContext::decl_iterator Current;
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001404 /// SkipToNextDecl - Advances the current position up to the next
1405 /// declaration of type SpecificDecl that also meets the criteria
1406 /// required by Acceptable.
1407 void SkipToNextDecl() {
Douglas Gregor669c9a22009-02-02 18:25:48 +00001408 while (*Current && !isa<SpecificDecl>(*Current))
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001409 ++Current;
1410 }
1411
1412 public:
David Blaikie581deb32012-06-06 20:45:41 +00001413 typedef SpecificDecl *value_type;
1414 // TODO: Add reference and pointer typedefs (with some appropriate proxy
1415 // type) if we ever have a need for them.
1416 typedef void reference;
1417 typedef void pointer;
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001418 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1419 difference_type;
1420 typedef std::forward_iterator_tag iterator_category;
1421
Douglas Gregor669c9a22009-02-02 18:25:48 +00001422 specific_decl_iterator() : Current() { }
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001423
1424 /// specific_decl_iterator - Construct a new iterator over a
Douglas Gregord6f0b4e2009-02-02 17:56:05 +00001425 /// subset of the declarations the range [C,
1426 /// end-of-declarations). If A is non-NULL, it is a pointer to a
1427 /// member function of SpecificDecl that should return true for
1428 /// all of the SpecificDecl instances that will be in the subset
1429 /// of iterators. For example, if you want Objective-C instance
1430 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1431 /// &ObjCMethodDecl::isInstanceMethod.
Douglas Gregor669c9a22009-02-02 18:25:48 +00001432 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001433 SkipToNextDecl();
1434 }
1435
David Blaikie581deb32012-06-06 20:45:41 +00001436 value_type operator*() const { return cast<SpecificDecl>(*Current); }
1437 // This doesn't meet the iterator requirements, but it's convenient
1438 value_type operator->() const { return **this; }
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001439
1440 specific_decl_iterator& operator++() {
1441 ++Current;
1442 SkipToNextDecl();
1443 return *this;
1444 }
1445
1446 specific_decl_iterator operator++(int) {
1447 specific_decl_iterator tmp(*this);
1448 ++(*this);
1449 return tmp;
1450 }
Mike Stump1eb44332009-09-09 15:08:12 +00001451
David Blaikieba243b52011-11-09 06:07:30 +00001452 friend bool operator==(const specific_decl_iterator& x,
1453 const specific_decl_iterator& y) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001454 return x.Current == y.Current;
1455 }
Mike Stump1eb44332009-09-09 15:08:12 +00001456
David Blaikieba243b52011-11-09 06:07:30 +00001457 friend bool operator!=(const specific_decl_iterator& x,
1458 const specific_decl_iterator& y) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001459 return x.Current != y.Current;
1460 }
1461 };
1462
Douglas Gregor669c9a22009-02-02 18:25:48 +00001463 /// \brief Iterates over a filtered subrange of declarations stored
1464 /// in a DeclContext.
1465 ///
1466 /// This iterator visits only those declarations that are of type
1467 /// SpecificDecl (or a class derived from it) and that meet some
1468 /// additional run-time criteria. This iterator is used, for
1469 /// example, to provide access to the instance methods within an
1470 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
1471 /// Acceptable = ObjCMethodDecl::isInstanceMethod).
1472 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
1473 class filtered_decl_iterator {
1474 /// Current - The current, underlying declaration iterator, which
1475 /// will either be NULL or will point to a declaration of
1476 /// type SpecificDecl.
1477 DeclContext::decl_iterator Current;
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Douglas Gregor669c9a22009-02-02 18:25:48 +00001479 /// SkipToNextDecl - Advances the current position up to the next
1480 /// declaration of type SpecificDecl that also meets the criteria
1481 /// required by Acceptable.
1482 void SkipToNextDecl() {
1483 while (*Current &&
1484 (!isa<SpecificDecl>(*Current) ||
1485 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
1486 ++Current;
1487 }
1488
1489 public:
David Blaikie581deb32012-06-06 20:45:41 +00001490 typedef SpecificDecl *value_type;
1491 // TODO: Add reference and pointer typedefs (with some appropriate proxy
1492 // type) if we ever have a need for them.
1493 typedef void reference;
1494 typedef void pointer;
Douglas Gregor669c9a22009-02-02 18:25:48 +00001495 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1496 difference_type;
1497 typedef std::forward_iterator_tag iterator_category;
1498
1499 filtered_decl_iterator() : Current() { }
1500
David Blaikiebd4fa452012-05-01 00:48:43 +00001501 /// filtered_decl_iterator - Construct a new iterator over a
Douglas Gregor669c9a22009-02-02 18:25:48 +00001502 /// subset of the declarations the range [C,
1503 /// end-of-declarations). If A is non-NULL, it is a pointer to a
1504 /// member function of SpecificDecl that should return true for
1505 /// all of the SpecificDecl instances that will be in the subset
1506 /// of iterators. For example, if you want Objective-C instance
1507 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1508 /// &ObjCMethodDecl::isInstanceMethod.
1509 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
1510 SkipToNextDecl();
1511 }
1512
David Blaikie581deb32012-06-06 20:45:41 +00001513 value_type operator*() const { return cast<SpecificDecl>(*Current); }
1514 value_type operator->() const { return cast<SpecificDecl>(*Current); }
Douglas Gregor669c9a22009-02-02 18:25:48 +00001515
1516 filtered_decl_iterator& operator++() {
1517 ++Current;
1518 SkipToNextDecl();
1519 return *this;
1520 }
1521
1522 filtered_decl_iterator operator++(int) {
1523 filtered_decl_iterator tmp(*this);
1524 ++(*this);
1525 return tmp;
1526 }
Mike Stump1eb44332009-09-09 15:08:12 +00001527
David Blaikieba243b52011-11-09 06:07:30 +00001528 friend bool operator==(const filtered_decl_iterator& x,
1529 const filtered_decl_iterator& y) {
Douglas Gregor669c9a22009-02-02 18:25:48 +00001530 return x.Current == y.Current;
1531 }
Mike Stump1eb44332009-09-09 15:08:12 +00001532
David Blaikieba243b52011-11-09 06:07:30 +00001533 friend bool operator!=(const filtered_decl_iterator& x,
1534 const filtered_decl_iterator& y) {
Douglas Gregor669c9a22009-02-02 18:25:48 +00001535 return x.Current != y.Current;
1536 }
1537 };
1538
Douglas Gregor40f4e692009-01-20 16:54:50 +00001539 /// @brief Add the declaration D into this context.
1540 ///
1541 /// This routine should be invoked when the declaration D has first
1542 /// been declared, to place D into the context where it was
1543 /// (lexically) defined. Every declaration must be added to one
1544 /// (and only one!) context, where it can be visited via
1545 /// [decls_begin(), decls_end()). Once a declaration has been added
1546 /// to its lexical context, the corresponding DeclContext owns the
1547 /// declaration.
1548 ///
1549 /// If D is also a NamedDecl, it will be made visible within its
1550 /// semantic context via makeDeclVisibleInContext.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001551 void addDecl(Decl *D);
Sean Callanan381509e2011-10-21 16:15:18 +00001552
1553 /// @brief Add the declaration D into this context, but suppress
1554 /// searches for external declarations with the same name.
1555 ///
1556 /// Although analogous in function to addDecl, this removes an
1557 /// important check. This is only useful if the Decl is being
1558 /// added in response to an external search; in all other cases,
1559 /// addDecl() is the right function to use.
1560 /// See the ASTImporter for use cases.
Sean Callanan9faf8102011-10-21 02:57:43 +00001561 void addDeclInternal(Decl *D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001562
John McCall3f9a8a62009-08-11 06:59:38 +00001563 /// @brief Add the declaration D to this context without modifying
1564 /// any lookup tables.
1565 ///
1566 /// This is useful for some operations in dependent contexts where
1567 /// the semantic context might not be dependent; this basically
1568 /// only happens with friends.
1569 void addHiddenDecl(Decl *D);
1570
John McCall9f54ad42009-12-10 09:41:52 +00001571 /// @brief Removes a declaration from this context.
1572 void removeDecl(Decl *D);
Sean Callanancd904e82013-05-04 02:04:27 +00001573
1574 /// @brief Checks whether a declaration is in this context.
1575 bool containsDecl(Decl *D) const;
John McCall9f54ad42009-12-10 09:41:52 +00001576
John McCallea318642010-08-26 09:15:37 +00001577 typedef DeclContextLookupResult lookup_result;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001578 typedef lookup_result::iterator lookup_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +00001579
1580 /// lookup - Find the declarations (if any) with the given Name in
1581 /// this context. Returns a range of iterators that contains all of
Douglas Gregor40f4e692009-01-20 16:54:50 +00001582 /// the declarations with this name, with object, function, member,
1583 /// and enumerator names preceding any tag name. Note that this
1584 /// routine will not look into parent contexts.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001585 lookup_result lookup(DeclarationName Name) const;
Douglas Gregor44b43212008-12-11 16:49:14 +00001586
Richard Smithe7bae152013-07-13 02:00:19 +00001587 /// \brief Find the declarations with the given name that are visible
1588 /// within this context; don't attempt to retrieve anything from an
1589 /// external source.
1590 lookup_result noload_lookup(DeclarationName Name);
1591
Douglas Gregorb75a3452011-10-15 00:10:27 +00001592 /// \brief A simplistic name lookup mechanism that performs name lookup
1593 /// into this declaration context without consulting the external source.
1594 ///
David Blaikieba243b52011-11-09 06:07:30 +00001595 /// This function should almost never be used, because it subverts the
Douglas Gregorb75a3452011-10-15 00:10:27 +00001596 /// usual relationship between a DeclContext and the external source.
1597 /// See the ASTImporter for the (few, but important) use cases.
Richard Smithe7bae152013-07-13 02:00:19 +00001598 ///
1599 /// FIXME: This is very inefficient; replace uses of it with uses of
1600 /// noload_lookup.
David Blaikieba243b52011-11-09 06:07:30 +00001601 void localUncachedLookup(DeclarationName Name,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001602 SmallVectorImpl<NamedDecl *> &Results);
David Blaikieba243b52011-11-09 06:07:30 +00001603
Douglas Gregor40f4e692009-01-20 16:54:50 +00001604 /// @brief Makes a declaration visible within this context.
1605 ///
1606 /// This routine makes the declaration D visible to name lookup
1607 /// within this context and, if this is a transparent context,
1608 /// within its parent contexts up to the first enclosing
1609 /// non-transparent context. Making a declaration visible within a
1610 /// context does not transfer ownership of a declaration, and a
1611 /// declaration can be visible in many contexts that aren't its
1612 /// lexical context.
1613 ///
1614 /// If D is a redeclaration of an existing declaration that is
1615 /// visible from this context, as determined by
1616 /// NamedDecl::declarationReplaces, the previous declaration will be
1617 /// replaced with D.
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001618 void makeDeclVisibleInContext(NamedDecl *D);
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +00001619
Nick Lewycky173a37a2012-04-03 21:44:08 +00001620 /// all_lookups_iterator - An iterator that provides a view over the results
1621 /// of looking up every possible name.
1622 class all_lookups_iterator;
1623
Stephen Hines651f13c2014-04-23 16:59:28 -07001624 typedef llvm::iterator_range<all_lookups_iterator> lookups_range;
1625
1626 lookups_range lookups() const;
1627 lookups_range noload_lookups() const;
1628
Richard Smith6e322c02013-06-22 21:49:40 +00001629 /// \brief Iterators over all possible lookups within this context.
Nick Lewycky173a37a2012-04-03 21:44:08 +00001630 all_lookups_iterator lookups_begin() const;
Nick Lewycky173a37a2012-04-03 21:44:08 +00001631 all_lookups_iterator lookups_end() const;
1632
Richard Smith6e322c02013-06-22 21:49:40 +00001633 /// \brief Iterators over all possible lookups within this context that are
1634 /// currently loaded; don't attempt to retrieve anything from an external
1635 /// source.
1636 all_lookups_iterator noload_lookups_begin() const;
1637 all_lookups_iterator noload_lookups_end() const;
1638
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001639 struct udir_iterator;
1640 typedef llvm::iterator_adaptor_base<udir_iterator, lookup_iterator,
1641 std::random_access_iterator_tag,
1642 UsingDirectiveDecl *> udir_iterator_base;
1643 struct udir_iterator : udir_iterator_base {
1644 udir_iterator(lookup_iterator I) : udir_iterator_base(I) {}
1645 UsingDirectiveDecl *operator*() const;
1646 };
1647
1648 typedef llvm::iterator_range<udir_iterator> udir_range;
Mike Stump1eb44332009-09-09 15:08:12 +00001649
Stephen Hines651f13c2014-04-23 16:59:28 -07001650 udir_range using_directives() const;
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001651
John McCall0c01d182010-03-24 05:22:00 +00001652 // These are all defined in DependentDiagnostic.h.
1653 class ddiag_iterator;
Stephen Hines651f13c2014-04-23 16:59:28 -07001654 typedef llvm::iterator_range<DeclContext::ddiag_iterator> ddiag_range;
1655
1656 inline ddiag_range ddiags() const;
John McCall0c01d182010-03-24 05:22:00 +00001657
Douglas Gregorc2ee10d2009-04-07 17:20:56 +00001658 // Low-level accessors
Richard Smith5b8d0af2013-04-03 22:50:34 +00001659
Sean Callanand5a20c12012-08-23 21:16:40 +00001660 /// \brief Mark the lookup table as needing to be built. This should be
Richard Smith63a95142013-02-12 02:32:35 +00001661 /// used only if setHasExternalLexicalStorage() has been called on any
1662 /// decl context for which this is the primary context.
Sean Callanand5a20c12012-08-23 21:16:40 +00001663 void setMustBuildLookupTable() {
Sean Callanand5a20c12012-08-23 21:16:40 +00001664 LookupPtr.setInt(true);
1665 }
Douglas Gregorc2ee10d2009-04-07 17:20:56 +00001666
Douglas Gregorc2ee10d2009-04-07 17:20:56 +00001667 /// \brief Retrieve the internal representation of the lookup structure.
Richard Smithc5d3e802012-03-16 06:12:59 +00001668 /// This may omit some names if we are lazily building the structure.
1669 StoredDeclsMap *getLookupPtr() const { return LookupPtr.getPointer(); }
1670
1671 /// \brief Ensure the lookup structure is fully-built and return it.
1672 StoredDeclsMap *buildLookup();
Douglas Gregorc2ee10d2009-04-07 17:20:56 +00001673
Douglas Gregor2cf26342009-04-09 22:27:44 +00001674 /// \brief Whether this DeclContext has external storage containing
1675 /// additional declarations that are lexically in this context.
1676 bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; }
1677
1678 /// \brief State whether this DeclContext has external storage for
1679 /// declarations lexically in this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001680 void setHasExternalLexicalStorage(bool ES = true) {
1681 ExternalLexicalStorage = ES;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001682 }
1683
1684 /// \brief Whether this DeclContext has external storage containing
1685 /// additional declarations that are visible in this context.
1686 bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; }
1687
1688 /// \brief State whether this DeclContext has external storage for
1689 /// declarations visible in this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001690 void setHasExternalVisibleStorage(bool ES = true) {
1691 ExternalVisibleStorage = ES;
Richard Smith88963392013-02-11 22:02:16 +00001692 if (ES && LookupPtr.getPointer())
Richard Smithbbcd0f32013-02-07 03:37:08 +00001693 NeedToReconcileExternalVisibleStorage = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001694 }
1695
Douglas Gregor2ea054f2011-08-26 22:04:51 +00001696 /// \brief Determine whether the given declaration is stored in the list of
1697 /// declarations lexically within this context.
1698 bool isDeclInLexicalTraversal(const Decl *D) const {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001699 return D && (D->NextInContextAndBits.getPointer() || D == FirstDecl ||
1700 D == LastDecl);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00001701 }
David Blaikieba243b52011-11-09 06:07:30 +00001702
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +00001703 static bool classof(const Decl *D);
Chris Lattnerb048c982008-04-06 04:47:34 +00001704 static bool classof(const DeclContext *D) { return true; }
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +00001705
Stephen Hines651f13c2014-04-23 16:59:28 -07001706 void dumpDeclContext() const;
1707 void dumpLookups() const;
Stephen Hines176edba2014-12-01 14:53:08 -08001708 void dumpLookups(llvm::raw_ostream &OS, bool DumpDecls = false) const;
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +00001709
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +00001710private:
Stephen Hines651f13c2014-04-23 16:59:28 -07001711 void reconcileExternalVisibleStorage() const;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001712 void LoadLexicalDeclsFromExternalStorage() const;
David Blaikieba243b52011-11-09 06:07:30 +00001713
1714 /// @brief Makes a declaration visible within this context, but
Sean Callanan381509e2011-10-21 16:15:18 +00001715 /// suppresses searches for external declarations with the same
1716 /// name.
1717 ///
1718 /// Analogous to makeDeclVisibleInContext, but for the exclusive
1719 /// use of addDeclInternal().
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001720 void makeDeclVisibleInContextInternal(NamedDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001721
John McCall0c01d182010-03-24 05:22:00 +00001722 friend class DependentDiagnostic;
1723 StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const;
1724
Richard Smithe7bae152013-07-13 02:00:19 +00001725 template<decl_iterator (DeclContext::*Begin)() const,
1726 decl_iterator (DeclContext::*End)() const>
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001727 void buildLookupImpl(DeclContext *DCtx, bool Internal);
Richard Smithc5d3e802012-03-16 06:12:59 +00001728 void makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1729 bool Rediscoverable);
Sean Callanan9faf8102011-10-21 02:57:43 +00001730 void makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001731};
1732
Douglas Gregorf57172b2008-12-08 18:40:42 +00001733inline bool Decl::isTemplateParameter() const {
Douglas Gregor79c22782010-01-16 20:21:20 +00001734 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm ||
1735 getKind() == TemplateTemplateParm;
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001736}
1737
John McCall80cd64a2010-01-29 01:45:37 +00001738// Specialization selected when ToTy is not a known subclass of DeclContext.
1739template <class ToTy,
Stephen Hines651f13c2014-04-23 16:59:28 -07001740 bool IsKnownSubtype = ::std::is_base_of<DeclContext, ToTy>::value>
John McCall80cd64a2010-01-29 01:45:37 +00001741struct cast_convert_decl_context {
1742 static const ToTy *doit(const DeclContext *Val) {
1743 return static_cast<const ToTy*>(Decl::castFromDeclContext(Val));
1744 }
1745
1746 static ToTy *doit(DeclContext *Val) {
1747 return static_cast<ToTy*>(Decl::castFromDeclContext(Val));
1748 }
1749};
1750
1751// Specialization selected when ToTy is a known subclass of DeclContext.
1752template <class ToTy>
1753struct cast_convert_decl_context<ToTy, true> {
1754 static const ToTy *doit(const DeclContext *Val) {
1755 return static_cast<const ToTy*>(Val);
1756 }
1757
1758 static ToTy *doit(DeclContext *Val) {
1759 return static_cast<ToTy*>(Val);
1760 }
1761};
1762
1763
Chris Lattner0ed844b2008-04-04 06:12:32 +00001764} // end clang.
1765
1766namespace llvm {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001767
John McCall80cd64a2010-01-29 01:45:37 +00001768/// isa<T>(DeclContext*)
Eli Friedman4d509342011-05-21 19:15:39 +00001769template <typename To>
1770struct isa_impl<To, ::clang::DeclContext> {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001771 static bool doit(const ::clang::DeclContext &Val) {
Eli Friedman4d509342011-05-21 19:15:39 +00001772 return To::classofKind(Val.getDeclKind());
Chris Lattner0ed844b2008-04-04 06:12:32 +00001773 }
1774};
Chris Lattner0ed844b2008-04-04 06:12:32 +00001775
John McCall80cd64a2010-01-29 01:45:37 +00001776/// cast<T>(DeclContext*)
1777template<class ToTy>
1778struct cast_convert_val<ToTy,
1779 const ::clang::DeclContext,const ::clang::DeclContext> {
1780 static const ToTy &doit(const ::clang::DeclContext &Val) {
1781 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1782 }
1783};
1784template<class ToTy>
1785struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> {
1786 static ToTy &doit(::clang::DeclContext &Val) {
1787 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1788 }
1789};
1790template<class ToTy>
1791struct cast_convert_val<ToTy,
1792 const ::clang::DeclContext*, const ::clang::DeclContext*> {
1793 static const ToTy *doit(const ::clang::DeclContext *Val) {
1794 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1795 }
1796};
1797template<class ToTy>
1798struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> {
1799 static ToTy *doit(::clang::DeclContext *Val) {
1800 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1801 }
1802};
1803
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001804/// Implement cast_convert_val for Decl -> DeclContext conversions.
Chris Lattner0ed844b2008-04-04 06:12:32 +00001805template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001806struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
Chris Lattnerb048c982008-04-06 04:47:34 +00001807 static ::clang::DeclContext &doit(const FromTy &Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001808 return *FromTy::castToDeclContext(&Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001809 }
1810};
Chris Lattner0ed844b2008-04-04 06:12:32 +00001811
1812template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001813struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
Chris Lattnerb048c982008-04-06 04:47:34 +00001814 static ::clang::DeclContext *doit(const FromTy *Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001815 return FromTy::castToDeclContext(Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001816 }
1817};
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001818
Douglas Gregor44b43212008-12-11 16:49:14 +00001819template<class FromTy>
1820struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
1821 static const ::clang::DeclContext &doit(const FromTy &Val) {
1822 return *FromTy::castToDeclContext(&Val);
1823 }
1824};
1825
1826template<class FromTy>
1827struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
1828 static const ::clang::DeclContext *doit(const FromTy *Val) {
1829 return FromTy::castToDeclContext(Val);
1830 }
1831};
1832
Chris Lattner0ed844b2008-04-04 06:12:32 +00001833} // end namespace llvm
1834
1835#endif