blob: f854118d04e43facff0bfd9e0a01e7e1b3fe1caa [file] [log] [blame]
Chris Lattnerd0745a02006-08-17 05:18:35 +00001//===--- Decl.h - Classes for representing declarations ---------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerd0745a02006-08-17 05:18:35 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis2951e142008-06-09 21:05:31 +000010// This file defines the Decl subclasses.
Chris Lattnerd0745a02006-08-17 05:18:35 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner3e7bd4e2006-08-17 05:51:27 +000014#ifndef LLVM_CLANG_AST_DECL_H
15#define LLVM_CLANG_AST_DECL_H
Chris Lattnerd0745a02006-08-17 05:18:35 +000016
Douglas Gregor31cf12c2009-05-26 18:54:04 +000017#include "clang/AST/APValue.h"
Chris Lattnerc5ffed42008-04-04 06:12:32 +000018#include "clang/AST/DeclBase.h"
Chris Lattnera2e21672009-02-20 21:06:29 +000019#include "clang/AST/DeclarationName.h"
Douglas Gregore3dcb2d2009-04-18 00:02:19 +000020#include "clang/AST/ExternalASTSource.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000021#include "clang/AST/Redeclarable.h"
22#include "clang/AST/Type.h"
Douglas Gregor7dc5c172010-02-03 09:33:45 +000023#include "clang/Basic/Linkage.h"
David Blaikie9c70e042011-09-21 18:16:56 +000024#include "llvm/ADT/ArrayRef.h"
Douglas Gregor1baf38f2011-03-26 12:10:19 +000025#include "llvm/ADT/Optional.h"
Daniel Dunbare4775e12012-03-09 19:35:29 +000026#include "llvm/Support/Compiler.h"
Aaron Ballmane475dcd2013-05-14 23:52:21 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenek1f1e7562007-10-25 21:37:16 +000028
Chris Lattnerd0745a02006-08-17 05:18:35 +000029namespace clang {
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000030struct ASTTemplateArgumentListInfo;
Anders Carlsson714d0962009-12-15 19:16:31 +000031class CXXTemporary;
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000032class CompoundStmt;
33class DependentFunctionTemplateSpecializationInfo;
Chris Lattnerc1915e22007-01-25 07:29:02 +000034class Expr;
Douglas Gregor24c332b2009-05-14 21:06:31 +000035class FunctionTemplateDecl;
Douglas Gregor70d83e22009-06-29 17:30:29 +000036class FunctionTemplateSpecializationInfo;
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000037class LabelStmt;
38class MemberSpecializationInfo;
39class Module;
40class NestedNameSpecifier;
41class Stmt;
42class StringLiteral;
43class TemplateArgumentList;
44class TemplateParameterList;
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000045class TypeLoc;
John McCallb9c78482010-04-08 09:05:18 +000046class UnresolvedSetImpl;
Larisse Voufo39a1e502013-08-06 01:03:05 +000047class VarTemplateDecl;
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000048
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000049/// \brief A container of type source information.
50///
51/// A client can read the relevant info using TypeLoc wrappers, e.g:
52/// @code
John McCallbcd03502009-12-07 02:54:59 +000053/// TypeLoc TL = TypeSourceInfo->getTypeLoc();
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000054/// if (PointerLoc *PL = dyn_cast<PointerLoc>(&TL))
55/// PL->getStarLoc().print(OS, SrcMgr);
56/// @endcode
57///
John McCallbcd03502009-12-07 02:54:59 +000058class TypeSourceInfo {
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000059 QualType Ty;
60 // Contains a memory block after the class, used for type source information,
61 // allocated by ASTContext.
62 friend class ASTContext;
John McCallbcd03502009-12-07 02:54:59 +000063 TypeSourceInfo(QualType ty) : Ty(ty) { }
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000064public:
John McCallff470312009-10-16 21:42:04 +000065 /// \brief Return the type wrapped by this type source info.
66 QualType getType() const { return Ty; }
67
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000068 /// \brief Return the TypeLoc wrapper for the type source info.
John McCall00861902010-05-06 09:25:57 +000069 TypeLoc getTypeLoc() const; // implemented in TypeLoc.h
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000070};
71
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +000072/// TranslationUnitDecl - The top declaration context.
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +000073class TranslationUnitDecl : public Decl, public DeclContext {
David Blaikie68e081d2011-12-20 02:48:34 +000074 virtual void anchor();
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +000075 ASTContext &Ctx;
Mike Stump11289f42009-09-09 15:08:12 +000076
John McCall0db42252009-12-16 02:06:49 +000077 /// The (most recently entered) anonymous namespace for this
78 /// translation unit, if one has been created.
79 NamespaceDecl *AnonymousNamespace;
80
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +000081 explicit TranslationUnitDecl(ASTContext &ctx)
Douglas Gregor6e6ad602009-01-20 01:17:11 +000082 : Decl(TranslationUnit, 0, SourceLocation()),
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +000083 DeclContext(TranslationUnit),
John McCall0db42252009-12-16 02:06:49 +000084 Ctx(ctx), AnonymousNamespace(0) {}
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +000085public:
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +000086 ASTContext &getASTContext() const { return Ctx; }
Mike Stump11289f42009-09-09 15:08:12 +000087
John McCall0db42252009-12-16 02:06:49 +000088 NamespaceDecl *getAnonymousNamespace() const { return AnonymousNamespace; }
89 void setAnonymousNamespace(NamespaceDecl *D) { AnonymousNamespace = D; }
90
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +000091 static TranslationUnitDecl *Create(ASTContext &C);
92 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +000093 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +000094 static bool classofKind(Kind K) { return K == TranslationUnit; }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +000095 static DeclContext *castToDeclContext(const TranslationUnitDecl *D) {
96 return static_cast<DeclContext *>(const_cast<TranslationUnitDecl*>(D));
97 }
98 static TranslationUnitDecl *castFromDeclContext(const DeclContext *DC) {
99 return static_cast<TranslationUnitDecl *>(const_cast<DeclContext*>(DC));
100 }
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +0000101};
102
Chris Lattner79413952008-12-04 23:50:19 +0000103/// NamedDecl - This represents a decl with a name. Many decls have names such
Chandler Carruth48687a52012-06-07 17:55:42 +0000104/// as ObjCMethodDecl, but not \@class, etc.
Chris Lattnera4016552007-10-06 22:53:46 +0000105class NamedDecl : public Decl {
David Blaikie68e081d2011-12-20 02:48:34 +0000106 virtual void anchor();
Douglas Gregor77324f32008-11-17 14:58:09 +0000107 /// Name - The name of this declaration, which is typically a normal
108 /// identifier but may also be a special kind of name (C++
109 /// constructor, Objective-C selector, etc.)
110 DeclarationName Name;
111
Daniel Dunbar47c32282012-03-08 20:29:02 +0000112private:
Alp Tokera2794f92014-01-22 07:29:52 +0000113 NamedDecl *getUnderlyingDeclImpl() LLVM_READONLY;
Daniel Dunbar47c32282012-03-08 20:29:02 +0000114
Douglas Gregor77324f32008-11-17 14:58:09 +0000115protected:
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000116 NamedDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName N)
Douglas Gregor027ba502010-12-06 17:49:01 +0000117 : Decl(DK, DC, L), Name(N) { }
118
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000119public:
Douglas Gregor77324f32008-11-17 14:58:09 +0000120 /// getIdentifier - Get the identifier that names this declaration,
121 /// if there is one. This will return NULL if this declaration has
122 /// no name (e.g., for an unnamed class) or if the name is a special
123 /// name (C++ constructor, Objective-C selector, etc.).
124 IdentifierInfo *getIdentifier() const { return Name.getAsIdentifierInfo(); }
125
Daniel Dunbar0013afb2009-10-18 02:09:45 +0000126 /// getName - Get the name of identifier for this declaration as a StringRef.
127 /// This requires that the declaration have a name and that it be a simple
128 /// identifier.
Chris Lattner01cf8db2011-07-20 06:58:45 +0000129 StringRef getName() const {
Daniel Dunbar0914afe2009-10-19 01:21:12 +0000130 assert(Name.isIdentifier() && "Name is not a simple identifier");
131 return getIdentifier() ? getIdentifier()->getName() : "";
Daniel Dunbar0013afb2009-10-18 02:09:45 +0000132 }
133
Daniel Dunbar0013afb2009-10-18 02:09:45 +0000134 /// getNameAsString - Get a human-readable name for the declaration, even if
135 /// it is one of the special kinds of names (C++ constructor, Objective-C
136 /// selector, etc). Creating this name requires expensive string
137 /// manipulation, so it should be called only when performance doesn't matter.
138 /// For simple declarations, getNameAsCString() should suffice.
139 //
Daniel Dunbar8b794812009-10-19 01:20:50 +0000140 // FIXME: This function should be renamed to indicate that it is not just an
141 // alternate form of getName(), and clients should move as appropriate.
142 //
Daniel Dunbar0013afb2009-10-18 02:09:45 +0000143 // FIXME: Deprecated, move clients to getName().
144 std::string getNameAsString() const { return Name.getAsString(); }
145
David Blaikied4da8722013-05-14 21:04:00 +0000146 void printName(raw_ostream &os) const { os << Name; }
Ted Kremenekc14f5eb2010-05-07 20:07:19 +0000147
Douglas Gregor77324f32008-11-17 14:58:09 +0000148 /// getDeclName - Get the actual, stored name of the declaration,
149 /// which may be a special name.
150 DeclarationName getDeclName() const { return Name; }
151
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000152 /// \brief Set the name of this declaration.
153 void setDeclName(DeclarationName N) { Name = N; }
154
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000155 /// printQualifiedName - Returns human-readable qualified name for
Douglas Gregor2ada0482009-02-04 17:27:36 +0000156 /// declaration, like A::B::i, for i being member of namespace A::B.
157 /// If declaration is not member of context which can be named (record,
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000158 /// namespace), it will return same result as printName().
Douglas Gregor2ada0482009-02-04 17:27:36 +0000159 /// Creating this name is expensive, so it should be called only when
160 /// performance doesn't matter.
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000161 void printQualifiedName(raw_ostream &OS) const;
162 void printQualifiedName(raw_ostream &OS, const PrintingPolicy &Policy) const;
163
Aaron Ballman75ee4cc2014-01-03 18:42:48 +0000164 // FIXME: Remove string version.
Douglas Gregor2ada0482009-02-04 17:27:36 +0000165 std::string getQualifiedNameAsString() const;
Douglas Gregor2ada0482009-02-04 17:27:36 +0000166
John McCalle1f2ec22009-09-11 06:45:03 +0000167 /// getNameForDiagnostic - Appends a human-readable name for this
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000168 /// declaration into the given stream.
John McCalle1f2ec22009-09-11 06:45:03 +0000169 ///
170 /// This is the method invoked by Sema when displaying a NamedDecl
171 /// in a diagnostic. It does not necessarily produce the same
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000172 /// result as printName(); for example, class template
John McCalle1f2ec22009-09-11 06:45:03 +0000173 /// specializations are printed with their template arguments.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000174 virtual void getNameForDiagnostic(raw_ostream &OS,
Douglas Gregorb11aad82011-02-19 18:51:44 +0000175 const PrintingPolicy &Policy,
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000176 bool Qualified) const;
John McCalle1f2ec22009-09-11 06:45:03 +0000177
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000178 /// declarationReplaces - Determine whether this declaration, if
179 /// known to be well-formed within its context, will replace the
180 /// declaration OldD if introduced into scope. A declaration will
181 /// replace another declaration if, for example, it is a
182 /// redeclaration of the same variable or function, but not if it is
183 /// a declaration of a different kind (function vs. class) or an
184 /// overloaded function.
185 bool declarationReplaces(NamedDecl *OldD) const;
186
Douglas Gregoreddf4332009-02-24 20:03:32 +0000187 /// \brief Determine whether this declaration has linkage.
188 bool hasLinkage() const;
189
Douglas Gregor781f7132012-01-06 16:59:53 +0000190 using Decl::isModulePrivate;
191 using Decl::setModulePrivate;
Richard Smith42713d72013-07-14 02:01:48 +0000192
Douglas Gregor768e5b12012-01-06 16:22:39 +0000193 /// \brief Determine whether this declaration is hidden from name lookup.
194 bool isHidden() const { return Hidden; }
Richard Smith42713d72013-07-14 02:01:48 +0000195
196 /// \brief Set whether this declaration is hidden from name lookup.
197 void setHidden(bool Hide) { Hidden = Hide; }
198
John McCall57500772009-12-16 12:17:52 +0000199 /// \brief Determine whether this declaration is a C++ class member.
200 bool isCXXClassMember() const {
201 const DeclContext *DC = getDeclContext();
202
203 // C++0x [class.mem]p1:
204 // The enumerators of an unscoped enumeration defined in
205 // the class are members of the class.
206 // FIXME: support C++0x scoped enumerations.
207 if (isa<EnumDecl>(DC))
208 DC = DC->getParent();
209
210 return DC->isRecord();
211 }
212
Douglas Gregor3f28ec22012-03-08 02:08:05 +0000213 /// \brief Determine whether the given declaration is an instance member of
214 /// a C++ class.
John McCalla8ae2222010-04-06 21:38:20 +0000215 bool isCXXInstanceMember() const;
216
Douglas Gregorf73b2822009-11-25 22:24:25 +0000217 /// \brief Determine what kind of linkage this entity has.
Rafael Espindola3ae00052013-05-13 00:12:11 +0000218 /// This is not the linkage as defined by the standard or the codegen notion
219 /// of linkage. It is just an implementation detail that is used to compute
220 /// those.
221 Linkage getLinkageInternal() const;
222
223 /// \brief Get the linkage from a semantic point of view. Entities in
224 /// anonymous namespaces are external (in c++98).
225 Linkage getFormalLinkage() const {
Rafael Espindolab97e8962013-05-27 14:14:42 +0000226 return clang::getFormalLinkage(getLinkageInternal());
Rafael Espindola3ae00052013-05-13 00:12:11 +0000227 }
John McCall457a04e2010-10-22 21:05:15 +0000228
Rafael Espindola45b620a2013-03-07 02:00:27 +0000229 /// \brief True if this decl has external linkage.
Rafael Espindola3ae00052013-05-13 00:12:11 +0000230 bool hasExternalFormalLinkage() const {
Rafael Espindolab97e8962013-05-27 14:14:42 +0000231 return isExternalFormalLinkage(getLinkageInternal());
Rafael Espindola3ae00052013-05-13 00:12:11 +0000232 }
Rafael Espindolab97e8962013-05-27 14:14:42 +0000233
Rafael Espindola3ae00052013-05-13 00:12:11 +0000234 bool isExternallyVisible() const {
Rafael Espindolab97e8962013-05-27 14:14:42 +0000235 return clang::isExternallyVisible(getLinkageInternal());
Rafael Espindola45b620a2013-03-07 02:00:27 +0000236 }
237
John McCall457a04e2010-10-22 21:05:15 +0000238 /// \brief Determines the visibility of this entity.
David Blaikie21bfbf82011-11-09 06:07:30 +0000239 Visibility getVisibility() const {
Rafael Espindola4a5da442013-02-27 02:56:45 +0000240 return getLinkageAndVisibility().getVisibility();
David Blaikie21bfbf82011-11-09 06:07:30 +0000241 }
John McCall457a04e2010-10-22 21:05:15 +0000242
243 /// \brief Determines the linkage and visibility of this entity.
John McCallc273f242010-10-30 11:50:40 +0000244 LinkageInfo getLinkageAndVisibility() const;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000245
John McCalld041a9b2013-02-20 01:54:26 +0000246 /// Kinds of explicit visibility.
247 enum ExplicitVisibilityKind {
248 VisibilityForType,
249 VisibilityForValue
250 };
251
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000252 /// \brief If visibility was explicitly specified for this
253 /// declaration, return that visibility.
David Blaikie05785d162013-02-20 22:23:23 +0000254 Optional<Visibility>
John McCalld041a9b2013-02-20 01:54:26 +0000255 getExplicitVisibility(ExplicitVisibilityKind kind) const;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000256
Rafael Espindola0e0d0092013-03-14 03:07:35 +0000257 /// \brief True if the computed linkage is valid. Used for consistency
258 /// checking. Should always return true.
259 bool isLinkageValid() const;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000260
John McCall2575d882014-01-30 01:12:53 +0000261 /// \brief True if something has required us to compute the linkage
262 /// of this declaration.
263 ///
264 /// Language features which can retroactively change linkage (like a
265 /// typedef name for linkage purposes) may need to consider this,
266 /// but hopefully only in transitory ways during parsing.
267 bool hasLinkageBeenComputed() const {
268 return hasCachedLinkage();
269 }
270
Anders Carlsson6915bf62009-06-26 06:29:23 +0000271 /// \brief Looks through UsingDecls and ObjCCompatibleAliasDecls for
272 /// the underlying named decl.
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +0000273 NamedDecl *getUnderlyingDecl() {
Daniel Dunbar47c32282012-03-08 20:29:02 +0000274 // Fast-path the common case.
275 if (this->getKind() != UsingShadow &&
276 this->getKind() != ObjCCompatibleAlias)
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +0000277 return this;
Daniel Dunbar47c32282012-03-08 20:29:02 +0000278
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +0000279 return getUnderlyingDeclImpl();
280 }
Anders Carlsson6915bf62009-06-26 06:29:23 +0000281 const NamedDecl *getUnderlyingDecl() const {
282 return const_cast<NamedDecl*>(this)->getUnderlyingDecl();
283 }
Mike Stump11289f42009-09-09 15:08:12 +0000284
Rafael Espindola7b56f6c2013-10-19 16:55:03 +0000285 NamedDecl *getMostRecentDecl() {
Aaron Ballman5116a8e2013-11-06 22:39:46 +0000286 return cast<NamedDecl>(static_cast<Decl *>(this)->getMostRecentDecl());
Rafael Espindola7b56f6c2013-10-19 16:55:03 +0000287 }
288 const NamedDecl *getMostRecentDecl() const {
289 return const_cast<NamedDecl*>(this)->getMostRecentDecl();
290 }
291
John McCall180ef092010-01-29 01:45:37 +0000292 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Alexis Hunted053252010-05-30 07:21:58 +0000293 static bool classofKind(Kind K) { return K >= firstNamed && K <= lastNamed; }
Chris Lattnera4016552007-10-06 22:53:46 +0000294};
295
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000296inline raw_ostream &operator<<(raw_ostream &OS, const NamedDecl &ND) {
297 ND.printName(OS);
Benjamin Kramer95c7c422010-04-17 09:56:45 +0000298 return OS;
299}
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000300
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000301/// LabelDecl - Represents the declaration of a label. Labels also have a
302/// corresponding LabelStmt, which indicates the position that the label was
303/// defined at. For normal labels, the location of the decl is the same as the
304/// location of the statement. For GNU local labels (__label__), the decl
305/// location is where the __label__ is.
306class LabelDecl : public NamedDecl {
Craig Toppercbce6e92014-03-11 06:22:39 +0000307 void anchor() override;
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000308 LabelStmt *TheStmt;
309 /// LocStart - For normal labels, this is the same as the main declaration
310 /// label, i.e., the location of the identifier; for GNU local labels,
311 /// this is the location of the __label__ keyword.
312 SourceLocation LocStart;
313
314 LabelDecl(DeclContext *DC, SourceLocation IdentL, IdentifierInfo *II,
315 LabelStmt *S, SourceLocation StartL)
316 : NamedDecl(Label, DC, IdentL, II), TheStmt(S), LocStart(StartL) {}
317
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000318public:
319 static LabelDecl *Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000320 SourceLocation IdentL, IdentifierInfo *II);
321 static LabelDecl *Create(ASTContext &C, DeclContext *DC,
322 SourceLocation IdentL, IdentifierInfo *II,
323 SourceLocation GnuLabelL);
Douglas Gregor72172e92012-01-05 21:55:30 +0000324 static LabelDecl *CreateDeserialized(ASTContext &C, unsigned ID);
325
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000326 LabelStmt *getStmt() const { return TheStmt; }
327 void setStmt(LabelStmt *T) { TheStmt = T; }
328
329 bool isGnuLocal() const { return LocStart != getLocation(); }
330 void setLocStart(SourceLocation L) { LocStart = L; }
331
Craig Toppercbce6e92014-03-11 06:22:39 +0000332 SourceRange getSourceRange() const override LLVM_READONLY {
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000333 return SourceRange(LocStart, getLocation());
334 }
Abramo Bagnara124fdf62011-03-03 18:24:14 +0000335
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000336 // Implement isa/cast/dyncast/etc.
337 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000338 static bool classofKind(Kind K) { return K == Label; }
339};
David Blaikie21bfbf82011-11-09 06:07:30 +0000340
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +0000341/// NamespaceDecl - Represent a C++ namespace.
Douglas Gregore57e7522012-01-07 09:11:48 +0000342class NamespaceDecl : public NamedDecl, public DeclContext,
343 public Redeclarable<NamespaceDecl>
344{
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000345 /// LocStart - The starting location of the source range, pointing
346 /// to either the namespace or the inline keyword.
347 SourceLocation LocStart;
348 /// RBraceLoc - The ending location of the source range.
349 SourceLocation RBraceLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000350
Douglas Gregore57e7522012-01-07 09:11:48 +0000351 /// \brief A pointer to either the anonymous namespace that lives just inside
352 /// this namespace or to the first namespace in the chain (the latter case
353 /// only when this is not the first in the chain), along with a
354 /// boolean value indicating whether this is an inline namespace.
355 llvm::PointerIntPair<NamespaceDecl *, 1, bool> AnonOrFirstNamespaceAndInline;
Mike Stump11289f42009-09-09 15:08:12 +0000356
Douglas Gregore57e7522012-01-07 09:11:48 +0000357 NamespaceDecl(DeclContext *DC, bool Inline, SourceLocation StartLoc,
358 SourceLocation IdLoc, IdentifierInfo *Id,
359 NamespaceDecl *PrevDecl);
Rafael Espindola8ef7f682013-11-26 15:12:20 +0000360
Douglas Gregore57e7522012-01-07 09:11:48 +0000361 typedef Redeclarable<NamespaceDecl> redeclarable_base;
Craig Toppercbce6e92014-03-11 06:22:39 +0000362 NamespaceDecl *getNextRedeclaration() override;
363 NamespaceDecl *getPreviousDeclImpl() override;
364 NamespaceDecl *getMostRecentDeclImpl() override;
Rafael Espindola8ef7f682013-11-26 15:12:20 +0000365
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +0000366public:
367 static NamespaceDecl *Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +0000368 bool Inline, SourceLocation StartLoc,
369 SourceLocation IdLoc, IdentifierInfo *Id,
370 NamespaceDecl *PrevDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000371
Douglas Gregor72172e92012-01-05 21:55:30 +0000372 static NamespaceDecl *CreateDeserialized(ASTContext &C, unsigned ID);
Douglas Gregorec9fd132012-01-14 16:38:05 +0000373
Aaron Ballman86c93902014-03-06 23:45:36 +0000374 typedef redeclarable_base::redecl_range redecl_range;
Aaron Ballman211cd8c2014-03-07 00:10:58 +0000375 typedef redeclarable_base::redecl_iterator redecl_iterator;
376 using redeclarable_base::redecls_begin;
377 using redeclarable_base::redecls_end;
Aaron Ballman86c93902014-03-06 23:45:36 +0000378 using redeclarable_base::redecls;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000379 using redeclarable_base::getPreviousDecl;
380 using redeclarable_base::getMostRecentDecl;
Rafael Espindola3f9e4442013-10-19 02:13:21 +0000381 using redeclarable_base::isFirstDecl;
Douglas Gregore57e7522012-01-07 09:11:48 +0000382
Sebastian Redlb5c2baa2010-08-31 00:36:36 +0000383 /// \brief Returns true if this is an anonymous namespace declaration.
384 ///
385 /// For example:
Chris Lattnerca025db2010-05-07 21:43:38 +0000386 /// \code
Sebastian Redlb5c2baa2010-08-31 00:36:36 +0000387 /// namespace {
388 /// ...
389 /// };
390 /// \endcode
391 /// q.v. C++ [namespace.unnamed]
John McCall4fa53422009-10-01 00:25:31 +0000392 bool isAnonymousNamespace() const {
393 return !getIdentifier();
394 }
395
Sebastian Redlb5c2baa2010-08-31 00:36:36 +0000396 /// \brief Returns true if this is an inline namespace declaration.
Sebastian Redl50c68252010-08-31 00:36:30 +0000397 bool isInline() const {
Douglas Gregore57e7522012-01-07 09:11:48 +0000398 return AnonOrFirstNamespaceAndInline.getInt();
Sebastian Redlb5c2baa2010-08-31 00:36:36 +0000399 }
400
401 /// \brief Set whether this is an inline namespace declaration.
402 void setInline(bool Inline) {
Douglas Gregore57e7522012-01-07 09:11:48 +0000403 AnonOrFirstNamespaceAndInline.setInt(Inline);
Sebastian Redl50c68252010-08-31 00:36:30 +0000404 }
405
Chris Lattnerca025db2010-05-07 21:43:38 +0000406 /// \brief Get the original (first) namespace declaration.
Douglas Gregore57e7522012-01-07 09:11:48 +0000407 NamespaceDecl *getOriginalNamespace() {
Rafael Espindola8db352d2013-10-17 15:37:26 +0000408 if (isFirstDecl())
Benjamin Kramerd78f1ec2012-03-06 18:07:25 +0000409 return this;
410
411 return AnonOrFirstNamespaceAndInline.getPointer();
Douglas Gregore57e7522012-01-07 09:11:48 +0000412 }
Douglas Gregorf9f54ea2010-03-24 00:46:35 +0000413
Douglas Gregore57e7522012-01-07 09:11:48 +0000414 /// \brief Get the original (first) namespace declaration.
415 const NamespaceDecl *getOriginalNamespace() const {
Rafael Espindola8db352d2013-10-17 15:37:26 +0000416 if (isFirstDecl())
Benjamin Kramerd78f1ec2012-03-06 18:07:25 +0000417 return this;
418
419 return AnonOrFirstNamespaceAndInline.getPointer();
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +0000420 }
Douglas Gregorf9f54ea2010-03-24 00:46:35 +0000421
Chris Lattnerca025db2010-05-07 21:43:38 +0000422 /// \brief Return true if this declaration is an original (first) declaration
423 /// of the namespace. This is false for non-original (subsequent) namespace
424 /// declarations and anonymous namespaces.
Rafael Espindola8db352d2013-10-17 15:37:26 +0000425 bool isOriginalNamespace() const { return isFirstDecl(); }
Chris Lattnerca025db2010-05-07 21:43:38 +0000426
Douglas Gregore57e7522012-01-07 09:11:48 +0000427 /// \brief Retrieve the anonymous namespace nested inside this namespace,
428 /// if any.
John McCall0db42252009-12-16 02:06:49 +0000429 NamespaceDecl *getAnonymousNamespace() const {
Douglas Gregore57e7522012-01-07 09:11:48 +0000430 return getOriginalNamespace()->AnonOrFirstNamespaceAndInline.getPointer();
John McCall0db42252009-12-16 02:06:49 +0000431 }
432
433 void setAnonymousNamespace(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +0000434 getOriginalNamespace()->AnonOrFirstNamespaceAndInline.setPointer(D);
John McCall0db42252009-12-16 02:06:49 +0000435 }
436
Douglas Gregore57e7522012-01-07 09:11:48 +0000437 /// Retrieves the canonical declaration of this namespace.
Craig Toppercbce6e92014-03-11 06:22:39 +0000438 NamespaceDecl *getCanonicalDecl() override {
Benjamin Kramerd78f1ec2012-03-06 18:07:25 +0000439 return getOriginalNamespace();
Douglas Gregore57e7522012-01-07 09:11:48 +0000440 }
David Blaikie21bfbf82011-11-09 06:07:30 +0000441 const NamespaceDecl *getCanonicalDecl() const {
Benjamin Kramerd78f1ec2012-03-06 18:07:25 +0000442 return getOriginalNamespace();
Douglas Gregorf9f54ea2010-03-24 00:46:35 +0000443 }
Craig Toppercbce6e92014-03-11 06:22:39 +0000444
445 SourceRange getSourceRange() const override LLVM_READONLY {
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000446 return SourceRange(LocStart, RBraceLoc);
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +0000447 }
448
Daniel Dunbare4775e12012-03-09 19:35:29 +0000449 SourceLocation getLocStart() const LLVM_READONLY { return LocStart; }
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000450 SourceLocation getRBraceLoc() const { return RBraceLoc; }
451 void setLocStart(SourceLocation L) { LocStart = L; }
452 void setRBraceLoc(SourceLocation L) { RBraceLoc = L; }
453
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +0000454 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +0000455 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +0000456 static bool classofKind(Kind K) { return K == Namespace; }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000457 static DeclContext *castToDeclContext(const NamespaceDecl *D) {
458 return static_cast<DeclContext *>(const_cast<NamespaceDecl*>(D));
459 }
460 static NamespaceDecl *castFromDeclContext(const DeclContext *DC) {
461 return static_cast<NamespaceDecl *>(const_cast<DeclContext*>(DC));
462 }
David Blaikie21bfbf82011-11-09 06:07:30 +0000463
Sebastian Redlb3298c32010-08-18 23:56:48 +0000464 friend class ASTDeclReader;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000465 friend class ASTDeclWriter;
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +0000466};
467
Mike Stump11289f42009-09-09 15:08:12 +0000468/// ValueDecl - Represent the declaration of a variable (in which case it is
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000469/// an lvalue) a function (in which case it is a function designator) or
Mike Stump11289f42009-09-09 15:08:12 +0000470/// an enum constant.
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000471class ValueDecl : public NamedDecl {
Craig Toppercbce6e92014-03-11 06:22:39 +0000472 void anchor() override;
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000473 QualType DeclType;
Nate Begemana0f78972007-11-13 22:14:47 +0000474
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000475protected:
Chris Lattnerbec41342008-04-22 18:39:57 +0000476 ValueDecl(Kind DK, DeclContext *DC, SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +0000477 DeclarationName N, QualType T)
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000478 : NamedDecl(DK, DC, L, N), DeclType(T) {}
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000479public:
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000480 QualType getType() const { return DeclType; }
Steve Naroff4dddb612007-07-09 18:55:26 +0000481 void setType(QualType newType) { DeclType = newType; }
Mike Stump11289f42009-09-09 15:08:12 +0000482
Lang Hamesd42bb472011-12-05 20:16:26 +0000483 /// \brief Determine whether this symbol is weakly-imported,
484 /// or declared with the weak or weak-ref attr.
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000485 bool isWeak() const;
Lang Hamesd42bb472011-12-05 20:16:26 +0000486
Chris Lattner17ed4872006-11-20 04:58:19 +0000487 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +0000488 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Alexis Hunted053252010-05-30 07:21:58 +0000489 static bool classofKind(Kind K) { return K >= firstValue && K <= lastValue; }
Chris Lattner17ed4872006-11-20 04:58:19 +0000490};
491
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000492/// QualifierInfo - A struct with extended info about a syntactic
493/// name qualifier, to be used for the case of out-of-line declarations.
494struct QualifierInfo {
Douglas Gregor14454802011-02-25 02:25:35 +0000495 NestedNameSpecifierLoc QualifierLoc;
Abramo Bagnara60804e12011-03-18 15:16:37 +0000496
497 /// NumTemplParamLists - The number of "outer" template parameter lists.
498 /// The count includes all of the template parameter lists that were matched
499 /// against the template-ids occurring into the NNS and possibly (in the
500 /// case of an explicit specialization) a final "template <>".
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000501 unsigned NumTemplParamLists;
Abramo Bagnara60804e12011-03-18 15:16:37 +0000502
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000503 /// TemplParamLists - A new-allocated array of size NumTemplParamLists,
Abramo Bagnara60804e12011-03-18 15:16:37 +0000504 /// containing pointers to the "outer" template parameter lists.
505 /// It includes all of the template parameter lists that were matched
506 /// against the template-ids occurring into the NNS and possibly (in the
507 /// case of an explicit specialization) a final "template <>".
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000508 TemplateParameterList** TemplParamLists;
509
510 /// Default constructor.
Douglas Gregor14454802011-02-25 02:25:35 +0000511 QualifierInfo() : QualifierLoc(), NumTemplParamLists(0), TemplParamLists(0) {}
Abramo Bagnara60804e12011-03-18 15:16:37 +0000512
513 /// setTemplateParameterListsInfo - Sets info about "outer" template
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000514 /// parameter lists.
Douglas Gregor20527e22010-06-15 17:44:38 +0000515 void setTemplateParameterListsInfo(ASTContext &Context,
516 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000517 TemplateParameterList **TPLists);
David Blaikie21bfbf82011-11-09 06:07:30 +0000518
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000519private:
520 // Copy constructor and copy assignment are disabled.
Craig Topper43201a82012-09-18 04:52:05 +0000521 QualifierInfo(const QualifierInfo&) LLVM_DELETED_FUNCTION;
522 QualifierInfo& operator=(const QualifierInfo&) LLVM_DELETED_FUNCTION;
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000523};
524
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000525/// \brief Represents a ValueDecl that came out of a declarator.
John McCallbcd03502009-12-07 02:54:59 +0000526/// Contains type source information through TypeSourceInfo.
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000527class DeclaratorDecl : public ValueDecl {
John McCall3e11ebe2010-03-15 10:12:16 +0000528 // A struct representing both a TInfo and a syntactic qualifier,
529 // to be used for the (uncommon) case of out-of-line declarations.
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000530 struct ExtInfo : public QualifierInfo {
John McCall3e11ebe2010-03-15 10:12:16 +0000531 TypeSourceInfo *TInfo;
John McCall3e11ebe2010-03-15 10:12:16 +0000532 };
533
534 llvm::PointerUnion<TypeSourceInfo*, ExtInfo*> DeclInfo;
535
Abramo Bagnaradff19302011-03-08 08:55:46 +0000536 /// InnerLocStart - The start of the source range for this declaration,
537 /// ignoring outer template declarations.
538 SourceLocation InnerLocStart;
539
John McCall3e11ebe2010-03-15 10:12:16 +0000540 bool hasExtInfo() const { return DeclInfo.is<ExtInfo*>(); }
541 ExtInfo *getExtInfo() { return DeclInfo.get<ExtInfo*>(); }
542 const ExtInfo *getExtInfo() const { return DeclInfo.get<ExtInfo*>(); }
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000543
544protected:
545 DeclaratorDecl(Kind DK, DeclContext *DC, SourceLocation L,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000546 DeclarationName N, QualType T, TypeSourceInfo *TInfo,
547 SourceLocation StartL)
548 : ValueDecl(DK, DC, L, N, T), DeclInfo(TInfo), InnerLocStart(StartL) {
549 }
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000550
551public:
John McCall3e11ebe2010-03-15 10:12:16 +0000552 TypeSourceInfo *getTypeSourceInfo() const {
553 return hasExtInfo()
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000554 ? getExtInfo()->TInfo
John McCall3e11ebe2010-03-15 10:12:16 +0000555 : DeclInfo.get<TypeSourceInfo*>();
556 }
557 void setTypeSourceInfo(TypeSourceInfo *TI) {
558 if (hasExtInfo())
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000559 getExtInfo()->TInfo = TI;
John McCall3e11ebe2010-03-15 10:12:16 +0000560 else
561 DeclInfo = TI;
562 }
563
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000564 /// getInnerLocStart - Return SourceLocation representing start of source
565 /// range ignoring outer template declarations.
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000566 SourceLocation getInnerLocStart() const { return InnerLocStart; }
Abramo Bagnaradff19302011-03-08 08:55:46 +0000567 void setInnerLocStart(SourceLocation L) { InnerLocStart = L; }
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000568
569 /// getOuterLocStart - Return SourceLocation representing start of source
570 /// range taking into account any outer template declarations.
571 SourceLocation getOuterLocStart() const;
Abramo Bagnaraea947882011-03-08 16:41:52 +0000572
Craig Toppercbce6e92014-03-11 06:22:39 +0000573 SourceRange getSourceRange() const override LLVM_READONLY;
Daniel Dunbare4775e12012-03-09 19:35:29 +0000574 SourceLocation getLocStart() const LLVM_READONLY {
Daniel Dunbarcdf295c2012-03-09 15:39:24 +0000575 return getOuterLocStart();
576 }
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000577
Douglas Gregor14454802011-02-25 02:25:35 +0000578 /// \brief Retrieve the nested-name-specifier that qualifies the name of this
579 /// declaration, if it was present in the source.
John McCall3e11ebe2010-03-15 10:12:16 +0000580 NestedNameSpecifier *getQualifier() const {
Douglas Gregor14454802011-02-25 02:25:35 +0000581 return hasExtInfo() ? getExtInfo()->QualifierLoc.getNestedNameSpecifier()
582 : 0;
John McCall3e11ebe2010-03-15 10:12:16 +0000583 }
David Blaikie21bfbf82011-11-09 06:07:30 +0000584
585 /// \brief Retrieve the nested-name-specifier (with source-location
586 /// information) that qualifies the name of this declaration, if it was
Douglas Gregor14454802011-02-25 02:25:35 +0000587 /// present in the source.
588 NestedNameSpecifierLoc getQualifierLoc() const {
589 return hasExtInfo() ? getExtInfo()->QualifierLoc
590 : NestedNameSpecifierLoc();
John McCall3e11ebe2010-03-15 10:12:16 +0000591 }
David Blaikie21bfbf82011-11-09 06:07:30 +0000592
Douglas Gregor14454802011-02-25 02:25:35 +0000593 void setQualifierInfo(NestedNameSpecifierLoc QualifierLoc);
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000594
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000595 unsigned getNumTemplateParameterLists() const {
596 return hasExtInfo() ? getExtInfo()->NumTemplParamLists : 0;
597 }
598 TemplateParameterList *getTemplateParameterList(unsigned index) const {
599 assert(index < getNumTemplateParameterLists());
600 return getExtInfo()->TemplParamLists[index];
601 }
Douglas Gregor20527e22010-06-15 17:44:38 +0000602 void setTemplateParameterListsInfo(ASTContext &Context, unsigned NumTPLists,
Abramo Bagnara60804e12011-03-18 15:16:37 +0000603 TemplateParameterList **TPLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000604
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000605 SourceLocation getTypeSpecStartLoc() const;
606
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000607 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +0000608 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +0000609 static bool classofKind(Kind K) {
Alexis Hunted053252010-05-30 07:21:58 +0000610 return K >= firstDeclarator && K <= lastDeclarator;
John McCall180ef092010-01-29 01:45:37 +0000611 }
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +0000612
613 friend class ASTDeclReader;
614 friend class ASTDeclWriter;
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000615};
616
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000617/// \brief Structure used to store a statement, the constant value to
618/// which it was evaluated (if any), and whether or not the statement
619/// is an integral constant expression (if known).
620struct EvaluatedStmt {
Eli Friedman1d6fb162009-12-03 20:31:57 +0000621 EvaluatedStmt() : WasEvaluated(false), IsEvaluating(false), CheckedICE(false),
622 CheckingICE(false), IsICE(false) { }
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000623
624 /// \brief Whether this statement was already evaluated.
625 bool WasEvaluated : 1;
626
Eli Friedman1d6fb162009-12-03 20:31:57 +0000627 /// \brief Whether this statement is being evaluated.
628 bool IsEvaluating : 1;
629
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000630 /// \brief Whether we already checked whether this statement was an
631 /// integral constant expression.
632 bool CheckedICE : 1;
633
Eli Friedman1d6fb162009-12-03 20:31:57 +0000634 /// \brief Whether we are checking whether this statement is an
635 /// integral constant expression.
636 bool CheckingICE : 1;
637
Richard Smithd0b4dd62011-12-19 06:19:21 +0000638 /// \brief Whether this statement is an integral constant expression,
639 /// or in C++11, whether the statement is a constant expression. Only
640 /// valid if CheckedICE is true.
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000641 bool IsICE : 1;
642
643 Stmt *Value;
644 APValue Evaluated;
645};
646
Chris Lattner17ed4872006-11-20 04:58:19 +0000647/// VarDecl - An instance of this class is created to represent a variable
648/// declaration or definition.
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000649class VarDecl : public DeclaratorDecl, public Redeclarable<VarDecl> {
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000650public:
John McCall8e7d6562010-08-26 03:08:43 +0000651 typedef clang::StorageClass StorageClass;
Daniel Dunbarb76b74572009-04-14 02:08:49 +0000652
653 /// getStorageClassSpecifierString - Return the string used to
Dmitri Gribenko8994e0c2012-09-13 13:11:20 +0000654 /// specify the storage class \p SC.
Daniel Dunbarb76b74572009-04-14 02:08:49 +0000655 ///
656 /// It is illegal to call this function with SC == None.
657 static const char *getStorageClassSpecifierString(StorageClass SC);
658
Sebastian Redla9351792012-02-11 23:51:47 +0000659 /// \brief Initialization styles.
660 enum InitializationStyle {
661 CInit, ///< C-style initialization with assignment
662 CallInit, ///< Call-style initialization (C++98)
663 ListInit ///< Direct list-initialization (C++11)
664 };
665
Richard Smithfd3834f2013-04-13 02:43:54 +0000666 /// \brief Kinds of thread-local storage.
667 enum TLSKind {
668 TLS_None, ///< Not a TLS variable.
669 TLS_Static, ///< TLS with a known-constant initializer.
670 TLS_Dynamic ///< TLS with a dynamic initializer.
671 };
672
Douglas Gregord12df6b2009-07-20 22:03:28 +0000673protected:
674 /// \brief Placeholder type used in Init to denote an unparsed C++ default
675 /// argument.
676 struct UnparsedDefaultArgument;
Anders Carlssona5224ec2009-08-25 01:11:14 +0000677
Mike Stump11289f42009-09-09 15:08:12 +0000678 /// \brief Placeholder type used in Init to denote an uninstantiated C++
Anders Carlssona5224ec2009-08-25 01:11:14 +0000679 /// default argument.
680 struct UninstantiatedDefaultArgument;
681
Mike Stump11289f42009-09-09 15:08:12 +0000682 typedef llvm::PointerUnion4<Stmt *, EvaluatedStmt *,
683 UnparsedDefaultArgument *,
Douglas Gregor770a5322009-09-01 16:13:00 +0000684 UninstantiatedDefaultArgument *> InitType;
Mike Stump11289f42009-09-09 15:08:12 +0000685
686 /// \brief The initializer for this variable or, for a ParmVarDecl, the
Douglas Gregord12df6b2009-07-20 22:03:28 +0000687 /// C++ default argument.
Douglas Gregor770a5322009-09-01 16:13:00 +0000688 mutable InitType Init;
Mike Stump11289f42009-09-09 15:08:12 +0000689
Chris Lattner4b08ca82008-03-15 21:10:16 +0000690private:
John McCallbeaa11c2011-05-01 02:13:58 +0000691 class VarDeclBitfields {
692 friend class VarDecl;
693 friend class ASTDeclReader;
Douglas Gregor85970ca2008-12-10 23:01:14 +0000694
John McCallbeaa11c2011-05-01 02:13:58 +0000695 unsigned SClass : 3;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000696 unsigned TSCSpec : 2;
Sebastian Redla9351792012-02-11 23:51:47 +0000697 unsigned InitStyle : 2;
John McCallbeaa11c2011-05-01 02:13:58 +0000698
699 /// \brief Whether this variable is the exception variable in a C++ catch
700 /// or an Objective-C @catch statement.
701 unsigned ExceptionVar : 1;
David Blaikie21bfbf82011-11-09 06:07:30 +0000702
John McCallbeaa11c2011-05-01 02:13:58 +0000703 /// \brief Whether this local variable could be allocated in the return
David Blaikie21bfbf82011-11-09 06:07:30 +0000704 /// slot of its function, enabling the named return value optimization
705 /// (NRVO).
John McCallbeaa11c2011-05-01 02:13:58 +0000706 unsigned NRVOVariable : 1;
Richard Smith30482bc2011-02-20 03:19:35 +0000707
John McCallbeaa11c2011-05-01 02:13:58 +0000708 /// \brief Whether this variable is the for-range-declaration in a C++0x
709 /// for-range statement.
710 unsigned CXXForRangeDecl : 1;
John McCalld4631322011-06-17 06:42:21 +0000711
712 /// \brief Whether this variable is an ARC pseudo-__strong
713 /// variable; see isARCPseudoStrong() for details.
714 unsigned ARCPseudoStrong : 1;
Richard Smitha77a0a62011-08-15 21:04:07 +0000715
716 /// \brief Whether this variable is (C++0x) constexpr.
717 unsigned IsConstexpr : 1;
Richard Smith1c34fb72013-08-13 18:18:50 +0000718
Richard Smithbb13c9a2013-09-28 04:02:39 +0000719 /// \brief Whether this variable is the implicit variable for a lambda
720 /// init-capture.
721 unsigned IsInitCapture : 1;
722
Richard Smith1c34fb72013-08-13 18:18:50 +0000723 /// \brief Whether this local extern variable's previous declaration was
724 /// declared in the same block scope. This controls whether we should merge
725 /// the type of this declaration with its previous declaration.
726 unsigned PreviousDeclInSameBlockScope : 1;
John McCallbeaa11c2011-05-01 02:13:58 +0000727 };
Richard Smithbb13c9a2013-09-28 04:02:39 +0000728 enum { NumVarDeclBits = 14 };
Richard Smith02e85f32011-04-14 22:09:26 +0000729
Douglas Gregorbf62d642010-12-06 18:36:25 +0000730 friend class ASTDeclReader;
John McCallbeaa11c2011-05-01 02:13:58 +0000731 friend class StmtIteratorBase;
Larisse Voufo39a1e502013-08-06 01:03:05 +0000732 friend class ASTNodeImporter;
David Blaikie21bfbf82011-11-09 06:07:30 +0000733
Chris Lattner4b08ca82008-03-15 21:10:16 +0000734protected:
Ted Kremenek9504ae92011-10-06 04:19:35 +0000735 enum { NumParameterIndexBits = 8 };
David Blaikie21bfbf82011-11-09 06:07:30 +0000736
John McCallbeaa11c2011-05-01 02:13:58 +0000737 class ParmVarDeclBitfields {
738 friend class ParmVarDecl;
739 friend class ASTDeclReader;
740
741 unsigned : NumVarDeclBits;
John McCallbeaa11c2011-05-01 02:13:58 +0000742
743 /// Whether this parameter inherits a default argument from a
744 /// prior declaration.
745 unsigned HasInheritedDefaultArg : 1;
746
747 /// Whether this parameter undergoes K&R argument promotion.
748 unsigned IsKNRPromoted : 1;
John McCall82490832011-05-02 00:30:12 +0000749
750 /// Whether this parameter is an ObjC method parameter or not.
751 unsigned IsObjCMethodParam : 1;
752
753 /// If IsObjCMethodParam, a Decl::ObjCDeclQualifier.
754 /// Otherwise, the number of function parameter scopes enclosing
755 /// the function parameter scope in which this parameter was
756 /// declared.
Sebastian Redla9351792012-02-11 23:51:47 +0000757 unsigned ScopeDepthOrObjCQuals : 7;
John McCall82490832011-05-02 00:30:12 +0000758
759 /// The number of parameters preceding this parameter in the
760 /// function parameter scope in which it was declared.
Ted Kremenek9504ae92011-10-06 04:19:35 +0000761 unsigned ParameterIndex : NumParameterIndexBits;
John McCallbeaa11c2011-05-01 02:13:58 +0000762 };
763
764 union {
765 unsigned AllBits;
766 VarDeclBitfields VarDeclBits;
767 ParmVarDeclBitfields ParmVarDeclBits;
768 };
769
Abramo Bagnaradff19302011-03-08 08:55:46 +0000770 VarDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc,
Larisse Voufo39a1e502013-08-06 01:03:05 +0000771 SourceLocation IdLoc, IdentifierInfo *Id, QualType T,
772 TypeSourceInfo *TInfo, StorageClass SC);
Argyrios Kyrtzidis05898da2009-07-18 08:50:35 +0000773
774 typedef Redeclarable<VarDecl> redeclarable_base;
Craig Toppercbce6e92014-03-11 06:22:39 +0000775 VarDecl *getNextRedeclaration() override { return RedeclLink.getNext(); }
776 VarDecl *getPreviousDeclImpl() override {
Douglas Gregorec9fd132012-01-14 16:38:05 +0000777 return getPreviousDecl();
778 }
Craig Toppercbce6e92014-03-11 06:22:39 +0000779 VarDecl *getMostRecentDeclImpl() override {
Douglas Gregorec9fd132012-01-14 16:38:05 +0000780 return getMostRecentDecl();
781 }
Douglas Gregorb11aad82011-02-19 18:51:44 +0000782
Chris Lattner4b08ca82008-03-15 21:10:16 +0000783public:
Aaron Ballman86c93902014-03-06 23:45:36 +0000784 typedef redeclarable_base::redecl_range redecl_range;
Aaron Ballman211cd8c2014-03-07 00:10:58 +0000785 typedef redeclarable_base::redecl_iterator redecl_iterator;
786 using redeclarable_base::redecls_begin;
787 using redeclarable_base::redecls_end;
Aaron Ballman86c93902014-03-06 23:45:36 +0000788 using redeclarable_base::redecls;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000789 using redeclarable_base::getPreviousDecl;
790 using redeclarable_base::getMostRecentDecl;
Rafael Espindola3f9e4442013-10-19 02:13:21 +0000791 using redeclarable_base::isFirstDecl;
Douglas Gregor0bc8a212012-01-14 15:55:47 +0000792
Chris Lattnerbec41342008-04-22 18:39:57 +0000793 static VarDecl *Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000794 SourceLocation StartLoc, SourceLocation IdLoc,
795 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000796 StorageClass S);
Nuno Lopes394ec982008-12-17 23:39:55 +0000797
Douglas Gregor72172e92012-01-05 21:55:30 +0000798 static VarDecl *CreateDeserialized(ASTContext &C, unsigned ID);
Craig Toppercbce6e92014-03-11 06:22:39 +0000799
800 SourceRange getSourceRange() const override LLVM_READONLY;
Nuno Lopes394ec982008-12-17 23:39:55 +0000801
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000802 /// \brief Returns the storage class as written in the source. For the
803 /// computed linkage of symbol, see getLinkage.
John McCallbeaa11c2011-05-01 02:13:58 +0000804 StorageClass getStorageClass() const {
805 return (StorageClass) VarDeclBits.SClass;
806 }
Douglas Gregorbf62d642010-12-06 18:36:25 +0000807 void setStorageClass(StorageClass SC);
Mike Stump11289f42009-09-09 15:08:12 +0000808
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000809 void setTSCSpec(ThreadStorageClassSpecifier TSC) {
810 VarDeclBits.TSCSpec = TSC;
Reid Kleckner7d6d2702014-05-01 03:16:47 +0000811 assert(VarDeclBits.TSCSpec == TSC && "truncation");
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000812 }
813 ThreadStorageClassSpecifier getTSCSpec() const {
814 return static_cast<ThreadStorageClassSpecifier>(VarDeclBits.TSCSpec);
815 }
Reid Kleckner7d6d2702014-05-01 03:16:47 +0000816 TLSKind getTLSKind() const;
Sebastian Redl833ef452010-01-26 22:01:41 +0000817
818 /// hasLocalStorage - Returns true if a variable with function scope
819 /// is a non-static local variable.
820 bool hasLocalStorage() const {
John McCall8e7d6562010-08-26 03:08:43 +0000821 if (getStorageClass() == SC_None)
Enea Zaffanella28f36ba2013-05-10 20:34:44 +0000822 // Second check is for C++11 [dcl.stc]p4.
Richard Smithe6c01442013-06-05 00:46:14 +0000823 return !isFileVarDecl() && getTSCSpec() == TSCS_unspecified;
Sebastian Redl833ef452010-01-26 22:01:41 +0000824
825 // Return true for: Auto, Register.
Peter Collingbourne2dbb7082011-09-19 21:14:35 +0000826 // Return false for: Extern, Static, PrivateExtern, OpenCLWorkGroupLocal.
Sebastian Redl833ef452010-01-26 22:01:41 +0000827
John McCall8e7d6562010-08-26 03:08:43 +0000828 return getStorageClass() >= SC_Auto;
Sebastian Redl833ef452010-01-26 22:01:41 +0000829 }
830
David Blaikie21bfbf82011-11-09 06:07:30 +0000831 /// isStaticLocal - Returns true if a variable with function scope is a
Fariborz Jahanian3fef72f2010-04-18 21:01:23 +0000832 /// static local variable.
833 bool isStaticLocal() const {
Enea Zaffanella28f36ba2013-05-10 20:34:44 +0000834 return (getStorageClass() == SC_Static ||
835 // C++11 [dcl.stc]p4
836 (getStorageClass() == SC_None && getTSCSpec() == TSCS_thread_local))
837 && !isFileVarDecl();
Fariborz Jahanian3fef72f2010-04-18 21:01:23 +0000838 }
David Blaikie21bfbf82011-11-09 06:07:30 +0000839
Rafael Espindola2199c122013-03-07 02:43:24 +0000840 /// \brief Returns true if a variable has extern or __private_extern__
841 /// storage.
Sebastian Redl833ef452010-01-26 22:01:41 +0000842 bool hasExternalStorage() const {
John McCall8e7d6562010-08-26 03:08:43 +0000843 return getStorageClass() == SC_Extern ||
844 getStorageClass() == SC_PrivateExtern;
Sebastian Redl833ef452010-01-26 22:01:41 +0000845 }
846
James Dennett42c4e502013-11-05 19:37:38 +0000847 /// \brief Returns true for all variables that do not have local storage.
848 ///
849 /// This includes all global variables as well as static variables declared
850 /// within a function.
Sebastian Redl833ef452010-01-26 22:01:41 +0000851 bool hasGlobalStorage() const { return !hasLocalStorage(); }
852
James Dennett42c4e502013-11-05 19:37:38 +0000853 /// \brief Get the storage duration of this variable, per C++ [basic.stc].
Richard Smithe6c01442013-06-05 00:46:14 +0000854 StorageDuration getStorageDuration() const {
855 return hasLocalStorage() ? SD_Automatic :
856 getTSCSpec() ? SD_Thread : SD_Static;
857 }
858
James Dennett42c4e502013-11-05 19:37:38 +0000859 /// \brief Compute the language linkage.
Rafael Espindolaf4187652013-02-14 01:18:37 +0000860 LanguageLinkage getLanguageLinkage() const;
861
Rafael Espindola5bda63f2013-02-14 01:47:04 +0000862 /// \brief Determines whether this variable is a variable with
863 /// external, C linkage.
Rafael Espindola0e0d0092013-03-14 03:07:35 +0000864 bool isExternC() const;
Rafael Espindola576127d2012-12-28 14:21:58 +0000865
Rafael Espindola593537a2013-05-05 20:15:21 +0000866 /// \brief Determines whether this variable's context is, or is nested within,
867 /// a C++ extern "C" linkage spec.
868 bool isInExternCContext() const;
869
870 /// \brief Determines whether this variable's context is, or is nested within,
871 /// a C++ extern "C++" linkage spec.
872 bool isInExternCXXContext() const;
873
John McCall1c9c3fd2010-10-15 04:57:14 +0000874 /// isLocalVarDecl - Returns true for local variable declarations
875 /// other than parameters. Note that this includes static variables
876 /// inside of functions. It also includes variables inside blocks.
Sebastian Redl833ef452010-01-26 22:01:41 +0000877 ///
878 /// void foo() { int x; static int y; extern int z; }
879 ///
John McCall1c9c3fd2010-10-15 04:57:14 +0000880 bool isLocalVarDecl() const {
Sebastian Redl833ef452010-01-26 22:01:41 +0000881 if (getKind() != Decl::Var)
882 return false;
Richard Smith541b38b2013-09-20 01:15:31 +0000883 if (const DeclContext *DC = getLexicalDeclContext())
Sebastian Redl50c68252010-08-31 00:36:30 +0000884 return DC->getRedeclContext()->isFunctionOrMethod();
Sebastian Redl833ef452010-01-26 22:01:41 +0000885 return false;
886 }
887
John McCall1c9c3fd2010-10-15 04:57:14 +0000888 /// isFunctionOrMethodVarDecl - Similar to isLocalVarDecl, but
889 /// excludes variables declared in blocks.
Fariborz Jahanian3a106e72010-03-11 18:20:03 +0000890 bool isFunctionOrMethodVarDecl() const {
891 if (getKind() != Decl::Var)
892 return false;
Richard Smith541b38b2013-09-20 01:15:31 +0000893 const DeclContext *DC = getLexicalDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +0000894 return DC->isFunctionOrMethod() && DC->getDeclKind() != Decl::Block;
Fariborz Jahanian3a106e72010-03-11 18:20:03 +0000895 }
896
Sebastian Redl833ef452010-01-26 22:01:41 +0000897 /// \brief Determines whether this is a static data member.
898 ///
899 /// This will only be true in C++, and applies to, e.g., the
900 /// variable 'x' in:
901 /// \code
902 /// struct S {
903 /// static int x;
904 /// };
905 /// \endcode
906 bool isStaticDataMember() const {
Sebastian Redl35351a92010-01-31 22:27:38 +0000907 // If it wasn't static, it would be a FieldDecl.
Argyrios Kyrtzidis3816ed42010-07-19 10:14:41 +0000908 return getKind() != Decl::ParmVar && getDeclContext()->isRecord();
Sebastian Redl833ef452010-01-26 22:01:41 +0000909 }
910
Craig Toppercbce6e92014-03-11 06:22:39 +0000911 VarDecl *getCanonicalDecl() override;
Sebastian Redl833ef452010-01-26 22:01:41 +0000912 const VarDecl *getCanonicalDecl() const {
913 return const_cast<VarDecl*>(this)->getCanonicalDecl();
914 }
915
Sebastian Redl35351a92010-01-31 22:27:38 +0000916 enum DefinitionKind {
917 DeclarationOnly, ///< This declaration is only a declaration.
918 TentativeDefinition, ///< This declaration is a tentative definition.
919 Definition ///< This declaration is definitely a definition.
920 };
921
922 /// \brief Check whether this declaration is a definition. If this could be
923 /// a tentative definition (in C), don't check whether there's an overriding
924 /// definition.
Daniel Dunbar9d355812012-03-09 01:51:51 +0000925 DefinitionKind isThisDeclarationADefinition(ASTContext &) const;
926 DefinitionKind isThisDeclarationADefinition() const {
927 return isThisDeclarationADefinition(getASTContext());
928 }
Sebastian Redl35351a92010-01-31 22:27:38 +0000929
John McCall37bb6c92010-10-29 22:22:43 +0000930 /// \brief Check whether this variable is defined in this
931 /// translation unit.
Daniel Dunbar9d355812012-03-09 01:51:51 +0000932 DefinitionKind hasDefinition(ASTContext &) const;
933 DefinitionKind hasDefinition() const {
934 return hasDefinition(getASTContext());
935 }
John McCall37bb6c92010-10-29 22:22:43 +0000936
Sebastian Redl35351a92010-01-31 22:27:38 +0000937 /// \brief Get the tentative definition that acts as the real definition in
938 /// a TU. Returns null if there is a proper definition available.
Sebastian Redl35351a92010-01-31 22:27:38 +0000939 VarDecl *getActingDefinition();
Sebastian Redl5ca79842010-02-01 20:16:42 +0000940 const VarDecl *getActingDefinition() const {
941 return const_cast<VarDecl*>(this)->getActingDefinition();
942 }
Sebastian Redl35351a92010-01-31 22:27:38 +0000943
Sebastian Redl5ca79842010-02-01 20:16:42 +0000944 /// \brief Get the real (not just tentative) definition for this declaration.
Daniel Dunbar9d355812012-03-09 01:51:51 +0000945 VarDecl *getDefinition(ASTContext &);
946 const VarDecl *getDefinition(ASTContext &C) const {
947 return const_cast<VarDecl*>(this)->getDefinition(C);
948 }
949 VarDecl *getDefinition() {
950 return getDefinition(getASTContext());
951 }
Sebastian Redl5ca79842010-02-01 20:16:42 +0000952 const VarDecl *getDefinition() const {
953 return const_cast<VarDecl*>(this)->getDefinition();
Sebastian Redl833ef452010-01-26 22:01:41 +0000954 }
955
David Blaikie21bfbf82011-11-09 06:07:30 +0000956 /// \brief Determine whether this is or was instantiated from an out-of-line
Sebastian Redl833ef452010-01-26 22:01:41 +0000957 /// definition of a static data member.
Craig Toppercbce6e92014-03-11 06:22:39 +0000958 bool isOutOfLine() const override;
Sebastian Redl833ef452010-01-26 22:01:41 +0000959
960 /// \brief If this is a static data member, find its out-of-line definition.
961 VarDecl *getOutOfLineDefinition();
David Blaikie21bfbf82011-11-09 06:07:30 +0000962
Sebastian Redl833ef452010-01-26 22:01:41 +0000963 /// isFileVarDecl - Returns true for file scoped variable declaration.
964 bool isFileVarDecl() const {
Larisse Voufo39a1e502013-08-06 01:03:05 +0000965 Kind K = getKind();
966 if (K == ParmVar || K == ImplicitParam)
Sebastian Redl833ef452010-01-26 22:01:41 +0000967 return false;
David Blaikie21bfbf82011-11-09 06:07:30 +0000968
Richard Smith541b38b2013-09-20 01:15:31 +0000969 if (getLexicalDeclContext()->getRedeclContext()->isFileContext())
Douglas Gregorfcee9462010-08-27 22:55:10 +0000970 return true;
David Blaikie21bfbf82011-11-09 06:07:30 +0000971
Sebastian Redl833ef452010-01-26 22:01:41 +0000972 if (isStaticDataMember())
973 return true;
974
975 return false;
976 }
977
Sebastian Redl5ca79842010-02-01 20:16:42 +0000978 /// getAnyInitializer - Get the initializer for this variable, no matter which
979 /// declaration it is attached to.
980 const Expr *getAnyInitializer() const {
981 const VarDecl *D;
982 return getAnyInitializer(D);
983 }
984
985 /// getAnyInitializer - Get the initializer for this variable, no matter which
986 /// declaration it is attached to. Also get that declaration.
987 const Expr *getAnyInitializer(const VarDecl *&D) const;
988
Sebastian Redl35351a92010-01-31 22:27:38 +0000989 bool hasInit() const {
Chandler Carruth7a6d2e92010-12-13 07:40:47 +0000990 return !Init.isNull() && (Init.is<Stmt *>() || Init.is<EvaluatedStmt *>());
Sebastian Redl35351a92010-01-31 22:27:38 +0000991 }
Mike Stump11289f42009-09-09 15:08:12 +0000992 const Expr *getInit() const {
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000993 if (Init.isNull())
994 return 0;
995
996 const Stmt *S = Init.dyn_cast<Stmt *>();
Douglas Gregord12df6b2009-07-20 22:03:28 +0000997 if (!S) {
998 if (EvaluatedStmt *ES = Init.dyn_cast<EvaluatedStmt*>())
999 S = ES->Value;
1000 }
Mike Stump11289f42009-09-09 15:08:12 +00001001 return (const Expr*) S;
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001002 }
Mike Stump11289f42009-09-09 15:08:12 +00001003 Expr *getInit() {
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001004 if (Init.isNull())
1005 return 0;
1006
1007 Stmt *S = Init.dyn_cast<Stmt *>();
Douglas Gregord12df6b2009-07-20 22:03:28 +00001008 if (!S) {
1009 if (EvaluatedStmt *ES = Init.dyn_cast<EvaluatedStmt*>())
1010 S = ES->Value;
1011 }
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001012
Mike Stump11289f42009-09-09 15:08:12 +00001013 return (Expr*) S;
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001014 }
1015
1016 /// \brief Retrieve the address of the initializer expression.
1017 Stmt **getInitAddress() {
Douglas Gregord12df6b2009-07-20 22:03:28 +00001018 if (EvaluatedStmt *ES = Init.dyn_cast<EvaluatedStmt*>())
1019 return &ES->Value;
Mike Stump11289f42009-09-09 15:08:12 +00001020
Douglas Gregor770a5322009-09-01 16:13:00 +00001021 // This union hack tip-toes around strict-aliasing rules.
1022 union {
1023 InitType *InitPtr;
1024 Stmt **StmtPtr;
1025 };
Mike Stump11289f42009-09-09 15:08:12 +00001026
Douglas Gregor770a5322009-09-01 16:13:00 +00001027 InitPtr = &Init;
1028 return StmtPtr;
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001029 }
1030
Douglas Gregord5058122010-02-11 01:19:42 +00001031 void setInit(Expr *I);
Mike Stump11289f42009-09-09 15:08:12 +00001032
Richard Smith242ad892011-12-21 02:55:12 +00001033 /// \brief Determine whether this variable's value can be used in a
1034 /// constant expression, according to the relevant language standard.
1035 /// This only checks properties of the declaration, and does not check
1036 /// whether the initializer is in fact a constant expression.
Daniel Dunbar9d355812012-03-09 01:51:51 +00001037 bool isUsableInConstantExpressions(ASTContext &C) const;
Richard Smith242ad892011-12-21 02:55:12 +00001038
Richard Smithd0b4dd62011-12-19 06:19:21 +00001039 EvaluatedStmt *ensureEvaluatedStmt() const;
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001040
Richard Smithd0b4dd62011-12-19 06:19:21 +00001041 /// \brief Attempt to evaluate the value of the initializer attached to this
1042 /// declaration, and produce notes explaining why it cannot be evaluated or is
Richard Smithdafff942012-01-14 04:30:29 +00001043 /// not a constant expression. Returns a pointer to the value if evaluation
1044 /// succeeded, 0 otherwise.
1045 APValue *evaluateValue() const;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001046 APValue *evaluateValue(SmallVectorImpl<PartialDiagnosticAt> &Notes) const;
Mike Stump11289f42009-09-09 15:08:12 +00001047
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001048 /// \brief Return the already-evaluated value of this variable's
Eli Friedman1d6fb162009-12-03 20:31:57 +00001049 /// initializer, or NULL if the value is not yet known. Returns pointer
1050 /// to untyped APValue if the value could not be evaluated.
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001051 APValue *getEvaluatedValue() const {
1052 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
1053 if (Eval->WasEvaluated)
1054 return &Eval->Evaluated;
1055
1056 return 0;
1057 }
1058
1059 /// \brief Determines whether it is already known whether the
1060 /// initializer is an integral constant expression or not.
1061 bool isInitKnownICE() const {
1062 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
1063 return Eval->CheckedICE;
1064
1065 return false;
1066 }
1067
Richard Smithd0b4dd62011-12-19 06:19:21 +00001068 /// \brief Determines whether the initializer is an integral constant
1069 /// expression, or in C++11, whether the initializer is a constant
1070 /// expression.
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001071 ///
1072 /// \pre isInitKnownICE()
1073 bool isInitICE() const {
Mike Stump11289f42009-09-09 15:08:12 +00001074 assert(isInitKnownICE() &&
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001075 "Check whether we already know that the initializer is an ICE");
1076 return Init.get<EvaluatedStmt *>()->IsICE;
1077 }
1078
Richard Smithd0b4dd62011-12-19 06:19:21 +00001079 /// \brief Determine whether the value of the initializer attached to this
1080 /// declaration is an integral constant expression.
1081 bool checkInitIsICE() const;
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001082
Sebastian Redla9351792012-02-11 23:51:47 +00001083 void setInitStyle(InitializationStyle Style) {
1084 VarDeclBits.InitStyle = Style;
1085 }
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001086
Sebastian Redla9351792012-02-11 23:51:47 +00001087 /// \brief The style of initialization for this declaration.
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001088 ///
Sebastian Redla9351792012-02-11 23:51:47 +00001089 /// C-style initialization is "int x = 1;". Call-style initialization is
1090 /// a C++98 direct-initializer, e.g. "int x(1);". The Init expression will be
1091 /// the expression inside the parens or a "ClassType(a,b,c)" class constructor
1092 /// expression for class types. List-style initialization is C++11 syntax,
1093 /// e.g. "int x{1};". Clients can distinguish between different forms of
1094 /// initialization by checking this value. In particular, "int x = {1};" is
1095 /// C-style, "int x({1})" is call-style, and "int x{1};" is list-style; the
1096 /// Init expression in all three cases is an InitListExpr.
1097 InitializationStyle getInitStyle() const {
1098 return static_cast<InitializationStyle>(VarDeclBits.InitStyle);
1099 }
1100
1101 /// \brief Whether the initializer is a direct-initializer (list or call).
1102 bool isDirectInit() const {
1103 return getInitStyle() != CInit;
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001104 }
Mike Stump11289f42009-09-09 15:08:12 +00001105
Douglas Gregor3f324d562010-05-03 18:51:14 +00001106 /// \brief Determine whether this variable is the exception variable in a
Chandler Carruth48687a52012-06-07 17:55:42 +00001107 /// C++ catch statememt or an Objective-C \@catch statement.
Douglas Gregor3f324d562010-05-03 18:51:14 +00001108 bool isExceptionVariable() const {
John McCallbeaa11c2011-05-01 02:13:58 +00001109 return VarDeclBits.ExceptionVar;
Douglas Gregor3f324d562010-05-03 18:51:14 +00001110 }
John McCallbeaa11c2011-05-01 02:13:58 +00001111 void setExceptionVariable(bool EV) { VarDeclBits.ExceptionVar = EV; }
David Blaikie21bfbf82011-11-09 06:07:30 +00001112
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001113 /// \brief Determine whether this local variable can be used with the named
1114 /// return value optimization (NRVO).
1115 ///
1116 /// The named return value optimization (NRVO) works by marking certain
1117 /// non-volatile local variables of class type as NRVO objects. These
1118 /// locals can be allocated within the return slot of their containing
1119 /// function, in which case there is no need to copy the object to the
1120 /// return slot when returning from the function. Within the function body,
1121 /// each return that returns the NRVO object will have this variable as its
1122 /// NRVO candidate.
John McCallbeaa11c2011-05-01 02:13:58 +00001123 bool isNRVOVariable() const { return VarDeclBits.NRVOVariable; }
1124 void setNRVOVariable(bool NRVO) { VarDeclBits.NRVOVariable = NRVO; }
Richard Smith02e85f32011-04-14 22:09:26 +00001125
1126 /// \brief Determine whether this variable is the for-range-declaration in
1127 /// a C++0x for-range statement.
John McCallbeaa11c2011-05-01 02:13:58 +00001128 bool isCXXForRangeDecl() const { return VarDeclBits.CXXForRangeDecl; }
1129 void setCXXForRangeDecl(bool FRD) { VarDeclBits.CXXForRangeDecl = FRD; }
John McCalld4631322011-06-17 06:42:21 +00001130
1131 /// \brief Determine whether this variable is an ARC pseudo-__strong
1132 /// variable. A pseudo-__strong variable has a __strong-qualified
1133 /// type but does not actually retain the object written into it.
1134 /// Generally such variables are also 'const' for safety.
1135 bool isARCPseudoStrong() const { return VarDeclBits.ARCPseudoStrong; }
1136 void setARCPseudoStrong(bool ps) { VarDeclBits.ARCPseudoStrong = ps; }
David Blaikie21bfbf82011-11-09 06:07:30 +00001137
Richard Smith12f247f2012-06-08 21:09:22 +00001138 /// Whether this variable is (C++11) constexpr.
Richard Smitha77a0a62011-08-15 21:04:07 +00001139 bool isConstexpr() const { return VarDeclBits.IsConstexpr; }
1140 void setConstexpr(bool IC) { VarDeclBits.IsConstexpr = IC; }
1141
Richard Smithbb13c9a2013-09-28 04:02:39 +00001142 /// Whether this variable is the implicit variable for a lambda init-capture.
1143 bool isInitCapture() const { return VarDeclBits.IsInitCapture; }
1144 void setInitCapture(bool IC) { VarDeclBits.IsInitCapture = IC; }
1145
Richard Smith1c34fb72013-08-13 18:18:50 +00001146 /// Whether this local extern variable declaration's previous declaration
1147 /// was declared in the same block scope. Only correct in C++.
1148 bool isPreviousDeclInSameBlockScope() const {
1149 return VarDeclBits.PreviousDeclInSameBlockScope;
1150 }
1151 void setPreviousDeclInSameBlockScope(bool Same) {
1152 VarDeclBits.PreviousDeclInSameBlockScope = Same;
1153 }
1154
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001155 /// \brief If this variable is an instantiated static data member of a
Mike Stump11289f42009-09-09 15:08:12 +00001156 /// class template specialization, returns the templated static data member
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001157 /// from which it was instantiated.
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001158 VarDecl *getInstantiatedFromStaticDataMember() const;
Mike Stump11289f42009-09-09 15:08:12 +00001159
Richard Smith8809a0c2013-09-27 20:14:12 +00001160 /// \brief If this variable is an instantiation of a variable template or a
1161 /// static data member of a class template, determine what kind of
Douglas Gregor86d142a2009-10-08 07:24:58 +00001162 /// template specialization or instantiation this is.
Douglas Gregor3c74d412009-10-14 20:14:33 +00001163 TemplateSpecializationKind getTemplateSpecializationKind() const;
David Blaikie21bfbf82011-11-09 06:07:30 +00001164
Richard Smith8809a0c2013-09-27 20:14:12 +00001165 /// \brief If this variable is an instantiation of a variable template or a
1166 /// static data member of a class template, determine its point of
1167 /// instantiation.
1168 SourceLocation getPointOfInstantiation() const;
1169
Douglas Gregor06db9f52009-10-12 20:18:28 +00001170 /// \brief If this variable is an instantiation of a static data member of a
1171 /// class template specialization, retrieves the member specialization
1172 /// information.
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001173 MemberSpecializationInfo *getMemberSpecializationInfo() const;
David Blaikie21bfbf82011-11-09 06:07:30 +00001174
Douglas Gregor86d142a2009-10-08 07:24:58 +00001175 /// \brief For a static data member that was instantiated from a static
1176 /// data member of a class template, set the template specialiation kind.
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001177 void setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1178 SourceLocation PointOfInstantiation = SourceLocation());
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001179
Larisse Voufo39a1e502013-08-06 01:03:05 +00001180 /// \brief Specify that this variable is an instantiation of the
1181 /// static data member VD.
1182 void setInstantiationOfStaticDataMember(VarDecl *VD,
1183 TemplateSpecializationKind TSK);
1184
1185 /// \brief Retrieves the variable template that is described by this
1186 /// variable declaration.
1187 ///
1188 /// Every variable template is represented as a VarTemplateDecl and a
1189 /// VarDecl. The former contains template properties (such as
1190 /// the template parameter lists) while the latter contains the
1191 /// actual description of the template's
1192 /// contents. VarTemplateDecl::getTemplatedDecl() retrieves the
1193 /// VarDecl that from a VarTemplateDecl, while
1194 /// getDescribedVarTemplate() retrieves the VarTemplateDecl from
1195 /// a VarDecl.
1196 VarTemplateDecl *getDescribedVarTemplate() const;
1197
1198 void setDescribedVarTemplate(VarTemplateDecl *Template);
1199
Steve Naroffef2ab6a2007-04-02 20:50:54 +00001200 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00001201 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Alexis Hunted053252010-05-30 07:21:58 +00001202 static bool classofKind(Kind K) { return K >= firstVar && K <= lastVar; }
Steve Naroffef2ab6a2007-04-02 20:50:54 +00001203};
1204
Chris Lattner5696e7b2008-06-17 18:05:57 +00001205class ImplicitParamDecl : public VarDecl {
Craig Toppercbce6e92014-03-11 06:22:39 +00001206 void anchor() override;
Chris Lattner5696e7b2008-06-17 18:05:57 +00001207public:
1208 static ImplicitParamDecl *Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001209 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregord12df6b2009-07-20 22:03:28 +00001210 QualType T);
John McCall550d13a2011-02-22 22:25:56 +00001211
Douglas Gregor72172e92012-01-05 21:55:30 +00001212 static ImplicitParamDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1213
Abramo Bagnaradff19302011-03-08 08:55:46 +00001214 ImplicitParamDecl(DeclContext *DC, SourceLocation IdLoc,
1215 IdentifierInfo *Id, QualType Type)
1216 : VarDecl(ImplicitParam, DC, IdLoc, IdLoc, Id, Type,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001217 /*tinfo*/ 0, SC_None) {
John McCall550d13a2011-02-22 22:25:56 +00001218 setImplicit();
1219 }
1220
Chris Lattner5696e7b2008-06-17 18:05:57 +00001221 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00001222 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1223 static bool classofKind(Kind K) { return K == ImplicitParam; }
Chris Lattner5696e7b2008-06-17 18:05:57 +00001224};
1225
John McCall8fb0d9d2011-05-01 22:35:37 +00001226/// ParmVarDecl - Represents a parameter to a function.
Steve Naroffef2ab6a2007-04-02 20:50:54 +00001227class ParmVarDecl : public VarDecl {
John McCall82490832011-05-02 00:30:12 +00001228public:
1229 enum { MaxFunctionScopeDepth = 255 };
1230 enum { MaxFunctionScopeIndex = 255 };
John McCall8fb0d9d2011-05-01 22:35:37 +00001231
Fariborz Jahanian7664ffb2008-12-20 20:56:12 +00001232protected:
Abramo Bagnaradff19302011-03-08 08:55:46 +00001233 ParmVarDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc,
1234 SourceLocation IdLoc, IdentifierInfo *Id,
1235 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001236 StorageClass S, Expr *DefArg)
1237 : VarDecl(DK, DC, StartLoc, IdLoc, Id, T, TInfo, S) {
John McCallbeaa11c2011-05-01 02:13:58 +00001238 assert(ParmVarDeclBits.HasInheritedDefaultArg == false);
1239 assert(ParmVarDeclBits.IsKNRPromoted == false);
John McCall82490832011-05-02 00:30:12 +00001240 assert(ParmVarDeclBits.IsObjCMethodParam == false);
Douglas Gregord12df6b2009-07-20 22:03:28 +00001241 setDefaultArg(DefArg);
1242 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001243
Chris Lattner4b08ca82008-03-15 21:10:16 +00001244public:
Chris Lattnerbec41342008-04-22 18:39:57 +00001245 static ParmVarDecl *Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001246 SourceLocation StartLoc,
1247 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001248 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001249 StorageClass S, Expr *DefArg);
Mike Stump11289f42009-09-09 15:08:12 +00001250
Douglas Gregor72172e92012-01-05 21:55:30 +00001251 static ParmVarDecl *CreateDeserialized(ASTContext &C, unsigned ID);
Craig Toppercbce6e92014-03-11 06:22:39 +00001252
1253 SourceRange getSourceRange() const override LLVM_READONLY;
David Blaikie21bfbf82011-11-09 06:07:30 +00001254
John McCall82490832011-05-02 00:30:12 +00001255 void setObjCMethodScopeInfo(unsigned parameterIndex) {
1256 ParmVarDeclBits.IsObjCMethodParam = true;
Ted Kremenek9504ae92011-10-06 04:19:35 +00001257 setParameterIndex(parameterIndex);
John McCall82490832011-05-02 00:30:12 +00001258 }
1259
1260 void setScopeInfo(unsigned scopeDepth, unsigned parameterIndex) {
1261 assert(!ParmVarDeclBits.IsObjCMethodParam);
1262
1263 ParmVarDeclBits.ScopeDepthOrObjCQuals = scopeDepth;
David Blaikie21bfbf82011-11-09 06:07:30 +00001264 assert(ParmVarDeclBits.ScopeDepthOrObjCQuals == scopeDepth
1265 && "truncation!");
John McCall82490832011-05-02 00:30:12 +00001266
Ted Kremenek9504ae92011-10-06 04:19:35 +00001267 setParameterIndex(parameterIndex);
John McCall82490832011-05-02 00:30:12 +00001268 }
1269
1270 bool isObjCMethodParameter() const {
1271 return ParmVarDeclBits.IsObjCMethodParam;
John McCall8fb0d9d2011-05-01 22:35:37 +00001272 }
1273
1274 unsigned getFunctionScopeDepth() const {
John McCall82490832011-05-02 00:30:12 +00001275 if (ParmVarDeclBits.IsObjCMethodParam) return 0;
1276 return ParmVarDeclBits.ScopeDepthOrObjCQuals;
John McCall8fb0d9d2011-05-01 22:35:37 +00001277 }
1278
John McCall82490832011-05-02 00:30:12 +00001279 /// Returns the index of this parameter in its prototype or method scope.
John McCall8fb0d9d2011-05-01 22:35:37 +00001280 unsigned getFunctionScopeIndex() const {
Ted Kremenek540017e2011-10-06 05:00:56 +00001281 return getParameterIndex();
John McCall8fb0d9d2011-05-01 22:35:37 +00001282 }
1283
Ted Kremenek72be0682008-02-24 03:55:14 +00001284 ObjCDeclQualifier getObjCDeclQualifier() const {
John McCall82490832011-05-02 00:30:12 +00001285 if (!ParmVarDeclBits.IsObjCMethodParam) return OBJC_TQ_None;
1286 return ObjCDeclQualifier(ParmVarDeclBits.ScopeDepthOrObjCQuals);
Ted Kremenek72be0682008-02-24 03:55:14 +00001287 }
Chris Lattner3d722972008-12-17 07:32:46 +00001288 void setObjCDeclQualifier(ObjCDeclQualifier QTVal) {
John McCall82490832011-05-02 00:30:12 +00001289 assert(ParmVarDeclBits.IsObjCMethodParam);
1290 ParmVarDeclBits.ScopeDepthOrObjCQuals = QTVal;
Chris Lattner3d722972008-12-17 07:32:46 +00001291 }
Mike Stump11289f42009-09-09 15:08:12 +00001292
John McCall6a014d52011-03-09 04:22:44 +00001293 /// True if the value passed to this parameter must undergo
1294 /// K&R-style default argument promotion:
1295 ///
1296 /// C99 6.5.2.2.
1297 /// If the expression that denotes the called function has a type
1298 /// that does not include a prototype, the integer promotions are
1299 /// performed on each argument, and arguments that have type float
1300 /// are promoted to double.
John McCallbeaa11c2011-05-01 02:13:58 +00001301 bool isKNRPromoted() const {
1302 return ParmVarDeclBits.IsKNRPromoted;
1303 }
1304 void setKNRPromoted(bool promoted) {
1305 ParmVarDeclBits.IsKNRPromoted = promoted;
1306 }
John McCall6a014d52011-03-09 04:22:44 +00001307
Anders Carlsson714d0962009-12-15 19:16:31 +00001308 Expr *getDefaultArg();
Mike Stump11289f42009-09-09 15:08:12 +00001309 const Expr *getDefaultArg() const {
Anders Carlsson714d0962009-12-15 19:16:31 +00001310 return const_cast<ParmVarDecl *>(this)->getDefaultArg();
Anders Carlsson85446472009-06-06 04:14:07 +00001311 }
David Blaikie21bfbf82011-11-09 06:07:30 +00001312
Mike Stump11289f42009-09-09 15:08:12 +00001313 void setDefaultArg(Expr *defarg) {
Douglas Gregord12df6b2009-07-20 22:03:28 +00001314 Init = reinterpret_cast<Stmt *>(defarg);
1315 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001316
Douglas Gregorc732aba2009-09-11 18:44:32 +00001317 /// \brief Retrieve the source range that covers the entire default
1318 /// argument.
David Blaikie21bfbf82011-11-09 06:07:30 +00001319 SourceRange getDefaultArgRange() const;
Anders Carlssona5224ec2009-08-25 01:11:14 +00001320 void setUninstantiatedDefaultArg(Expr *arg) {
1321 Init = reinterpret_cast<UninstantiatedDefaultArgument *>(arg);
1322 }
1323 Expr *getUninstantiatedDefaultArg() {
1324 return (Expr *)Init.get<UninstantiatedDefaultArgument *>();
1325 }
Douglas Gregorc732aba2009-09-11 18:44:32 +00001326 const Expr *getUninstantiatedDefaultArg() const {
1327 return (const Expr *)Init.get<UninstantiatedDefaultArgument *>();
1328 }
Mike Stump11289f42009-09-09 15:08:12 +00001329
Anders Carlsson85446472009-06-06 04:14:07 +00001330 /// hasDefaultArg - Determines whether this parameter has a default argument,
1331 /// either parsed or not.
1332 bool hasDefaultArg() const {
Mike Stump11289f42009-09-09 15:08:12 +00001333 return getInit() || hasUnparsedDefaultArg() ||
Anders Carlssona5224ec2009-08-25 01:11:14 +00001334 hasUninstantiatedDefaultArg();
Anders Carlsson85446472009-06-06 04:14:07 +00001335 }
Mike Stump11289f42009-09-09 15:08:12 +00001336
Douglas Gregor58354032008-12-24 00:01:03 +00001337 /// hasUnparsedDefaultArg - Determines whether this parameter has a
1338 /// default argument that has not yet been parsed. This will occur
1339 /// during the processing of a C++ class whose member functions have
1340 /// default arguments, e.g.,
1341 /// @code
1342 /// class X {
1343 /// public:
1344 /// void f(int x = 17); // x has an unparsed default argument now
1345 /// }; // x has a regular default argument now
1346 /// @endcode
1347 bool hasUnparsedDefaultArg() const {
Douglas Gregord12df6b2009-07-20 22:03:28 +00001348 return Init.is<UnparsedDefaultArgument*>();
Douglas Gregor58354032008-12-24 00:01:03 +00001349 }
1350
Anders Carlssona5224ec2009-08-25 01:11:14 +00001351 bool hasUninstantiatedDefaultArg() const {
1352 return Init.is<UninstantiatedDefaultArgument*>();
1353 }
Mike Stump11289f42009-09-09 15:08:12 +00001354
Douglas Gregor58354032008-12-24 00:01:03 +00001355 /// setUnparsedDefaultArg - Specify that this parameter has an
1356 /// unparsed default argument. The argument will be replaced with a
1357 /// real default argument via setDefaultArg when the class
1358 /// definition enclosing the function declaration that owns this
1359 /// default argument is completed.
Mike Stump11289f42009-09-09 15:08:12 +00001360 void setUnparsedDefaultArg() {
Douglas Gregord12df6b2009-07-20 22:03:28 +00001361 Init = (UnparsedDefaultArgument *)0;
1362 }
Douglas Gregor58354032008-12-24 00:01:03 +00001363
John McCallf3cd6652010-03-12 18:31:32 +00001364 bool hasInheritedDefaultArg() const {
John McCallbeaa11c2011-05-01 02:13:58 +00001365 return ParmVarDeclBits.HasInheritedDefaultArg;
John McCallf3cd6652010-03-12 18:31:32 +00001366 }
1367
1368 void setHasInheritedDefaultArg(bool I = true) {
John McCallbeaa11c2011-05-01 02:13:58 +00001369 ParmVarDeclBits.HasInheritedDefaultArg = I;
John McCallf3cd6652010-03-12 18:31:32 +00001370 }
1371
Reid Kleckner8a365022013-06-24 17:51:48 +00001372 QualType getOriginalType() const;
Mike Stump11289f42009-09-09 15:08:12 +00001373
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001374 /// \brief Determine whether this parameter is actually a function
1375 /// parameter pack.
1376 bool isParameterPack() const;
David Blaikie21bfbf82011-11-09 06:07:30 +00001377
Douglas Gregorc72e6452009-01-09 18:51:29 +00001378 /// setOwningFunction - Sets the function declaration that owns this
1379 /// ParmVarDecl. Since ParmVarDecls are often created before the
1380 /// FunctionDecls that own them, this routine is required to update
1381 /// the DeclContext appropriately.
1382 void setOwningFunction(DeclContext *FD) { setDeclContext(FD); }
1383
Steve Naroffef2ab6a2007-04-02 20:50:54 +00001384 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00001385 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +00001386 static bool classofKind(Kind K) { return K == ParmVar; }
David Blaikie21bfbf82011-11-09 06:07:30 +00001387
Ted Kremenek9504ae92011-10-06 04:19:35 +00001388private:
Ted Kremenek540017e2011-10-06 05:00:56 +00001389 enum { ParameterIndexSentinel = (1 << NumParameterIndexBits) - 1 };
1390
Ted Kremenek9504ae92011-10-06 04:19:35 +00001391 void setParameterIndex(unsigned parameterIndex) {
Ted Kremenek540017e2011-10-06 05:00:56 +00001392 if (parameterIndex >= ParameterIndexSentinel) {
1393 setParameterIndexLarge(parameterIndex);
1394 return;
1395 }
David Blaikie21bfbf82011-11-09 06:07:30 +00001396
Ted Kremenek9504ae92011-10-06 04:19:35 +00001397 ParmVarDeclBits.ParameterIndex = parameterIndex;
1398 assert(ParmVarDeclBits.ParameterIndex == parameterIndex && "truncation!");
1399 }
Ted Kremenek540017e2011-10-06 05:00:56 +00001400 unsigned getParameterIndex() const {
1401 unsigned d = ParmVarDeclBits.ParameterIndex;
1402 return d == ParameterIndexSentinel ? getParameterIndexLarge() : d;
1403 }
David Blaikie21bfbf82011-11-09 06:07:30 +00001404
Ted Kremenek540017e2011-10-06 05:00:56 +00001405 void setParameterIndexLarge(unsigned parameterIndex);
1406 unsigned getParameterIndexLarge() const;
Chris Lattner17ed4872006-11-20 04:58:19 +00001407};
1408
Douglas Gregor89f238c2008-04-21 02:02:58 +00001409/// FunctionDecl - An instance of this class is created to represent a
Mike Stump11289f42009-09-09 15:08:12 +00001410/// function declaration or definition.
Douglas Gregor89f238c2008-04-21 02:02:58 +00001411///
1412/// Since a given function can be declared several times in a program,
1413/// there may be several FunctionDecls that correspond to that
1414/// function. Only one of those FunctionDecls will be found when
1415/// traversing the list of declarations in the context of the
1416/// FunctionDecl (e.g., the translation unit); this FunctionDecl
1417/// contains all of the information known about the function. Other,
1418/// previous declarations of the function are available via the
Douglas Gregorec9fd132012-01-14 16:38:05 +00001419/// getPreviousDecl() chain.
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001420class FunctionDecl : public DeclaratorDecl, public DeclContext,
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +00001421 public Redeclarable<FunctionDecl> {
Chris Lattnera11999d2006-10-15 22:34:45 +00001422public:
John McCall8e7d6562010-08-26 03:08:43 +00001423 typedef clang::StorageClass StorageClass;
Mike Stump11289f42009-09-09 15:08:12 +00001424
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001425 /// \brief The kind of templated function a FunctionDecl can be.
1426 enum TemplatedKind {
1427 TK_NonTemplate,
1428 TK_FunctionTemplate,
1429 TK_MemberSpecialization,
1430 TK_FunctionTemplateSpecialization,
1431 TK_DependentFunctionTemplateSpecialization
1432 };
1433
Mike Stump11289f42009-09-09 15:08:12 +00001434private:
Chris Lattneree1284a2008-03-16 00:16:02 +00001435 /// ParamInfo - new[]'d array of pointers to VarDecls for the formal
1436 /// parameters of this function. This is null if a prototype or if there are
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001437 /// no formals.
Chris Lattneree1284a2008-03-16 00:16:02 +00001438 ParmVarDecl **ParamInfo;
Mike Stump11289f42009-09-09 15:08:12 +00001439
James Molloy6f8780b2012-02-29 10:24:19 +00001440 /// DeclsInPrototypeScope - Array of pointers to NamedDecls for
1441 /// decls defined in the function prototype that are not parameters. E.g.
1442 /// 'enum Y' in 'void f(enum Y {AA} x) {}'.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001443 ArrayRef<NamedDecl *> DeclsInPrototypeScope;
James Molloy6f8780b2012-02-29 10:24:19 +00001444
Chris Lattner9c28af02009-04-27 05:46:25 +00001445 LazyDeclStmtPtr Body;
Douglas Gregor89f238c2008-04-21 02:02:58 +00001446
Daniel Dunbar14a41c52009-03-04 02:27:50 +00001447 // FIXME: This can be packed into the bitfields in Decl.
Argyrios Kyrtzidis2951e142008-06-09 21:05:31 +00001448 // NOTE: VC++ treats enums as signed, avoid using the StorageClass enum
1449 unsigned SClass : 2;
1450 bool IsInline : 1;
Douglas Gregorff76cb92010-12-09 16:59:22 +00001451 bool IsInlineSpecified : 1;
Anders Carlsson0a7c01f2009-05-14 22:15:41 +00001452 bool IsVirtualAsWritten : 1;
Sebastian Redl60795252009-01-09 19:57:06 +00001453 bool IsPure : 1;
Anders Carlssone0dd1d52009-05-14 21:46:00 +00001454 bool HasInheritedPrototype : 1;
1455 bool HasWrittenPrototype : 1;
Douglas Gregor171c45a2009-02-18 21:56:37 +00001456 bool IsDeleted : 1;
Douglas Gregor8a273912009-07-22 18:25:24 +00001457 bool IsTrivial : 1; // sunk from CXXMethodDecl
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001458 bool IsDefaulted : 1; // sunk from CXXMethoDecl
1459 bool IsExplicitlyDefaulted : 1; //sunk from CXXMethodDecl
John McCallcaa19452009-07-28 01:00:58 +00001460 bool HasImplicitReturnZero : 1;
Francois Pichet1c229c02011-04-22 22:18:13 +00001461 bool IsLateTemplateParsed : 1;
Richard Smitha77a0a62011-08-15 21:04:07 +00001462 bool IsConstexpr : 1;
Mike Stump11289f42009-09-09 15:08:12 +00001463
Argyrios Kyrtzidis1eb71a12012-12-06 18:59:10 +00001464 /// \brief Indicates if the function was a definition but its body was
1465 /// skipped.
1466 unsigned HasSkippedBody : 1;
1467
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +00001468 /// \brief End part of this FunctionDecl's source range.
1469 ///
1470 /// We could compute the full range in getSourceRange(). However, when we're
1471 /// dealing with a function definition deserialized from a PCH/AST file,
1472 /// we can only compute the full range once the function body has been
1473 /// de-serialized, so it's far better to have the (sometimes-redundant)
1474 /// EndRangeLoc.
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001475 SourceLocation EndRangeLoc;
Douglas Gregor24c332b2009-05-14 21:06:31 +00001476
1477 /// \brief The template or declaration that this declaration
1478 /// describes or was instantiated from, respectively.
Mike Stump11289f42009-09-09 15:08:12 +00001479 ///
Douglas Gregor24c332b2009-05-14 21:06:31 +00001480 /// For non-templates, this value will be NULL. For function
1481 /// declarations that describe a function template, this will be a
1482 /// pointer to a FunctionTemplateDecl. For member functions
Douglas Gregord801b062009-10-07 23:56:10 +00001483 /// of class template specializations, this will be a MemberSpecializationInfo
1484 /// pointer containing information about the specialization.
Mike Stump11289f42009-09-09 15:08:12 +00001485 /// For function template specializations, this will be a
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001486 /// FunctionTemplateSpecializationInfo, which contains information about
Mike Stump11289f42009-09-09 15:08:12 +00001487 /// the template being specialized and the template arguments involved in
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001488 /// that specialization.
David Blaikie21bfbf82011-11-09 06:07:30 +00001489 llvm::PointerUnion4<FunctionTemplateDecl *,
Douglas Gregord801b062009-10-07 23:56:10 +00001490 MemberSpecializationInfo *,
John McCallb9c78482010-04-08 09:05:18 +00001491 FunctionTemplateSpecializationInfo *,
1492 DependentFunctionTemplateSpecializationInfo *>
Douglas Gregor70d83e22009-06-29 17:30:29 +00001493 TemplateOrSpecialization;
Douglas Gregor24c332b2009-05-14 21:06:31 +00001494
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001495 /// DNLoc - Provides source/type location info for the
1496 /// declaration name embedded in the DeclaratorDecl base class.
1497 DeclarationNameLoc DNLoc;
1498
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001499 /// \brief Specify that this function declaration is actually a function
1500 /// template specialization.
1501 ///
1502 /// \param C the ASTContext.
1503 ///
1504 /// \param Template the function template that this function template
1505 /// specialization specializes.
1506 ///
1507 /// \param TemplateArgs the template arguments that produced this
1508 /// function template specialization from the template.
1509 ///
1510 /// \param InsertPos If non-NULL, the position in the function template
1511 /// specialization set where the function template specialization data will
1512 /// be inserted.
1513 ///
1514 /// \param TSK the kind of template specialization this is.
1515 ///
1516 /// \param TemplateArgsAsWritten location info of template arguments.
1517 ///
1518 /// \param PointOfInstantiation point at which the function template
David Blaikie21bfbf82011-11-09 06:07:30 +00001519 /// specialization was first instantiated.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001520 void setFunctionTemplateSpecialization(ASTContext &C,
1521 FunctionTemplateDecl *Template,
1522 const TemplateArgumentList *TemplateArgs,
1523 void *InsertPos,
1524 TemplateSpecializationKind TSK,
1525 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1526 SourceLocation PointOfInstantiation);
1527
1528 /// \brief Specify that this record is an instantiation of the
1529 /// member function FD.
1530 void setInstantiationOfMemberFunction(ASTContext &C, FunctionDecl *FD,
1531 TemplateSpecializationKind TSK);
1532
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001533 void setParams(ASTContext &C, ArrayRef<ParmVarDecl *> NewParamInfo);
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001534
Argyrios Kyrtzidis2951e142008-06-09 21:05:31 +00001535protected:
Abramo Bagnaradff19302011-03-08 08:55:46 +00001536 FunctionDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc,
1537 const DeclarationNameInfo &NameInfo,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001538 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001539 StorageClass S, bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00001540 bool isConstexprSpecified)
Abramo Bagnaradff19302011-03-08 08:55:46 +00001541 : DeclaratorDecl(DK, DC, NameInfo.getLoc(), NameInfo.getName(), T, TInfo,
1542 StartLoc),
Argyrios Kyrtzidis2951e142008-06-09 21:05:31 +00001543 DeclContext(DK),
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +00001544 ParamInfo(0), Body(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001545 SClass(S),
Douglas Gregorff76cb92010-12-09 16:59:22 +00001546 IsInline(isInlineSpecified), IsInlineSpecified(isInlineSpecified),
Mike Stump11289f42009-09-09 15:08:12 +00001547 IsVirtualAsWritten(false), IsPure(false), HasInheritedPrototype(false),
Douglas Gregor8a273912009-07-22 18:25:24 +00001548 HasWrittenPrototype(true), IsDeleted(false), IsTrivial(false),
Alexis Hunt88c75c32011-05-09 21:45:35 +00001549 IsDefaulted(false), IsExplicitlyDefaulted(false),
Francois Pichet1c229c02011-04-22 22:18:13 +00001550 HasImplicitReturnZero(false), IsLateTemplateParsed(false),
Argyrios Kyrtzidis8c1e6672012-12-06 19:41:23 +00001551 IsConstexpr(isConstexprSpecified), HasSkippedBody(false),
1552 EndRangeLoc(NameInfo.getEndLoc()),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001553 TemplateOrSpecialization(),
1554 DNLoc(NameInfo.getInfo()) {}
Douglas Gregor89f238c2008-04-21 02:02:58 +00001555
Argyrios Kyrtzidis05898da2009-07-18 08:50:35 +00001556 typedef Redeclarable<FunctionDecl> redeclarable_base;
Craig Toppercbce6e92014-03-11 06:22:39 +00001557 FunctionDecl *getNextRedeclaration() override { return RedeclLink.getNext(); }
1558 FunctionDecl *getPreviousDeclImpl() override {
Douglas Gregorec9fd132012-01-14 16:38:05 +00001559 return getPreviousDecl();
1560 }
Craig Toppercbce6e92014-03-11 06:22:39 +00001561 FunctionDecl *getMostRecentDeclImpl() override {
Douglas Gregorec9fd132012-01-14 16:38:05 +00001562 return getMostRecentDecl();
1563 }
Argyrios Kyrtzidis05898da2009-07-18 08:50:35 +00001564
Chris Lattner5072bae2008-03-15 21:24:04 +00001565public:
Aaron Ballman86c93902014-03-06 23:45:36 +00001566 typedef redeclarable_base::redecl_range redecl_range;
Aaron Ballman211cd8c2014-03-07 00:10:58 +00001567 typedef redeclarable_base::redecl_iterator redecl_iterator;
1568 using redeclarable_base::redecls_begin;
1569 using redeclarable_base::redecls_end;
Aaron Ballman86c93902014-03-06 23:45:36 +00001570 using redeclarable_base::redecls;
Douglas Gregorec9fd132012-01-14 16:38:05 +00001571 using redeclarable_base::getPreviousDecl;
1572 using redeclarable_base::getMostRecentDecl;
Rafael Espindola3f9e4442013-10-19 02:13:21 +00001573 using redeclarable_base::isFirstDecl;
Douglas Gregor0bc8a212012-01-14 15:55:47 +00001574
Abramo Bagnaradff19302011-03-08 08:55:46 +00001575 static FunctionDecl *Create(ASTContext &C, DeclContext *DC,
1576 SourceLocation StartLoc, SourceLocation NLoc,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001577 DeclarationName N, QualType T,
John McCallbcd03502009-12-07 02:54:59 +00001578 TypeSourceInfo *TInfo,
Rafael Espindolab49ec5d2013-03-30 23:04:08 +00001579 StorageClass SC,
Douglas Gregorff76cb92010-12-09 16:59:22 +00001580 bool isInlineSpecified = false,
Richard Smitha77a0a62011-08-15 21:04:07 +00001581 bool hasWrittenPrototype = true,
1582 bool isConstexprSpecified = false) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001583 DeclarationNameInfo NameInfo(N, NLoc);
1584 return FunctionDecl::Create(C, DC, StartLoc, NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001585 SC,
Richard Smitha77a0a62011-08-15 21:04:07 +00001586 isInlineSpecified, hasWrittenPrototype,
1587 isConstexprSpecified);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001588 }
1589
1590 static FunctionDecl *Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001591 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001592 const DeclarationNameInfo &NameInfo,
1593 QualType T, TypeSourceInfo *TInfo,
Rafael Espindolab49ec5d2013-03-30 23:04:08 +00001594 StorageClass SC,
Rafael Espindolab49ec5d2013-03-30 23:04:08 +00001595 bool isInlineSpecified,
1596 bool hasWrittenPrototype,
Richard Smitha77a0a62011-08-15 21:04:07 +00001597 bool isConstexprSpecified = false);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001598
Douglas Gregor72172e92012-01-05 21:55:30 +00001599 static FunctionDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1600
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001601 DeclarationNameInfo getNameInfo() const {
1602 return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc);
1603 }
1604
Craig Toppercbce6e92014-03-11 06:22:39 +00001605 void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy,
1606 bool Qualified) const override;
Douglas Gregorb11aad82011-02-19 18:51:44 +00001607
Abramo Bagnaraea947882011-03-08 16:41:52 +00001608 void setRangeEnd(SourceLocation E) { EndRangeLoc = E; }
1609
Craig Toppercbce6e92014-03-11 06:22:39 +00001610 SourceRange getSourceRange() const override LLVM_READONLY;
Steve Naroff22315692008-10-03 00:02:03 +00001611
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001612 /// \brief Returns true if the function has a body (definition). The
1613 /// function body might be in any of the (re-)declarations of this
1614 /// function. The variant that accepts a FunctionDecl pointer will
1615 /// set that function declaration to the actual declaration
1616 /// containing the body (if there is one).
1617 bool hasBody(const FunctionDecl *&Definition) const;
1618
Craig Toppercbce6e92014-03-11 06:22:39 +00001619 bool hasBody() const override {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001620 const FunctionDecl* Definition;
1621 return hasBody(Definition);
1622 }
1623
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001624 /// hasTrivialBody - Returns whether the function has a trivial body that does
1625 /// not require any specific codegen.
1626 bool hasTrivialBody() const;
1627
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001628 /// isDefined - Returns true if the function is defined at all, including
1629 /// a deleted definition. Except for the behavior when the function is
1630 /// deleted, behaves like hasBody.
1631 bool isDefined(const FunctionDecl *&Definition) const;
1632
1633 virtual bool isDefined() const {
1634 const FunctionDecl* Definition;
1635 return isDefined(Definition);
1636 }
1637
Douglas Gregor89f238c2008-04-21 02:02:58 +00001638 /// getBody - Retrieve the body (definition) of the function. The
1639 /// function body might be in any of the (re-)declarations of this
1640 /// function. The variant that accepts a FunctionDecl pointer will
1641 /// set that function declaration to the actual declaration
1642 /// containing the body (if there is one).
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001643 /// NOTE: For checking if there is a body, use hasBody() instead, to avoid
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001644 /// unnecessary AST de-serialization of the body.
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001645 Stmt *getBody(const FunctionDecl *&Definition) const;
Ted Kremeneke5b40a92008-07-31 17:32:12 +00001646
Craig Toppercbce6e92014-03-11 06:22:39 +00001647 Stmt *getBody() const override {
Douglas Gregor89f238c2008-04-21 02:02:58 +00001648 const FunctionDecl* Definition;
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001649 return getBody(Definition);
Douglas Gregor89f238c2008-04-21 02:02:58 +00001650 }
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001651
Douglas Gregor89f238c2008-04-21 02:02:58 +00001652 /// isThisDeclarationADefinition - Returns whether this specific
1653 /// declaration of the function is also a definition. This does not
1654 /// determine whether the function has been defined (e.g., in a
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001655 /// previous definition); for that information, use isDefined. Note
1656 /// that this returns false for a defaulted function unless that function
1657 /// has been implicitly defined (possibly as deleted).
Francois Pichet1c229c02011-04-22 22:18:13 +00001658 bool isThisDeclarationADefinition() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001659 return IsDeleted || Body || IsLateTemplateParsed;
1660 }
1661
1662 /// doesThisDeclarationHaveABody - Returns whether this specific
1663 /// declaration of the function has a body - that is, if it is a non-
1664 /// deleted definition.
1665 bool doesThisDeclarationHaveABody() const {
Francois Pichet1c229c02011-04-22 22:18:13 +00001666 return Body || IsLateTemplateParsed;
1667 }
Douglas Gregor89f238c2008-04-21 02:02:58 +00001668
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001669 void setBody(Stmt *B);
Douglas Gregor3c3aa612009-04-18 00:07:54 +00001670 void setLazyBody(uint64_t Offset) { Body = Offset; }
Sebastian Redl60795252009-01-09 19:57:06 +00001671
Ted Kremenek186a0742010-04-29 16:49:01 +00001672 /// Whether this function is variadic.
1673 bool isVariadic() const;
1674
Anders Carlsson0a7c01f2009-05-14 22:15:41 +00001675 /// Whether this function is marked as virtual explicitly.
1676 bool isVirtualAsWritten() const { return IsVirtualAsWritten; }
1677 void setVirtualAsWritten(bool V) { IsVirtualAsWritten = V; }
Sebastian Redl60795252009-01-09 19:57:06 +00001678
Sebastian Redl6138f2b2009-01-09 22:29:03 +00001679 /// Whether this virtual function is pure, i.e. makes the containing class
1680 /// abstract.
Anders Carlsson7cbd8fb2009-03-22 01:52:17 +00001681 bool isPure() const { return IsPure; }
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001682 void setPure(bool P = true);
Sebastian Redl60795252009-01-09 19:57:06 +00001683
Francois Pichet1c229c02011-04-22 22:18:13 +00001684 /// Whether this templated function will be late parsed.
1685 bool isLateTemplateParsed() const { return IsLateTemplateParsed; }
1686 void setLateTemplateParsed(bool ILT = true) { IsLateTemplateParsed = ILT; }
1687
Douglas Gregor8a273912009-07-22 18:25:24 +00001688 /// Whether this function is "trivial" in some specialized C++ senses.
1689 /// Can only be true for default constructors, copy constructors,
1690 /// copy assignment operators, and destructors. Not meaningful until
1691 /// the class has been fully built by Sema.
1692 bool isTrivial() const { return IsTrivial; }
1693 void setTrivial(bool IT) { IsTrivial = IT; }
Mike Stump11289f42009-09-09 15:08:12 +00001694
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001695 /// Whether this function is defaulted per C++0x. Only valid for
David Blaikie21bfbf82011-11-09 06:07:30 +00001696 /// special member functions.
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001697 bool isDefaulted() const { return IsDefaulted; }
1698 void setDefaulted(bool D = true) { IsDefaulted = D; }
1699
1700 /// Whether this function is explicitly defaulted per C++0x. Only valid
1701 /// for special member functions.
1702 bool isExplicitlyDefaulted() const { return IsExplicitlyDefaulted; }
1703 void setExplicitlyDefaulted(bool ED = true) { IsExplicitlyDefaulted = ED; }
1704
John McCallcaa19452009-07-28 01:00:58 +00001705 /// Whether falling off this function implicitly returns null/zero.
1706 /// If a more specific implicit return value is required, front-ends
1707 /// should synthesize the appropriate return statements.
1708 bool hasImplicitReturnZero() const { return HasImplicitReturnZero; }
1709 void setHasImplicitReturnZero(bool IRZ) { HasImplicitReturnZero = IRZ; }
1710
Douglas Gregor739ef0c2009-02-25 16:33:18 +00001711 /// \brief Whether this function has a prototype, either because one
1712 /// was explicitly written or because it was "inherited" by merging
1713 /// a declaration without a prototype with a declaration that has a
1714 /// prototype.
Mike Stump11289f42009-09-09 15:08:12 +00001715 bool hasPrototype() const {
1716 return HasWrittenPrototype || HasInheritedPrototype;
Anders Carlssone0dd1d52009-05-14 21:46:00 +00001717 }
Mike Stump11289f42009-09-09 15:08:12 +00001718
Anders Carlssone0dd1d52009-05-14 21:46:00 +00001719 bool hasWrittenPrototype() const { return HasWrittenPrototype; }
Douglas Gregor739ef0c2009-02-25 16:33:18 +00001720
Douglas Gregorbcbf8632009-02-16 18:20:44 +00001721 /// \brief Whether this function inherited its prototype from a
1722 /// previous declaration.
Anders Carlssone0dd1d52009-05-14 21:46:00 +00001723 bool hasInheritedPrototype() const { return HasInheritedPrototype; }
1724 void setHasInheritedPrototype(bool P = true) { HasInheritedPrototype = P; }
Douglas Gregorbcbf8632009-02-16 18:20:44 +00001725
Richard Smith12f247f2012-06-08 21:09:22 +00001726 /// Whether this is a (C++11) constexpr function or constexpr constructor.
Richard Smitha77a0a62011-08-15 21:04:07 +00001727 bool isConstexpr() const { return IsConstexpr; }
Richard Smith92f241f2012-12-08 02:53:02 +00001728 void setConstexpr(bool IC) { IsConstexpr = IC; }
Richard Smitha77a0a62011-08-15 21:04:07 +00001729
Douglas Gregor171c45a2009-02-18 21:56:37 +00001730 /// \brief Whether this function has been deleted.
1731 ///
1732 /// A function that is "deleted" (via the C++0x "= delete" syntax)
1733 /// acts like a normal function, except that it cannot actually be
1734 /// called or have its address taken. Deleted functions are
1735 /// typically used in C++ overload resolution to attract arguments
1736 /// whose type or lvalue/rvalue-ness would permit the use of a
1737 /// different overload that would behave incorrectly. For example,
1738 /// one might use deleted functions to ban implicit conversion from
1739 /// a floating-point number to an Integer type:
1740 ///
1741 /// @code
1742 /// struct Integer {
1743 /// Integer(long); // construct from a long
1744 /// Integer(double) = delete; // no construction from float or double
1745 /// Integer(long double) = delete; // no construction from long double
1746 /// };
1747 /// @endcode
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001748 // If a function is deleted, its first declaration must be.
1749 bool isDeleted() const { return getCanonicalDecl()->IsDeleted; }
1750 bool isDeletedAsWritten() const { return IsDeleted && !IsDefaulted; }
1751 void setDeletedAsWritten(bool D = true) { IsDeleted = D; }
Douglas Gregor171c45a2009-02-18 21:56:37 +00001752
John McCall53ffd372011-05-15 17:49:20 +00001753 /// \brief Determines whether this function is "main", which is the
1754 /// entry point into an executable program.
Douglas Gregor16618f22009-09-12 00:17:51 +00001755 bool isMain() const;
Douglas Gregore62c0a42009-02-24 01:23:02 +00001756
David Majnemerc729b0b2013-09-16 22:44:20 +00001757 /// \brief Determines whether this function is a MSVCRT user defined entry
1758 /// point.
1759 bool isMSVCRTEntryPoint() const;
1760
John McCall53ffd372011-05-15 17:49:20 +00001761 /// \brief Determines whether this operator new or delete is one
1762 /// of the reserved global placement operators:
1763 /// void *operator new(size_t, void *);
1764 /// void *operator new[](size_t, void *);
1765 /// void operator delete(void *, void *);
1766 /// void operator delete[](void *, void *);
1767 /// These functions have special behavior under [new.delete.placement]:
1768 /// These functions are reserved, a C++ program may not define
1769 /// functions that displace the versions in the Standard C++ library.
1770 /// The provisions of [basic.stc.dynamic] do not apply to these
1771 /// reserved placement forms of operator new and operator delete.
1772 ///
1773 /// This function must be an allocation or deallocation function.
1774 bool isReservedGlobalPlacementOperator() const;
1775
Richard Smith8d0dc312013-07-21 23:12:18 +00001776 /// \brief Determines whether this function is one of the replaceable
1777 /// global allocation functions:
1778 /// void *operator new(size_t);
1779 /// void *operator new(size_t, const std::nothrow_t &) noexcept;
1780 /// void *operator new[](size_t);
1781 /// void *operator new[](size_t, const std::nothrow_t &) noexcept;
1782 /// void operator delete(void *) noexcept;
Richard Smith1cdec012013-09-29 04:40:38 +00001783 /// void operator delete(void *, std::size_t) noexcept; [C++1y]
Richard Smith8d0dc312013-07-21 23:12:18 +00001784 /// void operator delete(void *, const std::nothrow_t &) noexcept;
1785 /// void operator delete[](void *) noexcept;
Richard Smith1cdec012013-09-29 04:40:38 +00001786 /// void operator delete[](void *, std::size_t) noexcept; [C++1y]
Richard Smith8d0dc312013-07-21 23:12:18 +00001787 /// void operator delete[](void *, const std::nothrow_t &) noexcept;
1788 /// These functions have special behavior under C++1y [expr.new]:
1789 /// An implementation is allowed to omit a call to a replaceable global
1790 /// allocation function. [...]
1791 bool isReplaceableGlobalAllocationFunction() const;
1792
Richard Smithb47c36f2013-11-05 09:12:18 +00001793 /// \brief Determine whether this function is a sized global deallocation
1794 /// function in C++1y. If so, find and return the corresponding unsized
1795 /// deallocation function.
1796 FunctionDecl *getCorrespondingUnsizedGlobalDeallocationFunction() const;
1797
Rafael Espindolaf4187652013-02-14 01:18:37 +00001798 /// Compute the language linkage.
1799 LanguageLinkage getLanguageLinkage() const;
1800
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001801 /// \brief Determines whether this function is a function with
1802 /// external, C linkage.
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001803 bool isExternC() const;
Rafael Espindola576127d2012-12-28 14:21:58 +00001804
Rafael Espindola593537a2013-05-05 20:15:21 +00001805 /// \brief Determines whether this function's context is, or is nested within,
1806 /// a C++ extern "C" linkage spec.
1807 bool isInExternCContext() const;
1808
1809 /// \brief Determines whether this function's context is, or is nested within,
1810 /// a C++ extern "C++" linkage spec.
1811 bool isInExternCXXContext() const;
1812
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001813 /// \brief Determines whether this is a global function.
1814 bool isGlobal() const;
1815
Richard Smith10876ef2013-01-17 01:30:42 +00001816 /// \brief Determines whether this function is known to be 'noreturn', through
1817 /// an attribute on its declaration or its type.
1818 bool isNoReturn() const;
1819
Argyrios Kyrtzidis1eb71a12012-12-06 18:59:10 +00001820 /// \brief True if the function was a definition but its body was skipped.
1821 bool hasSkippedBody() const { return HasSkippedBody; }
1822 void setHasSkippedBody(bool Skipped = true) { HasSkippedBody = Skipped; }
1823
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001824 void setPreviousDeclaration(FunctionDecl * PrevDecl);
Eli Friedmanaee9e542008-05-27 05:07:37 +00001825
Douglas Gregorb11aad82011-02-19 18:51:44 +00001826 virtual const FunctionDecl *getCanonicalDecl() const;
Craig Toppercbce6e92014-03-11 06:22:39 +00001827 FunctionDecl *getCanonicalDecl() override;
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001828
Douglas Gregor15fc9562009-09-12 00:22:50 +00001829 unsigned getBuiltinID() const;
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001830
Chris Lattner9b65d352007-12-19 23:58:25 +00001831 // Iterator access to formal parameters.
1832 unsigned param_size() const { return getNumParams(); }
1833 typedef ParmVarDecl **param_iterator;
1834 typedef ParmVarDecl * const *param_const_iterator;
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00001835 typedef llvm::iterator_range<param_iterator> param_range;
1836 typedef llvm::iterator_range<param_const_iterator> param_const_range;
Mike Stump11289f42009-09-09 15:08:12 +00001837
Aaron Ballmanda634f12014-03-07 22:17:20 +00001838 param_iterator param_begin() { return param_iterator(ParamInfo); }
1839 param_iterator param_end() {
1840 return param_iterator(ParamInfo + param_size());
1841 }
Aaron Ballman43027eb2014-03-08 18:30:40 +00001842 param_range params() { return param_range(param_begin(), param_end()); }
Mike Stump11289f42009-09-09 15:08:12 +00001843
Aaron Ballmanda634f12014-03-07 22:17:20 +00001844 param_const_iterator param_begin() const {
1845 return param_const_iterator(ParamInfo);
1846 }
1847 param_const_iterator param_end() const {
1848 return param_const_iterator(ParamInfo + param_size());
1849 }
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00001850 param_const_range params() const {
Aaron Ballman43027eb2014-03-08 18:30:40 +00001851 return param_const_range(param_begin(), param_end());
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00001852 }
Mike Stump11289f42009-09-09 15:08:12 +00001853
Chris Lattner47c0d002009-04-25 06:03:53 +00001854 /// getNumParams - Return the number of parameters this function must have
Sebastian Redl833ef452010-01-26 22:01:41 +00001855 /// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001856 /// after it has been created.
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001857 unsigned getNumParams() const;
Mike Stump11289f42009-09-09 15:08:12 +00001858
Chris Lattner53621a52007-06-13 20:44:40 +00001859 const ParmVarDecl *getParamDecl(unsigned i) const {
Chris Lattner2e89ebb2007-01-21 23:09:17 +00001860 assert(i < getNumParams() && "Illegal param #");
1861 return ParamInfo[i];
1862 }
Chris Lattner53621a52007-06-13 20:44:40 +00001863 ParmVarDecl *getParamDecl(unsigned i) {
1864 assert(i < getNumParams() && "Illegal param #");
1865 return ParamInfo[i];
1866 }
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001867 void setParams(ArrayRef<ParmVarDecl *> NewParamInfo) {
David Blaikie9c70e042011-09-21 18:16:56 +00001868 setParams(getASTContext(), NewParamInfo);
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001869 }
Chris Lattnerb0d38442008-04-12 23:52:44 +00001870
Ted Kremenek07e4a662014-01-17 06:24:50 +00001871 // ArrayRef iterface to parameters.
1872 // FIXME: Should one day replace iterator interface.
1873 ArrayRef<ParmVarDecl*> parameters() const {
1874 return llvm::makeArrayRef(ParamInfo, getNumParams());
1875 }
1876
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001877 const ArrayRef<NamedDecl *> &getDeclsInPrototypeScope() const {
James Molloy6f8780b2012-02-29 10:24:19 +00001878 return DeclsInPrototypeScope;
1879 }
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001880 void setDeclsInPrototypeScope(ArrayRef<NamedDecl *> NewDecls);
James Molloy6f8780b2012-02-29 10:24:19 +00001881
Chris Lattnerb0d38442008-04-12 23:52:44 +00001882 /// getMinRequiredArguments - Returns the minimum number of arguments
1883 /// needed to call this function. This may be fewer than the number of
1884 /// function parameters, if some of the parameters have default
1885 /// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001886 unsigned getMinRequiredArguments() const;
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001887
Alp Toker314cc812014-01-25 16:55:45 +00001888 QualType getReturnType() const {
1889 return getType()->getAs<FunctionType>()->getReturnType();
Steve Naroff9358c712007-05-27 23:58:33 +00001890 }
David Blaikie21bfbf82011-11-09 06:07:30 +00001891
Douglas Gregor603d81b2010-07-13 08:18:22 +00001892 /// \brief Determine the type of an expression that calls this function.
1893 QualType getCallResultType() const {
1894 return getType()->getAs<FunctionType>()->getCallResultType(getASTContext());
1895 }
David Blaikie21bfbf82011-11-09 06:07:30 +00001896
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001897 /// \brief Returns the storage class as written in the source. For the
1898 /// computed linkage of symbol, see getLinkage.
Ted Kremenek72be0682008-02-24 03:55:14 +00001899 StorageClass getStorageClass() const { return StorageClass(SClass); }
Douglas Gregorc4df4072010-04-19 22:54:31 +00001900
Douglas Gregor35b57532009-10-27 21:01:01 +00001901 /// \brief Determine whether the "inline" keyword was specified for this
1902 /// function.
Douglas Gregorff76cb92010-12-09 16:59:22 +00001903 bool isInlineSpecified() const { return IsInlineSpecified; }
David Blaikie21bfbf82011-11-09 06:07:30 +00001904
Douglas Gregor35b57532009-10-27 21:01:01 +00001905 /// Set whether the "inline" keyword was specified for this function.
David Blaikie21bfbf82011-11-09 06:07:30 +00001906 void setInlineSpecified(bool I) {
1907 IsInlineSpecified = I;
Douglas Gregorff76cb92010-12-09 16:59:22 +00001908 IsInline = I;
1909 }
Douglas Gregore62c0a42009-02-24 01:23:02 +00001910
John McCall357d0f32010-12-15 04:00:32 +00001911 /// Flag that this function is implicitly inline.
1912 void setImplicitlyInline() {
1913 IsInline = true;
1914 }
1915
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001916 /// \brief Determine whether this function should be inlined, because it is
Richard Smith7cd25b22011-09-30 00:45:47 +00001917 /// either marked "inline" or "constexpr" or is a member function of a class
1918 /// that was defined in the class body.
Richard Smithf3814ad2013-01-25 00:08:28 +00001919 bool isInlined() const { return IsInline; }
John McCall357d0f32010-12-15 04:00:32 +00001920
Douglas Gregor299d76e2009-09-13 07:46:26 +00001921 bool isInlineDefinitionExternallyVisible() const;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001922
David Majnemer54e3ba52014-04-02 23:17:29 +00001923 bool isMSExternInline() const;
1924
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001925 bool doesDeclarationForceExternallyVisibleDefinition() const;
David Blaikie21bfbf82011-11-09 06:07:30 +00001926
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001927 /// isOverloadedOperator - Whether this function declaration
1928 /// represents an C++ overloaded operator, e.g., "operator+".
Mike Stump11289f42009-09-09 15:08:12 +00001929 bool isOverloadedOperator() const {
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001930 return getOverloadedOperator() != OO_None;
Daniel Dunbare017ecc2009-12-19 17:50:07 +00001931 }
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001932
1933 OverloadedOperatorKind getOverloadedOperator() const;
1934
Alexis Huntc88db062010-01-13 09:01:02 +00001935 const IdentifierInfo *getLiteralIdentifier() const;
1936
Douglas Gregor24c332b2009-05-14 21:06:31 +00001937 /// \brief If this function is an instantiation of a member function
1938 /// of a class template specialization, retrieves the function from
1939 /// which it was instantiated.
1940 ///
1941 /// This routine will return non-NULL for (non-templated) member
1942 /// functions of class templates and for instantiations of function
1943 /// templates. For example, given:
1944 ///
1945 /// \code
1946 /// template<typename T>
1947 /// struct X {
1948 /// void f(T);
1949 /// };
1950 /// \endcode
1951 ///
1952 /// The declaration for X<int>::f is a (non-templated) FunctionDecl
1953 /// whose parent is the class template specialization X<int>. For
1954 /// this declaration, getInstantiatedFromFunction() will return
1955 /// the FunctionDecl X<T>::A. When a complete definition of
1956 /// X<int>::A is required, it will be instantiated from the
1957 /// declaration returned by getInstantiatedFromMemberFunction().
Douglas Gregord801b062009-10-07 23:56:10 +00001958 FunctionDecl *getInstantiatedFromMemberFunction() const;
David Blaikie21bfbf82011-11-09 06:07:30 +00001959
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001960 /// \brief What kind of templated function this is.
1961 TemplatedKind getTemplatedKind() const;
Douglas Gregor24c332b2009-05-14 21:06:31 +00001962
Douglas Gregor06db9f52009-10-12 20:18:28 +00001963 /// \brief If this function is an instantiation of a member function of a
1964 /// class template specialization, retrieves the member specialization
1965 /// information.
John McCall5f46c482013-02-21 23:42:58 +00001966 MemberSpecializationInfo *getMemberSpecializationInfo() const {
1967 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1968 }
David Blaikie21bfbf82011-11-09 06:07:30 +00001969
Douglas Gregor24c332b2009-05-14 21:06:31 +00001970 /// \brief Specify that this record is an instantiation of the
Douglas Gregord801b062009-10-07 23:56:10 +00001971 /// member function FD.
1972 void setInstantiationOfMemberFunction(FunctionDecl *FD,
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001973 TemplateSpecializationKind TSK) {
1974 setInstantiationOfMemberFunction(getASTContext(), FD, TSK);
1975 }
Douglas Gregor24c332b2009-05-14 21:06:31 +00001976
1977 /// \brief Retrieves the function template that is described by this
1978 /// function declaration.
1979 ///
1980 /// Every function template is represented as a FunctionTemplateDecl
1981 /// and a FunctionDecl (or something derived from FunctionDecl). The
1982 /// former contains template properties (such as the template
1983 /// parameter lists) while the latter contains the actual
1984 /// description of the template's
1985 /// contents. FunctionTemplateDecl::getTemplatedDecl() retrieves the
1986 /// FunctionDecl that describes the function template,
1987 /// getDescribedFunctionTemplate() retrieves the
1988 /// FunctionTemplateDecl from a FunctionDecl.
1989 FunctionTemplateDecl *getDescribedFunctionTemplate() const {
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001990 return TemplateOrSpecialization.dyn_cast<FunctionTemplateDecl*>();
Douglas Gregor24c332b2009-05-14 21:06:31 +00001991 }
1992
1993 void setDescribedFunctionTemplate(FunctionTemplateDecl *Template) {
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001994 TemplateOrSpecialization = Template;
Douglas Gregor24c332b2009-05-14 21:06:31 +00001995 }
1996
David Blaikie21bfbf82011-11-09 06:07:30 +00001997 /// \brief Determine whether this function is a function template
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001998 /// specialization.
1999 bool isFunctionTemplateSpecialization() const {
2000 return getPrimaryTemplate() != 0;
2001 }
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002002
2003 /// \brief Retrieve the class scope template pattern that this function
2004 /// template specialization is instantiated from.
2005 FunctionDecl *getClassScopeSpecializationPattern() const;
2006
Douglas Gregor06db9f52009-10-12 20:18:28 +00002007 /// \brief If this function is actually a function template specialization,
David Blaikie21bfbf82011-11-09 06:07:30 +00002008 /// retrieve information about this function template specialization.
Douglas Gregor06db9f52009-10-12 20:18:28 +00002009 /// Otherwise, returns NULL.
2010 FunctionTemplateSpecializationInfo *getTemplateSpecializationInfo() const {
2011 return TemplateOrSpecialization.
2012 dyn_cast<FunctionTemplateSpecializationInfo*>();
2013 }
Douglas Gregorafca3b42009-10-27 20:53:28 +00002014
2015 /// \brief Determines whether this function is a function template
2016 /// specialization or a member of a class template specialization that can
2017 /// be implicitly instantiated.
2018 bool isImplicitlyInstantiable() const;
David Blaikie21bfbf82011-11-09 06:07:30 +00002019
Ted Kremenek85825ae2011-12-01 00:59:17 +00002020 /// \brief Determines if the given function was instantiated from a
2021 /// function template.
2022 bool isTemplateInstantiation() const;
2023
Douglas Gregorafca3b42009-10-27 20:53:28 +00002024 /// \brief Retrieve the function declaration from which this function could
2025 /// be instantiated, if it is an instantiation (rather than a non-template
2026 /// or a specialization, for example).
2027 FunctionDecl *getTemplateInstantiationPattern() const;
2028
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002029 /// \brief Retrieve the primary template that this function template
2030 /// specialization either specializes or was instantiated from.
2031 ///
2032 /// If this function declaration is not a function template specialization,
2033 /// returns NULL.
Douglas Gregor70d83e22009-06-29 17:30:29 +00002034 FunctionTemplateDecl *getPrimaryTemplate() const;
Mike Stump11289f42009-09-09 15:08:12 +00002035
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002036 /// \brief Retrieve the template arguments used to produce this function
2037 /// template specialization from the primary template.
2038 ///
2039 /// If this function declaration is not a function template specialization,
2040 /// returns NULL.
Mike Stump11289f42009-09-09 15:08:12 +00002041 const TemplateArgumentList *getTemplateSpecializationArgs() const;
2042
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002043 /// \brief Retrieve the template argument list as written in the sources,
2044 /// if any.
2045 ///
2046 /// If this function declaration is not a function template specialization
2047 /// or if it had no explicit template argument list, returns NULL.
2048 /// Note that it an explicit template argument list may be written empty,
2049 /// e.g., template<> void foo<>(char* s);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002050 const ASTTemplateArgumentListInfo*
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002051 getTemplateSpecializationArgsAsWritten() const;
2052
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002053 /// \brief Specify that this function declaration is actually a function
2054 /// template specialization.
2055 ///
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002056 /// \param Template the function template that this function template
2057 /// specialization specializes.
2058 ///
2059 /// \param TemplateArgs the template arguments that produced this
2060 /// function template specialization from the template.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002061 ///
2062 /// \param InsertPos If non-NULL, the position in the function template
2063 /// specialization set where the function template specialization data will
2064 /// be inserted.
2065 ///
2066 /// \param TSK the kind of template specialization this is.
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002067 ///
2068 /// \param TemplateArgsAsWritten location info of template arguments.
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002069 ///
2070 /// \param PointOfInstantiation point at which the function template
David Blaikie21bfbf82011-11-09 06:07:30 +00002071 /// specialization was first instantiated.
Douglas Gregord5058122010-02-11 01:19:42 +00002072 void setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002073 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002074 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002075 TemplateSpecializationKind TSK = TSK_ImplicitInstantiation,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002076 const TemplateArgumentListInfo *TemplateArgsAsWritten = 0,
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002077 SourceLocation PointOfInstantiation = SourceLocation()) {
2078 setFunctionTemplateSpecialization(getASTContext(), Template, TemplateArgs,
2079 InsertPos, TSK, TemplateArgsAsWritten,
2080 PointOfInstantiation);
2081 }
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002082
John McCallb9c78482010-04-08 09:05:18 +00002083 /// \brief Specifies that this function declaration is actually a
2084 /// dependent function template specialization.
2085 void setDependentTemplateSpecialization(ASTContext &Context,
2086 const UnresolvedSetImpl &Templates,
2087 const TemplateArgumentListInfo &TemplateArgs);
2088
2089 DependentFunctionTemplateSpecializationInfo *
2090 getDependentSpecializationInfo() const {
2091 return TemplateOrSpecialization.
2092 dyn_cast<DependentFunctionTemplateSpecializationInfo*>();
2093 }
2094
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002095 /// \brief Determine what kind of template instantiation this function
2096 /// represents.
2097 TemplateSpecializationKind getTemplateSpecializationKind() const;
Douglas Gregore8925db2009-06-29 22:39:32 +00002098
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002099 /// \brief Determine what kind of template instantiation this function
2100 /// represents.
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002101 void setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2102 SourceLocation PointOfInstantiation = SourceLocation());
2103
2104 /// \brief Retrieve the (first) point of instantiation of a function template
2105 /// specialization or a member of a class template specialization.
2106 ///
David Blaikie21bfbf82011-11-09 06:07:30 +00002107 /// \returns the first point of instantiation, if this function was
2108 /// instantiated from a template; otherwise, returns an invalid source
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002109 /// location.
2110 SourceLocation getPointOfInstantiation() const;
David Blaikie21bfbf82011-11-09 06:07:30 +00002111
2112 /// \brief Determine whether this is or was instantiated from an out-of-line
Douglas Gregor6411b922009-09-11 20:15:17 +00002113 /// definition of a member function.
Craig Toppercbce6e92014-03-11 06:22:39 +00002114 bool isOutOfLine() const override;
David Blaikie21bfbf82011-11-09 06:07:30 +00002115
Anna Zaks22122702012-01-17 00:37:07 +00002116 /// \brief Identify a memory copying or setting function.
2117 /// If the given function is a memory copy or setting function, returns
2118 /// the corresponding Builtin ID. If the function is not a memory function,
2119 /// returns 0.
Anna Zaks28db7ce2012-01-18 02:45:01 +00002120 unsigned getMemoryFunctionKind() const;
Anna Zaks201d4892012-01-13 21:52:01 +00002121
Chris Lattnerda8aa7b2006-11-19 23:12:30 +00002122 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00002123 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +00002124 static bool classofKind(Kind K) {
Alexis Hunted053252010-05-30 07:21:58 +00002125 return K >= firstFunction && K <= lastFunction;
John McCall180ef092010-01-29 01:45:37 +00002126 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +00002127 static DeclContext *castToDeclContext(const FunctionDecl *D) {
2128 return static_cast<DeclContext *>(const_cast<FunctionDecl*>(D));
2129 }
2130 static FunctionDecl *castFromDeclContext(const DeclContext *DC) {
2131 return static_cast<FunctionDecl *>(const_cast<DeclContext*>(DC));
2132 }
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00002133
Sebastian Redlb3298c32010-08-18 23:56:48 +00002134 friend class ASTDeclReader;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002135 friend class ASTDeclWriter;
Chris Lattnera11999d2006-10-15 22:34:45 +00002136};
2137
Chris Lattner1300fb92007-01-23 23:42:53 +00002138
Mike Stump11289f42009-09-09 15:08:12 +00002139/// FieldDecl - An instance of this class is created by Sema::ActOnField to
Steve Naroffcc321422007-03-26 23:09:51 +00002140/// represent a member of a struct/union/class.
Richard Smith0b87e072013-10-07 08:02:11 +00002141class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> {
Daniel Dunbar14a41c52009-03-04 02:27:50 +00002142 // FIXME: This can be packed into the bitfields in Decl.
Douglas Gregor91f84212008-12-11 16:49:14 +00002143 bool Mutable : 1;
John McCall4e819612011-01-20 07:57:12 +00002144 mutable unsigned CachedFieldIndex : 31;
2145
Richard Smith2b013182012-06-10 03:12:00 +00002146 /// \brief An InClassInitStyle value, and either a bit width expression (if
2147 /// the InClassInitStyle value is ICIS_NoInit), or a pointer to the in-class
2148 /// initializer for this field (otherwise).
Richard Smith938f40b2011-06-11 17:19:42 +00002149 ///
2150 /// We can safely combine these two because in-class initializers are not
2151 /// permitted for bit-fields.
2152 ///
Richard Smith2b013182012-06-10 03:12:00 +00002153 /// If the InClassInitStyle is not ICIS_NoInit and the initializer is null,
2154 /// then this field has an in-class initializer which has not yet been parsed
2155 /// and attached.
2156 llvm::PointerIntPair<Expr *, 2, unsigned> InitializerOrBitWidth;
Chris Lattnerd3f989c2008-03-15 23:04:32 +00002157protected:
Abramo Bagnaradff19302011-03-08 08:55:46 +00002158 FieldDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc,
2159 SourceLocation IdLoc, IdentifierInfo *Id,
Richard Smith938f40b2011-06-11 17:19:42 +00002160 QualType T, TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smith2b013182012-06-10 03:12:00 +00002161 InClassInitStyle InitStyle)
Abramo Bagnaradff19302011-03-08 08:55:46 +00002162 : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc),
Richard Smith938f40b2011-06-11 17:19:42 +00002163 Mutable(Mutable), CachedFieldIndex(0),
Richard Smith2b013182012-06-10 03:12:00 +00002164 InitializerOrBitWidth(BW, InitStyle) {
2165 assert((!BW || InitStyle == ICIS_NoInit) && "got initializer for bitfield");
Mike Stump11289f42009-09-09 15:08:12 +00002166 }
Douglas Gregor91f84212008-12-11 16:49:14 +00002167
Chris Lattneree1284a2008-03-16 00:16:02 +00002168public:
Jay Foad39c79802011-01-12 09:06:06 +00002169 static FieldDecl *Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002170 SourceLocation StartLoc, SourceLocation IdLoc,
2171 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002172 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smith2b013182012-06-10 03:12:00 +00002173 InClassInitStyle InitStyle);
Steve Naroff46ba1eb2007-04-03 23:13:13 +00002174
Douglas Gregor72172e92012-01-05 21:55:30 +00002175 static FieldDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2176
John McCall4e819612011-01-20 07:57:12 +00002177 /// getFieldIndex - Returns the index of this field within its record,
2178 /// as appropriate for passing to ASTRecordLayout::getFieldOffset.
2179 unsigned getFieldIndex() const;
2180
Douglas Gregor91f84212008-12-11 16:49:14 +00002181 /// isMutable - Determines whether this field is mutable (C++ only).
2182 bool isMutable() const { return Mutable; }
2183
2184 /// isBitfield - Determines whether this field is a bitfield.
Richard Smith938f40b2011-06-11 17:19:42 +00002185 bool isBitField() const {
Richard Smith2b013182012-06-10 03:12:00 +00002186 return getInClassInitStyle() == ICIS_NoInit &&
2187 InitializerOrBitWidth.getPointer();
Richard Smith938f40b2011-06-11 17:19:42 +00002188 }
Douglas Gregor91f84212008-12-11 16:49:14 +00002189
Douglas Gregor347f7ea2009-01-28 21:54:33 +00002190 /// @brief Determines whether this is an unnamed bitfield.
Richard Smith938f40b2011-06-11 17:19:42 +00002191 bool isUnnamedBitfield() const { return isBitField() && !getDeclName(); }
Douglas Gregor347f7ea2009-01-28 21:54:33 +00002192
Douglas Gregorf4d33272009-01-07 19:46:03 +00002193 /// isAnonymousStructOrUnion - Determines whether this field is a
2194 /// representative for an anonymous struct or union. Such fields are
2195 /// unnamed and are implicitly generated by the implementation to
2196 /// store the data for the anonymous union or struct.
2197 bool isAnonymousStructOrUnion() const;
2198
Richard Smith938f40b2011-06-11 17:19:42 +00002199 Expr *getBitWidth() const {
2200 return isBitField() ? InitializerOrBitWidth.getPointer() : 0;
2201 }
Richard Smithcaf33902011-10-10 18:28:20 +00002202 unsigned getBitWidthValue(const ASTContext &Ctx) const;
Richard Smith2b013182012-06-10 03:12:00 +00002203
Abramo Bagnarab1cdde72012-07-02 20:35:48 +00002204 /// setBitWidth - Set the bit-field width for this member.
2205 // Note: used by some clients (i.e., do not remove it).
2206 void setBitWidth(Expr *Width);
2207 /// removeBitWidth - Remove the bit-field width from this member.
2208 // Note: used by some clients (i.e., do not remove it).
2209 void removeBitWidth() {
2210 assert(isBitField() && "no bitfield width to remove");
2211 InitializerOrBitWidth.setPointer(0);
2212 }
2213
Richard Smith2b013182012-06-10 03:12:00 +00002214 /// getInClassInitStyle - Get the kind of (C++11) in-class initializer which
2215 /// this field has.
2216 InClassInitStyle getInClassInitStyle() const {
2217 return static_cast<InClassInitStyle>(InitializerOrBitWidth.getInt());
Abramo Bagnara612f6dc2011-07-11 08:43:20 +00002218 }
Richard Smith938f40b2011-06-11 17:19:42 +00002219
Richard Smith2b013182012-06-10 03:12:00 +00002220 /// hasInClassInitializer - Determine whether this member has a C++11 in-class
Richard Smith938f40b2011-06-11 17:19:42 +00002221 /// initializer.
2222 bool hasInClassInitializer() const {
Richard Smith2b013182012-06-10 03:12:00 +00002223 return getInClassInitStyle() != ICIS_NoInit;
Richard Smith938f40b2011-06-11 17:19:42 +00002224 }
Richard Smith2b013182012-06-10 03:12:00 +00002225 /// getInClassInitializer - Get the C++11 in-class initializer for this
Richard Smith938f40b2011-06-11 17:19:42 +00002226 /// member, or null if one has not been set. If a valid declaration has an
2227 /// in-class initializer, but this returns null, then we have not parsed and
2228 /// attached it yet.
2229 Expr *getInClassInitializer() const {
2230 return hasInClassInitializer() ? InitializerOrBitWidth.getPointer() : 0;
2231 }
Richard Smith2b013182012-06-10 03:12:00 +00002232 /// setInClassInitializer - Set the C++11 in-class initializer for this
David Blaikie21bfbf82011-11-09 06:07:30 +00002233 /// member.
Richard Smith938f40b2011-06-11 17:19:42 +00002234 void setInClassInitializer(Expr *Init);
Richard Smith2b013182012-06-10 03:12:00 +00002235 /// removeInClassInitializer - Remove the C++11 in-class initializer from this
Richard Smith938f40b2011-06-11 17:19:42 +00002236 /// member.
2237 void removeInClassInitializer() {
Richard Smith2b013182012-06-10 03:12:00 +00002238 assert(hasInClassInitializer() && "no initializer to remove");
Richard Smith938f40b2011-06-11 17:19:42 +00002239 InitializerOrBitWidth.setPointer(0);
Richard Smith2b013182012-06-10 03:12:00 +00002240 InitializerOrBitWidth.setInt(ICIS_NoInit);
Richard Smith938f40b2011-06-11 17:19:42 +00002241 }
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002242
Anders Carlsson5d8645b2010-01-29 05:05:36 +00002243 /// getParent - Returns the parent of this field declaration, which
2244 /// is the struct in which this method is defined.
2245 const RecordDecl *getParent() const {
2246 return cast<RecordDecl>(getDeclContext());
2247 }
2248
2249 RecordDecl *getParent() {
2250 return cast<RecordDecl>(getDeclContext());
2251 }
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002252
Craig Toppercbce6e92014-03-11 06:22:39 +00002253 SourceRange getSourceRange() const override LLVM_READONLY;
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002254
Richard Smith0b87e072013-10-07 08:02:11 +00002255 /// Retrieves the canonical declaration of this field.
Craig Toppercbce6e92014-03-11 06:22:39 +00002256 FieldDecl *getCanonicalDecl() override { return getFirstDecl(); }
Rafael Espindola8db352d2013-10-17 15:37:26 +00002257 const FieldDecl *getCanonicalDecl() const { return getFirstDecl(); }
Richard Smith0b87e072013-10-07 08:02:11 +00002258
Chris Lattnerce3a7022007-01-24 02:10:37 +00002259 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00002260 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Alexis Hunted053252010-05-30 07:21:58 +00002261 static bool classofKind(Kind K) { return K >= firstField && K <= lastField; }
Richard Smith2b013182012-06-10 03:12:00 +00002262
2263 friend class ASTDeclReader;
2264 friend class ASTDeclWriter;
Chris Lattner1300fb92007-01-23 23:42:53 +00002265};
2266
Chris Lattnerc1915e22007-01-25 07:29:02 +00002267/// EnumConstantDecl - An instance of this object exists for each enum constant
2268/// that is defined. For example, in "enum X {a,b}", each of a/b are
2269/// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a
Steve Narofff1e53692007-03-23 22:27:02 +00002270/// TagType for the X EnumDecl.
Richard Smith01a73372013-10-15 22:02:41 +00002271class EnumConstantDecl : public ValueDecl, public Mergeable<EnumConstantDecl> {
Ted Kremenek85956682008-05-30 16:14:41 +00002272 Stmt *Init; // an integer constant expression
Chris Lattner23b7eb62007-06-15 23:05:46 +00002273 llvm::APSInt Val; // The value.
Chris Lattnera7b32872008-03-15 06:12:44 +00002274protected:
Chris Lattnerbec41342008-04-22 18:39:57 +00002275 EnumConstantDecl(DeclContext *DC, SourceLocation L,
Chris Lattnerc5ffed42008-04-04 06:12:32 +00002276 IdentifierInfo *Id, QualType T, Expr *E,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002277 const llvm::APSInt &V)
2278 : ValueDecl(EnumConstant, DC, L, Id, T), Init((Stmt*)E), Val(V) {}
Ted Kremenekce20e8f2008-05-20 00:43:19 +00002279
Chris Lattnera7b32872008-03-15 06:12:44 +00002280public:
Steve Naroff46ba1eb2007-04-03 23:13:13 +00002281
Chris Lattnerbec41342008-04-22 18:39:57 +00002282 static EnumConstantDecl *Create(ASTContext &C, EnumDecl *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +00002283 SourceLocation L, IdentifierInfo *Id,
2284 QualType T, Expr *E,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002285 const llvm::APSInt &V);
Douglas Gregor72172e92012-01-05 21:55:30 +00002286 static EnumConstantDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2287
Ted Kremenek85956682008-05-30 16:14:41 +00002288 const Expr *getInitExpr() const { return (const Expr*) Init; }
2289 Expr *getInitExpr() { return (Expr*) Init; }
Chris Lattner23b7eb62007-06-15 23:05:46 +00002290 const llvm::APSInt &getInitVal() const { return Val; }
Chris Lattner959bcdc2007-08-29 17:23:37 +00002291
Ted Kremenek85956682008-05-30 16:14:41 +00002292 void setInitExpr(Expr *E) { Init = (Stmt*) E; }
Chris Lattnera3343a82008-02-26 00:32:36 +00002293 void setInitVal(const llvm::APSInt &V) { Val = V; }
Mike Stump11289f42009-09-09 15:08:12 +00002294
Craig Toppercbce6e92014-03-11 06:22:39 +00002295 SourceRange getSourceRange() const override LLVM_READONLY;
David Blaikie21bfbf82011-11-09 06:07:30 +00002296
Richard Smith01a73372013-10-15 22:02:41 +00002297 /// Retrieves the canonical declaration of this enumerator.
Craig Toppercbce6e92014-03-11 06:22:39 +00002298 EnumConstantDecl *getCanonicalDecl() override { return getFirstDecl(); }
Rafael Espindola8db352d2013-10-17 15:37:26 +00002299 const EnumConstantDecl *getCanonicalDecl() const { return getFirstDecl(); }
Richard Smith01a73372013-10-15 22:02:41 +00002300
Chris Lattnerc1915e22007-01-25 07:29:02 +00002301 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00002302 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +00002303 static bool classofKind(Kind K) { return K == EnumConstant; }
Mike Stump11289f42009-09-09 15:08:12 +00002304
Ted Kremenek066f6032007-10-25 22:24:19 +00002305 friend class StmtIteratorBase;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002306};
2307
Francois Pichet783dd6e2010-11-21 06:08:52 +00002308/// IndirectFieldDecl - An instance of this class is created to represent a
2309/// field injected from an anonymous union/struct into the parent scope.
2310/// IndirectFieldDecl are always implicit.
2311class IndirectFieldDecl : public ValueDecl {
Craig Toppercbce6e92014-03-11 06:22:39 +00002312 void anchor() override;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002313 NamedDecl **Chaining;
Benjamin Kramer39593702010-11-21 14:11:41 +00002314 unsigned ChainingSize;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002315
2316 IndirectFieldDecl(DeclContext *DC, SourceLocation L,
2317 DeclarationName N, QualType T,
Benjamin Kramer39593702010-11-21 14:11:41 +00002318 NamedDecl **CH, unsigned CHS)
Francois Pichet783dd6e2010-11-21 06:08:52 +00002319 : ValueDecl(IndirectField, DC, L, N, T), Chaining(CH), ChainingSize(CHS) {}
2320
2321public:
2322 static IndirectFieldDecl *Create(ASTContext &C, DeclContext *DC,
2323 SourceLocation L, IdentifierInfo *Id,
Benjamin Kramer39593702010-11-21 14:11:41 +00002324 QualType T, NamedDecl **CH, unsigned CHS);
David Blaikie21bfbf82011-11-09 06:07:30 +00002325
Douglas Gregor72172e92012-01-05 21:55:30 +00002326 static IndirectFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2327
Francois Pichet783dd6e2010-11-21 06:08:52 +00002328 typedef NamedDecl * const *chain_iterator;
Aaron Ballman13916082014-03-07 18:11:58 +00002329 typedef llvm::iterator_range<chain_iterator> chain_range;
2330
Aaron Ballman43027eb2014-03-08 18:30:40 +00002331 chain_range chain() const { return chain_range(chain_begin(), chain_end()); }
2332 chain_iterator chain_begin() const { return chain_iterator(Chaining); }
2333 chain_iterator chain_end() const {
2334 return chain_iterator(Chaining + ChainingSize);
Aaron Ballman13916082014-03-07 18:11:58 +00002335 }
Francois Pichet783dd6e2010-11-21 06:08:52 +00002336
Benjamin Kramer39593702010-11-21 14:11:41 +00002337 unsigned getChainingSize() const { return ChainingSize; }
Francois Pichet783dd6e2010-11-21 06:08:52 +00002338
2339 FieldDecl *getAnonField() const {
2340 assert(ChainingSize >= 2);
2341 return cast<FieldDecl>(Chaining[ChainingSize - 1]);
2342 }
2343
2344 VarDecl *getVarDecl() const {
2345 assert(ChainingSize >= 2);
2346 return dyn_cast<VarDecl>(*chain_begin());
2347 }
2348
2349 // Implement isa/cast/dyncast/etc.
2350 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Francois Pichet783dd6e2010-11-21 06:08:52 +00002351 static bool classofKind(Kind K) { return K == IndirectField; }
2352 friend class ASTDeclReader;
2353};
Chris Lattnerc1915e22007-01-25 07:29:02 +00002354
Chris Lattner0659f482007-01-26 02:12:16 +00002355/// TypeDecl - Represents a declaration of a type.
2356///
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002357class TypeDecl : public NamedDecl {
Craig Toppercbce6e92014-03-11 06:22:39 +00002358 void anchor() override;
Douglas Gregoreff93e02009-02-05 23:33:38 +00002359 /// TypeForDecl - This indicates the Type object that represents
2360 /// this TypeDecl. It is a cache maintained by
2361 /// ASTContext::getTypedefType, ASTContext::getTagDeclType, and
2362 /// ASTContext::getTemplateTypeParmType, and TemplateTypeParmDecl.
John McCall424cec92011-01-19 06:33:43 +00002363 mutable const Type *TypeForDecl;
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002364 /// LocStart - The start of the source range for this declaration.
2365 SourceLocation LocStart;
Chris Lattner733067d2007-01-26 01:42:24 +00002366 friend class ASTContext;
Douglas Gregordee1be82009-01-17 00:42:38 +00002367
Chris Lattner0659f482007-01-26 02:12:16 +00002368protected:
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002369 TypeDecl(Kind DK, DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
2370 SourceLocation StartL = SourceLocation())
2371 : NamedDecl(DK, DC, L, Id), TypeForDecl(0), LocStart(StartL) {}
Argyrios Kyrtzidis2951e142008-06-09 21:05:31 +00002372
Douglas Gregorf4d33272009-01-07 19:46:03 +00002373public:
Douglas Gregor4b12ba02012-07-17 23:09:36 +00002374 // Low-level accessor. If you just want the type defined by this node,
2375 // check out ASTContext::getTypeDeclType or one of
2376 // ASTContext::getTypedefType, ASTContext::getRecordType, etc. if you
2377 // already know the specific kind of node this is.
John McCall424cec92011-01-19 06:33:43 +00002378 const Type *getTypeForDecl() const { return TypeForDecl; }
2379 void setTypeForDecl(const Type *TD) { TypeForDecl = TD; }
Douglas Gregorb1fe2c92009-04-07 17:20:56 +00002380
Daniel Dunbare4775e12012-03-09 19:35:29 +00002381 SourceLocation getLocStart() const LLVM_READONLY { return LocStart; }
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002382 void setLocStart(SourceLocation L) { LocStart = L; }
Craig Toppercbce6e92014-03-11 06:22:39 +00002383 SourceRange getSourceRange() const override LLVM_READONLY {
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002384 if (LocStart.isValid())
2385 return SourceRange(LocStart, getLocation());
2386 else
2387 return SourceRange(getLocation());
2388 }
2389
Chris Lattner0659f482007-01-26 02:12:16 +00002390 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00002391 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Alexis Hunted053252010-05-30 07:21:58 +00002392 static bool classofKind(Kind K) { return K >= firstType && K <= lastType; }
Chris Lattner0659f482007-01-26 02:12:16 +00002393};
2394
2395
Richard Smithdda56e42011-04-15 14:24:37 +00002396/// Base class for declarations which introduce a typedef-name.
2397class TypedefNameDecl : public TypeDecl, public Redeclarable<TypedefNameDecl> {
Craig Toppercbce6e92014-03-11 06:22:39 +00002398 void anchor() override;
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002399 typedef std::pair<TypeSourceInfo*, QualType> ModedTInfo;
2400 llvm::PointerUnion<TypeSourceInfo*, ModedTInfo*> MaybeModedTInfo;
John McCall703a3f82009-10-24 08:00:42 +00002401
Douglas Gregorecd99b12010-05-25 19:53:14 +00002402protected:
Richard Smithdda56e42011-04-15 14:24:37 +00002403 TypedefNameDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc,
2404 SourceLocation IdLoc, IdentifierInfo *Id,
2405 TypeSourceInfo *TInfo)
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002406 : TypeDecl(DK, DC, IdLoc, Id, StartLoc), MaybeModedTInfo(TInfo) {}
Richard Smithdda56e42011-04-15 14:24:37 +00002407
2408 typedef Redeclarable<TypedefNameDecl> redeclarable_base;
Craig Toppercbce6e92014-03-11 06:22:39 +00002409 TypedefNameDecl *getNextRedeclaration() override {
Richard Smithdda56e42011-04-15 14:24:37 +00002410 return RedeclLink.getNext();
2411 }
Craig Toppercbce6e92014-03-11 06:22:39 +00002412 TypedefNameDecl *getPreviousDeclImpl() override {
Douglas Gregorec9fd132012-01-14 16:38:05 +00002413 return getPreviousDecl();
2414 }
Craig Toppercbce6e92014-03-11 06:22:39 +00002415 TypedefNameDecl *getMostRecentDeclImpl() override {
Douglas Gregorec9fd132012-01-14 16:38:05 +00002416 return getMostRecentDecl();
2417 }
Douglas Gregorecd99b12010-05-25 19:53:14 +00002418
Chris Lattnera7b32872008-03-15 06:12:44 +00002419public:
Aaron Ballman86c93902014-03-06 23:45:36 +00002420 typedef redeclarable_base::redecl_range redecl_range;
Aaron Ballman211cd8c2014-03-07 00:10:58 +00002421 typedef redeclarable_base::redecl_iterator redecl_iterator;
2422 using redeclarable_base::redecls_begin;
2423 using redeclarable_base::redecls_end;
Aaron Ballman86c93902014-03-06 23:45:36 +00002424 using redeclarable_base::redecls;
Douglas Gregorec9fd132012-01-14 16:38:05 +00002425 using redeclarable_base::getPreviousDecl;
2426 using redeclarable_base::getMostRecentDecl;
Rafael Espindola3f9e4442013-10-19 02:13:21 +00002427 using redeclarable_base::isFirstDecl;
Douglas Gregor0bc8a212012-01-14 15:55:47 +00002428
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002429 bool isModed() const { return MaybeModedTInfo.is<ModedTInfo*>(); }
2430
John McCallbcd03502009-12-07 02:54:59 +00002431 TypeSourceInfo *getTypeSourceInfo() const {
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002432 return isModed()
2433 ? MaybeModedTInfo.get<ModedTInfo*>()->first
2434 : MaybeModedTInfo.get<TypeSourceInfo*>();
2435 }
2436 QualType getUnderlyingType() const {
2437 return isModed()
2438 ? MaybeModedTInfo.get<ModedTInfo*>()->second
2439 : MaybeModedTInfo.get<TypeSourceInfo*>()->getType();
2440 }
2441 void setTypeSourceInfo(TypeSourceInfo *newType) {
2442 MaybeModedTInfo = newType;
2443 }
2444 void setModedTypeSourceInfo(TypeSourceInfo *unmodedTSI, QualType modedTy) {
2445 MaybeModedTInfo = new (getASTContext()) ModedTInfo(unmodedTSI, modedTy);
John McCall703a3f82009-10-24 08:00:42 +00002446 }
2447
Richard Smithdda56e42011-04-15 14:24:37 +00002448 /// Retrieves the canonical declaration of this typedef-name.
Craig Toppercbce6e92014-03-11 06:22:39 +00002449 TypedefNameDecl *getCanonicalDecl() override { return getFirstDecl(); }
Rafael Espindola8db352d2013-10-17 15:37:26 +00002450 const TypedefNameDecl *getCanonicalDecl() const { return getFirstDecl(); }
John McCall91f1a022009-12-30 00:31:22 +00002451
Richard Smithdda56e42011-04-15 14:24:37 +00002452 // Implement isa/cast/dyncast/etc.
2453 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Richard Smithdda56e42011-04-15 14:24:37 +00002454 static bool classofKind(Kind K) {
2455 return K >= firstTypedefName && K <= lastTypedefName;
2456 }
2457};
2458
2459/// TypedefDecl - Represents the declaration of a typedef-name via the 'typedef'
2460/// type specifier.
2461class TypedefDecl : public TypedefNameDecl {
2462 TypedefDecl(DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc,
2463 IdentifierInfo *Id, TypeSourceInfo *TInfo)
2464 : TypedefNameDecl(Typedef, DC, StartLoc, IdLoc, Id, TInfo) {}
2465
2466public:
2467 static TypedefDecl *Create(ASTContext &C, DeclContext *DC,
2468 SourceLocation StartLoc, SourceLocation IdLoc,
2469 IdentifierInfo *Id, TypeSourceInfo *TInfo);
Douglas Gregor72172e92012-01-05 21:55:30 +00002470 static TypedefDecl *CreateDeserialized(ASTContext &C, unsigned ID);
Craig Toppercbce6e92014-03-11 06:22:39 +00002471
2472 SourceRange getSourceRange() const override LLVM_READONLY;
Abramo Bagnaraea947882011-03-08 16:41:52 +00002473
Chris Lattner0659f482007-01-26 02:12:16 +00002474 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00002475 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +00002476 static bool classofKind(Kind K) { return K == Typedef; }
Chris Lattner0659f482007-01-26 02:12:16 +00002477};
2478
Richard Smithdda56e42011-04-15 14:24:37 +00002479/// TypeAliasDecl - Represents the declaration of a typedef-name via a C++0x
2480/// alias-declaration.
2481class TypeAliasDecl : public TypedefNameDecl {
2482 TypeAliasDecl(DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc,
2483 IdentifierInfo *Id, TypeSourceInfo *TInfo)
2484 : TypedefNameDecl(TypeAlias, DC, StartLoc, IdLoc, Id, TInfo) {}
2485
2486public:
2487 static TypeAliasDecl *Create(ASTContext &C, DeclContext *DC,
2488 SourceLocation StartLoc, SourceLocation IdLoc,
2489 IdentifierInfo *Id, TypeSourceInfo *TInfo);
Douglas Gregor72172e92012-01-05 21:55:30 +00002490 static TypeAliasDecl *CreateDeserialized(ASTContext &C, unsigned ID);
Richard Smithdda56e42011-04-15 14:24:37 +00002491
Craig Toppercbce6e92014-03-11 06:22:39 +00002492 SourceRange getSourceRange() const override LLVM_READONLY;
Richard Smithdda56e42011-04-15 14:24:37 +00002493
2494 // Implement isa/cast/dyncast/etc.
2495 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Richard Smithdda56e42011-04-15 14:24:37 +00002496 static bool classofKind(Kind K) { return K == TypeAlias; }
2497};
2498
Chris Lattner0659f482007-01-26 02:12:16 +00002499/// TagDecl - Represents the declaration of a struct/union/class/enum.
Mike Stump11289f42009-09-09 15:08:12 +00002500class TagDecl
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002501 : public TypeDecl, public DeclContext, public Redeclarable<TagDecl> {
Argyrios Kyrtzidis554a07b2008-06-09 23:19:58 +00002502public:
John McCallfcc33b02009-09-05 00:15:47 +00002503 // This is really ugly.
Abramo Bagnara6150c882010-05-11 21:36:43 +00002504 typedef TagTypeKind TagKind;
Argyrios Kyrtzidis554a07b2008-06-09 23:19:58 +00002505
2506private:
Daniel Dunbar14a41c52009-03-04 02:27:50 +00002507 // FIXME: This can be packed into the bitfields in Decl.
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00002508 /// TagDeclKind - The TagKind enum.
Joao Matosdc86f942012-08-31 18:45:21 +00002509 unsigned TagDeclKind : 3;
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00002510
John McCallf937c022011-10-07 06:10:15 +00002511 /// IsCompleteDefinition - True if this is a definition ("struct foo
2512 /// {};"), false if it is a declaration ("struct foo;"). It is not
2513 /// a definition until the definition has been fully processed.
2514 bool IsCompleteDefinition : 1;
Mike Stump11289f42009-09-09 15:08:12 +00002515
Douglas Gregorc8a73492012-02-13 15:44:47 +00002516protected:
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002517 /// IsBeingDefined - True if this is currently being defined.
2518 bool IsBeingDefined : 1;
2519
Douglas Gregorc8a73492012-02-13 15:44:47 +00002520private:
Douglas Gregor5089c762010-02-12 17:40:34 +00002521 /// IsEmbeddedInDeclarator - True if this tag declaration is
2522 /// "embedded" (i.e., defined or declared for the very first time)
John McCall3e11ebe2010-03-15 10:12:16 +00002523 /// in the syntax of a declarator.
Douglas Gregor5089c762010-02-12 17:40:34 +00002524 bool IsEmbeddedInDeclarator : 1;
Douglas Gregor586d0f92010-02-08 22:07:33 +00002525
Richard Smith4b38ded2012-03-14 23:13:10 +00002526 /// \brief True if this tag is free standing, e.g. "struct foo;".
Argyrios Kyrtzidis201d3772011-09-30 22:11:31 +00002527 bool IsFreeStanding : 1;
2528
John McCall9aa35be2010-05-06 08:49:23 +00002529protected:
2530 // These are used by (and only defined for) EnumDecl.
2531 unsigned NumPositiveBits : 8;
2532 unsigned NumNegativeBits : 8;
2533
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002534 /// IsScoped - True if this tag declaration is a scoped enumeration. Only
Richard Smith4b38ded2012-03-14 23:13:10 +00002535 /// possible in C++11 mode.
Douglas Gregor0bf31402010-10-08 23:50:27 +00002536 bool IsScoped : 1;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002537 /// IsScopedUsingClassTag - If this tag declaration is a scoped enum,
2538 /// then this is true if the scoped enum was declared using the class
2539 /// tag, false if it was declared with the struct tag. No meaning is
2540 /// associated if this tag declaration is not a scoped enum.
2541 bool IsScopedUsingClassTag : 1;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002542
2543 /// IsFixed - True if this is an enumeration with fixed underlying type. Only
Adrian Prantlc60dc712013-04-19 19:56:39 +00002544 /// possible in C++11, Microsoft extensions, or Objective C mode.
Douglas Gregor0bf31402010-10-08 23:50:27 +00002545 bool IsFixed : 1;
2546
Douglas Gregor7dab26b2013-02-09 01:35:03 +00002547 /// \brief Indicates whether it is possible for declarations of this kind
2548 /// to have an out-of-date definition.
2549 ///
2550 /// This option is only enabled when modules are enabled.
2551 bool MayHaveOutOfDateDef : 1;
2552
David Blaikie48ad6dc2013-07-13 21:08:14 +00002553 /// Has the full definition of this type been required by a use somewhere in
2554 /// the TU.
2555 bool IsCompleteDefinitionRequired : 1;
John McCall9aa35be2010-05-06 08:49:23 +00002556private:
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002557 SourceLocation RBraceLoc;
2558
John McCall3e11ebe2010-03-15 10:12:16 +00002559 // A struct representing syntactic qualifier info,
2560 // to be used for the (uncommon) case of out-of-line declarations.
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00002561 typedef QualifierInfo ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002562
David Majnemer50ce8352013-09-17 23:57:10 +00002563 /// \brief If the (out-of-line) tag declaration name
John McCall3e11ebe2010-03-15 10:12:16 +00002564 /// is qualified, it points to the qualifier info (nns and range);
2565 /// otherwise, if the tag declaration is anonymous and it is part of
Richard Smithdda56e42011-04-15 14:24:37 +00002566 /// a typedef or alias, it points to the TypedefNameDecl (used for mangling);
David Majnemer50ce8352013-09-17 23:57:10 +00002567 /// otherwise, if the tag declaration is anonymous and it is used as a
2568 /// declaration specifier for variables, it points to the first VarDecl (used
2569 /// for mangling);
Richard Smithdda56e42011-04-15 14:24:37 +00002570 /// otherwise, it is a null (TypedefNameDecl) pointer.
David Majnemer50ce8352013-09-17 23:57:10 +00002571 llvm::PointerUnion<NamedDecl *, ExtInfo *> NamedDeclOrQualifier;
John McCall3e11ebe2010-03-15 10:12:16 +00002572
David Majnemer50ce8352013-09-17 23:57:10 +00002573 bool hasExtInfo() const { return NamedDeclOrQualifier.is<ExtInfo *>(); }
2574 ExtInfo *getExtInfo() { return NamedDeclOrQualifier.get<ExtInfo *>(); }
John McCall3e11ebe2010-03-15 10:12:16 +00002575 const ExtInfo *getExtInfo() const {
David Majnemer50ce8352013-09-17 23:57:10 +00002576 return NamedDeclOrQualifier.get<ExtInfo *>();
John McCall3e11ebe2010-03-15 10:12:16 +00002577 }
2578
Chris Lattner28743e22007-01-22 07:41:08 +00002579protected:
David Blaikieb05ccfe2013-07-13 22:29:32 +00002580 TagDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
2581 IdentifierInfo *Id, TagDecl *PrevDecl, SourceLocation StartL)
2582 : TypeDecl(DK, DC, L, Id, StartL), DeclContext(DK), TagDeclKind(TK),
2583 IsCompleteDefinition(false), IsBeingDefined(false),
2584 IsEmbeddedInDeclarator(false), IsFreeStanding(false),
2585 IsCompleteDefinitionRequired(false),
David Majnemer50ce8352013-09-17 23:57:10 +00002586 NamedDeclOrQualifier((NamedDecl *)0) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00002587 assert((DK != Enum || TK == TTK_Enum) &&
2588 "EnumDecl not matched with TTK_Enum");
Rafael Espindola8db352d2013-10-17 15:37:26 +00002589 setPreviousDecl(PrevDecl);
Chris Lattner7b9ace62007-01-23 20:11:08 +00002590 }
Mike Stump11289f42009-09-09 15:08:12 +00002591
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002592 typedef Redeclarable<TagDecl> redeclarable_base;
Craig Toppercbce6e92014-03-11 06:22:39 +00002593 TagDecl *getNextRedeclaration() override { return RedeclLink.getNext(); }
2594 TagDecl *getPreviousDeclImpl() override {
Douglas Gregorec9fd132012-01-14 16:38:05 +00002595 return getPreviousDecl();
2596 }
Craig Toppercbce6e92014-03-11 06:22:39 +00002597 TagDecl *getMostRecentDeclImpl() override {
Douglas Gregorec9fd132012-01-14 16:38:05 +00002598 return getMostRecentDecl();
2599 }
Mike Stump11289f42009-09-09 15:08:12 +00002600
Douglas Gregor8fb95122010-09-29 00:15:42 +00002601 /// @brief Completes the definition of this tag declaration.
2602 ///
2603 /// This is a helper function for derived classes.
David Blaikie21bfbf82011-11-09 06:07:30 +00002604 void completeDefinition();
2605
Chris Lattner28743e22007-01-22 07:41:08 +00002606public:
Aaron Ballman86c93902014-03-06 23:45:36 +00002607 typedef redeclarable_base::redecl_range redecl_range;
Aaron Ballman211cd8c2014-03-07 00:10:58 +00002608 typedef redeclarable_base::redecl_iterator redecl_iterator;
2609 using redeclarable_base::redecls_begin;
2610 using redeclarable_base::redecls_end;
Aaron Ballman86c93902014-03-06 23:45:36 +00002611 using redeclarable_base::redecls;
Douglas Gregorec9fd132012-01-14 16:38:05 +00002612 using redeclarable_base::getPreviousDecl;
2613 using redeclarable_base::getMostRecentDecl;
Rafael Espindola3f9e4442013-10-19 02:13:21 +00002614 using redeclarable_base::isFirstDecl;
Douglas Gregor0bc8a212012-01-14 15:55:47 +00002615
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002616 SourceLocation getRBraceLoc() const { return RBraceLoc; }
2617 void setRBraceLoc(SourceLocation L) { RBraceLoc = L; }
2618
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002619 /// getInnerLocStart - Return SourceLocation representing start of source
2620 /// range ignoring outer template declarations.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002621 SourceLocation getInnerLocStart() const { return getLocStart(); }
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002622
2623 /// getOuterLocStart - Return SourceLocation representing start of source
2624 /// range taking into account any outer template declarations.
2625 SourceLocation getOuterLocStart() const;
Craig Toppercbce6e92014-03-11 06:22:39 +00002626 SourceRange getSourceRange() const override LLVM_READONLY;
Mike Stump11289f42009-09-09 15:08:12 +00002627
Craig Toppercbce6e92014-03-11 06:22:39 +00002628 TagDecl* getCanonicalDecl() override;
John McCall84c16cf2009-11-12 03:15:40 +00002629 const TagDecl* getCanonicalDecl() const {
2630 return const_cast<TagDecl*>(this)->getCanonicalDecl();
2631 }
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002632
John McCallc265ad22010-08-31 22:21:26 +00002633 /// isThisDeclarationADefinition() - Return true if this declaration
John McCallf937c022011-10-07 06:10:15 +00002634 /// is a completion definintion of the type. Provided for consistency.
John McCallc265ad22010-08-31 22:21:26 +00002635 bool isThisDeclarationADefinition() const {
John McCallf937c022011-10-07 06:10:15 +00002636 return isCompleteDefinition();
John McCallc265ad22010-08-31 22:21:26 +00002637 }
2638
John McCallf937c022011-10-07 06:10:15 +00002639 /// isCompleteDefinition - Return true if this decl has its body
2640 /// fully specified.
2641 bool isCompleteDefinition() const {
2642 return IsCompleteDefinition;
Chris Lattner7b9ace62007-01-23 20:11:08 +00002643 }
Douglas Gregore362cea2009-05-10 22:57:19 +00002644
David Blaikie48ad6dc2013-07-13 21:08:14 +00002645 /// \brief Return true if this complete decl is
2646 /// required to be complete for some existing use.
2647 bool isCompleteDefinitionRequired() const {
2648 return IsCompleteDefinitionRequired;
2649 }
2650
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002651 /// isBeingDefined - Return true if this decl is currently being defined.
2652 bool isBeingDefined() const {
2653 return IsBeingDefined;
2654 }
2655
Douglas Gregor5089c762010-02-12 17:40:34 +00002656 bool isEmbeddedInDeclarator() const {
2657 return IsEmbeddedInDeclarator;
Douglas Gregor586d0f92010-02-08 22:07:33 +00002658 }
Douglas Gregor5089c762010-02-12 17:40:34 +00002659 void setEmbeddedInDeclarator(bool isInDeclarator) {
2660 IsEmbeddedInDeclarator = isInDeclarator;
Douglas Gregor586d0f92010-02-08 22:07:33 +00002661 }
2662
Argyrios Kyrtzidis201d3772011-09-30 22:11:31 +00002663 bool isFreeStanding() const { return IsFreeStanding; }
2664 void setFreeStanding(bool isFreeStanding = true) {
2665 IsFreeStanding = isFreeStanding;
2666 }
2667
Douglas Gregore362cea2009-05-10 22:57:19 +00002668 /// \brief Whether this declaration declares a type that is
2669 /// dependent, i.e., a type that somehow depends on template
2670 /// parameters.
Douglas Gregor9e927ab2009-05-28 16:34:51 +00002671 bool isDependentType() const { return isDependentContext(); }
Douglas Gregore362cea2009-05-10 22:57:19 +00002672
Douglas Gregordee1be82009-01-17 00:42:38 +00002673 /// @brief Starts the definition of this tag declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002674 ///
Douglas Gregordee1be82009-01-17 00:42:38 +00002675 /// This method should be invoked at the beginning of the definition
2676 /// of this tag declaration. It will set the tag type into a state
2677 /// where it is in the process of being defined.
2678 void startDefinition();
2679
Mike Stump11289f42009-09-09 15:08:12 +00002680 /// getDefinition - Returns the TagDecl that actually defines this
Ted Kremenek21475702008-09-05 17:16:31 +00002681 /// struct/union/class/enum. When determining whether or not a
John McCallf937c022011-10-07 06:10:15 +00002682 /// struct/union/class/enum has a definition, one should use this
2683 /// method as opposed to 'isDefinition'. 'isDefinition' indicates
2684 /// whether or not a specific TagDecl is defining declaration, not
2685 /// whether or not the struct/union/class/enum type is defined.
2686 /// This method returns NULL if there is no TagDecl that defines
2687 /// the struct/union/class/enum.
2688 TagDecl *getDefinition() const;
Mike Stump11289f42009-09-09 15:08:12 +00002689
John McCallf937c022011-10-07 06:10:15 +00002690 void setCompleteDefinition(bool V) { IsCompleteDefinition = V; }
Sebastian Redl833ef452010-01-26 22:01:41 +00002691
David Blaikiea8d23ce2013-07-14 01:07:41 +00002692 void setCompleteDefinitionRequired(bool V = true) {
2693 IsCompleteDefinitionRequired = V;
2694 }
David Blaikie48ad6dc2013-07-13 21:08:14 +00002695
Alp Tokera030cd02014-05-05 12:38:48 +00002696 StringRef getKindName() const {
Abramo Bagnara6150c882010-05-11 21:36:43 +00002697 return TypeWithKeyword::getTagTypeKindName(getTagKind());
Chris Lattnerfb0724632007-01-23 05:45:31 +00002698 }
Argyrios Kyrtzidis554a07b2008-06-09 23:19:58 +00002699
2700 TagKind getTagKind() const {
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00002701 return TagKind(TagDeclKind);
Argyrios Kyrtzidis554a07b2008-06-09 23:19:58 +00002702 }
2703
Douglas Gregor1daeb692009-04-13 18:14:40 +00002704 void setTagKind(TagKind TK) { TagDeclKind = TK; }
2705
Abramo Bagnara6150c882010-05-11 21:36:43 +00002706 bool isStruct() const { return getTagKind() == TTK_Struct; }
Joao Matosdc86f942012-08-31 18:45:21 +00002707 bool isInterface() const { return getTagKind() == TTK_Interface; }
Abramo Bagnara6150c882010-05-11 21:36:43 +00002708 bool isClass() const { return getTagKind() == TTK_Class; }
2709 bool isUnion() const { return getTagKind() == TTK_Union; }
2710 bool isEnum() const { return getTagKind() == TTK_Enum; }
Mike Stump11289f42009-09-09 15:08:12 +00002711
John McCall5ea95772013-03-09 00:54:27 +00002712 /// Is this tag type named, either directly or via being defined in
2713 /// a typedef of this type?
2714 ///
2715 /// C++11 [basic.link]p8:
2716 /// A type is said to have linkage if and only if:
2717 /// - it is a class or enumeration type that is named (or has a
2718 /// name for linkage purposes) and the name has linkage; ...
2719 /// C++11 [dcl.typedef]p9:
2720 /// If the typedef declaration defines an unnamed class (or enum),
2721 /// the first typedef-name declared by the declaration to be that
2722 /// class type (or enum type) is used to denote the class type (or
2723 /// enum type) for linkage purposes only.
2724 ///
2725 /// C does not have an analogous rule, but the same concept is
2726 /// nonetheless useful in some places.
2727 bool hasNameForLinkage() const {
2728 return (getDeclName() || getTypedefNameForAnonDecl());
2729 }
2730
David Majnemer50ce8352013-09-17 23:57:10 +00002731 bool hasDeclaratorForAnonDecl() const {
2732 return dyn_cast_or_null<DeclaratorDecl>(
2733 NamedDeclOrQualifier.get<NamedDecl *>());
David Majnemer1ebb1452013-09-17 22:21:27 +00002734 }
David Majnemer50ce8352013-09-17 23:57:10 +00002735 DeclaratorDecl *getDeclaratorForAnonDecl() const {
2736 return hasExtInfo() ? 0 : dyn_cast_or_null<DeclaratorDecl>(
2737 NamedDeclOrQualifier.get<NamedDecl *>());
2738 }
2739
2740 TypedefNameDecl *getTypedefNameForAnonDecl() const {
2741 return hasExtInfo() ? 0 : dyn_cast_or_null<TypedefNameDecl>(
2742 NamedDeclOrQualifier.get<NamedDecl *>());
2743 }
2744
2745 void setDeclaratorForAnonDecl(DeclaratorDecl *DD) { NamedDeclOrQualifier = DD; }
David Majnemer1ebb1452013-09-17 22:21:27 +00002746
Richard Smithdda56e42011-04-15 14:24:37 +00002747 void setTypedefNameForAnonDecl(TypedefNameDecl *TDD);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00002748
Douglas Gregor14454802011-02-25 02:25:35 +00002749 /// \brief Retrieve the nested-name-specifier that qualifies the name of this
2750 /// declaration, if it was present in the source.
John McCall3e11ebe2010-03-15 10:12:16 +00002751 NestedNameSpecifier *getQualifier() const {
Douglas Gregor14454802011-02-25 02:25:35 +00002752 return hasExtInfo() ? getExtInfo()->QualifierLoc.getNestedNameSpecifier()
2753 : 0;
John McCall3e11ebe2010-03-15 10:12:16 +00002754 }
David Blaikie21bfbf82011-11-09 06:07:30 +00002755
2756 /// \brief Retrieve the nested-name-specifier (with source-location
2757 /// information) that qualifies the name of this declaration, if it was
Douglas Gregor14454802011-02-25 02:25:35 +00002758 /// present in the source.
2759 NestedNameSpecifierLoc getQualifierLoc() const {
2760 return hasExtInfo() ? getExtInfo()->QualifierLoc
2761 : NestedNameSpecifierLoc();
John McCall3e11ebe2010-03-15 10:12:16 +00002762 }
David Blaikie21bfbf82011-11-09 06:07:30 +00002763
Douglas Gregor14454802011-02-25 02:25:35 +00002764 void setQualifierInfo(NestedNameSpecifierLoc QualifierLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002765
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00002766 unsigned getNumTemplateParameterLists() const {
2767 return hasExtInfo() ? getExtInfo()->NumTemplParamLists : 0;
2768 }
2769 TemplateParameterList *getTemplateParameterList(unsigned i) const {
2770 assert(i < getNumTemplateParameterLists());
2771 return getExtInfo()->TemplParamLists[i];
2772 }
Douglas Gregor20527e22010-06-15 17:44:38 +00002773 void setTemplateParameterListsInfo(ASTContext &Context, unsigned NumTPLists,
Abramo Bagnara60804e12011-03-18 15:16:37 +00002774 TemplateParameterList **TPLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00002775
Chris Lattner28743e22007-01-22 07:41:08 +00002776 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00002777 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Alexis Hunted053252010-05-30 07:21:58 +00002778 static bool classofKind(Kind K) { return K >= firstTag && K <= lastTag; }
Douglas Gregordee1be82009-01-17 00:42:38 +00002779
2780 static DeclContext *castToDeclContext(const TagDecl *D) {
2781 return static_cast<DeclContext *>(const_cast<TagDecl*>(D));
2782 }
2783 static TagDecl *castFromDeclContext(const DeclContext *DC) {
2784 return static_cast<TagDecl *>(const_cast<DeclContext*>(DC));
2785 }
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +00002786
Sebastian Redlb3298c32010-08-18 23:56:48 +00002787 friend class ASTDeclReader;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002788 friend class ASTDeclWriter;
Chris Lattner28743e22007-01-22 07:41:08 +00002789};
2790
Richard Smith4b38ded2012-03-14 23:13:10 +00002791/// EnumDecl - Represents an enum. In C++11, enums can be forward-declared
2792/// with a fixed underlying type, and in C we allow them to be forward-declared
2793/// with no underlying type as an extension.
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002794class EnumDecl : public TagDecl {
Craig Toppercbce6e92014-03-11 06:22:39 +00002795 void anchor() override;
Chris Lattner1c1f9322007-08-28 18:24:31 +00002796 /// IntegerType - This represent the integer type that the enum corresponds
2797 /// to for code generation purposes. Note that the enumerator constants may
2798 /// have a different type than this does.
Douglas Gregor0bf31402010-10-08 23:50:27 +00002799 ///
2800 /// If the underlying integer type was explicitly stated in the source
2801 /// code, this is a TypeSourceInfo* for that type. Otherwise this type
2802 /// was automatically deduced somehow, and this is a Type*.
2803 ///
2804 /// Normally if IsFixed(), this would contain a TypeSourceInfo*, but in
2805 /// some cases it won't.
2806 ///
2807 /// The underlying type of an enumeration never has any qualifiers, so
2808 /// we can get away with just storing a raw Type*, and thus save an
2809 /// extra pointer when TypeSourceInfo is needed.
2810
2811 llvm::PointerUnion<const Type*, TypeSourceInfo*> IntegerType;
Douglas Gregor7a749382009-05-27 17:20:35 +00002812
John McCall56774992009-12-09 09:09:27 +00002813 /// PromotionType - The integer type that values of this type should
2814 /// promote to. In C, enumerators are generally of an integer type
2815 /// directly, but gcc-style large enumerators (and all enumerators
2816 /// in C++) are of the enum type instead.
2817 QualType PromotionType;
2818
Richard Smith4b38ded2012-03-14 23:13:10 +00002819 /// \brief If this enumeration is an instantiation of a member enumeration
2820 /// of a class template specialization, this is the member specialization
2821 /// information.
2822 MemberSpecializationInfo *SpecializationInfo;
Douglas Gregor7a749382009-05-27 17:20:35 +00002823
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002824 EnumDecl(DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc,
2825 IdentifierInfo *Id, EnumDecl *PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002826 bool Scoped, bool ScopedUsingClassTag, bool Fixed)
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002827 : TagDecl(Enum, TTK_Enum, DC, IdLoc, Id, PrevDecl, StartLoc),
Richard Smith4b38ded2012-03-14 23:13:10 +00002828 SpecializationInfo(0) {
Abramo Bagnara50228632011-03-06 16:09:14 +00002829 assert(Scoped || !ScopedUsingClassTag);
2830 IntegerType = (const Type*)0;
2831 NumNegativeBits = 0;
2832 NumPositiveBits = 0;
2833 IsScoped = Scoped;
2834 IsScopedUsingClassTag = ScopedUsingClassTag;
2835 IsFixed = Fixed;
2836 }
Richard Smith4b38ded2012-03-14 23:13:10 +00002837
2838 void setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2839 TemplateSpecializationKind TSK);
Chris Lattnera7b32872008-03-15 06:12:44 +00002840public:
Craig Toppercbce6e92014-03-11 06:22:39 +00002841 EnumDecl *getCanonicalDecl() override {
John McCall59660882009-08-29 08:11:13 +00002842 return cast<EnumDecl>(TagDecl::getCanonicalDecl());
2843 }
John McCall84c16cf2009-11-12 03:15:40 +00002844 const EnumDecl *getCanonicalDecl() const {
Rafael Espindola7b56f6c2013-10-19 16:55:03 +00002845 return const_cast<EnumDecl*>(this)->getCanonicalDecl();
John McCall84c16cf2009-11-12 03:15:40 +00002846 }
John McCall59660882009-08-29 08:11:13 +00002847
Douglas Gregorec9fd132012-01-14 16:38:05 +00002848 EnumDecl *getPreviousDecl() {
Aaron Ballman5116a8e2013-11-06 22:39:46 +00002849 return cast_or_null<EnumDecl>(
2850 static_cast<TagDecl *>(this)->getPreviousDecl());
Douglas Gregorec9fd132012-01-14 16:38:05 +00002851 }
Rafael Espindola7b56f6c2013-10-19 16:55:03 +00002852 const EnumDecl *getPreviousDecl() const {
2853 return const_cast<EnumDecl*>(this)->getPreviousDecl();
Douglas Gregorec9fd132012-01-14 16:38:05 +00002854 }
Rafael Espindola7b56f6c2013-10-19 16:55:03 +00002855
Douglas Gregorec9fd132012-01-14 16:38:05 +00002856 EnumDecl *getMostRecentDecl() {
Aaron Ballman5116a8e2013-11-06 22:39:46 +00002857 return cast<EnumDecl>(static_cast<TagDecl *>(this)->getMostRecentDecl());
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00002858 }
Rafael Espindola7b56f6c2013-10-19 16:55:03 +00002859 const EnumDecl *getMostRecentDecl() const {
2860 return const_cast<EnumDecl*>(this)->getMostRecentDecl();
2861 }
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00002862
Richard Smith4b38ded2012-03-14 23:13:10 +00002863 EnumDecl *getDefinition() const {
2864 return cast_or_null<EnumDecl>(TagDecl::getDefinition());
2865 }
2866
Chris Lattnerbec41342008-04-22 18:39:57 +00002867 static EnumDecl *Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002868 SourceLocation StartLoc, SourceLocation IdLoc,
2869 IdentifierInfo *Id, EnumDecl *PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002870 bool IsScoped, bool IsScopedUsingClassTag,
2871 bool IsFixed);
Douglas Gregor72172e92012-01-05 21:55:30 +00002872 static EnumDecl *CreateDeserialized(ASTContext &C, unsigned ID);
Mike Stump11289f42009-09-09 15:08:12 +00002873
Douglas Gregor91f84212008-12-11 16:49:14 +00002874 /// completeDefinition - When created, the EnumDecl corresponds to a
2875 /// forward-declared enum. This method is used to mark the
2876 /// declaration as being defined; it's enumerators have already been
2877 /// added (via DeclContext::addDecl). NewType is the new underlying
2878 /// type of the enumeration type.
Douglas Gregord5058122010-02-11 01:19:42 +00002879 void completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002880 QualType PromotionType,
2881 unsigned NumPositiveBits,
2882 unsigned NumNegativeBits);
Mike Stump11289f42009-09-09 15:08:12 +00002883
Douglas Gregor91f84212008-12-11 16:49:14 +00002884 // enumerator_iterator - Iterates through the enumerators of this
2885 // enumeration.
Douglas Gregorffca3a22009-01-09 17:18:27 +00002886 typedef specific_decl_iterator<EnumConstantDecl> enumerator_iterator;
Aaron Ballman53d413f2014-03-09 12:59:45 +00002887 typedef llvm::iterator_range<specific_decl_iterator<EnumConstantDecl>>
2888 enumerator_range;
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00002889
2890 enumerator_range enumerators() const {
2891 return enumerator_range(enumerator_begin(), enumerator_end());
2892 }
Douglas Gregor91f84212008-12-11 16:49:14 +00002893
Mike Stump11289f42009-09-09 15:08:12 +00002894 enumerator_iterator enumerator_begin() const {
Richard Smith4b38ded2012-03-14 23:13:10 +00002895 const EnumDecl *E = getDefinition();
Douglas Gregora46d6612010-06-22 14:45:56 +00002896 if (!E)
2897 E = this;
2898 return enumerator_iterator(E->decls_begin());
Douglas Gregor91f84212008-12-11 16:49:14 +00002899 }
2900
Mike Stump11289f42009-09-09 15:08:12 +00002901 enumerator_iterator enumerator_end() const {
Richard Smith4b38ded2012-03-14 23:13:10 +00002902 const EnumDecl *E = getDefinition();
Douglas Gregora46d6612010-06-22 14:45:56 +00002903 if (!E)
2904 E = this;
2905 return enumerator_iterator(E->decls_end());
Douglas Gregor91f84212008-12-11 16:49:14 +00002906 }
2907
John McCall56774992009-12-09 09:09:27 +00002908 /// getPromotionType - Return the integer type that enumerators
2909 /// should promote to.
2910 QualType getPromotionType() const { return PromotionType; }
2911
2912 /// \brief Set the promotion type.
2913 void setPromotionType(QualType T) { PromotionType = T; }
2914
Chris Lattner1c1f9322007-08-28 18:24:31 +00002915 /// getIntegerType - Return the integer type this enum decl corresponds to.
Richard Smith8bcc0862014-01-08 01:16:19 +00002916 /// This returns a null QualType for an enum forward definition with no fixed
2917 /// underlying type.
Douglas Gregor0bf31402010-10-08 23:50:27 +00002918 QualType getIntegerType() const {
2919 if (!IntegerType)
2920 return QualType();
Richard Smith8bcc0862014-01-08 01:16:19 +00002921 if (const Type *T = IntegerType.dyn_cast<const Type*>())
Douglas Gregor0bf31402010-10-08 23:50:27 +00002922 return QualType(T, 0);
Richard Smith8bcc0862014-01-08 01:16:19 +00002923 return IntegerType.get<TypeSourceInfo*>()->getType().getUnqualifiedType();
Douglas Gregor0bf31402010-10-08 23:50:27 +00002924 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00002925
2926 /// \brief Set the underlying integer type.
Douglas Gregor0cdc8322010-12-10 17:03:06 +00002927 void setIntegerType(QualType T) { IntegerType = T.getTypePtrOrNull(); }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002928
2929 /// \brief Set the underlying integer type source info.
Richard Smith8bcc0862014-01-08 01:16:19 +00002930 void setIntegerTypeSourceInfo(TypeSourceInfo *TInfo) { IntegerType = TInfo; }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002931
2932 /// \brief Return the type source info for the underlying integer type,
2933 /// if no type source info exists, return 0.
Richard Smith8bcc0862014-01-08 01:16:19 +00002934 TypeSourceInfo *getIntegerTypeSourceInfo() const {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002935 return IntegerType.dyn_cast<TypeSourceInfo*>();
2936 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00002937
Alp Tokerb9fa5122014-01-06 11:31:18 +00002938 /// \brief Retrieve the source range that covers the underlying type if
2939 /// specified.
2940 SourceRange getIntegerTypeRange() const LLVM_READONLY;
2941
Chris Lattner57540c52011-04-15 05:22:18 +00002942 /// \brief Returns the width in bits required to store all the
John McCall9aa35be2010-05-06 08:49:23 +00002943 /// non-negative enumerators of this enum.
2944 unsigned getNumPositiveBits() const {
2945 return NumPositiveBits;
2946 }
2947 void setNumPositiveBits(unsigned Num) {
2948 NumPositiveBits = Num;
2949 assert(NumPositiveBits == Num && "can't store this bitcount");
2950 }
2951
Chris Lattner57540c52011-04-15 05:22:18 +00002952 /// \brief Returns the width in bits required to store all the
John McCall9aa35be2010-05-06 08:49:23 +00002953 /// negative enumerators of this enum. These widths include
2954 /// the rightmost leading 1; that is:
David Blaikie21bfbf82011-11-09 06:07:30 +00002955 ///
John McCall9aa35be2010-05-06 08:49:23 +00002956 /// MOST NEGATIVE ENUMERATOR PATTERN NUM NEGATIVE BITS
2957 /// ------------------------ ------- -----------------
2958 /// -1 1111111 1
2959 /// -10 1110110 5
2960 /// -101 1001011 8
2961 unsigned getNumNegativeBits() const {
2962 return NumNegativeBits;
2963 }
2964 void setNumNegativeBits(unsigned Num) {
2965 NumNegativeBits = Num;
2966 }
2967
Adrian Prantl6847fe32013-04-19 19:56:35 +00002968 /// \brief Returns true if this is a C++11 scoped enumeration.
Douglas Gregor0bf31402010-10-08 23:50:27 +00002969 bool isScoped() const {
2970 return IsScoped;
2971 }
2972
Adrian Prantl6847fe32013-04-19 19:56:35 +00002973 /// \brief Returns true if this is a C++11 scoped enumeration.
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002974 bool isScopedUsingClassTag() const {
2975 return IsScopedUsingClassTag;
2976 }
2977
Adrian Prantlc60dc712013-04-19 19:56:39 +00002978 /// \brief Returns true if this is an Objective-C, C++11, or
2979 /// Microsoft-style enumeration with a fixed underlying type.
Douglas Gregor0bf31402010-10-08 23:50:27 +00002980 bool isFixed() const {
2981 return IsFixed;
2982 }
2983
2984 /// \brief Returns true if this can be considered a complete type.
2985 bool isComplete() const {
John McCallf937c022011-10-07 06:10:15 +00002986 return isCompleteDefinition() || isFixed();
Douglas Gregor0bf31402010-10-08 23:50:27 +00002987 }
2988
Douglas Gregor7a749382009-05-27 17:20:35 +00002989 /// \brief Returns the enumeration (declared within the template)
2990 /// from which this enumeration type was instantiated, or NULL if
2991 /// this enumeration was not instantiated from any template.
Richard Smith4b38ded2012-03-14 23:13:10 +00002992 EnumDecl *getInstantiatedFromMemberEnum() const;
2993
Richard Smith7d137e32012-03-23 03:33:32 +00002994 /// \brief If this enumeration is a member of a specialization of a
2995 /// templated class, determine what kind of template specialization
2996 /// or instantiation this is.
2997 TemplateSpecializationKind getTemplateSpecializationKind() const;
2998
2999 /// \brief For an enumeration member that was instantiated from a member
3000 /// enumeration of a templated class, set the template specialiation kind.
3001 void setTemplateSpecializationKind(TemplateSpecializationKind TSK,
3002 SourceLocation PointOfInstantiation = SourceLocation());
3003
Richard Smith4b38ded2012-03-14 23:13:10 +00003004 /// \brief If this enumeration is an instantiation of a member enumeration of
3005 /// a class template specialization, retrieves the member specialization
3006 /// information.
3007 MemberSpecializationInfo *getMemberSpecializationInfo() const {
3008 return SpecializationInfo;
Douglas Gregor7a749382009-05-27 17:20:35 +00003009 }
3010
Richard Smith4b38ded2012-03-14 23:13:10 +00003011 /// \brief Specify that this enumeration is an instantiation of the
3012 /// member enumeration ED.
3013 void setInstantiationOfMemberEnum(EnumDecl *ED,
3014 TemplateSpecializationKind TSK) {
3015 setInstantiationOfMemberEnum(getASTContext(), ED, TSK);
3016 }
Douglas Gregor7a749382009-05-27 17:20:35 +00003017
John McCall180ef092010-01-29 01:45:37 +00003018 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +00003019 static bool classofKind(Kind K) { return K == Enum; }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003020
3021 friend class ASTDeclReader;
Chris Lattner5f521502007-01-25 06:27:24 +00003022};
3023
3024
Chris Lattner90c26ba2007-09-30 08:13:22 +00003025/// RecordDecl - Represents a struct/union/class. For example:
3026/// struct X; // Forward declaration, no "body".
3027/// union Y { int A, B; }; // Has body with members A and B (FieldDecls).
Steve Naroffb5fc2552008-02-11 21:52:37 +00003028/// This decl will be marked invalid if *any* members are invalid.
Chris Lattner90c26ba2007-09-30 08:13:22 +00003029///
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003030class RecordDecl : public TagDecl {
Daniel Dunbar14a41c52009-03-04 02:27:50 +00003031 // FIXME: This can be packed into the bitfields in Decl.
Chris Lattner720a0542007-01-25 00:44:24 +00003032 /// HasFlexibleArrayMember - This is true if this struct ends with a flexible
3033 /// array member (e.g. int X[]) or if this union contains a struct that does.
3034 /// If so, this cannot be contained in arrays or other structs as a member.
Chris Lattner41943152007-01-25 04:52:46 +00003035 bool HasFlexibleArrayMember : 1;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003036
Daniel Dunbar76dc2e62010-03-30 22:26:15 +00003037 /// AnonymousStructOrUnion - Whether this is the type of an anonymous struct
3038 /// or union.
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003039 bool AnonymousStructOrUnion : 1;
Mike Stump11289f42009-09-09 15:08:12 +00003040
Daniel Dunbar76dc2e62010-03-30 22:26:15 +00003041 /// HasObjectMember - This is true if this struct has at least one member
Eli Friedman433aae62012-02-06 23:34:35 +00003042 /// containing an Objective-C object pointer type.
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003043 bool HasObjectMember : 1;
Fariborz Jahanian78652202013-01-25 23:57:05 +00003044
3045 /// HasVolatileMember - This is true if struct has at least one member of
3046 /// 'volatile' type.
3047 bool HasVolatileMember : 1;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003048
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003049 /// \brief Whether the field declarations of this record have been loaded
3050 /// from external storage. To avoid unnecessary deserialization of
3051 /// methods/nested types we allow deserialization of just the fields
3052 /// when needed.
3053 mutable bool LoadedFieldsFromExternalStorage : 1;
Argyrios Kyrtzidisa665dc02010-10-14 21:35:29 +00003054 friend class DeclContext;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003055
Argyrios Kyrtzidis2951e142008-06-09 21:05:31 +00003056protected:
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00003057 RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003058 SourceLocation StartLoc, SourceLocation IdLoc,
3059 IdentifierInfo *Id, RecordDecl *PrevDecl);
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00003060
Chris Lattnera7b32872008-03-15 06:12:44 +00003061public:
Jay Foad39c79802011-01-12 09:06:06 +00003062 static RecordDecl *Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003063 SourceLocation StartLoc, SourceLocation IdLoc,
3064 IdentifierInfo *Id, RecordDecl* PrevDecl = 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00003065 static RecordDecl *CreateDeserialized(const ASTContext &C, unsigned ID);
Argyrios Kyrtzidis69bc5ba2008-08-08 22:25:06 +00003066
Douglas Gregorec9fd132012-01-14 16:38:05 +00003067 RecordDecl *getPreviousDecl() {
Aaron Ballman5116a8e2013-11-06 22:39:46 +00003068 return cast_or_null<RecordDecl>(
3069 static_cast<TagDecl *>(this)->getPreviousDecl());
Douglas Gregorec9fd132012-01-14 16:38:05 +00003070 }
Rafael Espindola7b56f6c2013-10-19 16:55:03 +00003071 const RecordDecl *getPreviousDecl() const {
3072 return const_cast<RecordDecl*>(this)->getPreviousDecl();
Douglas Gregorec9fd132012-01-14 16:38:05 +00003073 }
Rafael Espindola7b56f6c2013-10-19 16:55:03 +00003074
Douglas Gregorec9fd132012-01-14 16:38:05 +00003075 RecordDecl *getMostRecentDecl() {
Aaron Ballman5116a8e2013-11-06 22:39:46 +00003076 return cast<RecordDecl>(static_cast<TagDecl *>(this)->getMostRecentDecl());
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00003077 }
Rafael Espindola7b56f6c2013-10-19 16:55:03 +00003078 const RecordDecl *getMostRecentDecl() const {
3079 return const_cast<RecordDecl*>(this)->getMostRecentDecl();
3080 }
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00003081
Chris Lattner720a0542007-01-25 00:44:24 +00003082 bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember; }
3083 void setHasFlexibleArrayMember(bool V) { HasFlexibleArrayMember = V; }
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003084
3085 /// isAnonymousStructOrUnion - Whether this is an anonymous struct
3086 /// or union. To be an anonymous struct or union, it must have been
3087 /// declared without a name and there must be no objects of this
3088 /// type declared, e.g.,
3089 /// @code
3090 /// union { int i; float f; };
Mike Stump11289f42009-09-09 15:08:12 +00003091 /// @endcode
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003092 /// is an anonymous union but neither of the following are:
3093 /// @code
3094 /// union X { int i; float f; };
3095 /// union { int i; float f; } obj;
3096 /// @endcode
3097 bool isAnonymousStructOrUnion() const { return AnonymousStructOrUnion; }
3098 void setAnonymousStructOrUnion(bool Anon) {
3099 AnonymousStructOrUnion = Anon;
3100 }
3101
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003102 bool hasObjectMember() const { return HasObjectMember; }
3103 void setHasObjectMember (bool val) { HasObjectMember = val; }
Mike Stump11289f42009-09-09 15:08:12 +00003104
Fariborz Jahanian78652202013-01-25 23:57:05 +00003105 bool hasVolatileMember() const { return HasVolatileMember; }
3106 void setHasVolatileMember (bool val) { HasVolatileMember = val; }
3107
Douglas Gregordfcad112009-03-25 15:59:44 +00003108 /// \brief Determines whether this declaration represents the
3109 /// injected class name.
3110 ///
3111 /// The injected class name in C++ is the name of the class that
3112 /// appears inside the class itself. For example:
3113 ///
3114 /// \code
3115 /// struct C {
3116 /// // C is implicitly declared here as a synonym for the class name.
3117 /// };
3118 ///
3119 /// C::C c; // same as "C c;"
3120 /// \endcode
3121 bool isInjectedClassName() const;
3122
John McCallf937c022011-10-07 06:10:15 +00003123 /// getDefinition - Returns the RecordDecl that actually defines
3124 /// this struct/union/class. When determining whether or not a
3125 /// struct/union/class is completely defined, one should use this
3126 /// method as opposed to 'isCompleteDefinition'.
3127 /// 'isCompleteDefinition' indicates whether or not a specific
3128 /// RecordDecl is a completed definition, not whether or not the
3129 /// record type is defined. This method returns NULL if there is
3130 /// no RecordDecl that defines the struct/union/tag.
3131 RecordDecl *getDefinition() const {
Douglas Gregor0a5a2212010-02-11 01:04:33 +00003132 return cast_or_null<RecordDecl>(TagDecl::getDefinition());
Ted Kremenek21475702008-09-05 17:16:31 +00003133 }
Mike Stump11289f42009-09-09 15:08:12 +00003134
Douglas Gregore0295612008-12-11 17:59:21 +00003135 // Iterator access to field members. The field iterator only visits
3136 // the non-static data members of this class, ignoring any static
3137 // data members, functions, constructors, destructors, etc.
Douglas Gregorffca3a22009-01-09 17:18:27 +00003138 typedef specific_decl_iterator<FieldDecl> field_iterator;
Aaron Ballman53d413f2014-03-09 12:59:45 +00003139 typedef llvm::iterator_range<specific_decl_iterator<FieldDecl>> field_range;
Argyrios Kyrtzidiscd5f3bd2008-08-08 13:54:06 +00003140
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00003141 field_range fields() const { return field_range(field_begin(), field_end()); }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003142 field_iterator field_begin() const;
3143
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003144 field_iterator field_end() const {
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003145 return field_iterator(decl_iterator());
Argyrios Kyrtzidiscd5f3bd2008-08-08 13:54:06 +00003146 }
3147
Douglas Gregor7a4fad12008-12-11 20:41:00 +00003148 // field_empty - Whether there are any fields (non-static data
3149 // members) in this record.
Mike Stump11289f42009-09-09 15:08:12 +00003150 bool field_empty() const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003151 return field_begin() == field_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00003152 }
Douglas Gregor7a4fad12008-12-11 20:41:00 +00003153
Douglas Gregorb11aad82011-02-19 18:51:44 +00003154 /// completeDefinition - Notes that the definition of this type is
3155 /// now complete.
3156 virtual void completeDefinition();
Steve Naroffcc321422007-03-26 23:09:51 +00003157
John McCall180ef092010-01-29 01:45:37 +00003158 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +00003159 static bool classofKind(Kind K) {
Alexis Hunted053252010-05-30 07:21:58 +00003160 return K >= firstRecord && K <= lastRecord;
John McCall180ef092010-01-29 01:45:37 +00003161 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003162
Eli Friedman9ee2d0472012-10-12 23:29:20 +00003163 /// isMsStrust - Get whether or not this is an ms_struct which can
3164 /// be turned on with an attribute, pragma, or -mms-bitfields
3165 /// commandline option.
3166 bool isMsStruct(const ASTContext &C) const;
3167
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003168private:
3169 /// \brief Deserialize just the fields.
3170 void LoadFieldsFromExternalStorage() const;
Chris Lattner28743e22007-01-22 07:41:08 +00003171};
3172
Anders Carlsson5c6c0592008-02-08 00:33:21 +00003173class FileScopeAsmDecl : public Decl {
David Blaikie68e081d2011-12-20 02:48:34 +00003174 virtual void anchor();
Anders Carlsson5c6c0592008-02-08 00:33:21 +00003175 StringLiteral *AsmString;
Abramo Bagnara348823a2011-03-03 14:20:18 +00003176 SourceLocation RParenLoc;
3177 FileScopeAsmDecl(DeclContext *DC, StringLiteral *asmstring,
3178 SourceLocation StartL, SourceLocation EndL)
3179 : Decl(FileScopeAsm, DC, StartL), AsmString(asmstring), RParenLoc(EndL) {}
Chris Lattneree1284a2008-03-16 00:16:02 +00003180public:
Douglas Gregor6e6ad602009-01-20 01:17:11 +00003181 static FileScopeAsmDecl *Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00003182 StringLiteral *Str, SourceLocation AsmLoc,
3183 SourceLocation RParenLoc);
3184
Douglas Gregor72172e92012-01-05 21:55:30 +00003185 static FileScopeAsmDecl *CreateDeserialized(ASTContext &C, unsigned ID);
3186
Abramo Bagnara348823a2011-03-03 14:20:18 +00003187 SourceLocation getAsmLoc() const { return getLocation(); }
3188 SourceLocation getRParenLoc() const { return RParenLoc; }
3189 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
Craig Toppercbce6e92014-03-11 06:22:39 +00003190 SourceRange getSourceRange() const override LLVM_READONLY {
Abramo Bagnara348823a2011-03-03 14:20:18 +00003191 return SourceRange(getAsmLoc(), getRParenLoc());
3192 }
Anders Carlsson5c6c0592008-02-08 00:33:21 +00003193
3194 const StringLiteral *getAsmString() const { return AsmString; }
3195 StringLiteral *getAsmString() { return AsmString; }
Douglas Gregora5414852009-04-13 22:49:25 +00003196 void setAsmString(StringLiteral *Asm) { AsmString = Asm; }
3197
John McCall180ef092010-01-29 01:45:37 +00003198 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +00003199 static bool classofKind(Kind K) { return K == FileScopeAsm; }
Anders Carlsson5c6c0592008-02-08 00:33:21 +00003200};
Chris Lattner38376f12008-01-12 07:05:38 +00003201
Steve Naroff415d3d52008-10-08 17:01:13 +00003202/// BlockDecl - This represents a block literal declaration, which is like an
3203/// unnamed FunctionDecl. For example:
3204/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body }
3205///
3206class BlockDecl : public Decl, public DeclContext {
John McCall351762c2011-02-07 10:33:21 +00003207public:
3208 /// A class which contains all the information about a particular
3209 /// captured value.
3210 class Capture {
3211 enum {
3212 flag_isByRef = 0x1,
3213 flag_isNested = 0x2
3214 };
3215
3216 /// The variable being captured.
3217 llvm::PointerIntPair<VarDecl*, 2> VariableAndFlags;
3218
3219 /// The copy expression, expressed in terms of a DeclRef (or
3220 /// BlockDeclRef) to the captured variable. Only required if the
3221 /// variable has a C++ class type.
3222 Expr *CopyExpr;
3223
3224 public:
3225 Capture(VarDecl *variable, bool byRef, bool nested, Expr *copy)
3226 : VariableAndFlags(variable,
3227 (byRef ? flag_isByRef : 0) | (nested ? flag_isNested : 0)),
3228 CopyExpr(copy) {}
3229
3230 /// The variable being captured.
3231 VarDecl *getVariable() const { return VariableAndFlags.getPointer(); }
3232
3233 /// Whether this is a "by ref" capture, i.e. a capture of a __block
3234 /// variable.
3235 bool isByRef() const { return VariableAndFlags.getInt() & flag_isByRef; }
3236
3237 /// Whether this is a nested capture, i.e. the variable captured
3238 /// is not from outside the immediately enclosing function/block.
3239 bool isNested() const { return VariableAndFlags.getInt() & flag_isNested; }
3240
3241 bool hasCopyExpr() const { return CopyExpr != 0; }
3242 Expr *getCopyExpr() const { return CopyExpr; }
3243 void setCopyExpr(Expr *e) { CopyExpr = e; }
3244 };
3245
3246private:
Fariborz Jahanian960910a2009-05-19 17:08:59 +00003247 // FIXME: This can be packed into the bitfields in Decl.
Ted Kremenek8af4f402010-04-29 16:48:58 +00003248 bool IsVariadic : 1;
John McCallc63de662011-02-02 13:00:07 +00003249 bool CapturesCXXThis : 1;
Fariborz Jahaniandd5eb9d2011-12-03 17:47:53 +00003250 bool BlockMissingReturnType : 1;
Eli Friedman98b01ed2012-03-01 04:01:32 +00003251 bool IsConversionFromLambda : 1;
Steve Naroffc4b30e52009-03-13 16:56:44 +00003252 /// ParamInfo - new[]'d array of pointers to ParmVarDecls for the formal
3253 /// parameters of this function. This is null if a prototype or if there are
3254 /// no formals.
3255 ParmVarDecl **ParamInfo;
3256 unsigned NumParams;
Mike Stump11289f42009-09-09 15:08:12 +00003257
Steve Naroff415d3d52008-10-08 17:01:13 +00003258 Stmt *Body;
John McCalla3ccba02010-06-04 11:21:44 +00003259 TypeSourceInfo *SignatureAsWritten;
Mike Stump11289f42009-09-09 15:08:12 +00003260
John McCall351762c2011-02-07 10:33:21 +00003261 Capture *Captures;
3262 unsigned NumCaptures;
John McCallc63de662011-02-02 13:00:07 +00003263
Eli Friedman7e346a82013-07-01 20:22:57 +00003264 unsigned ManglingNumber;
3265 Decl *ManglingContextDecl;
3266
Steve Naroff415d3d52008-10-08 17:01:13 +00003267protected:
Steve Naroff1d95e5a2008-10-10 01:28:17 +00003268 BlockDecl(DeclContext *DC, SourceLocation CaretLoc)
Mike Stump11289f42009-09-09 15:08:12 +00003269 : Decl(Block, DC, CaretLoc), DeclContext(Block),
John McCallc63de662011-02-02 13:00:07 +00003270 IsVariadic(false), CapturesCXXThis(false),
Eli Friedman98b01ed2012-03-01 04:01:32 +00003271 BlockMissingReturnType(true), IsConversionFromLambda(false),
John McCallc63de662011-02-02 13:00:07 +00003272 ParamInfo(0), NumParams(0), Body(0),
Eli Friedman7e346a82013-07-01 20:22:57 +00003273 SignatureAsWritten(0), Captures(0), NumCaptures(0),
3274 ManglingNumber(0), ManglingContextDecl(0) {}
Ted Kremenek1f1e7562007-10-25 21:37:16 +00003275
Steve Naroff415d3d52008-10-08 17:01:13 +00003276public:
Douglas Gregor72172e92012-01-05 21:55:30 +00003277 static BlockDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L);
3278 static BlockDecl *CreateDeserialized(ASTContext &C, unsigned ID);
3279
Steve Naroff415d3d52008-10-08 17:01:13 +00003280 SourceLocation getCaretLocation() const { return getLocation(); }
3281
Ted Kremenek8af4f402010-04-29 16:48:58 +00003282 bool isVariadic() const { return IsVariadic; }
3283 void setIsVariadic(bool value) { IsVariadic = value; }
Mike Stump11289f42009-09-09 15:08:12 +00003284
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00003285 CompoundStmt *getCompoundBody() const { return (CompoundStmt*) Body; }
Craig Toppercbce6e92014-03-11 06:22:39 +00003286 Stmt *getBody() const override { return (Stmt*) Body; }
Ted Kremenek73980592009-03-12 18:33:24 +00003287 void setBody(CompoundStmt *B) { Body = (Stmt*) B; }
Steve Naroff415d3d52008-10-08 17:01:13 +00003288
John McCalla3ccba02010-06-04 11:21:44 +00003289 void setSignatureAsWritten(TypeSourceInfo *Sig) { SignatureAsWritten = Sig; }
3290 TypeSourceInfo *getSignatureAsWritten() const { return SignatureAsWritten; }
3291
Steve Naroffc4b30e52009-03-13 16:56:44 +00003292 // Iterator access to formal parameters.
3293 unsigned param_size() const { return getNumParams(); }
3294 typedef ParmVarDecl **param_iterator;
3295 typedef ParmVarDecl * const *param_const_iterator;
Aaron Ballmanb2b8b1d2014-03-07 16:09:59 +00003296 typedef llvm::iterator_range<param_iterator> param_range;
3297 typedef llvm::iterator_range<param_const_iterator> param_const_range;
Mike Stump11289f42009-09-09 15:08:12 +00003298
Ted Kremenek81541c52014-01-17 07:15:26 +00003299 // ArrayRef access to formal parameters.
3300 // FIXME: Should eventual replace iterator access.
3301 ArrayRef<ParmVarDecl*> parameters() const {
3302 return llvm::makeArrayRef(ParamInfo, param_size());
3303 }
3304
Steve Naroffc4b30e52009-03-13 16:56:44 +00003305 bool param_empty() const { return NumParams == 0; }
Aaron Ballman43027eb2014-03-08 18:30:40 +00003306 param_range params() { return param_range(param_begin(), param_end()); }
3307 param_iterator param_begin() { return param_iterator(ParamInfo); }
3308 param_iterator param_end() {
3309 return param_iterator(ParamInfo + param_size());
Aaron Ballmanb2b8b1d2014-03-07 16:09:59 +00003310 }
Mike Stump11289f42009-09-09 15:08:12 +00003311
Aaron Ballmanb2b8b1d2014-03-07 16:09:59 +00003312 param_const_range params() const {
Aaron Ballman43027eb2014-03-08 18:30:40 +00003313 return param_const_range(param_begin(), param_end());
Aaron Ballmanb2b8b1d2014-03-07 16:09:59 +00003314 }
Aaron Ballman43027eb2014-03-08 18:30:40 +00003315 param_const_iterator param_begin() const {
3316 return param_const_iterator(ParamInfo);
3317 }
3318 param_const_iterator param_end() const {
3319 return param_const_iterator(ParamInfo + param_size());
3320 }
Mike Stump11289f42009-09-09 15:08:12 +00003321
John McCallc63de662011-02-02 13:00:07 +00003322 unsigned getNumParams() const { return NumParams; }
Steve Naroffc4b30e52009-03-13 16:56:44 +00003323 const ParmVarDecl *getParamDecl(unsigned i) const {
3324 assert(i < getNumParams() && "Illegal param #");
3325 return ParamInfo[i];
3326 }
3327 ParmVarDecl *getParamDecl(unsigned i) {
3328 assert(i < getNumParams() && "Illegal param #");
3329 return ParamInfo[i];
3330 }
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003331 void setParams(ArrayRef<ParmVarDecl *> NewParamInfo);
Mike Stump11289f42009-09-09 15:08:12 +00003332
John McCallc63de662011-02-02 13:00:07 +00003333 /// hasCaptures - True if this block (or its nested blocks) captures
3334 /// anything of local storage from its enclosing scopes.
John McCall351762c2011-02-07 10:33:21 +00003335 bool hasCaptures() const { return NumCaptures != 0 || CapturesCXXThis; }
John McCallc63de662011-02-02 13:00:07 +00003336
John McCall351762c2011-02-07 10:33:21 +00003337 /// getNumCaptures - Returns the number of captured variables.
3338 /// Does not include an entry for 'this'.
3339 unsigned getNumCaptures() const { return NumCaptures; }
John McCallc63de662011-02-02 13:00:07 +00003340
John McCall351762c2011-02-07 10:33:21 +00003341 typedef const Capture *capture_iterator;
3342 typedef const Capture *capture_const_iterator;
Aaron Ballman9371dd22014-03-14 18:34:04 +00003343 typedef llvm::iterator_range<capture_iterator> capture_range;
3344 typedef llvm::iterator_range<capture_const_iterator> capture_const_range;
3345
3346 capture_range captures() {
3347 return capture_range(capture_begin(), capture_end());
3348 }
3349 capture_const_range captures() const {
3350 return capture_const_range(capture_begin(), capture_end());
3351 }
3352
John McCall351762c2011-02-07 10:33:21 +00003353 capture_iterator capture_begin() { return Captures; }
3354 capture_iterator capture_end() { return Captures + NumCaptures; }
3355 capture_const_iterator capture_begin() const { return Captures; }
3356 capture_const_iterator capture_end() const { return Captures + NumCaptures; }
John McCallc63de662011-02-02 13:00:07 +00003357
3358 bool capturesCXXThis() const { return CapturesCXXThis; }
Fariborz Jahaniandd5eb9d2011-12-03 17:47:53 +00003359 bool blockMissingReturnType() const { return BlockMissingReturnType; }
3360 void setBlockMissingReturnType(bool val) { BlockMissingReturnType = val; }
John McCallc63de662011-02-02 13:00:07 +00003361
Eli Friedman98b01ed2012-03-01 04:01:32 +00003362 bool isConversionFromLambda() const { return IsConversionFromLambda; }
3363 void setIsConversionFromLambda(bool val) { IsConversionFromLambda = val; }
3364
John McCallce45f882011-06-15 22:51:16 +00003365 bool capturesVariable(const VarDecl *var) const;
3366
John McCall351762c2011-02-07 10:33:21 +00003367 void setCaptures(ASTContext &Context,
3368 const Capture *begin,
3369 const Capture *end,
3370 bool capturesCXXThis);
John McCallc63de662011-02-02 13:00:07 +00003371
Eli Friedman7e346a82013-07-01 20:22:57 +00003372 unsigned getBlockManglingNumber() const {
3373 return ManglingNumber;
3374 }
3375 Decl *getBlockManglingContextDecl() const {
3376 return ManglingContextDecl;
3377 }
3378
3379 void setBlockMangling(unsigned Number, Decl *Ctx) {
3380 ManglingNumber = Number;
3381 ManglingContextDecl = Ctx;
3382 }
3383
Craig Toppercbce6e92014-03-11 06:22:39 +00003384 SourceRange getSourceRange() const override LLVM_READONLY;
David Blaikie21bfbf82011-11-09 06:07:30 +00003385
Steve Naroff415d3d52008-10-08 17:01:13 +00003386 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00003387 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +00003388 static bool classofKind(Kind K) { return K == Block; }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +00003389 static DeclContext *castToDeclContext(const BlockDecl *D) {
3390 return static_cast<DeclContext *>(const_cast<BlockDecl*>(D));
3391 }
3392 static BlockDecl *castFromDeclContext(const DeclContext *DC) {
3393 return static_cast<BlockDecl *>(const_cast<DeclContext*>(DC));
3394 }
Steve Naroff415d3d52008-10-08 17:01:13 +00003395};
3396
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003397/// \brief This represents the body of a CapturedStmt, and serves as its
3398/// DeclContext.
3399class CapturedDecl : public Decl, public DeclContext {
3400private:
Ben Langmuir37943a72013-05-03 19:00:33 +00003401 /// \brief The number of parameters to the outlined function.
3402 unsigned NumParams;
3403 /// \brief The body of the outlined function.
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003404 Stmt *Body;
3405
Ben Langmuir37943a72013-05-03 19:00:33 +00003406 explicit CapturedDecl(DeclContext *DC, unsigned NumParams)
3407 : Decl(Captured, DC, SourceLocation()), DeclContext(Captured),
3408 NumParams(NumParams), Body(0) { }
3409
3410 ImplicitParamDecl **getParams() const {
3411 return reinterpret_cast<ImplicitParamDecl **>(
3412 const_cast<CapturedDecl *>(this) + 1);
3413 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003414
3415public:
Ben Langmuir37943a72013-05-03 19:00:33 +00003416 static CapturedDecl *Create(ASTContext &C, DeclContext *DC, unsigned NumParams);
Ben Langmuirce914fc2013-05-03 19:20:19 +00003417 static CapturedDecl *CreateDeserialized(ASTContext &C, unsigned ID,
3418 unsigned NumParams);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003419
Craig Toppercbce6e92014-03-11 06:22:39 +00003420 Stmt *getBody() const override { return Body; }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003421 void setBody(Stmt *B) { Body = B; }
3422
Ben Langmuirce914fc2013-05-03 19:20:19 +00003423 unsigned getNumParams() const { return NumParams; }
3424
Ben Langmuir37943a72013-05-03 19:00:33 +00003425 ImplicitParamDecl *getParam(unsigned i) const {
3426 assert(i < NumParams);
3427 return getParams()[i];
3428 }
3429 void setParam(unsigned i, ImplicitParamDecl *P) {
3430 assert(i < NumParams);
3431 getParams()[i] = P;
3432 }
3433
3434 /// \brief Retrieve the parameter containing captured variables.
3435 ImplicitParamDecl *getContextParam() const { return getParam(0); }
3436 void setContextParam(ImplicitParamDecl *P) { setParam(0, P); }
3437
3438 typedef ImplicitParamDecl **param_iterator;
Aaron Ballmane7fbde82014-03-07 16:40:17 +00003439 typedef llvm::iterator_range<param_iterator> param_range;
3440
Ben Langmuir37943a72013-05-03 19:00:33 +00003441 /// \brief Retrieve an iterator pointing to the first parameter decl.
3442 param_iterator param_begin() const { return getParams(); }
3443 /// \brief Retrieve an iterator one past the last parameter decl.
3444 param_iterator param_end() const { return getParams() + NumParams; }
3445
Aaron Ballmane7fbde82014-03-07 16:40:17 +00003446 /// \brief Retrieve an iterator range for the parameter declarations.
Aaron Ballman43027eb2014-03-08 18:30:40 +00003447 param_range params() const { return param_range(param_begin(), param_end()); }
Aaron Ballmane7fbde82014-03-07 16:40:17 +00003448
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003449 // Implement isa/cast/dyncast/etc.
3450 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3451 static bool classofKind(Kind K) { return K == Captured; }
3452 static DeclContext *castToDeclContext(const CapturedDecl *D) {
3453 return static_cast<DeclContext *>(const_cast<CapturedDecl *>(D));
3454 }
3455 static CapturedDecl *castFromDeclContext(const DeclContext *DC) {
3456 return static_cast<CapturedDecl *>(const_cast<DeclContext *>(DC));
3457 }
Ben Langmuirce914fc2013-05-03 19:20:19 +00003458
3459 friend class ASTDeclReader;
3460 friend class ASTDeclWriter;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003461};
3462
Douglas Gregorba345522011-12-02 23:23:56 +00003463/// \brief Describes a module import declaration, which makes the contents
3464/// of the named module visible in the current translation unit.
3465///
3466/// An import declaration imports the named module (or submodule). For example:
3467/// \code
Douglas Gregorc50d4922012-12-11 22:11:52 +00003468/// @import std.vector;
Douglas Gregorba345522011-12-02 23:23:56 +00003469/// \endcode
3470///
James Dennett1355bd12012-06-11 06:19:40 +00003471/// Import declarations can also be implicitly generated from
3472/// \#include/\#import directives.
Douglas Gregorba345522011-12-02 23:23:56 +00003473class ImportDecl : public Decl {
3474 /// \brief The imported module, along with a bit that indicates whether
3475 /// we have source-location information for each identifier in the module
3476 /// name.
3477 ///
3478 /// When the bit is false, we only have a single source location for the
3479 /// end of the import declaration.
3480 llvm::PointerIntPair<Module *, 1, bool> ImportedAndComplete;
3481
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003482 /// \brief The next import in the list of imports local to the translation
3483 /// unit being parsed (not loaded from an AST file).
3484 ImportDecl *NextLocalImport;
3485
Douglas Gregorba345522011-12-02 23:23:56 +00003486 friend class ASTReader;
3487 friend class ASTDeclReader;
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003488 friend class ASTContext;
Douglas Gregorba345522011-12-02 23:23:56 +00003489
Douglas Gregor22d09742012-01-03 18:04:46 +00003490 ImportDecl(DeclContext *DC, SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003491 ArrayRef<SourceLocation> IdentifierLocs);
3492
Douglas Gregor22d09742012-01-03 18:04:46 +00003493 ImportDecl(DeclContext *DC, SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003494 SourceLocation EndLoc);
3495
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003496 ImportDecl(EmptyShell Empty) : Decl(Import, Empty), NextLocalImport() { }
Douglas Gregorba345522011-12-02 23:23:56 +00003497
3498public:
3499 /// \brief Create a new module import declaration.
3500 static ImportDecl *Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003501 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003502 ArrayRef<SourceLocation> IdentifierLocs);
3503
3504 /// \brief Create a new module import declaration for an implicitly-generated
3505 /// import.
3506 static ImportDecl *CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003507 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003508 SourceLocation EndLoc);
3509
Douglas Gregor72172e92012-01-05 21:55:30 +00003510 /// \brief Create a new, deserialized module import declaration.
3511 static ImportDecl *CreateDeserialized(ASTContext &C, unsigned ID,
3512 unsigned NumLocations);
Douglas Gregorba345522011-12-02 23:23:56 +00003513
3514 /// \brief Retrieve the module that was imported by the import declaration.
3515 Module *getImportedModule() const { return ImportedAndComplete.getPointer(); }
3516
3517 /// \brief Retrieves the locations of each of the identifiers that make up
3518 /// the complete module name in the import declaration.
3519 ///
3520 /// This will return an empty array if the locations of the individual
3521 /// identifiers aren't available.
3522 ArrayRef<SourceLocation> getIdentifierLocs() const;
Craig Toppercbce6e92014-03-11 06:22:39 +00003523
3524 SourceRange getSourceRange() const override LLVM_READONLY;
3525
Douglas Gregorba345522011-12-02 23:23:56 +00003526 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Douglas Gregorba345522011-12-02 23:23:56 +00003527 static bool classofKind(Kind K) { return K == Import; }
3528};
Michael Han84324352013-02-22 17:15:32 +00003529
3530/// \brief Represents an empty-declaration.
3531class EmptyDecl : public Decl {
3532 virtual void anchor();
3533 EmptyDecl(DeclContext *DC, SourceLocation L)
3534 : Decl(Empty, DC, L) { }
3535
3536public:
3537 static EmptyDecl *Create(ASTContext &C, DeclContext *DC,
3538 SourceLocation L);
3539 static EmptyDecl *CreateDeserialized(ASTContext &C, unsigned ID);
3540
3541 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3542 static bool classofKind(Kind K) { return K == Empty; }
3543};
Douglas Gregorba345522011-12-02 23:23:56 +00003544
Douglas Gregor2ada0482009-02-04 17:27:36 +00003545/// Insertion operator for diagnostics. This allows sending NamedDecl's
3546/// into a diagnostic with <<.
Benjamin Kramer7ec12c92012-02-07 22:29:24 +00003547inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
3548 const NamedDecl* ND) {
3549 DB.AddTaggedVal(reinterpret_cast<intptr_t>(ND),
3550 DiagnosticsEngine::ak_nameddecl);
3551 return DB;
3552}
3553inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
3554 const NamedDecl* ND) {
3555 PD.AddTaggedVal(reinterpret_cast<intptr_t>(ND),
3556 DiagnosticsEngine::ak_nameddecl);
3557 return PD;
3558}
Douglas Gregor2ada0482009-02-04 17:27:36 +00003559
Douglas Gregorbf62d642010-12-06 18:36:25 +00003560template<typename decl_type>
Rafael Espindola8db352d2013-10-17 15:37:26 +00003561void Redeclarable<decl_type>::setPreviousDecl(decl_type *PrevDecl) {
Douglas Gregorbf62d642010-12-06 18:36:25 +00003562 // Note: This routine is implemented here because we need both NamedDecl
3563 // and Redeclarable to be defined.
3564
3565 decl_type *First;
David Blaikie21bfbf82011-11-09 06:07:30 +00003566
Douglas Gregorbf62d642010-12-06 18:36:25 +00003567 if (PrevDecl) {
3568 // Point to previous. Make sure that this is actually the most recent
3569 // redeclaration, or we can build invalid chains. If the most recent
3570 // redeclaration is invalid, it won't be PrevDecl, but we want it anyway.
Rafael Espindola8db352d2013-10-17 15:37:26 +00003571 First = PrevDecl->getFirstDecl();
Douglas Gregorbf62d642010-12-06 18:36:25 +00003572 assert(First->RedeclLink.NextIsLatest() && "Expected first");
Rafael Espindolacd7720a2012-11-20 23:23:57 +00003573 decl_type *MostRecent = First->RedeclLink.getNext();
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003574 RedeclLink = PreviousDeclLink(cast<decl_type>(MostRecent));
Richard Smith64017682013-07-17 23:53:16 +00003575
3576 // If the declaration was previously visible, a redeclaration of it remains
3577 // visible even if it wouldn't be visible by itself.
Richard Smith64017682013-07-17 23:53:16 +00003578 static_cast<decl_type*>(this)->IdentifierNamespace |=
Richard Smith541b38b2013-09-20 01:15:31 +00003579 MostRecent->getIdentifierNamespace() &
Richard Smith64017682013-07-17 23:53:16 +00003580 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Douglas Gregorbf62d642010-12-06 18:36:25 +00003581 } else {
3582 // Make this first.
3583 First = static_cast<decl_type*>(this);
3584 }
David Blaikie21bfbf82011-11-09 06:07:30 +00003585
Douglas Gregorbf62d642010-12-06 18:36:25 +00003586 // First one will point to this one as latest.
3587 First->RedeclLink = LatestDeclLink(static_cast<decl_type*>(this));
Chandler Carruth5aa9d792013-03-14 11:17:20 +00003588 assert(!isa<NamedDecl>(static_cast<decl_type*>(this)) ||
3589 cast<NamedDecl>(static_cast<decl_type*>(this))->isLinkageValid());
Douglas Gregorbf62d642010-12-06 18:36:25 +00003590}
3591
Daniel Dunbar49248822012-03-06 18:20:20 +00003592// Inline function definitions.
3593
3594/// \brief Check if the given decl is complete.
3595///
3596/// We use this function to break a cycle between the inline definitions in
3597/// Type.h and Decl.h.
3598inline bool IsEnumDeclComplete(EnumDecl *ED) {
3599 return ED->isComplete();
3600}
3601
Daniel Dunbard0437332012-03-08 02:52:18 +00003602/// \brief Check if the given decl is scoped.
3603///
3604/// We use this function to break a cycle between the inline definitions in
3605/// Type.h and Decl.h.
3606inline bool IsEnumDeclScoped(EnumDecl *ED) {
3607 return ED->isScoped();
3608}
3609
Steve Naroff415d3d52008-10-08 17:01:13 +00003610} // end namespace clang
Nate Begemanf2a6e5f2008-12-16 19:57:09 +00003611
Chris Lattnerd0745a02006-08-17 05:18:35 +00003612#endif