blob: 5b5ad34b0d419da0d5a0dc07b908676e5548588f [file] [log] [blame]
Eugene Zelenkode0215c2017-11-13 23:01:27 +00001//===- Decl.h - Classes for representing declarations -----------*- C++ -*-===//
Chris Lattnerd0745a02006-08-17 05:18:35 +00002//
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"
Eugene Zelenkode0215c2017-11-13 23:01:27 +000021#include "clang/AST/NestedNameSpecifier.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000022#include "clang/AST/Redeclarable.h"
23#include "clang/AST/Type.h"
Eugene Zelenkode0215c2017-11-13 23:01:27 +000024#include "clang/Basic/AddressSpaces.h"
25#include "clang/Basic/Diagnostic.h"
26#include "clang/Basic/IdentifierTable.h"
27#include "clang/Basic/LLVM.h"
Douglas Gregor7dc5c172010-02-03 09:33:45 +000028#include "clang/Basic/Linkage.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000029#include "clang/Basic/OperatorKinds.h"
Eugene Zelenkode0215c2017-11-13 23:01:27 +000030#include "clang/Basic/PartialDiagnostic.h"
Nico Weber66220292016-03-02 17:28:48 +000031#include "clang/Basic/PragmaKinds.h"
Eugene Zelenkode0215c2017-11-13 23:01:27 +000032#include "clang/Basic/SourceLocation.h"
33#include "clang/Basic/Specifiers.h"
34#include "clang/Basic/Visibility.h"
35#include "llvm/ADT/APSInt.h"
David Blaikie9c70e042011-09-21 18:16:56 +000036#include "llvm/ADT/ArrayRef.h"
Douglas Gregor1baf38f2011-03-26 12:10:19 +000037#include "llvm/ADT/Optional.h"
Eugene Zelenkode0215c2017-11-13 23:01:27 +000038#include "llvm/ADT/PointerIntPair.h"
39#include "llvm/ADT/PointerUnion.h"
40#include "llvm/ADT/StringRef.h"
41#include "llvm/ADT/iterator_range.h"
42#include "llvm/Support/Casting.h"
Daniel Dunbare4775e12012-03-09 19:35:29 +000043#include "llvm/Support/Compiler.h"
James Y Knight7a22b242015-08-06 20:26:32 +000044#include "llvm/Support/TrailingObjects.h"
Eugene Zelenkode0215c2017-11-13 23:01:27 +000045#include <cassert>
46#include <cstddef>
47#include <cstdint>
48#include <string>
49#include <utility>
Ted Kremenek1f1e7562007-10-25 21:37:16 +000050
Chris Lattnerd0745a02006-08-17 05:18:35 +000051namespace clang {
Eugene Zelenkode0215c2017-11-13 23:01:27 +000052
53class ASTContext;
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000054struct ASTTemplateArgumentListInfo;
Eugene Zelenkode0215c2017-11-13 23:01:27 +000055class Attr;
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000056class CompoundStmt;
57class DependentFunctionTemplateSpecializationInfo;
Eugene Zelenkode0215c2017-11-13 23:01:27 +000058class EnumDecl;
Chris Lattnerc1915e22007-01-25 07:29:02 +000059class Expr;
Douglas Gregor24c332b2009-05-14 21:06:31 +000060class FunctionTemplateDecl;
Douglas Gregor70d83e22009-06-29 17:30:29 +000061class FunctionTemplateSpecializationInfo;
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000062class LabelStmt;
63class MemberSpecializationInfo;
Eugene Zelenkode0215c2017-11-13 23:01:27 +000064class Module;
65class NamespaceDecl;
David Majnemerfa7bc782015-05-19 00:57:16 +000066class ParmVarDecl;
Eugene Zelenkode0215c2017-11-13 23:01:27 +000067class RecordDecl;
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000068class Stmt;
69class StringLiteral;
Eugene Zelenkode0215c2017-11-13 23:01:27 +000070class TagDecl;
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000071class TemplateArgumentList;
Eugene Zelenkode0215c2017-11-13 23:01:27 +000072class TemplateArgumentListInfo;
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000073class TemplateParameterList;
Richard Smith43ccec8e2014-08-26 03:52:16 +000074class TypeAliasTemplateDecl;
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000075class TypeLoc;
John McCallb9c78482010-04-08 09:05:18 +000076class UnresolvedSetImpl;
Larisse Voufo39a1e502013-08-06 01:03:05 +000077class VarTemplateDecl;
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000078
James Dennettb8973efb2018-02-02 20:22:29 +000079/// A container of type source information.
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000080///
81/// A client can read the relevant info using TypeLoc wrappers, e.g:
82/// @code
John McCallbcd03502009-12-07 02:54:59 +000083/// TypeLoc TL = TypeSourceInfo->getTypeLoc();
Yaron Keren4e9d3c32014-07-07 09:52:31 +000084/// TL.getStartLoc().print(OS, SrcMgr);
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000085/// @endcode
Benjamin Kramer4bb33342018-01-26 20:01:13 +000086class LLVM_ALIGNAS(8) TypeSourceInfo {
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000087 // Contains a memory block after the class, used for type source information,
88 // allocated by ASTContext.
89 friend class ASTContext;
Eugene Zelenkode0215c2017-11-13 23:01:27 +000090
91 QualType Ty;
92
93 TypeSourceInfo(QualType ty) : Ty(ty) {}
94
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000095public:
James Dennettb8973efb2018-02-02 20:22:29 +000096 /// Return the type wrapped by this type source info.
John McCallff470312009-10-16 21:42:04 +000097 QualType getType() const { return Ty; }
98
James Dennettb8973efb2018-02-02 20:22:29 +000099 /// Return the TypeLoc wrapper for the type source info.
John McCall00861902010-05-06 09:25:57 +0000100 TypeLoc getTypeLoc() const; // implemented in TypeLoc.h
Richard Smith0b3a4622014-11-13 20:01:57 +0000101
James Dennettb8973efb2018-02-02 20:22:29 +0000102 /// Override the type stored in this TypeSourceInfo. Use with caution!
Richard Smith0b3a4622014-11-13 20:01:57 +0000103 void overrideType(QualType T) { Ty = T; }
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000104};
105
James Dennett31a57342018-02-02 21:38:22 +0000106/// The top declaration context.
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +0000107class TranslationUnitDecl : public Decl, public DeclContext {
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000108 ASTContext &Ctx;
Mike Stump11289f42009-09-09 15:08:12 +0000109
John McCall0db42252009-12-16 02:06:49 +0000110 /// The (most recently entered) anonymous namespace for this
111 /// translation unit, if one has been created.
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000112 NamespaceDecl *AnonymousNamespace = nullptr;
John McCall0db42252009-12-16 02:06:49 +0000113
Richard Smith42413142015-05-15 20:05:43 +0000114 explicit TranslationUnitDecl(ASTContext &ctx);
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000115
116 virtual void anchor();
117
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +0000118public:
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000119 ASTContext &getASTContext() const { return Ctx; }
Mike Stump11289f42009-09-09 15:08:12 +0000120
John McCall0db42252009-12-16 02:06:49 +0000121 NamespaceDecl *getAnonymousNamespace() const { return AnonymousNamespace; }
122 void setAnonymousNamespace(NamespaceDecl *D) { AnonymousNamespace = D; }
123
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +0000124 static TranslationUnitDecl *Create(ASTContext &C);
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000125
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +0000126 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +0000127 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +0000128 static bool classofKind(Kind K) { return K == TranslationUnit; }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000129 static DeclContext *castToDeclContext(const TranslationUnitDecl *D) {
130 return static_cast<DeclContext *>(const_cast<TranslationUnitDecl*>(D));
131 }
132 static TranslationUnitDecl *castFromDeclContext(const DeclContext *DC) {
133 return static_cast<TranslationUnitDecl *>(const_cast<DeclContext*>(DC));
134 }
Argyrios Kyrtzidisc3b69ae2008-04-17 14:40:12 +0000135};
136
James Dennettb8973efb2018-02-02 20:22:29 +0000137/// Represents a `#pragma comment` line. Always a child of
Nico Weber66220292016-03-02 17:28:48 +0000138/// TranslationUnitDecl.
139class PragmaCommentDecl final
140 : public Decl,
141 private llvm::TrailingObjects<PragmaCommentDecl, char> {
Nico Weber66220292016-03-02 17:28:48 +0000142 friend class ASTDeclReader;
143 friend class ASTDeclWriter;
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000144 friend TrailingObjects;
145
146 PragmaMSCommentKind CommentKind;
Nico Weber66220292016-03-02 17:28:48 +0000147
148 PragmaCommentDecl(TranslationUnitDecl *TU, SourceLocation CommentLoc,
149 PragmaMSCommentKind CommentKind)
150 : Decl(PragmaComment, TU, CommentLoc), CommentKind(CommentKind) {}
151
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000152 virtual void anchor();
153
Nico Weber66220292016-03-02 17:28:48 +0000154public:
155 static PragmaCommentDecl *Create(const ASTContext &C, TranslationUnitDecl *DC,
156 SourceLocation CommentLoc,
157 PragmaMSCommentKind CommentKind,
158 StringRef Arg);
159 static PragmaCommentDecl *CreateDeserialized(ASTContext &C, unsigned ID,
160 unsigned ArgSize);
161
162 PragmaMSCommentKind getCommentKind() const { return CommentKind; }
163
164 StringRef getArg() const { return getTrailingObjects<char>(); }
165
166 // Implement isa/cast/dyncast/etc.
167 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
168 static bool classofKind(Kind K) { return K == PragmaComment; }
169};
170
James Dennettb8973efb2018-02-02 20:22:29 +0000171/// Represents a `#pragma detect_mismatch` line. Always a child of
Nico Webercbbaeb12016-03-02 19:28:54 +0000172/// TranslationUnitDecl.
173class PragmaDetectMismatchDecl final
174 : public Decl,
175 private llvm::TrailingObjects<PragmaDetectMismatchDecl, char> {
Nico Webercbbaeb12016-03-02 19:28:54 +0000176 friend class ASTDeclReader;
177 friend class ASTDeclWriter;
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000178 friend TrailingObjects;
179
180 size_t ValueStart;
Nico Webercbbaeb12016-03-02 19:28:54 +0000181
182 PragmaDetectMismatchDecl(TranslationUnitDecl *TU, SourceLocation Loc,
183 size_t ValueStart)
184 : Decl(PragmaDetectMismatch, TU, Loc), ValueStart(ValueStart) {}
185
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000186 virtual void anchor();
187
Nico Webercbbaeb12016-03-02 19:28:54 +0000188public:
189 static PragmaDetectMismatchDecl *Create(const ASTContext &C,
190 TranslationUnitDecl *DC,
191 SourceLocation Loc, StringRef Name,
192 StringRef Value);
193 static PragmaDetectMismatchDecl *
194 CreateDeserialized(ASTContext &C, unsigned ID, unsigned NameValueSize);
195
196 StringRef getName() const { return getTrailingObjects<char>(); }
197 StringRef getValue() const { return getTrailingObjects<char>() + ValueStart; }
198
199 // Implement isa/cast/dyncast/etc.
200 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
201 static bool classofKind(Kind K) { return K == PragmaDetectMismatch; }
202};
203
James Dennettb8973efb2018-02-02 20:22:29 +0000204/// Declaration context for names declared as extern "C" in C++. This
Richard Smithf19e1272015-03-07 00:04:49 +0000205/// is neither the semantic nor lexical context for such declarations, but is
206/// used to check for conflicts with other extern "C" declarations. Example:
207///
208/// \code
209/// namespace N { extern "C" void f(); } // #1
210/// void N::f() {} // #2
211/// namespace M { extern "C" void f(); } // #3
212/// \endcode
213///
214/// The semantic context of #1 is namespace N and its lexical context is the
215/// LinkageSpecDecl; the semantic context of #2 is namespace N and its lexical
216/// context is the TU. However, both declarations are also visible in the
217/// extern "C" context.
218///
219/// The declaration at #3 finds it is a redeclaration of \c N::f through
220/// lookup in the extern "C" context.
221class ExternCContextDecl : public Decl, public DeclContext {
Richard Smithf19e1272015-03-07 00:04:49 +0000222 explicit ExternCContextDecl(TranslationUnitDecl *TU)
223 : Decl(ExternCContext, TU, SourceLocation()),
224 DeclContext(ExternCContext) {}
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000225
226 virtual void anchor();
227
Richard Smithf19e1272015-03-07 00:04:49 +0000228public:
229 static ExternCContextDecl *Create(const ASTContext &C,
230 TranslationUnitDecl *TU);
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000231
Richard Smithf19e1272015-03-07 00:04:49 +0000232 // Implement isa/cast/dyncast/etc.
233 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
234 static bool classofKind(Kind K) { return K == ExternCContext; }
235 static DeclContext *castToDeclContext(const ExternCContextDecl *D) {
236 return static_cast<DeclContext *>(const_cast<ExternCContextDecl*>(D));
237 }
238 static ExternCContextDecl *castFromDeclContext(const DeclContext *DC) {
239 return static_cast<ExternCContextDecl *>(const_cast<DeclContext*>(DC));
240 }
241};
242
James Dennettb8973efb2018-02-02 20:22:29 +0000243/// This represents a decl that may have a name. Many decls have names such
Chandler Carruth48687a52012-06-07 17:55:42 +0000244/// as ObjCMethodDecl, but not \@class, etc.
James Dennettb8973efb2018-02-02 20:22:29 +0000245///
246/// Note that not every NamedDecl is actually named (e.g., a struct might
247/// be anonymous), and not every name is an identifier.
Chris Lattnera4016552007-10-06 22:53:46 +0000248class NamedDecl : public Decl {
James Dennettb8973efb2018-02-02 20:22:29 +0000249 /// The name of this declaration, which is typically a normal
Douglas Gregor77324f32008-11-17 14:58:09 +0000250 /// identifier but may also be a special kind of name (C++
251 /// constructor, Objective-C selector, etc.)
252 DeclarationName Name;
253
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000254 virtual void anchor();
255
Daniel Dunbar47c32282012-03-08 20:29:02 +0000256private:
Alp Tokera2794f92014-01-22 07:29:52 +0000257 NamedDecl *getUnderlyingDeclImpl() LLVM_READONLY;
Daniel Dunbar47c32282012-03-08 20:29:02 +0000258
Douglas Gregor77324f32008-11-17 14:58:09 +0000259protected:
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000260 NamedDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName N)
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000261 : Decl(DK, DC, L), Name(N) {}
Douglas Gregor027ba502010-12-06 17:49:01 +0000262
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000263public:
James Dennettb8973efb2018-02-02 20:22:29 +0000264 /// Get the identifier that names this declaration, if there is one.
265 ///
266 /// This will return NULL if this declaration has no name (e.g., for
267 /// an unnamed class) or if the name is a special name (C++ constructor,
268 /// Objective-C selector, etc.).
Douglas Gregor77324f32008-11-17 14:58:09 +0000269 IdentifierInfo *getIdentifier() const { return Name.getAsIdentifierInfo(); }
270
James Dennettb8973efb2018-02-02 20:22:29 +0000271 /// Get the name of identifier for this declaration as a StringRef.
272 ///
Daniel Dunbar0013afb2009-10-18 02:09:45 +0000273 /// This requires that the declaration have a name and that it be a simple
274 /// identifier.
Chris Lattner01cf8db2011-07-20 06:58:45 +0000275 StringRef getName() const {
Daniel Dunbar0914afe2009-10-19 01:21:12 +0000276 assert(Name.isIdentifier() && "Name is not a simple identifier");
277 return getIdentifier() ? getIdentifier()->getName() : "";
Daniel Dunbar0013afb2009-10-18 02:09:45 +0000278 }
279
James Dennettb8973efb2018-02-02 20:22:29 +0000280 /// Get a human-readable name for the declaration, even if it is one of the
281 /// special kinds of names (C++ constructor, Objective-C selector, etc).
282 ///
283 /// Creating this name requires expensive string manipulation, so it should
284 /// be called only when performance doesn't matter. For simple declarations,
285 /// getNameAsCString() should suffice.
Daniel Dunbar0013afb2009-10-18 02:09:45 +0000286 //
Daniel Dunbar8b794812009-10-19 01:20:50 +0000287 // FIXME: This function should be renamed to indicate that it is not just an
288 // alternate form of getName(), and clients should move as appropriate.
289 //
Daniel Dunbar0013afb2009-10-18 02:09:45 +0000290 // FIXME: Deprecated, move clients to getName().
291 std::string getNameAsString() const { return Name.getAsString(); }
292
Richard Smith7873de02016-08-11 22:25:46 +0000293 virtual void printName(raw_ostream &os) const;
Ted Kremenekc14f5eb2010-05-07 20:07:19 +0000294
James Dennettb8973efb2018-02-02 20:22:29 +0000295 /// Get the actual, stored name of the declaration, which may be a special
296 /// name.
Douglas Gregor77324f32008-11-17 14:58:09 +0000297 DeclarationName getDeclName() const { return Name; }
298
James Dennettb8973efb2018-02-02 20:22:29 +0000299 /// Set the name of this declaration.
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000300 void setDeclName(DeclarationName N) { Name = N; }
301
James Dennettb8973efb2018-02-02 20:22:29 +0000302 /// Returns a human-readable qualified name for this declaration, like
303 /// A::B::i, for i being member of namespace A::B.
304 ///
305 /// If the declaration is not a member of context which can be named (record,
306 /// namespace), it will return the same result as printName().
307 ///
Douglas Gregor2ada0482009-02-04 17:27:36 +0000308 /// Creating this name is expensive, so it should be called only when
309 /// performance doesn't matter.
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000310 void printQualifiedName(raw_ostream &OS) const;
311 void printQualifiedName(raw_ostream &OS, const PrintingPolicy &Policy) const;
312
Aaron Ballman75ee4cc2014-01-03 18:42:48 +0000313 // FIXME: Remove string version.
Douglas Gregor2ada0482009-02-04 17:27:36 +0000314 std::string getQualifiedNameAsString() const;
Douglas Gregor2ada0482009-02-04 17:27:36 +0000315
Erich Keane7670b4b2017-12-15 16:37:14 +0000316 /// Appends a human-readable name for this declaration into the given stream.
John McCalle1f2ec22009-09-11 06:45:03 +0000317 ///
318 /// This is the method invoked by Sema when displaying a NamedDecl
319 /// in a diagnostic. It does not necessarily produce the same
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000320 /// result as printName(); for example, class template
John McCalle1f2ec22009-09-11 06:45:03 +0000321 /// specializations are printed with their template arguments.
Benjamin Kramer9170e912013-02-22 15:46:01 +0000322 virtual void getNameForDiagnostic(raw_ostream &OS,
Douglas Gregorb11aad82011-02-19 18:51:44 +0000323 const PrintingPolicy &Policy,
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000324 bool Qualified) const;
John McCalle1f2ec22009-09-11 06:45:03 +0000325
James Dennettb8973efb2018-02-02 20:22:29 +0000326 /// Determine whether this declaration, if known to be well-formed within
327 /// its context, will replace the declaration OldD if introduced into scope.
328 ///
329 /// A declaration will replace another declaration if, for example, it is
330 /// a redeclaration of the same variable or function, but not if it is a
331 /// declaration of a different kind (function vs. class) or an overloaded
332 /// function.
Richard Smithe8292b12015-02-10 03:28:10 +0000333 ///
334 /// \param IsKnownNewer \c true if this declaration is known to be newer
335 /// than \p OldD (for instance, if this declaration is newly-created).
336 bool declarationReplaces(NamedDecl *OldD, bool IsKnownNewer = true) const;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000337
James Dennettb8973efb2018-02-02 20:22:29 +0000338 /// Determine whether this declaration has linkage.
Douglas Gregoreddf4332009-02-24 20:03:32 +0000339 bool hasLinkage() const;
340
Douglas Gregor781f7132012-01-06 16:59:53 +0000341 using Decl::isModulePrivate;
342 using Decl::setModulePrivate;
Richard Smith42713d72013-07-14 02:01:48 +0000343
James Dennettb8973efb2018-02-02 20:22:29 +0000344 /// Determine whether this declaration is a C++ class member.
John McCall57500772009-12-16 12:17:52 +0000345 bool isCXXClassMember() const {
346 const DeclContext *DC = getDeclContext();
347
348 // C++0x [class.mem]p1:
349 // The enumerators of an unscoped enumeration defined in
350 // the class are members of the class.
John McCall57500772009-12-16 12:17:52 +0000351 if (isa<EnumDecl>(DC))
Alp Toker363e7c42014-05-31 06:11:09 +0000352 DC = DC->getRedeclContext();
John McCall57500772009-12-16 12:17:52 +0000353
354 return DC->isRecord();
355 }
356
James Dennettb8973efb2018-02-02 20:22:29 +0000357 /// Determine whether the given declaration is an instance member of
Douglas Gregor3f28ec22012-03-08 02:08:05 +0000358 /// a C++ class.
John McCalla8ae2222010-04-06 21:38:20 +0000359 bool isCXXInstanceMember() const;
360
James Dennettb8973efb2018-02-02 20:22:29 +0000361 /// Determine what kind of linkage this entity has.
362 ///
Rafael Espindola3ae00052013-05-13 00:12:11 +0000363 /// This is not the linkage as defined by the standard or the codegen notion
364 /// of linkage. It is just an implementation detail that is used to compute
365 /// those.
366 Linkage getLinkageInternal() const;
367
James Dennettb8973efb2018-02-02 20:22:29 +0000368 /// Get the linkage from a semantic point of view. Entities in
Rafael Espindola3ae00052013-05-13 00:12:11 +0000369 /// anonymous namespaces are external (in c++98).
370 Linkage getFormalLinkage() const {
Rafael Espindolab97e8962013-05-27 14:14:42 +0000371 return clang::getFormalLinkage(getLinkageInternal());
Rafael Espindola3ae00052013-05-13 00:12:11 +0000372 }
John McCall457a04e2010-10-22 21:05:15 +0000373
James Dennettb8973efb2018-02-02 20:22:29 +0000374 /// True if this decl has external linkage.
Rafael Espindola3ae00052013-05-13 00:12:11 +0000375 bool hasExternalFormalLinkage() const {
Rafael Espindolab97e8962013-05-27 14:14:42 +0000376 return isExternalFormalLinkage(getLinkageInternal());
Rafael Espindola3ae00052013-05-13 00:12:11 +0000377 }
Rafael Espindolab97e8962013-05-27 14:14:42 +0000378
Rafael Espindola3ae00052013-05-13 00:12:11 +0000379 bool isExternallyVisible() const {
Rafael Espindolab97e8962013-05-27 14:14:42 +0000380 return clang::isExternallyVisible(getLinkageInternal());
Rafael Espindola45b620a2013-03-07 02:00:27 +0000381 }
382
Richard Smithbecb92d2017-10-10 22:33:17 +0000383 /// Determine whether this declaration can be redeclared in a
384 /// different translation unit.
385 bool isExternallyDeclarable() const {
386 return isExternallyVisible() && !getOwningModuleForLinkage();
387 }
388
James Dennettb8973efb2018-02-02 20:22:29 +0000389 /// Determines the visibility of this entity.
David Blaikie21bfbf82011-11-09 06:07:30 +0000390 Visibility getVisibility() const {
Rafael Espindola4a5da442013-02-27 02:56:45 +0000391 return getLinkageAndVisibility().getVisibility();
David Blaikie21bfbf82011-11-09 06:07:30 +0000392 }
John McCall457a04e2010-10-22 21:05:15 +0000393
James Dennettb8973efb2018-02-02 20:22:29 +0000394 /// Determines the linkage and visibility of this entity.
John McCallc273f242010-10-30 11:50:40 +0000395 LinkageInfo getLinkageAndVisibility() const;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000396
John McCalld041a9b2013-02-20 01:54:26 +0000397 /// Kinds of explicit visibility.
398 enum ExplicitVisibilityKind {
Richard Smithfbe7b462017-09-22 02:22:32 +0000399 /// Do an LV computation for, ultimately, a type.
400 /// Visibility may be restricted by type visibility settings and
401 /// the visibility of template arguments.
John McCalld041a9b2013-02-20 01:54:26 +0000402 VisibilityForType,
Richard Smithfbe7b462017-09-22 02:22:32 +0000403
404 /// Do an LV computation for, ultimately, a non-type declaration.
405 /// Visibility may be restricted by value visibility settings and
406 /// the visibility of template arguments.
John McCalld041a9b2013-02-20 01:54:26 +0000407 VisibilityForValue
408 };
409
James Dennettb8973efb2018-02-02 20:22:29 +0000410 /// If visibility was explicitly specified for this
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000411 /// declaration, return that visibility.
David Blaikie05785d162013-02-20 22:23:23 +0000412 Optional<Visibility>
John McCalld041a9b2013-02-20 01:54:26 +0000413 getExplicitVisibility(ExplicitVisibilityKind kind) const;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000414
James Dennettb8973efb2018-02-02 20:22:29 +0000415 /// True if the computed linkage is valid. Used for consistency
Rafael Espindola0e0d0092013-03-14 03:07:35 +0000416 /// checking. Should always return true.
417 bool isLinkageValid() const;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000418
James Dennettb8973efb2018-02-02 20:22:29 +0000419 /// True if something has required us to compute the linkage
John McCall2575d882014-01-30 01:12:53 +0000420 /// of this declaration.
421 ///
422 /// Language features which can retroactively change linkage (like a
423 /// typedef name for linkage purposes) may need to consider this,
424 /// but hopefully only in transitory ways during parsing.
425 bool hasLinkageBeenComputed() const {
426 return hasCachedLinkage();
427 }
428
James Dennettb8973efb2018-02-02 20:22:29 +0000429 /// Looks through UsingDecls and ObjCCompatibleAliasDecls for
Anders Carlsson6915bf62009-06-26 06:29:23 +0000430 /// the underlying named decl.
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +0000431 NamedDecl *getUnderlyingDecl() {
Daniel Dunbar47c32282012-03-08 20:29:02 +0000432 // Fast-path the common case.
433 if (this->getKind() != UsingShadow &&
Richard Smith5179eb72016-06-28 19:03:57 +0000434 this->getKind() != ConstructorUsingShadow &&
Richard Smithf2005d32015-12-29 23:34:32 +0000435 this->getKind() != ObjCCompatibleAlias &&
436 this->getKind() != NamespaceAlias)
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +0000437 return this;
Daniel Dunbar47c32282012-03-08 20:29:02 +0000438
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +0000439 return getUnderlyingDeclImpl();
440 }
Anders Carlsson6915bf62009-06-26 06:29:23 +0000441 const NamedDecl *getUnderlyingDecl() const {
442 return const_cast<NamedDecl*>(this)->getUnderlyingDecl();
443 }
Mike Stump11289f42009-09-09 15:08:12 +0000444
Rafael Espindola7b56f6c2013-10-19 16:55:03 +0000445 NamedDecl *getMostRecentDecl() {
Aaron Ballman5116a8e2013-11-06 22:39:46 +0000446 return cast<NamedDecl>(static_cast<Decl *>(this)->getMostRecentDecl());
Rafael Espindola7b56f6c2013-10-19 16:55:03 +0000447 }
448 const NamedDecl *getMostRecentDecl() const {
449 return const_cast<NamedDecl*>(this)->getMostRecentDecl();
450 }
451
Fariborz Jahanian6485fe42014-09-09 23:10:54 +0000452 ObjCStringFormatFamily getObjCFStringFormattingFamily() const;
453
John McCall180ef092010-01-29 01:45:37 +0000454 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Alexis Hunted053252010-05-30 07:21:58 +0000455 static bool classofKind(Kind K) { return K >= firstNamed && K <= lastNamed; }
Chris Lattnera4016552007-10-06 22:53:46 +0000456};
457
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000458inline raw_ostream &operator<<(raw_ostream &OS, const NamedDecl &ND) {
459 ND.printName(OS);
Benjamin Kramer95c7c422010-04-17 09:56:45 +0000460 return OS;
461}
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000462
James Dennett31a57342018-02-02 21:38:22 +0000463/// Represents the declaration of a label. Labels also have a
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000464/// corresponding LabelStmt, which indicates the position that the label was
465/// defined at. For normal labels, the location of the decl is the same as the
466/// location of the statement. For GNU local labels (__label__), the decl
467/// location is where the __label__ is.
468class LabelDecl : public NamedDecl {
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000469 LabelStmt *TheStmt;
Ehsan Akhgari31097582014-09-22 02:21:54 +0000470 StringRef MSAsmName;
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000471 bool MSAsmNameResolved = false;
472
James Dennett31a57342018-02-02 21:38:22 +0000473 /// For normal labels, this is the same as the main declaration
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000474 /// label, i.e., the location of the identifier; for GNU local labels,
475 /// this is the location of the __label__ keyword.
476 SourceLocation LocStart;
477
478 LabelDecl(DeclContext *DC, SourceLocation IdentL, IdentifierInfo *II,
479 LabelStmt *S, SourceLocation StartL)
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000480 : NamedDecl(Label, DC, IdentL, II), TheStmt(S), LocStart(StartL) {}
481
482 void anchor() override;
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000483
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000484public:
485 static LabelDecl *Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000486 SourceLocation IdentL, IdentifierInfo *II);
487 static LabelDecl *Create(ASTContext &C, DeclContext *DC,
488 SourceLocation IdentL, IdentifierInfo *II,
489 SourceLocation GnuLabelL);
Douglas Gregor72172e92012-01-05 21:55:30 +0000490 static LabelDecl *CreateDeserialized(ASTContext &C, unsigned ID);
491
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000492 LabelStmt *getStmt() const { return TheStmt; }
493 void setStmt(LabelStmt *T) { TheStmt = T; }
494
495 bool isGnuLocal() const { return LocStart != getLocation(); }
496 void setLocStart(SourceLocation L) { LocStart = L; }
497
Craig Toppercbce6e92014-03-11 06:22:39 +0000498 SourceRange getSourceRange() const override LLVM_READONLY {
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000499 return SourceRange(LocStart, getLocation());
500 }
Abramo Bagnara124fdf62011-03-03 18:24:14 +0000501
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000502 bool isMSAsmLabel() const { return !MSAsmName.empty(); }
Ehsan Akhgari31097582014-09-22 02:21:54 +0000503 bool isResolvedMSAsmLabel() const { return isMSAsmLabel() && MSAsmNameResolved; }
504 void setMSAsmLabel(StringRef Name);
505 StringRef getMSAsmLabel() const { return MSAsmName; }
506 void setMSAsmLabelResolved() { MSAsmNameResolved = true; }
507
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000508 // Implement isa/cast/dyncast/etc.
509 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000510 static bool classofKind(Kind K) { return K == Label; }
511};
David Blaikie21bfbf82011-11-09 06:07:30 +0000512
James Dennett31a57342018-02-02 21:38:22 +0000513/// Represent a C++ namespace.
Douglas Gregore57e7522012-01-07 09:11:48 +0000514class NamespaceDecl : public NamedDecl, public DeclContext,
515 public Redeclarable<NamespaceDecl>
516{
James Dennett31a57342018-02-02 21:38:22 +0000517 /// The starting location of the source range, pointing
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000518 /// to either the namespace or the inline keyword.
519 SourceLocation LocStart;
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000520
James Dennett31a57342018-02-02 21:38:22 +0000521 /// The ending location of the source range.
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000522 SourceLocation RBraceLoc;
Mike Stump11289f42009-09-09 15:08:12 +0000523
James Dennettb8973efb2018-02-02 20:22:29 +0000524 /// A pointer to either the anonymous namespace that lives just inside
Douglas Gregore57e7522012-01-07 09:11:48 +0000525 /// this namespace or to the first namespace in the chain (the latter case
526 /// only when this is not the first in the chain), along with a
527 /// boolean value indicating whether this is an inline namespace.
528 llvm::PointerIntPair<NamespaceDecl *, 1, bool> AnonOrFirstNamespaceAndInline;
Mike Stump11289f42009-09-09 15:08:12 +0000529
Richard Smith053f6c62014-05-16 23:01:30 +0000530 NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
531 SourceLocation StartLoc, SourceLocation IdLoc,
532 IdentifierInfo *Id, NamespaceDecl *PrevDecl);
Rafael Espindola8ef7f682013-11-26 15:12:20 +0000533
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000534 using redeclarable_base = Redeclarable<NamespaceDecl>;
535
Richard Smithd7af8a32014-05-10 01:17:36 +0000536 NamespaceDecl *getNextRedeclarationImpl() override;
Craig Toppercbce6e92014-03-11 06:22:39 +0000537 NamespaceDecl *getPreviousDeclImpl() override;
538 NamespaceDecl *getMostRecentDeclImpl() override;
Rafael Espindola8ef7f682013-11-26 15:12:20 +0000539
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +0000540public:
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000541 friend class ASTDeclReader;
542 friend class ASTDeclWriter;
543
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +0000544 static NamespaceDecl *Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +0000545 bool Inline, SourceLocation StartLoc,
546 SourceLocation IdLoc, IdentifierInfo *Id,
547 NamespaceDecl *PrevDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000548
Douglas Gregor72172e92012-01-05 21:55:30 +0000549 static NamespaceDecl *CreateDeserialized(ASTContext &C, unsigned ID);
Douglas Gregorec9fd132012-01-14 16:38:05 +0000550
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000551 using redecl_range = redeclarable_base::redecl_range;
552 using redecl_iterator = redeclarable_base::redecl_iterator;
553
Aaron Ballman211cd8c2014-03-07 00:10:58 +0000554 using redeclarable_base::redecls_begin;
555 using redeclarable_base::redecls_end;
Aaron Ballman86c93902014-03-06 23:45:36 +0000556 using redeclarable_base::redecls;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000557 using redeclarable_base::getPreviousDecl;
558 using redeclarable_base::getMostRecentDecl;
Rafael Espindola3f9e4442013-10-19 02:13:21 +0000559 using redeclarable_base::isFirstDecl;
Douglas Gregore57e7522012-01-07 09:11:48 +0000560
James Dennettb8973efb2018-02-02 20:22:29 +0000561 /// Returns true if this is an anonymous namespace declaration.
Sebastian Redlb5c2baa2010-08-31 00:36:36 +0000562 ///
563 /// For example:
Chris Lattnerca025db2010-05-07 21:43:38 +0000564 /// \code
Sebastian Redlb5c2baa2010-08-31 00:36:36 +0000565 /// namespace {
566 /// ...
567 /// };
568 /// \endcode
569 /// q.v. C++ [namespace.unnamed]
John McCall4fa53422009-10-01 00:25:31 +0000570 bool isAnonymousNamespace() const {
571 return !getIdentifier();
572 }
573
James Dennettb8973efb2018-02-02 20:22:29 +0000574 /// Returns true if this is an inline namespace declaration.
Sebastian Redl50c68252010-08-31 00:36:30 +0000575 bool isInline() const {
Douglas Gregore57e7522012-01-07 09:11:48 +0000576 return AnonOrFirstNamespaceAndInline.getInt();
Sebastian Redlb5c2baa2010-08-31 00:36:36 +0000577 }
578
James Dennettb8973efb2018-02-02 20:22:29 +0000579 /// Set whether this is an inline namespace declaration.
Sebastian Redlb5c2baa2010-08-31 00:36:36 +0000580 void setInline(bool Inline) {
Douglas Gregore57e7522012-01-07 09:11:48 +0000581 AnonOrFirstNamespaceAndInline.setInt(Inline);
Sebastian Redl50c68252010-08-31 00:36:30 +0000582 }
583
James Dennettb8973efb2018-02-02 20:22:29 +0000584 /// Get the original (first) namespace declaration.
Chandler Carruth21c90602015-12-30 03:24:14 +0000585 NamespaceDecl *getOriginalNamespace();
Douglas Gregorf9f54ea2010-03-24 00:46:35 +0000586
James Dennettb8973efb2018-02-02 20:22:29 +0000587 /// Get the original (first) namespace declaration.
Chandler Carruth21c90602015-12-30 03:24:14 +0000588 const NamespaceDecl *getOriginalNamespace() const;
Douglas Gregorf9f54ea2010-03-24 00:46:35 +0000589
James Dennettb8973efb2018-02-02 20:22:29 +0000590 /// Return true if this declaration is an original (first) declaration
Chris Lattnerca025db2010-05-07 21:43:38 +0000591 /// of the namespace. This is false for non-original (subsequent) namespace
592 /// declarations and anonymous namespaces.
Chandler Carruth21c90602015-12-30 03:24:14 +0000593 bool isOriginalNamespace() const;
Chris Lattnerca025db2010-05-07 21:43:38 +0000594
James Dennettb8973efb2018-02-02 20:22:29 +0000595 /// Retrieve the anonymous namespace nested inside this namespace,
Douglas Gregore57e7522012-01-07 09:11:48 +0000596 /// if any.
John McCall0db42252009-12-16 02:06:49 +0000597 NamespaceDecl *getAnonymousNamespace() const {
Douglas Gregore57e7522012-01-07 09:11:48 +0000598 return getOriginalNamespace()->AnonOrFirstNamespaceAndInline.getPointer();
John McCall0db42252009-12-16 02:06:49 +0000599 }
600
601 void setAnonymousNamespace(NamespaceDecl *D) {
Douglas Gregore57e7522012-01-07 09:11:48 +0000602 getOriginalNamespace()->AnonOrFirstNamespaceAndInline.setPointer(D);
John McCall0db42252009-12-16 02:06:49 +0000603 }
604
Douglas Gregore57e7522012-01-07 09:11:48 +0000605 /// Retrieves the canonical declaration of this namespace.
Craig Toppercbce6e92014-03-11 06:22:39 +0000606 NamespaceDecl *getCanonicalDecl() override {
Benjamin Kramerd78f1ec2012-03-06 18:07:25 +0000607 return getOriginalNamespace();
Douglas Gregore57e7522012-01-07 09:11:48 +0000608 }
David Blaikie21bfbf82011-11-09 06:07:30 +0000609 const NamespaceDecl *getCanonicalDecl() const {
Benjamin Kramerd78f1ec2012-03-06 18:07:25 +0000610 return getOriginalNamespace();
Douglas Gregorf9f54ea2010-03-24 00:46:35 +0000611 }
Craig Toppercbce6e92014-03-11 06:22:39 +0000612
613 SourceRange getSourceRange() const override LLVM_READONLY {
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000614 return SourceRange(LocStart, RBraceLoc);
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +0000615 }
616
Daniel Dunbare4775e12012-03-09 19:35:29 +0000617 SourceLocation getLocStart() const LLVM_READONLY { return LocStart; }
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000618 SourceLocation getRBraceLoc() const { return RBraceLoc; }
619 void setLocStart(SourceLocation L) { LocStart = L; }
620 void setRBraceLoc(SourceLocation L) { RBraceLoc = L; }
621
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +0000622 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +0000623 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +0000624 static bool classofKind(Kind K) { return K == Namespace; }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000625 static DeclContext *castToDeclContext(const NamespaceDecl *D) {
626 return static_cast<DeclContext *>(const_cast<NamespaceDecl*>(D));
627 }
628 static NamespaceDecl *castFromDeclContext(const DeclContext *DC) {
629 return static_cast<NamespaceDecl *>(const_cast<DeclContext*>(DC));
630 }
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +0000631};
632
James Dennett31a57342018-02-02 21:38:22 +0000633/// Represent the declaration of a variable (in which case it is
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000634/// an lvalue) a function (in which case it is a function designator) or
Mike Stump11289f42009-09-09 15:08:12 +0000635/// an enum constant.
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000636class ValueDecl : public NamedDecl {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000637 QualType DeclType;
Nate Begemana0f78972007-11-13 22:14:47 +0000638
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000639 void anchor() override;
640
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000641protected:
Chris Lattnerbec41342008-04-22 18:39:57 +0000642 ValueDecl(Kind DK, DeclContext *DC, SourceLocation L,
Mike Stump11289f42009-09-09 15:08:12 +0000643 DeclarationName N, QualType T)
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000644 : NamedDecl(DK, DC, L, N), DeclType(T) {}
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000645
Steve Naroff6fbf0dc2007-03-16 00:33:25 +0000646public:
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000647 QualType getType() const { return DeclType; }
Steve Naroff4dddb612007-07-09 18:55:26 +0000648 void setType(QualType newType) { DeclType = newType; }
Mike Stump11289f42009-09-09 15:08:12 +0000649
James Dennettb8973efb2018-02-02 20:22:29 +0000650 /// Determine whether this symbol is weakly-imported,
Lang Hamesd42bb472011-12-05 20:16:26 +0000651 /// or declared with the weak or weak-ref attr.
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000652 bool isWeak() const;
Lang Hamesd42bb472011-12-05 20:16:26 +0000653
Chris Lattner17ed4872006-11-20 04:58:19 +0000654 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +0000655 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Alexis Hunted053252010-05-30 07:21:58 +0000656 static bool classofKind(Kind K) { return K >= firstValue && K <= lastValue; }
Chris Lattner17ed4872006-11-20 04:58:19 +0000657};
658
James Dennett31a57342018-02-02 21:38:22 +0000659/// A struct with extended info about a syntactic
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000660/// name qualifier, to be used for the case of out-of-line declarations.
661struct QualifierInfo {
Douglas Gregor14454802011-02-25 02:25:35 +0000662 NestedNameSpecifierLoc QualifierLoc;
Abramo Bagnara60804e12011-03-18 15:16:37 +0000663
James Dennett31a57342018-02-02 21:38:22 +0000664 /// The number of "outer" template parameter lists.
Abramo Bagnara60804e12011-03-18 15:16:37 +0000665 /// The count includes all of the template parameter lists that were matched
666 /// against the template-ids occurring into the NNS and possibly (in the
667 /// case of an explicit specialization) a final "template <>".
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000668 unsigned NumTemplParamLists = 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +0000669
James Dennett31a57342018-02-02 21:38:22 +0000670 /// A new-allocated array of size NumTemplParamLists,
Abramo Bagnara60804e12011-03-18 15:16:37 +0000671 /// containing pointers to the "outer" template parameter lists.
672 /// It includes all of the template parameter lists that were matched
673 /// against the template-ids occurring into the NNS and possibly (in the
674 /// case of an explicit specialization) a final "template <>".
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000675 TemplateParameterList** TemplParamLists = nullptr;
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000676
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000677 QualifierInfo() = default;
678 QualifierInfo(const QualifierInfo &) = delete;
679 QualifierInfo& operator=(const QualifierInfo &) = delete;
Abramo Bagnara60804e12011-03-18 15:16:37 +0000680
James Dennett31a57342018-02-02 21:38:22 +0000681 /// Sets info about "outer" template parameter lists.
Douglas Gregor20527e22010-06-15 17:44:38 +0000682 void setTemplateParameterListsInfo(ASTContext &Context,
Benjamin Kramer9cc210652015-08-05 09:40:49 +0000683 ArrayRef<TemplateParameterList *> TPLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000684};
685
James Dennettb8973efb2018-02-02 20:22:29 +0000686/// Represents a ValueDecl that came out of a declarator.
John McCallbcd03502009-12-07 02:54:59 +0000687/// Contains type source information through TypeSourceInfo.
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000688class DeclaratorDecl : public ValueDecl {
John McCall3e11ebe2010-03-15 10:12:16 +0000689 // A struct representing both a TInfo and a syntactic qualifier,
690 // to be used for the (uncommon) case of out-of-line declarations.
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000691 struct ExtInfo : public QualifierInfo {
John McCall3e11ebe2010-03-15 10:12:16 +0000692 TypeSourceInfo *TInfo;
John McCall3e11ebe2010-03-15 10:12:16 +0000693 };
694
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000695 llvm::PointerUnion<TypeSourceInfo *, ExtInfo *> DeclInfo;
John McCall3e11ebe2010-03-15 10:12:16 +0000696
James Dennett31a57342018-02-02 21:38:22 +0000697 /// The start of the source range for this declaration,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000698 /// ignoring outer template declarations.
699 SourceLocation InnerLocStart;
700
John McCall3e11ebe2010-03-15 10:12:16 +0000701 bool hasExtInfo() const { return DeclInfo.is<ExtInfo*>(); }
702 ExtInfo *getExtInfo() { return DeclInfo.get<ExtInfo*>(); }
703 const ExtInfo *getExtInfo() const { return DeclInfo.get<ExtInfo*>(); }
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000704
705protected:
706 DeclaratorDecl(Kind DK, DeclContext *DC, SourceLocation L,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000707 DeclarationName N, QualType T, TypeSourceInfo *TInfo,
708 SourceLocation StartL)
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000709 : ValueDecl(DK, DC, L, N, T), DeclInfo(TInfo), InnerLocStart(StartL) {}
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000710
711public:
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000712 friend class ASTDeclReader;
713 friend class ASTDeclWriter;
714
John McCall3e11ebe2010-03-15 10:12:16 +0000715 TypeSourceInfo *getTypeSourceInfo() const {
716 return hasExtInfo()
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000717 ? getExtInfo()->TInfo
John McCall3e11ebe2010-03-15 10:12:16 +0000718 : DeclInfo.get<TypeSourceInfo*>();
719 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000720
John McCall3e11ebe2010-03-15 10:12:16 +0000721 void setTypeSourceInfo(TypeSourceInfo *TI) {
722 if (hasExtInfo())
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000723 getExtInfo()->TInfo = TI;
John McCall3e11ebe2010-03-15 10:12:16 +0000724 else
725 DeclInfo = TI;
726 }
727
James Dennett31a57342018-02-02 21:38:22 +0000728 /// Return start of source range ignoring outer template declarations.
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000729 SourceLocation getInnerLocStart() const { return InnerLocStart; }
Abramo Bagnaradff19302011-03-08 08:55:46 +0000730 void setInnerLocStart(SourceLocation L) { InnerLocStart = L; }
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000731
James Dennett31a57342018-02-02 21:38:22 +0000732 /// Return start of source range taking into account any outer template
733 /// declarations.
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000734 SourceLocation getOuterLocStart() const;
Abramo Bagnaraea947882011-03-08 16:41:52 +0000735
Craig Toppercbce6e92014-03-11 06:22:39 +0000736 SourceRange getSourceRange() const override LLVM_READONLY;
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000737
Daniel Dunbare4775e12012-03-09 19:35:29 +0000738 SourceLocation getLocStart() const LLVM_READONLY {
Daniel Dunbarcdf295c2012-03-09 15:39:24 +0000739 return getOuterLocStart();
740 }
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000741
James Dennettb8973efb2018-02-02 20:22:29 +0000742 /// Retrieve the nested-name-specifier that qualifies the name of this
Douglas Gregor14454802011-02-25 02:25:35 +0000743 /// declaration, if it was present in the source.
John McCall3e11ebe2010-03-15 10:12:16 +0000744 NestedNameSpecifier *getQualifier() const {
Douglas Gregor14454802011-02-25 02:25:35 +0000745 return hasExtInfo() ? getExtInfo()->QualifierLoc.getNestedNameSpecifier()
Craig Topper34b4c432014-05-06 06:48:52 +0000746 : nullptr;
John McCall3e11ebe2010-03-15 10:12:16 +0000747 }
David Blaikie21bfbf82011-11-09 06:07:30 +0000748
James Dennettb8973efb2018-02-02 20:22:29 +0000749 /// Retrieve the nested-name-specifier (with source-location
David Blaikie21bfbf82011-11-09 06:07:30 +0000750 /// information) that qualifies the name of this declaration, if it was
Douglas Gregor14454802011-02-25 02:25:35 +0000751 /// present in the source.
752 NestedNameSpecifierLoc getQualifierLoc() const {
753 return hasExtInfo() ? getExtInfo()->QualifierLoc
754 : NestedNameSpecifierLoc();
John McCall3e11ebe2010-03-15 10:12:16 +0000755 }
David Blaikie21bfbf82011-11-09 06:07:30 +0000756
Douglas Gregor14454802011-02-25 02:25:35 +0000757 void setQualifierInfo(NestedNameSpecifierLoc QualifierLoc);
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000758
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000759 unsigned getNumTemplateParameterLists() const {
760 return hasExtInfo() ? getExtInfo()->NumTemplParamLists : 0;
761 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000762
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000763 TemplateParameterList *getTemplateParameterList(unsigned index) const {
764 assert(index < getNumTemplateParameterLists());
765 return getExtInfo()->TemplParamLists[index];
766 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000767
Benjamin Kramer9cc210652015-08-05 09:40:49 +0000768 void setTemplateParameterListsInfo(ASTContext &Context,
769 ArrayRef<TemplateParameterList *> TPLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000770
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000771 SourceLocation getTypeSpecStartLoc() const;
772
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000773 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +0000774 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +0000775 static bool classofKind(Kind K) {
Alexis Hunted053252010-05-30 07:21:58 +0000776 return K >= firstDeclarator && K <= lastDeclarator;
John McCall180ef092010-01-29 01:45:37 +0000777 }
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000778};
779
James Dennettb8973efb2018-02-02 20:22:29 +0000780/// Structure used to store a statement, the constant value to
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000781/// which it was evaluated (if any), and whether or not the statement
782/// is an integral constant expression (if known).
783struct EvaluatedStmt {
James Dennettb8973efb2018-02-02 20:22:29 +0000784 /// Whether this statement was already evaluated.
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000785 bool WasEvaluated : 1;
786
James Dennettb8973efb2018-02-02 20:22:29 +0000787 /// Whether this statement is being evaluated.
Eli Friedman1d6fb162009-12-03 20:31:57 +0000788 bool IsEvaluating : 1;
789
James Dennettb8973efb2018-02-02 20:22:29 +0000790 /// Whether we already checked whether this statement was an
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000791 /// integral constant expression.
792 bool CheckedICE : 1;
793
James Dennettb8973efb2018-02-02 20:22:29 +0000794 /// Whether we are checking whether this statement is an
Eli Friedman1d6fb162009-12-03 20:31:57 +0000795 /// integral constant expression.
796 bool CheckingICE : 1;
797
James Dennettb8973efb2018-02-02 20:22:29 +0000798 /// Whether this statement is an integral constant expression,
Richard Smithd0b4dd62011-12-19 06:19:21 +0000799 /// or in C++11, whether the statement is a constant expression. Only
800 /// valid if CheckedICE is true.
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000801 bool IsICE : 1;
802
803 Stmt *Value;
804 APValue Evaluated;
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000805
806 EvaluatedStmt() : WasEvaluated(false), IsEvaluating(false), CheckedICE(false),
807 CheckingICE(false), IsICE(false) {}
808
Douglas Gregor31cf12c2009-05-26 18:54:04 +0000809};
810
James Dennett31a57342018-02-02 21:38:22 +0000811/// Represents a variable declaration or definition.
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +0000812class VarDecl : public DeclaratorDecl, public Redeclarable<VarDecl> {
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000813public:
James Dennettb8973efb2018-02-02 20:22:29 +0000814 /// Initialization styles.
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000815 enum InitializationStyle {
816 /// C-style initialization with assignment
817 CInit,
818
819 /// Call-style initialization (C++98)
820 CallInit,
821
822 /// Direct list-initialization (C++11)
823 ListInit
824 };
825
James Dennettb8973efb2018-02-02 20:22:29 +0000826 /// Kinds of thread-local storage.
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000827 enum TLSKind {
828 /// Not a TLS variable.
829 TLS_None,
830
831 /// TLS with a known-constant initializer.
832 TLS_Static,
833
834 /// TLS with a dynamic initializer.
835 TLS_Dynamic
836 };
837
James Dennett31a57342018-02-02 21:38:22 +0000838 /// Return the string used to specify the storage class \p SC.
Daniel Dunbarb76b74572009-04-14 02:08:49 +0000839 ///
840 /// It is illegal to call this function with SC == None.
841 static const char *getStorageClassSpecifierString(StorageClass SC);
842
Douglas Gregord12df6b2009-07-20 22:03:28 +0000843protected:
Chandler Carruth813faed2015-12-30 02:51:00 +0000844 // A pointer union of Stmt * and EvaluatedStmt *. When an EvaluatedStmt, we
Justin Lebar41e9dad2016-07-29 05:26:58 +0000845 // have allocated the auxiliary struct of information there.
Chandler Carruth813faed2015-12-30 02:51:00 +0000846 //
847 // TODO: It is a bit unfortunate to use a PointerUnion inside the VarDecl for
848 // this as *many* VarDecls are ParmVarDecls that don't have default
849 // arguments. We could save some space by moving this pointer union to be
850 // allocated in trailing space when necessary.
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000851 using InitType = llvm::PointerUnion<Stmt *, EvaluatedStmt *>;
Mike Stump11289f42009-09-09 15:08:12 +0000852
James Dennettb8973efb2018-02-02 20:22:29 +0000853 /// The initializer for this variable or, for a ParmVarDecl, the
Douglas Gregord12df6b2009-07-20 22:03:28 +0000854 /// C++ default argument.
Douglas Gregor770a5322009-09-01 16:13:00 +0000855 mutable InitType Init;
Mike Stump11289f42009-09-09 15:08:12 +0000856
Chris Lattner4b08ca82008-03-15 21:10:16 +0000857private:
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000858 friend class ASTDeclReader;
859 friend class ASTNodeImporter;
860 friend class StmtIteratorBase;
861
John McCallbeaa11c2011-05-01 02:13:58 +0000862 class VarDeclBitfields {
John McCallbeaa11c2011-05-01 02:13:58 +0000863 friend class ASTDeclReader;
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000864 friend class VarDecl;
Douglas Gregor85970ca2008-12-10 23:01:14 +0000865
John McCallbeaa11c2011-05-01 02:13:58 +0000866 unsigned SClass : 3;
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +0000867 unsigned TSCSpec : 2;
Sebastian Redla9351792012-02-11 23:51:47 +0000868 unsigned InitStyle : 2;
John McCallbeaa11c2011-05-01 02:13:58 +0000869 };
David Majnemerfa7bc782015-05-19 00:57:16 +0000870 enum { NumVarDeclBits = 7 };
Richard Smith02e85f32011-04-14 22:09:26 +0000871
Chris Lattner4b08ca82008-03-15 21:10:16 +0000872protected:
Ted Kremenek9504ae92011-10-06 04:19:35 +0000873 enum { NumParameterIndexBits = 8 };
David Blaikie21bfbf82011-11-09 06:07:30 +0000874
Chandler Carruth813faed2015-12-30 02:51:00 +0000875 enum DefaultArgKind {
876 DAK_None,
877 DAK_Unparsed,
878 DAK_Uninstantiated,
879 DAK_Normal
880 };
881
John McCallbeaa11c2011-05-01 02:13:58 +0000882 class ParmVarDeclBitfields {
John McCallbeaa11c2011-05-01 02:13:58 +0000883 friend class ASTDeclReader;
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000884 friend class ParmVarDecl;
John McCallbeaa11c2011-05-01 02:13:58 +0000885
886 unsigned : NumVarDeclBits;
John McCallbeaa11c2011-05-01 02:13:58 +0000887
888 /// Whether this parameter inherits a default argument from a
889 /// prior declaration.
890 unsigned HasInheritedDefaultArg : 1;
891
Chandler Carruth813faed2015-12-30 02:51:00 +0000892 /// Describes the kind of default argument for this parameter. By default
893 /// this is none. If this is normal, then the default argument is stored in
Simon Pilgrim750bde62017-03-31 11:00:53 +0000894 /// the \c VarDecl initializer expression unless we were unable to parse
Chandler Carruth813faed2015-12-30 02:51:00 +0000895 /// (even an invalid) expression for the default argument.
896 unsigned DefaultArgKind : 2;
897
John McCallbeaa11c2011-05-01 02:13:58 +0000898 /// Whether this parameter undergoes K&R argument promotion.
899 unsigned IsKNRPromoted : 1;
John McCall82490832011-05-02 00:30:12 +0000900
901 /// Whether this parameter is an ObjC method parameter or not.
902 unsigned IsObjCMethodParam : 1;
903
904 /// If IsObjCMethodParam, a Decl::ObjCDeclQualifier.
905 /// Otherwise, the number of function parameter scopes enclosing
906 /// the function parameter scope in which this parameter was
907 /// declared.
Sebastian Redla9351792012-02-11 23:51:47 +0000908 unsigned ScopeDepthOrObjCQuals : 7;
John McCall82490832011-05-02 00:30:12 +0000909
910 /// The number of parameters preceding this parameter in the
911 /// function parameter scope in which it was declared.
Ted Kremenek9504ae92011-10-06 04:19:35 +0000912 unsigned ParameterIndex : NumParameterIndexBits;
John McCallbeaa11c2011-05-01 02:13:58 +0000913 };
914
David Majnemerfa7bc782015-05-19 00:57:16 +0000915 class NonParmVarDeclBitfields {
David Majnemerfa7bc782015-05-19 00:57:16 +0000916 friend class ASTDeclReader;
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000917 friend class ImplicitParamDecl;
918 friend class VarDecl;
David Majnemerfa7bc782015-05-19 00:57:16 +0000919
920 unsigned : NumVarDeclBits;
921
Richard Smithedbc6e92016-10-14 21:41:24 +0000922 // FIXME: We need something similar to CXXRecordDecl::DefinitionData.
James Dennettb8973efb2018-02-02 20:22:29 +0000923 /// Whether this variable is a definition which was demoted due to
Richard Smithedbc6e92016-10-14 21:41:24 +0000924 /// module merge.
925 unsigned IsThisDeclarationADemotedDefinition : 1;
926
James Dennettb8973efb2018-02-02 20:22:29 +0000927 /// Whether this variable is the exception variable in a C++ catch
David Majnemerfa7bc782015-05-19 00:57:16 +0000928 /// or an Objective-C @catch statement.
929 unsigned ExceptionVar : 1;
930
James Dennettb8973efb2018-02-02 20:22:29 +0000931 /// Whether this local variable could be allocated in the return
David Majnemerfa7bc782015-05-19 00:57:16 +0000932 /// slot of its function, enabling the named return value optimization
933 /// (NRVO).
934 unsigned NRVOVariable : 1;
935
James Dennettb8973efb2018-02-02 20:22:29 +0000936 /// Whether this variable is the for-range-declaration in a C++0x
David Majnemerfa7bc782015-05-19 00:57:16 +0000937 /// for-range statement.
938 unsigned CXXForRangeDecl : 1;
939
James Dennettb8973efb2018-02-02 20:22:29 +0000940 /// Whether this variable is an ARC pseudo-__strong
David Majnemerfa7bc782015-05-19 00:57:16 +0000941 /// variable; see isARCPseudoStrong() for details.
942 unsigned ARCPseudoStrong : 1;
943
James Dennettb8973efb2018-02-02 20:22:29 +0000944 /// Whether this variable is (C++1z) inline.
Richard Smith62f19e72016-06-25 00:15:56 +0000945 unsigned IsInline : 1;
946
James Dennettb8973efb2018-02-02 20:22:29 +0000947 /// Whether this variable has (C++1z) inline explicitly specified.
Richard Smith62f19e72016-06-25 00:15:56 +0000948 unsigned IsInlineSpecified : 1;
949
James Dennettb8973efb2018-02-02 20:22:29 +0000950 /// Whether this variable is (C++0x) constexpr.
David Majnemerfa7bc782015-05-19 00:57:16 +0000951 unsigned IsConstexpr : 1;
952
James Dennettb8973efb2018-02-02 20:22:29 +0000953 /// Whether this variable is the implicit variable for a lambda
David Majnemerfa7bc782015-05-19 00:57:16 +0000954 /// init-capture.
955 unsigned IsInitCapture : 1;
956
James Dennettb8973efb2018-02-02 20:22:29 +0000957 /// Whether this local extern variable's previous declaration was
David Majnemerfa7bc782015-05-19 00:57:16 +0000958 /// declared in the same block scope. This controls whether we should merge
959 /// the type of this declaration with its previous declaration.
960 unsigned PreviousDeclInSameBlockScope : 1;
Alexey Bataev56223232017-06-09 13:40:18 +0000961
962 /// Defines kind of the ImplicitParamDecl: 'this', 'self', 'vtt', '_cmd' or
963 /// something else.
964 unsigned ImplicitParamKind : 3;
David Majnemerfa7bc782015-05-19 00:57:16 +0000965 };
966
John McCallbeaa11c2011-05-01 02:13:58 +0000967 union {
968 unsigned AllBits;
969 VarDeclBitfields VarDeclBits;
970 ParmVarDeclBitfields ParmVarDeclBits;
David Majnemerfa7bc782015-05-19 00:57:16 +0000971 NonParmVarDeclBitfields NonParmVarDeclBits;
John McCallbeaa11c2011-05-01 02:13:58 +0000972 };
973
Richard Smith053f6c62014-05-16 23:01:30 +0000974 VarDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
Larisse Voufo39a1e502013-08-06 01:03:05 +0000975 SourceLocation IdLoc, IdentifierInfo *Id, QualType T,
976 TypeSourceInfo *TInfo, StorageClass SC);
Argyrios Kyrtzidis05898da2009-07-18 08:50:35 +0000977
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000978 using redeclarable_base = Redeclarable<VarDecl>;
979
Richard Smithd7af8a32014-05-10 01:17:36 +0000980 VarDecl *getNextRedeclarationImpl() override {
981 return getNextRedeclaration();
982 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000983
Craig Toppercbce6e92014-03-11 06:22:39 +0000984 VarDecl *getPreviousDeclImpl() override {
Douglas Gregorec9fd132012-01-14 16:38:05 +0000985 return getPreviousDecl();
986 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000987
Craig Toppercbce6e92014-03-11 06:22:39 +0000988 VarDecl *getMostRecentDeclImpl() override {
Douglas Gregorec9fd132012-01-14 16:38:05 +0000989 return getMostRecentDecl();
990 }
Douglas Gregorb11aad82011-02-19 18:51:44 +0000991
Chris Lattner4b08ca82008-03-15 21:10:16 +0000992public:
Eugene Zelenkode0215c2017-11-13 23:01:27 +0000993 using redecl_range = redeclarable_base::redecl_range;
994 using redecl_iterator = redeclarable_base::redecl_iterator;
995
Aaron Ballman211cd8c2014-03-07 00:10:58 +0000996 using redeclarable_base::redecls_begin;
997 using redeclarable_base::redecls_end;
Aaron Ballman86c93902014-03-06 23:45:36 +0000998 using redeclarable_base::redecls;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000999 using redeclarable_base::getPreviousDecl;
1000 using redeclarable_base::getMostRecentDecl;
Rafael Espindola3f9e4442013-10-19 02:13:21 +00001001 using redeclarable_base::isFirstDecl;
Douglas Gregor0bc8a212012-01-14 15:55:47 +00001002
Chris Lattnerbec41342008-04-22 18:39:57 +00001003 static VarDecl *Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001004 SourceLocation StartLoc, SourceLocation IdLoc,
1005 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001006 StorageClass S);
Nuno Lopes394ec982008-12-17 23:39:55 +00001007
Douglas Gregor72172e92012-01-05 21:55:30 +00001008 static VarDecl *CreateDeserialized(ASTContext &C, unsigned ID);
Craig Toppercbce6e92014-03-11 06:22:39 +00001009
1010 SourceRange getSourceRange() const override LLVM_READONLY;
Nuno Lopes394ec982008-12-17 23:39:55 +00001011
James Dennettb8973efb2018-02-02 20:22:29 +00001012 /// Returns the storage class as written in the source. For the
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001013 /// computed linkage of symbol, see getLinkage.
John McCallbeaa11c2011-05-01 02:13:58 +00001014 StorageClass getStorageClass() const {
1015 return (StorageClass) VarDeclBits.SClass;
1016 }
Douglas Gregorbf62d642010-12-06 18:36:25 +00001017 void setStorageClass(StorageClass SC);
Mike Stump11289f42009-09-09 15:08:12 +00001018
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +00001019 void setTSCSpec(ThreadStorageClassSpecifier TSC) {
1020 VarDeclBits.TSCSpec = TSC;
Reid Kleckner7d6d2702014-05-01 03:16:47 +00001021 assert(VarDeclBits.TSCSpec == TSC && "truncation");
Enea Zaffanellaacb8ecd2013-05-04 08:27:07 +00001022 }
1023 ThreadStorageClassSpecifier getTSCSpec() const {
1024 return static_cast<ThreadStorageClassSpecifier>(VarDeclBits.TSCSpec);
1025 }
Reid Kleckner7d6d2702014-05-01 03:16:47 +00001026 TLSKind getTLSKind() const;
Sebastian Redl833ef452010-01-26 22:01:41 +00001027
James Dennett31a57342018-02-02 21:38:22 +00001028 /// Returns true if a variable with function scope is a non-static local
1029 /// variable.
Sebastian Redl833ef452010-01-26 22:01:41 +00001030 bool hasLocalStorage() const {
Yaxun Liu4f33b3d2017-05-15 14:47:47 +00001031 if (getStorageClass() == SC_None) {
1032 // OpenCL v1.2 s6.5.3: The __constant or constant address space name is
1033 // used to describe variables allocated in global memory and which are
1034 // accessed inside a kernel(s) as read-only variables. As such, variables
1035 // in constant address space cannot have local storage.
1036 if (getType().getAddressSpace() == LangAS::opencl_constant)
1037 return false;
Enea Zaffanella28f36ba2013-05-10 20:34:44 +00001038 // Second check is for C++11 [dcl.stc]p4.
Richard Smithe6c01442013-06-05 00:46:14 +00001039 return !isFileVarDecl() && getTSCSpec() == TSCS_unspecified;
Yaxun Liu4f33b3d2017-05-15 14:47:47 +00001040 }
Sebastian Redl833ef452010-01-26 22:01:41 +00001041
Renato Golin230c5eb2014-05-19 18:15:42 +00001042 // Global Named Register (GNU extension)
Anna Zaksa3d95342015-02-04 07:15:12 +00001043 if (getStorageClass() == SC_Register && !isLocalVarDeclOrParm())
Renato Golin230c5eb2014-05-19 18:15:42 +00001044 return false;
1045
Sebastian Redl833ef452010-01-26 22:01:41 +00001046 // Return true for: Auto, Register.
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001047 // Return false for: Extern, Static, PrivateExtern, OpenCLWorkGroupLocal.
Sebastian Redl833ef452010-01-26 22:01:41 +00001048
John McCall8e7d6562010-08-26 03:08:43 +00001049 return getStorageClass() >= SC_Auto;
Sebastian Redl833ef452010-01-26 22:01:41 +00001050 }
1051
James Dennett31a57342018-02-02 21:38:22 +00001052 /// Returns true if a variable with function scope is a static local
1053 /// variable.
Fariborz Jahanian3fef72f2010-04-18 21:01:23 +00001054 bool isStaticLocal() const {
Enea Zaffanella28f36ba2013-05-10 20:34:44 +00001055 return (getStorageClass() == SC_Static ||
1056 // C++11 [dcl.stc]p4
1057 (getStorageClass() == SC_None && getTSCSpec() == TSCS_thread_local))
1058 && !isFileVarDecl();
Fariborz Jahanian3fef72f2010-04-18 21:01:23 +00001059 }
David Blaikie21bfbf82011-11-09 06:07:30 +00001060
James Dennettb8973efb2018-02-02 20:22:29 +00001061 /// Returns true if a variable has extern or __private_extern__
Rafael Espindola2199c122013-03-07 02:43:24 +00001062 /// storage.
Sebastian Redl833ef452010-01-26 22:01:41 +00001063 bool hasExternalStorage() const {
John McCall8e7d6562010-08-26 03:08:43 +00001064 return getStorageClass() == SC_Extern ||
1065 getStorageClass() == SC_PrivateExtern;
Sebastian Redl833ef452010-01-26 22:01:41 +00001066 }
1067
James Dennettb8973efb2018-02-02 20:22:29 +00001068 /// Returns true for all variables that do not have local storage.
James Dennett42c4e502013-11-05 19:37:38 +00001069 ///
1070 /// This includes all global variables as well as static variables declared
1071 /// within a function.
Sebastian Redl833ef452010-01-26 22:01:41 +00001072 bool hasGlobalStorage() const { return !hasLocalStorage(); }
1073
James Dennettb8973efb2018-02-02 20:22:29 +00001074 /// Get the storage duration of this variable, per C++ [basic.stc].
Richard Smithe6c01442013-06-05 00:46:14 +00001075 StorageDuration getStorageDuration() const {
1076 return hasLocalStorage() ? SD_Automatic :
1077 getTSCSpec() ? SD_Thread : SD_Static;
1078 }
1079
James Dennettb8973efb2018-02-02 20:22:29 +00001080 /// Compute the language linkage.
Rafael Espindolaf4187652013-02-14 01:18:37 +00001081 LanguageLinkage getLanguageLinkage() const;
1082
James Dennettb8973efb2018-02-02 20:22:29 +00001083 /// Determines whether this variable is a variable with external, C linkage.
Rafael Espindola0e0d0092013-03-14 03:07:35 +00001084 bool isExternC() const;
Rafael Espindola576127d2012-12-28 14:21:58 +00001085
James Dennettb8973efb2018-02-02 20:22:29 +00001086 /// Determines whether this variable's context is, or is nested within,
Rafael Espindola593537a2013-05-05 20:15:21 +00001087 /// a C++ extern "C" linkage spec.
1088 bool isInExternCContext() const;
1089
James Dennettb8973efb2018-02-02 20:22:29 +00001090 /// Determines whether this variable's context is, or is nested within,
Rafael Espindola593537a2013-05-05 20:15:21 +00001091 /// a C++ extern "C++" linkage spec.
1092 bool isInExternCXXContext() const;
1093
James Dennettb8973efb2018-02-02 20:22:29 +00001094 /// Returns true for local variable declarations other than parameters.
1095 /// Note that this includes static variables inside of functions. It also
1096 /// includes variables inside blocks.
Sebastian Redl833ef452010-01-26 22:01:41 +00001097 ///
1098 /// void foo() { int x; static int y; extern int z; }
John McCall1c9c3fd2010-10-15 04:57:14 +00001099 bool isLocalVarDecl() const {
Richard Smith7873de02016-08-11 22:25:46 +00001100 if (getKind() != Decl::Var && getKind() != Decl::Decomposition)
Sebastian Redl833ef452010-01-26 22:01:41 +00001101 return false;
Richard Smith541b38b2013-09-20 01:15:31 +00001102 if (const DeclContext *DC = getLexicalDeclContext())
Sebastian Redl50c68252010-08-31 00:36:30 +00001103 return DC->getRedeclContext()->isFunctionOrMethod();
Sebastian Redl833ef452010-01-26 22:01:41 +00001104 return false;
1105 }
1106
James Dennettb8973efb2018-02-02 20:22:29 +00001107 /// Similar to isLocalVarDecl but also includes parameters.
David Majnemera3b04ce2015-01-14 00:31:13 +00001108 bool isLocalVarDeclOrParm() const {
1109 return isLocalVarDecl() || getKind() == Decl::ParmVar;
1110 }
1111
James Dennettb8973efb2018-02-02 20:22:29 +00001112 /// Similar to isLocalVarDecl, but excludes variables declared in blocks.
Fariborz Jahanian3a106e72010-03-11 18:20:03 +00001113 bool isFunctionOrMethodVarDecl() const {
Richard Smith7873de02016-08-11 22:25:46 +00001114 if (getKind() != Decl::Var && getKind() != Decl::Decomposition)
Fariborz Jahanian3a106e72010-03-11 18:20:03 +00001115 return false;
Richard Smith541b38b2013-09-20 01:15:31 +00001116 const DeclContext *DC = getLexicalDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +00001117 return DC->isFunctionOrMethod() && DC->getDeclKind() != Decl::Block;
Fariborz Jahanian3a106e72010-03-11 18:20:03 +00001118 }
1119
James Dennettb8973efb2018-02-02 20:22:29 +00001120 /// Determines whether this is a static data member.
Sebastian Redl833ef452010-01-26 22:01:41 +00001121 ///
1122 /// This will only be true in C++, and applies to, e.g., the
1123 /// variable 'x' in:
1124 /// \code
1125 /// struct S {
1126 /// static int x;
1127 /// };
1128 /// \endcode
1129 bool isStaticDataMember() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001130 // If it wasn't static, it would be a FieldDecl.
Argyrios Kyrtzidis3816ed42010-07-19 10:14:41 +00001131 return getKind() != Decl::ParmVar && getDeclContext()->isRecord();
Sebastian Redl833ef452010-01-26 22:01:41 +00001132 }
1133
Craig Toppercbce6e92014-03-11 06:22:39 +00001134 VarDecl *getCanonicalDecl() override;
Sebastian Redl833ef452010-01-26 22:01:41 +00001135 const VarDecl *getCanonicalDecl() const {
1136 return const_cast<VarDecl*>(this)->getCanonicalDecl();
1137 }
1138
Sebastian Redl35351a92010-01-31 22:27:38 +00001139 enum DefinitionKind {
Eugene Zelenkode0215c2017-11-13 23:01:27 +00001140 /// This declaration is only a declaration.
1141 DeclarationOnly,
1142
1143 /// This declaration is a tentative definition.
1144 TentativeDefinition,
1145
1146 /// This declaration is definitely a definition.
1147 Definition
Sebastian Redl35351a92010-01-31 22:27:38 +00001148 };
1149
James Dennettb8973efb2018-02-02 20:22:29 +00001150 /// Check whether this declaration is a definition. If this could be
Sebastian Redl35351a92010-01-31 22:27:38 +00001151 /// a tentative definition (in C), don't check whether there's an overriding
1152 /// definition.
Daniel Dunbar9d355812012-03-09 01:51:51 +00001153 DefinitionKind isThisDeclarationADefinition(ASTContext &) const;
1154 DefinitionKind isThisDeclarationADefinition() const {
1155 return isThisDeclarationADefinition(getASTContext());
1156 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001157
James Dennettb8973efb2018-02-02 20:22:29 +00001158 /// Check whether this variable is defined in this translation unit.
Daniel Dunbar9d355812012-03-09 01:51:51 +00001159 DefinitionKind hasDefinition(ASTContext &) const;
1160 DefinitionKind hasDefinition() const {
1161 return hasDefinition(getASTContext());
1162 }
John McCall37bb6c92010-10-29 22:22:43 +00001163
James Dennettb8973efb2018-02-02 20:22:29 +00001164 /// Get the tentative definition that acts as the real definition in a TU.
1165 /// Returns null if there is a proper definition available.
Sebastian Redl35351a92010-01-31 22:27:38 +00001166 VarDecl *getActingDefinition();
Sebastian Redl5ca79842010-02-01 20:16:42 +00001167 const VarDecl *getActingDefinition() const {
1168 return const_cast<VarDecl*>(this)->getActingDefinition();
1169 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001170
James Dennettb8973efb2018-02-02 20:22:29 +00001171 /// Get the real (not just tentative) definition for this declaration.
Daniel Dunbar9d355812012-03-09 01:51:51 +00001172 VarDecl *getDefinition(ASTContext &);
1173 const VarDecl *getDefinition(ASTContext &C) const {
1174 return const_cast<VarDecl*>(this)->getDefinition(C);
1175 }
1176 VarDecl *getDefinition() {
1177 return getDefinition(getASTContext());
1178 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001179 const VarDecl *getDefinition() const {
1180 return const_cast<VarDecl*>(this)->getDefinition();
Sebastian Redl833ef452010-01-26 22:01:41 +00001181 }
1182
James Dennettb8973efb2018-02-02 20:22:29 +00001183 /// Determine whether this is or was instantiated from an out-of-line
Sebastian Redl833ef452010-01-26 22:01:41 +00001184 /// definition of a static data member.
Craig Toppercbce6e92014-03-11 06:22:39 +00001185 bool isOutOfLine() const override;
Sebastian Redl833ef452010-01-26 22:01:41 +00001186
James Dennettb8973efb2018-02-02 20:22:29 +00001187 /// Returns true for file scoped variable declaration.
Sebastian Redl833ef452010-01-26 22:01:41 +00001188 bool isFileVarDecl() const {
Larisse Voufo39a1e502013-08-06 01:03:05 +00001189 Kind K = getKind();
1190 if (K == ParmVar || K == ImplicitParam)
Sebastian Redl833ef452010-01-26 22:01:41 +00001191 return false;
David Blaikie21bfbf82011-11-09 06:07:30 +00001192
Richard Smith541b38b2013-09-20 01:15:31 +00001193 if (getLexicalDeclContext()->getRedeclContext()->isFileContext())
Douglas Gregorfcee9462010-08-27 22:55:10 +00001194 return true;
David Blaikie21bfbf82011-11-09 06:07:30 +00001195
Sebastian Redl833ef452010-01-26 22:01:41 +00001196 if (isStaticDataMember())
1197 return true;
1198
1199 return false;
1200 }
1201
James Dennettb8973efb2018-02-02 20:22:29 +00001202 /// Get the initializer for this variable, no matter which
Sebastian Redl5ca79842010-02-01 20:16:42 +00001203 /// declaration it is attached to.
1204 const Expr *getAnyInitializer() const {
1205 const VarDecl *D;
1206 return getAnyInitializer(D);
1207 }
1208
James Dennettb8973efb2018-02-02 20:22:29 +00001209 /// Get the initializer for this variable, no matter which
Sebastian Redl5ca79842010-02-01 20:16:42 +00001210 /// declaration it is attached to. Also get that declaration.
1211 const Expr *getAnyInitializer(const VarDecl *&D) const;
1212
Chandler Carruth813faed2015-12-30 02:51:00 +00001213 bool hasInit() const;
Mike Stump11289f42009-09-09 15:08:12 +00001214 const Expr *getInit() const {
Chandler Carruth813faed2015-12-30 02:51:00 +00001215 return const_cast<VarDecl *>(this)->getInit();
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001216 }
Chandler Carruth813faed2015-12-30 02:51:00 +00001217 Expr *getInit();
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001218
James Dennettb8973efb2018-02-02 20:22:29 +00001219 /// Retrieve the address of the initializer expression.
Chandler Carruth813faed2015-12-30 02:51:00 +00001220 Stmt **getInitAddress();
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001221
Douglas Gregord5058122010-02-11 01:19:42 +00001222 void setInit(Expr *I);
Mike Stump11289f42009-09-09 15:08:12 +00001223
James Dennettb8973efb2018-02-02 20:22:29 +00001224 /// Determine whether this variable's value can be used in a
Richard Smith242ad892011-12-21 02:55:12 +00001225 /// constant expression, according to the relevant language standard.
1226 /// This only checks properties of the declaration, and does not check
1227 /// whether the initializer is in fact a constant expression.
Daniel Dunbar9d355812012-03-09 01:51:51 +00001228 bool isUsableInConstantExpressions(ASTContext &C) const;
Richard Smith242ad892011-12-21 02:55:12 +00001229
Richard Smithd0b4dd62011-12-19 06:19:21 +00001230 EvaluatedStmt *ensureEvaluatedStmt() const;
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001231
Richard Smithd0b4dd62011-12-19 06:19:21 +00001232 /// \brief Attempt to evaluate the value of the initializer attached to this
1233 /// declaration, and produce notes explaining why it cannot be evaluated or is
Richard Smithdafff942012-01-14 04:30:29 +00001234 /// not a constant expression. Returns a pointer to the value if evaluation
1235 /// succeeded, 0 otherwise.
1236 APValue *evaluateValue() const;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001237 APValue *evaluateValue(SmallVectorImpl<PartialDiagnosticAt> &Notes) const;
Mike Stump11289f42009-09-09 15:08:12 +00001238
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001239 /// \brief Return the already-evaluated value of this variable's
Eli Friedman1d6fb162009-12-03 20:31:57 +00001240 /// initializer, or NULL if the value is not yet known. Returns pointer
1241 /// to untyped APValue if the value could not be evaluated.
Chandler Carruth813faed2015-12-30 02:51:00 +00001242 APValue *getEvaluatedValue() const;
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001243
1244 /// \brief Determines whether it is already known whether the
1245 /// initializer is an integral constant expression or not.
Chandler Carruth813faed2015-12-30 02:51:00 +00001246 bool isInitKnownICE() const;
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001247
Richard Smithd0b4dd62011-12-19 06:19:21 +00001248 /// \brief Determines whether the initializer is an integral constant
1249 /// expression, or in C++11, whether the initializer is a constant
1250 /// expression.
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001251 ///
1252 /// \pre isInitKnownICE()
Chandler Carruth813faed2015-12-30 02:51:00 +00001253 bool isInitICE() const;
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001254
Richard Smithd0b4dd62011-12-19 06:19:21 +00001255 /// \brief Determine whether the value of the initializer attached to this
1256 /// declaration is an integral constant expression.
1257 bool checkInitIsICE() const;
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001258
Sebastian Redla9351792012-02-11 23:51:47 +00001259 void setInitStyle(InitializationStyle Style) {
1260 VarDeclBits.InitStyle = Style;
1261 }
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001262
Sebastian Redla9351792012-02-11 23:51:47 +00001263 /// \brief The style of initialization for this declaration.
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001264 ///
Sebastian Redla9351792012-02-11 23:51:47 +00001265 /// C-style initialization is "int x = 1;". Call-style initialization is
1266 /// a C++98 direct-initializer, e.g. "int x(1);". The Init expression will be
1267 /// the expression inside the parens or a "ClassType(a,b,c)" class constructor
1268 /// expression for class types. List-style initialization is C++11 syntax,
1269 /// e.g. "int x{1};". Clients can distinguish between different forms of
1270 /// initialization by checking this value. In particular, "int x = {1};" is
1271 /// C-style, "int x({1})" is call-style, and "int x{1};" is list-style; the
1272 /// Init expression in all three cases is an InitListExpr.
1273 InitializationStyle getInitStyle() const {
1274 return static_cast<InitializationStyle>(VarDeclBits.InitStyle);
1275 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +00001276
Sebastian Redla9351792012-02-11 23:51:47 +00001277 /// \brief Whether the initializer is a direct-initializer (list or call).
1278 bool isDirectInit() const {
1279 return getInitStyle() != CInit;
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001280 }
Mike Stump11289f42009-09-09 15:08:12 +00001281
Richard Smithedbc6e92016-10-14 21:41:24 +00001282 /// \brief If this definition should pretend to be a declaration.
1283 bool isThisDeclarationADemotedDefinition() const {
1284 return isa<ParmVarDecl>(this) ? false :
1285 NonParmVarDeclBits.IsThisDeclarationADemotedDefinition;
1286 }
1287
1288 /// \brief This is a definition which should be demoted to a declaration.
1289 ///
1290 /// In some cases (mostly module merging) we can end up with two visible
1291 /// definitions one of which needs to be demoted to a declaration to keep
1292 /// the AST invariants.
1293 void demoteThisDefinitionToDeclaration() {
Eugene Zelenkode0215c2017-11-13 23:01:27 +00001294 assert(isThisDeclarationADefinition() && "Not a definition!");
1295 assert(!isa<ParmVarDecl>(this) && "Cannot demote ParmVarDecls!");
Richard Smithedbc6e92016-10-14 21:41:24 +00001296 NonParmVarDeclBits.IsThisDeclarationADemotedDefinition = 1;
1297 }
1298
Douglas Gregor3f324d562010-05-03 18:51:14 +00001299 /// \brief Determine whether this variable is the exception variable in a
Chandler Carruth48687a52012-06-07 17:55:42 +00001300 /// C++ catch statememt or an Objective-C \@catch statement.
Douglas Gregor3f324d562010-05-03 18:51:14 +00001301 bool isExceptionVariable() const {
David Majnemerfa7bc782015-05-19 00:57:16 +00001302 return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.ExceptionVar;
Douglas Gregor3f324d562010-05-03 18:51:14 +00001303 }
David Majnemerfa7bc782015-05-19 00:57:16 +00001304 void setExceptionVariable(bool EV) {
1305 assert(!isa<ParmVarDecl>(this));
1306 NonParmVarDeclBits.ExceptionVar = EV;
1307 }
David Blaikie21bfbf82011-11-09 06:07:30 +00001308
Douglas Gregor6fd1b182010-05-15 06:01:05 +00001309 /// \brief Determine whether this local variable can be used with the named
1310 /// return value optimization (NRVO).
1311 ///
1312 /// The named return value optimization (NRVO) works by marking certain
1313 /// non-volatile local variables of class type as NRVO objects. These
1314 /// locals can be allocated within the return slot of their containing
1315 /// function, in which case there is no need to copy the object to the
1316 /// return slot when returning from the function. Within the function body,
1317 /// each return that returns the NRVO object will have this variable as its
1318 /// NRVO candidate.
David Majnemerfa7bc782015-05-19 00:57:16 +00001319 bool isNRVOVariable() const {
1320 return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.NRVOVariable;
1321 }
1322 void setNRVOVariable(bool NRVO) {
1323 assert(!isa<ParmVarDecl>(this));
1324 NonParmVarDeclBits.NRVOVariable = NRVO;
1325 }
Richard Smith02e85f32011-04-14 22:09:26 +00001326
1327 /// \brief Determine whether this variable is the for-range-declaration in
1328 /// a C++0x for-range statement.
David Majnemerfa7bc782015-05-19 00:57:16 +00001329 bool isCXXForRangeDecl() const {
1330 return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.CXXForRangeDecl;
1331 }
1332 void setCXXForRangeDecl(bool FRD) {
1333 assert(!isa<ParmVarDecl>(this));
1334 NonParmVarDeclBits.CXXForRangeDecl = FRD;
1335 }
John McCalld4631322011-06-17 06:42:21 +00001336
1337 /// \brief Determine whether this variable is an ARC pseudo-__strong
1338 /// variable. A pseudo-__strong variable has a __strong-qualified
1339 /// type but does not actually retain the object written into it.
1340 /// Generally such variables are also 'const' for safety.
David Majnemerfa7bc782015-05-19 00:57:16 +00001341 bool isARCPseudoStrong() const {
1342 return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.ARCPseudoStrong;
1343 }
1344 void setARCPseudoStrong(bool ps) {
1345 assert(!isa<ParmVarDecl>(this));
1346 NonParmVarDeclBits.ARCPseudoStrong = ps;
1347 }
David Blaikie21bfbf82011-11-09 06:07:30 +00001348
Richard Smith62f19e72016-06-25 00:15:56 +00001349 /// Whether this variable is (C++1z) inline.
1350 bool isInline() const {
1351 return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsInline;
1352 }
1353 bool isInlineSpecified() const {
1354 return isa<ParmVarDecl>(this) ? false
1355 : NonParmVarDeclBits.IsInlineSpecified;
1356 }
1357 void setInlineSpecified() {
1358 assert(!isa<ParmVarDecl>(this));
1359 NonParmVarDeclBits.IsInline = true;
1360 NonParmVarDeclBits.IsInlineSpecified = true;
1361 }
1362 void setImplicitlyInline() {
1363 assert(!isa<ParmVarDecl>(this));
1364 NonParmVarDeclBits.IsInline = true;
1365 }
1366
Richard Smith12f247f2012-06-08 21:09:22 +00001367 /// Whether this variable is (C++11) constexpr.
David Majnemerfa7bc782015-05-19 00:57:16 +00001368 bool isConstexpr() const {
1369 return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsConstexpr;
1370 }
1371 void setConstexpr(bool IC) {
1372 assert(!isa<ParmVarDecl>(this));
1373 NonParmVarDeclBits.IsConstexpr = IC;
1374 }
Richard Smitha77a0a62011-08-15 21:04:07 +00001375
Richard Smithbb13c9a2013-09-28 04:02:39 +00001376 /// Whether this variable is the implicit variable for a lambda init-capture.
David Majnemerfa7bc782015-05-19 00:57:16 +00001377 bool isInitCapture() const {
1378 return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsInitCapture;
1379 }
1380 void setInitCapture(bool IC) {
1381 assert(!isa<ParmVarDecl>(this));
1382 NonParmVarDeclBits.IsInitCapture = IC;
1383 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001384
Richard Smith1c34fb72013-08-13 18:18:50 +00001385 /// Whether this local extern variable declaration's previous declaration
1386 /// was declared in the same block scope. Only correct in C++.
1387 bool isPreviousDeclInSameBlockScope() const {
David Majnemerfa7bc782015-05-19 00:57:16 +00001388 return isa<ParmVarDecl>(this)
1389 ? false
1390 : NonParmVarDeclBits.PreviousDeclInSameBlockScope;
Richard Smith1c34fb72013-08-13 18:18:50 +00001391 }
1392 void setPreviousDeclInSameBlockScope(bool Same) {
David Majnemerfa7bc782015-05-19 00:57:16 +00001393 assert(!isa<ParmVarDecl>(this));
1394 NonParmVarDeclBits.PreviousDeclInSameBlockScope = Same;
Richard Smith1c34fb72013-08-13 18:18:50 +00001395 }
1396
Richard Smithedbc6e92016-10-14 21:41:24 +00001397 /// \brief Retrieve the variable declaration from which this variable could
1398 /// be instantiated, if it is an instantiation (rather than a non-template).
1399 VarDecl *getTemplateInstantiationPattern() const;
1400
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001401 /// \brief If this variable is an instantiated static data member of a
Mike Stump11289f42009-09-09 15:08:12 +00001402 /// class template specialization, returns the templated static data member
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001403 /// from which it was instantiated.
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001404 VarDecl *getInstantiatedFromStaticDataMember() const;
Mike Stump11289f42009-09-09 15:08:12 +00001405
Richard Smith8809a0c2013-09-27 20:14:12 +00001406 /// \brief If this variable is an instantiation of a variable template or a
1407 /// static data member of a class template, determine what kind of
Douglas Gregor86d142a2009-10-08 07:24:58 +00001408 /// template specialization or instantiation this is.
Douglas Gregor3c74d412009-10-14 20:14:33 +00001409 TemplateSpecializationKind getTemplateSpecializationKind() const;
David Blaikie21bfbf82011-11-09 06:07:30 +00001410
Richard Smith8809a0c2013-09-27 20:14:12 +00001411 /// \brief If this variable is an instantiation of a variable template or a
1412 /// static data member of a class template, determine its point of
1413 /// instantiation.
1414 SourceLocation getPointOfInstantiation() const;
1415
Douglas Gregor06db9f52009-10-12 20:18:28 +00001416 /// \brief If this variable is an instantiation of a static data member of a
1417 /// class template specialization, retrieves the member specialization
1418 /// information.
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001419 MemberSpecializationInfo *getMemberSpecializationInfo() const;
David Blaikie21bfbf82011-11-09 06:07:30 +00001420
Douglas Gregor86d142a2009-10-08 07:24:58 +00001421 /// \brief For a static data member that was instantiated from a static
1422 /// data member of a class template, set the template specialiation kind.
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001423 void setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1424 SourceLocation PointOfInstantiation = SourceLocation());
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001425
Larisse Voufo39a1e502013-08-06 01:03:05 +00001426 /// \brief Specify that this variable is an instantiation of the
1427 /// static data member VD.
1428 void setInstantiationOfStaticDataMember(VarDecl *VD,
1429 TemplateSpecializationKind TSK);
1430
1431 /// \brief Retrieves the variable template that is described by this
1432 /// variable declaration.
1433 ///
1434 /// Every variable template is represented as a VarTemplateDecl and a
1435 /// VarDecl. The former contains template properties (such as
1436 /// the template parameter lists) while the latter contains the
1437 /// actual description of the template's
1438 /// contents. VarTemplateDecl::getTemplatedDecl() retrieves the
1439 /// VarDecl that from a VarTemplateDecl, while
1440 /// getDescribedVarTemplate() retrieves the VarTemplateDecl from
1441 /// a VarDecl.
1442 VarTemplateDecl *getDescribedVarTemplate() const;
1443
1444 void setDescribedVarTemplate(VarTemplateDecl *Template);
1445
Steve Naroffef2ab6a2007-04-02 20:50:54 +00001446 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00001447 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Alexis Hunted053252010-05-30 07:21:58 +00001448 static bool classofKind(Kind K) { return K >= firstVar && K <= lastVar; }
Steve Naroffef2ab6a2007-04-02 20:50:54 +00001449};
1450
Chris Lattner5696e7b2008-06-17 18:05:57 +00001451class ImplicitParamDecl : public VarDecl {
Craig Toppercbce6e92014-03-11 06:22:39 +00001452 void anchor() override;
Alexey Bataev56223232017-06-09 13:40:18 +00001453
Chris Lattner5696e7b2008-06-17 18:05:57 +00001454public:
Alexey Bataev56223232017-06-09 13:40:18 +00001455 /// Defines the kind of the implicit parameter: is this an implicit parameter
1456 /// with pointer to 'this', 'self', '_cmd', virtual table pointers, captured
1457 /// context or something else.
1458 enum ImplicitParamKind : unsigned {
Eugene Zelenkode0215c2017-11-13 23:01:27 +00001459 /// Parameter for Objective-C 'self' argument
1460 ObjCSelf,
1461
1462 /// Parameter for Objective-C '_cmd' argument
1463 ObjCCmd,
1464
1465 /// Parameter for C++ 'this' argument
1466 CXXThis,
1467
1468 /// Parameter for C++ virtual table pointers
1469 CXXVTT,
1470
1471 /// Parameter for captured context
1472 CapturedContext,
1473
1474 /// Other implicit parameter
1475 Other,
Alexey Bataev56223232017-06-09 13:40:18 +00001476 };
1477
1478 /// Create implicit parameter.
Chris Lattner5696e7b2008-06-17 18:05:57 +00001479 static ImplicitParamDecl *Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001480 SourceLocation IdLoc, IdentifierInfo *Id,
Alexey Bataev56223232017-06-09 13:40:18 +00001481 QualType T, ImplicitParamKind ParamKind);
1482 static ImplicitParamDecl *Create(ASTContext &C, QualType T,
1483 ImplicitParamKind ParamKind);
John McCall550d13a2011-02-22 22:25:56 +00001484
Douglas Gregor72172e92012-01-05 21:55:30 +00001485 static ImplicitParamDecl *CreateDeserialized(ASTContext &C, unsigned ID);
Richard Smith053f6c62014-05-16 23:01:30 +00001486
1487 ImplicitParamDecl(ASTContext &C, DeclContext *DC, SourceLocation IdLoc,
Alexey Bataev56223232017-06-09 13:40:18 +00001488 IdentifierInfo *Id, QualType Type,
1489 ImplicitParamKind ParamKind)
1490 : VarDecl(ImplicitParam, C, DC, IdLoc, IdLoc, Id, Type,
1491 /*TInfo=*/nullptr, SC_None) {
1492 NonParmVarDeclBits.ImplicitParamKind = ParamKind;
John McCall550d13a2011-02-22 22:25:56 +00001493 setImplicit();
1494 }
1495
Alexey Bataev56223232017-06-09 13:40:18 +00001496 ImplicitParamDecl(ASTContext &C, QualType Type, ImplicitParamKind ParamKind)
1497 : VarDecl(ImplicitParam, C, /*DC=*/nullptr, SourceLocation(),
1498 SourceLocation(), /*Id=*/nullptr, Type,
1499 /*TInfo=*/nullptr, SC_None) {
1500 NonParmVarDeclBits.ImplicitParamKind = ParamKind;
1501 setImplicit();
1502 }
1503
1504 /// Returns the implicit parameter kind.
1505 ImplicitParamKind getParameterKind() const {
1506 return static_cast<ImplicitParamKind>(NonParmVarDeclBits.ImplicitParamKind);
1507 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +00001508
Chris Lattner5696e7b2008-06-17 18:05:57 +00001509 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00001510 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1511 static bool classofKind(Kind K) { return K == ImplicitParam; }
Chris Lattner5696e7b2008-06-17 18:05:57 +00001512};
1513
James Dennettb8973efb2018-02-02 20:22:29 +00001514/// Represents a parameter to a function.
Steve Naroffef2ab6a2007-04-02 20:50:54 +00001515class ParmVarDecl : public VarDecl {
John McCall82490832011-05-02 00:30:12 +00001516public:
1517 enum { MaxFunctionScopeDepth = 255 };
1518 enum { MaxFunctionScopeIndex = 255 };
John McCall8fb0d9d2011-05-01 22:35:37 +00001519
Fariborz Jahanian7664ffb2008-12-20 20:56:12 +00001520protected:
Richard Smith053f6c62014-05-16 23:01:30 +00001521 ParmVarDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
1522 SourceLocation IdLoc, IdentifierInfo *Id, QualType T,
1523 TypeSourceInfo *TInfo, StorageClass S, Expr *DefArg)
1524 : VarDecl(DK, C, DC, StartLoc, IdLoc, Id, T, TInfo, S) {
John McCallbeaa11c2011-05-01 02:13:58 +00001525 assert(ParmVarDeclBits.HasInheritedDefaultArg == false);
Chandler Carruth813faed2015-12-30 02:51:00 +00001526 assert(ParmVarDeclBits.DefaultArgKind == DAK_None);
John McCallbeaa11c2011-05-01 02:13:58 +00001527 assert(ParmVarDeclBits.IsKNRPromoted == false);
John McCall82490832011-05-02 00:30:12 +00001528 assert(ParmVarDeclBits.IsObjCMethodParam == false);
Douglas Gregord12df6b2009-07-20 22:03:28 +00001529 setDefaultArg(DefArg);
1530 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001531
Chris Lattner4b08ca82008-03-15 21:10:16 +00001532public:
Chris Lattnerbec41342008-04-22 18:39:57 +00001533 static ParmVarDecl *Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001534 SourceLocation StartLoc,
1535 SourceLocation IdLoc, IdentifierInfo *Id,
John McCallbcd03502009-12-07 02:54:59 +00001536 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001537 StorageClass S, Expr *DefArg);
Mike Stump11289f42009-09-09 15:08:12 +00001538
Douglas Gregor72172e92012-01-05 21:55:30 +00001539 static ParmVarDecl *CreateDeserialized(ASTContext &C, unsigned ID);
Craig Toppercbce6e92014-03-11 06:22:39 +00001540
1541 SourceRange getSourceRange() const override LLVM_READONLY;
David Blaikie21bfbf82011-11-09 06:07:30 +00001542
John McCall82490832011-05-02 00:30:12 +00001543 void setObjCMethodScopeInfo(unsigned parameterIndex) {
1544 ParmVarDeclBits.IsObjCMethodParam = true;
Ted Kremenek9504ae92011-10-06 04:19:35 +00001545 setParameterIndex(parameterIndex);
John McCall82490832011-05-02 00:30:12 +00001546 }
1547
1548 void setScopeInfo(unsigned scopeDepth, unsigned parameterIndex) {
1549 assert(!ParmVarDeclBits.IsObjCMethodParam);
1550
1551 ParmVarDeclBits.ScopeDepthOrObjCQuals = scopeDepth;
David Blaikie21bfbf82011-11-09 06:07:30 +00001552 assert(ParmVarDeclBits.ScopeDepthOrObjCQuals == scopeDepth
1553 && "truncation!");
John McCall82490832011-05-02 00:30:12 +00001554
Ted Kremenek9504ae92011-10-06 04:19:35 +00001555 setParameterIndex(parameterIndex);
John McCall82490832011-05-02 00:30:12 +00001556 }
1557
1558 bool isObjCMethodParameter() const {
1559 return ParmVarDeclBits.IsObjCMethodParam;
John McCall8fb0d9d2011-05-01 22:35:37 +00001560 }
1561
1562 unsigned getFunctionScopeDepth() const {
John McCall82490832011-05-02 00:30:12 +00001563 if (ParmVarDeclBits.IsObjCMethodParam) return 0;
1564 return ParmVarDeclBits.ScopeDepthOrObjCQuals;
John McCall8fb0d9d2011-05-01 22:35:37 +00001565 }
1566
John McCall82490832011-05-02 00:30:12 +00001567 /// Returns the index of this parameter in its prototype or method scope.
John McCall8fb0d9d2011-05-01 22:35:37 +00001568 unsigned getFunctionScopeIndex() const {
Ted Kremenek540017e2011-10-06 05:00:56 +00001569 return getParameterIndex();
John McCall8fb0d9d2011-05-01 22:35:37 +00001570 }
1571
Ted Kremenek72be0682008-02-24 03:55:14 +00001572 ObjCDeclQualifier getObjCDeclQualifier() const {
John McCall82490832011-05-02 00:30:12 +00001573 if (!ParmVarDeclBits.IsObjCMethodParam) return OBJC_TQ_None;
1574 return ObjCDeclQualifier(ParmVarDeclBits.ScopeDepthOrObjCQuals);
Ted Kremenek72be0682008-02-24 03:55:14 +00001575 }
Chris Lattner3d722972008-12-17 07:32:46 +00001576 void setObjCDeclQualifier(ObjCDeclQualifier QTVal) {
John McCall82490832011-05-02 00:30:12 +00001577 assert(ParmVarDeclBits.IsObjCMethodParam);
1578 ParmVarDeclBits.ScopeDepthOrObjCQuals = QTVal;
Chris Lattner3d722972008-12-17 07:32:46 +00001579 }
Mike Stump11289f42009-09-09 15:08:12 +00001580
John McCall6a014d52011-03-09 04:22:44 +00001581 /// True if the value passed to this parameter must undergo
1582 /// K&R-style default argument promotion:
1583 ///
1584 /// C99 6.5.2.2.
1585 /// If the expression that denotes the called function has a type
1586 /// that does not include a prototype, the integer promotions are
1587 /// performed on each argument, and arguments that have type float
1588 /// are promoted to double.
John McCallbeaa11c2011-05-01 02:13:58 +00001589 bool isKNRPromoted() const {
1590 return ParmVarDeclBits.IsKNRPromoted;
1591 }
1592 void setKNRPromoted(bool promoted) {
1593 ParmVarDeclBits.IsKNRPromoted = promoted;
1594 }
John McCall6a014d52011-03-09 04:22:44 +00001595
Anders Carlsson714d0962009-12-15 19:16:31 +00001596 Expr *getDefaultArg();
Mike Stump11289f42009-09-09 15:08:12 +00001597 const Expr *getDefaultArg() const {
Anders Carlsson714d0962009-12-15 19:16:31 +00001598 return const_cast<ParmVarDecl *>(this)->getDefaultArg();
Anders Carlsson85446472009-06-06 04:14:07 +00001599 }
David Blaikie21bfbf82011-11-09 06:07:30 +00001600
Chandler Carruth813faed2015-12-30 02:51:00 +00001601 void setDefaultArg(Expr *defarg);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001602
Douglas Gregorc732aba2009-09-11 18:44:32 +00001603 /// \brief Retrieve the source range that covers the entire default
1604 /// argument.
David Blaikie21bfbf82011-11-09 06:07:30 +00001605 SourceRange getDefaultArgRange() const;
Chandler Carruth813faed2015-12-30 02:51:00 +00001606 void setUninstantiatedDefaultArg(Expr *arg);
1607 Expr *getUninstantiatedDefaultArg();
Douglas Gregorc732aba2009-09-11 18:44:32 +00001608 const Expr *getUninstantiatedDefaultArg() const {
Chandler Carruth813faed2015-12-30 02:51:00 +00001609 return const_cast<ParmVarDecl *>(this)->getUninstantiatedDefaultArg();
Douglas Gregorc732aba2009-09-11 18:44:32 +00001610 }
Mike Stump11289f42009-09-09 15:08:12 +00001611
James Dennettb8973efb2018-02-02 20:22:29 +00001612 /// Determines whether this parameter has a default argument,
Anders Carlsson85446472009-06-06 04:14:07 +00001613 /// either parsed or not.
Chandler Carruth813faed2015-12-30 02:51:00 +00001614 bool hasDefaultArg() const;
Mike Stump11289f42009-09-09 15:08:12 +00001615
James Dennettb8973efb2018-02-02 20:22:29 +00001616 /// Determines whether this parameter has a default argument that has not
1617 /// yet been parsed. This will occur during the processing of a C++ class
1618 /// whose member functions have default arguments, e.g.,
Douglas Gregor58354032008-12-24 00:01:03 +00001619 /// @code
1620 /// class X {
1621 /// public:
1622 /// void f(int x = 17); // x has an unparsed default argument now
1623 /// }; // x has a regular default argument now
1624 /// @endcode
1625 bool hasUnparsedDefaultArg() const {
Chandler Carruth813faed2015-12-30 02:51:00 +00001626 return ParmVarDeclBits.DefaultArgKind == DAK_Unparsed;
Douglas Gregor58354032008-12-24 00:01:03 +00001627 }
1628
Anders Carlssona5224ec2009-08-25 01:11:14 +00001629 bool hasUninstantiatedDefaultArg() const {
Chandler Carruth813faed2015-12-30 02:51:00 +00001630 return ParmVarDeclBits.DefaultArgKind == DAK_Uninstantiated;
Anders Carlssona5224ec2009-08-25 01:11:14 +00001631 }
Mike Stump11289f42009-09-09 15:08:12 +00001632
James Dennettb8973efb2018-02-02 20:22:29 +00001633 /// Specify that this parameter has an unparsed default argument.
1634 /// The argument will be replaced with a real default argument via
1635 /// setDefaultArg when the class definition enclosing the function
1636 /// declaration that owns this default argument is completed.
Chandler Carruth813faed2015-12-30 02:51:00 +00001637 void setUnparsedDefaultArg() {
1638 ParmVarDeclBits.DefaultArgKind = DAK_Unparsed;
1639 }
Douglas Gregor58354032008-12-24 00:01:03 +00001640
John McCallf3cd6652010-03-12 18:31:32 +00001641 bool hasInheritedDefaultArg() const {
John McCallbeaa11c2011-05-01 02:13:58 +00001642 return ParmVarDeclBits.HasInheritedDefaultArg;
John McCallf3cd6652010-03-12 18:31:32 +00001643 }
1644
1645 void setHasInheritedDefaultArg(bool I = true) {
John McCallbeaa11c2011-05-01 02:13:58 +00001646 ParmVarDeclBits.HasInheritedDefaultArg = I;
John McCallf3cd6652010-03-12 18:31:32 +00001647 }
1648
Reid Kleckner8a365022013-06-24 17:51:48 +00001649 QualType getOriginalType() const;
Mike Stump11289f42009-09-09 15:08:12 +00001650
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001651 /// \brief Determine whether this parameter is actually a function
1652 /// parameter pack.
1653 bool isParameterPack() const;
David Blaikie21bfbf82011-11-09 06:07:30 +00001654
James Dennettb8973efb2018-02-02 20:22:29 +00001655 /// Sets the function declaration that owns this
Douglas Gregorc72e6452009-01-09 18:51:29 +00001656 /// ParmVarDecl. Since ParmVarDecls are often created before the
1657 /// FunctionDecls that own them, this routine is required to update
1658 /// the DeclContext appropriately.
1659 void setOwningFunction(DeclContext *FD) { setDeclContext(FD); }
1660
Steve Naroffef2ab6a2007-04-02 20:50:54 +00001661 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00001662 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +00001663 static bool classofKind(Kind K) { return K == ParmVar; }
David Blaikie21bfbf82011-11-09 06:07:30 +00001664
Ted Kremenek9504ae92011-10-06 04:19:35 +00001665private:
Ted Kremenek540017e2011-10-06 05:00:56 +00001666 enum { ParameterIndexSentinel = (1 << NumParameterIndexBits) - 1 };
1667
Ted Kremenek9504ae92011-10-06 04:19:35 +00001668 void setParameterIndex(unsigned parameterIndex) {
Ted Kremenek540017e2011-10-06 05:00:56 +00001669 if (parameterIndex >= ParameterIndexSentinel) {
1670 setParameterIndexLarge(parameterIndex);
1671 return;
1672 }
David Blaikie21bfbf82011-11-09 06:07:30 +00001673
Ted Kremenek9504ae92011-10-06 04:19:35 +00001674 ParmVarDeclBits.ParameterIndex = parameterIndex;
1675 assert(ParmVarDeclBits.ParameterIndex == parameterIndex && "truncation!");
1676 }
Ted Kremenek540017e2011-10-06 05:00:56 +00001677 unsigned getParameterIndex() const {
1678 unsigned d = ParmVarDeclBits.ParameterIndex;
1679 return d == ParameterIndexSentinel ? getParameterIndexLarge() : d;
1680 }
David Blaikie21bfbf82011-11-09 06:07:30 +00001681
Ted Kremenek540017e2011-10-06 05:00:56 +00001682 void setParameterIndexLarge(unsigned parameterIndex);
1683 unsigned getParameterIndexLarge() const;
Chris Lattner17ed4872006-11-20 04:58:19 +00001684};
1685
James Dennettb8973efb2018-02-02 20:22:29 +00001686/// Represents a function declaration or definition.
Douglas Gregor89f238c2008-04-21 02:02:58 +00001687///
1688/// Since a given function can be declared several times in a program,
1689/// there may be several FunctionDecls that correspond to that
1690/// function. Only one of those FunctionDecls will be found when
1691/// traversing the list of declarations in the context of the
1692/// FunctionDecl (e.g., the translation unit); this FunctionDecl
1693/// contains all of the information known about the function. Other,
1694/// previous declarations of the function are available via the
Douglas Gregorec9fd132012-01-14 16:38:05 +00001695/// getPreviousDecl() chain.
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001696class FunctionDecl : public DeclaratorDecl, public DeclContext,
Argyrios Kyrtzidisfad334c2009-07-18 08:50:13 +00001697 public Redeclarable<FunctionDecl> {
Chris Lattnera11999d2006-10-15 22:34:45 +00001698public:
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001699 /// \brief The kind of templated function a FunctionDecl can be.
1700 enum TemplatedKind {
1701 TK_NonTemplate,
1702 TK_FunctionTemplate,
1703 TK_MemberSpecialization,
1704 TK_FunctionTemplateSpecialization,
1705 TK_DependentFunctionTemplateSpecialization
1706 };
1707
Mike Stump11289f42009-09-09 15:08:12 +00001708private:
James Dennettb8973efb2018-02-02 20:22:29 +00001709 /// A new[]'d array of pointers to VarDecls for the formal
Chris Lattneree1284a2008-03-16 00:16:02 +00001710 /// parameters of this function. This is null if a prototype or if there are
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001711 /// no formals.
Eugene Zelenkode0215c2017-11-13 23:01:27 +00001712 ParmVarDecl **ParamInfo = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001713
Chris Lattner9c28af02009-04-27 05:46:25 +00001714 LazyDeclStmtPtr Body;
Douglas Gregor89f238c2008-04-21 02:02:58 +00001715
Richard Smitha4a3c182015-12-29 22:19:20 +00001716 // FIXME: This can be packed into the bitfields in DeclContext.
1717 // NOTE: VC++ packs bitfields poorly if the types differ.
Reid Kleckner1f55d692017-03-13 22:33:04 +00001718 unsigned SClass : 3;
Richard Smitha4a3c182015-12-29 22:19:20 +00001719 unsigned IsInline : 1;
1720 unsigned IsInlineSpecified : 1;
Eugene Zelenkode0215c2017-11-13 23:01:27 +00001721
Richard Smithbc491202017-02-17 20:05:37 +00001722protected:
1723 // This is shared by CXXConstructorDecl, CXXConversionDecl, and
1724 // CXXDeductionGuideDecl.
Richard Smith78e3d702017-02-10 01:32:04 +00001725 unsigned IsExplicitSpecified : 1;
Eugene Zelenkode0215c2017-11-13 23:01:27 +00001726
Richard Smithbc491202017-02-17 20:05:37 +00001727private:
Richard Smitha4a3c182015-12-29 22:19:20 +00001728 unsigned IsVirtualAsWritten : 1;
1729 unsigned IsPure : 1;
1730 unsigned HasInheritedPrototype : 1;
1731 unsigned HasWrittenPrototype : 1;
1732 unsigned IsDeleted : 1;
1733 unsigned IsTrivial : 1; // sunk from CXXMethodDecl
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001734
1735 /// This flag indicates whether this function is trivial for the purpose of
1736 /// calls. This is meaningful only when this function is a copy/move
1737 /// constructor or a destructor.
1738 unsigned IsTrivialForCall : 1;
1739
Richard Smitha4a3c182015-12-29 22:19:20 +00001740 unsigned IsDefaulted : 1; // sunk from CXXMethoDecl
1741 unsigned IsExplicitlyDefaulted : 1; //sunk from CXXMethodDecl
1742 unsigned HasImplicitReturnZero : 1;
1743 unsigned IsLateTemplateParsed : 1;
1744 unsigned IsConstexpr : 1;
Eugene Zelenkode0215c2017-11-13 23:01:27 +00001745 unsigned InstantiationIsPending : 1;
Mike Stump11289f42009-09-09 15:08:12 +00001746
Reid Klecknerdeeddec2015-02-05 18:56:03 +00001747 /// \brief Indicates if the function uses __try.
Richard Smitha4a3c182015-12-29 22:19:20 +00001748 unsigned UsesSEHTry : 1;
Reid Klecknerdeeddec2015-02-05 18:56:03 +00001749
Argyrios Kyrtzidis1eb71a12012-12-06 18:59:10 +00001750 /// \brief Indicates if the function was a definition but its body was
1751 /// skipped.
1752 unsigned HasSkippedBody : 1;
1753
Faisal Vali1f3a2af2017-11-11 18:02:29 +00001754 /// Indicates if the function declaration will have a body, once we're done
1755 /// parsing it.
1756 unsigned WillHaveBody : 1;
1757
Erich Keane281d20b2018-01-08 21:34:17 +00001758 /// Indicates that this function is a multiversioned function using attribute
1759 /// 'target'.
1760 unsigned IsMultiVersion : 1;
1761
Faisal Vali81b756e2017-10-22 14:45:08 +00001762protected:
Faisal Vali1f3a2af2017-11-11 18:02:29 +00001763 /// [C++17] Only used by CXXDeductionGuideDecl. Declared here to avoid
1764 /// increasing the size of CXXDeductionGuideDecl by the size of an unsigned
1765 /// int as opposed to adding a single bit to FunctionDecl.
1766 /// Indicates that the Deduction Guide is the implicitly generated 'copy
1767 /// deduction candidate' (is used during overload resolution).
1768 unsigned IsCopyDeductionCandidate : 1;
Eugene Zelenkode0215c2017-11-13 23:01:27 +00001769
Faisal Vali81b756e2017-10-22 14:45:08 +00001770private:
Richard Trieue6caa262017-12-23 00:41:01 +00001771
1772 /// Store the ODRHash after first calculation.
1773 unsigned HasODRHash : 1;
1774 unsigned ODRHash;
1775
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +00001776 /// \brief End part of this FunctionDecl's source range.
1777 ///
1778 /// We could compute the full range in getSourceRange(). However, when we're
1779 /// dealing with a function definition deserialized from a PCH/AST file,
1780 /// we can only compute the full range once the function body has been
1781 /// de-serialized, so it's far better to have the (sometimes-redundant)
1782 /// EndRangeLoc.
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001783 SourceLocation EndRangeLoc;
Douglas Gregor24c332b2009-05-14 21:06:31 +00001784
1785 /// \brief The template or declaration that this declaration
1786 /// describes or was instantiated from, respectively.
Mike Stump11289f42009-09-09 15:08:12 +00001787 ///
Douglas Gregor24c332b2009-05-14 21:06:31 +00001788 /// For non-templates, this value will be NULL. For function
1789 /// declarations that describe a function template, this will be a
1790 /// pointer to a FunctionTemplateDecl. For member functions
Douglas Gregord801b062009-10-07 23:56:10 +00001791 /// of class template specializations, this will be a MemberSpecializationInfo
1792 /// pointer containing information about the specialization.
Mike Stump11289f42009-09-09 15:08:12 +00001793 /// For function template specializations, this will be a
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001794 /// FunctionTemplateSpecializationInfo, which contains information about
Mike Stump11289f42009-09-09 15:08:12 +00001795 /// the template being specialized and the template arguments involved in
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001796 /// that specialization.
David Blaikie21bfbf82011-11-09 06:07:30 +00001797 llvm::PointerUnion4<FunctionTemplateDecl *,
Douglas Gregord801b062009-10-07 23:56:10 +00001798 MemberSpecializationInfo *,
John McCallb9c78482010-04-08 09:05:18 +00001799 FunctionTemplateSpecializationInfo *,
1800 DependentFunctionTemplateSpecializationInfo *>
Douglas Gregor70d83e22009-06-29 17:30:29 +00001801 TemplateOrSpecialization;
Douglas Gregor24c332b2009-05-14 21:06:31 +00001802
Erich Keane7670b4b2017-12-15 16:37:14 +00001803 /// Provides source/type location info for the declaration name embedded in
1804 /// the DeclaratorDecl base class.
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001805 DeclarationNameLoc DNLoc;
1806
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001807 /// \brief Specify that this function declaration is actually a function
1808 /// template specialization.
1809 ///
1810 /// \param C the ASTContext.
1811 ///
1812 /// \param Template the function template that this function template
1813 /// specialization specializes.
1814 ///
1815 /// \param TemplateArgs the template arguments that produced this
1816 /// function template specialization from the template.
1817 ///
1818 /// \param InsertPos If non-NULL, the position in the function template
1819 /// specialization set where the function template specialization data will
1820 /// be inserted.
1821 ///
1822 /// \param TSK the kind of template specialization this is.
1823 ///
1824 /// \param TemplateArgsAsWritten location info of template arguments.
1825 ///
1826 /// \param PointOfInstantiation point at which the function template
David Blaikie21bfbf82011-11-09 06:07:30 +00001827 /// specialization was first instantiated.
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001828 void setFunctionTemplateSpecialization(ASTContext &C,
1829 FunctionTemplateDecl *Template,
1830 const TemplateArgumentList *TemplateArgs,
1831 void *InsertPos,
1832 TemplateSpecializationKind TSK,
1833 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1834 SourceLocation PointOfInstantiation);
1835
1836 /// \brief Specify that this record is an instantiation of the
1837 /// member function FD.
1838 void setInstantiationOfMemberFunction(ASTContext &C, FunctionDecl *FD,
1839 TemplateSpecializationKind TSK);
1840
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001841 void setParams(ASTContext &C, ArrayRef<ParmVarDecl *> NewParamInfo);
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001842
Argyrios Kyrtzidis2951e142008-06-09 21:05:31 +00001843protected:
Richard Smith053f6c62014-05-16 23:01:30 +00001844 FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
Justin Lebar2b42ccc2016-10-28 16:26:26 +00001845 const DeclarationNameInfo &NameInfo, QualType T,
1846 TypeSourceInfo *TInfo, StorageClass S, bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00001847 bool isConstexprSpecified)
Justin Lebar2b42ccc2016-10-28 16:26:26 +00001848 : DeclaratorDecl(DK, DC, NameInfo.getLoc(), NameInfo.getName(), T, TInfo,
1849 StartLoc),
Eugene Zelenkode0215c2017-11-13 23:01:27 +00001850 DeclContext(DK), redeclarable_base(C), SClass(S),
1851 IsInline(isInlineSpecified), IsInlineSpecified(isInlineSpecified),
1852 IsExplicitSpecified(false), IsVirtualAsWritten(false), IsPure(false),
1853 HasInheritedPrototype(false), HasWrittenPrototype(true),
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001854 IsDeleted(false), IsTrivial(false), IsTrivialForCall(false),
1855 IsDefaulted(false),
Eugene Zelenkode0215c2017-11-13 23:01:27 +00001856 IsExplicitlyDefaulted(false), HasImplicitReturnZero(false),
1857 IsLateTemplateParsed(false), IsConstexpr(isConstexprSpecified),
1858 InstantiationIsPending(false), UsesSEHTry(false), HasSkippedBody(false),
Erich Keane281d20b2018-01-08 21:34:17 +00001859 WillHaveBody(false), IsMultiVersion(false),
1860 IsCopyDeductionCandidate(false), HasODRHash(false), ODRHash(0),
1861 EndRangeLoc(NameInfo.getEndLoc()), DNLoc(NameInfo.getInfo()) {}
Douglas Gregor89f238c2008-04-21 02:02:58 +00001862
Eugene Zelenkode0215c2017-11-13 23:01:27 +00001863 using redeclarable_base = Redeclarable<FunctionDecl>;
1864
Richard Smithd7af8a32014-05-10 01:17:36 +00001865 FunctionDecl *getNextRedeclarationImpl() override {
1866 return getNextRedeclaration();
1867 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +00001868
Craig Toppercbce6e92014-03-11 06:22:39 +00001869 FunctionDecl *getPreviousDeclImpl() override {
Douglas Gregorec9fd132012-01-14 16:38:05 +00001870 return getPreviousDecl();
1871 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +00001872
Craig Toppercbce6e92014-03-11 06:22:39 +00001873 FunctionDecl *getMostRecentDeclImpl() override {
Douglas Gregorec9fd132012-01-14 16:38:05 +00001874 return getMostRecentDecl();
1875 }
Argyrios Kyrtzidis05898da2009-07-18 08:50:35 +00001876
Chris Lattner5072bae2008-03-15 21:24:04 +00001877public:
Eugene Zelenkode0215c2017-11-13 23:01:27 +00001878 friend class ASTDeclReader;
1879 friend class ASTDeclWriter;
1880
1881 using redecl_range = redeclarable_base::redecl_range;
1882 using redecl_iterator = redeclarable_base::redecl_iterator;
1883
Aaron Ballman211cd8c2014-03-07 00:10:58 +00001884 using redeclarable_base::redecls_begin;
1885 using redeclarable_base::redecls_end;
Aaron Ballman86c93902014-03-06 23:45:36 +00001886 using redeclarable_base::redecls;
Douglas Gregorec9fd132012-01-14 16:38:05 +00001887 using redeclarable_base::getPreviousDecl;
1888 using redeclarable_base::getMostRecentDecl;
Rafael Espindola3f9e4442013-10-19 02:13:21 +00001889 using redeclarable_base::isFirstDecl;
Douglas Gregor0bc8a212012-01-14 15:55:47 +00001890
Abramo Bagnaradff19302011-03-08 08:55:46 +00001891 static FunctionDecl *Create(ASTContext &C, DeclContext *DC,
1892 SourceLocation StartLoc, SourceLocation NLoc,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001893 DeclarationName N, QualType T,
John McCallbcd03502009-12-07 02:54:59 +00001894 TypeSourceInfo *TInfo,
Rafael Espindolab49ec5d2013-03-30 23:04:08 +00001895 StorageClass SC,
Douglas Gregorff76cb92010-12-09 16:59:22 +00001896 bool isInlineSpecified = false,
Richard Smitha77a0a62011-08-15 21:04:07 +00001897 bool hasWrittenPrototype = true,
1898 bool isConstexprSpecified = false) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001899 DeclarationNameInfo NameInfo(N, NLoc);
1900 return FunctionDecl::Create(C, DC, StartLoc, NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001901 SC,
Richard Smitha77a0a62011-08-15 21:04:07 +00001902 isInlineSpecified, hasWrittenPrototype,
1903 isConstexprSpecified);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001904 }
1905
1906 static FunctionDecl *Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001907 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001908 const DeclarationNameInfo &NameInfo,
1909 QualType T, TypeSourceInfo *TInfo,
Rafael Espindolab49ec5d2013-03-30 23:04:08 +00001910 StorageClass SC,
Rafael Espindolab49ec5d2013-03-30 23:04:08 +00001911 bool isInlineSpecified,
1912 bool hasWrittenPrototype,
Richard Smitha77a0a62011-08-15 21:04:07 +00001913 bool isConstexprSpecified = false);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001914
Douglas Gregor72172e92012-01-05 21:55:30 +00001915 static FunctionDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1916
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001917 DeclarationNameInfo getNameInfo() const {
1918 return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc);
1919 }
1920
Craig Toppercbce6e92014-03-11 06:22:39 +00001921 void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy,
1922 bool Qualified) const override;
Douglas Gregorb11aad82011-02-19 18:51:44 +00001923
Abramo Bagnaraea947882011-03-08 16:41:52 +00001924 void setRangeEnd(SourceLocation E) { EndRangeLoc = E; }
1925
Craig Toppercbce6e92014-03-11 06:22:39 +00001926 SourceRange getSourceRange() const override LLVM_READONLY;
Steve Naroff22315692008-10-03 00:02:03 +00001927
Serge Pavlove6e534c2018-03-01 07:04:11 +00001928 // Function definitions.
1929 //
1930 // A function declaration may be:
1931 // - a non defining declaration,
1932 // - a definition. A function may be defined because:
1933 // - it has a body, or will have it in the case of late parsing.
1934 // - it has an uninstantiated body. The body does not exist because the
1935 // function is not used yet, but the declaration is considered a
1936 // definition and does not allow other definition of this function.
1937 // - it does not have a user specified body, but it does not allow
1938 // redefinition, because it is deleted/defaulted or is defined through
1939 // some other mechanism (alias, ifunc).
1940
1941 /// Returns true if the function has a body.
1942 ///
1943 /// The function body might be in any of the (re-)declarations of this
1944 /// function. The variant that accepts a FunctionDecl pointer will set that
1945 /// function declaration to the actual declaration containing the body (if
1946 /// there is one).
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001947 bool hasBody(const FunctionDecl *&Definition) const;
1948
Craig Toppercbce6e92014-03-11 06:22:39 +00001949 bool hasBody() const override {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001950 const FunctionDecl* Definition;
1951 return hasBody(Definition);
1952 }
1953
Erich Keane7670b4b2017-12-15 16:37:14 +00001954 /// Returns whether the function has a trivial body that does not require any
1955 /// specific codegen.
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001956 bool hasTrivialBody() const;
1957
Serge Pavlove6e534c2018-03-01 07:04:11 +00001958 /// Returns true if the function has a definition that does not need to be
1959 /// instantiated.
1960 ///
1961 /// The variant that accepts a FunctionDecl pointer will set that function
1962 /// declaration to the declaration that is a definition (if there is one).
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001963 bool isDefined(const FunctionDecl *&Definition) const;
1964
1965 virtual bool isDefined() const {
1966 const FunctionDecl* Definition;
1967 return isDefined(Definition);
1968 }
1969
Yaron Keren18c3d062016-07-13 19:04:51 +00001970 /// \brief Get the definition for this declaration.
1971 FunctionDecl *getDefinition() {
1972 const FunctionDecl *Definition;
1973 if (isDefined(Definition))
1974 return const_cast<FunctionDecl *>(Definition);
1975 return nullptr;
1976 }
1977 const FunctionDecl *getDefinition() const {
1978 return const_cast<FunctionDecl *>(this)->getDefinition();
1979 }
1980
Erich Keane7670b4b2017-12-15 16:37:14 +00001981 /// Retrieve the body (definition) of the function. The function body might be
1982 /// in any of the (re-)declarations of this function. The variant that accepts
1983 /// a FunctionDecl pointer will set that function declaration to the actual
1984 /// declaration containing the body (if there is one).
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001985 /// NOTE: For checking if there is a body, use hasBody() instead, to avoid
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001986 /// unnecessary AST de-serialization of the body.
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001987 Stmt *getBody(const FunctionDecl *&Definition) const;
Ted Kremeneke5b40a92008-07-31 17:32:12 +00001988
Nico Weber2a9b6aa2014-09-03 15:28:00 +00001989 Stmt *getBody() const override {
Douglas Gregor89f238c2008-04-21 02:02:58 +00001990 const FunctionDecl* Definition;
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001991 return getBody(Definition);
Douglas Gregor89f238c2008-04-21 02:02:58 +00001992 }
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001993
Serge Pavlov10673c92017-06-04 12:53:12 +00001994 /// Returns whether this specific declaration of the function is also a
1995 /// definition that does not contain uninstantiated body.
1996 ///
1997 /// This does not determine whether the function has been defined (e.g., in a
1998 /// previous definition); for that information, use isDefined.
Francois Pichet1c229c02011-04-22 22:18:13 +00001999 bool isThisDeclarationADefinition() const {
Ilya Biryukovbf484aa2017-12-14 13:00:33 +00002000 return IsDeleted || IsDefaulted || Body || HasSkippedBody ||
2001 IsLateTemplateParsed || WillHaveBody || hasDefiningAttr();
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002002 }
2003
Serge Pavlove6e534c2018-03-01 07:04:11 +00002004 /// Returns whether this specific declaration of the function has a body.
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002005 bool doesThisDeclarationHaveABody() const {
Francois Pichet1c229c02011-04-22 22:18:13 +00002006 return Body || IsLateTemplateParsed;
2007 }
Douglas Gregor89f238c2008-04-21 02:02:58 +00002008
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00002009 void setBody(Stmt *B);
Douglas Gregor3c3aa612009-04-18 00:07:54 +00002010 void setLazyBody(uint64_t Offset) { Body = Offset; }
Sebastian Redl60795252009-01-09 19:57:06 +00002011
Ted Kremenek186a0742010-04-29 16:49:01 +00002012 /// Whether this function is variadic.
2013 bool isVariadic() const;
2014
Anders Carlsson0a7c01f2009-05-14 22:15:41 +00002015 /// Whether this function is marked as virtual explicitly.
2016 bool isVirtualAsWritten() const { return IsVirtualAsWritten; }
2017 void setVirtualAsWritten(bool V) { IsVirtualAsWritten = V; }
Sebastian Redl60795252009-01-09 19:57:06 +00002018
Sebastian Redl6138f2b2009-01-09 22:29:03 +00002019 /// Whether this virtual function is pure, i.e. makes the containing class
2020 /// abstract.
Anders Carlsson7cbd8fb2009-03-22 01:52:17 +00002021 bool isPure() const { return IsPure; }
Douglas Gregor7d9120c2010-09-28 21:55:22 +00002022 void setPure(bool P = true);
Sebastian Redl60795252009-01-09 19:57:06 +00002023
Francois Pichet1c229c02011-04-22 22:18:13 +00002024 /// Whether this templated function will be late parsed.
2025 bool isLateTemplateParsed() const { return IsLateTemplateParsed; }
2026 void setLateTemplateParsed(bool ILT = true) { IsLateTemplateParsed = ILT; }
2027
Douglas Gregor8a273912009-07-22 18:25:24 +00002028 /// Whether this function is "trivial" in some specialized C++ senses.
2029 /// Can only be true for default constructors, copy constructors,
2030 /// copy assignment operators, and destructors. Not meaningful until
2031 /// the class has been fully built by Sema.
2032 bool isTrivial() const { return IsTrivial; }
2033 void setTrivial(bool IT) { IsTrivial = IT; }
Mike Stump11289f42009-09-09 15:08:12 +00002034
Akira Hatanaka02914dc2018-02-05 20:23:22 +00002035 bool isTrivialForCall() const { return IsTrivialForCall; }
2036 void setTrivialForCall(bool IT) { IsTrivialForCall = IT; }
2037
Alexis Hunt5dafebc2011-05-06 01:42:00 +00002038 /// Whether this function is defaulted per C++0x. Only valid for
David Blaikie21bfbf82011-11-09 06:07:30 +00002039 /// special member functions.
Alexis Hunt5dafebc2011-05-06 01:42:00 +00002040 bool isDefaulted() const { return IsDefaulted; }
2041 void setDefaulted(bool D = true) { IsDefaulted = D; }
2042
2043 /// Whether this function is explicitly defaulted per C++0x. Only valid
2044 /// for special member functions.
2045 bool isExplicitlyDefaulted() const { return IsExplicitlyDefaulted; }
2046 void setExplicitlyDefaulted(bool ED = true) { IsExplicitlyDefaulted = ED; }
2047
John McCallcaa19452009-07-28 01:00:58 +00002048 /// Whether falling off this function implicitly returns null/zero.
2049 /// If a more specific implicit return value is required, front-ends
2050 /// should synthesize the appropriate return statements.
2051 bool hasImplicitReturnZero() const { return HasImplicitReturnZero; }
2052 void setHasImplicitReturnZero(bool IRZ) { HasImplicitReturnZero = IRZ; }
2053
Douglas Gregor739ef0c2009-02-25 16:33:18 +00002054 /// \brief Whether this function has a prototype, either because one
2055 /// was explicitly written or because it was "inherited" by merging
2056 /// a declaration without a prototype with a declaration that has a
2057 /// prototype.
Mike Stump11289f42009-09-09 15:08:12 +00002058 bool hasPrototype() const {
2059 return HasWrittenPrototype || HasInheritedPrototype;
Anders Carlssone0dd1d52009-05-14 21:46:00 +00002060 }
Mike Stump11289f42009-09-09 15:08:12 +00002061
Anders Carlssone0dd1d52009-05-14 21:46:00 +00002062 bool hasWrittenPrototype() const { return HasWrittenPrototype; }
Douglas Gregor739ef0c2009-02-25 16:33:18 +00002063
Douglas Gregorbcbf8632009-02-16 18:20:44 +00002064 /// \brief Whether this function inherited its prototype from a
2065 /// previous declaration.
Anders Carlssone0dd1d52009-05-14 21:46:00 +00002066 bool hasInheritedPrototype() const { return HasInheritedPrototype; }
2067 void setHasInheritedPrototype(bool P = true) { HasInheritedPrototype = P; }
Douglas Gregorbcbf8632009-02-16 18:20:44 +00002068
Richard Smith12f247f2012-06-08 21:09:22 +00002069 /// Whether this is a (C++11) constexpr function or constexpr constructor.
Richard Smitha77a0a62011-08-15 21:04:07 +00002070 bool isConstexpr() const { return IsConstexpr; }
Richard Smith92f241f2012-12-08 02:53:02 +00002071 void setConstexpr(bool IC) { IsConstexpr = IC; }
Richard Smitha77a0a62011-08-15 21:04:07 +00002072
Sunil Srivastava15ed2922017-06-20 22:08:44 +00002073 /// \brief Whether the instantiation of this function is pending.
2074 /// This bit is set when the decision to instantiate this function is made
2075 /// and unset if and when the function body is created. That leaves out
2076 /// cases where instantiation did not happen because the template definition
2077 /// was not seen in this TU. This bit remains set in those cases, under the
2078 /// assumption that the instantiation will happen in some other TU.
2079 bool instantiationIsPending() const { return InstantiationIsPending; }
2080 void setInstantiationIsPending(bool IC) { InstantiationIsPending = IC; }
2081
Aaron Ballman6ca7e642015-08-24 18:50:01 +00002082 /// \brief Indicates the function uses __try.
Reid Klecknerdeeddec2015-02-05 18:56:03 +00002083 bool usesSEHTry() const { return UsesSEHTry; }
2084 void setUsesSEHTry(bool UST) { UsesSEHTry = UST; }
2085
Douglas Gregor171c45a2009-02-18 21:56:37 +00002086 /// \brief Whether this function has been deleted.
2087 ///
2088 /// A function that is "deleted" (via the C++0x "= delete" syntax)
2089 /// acts like a normal function, except that it cannot actually be
2090 /// called or have its address taken. Deleted functions are
2091 /// typically used in C++ overload resolution to attract arguments
2092 /// whose type or lvalue/rvalue-ness would permit the use of a
2093 /// different overload that would behave incorrectly. For example,
2094 /// one might use deleted functions to ban implicit conversion from
2095 /// a floating-point number to an Integer type:
2096 ///
2097 /// @code
2098 /// struct Integer {
2099 /// Integer(long); // construct from a long
2100 /// Integer(double) = delete; // no construction from float or double
2101 /// Integer(long double) = delete; // no construction from long double
2102 /// };
2103 /// @endcode
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002104 // If a function is deleted, its first declaration must be.
2105 bool isDeleted() const { return getCanonicalDecl()->IsDeleted; }
2106 bool isDeletedAsWritten() const { return IsDeleted && !IsDefaulted; }
2107 void setDeletedAsWritten(bool D = true) { IsDeleted = D; }
Douglas Gregor171c45a2009-02-18 21:56:37 +00002108
John McCall53ffd372011-05-15 17:49:20 +00002109 /// \brief Determines whether this function is "main", which is the
2110 /// entry point into an executable program.
Douglas Gregor16618f22009-09-12 00:17:51 +00002111 bool isMain() const;
Douglas Gregore62c0a42009-02-24 01:23:02 +00002112
David Majnemerc729b0b2013-09-16 22:44:20 +00002113 /// \brief Determines whether this function is a MSVCRT user defined entry
2114 /// point.
2115 bool isMSVCRTEntryPoint() const;
2116
John McCall53ffd372011-05-15 17:49:20 +00002117 /// \brief Determines whether this operator new or delete is one
2118 /// of the reserved global placement operators:
2119 /// void *operator new(size_t, void *);
2120 /// void *operator new[](size_t, void *);
2121 /// void operator delete(void *, void *);
2122 /// void operator delete[](void *, void *);
2123 /// These functions have special behavior under [new.delete.placement]:
2124 /// These functions are reserved, a C++ program may not define
2125 /// functions that displace the versions in the Standard C++ library.
2126 /// The provisions of [basic.stc.dynamic] do not apply to these
2127 /// reserved placement forms of operator new and operator delete.
2128 ///
2129 /// This function must be an allocation or deallocation function.
2130 bool isReservedGlobalPlacementOperator() const;
2131
Richard Smith8d0dc312013-07-21 23:12:18 +00002132 /// \brief Determines whether this function is one of the replaceable
2133 /// global allocation functions:
2134 /// void *operator new(size_t);
2135 /// void *operator new(size_t, const std::nothrow_t &) noexcept;
2136 /// void *operator new[](size_t);
2137 /// void *operator new[](size_t, const std::nothrow_t &) noexcept;
2138 /// void operator delete(void *) noexcept;
Richard Smith1cdec012013-09-29 04:40:38 +00002139 /// void operator delete(void *, std::size_t) noexcept; [C++1y]
Richard Smith8d0dc312013-07-21 23:12:18 +00002140 /// void operator delete(void *, const std::nothrow_t &) noexcept;
2141 /// void operator delete[](void *) noexcept;
Richard Smith1cdec012013-09-29 04:40:38 +00002142 /// void operator delete[](void *, std::size_t) noexcept; [C++1y]
Richard Smith8d0dc312013-07-21 23:12:18 +00002143 /// void operator delete[](void *, const std::nothrow_t &) noexcept;
2144 /// These functions have special behavior under C++1y [expr.new]:
2145 /// An implementation is allowed to omit a call to a replaceable global
2146 /// allocation function. [...]
Akira Hatanakacae83f72017-06-29 18:48:40 +00002147 ///
2148 /// If this function is an aligned allocation/deallocation function, return
2149 /// true through IsAligned.
2150 bool isReplaceableGlobalAllocationFunction(bool *IsAligned = nullptr) const;
Richard Smith8d0dc312013-07-21 23:12:18 +00002151
Richard Smith5b349582017-10-13 01:55:36 +00002152 /// \brief Determine whether this is a destroying operator delete.
2153 bool isDestroyingOperatorDelete() const;
2154
Rafael Espindolaf4187652013-02-14 01:18:37 +00002155 /// Compute the language linkage.
2156 LanguageLinkage getLanguageLinkage() const;
2157
Rafael Espindola5bda63f2013-02-14 01:47:04 +00002158 /// \brief Determines whether this function is a function with
2159 /// external, C linkage.
Rafael Espindola0e0d0092013-03-14 03:07:35 +00002160 bool isExternC() const;
Rafael Espindola576127d2012-12-28 14:21:58 +00002161
Rafael Espindola593537a2013-05-05 20:15:21 +00002162 /// \brief Determines whether this function's context is, or is nested within,
2163 /// a C++ extern "C" linkage spec.
2164 bool isInExternCContext() const;
2165
2166 /// \brief Determines whether this function's context is, or is nested within,
2167 /// a C++ extern "C++" linkage spec.
2168 bool isInExternCXXContext() const;
2169
Douglas Gregorf1b876d2009-03-31 16:35:03 +00002170 /// \brief Determines whether this is a global function.
2171 bool isGlobal() const;
2172
Richard Smith10876ef2013-01-17 01:30:42 +00002173 /// \brief Determines whether this function is known to be 'noreturn', through
2174 /// an attribute on its declaration or its type.
2175 bool isNoReturn() const;
2176
Argyrios Kyrtzidis1eb71a12012-12-06 18:59:10 +00002177 /// \brief True if the function was a definition but its body was skipped.
2178 bool hasSkippedBody() const { return HasSkippedBody; }
2179 void setHasSkippedBody(bool Skipped = true) { HasSkippedBody = Skipped; }
2180
Justin Lebar2b42ccc2016-10-28 16:26:26 +00002181 /// True if this function will eventually have a body, once it's fully parsed.
2182 bool willHaveBody() const { return WillHaveBody; }
2183 void setWillHaveBody(bool V = true) { WillHaveBody = V; }
2184
Erich Keane281d20b2018-01-08 21:34:17 +00002185 /// True if this function is considered a multiversioned function.
2186 bool isMultiVersion() const { return getCanonicalDecl()->IsMultiVersion; }
2187
2188 /// Sets the multiversion state for this declaration and all of its
2189 /// redeclarations.
2190 void setIsMultiVersion(bool V = true) {
2191 getCanonicalDecl()->IsMultiVersion = V;
2192 }
2193
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002194 void setPreviousDeclaration(FunctionDecl * PrevDecl);
Eli Friedmanaee9e542008-05-27 05:07:37 +00002195
Craig Toppercbce6e92014-03-11 06:22:39 +00002196 FunctionDecl *getCanonicalDecl() override;
Craig Topper38227742015-05-10 18:40:12 +00002197 const FunctionDecl *getCanonicalDecl() const {
2198 return const_cast<FunctionDecl*>(this)->getCanonicalDecl();
2199 }
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00002200
Douglas Gregor15fc9562009-09-12 00:22:50 +00002201 unsigned getBuiltinID() const;
Douglas Gregorb9063fc2009-02-13 23:20:09 +00002202
David Majnemer59f77922016-06-24 04:05:48 +00002203 // ArrayRef interface to parameters.
2204 ArrayRef<ParmVarDecl *> parameters() const {
2205 return {ParamInfo, getNumParams()};
2206 }
2207 MutableArrayRef<ParmVarDecl *> parameters() {
2208 return {ParamInfo, getNumParams()};
2209 }
2210
Chris Lattner9b65d352007-12-19 23:58:25 +00002211 // Iterator access to formal parameters.
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002212 using param_iterator = MutableArrayRef<ParmVarDecl *>::iterator;
2213 using param_const_iterator = ArrayRef<ParmVarDecl *>::const_iterator;
2214
David Majnemer59f77922016-06-24 04:05:48 +00002215 bool param_empty() const { return parameters().empty(); }
2216 param_iterator param_begin() { return parameters().begin(); }
2217 param_iterator param_end() { return parameters().end(); }
2218 param_const_iterator param_begin() const { return parameters().begin(); }
2219 param_const_iterator param_end() const { return parameters().end(); }
2220 size_t param_size() const { return parameters().size(); }
Mike Stump11289f42009-09-09 15:08:12 +00002221
Erich Keane7670b4b2017-12-15 16:37:14 +00002222 /// Return the number of parameters this function must have based on its
2223 /// FunctionType. This is the length of the ParamInfo array after it has been
2224 /// created.
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00002225 unsigned getNumParams() const;
Mike Stump11289f42009-09-09 15:08:12 +00002226
Chris Lattner53621a52007-06-13 20:44:40 +00002227 const ParmVarDecl *getParamDecl(unsigned i) const {
Chris Lattner2e89ebb2007-01-21 23:09:17 +00002228 assert(i < getNumParams() && "Illegal param #");
2229 return ParamInfo[i];
2230 }
Chris Lattner53621a52007-06-13 20:44:40 +00002231 ParmVarDecl *getParamDecl(unsigned i) {
2232 assert(i < getNumParams() && "Illegal param #");
2233 return ParamInfo[i];
2234 }
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002235 void setParams(ArrayRef<ParmVarDecl *> NewParamInfo) {
David Blaikie9c70e042011-09-21 18:16:56 +00002236 setParams(getASTContext(), NewParamInfo);
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002237 }
Chris Lattnerb0d38442008-04-12 23:52:44 +00002238
Erich Keane7670b4b2017-12-15 16:37:14 +00002239 /// Returns the minimum number of arguments needed to call this function. This
2240 /// may be fewer than the number of function parameters, if some of the
2241 /// parameters have default arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00002242 unsigned getMinRequiredArguments() const;
Steve Naroff46ba1eb2007-04-03 23:13:13 +00002243
Alp Toker314cc812014-01-25 16:55:45 +00002244 QualType getReturnType() const {
Alexey Bataev2c485a702016-01-15 04:36:32 +00002245 assert(getType()->getAs<FunctionType>() && "Expected a FunctionType!");
Alp Toker314cc812014-01-25 16:55:45 +00002246 return getType()->getAs<FunctionType>()->getReturnType();
Steve Naroff9358c712007-05-27 23:58:33 +00002247 }
David Blaikie21bfbf82011-11-09 06:07:30 +00002248
Alp Tokerf5b10792014-07-02 12:55:58 +00002249 /// \brief Attempt to compute an informative source range covering the
2250 /// function return type. This may omit qualifiers and other information with
2251 /// limited representation in the AST.
Alp Tokerd0787eb2014-07-02 01:47:15 +00002252 SourceRange getReturnTypeSourceRange() const;
2253
Malcolm Parsonsa3220ce2017-01-12 16:11:28 +00002254 /// \brief Attempt to compute an informative source range covering the
2255 /// function exception specification, if any.
2256 SourceRange getExceptionSpecSourceRange() const;
2257
Douglas Gregor603d81b2010-07-13 08:18:22 +00002258 /// \brief Determine the type of an expression that calls this function.
2259 QualType getCallResultType() const {
Alexey Bataev2c485a702016-01-15 04:36:32 +00002260 assert(getType()->getAs<FunctionType>() && "Expected a FunctionType!");
Douglas Gregor603d81b2010-07-13 08:18:22 +00002261 return getType()->getAs<FunctionType>()->getCallResultType(getASTContext());
2262 }
David Blaikie21bfbf82011-11-09 06:07:30 +00002263
Aaron Ballmane7964782016-03-07 22:44:55 +00002264 /// \brief Returns the WarnUnusedResultAttr that is either declared on this
2265 /// function, or its return type declaration.
2266 const Attr *getUnusedResultAttr() const;
2267
Kaelyn Takata0a2e84c2015-04-09 19:43:04 +00002268 /// \brief Returns true if this function or its return type has the
Erich Keane4b87d812017-04-19 21:24:55 +00002269 /// warn_unused_result attribute.
Aaron Ballmane7964782016-03-07 22:44:55 +00002270 bool hasUnusedResultAttr() const { return getUnusedResultAttr() != nullptr; }
Kaelyn Takata0a2e84c2015-04-09 19:43:04 +00002271
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002272 /// \brief Returns the storage class as written in the source. For the
2273 /// computed linkage of symbol, see getLinkage.
Ted Kremenek72be0682008-02-24 03:55:14 +00002274 StorageClass getStorageClass() const { return StorageClass(SClass); }
Douglas Gregorc4df4072010-04-19 22:54:31 +00002275
Douglas Gregor35b57532009-10-27 21:01:01 +00002276 /// \brief Determine whether the "inline" keyword was specified for this
2277 /// function.
Douglas Gregorff76cb92010-12-09 16:59:22 +00002278 bool isInlineSpecified() const { return IsInlineSpecified; }
David Blaikie21bfbf82011-11-09 06:07:30 +00002279
Douglas Gregor35b57532009-10-27 21:01:01 +00002280 /// Set whether the "inline" keyword was specified for this function.
David Blaikie21bfbf82011-11-09 06:07:30 +00002281 void setInlineSpecified(bool I) {
2282 IsInlineSpecified = I;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002283 IsInline = I;
2284 }
Douglas Gregore62c0a42009-02-24 01:23:02 +00002285
John McCall357d0f32010-12-15 04:00:32 +00002286 /// Flag that this function is implicitly inline.
2287 void setImplicitlyInline() {
2288 IsInline = true;
2289 }
2290
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002291 /// \brief Determine whether this function should be inlined, because it is
Richard Smith7cd25b22011-09-30 00:45:47 +00002292 /// either marked "inline" or "constexpr" or is a member function of a class
2293 /// that was defined in the class body.
Richard Smithf3814ad2013-01-25 00:08:28 +00002294 bool isInlined() const { return IsInline; }
John McCall357d0f32010-12-15 04:00:32 +00002295
Douglas Gregor299d76e2009-09-13 07:46:26 +00002296 bool isInlineDefinitionExternallyVisible() const;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002297
David Majnemer54e3ba52014-04-02 23:17:29 +00002298 bool isMSExternInline() const;
2299
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002300 bool doesDeclarationForceExternallyVisibleDefinition() const;
David Blaikie21bfbf82011-11-09 06:07:30 +00002301
James Dennettb8973efb2018-02-02 20:22:29 +00002302 /// Whether this function declaration represents an C++ overloaded
2303 /// operator, e.g., "operator+".
Mike Stump11289f42009-09-09 15:08:12 +00002304 bool isOverloadedOperator() const {
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002305 return getOverloadedOperator() != OO_None;
Daniel Dunbare017ecc2009-12-19 17:50:07 +00002306 }
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002307
2308 OverloadedOperatorKind getOverloadedOperator() const;
2309
Alexis Huntc88db062010-01-13 09:01:02 +00002310 const IdentifierInfo *getLiteralIdentifier() const;
2311
Douglas Gregor24c332b2009-05-14 21:06:31 +00002312 /// \brief If this function is an instantiation of a member function
2313 /// of a class template specialization, retrieves the function from
2314 /// which it was instantiated.
2315 ///
2316 /// This routine will return non-NULL for (non-templated) member
2317 /// functions of class templates and for instantiations of function
2318 /// templates. For example, given:
2319 ///
2320 /// \code
2321 /// template<typename T>
2322 /// struct X {
2323 /// void f(T);
2324 /// };
2325 /// \endcode
2326 ///
2327 /// The declaration for X<int>::f is a (non-templated) FunctionDecl
2328 /// whose parent is the class template specialization X<int>. For
2329 /// this declaration, getInstantiatedFromFunction() will return
2330 /// the FunctionDecl X<T>::A. When a complete definition of
2331 /// X<int>::A is required, it will be instantiated from the
2332 /// declaration returned by getInstantiatedFromMemberFunction().
Douglas Gregord801b062009-10-07 23:56:10 +00002333 FunctionDecl *getInstantiatedFromMemberFunction() const;
David Blaikie21bfbf82011-11-09 06:07:30 +00002334
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002335 /// \brief What kind of templated function this is.
2336 TemplatedKind getTemplatedKind() const;
Douglas Gregor24c332b2009-05-14 21:06:31 +00002337
Douglas Gregor06db9f52009-10-12 20:18:28 +00002338 /// \brief If this function is an instantiation of a member function of a
2339 /// class template specialization, retrieves the member specialization
2340 /// information.
Chandler Carruth21c90602015-12-30 03:24:14 +00002341 MemberSpecializationInfo *getMemberSpecializationInfo() const;
David Blaikie21bfbf82011-11-09 06:07:30 +00002342
Douglas Gregor24c332b2009-05-14 21:06:31 +00002343 /// \brief Specify that this record is an instantiation of the
Douglas Gregord801b062009-10-07 23:56:10 +00002344 /// member function FD.
2345 void setInstantiationOfMemberFunction(FunctionDecl *FD,
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002346 TemplateSpecializationKind TSK) {
2347 setInstantiationOfMemberFunction(getASTContext(), FD, TSK);
2348 }
Douglas Gregor24c332b2009-05-14 21:06:31 +00002349
2350 /// \brief Retrieves the function template that is described by this
2351 /// function declaration.
2352 ///
2353 /// Every function template is represented as a FunctionTemplateDecl
2354 /// and a FunctionDecl (or something derived from FunctionDecl). The
2355 /// former contains template properties (such as the template
2356 /// parameter lists) while the latter contains the actual
2357 /// description of the template's
2358 /// contents. FunctionTemplateDecl::getTemplatedDecl() retrieves the
2359 /// FunctionDecl that describes the function template,
2360 /// getDescribedFunctionTemplate() retrieves the
2361 /// FunctionTemplateDecl from a FunctionDecl.
Chandler Carruth21c90602015-12-30 03:24:14 +00002362 FunctionTemplateDecl *getDescribedFunctionTemplate() const;
Douglas Gregor24c332b2009-05-14 21:06:31 +00002363
Chandler Carruth21c90602015-12-30 03:24:14 +00002364 void setDescribedFunctionTemplate(FunctionTemplateDecl *Template);
Douglas Gregor24c332b2009-05-14 21:06:31 +00002365
David Blaikie21bfbf82011-11-09 06:07:30 +00002366 /// \brief Determine whether this function is a function template
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002367 /// specialization.
2368 bool isFunctionTemplateSpecialization() const {
Craig Topper34b4c432014-05-06 06:48:52 +00002369 return getPrimaryTemplate() != nullptr;
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002370 }
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002371
2372 /// \brief Retrieve the class scope template pattern that this function
2373 /// template specialization is instantiated from.
2374 FunctionDecl *getClassScopeSpecializationPattern() const;
2375
Douglas Gregor06db9f52009-10-12 20:18:28 +00002376 /// \brief If this function is actually a function template specialization,
David Blaikie21bfbf82011-11-09 06:07:30 +00002377 /// retrieve information about this function template specialization.
Douglas Gregor06db9f52009-10-12 20:18:28 +00002378 /// Otherwise, returns NULL.
Chandler Carruth21c90602015-12-30 03:24:14 +00002379 FunctionTemplateSpecializationInfo *getTemplateSpecializationInfo() const;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002380
2381 /// \brief Determines whether this function is a function template
2382 /// specialization or a member of a class template specialization that can
2383 /// be implicitly instantiated.
2384 bool isImplicitlyInstantiable() const;
David Blaikie21bfbf82011-11-09 06:07:30 +00002385
Ted Kremenek85825ae2011-12-01 00:59:17 +00002386 /// \brief Determines if the given function was instantiated from a
2387 /// function template.
2388 bool isTemplateInstantiation() const;
2389
Douglas Gregorafca3b42009-10-27 20:53:28 +00002390 /// \brief Retrieve the function declaration from which this function could
2391 /// be instantiated, if it is an instantiation (rather than a non-template
2392 /// or a specialization, for example).
2393 FunctionDecl *getTemplateInstantiationPattern() const;
2394
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002395 /// \brief Retrieve the primary template that this function template
2396 /// specialization either specializes or was instantiated from.
2397 ///
2398 /// If this function declaration is not a function template specialization,
2399 /// returns NULL.
Douglas Gregor70d83e22009-06-29 17:30:29 +00002400 FunctionTemplateDecl *getPrimaryTemplate() const;
Mike Stump11289f42009-09-09 15:08:12 +00002401
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002402 /// \brief Retrieve the template arguments used to produce this function
2403 /// template specialization from the primary template.
2404 ///
2405 /// If this function declaration is not a function template specialization,
2406 /// returns NULL.
Mike Stump11289f42009-09-09 15:08:12 +00002407 const TemplateArgumentList *getTemplateSpecializationArgs() const;
2408
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002409 /// \brief Retrieve the template argument list as written in the sources,
2410 /// if any.
2411 ///
2412 /// If this function declaration is not a function template specialization
2413 /// or if it had no explicit template argument list, returns NULL.
2414 /// Note that it an explicit template argument list may be written empty,
2415 /// e.g., template<> void foo<>(char* s);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002416 const ASTTemplateArgumentListInfo*
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002417 getTemplateSpecializationArgsAsWritten() const;
2418
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002419 /// \brief Specify that this function declaration is actually a function
2420 /// template specialization.
2421 ///
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002422 /// \param Template the function template that this function template
2423 /// specialization specializes.
2424 ///
2425 /// \param TemplateArgs the template arguments that produced this
2426 /// function template specialization from the template.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002427 ///
2428 /// \param InsertPos If non-NULL, the position in the function template
2429 /// specialization set where the function template specialization data will
2430 /// be inserted.
2431 ///
2432 /// \param TSK the kind of template specialization this is.
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002433 ///
2434 /// \param TemplateArgsAsWritten location info of template arguments.
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002435 ///
2436 /// \param PointOfInstantiation point at which the function template
David Blaikie21bfbf82011-11-09 06:07:30 +00002437 /// specialization was first instantiated.
Douglas Gregord5058122010-02-11 01:19:42 +00002438 void setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
Craig Topper34b4c432014-05-06 06:48:52 +00002439 const TemplateArgumentList *TemplateArgs,
2440 void *InsertPos,
2441 TemplateSpecializationKind TSK = TSK_ImplicitInstantiation,
2442 const TemplateArgumentListInfo *TemplateArgsAsWritten = nullptr,
2443 SourceLocation PointOfInstantiation = SourceLocation()) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002444 setFunctionTemplateSpecialization(getASTContext(), Template, TemplateArgs,
2445 InsertPos, TSK, TemplateArgsAsWritten,
2446 PointOfInstantiation);
2447 }
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002448
John McCallb9c78482010-04-08 09:05:18 +00002449 /// \brief Specifies that this function declaration is actually a
2450 /// dependent function template specialization.
2451 void setDependentTemplateSpecialization(ASTContext &Context,
2452 const UnresolvedSetImpl &Templates,
2453 const TemplateArgumentListInfo &TemplateArgs);
2454
2455 DependentFunctionTemplateSpecializationInfo *
Chandler Carruth21c90602015-12-30 03:24:14 +00002456 getDependentSpecializationInfo() const;
John McCallb9c78482010-04-08 09:05:18 +00002457
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002458 /// \brief Determine what kind of template instantiation this function
2459 /// represents.
2460 TemplateSpecializationKind getTemplateSpecializationKind() const;
Douglas Gregore8925db2009-06-29 22:39:32 +00002461
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002462 /// \brief Determine what kind of template instantiation this function
2463 /// represents.
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002464 void setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2465 SourceLocation PointOfInstantiation = SourceLocation());
2466
2467 /// \brief Retrieve the (first) point of instantiation of a function template
2468 /// specialization or a member of a class template specialization.
2469 ///
David Blaikie21bfbf82011-11-09 06:07:30 +00002470 /// \returns the first point of instantiation, if this function was
2471 /// instantiated from a template; otherwise, returns an invalid source
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002472 /// location.
2473 SourceLocation getPointOfInstantiation() const;
David Blaikie21bfbf82011-11-09 06:07:30 +00002474
2475 /// \brief Determine whether this is or was instantiated from an out-of-line
Douglas Gregor6411b922009-09-11 20:15:17 +00002476 /// definition of a member function.
Craig Toppercbce6e92014-03-11 06:22:39 +00002477 bool isOutOfLine() const override;
David Blaikie21bfbf82011-11-09 06:07:30 +00002478
Anna Zaks22122702012-01-17 00:37:07 +00002479 /// \brief Identify a memory copying or setting function.
2480 /// If the given function is a memory copy or setting function, returns
2481 /// the corresponding Builtin ID. If the function is not a memory function,
2482 /// returns 0.
Anna Zaks28db7ce2012-01-18 02:45:01 +00002483 unsigned getMemoryFunctionKind() const;
Anna Zaks201d4892012-01-13 21:52:01 +00002484
Richard Trieue6caa262017-12-23 00:41:01 +00002485 /// \brief Returns ODRHash of the function. This value is calculated and
2486 /// stored on first call, then the stored value returned on the other calls.
2487 unsigned getODRHash();
2488
Chris Lattnerda8aa7b2006-11-19 23:12:30 +00002489 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00002490 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +00002491 static bool classofKind(Kind K) {
Alexis Hunted053252010-05-30 07:21:58 +00002492 return K >= firstFunction && K <= lastFunction;
John McCall180ef092010-01-29 01:45:37 +00002493 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +00002494 static DeclContext *castToDeclContext(const FunctionDecl *D) {
2495 return static_cast<DeclContext *>(const_cast<FunctionDecl*>(D));
2496 }
2497 static FunctionDecl *castFromDeclContext(const DeclContext *DC) {
2498 return static_cast<FunctionDecl *>(const_cast<DeclContext*>(DC));
2499 }
Chris Lattnera11999d2006-10-15 22:34:45 +00002500};
2501
James Dennett31a57342018-02-02 21:38:22 +00002502/// Represents a member of a struct/union/class.
Richard Smith0b87e072013-10-07 08:02:11 +00002503class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> {
Richard Smith6b8e3c02017-08-28 00:28:14 +00002504 unsigned BitField : 1;
David Majnemerf440d262016-06-24 04:05:35 +00002505 unsigned Mutable : 1;
Richard Smith6b8e3c02017-08-28 00:28:14 +00002506 mutable unsigned CachedFieldIndex : 30;
John McCall4e819612011-01-20 07:57:12 +00002507
John McCallc90c1492014-10-10 18:44:34 +00002508 /// The kinds of value we can store in InitializerOrBitWidth.
Richard Smith938f40b2011-06-11 17:19:42 +00002509 ///
John McCallc90c1492014-10-10 18:44:34 +00002510 /// Note that this is compatible with InClassInitStyle except for
2511 /// ISK_CapturedVLAType.
2512 enum InitStorageKind {
2513 /// If the pointer is null, there's nothing special. Otherwise,
2514 /// this is a bitfield and the pointer is the Expr* storing the
2515 /// bit-width.
Richard Smith6b8e3c02017-08-28 00:28:14 +00002516 ISK_NoInit = (unsigned) ICIS_NoInit,
John McCallc90c1492014-10-10 18:44:34 +00002517
2518 /// The pointer is an (optional due to delayed parsing) Expr*
2519 /// holding the copy-initializer.
2520 ISK_InClassCopyInit = (unsigned) ICIS_CopyInit,
2521
2522 /// The pointer is an (optional due to delayed parsing) Expr*
2523 /// holding the list-initializer.
2524 ISK_InClassListInit = (unsigned) ICIS_ListInit,
2525
2526 /// The pointer is a VariableArrayType* that's been captured;
2527 /// the enclosing context is a lambda or captured statement.
2528 ISK_CapturedVLAType,
2529 };
2530
Richard Smith6b8e3c02017-08-28 00:28:14 +00002531 /// If this is a bitfield with a default member initializer, this
2532 /// structure is used to represent the two expressions.
2533 struct InitAndBitWidth {
2534 Expr *Init;
2535 Expr *BitWidth;
2536 };
2537
2538 /// \brief Storage for either the bit-width, the in-class initializer, or
2539 /// both (via InitAndBitWidth), or the captured variable length array bound.
John McCallc90c1492014-10-10 18:44:34 +00002540 ///
2541 /// If the storage kind is ISK_InClassCopyInit or
2542 /// ISK_InClassListInit, but the initializer is null, then this
Richard Smith6b8e3c02017-08-28 00:28:14 +00002543 /// field has an in-class initializer that has not yet been parsed
Richard Smith2b013182012-06-10 03:12:00 +00002544 /// and attached.
Richard Smith6b8e3c02017-08-28 00:28:14 +00002545 // FIXME: Tail-allocate this to reduce the size of FieldDecl in the
2546 // overwhelmingly common case that we have none of these things.
John McCallc90c1492014-10-10 18:44:34 +00002547 llvm::PointerIntPair<void *, 2, InitStorageKind> InitStorage;
Richard Smith6b8e3c02017-08-28 00:28:14 +00002548
Chris Lattnerd3f989c2008-03-15 23:04:32 +00002549protected:
Abramo Bagnaradff19302011-03-08 08:55:46 +00002550 FieldDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc,
2551 SourceLocation IdLoc, IdentifierInfo *Id,
Richard Smith938f40b2011-06-11 17:19:42 +00002552 QualType T, TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smith2b013182012-06-10 03:12:00 +00002553 InClassInitStyle InitStyle)
Abramo Bagnaradff19302011-03-08 08:55:46 +00002554 : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc),
Richard Smith6b8e3c02017-08-28 00:28:14 +00002555 BitField(false), Mutable(Mutable), CachedFieldIndex(0),
2556 InitStorage(nullptr, (InitStorageKind) InitStyle) {
2557 if (BW)
2558 setBitWidth(BW);
Mike Stump11289f42009-09-09 15:08:12 +00002559 }
Douglas Gregor91f84212008-12-11 16:49:14 +00002560
Chris Lattneree1284a2008-03-16 00:16:02 +00002561public:
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002562 friend class ASTDeclReader;
2563 friend class ASTDeclWriter;
2564
Jay Foad39c79802011-01-12 09:06:06 +00002565 static FieldDecl *Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002566 SourceLocation StartLoc, SourceLocation IdLoc,
2567 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002568 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smith2b013182012-06-10 03:12:00 +00002569 InClassInitStyle InitStyle);
Steve Naroff46ba1eb2007-04-03 23:13:13 +00002570
Douglas Gregor72172e92012-01-05 21:55:30 +00002571 static FieldDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2572
James Dennettb8973efb2018-02-02 20:22:29 +00002573 /// Returns the index of this field within its record,
John McCall4e819612011-01-20 07:57:12 +00002574 /// as appropriate for passing to ASTRecordLayout::getFieldOffset.
2575 unsigned getFieldIndex() const;
2576
James Dennettb8973efb2018-02-02 20:22:29 +00002577 /// Determines whether this field is mutable (C++ only).
Douglas Gregor91f84212008-12-11 16:49:14 +00002578 bool isMutable() const { return Mutable; }
2579
James Dennettb8973efb2018-02-02 20:22:29 +00002580 /// Determines whether this field is a bitfield.
Richard Smith6b8e3c02017-08-28 00:28:14 +00002581 bool isBitField() const { return BitField; }
Douglas Gregor91f84212008-12-11 16:49:14 +00002582
James Dennettb8973efb2018-02-02 20:22:29 +00002583 /// Determines whether this is an unnamed bitfield.
Richard Smith938f40b2011-06-11 17:19:42 +00002584 bool isUnnamedBitfield() const { return isBitField() && !getDeclName(); }
Douglas Gregor347f7ea2009-01-28 21:54:33 +00002585
James Dennettb8973efb2018-02-02 20:22:29 +00002586 /// Determines whether this field is a
Douglas Gregorf4d33272009-01-07 19:46:03 +00002587 /// representative for an anonymous struct or union. Such fields are
2588 /// unnamed and are implicitly generated by the implementation to
2589 /// store the data for the anonymous union or struct.
2590 bool isAnonymousStructOrUnion() const;
2591
Richard Smith938f40b2011-06-11 17:19:42 +00002592 Expr *getBitWidth() const {
Richard Smith6b8e3c02017-08-28 00:28:14 +00002593 if (!BitField)
2594 return nullptr;
2595 void *Ptr = InitStorage.getPointer();
2596 if (getInClassInitStyle())
2597 return static_cast<InitAndBitWidth*>(Ptr)->BitWidth;
2598 return static_cast<Expr*>(Ptr);
Richard Smith938f40b2011-06-11 17:19:42 +00002599 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002600
Richard Smithcaf33902011-10-10 18:28:20 +00002601 unsigned getBitWidthValue(const ASTContext &Ctx) const;
Richard Smith2b013182012-06-10 03:12:00 +00002602
James Dennettb8973efb2018-02-02 20:22:29 +00002603 /// Set the bit-field width for this member.
Abramo Bagnarab1cdde72012-07-02 20:35:48 +00002604 // Note: used by some clients (i.e., do not remove it).
John McCallc90c1492014-10-10 18:44:34 +00002605 void setBitWidth(Expr *Width) {
Richard Smith6b8e3c02017-08-28 00:28:14 +00002606 assert(!hasCapturedVLAType() && !BitField &&
2607 "bit width or captured type already set");
2608 assert(Width && "no bit width specified");
2609 InitStorage.setPointer(
2610 InitStorage.getInt()
2611 ? new (getASTContext())
2612 InitAndBitWidth{getInClassInitializer(), Width}
2613 : static_cast<void*>(Width));
2614 BitField = true;
John McCallc90c1492014-10-10 18:44:34 +00002615 }
2616
James Dennettb8973efb2018-02-02 20:22:29 +00002617 /// Remove the bit-field width from this member.
Abramo Bagnarab1cdde72012-07-02 20:35:48 +00002618 // Note: used by some clients (i.e., do not remove it).
2619 void removeBitWidth() {
2620 assert(isBitField() && "no bitfield width to remove");
Richard Smith6b8e3c02017-08-28 00:28:14 +00002621 InitStorage.setPointer(getInClassInitializer());
2622 BitField = false;
Abramo Bagnarab1cdde72012-07-02 20:35:48 +00002623 }
2624
Richard Smith6b8e3c02017-08-28 00:28:14 +00002625 /// Get the kind of (C++11) default member initializer that this field has.
Richard Smith2b013182012-06-10 03:12:00 +00002626 InClassInitStyle getInClassInitStyle() const {
John McCallc90c1492014-10-10 18:44:34 +00002627 InitStorageKind storageKind = InitStorage.getInt();
2628 return (storageKind == ISK_CapturedVLAType
2629 ? ICIS_NoInit : (InClassInitStyle) storageKind);
Abramo Bagnara612f6dc2011-07-11 08:43:20 +00002630 }
Richard Smith938f40b2011-06-11 17:19:42 +00002631
Richard Smith6b8e3c02017-08-28 00:28:14 +00002632 /// Determine whether this member has a C++11 default member initializer.
Richard Smith938f40b2011-06-11 17:19:42 +00002633 bool hasInClassInitializer() const {
Richard Smith2b013182012-06-10 03:12:00 +00002634 return getInClassInitStyle() != ICIS_NoInit;
Richard Smith938f40b2011-06-11 17:19:42 +00002635 }
John McCallc90c1492014-10-10 18:44:34 +00002636
Richard Smith6b8e3c02017-08-28 00:28:14 +00002637 /// Get the C++11 default member initializer for this member, or null if one
2638 /// has not been set. If a valid declaration has a default member initializer,
2639 /// but this returns null, then we have not parsed and attached it yet.
Richard Smith938f40b2011-06-11 17:19:42 +00002640 Expr *getInClassInitializer() const {
Richard Smith6b8e3c02017-08-28 00:28:14 +00002641 if (!hasInClassInitializer())
2642 return nullptr;
2643 void *Ptr = InitStorage.getPointer();
2644 if (BitField)
2645 return static_cast<InitAndBitWidth*>(Ptr)->Init;
2646 return static_cast<Expr*>(Ptr);
Richard Smith938f40b2011-06-11 17:19:42 +00002647 }
John McCallc90c1492014-10-10 18:44:34 +00002648
James Dennettb8973efb2018-02-02 20:22:29 +00002649 /// Set the C++11 in-class initializer for this member.
John McCallc90c1492014-10-10 18:44:34 +00002650 void setInClassInitializer(Expr *Init) {
Richard Smith6b8e3c02017-08-28 00:28:14 +00002651 assert(hasInClassInitializer() && !getInClassInitializer());
2652 if (BitField)
2653 static_cast<InitAndBitWidth*>(InitStorage.getPointer())->Init = Init;
2654 else
2655 InitStorage.setPointer(Init);
John McCallc90c1492014-10-10 18:44:34 +00002656 }
2657
James Dennettb8973efb2018-02-02 20:22:29 +00002658 /// Remove the C++11 in-class initializer from this member.
Richard Smith938f40b2011-06-11 17:19:42 +00002659 void removeInClassInitializer() {
Richard Smith2b013182012-06-10 03:12:00 +00002660 assert(hasInClassInitializer() && "no initializer to remove");
Richard Smith6b8e3c02017-08-28 00:28:14 +00002661 InitStorage.setPointerAndInt(getBitWidth(), ISK_NoInit);
Richard Smith938f40b2011-06-11 17:19:42 +00002662 }
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002663
Alexey Bataev39c81e22014-08-28 04:28:19 +00002664 /// \brief Determine whether this member captures the variable length array
2665 /// type.
John McCallc90c1492014-10-10 18:44:34 +00002666 bool hasCapturedVLAType() const {
2667 return InitStorage.getInt() == ISK_CapturedVLAType;
2668 }
2669
Alexey Bataev39c81e22014-08-28 04:28:19 +00002670 /// \brief Get the captured variable length array type.
2671 const VariableArrayType *getCapturedVLAType() const {
2672 return hasCapturedVLAType() ? static_cast<const VariableArrayType *>(
John McCallc90c1492014-10-10 18:44:34 +00002673 InitStorage.getPointer())
Alexey Bataev39c81e22014-08-28 04:28:19 +00002674 : nullptr;
2675 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002676
Alexey Bataev39c81e22014-08-28 04:28:19 +00002677 /// \brief Set the captured variable length array type for this field.
2678 void setCapturedVLAType(const VariableArrayType *VLAType);
2679
James Dennettb8973efb2018-02-02 20:22:29 +00002680 /// Returns the parent of this field declaration, which
Malcolm Parsons2eac1d82017-05-08 16:43:29 +00002681 /// is the struct in which this field is defined.
Anders Carlsson5d8645b2010-01-29 05:05:36 +00002682 const RecordDecl *getParent() const {
2683 return cast<RecordDecl>(getDeclContext());
2684 }
2685
2686 RecordDecl *getParent() {
2687 return cast<RecordDecl>(getDeclContext());
2688 }
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002689
Craig Toppercbce6e92014-03-11 06:22:39 +00002690 SourceRange getSourceRange() const override LLVM_READONLY;
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002691
Richard Smith0b87e072013-10-07 08:02:11 +00002692 /// Retrieves the canonical declaration of this field.
Craig Toppercbce6e92014-03-11 06:22:39 +00002693 FieldDecl *getCanonicalDecl() override { return getFirstDecl(); }
Rafael Espindola8db352d2013-10-17 15:37:26 +00002694 const FieldDecl *getCanonicalDecl() const { return getFirstDecl(); }
Richard Smith0b87e072013-10-07 08:02:11 +00002695
Chris Lattnerce3a7022007-01-24 02:10:37 +00002696 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00002697 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Alexis Hunted053252010-05-30 07:21:58 +00002698 static bool classofKind(Kind K) { return K >= firstField && K <= lastField; }
Chris Lattner1300fb92007-01-23 23:42:53 +00002699};
2700
James Dennettb8973efb2018-02-02 20:22:29 +00002701/// An instance of this object exists for each enum constant
Chris Lattnerc1915e22007-01-25 07:29:02 +00002702/// that is defined. For example, in "enum X {a,b}", each of a/b are
2703/// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a
Steve Narofff1e53692007-03-23 22:27:02 +00002704/// TagType for the X EnumDecl.
Richard Smith01a73372013-10-15 22:02:41 +00002705class EnumConstantDecl : public ValueDecl, public Mergeable<EnumConstantDecl> {
Ted Kremenek85956682008-05-30 16:14:41 +00002706 Stmt *Init; // an integer constant expression
Chris Lattner23b7eb62007-06-15 23:05:46 +00002707 llvm::APSInt Val; // The value.
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002708
Chris Lattnera7b32872008-03-15 06:12:44 +00002709protected:
Chris Lattnerbec41342008-04-22 18:39:57 +00002710 EnumConstantDecl(DeclContext *DC, SourceLocation L,
Chris Lattnerc5ffed42008-04-04 06:12:32 +00002711 IdentifierInfo *Id, QualType T, Expr *E,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002712 const llvm::APSInt &V)
2713 : ValueDecl(EnumConstant, DC, L, Id, T), Init((Stmt*)E), Val(V) {}
Ted Kremenekce20e8f2008-05-20 00:43:19 +00002714
Chris Lattnera7b32872008-03-15 06:12:44 +00002715public:
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002716 friend class StmtIteratorBase;
Steve Naroff46ba1eb2007-04-03 23:13:13 +00002717
Chris Lattnerbec41342008-04-22 18:39:57 +00002718 static EnumConstantDecl *Create(ASTContext &C, EnumDecl *DC,
Chris Lattnerc5ffed42008-04-04 06:12:32 +00002719 SourceLocation L, IdentifierInfo *Id,
2720 QualType T, Expr *E,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002721 const llvm::APSInt &V);
Douglas Gregor72172e92012-01-05 21:55:30 +00002722 static EnumConstantDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2723
Ted Kremenek85956682008-05-30 16:14:41 +00002724 const Expr *getInitExpr() const { return (const Expr*) Init; }
2725 Expr *getInitExpr() { return (Expr*) Init; }
Chris Lattner23b7eb62007-06-15 23:05:46 +00002726 const llvm::APSInt &getInitVal() const { return Val; }
Chris Lattner959bcdc2007-08-29 17:23:37 +00002727
Ted Kremenek85956682008-05-30 16:14:41 +00002728 void setInitExpr(Expr *E) { Init = (Stmt*) E; }
Chris Lattnera3343a82008-02-26 00:32:36 +00002729 void setInitVal(const llvm::APSInt &V) { Val = V; }
Mike Stump11289f42009-09-09 15:08:12 +00002730
Craig Toppercbce6e92014-03-11 06:22:39 +00002731 SourceRange getSourceRange() const override LLVM_READONLY;
David Blaikie21bfbf82011-11-09 06:07:30 +00002732
Richard Smith01a73372013-10-15 22:02:41 +00002733 /// Retrieves the canonical declaration of this enumerator.
Craig Toppercbce6e92014-03-11 06:22:39 +00002734 EnumConstantDecl *getCanonicalDecl() override { return getFirstDecl(); }
Rafael Espindola8db352d2013-10-17 15:37:26 +00002735 const EnumConstantDecl *getCanonicalDecl() const { return getFirstDecl(); }
Richard Smith01a73372013-10-15 22:02:41 +00002736
Chris Lattnerc1915e22007-01-25 07:29:02 +00002737 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00002738 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +00002739 static bool classofKind(Kind K) { return K == EnumConstant; }
Chris Lattnerc1915e22007-01-25 07:29:02 +00002740};
2741
James Dennettb8973efb2018-02-02 20:22:29 +00002742/// Represents a field injected from an anonymous union/struct into the parent
2743/// scope. These are always implicit.
Richard Smith8cbd8952015-08-04 02:05:09 +00002744class IndirectFieldDecl : public ValueDecl,
2745 public Mergeable<IndirectFieldDecl> {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002746 NamedDecl **Chaining;
Benjamin Kramer39593702010-11-21 14:11:41 +00002747 unsigned ChainingSize;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002748
Richard Smith9f951822016-01-06 22:49:11 +00002749 IndirectFieldDecl(ASTContext &C, DeclContext *DC, SourceLocation L,
Francois Pichet783dd6e2010-11-21 06:08:52 +00002750 DeclarationName N, QualType T,
David Majnemer59f77922016-06-24 04:05:48 +00002751 MutableArrayRef<NamedDecl *> CH);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002752
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002753 void anchor() override;
2754
Francois Pichet783dd6e2010-11-21 06:08:52 +00002755public:
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002756 friend class ASTDeclReader;
2757
Francois Pichet783dd6e2010-11-21 06:08:52 +00002758 static IndirectFieldDecl *Create(ASTContext &C, DeclContext *DC,
2759 SourceLocation L, IdentifierInfo *Id,
David Majnemer59f77922016-06-24 04:05:48 +00002760 QualType T, llvm::MutableArrayRef<NamedDecl *> CH);
David Blaikie21bfbf82011-11-09 06:07:30 +00002761
Douglas Gregor72172e92012-01-05 21:55:30 +00002762 static IndirectFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID);
Aaron Ballman13916082014-03-07 18:11:58 +00002763
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002764 using chain_iterator = ArrayRef<NamedDecl *>::const_iterator;
David Majnemer59f77922016-06-24 04:05:48 +00002765
2766 ArrayRef<NamedDecl *> chain() const {
2767 return llvm::makeArrayRef(Chaining, ChainingSize);
Aaron Ballman13916082014-03-07 18:11:58 +00002768 }
David Majnemer59f77922016-06-24 04:05:48 +00002769 chain_iterator chain_begin() const { return chain().begin(); }
2770 chain_iterator chain_end() const { return chain().end(); }
Francois Pichet783dd6e2010-11-21 06:08:52 +00002771
Benjamin Kramer39593702010-11-21 14:11:41 +00002772 unsigned getChainingSize() const { return ChainingSize; }
Francois Pichet783dd6e2010-11-21 06:08:52 +00002773
2774 FieldDecl *getAnonField() const {
David Majnemer59f77922016-06-24 04:05:48 +00002775 assert(chain().size() >= 2);
2776 return cast<FieldDecl>(chain().back());
Francois Pichet783dd6e2010-11-21 06:08:52 +00002777 }
2778
2779 VarDecl *getVarDecl() const {
David Majnemer59f77922016-06-24 04:05:48 +00002780 assert(chain().size() >= 2);
2781 return dyn_cast<VarDecl>(chain().front());
Francois Pichet783dd6e2010-11-21 06:08:52 +00002782 }
2783
Richard Smith8cbd8952015-08-04 02:05:09 +00002784 IndirectFieldDecl *getCanonicalDecl() override { return getFirstDecl(); }
2785 const IndirectFieldDecl *getCanonicalDecl() const { return getFirstDecl(); }
2786
Francois Pichet783dd6e2010-11-21 06:08:52 +00002787 // Implement isa/cast/dyncast/etc.
2788 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Francois Pichet783dd6e2010-11-21 06:08:52 +00002789 static bool classofKind(Kind K) { return K == IndirectField; }
Francois Pichet783dd6e2010-11-21 06:08:52 +00002790};
Chris Lattnerc1915e22007-01-25 07:29:02 +00002791
James Dennettb8973efb2018-02-02 20:22:29 +00002792/// Represents a declaration of a type.
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002793class TypeDecl : public NamedDecl {
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002794 friend class ASTContext;
2795
James Dennett31a57342018-02-02 21:38:22 +00002796 /// This indicates the Type object that represents
Douglas Gregoreff93e02009-02-05 23:33:38 +00002797 /// this TypeDecl. It is a cache maintained by
2798 /// ASTContext::getTypedefType, ASTContext::getTagDeclType, and
2799 /// ASTContext::getTemplateTypeParmType, and TemplateTypeParmDecl.
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002800 mutable const Type *TypeForDecl = nullptr;
2801
James Dennett31a57342018-02-02 21:38:22 +00002802 /// The start of the source range for this declaration.
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002803 SourceLocation LocStart;
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002804
2805 void anchor() override;
Douglas Gregordee1be82009-01-17 00:42:38 +00002806
Chris Lattner0659f482007-01-26 02:12:16 +00002807protected:
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002808 TypeDecl(Kind DK, DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
2809 SourceLocation StartL = SourceLocation())
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002810 : NamedDecl(DK, DC, L, Id), LocStart(StartL) {}
Argyrios Kyrtzidis2951e142008-06-09 21:05:31 +00002811
Douglas Gregorf4d33272009-01-07 19:46:03 +00002812public:
Douglas Gregor4b12ba02012-07-17 23:09:36 +00002813 // Low-level accessor. If you just want the type defined by this node,
2814 // check out ASTContext::getTypeDeclType or one of
2815 // ASTContext::getTypedefType, ASTContext::getRecordType, etc. if you
2816 // already know the specific kind of node this is.
John McCall424cec92011-01-19 06:33:43 +00002817 const Type *getTypeForDecl() const { return TypeForDecl; }
2818 void setTypeForDecl(const Type *TD) { TypeForDecl = TD; }
Douglas Gregorb1fe2c92009-04-07 17:20:56 +00002819
Daniel Dunbare4775e12012-03-09 19:35:29 +00002820 SourceLocation getLocStart() const LLVM_READONLY { return LocStart; }
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002821 void setLocStart(SourceLocation L) { LocStart = L; }
Craig Toppercbce6e92014-03-11 06:22:39 +00002822 SourceRange getSourceRange() const override LLVM_READONLY {
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002823 if (LocStart.isValid())
2824 return SourceRange(LocStart, getLocation());
2825 else
2826 return SourceRange(getLocation());
2827 }
2828
Chris Lattner0659f482007-01-26 02:12:16 +00002829 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00002830 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Alexis Hunted053252010-05-30 07:21:58 +00002831 static bool classofKind(Kind K) { return K >= firstType && K <= lastType; }
Chris Lattner0659f482007-01-26 02:12:16 +00002832};
2833
Richard Smithdda56e42011-04-15 14:24:37 +00002834/// Base class for declarations which introduce a typedef-name.
2835class TypedefNameDecl : public TypeDecl, public Redeclarable<TypedefNameDecl> {
Benjamin Kramer4bb33342018-01-26 20:01:13 +00002836 struct LLVM_ALIGNAS(8) ModedTInfo {
2837 TypeSourceInfo *first;
2838 QualType second;
2839 };
John McCall703a3f82009-10-24 08:00:42 +00002840
Benjamin Kramerdfb730a2018-01-26 14:14:11 +00002841 /// If int part is 0, we have not computed IsTransparentTag.
2842 /// Otherwise, IsTransparentTag is (getInt() >> 1).
2843 mutable llvm::PointerIntPair<
2844 llvm::PointerUnion<TypeSourceInfo *, ModedTInfo *>, 2>
2845 MaybeModedTInfo;
Argyrios Kyrtzidis3b25c912017-03-21 16:56:02 +00002846
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002847 void anchor() override;
2848
Douglas Gregorecd99b12010-05-25 19:53:14 +00002849protected:
Richard Smith053f6c62014-05-16 23:01:30 +00002850 TypedefNameDecl(Kind DK, ASTContext &C, DeclContext *DC,
2851 SourceLocation StartLoc, SourceLocation IdLoc,
2852 IdentifierInfo *Id, TypeSourceInfo *TInfo)
2853 : TypeDecl(DK, DC, IdLoc, Id, StartLoc), redeclarable_base(C),
Benjamin Kramerdfb730a2018-01-26 14:14:11 +00002854 MaybeModedTInfo(TInfo, 0) {}
Richard Smithdda56e42011-04-15 14:24:37 +00002855
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002856 using redeclarable_base = Redeclarable<TypedefNameDecl>;
2857
Richard Smithd7af8a32014-05-10 01:17:36 +00002858 TypedefNameDecl *getNextRedeclarationImpl() override {
2859 return getNextRedeclaration();
Richard Smithdda56e42011-04-15 14:24:37 +00002860 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002861
Craig Toppercbce6e92014-03-11 06:22:39 +00002862 TypedefNameDecl *getPreviousDeclImpl() override {
Douglas Gregorec9fd132012-01-14 16:38:05 +00002863 return getPreviousDecl();
2864 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002865
Craig Toppercbce6e92014-03-11 06:22:39 +00002866 TypedefNameDecl *getMostRecentDeclImpl() override {
Douglas Gregorec9fd132012-01-14 16:38:05 +00002867 return getMostRecentDecl();
2868 }
Douglas Gregorecd99b12010-05-25 19:53:14 +00002869
Chris Lattnera7b32872008-03-15 06:12:44 +00002870public:
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002871 using redecl_range = redeclarable_base::redecl_range;
2872 using redecl_iterator = redeclarable_base::redecl_iterator;
2873
Aaron Ballman211cd8c2014-03-07 00:10:58 +00002874 using redeclarable_base::redecls_begin;
2875 using redeclarable_base::redecls_end;
Aaron Ballman86c93902014-03-06 23:45:36 +00002876 using redeclarable_base::redecls;
Douglas Gregorec9fd132012-01-14 16:38:05 +00002877 using redeclarable_base::getPreviousDecl;
2878 using redeclarable_base::getMostRecentDecl;
Rafael Espindola3f9e4442013-10-19 02:13:21 +00002879 using redeclarable_base::isFirstDecl;
Douglas Gregor0bc8a212012-01-14 15:55:47 +00002880
Benjamin Kramerdfb730a2018-01-26 14:14:11 +00002881 bool isModed() const {
2882 return MaybeModedTInfo.getPointer().is<ModedTInfo *>();
2883 }
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002884
John McCallbcd03502009-12-07 02:54:59 +00002885 TypeSourceInfo *getTypeSourceInfo() const {
Benjamin Kramerdfb730a2018-01-26 14:14:11 +00002886 return isModed() ? MaybeModedTInfo.getPointer().get<ModedTInfo *>()->first
2887 : MaybeModedTInfo.getPointer().get<TypeSourceInfo *>();
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002888 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002889
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002890 QualType getUnderlyingType() const {
Benjamin Kramerdfb730a2018-01-26 14:14:11 +00002891 return isModed() ? MaybeModedTInfo.getPointer().get<ModedTInfo *>()->second
2892 : MaybeModedTInfo.getPointer()
2893 .get<TypeSourceInfo *>()
2894 ->getType();
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002895 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002896
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002897 void setTypeSourceInfo(TypeSourceInfo *newType) {
Benjamin Kramerdfb730a2018-01-26 14:14:11 +00002898 MaybeModedTInfo.setPointer(newType);
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002899 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002900
Enea Zaffanellaa86d88c2013-06-20 12:46:19 +00002901 void setModedTypeSourceInfo(TypeSourceInfo *unmodedTSI, QualType modedTy) {
Benjamin Kramerdfb730a2018-01-26 14:14:11 +00002902 MaybeModedTInfo.setPointer(new (getASTContext(), 8)
Benjamin Kramer4bb33342018-01-26 20:01:13 +00002903 ModedTInfo({unmodedTSI, modedTy}));
John McCall703a3f82009-10-24 08:00:42 +00002904 }
2905
Richard Smithdda56e42011-04-15 14:24:37 +00002906 /// Retrieves the canonical declaration of this typedef-name.
Craig Toppercbce6e92014-03-11 06:22:39 +00002907 TypedefNameDecl *getCanonicalDecl() override { return getFirstDecl(); }
Rafael Espindola8db352d2013-10-17 15:37:26 +00002908 const TypedefNameDecl *getCanonicalDecl() const { return getFirstDecl(); }
John McCall91f1a022009-12-30 00:31:22 +00002909
Richard Smitha5230222015-03-27 01:37:43 +00002910 /// Retrieves the tag declaration for which this is the typedef name for
2911 /// linkage purposes, if any.
Richard Smith42413142015-05-15 20:05:43 +00002912 ///
2913 /// \param AnyRedecl Look for the tag declaration in any redeclaration of
2914 /// this typedef declaration.
2915 TagDecl *getAnonDeclWithTypedefName(bool AnyRedecl = false) const;
Richard Smitha5230222015-03-27 01:37:43 +00002916
Argyrios Kyrtzidis3b25c912017-03-21 16:56:02 +00002917 /// Determines if this typedef shares a name and spelling location with its
2918 /// underlying tag type, as is the case with the NS_ENUM macro.
2919 bool isTransparentTag() const {
Benjamin Kramerdfb730a2018-01-26 14:14:11 +00002920 if (MaybeModedTInfo.getInt())
2921 return MaybeModedTInfo.getInt() & 0x2;
Argyrios Kyrtzidis3b25c912017-03-21 16:56:02 +00002922 return isTransparentTagSlow();
2923 }
2924
Richard Smithdda56e42011-04-15 14:24:37 +00002925 // Implement isa/cast/dyncast/etc.
2926 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Richard Smithdda56e42011-04-15 14:24:37 +00002927 static bool classofKind(Kind K) {
2928 return K >= firstTypedefName && K <= lastTypedefName;
2929 }
Argyrios Kyrtzidis3b25c912017-03-21 16:56:02 +00002930
2931private:
2932 bool isTransparentTagSlow() const;
Richard Smithdda56e42011-04-15 14:24:37 +00002933};
2934
James Dennett31a57342018-02-02 21:38:22 +00002935/// Represents the declaration of a typedef-name via the 'typedef'
Richard Smithdda56e42011-04-15 14:24:37 +00002936/// type specifier.
2937class TypedefDecl : public TypedefNameDecl {
Richard Smith053f6c62014-05-16 23:01:30 +00002938 TypedefDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
2939 SourceLocation IdLoc, IdentifierInfo *Id, TypeSourceInfo *TInfo)
2940 : TypedefNameDecl(Typedef, C, DC, StartLoc, IdLoc, Id, TInfo) {}
Richard Smithdda56e42011-04-15 14:24:37 +00002941
2942public:
2943 static TypedefDecl *Create(ASTContext &C, DeclContext *DC,
2944 SourceLocation StartLoc, SourceLocation IdLoc,
2945 IdentifierInfo *Id, TypeSourceInfo *TInfo);
Douglas Gregor72172e92012-01-05 21:55:30 +00002946 static TypedefDecl *CreateDeserialized(ASTContext &C, unsigned ID);
Craig Toppercbce6e92014-03-11 06:22:39 +00002947
2948 SourceRange getSourceRange() const override LLVM_READONLY;
Abramo Bagnaraea947882011-03-08 16:41:52 +00002949
Chris Lattner0659f482007-01-26 02:12:16 +00002950 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00002951 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +00002952 static bool classofKind(Kind K) { return K == Typedef; }
Chris Lattner0659f482007-01-26 02:12:16 +00002953};
2954
James Dennett31a57342018-02-02 21:38:22 +00002955/// Represents the declaration of a typedef-name via a C++11
Richard Smithdda56e42011-04-15 14:24:37 +00002956/// alias-declaration.
2957class TypeAliasDecl : public TypedefNameDecl {
Richard Smith43ccec8e2014-08-26 03:52:16 +00002958 /// The template for which this is the pattern, if any.
2959 TypeAliasTemplateDecl *Template;
2960
Richard Smith053f6c62014-05-16 23:01:30 +00002961 TypeAliasDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
2962 SourceLocation IdLoc, IdentifierInfo *Id, TypeSourceInfo *TInfo)
Richard Smith43ccec8e2014-08-26 03:52:16 +00002963 : TypedefNameDecl(TypeAlias, C, DC, StartLoc, IdLoc, Id, TInfo),
2964 Template(nullptr) {}
Richard Smithdda56e42011-04-15 14:24:37 +00002965
2966public:
2967 static TypeAliasDecl *Create(ASTContext &C, DeclContext *DC,
2968 SourceLocation StartLoc, SourceLocation IdLoc,
2969 IdentifierInfo *Id, TypeSourceInfo *TInfo);
Douglas Gregor72172e92012-01-05 21:55:30 +00002970 static TypeAliasDecl *CreateDeserialized(ASTContext &C, unsigned ID);
Richard Smithdda56e42011-04-15 14:24:37 +00002971
Craig Toppercbce6e92014-03-11 06:22:39 +00002972 SourceRange getSourceRange() const override LLVM_READONLY;
Richard Smithdda56e42011-04-15 14:24:37 +00002973
Richard Smith43ccec8e2014-08-26 03:52:16 +00002974 TypeAliasTemplateDecl *getDescribedAliasTemplate() const { return Template; }
2975 void setDescribedAliasTemplate(TypeAliasTemplateDecl *TAT) { Template = TAT; }
2976
Richard Smithdda56e42011-04-15 14:24:37 +00002977 // Implement isa/cast/dyncast/etc.
2978 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Richard Smithdda56e42011-04-15 14:24:37 +00002979 static bool classofKind(Kind K) { return K == TypeAlias; }
2980};
2981
James Dennett31a57342018-02-02 21:38:22 +00002982/// Represents the declaration of a struct/union/class/enum.
Mike Stump11289f42009-09-09 15:08:12 +00002983class TagDecl
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002984 : public TypeDecl, public DeclContext, public Redeclarable<TagDecl> {
Argyrios Kyrtzidis554a07b2008-06-09 23:19:58 +00002985public:
John McCallfcc33b02009-09-05 00:15:47 +00002986 // This is really ugly.
Eugene Zelenkode0215c2017-11-13 23:01:27 +00002987 using TagKind = TagTypeKind;
Argyrios Kyrtzidis554a07b2008-06-09 23:19:58 +00002988
2989private:
Daniel Dunbar14a41c52009-03-04 02:27:50 +00002990 // FIXME: This can be packed into the bitfields in Decl.
James Dennett31a57342018-02-02 21:38:22 +00002991 /// The TagKind enum.
Joao Matosdc86f942012-08-31 18:45:21 +00002992 unsigned TagDeclKind : 3;
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00002993
James Dennett31a57342018-02-02 21:38:22 +00002994 /// True if this is a definition ("struct foo {};"), false if it is a
2995 /// declaration ("struct foo;"). It is not considered a definition
2996 /// until the definition has been fully processed.
David Majnemerf440d262016-06-24 04:05:35 +00002997 unsigned IsCompleteDefinition : 1;
Mike Stump11289f42009-09-09 15:08:12 +00002998
Douglas Gregorc8a73492012-02-13 15:44:47 +00002999protected:
James Dennett31a57342018-02-02 21:38:22 +00003000 /// True if this is currently being defined.
David Majnemerf440d262016-06-24 04:05:35 +00003001 unsigned IsBeingDefined : 1;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00003002
Douglas Gregorc8a73492012-02-13 15:44:47 +00003003private:
James Dennett31a57342018-02-02 21:38:22 +00003004 /// True if this tag declaration is "embedded" (i.e., defined or declared
3005 /// for the very first time) in the syntax of a declarator.
David Majnemerf440d262016-06-24 04:05:35 +00003006 unsigned IsEmbeddedInDeclarator : 1;
Douglas Gregor586d0f92010-02-08 22:07:33 +00003007
James Dennett31a57342018-02-02 21:38:22 +00003008 /// True if this tag is free standing, e.g. "struct foo;".
David Majnemerf440d262016-06-24 04:05:35 +00003009 unsigned IsFreeStanding : 1;
Argyrios Kyrtzidis201d3772011-09-30 22:11:31 +00003010
John McCall9aa35be2010-05-06 08:49:23 +00003011protected:
3012 // These are used by (and only defined for) EnumDecl.
3013 unsigned NumPositiveBits : 8;
3014 unsigned NumNegativeBits : 8;
3015
James Dennett31a57342018-02-02 21:38:22 +00003016 /// True if this tag declaration is a scoped enumeration. Only
Richard Smith4b38ded2012-03-14 23:13:10 +00003017 /// possible in C++11 mode.
David Majnemerf440d262016-06-24 04:05:35 +00003018 unsigned IsScoped : 1;
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003019
James Dennett31a57342018-02-02 21:38:22 +00003020 /// If this tag declaration is a scoped enum,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003021 /// then this is true if the scoped enum was declared using the class
3022 /// tag, false if it was declared with the struct tag. No meaning is
3023 /// associated if this tag declaration is not a scoped enum.
David Majnemerf440d262016-06-24 04:05:35 +00003024 unsigned IsScopedUsingClassTag : 1;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003025
James Dennett31a57342018-02-02 21:38:22 +00003026 /// True if this is an enumeration with fixed underlying type. Only
Adrian Prantlc60dc712013-04-19 19:56:39 +00003027 /// possible in C++11, Microsoft extensions, or Objective C mode.
David Majnemerf440d262016-06-24 04:05:35 +00003028 unsigned IsFixed : 1;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003029
James Dennett31a57342018-02-02 21:38:22 +00003030 /// Indicates whether it is possible for declarations of this kind
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003031 /// to have an out-of-date definition.
3032 ///
3033 /// This option is only enabled when modules are enabled.
David Majnemerf440d262016-06-24 04:05:35 +00003034 unsigned MayHaveOutOfDateDef : 1;
Douglas Gregor7dab26b2013-02-09 01:35:03 +00003035
David Blaikie48ad6dc2013-07-13 21:08:14 +00003036 /// Has the full definition of this type been required by a use somewhere in
3037 /// the TU.
David Majnemerf440d262016-06-24 04:05:35 +00003038 unsigned IsCompleteDefinitionRequired : 1;
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003039
John McCall9aa35be2010-05-06 08:49:23 +00003040private:
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00003041 SourceRange BraceRange;
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00003042
John McCall3e11ebe2010-03-15 10:12:16 +00003043 // A struct representing syntactic qualifier info,
3044 // to be used for the (uncommon) case of out-of-line declarations.
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003045 using ExtInfo = QualifierInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00003046
David Majnemer50ce8352013-09-17 23:57:10 +00003047 /// \brief If the (out-of-line) tag declaration name
John McCall3e11ebe2010-03-15 10:12:16 +00003048 /// is qualified, it points to the qualifier info (nns and range);
3049 /// otherwise, if the tag declaration is anonymous and it is part of
Richard Smithdda56e42011-04-15 14:24:37 +00003050 /// a typedef or alias, it points to the TypedefNameDecl (used for mangling);
David Majnemer50ce8352013-09-17 23:57:10 +00003051 /// otherwise, if the tag declaration is anonymous and it is used as a
3052 /// declaration specifier for variables, it points to the first VarDecl (used
3053 /// for mangling);
Richard Smithdda56e42011-04-15 14:24:37 +00003054 /// otherwise, it is a null (TypedefNameDecl) pointer.
David Majnemer00350522015-08-31 18:48:39 +00003055 llvm::PointerUnion<TypedefNameDecl *, ExtInfo *> TypedefNameDeclOrQualifier;
John McCall3e11ebe2010-03-15 10:12:16 +00003056
David Majnemer00350522015-08-31 18:48:39 +00003057 bool hasExtInfo() const { return TypedefNameDeclOrQualifier.is<ExtInfo *>(); }
3058 ExtInfo *getExtInfo() { return TypedefNameDeclOrQualifier.get<ExtInfo *>(); }
John McCall3e11ebe2010-03-15 10:12:16 +00003059 const ExtInfo *getExtInfo() const {
David Majnemer00350522015-08-31 18:48:39 +00003060 return TypedefNameDeclOrQualifier.get<ExtInfo *>();
John McCall3e11ebe2010-03-15 10:12:16 +00003061 }
3062
Chris Lattner28743e22007-01-22 07:41:08 +00003063protected:
Richard Smith053f6c62014-05-16 23:01:30 +00003064 TagDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC,
3065 SourceLocation L, IdentifierInfo *Id, TagDecl *PrevDecl,
3066 SourceLocation StartL)
3067 : TypeDecl(DK, DC, L, Id, StartL), DeclContext(DK), redeclarable_base(C),
3068 TagDeclKind(TK), IsCompleteDefinition(false), IsBeingDefined(false),
David Blaikieb05ccfe2013-07-13 22:29:32 +00003069 IsEmbeddedInDeclarator(false), IsFreeStanding(false),
3070 IsCompleteDefinitionRequired(false),
David Majnemer00350522015-08-31 18:48:39 +00003071 TypedefNameDeclOrQualifier((TypedefNameDecl *)nullptr) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00003072 assert((DK != Enum || TK == TTK_Enum) &&
3073 "EnumDecl not matched with TTK_Enum");
Rafael Espindola8db352d2013-10-17 15:37:26 +00003074 setPreviousDecl(PrevDecl);
Chris Lattner7b9ace62007-01-23 20:11:08 +00003075 }
Mike Stump11289f42009-09-09 15:08:12 +00003076
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003077 using redeclarable_base = Redeclarable<TagDecl>;
3078
Richard Smithd7af8a32014-05-10 01:17:36 +00003079 TagDecl *getNextRedeclarationImpl() override {
3080 return getNextRedeclaration();
3081 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003082
Craig Toppercbce6e92014-03-11 06:22:39 +00003083 TagDecl *getPreviousDeclImpl() override {
Douglas Gregorec9fd132012-01-14 16:38:05 +00003084 return getPreviousDecl();
3085 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003086
Craig Toppercbce6e92014-03-11 06:22:39 +00003087 TagDecl *getMostRecentDeclImpl() override {
Douglas Gregorec9fd132012-01-14 16:38:05 +00003088 return getMostRecentDecl();
3089 }
Mike Stump11289f42009-09-09 15:08:12 +00003090
Douglas Gregor8fb95122010-09-29 00:15:42 +00003091 /// @brief Completes the definition of this tag declaration.
3092 ///
3093 /// This is a helper function for derived classes.
David Blaikie21bfbf82011-11-09 06:07:30 +00003094 void completeDefinition();
3095
Chris Lattner28743e22007-01-22 07:41:08 +00003096public:
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003097 friend class ASTDeclReader;
3098 friend class ASTDeclWriter;
3099
3100 using redecl_range = redeclarable_base::redecl_range;
3101 using redecl_iterator = redeclarable_base::redecl_iterator;
3102
Aaron Ballman211cd8c2014-03-07 00:10:58 +00003103 using redeclarable_base::redecls_begin;
3104 using redeclarable_base::redecls_end;
Aaron Ballman86c93902014-03-06 23:45:36 +00003105 using redeclarable_base::redecls;
Douglas Gregorec9fd132012-01-14 16:38:05 +00003106 using redeclarable_base::getPreviousDecl;
3107 using redeclarable_base::getMostRecentDecl;
Rafael Espindola3f9e4442013-10-19 02:13:21 +00003108 using redeclarable_base::isFirstDecl;
Douglas Gregor0bc8a212012-01-14 15:55:47 +00003109
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00003110 SourceRange getBraceRange() const { return BraceRange; }
3111 void setBraceRange(SourceRange R) { BraceRange = R; }
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00003112
James Dennett31a57342018-02-02 21:38:22 +00003113 /// Return SourceLocation representing start of source
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00003114 /// range ignoring outer template declarations.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003115 SourceLocation getInnerLocStart() const { return getLocStart(); }
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00003116
James Dennett31a57342018-02-02 21:38:22 +00003117 /// Return SourceLocation representing start of source
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00003118 /// range taking into account any outer template declarations.
3119 SourceLocation getOuterLocStart() const;
Craig Toppercbce6e92014-03-11 06:22:39 +00003120 SourceRange getSourceRange() const override LLVM_READONLY;
Mike Stump11289f42009-09-09 15:08:12 +00003121
Richard Smith053f6c62014-05-16 23:01:30 +00003122 TagDecl *getCanonicalDecl() override;
3123 const TagDecl *getCanonicalDecl() const {
John McCall84c16cf2009-11-12 03:15:40 +00003124 return const_cast<TagDecl*>(this)->getCanonicalDecl();
3125 }
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00003126
James Dennett31a57342018-02-02 21:38:22 +00003127 /// Return true if this declaration is a completion definition of the type.
3128 /// Provided for consistency.
John McCallc265ad22010-08-31 22:21:26 +00003129 bool isThisDeclarationADefinition() const {
John McCallf937c022011-10-07 06:10:15 +00003130 return isCompleteDefinition();
John McCallc265ad22010-08-31 22:21:26 +00003131 }
3132
James Dennett31a57342018-02-02 21:38:22 +00003133 /// Return true if this decl has its body fully specified.
John McCallf937c022011-10-07 06:10:15 +00003134 bool isCompleteDefinition() const {
3135 return IsCompleteDefinition;
Chris Lattner7b9ace62007-01-23 20:11:08 +00003136 }
Douglas Gregore362cea2009-05-10 22:57:19 +00003137
David Blaikie48ad6dc2013-07-13 21:08:14 +00003138 /// \brief Return true if this complete decl is
3139 /// required to be complete for some existing use.
3140 bool isCompleteDefinitionRequired() const {
3141 return IsCompleteDefinitionRequired;
3142 }
3143
James Dennett31a57342018-02-02 21:38:22 +00003144 /// Return true if this decl is currently being defined.
Sebastian Redl9d8854e2010-08-02 18:27:05 +00003145 bool isBeingDefined() const {
3146 return IsBeingDefined;
3147 }
3148
Douglas Gregor5089c762010-02-12 17:40:34 +00003149 bool isEmbeddedInDeclarator() const {
3150 return IsEmbeddedInDeclarator;
Douglas Gregor586d0f92010-02-08 22:07:33 +00003151 }
Douglas Gregor5089c762010-02-12 17:40:34 +00003152 void setEmbeddedInDeclarator(bool isInDeclarator) {
3153 IsEmbeddedInDeclarator = isInDeclarator;
Douglas Gregor586d0f92010-02-08 22:07:33 +00003154 }
3155
Argyrios Kyrtzidis201d3772011-09-30 22:11:31 +00003156 bool isFreeStanding() const { return IsFreeStanding; }
3157 void setFreeStanding(bool isFreeStanding = true) {
3158 IsFreeStanding = isFreeStanding;
3159 }
3160
Douglas Gregore362cea2009-05-10 22:57:19 +00003161 /// \brief Whether this declaration declares a type that is
3162 /// dependent, i.e., a type that somehow depends on template
3163 /// parameters.
Douglas Gregor9e927ab2009-05-28 16:34:51 +00003164 bool isDependentType() const { return isDependentContext(); }
Douglas Gregore362cea2009-05-10 22:57:19 +00003165
James Dennett31a57342018-02-02 21:38:22 +00003166 /// Starts the definition of this tag declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003167 ///
Douglas Gregordee1be82009-01-17 00:42:38 +00003168 /// This method should be invoked at the beginning of the definition
3169 /// of this tag declaration. It will set the tag type into a state
3170 /// where it is in the process of being defined.
3171 void startDefinition();
3172
James Dennett31a57342018-02-02 21:38:22 +00003173 /// Returns the TagDecl that actually defines this
Ted Kremenek21475702008-09-05 17:16:31 +00003174 /// struct/union/class/enum. When determining whether or not a
John McCallf937c022011-10-07 06:10:15 +00003175 /// struct/union/class/enum has a definition, one should use this
3176 /// method as opposed to 'isDefinition'. 'isDefinition' indicates
3177 /// whether or not a specific TagDecl is defining declaration, not
3178 /// whether or not the struct/union/class/enum type is defined.
3179 /// This method returns NULL if there is no TagDecl that defines
3180 /// the struct/union/class/enum.
3181 TagDecl *getDefinition() const;
Mike Stump11289f42009-09-09 15:08:12 +00003182
John McCallf937c022011-10-07 06:10:15 +00003183 void setCompleteDefinition(bool V) { IsCompleteDefinition = V; }
Sebastian Redl833ef452010-01-26 22:01:41 +00003184
David Blaikiea8d23ce2013-07-14 01:07:41 +00003185 void setCompleteDefinitionRequired(bool V = true) {
3186 IsCompleteDefinitionRequired = V;
3187 }
David Blaikie48ad6dc2013-07-13 21:08:14 +00003188
Alp Tokera030cd02014-05-05 12:38:48 +00003189 StringRef getKindName() const {
Abramo Bagnara6150c882010-05-11 21:36:43 +00003190 return TypeWithKeyword::getTagTypeKindName(getTagKind());
Chris Lattnerfb0724632007-01-23 05:45:31 +00003191 }
Argyrios Kyrtzidis554a07b2008-06-09 23:19:58 +00003192
3193 TagKind getTagKind() const {
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00003194 return TagKind(TagDeclKind);
Argyrios Kyrtzidis554a07b2008-06-09 23:19:58 +00003195 }
3196
Douglas Gregor1daeb692009-04-13 18:14:40 +00003197 void setTagKind(TagKind TK) { TagDeclKind = TK; }
3198
Abramo Bagnara6150c882010-05-11 21:36:43 +00003199 bool isStruct() const { return getTagKind() == TTK_Struct; }
Joao Matosdc86f942012-08-31 18:45:21 +00003200 bool isInterface() const { return getTagKind() == TTK_Interface; }
Abramo Bagnara6150c882010-05-11 21:36:43 +00003201 bool isClass() const { return getTagKind() == TTK_Class; }
3202 bool isUnion() const { return getTagKind() == TTK_Union; }
3203 bool isEnum() const { return getTagKind() == TTK_Enum; }
Mike Stump11289f42009-09-09 15:08:12 +00003204
John McCall5ea95772013-03-09 00:54:27 +00003205 /// Is this tag type named, either directly or via being defined in
3206 /// a typedef of this type?
3207 ///
3208 /// C++11 [basic.link]p8:
3209 /// A type is said to have linkage if and only if:
3210 /// - it is a class or enumeration type that is named (or has a
3211 /// name for linkage purposes) and the name has linkage; ...
3212 /// C++11 [dcl.typedef]p9:
3213 /// If the typedef declaration defines an unnamed class (or enum),
3214 /// the first typedef-name declared by the declaration to be that
3215 /// class type (or enum type) is used to denote the class type (or
3216 /// enum type) for linkage purposes only.
3217 ///
3218 /// C does not have an analogous rule, but the same concept is
3219 /// nonetheless useful in some places.
3220 bool hasNameForLinkage() const {
3221 return (getDeclName() || getTypedefNameForAnonDecl());
3222 }
3223
David Majnemer50ce8352013-09-17 23:57:10 +00003224 TypedefNameDecl *getTypedefNameForAnonDecl() const {
David Majnemer00350522015-08-31 18:48:39 +00003225 return hasExtInfo() ? nullptr
3226 : TypedefNameDeclOrQualifier.get<TypedefNameDecl *>();
David Majnemer50ce8352013-09-17 23:57:10 +00003227 }
3228
Richard Smithdda56e42011-04-15 14:24:37 +00003229 void setTypedefNameForAnonDecl(TypedefNameDecl *TDD);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00003230
Douglas Gregor14454802011-02-25 02:25:35 +00003231 /// \brief Retrieve the nested-name-specifier that qualifies the name of this
3232 /// declaration, if it was present in the source.
John McCall3e11ebe2010-03-15 10:12:16 +00003233 NestedNameSpecifier *getQualifier() const {
Douglas Gregor14454802011-02-25 02:25:35 +00003234 return hasExtInfo() ? getExtInfo()->QualifierLoc.getNestedNameSpecifier()
Craig Topper34b4c432014-05-06 06:48:52 +00003235 : nullptr;
John McCall3e11ebe2010-03-15 10:12:16 +00003236 }
David Blaikie21bfbf82011-11-09 06:07:30 +00003237
3238 /// \brief Retrieve the nested-name-specifier (with source-location
3239 /// information) that qualifies the name of this declaration, if it was
Douglas Gregor14454802011-02-25 02:25:35 +00003240 /// present in the source.
3241 NestedNameSpecifierLoc getQualifierLoc() const {
3242 return hasExtInfo() ? getExtInfo()->QualifierLoc
3243 : NestedNameSpecifierLoc();
John McCall3e11ebe2010-03-15 10:12:16 +00003244 }
David Blaikie21bfbf82011-11-09 06:07:30 +00003245
Douglas Gregor14454802011-02-25 02:25:35 +00003246 void setQualifierInfo(NestedNameSpecifierLoc QualifierLoc);
Mike Stump11289f42009-09-09 15:08:12 +00003247
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00003248 unsigned getNumTemplateParameterLists() const {
3249 return hasExtInfo() ? getExtInfo()->NumTemplParamLists : 0;
3250 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003251
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00003252 TemplateParameterList *getTemplateParameterList(unsigned i) const {
3253 assert(i < getNumTemplateParameterLists());
3254 return getExtInfo()->TemplParamLists[i];
3255 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003256
Benjamin Kramer9cc210652015-08-05 09:40:49 +00003257 void setTemplateParameterListsInfo(ASTContext &Context,
3258 ArrayRef<TemplateParameterList *> TPLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00003259
Chris Lattner28743e22007-01-22 07:41:08 +00003260 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00003261 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Alexis Hunted053252010-05-30 07:21:58 +00003262 static bool classofKind(Kind K) { return K >= firstTag && K <= lastTag; }
Douglas Gregordee1be82009-01-17 00:42:38 +00003263
3264 static DeclContext *castToDeclContext(const TagDecl *D) {
3265 return static_cast<DeclContext *>(const_cast<TagDecl*>(D));
3266 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003267
Douglas Gregordee1be82009-01-17 00:42:38 +00003268 static TagDecl *castFromDeclContext(const DeclContext *DC) {
3269 return static_cast<TagDecl *>(const_cast<DeclContext*>(DC));
3270 }
Chris Lattner28743e22007-01-22 07:41:08 +00003271};
3272
James Dennett31a57342018-02-02 21:38:22 +00003273/// Represents an enum. In C++11, enums can be forward-declared
Richard Smith4b38ded2012-03-14 23:13:10 +00003274/// with a fixed underlying type, and in C we allow them to be forward-declared
3275/// with no underlying type as an extension.
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003276class EnumDecl : public TagDecl {
James Dennett31a57342018-02-02 21:38:22 +00003277 /// This represent the integer type that the enum corresponds
Chris Lattner1c1f9322007-08-28 18:24:31 +00003278 /// to for code generation purposes. Note that the enumerator constants may
3279 /// have a different type than this does.
Douglas Gregor0bf31402010-10-08 23:50:27 +00003280 ///
3281 /// If the underlying integer type was explicitly stated in the source
3282 /// code, this is a TypeSourceInfo* for that type. Otherwise this type
3283 /// was automatically deduced somehow, and this is a Type*.
3284 ///
3285 /// Normally if IsFixed(), this would contain a TypeSourceInfo*, but in
3286 /// some cases it won't.
3287 ///
3288 /// The underlying type of an enumeration never has any qualifiers, so
3289 /// we can get away with just storing a raw Type*, and thus save an
3290 /// extra pointer when TypeSourceInfo is needed.
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003291 llvm::PointerUnion<const Type *, TypeSourceInfo *> IntegerType;
Douglas Gregor7a749382009-05-27 17:20:35 +00003292
James Dennett31a57342018-02-02 21:38:22 +00003293 /// The integer type that values of this type should
John McCall56774992009-12-09 09:09:27 +00003294 /// promote to. In C, enumerators are generally of an integer type
3295 /// directly, but gcc-style large enumerators (and all enumerators
3296 /// in C++) are of the enum type instead.
3297 QualType PromotionType;
3298
Richard Smith4b38ded2012-03-14 23:13:10 +00003299 /// \brief If this enumeration is an instantiation of a member enumeration
3300 /// of a class template specialization, this is the member specialization
3301 /// information.
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003302 MemberSpecializationInfo *SpecializationInfo = nullptr;
Douglas Gregor7a749382009-05-27 17:20:35 +00003303
Richard Smith053f6c62014-05-16 23:01:30 +00003304 EnumDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
3305 SourceLocation IdLoc, IdentifierInfo *Id, EnumDecl *PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003306 bool Scoped, bool ScopedUsingClassTag, bool Fixed)
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003307 : TagDecl(Enum, TTK_Enum, C, DC, IdLoc, Id, PrevDecl, StartLoc) {
Abramo Bagnara50228632011-03-06 16:09:14 +00003308 assert(Scoped || !ScopedUsingClassTag);
Craig Topper34b4c432014-05-06 06:48:52 +00003309 IntegerType = (const Type *)nullptr;
Abramo Bagnara50228632011-03-06 16:09:14 +00003310 NumNegativeBits = 0;
3311 NumPositiveBits = 0;
3312 IsScoped = Scoped;
3313 IsScopedUsingClassTag = ScopedUsingClassTag;
3314 IsFixed = Fixed;
3315 }
Richard Smith4b38ded2012-03-14 23:13:10 +00003316
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003317 void anchor() override;
3318
Richard Smith4b38ded2012-03-14 23:13:10 +00003319 void setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
3320 TemplateSpecializationKind TSK);
Chris Lattnera7b32872008-03-15 06:12:44 +00003321public:
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003322 friend class ASTDeclReader;
3323
Craig Toppercbce6e92014-03-11 06:22:39 +00003324 EnumDecl *getCanonicalDecl() override {
John McCall59660882009-08-29 08:11:13 +00003325 return cast<EnumDecl>(TagDecl::getCanonicalDecl());
3326 }
John McCall84c16cf2009-11-12 03:15:40 +00003327 const EnumDecl *getCanonicalDecl() const {
Rafael Espindola7b56f6c2013-10-19 16:55:03 +00003328 return const_cast<EnumDecl*>(this)->getCanonicalDecl();
John McCall84c16cf2009-11-12 03:15:40 +00003329 }
John McCall59660882009-08-29 08:11:13 +00003330
Douglas Gregorec9fd132012-01-14 16:38:05 +00003331 EnumDecl *getPreviousDecl() {
Aaron Ballman5116a8e2013-11-06 22:39:46 +00003332 return cast_or_null<EnumDecl>(
3333 static_cast<TagDecl *>(this)->getPreviousDecl());
Douglas Gregorec9fd132012-01-14 16:38:05 +00003334 }
Rafael Espindola7b56f6c2013-10-19 16:55:03 +00003335 const EnumDecl *getPreviousDecl() const {
3336 return const_cast<EnumDecl*>(this)->getPreviousDecl();
Douglas Gregorec9fd132012-01-14 16:38:05 +00003337 }
Rafael Espindola7b56f6c2013-10-19 16:55:03 +00003338
Douglas Gregorec9fd132012-01-14 16:38:05 +00003339 EnumDecl *getMostRecentDecl() {
Aaron Ballman5116a8e2013-11-06 22:39:46 +00003340 return cast<EnumDecl>(static_cast<TagDecl *>(this)->getMostRecentDecl());
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00003341 }
Rafael Espindola7b56f6c2013-10-19 16:55:03 +00003342 const EnumDecl *getMostRecentDecl() const {
3343 return const_cast<EnumDecl*>(this)->getMostRecentDecl();
3344 }
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00003345
Richard Smith4b38ded2012-03-14 23:13:10 +00003346 EnumDecl *getDefinition() const {
3347 return cast_or_null<EnumDecl>(TagDecl::getDefinition());
3348 }
3349
Chris Lattnerbec41342008-04-22 18:39:57 +00003350 static EnumDecl *Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003351 SourceLocation StartLoc, SourceLocation IdLoc,
3352 IdentifierInfo *Id, EnumDecl *PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003353 bool IsScoped, bool IsScopedUsingClassTag,
3354 bool IsFixed);
Douglas Gregor72172e92012-01-05 21:55:30 +00003355 static EnumDecl *CreateDeserialized(ASTContext &C, unsigned ID);
Mike Stump11289f42009-09-09 15:08:12 +00003356
James Dennett31a57342018-02-02 21:38:22 +00003357 /// When created, the EnumDecl corresponds to a
Douglas Gregor91f84212008-12-11 16:49:14 +00003358 /// forward-declared enum. This method is used to mark the
James Dennett31a57342018-02-02 21:38:22 +00003359 /// declaration as being defined; its enumerators have already been
Douglas Gregor91f84212008-12-11 16:49:14 +00003360 /// added (via DeclContext::addDecl). NewType is the new underlying
3361 /// type of the enumeration type.
Douglas Gregord5058122010-02-11 01:19:42 +00003362 void completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00003363 QualType PromotionType,
3364 unsigned NumPositiveBits,
3365 unsigned NumNegativeBits);
Mike Stump11289f42009-09-09 15:08:12 +00003366
James Dennett31a57342018-02-02 21:38:22 +00003367 // Iterates through the enumerators of this enumeration.
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003368 using enumerator_iterator = specific_decl_iterator<EnumConstantDecl>;
3369 using enumerator_range =
3370 llvm::iterator_range<specific_decl_iterator<EnumConstantDecl>>;
Aaron Ballman23a6dcb2014-03-08 18:45:14 +00003371
3372 enumerator_range enumerators() const {
3373 return enumerator_range(enumerator_begin(), enumerator_end());
3374 }
Douglas Gregor91f84212008-12-11 16:49:14 +00003375
Mike Stump11289f42009-09-09 15:08:12 +00003376 enumerator_iterator enumerator_begin() const {
Richard Smith4b38ded2012-03-14 23:13:10 +00003377 const EnumDecl *E = getDefinition();
Douglas Gregora46d6612010-06-22 14:45:56 +00003378 if (!E)
3379 E = this;
3380 return enumerator_iterator(E->decls_begin());
Douglas Gregor91f84212008-12-11 16:49:14 +00003381 }
3382
Mike Stump11289f42009-09-09 15:08:12 +00003383 enumerator_iterator enumerator_end() const {
Richard Smith4b38ded2012-03-14 23:13:10 +00003384 const EnumDecl *E = getDefinition();
Douglas Gregora46d6612010-06-22 14:45:56 +00003385 if (!E)
3386 E = this;
3387 return enumerator_iterator(E->decls_end());
Douglas Gregor91f84212008-12-11 16:49:14 +00003388 }
3389
James Dennett31a57342018-02-02 21:38:22 +00003390 /// Return the integer type that enumerators should promote to.
John McCall56774992009-12-09 09:09:27 +00003391 QualType getPromotionType() const { return PromotionType; }
3392
James Dennett31a57342018-02-02 21:38:22 +00003393 /// Set the promotion type.
John McCall56774992009-12-09 09:09:27 +00003394 void setPromotionType(QualType T) { PromotionType = T; }
3395
James Dennett31a57342018-02-02 21:38:22 +00003396 /// Return the integer type this enum decl corresponds to.
Richard Smith8bcc0862014-01-08 01:16:19 +00003397 /// This returns a null QualType for an enum forward definition with no fixed
3398 /// underlying type.
Douglas Gregor0bf31402010-10-08 23:50:27 +00003399 QualType getIntegerType() const {
3400 if (!IntegerType)
3401 return QualType();
Richard Smith8bcc0862014-01-08 01:16:19 +00003402 if (const Type *T = IntegerType.dyn_cast<const Type*>())
Douglas Gregor0bf31402010-10-08 23:50:27 +00003403 return QualType(T, 0);
Richard Smith8bcc0862014-01-08 01:16:19 +00003404 return IntegerType.get<TypeSourceInfo*>()->getType().getUnqualifiedType();
Douglas Gregor0bf31402010-10-08 23:50:27 +00003405 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00003406
3407 /// \brief Set the underlying integer type.
Douglas Gregor0cdc8322010-12-10 17:03:06 +00003408 void setIntegerType(QualType T) { IntegerType = T.getTypePtrOrNull(); }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003409
3410 /// \brief Set the underlying integer type source info.
Richard Smith8bcc0862014-01-08 01:16:19 +00003411 void setIntegerTypeSourceInfo(TypeSourceInfo *TInfo) { IntegerType = TInfo; }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003412
3413 /// \brief Return the type source info for the underlying integer type,
3414 /// if no type source info exists, return 0.
Richard Smith8bcc0862014-01-08 01:16:19 +00003415 TypeSourceInfo *getIntegerTypeSourceInfo() const {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003416 return IntegerType.dyn_cast<TypeSourceInfo*>();
3417 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00003418
Alp Tokerb9fa5122014-01-06 11:31:18 +00003419 /// \brief Retrieve the source range that covers the underlying type if
3420 /// specified.
3421 SourceRange getIntegerTypeRange() const LLVM_READONLY;
3422
Chris Lattner57540c52011-04-15 05:22:18 +00003423 /// \brief Returns the width in bits required to store all the
John McCall9aa35be2010-05-06 08:49:23 +00003424 /// non-negative enumerators of this enum.
3425 unsigned getNumPositiveBits() const {
3426 return NumPositiveBits;
3427 }
3428 void setNumPositiveBits(unsigned Num) {
3429 NumPositiveBits = Num;
3430 assert(NumPositiveBits == Num && "can't store this bitcount");
3431 }
3432
Chris Lattner57540c52011-04-15 05:22:18 +00003433 /// \brief Returns the width in bits required to store all the
John McCall9aa35be2010-05-06 08:49:23 +00003434 /// negative enumerators of this enum. These widths include
3435 /// the rightmost leading 1; that is:
David Blaikie21bfbf82011-11-09 06:07:30 +00003436 ///
John McCall9aa35be2010-05-06 08:49:23 +00003437 /// MOST NEGATIVE ENUMERATOR PATTERN NUM NEGATIVE BITS
3438 /// ------------------------ ------- -----------------
3439 /// -1 1111111 1
3440 /// -10 1110110 5
3441 /// -101 1001011 8
3442 unsigned getNumNegativeBits() const {
3443 return NumNegativeBits;
3444 }
3445 void setNumNegativeBits(unsigned Num) {
3446 NumNegativeBits = Num;
3447 }
3448
Adrian Prantl6847fe32013-04-19 19:56:35 +00003449 /// \brief Returns true if this is a C++11 scoped enumeration.
Douglas Gregor0bf31402010-10-08 23:50:27 +00003450 bool isScoped() const {
3451 return IsScoped;
3452 }
3453
Adrian Prantl6847fe32013-04-19 19:56:35 +00003454 /// \brief Returns true if this is a C++11 scoped enumeration.
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003455 bool isScopedUsingClassTag() const {
3456 return IsScopedUsingClassTag;
3457 }
3458
Adrian Prantlc60dc712013-04-19 19:56:39 +00003459 /// \brief Returns true if this is an Objective-C, C++11, or
3460 /// Microsoft-style enumeration with a fixed underlying type.
Douglas Gregor0bf31402010-10-08 23:50:27 +00003461 bool isFixed() const {
3462 return IsFixed;
3463 }
3464
3465 /// \brief Returns true if this can be considered a complete type.
3466 bool isComplete() const {
Reid Klecknerb0a17ed2018-02-12 17:37:06 +00003467 // IntegerType is set for fixed type enums and non-fixed but implicitly
3468 // int-sized Microsoft enums.
3469 return isCompleteDefinition() || IntegerType;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003470 }
3471
Akira Hatanaka3c268af2017-03-21 02:23:00 +00003472 /// Returns true if this enum is either annotated with
3473 /// enum_extensibility(closed) or isn't annotated with enum_extensibility.
3474 bool isClosed() const;
3475
3476 /// Returns true if this enum is annotated with flag_enum and isn't annotated
3477 /// with enum_extensibility(open).
3478 bool isClosedFlag() const;
3479
3480 /// Returns true if this enum is annotated with neither flag_enum nor
3481 /// enum_extensibility(open).
3482 bool isClosedNonFlag() const;
3483
Richard Smith6739a102016-05-05 00:56:12 +00003484 /// \brief Retrieve the enum definition from which this enumeration could
3485 /// be instantiated, if it is an instantiation (rather than a non-template).
3486 EnumDecl *getTemplateInstantiationPattern() const;
3487
Douglas Gregor7a749382009-05-27 17:20:35 +00003488 /// \brief Returns the enumeration (declared within the template)
3489 /// from which this enumeration type was instantiated, or NULL if
3490 /// this enumeration was not instantiated from any template.
Richard Smith4b38ded2012-03-14 23:13:10 +00003491 EnumDecl *getInstantiatedFromMemberEnum() const;
3492
Richard Smith7d137e32012-03-23 03:33:32 +00003493 /// \brief If this enumeration is a member of a specialization of a
3494 /// templated class, determine what kind of template specialization
3495 /// or instantiation this is.
3496 TemplateSpecializationKind getTemplateSpecializationKind() const;
3497
3498 /// \brief For an enumeration member that was instantiated from a member
3499 /// enumeration of a templated class, set the template specialiation kind.
3500 void setTemplateSpecializationKind(TemplateSpecializationKind TSK,
3501 SourceLocation PointOfInstantiation = SourceLocation());
3502
Richard Smith4b38ded2012-03-14 23:13:10 +00003503 /// \brief If this enumeration is an instantiation of a member enumeration of
3504 /// a class template specialization, retrieves the member specialization
3505 /// information.
3506 MemberSpecializationInfo *getMemberSpecializationInfo() const {
3507 return SpecializationInfo;
Douglas Gregor7a749382009-05-27 17:20:35 +00003508 }
3509
Richard Smith4b38ded2012-03-14 23:13:10 +00003510 /// \brief Specify that this enumeration is an instantiation of the
3511 /// member enumeration ED.
3512 void setInstantiationOfMemberEnum(EnumDecl *ED,
3513 TemplateSpecializationKind TSK) {
3514 setInstantiationOfMemberEnum(getASTContext(), ED, TSK);
3515 }
Douglas Gregor7a749382009-05-27 17:20:35 +00003516
John McCall180ef092010-01-29 01:45:37 +00003517 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +00003518 static bool classofKind(Kind K) { return K == Enum; }
Chris Lattner5f521502007-01-25 06:27:24 +00003519};
3520
James Dennett31a57342018-02-02 21:38:22 +00003521/// Represents a struct/union/class. For example:
Chris Lattner90c26ba2007-09-30 08:13:22 +00003522/// struct X; // Forward declaration, no "body".
3523/// union Y { int A, B; }; // Has body with members A and B (FieldDecls).
Steve Naroffb5fc2552008-02-11 21:52:37 +00003524/// This decl will be marked invalid if *any* members are invalid.
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003525class RecordDecl : public TagDecl {
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003526 friend class DeclContext;
3527
Daniel Dunbar14a41c52009-03-04 02:27:50 +00003528 // FIXME: This can be packed into the bitfields in Decl.
James Dennett31a57342018-02-02 21:38:22 +00003529 /// This is true if this struct ends with a flexible
Chris Lattner720a0542007-01-25 00:44:24 +00003530 /// array member (e.g. int X[]) or if this union contains a struct that does.
3531 /// If so, this cannot be contained in arrays or other structs as a member.
Chris Lattner41943152007-01-25 04:52:46 +00003532 bool HasFlexibleArrayMember : 1;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003533
James Dennett31a57342018-02-02 21:38:22 +00003534 /// Whether this is the type of an anonymous struct or union.
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003535 bool AnonymousStructOrUnion : 1;
Mike Stump11289f42009-09-09 15:08:12 +00003536
James Dennett31a57342018-02-02 21:38:22 +00003537 /// This is true if this struct has at least one member
Eli Friedman433aae62012-02-06 23:34:35 +00003538 /// containing an Objective-C object pointer type.
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003539 bool HasObjectMember : 1;
James Dennett31a57342018-02-02 21:38:22 +00003540
3541 /// This is true if struct has at least one member of
Fariborz Jahanian78652202013-01-25 23:57:05 +00003542 /// 'volatile' type.
3543 bool HasVolatileMember : 1;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003544
James Dennett31a57342018-02-02 21:38:22 +00003545 /// Whether the field declarations of this record have been loaded
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003546 /// from external storage. To avoid unnecessary deserialization of
3547 /// methods/nested types we allow deserialization of just the fields
3548 /// when needed.
3549 mutable bool LoadedFieldsFromExternalStorage : 1;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003550
Akira Hatanaka7275da02018-02-28 07:15:55 +00003551 /// Basic properties of non-trivial C structs.
3552 bool NonTrivialToPrimitiveDefaultInitialize : 1;
3553 bool NonTrivialToPrimitiveCopy : 1;
3554 bool NonTrivialToPrimitiveDestroy : 1;
3555
Argyrios Kyrtzidis2951e142008-06-09 21:05:31 +00003556protected:
Richard Smith053f6c62014-05-16 23:01:30 +00003557 RecordDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003558 SourceLocation StartLoc, SourceLocation IdLoc,
3559 IdentifierInfo *Id, RecordDecl *PrevDecl);
Argyrios Kyrtzidis6bd44af2008-08-08 14:08:55 +00003560
Chris Lattnera7b32872008-03-15 06:12:44 +00003561public:
Jay Foad39c79802011-01-12 09:06:06 +00003562 static RecordDecl *Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003563 SourceLocation StartLoc, SourceLocation IdLoc,
Craig Topper34b4c432014-05-06 06:48:52 +00003564 IdentifierInfo *Id, RecordDecl* PrevDecl = nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00003565 static RecordDecl *CreateDeserialized(const ASTContext &C, unsigned ID);
Argyrios Kyrtzidis69bc5ba2008-08-08 22:25:06 +00003566
Douglas Gregorec9fd132012-01-14 16:38:05 +00003567 RecordDecl *getPreviousDecl() {
Aaron Ballman5116a8e2013-11-06 22:39:46 +00003568 return cast_or_null<RecordDecl>(
3569 static_cast<TagDecl *>(this)->getPreviousDecl());
Douglas Gregorec9fd132012-01-14 16:38:05 +00003570 }
Rafael Espindola7b56f6c2013-10-19 16:55:03 +00003571 const RecordDecl *getPreviousDecl() const {
3572 return const_cast<RecordDecl*>(this)->getPreviousDecl();
Douglas Gregorec9fd132012-01-14 16:38:05 +00003573 }
Rafael Espindola7b56f6c2013-10-19 16:55:03 +00003574
Douglas Gregorec9fd132012-01-14 16:38:05 +00003575 RecordDecl *getMostRecentDecl() {
Aaron Ballman5116a8e2013-11-06 22:39:46 +00003576 return cast<RecordDecl>(static_cast<TagDecl *>(this)->getMostRecentDecl());
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00003577 }
Rafael Espindola7b56f6c2013-10-19 16:55:03 +00003578 const RecordDecl *getMostRecentDecl() const {
3579 return const_cast<RecordDecl*>(this)->getMostRecentDecl();
3580 }
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00003581
Chris Lattner720a0542007-01-25 00:44:24 +00003582 bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember; }
3583 void setHasFlexibleArrayMember(bool V) { HasFlexibleArrayMember = V; }
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003584
James Dennett31a57342018-02-02 21:38:22 +00003585 /// Whether this is an anonymous struct or union. To be an anonymous
3586 /// struct or union, it must have been declared without a name and
3587 /// there must be no objects of this type declared, e.g.,
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003588 /// @code
3589 /// union { int i; float f; };
Mike Stump11289f42009-09-09 15:08:12 +00003590 /// @endcode
Douglas Gregor9ac7a072009-01-07 00:43:41 +00003591 /// is an anonymous union but neither of the following are:
3592 /// @code
3593 /// union X { int i; float f; };
3594 /// union { int i; float f; } obj;
3595 /// @endcode
3596 bool isAnonymousStructOrUnion() const { return AnonymousStructOrUnion; }
3597 void setAnonymousStructOrUnion(bool Anon) {
3598 AnonymousStructOrUnion = Anon;
3599 }
3600
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003601 bool hasObjectMember() const { return HasObjectMember; }
3602 void setHasObjectMember (bool val) { HasObjectMember = val; }
Mike Stump11289f42009-09-09 15:08:12 +00003603
Fariborz Jahanian78652202013-01-25 23:57:05 +00003604 bool hasVolatileMember() const { return HasVolatileMember; }
3605 void setHasVolatileMember (bool val) { HasVolatileMember = val; }
Adrian Prantl354bebd2015-12-01 19:54:07 +00003606
Adrian Prantld6ec3bc2015-12-01 20:19:44 +00003607 bool hasLoadedFieldsFromExternalStorage() const {
3608 return LoadedFieldsFromExternalStorage;
3609 }
3610 void setHasLoadedFieldsFromExternalStorage(bool val) {
3611 LoadedFieldsFromExternalStorage = val;
3612 }
Adrian Prantl354bebd2015-12-01 19:54:07 +00003613
Akira Hatanaka7275da02018-02-28 07:15:55 +00003614 /// Functions to query basic properties of non-trivial C structs.
3615 bool isNonTrivialToPrimitiveDefaultInitialize() const {
3616 return NonTrivialToPrimitiveDefaultInitialize;
3617 }
3618
3619 void setNonTrivialToPrimitiveDefaultInitialize() {
3620 NonTrivialToPrimitiveDefaultInitialize = true;
3621 }
3622
3623 bool isNonTrivialToPrimitiveCopy() const {
3624 return NonTrivialToPrimitiveCopy;
3625 }
3626
3627 void setNonTrivialToPrimitiveCopy() {
3628 NonTrivialToPrimitiveCopy = true;
3629 }
3630
3631 bool isNonTrivialToPrimitiveDestroy() const {
3632 return NonTrivialToPrimitiveDestroy;
3633 }
3634
3635 void setNonTrivialToPrimitiveDestroy() {
3636 NonTrivialToPrimitiveDestroy = true;
3637 }
3638
Douglas Gregordfcad112009-03-25 15:59:44 +00003639 /// \brief Determines whether this declaration represents the
3640 /// injected class name.
3641 ///
3642 /// The injected class name in C++ is the name of the class that
3643 /// appears inside the class itself. For example:
3644 ///
3645 /// \code
3646 /// struct C {
3647 /// // C is implicitly declared here as a synonym for the class name.
3648 /// };
3649 ///
3650 /// C::C c; // same as "C c;"
3651 /// \endcode
3652 bool isInjectedClassName() const;
3653
Alexey Bataev39c81e22014-08-28 04:28:19 +00003654 /// \brief Determine whether this record is a class describing a lambda
3655 /// function object.
3656 bool isLambda() const;
3657
Alexey Bataev330de032014-10-29 12:21:55 +00003658 /// \brief Determine whether this record is a record for captured variables in
3659 /// CapturedStmt construct.
3660 bool isCapturedRecord() const;
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003661
Alexey Bataev330de032014-10-29 12:21:55 +00003662 /// \brief Mark the record as a record for captured variables in CapturedStmt
3663 /// construct.
3664 void setCapturedRecord();
3665
James Dennett31a57342018-02-02 21:38:22 +00003666 /// Returns the RecordDecl that actually defines
John McCallf937c022011-10-07 06:10:15 +00003667 /// this struct/union/class. When determining whether or not a
3668 /// struct/union/class is completely defined, one should use this
3669 /// method as opposed to 'isCompleteDefinition'.
3670 /// 'isCompleteDefinition' indicates whether or not a specific
3671 /// RecordDecl is a completed definition, not whether or not the
3672 /// record type is defined. This method returns NULL if there is
3673 /// no RecordDecl that defines the struct/union/tag.
3674 RecordDecl *getDefinition() const {
Douglas Gregor0a5a2212010-02-11 01:04:33 +00003675 return cast_or_null<RecordDecl>(TagDecl::getDefinition());
Ted Kremenek21475702008-09-05 17:16:31 +00003676 }
Mike Stump11289f42009-09-09 15:08:12 +00003677
Douglas Gregore0295612008-12-11 17:59:21 +00003678 // Iterator access to field members. The field iterator only visits
3679 // the non-static data members of this class, ignoring any static
3680 // data members, functions, constructors, destructors, etc.
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003681 using field_iterator = specific_decl_iterator<FieldDecl>;
3682 using field_range = llvm::iterator_range<specific_decl_iterator<FieldDecl>>;
Argyrios Kyrtzidiscd5f3bd2008-08-08 13:54:06 +00003683
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00003684 field_range fields() const { return field_range(field_begin(), field_end()); }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003685 field_iterator field_begin() const;
3686
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003687 field_iterator field_end() const {
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003688 return field_iterator(decl_iterator());
Argyrios Kyrtzidiscd5f3bd2008-08-08 13:54:06 +00003689 }
3690
James Dennett31a57342018-02-02 21:38:22 +00003691 // Whether there are any fields (non-static data members) in this record.
Mike Stump11289f42009-09-09 15:08:12 +00003692 bool field_empty() const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003693 return field_begin() == field_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00003694 }
Douglas Gregor7a4fad12008-12-11 20:41:00 +00003695
James Dennett31a57342018-02-02 21:38:22 +00003696 /// Note that the definition of this type is now complete.
Douglas Gregorb11aad82011-02-19 18:51:44 +00003697 virtual void completeDefinition();
Steve Naroffcc321422007-03-26 23:09:51 +00003698
John McCall180ef092010-01-29 01:45:37 +00003699 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +00003700 static bool classofKind(Kind K) {
Alexis Hunted053252010-05-30 07:21:58 +00003701 return K >= firstRecord && K <= lastRecord;
John McCall180ef092010-01-29 01:45:37 +00003702 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003703
Erich Keaned232e2c2017-10-24 16:16:34 +00003704 /// \brief Get whether or not this is an ms_struct which can
Eli Friedman9ee2d0472012-10-12 23:29:20 +00003705 /// be turned on with an attribute, pragma, or -mms-bitfields
3706 /// commandline option.
3707 bool isMsStruct(const ASTContext &C) const;
3708
Kostya Serebryany293dc9b2014-10-16 20:54:52 +00003709 /// \brief Whether we are allowed to insert extra padding between fields.
3710 /// These padding are added to help AddressSanitizer detect
3711 /// intra-object-overflow bugs.
3712 bool mayInsertExtraPadding(bool EmitRemark = false) const;
3713
Evgeny Astigeevich665027d2014-12-12 16:17:46 +00003714 /// Finds the first data member which has a name.
3715 /// nullptr is returned if no named data member exists.
3716 const FieldDecl *findFirstNamedDataMember() const;
3717
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003718private:
3719 /// \brief Deserialize just the fields.
3720 void LoadFieldsFromExternalStorage() const;
Chris Lattner28743e22007-01-22 07:41:08 +00003721};
3722
Anders Carlsson5c6c0592008-02-08 00:33:21 +00003723class FileScopeAsmDecl : public Decl {
Anders Carlsson5c6c0592008-02-08 00:33:21 +00003724 StringLiteral *AsmString;
Abramo Bagnara348823a2011-03-03 14:20:18 +00003725 SourceLocation RParenLoc;
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003726
Abramo Bagnara348823a2011-03-03 14:20:18 +00003727 FileScopeAsmDecl(DeclContext *DC, StringLiteral *asmstring,
3728 SourceLocation StartL, SourceLocation EndL)
3729 : Decl(FileScopeAsm, DC, StartL), AsmString(asmstring), RParenLoc(EndL) {}
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003730
3731 virtual void anchor();
3732
Chris Lattneree1284a2008-03-16 00:16:02 +00003733public:
Douglas Gregor6e6ad602009-01-20 01:17:11 +00003734 static FileScopeAsmDecl *Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00003735 StringLiteral *Str, SourceLocation AsmLoc,
3736 SourceLocation RParenLoc);
3737
Douglas Gregor72172e92012-01-05 21:55:30 +00003738 static FileScopeAsmDecl *CreateDeserialized(ASTContext &C, unsigned ID);
3739
Abramo Bagnara348823a2011-03-03 14:20:18 +00003740 SourceLocation getAsmLoc() const { return getLocation(); }
3741 SourceLocation getRParenLoc() const { return RParenLoc; }
3742 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
Craig Toppercbce6e92014-03-11 06:22:39 +00003743 SourceRange getSourceRange() const override LLVM_READONLY {
Abramo Bagnara348823a2011-03-03 14:20:18 +00003744 return SourceRange(getAsmLoc(), getRParenLoc());
3745 }
Anders Carlsson5c6c0592008-02-08 00:33:21 +00003746
3747 const StringLiteral *getAsmString() const { return AsmString; }
3748 StringLiteral *getAsmString() { return AsmString; }
Douglas Gregora5414852009-04-13 22:49:25 +00003749 void setAsmString(StringLiteral *Asm) { AsmString = Asm; }
3750
John McCall180ef092010-01-29 01:45:37 +00003751 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +00003752 static bool classofKind(Kind K) { return K == FileScopeAsm; }
Anders Carlsson5c6c0592008-02-08 00:33:21 +00003753};
Chris Lattner38376f12008-01-12 07:05:38 +00003754
James Dennett31a57342018-02-02 21:38:22 +00003755/// Pepresents a block literal declaration, which is like an
Steve Naroff415d3d52008-10-08 17:01:13 +00003756/// unnamed FunctionDecl. For example:
3757/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body }
Steve Naroff415d3d52008-10-08 17:01:13 +00003758class BlockDecl : public Decl, public DeclContext {
John McCall351762c2011-02-07 10:33:21 +00003759public:
3760 /// A class which contains all the information about a particular
3761 /// captured value.
3762 class Capture {
3763 enum {
3764 flag_isByRef = 0x1,
3765 flag_isNested = 0x2
3766 };
3767
3768 /// The variable being captured.
3769 llvm::PointerIntPair<VarDecl*, 2> VariableAndFlags;
3770
3771 /// The copy expression, expressed in terms of a DeclRef (or
3772 /// BlockDeclRef) to the captured variable. Only required if the
3773 /// variable has a C++ class type.
3774 Expr *CopyExpr;
3775
3776 public:
3777 Capture(VarDecl *variable, bool byRef, bool nested, Expr *copy)
3778 : VariableAndFlags(variable,
3779 (byRef ? flag_isByRef : 0) | (nested ? flag_isNested : 0)),
3780 CopyExpr(copy) {}
3781
3782 /// The variable being captured.
3783 VarDecl *getVariable() const { return VariableAndFlags.getPointer(); }
3784
3785 /// Whether this is a "by ref" capture, i.e. a capture of a __block
3786 /// variable.
3787 bool isByRef() const { return VariableAndFlags.getInt() & flag_isByRef; }
3788
3789 /// Whether this is a nested capture, i.e. the variable captured
3790 /// is not from outside the immediately enclosing function/block.
3791 bool isNested() const { return VariableAndFlags.getInt() & flag_isNested; }
3792
Craig Topper34b4c432014-05-06 06:48:52 +00003793 bool hasCopyExpr() const { return CopyExpr != nullptr; }
John McCall351762c2011-02-07 10:33:21 +00003794 Expr *getCopyExpr() const { return CopyExpr; }
3795 void setCopyExpr(Expr *e) { CopyExpr = e; }
3796 };
3797
3798private:
Fariborz Jahanian960910a2009-05-19 17:08:59 +00003799 // FIXME: This can be packed into the bitfields in Decl.
Ted Kremenek8af4f402010-04-29 16:48:58 +00003800 bool IsVariadic : 1;
John McCallc63de662011-02-02 13:00:07 +00003801 bool CapturesCXXThis : 1;
Fariborz Jahaniandd5eb9d2011-12-03 17:47:53 +00003802 bool BlockMissingReturnType : 1;
Eli Friedman98b01ed2012-03-01 04:01:32 +00003803 bool IsConversionFromLambda : 1;
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003804
James Dennett31a57342018-02-02 21:38:22 +00003805 /// A new[]'d array of pointers to ParmVarDecls for the formal
Steve Naroffc4b30e52009-03-13 16:56:44 +00003806 /// parameters of this function. This is null if a prototype or if there are
3807 /// no formals.
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003808 ParmVarDecl **ParamInfo = nullptr;
3809 unsigned NumParams = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003810
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003811 Stmt *Body = nullptr;
3812 TypeSourceInfo *SignatureAsWritten = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00003813
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003814 const Capture *Captures = nullptr;
3815 unsigned NumCaptures = 0;
John McCallc63de662011-02-02 13:00:07 +00003816
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003817 unsigned ManglingNumber = 0;
3818 Decl *ManglingContextDecl = nullptr;
Eli Friedman7e346a82013-07-01 20:22:57 +00003819
Steve Naroff415d3d52008-10-08 17:01:13 +00003820protected:
Steve Naroff1d95e5a2008-10-10 01:28:17 +00003821 BlockDecl(DeclContext *DC, SourceLocation CaretLoc)
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003822 : Decl(Block, DC, CaretLoc), DeclContext(Block), IsVariadic(false),
3823 CapturesCXXThis(false), BlockMissingReturnType(true),
3824 IsConversionFromLambda(false) {}
Ted Kremenek1f1e7562007-10-25 21:37:16 +00003825
Steve Naroff415d3d52008-10-08 17:01:13 +00003826public:
Douglas Gregor72172e92012-01-05 21:55:30 +00003827 static BlockDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L);
3828 static BlockDecl *CreateDeserialized(ASTContext &C, unsigned ID);
3829
Steve Naroff415d3d52008-10-08 17:01:13 +00003830 SourceLocation getCaretLocation() const { return getLocation(); }
3831
Ted Kremenek8af4f402010-04-29 16:48:58 +00003832 bool isVariadic() const { return IsVariadic; }
3833 void setIsVariadic(bool value) { IsVariadic = value; }
Mike Stump11289f42009-09-09 15:08:12 +00003834
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00003835 CompoundStmt *getCompoundBody() const { return (CompoundStmt*) Body; }
Craig Toppercbce6e92014-03-11 06:22:39 +00003836 Stmt *getBody() const override { return (Stmt*) Body; }
Ted Kremenek73980592009-03-12 18:33:24 +00003837 void setBody(CompoundStmt *B) { Body = (Stmt*) B; }
Steve Naroff415d3d52008-10-08 17:01:13 +00003838
John McCalla3ccba02010-06-04 11:21:44 +00003839 void setSignatureAsWritten(TypeSourceInfo *Sig) { SignatureAsWritten = Sig; }
3840 TypeSourceInfo *getSignatureAsWritten() const { return SignatureAsWritten; }
3841
Ted Kremenek81541c52014-01-17 07:15:26 +00003842 // ArrayRef access to formal parameters.
David Majnemer59f77922016-06-24 04:05:48 +00003843 ArrayRef<ParmVarDecl *> parameters() const {
3844 return {ParamInfo, getNumParams()};
3845 }
3846 MutableArrayRef<ParmVarDecl *> parameters() {
3847 return {ParamInfo, getNumParams()};
Ted Kremenek81541c52014-01-17 07:15:26 +00003848 }
3849
David Majnemer59f77922016-06-24 04:05:48 +00003850 // Iterator access to formal parameters.
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003851 using param_iterator = MutableArrayRef<ParmVarDecl *>::iterator;
3852 using param_const_iterator = ArrayRef<ParmVarDecl *>::const_iterator;
3853
David Majnemer59f77922016-06-24 04:05:48 +00003854 bool param_empty() const { return parameters().empty(); }
3855 param_iterator param_begin() { return parameters().begin(); }
3856 param_iterator param_end() { return parameters().end(); }
3857 param_const_iterator param_begin() const { return parameters().begin(); }
3858 param_const_iterator param_end() const { return parameters().end(); }
3859 size_t param_size() const { return parameters().size(); }
Mike Stump11289f42009-09-09 15:08:12 +00003860
John McCallc63de662011-02-02 13:00:07 +00003861 unsigned getNumParams() const { return NumParams; }
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003862
Steve Naroffc4b30e52009-03-13 16:56:44 +00003863 const ParmVarDecl *getParamDecl(unsigned i) const {
3864 assert(i < getNumParams() && "Illegal param #");
3865 return ParamInfo[i];
3866 }
3867 ParmVarDecl *getParamDecl(unsigned i) {
3868 assert(i < getNumParams() && "Illegal param #");
3869 return ParamInfo[i];
3870 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003871
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003872 void setParams(ArrayRef<ParmVarDecl *> NewParamInfo);
Mike Stump11289f42009-09-09 15:08:12 +00003873
James Dennett31a57342018-02-02 21:38:22 +00003874 /// True if this block (or its nested blocks) captures
John McCallc63de662011-02-02 13:00:07 +00003875 /// anything of local storage from its enclosing scopes.
John McCall351762c2011-02-07 10:33:21 +00003876 bool hasCaptures() const { return NumCaptures != 0 || CapturesCXXThis; }
John McCallc63de662011-02-02 13:00:07 +00003877
James Dennett31a57342018-02-02 21:38:22 +00003878 /// Returns the number of captured variables.
John McCall351762c2011-02-07 10:33:21 +00003879 /// Does not include an entry for 'this'.
3880 unsigned getNumCaptures() const { return NumCaptures; }
John McCallc63de662011-02-02 13:00:07 +00003881
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003882 using capture_const_iterator = ArrayRef<Capture>::const_iterator;
Aaron Ballman9371dd22014-03-14 18:34:04 +00003883
David Majnemer59f77922016-06-24 04:05:48 +00003884 ArrayRef<Capture> captures() const { return {Captures, NumCaptures}; }
Aaron Ballman9371dd22014-03-14 18:34:04 +00003885
David Majnemer59f77922016-06-24 04:05:48 +00003886 capture_const_iterator capture_begin() const { return captures().begin(); }
3887 capture_const_iterator capture_end() const { return captures().end(); }
John McCallc63de662011-02-02 13:00:07 +00003888
3889 bool capturesCXXThis() const { return CapturesCXXThis; }
Fariborz Jahaniandd5eb9d2011-12-03 17:47:53 +00003890 bool blockMissingReturnType() const { return BlockMissingReturnType; }
3891 void setBlockMissingReturnType(bool val) { BlockMissingReturnType = val; }
John McCallc63de662011-02-02 13:00:07 +00003892
Eli Friedman98b01ed2012-03-01 04:01:32 +00003893 bool isConversionFromLambda() const { return IsConversionFromLambda; }
3894 void setIsConversionFromLambda(bool val) { IsConversionFromLambda = val; }
3895
John McCallce45f882011-06-15 22:51:16 +00003896 bool capturesVariable(const VarDecl *var) const;
3897
Benjamin Kramerb40e4af2015-08-05 09:40:35 +00003898 void setCaptures(ASTContext &Context, ArrayRef<Capture> Captures,
3899 bool CapturesCXXThis);
John McCallc63de662011-02-02 13:00:07 +00003900
Eli Friedman7e346a82013-07-01 20:22:57 +00003901 unsigned getBlockManglingNumber() const {
3902 return ManglingNumber;
3903 }
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003904
Eli Friedman7e346a82013-07-01 20:22:57 +00003905 Decl *getBlockManglingContextDecl() const {
3906 return ManglingContextDecl;
3907 }
3908
3909 void setBlockMangling(unsigned Number, Decl *Ctx) {
3910 ManglingNumber = Number;
3911 ManglingContextDecl = Ctx;
3912 }
3913
Craig Toppercbce6e92014-03-11 06:22:39 +00003914 SourceRange getSourceRange() const override LLVM_READONLY;
David Blaikie21bfbf82011-11-09 06:07:30 +00003915
Steve Naroff415d3d52008-10-08 17:01:13 +00003916 // Implement isa/cast/dyncast/etc.
John McCall180ef092010-01-29 01:45:37 +00003917 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
John McCall180ef092010-01-29 01:45:37 +00003918 static bool classofKind(Kind K) { return K == Block; }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +00003919 static DeclContext *castToDeclContext(const BlockDecl *D) {
3920 return static_cast<DeclContext *>(const_cast<BlockDecl*>(D));
3921 }
3922 static BlockDecl *castFromDeclContext(const DeclContext *DC) {
3923 return static_cast<BlockDecl *>(const_cast<DeclContext*>(DC));
3924 }
Steve Naroff415d3d52008-10-08 17:01:13 +00003925};
3926
James Dennett31a57342018-02-02 21:38:22 +00003927/// Represents the body of a CapturedStmt, and serves as its DeclContext.
James Y Knight7a22b242015-08-06 20:26:32 +00003928class CapturedDecl final
3929 : public Decl,
3930 public DeclContext,
3931 private llvm::TrailingObjects<CapturedDecl, ImplicitParamDecl *> {
3932protected:
3933 size_t numTrailingObjects(OverloadToken<ImplicitParamDecl>) {
3934 return NumParams;
3935 }
3936
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003937private:
Ben Langmuir37943a72013-05-03 19:00:33 +00003938 /// \brief The number of parameters to the outlined function.
3939 unsigned NumParams;
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003940
Alexey Bataev9959db52014-05-06 10:08:46 +00003941 /// \brief The position of context parameter in list of parameters.
3942 unsigned ContextParam;
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003943
Ben Langmuir37943a72013-05-03 19:00:33 +00003944 /// \brief The body of the outlined function.
Alexey Bataev9959db52014-05-06 10:08:46 +00003945 llvm::PointerIntPair<Stmt *, 1, bool> BodyAndNothrow;
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003946
Chandler Carruth21c90602015-12-30 03:24:14 +00003947 explicit CapturedDecl(DeclContext *DC, unsigned NumParams);
Ben Langmuir37943a72013-05-03 19:00:33 +00003948
James Y Knight7a22b242015-08-06 20:26:32 +00003949 ImplicitParamDecl *const *getParams() const {
3950 return getTrailingObjects<ImplicitParamDecl *>();
3951 }
3952
3953 ImplicitParamDecl **getParams() {
3954 return getTrailingObjects<ImplicitParamDecl *>();
Ben Langmuir37943a72013-05-03 19:00:33 +00003955 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003956
3957public:
Eugene Zelenkode0215c2017-11-13 23:01:27 +00003958 friend class ASTDeclReader;
3959 friend class ASTDeclWriter;
3960 friend TrailingObjects;
3961
Alexey Bataev9959db52014-05-06 10:08:46 +00003962 static CapturedDecl *Create(ASTContext &C, DeclContext *DC,
3963 unsigned NumParams);
Ben Langmuirce914fc2013-05-03 19:20:19 +00003964 static CapturedDecl *CreateDeserialized(ASTContext &C, unsigned ID,
3965 unsigned NumParams);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003966
Chandler Carruth21c90602015-12-30 03:24:14 +00003967 Stmt *getBody() const override;
3968 void setBody(Stmt *B);
Alexey Bataev9959db52014-05-06 10:08:46 +00003969
Chandler Carruth21c90602015-12-30 03:24:14 +00003970 bool isNothrow() const;
3971 void setNothrow(bool Nothrow = true);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00003972
Ben Langmuirce914fc2013-05-03 19:20:19 +00003973 unsigned getNumParams() const { return NumParams; }
3974
Ben Langmuir37943a72013-05-03 19:00:33 +00003975 ImplicitParamDecl *getParam(unsigned i) const {
3976 assert(i < NumParams);
3977 return getParams()[i];
3978 }
3979 void setParam(unsigned i, ImplicitParamDecl *P) {
3980 assert(i < NumParams);
3981 getParams()[i] = P;
3982 }
3983
Yaron Kerenfe813632016-06-29 18:55:53 +00003984 // ArrayRef interface to parameters.
3985 ArrayRef<ImplicitParamDecl *> parameters() const {
3986 return {getParams(), getNumParams()};
3987 }
3988 MutableArrayRef<ImplicitParamDecl *> parameters() {
3989 return {getParams(), getNumParams()};
3990 }
3991
Ben Langmuir37943a72013-05-03 19:00:33 +00003992 /// \brief Retrieve the parameter containing captured variables.
Alexey Bataev9959db52014-05-06 10:08:46 +00003993 ImplicitParamDecl *getContextParam() const {
3994 assert(ContextParam < NumParams);
3995 return getParam(ContextParam);
3996 }
3997 void setContextParam(unsigned i, ImplicitParamDecl *P) {
3998 assert(i < NumParams);
3999 ContextParam = i;
4000 setParam(i, P);
4001 }
4002 unsigned getContextParamPosition() const { return ContextParam; }
Ben Langmuir37943a72013-05-03 19:00:33 +00004003
Eugene Zelenkode0215c2017-11-13 23:01:27 +00004004 using param_iterator = ImplicitParamDecl *const *;
4005 using param_range = llvm::iterator_range<param_iterator>;
Aaron Ballmane7fbde82014-03-07 16:40:17 +00004006
Ben Langmuir37943a72013-05-03 19:00:33 +00004007 /// \brief Retrieve an iterator pointing to the first parameter decl.
4008 param_iterator param_begin() const { return getParams(); }
4009 /// \brief Retrieve an iterator one past the last parameter decl.
4010 param_iterator param_end() const { return getParams() + NumParams; }
4011
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00004012 // Implement isa/cast/dyncast/etc.
4013 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
4014 static bool classofKind(Kind K) { return K == Captured; }
4015 static DeclContext *castToDeclContext(const CapturedDecl *D) {
4016 return static_cast<DeclContext *>(const_cast<CapturedDecl *>(D));
4017 }
4018 static CapturedDecl *castFromDeclContext(const DeclContext *DC) {
4019 return static_cast<CapturedDecl *>(const_cast<DeclContext *>(DC));
4020 }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00004021};
4022
Douglas Gregorba345522011-12-02 23:23:56 +00004023/// \brief Describes a module import declaration, which makes the contents
4024/// of the named module visible in the current translation unit.
4025///
4026/// An import declaration imports the named module (or submodule). For example:
4027/// \code
Douglas Gregorc50d4922012-12-11 22:11:52 +00004028/// @import std.vector;
Douglas Gregorba345522011-12-02 23:23:56 +00004029/// \endcode
4030///
James Dennett1355bd12012-06-11 06:19:40 +00004031/// Import declarations can also be implicitly generated from
4032/// \#include/\#import directives.
James Y Knight7a22b242015-08-06 20:26:32 +00004033class ImportDecl final : public Decl,
4034 llvm::TrailingObjects<ImportDecl, SourceLocation> {
Eugene Zelenkode0215c2017-11-13 23:01:27 +00004035 friend class ASTContext;
4036 friend class ASTDeclReader;
4037 friend class ASTReader;
4038 friend TrailingObjects;
4039
Douglas Gregorba345522011-12-02 23:23:56 +00004040 /// \brief The imported module, along with a bit that indicates whether
4041 /// we have source-location information for each identifier in the module
4042 /// name.
4043 ///
4044 /// When the bit is false, we only have a single source location for the
4045 /// end of the import declaration.
4046 llvm::PointerIntPair<Module *, 1, bool> ImportedAndComplete;
4047
Douglas Gregor0f2a3602011-12-03 00:30:27 +00004048 /// \brief The next import in the list of imports local to the translation
4049 /// unit being parsed (not loaded from an AST file).
Eugene Zelenkode0215c2017-11-13 23:01:27 +00004050 ImportDecl *NextLocalImport = nullptr;
Douglas Gregor0f2a3602011-12-03 00:30:27 +00004051
Douglas Gregor22d09742012-01-03 18:04:46 +00004052 ImportDecl(DeclContext *DC, SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00004053 ArrayRef<SourceLocation> IdentifierLocs);
4054
Douglas Gregor22d09742012-01-03 18:04:46 +00004055 ImportDecl(DeclContext *DC, SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00004056 SourceLocation EndLoc);
4057
Eugene Zelenkode0215c2017-11-13 23:01:27 +00004058 ImportDecl(EmptyShell Empty) : Decl(Import, Empty) {}
Douglas Gregorba345522011-12-02 23:23:56 +00004059
4060public:
4061 /// \brief Create a new module import declaration.
4062 static ImportDecl *Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00004063 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00004064 ArrayRef<SourceLocation> IdentifierLocs);
4065
4066 /// \brief Create a new module import declaration for an implicitly-generated
4067 /// import.
4068 static ImportDecl *CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00004069 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00004070 SourceLocation EndLoc);
4071
Douglas Gregor72172e92012-01-05 21:55:30 +00004072 /// \brief Create a new, deserialized module import declaration.
4073 static ImportDecl *CreateDeserialized(ASTContext &C, unsigned ID,
4074 unsigned NumLocations);
Douglas Gregorba345522011-12-02 23:23:56 +00004075
4076 /// \brief Retrieve the module that was imported by the import declaration.
4077 Module *getImportedModule() const { return ImportedAndComplete.getPointer(); }
4078
4079 /// \brief Retrieves the locations of each of the identifiers that make up
4080 /// the complete module name in the import declaration.
4081 ///
4082 /// This will return an empty array if the locations of the individual
4083 /// identifiers aren't available.
4084 ArrayRef<SourceLocation> getIdentifierLocs() const;
Craig Toppercbce6e92014-03-11 06:22:39 +00004085
4086 SourceRange getSourceRange() const override LLVM_READONLY;
4087
Douglas Gregorba345522011-12-02 23:23:56 +00004088 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
Douglas Gregorba345522011-12-02 23:23:56 +00004089 static bool classofKind(Kind K) { return K == Import; }
4090};
Michael Han84324352013-02-22 17:15:32 +00004091
Richard Smith8df390f2016-09-08 23:14:54 +00004092/// \brief Represents a C++ Modules TS module export declaration.
4093///
4094/// For example:
4095/// \code
4096/// export void foo();
4097/// \endcode
4098class ExportDecl final : public Decl, public DeclContext {
4099 virtual void anchor();
Eugene Zelenkode0215c2017-11-13 23:01:27 +00004100
Richard Smith8df390f2016-09-08 23:14:54 +00004101private:
Eugene Zelenkode0215c2017-11-13 23:01:27 +00004102 friend class ASTDeclReader;
4103
Richard Smith8df390f2016-09-08 23:14:54 +00004104 /// \brief The source location for the right brace (if valid).
4105 SourceLocation RBraceLoc;
4106
4107 ExportDecl(DeclContext *DC, SourceLocation ExportLoc)
Eugene Zelenkode0215c2017-11-13 23:01:27 +00004108 : Decl(Export, DC, ExportLoc), DeclContext(Export),
4109 RBraceLoc(SourceLocation()) {}
Richard Smith8df390f2016-09-08 23:14:54 +00004110
4111public:
4112 static ExportDecl *Create(ASTContext &C, DeclContext *DC,
4113 SourceLocation ExportLoc);
4114 static ExportDecl *CreateDeserialized(ASTContext &C, unsigned ID);
4115
4116 SourceLocation getExportLoc() const { return getLocation(); }
4117 SourceLocation getRBraceLoc() const { return RBraceLoc; }
4118 void setRBraceLoc(SourceLocation L) { RBraceLoc = L; }
4119
4120 SourceLocation getLocEnd() const LLVM_READONLY {
4121 if (RBraceLoc.isValid())
4122 return RBraceLoc;
4123 // No braces: get the end location of the (only) declaration in context
4124 // (if present).
4125 return decls_empty() ? getLocation() : decls_begin()->getLocEnd();
4126 }
4127
4128 SourceRange getSourceRange() const override LLVM_READONLY {
4129 return SourceRange(getLocation(), getLocEnd());
4130 }
4131
4132 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
4133 static bool classofKind(Kind K) { return K == Export; }
4134 static DeclContext *castToDeclContext(const ExportDecl *D) {
4135 return static_cast<DeclContext *>(const_cast<ExportDecl*>(D));
4136 }
4137 static ExportDecl *castFromDeclContext(const DeclContext *DC) {
4138 return static_cast<ExportDecl *>(const_cast<DeclContext*>(DC));
4139 }
4140};
4141
James Dennettb8973efb2018-02-02 20:22:29 +00004142/// Represents an empty-declaration.
Michael Han84324352013-02-22 17:15:32 +00004143class EmptyDecl : public Decl {
Eugene Zelenkode0215c2017-11-13 23:01:27 +00004144 EmptyDecl(DeclContext *DC, SourceLocation L) : Decl(Empty, DC, L) {}
4145
Michael Han84324352013-02-22 17:15:32 +00004146 virtual void anchor();
Michael Han84324352013-02-22 17:15:32 +00004147
4148public:
4149 static EmptyDecl *Create(ASTContext &C, DeclContext *DC,
4150 SourceLocation L);
4151 static EmptyDecl *CreateDeserialized(ASTContext &C, unsigned ID);
4152
4153 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
4154 static bool classofKind(Kind K) { return K == Empty; }
4155};
Douglas Gregorba345522011-12-02 23:23:56 +00004156
Douglas Gregor2ada0482009-02-04 17:27:36 +00004157/// Insertion operator for diagnostics. This allows sending NamedDecl's
4158/// into a diagnostic with <<.
Benjamin Kramer7ec12c92012-02-07 22:29:24 +00004159inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
4160 const NamedDecl* ND) {
4161 DB.AddTaggedVal(reinterpret_cast<intptr_t>(ND),
4162 DiagnosticsEngine::ak_nameddecl);
4163 return DB;
4164}
4165inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
4166 const NamedDecl* ND) {
4167 PD.AddTaggedVal(reinterpret_cast<intptr_t>(ND),
4168 DiagnosticsEngine::ak_nameddecl);
4169 return PD;
4170}
Douglas Gregor2ada0482009-02-04 17:27:36 +00004171
Douglas Gregorbf62d642010-12-06 18:36:25 +00004172template<typename decl_type>
Rafael Espindola8db352d2013-10-17 15:37:26 +00004173void Redeclarable<decl_type>::setPreviousDecl(decl_type *PrevDecl) {
Douglas Gregorbf62d642010-12-06 18:36:25 +00004174 // Note: This routine is implemented here because we need both NamedDecl
4175 // and Redeclarable to be defined.
Richard Smith053f6c62014-05-16 23:01:30 +00004176 assert(RedeclLink.NextIsLatest() &&
4177 "setPreviousDecl on a decl already in a redeclaration chain");
Douglas Gregorbf62d642010-12-06 18:36:25 +00004178
Douglas Gregorbf62d642010-12-06 18:36:25 +00004179 if (PrevDecl) {
4180 // Point to previous. Make sure that this is actually the most recent
4181 // redeclaration, or we can build invalid chains. If the most recent
4182 // redeclaration is invalid, it won't be PrevDecl, but we want it anyway.
Rafael Espindola8db352d2013-10-17 15:37:26 +00004183 First = PrevDecl->getFirstDecl();
Douglas Gregorbf62d642010-12-06 18:36:25 +00004184 assert(First->RedeclLink.NextIsLatest() && "Expected first");
Richard Smith053f6c62014-05-16 23:01:30 +00004185 decl_type *MostRecent = First->getNextRedeclaration();
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004186 RedeclLink = PreviousDeclLink(cast<decl_type>(MostRecent));
Richard Smith64017682013-07-17 23:53:16 +00004187
4188 // If the declaration was previously visible, a redeclaration of it remains
4189 // visible even if it wouldn't be visible by itself.
Richard Smith64017682013-07-17 23:53:16 +00004190 static_cast<decl_type*>(this)->IdentifierNamespace |=
Richard Smith541b38b2013-09-20 01:15:31 +00004191 MostRecent->getIdentifierNamespace() &
Richard Smith64017682013-07-17 23:53:16 +00004192 (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type);
Douglas Gregorbf62d642010-12-06 18:36:25 +00004193 } else {
4194 // Make this first.
4195 First = static_cast<decl_type*>(this);
4196 }
David Blaikie21bfbf82011-11-09 06:07:30 +00004197
Douglas Gregorbf62d642010-12-06 18:36:25 +00004198 // First one will point to this one as latest.
Richard Smith053f6c62014-05-16 23:01:30 +00004199 First->RedeclLink.setLatest(static_cast<decl_type*>(this));
4200
Chandler Carruth5aa9d792013-03-14 11:17:20 +00004201 assert(!isa<NamedDecl>(static_cast<decl_type*>(this)) ||
4202 cast<NamedDecl>(static_cast<decl_type*>(this))->isLinkageValid());
Douglas Gregorbf62d642010-12-06 18:36:25 +00004203}
4204
Daniel Dunbar49248822012-03-06 18:20:20 +00004205// Inline function definitions.
4206
James Dennettb8973efb2018-02-02 20:22:29 +00004207/// Check if the given decl is complete.
Daniel Dunbar49248822012-03-06 18:20:20 +00004208///
4209/// We use this function to break a cycle between the inline definitions in
4210/// Type.h and Decl.h.
4211inline bool IsEnumDeclComplete(EnumDecl *ED) {
4212 return ED->isComplete();
4213}
4214
James Dennettb8973efb2018-02-02 20:22:29 +00004215/// Check if the given decl is scoped.
Daniel Dunbard0437332012-03-08 02:52:18 +00004216///
4217/// We use this function to break a cycle between the inline definitions in
4218/// Type.h and Decl.h.
4219inline bool IsEnumDeclScoped(EnumDecl *ED) {
4220 return ED->isScoped();
4221}
4222
Eugene Zelenkode0215c2017-11-13 23:01:27 +00004223} // namespace clang
Nate Begemanf2a6e5f2008-12-16 19:57:09 +00004224
Eugene Zelenkode0215c2017-11-13 23:01:27 +00004225#endif // LLVM_CLANG_AST_DECL_H