blob: b5cbf9b814b5c2bbbe29ff966e2e616fb1e9cd83 [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"
Rafael Espindolaa99ecbc2013-05-25 17:16:20 +000019#include "clang/Basic/Linkage.h"
John McCallad2b8042010-01-18 23:21:37 +000020#include "clang/Basic/Specifiers.h"
Chris Lattneree219fd2009-03-29 06:06:59 +000021#include "llvm/ADT/PointerUnion.h"
Daniel Dunbaraa49a7d2012-03-09 19:35:29 +000022#include "llvm/Support/Compiler.h"
23#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner0ed844b2008-04-04 06:12:32 +000024
25namespace clang {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000026class ASTMutationListener;
Benjamin Kramer9852f582012-12-01 16:35:25 +000027class BlockDecl;
28class CXXRecordDecl;
29class CompoundStmt;
30class DeclContext;
31class DeclarationName;
32class DependentDiagnostic;
33class EnumDecl;
34class FunctionDecl;
Rafael Espindoladc070562013-05-28 19:43:11 +000035class LinkageComputer;
Benjamin Kramer9852f582012-12-01 16:35:25 +000036class LinkageSpecDecl;
Douglas Gregorca2ab452013-01-12 01:29:50 +000037class Module;
Benjamin Kramer9852f582012-12-01 16:35:25 +000038class NamedDecl;
39class NamespaceDecl;
40class ObjCCategoryDecl;
41class ObjCCategoryImplDecl;
42class ObjCContainerDecl;
43class ObjCImplDecl;
44class ObjCImplementationDecl;
45class ObjCInterfaceDecl;
46class ObjCMethodDecl;
47class ObjCProtocolDecl;
48struct PrintingPolicy;
49class Stmt;
50class StoredDeclsMap;
51class TranslationUnitDecl;
52class UsingDirectiveDecl;
Chris Lattner0eda3b32009-03-29 04:32:54 +000053}
54
55namespace llvm {
56// DeclContext* is only 4-byte aligned on 32-bit systems.
57template<>
58 class PointerLikeTypeTraits<clang::DeclContext*> {
59 typedef clang::DeclContext* PT;
60public:
61 static inline void *getAsVoidPointer(PT P) { return P; }
62 static inline PT getFromVoidPointer(void *P) {
63 return static_cast<PT>(P);
64 }
65 enum { NumLowBitsAvailable = 2 };
66};
67}
68
69namespace clang {
Chris Lattner0ed844b2008-04-04 06:12:32 +000070
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000071 /// \brief Captures the result of checking the availability of a
72 /// declaration.
73 enum AvailabilityResult {
74 AR_Available = 0,
75 AR_NotYetIntroduced,
76 AR_Deprecated,
77 AR_Unavailable
78 };
79
Mike Stump1eb44332009-09-09 15:08:12 +000080/// Decl - This represents one declaration (or definition), e.g. a variable,
81/// typedef, function, struct, etc.
Chris Lattner0ed844b2008-04-04 06:12:32 +000082///
83class Decl {
84public:
Douglas Gregor64650af2009-02-02 23:39:07 +000085 /// \brief Lists the kind of concrete classes of Decl.
Chris Lattner0ed844b2008-04-04 06:12:32 +000086 enum Kind {
Sean Hunt9a555912010-05-30 07:21:58 +000087#define DECL(DERIVED, BASE) DERIVED,
88#define ABSTRACT_DECL(DECL)
89#define DECL_RANGE(BASE, START, END) \
90 first##BASE = START, last##BASE = END,
91#define LAST_DECL_RANGE(BASE, START, END) \
92 first##BASE = START, last##BASE = END
93#include "clang/AST/DeclNodes.inc"
Chris Lattner0ed844b2008-04-04 06:12:32 +000094 };
95
Chris Lattner6ad9ac02010-05-07 21:43:38 +000096 /// \brief A placeholder type used to construct an empty shell of a
97 /// decl-derived type that will be filled in later (e.g., by some
98 /// deserialization method).
99 struct EmptyShell { };
100
John McCall0d6b1642010-04-23 18:46:30 +0000101 /// IdentifierNamespace - The different namespaces in which
102 /// declarations may appear. According to C99 6.2.3, there are
103 /// four namespaces, labels, tags, members and ordinary
104 /// identifiers. C++ describes lookup completely differently:
105 /// certain lookups merely "ignore" certain kinds of declarations,
106 /// usually based on whether the declaration is of a type, etc.
David Blaikieba243b52011-11-09 06:07:30 +0000107 ///
John McCall0d6b1642010-04-23 18:46:30 +0000108 /// These are meant as bitmasks, so that searches in
109 /// C++ can look into the "tag" namespace during ordinary lookup.
110 ///
Sebastian Redl07a353c2010-07-14 20:26:45 +0000111 /// Decl currently provides 15 bits of IDNS bits.
Chris Lattner0ed844b2008-04-04 06:12:32 +0000112 enum IdentifierNamespace {
John McCall0d6b1642010-04-23 18:46:30 +0000113 /// Labels, declared with 'x:' and referenced with 'goto x'.
114 IDNS_Label = 0x0001,
115
116 /// Tags, declared with 'struct foo;' and referenced with
117 /// 'struct foo'. All tags are also types. This is what
118 /// elaborated-type-specifiers look for in C.
119 IDNS_Tag = 0x0002,
120
121 /// Types, declared with 'struct foo', typedefs, etc.
122 /// This is what elaborated-type-specifiers look for in C++,
123 /// but note that it's ill-formed to find a non-tag.
124 IDNS_Type = 0x0004,
125
126 /// Members, declared with object declarations within tag
127 /// definitions. In C, these can only be found by "qualified"
128 /// lookup in member expressions. In C++, they're found by
129 /// normal lookup.
130 IDNS_Member = 0x0008,
131
132 /// Namespaces, declared with 'namespace foo {}'.
133 /// Lookup for nested-name-specifiers find these.
134 IDNS_Namespace = 0x0010,
135
136 /// Ordinary names. In C, everything that's not a label, tag,
137 /// or member ends up here.
138 IDNS_Ordinary = 0x0020,
139
Jordan Rosed4582b82013-04-03 01:39:23 +0000140 /// Objective C \@protocol.
John McCall0d6b1642010-04-23 18:46:30 +0000141 IDNS_ObjCProtocol = 0x0040,
142
143 /// This declaration is a friend function. A friend function
144 /// declaration is always in this namespace but may also be in
145 /// IDNS_Ordinary if it was previously declared.
146 IDNS_OrdinaryFriend = 0x0080,
147
148 /// This declaration is a friend class. A friend class
149 /// declaration is always in this namespace but may also be in
150 /// IDNS_Tag|IDNS_Type if it was previously declared.
151 IDNS_TagFriend = 0x0100,
152
153 /// This declaration is a using declaration. A using declaration
154 /// *introduces* a number of other declarations into the current
155 /// scope, and those declarations use the IDNS of their targets,
156 /// but the actual using declarations go in this namespace.
John McCall76d32642010-04-24 01:30:58 +0000157 IDNS_Using = 0x0200,
158
159 /// This declaration is a C++ operator declared in a non-class
160 /// context. All such operators are also in IDNS_Ordinary.
161 /// C++ lexical operator lookup looks for these.
162 IDNS_NonMemberOperator = 0x0400
Chris Lattner0ed844b2008-04-04 06:12:32 +0000163 };
Mike Stump1eb44332009-09-09 15:08:12 +0000164
John McCall09e2c522011-05-01 03:04:29 +0000165 /// ObjCDeclQualifier - 'Qualifiers' written next to the return and
166 /// parameter types in method declarations. Other than remembering
167 /// them and mangling them into the method's signature string, these
168 /// are ignored by the compiler; they are consumed by certain
169 /// remote-messaging frameworks.
170 ///
171 /// in, inout, and out are mutually exclusive and apply only to
172 /// method parameters. bycopy and byref are mutually exclusive and
173 /// apply only to method parameters (?). oneway applies only to
174 /// results. All of these expect their corresponding parameter to
175 /// have a particular type. None of this is currently enforced by
176 /// clang.
177 ///
178 /// This should be kept in sync with ObjCDeclSpec::ObjCDeclQualifier.
Chris Lattner0ed844b2008-04-04 06:12:32 +0000179 enum ObjCDeclQualifier {
180 OBJC_TQ_None = 0x0,
181 OBJC_TQ_In = 0x1,
182 OBJC_TQ_Inout = 0x2,
183 OBJC_TQ_Out = 0x4,
184 OBJC_TQ_Bycopy = 0x8,
185 OBJC_TQ_Byref = 0x10,
186 OBJC_TQ_Oneway = 0x20
187 };
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Douglas Gregor46cd2182012-01-06 16:59:53 +0000189protected:
190 // Enumeration values used in the bits stored in NextInContextAndBits.
191 enum {
192 /// \brief Whether this declaration is a top-level declaration (function,
193 /// global variable, etc.) that is lexically inside an objc container
194 /// definition.
195 TopLevelDeclInObjCContainerFlag = 0x01,
196
197 /// \brief Whether this declaration is private to the module in which it was
198 /// defined.
199 ModulePrivateFlag = 0x02
200 };
201
202 /// \brief The next declaration within the same lexical
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000203 /// DeclContext. These pointers form the linked list that is
204 /// traversed via DeclContext's decls_begin()/decls_end().
Douglas Gregor46cd2182012-01-06 16:59:53 +0000205 ///
206 /// The extra two bits are used for the TopLevelDeclInObjCContainer and
207 /// ModulePrivate bits.
208 llvm::PointerIntPair<Decl *, 2, unsigned> NextInContextAndBits;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000209
Douglas Gregor46cd2182012-01-06 16:59:53 +0000210private:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000211 friend class DeclContext;
212
Chris Lattneree219fd2009-03-29 06:06:59 +0000213 struct MultipleDC {
214 DeclContext *SemanticDC;
215 DeclContext *LexicalDC;
216 };
Mike Stump1eb44332009-09-09 15:08:12 +0000217
218
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000219 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
220 /// For declarations that don't contain C++ scope specifiers, it contains
221 /// the DeclContext where the Decl was declared.
222 /// For declarations with C++ scope specifiers, it contains a MultipleDC*
223 /// with the context where it semantically belongs (SemanticDC) and the
224 /// context where it was lexically declared (LexicalDC).
225 /// e.g.:
226 ///
227 /// namespace A {
228 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
229 /// }
230 /// void A::f(); // SemanticDC == namespace 'A'
231 /// // LexicalDC == global namespace
Chris Lattneree219fd2009-03-29 06:06:59 +0000232 llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000233
Chris Lattneree219fd2009-03-29 06:06:59 +0000234 inline bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); }
235 inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000236 inline MultipleDC *getMultipleDC() const {
Chris Lattneree219fd2009-03-29 06:06:59 +0000237 return DeclCtx.get<MultipleDC*>();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000238 }
Chris Lattner10d83792009-03-27 18:46:15 +0000239 inline DeclContext *getSemanticDC() const {
Chris Lattneree219fd2009-03-29 06:06:59 +0000240 return DeclCtx.get<DeclContext*>();
Chris Lattner10d83792009-03-27 18:46:15 +0000241 }
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Douglas Gregor381d34e2010-12-06 18:36:25 +0000243 /// Loc - The location of this decl.
Daniel Dunbar39d76502009-03-04 02:26:41 +0000244 SourceLocation Loc;
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Chris Lattner0ed844b2008-04-04 06:12:32 +0000246 /// DeclKind - This indicates which class this is.
Rafael Espindola838dc592013-01-12 06:42:30 +0000247 unsigned DeclKind : 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Chris Lattner0ed844b2008-04-04 06:12:32 +0000249 /// InvalidDecl - This indicates a semantic error occurred.
John McCall35043e52010-10-19 05:43:52 +0000250 unsigned InvalidDecl : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Chris Lattner0ed844b2008-04-04 06:12:32 +0000252 /// HasAttrs - This indicates whether the decl has attributes or not.
John McCall35043e52010-10-19 05:43:52 +0000253 unsigned HasAttrs : 1;
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000254
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000255 /// Implicit - Whether this declaration was implicitly generated by
256 /// the implementation rather than explicitly written by the user.
John McCall35043e52010-10-19 05:43:52 +0000257 unsigned Implicit : 1;
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000258
Douglas Gregore0762c92009-06-19 23:52:42 +0000259 /// \brief Whether this declaration was "used", meaning that a definition is
260 /// required.
John McCall35043e52010-10-19 05:43:52 +0000261 unsigned Used : 1;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000262
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000263 /// \brief Whether this declaration was "referenced".
264 /// The difference with 'Used' is whether the reference appears in a
265 /// evaluated context or not, e.g. functions used in uninstantiated templates
266 /// are regarded as "referenced" but not "used".
267 unsigned Referenced : 1;
David Blaikieba243b52011-11-09 06:07:30 +0000268
Daniel Dunbar02892a62012-03-05 21:42:49 +0000269 /// \brief Whether statistic collection is enabled.
270 static bool StatisticsEnabled;
271
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000272protected:
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000273 /// Access - Used by C++ decls for the access specifier.
274 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
275 unsigned Access : 2;
276 friend class CXXClassMemberWrapper;
Sebastian Redl0b17c612010-08-13 00:28:03 +0000277
Douglas Gregor08e0bc12011-09-10 00:09:20 +0000278 /// \brief Whether this declaration was loaded from an AST file.
279 unsigned FromASTFile : 1;
David Blaikieba243b52011-11-09 06:07:30 +0000280
Douglas Gregorf143ffc2012-01-06 16:22:39 +0000281 /// \brief Whether this declaration is hidden from normal name lookup, e.g.,
282 /// because it is was loaded from an AST file is either module-private or
283 /// because its submodule has not been made visible.
284 unsigned Hidden : 1;
285
Chris Lattner769dbdf2009-03-27 20:18:19 +0000286 /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in.
Douglas Gregor381d34e2010-12-06 18:36:25 +0000287 unsigned IdentifierNamespace : 12;
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Rafael Espindolaa99ecbc2013-05-25 17:16:20 +0000289 /// \brief If 0, we have not computed the linkage of this declaration.
290 /// Otherwise, it is the linkage + 1.
291 mutable unsigned CacheValidAndLinkage : 3;
David Blaikieba243b52011-11-09 06:07:30 +0000292
Douglas Gregor8d267c52011-09-09 02:06:17 +0000293 friend class ASTDeclWriter;
294 friend class ASTDeclReader;
Douglas Gregorecc2c092011-12-01 22:20:10 +0000295 friend class ASTReader;
Rafael Espindoladc070562013-05-28 19:43:11 +0000296 friend class LinkageComputer;
Douglas Gregor8d267c52011-09-09 02:06:17 +0000297
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000298private:
Anders Carlsson1329c272009-03-25 23:38:06 +0000299 void CheckAccessDeclContext() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000301protected:
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000302
Mike Stump1eb44332009-09-09 15:08:12 +0000303 Decl(Kind DK, DeclContext *DC, SourceLocation L)
Douglas Gregor46cd2182012-01-06 16:59:53 +0000304 : NextInContextAndBits(), DeclCtx(DC),
Daniel Dunbar39d76502009-03-04 02:26:41 +0000305 Loc(L), DeclKind(DK), InvalidDecl(0),
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000306 HasAttrs(false), Implicit(false), Used(false), Referenced(false),
Douglas Gregor46cd2182012-01-06 16:59:53 +0000307 Access(AS_none), FromASTFile(0), Hidden(0),
Douglas Gregor381d34e2010-12-06 18:36:25 +0000308 IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
Rafael Espindolaa99ecbc2013-05-25 17:16:20 +0000309 CacheValidAndLinkage(0)
Douglas Gregor381d34e2010-12-06 18:36:25 +0000310 {
Daniel Dunbar02892a62012-03-05 21:42:49 +0000311 if (StatisticsEnabled) add(DK);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000312 }
Sam Bishop1bb19632008-04-11 18:04:39 +0000313
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +0000314 Decl(Kind DK, EmptyShell Empty)
Douglas Gregor46cd2182012-01-06 16:59:53 +0000315 : NextInContextAndBits(), DeclKind(DK), InvalidDecl(0),
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000316 HasAttrs(false), Implicit(false), Used(false), Referenced(false),
Douglas Gregor46cd2182012-01-06 16:59:53 +0000317 Access(AS_none), FromASTFile(0), Hidden(0),
Douglas Gregor381d34e2010-12-06 18:36:25 +0000318 IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
Rafael Espindolaa99ecbc2013-05-25 17:16:20 +0000319 CacheValidAndLinkage(0)
Douglas Gregor381d34e2010-12-06 18:36:25 +0000320 {
Daniel Dunbar02892a62012-03-05 21:42:49 +0000321 if (StatisticsEnabled) add(DK);
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +0000322 }
323
Douglas Gregorda2142f2011-02-19 18:51:44 +0000324 virtual ~Decl();
325
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000326 /// \brief Allocate memory for a deserialized declaration.
327 ///
328 /// This routine must be used to allocate memory for any declaration that is
329 /// deserialized from a module file.
330 ///
331 /// \param Context The context in which we will allocate memory.
332 /// \param ID The global ID of the deserialized declaration.
333 /// \param Size The size of the allocated object.
334 static void *AllocateDeserializedDecl(const ASTContext &Context,
335 unsigned ID,
336 unsigned Size);
Douglas Gregor6bd99292013-02-09 01:35:03 +0000337
338 /// \brief Update a potentially out-of-date declaration.
339 void updateOutOfDate(IdentifierInfo &II) const;
340
Rafael Espindolaa99ecbc2013-05-25 17:16:20 +0000341 Linkage getCachedLinkage() const {
342 return Linkage(CacheValidAndLinkage - 1);
343 }
344
345 void setCachedLinkage(Linkage L) const {
346 CacheValidAndLinkage = L + 1;
347 }
348
349 bool hasCachedLinkage() const {
350 return CacheValidAndLinkage;
351 }
352
Sam Bishop1bb19632008-04-11 18:04:39 +0000353public:
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000354
355 /// \brief Source range that this declaration covers.
Daniel Dunbaraa49a7d2012-03-09 19:35:29 +0000356 virtual SourceRange getSourceRange() const LLVM_READONLY {
Douglas Gregorda2142f2011-02-19 18:51:44 +0000357 return SourceRange(getLocation(), getLocation());
358 }
Daniel Dunbaraa49a7d2012-03-09 19:35:29 +0000359 SourceLocation getLocStart() const LLVM_READONLY {
360 return getSourceRange().getBegin();
361 }
362 SourceLocation getLocEnd() const LLVM_READONLY {
363 return getSourceRange().getEnd();
364 }
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000365
Chris Lattner0ed844b2008-04-04 06:12:32 +0000366 SourceLocation getLocation() const { return Loc; }
367 void setLocation(SourceLocation L) { Loc = L; }
368
John McCall35043e52010-10-19 05:43:52 +0000369 Kind getKind() const { return static_cast<Kind>(DeclKind); }
Chris Lattner0ed844b2008-04-04 06:12:32 +0000370 const char *getDeclKindName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Douglas Gregor46cd2182012-01-06 16:59:53 +0000372 Decl *getNextDeclInContext() { return NextInContextAndBits.getPointer(); }
373 const Decl *getNextDeclInContext() const {return NextInContextAndBits.getPointer();}
Chris Lattner96f44682009-03-28 05:59:45 +0000374
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000375 DeclContext *getDeclContext() {
Chris Lattner10d83792009-03-27 18:46:15 +0000376 if (isInSemaDC())
377 return getSemanticDC();
378 return getMultipleDC()->SemanticDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000379 }
Chris Lattner0cf2b192009-03-27 19:19:59 +0000380 const DeclContext *getDeclContext() const {
381 return const_cast<Decl*>(this)->getDeclContext();
382 }
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000383
John McCallf5ebf9b2013-05-03 07:33:41 +0000384 /// Find the innermost non-closure ancestor of this declaration,
385 /// walking up through blocks, lambdas, etc. If that ancestor is
386 /// not a code context (!isFunctionOrMethod()), returns null.
387 ///
388 /// A declaration may be its own non-closure context.
389 Decl *getNonClosureContext();
390 const Decl *getNonClosureContext() const {
John McCallaab9e312011-02-22 22:25:23 +0000391 return const_cast<Decl*>(this)->getNonClosureContext();
392 }
393
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000394 TranslationUnitDecl *getTranslationUnitDecl();
395 const TranslationUnitDecl *getTranslationUnitDecl() const {
396 return const_cast<Decl*>(this)->getTranslationUnitDecl();
397 }
398
John McCall9aeed322009-10-01 00:25:31 +0000399 bool isInAnonymousNamespace() const;
400
Daniel Dunbaraa49a7d2012-03-09 19:35:29 +0000401 ASTContext &getASTContext() const LLVM_READONLY;
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Anders Carlsson1329c272009-03-25 23:38:06 +0000403 void setAccess(AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000404 Access = AS;
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000405#ifndef NDEBUG
Anders Carlsson1329c272009-03-25 23:38:06 +0000406 CheckAccessDeclContext();
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000407#endif
Anders Carlssonb8547e82009-03-25 20:19:57 +0000408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
410 AccessSpecifier getAccess() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000411#ifndef NDEBUG
Anders Carlsson1329c272009-03-25 23:38:06 +0000412 CheckAccessDeclContext();
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000413#endif
Mike Stump1eb44332009-09-09 15:08:12 +0000414 return AccessSpecifier(Access);
Anders Carlsson1329c272009-03-25 23:38:06 +0000415 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000416
Douglas Gregor109f5fc2013-04-08 17:12:58 +0000417 /// \brief Retrieve the access specifier for this declaration, even though
418 /// it may not yet have been properly set.
419 AccessSpecifier getAccessUnsafe() const {
420 return AccessSpecifier(Access);
421 }
422
Chris Lattner76a642f2009-02-15 22:43:40 +0000423 bool hasAttrs() const { return HasAttrs; }
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000424 void setAttrs(const AttrVec& Attrs) {
425 return setAttrsImpl(Attrs, getASTContext());
426 }
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000427 AttrVec &getAttrs() {
Sean Huntcf807c42010-08-18 23:23:40 +0000428 return const_cast<AttrVec&>(const_cast<const Decl*>(this)->getAttrs());
Chris Lattner81abbdd2009-03-21 06:27:31 +0000429 }
Sean Huntcf807c42010-08-18 23:23:40 +0000430 const AttrVec &getAttrs() const;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000431 void swapAttrs(Decl *D);
Sean Huntcf807c42010-08-18 23:23:40 +0000432 void dropAttrs();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000433
Douglas Gregorb5f35ba2010-12-06 17:49:01 +0000434 void addAttr(Attr *A) {
435 if (hasAttrs())
436 getAttrs().push_back(A);
437 else
438 setAttrs(AttrVec(1, A));
439 }
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Sean Huntcf807c42010-08-18 23:23:40 +0000441 typedef AttrVec::const_iterator attr_iterator;
442
443 // FIXME: Do not rely on iterators having comparable singular values.
444 // Note that this should error out if they do not.
445 attr_iterator attr_begin() const {
446 return hasAttrs() ? getAttrs().begin() : 0;
447 }
448 attr_iterator attr_end() const {
449 return hasAttrs() ? getAttrs().end() : 0;
450 }
David Blaikieba243b52011-11-09 06:07:30 +0000451
Fariborz Jahanianc3ca14d2011-06-23 17:50:10 +0000452 template <typename T>
Fariborz Jahanian8cf0f522011-06-23 20:24:38 +0000453 void dropAttr() {
454 if (!HasAttrs) return;
David Blaikieba243b52011-11-09 06:07:30 +0000455
Benjamin Kramer10be5ba2012-10-14 11:50:50 +0000456 AttrVec &Vec = getAttrs();
457 Vec.erase(std::remove_if(Vec.begin(), Vec.end(), isa<T, Attr*>), Vec.end());
458
459 if (Vec.empty())
Fariborz Jahanian8cf0f522011-06-23 20:24:38 +0000460 HasAttrs = false;
461 }
David Blaikieba243b52011-11-09 06:07:30 +0000462
Sean Huntcf807c42010-08-18 23:23:40 +0000463 template <typename T>
464 specific_attr_iterator<T> specific_attr_begin() const {
465 return specific_attr_iterator<T>(attr_begin());
466 }
467 template <typename T>
468 specific_attr_iterator<T> specific_attr_end() const {
469 return specific_attr_iterator<T>(attr_end());
470 }
471
472 template<typename T> T *getAttr() const {
473 return hasAttrs() ? getSpecificAttr<T>(getAttrs()) : 0;
474 }
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000475 template<typename T> bool hasAttr() const {
Sean Huntcf807c42010-08-18 23:23:40 +0000476 return hasAttrs() && hasSpecificAttr<T>(getAttrs());
477 }
478
479 /// getMaxAlignment - return the maximum alignment specified by attributes
480 /// on this decl, 0 if there are none.
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +0000481 unsigned getMaxAlignment() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Chris Lattner0ed844b2008-04-04 06:12:32 +0000483 /// setInvalidDecl - Indicates the Decl had a semantic error. This
484 /// allows for graceful error recovery.
Douglas Gregor42738572010-03-05 00:26:45 +0000485 void setInvalidDecl(bool Invalid = true);
Chris Lattner0ed844b2008-04-04 06:12:32 +0000486 bool isInvalidDecl() const { return (bool) InvalidDecl; }
Douglas Gregor6b3945f2009-01-07 19:46:03 +0000487
488 /// isImplicit - Indicates whether the declaration was implicitly
489 /// generated by the implementation. If false, this declaration
490 /// was written explicitly in the source code.
491 bool isImplicit() const { return Implicit; }
492 void setImplicit(bool I = true) { Implicit = I; }
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Douglas Gregore0762c92009-06-19 23:52:42 +0000494 /// \brief Whether this declaration was used, meaning that a definition
495 /// is required.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000496 ///
497 /// \param CheckUsedAttr When true, also consider the "used" attribute
498 /// (in addition to the "used" bit set by \c setUsed()) when determining
499 /// whether the function is used.
500 bool isUsed(bool CheckUsedAttr = true) const;
Sean Huntcf807c42010-08-18 23:23:40 +0000501
Douglas Gregore0762c92009-06-19 23:52:42 +0000502 void setUsed(bool U = true) { Used = U; }
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000504 /// \brief Whether this declaration was referenced.
505 bool isReferenced() const;
506
507 void setReferenced(bool R = true) { Referenced = R; }
508
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +0000509 /// \brief Whether this declaration is a top-level declaration (function,
510 /// global variable, etc.) that is lexically inside an objc container
511 /// definition.
512 bool isTopLevelDeclInObjCContainer() const {
Douglas Gregor46cd2182012-01-06 16:59:53 +0000513 return NextInContextAndBits.getInt() & TopLevelDeclInObjCContainerFlag;
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +0000514 }
515
516 void setTopLevelDeclInObjCContainer(bool V = true) {
Douglas Gregor46cd2182012-01-06 16:59:53 +0000517 unsigned Bits = NextInContextAndBits.getInt();
518 if (V)
519 Bits |= TopLevelDeclInObjCContainerFlag;
520 else
521 Bits &= ~TopLevelDeclInObjCContainerFlag;
522 NextInContextAndBits.setInt(Bits);
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +0000523 }
524
Douglas Gregor46cd2182012-01-06 16:59:53 +0000525protected:
526 /// \brief Whether this declaration was marked as being private to the
527 /// module in which it was defined.
528 bool isModulePrivate() const {
529 return NextInContextAndBits.getInt() & ModulePrivateFlag;
530 }
531
532 /// \brief Specify whether this declaration was marked as being private
533 /// to the module in which it was defined.
534 void setModulePrivate(bool MP = true) {
535 unsigned Bits = NextInContextAndBits.getInt();
536 if (MP)
537 Bits |= ModulePrivateFlag;
538 else
539 Bits &= ~ModulePrivateFlag;
540 NextInContextAndBits.setInt(Bits);
541 }
542
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +0000543 /// \brief Set the owning module ID.
544 void setOwningModuleID(unsigned ID) {
545 assert(isFromASTFile() && "Only works on a deserialized declaration");
546 *((unsigned*)this - 2) = ID;
547 }
548
Douglas Gregor46cd2182012-01-06 16:59:53 +0000549public:
550
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000551 /// \brief Determine the availability of the given declaration.
552 ///
553 /// This routine will determine the most restrictive availability of
554 /// the given declaration (e.g., preferring 'unavailable' to
555 /// 'deprecated').
556 ///
557 /// \param Message If non-NULL and the result is not \c
558 /// AR_Available, will be set to a (possibly empty) message
559 /// describing why the declaration has not been introduced, is
560 /// deprecated, or is unavailable.
561 AvailabilityResult getAvailability(std::string *Message = 0) const;
562
563 /// \brief Determine whether this declaration is marked 'deprecated'.
564 ///
565 /// \param Message If non-NULL and the declaration is deprecated,
566 /// this will be set to the message describing why the declaration
567 /// was deprecated (which may be empty).
568 bool isDeprecated(std::string *Message = 0) const {
569 return getAvailability(Message) == AR_Deprecated;
570 }
571
572 /// \brief Determine whether this declaration is marked 'unavailable'.
573 ///
574 /// \param Message If non-NULL and the declaration is unavailable,
575 /// this will be set to the message describing why the declaration
576 /// was made unavailable (which may be empty).
577 bool isUnavailable(std::string *Message = 0) const {
578 return getAvailability(Message) == AR_Unavailable;
579 }
580
581 /// \brief Determine whether this is a weak-imported symbol.
582 ///
583 /// Weak-imported symbols are typically marked with the
Eli Friedman7f3ad232011-03-28 02:00:21 +0000584 /// 'weak_import' attribute, but may also be marked with an
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000585 /// 'availability' attribute where we're targing a platform prior to
586 /// the introduction of this feature.
587 bool isWeakImported() const;
588
589 /// \brief Determines whether this symbol can be weak-imported,
590 /// e.g., whether it would be well-formed to add the weak_import
591 /// attribute.
592 ///
593 /// \param IsDefinition Set to \c true to indicate that this
594 /// declaration cannot be weak-imported because it has a definition.
595 bool canBeWeakImported(bool &IsDefinition) const;
596
Douglas Gregor919814d2011-09-09 23:01:35 +0000597 /// \brief Determine whether this declaration came from an AST file (such as
598 /// a precompiled header or module) rather than having been parsed.
Douglas Gregor08e0bc12011-09-10 00:09:20 +0000599 bool isFromASTFile() const { return FromASTFile; }
David Blaikieba243b52011-11-09 06:07:30 +0000600
Douglas Gregorb6b60c12012-01-05 22:27:05 +0000601 /// \brief Retrieve the global declaration ID associated with this
602 /// declaration, which specifies where in the
603 unsigned getGlobalID() const {
604 if (isFromASTFile())
605 return *((const unsigned*)this - 1);
606 return 0;
607 }
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +0000608
609 /// \brief Retrieve the global ID of the module that owns this particular
610 /// declaration.
611 unsigned getOwningModuleID() const {
612 if (isFromASTFile())
613 return *((const unsigned*)this - 2);
614
615 return 0;
616 }
Douglas Gregorca2ab452013-01-12 01:29:50 +0000617
618private:
619 Module *getOwningModuleSlow() const;
620
621public:
622 Module *getOwningModule() const {
623 if (!isFromASTFile())
624 return 0;
625
626 return getOwningModuleSlow();
627 }
628
Douglas Gregorcc636682009-02-17 23:15:12 +0000629 unsigned getIdentifierNamespace() const {
Chris Lattner769dbdf2009-03-27 20:18:19 +0000630 return IdentifierNamespace;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000631 }
Chris Lattnerd62fdc42009-01-06 07:16:40 +0000632 bool isInIdentifierNamespace(unsigned NS) const {
633 return getIdentifierNamespace() & NS;
634 }
Chris Lattner769dbdf2009-03-27 20:18:19 +0000635 static unsigned getIdentifierNamespaceForKind(Kind DK);
636
John McCall0d6b1642010-04-23 18:46:30 +0000637 bool hasTagIdentifierNamespace() const {
638 return isTagIdentifierNamespace(getIdentifierNamespace());
639 }
640 static bool isTagIdentifierNamespace(unsigned NS) {
641 // TagDecls have Tag and Type set and may also have TagFriend.
642 return (NS & ~IDNS_TagFriend) == (IDNS_Tag | IDNS_Type);
643 }
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000645 /// getLexicalDeclContext - The declaration context where this Decl was
646 /// lexically declared (LexicalDC). May be different from
647 /// getDeclContext() (SemanticDC).
648 /// e.g.:
649 ///
650 /// namespace A {
651 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
652 /// }
653 /// void A::f(); // SemanticDC == namespace 'A'
654 /// // LexicalDC == global namespace
Chris Lattner10d83792009-03-27 18:46:15 +0000655 DeclContext *getLexicalDeclContext() {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000656 if (isInSemaDC())
Chris Lattner10d83792009-03-27 18:46:15 +0000657 return getSemanticDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000658 return getMultipleDC()->LexicalDC;
659 }
Chris Lattner10d83792009-03-27 18:46:15 +0000660 const DeclContext *getLexicalDeclContext() const {
661 return const_cast<Decl*>(this)->getLexicalDeclContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000662 }
Argyrios Kyrtzidisf5cecfb2009-06-17 22:49:50 +0000663
Douglas Gregorda2142f2011-02-19 18:51:44 +0000664 virtual bool isOutOfLine() const {
665 return getLexicalDeclContext() != getDeclContext();
666 }
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Douglas Gregor6ab35242009-04-09 21:40:53 +0000668 /// setDeclContext - Set both the semantic and lexical DeclContext
669 /// to DC.
670 void setDeclContext(DeclContext *DC);
671
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000672 void setLexicalDeclContext(DeclContext *DC);
673
Sebastian Redl7a126a42010-08-31 00:36:30 +0000674 /// isDefinedOutsideFunctionOrMethod - This predicate returns true if this
675 /// scoped decl is defined outside the current function or method. This is
676 /// roughly global variables and functions, but also handles enums (which
677 /// could be defined inside or outside a function etc).
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000678 bool isDefinedOutsideFunctionOrMethod() const {
679 return getParentFunctionOrMethod() == 0;
680 }
681
682 /// \brief If this decl is defined inside a function/method/block it returns
683 /// the corresponding DeclContext, otherwise it returns null.
684 const DeclContext *getParentFunctionOrMethod() const;
Argyrios Kyrtzidisfcc1e502011-09-28 18:14:24 +0000685 DeclContext *getParentFunctionOrMethod() {
686 return const_cast<DeclContext*>(
687 const_cast<const Decl*>(this)->getParentFunctionOrMethod());
688 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000689
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000690 /// \brief Retrieves the "canonical" declaration of the given declaration.
Douglas Gregorda2142f2011-02-19 18:51:44 +0000691 virtual Decl *getCanonicalDecl() { return this; }
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000692 const Decl *getCanonicalDecl() const {
693 return const_cast<Decl*>(this)->getCanonicalDecl();
694 }
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000695
Argyrios Kyrtzidisac4e3792009-07-18 08:50:48 +0000696 /// \brief Whether this particular Decl is a canonical one.
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000697 bool isCanonicalDecl() const { return getCanonicalDecl() == this; }
Douglas Gregorc896ad02011-12-14 21:44:45 +0000698
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000699protected:
700 /// \brief Returns the next redeclaration or itself if this is the only decl.
701 ///
702 /// Decl subclasses that can be redeclared should override this method so that
703 /// Decl::redecl_iterator can iterate over them.
Douglas Gregorda2142f2011-02-19 18:51:44 +0000704 virtual Decl *getNextRedeclaration() { return this; }
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000705
Douglas Gregoref96ee02012-01-14 16:38:05 +0000706 /// \brief Implementation of getPreviousDecl(), to be overridden by any
707 /// subclass that has a redeclaration chain.
708 virtual Decl *getPreviousDeclImpl() { return 0; }
709
710 /// \brief Implementation of getMostRecentDecl(), to be overridden by any
711 /// subclass that has a redeclaration chain.
712 virtual Decl *getMostRecentDeclImpl() { return this; }
713
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000714public:
715 /// \brief Iterates through all the redeclarations of the same decl.
716 class redecl_iterator {
717 /// Current - The current declaration.
718 Decl *Current;
719 Decl *Starter;
720
721 public:
David Blaikie581deb32012-06-06 20:45:41 +0000722 typedef Decl *value_type;
723 typedef const value_type &reference;
724 typedef const value_type *pointer;
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000725 typedef std::forward_iterator_tag iterator_category;
David Blaikie581deb32012-06-06 20:45:41 +0000726 typedef std::ptrdiff_t difference_type;
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000727
728 redecl_iterator() : Current(0) { }
729 explicit redecl_iterator(Decl *C) : Current(C), Starter(C) { }
730
David Blaikie581deb32012-06-06 20:45:41 +0000731 reference operator*() const { return Current; }
732 value_type operator->() const { return Current; }
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000733
734 redecl_iterator& operator++() {
735 assert(Current && "Advancing while iterator has reached end");
736 // Get either previous decl or latest decl.
737 Decl *Next = Current->getNextRedeclaration();
Argyrios Kyrtzidis22cbd2b2009-07-21 00:06:27 +0000738 assert(Next && "Should return next redeclaration or itself, never null!");
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000739 Current = (Next != Starter ? Next : 0);
740 return *this;
741 }
742
743 redecl_iterator operator++(int) {
744 redecl_iterator tmp(*this);
745 ++(*this);
746 return tmp;
747 }
748
Mike Stump1eb44332009-09-09 15:08:12 +0000749 friend bool operator==(redecl_iterator x, redecl_iterator y) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000750 return x.Current == y.Current;
751 }
Mike Stump1eb44332009-09-09 15:08:12 +0000752 friend bool operator!=(redecl_iterator x, redecl_iterator y) {
Argyrios Kyrtzidis1e4bc092009-07-18 08:50:35 +0000753 return x.Current != y.Current;
754 }
755 };
756
757 /// \brief Returns iterator for all the redeclarations of the same decl.
758 /// It will iterate at least once (when this decl is the only one).
759 redecl_iterator redecls_begin() const {
760 return redecl_iterator(const_cast<Decl*>(this));
761 }
762 redecl_iterator redecls_end() const { return redecl_iterator(); }
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +0000763
Douglas Gregorf785a7d2012-01-14 15:55:47 +0000764 /// \brief Retrieve the previous declaration that declares the same entity
765 /// as this declaration, or NULL if there is no previous declaration.
Douglas Gregoref96ee02012-01-14 16:38:05 +0000766 Decl *getPreviousDecl() { return getPreviousDeclImpl(); }
767
768 /// \brief Retrieve the most recent declaration that declares the same entity
769 /// as this declaration, or NULL if there is no previous declaration.
770 const Decl *getPreviousDecl() const {
771 return const_cast<Decl *>(this)->getPreviousDeclImpl();
772 }
Douglas Gregorf785a7d2012-01-14 15:55:47 +0000773
774 /// \brief Retrieve the most recent declaration that declares the same entity
775 /// as this declaration (which may be this declaration).
Douglas Gregoref96ee02012-01-14 16:38:05 +0000776 Decl *getMostRecentDecl() { return getMostRecentDeclImpl(); }
777
778 /// \brief Retrieve the most recent declaration that declares the same entity
779 /// as this declaration (which may be this declaration).
780 const Decl *getMostRecentDecl() const {
781 return const_cast<Decl *>(this)->getMostRecentDeclImpl();
782 }
783
Sebastian Redld3a413d2009-04-26 20:35:05 +0000784 /// getBody - If this Decl represents a declaration for a body of code,
785 /// such as a function or method definition, this method returns the
786 /// top-level Stmt* of that body. Otherwise this method returns null.
Douglas Gregorda2142f2011-02-19 18:51:44 +0000787 virtual Stmt* getBody() const { return 0; }
Sebastian Redld3a413d2009-04-26 20:35:05 +0000788
Manuel Klimeka438e682013-06-13 09:05:19 +0000789 /// \brief Returns true if this \c Decl represents a declaration for a body of
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000790 /// code, such as a function or method definition.
Manuel Klimeka438e682013-06-13 09:05:19 +0000791 /// Note that \c hasBody can also return true if any redeclaration of this
792 /// \c Decl represents a declaration for a body of code.
Douglas Gregorda2142f2011-02-19 18:51:44 +0000793 virtual bool hasBody() const { return getBody() != 0; }
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000794
Sebastian Redld3a413d2009-04-26 20:35:05 +0000795 /// getBodyRBrace - Gets the right brace of the body, if a body exists.
796 /// This works whether the body is a CompoundStmt or a CXXTryStmt.
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000797 SourceLocation getBodyRBrace() const;
Sebastian Redld3a413d2009-04-26 20:35:05 +0000798
Chris Lattner0ed844b2008-04-04 06:12:32 +0000799 // global temp stats (until we have a per-module visitor)
Sean Hunt9a555912010-05-30 07:21:58 +0000800 static void add(Kind k);
Daniel Dunbar02892a62012-03-05 21:42:49 +0000801 static void EnableStatistics();
Chris Lattner0ed844b2008-04-04 06:12:32 +0000802 static void PrintStats();
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Anders Carlsson67e33202009-06-13 00:08:58 +0000804 /// isTemplateParameter - Determines whether this declaration is a
Douglas Gregorf57172b2008-12-08 18:40:42 +0000805 /// template parameter.
806 bool isTemplateParameter() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Anders Carlsson67e33202009-06-13 00:08:58 +0000808 /// isTemplateParameter - Determines whether this declaration is a
809 /// template parameter pack.
810 bool isTemplateParameterPack() const;
Douglas Gregorf57172b2008-12-08 18:40:42 +0000811
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000812 /// \brief Whether this declaration is a parameter pack.
813 bool isParameterPack() const;
David Blaikieba243b52011-11-09 06:07:30 +0000814
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000815 /// \brief returns true if this declaration is a template
816 bool isTemplateDecl() const;
817
Douglas Gregore53060f2009-06-25 22:08:12 +0000818 /// \brief Whether this declaration is a function or function template.
819 bool isFunctionOrFunctionTemplate() const;
John McCall02cace72009-08-28 07:59:38 +0000820
821 /// \brief Changes the namespace of this declaration to reflect that it's
822 /// the object of a friend declaration.
823 ///
824 /// These declarations appear in the lexical context of the friending
825 /// class, but in the semantic context of the actual entity. This property
826 /// applies only to a specific decl object; other redeclarations of the
827 /// same entity may not (and probably don't) share this property.
828 void setObjectOfFriendDecl(bool PreviouslyDeclared) {
829 unsigned OldNS = IdentifierNamespace;
John McCallb0cb0222010-03-27 05:57:59 +0000830 assert((OldNS & (IDNS_Tag | IDNS_Ordinary |
831 IDNS_TagFriend | IDNS_OrdinaryFriend)) &&
832 "namespace includes neither ordinary nor tag");
John McCall0d6b1642010-04-23 18:46:30 +0000833 assert(!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type |
John McCallb0cb0222010-03-27 05:57:59 +0000834 IDNS_TagFriend | IDNS_OrdinaryFriend)) &&
835 "namespace includes other than ordinary or tag");
John McCall02cace72009-08-28 07:59:38 +0000836
John McCallb0cb0222010-03-27 05:57:59 +0000837 IdentifierNamespace = 0;
838 if (OldNS & (IDNS_Tag | IDNS_TagFriend)) {
John McCall02cace72009-08-28 07:59:38 +0000839 IdentifierNamespace |= IDNS_TagFriend;
John McCall0d6b1642010-04-23 18:46:30 +0000840 if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Tag | IDNS_Type;
John McCallb0cb0222010-03-27 05:57:59 +0000841 }
842
843 if (OldNS & (IDNS_Ordinary | IDNS_OrdinaryFriend)) {
John McCall02cace72009-08-28 07:59:38 +0000844 IdentifierNamespace |= IDNS_OrdinaryFriend;
John McCallb0cb0222010-03-27 05:57:59 +0000845 if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Ordinary;
846 }
John McCall02cace72009-08-28 07:59:38 +0000847 }
848
849 enum FriendObjectKind {
David Majnemer7841abf2013-06-26 21:28:41 +0000850 FOK_None, ///< Not a friend object.
851 FOK_Declared, ///< A friend of a previously-declared entity.
852 FOK_Undeclared ///< A friend of a previously-undeclared entity.
John McCall02cace72009-08-28 07:59:38 +0000853 };
854
855 /// \brief Determines whether this declaration is the object of a
856 /// friend declaration and, if so, what kind.
857 ///
858 /// There is currently no direct way to find the associated FriendDecl.
859 FriendObjectKind getFriendObjectKind() const {
David Majnemer2946ceb2013-06-23 03:38:10 +0000860 unsigned mask =
861 (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend));
John McCall02cace72009-08-28 07:59:38 +0000862 if (!mask) return FOK_None;
David Majnemer2946ceb2013-06-23 03:38:10 +0000863 return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ? FOK_Declared
864 : FOK_Undeclared);
John McCall02cace72009-08-28 07:59:38 +0000865 }
Mike Stump1eb44332009-09-09 15:08:12 +0000866
John McCall76d32642010-04-24 01:30:58 +0000867 /// Specifies that this declaration is a C++ overloaded non-member.
868 void setNonMemberOperator() {
869 assert(getKind() == Function || getKind() == FunctionTemplate);
870 assert((IdentifierNamespace & IDNS_Ordinary) &&
871 "visible non-member operators should be in ordinary namespace");
872 IdentifierNamespace |= IDNS_NonMemberOperator;
873 }
874
John McCall80cd64a2010-01-29 01:45:37 +0000875 static bool classofKind(Kind K) { return true; }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000876 static DeclContext *castToDeclContext(const Decl *);
877 static Decl *castFromDeclContext(const DeclContext *);
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Richard Trieu5cb3d692011-07-28 00:19:05 +0000879 void print(raw_ostream &Out, unsigned Indentation = 0,
880 bool PrintInstantiation = false) const;
Chris Lattner8cc488f2011-07-20 07:06:53 +0000881 void print(raw_ostream &Out, const PrintingPolicy &Policy,
Richard Trieu5cb3d692011-07-28 00:19:05 +0000882 unsigned Indentation = 0, bool PrintInstantiation = false) const;
Eli Friedman42f42c02009-05-30 04:20:30 +0000883 static void printGroup(Decl** Begin, unsigned NumDecls,
Chris Lattner8cc488f2011-07-20 07:06:53 +0000884 raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000885 unsigned Indentation = 0);
Alexander Kornienko559b9282012-07-26 17:11:45 +0000886 // Debuggers don't usually respect default arguments.
Benjamin Kramer42c72c22012-08-14 14:50:32 +0000887 LLVM_ATTRIBUTE_USED void dump() const;
Richard Trieu7ba443a2013-01-26 01:31:20 +0000888 // Same as dump(), but forces color printing.
889 LLVM_ATTRIBUTE_USED void dumpColor() const;
Alexander Kornienko559b9282012-07-26 17:11:45 +0000890 void dump(raw_ostream &Out) const;
891 // Debuggers don't usually respect default arguments.
Benjamin Kramer42c72c22012-08-14 14:50:32 +0000892 LLVM_ATTRIBUTE_USED void dumpXML() const;
Alexander Kornienko559b9282012-07-26 17:11:45 +0000893 void dumpXML(raw_ostream &OS) const;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000894
Chris Lattner81abbdd2009-03-21 06:27:31 +0000895private:
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000896 void setAttrsImpl(const AttrVec& Attrs, ASTContext &Ctx);
897 void setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
898 ASTContext &Ctx);
Chris Lattner81abbdd2009-03-21 06:27:31 +0000899
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000900protected:
901 ASTMutationListener *getASTMutationListener() const;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000902};
903
Douglas Gregoreaa95112011-12-14 23:59:32 +0000904/// \brief Determine whether two declarations declare the same entity.
905inline bool declaresSameEntity(const Decl *D1, const Decl *D2) {
Douglas Gregoreaa95112011-12-14 23:59:32 +0000906 if (!D1 || !D2)
907 return false;
908
Douglas Gregordec1cc42011-12-15 17:15:07 +0000909 if (D1 == D2)
910 return true;
911
Douglas Gregoreaa95112011-12-14 23:59:32 +0000912 return D1->getCanonicalDecl() == D2->getCanonicalDecl();
913}
914
Chris Lattner49f28ca2009-03-05 08:00:35 +0000915/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when
916/// doing something to a specific decl.
917class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
Daniel Dunbarc5236562009-11-21 09:05:59 +0000918 const Decl *TheDecl;
Chris Lattner49f28ca2009-03-05 08:00:35 +0000919 SourceLocation Loc;
920 SourceManager &SM;
921 const char *Message;
922public:
Daniel Dunbarc5236562009-11-21 09:05:59 +0000923 PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L,
Chris Lattner49f28ca2009-03-05 08:00:35 +0000924 SourceManager &sm, const char *Msg)
925 : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Chris Lattner8cc488f2011-07-20 07:06:53 +0000927 virtual void print(raw_ostream &OS) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000928};
929
David Blaikie3bc93e32012-12-19 00:45:41 +0000930typedef llvm::MutableArrayRef<NamedDecl*> DeclContextLookupResult;
John McCallea318642010-08-26 09:15:37 +0000931
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000932typedef ArrayRef<NamedDecl *> DeclContextLookupConstResult;
Chris Lattner49f28ca2009-03-05 08:00:35 +0000933
Chris Lattnerb048c982008-04-06 04:47:34 +0000934/// DeclContext - This is used only as base class of specific decl types that
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000935/// can act as declaration contexts. These decls are (only the top classes
936/// that directly derive from DeclContext are mentioned, not their subclasses):
Chris Lattner0ed844b2008-04-04 06:12:32 +0000937///
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +0000938/// TranslationUnitDecl
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000939/// NamespaceDecl
Chris Lattner0ed844b2008-04-04 06:12:32 +0000940/// FunctionDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000941/// TagDecl
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +0000942/// ObjCMethodDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000943/// ObjCContainerDecl
Douglas Gregor074149e2009-01-05 19:45:36 +0000944/// LinkageSpecDecl
Steve Naroff56ee6892008-10-08 17:01:13 +0000945/// BlockDecl
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +0000946///
Chris Lattnerb048c982008-04-06 04:47:34 +0000947class DeclContext {
Chris Lattner0ed844b2008-04-04 06:12:32 +0000948 /// DeclKind - This indicates which class this is.
John McCall35043e52010-10-19 05:43:52 +0000949 unsigned DeclKind : 8;
Chris Lattner0ed844b2008-04-04 06:12:32 +0000950
Douglas Gregor2cf26342009-04-09 22:27:44 +0000951 /// \brief Whether this declaration context also has some external
952 /// storage that contains additional declarations that are lexically
953 /// part of this context.
Richard Smithbbcd0f32013-02-07 03:37:08 +0000954 mutable bool ExternalLexicalStorage : 1;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000955
956 /// \brief Whether this declaration context also has some external
957 /// storage that contains additional declarations that are visible
958 /// in this context.
Richard Smithbbcd0f32013-02-07 03:37:08 +0000959 mutable bool ExternalVisibleStorage : 1;
960
961 /// \brief Whether this declaration context has had external visible
962 /// storage added since the last lookup. In this case, \c LookupPtr's
963 /// invariant may not hold and needs to be fixed before we perform
964 /// another lookup.
965 mutable bool NeedToReconcileExternalVisibleStorage : 1;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000966
Douglas Gregorc36c5402009-04-09 17:29:08 +0000967 /// \brief Pointer to the data structure used to lookup declarations
John McCall0c01d182010-03-24 05:22:00 +0000968 /// within this context (or a DependentStoredDeclsMap if this is a
Richard Smithc5d3e802012-03-16 06:12:59 +0000969 /// dependent context), and a bool indicating whether we have lazily
970 /// omitted any declarations from the map. We maintain the invariant
Richard Smithbbcd0f32013-02-07 03:37:08 +0000971 /// that, if the map contains an entry for a DeclarationName (and we
972 /// haven't lazily omitted anything), then it contains all relevant
973 /// entries for that name.
Richard Smithc5d3e802012-03-16 06:12:59 +0000974 mutable llvm::PointerIntPair<StoredDeclsMap*, 1, bool> LookupPtr;
Douglas Gregor44b43212008-12-11 16:49:14 +0000975
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000976protected:
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000977 /// FirstDecl - The first declaration stored within this declaration
978 /// context.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000979 mutable Decl *FirstDecl;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000980
981 /// LastDecl - The last declaration stored within this declaration
982 /// context. FIXME: We could probably cache this value somewhere
983 /// outside of the DeclContext, to reduce the size of DeclContext by
984 /// another pointer.
Douglas Gregor2cf26342009-04-09 22:27:44 +0000985 mutable Decl *LastDecl;
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +0000986
John McCall76bd1f32010-06-01 09:23:16 +0000987 friend class ExternalASTSource;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +0000988 friend class ASTWriter;
John McCall76bd1f32010-06-01 09:23:16 +0000989
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000990 /// \brief Build up a chain of declarations.
991 ///
992 /// \returns the first/last pair of declarations.
993 static std::pair<Decl *, Decl *>
Bill Wendling341785e2012-02-22 09:51:33 +0000994 BuildDeclChain(ArrayRef<Decl*> Decls, bool FieldsAlreadyLoaded);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000995
Richard Smithbbcd0f32013-02-07 03:37:08 +0000996 DeclContext(Decl::Kind K)
997 : DeclKind(K), ExternalLexicalStorage(false),
998 ExternalVisibleStorage(false),
999 NeedToReconcileExternalVisibleStorage(false), LookupPtr(0, false),
1000 FirstDecl(0), LastDecl(0) {}
Douglas Gregor44b43212008-12-11 16:49:14 +00001001
Chris Lattner0ed844b2008-04-04 06:12:32 +00001002public:
Douglas Gregor44b43212008-12-11 16:49:14 +00001003 ~DeclContext();
1004
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +00001005 Decl::Kind getDeclKind() const {
John McCall35043e52010-10-19 05:43:52 +00001006 return static_cast<Decl::Kind>(DeclKind);
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +00001007 }
Steve Naroff0a473932009-01-20 19:53:53 +00001008 const char *getDeclKindName() const;
Argyrios Kyrtzidis9b9ca012009-01-13 13:11:58 +00001009
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +00001010 /// getParent - Returns the containing DeclContext.
Chris Lattner0cf2b192009-03-27 19:19:59 +00001011 DeclContext *getParent() {
Argyrios Kyrtzidis305ec422009-02-17 20:26:05 +00001012 return cast<Decl>(this)->getDeclContext();
1013 }
Chris Lattner0cf2b192009-03-27 19:19:59 +00001014 const DeclContext *getParent() const {
1015 return const_cast<DeclContext*>(this)->getParent();
Argyrios Kyrtzidisd2595ec2008-10-12 18:40:01 +00001016 }
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +00001018 /// getLexicalParent - Returns the containing lexical DeclContext. May be
Douglas Gregor44b43212008-12-11 16:49:14 +00001019 /// different from getParent, e.g.:
1020 ///
1021 /// namespace A {
1022 /// struct S;
1023 /// }
1024 /// struct A::S {}; // getParent() == namespace 'A'
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +00001025 /// // getLexicalParent() == translation unit
1026 ///
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +00001027 DeclContext *getLexicalParent() {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001028 return cast<Decl>(this)->getLexicalDeclContext();
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +00001029 }
Chris Lattner0cf2b192009-03-27 19:19:59 +00001030 const DeclContext *getLexicalParent() const {
1031 return const_cast<DeclContext*>(this)->getLexicalParent();
Mike Stump1eb44332009-09-09 15:08:12 +00001032 }
Argyrios Kyrtzidis048f30a2009-06-30 02:35:38 +00001033
Douglas Gregore942bbe2009-09-10 16:57:35 +00001034 DeclContext *getLookupParent();
David Blaikieba243b52011-11-09 06:07:30 +00001035
Douglas Gregore942bbe2009-09-10 16:57:35 +00001036 const DeclContext *getLookupParent() const {
1037 return const_cast<DeclContext*>(this)->getLookupParent();
1038 }
David Blaikieba243b52011-11-09 06:07:30 +00001039
Argyrios Kyrtzidis048f30a2009-06-30 02:35:38 +00001040 ASTContext &getParentASTContext() const {
1041 return cast<Decl>(this)->getASTContext();
1042 }
1043
John McCallaab9e312011-02-22 22:25:23 +00001044 bool isClosure() const {
1045 return DeclKind == Decl::Block;
1046 }
1047
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001048 bool isObjCContainer() const {
Fariborz Jahanian80f77532011-08-22 17:13:51 +00001049 switch (DeclKind) {
1050 case Decl::ObjCCategory:
1051 case Decl::ObjCCategoryImpl:
1052 case Decl::ObjCImplementation:
1053 case Decl::ObjCInterface:
1054 case Decl::ObjCProtocol:
1055 return true;
1056 }
1057 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001058 }
1059
Chris Lattner0ed844b2008-04-04 06:12:32 +00001060 bool isFunctionOrMethod() const {
1061 switch (DeclKind) {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001062 case Decl::Block:
Tareq A. Siraj6afcf882013-04-16 19:37:38 +00001063 case Decl::Captured:
Chris Lattner0cf2b192009-03-27 19:19:59 +00001064 case Decl::ObjCMethod:
1065 return true;
1066 default:
Sean Hunt9a555912010-05-30 07:21:58 +00001067 return DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction;
Chris Lattner0ed844b2008-04-04 06:12:32 +00001068 }
1069 }
1070
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +00001071 bool isFileContext() const {
1072 return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
1073 }
1074
Douglas Gregor5f2bfd42009-02-13 00:10:09 +00001075 bool isTranslationUnit() const {
1076 return DeclKind == Decl::TranslationUnit;
1077 }
1078
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001079 bool isRecord() const {
Sean Hunt9a555912010-05-30 07:21:58 +00001080 return DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord;
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001081 }
1082
Douglas Gregor44b43212008-12-11 16:49:14 +00001083 bool isNamespace() const {
1084 return DeclKind == Decl::Namespace;
1085 }
1086
Sebastian Redl410c4f22010-08-31 20:53:31 +00001087 bool isInlineNamespace() const;
1088
Douglas Gregorbc221632009-05-28 16:34:51 +00001089 /// \brief Determines whether this context is dependent on a
1090 /// template parameter.
1091 bool isDependentContext() const;
1092
Douglas Gregor074149e2009-01-05 19:45:36 +00001093 /// isTransparentContext - Determines whether this context is a
1094 /// "transparent" context, meaning that the members declared in this
1095 /// context are semantically declared in the nearest enclosing
1096 /// non-transparent (opaque) context but are lexically declared in
1097 /// this context. For example, consider the enumerators of an
Mike Stump1eb44332009-09-09 15:08:12 +00001098 /// enumeration type:
Douglas Gregor074149e2009-01-05 19:45:36 +00001099 /// @code
1100 /// enum E {
Mike Stump1eb44332009-09-09 15:08:12 +00001101 /// Val1
Douglas Gregor074149e2009-01-05 19:45:36 +00001102 /// };
1103 /// @endcode
1104 /// Here, E is a transparent context, so its enumerator (Val1) will
1105 /// appear (semantically) that it is in the same context of E.
1106 /// Examples of transparent contexts include: enumerations (except for
Sebastian Redl410c4f22010-08-31 20:53:31 +00001107 /// C++0x scoped enums), and C++ linkage specifications.
Douglas Gregor074149e2009-01-05 19:45:36 +00001108 bool isTransparentContext() const;
1109
Douglas Gregor61481da2009-09-01 17:22:34 +00001110 /// \brief Determine whether this declaration context is equivalent
1111 /// to the declaration context DC.
Sebastian Redl7a126a42010-08-31 00:36:30 +00001112 bool Equals(const DeclContext *DC) const {
Douglas Gregordbdf5e72010-03-15 15:26:48 +00001113 return DC && this->getPrimaryContext() == DC->getPrimaryContext();
Douglas Gregor61481da2009-09-01 17:22:34 +00001114 }
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Douglas Gregor6dd38da2009-08-27 06:03:53 +00001116 /// \brief Determine whether this declaration context encloses the
1117 /// declaration context DC.
Sebastian Redl7a126a42010-08-31 00:36:30 +00001118 bool Encloses(const DeclContext *DC) const;
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +00001119
John McCall4b9c2d22011-11-06 09:01:30 +00001120 /// \brief Find the nearest non-closure ancestor of this context,
1121 /// i.e. the innermost semantic parent of this context which is not
1122 /// a closure. A context may be its own non-closure ancestor.
John McCallf5ebf9b2013-05-03 07:33:41 +00001123 Decl *getNonClosureAncestor();
1124 const Decl *getNonClosureAncestor() const {
John McCall4b9c2d22011-11-06 09:01:30 +00001125 return const_cast<DeclContext*>(this)->getNonClosureAncestor();
1126 }
1127
Douglas Gregor44b43212008-12-11 16:49:14 +00001128 /// getPrimaryContext - There may be many different
1129 /// declarations of the same entity (including forward declarations
1130 /// of classes, multiple definitions of namespaces, etc.), each with
1131 /// a different set of declarations. This routine returns the
1132 /// "primary" DeclContext structure, which will contain the
1133 /// information needed to perform name lookup into this context.
Steve Naroff0701bbb2009-01-08 17:28:14 +00001134 DeclContext *getPrimaryContext();
John McCall0c01d182010-03-24 05:22:00 +00001135 const DeclContext *getPrimaryContext() const {
1136 return const_cast<DeclContext*>(this)->getPrimaryContext();
1137 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001138
Sebastian Redl7a126a42010-08-31 00:36:30 +00001139 /// getRedeclContext - Retrieve the context in which an entity conflicts with
1140 /// other entities of the same name, or where it is a redeclaration if the
Sebastian Redl410c4f22010-08-31 20:53:31 +00001141 /// two entities are compatible. This skips through transparent contexts.
Sebastian Redl7a126a42010-08-31 00:36:30 +00001142 DeclContext *getRedeclContext();
1143 const DeclContext *getRedeclContext() const {
1144 return const_cast<DeclContext *>(this)->getRedeclContext();
Douglas Gregor17a9b9e2009-01-07 02:48:43 +00001145 }
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Douglas Gregor88b70942009-02-25 22:02:03 +00001147 /// \brief Retrieve the nearest enclosing namespace context.
1148 DeclContext *getEnclosingNamespaceContext();
1149 const DeclContext *getEnclosingNamespaceContext() const {
1150 return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
1151 }
1152
Sebastian Redl7a126a42010-08-31 00:36:30 +00001153 /// \brief Test if this context is part of the enclosing namespace set of
1154 /// the context NS, as defined in C++0x [namespace.def]p9. If either context
1155 /// isn't a namespace, this is equivalent to Equals().
1156 ///
1157 /// The enclosing namespace set of a namespace is the namespace and, if it is
1158 /// inline, its enclosing namespace, recursively.
1159 bool InEnclosingNamespaceSetOf(const DeclContext *NS) const;
1160
James Dennett18483f52012-06-15 08:37:50 +00001161 /// \brief Collects all of the declaration contexts that are semantically
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001162 /// connected to this declaration context.
1163 ///
1164 /// For declaration contexts that have multiple semantically connected but
1165 /// syntactically distinct contexts, such as C++ namespaces, this routine
1166 /// retrieves the complete set of such declaration contexts in source order.
1167 /// For example, given:
1168 ///
1169 /// \code
Douglas Gregor44b43212008-12-11 16:49:14 +00001170 /// namespace N {
1171 /// int x;
1172 /// }
1173 /// namespace N {
1174 /// int y;
1175 /// }
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001176 /// \endcode
1177 ///
1178 /// The \c Contexts parameter will contain both definitions of N.
1179 ///
1180 /// \param Contexts Will be cleared and set to the set of declaration
1181 /// contexts that are semanticaly connected to this declaration context,
1182 /// in source order, including this context (which may be the only result,
1183 /// for non-namespace contexts).
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001184 void collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts);
Douglas Gregor44b43212008-12-11 16:49:14 +00001185
1186 /// decl_iterator - Iterates through the declarations stored
1187 /// within this context.
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001188 class decl_iterator {
1189 /// Current - The current declaration.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001190 Decl *Current;
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001191
1192 public:
David Blaikie581deb32012-06-06 20:45:41 +00001193 typedef Decl *value_type;
1194 typedef const value_type &reference;
1195 typedef const value_type *pointer;
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001196 typedef std::forward_iterator_tag iterator_category;
1197 typedef std::ptrdiff_t difference_type;
1198
1199 decl_iterator() : Current(0) { }
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001200 explicit decl_iterator(Decl *C) : Current(C) { }
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001201
1202 reference operator*() const { return Current; }
David Blaikie581deb32012-06-06 20:45:41 +00001203 // This doesn't meet the iterator requirements, but it's convenient
1204 value_type operator->() const { return Current; }
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001205
Chris Lattner96f44682009-03-28 05:59:45 +00001206 decl_iterator& operator++() {
Chris Lattner244a67d2009-03-28 06:04:26 +00001207 Current = Current->getNextDeclInContext();
Chris Lattner96f44682009-03-28 05:59:45 +00001208 return *this;
1209 }
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001210
1211 decl_iterator operator++(int) {
1212 decl_iterator tmp(*this);
1213 ++(*this);
1214 return tmp;
1215 }
1216
Mike Stump1eb44332009-09-09 15:08:12 +00001217 friend bool operator==(decl_iterator x, decl_iterator y) {
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001218 return x.Current == y.Current;
1219 }
Mike Stump1eb44332009-09-09 15:08:12 +00001220 friend bool operator!=(decl_iterator x, decl_iterator y) {
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001221 return x.Current != y.Current;
1222 }
1223 };
Douglas Gregor44b43212008-12-11 16:49:14 +00001224
Douglas Gregor44b43212008-12-11 16:49:14 +00001225 /// decls_begin/decls_end - Iterate over the declarations stored in
Mike Stump1eb44332009-09-09 15:08:12 +00001226 /// this context.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001227 decl_iterator decls_begin() const;
David Blaikie581deb32012-06-06 20:45:41 +00001228 decl_iterator decls_end() const { return decl_iterator(); }
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001229 bool decls_empty() const;
Douglas Gregor44b43212008-12-11 16:49:14 +00001230
Sebastian Redl681d7232010-07-27 00:17:23 +00001231 /// noload_decls_begin/end - Iterate over the declarations stored in this
1232 /// context that are currently loaded; don't attempt to retrieve anything
1233 /// from an external source.
1234 decl_iterator noload_decls_begin() const;
David Blaikie581deb32012-06-06 20:45:41 +00001235 decl_iterator noload_decls_end() const { return decl_iterator(); }
Sebastian Redl681d7232010-07-27 00:17:23 +00001236
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001237 /// specific_decl_iterator - Iterates over a subrange of
1238 /// declarations stored in a DeclContext, providing only those that
Douglas Gregor669c9a22009-02-02 18:25:48 +00001239 /// are of type SpecificDecl (or a class derived from it). This
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001240 /// iterator is used, for example, to provide iteration over just
Douglas Gregor669c9a22009-02-02 18:25:48 +00001241 /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001242 template<typename SpecificDecl>
1243 class specific_decl_iterator {
1244 /// Current - The current, underlying declaration iterator, which
Douglas Gregord6f0b4e2009-02-02 17:56:05 +00001245 /// will either be NULL or will point to a declaration of
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001246 /// type SpecificDecl.
1247 DeclContext::decl_iterator Current;
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001249 /// SkipToNextDecl - Advances the current position up to the next
1250 /// declaration of type SpecificDecl that also meets the criteria
1251 /// required by Acceptable.
1252 void SkipToNextDecl() {
Douglas Gregor669c9a22009-02-02 18:25:48 +00001253 while (*Current && !isa<SpecificDecl>(*Current))
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001254 ++Current;
1255 }
1256
1257 public:
David Blaikie581deb32012-06-06 20:45:41 +00001258 typedef SpecificDecl *value_type;
1259 // TODO: Add reference and pointer typedefs (with some appropriate proxy
1260 // type) if we ever have a need for them.
1261 typedef void reference;
1262 typedef void pointer;
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001263 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1264 difference_type;
1265 typedef std::forward_iterator_tag iterator_category;
1266
Douglas Gregor669c9a22009-02-02 18:25:48 +00001267 specific_decl_iterator() : Current() { }
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001268
1269 /// specific_decl_iterator - Construct a new iterator over a
Douglas Gregord6f0b4e2009-02-02 17:56:05 +00001270 /// subset of the declarations the range [C,
1271 /// end-of-declarations). If A is non-NULL, it is a pointer to a
1272 /// member function of SpecificDecl that should return true for
1273 /// all of the SpecificDecl instances that will be in the subset
1274 /// of iterators. For example, if you want Objective-C instance
1275 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1276 /// &ObjCMethodDecl::isInstanceMethod.
Douglas Gregor669c9a22009-02-02 18:25:48 +00001277 explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001278 SkipToNextDecl();
1279 }
1280
David Blaikie581deb32012-06-06 20:45:41 +00001281 value_type operator*() const { return cast<SpecificDecl>(*Current); }
1282 // This doesn't meet the iterator requirements, but it's convenient
1283 value_type operator->() const { return **this; }
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001284
1285 specific_decl_iterator& operator++() {
1286 ++Current;
1287 SkipToNextDecl();
1288 return *this;
1289 }
1290
1291 specific_decl_iterator operator++(int) {
1292 specific_decl_iterator tmp(*this);
1293 ++(*this);
1294 return tmp;
1295 }
Mike Stump1eb44332009-09-09 15:08:12 +00001296
David Blaikieba243b52011-11-09 06:07:30 +00001297 friend bool operator==(const specific_decl_iterator& x,
1298 const specific_decl_iterator& y) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001299 return x.Current == y.Current;
1300 }
Mike Stump1eb44332009-09-09 15:08:12 +00001301
David Blaikieba243b52011-11-09 06:07:30 +00001302 friend bool operator!=(const specific_decl_iterator& x,
1303 const specific_decl_iterator& y) {
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001304 return x.Current != y.Current;
1305 }
1306 };
1307
Douglas Gregor669c9a22009-02-02 18:25:48 +00001308 /// \brief Iterates over a filtered subrange of declarations stored
1309 /// in a DeclContext.
1310 ///
1311 /// This iterator visits only those declarations that are of type
1312 /// SpecificDecl (or a class derived from it) and that meet some
1313 /// additional run-time criteria. This iterator is used, for
1314 /// example, to provide access to the instance methods within an
1315 /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
1316 /// Acceptable = ObjCMethodDecl::isInstanceMethod).
1317 template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
1318 class filtered_decl_iterator {
1319 /// Current - The current, underlying declaration iterator, which
1320 /// will either be NULL or will point to a declaration of
1321 /// type SpecificDecl.
1322 DeclContext::decl_iterator Current;
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Douglas Gregor669c9a22009-02-02 18:25:48 +00001324 /// SkipToNextDecl - Advances the current position up to the next
1325 /// declaration of type SpecificDecl that also meets the criteria
1326 /// required by Acceptable.
1327 void SkipToNextDecl() {
1328 while (*Current &&
1329 (!isa<SpecificDecl>(*Current) ||
1330 (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
1331 ++Current;
1332 }
1333
1334 public:
David Blaikie581deb32012-06-06 20:45:41 +00001335 typedef SpecificDecl *value_type;
1336 // TODO: Add reference and pointer typedefs (with some appropriate proxy
1337 // type) if we ever have a need for them.
1338 typedef void reference;
1339 typedef void pointer;
Douglas Gregor669c9a22009-02-02 18:25:48 +00001340 typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1341 difference_type;
1342 typedef std::forward_iterator_tag iterator_category;
1343
1344 filtered_decl_iterator() : Current() { }
1345
David Blaikiebd4fa452012-05-01 00:48:43 +00001346 /// filtered_decl_iterator - Construct a new iterator over a
Douglas Gregor669c9a22009-02-02 18:25:48 +00001347 /// subset of the declarations the range [C,
1348 /// end-of-declarations). If A is non-NULL, it is a pointer to a
1349 /// member function of SpecificDecl that should return true for
1350 /// all of the SpecificDecl instances that will be in the subset
1351 /// of iterators. For example, if you want Objective-C instance
1352 /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1353 /// &ObjCMethodDecl::isInstanceMethod.
1354 explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
1355 SkipToNextDecl();
1356 }
1357
David Blaikie581deb32012-06-06 20:45:41 +00001358 value_type operator*() const { return cast<SpecificDecl>(*Current); }
1359 value_type operator->() const { return cast<SpecificDecl>(*Current); }
Douglas Gregor669c9a22009-02-02 18:25:48 +00001360
1361 filtered_decl_iterator& operator++() {
1362 ++Current;
1363 SkipToNextDecl();
1364 return *this;
1365 }
1366
1367 filtered_decl_iterator operator++(int) {
1368 filtered_decl_iterator tmp(*this);
1369 ++(*this);
1370 return tmp;
1371 }
Mike Stump1eb44332009-09-09 15:08:12 +00001372
David Blaikieba243b52011-11-09 06:07:30 +00001373 friend bool operator==(const filtered_decl_iterator& x,
1374 const filtered_decl_iterator& y) {
Douglas Gregor669c9a22009-02-02 18:25:48 +00001375 return x.Current == y.Current;
1376 }
Mike Stump1eb44332009-09-09 15:08:12 +00001377
David Blaikieba243b52011-11-09 06:07:30 +00001378 friend bool operator!=(const filtered_decl_iterator& x,
1379 const filtered_decl_iterator& y) {
Douglas Gregor669c9a22009-02-02 18:25:48 +00001380 return x.Current != y.Current;
1381 }
1382 };
1383
Douglas Gregor40f4e692009-01-20 16:54:50 +00001384 /// @brief Add the declaration D into this context.
1385 ///
1386 /// This routine should be invoked when the declaration D has first
1387 /// been declared, to place D into the context where it was
1388 /// (lexically) defined. Every declaration must be added to one
1389 /// (and only one!) context, where it can be visited via
1390 /// [decls_begin(), decls_end()). Once a declaration has been added
1391 /// to its lexical context, the corresponding DeclContext owns the
1392 /// declaration.
1393 ///
1394 /// If D is also a NamedDecl, it will be made visible within its
1395 /// semantic context via makeDeclVisibleInContext.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001396 void addDecl(Decl *D);
Sean Callanan381509e2011-10-21 16:15:18 +00001397
1398 /// @brief Add the declaration D into this context, but suppress
1399 /// searches for external declarations with the same name.
1400 ///
1401 /// Although analogous in function to addDecl, this removes an
1402 /// important check. This is only useful if the Decl is being
1403 /// added in response to an external search; in all other cases,
1404 /// addDecl() is the right function to use.
1405 /// See the ASTImporter for use cases.
Sean Callanan9faf8102011-10-21 02:57:43 +00001406 void addDeclInternal(Decl *D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001407
John McCall3f9a8a62009-08-11 06:59:38 +00001408 /// @brief Add the declaration D to this context without modifying
1409 /// any lookup tables.
1410 ///
1411 /// This is useful for some operations in dependent contexts where
1412 /// the semantic context might not be dependent; this basically
1413 /// only happens with friends.
1414 void addHiddenDecl(Decl *D);
1415
John McCall9f54ad42009-12-10 09:41:52 +00001416 /// @brief Removes a declaration from this context.
1417 void removeDecl(Decl *D);
Sean Callanancd904e82013-05-04 02:04:27 +00001418
1419 /// @brief Checks whether a declaration is in this context.
1420 bool containsDecl(Decl *D) const;
John McCall9f54ad42009-12-10 09:41:52 +00001421
Douglas Gregor44b43212008-12-11 16:49:14 +00001422 /// lookup_iterator - An iterator that provides access to the results
1423 /// of looking up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001424 typedef NamedDecl **lookup_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +00001425
1426 /// lookup_const_iterator - An iterator that provides non-mutable
1427 /// access to the results of lookup up a name within this context.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001428 typedef NamedDecl * const * lookup_const_iterator;
Douglas Gregor44b43212008-12-11 16:49:14 +00001429
John McCallea318642010-08-26 09:15:37 +00001430 typedef DeclContextLookupResult lookup_result;
1431 typedef DeclContextLookupConstResult lookup_const_result;
Douglas Gregor44b43212008-12-11 16:49:14 +00001432
1433 /// lookup - Find the declarations (if any) with the given Name in
1434 /// this context. Returns a range of iterators that contains all of
Douglas Gregor40f4e692009-01-20 16:54:50 +00001435 /// the declarations with this name, with object, function, member,
1436 /// and enumerator names preceding any tag name. Note that this
1437 /// routine will not look into parent contexts.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001438 lookup_result lookup(DeclarationName Name);
Douglas Gregor514d3b62012-05-03 23:18:44 +00001439 lookup_const_result lookup(DeclarationName Name) const {
1440 return const_cast<DeclContext*>(this)->lookup(Name);
1441 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001442
Douglas Gregorb75a3452011-10-15 00:10:27 +00001443 /// \brief A simplistic name lookup mechanism that performs name lookup
1444 /// into this declaration context without consulting the external source.
1445 ///
David Blaikieba243b52011-11-09 06:07:30 +00001446 /// This function should almost never be used, because it subverts the
Douglas Gregorb75a3452011-10-15 00:10:27 +00001447 /// usual relationship between a DeclContext and the external source.
1448 /// See the ASTImporter for the (few, but important) use cases.
David Blaikieba243b52011-11-09 06:07:30 +00001449 void localUncachedLookup(DeclarationName Name,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001450 SmallVectorImpl<NamedDecl *> &Results);
David Blaikieba243b52011-11-09 06:07:30 +00001451
Douglas Gregor40f4e692009-01-20 16:54:50 +00001452 /// @brief Makes a declaration visible within this context.
1453 ///
1454 /// This routine makes the declaration D visible to name lookup
1455 /// within this context and, if this is a transparent context,
1456 /// within its parent contexts up to the first enclosing
1457 /// non-transparent context. Making a declaration visible within a
1458 /// context does not transfer ownership of a declaration, and a
1459 /// declaration can be visible in many contexts that aren't its
1460 /// lexical context.
1461 ///
1462 /// If D is a redeclaration of an existing declaration that is
1463 /// visible from this context, as determined by
1464 /// NamedDecl::declarationReplaces, the previous declaration will be
1465 /// replaced with D.
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001466 void makeDeclVisibleInContext(NamedDecl *D);
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +00001467
Nick Lewycky173a37a2012-04-03 21:44:08 +00001468 /// all_lookups_iterator - An iterator that provides a view over the results
1469 /// of looking up every possible name.
1470 class all_lookups_iterator;
1471
Richard Smith6e322c02013-06-22 21:49:40 +00001472 /// \brief Iterators over all possible lookups within this context.
Nick Lewycky173a37a2012-04-03 21:44:08 +00001473 all_lookups_iterator lookups_begin() const;
Nick Lewycky173a37a2012-04-03 21:44:08 +00001474 all_lookups_iterator lookups_end() const;
1475
Richard Smith6e322c02013-06-22 21:49:40 +00001476 /// \brief Iterators over all possible lookups within this context that are
1477 /// currently loaded; don't attempt to retrieve anything from an external
1478 /// source.
1479 all_lookups_iterator noload_lookups_begin() const;
1480 all_lookups_iterator noload_lookups_end() const;
1481
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001482 /// udir_iterator - Iterates through the using-directives stored
1483 /// within this context.
1484 typedef UsingDirectiveDecl * const * udir_iterator;
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001486 typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range;
1487
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001488 udir_iterator_range getUsingDirectives() const;
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001489
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001490 udir_iterator using_directives_begin() const {
1491 return getUsingDirectives().first;
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001492 }
1493
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001494 udir_iterator using_directives_end() const {
1495 return getUsingDirectives().second;
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001496 }
1497
John McCall0c01d182010-03-24 05:22:00 +00001498 // These are all defined in DependentDiagnostic.h.
1499 class ddiag_iterator;
1500 inline ddiag_iterator ddiag_begin() const;
1501 inline ddiag_iterator ddiag_end() const;
1502
Douglas Gregorc2ee10d2009-04-07 17:20:56 +00001503 // Low-level accessors
Richard Smith5b8d0af2013-04-03 22:50:34 +00001504
Sean Callanand5a20c12012-08-23 21:16:40 +00001505 /// \brief Mark the lookup table as needing to be built. This should be
Richard Smith63a95142013-02-12 02:32:35 +00001506 /// used only if setHasExternalLexicalStorage() has been called on any
1507 /// decl context for which this is the primary context.
Sean Callanand5a20c12012-08-23 21:16:40 +00001508 void setMustBuildLookupTable() {
Sean Callanand5a20c12012-08-23 21:16:40 +00001509 LookupPtr.setInt(true);
1510 }
Douglas Gregorc2ee10d2009-04-07 17:20:56 +00001511
Douglas Gregorc2ee10d2009-04-07 17:20:56 +00001512 /// \brief Retrieve the internal representation of the lookup structure.
Richard Smithc5d3e802012-03-16 06:12:59 +00001513 /// This may omit some names if we are lazily building the structure.
1514 StoredDeclsMap *getLookupPtr() const { return LookupPtr.getPointer(); }
1515
1516 /// \brief Ensure the lookup structure is fully-built and return it.
1517 StoredDeclsMap *buildLookup();
Douglas Gregorc2ee10d2009-04-07 17:20:56 +00001518
Douglas Gregor2cf26342009-04-09 22:27:44 +00001519 /// \brief Whether this DeclContext has external storage containing
1520 /// additional declarations that are lexically in this context.
1521 bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; }
1522
1523 /// \brief State whether this DeclContext has external storage for
1524 /// declarations lexically in this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001525 void setHasExternalLexicalStorage(bool ES = true) {
1526 ExternalLexicalStorage = ES;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001527 }
1528
1529 /// \brief Whether this DeclContext has external storage containing
1530 /// additional declarations that are visible in this context.
1531 bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; }
1532
1533 /// \brief State whether this DeclContext has external storage for
1534 /// declarations visible in this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001535 void setHasExternalVisibleStorage(bool ES = true) {
1536 ExternalVisibleStorage = ES;
Richard Smith88963392013-02-11 22:02:16 +00001537 if (ES && LookupPtr.getPointer())
Richard Smithbbcd0f32013-02-07 03:37:08 +00001538 NeedToReconcileExternalVisibleStorage = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001539 }
1540
Douglas Gregor2ea054f2011-08-26 22:04:51 +00001541 /// \brief Determine whether the given declaration is stored in the list of
1542 /// declarations lexically within this context.
1543 bool isDeclInLexicalTraversal(const Decl *D) const {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001544 return D && (D->NextInContextAndBits.getPointer() || D == FirstDecl ||
1545 D == LastDecl);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00001546 }
David Blaikieba243b52011-11-09 06:07:30 +00001547
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +00001548 static bool classof(const Decl *D);
Chris Lattnerb048c982008-04-06 04:47:34 +00001549 static bool classof(const DeclContext *D) { return true; }
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +00001550
Argyrios Kyrtzidis0ee7d942012-02-21 05:04:44 +00001551 LLVM_ATTRIBUTE_USED void dumpDeclContext() const;
Richard Smith6e322c02013-06-22 21:49:40 +00001552 LLVM_ATTRIBUTE_USED void dumpLookups() const;
Richard Smithab297cc2013-06-24 01:45:33 +00001553 LLVM_ATTRIBUTE_USED void dumpLookups(llvm::raw_ostream &OS) const;
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +00001554
Argyrios Kyrtzidis76435362008-06-10 01:32:09 +00001555private:
Richard Smithbbcd0f32013-02-07 03:37:08 +00001556 void reconcileExternalVisibleStorage();
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001557 void LoadLexicalDeclsFromExternalStorage() const;
David Blaikieba243b52011-11-09 06:07:30 +00001558
1559 /// @brief Makes a declaration visible within this context, but
Sean Callanan381509e2011-10-21 16:15:18 +00001560 /// suppresses searches for external declarations with the same
1561 /// name.
1562 ///
1563 /// Analogous to makeDeclVisibleInContext, but for the exclusive
1564 /// use of addDeclInternal().
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001565 void makeDeclVisibleInContextInternal(NamedDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001566
John McCall0c01d182010-03-24 05:22:00 +00001567 friend class DependentDiagnostic;
1568 StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const;
1569
Richard Smithc5d3e802012-03-16 06:12:59 +00001570 void buildLookupImpl(DeclContext *DCtx);
1571 void makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1572 bool Rediscoverable);
Sean Callanan9faf8102011-10-21 02:57:43 +00001573 void makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001574};
1575
Douglas Gregorf57172b2008-12-08 18:40:42 +00001576inline bool Decl::isTemplateParameter() const {
Douglas Gregor79c22782010-01-16 20:21:20 +00001577 return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm ||
1578 getKind() == TemplateTemplateParm;
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001579}
1580
John McCall80cd64a2010-01-29 01:45:37 +00001581// Specialization selected when ToTy is not a known subclass of DeclContext.
1582template <class ToTy,
1583 bool IsKnownSubtype = ::llvm::is_base_of< DeclContext, ToTy>::value>
1584struct cast_convert_decl_context {
1585 static const ToTy *doit(const DeclContext *Val) {
1586 return static_cast<const ToTy*>(Decl::castFromDeclContext(Val));
1587 }
1588
1589 static ToTy *doit(DeclContext *Val) {
1590 return static_cast<ToTy*>(Decl::castFromDeclContext(Val));
1591 }
1592};
1593
1594// Specialization selected when ToTy is a known subclass of DeclContext.
1595template <class ToTy>
1596struct cast_convert_decl_context<ToTy, true> {
1597 static const ToTy *doit(const DeclContext *Val) {
1598 return static_cast<const ToTy*>(Val);
1599 }
1600
1601 static ToTy *doit(DeclContext *Val) {
1602 return static_cast<ToTy*>(Val);
1603 }
1604};
1605
1606
Chris Lattner0ed844b2008-04-04 06:12:32 +00001607} // end clang.
1608
1609namespace llvm {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001610
John McCall80cd64a2010-01-29 01:45:37 +00001611/// isa<T>(DeclContext*)
Eli Friedman4d509342011-05-21 19:15:39 +00001612template <typename To>
1613struct isa_impl<To, ::clang::DeclContext> {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001614 static bool doit(const ::clang::DeclContext &Val) {
Eli Friedman4d509342011-05-21 19:15:39 +00001615 return To::classofKind(Val.getDeclKind());
Chris Lattner0ed844b2008-04-04 06:12:32 +00001616 }
1617};
Chris Lattner0ed844b2008-04-04 06:12:32 +00001618
John McCall80cd64a2010-01-29 01:45:37 +00001619/// cast<T>(DeclContext*)
1620template<class ToTy>
1621struct cast_convert_val<ToTy,
1622 const ::clang::DeclContext,const ::clang::DeclContext> {
1623 static const ToTy &doit(const ::clang::DeclContext &Val) {
1624 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1625 }
1626};
1627template<class ToTy>
1628struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> {
1629 static ToTy &doit(::clang::DeclContext &Val) {
1630 return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1631 }
1632};
1633template<class ToTy>
1634struct cast_convert_val<ToTy,
1635 const ::clang::DeclContext*, const ::clang::DeclContext*> {
1636 static const ToTy *doit(const ::clang::DeclContext *Val) {
1637 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1638 }
1639};
1640template<class ToTy>
1641struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> {
1642 static ToTy *doit(::clang::DeclContext *Val) {
1643 return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1644 }
1645};
1646
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001647/// Implement cast_convert_val for Decl -> DeclContext conversions.
Chris Lattner0ed844b2008-04-04 06:12:32 +00001648template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001649struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
Chris Lattnerb048c982008-04-06 04:47:34 +00001650 static ::clang::DeclContext &doit(const FromTy &Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001651 return *FromTy::castToDeclContext(&Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001652 }
1653};
Chris Lattner0ed844b2008-04-04 06:12:32 +00001654
1655template<class FromTy>
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001656struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
Chris Lattnerb048c982008-04-06 04:47:34 +00001657 static ::clang::DeclContext *doit(const FromTy *Val) {
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001658 return FromTy::castToDeclContext(Val);
Chris Lattner0ed844b2008-04-04 06:12:32 +00001659 }
1660};
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +00001661
Douglas Gregor44b43212008-12-11 16:49:14 +00001662template<class FromTy>
1663struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
1664 static const ::clang::DeclContext &doit(const FromTy &Val) {
1665 return *FromTy::castToDeclContext(&Val);
1666 }
1667};
1668
1669template<class FromTy>
1670struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
1671 static const ::clang::DeclContext *doit(const FromTy *Val) {
1672 return FromTy::castToDeclContext(Val);
1673 }
1674};
1675
Chris Lattner0ed844b2008-04-04 06:12:32 +00001676} // end namespace llvm
1677
1678#endif