Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1 | //===- Decl.h - Classes for representing declarations -----------*- C++ -*-===// |
Chris Lattner | d0745a0 | 2006-08-17 05:18:35 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | d0745a0 | 2006-08-17 05:18:35 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Argyrios Kyrtzidis | 2951e14 | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 9 | // This file defines the Decl subclasses. |
Chris Lattner | d0745a0 | 2006-08-17 05:18:35 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Chris Lattner | 3e7bd4e | 2006-08-17 05:51:27 +0000 | [diff] [blame] | 13 | #ifndef LLVM_CLANG_AST_DECL_H |
| 14 | #define LLVM_CLANG_AST_DECL_H |
Chris Lattner | d0745a0 | 2006-08-17 05:18:35 +0000 | [diff] [blame] | 15 | |
Craig Topper | 142f270 | 2024-01-16 12:49:11 -0800 | [diff] [blame] | 16 | #include "clang/AST/APNumericStorage.h" |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 17 | #include "clang/AST/APValue.h" |
Richard Trieu | afd6711 | 2019-01-10 03:23:25 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContextAllocate.h" |
Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 19 | #include "clang/AST/DeclAccessPair.h" |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclBase.h" |
Chris Lattner | a2e2167 | 2009-02-20 21:06:29 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclarationName.h" |
Douglas Gregor | e3dcb2d | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExternalASTSource.h" |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 23 | #include "clang/AST/NestedNameSpecifier.h" |
Benjamin Kramer | d7d2b1f | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 24 | #include "clang/AST/Redeclarable.h" |
| 25 | #include "clang/AST/Type.h" |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 26 | #include "clang/Basic/AddressSpaces.h" |
| 27 | #include "clang/Basic/Diagnostic.h" |
| 28 | #include "clang/Basic/IdentifierTable.h" |
| 29 | #include "clang/Basic/LLVM.h" |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 30 | #include "clang/Basic/Linkage.h" |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 31 | #include "clang/Basic/OperatorKinds.h" |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 32 | #include "clang/Basic/PartialDiagnostic.h" |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 33 | #include "clang/Basic/PragmaKinds.h" |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 34 | #include "clang/Basic/SourceLocation.h" |
| 35 | #include "clang/Basic/Specifiers.h" |
| 36 | #include "clang/Basic/Visibility.h" |
| 37 | #include "llvm/ADT/APSInt.h" |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/ArrayRef.h" |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/PointerIntPair.h" |
| 40 | #include "llvm/ADT/PointerUnion.h" |
| 41 | #include "llvm/ADT/StringRef.h" |
| 42 | #include "llvm/ADT/iterator_range.h" |
| 43 | #include "llvm/Support/Casting.h" |
Daniel Dunbar | e4775e1 | 2012-03-09 19:35:29 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Compiler.h" |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 45 | #include "llvm/Support/TrailingObjects.h" |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 46 | #include <cassert> |
| 47 | #include <cstddef> |
| 48 | #include <cstdint> |
Kazu Hirata | a1580d7 | 2023-01-14 11:07:21 -0800 | [diff] [blame] | 49 | #include <optional> |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 50 | #include <string> |
| 51 | #include <utility> |
Ted Kremenek | 1f1e756 | 2007-10-25 21:37:16 +0000 | [diff] [blame] | 52 | |
Chris Lattner | d0745a0 | 2006-08-17 05:18:35 +0000 | [diff] [blame] | 53 | namespace clang { |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 54 | |
| 55 | class ASTContext; |
Benjamin Kramer | d7d2b1f | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 56 | struct ASTTemplateArgumentListInfo; |
Benjamin Kramer | d7d2b1f | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 57 | class CompoundStmt; |
| 58 | class DependentFunctionTemplateSpecializationInfo; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 59 | class EnumDecl; |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 60 | class Expr; |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 61 | class FunctionTemplateDecl; |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 62 | class FunctionTemplateSpecializationInfo; |
Richard Smith | d052a578 | 2019-10-22 17:44:08 -0700 | [diff] [blame] | 63 | class FunctionTypeLoc; |
Benjamin Kramer | d7d2b1f | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 64 | class LabelStmt; |
| 65 | class MemberSpecializationInfo; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 66 | class Module; |
| 67 | class NamespaceDecl; |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 68 | class ParmVarDecl; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 69 | class RecordDecl; |
Benjamin Kramer | d7d2b1f | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 70 | class Stmt; |
| 71 | class StringLiteral; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 72 | class TagDecl; |
Benjamin Kramer | d7d2b1f | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 73 | class TemplateArgumentList; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 74 | class TemplateArgumentListInfo; |
Benjamin Kramer | d7d2b1f | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 75 | class TemplateParameterList; |
Richard Smith | 43ccec8e | 2014-08-26 03:52:16 +0000 | [diff] [blame] | 76 | class TypeAliasTemplateDecl; |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 77 | class UnresolvedSetImpl; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 78 | class VarTemplateDecl; |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 79 | enum class ImplicitParamKind; |
Benjamin Kramer | d7d2b1f | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 80 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 81 | /// The top declaration context. |
Vassil Vassilev | 11b47c1 | 2021-07-12 13:35:20 +0000 | [diff] [blame] | 82 | class TranslationUnitDecl : public Decl, |
| 83 | public DeclContext, |
| 84 | public Redeclarable<TranslationUnitDecl> { |
| 85 | using redeclarable_base = Redeclarable<TranslationUnitDecl>; |
| 86 | |
| 87 | TranslationUnitDecl *getNextRedeclarationImpl() override { |
| 88 | return getNextRedeclaration(); |
| 89 | } |
| 90 | |
| 91 | TranslationUnitDecl *getPreviousDeclImpl() override { |
| 92 | return getPreviousDecl(); |
| 93 | } |
| 94 | |
| 95 | TranslationUnitDecl *getMostRecentDeclImpl() override { |
| 96 | return getMostRecentDecl(); |
| 97 | } |
| 98 | |
Argyrios Kyrtzidis | 743e7db | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 99 | ASTContext &Ctx; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 101 | /// The (most recently entered) anonymous namespace for this |
| 102 | /// translation unit, if one has been created. |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 103 | NamespaceDecl *AnonymousNamespace = nullptr; |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 104 | |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 105 | explicit TranslationUnitDecl(ASTContext &ctx); |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 106 | |
| 107 | virtual void anchor(); |
| 108 | |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 109 | public: |
Vassil Vassilev | 11b47c1 | 2021-07-12 13:35:20 +0000 | [diff] [blame] | 110 | using redecl_range = redeclarable_base::redecl_range; |
| 111 | using redecl_iterator = redeclarable_base::redecl_iterator; |
| 112 | |
| 113 | using redeclarable_base::getMostRecentDecl; |
| 114 | using redeclarable_base::getPreviousDecl; |
| 115 | using redeclarable_base::isFirstDecl; |
| 116 | using redeclarable_base::redecls; |
| 117 | using redeclarable_base::redecls_begin; |
| 118 | using redeclarable_base::redecls_end; |
| 119 | |
Argyrios Kyrtzidis | 743e7db | 2009-06-29 17:38:40 +0000 | [diff] [blame] | 120 | ASTContext &getASTContext() const { return Ctx; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 122 | NamespaceDecl *getAnonymousNamespace() const { return AnonymousNamespace; } |
Chuanqi Xu | f2695a1 | 2024-04-17 16:40:00 +0800 | [diff] [blame] | 123 | void setAnonymousNamespace(NamespaceDecl *D); |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 124 | |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 125 | static TranslationUnitDecl *Create(ASTContext &C); |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 126 | |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 127 | // Implement isa/cast/dyncast/etc. |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 128 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 129 | static bool classofKind(Kind K) { return K == TranslationUnit; } |
Argyrios Kyrtzidis | 3768ad6 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 130 | static DeclContext *castToDeclContext(const TranslationUnitDecl *D) { |
| 131 | return static_cast<DeclContext *>(const_cast<TranslationUnitDecl*>(D)); |
| 132 | } |
| 133 | static TranslationUnitDecl *castFromDeclContext(const DeclContext *DC) { |
| 134 | return static_cast<TranslationUnitDecl *>(const_cast<DeclContext*>(DC)); |
| 135 | } |
Vipul Cariappa | 61c8b71 | 2024-09-27 12:33:32 +0530 | [diff] [blame] | 136 | |
| 137 | /// Retrieves the canonical declaration of this translation unit. |
| 138 | TranslationUnitDecl *getCanonicalDecl() override { return getFirstDecl(); } |
| 139 | const TranslationUnitDecl *getCanonicalDecl() const { return getFirstDecl(); } |
Argyrios Kyrtzidis | c3b69ae | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 142 | /// Represents a `#pragma comment` line. Always a child of |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 143 | /// TranslationUnitDecl. |
| 144 | class PragmaCommentDecl final |
| 145 | : public Decl, |
| 146 | private llvm::TrailingObjects<PragmaCommentDecl, char> { |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 147 | friend class ASTDeclReader; |
| 148 | friend class ASTDeclWriter; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 149 | friend TrailingObjects; |
| 150 | |
| 151 | PragmaMSCommentKind CommentKind; |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 152 | |
| 153 | PragmaCommentDecl(TranslationUnitDecl *TU, SourceLocation CommentLoc, |
| 154 | PragmaMSCommentKind CommentKind) |
| 155 | : Decl(PragmaComment, TU, CommentLoc), CommentKind(CommentKind) {} |
| 156 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 157 | virtual void anchor(); |
| 158 | |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 159 | public: |
| 160 | static PragmaCommentDecl *Create(const ASTContext &C, TranslationUnitDecl *DC, |
| 161 | SourceLocation CommentLoc, |
| 162 | PragmaMSCommentKind CommentKind, |
| 163 | StringRef Arg); |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 164 | static PragmaCommentDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID, |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 165 | unsigned ArgSize); |
| 166 | |
| 167 | PragmaMSCommentKind getCommentKind() const { return CommentKind; } |
| 168 | |
| 169 | StringRef getArg() const { return getTrailingObjects<char>(); } |
| 170 | |
| 171 | // Implement isa/cast/dyncast/etc. |
| 172 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 173 | static bool classofKind(Kind K) { return K == PragmaComment; } |
| 174 | }; |
| 175 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 176 | /// Represents a `#pragma detect_mismatch` line. Always a child of |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 177 | /// TranslationUnitDecl. |
| 178 | class PragmaDetectMismatchDecl final |
| 179 | : public Decl, |
| 180 | private llvm::TrailingObjects<PragmaDetectMismatchDecl, char> { |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 181 | friend class ASTDeclReader; |
| 182 | friend class ASTDeclWriter; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 183 | friend TrailingObjects; |
| 184 | |
| 185 | size_t ValueStart; |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 186 | |
| 187 | PragmaDetectMismatchDecl(TranslationUnitDecl *TU, SourceLocation Loc, |
| 188 | size_t ValueStart) |
| 189 | : Decl(PragmaDetectMismatch, TU, Loc), ValueStart(ValueStart) {} |
| 190 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 191 | virtual void anchor(); |
| 192 | |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 193 | public: |
| 194 | static PragmaDetectMismatchDecl *Create(const ASTContext &C, |
| 195 | TranslationUnitDecl *DC, |
| 196 | SourceLocation Loc, StringRef Name, |
| 197 | StringRef Value); |
| 198 | static PragmaDetectMismatchDecl * |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 199 | CreateDeserialized(ASTContext &C, GlobalDeclID ID, unsigned NameValueSize); |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 200 | |
| 201 | StringRef getName() const { return getTrailingObjects<char>(); } |
| 202 | StringRef getValue() const { return getTrailingObjects<char>() + ValueStart; } |
| 203 | |
| 204 | // Implement isa/cast/dyncast/etc. |
| 205 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 206 | static bool classofKind(Kind K) { return K == PragmaDetectMismatch; } |
| 207 | }; |
| 208 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 209 | /// Declaration context for names declared as extern "C" in C++. This |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 210 | /// is neither the semantic nor lexical context for such declarations, but is |
| 211 | /// used to check for conflicts with other extern "C" declarations. Example: |
| 212 | /// |
| 213 | /// \code |
| 214 | /// namespace N { extern "C" void f(); } // #1 |
| 215 | /// void N::f() {} // #2 |
| 216 | /// namespace M { extern "C" void f(); } // #3 |
| 217 | /// \endcode |
| 218 | /// |
| 219 | /// The semantic context of #1 is namespace N and its lexical context is the |
| 220 | /// LinkageSpecDecl; the semantic context of #2 is namespace N and its lexical |
| 221 | /// context is the TU. However, both declarations are also visible in the |
| 222 | /// extern "C" context. |
| 223 | /// |
| 224 | /// The declaration at #3 finds it is a redeclaration of \c N::f through |
| 225 | /// lookup in the extern "C" context. |
| 226 | class ExternCContextDecl : public Decl, public DeclContext { |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 227 | explicit ExternCContextDecl(TranslationUnitDecl *TU) |
| 228 | : Decl(ExternCContext, TU, SourceLocation()), |
| 229 | DeclContext(ExternCContext) {} |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 230 | |
| 231 | virtual void anchor(); |
| 232 | |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 233 | public: |
| 234 | static ExternCContextDecl *Create(const ASTContext &C, |
| 235 | TranslationUnitDecl *TU); |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 236 | |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 237 | // Implement isa/cast/dyncast/etc. |
| 238 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 239 | static bool classofKind(Kind K) { return K == ExternCContext; } |
| 240 | static DeclContext *castToDeclContext(const ExternCContextDecl *D) { |
| 241 | return static_cast<DeclContext *>(const_cast<ExternCContextDecl*>(D)); |
| 242 | } |
| 243 | static ExternCContextDecl *castFromDeclContext(const DeclContext *DC) { |
| 244 | return static_cast<ExternCContextDecl *>(const_cast<DeclContext*>(DC)); |
| 245 | } |
| 246 | }; |
| 247 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 248 | /// This represents a decl that may have a name. Many decls have names such |
Chandler Carruth | 48687a5 | 2012-06-07 17:55:42 +0000 | [diff] [blame] | 249 | /// as ObjCMethodDecl, but not \@class, etc. |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 250 | /// |
| 251 | /// Note that not every NamedDecl is actually named (e.g., a struct might |
| 252 | /// be anonymous), and not every name is an identifier. |
Chris Lattner | a401655 | 2007-10-06 22:53:46 +0000 | [diff] [blame] | 253 | class NamedDecl : public Decl { |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 254 | /// The name of this declaration, which is typically a normal |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 255 | /// identifier but may also be a special kind of name (C++ |
| 256 | /// constructor, Objective-C selector, etc.) |
| 257 | DeclarationName Name; |
| 258 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 259 | virtual void anchor(); |
| 260 | |
Daniel Dunbar | 47c3228 | 2012-03-08 20:29:02 +0000 | [diff] [blame] | 261 | private: |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 262 | NamedDecl *getUnderlyingDeclImpl() LLVM_READONLY; |
Daniel Dunbar | 47c3228 | 2012-03-08 20:29:02 +0000 | [diff] [blame] | 263 | |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 264 | protected: |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 265 | NamedDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName N) |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 266 | : Decl(DK, DC, L), Name(N) {} |
Douglas Gregor | 027ba50 | 2010-12-06 17:49:01 +0000 | [diff] [blame] | 267 | |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 268 | public: |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 269 | /// Get the identifier that names this declaration, if there is one. |
| 270 | /// |
| 271 | /// This will return NULL if this declaration has no name (e.g., for |
| 272 | /// an unnamed class) or if the name is a special name (C++ constructor, |
| 273 | /// Objective-C selector, etc.). |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 274 | IdentifierInfo *getIdentifier() const { return Name.getAsIdentifierInfo(); } |
| 275 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 276 | /// Get the name of identifier for this declaration as a StringRef. |
| 277 | /// |
Daniel Dunbar | 0013afb | 2009-10-18 02:09:45 +0000 | [diff] [blame] | 278 | /// This requires that the declaration have a name and that it be a simple |
| 279 | /// identifier. |
Chris Lattner | 01cf8db | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 280 | StringRef getName() const { |
Daniel Dunbar | 0914afe | 2009-10-19 01:21:12 +0000 | [diff] [blame] | 281 | assert(Name.isIdentifier() && "Name is not a simple identifier"); |
| 282 | return getIdentifier() ? getIdentifier()->getName() : ""; |
Daniel Dunbar | 0013afb | 2009-10-18 02:09:45 +0000 | [diff] [blame] | 283 | } |
| 284 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 285 | /// Get a human-readable name for the declaration, even if it is one of the |
| 286 | /// special kinds of names (C++ constructor, Objective-C selector, etc). |
| 287 | /// |
| 288 | /// Creating this name requires expensive string manipulation, so it should |
| 289 | /// be called only when performance doesn't matter. For simple declarations, |
| 290 | /// getNameAsCString() should suffice. |
Daniel Dunbar | 0013afb | 2009-10-18 02:09:45 +0000 | [diff] [blame] | 291 | // |
Daniel Dunbar | 8b79481 | 2009-10-19 01:20:50 +0000 | [diff] [blame] | 292 | // FIXME: This function should be renamed to indicate that it is not just an |
| 293 | // alternate form of getName(), and clients should move as appropriate. |
| 294 | // |
Daniel Dunbar | 0013afb | 2009-10-18 02:09:45 +0000 | [diff] [blame] | 295 | // FIXME: Deprecated, move clients to getName(). |
| 296 | std::string getNameAsString() const { return Name.getAsString(); } |
| 297 | |
Bruno Ricci | 6f2fa9d | 2020-08-05 11:43:39 +0100 | [diff] [blame] | 298 | /// Pretty-print the unqualified name of this declaration. Can be overloaded |
| 299 | /// by derived classes to provide a more user-friendly name when appropriate. |
Aaron Ballman | 19e984e | 2022-10-14 08:17:16 -0400 | [diff] [blame] | 300 | virtual void printName(raw_ostream &OS, const PrintingPolicy &Policy) const; |
| 301 | /// Calls printName() with the ASTContext printing policy from the decl. |
| 302 | void printName(raw_ostream &OS) const; |
Ted Kremenek | c14f5eb | 2010-05-07 20:07:19 +0000 | [diff] [blame] | 303 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 304 | /// Get the actual, stored name of the declaration, which may be a special |
| 305 | /// name. |
Bruno Ricci | 984cf99 | 2020-07-28 15:39:17 +0100 | [diff] [blame] | 306 | /// |
| 307 | /// Note that generally in diagnostics, the non-null \p NamedDecl* itself |
| 308 | /// should be sent into the diagnostic instead of using the result of |
| 309 | /// \p getDeclName(). |
| 310 | /// |
| 311 | /// A \p DeclarationName in a diagnostic will just be streamed to the output, |
| 312 | /// which will directly result in a call to \p DeclarationName::print. |
| 313 | /// |
| 314 | /// A \p NamedDecl* in a diagnostic will also ultimately result in a call to |
| 315 | /// \p DeclarationName::print, but with two customisation points along the |
| 316 | /// way (\p getNameForDiagnostic and \p printName). These are used to print |
| 317 | /// the template arguments if any, and to provide a user-friendly name for |
| 318 | /// some entities (such as unnamed variables and anonymous records). |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 319 | DeclarationName getDeclName() const { return Name; } |
| 320 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 321 | /// Set the name of this declaration. |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 322 | void setDeclName(DeclarationName N) { Name = N; } |
| 323 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 324 | /// Returns a human-readable qualified name for this declaration, like |
| 325 | /// A::B::i, for i being member of namespace A::B. |
| 326 | /// |
| 327 | /// If the declaration is not a member of context which can be named (record, |
| 328 | /// namespace), it will return the same result as printName(). |
| 329 | /// |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 330 | /// Creating this name is expensive, so it should be called only when |
| 331 | /// performance doesn't matter. |
Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 332 | void printQualifiedName(raw_ostream &OS) const; |
| 333 | void printQualifiedName(raw_ostream &OS, const PrintingPolicy &Policy) const; |
| 334 | |
Ilya Biryukov | 6648223 | 2019-09-25 15:46:04 +0000 | [diff] [blame] | 335 | /// Print only the nested name specifier part of a fully-qualified name, |
| 336 | /// including the '::' at the end. E.g. |
| 337 | /// when `printQualifiedName(D)` prints "A::B::i", |
| 338 | /// this function prints "A::B::". |
| 339 | void printNestedNameSpecifier(raw_ostream &OS) const; |
| 340 | void printNestedNameSpecifier(raw_ostream &OS, |
| 341 | const PrintingPolicy &Policy) const; |
| 342 | |
Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 343 | // FIXME: Remove string version. |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 344 | std::string getQualifiedNameAsString() const; |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 345 | |
Erich Keane | 7670b4b | 2017-12-15 16:37:14 +0000 | [diff] [blame] | 346 | /// Appends a human-readable name for this declaration into the given stream. |
John McCall | e1f2ec2 | 2009-09-11 06:45:03 +0000 | [diff] [blame] | 347 | /// |
| 348 | /// This is the method invoked by Sema when displaying a NamedDecl |
| 349 | /// in a diagnostic. It does not necessarily produce the same |
Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 350 | /// result as printName(); for example, class template |
John McCall | e1f2ec2 | 2009-09-11 06:45:03 +0000 | [diff] [blame] | 351 | /// specializations are printed with their template arguments. |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 352 | virtual void getNameForDiagnostic(raw_ostream &OS, |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 353 | const PrintingPolicy &Policy, |
Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 354 | bool Qualified) const; |
John McCall | e1f2ec2 | 2009-09-11 06:45:03 +0000 | [diff] [blame] | 355 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 356 | /// Determine whether this declaration, if known to be well-formed within |
| 357 | /// its context, will replace the declaration OldD if introduced into scope. |
| 358 | /// |
| 359 | /// A declaration will replace another declaration if, for example, it is |
| 360 | /// a redeclaration of the same variable or function, but not if it is a |
| 361 | /// declaration of a different kind (function vs. class) or an overloaded |
| 362 | /// function. |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 363 | /// |
| 364 | /// \param IsKnownNewer \c true if this declaration is known to be newer |
| 365 | /// than \p OldD (for instance, if this declaration is newly-created). |
Timm Bäder | 17fa04e | 2023-12-19 10:18:43 +0100 | [diff] [blame] | 366 | bool declarationReplaces(const NamedDecl *OldD, |
| 367 | bool IsKnownNewer = true) const; |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 368 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 369 | /// Determine whether this declaration has linkage. |
Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 370 | bool hasLinkage() const; |
| 371 | |
Douglas Gregor | 781f713 | 2012-01-06 16:59:53 +0000 | [diff] [blame] | 372 | using Decl::isModulePrivate; |
| 373 | using Decl::setModulePrivate; |
Richard Smith | 42713d7 | 2013-07-14 02:01:48 +0000 | [diff] [blame] | 374 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 375 | /// Determine whether this declaration is a C++ class member. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 376 | bool isCXXClassMember() const { |
| 377 | const DeclContext *DC = getDeclContext(); |
| 378 | |
| 379 | // C++0x [class.mem]p1: |
| 380 | // The enumerators of an unscoped enumeration defined in |
| 381 | // the class are members of the class. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 382 | if (isa<EnumDecl>(DC)) |
Alp Toker | 363e7c4 | 2014-05-31 06:11:09 +0000 | [diff] [blame] | 383 | DC = DC->getRedeclContext(); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 384 | |
| 385 | return DC->isRecord(); |
| 386 | } |
| 387 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 388 | /// Determine whether the given declaration is an instance member of |
Douglas Gregor | 3f28ec2 | 2012-03-08 02:08:05 +0000 | [diff] [blame] | 389 | /// a C++ class. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 390 | bool isCXXInstanceMember() const; |
| 391 | |
serge-sans-paille | b83b232 | 2020-12-09 09:26:27 +0100 | [diff] [blame] | 392 | /// Determine if the declaration obeys the reserved identifier rules of the |
| 393 | /// given language. |
| 394 | ReservedIdentifierStatus isReserved(const LangOptions &LangOpts) const; |
| 395 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 396 | /// Determine what kind of linkage this entity has. |
| 397 | /// |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 398 | /// This is not the linkage as defined by the standard or the codegen notion |
| 399 | /// of linkage. It is just an implementation detail that is used to compute |
| 400 | /// those. |
| 401 | Linkage getLinkageInternal() const; |
| 402 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 403 | /// Get the linkage from a semantic point of view. Entities in |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 404 | /// anonymous namespaces are external (in c++98). |
Chuanqi Xu | 2e9977c | 2023-03-13 15:43:08 +0800 | [diff] [blame] | 405 | Linkage getFormalLinkage() const; |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 406 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 407 | /// True if this decl has external linkage. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 408 | bool hasExternalFormalLinkage() const { |
Rafael Espindola | b97e896 | 2013-05-27 14:14:42 +0000 | [diff] [blame] | 409 | return isExternalFormalLinkage(getLinkageInternal()); |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 410 | } |
Rafael Espindola | b97e896 | 2013-05-27 14:14:42 +0000 | [diff] [blame] | 411 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 412 | bool isExternallyVisible() const { |
Rafael Espindola | b97e896 | 2013-05-27 14:14:42 +0000 | [diff] [blame] | 413 | return clang::isExternallyVisible(getLinkageInternal()); |
Rafael Espindola | 45b620a | 2013-03-07 02:00:27 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 416 | /// Determine whether this declaration can be redeclared in a |
| 417 | /// different translation unit. |
| 418 | bool isExternallyDeclarable() const { |
| 419 | return isExternallyVisible() && !getOwningModuleForLinkage(); |
| 420 | } |
| 421 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 422 | /// Determines the visibility of this entity. |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 423 | Visibility getVisibility() const { |
Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 424 | return getLinkageAndVisibility().getVisibility(); |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 425 | } |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 426 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 427 | /// Determines the linkage and visibility of this entity. |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 428 | LinkageInfo getLinkageAndVisibility() const; |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 429 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 430 | /// Kinds of explicit visibility. |
| 431 | enum ExplicitVisibilityKind { |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 432 | /// Do an LV computation for, ultimately, a type. |
| 433 | /// Visibility may be restricted by type visibility settings and |
| 434 | /// the visibility of template arguments. |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 435 | VisibilityForType, |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 436 | |
| 437 | /// Do an LV computation for, ultimately, a non-type declaration. |
| 438 | /// Visibility may be restricted by value visibility settings and |
| 439 | /// the visibility of template arguments. |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 440 | VisibilityForValue |
| 441 | }; |
| 442 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 443 | /// If visibility was explicitly specified for this |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 444 | /// declaration, return that visibility. |
Kazu Hirata | 6ad0788 | 2023-01-14 12:31:01 -0800 | [diff] [blame] | 445 | std::optional<Visibility> |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 446 | getExplicitVisibility(ExplicitVisibilityKind kind) const; |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 447 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 448 | /// True if the computed linkage is valid. Used for consistency |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 449 | /// checking. Should always return true. |
| 450 | bool isLinkageValid() const; |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 451 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 452 | /// True if something has required us to compute the linkage |
John McCall | 2575d88 | 2014-01-30 01:12:53 +0000 | [diff] [blame] | 453 | /// of this declaration. |
| 454 | /// |
| 455 | /// Language features which can retroactively change linkage (like a |
| 456 | /// typedef name for linkage purposes) may need to consider this, |
| 457 | /// but hopefully only in transitory ways during parsing. |
| 458 | bool hasLinkageBeenComputed() const { |
| 459 | return hasCachedLinkage(); |
| 460 | } |
| 461 | |
Corentin Jabot | a8bef88 | 2023-06-22 14:18:38 +0200 | [diff] [blame] | 462 | bool isPlaceholderVar(const LangOptions &LangOpts) const; |
| 463 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 464 | /// Looks through UsingDecls and ObjCCompatibleAliasDecls for |
Anders Carlsson | 6915bf6 | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 465 | /// the underlying named decl. |
Daniel Dunbar | 166ea9ad | 2012-03-08 18:20:41 +0000 | [diff] [blame] | 466 | NamedDecl *getUnderlyingDecl() { |
Daniel Dunbar | 47c3228 | 2012-03-08 20:29:02 +0000 | [diff] [blame] | 467 | // Fast-path the common case. |
| 468 | if (this->getKind() != UsingShadow && |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 469 | this->getKind() != ConstructorUsingShadow && |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 470 | this->getKind() != ObjCCompatibleAlias && |
| 471 | this->getKind() != NamespaceAlias) |
Daniel Dunbar | 166ea9ad | 2012-03-08 18:20:41 +0000 | [diff] [blame] | 472 | return this; |
Daniel Dunbar | 47c3228 | 2012-03-08 20:29:02 +0000 | [diff] [blame] | 473 | |
Daniel Dunbar | 166ea9ad | 2012-03-08 18:20:41 +0000 | [diff] [blame] | 474 | return getUnderlyingDeclImpl(); |
| 475 | } |
Anders Carlsson | 6915bf6 | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 476 | const NamedDecl *getUnderlyingDecl() const { |
| 477 | return const_cast<NamedDecl*>(this)->getUnderlyingDecl(); |
| 478 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | |
Rafael Espindola | 7b56f6c | 2013-10-19 16:55:03 +0000 | [diff] [blame] | 480 | NamedDecl *getMostRecentDecl() { |
Aaron Ballman | 5116a8e | 2013-11-06 22:39:46 +0000 | [diff] [blame] | 481 | return cast<NamedDecl>(static_cast<Decl *>(this)->getMostRecentDecl()); |
Rafael Espindola | 7b56f6c | 2013-10-19 16:55:03 +0000 | [diff] [blame] | 482 | } |
| 483 | const NamedDecl *getMostRecentDecl() const { |
| 484 | return const_cast<NamedDecl*>(this)->getMostRecentDecl(); |
| 485 | } |
| 486 | |
Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 487 | ObjCStringFormatFamily getObjCFStringFormattingFamily() const; |
| 488 | |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 489 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 490 | static bool classofKind(Kind K) { return K >= firstNamed && K <= lastNamed; } |
Chris Lattner | a401655 | 2007-10-06 22:53:46 +0000 | [diff] [blame] | 491 | }; |
| 492 | |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 493 | inline raw_ostream &operator<<(raw_ostream &OS, const NamedDecl &ND) { |
| 494 | ND.printName(OS); |
Benjamin Kramer | 95c7c42 | 2010-04-17 09:56:45 +0000 | [diff] [blame] | 495 | return OS; |
| 496 | } |
Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 497 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 498 | /// Represents the declaration of a label. Labels also have a |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 499 | /// corresponding LabelStmt, which indicates the position that the label was |
| 500 | /// defined at. For normal labels, the location of the decl is the same as the |
| 501 | /// location of the statement. For GNU local labels (__label__), the decl |
| 502 | /// location is where the __label__ is. |
| 503 | class LabelDecl : public NamedDecl { |
Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 504 | LabelStmt *TheStmt; |
Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 505 | StringRef MSAsmName; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 506 | bool MSAsmNameResolved = false; |
| 507 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 508 | /// For normal labels, this is the same as the main declaration |
Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 509 | /// label, i.e., the location of the identifier; for GNU local labels, |
| 510 | /// this is the location of the __label__ keyword. |
| 511 | SourceLocation LocStart; |
| 512 | |
| 513 | LabelDecl(DeclContext *DC, SourceLocation IdentL, IdentifierInfo *II, |
| 514 | LabelStmt *S, SourceLocation StartL) |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 515 | : NamedDecl(Label, DC, IdentL, II), TheStmt(S), LocStart(StartL) {} |
| 516 | |
| 517 | void anchor() override; |
Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 518 | |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 519 | public: |
| 520 | static LabelDecl *Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 521 | SourceLocation IdentL, IdentifierInfo *II); |
| 522 | static LabelDecl *Create(ASTContext &C, DeclContext *DC, |
| 523 | SourceLocation IdentL, IdentifierInfo *II, |
| 524 | SourceLocation GnuLabelL); |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 525 | static LabelDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 526 | |
Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 527 | LabelStmt *getStmt() const { return TheStmt; } |
| 528 | void setStmt(LabelStmt *T) { TheStmt = T; } |
| 529 | |
| 530 | bool isGnuLocal() const { return LocStart != getLocation(); } |
| 531 | void setLocStart(SourceLocation L) { LocStart = L; } |
| 532 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 533 | SourceRange getSourceRange() const override LLVM_READONLY { |
Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 534 | return SourceRange(LocStart, getLocation()); |
| 535 | } |
Abramo Bagnara | 124fdf6 | 2011-03-03 18:24:14 +0000 | [diff] [blame] | 536 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 537 | bool isMSAsmLabel() const { return !MSAsmName.empty(); } |
Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 538 | bool isResolvedMSAsmLabel() const { return isMSAsmLabel() && MSAsmNameResolved; } |
| 539 | void setMSAsmLabel(StringRef Name); |
| 540 | StringRef getMSAsmLabel() const { return MSAsmName; } |
| 541 | void setMSAsmLabelResolved() { MSAsmNameResolved = true; } |
| 542 | |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 543 | // Implement isa/cast/dyncast/etc. |
| 544 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 545 | static bool classofKind(Kind K) { return K == Label; } |
| 546 | }; |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 547 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 548 | /// Represent a C++ namespace. |
Krystian Stasiowski | e6ec7c8 | 2024-07-15 13:57:56 -0400 | [diff] [blame] | 549 | class NamespaceDecl : public NamedDecl, |
| 550 | public DeclContext, |
| 551 | public Redeclarable<NamespaceDecl> { |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 552 | /// The starting location of the source range, pointing |
Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 553 | /// to either the namespace or the inline keyword. |
| 554 | SourceLocation LocStart; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 555 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 556 | /// The ending location of the source range. |
Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 557 | SourceLocation RBraceLoc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | |
Krystian Stasiowski | e6ec7c8 | 2024-07-15 13:57:56 -0400 | [diff] [blame] | 559 | /// The unnamed namespace that inhabits this namespace, if any. |
| 560 | NamespaceDecl *AnonymousNamespace = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 561 | |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 562 | NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline, |
| 563 | SourceLocation StartLoc, SourceLocation IdLoc, |
Nathan James | 15e76ee | 2022-11-24 12:44:33 +0000 | [diff] [blame] | 564 | IdentifierInfo *Id, NamespaceDecl *PrevDecl, bool Nested); |
Rafael Espindola | 8ef7f68 | 2013-11-26 15:12:20 +0000 | [diff] [blame] | 565 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 566 | using redeclarable_base = Redeclarable<NamespaceDecl>; |
| 567 | |
Richard Smith | d7af8a3 | 2014-05-10 01:17:36 +0000 | [diff] [blame] | 568 | NamespaceDecl *getNextRedeclarationImpl() override; |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 569 | NamespaceDecl *getPreviousDeclImpl() override; |
| 570 | NamespaceDecl *getMostRecentDeclImpl() override; |
Rafael Espindola | 8ef7f68 | 2013-11-26 15:12:20 +0000 | [diff] [blame] | 571 | |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 572 | public: |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 573 | friend class ASTDeclReader; |
| 574 | friend class ASTDeclWriter; |
| 575 | |
Nathan James | 15e76ee | 2022-11-24 12:44:33 +0000 | [diff] [blame] | 576 | static NamespaceDecl *Create(ASTContext &C, DeclContext *DC, bool Inline, |
| 577 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 578 | IdentifierInfo *Id, NamespaceDecl *PrevDecl, |
| 579 | bool Nested); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 581 | static NamespaceDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 582 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 583 | using redecl_range = redeclarable_base::redecl_range; |
| 584 | using redecl_iterator = redeclarable_base::redecl_iterator; |
| 585 | |
Aaron Ballman | 211cd8c | 2014-03-07 00:10:58 +0000 | [diff] [blame] | 586 | using redeclarable_base::redecls_begin; |
| 587 | using redeclarable_base::redecls_end; |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 588 | using redeclarable_base::redecls; |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 589 | using redeclarable_base::getPreviousDecl; |
| 590 | using redeclarable_base::getMostRecentDecl; |
Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 591 | using redeclarable_base::isFirstDecl; |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 592 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 593 | /// Returns true if this is an anonymous namespace declaration. |
Sebastian Redl | b5c2baa | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 594 | /// |
| 595 | /// For example: |
Chris Lattner | ca025db | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 596 | /// \code |
Sebastian Redl | b5c2baa | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 597 | /// namespace { |
| 598 | /// ... |
| 599 | /// }; |
| 600 | /// \endcode |
| 601 | /// q.v. C++ [namespace.unnamed] |
John McCall | 4fa5342 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 602 | bool isAnonymousNamespace() const { |
| 603 | return !getIdentifier(); |
| 604 | } |
| 605 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 606 | /// Returns true if this is an inline namespace declaration. |
Krystian Stasiowski | e6ec7c8 | 2024-07-15 13:57:56 -0400 | [diff] [blame] | 607 | bool isInline() const { return NamespaceDeclBits.IsInline; } |
Sebastian Redl | b5c2baa | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 608 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 609 | /// Set whether this is an inline namespace declaration. |
Krystian Stasiowski | e6ec7c8 | 2024-07-15 13:57:56 -0400 | [diff] [blame] | 610 | void setInline(bool Inline) { NamespaceDeclBits.IsInline = Inline; } |
Nathan James | 15e76ee | 2022-11-24 12:44:33 +0000 | [diff] [blame] | 611 | |
| 612 | /// Returns true if this is a nested namespace declaration. |
| 613 | /// \code |
| 614 | /// namespace outer::nested { } |
| 615 | /// \endcode |
Krystian Stasiowski | e6ec7c8 | 2024-07-15 13:57:56 -0400 | [diff] [blame] | 616 | bool isNested() const { return NamespaceDeclBits.IsNested; } |
Nathan James | 15e76ee | 2022-11-24 12:44:33 +0000 | [diff] [blame] | 617 | |
| 618 | /// Set whether this is a nested namespace declaration. |
Krystian Stasiowski | e6ec7c8 | 2024-07-15 13:57:56 -0400 | [diff] [blame] | 619 | void setNested(bool Nested) { NamespaceDeclBits.IsNested = Nested; } |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 620 | |
Vassil Vassilev | 0cb7e7c | 2021-03-17 08:56:05 +0000 | [diff] [blame] | 621 | /// Returns true if the inline qualifier for \c Name is redundant. |
| 622 | bool isRedundantInlineQualifierFor(DeclarationName Name) const { |
| 623 | if (!isInline()) |
| 624 | return false; |
| 625 | auto X = lookup(Name); |
Aaron Ballman | 48f73ee | 2021-08-20 09:49:07 -0400 | [diff] [blame] | 626 | // We should not perform a lookup within a transparent context, so find a |
| 627 | // non-transparent parent context. |
| 628 | auto Y = getParent()->getNonTransparentContext()->lookup(Name); |
Vassil Vassilev | 0cb7e7c | 2021-03-17 08:56:05 +0000 | [diff] [blame] | 629 | return std::distance(X.begin(), X.end()) == |
| 630 | std::distance(Y.begin(), Y.end()); |
| 631 | } |
| 632 | |
Krystian Stasiowski | e6ec7c8 | 2024-07-15 13:57:56 -0400 | [diff] [blame] | 633 | /// Retrieve the anonymous namespace that inhabits this namespace, if any. |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 634 | NamespaceDecl *getAnonymousNamespace() const { |
Krystian Stasiowski | e6ec7c8 | 2024-07-15 13:57:56 -0400 | [diff] [blame] | 635 | return getFirstDecl()->AnonymousNamespace; |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | void setAnonymousNamespace(NamespaceDecl *D) { |
Krystian Stasiowski | e6ec7c8 | 2024-07-15 13:57:56 -0400 | [diff] [blame] | 639 | getFirstDecl()->AnonymousNamespace = D; |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 642 | /// Retrieves the canonical declaration of this namespace. |
Krystian Stasiowski | e6ec7c8 | 2024-07-15 13:57:56 -0400 | [diff] [blame] | 643 | NamespaceDecl *getCanonicalDecl() override { return getFirstDecl(); } |
| 644 | const NamespaceDecl *getCanonicalDecl() const { return getFirstDecl(); } |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 645 | |
| 646 | SourceRange getSourceRange() const override LLVM_READONLY { |
Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 647 | return SourceRange(LocStart, RBraceLoc); |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Stephen Kelly | 724e9e5 | 2018-08-09 20:05:03 +0000 | [diff] [blame] | 650 | SourceLocation getBeginLoc() const LLVM_READONLY { return LocStart; } |
Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 651 | SourceLocation getRBraceLoc() const { return RBraceLoc; } |
| 652 | void setLocStart(SourceLocation L) { LocStart = L; } |
| 653 | void setRBraceLoc(SourceLocation L) { RBraceLoc = L; } |
| 654 | |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 655 | // Implement isa/cast/dyncast/etc. |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 656 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 657 | static bool classofKind(Kind K) { return K == Namespace; } |
Argyrios Kyrtzidis | 3768ad6 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 658 | static DeclContext *castToDeclContext(const NamespaceDecl *D) { |
| 659 | return static_cast<DeclContext *>(const_cast<NamespaceDecl*>(D)); |
| 660 | } |
| 661 | static NamespaceDecl *castFromDeclContext(const DeclContext *DC) { |
| 662 | return static_cast<NamespaceDecl *>(const_cast<DeclContext*>(DC)); |
| 663 | } |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 664 | }; |
| 665 | |
Corentin Jabot | e1111e2 | 2022-11-02 12:54:46 +0100 | [diff] [blame] | 666 | class VarDecl; |
| 667 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 668 | /// Represent the declaration of a variable (in which case it is |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 669 | /// an lvalue) a function (in which case it is a function designator) or |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 670 | /// an enum constant. |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 671 | class ValueDecl : public NamedDecl { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 672 | QualType DeclType; |
Nate Begeman | a0f7897 | 2007-11-13 22:14:47 +0000 | [diff] [blame] | 673 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 674 | void anchor() override; |
| 675 | |
Steve Naroff | 6fbf0dc | 2007-03-16 00:33:25 +0000 | [diff] [blame] | 676 | protected: |
Chris Lattner | bec4134 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 677 | ValueDecl(Kind DK, DeclContext *DC, SourceLocation L, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 678 | DeclarationName N, QualType T) |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 679 | : NamedDecl(DK, DC, L, N), DeclType(T) {} |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 680 | |
Steve Naroff | 6fbf0dc | 2007-03-16 00:33:25 +0000 | [diff] [blame] | 681 | public: |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 682 | QualType getType() const { return DeclType; } |
Steve Naroff | 4dddb61 | 2007-07-09 18:55:26 +0000 | [diff] [blame] | 683 | void setType(QualType newType) { DeclType = newType; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 684 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 685 | /// Determine whether this symbol is weakly-imported, |
Lang Hames | d42bb47 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 686 | /// or declared with the weak or weak-ref attr. |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 687 | bool isWeak() const; |
Lang Hames | d42bb47 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 688 | |
Corentin Jabot | 127bf44 | 2022-03-30 14:27:44 +0200 | [diff] [blame] | 689 | /// Whether this variable is the implicit variable for a lambda init-capture. |
| 690 | /// Only VarDecl can be init captures, but both VarDecl and BindingDecl |
| 691 | /// can be captured. |
| 692 | bool isInitCapture() const; |
| 693 | |
Corentin Jabot | e1111e2 | 2022-11-02 12:54:46 +0100 | [diff] [blame] | 694 | // If this is a VarDecl, or a BindindDecl with an |
| 695 | // associated decomposed VarDecl, return that VarDecl. |
| 696 | VarDecl *getPotentiallyDecomposedVarDecl(); |
| 697 | const VarDecl *getPotentiallyDecomposedVarDecl() const { |
| 698 | return const_cast<ValueDecl *>(this)->getPotentiallyDecomposedVarDecl(); |
| 699 | } |
| 700 | |
Jason Rice | abc8812 | 2025-01-29 12:43:52 -0800 | [diff] [blame] | 701 | /// Determine whether this value is actually a function parameter pack, |
| 702 | /// init-capture pack, or structured binding pack |
| 703 | bool isParameterPack() const; |
| 704 | |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 705 | // Implement isa/cast/dyncast/etc. |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 706 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 707 | static bool classofKind(Kind K) { return K >= firstValue && K <= lastValue; } |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 708 | }; |
| 709 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 710 | /// A struct with extended info about a syntactic |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 711 | /// name qualifier, to be used for the case of out-of-line declarations. |
| 712 | struct QualifierInfo { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 713 | NestedNameSpecifierLoc QualifierLoc; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 714 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 715 | /// The number of "outer" template parameter lists. |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 716 | /// The count includes all of the template parameter lists that were matched |
| 717 | /// against the template-ids occurring into the NNS and possibly (in the |
| 718 | /// case of an explicit specialization) a final "template <>". |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 719 | unsigned NumTemplParamLists = 0; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 720 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 721 | /// A new-allocated array of size NumTemplParamLists, |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 722 | /// containing pointers to the "outer" template parameter lists. |
| 723 | /// It includes all of the template parameter lists that were matched |
| 724 | /// against the template-ids occurring into the NNS and possibly (in the |
| 725 | /// case of an explicit specialization) a final "template <>". |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 726 | TemplateParameterList** TemplParamLists = nullptr; |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 727 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 728 | QualifierInfo() = default; |
| 729 | QualifierInfo(const QualifierInfo &) = delete; |
| 730 | QualifierInfo& operator=(const QualifierInfo &) = delete; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 731 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 732 | /// Sets info about "outer" template parameter lists. |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 733 | void setTemplateParameterListsInfo(ASTContext &Context, |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 734 | ArrayRef<TemplateParameterList *> TPLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 735 | }; |
| 736 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 737 | /// Represents a ValueDecl that came out of a declarator. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 738 | /// Contains type source information through TypeSourceInfo. |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 739 | class DeclaratorDecl : public ValueDecl { |
Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 740 | // A struct representing a TInfo, a trailing requires-clause and a syntactic |
| 741 | // qualifier, to be used for the (uncommon) case of out-of-line declarations |
| 742 | // and constrained function decls. |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 743 | struct ExtInfo : public QualifierInfo { |
Boaz Brickner | bf43a13 | 2024-11-05 08:57:52 +0100 | [diff] [blame] | 744 | TypeSourceInfo *TInfo = nullptr; |
Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 745 | Expr *TrailingRequiresClause = nullptr; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 746 | }; |
| 747 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 748 | llvm::PointerUnion<TypeSourceInfo *, ExtInfo *> DeclInfo; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 749 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 750 | /// The start of the source range for this declaration, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 751 | /// ignoring outer template declarations. |
| 752 | SourceLocation InnerLocStart; |
| 753 | |
Kazu Hirata | 624cc70 | 2024-12-11 08:35:41 -0800 | [diff] [blame] | 754 | bool hasExtInfo() const { return isa<ExtInfo *>(DeclInfo); } |
| 755 | ExtInfo *getExtInfo() { return cast<ExtInfo *>(DeclInfo); } |
| 756 | const ExtInfo *getExtInfo() const { return cast<ExtInfo *>(DeclInfo); } |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 757 | |
| 758 | protected: |
| 759 | DeclaratorDecl(Kind DK, DeclContext *DC, SourceLocation L, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 760 | DeclarationName N, QualType T, TypeSourceInfo *TInfo, |
| 761 | SourceLocation StartL) |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 762 | : ValueDecl(DK, DC, L, N, T), DeclInfo(TInfo), InnerLocStart(StartL) {} |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 763 | |
| 764 | public: |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 765 | friend class ASTDeclReader; |
| 766 | friend class ASTDeclWriter; |
| 767 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 768 | TypeSourceInfo *getTypeSourceInfo() const { |
Kazu Hirata | 624cc70 | 2024-12-11 08:35:41 -0800 | [diff] [blame] | 769 | return hasExtInfo() ? getExtInfo()->TInfo |
| 770 | : cast<TypeSourceInfo *>(DeclInfo); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 771 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 772 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 773 | void setTypeSourceInfo(TypeSourceInfo *TI) { |
| 774 | if (hasExtInfo()) |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 775 | getExtInfo()->TInfo = TI; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 776 | else |
| 777 | DeclInfo = TI; |
| 778 | } |
| 779 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 780 | /// Return start of source range ignoring outer template declarations. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 781 | SourceLocation getInnerLocStart() const { return InnerLocStart; } |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 782 | void setInnerLocStart(SourceLocation L) { InnerLocStart = L; } |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 783 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 784 | /// Return start of source range taking into account any outer template |
| 785 | /// declarations. |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 786 | SourceLocation getOuterLocStart() const; |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 787 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 788 | SourceRange getSourceRange() const override LLVM_READONLY; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 789 | |
Stephen Kelly | 724e9e5 | 2018-08-09 20:05:03 +0000 | [diff] [blame] | 790 | SourceLocation getBeginLoc() const LLVM_READONLY { |
Daniel Dunbar | cdf295c | 2012-03-09 15:39:24 +0000 | [diff] [blame] | 791 | return getOuterLocStart(); |
| 792 | } |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 793 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 794 | /// Retrieve the nested-name-specifier that qualifies the name of this |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 795 | /// declaration, if it was present in the source. |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 796 | NestedNameSpecifier *getQualifier() const { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 797 | return hasExtInfo() ? getExtInfo()->QualifierLoc.getNestedNameSpecifier() |
Craig Topper | 34b4c43 | 2014-05-06 06:48:52 +0000 | [diff] [blame] | 798 | : nullptr; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 799 | } |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 800 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 801 | /// Retrieve the nested-name-specifier (with source-location |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 802 | /// information) that qualifies the name of this declaration, if it was |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 803 | /// present in the source. |
| 804 | NestedNameSpecifierLoc getQualifierLoc() const { |
| 805 | return hasExtInfo() ? getExtInfo()->QualifierLoc |
| 806 | : NestedNameSpecifierLoc(); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 807 | } |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 808 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 809 | void setQualifierInfo(NestedNameSpecifierLoc QualifierLoc); |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 810 | |
Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 811 | /// \brief Get the constraint-expression introduced by the trailing |
| 812 | /// requires-clause in the function/member declaration, or null if no |
| 813 | /// requires-clause was provided. |
| 814 | Expr *getTrailingRequiresClause() { |
| 815 | return hasExtInfo() ? getExtInfo()->TrailingRequiresClause |
| 816 | : nullptr; |
| 817 | } |
| 818 | |
| 819 | const Expr *getTrailingRequiresClause() const { |
| 820 | return hasExtInfo() ? getExtInfo()->TrailingRequiresClause |
| 821 | : nullptr; |
| 822 | } |
| 823 | |
| 824 | void setTrailingRequiresClause(Expr *TrailingRequiresClause); |
| 825 | |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 826 | unsigned getNumTemplateParameterLists() const { |
| 827 | return hasExtInfo() ? getExtInfo()->NumTemplParamLists : 0; |
| 828 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 829 | |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 830 | TemplateParameterList *getTemplateParameterList(unsigned index) const { |
| 831 | assert(index < getNumTemplateParameterLists()); |
| 832 | return getExtInfo()->TemplParamLists[index]; |
| 833 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 834 | |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 835 | void setTemplateParameterListsInfo(ASTContext &Context, |
| 836 | ArrayRef<TemplateParameterList *> TPLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 837 | |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 838 | SourceLocation getTypeSpecStartLoc() const; |
Mark de Wever | f022a5a | 2020-01-01 17:23:19 +0100 | [diff] [blame] | 839 | SourceLocation getTypeSpecEndLoc() const; |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 840 | |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 841 | // Implement isa/cast/dyncast/etc. |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 842 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 843 | static bool classofKind(Kind K) { |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 844 | return K >= firstDeclarator && K <= lastDeclarator; |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 845 | } |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 846 | }; |
| 847 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 848 | /// Structure used to store a statement, the constant value to |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 849 | /// which it was evaluated (if any), and whether or not the statement |
| 850 | /// is an integral constant expression (if known). |
| 851 | struct EvaluatedStmt { |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 852 | /// Whether this statement was already evaluated. |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 853 | bool WasEvaluated : 1; |
| 854 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 855 | /// Whether this statement is being evaluated. |
Eli Friedman | 1d6fb16 | 2009-12-03 20:31:57 +0000 | [diff] [blame] | 856 | bool IsEvaluating : 1; |
| 857 | |
Richard Smith | 3692d20 | 2020-10-19 21:29:13 -0700 | [diff] [blame] | 858 | /// Whether this variable is known to have constant initialization. This is |
| 859 | /// currently only computed in C++, for static / thread storage duration |
| 860 | /// variables that might have constant initialization and for variables that |
| 861 | /// are usable in constant expressions. |
| 862 | bool HasConstantInitialization : 1; |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 863 | |
Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 864 | /// Whether this variable is known to have constant destruction. That is, |
| 865 | /// whether running the destructor on the initial value is a side-effect |
| 866 | /// (and doesn't inspect any state that might have changed during program |
| 867 | /// execution). This is currently only computed if the destructor is |
| 868 | /// non-trivial. |
| 869 | bool HasConstantDestruction : 1; |
| 870 | |
Richard Smith | 3692d20 | 2020-10-19 21:29:13 -0700 | [diff] [blame] | 871 | /// In C++98, whether the initializer is an ICE. This affects whether the |
| 872 | /// variable is usable in constant expressions. |
| 873 | bool HasICEInit : 1; |
| 874 | bool CheckedForICEInit : 1; |
| 875 | |
Richard Smith | bc73ef0 | 2023-03-30 14:21:31 -0700 | [diff] [blame] | 876 | LazyDeclStmtPtr Value; |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 877 | APValue Evaluated; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 878 | |
Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 879 | EvaluatedStmt() |
Richard Smith | 3692d20 | 2020-10-19 21:29:13 -0700 | [diff] [blame] | 880 | : WasEvaluated(false), IsEvaluating(false), |
| 881 | HasConstantInitialization(false), HasConstantDestruction(false), |
| 882 | HasICEInit(false), CheckedForICEInit(false) {} |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 883 | }; |
| 884 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 885 | /// Represents a variable declaration or definition. |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 886 | class VarDecl : public DeclaratorDecl, public Redeclarable<VarDecl> { |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 887 | public: |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 888 | /// Initialization styles. |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 889 | enum InitializationStyle { |
Zequan Wu | e56e7bd | 2020-10-26 12:08:57 -0700 | [diff] [blame] | 890 | /// C-style initialization with assignment |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 891 | CInit, |
| 892 | |
| 893 | /// Call-style initialization (C++98) |
Zequan Wu | e56e7bd | 2020-10-26 12:08:57 -0700 | [diff] [blame] | 894 | CallInit, |
| 895 | |
| 896 | /// Direct list-initialization (C++11) |
Alan Zhao | 95a4c0c | 2023-01-04 15:12:00 -0800 | [diff] [blame] | 897 | ListInit, |
| 898 | |
| 899 | /// Parenthesized list-initialization (C++20) |
| 900 | ParenListInit |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 901 | }; |
| 902 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 903 | /// Kinds of thread-local storage. |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 904 | enum TLSKind { |
| 905 | /// Not a TLS variable. |
| 906 | TLS_None, |
| 907 | |
| 908 | /// TLS with a known-constant initializer. |
| 909 | TLS_Static, |
| 910 | |
| 911 | /// TLS with a dynamic initializer. |
| 912 | TLS_Dynamic |
| 913 | }; |
| 914 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 915 | /// Return the string used to specify the storage class \p SC. |
Daniel Dunbar | b76b7457 | 2009-04-14 02:08:49 +0000 | [diff] [blame] | 916 | /// |
| 917 | /// It is illegal to call this function with SC == None. |
| 918 | static const char *getStorageClassSpecifierString(StorageClass SC); |
| 919 | |
Douglas Gregor | d12df6b | 2009-07-20 22:03:28 +0000 | [diff] [blame] | 920 | protected: |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 921 | // A pointer union of Stmt * and EvaluatedStmt *. When an EvaluatedStmt, we |
Justin Lebar | 41e9dad | 2016-07-29 05:26:58 +0000 | [diff] [blame] | 922 | // have allocated the auxiliary struct of information there. |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 923 | // |
| 924 | // TODO: It is a bit unfortunate to use a PointerUnion inside the VarDecl for |
| 925 | // this as *many* VarDecls are ParmVarDecls that don't have default |
| 926 | // arguments. We could save some space by moving this pointer union to be |
| 927 | // allocated in trailing space when necessary. |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 928 | using InitType = llvm::PointerUnion<Stmt *, EvaluatedStmt *>; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 929 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 930 | /// The initializer for this variable or, for a ParmVarDecl, the |
Douglas Gregor | d12df6b | 2009-07-20 22:03:28 +0000 | [diff] [blame] | 931 | /// C++ default argument. |
Douglas Gregor | 770a532 | 2009-09-01 16:13:00 +0000 | [diff] [blame] | 932 | mutable InitType Init; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 933 | |
Chris Lattner | 4b08ca8 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 934 | private: |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 935 | friend class ASTDeclReader; |
| 936 | friend class ASTNodeImporter; |
| 937 | friend class StmtIteratorBase; |
| 938 | |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 939 | class VarDeclBitfields { |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 940 | friend class ASTDeclReader; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 941 | friend class VarDecl; |
Douglas Gregor | 85970ca | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 942 | |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 943 | LLVM_PREFERRED_TYPE(StorageClass) |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 944 | unsigned SClass : 3; |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 945 | LLVM_PREFERRED_TYPE(ThreadStorageClassSpecifier) |
Enea Zaffanella | acb8ecd | 2013-05-04 08:27:07 +0000 | [diff] [blame] | 946 | unsigned TSCSpec : 2; |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 947 | LLVM_PREFERRED_TYPE(InitializationStyle) |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 948 | unsigned InitStyle : 2; |
Erik Pilkington | 1e36882 | 2019-01-04 18:33:06 +0000 | [diff] [blame] | 949 | |
| 950 | /// Whether this variable is an ARC pseudo-__strong variable; see |
| 951 | /// isARCPseudoStrong() for details. |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 952 | LLVM_PREFERRED_TYPE(bool) |
Erik Pilkington | 1e36882 | 2019-01-04 18:33:06 +0000 | [diff] [blame] | 953 | unsigned ARCPseudoStrong : 1; |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 954 | }; |
Erik Pilkington | 1e36882 | 2019-01-04 18:33:06 +0000 | [diff] [blame] | 955 | enum { NumVarDeclBits = 8 }; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 956 | |
Chris Lattner | 4b08ca8 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 957 | protected: |
Ted Kremenek | 9504ae9 | 2011-10-06 04:19:35 +0000 | [diff] [blame] | 958 | enum { NumParameterIndexBits = 8 }; |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 959 | |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 960 | enum DefaultArgKind { |
| 961 | DAK_None, |
| 962 | DAK_Unparsed, |
| 963 | DAK_Uninstantiated, |
| 964 | DAK_Normal |
| 965 | }; |
| 966 | |
Martin Storsjö | f20fc65 | 2019-12-18 10:41:41 +0200 | [diff] [blame] | 967 | enum { NumScopeDepthOrObjCQualsBits = 7 }; |
| 968 | |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 969 | class ParmVarDeclBitfields { |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 970 | friend class ASTDeclReader; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 971 | friend class ParmVarDecl; |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 972 | |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 973 | LLVM_PREFERRED_TYPE(VarDeclBitfields) |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 974 | unsigned : NumVarDeclBits; |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 975 | |
| 976 | /// Whether this parameter inherits a default argument from a |
| 977 | /// prior declaration. |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 978 | LLVM_PREFERRED_TYPE(bool) |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 979 | unsigned HasInheritedDefaultArg : 1; |
| 980 | |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 981 | /// Describes the kind of default argument for this parameter. By default |
| 982 | /// this is none. If this is normal, then the default argument is stored in |
Simon Pilgrim | 750bde6 | 2017-03-31 11:00:53 +0000 | [diff] [blame] | 983 | /// the \c VarDecl initializer expression unless we were unable to parse |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 984 | /// (even an invalid) expression for the default argument. |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 985 | LLVM_PREFERRED_TYPE(DefaultArgKind) |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 986 | unsigned DefaultArgKind : 2; |
| 987 | |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 988 | /// Whether this parameter undergoes K&R argument promotion. |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 989 | LLVM_PREFERRED_TYPE(bool) |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 990 | unsigned IsKNRPromoted : 1; |
John McCall | 8249083 | 2011-05-02 00:30:12 +0000 | [diff] [blame] | 991 | |
| 992 | /// Whether this parameter is an ObjC method parameter or not. |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 993 | LLVM_PREFERRED_TYPE(bool) |
John McCall | 8249083 | 2011-05-02 00:30:12 +0000 | [diff] [blame] | 994 | unsigned IsObjCMethodParam : 1; |
| 995 | |
| 996 | /// If IsObjCMethodParam, a Decl::ObjCDeclQualifier. |
| 997 | /// Otherwise, the number of function parameter scopes enclosing |
| 998 | /// the function parameter scope in which this parameter was |
| 999 | /// declared. |
Mark de Wever | b9be5ce | 2019-11-09 15:32:35 +0100 | [diff] [blame] | 1000 | unsigned ScopeDepthOrObjCQuals : NumScopeDepthOrObjCQualsBits; |
John McCall | 8249083 | 2011-05-02 00:30:12 +0000 | [diff] [blame] | 1001 | |
| 1002 | /// The number of parameters preceding this parameter in the |
| 1003 | /// function parameter scope in which it was declared. |
Ted Kremenek | 9504ae9 | 2011-10-06 04:19:35 +0000 | [diff] [blame] | 1004 | unsigned ParameterIndex : NumParameterIndexBits; |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 1005 | }; |
| 1006 | |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1007 | class NonParmVarDeclBitfields { |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1008 | friend class ASTDeclReader; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1009 | friend class ImplicitParamDecl; |
| 1010 | friend class VarDecl; |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1011 | |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 1012 | LLVM_PREFERRED_TYPE(VarDeclBitfields) |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1013 | unsigned : NumVarDeclBits; |
| 1014 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 1015 | // FIXME: We need something similar to CXXRecordDecl::DefinitionData. |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1016 | /// Whether this variable is a definition which was demoted due to |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 1017 | /// module merge. |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 1018 | LLVM_PREFERRED_TYPE(bool) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 1019 | unsigned IsThisDeclarationADemotedDefinition : 1; |
| 1020 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1021 | /// Whether this variable is the exception variable in a C++ catch |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1022 | /// or an Objective-C @catch statement. |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 1023 | LLVM_PREFERRED_TYPE(bool) |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1024 | unsigned ExceptionVar : 1; |
| 1025 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1026 | /// Whether this local variable could be allocated in the return |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1027 | /// slot of its function, enabling the named return value optimization |
| 1028 | /// (NRVO). |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 1029 | LLVM_PREFERRED_TYPE(bool) |
Taiju Tsuiki | 3be68e1 | 2018-06-19 05:35:30 +0000 | [diff] [blame] | 1030 | unsigned NRVOVariable : 1; |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1031 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1032 | /// Whether this variable is the for-range-declaration in a C++0x |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1033 | /// for-range statement. |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 1034 | LLVM_PREFERRED_TYPE(bool) |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1035 | unsigned CXXForRangeDecl : 1; |
| 1036 | |
George Karpenkov | ec38cf7 | 2018-03-29 00:56:24 +0000 | [diff] [blame] | 1037 | /// Whether this variable is the for-in loop declaration in Objective-C. |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 1038 | LLVM_PREFERRED_TYPE(bool) |
George Karpenkov | ec38cf7 | 2018-03-29 00:56:24 +0000 | [diff] [blame] | 1039 | unsigned ObjCForDecl : 1; |
| 1040 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1041 | /// Whether this variable is (C++1z) inline. |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 1042 | LLVM_PREFERRED_TYPE(bool) |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 1043 | unsigned IsInline : 1; |
| 1044 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1045 | /// Whether this variable has (C++1z) inline explicitly specified. |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 1046 | LLVM_PREFERRED_TYPE(bool) |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 1047 | unsigned IsInlineSpecified : 1; |
| 1048 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1049 | /// Whether this variable is (C++0x) constexpr. |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 1050 | LLVM_PREFERRED_TYPE(bool) |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1051 | unsigned IsConstexpr : 1; |
| 1052 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1053 | /// Whether this variable is the implicit variable for a lambda |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1054 | /// init-capture. |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 1055 | LLVM_PREFERRED_TYPE(bool) |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1056 | unsigned IsInitCapture : 1; |
| 1057 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1058 | /// Whether this local extern variable's previous declaration was |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1059 | /// declared in the same block scope. This controls whether we should merge |
| 1060 | /// the type of this declaration with its previous declaration. |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 1061 | LLVM_PREFERRED_TYPE(bool) |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1062 | unsigned PreviousDeclInSameBlockScope : 1; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1063 | |
| 1064 | /// Defines kind of the ImplicitParamDecl: 'this', 'self', 'vtt', '_cmd' or |
| 1065 | /// something else. |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 1066 | LLVM_PREFERRED_TYPE(ImplicitParamKind) |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1067 | unsigned ImplicitParamKind : 3; |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 1068 | |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 1069 | LLVM_PREFERRED_TYPE(bool) |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 1070 | unsigned EscapingByref : 1; |
Youngsuk Kim | 90453f4 | 2024-04-04 20:45:18 -0400 | [diff] [blame] | 1071 | |
| 1072 | LLVM_PREFERRED_TYPE(bool) |
| 1073 | unsigned IsCXXCondDecl : 1; |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1074 | }; |
| 1075 | |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 1076 | union { |
| 1077 | unsigned AllBits; |
| 1078 | VarDeclBitfields VarDeclBits; |
| 1079 | ParmVarDeclBitfields ParmVarDeclBits; |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1080 | NonParmVarDeclBitfields NonParmVarDeclBits; |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 1081 | }; |
| 1082 | |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1083 | VarDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
Hubert Tong | da167a5 | 2022-03-23 11:03:44 -0400 | [diff] [blame] | 1084 | SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1085 | TypeSourceInfo *TInfo, StorageClass SC); |
Argyrios Kyrtzidis | 05898da | 2009-07-18 08:50:35 +0000 | [diff] [blame] | 1086 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1087 | using redeclarable_base = Redeclarable<VarDecl>; |
| 1088 | |
Richard Smith | d7af8a3 | 2014-05-10 01:17:36 +0000 | [diff] [blame] | 1089 | VarDecl *getNextRedeclarationImpl() override { |
| 1090 | return getNextRedeclaration(); |
| 1091 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1092 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 1093 | VarDecl *getPreviousDeclImpl() override { |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 1094 | return getPreviousDecl(); |
| 1095 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1096 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 1097 | VarDecl *getMostRecentDeclImpl() override { |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 1098 | return getMostRecentDecl(); |
| 1099 | } |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 1100 | |
Chris Lattner | 4b08ca8 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 1101 | public: |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1102 | using redecl_range = redeclarable_base::redecl_range; |
| 1103 | using redecl_iterator = redeclarable_base::redecl_iterator; |
| 1104 | |
Aaron Ballman | 211cd8c | 2014-03-07 00:10:58 +0000 | [diff] [blame] | 1105 | using redeclarable_base::redecls_begin; |
| 1106 | using redeclarable_base::redecls_end; |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 1107 | using redeclarable_base::redecls; |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 1108 | using redeclarable_base::getPreviousDecl; |
| 1109 | using redeclarable_base::getMostRecentDecl; |
Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 1110 | using redeclarable_base::isFirstDecl; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 1111 | |
Chris Lattner | bec4134 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 1112 | static VarDecl *Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1113 | SourceLocation StartLoc, SourceLocation IdLoc, |
Hubert Tong | da167a5 | 2022-03-23 11:03:44 -0400 | [diff] [blame] | 1114 | const IdentifierInfo *Id, QualType T, |
| 1115 | TypeSourceInfo *TInfo, StorageClass S); |
Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1116 | |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 1117 | static VarDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 1118 | |
| 1119 | SourceRange getSourceRange() const override LLVM_READONLY; |
Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1120 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1121 | /// Returns the storage class as written in the source. For the |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1122 | /// computed linkage of symbol, see getLinkage. |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 1123 | StorageClass getStorageClass() const { |
| 1124 | return (StorageClass) VarDeclBits.SClass; |
| 1125 | } |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1126 | void setStorageClass(StorageClass SC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1127 | |
Enea Zaffanella | acb8ecd | 2013-05-04 08:27:07 +0000 | [diff] [blame] | 1128 | void setTSCSpec(ThreadStorageClassSpecifier TSC) { |
| 1129 | VarDeclBits.TSCSpec = TSC; |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 1130 | assert(VarDeclBits.TSCSpec == TSC && "truncation"); |
Enea Zaffanella | acb8ecd | 2013-05-04 08:27:07 +0000 | [diff] [blame] | 1131 | } |
| 1132 | ThreadStorageClassSpecifier getTSCSpec() const { |
| 1133 | return static_cast<ThreadStorageClassSpecifier>(VarDeclBits.TSCSpec); |
| 1134 | } |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 1135 | TLSKind getTLSKind() const; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1136 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 1137 | /// Returns true if a variable with function scope is a non-static local |
| 1138 | /// variable. |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1139 | bool hasLocalStorage() const { |
Thorsten Schütt | 2fd11e0 | 2021-01-04 23:17:45 +0100 | [diff] [blame] | 1140 | if (getStorageClass() == SC_None) { |
Yaxun Liu | 4f33b3d | 2017-05-15 14:47:47 +0000 | [diff] [blame] | 1141 | // OpenCL v1.2 s6.5.3: The __constant or constant address space name is |
| 1142 | // used to describe variables allocated in global memory and which are |
| 1143 | // accessed inside a kernel(s) as read-only variables. As such, variables |
| 1144 | // in constant address space cannot have local storage. |
| 1145 | if (getType().getAddressSpace() == LangAS::opencl_constant) |
| 1146 | return false; |
Enea Zaffanella | 28f36ba | 2013-05-10 20:34:44 +0000 | [diff] [blame] | 1147 | // Second check is for C++11 [dcl.stc]p4. |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 1148 | return !isFileVarDecl() && getTSCSpec() == TSCS_unspecified; |
Yaxun Liu | 4f33b3d | 2017-05-15 14:47:47 +0000 | [diff] [blame] | 1149 | } |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1150 | |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1151 | // Global Named Register (GNU extension) |
Thorsten Schütt | 2fd11e0 | 2021-01-04 23:17:45 +0100 | [diff] [blame] | 1152 | if (getStorageClass() == SC_Register && !isLocalVarDeclOrParm()) |
Renato Golin | 230c5eb | 2014-05-19 18:15:42 +0000 | [diff] [blame] | 1153 | return false; |
| 1154 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1155 | // Return true for: Auto, Register. |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 1156 | // Return false for: Extern, Static, PrivateExtern, OpenCLWorkGroupLocal. |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1157 | |
Thorsten Schütt | 2fd11e0 | 2021-01-04 23:17:45 +0100 | [diff] [blame] | 1158 | return getStorageClass() >= SC_Auto; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 1161 | /// Returns true if a variable with function scope is a static local |
| 1162 | /// variable. |
Fariborz Jahanian | 3fef72f | 2010-04-18 21:01:23 +0000 | [diff] [blame] | 1163 | bool isStaticLocal() const { |
Thorsten Schütt | 2fd11e0 | 2021-01-04 23:17:45 +0100 | [diff] [blame] | 1164 | return (getStorageClass() == SC_Static || |
Enea Zaffanella | 28f36ba | 2013-05-10 20:34:44 +0000 | [diff] [blame] | 1165 | // C++11 [dcl.stc]p4 |
Thorsten Schütt | 2fd11e0 | 2021-01-04 23:17:45 +0100 | [diff] [blame] | 1166 | (getStorageClass() == SC_None && getTSCSpec() == TSCS_thread_local)) |
| 1167 | && !isFileVarDecl(); |
Fariborz Jahanian | 3fef72f | 2010-04-18 21:01:23 +0000 | [diff] [blame] | 1168 | } |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1169 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1170 | /// Returns true if a variable has extern or __private_extern__ |
Rafael Espindola | 2199c12 | 2013-03-07 02:43:24 +0000 | [diff] [blame] | 1171 | /// storage. |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1172 | bool hasExternalStorage() const { |
Thorsten Schütt | 2fd11e0 | 2021-01-04 23:17:45 +0100 | [diff] [blame] | 1173 | return getStorageClass() == SC_Extern || |
| 1174 | getStorageClass() == SC_PrivateExtern; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1177 | /// Returns true for all variables that do not have local storage. |
James Dennett | 42c4e50 | 2013-11-05 19:37:38 +0000 | [diff] [blame] | 1178 | /// |
| 1179 | /// This includes all global variables as well as static variables declared |
| 1180 | /// within a function. |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1181 | bool hasGlobalStorage() const { return !hasLocalStorage(); } |
| 1182 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1183 | /// Get the storage duration of this variable, per C++ [basic.stc]. |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 1184 | StorageDuration getStorageDuration() const { |
| 1185 | return hasLocalStorage() ? SD_Automatic : |
| 1186 | getTSCSpec() ? SD_Thread : SD_Static; |
| 1187 | } |
| 1188 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1189 | /// Compute the language linkage. |
Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1190 | LanguageLinkage getLanguageLinkage() const; |
| 1191 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1192 | /// Determines whether this variable is a variable with external, C linkage. |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1193 | bool isExternC() const; |
Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1194 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1195 | /// Determines whether this variable's context is, or is nested within, |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 1196 | /// a C++ extern "C" linkage spec. |
| 1197 | bool isInExternCContext() const; |
| 1198 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1199 | /// Determines whether this variable's context is, or is nested within, |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 1200 | /// a C++ extern "C++" linkage spec. |
| 1201 | bool isInExternCXXContext() const; |
| 1202 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1203 | /// Returns true for local variable declarations other than parameters. |
| 1204 | /// Note that this includes static variables inside of functions. It also |
| 1205 | /// includes variables inside blocks. |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1206 | /// |
| 1207 | /// void foo() { int x; static int y; extern int z; } |
John McCall | 1c9c3fd | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 1208 | bool isLocalVarDecl() const { |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 1209 | if (getKind() != Decl::Var && getKind() != Decl::Decomposition) |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1210 | return false; |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1211 | if (const DeclContext *DC = getLexicalDeclContext()) |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1212 | return DC->getRedeclContext()->isFunctionOrMethod(); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1213 | return false; |
| 1214 | } |
| 1215 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1216 | /// Similar to isLocalVarDecl but also includes parameters. |
David Majnemer | a3b04ce | 2015-01-14 00:31:13 +0000 | [diff] [blame] | 1217 | bool isLocalVarDeclOrParm() const { |
| 1218 | return isLocalVarDecl() || getKind() == Decl::ParmVar; |
| 1219 | } |
| 1220 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1221 | /// Similar to isLocalVarDecl, but excludes variables declared in blocks. |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 1222 | bool isFunctionOrMethodVarDecl() const { |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 1223 | if (getKind() != Decl::Var && getKind() != Decl::Decomposition) |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 1224 | return false; |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1225 | const DeclContext *DC = getLexicalDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1226 | return DC->isFunctionOrMethod() && DC->getDeclKind() != Decl::Block; |
Fariborz Jahanian | 3a106e7 | 2010-03-11 18:20:03 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1229 | /// Determines whether this is a static data member. |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1230 | /// |
| 1231 | /// This will only be true in C++, and applies to, e.g., the |
| 1232 | /// variable 'x' in: |
| 1233 | /// \code |
| 1234 | /// struct S { |
| 1235 | /// static int x; |
| 1236 | /// }; |
| 1237 | /// \endcode |
| 1238 | bool isStaticDataMember() const { |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1239 | // If it wasn't static, it would be a FieldDecl. |
Argyrios Kyrtzidis | 3816ed4 | 2010-07-19 10:14:41 +0000 | [diff] [blame] | 1240 | return getKind() != Decl::ParmVar && getDeclContext()->isRecord(); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 1243 | VarDecl *getCanonicalDecl() override; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1244 | const VarDecl *getCanonicalDecl() const { |
| 1245 | return const_cast<VarDecl*>(this)->getCanonicalDecl(); |
| 1246 | } |
| 1247 | |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1248 | enum DefinitionKind { |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1249 | /// This declaration is only a declaration. |
| 1250 | DeclarationOnly, |
| 1251 | |
| 1252 | /// This declaration is a tentative definition. |
| 1253 | TentativeDefinition, |
| 1254 | |
| 1255 | /// This declaration is definitely a definition. |
| 1256 | Definition |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1257 | }; |
| 1258 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1259 | /// Check whether this declaration is a definition. If this could be |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1260 | /// a tentative definition (in C), don't check whether there's an overriding |
| 1261 | /// definition. |
Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1262 | DefinitionKind isThisDeclarationADefinition(ASTContext &) const; |
| 1263 | DefinitionKind isThisDeclarationADefinition() const { |
| 1264 | return isThisDeclarationADefinition(getASTContext()); |
| 1265 | } |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1266 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1267 | /// Check whether this variable is defined in this translation unit. |
Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1268 | DefinitionKind hasDefinition(ASTContext &) const; |
| 1269 | DefinitionKind hasDefinition() const { |
| 1270 | return hasDefinition(getASTContext()); |
| 1271 | } |
John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1272 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1273 | /// Get the tentative definition that acts as the real definition in a TU. |
| 1274 | /// Returns null if there is a proper definition available. |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1275 | VarDecl *getActingDefinition(); |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1276 | const VarDecl *getActingDefinition() const { |
| 1277 | return const_cast<VarDecl*>(this)->getActingDefinition(); |
| 1278 | } |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1279 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1280 | /// Get the real (not just tentative) definition for this declaration. |
Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1281 | VarDecl *getDefinition(ASTContext &); |
| 1282 | const VarDecl *getDefinition(ASTContext &C) const { |
| 1283 | return const_cast<VarDecl*>(this)->getDefinition(C); |
| 1284 | } |
| 1285 | VarDecl *getDefinition() { |
| 1286 | return getDefinition(getASTContext()); |
| 1287 | } |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1288 | const VarDecl *getDefinition() const { |
| 1289 | return const_cast<VarDecl*>(this)->getDefinition(); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1292 | /// Determine whether this is or was instantiated from an out-of-line |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1293 | /// definition of a static data member. |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 1294 | bool isOutOfLine() const override; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1295 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1296 | /// Returns true for file scoped variable declaration. |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1297 | bool isFileVarDecl() const { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1298 | Kind K = getKind(); |
| 1299 | if (K == ParmVar || K == ImplicitParam) |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1300 | return false; |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1301 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1302 | if (getLexicalDeclContext()->getRedeclContext()->isFileContext()) |
Douglas Gregor | fcee946 | 2010-08-27 22:55:10 +0000 | [diff] [blame] | 1303 | return true; |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1304 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1305 | if (isStaticDataMember()) |
| 1306 | return true; |
| 1307 | |
| 1308 | return false; |
| 1309 | } |
| 1310 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1311 | /// Get the initializer for this variable, no matter which |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1312 | /// declaration it is attached to. |
| 1313 | const Expr *getAnyInitializer() const { |
| 1314 | const VarDecl *D; |
| 1315 | return getAnyInitializer(D); |
| 1316 | } |
| 1317 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1318 | /// Get the initializer for this variable, no matter which |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1319 | /// declaration it is attached to. Also get that declaration. |
| 1320 | const Expr *getAnyInitializer(const VarDecl *&D) const; |
| 1321 | |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 1322 | bool hasInit() const; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1323 | const Expr *getInit() const { |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 1324 | return const_cast<VarDecl *>(this)->getInit(); |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 1325 | } |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 1326 | Expr *getInit(); |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 1327 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1328 | /// Retrieve the address of the initializer expression. |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 1329 | Stmt **getInitAddress(); |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 1330 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1331 | void setInit(Expr *I); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1332 | |
Richard Smith | a6e8b68 | 2019-09-04 20:30:37 +0000 | [diff] [blame] | 1333 | /// Get the initializing declaration of this variable, if any. This is |
| 1334 | /// usually the definition, except that for a static data member it can be |
| 1335 | /// the in-class declaration. |
| 1336 | VarDecl *getInitializingDeclaration(); |
| 1337 | const VarDecl *getInitializingDeclaration() const { |
| 1338 | return const_cast<VarDecl *>(this)->getInitializingDeclaration(); |
| 1339 | } |
| 1340 | |
Richard Smith | 715f7a1 | 2019-06-11 17:50:32 +0000 | [diff] [blame] | 1341 | /// Determine whether this variable's value might be usable in a |
Richard Smith | 242ad89 | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 1342 | /// constant expression, according to the relevant language standard. |
| 1343 | /// This only checks properties of the declaration, and does not check |
| 1344 | /// whether the initializer is in fact a constant expression. |
Richard Smith | 7e7f38f | 2020-12-15 14:18:48 -0800 | [diff] [blame] | 1345 | /// |
| 1346 | /// This corresponds to C++20 [expr.const]p3's notion of a |
| 1347 | /// "potentially-constant" variable. |
Zequan Wu | e56e7bd | 2020-10-26 12:08:57 -0700 | [diff] [blame] | 1348 | bool mightBeUsableInConstantExpressions(const ASTContext &C) const; |
Richard Smith | 715f7a1 | 2019-06-11 17:50:32 +0000 | [diff] [blame] | 1349 | |
| 1350 | /// Determine whether this variable's value can be used in a |
| 1351 | /// constant expression, according to the relevant language standard, |
| 1352 | /// including checking whether it was initialized by a constant expression. |
Zequan Wu | e56e7bd | 2020-10-26 12:08:57 -0700 | [diff] [blame] | 1353 | bool isUsableInConstantExpressions(const ASTContext &C) const; |
Richard Smith | 242ad89 | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 1354 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1355 | EvaluatedStmt *ensureEvaluatedStmt() const; |
Zequan Wu | e56e7bd | 2020-10-26 12:08:57 -0700 | [diff] [blame] | 1356 | EvaluatedStmt *getEvaluatedStmt() const; |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 1357 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1358 | /// Attempt to evaluate the value of the initializer attached to this |
Richard Smith | 08c8d5b | 2020-10-19 20:03:18 -0700 | [diff] [blame] | 1359 | /// declaration, and produce notes explaining why it cannot be evaluated. |
| 1360 | /// Returns a pointer to the value if evaluation succeeded, 0 otherwise. |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 1361 | APValue *evaluateValue() const; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1362 | |
Richard Smith | 08c8d5b | 2020-10-19 20:03:18 -0700 | [diff] [blame] | 1363 | private: |
| 1364 | APValue *evaluateValueImpl(SmallVectorImpl<PartialDiagnosticAt> &Notes, |
| 1365 | bool IsConstantInitialization) const; |
| 1366 | |
| 1367 | public: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1368 | /// Return the already-evaluated value of this variable's |
Eli Friedman | 1d6fb16 | 2009-12-03 20:31:57 +0000 | [diff] [blame] | 1369 | /// initializer, or NULL if the value is not yet known. Returns pointer |
| 1370 | /// to untyped APValue if the value could not be evaluated. |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 1371 | APValue *getEvaluatedValue() const; |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 1372 | |
Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 1373 | /// Evaluate the destruction of this variable to determine if it constitutes |
| 1374 | /// constant destruction. |
| 1375 | /// |
Richard Smith | 3692d20 | 2020-10-19 21:29:13 -0700 | [diff] [blame] | 1376 | /// \pre hasConstantInitialization() |
Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 1377 | /// \return \c true if this variable has constant destruction, \c false if |
| 1378 | /// not. |
| 1379 | bool evaluateDestruction(SmallVectorImpl<PartialDiagnosticAt> &Notes) const; |
| 1380 | |
Richard Smith | 3692d20 | 2020-10-19 21:29:13 -0700 | [diff] [blame] | 1381 | /// Determine whether this variable has constant initialization. |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 1382 | /// |
Richard Smith | 3692d20 | 2020-10-19 21:29:13 -0700 | [diff] [blame] | 1383 | /// This is only set in two cases: when the language semantics require |
| 1384 | /// constant initialization (globals in C and some globals in C++), and when |
| 1385 | /// the variable is usable in constant expressions (constexpr, const int, and |
| 1386 | /// reference variables in C++). |
| 1387 | bool hasConstantInitialization() const; |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 1388 | |
Richard Smith | 3692d20 | 2020-10-19 21:29:13 -0700 | [diff] [blame] | 1389 | /// Determine whether the initializer of this variable is an integer constant |
| 1390 | /// expression. For use in C++98, where this affects whether the variable is |
| 1391 | /// usable in constant expressions. |
| 1392 | bool hasICEInitializer(const ASTContext &Context) const; |
| 1393 | |
| 1394 | /// Evaluate the initializer of this variable to determine whether it's a |
| 1395 | /// constant initializer. Should only be called once, after completing the |
| 1396 | /// definition of the variable. |
| 1397 | bool checkForConstantInitialization( |
| 1398 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const; |
Douglas Gregor | 31cf12c | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 1399 | |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1400 | void setInitStyle(InitializationStyle Style) { |
| 1401 | VarDeclBits.InitStyle = Style; |
| 1402 | } |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1403 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1404 | /// The style of initialization for this declaration. |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1405 | /// |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1406 | /// C-style initialization is "int x = 1;". Call-style initialization is |
| 1407 | /// a C++98 direct-initializer, e.g. "int x(1);". The Init expression will be |
| 1408 | /// the expression inside the parens or a "ClassType(a,b,c)" class constructor |
| 1409 | /// expression for class types. List-style initialization is C++11 syntax, |
| 1410 | /// e.g. "int x{1};". Clients can distinguish between different forms of |
| 1411 | /// initialization by checking this value. In particular, "int x = {1};" is |
| 1412 | /// C-style, "int x({1})" is call-style, and "int x{1};" is list-style; the |
| 1413 | /// Init expression in all three cases is an InitListExpr. |
| 1414 | InitializationStyle getInitStyle() const { |
| 1415 | return static_cast<InitializationStyle>(VarDeclBits.InitStyle); |
| 1416 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1417 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1418 | /// Whether the initializer is a direct-initializer (list or call). |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1419 | bool isDirectInit() const { |
| 1420 | return getInitStyle() != CInit; |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1421 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1422 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1423 | /// If this definition should pretend to be a declaration. |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 1424 | bool isThisDeclarationADemotedDefinition() const { |
| 1425 | return isa<ParmVarDecl>(this) ? false : |
| 1426 | NonParmVarDeclBits.IsThisDeclarationADemotedDefinition; |
| 1427 | } |
| 1428 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1429 | /// This is a definition which should be demoted to a declaration. |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 1430 | /// |
| 1431 | /// In some cases (mostly module merging) we can end up with two visible |
| 1432 | /// definitions one of which needs to be demoted to a declaration to keep |
| 1433 | /// the AST invariants. |
| 1434 | void demoteThisDefinitionToDeclaration() { |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1435 | assert(isThisDeclarationADefinition() && "Not a definition!"); |
| 1436 | assert(!isa<ParmVarDecl>(this) && "Cannot demote ParmVarDecls!"); |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 1437 | NonParmVarDeclBits.IsThisDeclarationADemotedDefinition = 1; |
| 1438 | } |
| 1439 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1440 | /// Determine whether this variable is the exception variable in a |
Chandler Carruth | 48687a5 | 2012-06-07 17:55:42 +0000 | [diff] [blame] | 1441 | /// C++ catch statememt or an Objective-C \@catch statement. |
Douglas Gregor | 3f324d56 | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 1442 | bool isExceptionVariable() const { |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1443 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.ExceptionVar; |
Douglas Gregor | 3f324d56 | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 1444 | } |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1445 | void setExceptionVariable(bool EV) { |
| 1446 | assert(!isa<ParmVarDecl>(this)); |
| 1447 | NonParmVarDeclBits.ExceptionVar = EV; |
| 1448 | } |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1449 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1450 | /// Determine whether this local variable can be used with the named |
Douglas Gregor | 6fd1b18 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 1451 | /// return value optimization (NRVO). |
| 1452 | /// |
| 1453 | /// The named return value optimization (NRVO) works by marking certain |
| 1454 | /// non-volatile local variables of class type as NRVO objects. These |
| 1455 | /// locals can be allocated within the return slot of their containing |
| 1456 | /// function, in which case there is no need to copy the object to the |
| 1457 | /// return slot when returning from the function. Within the function body, |
| 1458 | /// each return that returns the NRVO object will have this variable as its |
| 1459 | /// NRVO candidate. |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1460 | bool isNRVOVariable() const { |
Taiju Tsuiki | 3be68e1 | 2018-06-19 05:35:30 +0000 | [diff] [blame] | 1461 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.NRVOVariable; |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1462 | } |
| 1463 | void setNRVOVariable(bool NRVO) { |
| 1464 | assert(!isa<ParmVarDecl>(this)); |
Taiju Tsuiki | 3be68e1 | 2018-06-19 05:35:30 +0000 | [diff] [blame] | 1465 | NonParmVarDeclBits.NRVOVariable = NRVO; |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1466 | } |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1467 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1468 | /// Determine whether this variable is the for-range-declaration in |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1469 | /// a C++0x for-range statement. |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1470 | bool isCXXForRangeDecl() const { |
| 1471 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.CXXForRangeDecl; |
| 1472 | } |
| 1473 | void setCXXForRangeDecl(bool FRD) { |
| 1474 | assert(!isa<ParmVarDecl>(this)); |
| 1475 | NonParmVarDeclBits.CXXForRangeDecl = FRD; |
| 1476 | } |
John McCall | d463132 | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 1477 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1478 | /// Determine whether this variable is a for-loop declaration for a |
George Karpenkov | ec38cf7 | 2018-03-29 00:56:24 +0000 | [diff] [blame] | 1479 | /// for-in statement in Objective-C. |
| 1480 | bool isObjCForDecl() const { |
| 1481 | return NonParmVarDeclBits.ObjCForDecl; |
| 1482 | } |
| 1483 | |
| 1484 | void setObjCForDecl(bool FRD) { |
| 1485 | NonParmVarDeclBits.ObjCForDecl = FRD; |
| 1486 | } |
| 1487 | |
Erik Pilkington | 1e36882 | 2019-01-04 18:33:06 +0000 | [diff] [blame] | 1488 | /// Determine whether this variable is an ARC pseudo-__strong variable. A |
| 1489 | /// pseudo-__strong variable has a __strong-qualified type but does not |
| 1490 | /// actually retain the object written into it. Generally such variables are |
| 1491 | /// also 'const' for safety. There are 3 cases where this will be set, 1) if |
| 1492 | /// the variable is annotated with the objc_externally_retained attribute, 2) |
| 1493 | /// if its 'self' in a non-init method, or 3) if its the variable in an for-in |
| 1494 | /// loop. |
| 1495 | bool isARCPseudoStrong() const { return VarDeclBits.ARCPseudoStrong; } |
| 1496 | void setARCPseudoStrong(bool PS) { VarDeclBits.ARCPseudoStrong = PS; } |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1497 | |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 1498 | /// Whether this variable is (C++1z) inline. |
| 1499 | bool isInline() const { |
| 1500 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsInline; |
| 1501 | } |
| 1502 | bool isInlineSpecified() const { |
| 1503 | return isa<ParmVarDecl>(this) ? false |
| 1504 | : NonParmVarDeclBits.IsInlineSpecified; |
| 1505 | } |
| 1506 | void setInlineSpecified() { |
| 1507 | assert(!isa<ParmVarDecl>(this)); |
| 1508 | NonParmVarDeclBits.IsInline = true; |
| 1509 | NonParmVarDeclBits.IsInlineSpecified = true; |
| 1510 | } |
| 1511 | void setImplicitlyInline() { |
| 1512 | assert(!isa<ParmVarDecl>(this)); |
| 1513 | NonParmVarDeclBits.IsInline = true; |
| 1514 | } |
| 1515 | |
Richard Smith | 12f247f | 2012-06-08 21:09:22 +0000 | [diff] [blame] | 1516 | /// Whether this variable is (C++11) constexpr. |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1517 | bool isConstexpr() const { |
| 1518 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsConstexpr; |
| 1519 | } |
| 1520 | void setConstexpr(bool IC) { |
| 1521 | assert(!isa<ParmVarDecl>(this)); |
| 1522 | NonParmVarDeclBits.IsConstexpr = IC; |
| 1523 | } |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 1524 | |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 1525 | /// Whether this variable is the implicit variable for a lambda init-capture. |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1526 | bool isInitCapture() const { |
| 1527 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsInitCapture; |
| 1528 | } |
| 1529 | void setInitCapture(bool IC) { |
| 1530 | assert(!isa<ParmVarDecl>(this)); |
| 1531 | NonParmVarDeclBits.IsInitCapture = IC; |
| 1532 | } |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 1533 | |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 1534 | /// Whether this local extern variable declaration's previous declaration |
| 1535 | /// was declared in the same block scope. Only correct in C++. |
| 1536 | bool isPreviousDeclInSameBlockScope() const { |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1537 | return isa<ParmVarDecl>(this) |
| 1538 | ? false |
| 1539 | : NonParmVarDeclBits.PreviousDeclInSameBlockScope; |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 1540 | } |
| 1541 | void setPreviousDeclInSameBlockScope(bool Same) { |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1542 | assert(!isa<ParmVarDecl>(this)); |
| 1543 | NonParmVarDeclBits.PreviousDeclInSameBlockScope = Same; |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 1546 | /// Indicates the capture is a __block variable that is captured by a block |
| 1547 | /// that can potentially escape (a block for which BlockDecl::doesNotEscape |
| 1548 | /// returns false). |
| 1549 | bool isEscapingByref() const; |
| 1550 | |
| 1551 | /// Indicates the capture is a __block variable that is never captured by an |
| 1552 | /// escaping block. |
| 1553 | bool isNonEscapingByref() const; |
| 1554 | |
| 1555 | void setEscapingByref() { |
| 1556 | NonParmVarDeclBits.EscapingByref = true; |
| 1557 | } |
| 1558 | |
Youngsuk Kim | 90453f4 | 2024-04-04 20:45:18 -0400 | [diff] [blame] | 1559 | bool isCXXCondDecl() const { |
| 1560 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsCXXCondDecl; |
| 1561 | } |
| 1562 | |
| 1563 | void setCXXCondDecl() { |
| 1564 | assert(!isa<ParmVarDecl>(this)); |
| 1565 | NonParmVarDeclBits.IsCXXCondDecl = true; |
| 1566 | } |
| 1567 | |
Matheus Izvekov | f2d5fce | 2021-07-03 00:56:16 +0200 | [diff] [blame] | 1568 | /// Determines if this variable's alignment is dependent. |
| 1569 | bool hasDependentAlignment() const; |
| 1570 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1571 | /// Retrieve the variable declaration from which this variable could |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 1572 | /// be instantiated, if it is an instantiation (rather than a non-template). |
| 1573 | VarDecl *getTemplateInstantiationPattern() const; |
| 1574 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1575 | /// If this variable is an instantiated static data member of a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | /// class template specialization, returns the templated static data member |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1577 | /// from which it was instantiated. |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1578 | VarDecl *getInstantiatedFromStaticDataMember() const; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1580 | /// If this variable is an instantiation of a variable template or a |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 1581 | /// static data member of a class template, determine what kind of |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1582 | /// template specialization or instantiation this is. |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1583 | TemplateSpecializationKind getTemplateSpecializationKind() const; |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1584 | |
Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 1585 | /// Get the template specialization kind of this variable for the purposes of |
| 1586 | /// template instantiation. This differs from getTemplateSpecializationKind() |
| 1587 | /// for an instantiation of a class-scope explicit specialization. |
| 1588 | TemplateSpecializationKind |
| 1589 | getTemplateSpecializationKindForInstantiation() const; |
| 1590 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1591 | /// If this variable is an instantiation of a variable template or a |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 1592 | /// static data member of a class template, determine its point of |
| 1593 | /// instantiation. |
| 1594 | SourceLocation getPointOfInstantiation() const; |
| 1595 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1596 | /// If this variable is an instantiation of a static data member of a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1597 | /// class template specialization, retrieves the member specialization |
| 1598 | /// information. |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1599 | MemberSpecializationInfo *getMemberSpecializationInfo() const; |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1600 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1601 | /// For a static data member that was instantiated from a static |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1602 | /// data member of a class template, set the template specialiation kind. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1603 | void setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 1604 | SourceLocation PointOfInstantiation = SourceLocation()); |
Douglas Gregor | 5a80bd1 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1605 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1606 | /// Specify that this variable is an instantiation of the |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1607 | /// static data member VD. |
| 1608 | void setInstantiationOfStaticDataMember(VarDecl *VD, |
| 1609 | TemplateSpecializationKind TSK); |
| 1610 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1611 | /// Retrieves the variable template that is described by this |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1612 | /// variable declaration. |
| 1613 | /// |
| 1614 | /// Every variable template is represented as a VarTemplateDecl and a |
| 1615 | /// VarDecl. The former contains template properties (such as |
| 1616 | /// the template parameter lists) while the latter contains the |
| 1617 | /// actual description of the template's |
| 1618 | /// contents. VarTemplateDecl::getTemplatedDecl() retrieves the |
| 1619 | /// VarDecl that from a VarTemplateDecl, while |
| 1620 | /// getDescribedVarTemplate() retrieves the VarTemplateDecl from |
| 1621 | /// a VarDecl. |
| 1622 | VarTemplateDecl *getDescribedVarTemplate() const; |
| 1623 | |
| 1624 | void setDescribedVarTemplate(VarTemplateDecl *Template); |
| 1625 | |
Justin Lebar | 5489f85 | 2018-05-17 16:15:07 +0000 | [diff] [blame] | 1626 | // Is this variable known to have a definition somewhere in the complete |
| 1627 | // program? This may be true even if the declaration has internal linkage and |
| 1628 | // has no definition within this source file. |
| 1629 | bool isKnownToBeDefined() const; |
| 1630 | |
Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 1631 | /// Is destruction of this variable entirely suppressed? If so, the variable |
| 1632 | /// need not have a usable destructor at all. |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 1633 | bool isNoDestroy(const ASTContext &) const; |
| 1634 | |
Richard Smith | 1b8c84b | 2020-01-10 15:47:29 -0800 | [diff] [blame] | 1635 | /// Would the destruction of this variable have any effect, and if so, what |
| 1636 | /// kind? |
Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 1637 | QualType::DestructionKind needsDestruction(const ASTContext &Ctx) const; |
| 1638 | |
Eli Friedman | 4802edd | 2022-04-14 15:20:37 -0700 | [diff] [blame] | 1639 | /// Whether this variable has a flexible array member initialized with one |
| 1640 | /// or more elements. This can only be called for declarations where |
| 1641 | /// hasInit() is true. |
Eli Friedman | 5955a0f | 2022-04-14 11:56:40 -0700 | [diff] [blame] | 1642 | /// |
| 1643 | /// (The standard doesn't allow initializing flexible array members; this is |
| 1644 | /// a gcc/msvc extension.) |
Eli Friedman | 4802edd | 2022-04-14 15:20:37 -0700 | [diff] [blame] | 1645 | bool hasFlexibleArrayInit(const ASTContext &Ctx) const; |
| 1646 | |
| 1647 | /// If hasFlexibleArrayInit is true, compute the number of additional bytes |
| 1648 | /// necessary to store those elements. Otherwise, returns zero. |
| 1649 | /// |
| 1650 | /// This can only be called for declarations where hasInit() is true. |
Eli Friedman | 5955a0f | 2022-04-14 11:56:40 -0700 | [diff] [blame] | 1651 | CharUnits getFlexibleArrayInitChars(const ASTContext &Ctx) const; |
| 1652 | |
Steve Naroff | ef2ab6a | 2007-04-02 20:50:54 +0000 | [diff] [blame] | 1653 | // Implement isa/cast/dyncast/etc. |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 1654 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 1655 | static bool classofKind(Kind K) { return K >= firstVar && K <= lastVar; } |
Steve Naroff | ef2ab6a | 2007-04-02 20:50:54 +0000 | [diff] [blame] | 1656 | }; |
| 1657 | |
Vlad Serebrennikov | dda8e3d | 2023-11-06 12:00:40 +0300 | [diff] [blame] | 1658 | /// Defines the kind of the implicit parameter: is this an implicit parameter |
| 1659 | /// with pointer to 'this', 'self', '_cmd', virtual table pointers, captured |
| 1660 | /// context or something else. |
| 1661 | enum class ImplicitParamKind { |
| 1662 | /// Parameter for Objective-C 'self' argument |
| 1663 | ObjCSelf, |
| 1664 | |
| 1665 | /// Parameter for Objective-C '_cmd' argument |
| 1666 | ObjCCmd, |
| 1667 | |
| 1668 | /// Parameter for C++ 'this' argument |
| 1669 | CXXThis, |
| 1670 | |
| 1671 | /// Parameter for C++ virtual table pointers |
| 1672 | CXXVTT, |
| 1673 | |
| 1674 | /// Parameter for captured context |
| 1675 | CapturedContext, |
| 1676 | |
| 1677 | /// Parameter for Thread private variable |
| 1678 | ThreadPrivateVar, |
| 1679 | |
| 1680 | /// Other implicit parameter |
| 1681 | Other, |
| 1682 | }; |
| 1683 | |
Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 1684 | class ImplicitParamDecl : public VarDecl { |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 1685 | void anchor() override; |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1686 | |
Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 1687 | public: |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1688 | /// Create implicit parameter. |
Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 1689 | static ImplicitParamDecl *Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1690 | SourceLocation IdLoc, IdentifierInfo *Id, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1691 | QualType T, ImplicitParamKind ParamKind); |
| 1692 | static ImplicitParamDecl *Create(ASTContext &C, QualType T, |
| 1693 | ImplicitParamKind ParamKind); |
John McCall | 550d13a | 2011-02-22 22:25:56 +0000 | [diff] [blame] | 1694 | |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 1695 | static ImplicitParamDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1696 | |
| 1697 | ImplicitParamDecl(ASTContext &C, DeclContext *DC, SourceLocation IdLoc, |
Bill Wendling | fca5191 | 2024-04-11 00:33:40 +0000 | [diff] [blame] | 1698 | const IdentifierInfo *Id, QualType Type, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1699 | ImplicitParamKind ParamKind) |
| 1700 | : VarDecl(ImplicitParam, C, DC, IdLoc, IdLoc, Id, Type, |
Thorsten Schütt | 2fd11e0 | 2021-01-04 23:17:45 +0100 | [diff] [blame] | 1701 | /*TInfo=*/nullptr, SC_None) { |
Vlad Serebrennikov | dda8e3d | 2023-11-06 12:00:40 +0300 | [diff] [blame] | 1702 | NonParmVarDeclBits.ImplicitParamKind = llvm::to_underlying(ParamKind); |
John McCall | 550d13a | 2011-02-22 22:25:56 +0000 | [diff] [blame] | 1703 | setImplicit(); |
| 1704 | } |
| 1705 | |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1706 | ImplicitParamDecl(ASTContext &C, QualType Type, ImplicitParamKind ParamKind) |
| 1707 | : VarDecl(ImplicitParam, C, /*DC=*/nullptr, SourceLocation(), |
| 1708 | SourceLocation(), /*Id=*/nullptr, Type, |
Thorsten Schütt | 2fd11e0 | 2021-01-04 23:17:45 +0100 | [diff] [blame] | 1709 | /*TInfo=*/nullptr, SC_None) { |
Vlad Serebrennikov | dda8e3d | 2023-11-06 12:00:40 +0300 | [diff] [blame] | 1710 | NonParmVarDeclBits.ImplicitParamKind = llvm::to_underlying(ParamKind); |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 1711 | setImplicit(); |
| 1712 | } |
| 1713 | |
| 1714 | /// Returns the implicit parameter kind. |
| 1715 | ImplicitParamKind getParameterKind() const { |
| 1716 | return static_cast<ImplicitParamKind>(NonParmVarDeclBits.ImplicitParamKind); |
| 1717 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1718 | |
Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 1719 | // Implement isa/cast/dyncast/etc. |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 1720 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 1721 | static bool classofKind(Kind K) { return K == ImplicitParam; } |
Chris Lattner | 5696e7b | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 1722 | }; |
| 1723 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1724 | /// Represents a parameter to a function. |
Steve Naroff | ef2ab6a | 2007-04-02 20:50:54 +0000 | [diff] [blame] | 1725 | class ParmVarDecl : public VarDecl { |
John McCall | 8249083 | 2011-05-02 00:30:12 +0000 | [diff] [blame] | 1726 | public: |
| 1727 | enum { MaxFunctionScopeDepth = 255 }; |
| 1728 | enum { MaxFunctionScopeIndex = 255 }; |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1729 | |
Fariborz Jahanian | 7664ffb | 2008-12-20 20:56:12 +0000 | [diff] [blame] | 1730 | protected: |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1731 | ParmVarDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
Bill Wendling | fca5191 | 2024-04-11 00:33:40 +0000 | [diff] [blame] | 1732 | SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1733 | TypeSourceInfo *TInfo, StorageClass S, Expr *DefArg) |
| 1734 | : VarDecl(DK, C, DC, StartLoc, IdLoc, Id, T, TInfo, S) { |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 1735 | assert(ParmVarDeclBits.HasInheritedDefaultArg == false); |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 1736 | assert(ParmVarDeclBits.DefaultArgKind == DAK_None); |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 1737 | assert(ParmVarDeclBits.IsKNRPromoted == false); |
John McCall | 8249083 | 2011-05-02 00:30:12 +0000 | [diff] [blame] | 1738 | assert(ParmVarDeclBits.IsObjCMethodParam == false); |
Douglas Gregor | d12df6b | 2009-07-20 22:03:28 +0000 | [diff] [blame] | 1739 | setDefaultArg(DefArg); |
| 1740 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1741 | |
Chris Lattner | 4b08ca8 | 2008-03-15 21:10:16 +0000 | [diff] [blame] | 1742 | public: |
Chris Lattner | bec4134 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 1743 | static ParmVarDecl *Create(ASTContext &C, DeclContext *DC, |
Bill Wendling | fca5191 | 2024-04-11 00:33:40 +0000 | [diff] [blame] | 1744 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 1745 | const IdentifierInfo *Id, QualType T, |
| 1746 | TypeSourceInfo *TInfo, StorageClass S, |
| 1747 | Expr *DefArg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1748 | |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 1749 | static ParmVarDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 1750 | |
| 1751 | SourceRange getSourceRange() const override LLVM_READONLY; |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1752 | |
John McCall | 8249083 | 2011-05-02 00:30:12 +0000 | [diff] [blame] | 1753 | void setObjCMethodScopeInfo(unsigned parameterIndex) { |
| 1754 | ParmVarDeclBits.IsObjCMethodParam = true; |
Ted Kremenek | 9504ae9 | 2011-10-06 04:19:35 +0000 | [diff] [blame] | 1755 | setParameterIndex(parameterIndex); |
John McCall | 8249083 | 2011-05-02 00:30:12 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | void setScopeInfo(unsigned scopeDepth, unsigned parameterIndex) { |
| 1759 | assert(!ParmVarDeclBits.IsObjCMethodParam); |
| 1760 | |
| 1761 | ParmVarDeclBits.ScopeDepthOrObjCQuals = scopeDepth; |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1762 | assert(ParmVarDeclBits.ScopeDepthOrObjCQuals == scopeDepth |
| 1763 | && "truncation!"); |
John McCall | 8249083 | 2011-05-02 00:30:12 +0000 | [diff] [blame] | 1764 | |
Ted Kremenek | 9504ae9 | 2011-10-06 04:19:35 +0000 | [diff] [blame] | 1765 | setParameterIndex(parameterIndex); |
John McCall | 8249083 | 2011-05-02 00:30:12 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | bool isObjCMethodParameter() const { |
| 1769 | return ParmVarDeclBits.IsObjCMethodParam; |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
Akira Hatanaka | 34405b4 | 2020-12-28 11:52:27 -0800 | [diff] [blame] | 1772 | /// Determines whether this parameter is destroyed in the callee function. |
| 1773 | bool isDestroyedInCallee() const; |
| 1774 | |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1775 | unsigned getFunctionScopeDepth() const { |
John McCall | 8249083 | 2011-05-02 00:30:12 +0000 | [diff] [blame] | 1776 | if (ParmVarDeclBits.IsObjCMethodParam) return 0; |
| 1777 | return ParmVarDeclBits.ScopeDepthOrObjCQuals; |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
Mark de Wever | b9be5ce | 2019-11-09 15:32:35 +0100 | [diff] [blame] | 1780 | static constexpr unsigned getMaxFunctionScopeDepth() { |
Martin Storsjö | f20fc65 | 2019-12-18 10:41:41 +0200 | [diff] [blame] | 1781 | return (1u << NumScopeDepthOrObjCQualsBits) - 1; |
Mark de Wever | b9be5ce | 2019-11-09 15:32:35 +0100 | [diff] [blame] | 1782 | } |
| 1783 | |
John McCall | 8249083 | 2011-05-02 00:30:12 +0000 | [diff] [blame] | 1784 | /// Returns the index of this parameter in its prototype or method scope. |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1785 | unsigned getFunctionScopeIndex() const { |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 1786 | return getParameterIndex(); |
John McCall | 8fb0d9d | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
Ted Kremenek | 72be068 | 2008-02-24 03:55:14 +0000 | [diff] [blame] | 1789 | ObjCDeclQualifier getObjCDeclQualifier() const { |
John McCall | 8249083 | 2011-05-02 00:30:12 +0000 | [diff] [blame] | 1790 | if (!ParmVarDeclBits.IsObjCMethodParam) return OBJC_TQ_None; |
| 1791 | return ObjCDeclQualifier(ParmVarDeclBits.ScopeDepthOrObjCQuals); |
Ted Kremenek | 72be068 | 2008-02-24 03:55:14 +0000 | [diff] [blame] | 1792 | } |
Chris Lattner | 3d72297 | 2008-12-17 07:32:46 +0000 | [diff] [blame] | 1793 | void setObjCDeclQualifier(ObjCDeclQualifier QTVal) { |
John McCall | 8249083 | 2011-05-02 00:30:12 +0000 | [diff] [blame] | 1794 | assert(ParmVarDeclBits.IsObjCMethodParam); |
| 1795 | ParmVarDeclBits.ScopeDepthOrObjCQuals = QTVal; |
Chris Lattner | 3d72297 | 2008-12-17 07:32:46 +0000 | [diff] [blame] | 1796 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1797 | |
John McCall | 6a014d5 | 2011-03-09 04:22:44 +0000 | [diff] [blame] | 1798 | /// True if the value passed to this parameter must undergo |
| 1799 | /// K&R-style default argument promotion: |
| 1800 | /// |
| 1801 | /// C99 6.5.2.2. |
| 1802 | /// If the expression that denotes the called function has a type |
| 1803 | /// that does not include a prototype, the integer promotions are |
| 1804 | /// performed on each argument, and arguments that have type float |
| 1805 | /// are promoted to double. |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 1806 | bool isKNRPromoted() const { |
| 1807 | return ParmVarDeclBits.IsKNRPromoted; |
| 1808 | } |
| 1809 | void setKNRPromoted(bool promoted) { |
| 1810 | ParmVarDeclBits.IsKNRPromoted = promoted; |
| 1811 | } |
John McCall | 6a014d5 | 2011-03-09 04:22:44 +0000 | [diff] [blame] | 1812 | |
Corentin Jabot | af47517 | 2022-01-27 13:55:08 +0100 | [diff] [blame] | 1813 | bool isExplicitObjectParameter() const { |
| 1814 | return ExplicitObjectParameterIntroducerLoc.isValid(); |
| 1815 | } |
| 1816 | |
| 1817 | void setExplicitObjectParameterLoc(SourceLocation Loc) { |
| 1818 | ExplicitObjectParameterIntroducerLoc = Loc; |
| 1819 | } |
| 1820 | |
| 1821 | SourceLocation getExplicitObjectParamThisLoc() const { |
| 1822 | return ExplicitObjectParameterIntroducerLoc; |
| 1823 | } |
| 1824 | |
Anders Carlsson | 714d096 | 2009-12-15 19:16:31 +0000 | [diff] [blame] | 1825 | Expr *getDefaultArg(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1826 | const Expr *getDefaultArg() const { |
Anders Carlsson | 714d096 | 2009-12-15 19:16:31 +0000 | [diff] [blame] | 1827 | return const_cast<ParmVarDecl *>(this)->getDefaultArg(); |
Anders Carlsson | 8544647 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 1828 | } |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1829 | |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 1830 | void setDefaultArg(Expr *defarg); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1831 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1832 | /// Retrieve the source range that covers the entire default |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 1833 | /// argument. |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1834 | SourceRange getDefaultArgRange() const; |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 1835 | void setUninstantiatedDefaultArg(Expr *arg); |
| 1836 | Expr *getUninstantiatedDefaultArg(); |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 1837 | const Expr *getUninstantiatedDefaultArg() const { |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 1838 | return const_cast<ParmVarDecl *>(this)->getUninstantiatedDefaultArg(); |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 1839 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1840 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1841 | /// Determines whether this parameter has a default argument, |
Anders Carlsson | 8544647 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 1842 | /// either parsed or not. |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 1843 | bool hasDefaultArg() const; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1844 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1845 | /// Determines whether this parameter has a default argument that has not |
| 1846 | /// yet been parsed. This will occur during the processing of a C++ class |
| 1847 | /// whose member functions have default arguments, e.g., |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 1848 | /// @code |
| 1849 | /// class X { |
| 1850 | /// public: |
| 1851 | /// void f(int x = 17); // x has an unparsed default argument now |
| 1852 | /// }; // x has a regular default argument now |
| 1853 | /// @endcode |
| 1854 | bool hasUnparsedDefaultArg() const { |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 1855 | return ParmVarDeclBits.DefaultArgKind == DAK_Unparsed; |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
Anders Carlsson | a5224ec | 2009-08-25 01:11:14 +0000 | [diff] [blame] | 1858 | bool hasUninstantiatedDefaultArg() const { |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 1859 | return ParmVarDeclBits.DefaultArgKind == DAK_Uninstantiated; |
Anders Carlsson | a5224ec | 2009-08-25 01:11:14 +0000 | [diff] [blame] | 1860 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1861 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1862 | /// Specify that this parameter has an unparsed default argument. |
| 1863 | /// The argument will be replaced with a real default argument via |
| 1864 | /// setDefaultArg when the class definition enclosing the function |
| 1865 | /// declaration that owns this default argument is completed. |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 1866 | void setUnparsedDefaultArg() { |
| 1867 | ParmVarDeclBits.DefaultArgKind = DAK_Unparsed; |
| 1868 | } |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 1869 | |
John McCall | f3cd665 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 1870 | bool hasInheritedDefaultArg() const { |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 1871 | return ParmVarDeclBits.HasInheritedDefaultArg; |
John McCall | f3cd665 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | void setHasInheritedDefaultArg(bool I = true) { |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 1875 | ParmVarDeclBits.HasInheritedDefaultArg = I; |
John McCall | f3cd665 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 1878 | QualType getOriginalType() const; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1880 | /// Sets the function declaration that owns this |
Douglas Gregor | c72e645 | 2009-01-09 18:51:29 +0000 | [diff] [blame] | 1881 | /// ParmVarDecl. Since ParmVarDecls are often created before the |
| 1882 | /// FunctionDecls that own them, this routine is required to update |
| 1883 | /// the DeclContext appropriately. |
| 1884 | void setOwningFunction(DeclContext *FD) { setDeclContext(FD); } |
| 1885 | |
Steve Naroff | ef2ab6a | 2007-04-02 20:50:54 +0000 | [diff] [blame] | 1886 | // Implement isa/cast/dyncast/etc. |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 1887 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 1888 | static bool classofKind(Kind K) { return K == ParmVar; } |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1889 | |
Ted Kremenek | 9504ae9 | 2011-10-06 04:19:35 +0000 | [diff] [blame] | 1890 | private: |
Corentin Jabot | af47517 | 2022-01-27 13:55:08 +0100 | [diff] [blame] | 1891 | friend class ASTDeclReader; |
| 1892 | |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 1893 | enum { ParameterIndexSentinel = (1 << NumParameterIndexBits) - 1 }; |
Corentin Jabot | af47517 | 2022-01-27 13:55:08 +0100 | [diff] [blame] | 1894 | SourceLocation ExplicitObjectParameterIntroducerLoc; |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 1895 | |
Ted Kremenek | 9504ae9 | 2011-10-06 04:19:35 +0000 | [diff] [blame] | 1896 | void setParameterIndex(unsigned parameterIndex) { |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 1897 | if (parameterIndex >= ParameterIndexSentinel) { |
| 1898 | setParameterIndexLarge(parameterIndex); |
| 1899 | return; |
| 1900 | } |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1901 | |
Ted Kremenek | 9504ae9 | 2011-10-06 04:19:35 +0000 | [diff] [blame] | 1902 | ParmVarDeclBits.ParameterIndex = parameterIndex; |
| 1903 | assert(ParmVarDeclBits.ParameterIndex == parameterIndex && "truncation!"); |
| 1904 | } |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 1905 | unsigned getParameterIndex() const { |
| 1906 | unsigned d = ParmVarDeclBits.ParameterIndex; |
| 1907 | return d == ParameterIndexSentinel ? getParameterIndexLarge() : d; |
| 1908 | } |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 1909 | |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 1910 | void setParameterIndexLarge(unsigned parameterIndex); |
| 1911 | unsigned getParameterIndexLarge() const; |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 1912 | }; |
| 1913 | |
Erich Keane | 5c0d192 | 2018-11-28 18:34:14 +0000 | [diff] [blame] | 1914 | enum class MultiVersionKind { |
| 1915 | None, |
| 1916 | Target, |
| 1917 | CPUSpecific, |
Erich Keane | fc53eb6 | 2021-11-29 06:28:02 -0800 | [diff] [blame] | 1918 | CPUDispatch, |
Pavel Iliin | fe5cf48 | 2022-12-21 11:29:53 +0000 | [diff] [blame] | 1919 | TargetClones, |
| 1920 | TargetVersion |
Erich Keane | 5c0d192 | 2018-11-28 18:34:14 +0000 | [diff] [blame] | 1921 | }; |
| 1922 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1923 | /// Represents a function declaration or definition. |
Douglas Gregor | 89f238c | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 1924 | /// |
| 1925 | /// Since a given function can be declared several times in a program, |
| 1926 | /// there may be several FunctionDecls that correspond to that |
| 1927 | /// function. Only one of those FunctionDecls will be found when |
| 1928 | /// traversing the list of declarations in the context of the |
| 1929 | /// FunctionDecl (e.g., the translation unit); this FunctionDecl |
| 1930 | /// contains all of the information known about the function. Other, |
| 1931 | /// previous declarations of the function are available via the |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 1932 | /// getPreviousDecl() chain. |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 1933 | class FunctionDecl : public DeclaratorDecl, |
| 1934 | public DeclContext, |
Argyrios Kyrtzidis | fad334c | 2009-07-18 08:50:13 +0000 | [diff] [blame] | 1935 | public Redeclarable<FunctionDecl> { |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 1936 | // This class stores some data in DeclContext::FunctionDeclBits |
| 1937 | // to save some space. Use the provided accessors to access it. |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 1938 | public: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1939 | /// The kind of templated function a FunctionDecl can be. |
Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 1940 | enum TemplatedKind { |
Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 1941 | // Not templated. |
Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 1942 | TK_NonTemplate, |
Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 1943 | // The pattern in a function template declaration. |
Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 1944 | TK_FunctionTemplate, |
Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 1945 | // A non-template function that is an instantiation or explicit |
| 1946 | // specialization of a member of a templated class. |
Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 1947 | TK_MemberSpecialization, |
Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 1948 | // An instantiation or explicit specialization of a function template. |
| 1949 | // Note: this might have been instantiated from a templated class if it |
| 1950 | // is a class-scope explicit specialization. |
Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 1951 | TK_FunctionTemplateSpecialization, |
Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 1952 | // A function template specialization that hasn't yet been resolved to a |
| 1953 | // particular specialized function template. |
Erich Keane | 3ff86f9 | 2022-07-22 12:30:47 -0700 | [diff] [blame] | 1954 | TK_DependentFunctionTemplateSpecialization, |
| 1955 | // A non-template function which is in a dependent scope. |
| 1956 | TK_DependentNonTemplate |
| 1957 | |
Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 1958 | }; |
| 1959 | |
Sirraide | ef164ce | 2024-04-14 12:30:01 +0200 | [diff] [blame] | 1960 | /// Stashed information about a defaulted/deleted function body. |
| 1961 | class DefaultedOrDeletedFunctionInfo final |
| 1962 | : llvm::TrailingObjects<DefaultedOrDeletedFunctionInfo, DeclAccessPair, |
| 1963 | StringLiteral *> { |
Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 1964 | friend TrailingObjects; |
| 1965 | unsigned NumLookups; |
Sirraide | ef164ce | 2024-04-14 12:30:01 +0200 | [diff] [blame] | 1966 | bool HasDeletedMessage; |
| 1967 | |
| 1968 | size_t numTrailingObjects(OverloadToken<DeclAccessPair>) const { |
| 1969 | return NumLookups; |
| 1970 | } |
Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 1971 | |
| 1972 | public: |
Sirraide | ef164ce | 2024-04-14 12:30:01 +0200 | [diff] [blame] | 1973 | static DefaultedOrDeletedFunctionInfo * |
| 1974 | Create(ASTContext &Context, ArrayRef<DeclAccessPair> Lookups, |
| 1975 | StringLiteral *DeletedMessage = nullptr); |
| 1976 | |
Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 1977 | /// Get the unqualified lookup results that should be used in this |
| 1978 | /// defaulted function definition. |
| 1979 | ArrayRef<DeclAccessPair> getUnqualifiedLookups() const { |
| 1980 | return {getTrailingObjects<DeclAccessPair>(), NumLookups}; |
| 1981 | } |
Sirraide | ef164ce | 2024-04-14 12:30:01 +0200 | [diff] [blame] | 1982 | |
| 1983 | StringLiteral *getDeletedMessage() const { |
| 1984 | return HasDeletedMessage ? *getTrailingObjects<StringLiteral *>() |
| 1985 | : nullptr; |
| 1986 | } |
| 1987 | |
| 1988 | void setDeletedMessage(StringLiteral *Message); |
Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 1989 | }; |
| 1990 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1991 | private: |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 1992 | /// A new[]'d array of pointers to VarDecls for the formal |
Chris Lattner | ee1284a | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 1993 | /// parameters of this function. This is null if a prototype or if there are |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 1994 | /// no formals. |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1995 | ParmVarDecl **ParamInfo = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1996 | |
Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 1997 | /// The active member of this union is determined by |
Sirraide | ef164ce | 2024-04-14 12:30:01 +0200 | [diff] [blame] | 1998 | /// FunctionDeclBits.HasDefaultedOrDeletedInfo. |
Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 1999 | union { |
| 2000 | /// The body of the function. |
| 2001 | LazyDeclStmtPtr Body; |
| 2002 | /// Information about a future defaulted function definition. |
Sirraide | ef164ce | 2024-04-14 12:30:01 +0200 | [diff] [blame] | 2003 | DefaultedOrDeletedFunctionInfo *DefaultedOrDeletedInfo; |
Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 2004 | }; |
Douglas Gregor | 89f238c | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 2005 | |
Richard Trieu | e6caa26 | 2017-12-23 00:41:01 +0000 | [diff] [blame] | 2006 | unsigned ODRHash; |
| 2007 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2008 | /// End part of this FunctionDecl's source range. |
Argyrios Kyrtzidis | 49abd4d | 2009-06-22 17:13:31 +0000 | [diff] [blame] | 2009 | /// |
| 2010 | /// We could compute the full range in getSourceRange(). However, when we're |
| 2011 | /// dealing with a function definition deserialized from a PCH/AST file, |
| 2012 | /// we can only compute the full range once the function body has been |
| 2013 | /// de-serialized, so it's far better to have the (sometimes-redundant) |
| 2014 | /// EndRangeLoc. |
Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 2015 | SourceLocation EndRangeLoc; |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2016 | |
Nathan James | 1376c73 | 2022-10-04 19:38:09 +0100 | [diff] [blame] | 2017 | SourceLocation DefaultKWLoc; |
| 2018 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2019 | /// The template or declaration that this declaration |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2020 | /// describes or was instantiated from, respectively. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2021 | /// |
Erich Keane | 3ff86f9 | 2022-07-22 12:30:47 -0700 | [diff] [blame] | 2022 | /// For non-templates this value will be NULL, unless this declaration was |
| 2023 | /// declared directly inside of a function template, in which case it will |
| 2024 | /// have a pointer to a FunctionDecl, stored in the NamedDecl. For function |
| 2025 | /// declarations that describe a function template, this will be a pointer to |
| 2026 | /// a FunctionTemplateDecl, stored in the NamedDecl. For member functions of |
| 2027 | /// class template specializations, this will be a MemberSpecializationInfo |
Erich Keane | 258c3ae | 2022-07-01 11:20:16 -0700 | [diff] [blame] | 2028 | /// pointer containing information about the specialization. |
| 2029 | /// For function template specializations, this will be a |
| 2030 | /// FunctionTemplateSpecializationInfo, which contains information about |
| 2031 | /// the template being specialized and the template arguments involved in |
| 2032 | /// that specialization. |
Erich Keane | 3ff86f9 | 2022-07-22 12:30:47 -0700 | [diff] [blame] | 2033 | llvm::PointerUnion<NamedDecl *, MemberSpecializationInfo *, |
Dmitri Gribenko | 2948ec5c | 2020-01-14 15:46:13 +0100 | [diff] [blame] | 2034 | FunctionTemplateSpecializationInfo *, |
| 2035 | DependentFunctionTemplateSpecializationInfo *> |
Erich Keane | 3ff86f9 | 2022-07-22 12:30:47 -0700 | [diff] [blame] | 2036 | TemplateOrSpecialization; |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2037 | |
Erich Keane | 7670b4b | 2017-12-15 16:37:14 +0000 | [diff] [blame] | 2038 | /// Provides source/type location info for the declaration name embedded in |
| 2039 | /// the DeclaratorDecl base class. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2040 | DeclarationNameLoc DNLoc; |
| 2041 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2042 | /// Specify that this function declaration is actually a function |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2043 | /// template specialization. |
| 2044 | /// |
| 2045 | /// \param C the ASTContext. |
| 2046 | /// |
| 2047 | /// \param Template the function template that this function template |
| 2048 | /// specialization specializes. |
| 2049 | /// |
| 2050 | /// \param TemplateArgs the template arguments that produced this |
| 2051 | /// function template specialization from the template. |
| 2052 | /// |
| 2053 | /// \param InsertPos If non-NULL, the position in the function template |
| 2054 | /// specialization set where the function template specialization data will |
| 2055 | /// be inserted. |
| 2056 | /// |
| 2057 | /// \param TSK the kind of template specialization this is. |
| 2058 | /// |
| 2059 | /// \param TemplateArgsAsWritten location info of template arguments. |
| 2060 | /// |
| 2061 | /// \param PointOfInstantiation point at which the function template |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 2062 | /// specialization was first instantiated. |
Vlad Serebrennikov | 6677aef | 2024-05-17 17:49:50 +0400 | [diff] [blame] | 2063 | void setFunctionTemplateSpecialization( |
| 2064 | ASTContext &C, FunctionTemplateDecl *Template, |
| 2065 | TemplateArgumentList *TemplateArgs, void *InsertPos, |
| 2066 | TemplateSpecializationKind TSK, |
| 2067 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
| 2068 | SourceLocation PointOfInstantiation); |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2069 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2070 | /// Specify that this record is an instantiation of the |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2071 | /// member function FD. |
| 2072 | void setInstantiationOfMemberFunction(ASTContext &C, FunctionDecl *FD, |
| 2073 | TemplateSpecializationKind TSK); |
| 2074 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2075 | void setParams(ASTContext &C, ArrayRef<ParmVarDecl *> NewParamInfo); |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2076 | |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2077 | // This is unfortunately needed because ASTDeclWriter::VisitFunctionDecl |
| 2078 | // need to access this bit but we want to avoid making ASTDeclWriter |
| 2079 | // a friend of FunctionDeclBitfields just for this. |
| 2080 | bool isDeletedBit() const { return FunctionDeclBits.IsDeleted; } |
| 2081 | |
| 2082 | /// Whether an ODRHash has been stored. |
| 2083 | bool hasODRHash() const { return FunctionDeclBits.HasODRHash; } |
| 2084 | |
| 2085 | /// State that an ODRHash has been stored. |
| 2086 | void setHasODRHash(bool B = true) { FunctionDeclBits.HasODRHash = B; } |
| 2087 | |
Argyrios Kyrtzidis | 2951e14 | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 2088 | protected: |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 2089 | FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
Justin Lebar | 2b42ccc | 2016-10-28 16:26:26 +0000 | [diff] [blame] | 2090 | const DeclarationNameInfo &NameInfo, QualType T, |
Melanie Blower | bc5b5ea | 2021-07-29 12:02:20 -0400 | [diff] [blame] | 2091 | TypeSourceInfo *TInfo, StorageClass S, bool UsesFPIntrin, |
| 2092 | bool isInlineSpecified, ConstexprSpecKind ConstexprKind, |
Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 2093 | Expr *TrailingRequiresClause = nullptr); |
Douglas Gregor | 89f238c | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 2094 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 2095 | using redeclarable_base = Redeclarable<FunctionDecl>; |
| 2096 | |
Richard Smith | d7af8a3 | 2014-05-10 01:17:36 +0000 | [diff] [blame] | 2097 | FunctionDecl *getNextRedeclarationImpl() override { |
| 2098 | return getNextRedeclaration(); |
| 2099 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 2100 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 2101 | FunctionDecl *getPreviousDeclImpl() override { |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2102 | return getPreviousDecl(); |
| 2103 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 2104 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 2105 | FunctionDecl *getMostRecentDeclImpl() override { |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2106 | return getMostRecentDecl(); |
| 2107 | } |
Argyrios Kyrtzidis | 05898da | 2009-07-18 08:50:35 +0000 | [diff] [blame] | 2108 | |
Chris Lattner | 5072bae | 2008-03-15 21:24:04 +0000 | [diff] [blame] | 2109 | public: |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 2110 | friend class ASTDeclReader; |
| 2111 | friend class ASTDeclWriter; |
| 2112 | |
| 2113 | using redecl_range = redeclarable_base::redecl_range; |
| 2114 | using redecl_iterator = redeclarable_base::redecl_iterator; |
| 2115 | |
Aaron Ballman | 211cd8c | 2014-03-07 00:10:58 +0000 | [diff] [blame] | 2116 | using redeclarable_base::redecls_begin; |
| 2117 | using redeclarable_base::redecls_end; |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2118 | using redeclarable_base::redecls; |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2119 | using redeclarable_base::getPreviousDecl; |
| 2120 | using redeclarable_base::getMostRecentDecl; |
Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 2121 | using redeclarable_base::isFirstDecl; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 2122 | |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 2123 | static FunctionDecl * |
| 2124 | Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
| 2125 | SourceLocation NLoc, DeclarationName N, QualType T, |
Melanie Blower | bc5b5ea | 2021-07-29 12:02:20 -0400 | [diff] [blame] | 2126 | TypeSourceInfo *TInfo, StorageClass SC, bool UsesFPIntrin = false, |
| 2127 | bool isInlineSpecified = false, bool hasWrittenPrototype = true, |
Thorsten | 41b65f1 | 2020-11-16 14:08:33 -0500 | [diff] [blame] | 2128 | ConstexprSpecKind ConstexprKind = ConstexprSpecKind::Unspecified, |
Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 2129 | Expr *TrailingRequiresClause = nullptr) { |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2130 | DeclarationNameInfo NameInfo(N, NLoc); |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 2131 | return FunctionDecl::Create(C, DC, StartLoc, NameInfo, T, TInfo, SC, |
Melanie Blower | bc5b5ea | 2021-07-29 12:02:20 -0400 | [diff] [blame] | 2132 | UsesFPIntrin, isInlineSpecified, |
| 2133 | hasWrittenPrototype, ConstexprKind, |
| 2134 | TrailingRequiresClause); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
Melanie Blower | bc5b5ea | 2021-07-29 12:02:20 -0400 | [diff] [blame] | 2137 | static FunctionDecl * |
| 2138 | Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
| 2139 | const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo, |
| 2140 | StorageClass SC, bool UsesFPIntrin, bool isInlineSpecified, |
| 2141 | bool hasWrittenPrototype, ConstexprSpecKind ConstexprKind, |
| 2142 | Expr *TrailingRequiresClause); |
Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 2143 | |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 2144 | static FunctionDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2145 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2146 | DeclarationNameInfo getNameInfo() const { |
| 2147 | return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc); |
| 2148 | } |
| 2149 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 2150 | void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy, |
| 2151 | bool Qualified) const override; |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2152 | |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 2153 | void setRangeEnd(SourceLocation E) { EndRangeLoc = E; } |
| 2154 | |
Balazs Benics | 919df9d | 2024-05-22 17:41:31 +0200 | [diff] [blame] | 2155 | void setDeclarationNameLoc(DeclarationNameLoc L) { DNLoc = L; } |
| 2156 | |
Nicolas Manichon | cc3c935 | 2019-12-03 08:21:55 -0500 | [diff] [blame] | 2157 | /// Returns the location of the ellipsis of a variadic function. |
| 2158 | SourceLocation getEllipsisLoc() const { |
| 2159 | const auto *FPT = getType()->getAs<FunctionProtoType>(); |
| 2160 | if (FPT && FPT->isVariadic()) |
| 2161 | return FPT->getEllipsisLoc(); |
| 2162 | return SourceLocation(); |
| 2163 | } |
| 2164 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 2165 | SourceRange getSourceRange() const override LLVM_READONLY; |
Steve Naroff | 2231569 | 2008-10-03 00:02:03 +0000 | [diff] [blame] | 2166 | |
Serge Pavlov | e6e534c | 2018-03-01 07:04:11 +0000 | [diff] [blame] | 2167 | // Function definitions. |
| 2168 | // |
| 2169 | // A function declaration may be: |
| 2170 | // - a non defining declaration, |
| 2171 | // - a definition. A function may be defined because: |
| 2172 | // - it has a body, or will have it in the case of late parsing. |
| 2173 | // - it has an uninstantiated body. The body does not exist because the |
| 2174 | // function is not used yet, but the declaration is considered a |
| 2175 | // definition and does not allow other definition of this function. |
| 2176 | // - it does not have a user specified body, but it does not allow |
| 2177 | // redefinition, because it is deleted/defaulted or is defined through |
| 2178 | // some other mechanism (alias, ifunc). |
| 2179 | |
| 2180 | /// Returns true if the function has a body. |
| 2181 | /// |
| 2182 | /// The function body might be in any of the (re-)declarations of this |
| 2183 | /// function. The variant that accepts a FunctionDecl pointer will set that |
| 2184 | /// function declaration to the actual declaration containing the body (if |
| 2185 | /// there is one). |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2186 | bool hasBody(const FunctionDecl *&Definition) const; |
| 2187 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 2188 | bool hasBody() const override { |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2189 | const FunctionDecl* Definition; |
| 2190 | return hasBody(Definition); |
| 2191 | } |
| 2192 | |
Erich Keane | 7670b4b | 2017-12-15 16:37:14 +0000 | [diff] [blame] | 2193 | /// Returns whether the function has a trivial body that does not require any |
| 2194 | /// specific codegen. |
Anders Carlsson | 9bd7d16 | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 2195 | bool hasTrivialBody() const; |
| 2196 | |
Serge Pavlov | e6e534c | 2018-03-01 07:04:11 +0000 | [diff] [blame] | 2197 | /// Returns true if the function has a definition that does not need to be |
| 2198 | /// instantiated. |
| 2199 | /// |
| 2200 | /// The variant that accepts a FunctionDecl pointer will set that function |
| 2201 | /// declaration to the declaration that is a definition (if there is one). |
Richard Smith | dd8297b | 2020-10-30 18:30:56 -0700 | [diff] [blame] | 2202 | /// |
| 2203 | /// \param CheckForPendingFriendDefinition If \c true, also check for friend |
Ayushi Shukla | cc8237d | 2023-03-30 15:55:49 +0100 | [diff] [blame] | 2204 | /// declarations that were instantiated from function definitions. |
Richard Smith | dd8297b | 2020-10-30 18:30:56 -0700 | [diff] [blame] | 2205 | /// Such a declaration behaves as if it is a definition for the |
| 2206 | /// purpose of redefinition checking, but isn't actually a "real" |
| 2207 | /// definition until its body is instantiated. |
| 2208 | bool isDefined(const FunctionDecl *&Definition, |
| 2209 | bool CheckForPendingFriendDefinition = false) const; |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2210 | |
Reid Kleckner | 753a324 | 2020-03-31 09:01:06 -0700 | [diff] [blame] | 2211 | bool isDefined() const { |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2212 | const FunctionDecl* Definition; |
| 2213 | return isDefined(Definition); |
| 2214 | } |
| 2215 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2216 | /// Get the definition for this declaration. |
Yaron Keren | 18c3d06 | 2016-07-13 19:04:51 +0000 | [diff] [blame] | 2217 | FunctionDecl *getDefinition() { |
| 2218 | const FunctionDecl *Definition; |
| 2219 | if (isDefined(Definition)) |
| 2220 | return const_cast<FunctionDecl *>(Definition); |
| 2221 | return nullptr; |
| 2222 | } |
| 2223 | const FunctionDecl *getDefinition() const { |
| 2224 | return const_cast<FunctionDecl *>(this)->getDefinition(); |
| 2225 | } |
| 2226 | |
Erich Keane | 7670b4b | 2017-12-15 16:37:14 +0000 | [diff] [blame] | 2227 | /// Retrieve the body (definition) of the function. The function body might be |
| 2228 | /// in any of the (re-)declarations of this function. The variant that accepts |
| 2229 | /// a FunctionDecl pointer will set that function declaration to the actual |
| 2230 | /// declaration containing the body (if there is one). |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2231 | /// NOTE: For checking if there is a body, use hasBody() instead, to avoid |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2232 | /// unnecessary AST de-serialization of the body. |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 2233 | Stmt *getBody(const FunctionDecl *&Definition) const; |
Ted Kremenek | e5b40a9 | 2008-07-31 17:32:12 +0000 | [diff] [blame] | 2234 | |
Nico Weber | 2a9b6aa | 2014-09-03 15:28:00 +0000 | [diff] [blame] | 2235 | Stmt *getBody() const override { |
Douglas Gregor | 89f238c | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 2236 | const FunctionDecl* Definition; |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 2237 | return getBody(Definition); |
Douglas Gregor | 89f238c | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 2238 | } |
Douglas Gregor | e3dcb2d | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 2239 | |
Serge Pavlov | 10673c9 | 2017-06-04 12:53:12 +0000 | [diff] [blame] | 2240 | /// Returns whether this specific declaration of the function is also a |
| 2241 | /// definition that does not contain uninstantiated body. |
| 2242 | /// |
| 2243 | /// This does not determine whether the function has been defined (e.g., in a |
| 2244 | /// previous definition); for that information, use isDefined. |
Guillaume Chatelet | 98f3151 | 2019-09-25 11:31:28 +0200 | [diff] [blame] | 2245 | /// |
| 2246 | /// Note: the function declaration does not become a definition until the |
| 2247 | /// parser reaches the definition, if called before, this function will return |
| 2248 | /// `false`. |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2249 | bool isThisDeclarationADefinition() const { |
Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 2250 | return isDeletedAsWritten() || isDefaulted() || |
| 2251 | doesThisDeclarationHaveABody() || hasSkippedBody() || |
| 2252 | willHaveBody() || hasDefiningAttr(); |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Richard Smith | dd8297b | 2020-10-30 18:30:56 -0700 | [diff] [blame] | 2255 | /// Determine whether this specific declaration of the function is a friend |
| 2256 | /// declaration that was instantiated from a function definition. Such |
| 2257 | /// declarations behave like definitions in some contexts. |
| 2258 | bool isThisDeclarationInstantiatedFromAFriendDefinition() const; |
| 2259 | |
Serge Pavlov | e6e534c | 2018-03-01 07:04:11 +0000 | [diff] [blame] | 2260 | /// Returns whether this specific declaration of the function has a body. |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2261 | bool doesThisDeclarationHaveABody() const { |
Sirraide | ef164ce | 2024-04-14 12:30:01 +0200 | [diff] [blame] | 2262 | return (!FunctionDeclBits.HasDefaultedOrDeletedInfo && Body) || |
Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 2263 | isLateTemplateParsed(); |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2264 | } |
Douglas Gregor | 89f238c | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 2265 | |
Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 2266 | void setBody(Stmt *B); |
Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 2267 | void setLazyBody(uint64_t Offset) { |
Sirraide | ef164ce | 2024-04-14 12:30:01 +0200 | [diff] [blame] | 2268 | FunctionDeclBits.HasDefaultedOrDeletedInfo = false; |
Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 2269 | Body = LazyDeclStmtPtr(Offset); |
| 2270 | } |
| 2271 | |
Sirraide | ef164ce | 2024-04-14 12:30:01 +0200 | [diff] [blame] | 2272 | void setDefaultedOrDeletedInfo(DefaultedOrDeletedFunctionInfo *Info); |
| 2273 | DefaultedOrDeletedFunctionInfo *getDefalutedOrDeletedInfo() const; |
Sebastian Redl | 6079525 | 2009-01-09 19:57:06 +0000 | [diff] [blame] | 2274 | |
Ted Kremenek | 186a074 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 2275 | /// Whether this function is variadic. |
| 2276 | bool isVariadic() const; |
| 2277 | |
Anders Carlsson | 0a7c01f | 2009-05-14 22:15:41 +0000 | [diff] [blame] | 2278 | /// Whether this function is marked as virtual explicitly. |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2279 | bool isVirtualAsWritten() const { |
| 2280 | return FunctionDeclBits.IsVirtualAsWritten; |
| 2281 | } |
| 2282 | |
| 2283 | /// State that this function is marked as virtual explicitly. |
| 2284 | void setVirtualAsWritten(bool V) { FunctionDeclBits.IsVirtualAsWritten = V; } |
Sebastian Redl | 6079525 | 2009-01-09 19:57:06 +0000 | [diff] [blame] | 2285 | |
Sebastian Redl | 6138f2b | 2009-01-09 22:29:03 +0000 | [diff] [blame] | 2286 | /// Whether this virtual function is pure, i.e. makes the containing class |
| 2287 | /// abstract. |
cor3ntin | e90e43f | 2024-01-18 15:30:58 +0100 | [diff] [blame] | 2288 | bool isPureVirtual() const { return FunctionDeclBits.IsPureVirtual; } |
| 2289 | void setIsPureVirtual(bool P = true); |
Sebastian Redl | 6079525 | 2009-01-09 19:57:06 +0000 | [diff] [blame] | 2290 | |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2291 | /// Whether this templated function will be late parsed. |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2292 | bool isLateTemplateParsed() const { |
| 2293 | return FunctionDeclBits.IsLateTemplateParsed; |
| 2294 | } |
| 2295 | |
| 2296 | /// State that this templated function will be late parsed. |
| 2297 | void setLateTemplateParsed(bool ILT = true) { |
| 2298 | FunctionDeclBits.IsLateTemplateParsed = ILT; |
| 2299 | } |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2300 | |
Matheus Izvekov | 346077a | 2025-02-05 14:12:12 -0300 | [diff] [blame] | 2301 | bool isInstantiatedFromMemberTemplate() const { |
| 2302 | return FunctionDeclBits.IsInstantiatedFromMemberTemplate; |
| 2303 | } |
| 2304 | void setInstantiatedFromMemberTemplate(bool Val = true) { |
| 2305 | FunctionDeclBits.IsInstantiatedFromMemberTemplate = Val; |
| 2306 | } |
| 2307 | |
Douglas Gregor | 8a27391 | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2308 | /// Whether this function is "trivial" in some specialized C++ senses. |
| 2309 | /// Can only be true for default constructors, copy constructors, |
| 2310 | /// copy assignment operators, and destructors. Not meaningful until |
| 2311 | /// the class has been fully built by Sema. |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2312 | bool isTrivial() const { return FunctionDeclBits.IsTrivial; } |
| 2313 | void setTrivial(bool IT) { FunctionDeclBits.IsTrivial = IT; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2314 | |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2315 | bool isTrivialForCall() const { return FunctionDeclBits.IsTrivialForCall; } |
| 2316 | void setTrivialForCall(bool IT) { FunctionDeclBits.IsTrivialForCall = IT; } |
Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 2317 | |
mydeveloperday | 447ea9b | 2020-05-20 07:22:01 +0100 | [diff] [blame] | 2318 | /// Whether this function is defaulted. Valid for e.g. |
| 2319 | /// special member functions, defaulted comparisions (not methods!). |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2320 | bool isDefaulted() const { return FunctionDeclBits.IsDefaulted; } |
| 2321 | void setDefaulted(bool D = true) { FunctionDeclBits.IsDefaulted = D; } |
Alexis Hunt | 5dafebc | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 2322 | |
mydeveloperday | 447ea9b | 2020-05-20 07:22:01 +0100 | [diff] [blame] | 2323 | /// Whether this function is explicitly defaulted. |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2324 | bool isExplicitlyDefaulted() const { |
| 2325 | return FunctionDeclBits.IsExplicitlyDefaulted; |
| 2326 | } |
| 2327 | |
mydeveloperday | 447ea9b | 2020-05-20 07:22:01 +0100 | [diff] [blame] | 2328 | /// State that this function is explicitly defaulted. |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2329 | void setExplicitlyDefaulted(bool ED = true) { |
| 2330 | FunctionDeclBits.IsExplicitlyDefaulted = ED; |
| 2331 | } |
Alexis Hunt | 5dafebc | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 2332 | |
Nathan James | 1376c73 | 2022-10-04 19:38:09 +0100 | [diff] [blame] | 2333 | SourceLocation getDefaultLoc() const { |
| 2334 | return isExplicitlyDefaulted() ? DefaultKWLoc : SourceLocation(); |
| 2335 | } |
| 2336 | |
| 2337 | void setDefaultLoc(SourceLocation NewLoc) { |
Erich Keane | eed92a9 | 2022-10-04 14:33:36 -0700 | [diff] [blame] | 2338 | assert((NewLoc.isInvalid() || isExplicitlyDefaulted()) && |
| 2339 | "Can't set default loc is function isn't explicitly defaulted"); |
Nathan James | 1376c73 | 2022-10-04 19:38:09 +0100 | [diff] [blame] | 2340 | DefaultKWLoc = NewLoc; |
| 2341 | } |
| 2342 | |
Richard Smith | cafc741 | 2019-12-04 15:25:27 -0800 | [diff] [blame] | 2343 | /// True if this method is user-declared and was not |
| 2344 | /// deleted or defaulted on its first declaration. |
| 2345 | bool isUserProvided() const { |
| 2346 | auto *DeclAsWritten = this; |
| 2347 | if (FunctionDecl *Pattern = getTemplateInstantiationPattern()) |
| 2348 | DeclAsWritten = Pattern; |
| 2349 | return !(DeclAsWritten->isDeleted() || |
| 2350 | DeclAsWritten->getCanonicalDecl()->isDefaulted()); |
| 2351 | } |
| 2352 | |
Roy Jacobson | 21eb1af | 2022-06-16 20:52:12 +0300 | [diff] [blame] | 2353 | bool isIneligibleOrNotSelected() const { |
| 2354 | return FunctionDeclBits.IsIneligibleOrNotSelected; |
| 2355 | } |
| 2356 | void setIneligibleOrNotSelected(bool II) { |
| 2357 | FunctionDeclBits.IsIneligibleOrNotSelected = II; |
| 2358 | } |
| 2359 | |
John McCall | caa1945 | 2009-07-28 01:00:58 +0000 | [diff] [blame] | 2360 | /// Whether falling off this function implicitly returns null/zero. |
| 2361 | /// If a more specific implicit return value is required, front-ends |
| 2362 | /// should synthesize the appropriate return statements. |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2363 | bool hasImplicitReturnZero() const { |
| 2364 | return FunctionDeclBits.HasImplicitReturnZero; |
| 2365 | } |
| 2366 | |
| 2367 | /// State that falling off this function implicitly returns null/zero. |
| 2368 | /// If a more specific implicit return value is required, front-ends |
| 2369 | /// should synthesize the appropriate return statements. |
| 2370 | void setHasImplicitReturnZero(bool IRZ) { |
| 2371 | FunctionDeclBits.HasImplicitReturnZero = IRZ; |
| 2372 | } |
John McCall | caa1945 | 2009-07-28 01:00:58 +0000 | [diff] [blame] | 2373 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2374 | /// Whether this function has a prototype, either because one |
Douglas Gregor | 739ef0c | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 2375 | /// was explicitly written or because it was "inherited" by merging |
| 2376 | /// a declaration without a prototype with a declaration that has a |
| 2377 | /// prototype. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2378 | bool hasPrototype() const { |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2379 | return hasWrittenPrototype() || hasInheritedPrototype(); |
Anders Carlsson | e0dd1d5 | 2009-05-14 21:46:00 +0000 | [diff] [blame] | 2380 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2381 | |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2382 | /// Whether this function has a written prototype. |
| 2383 | bool hasWrittenPrototype() const { |
| 2384 | return FunctionDeclBits.HasWrittenPrototype; |
| 2385 | } |
| 2386 | |
| 2387 | /// State that this function has a written prototype. |
| 2388 | void setHasWrittenPrototype(bool P = true) { |
| 2389 | FunctionDeclBits.HasWrittenPrototype = P; |
| 2390 | } |
Douglas Gregor | 739ef0c | 2009-02-25 16:33:18 +0000 | [diff] [blame] | 2391 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2392 | /// Whether this function inherited its prototype from a |
Douglas Gregor | bcbf863 | 2009-02-16 18:20:44 +0000 | [diff] [blame] | 2393 | /// previous declaration. |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2394 | bool hasInheritedPrototype() const { |
| 2395 | return FunctionDeclBits.HasInheritedPrototype; |
| 2396 | } |
| 2397 | |
| 2398 | /// State that this function inherited its prototype from a |
| 2399 | /// previous declaration. |
| 2400 | void setHasInheritedPrototype(bool P = true) { |
| 2401 | FunctionDeclBits.HasInheritedPrototype = P; |
| 2402 | } |
Douglas Gregor | bcbf863 | 2009-02-16 18:20:44 +0000 | [diff] [blame] | 2403 | |
Richard Smith | 12f247f | 2012-06-08 21:09:22 +0000 | [diff] [blame] | 2404 | /// Whether this is a (C++11) constexpr function or constexpr constructor. |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 2405 | bool isConstexpr() const { |
Thorsten | 41b65f1 | 2020-11-16 14:08:33 -0500 | [diff] [blame] | 2406 | return getConstexprKind() != ConstexprSpecKind::Unspecified; |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 2407 | } |
| 2408 | void setConstexprKind(ConstexprSpecKind CSK) { |
Thorsten | 41b65f1 | 2020-11-16 14:08:33 -0500 | [diff] [blame] | 2409 | FunctionDeclBits.ConstexprKind = static_cast<uint64_t>(CSK); |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 2410 | } |
| 2411 | ConstexprSpecKind getConstexprKind() const { |
| 2412 | return static_cast<ConstexprSpecKind>(FunctionDeclBits.ConstexprKind); |
| 2413 | } |
| 2414 | bool isConstexprSpecified() const { |
Thorsten | 41b65f1 | 2020-11-16 14:08:33 -0500 | [diff] [blame] | 2415 | return getConstexprKind() == ConstexprSpecKind::Constexpr; |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 2416 | } |
| 2417 | bool isConsteval() const { |
Thorsten | 41b65f1 | 2020-11-16 14:08:33 -0500 | [diff] [blame] | 2418 | return getConstexprKind() == ConstexprSpecKind::Consteval; |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 2419 | } |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2420 | |
Corentin Jabot | 4676885 | 2023-05-08 12:18:43 +0200 | [diff] [blame] | 2421 | void setBodyContainsImmediateEscalatingExpressions(bool Set) { |
| 2422 | FunctionDeclBits.BodyContainsImmediateEscalatingExpression = Set; |
| 2423 | } |
| 2424 | |
| 2425 | bool BodyContainsImmediateEscalatingExpressions() const { |
| 2426 | return FunctionDeclBits.BodyContainsImmediateEscalatingExpression; |
| 2427 | } |
| 2428 | |
| 2429 | bool isImmediateEscalating() const; |
| 2430 | |
| 2431 | // The function is a C++ immediate function. |
| 2432 | // This can be either a consteval function, or an immediate escalating |
| 2433 | // function containing an immediate escalating expression. |
| 2434 | bool isImmediateFunction() const; |
| 2435 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2436 | /// Whether the instantiation of this function is pending. |
Sunil Srivastava | 15ed292 | 2017-06-20 22:08:44 +0000 | [diff] [blame] | 2437 | /// This bit is set when the decision to instantiate this function is made |
| 2438 | /// and unset if and when the function body is created. That leaves out |
| 2439 | /// cases where instantiation did not happen because the template definition |
| 2440 | /// was not seen in this TU. This bit remains set in those cases, under the |
| 2441 | /// assumption that the instantiation will happen in some other TU. |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2442 | bool instantiationIsPending() const { |
| 2443 | return FunctionDeclBits.InstantiationIsPending; |
| 2444 | } |
| 2445 | |
| 2446 | /// State that the instantiation of this function is pending. |
| 2447 | /// (see instantiationIsPending) |
| 2448 | void setInstantiationIsPending(bool IC) { |
| 2449 | FunctionDeclBits.InstantiationIsPending = IC; |
| 2450 | } |
Sunil Srivastava | 15ed292 | 2017-06-20 22:08:44 +0000 | [diff] [blame] | 2451 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2452 | /// Indicates the function uses __try. |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2453 | bool usesSEHTry() const { return FunctionDeclBits.UsesSEHTry; } |
| 2454 | void setUsesSEHTry(bool UST) { FunctionDeclBits.UsesSEHTry = UST; } |
Reid Kleckner | deeddec | 2015-02-05 18:56:03 +0000 | [diff] [blame] | 2455 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2456 | /// Whether this function has been deleted. |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2457 | /// |
| 2458 | /// A function that is "deleted" (via the C++0x "= delete" syntax) |
| 2459 | /// acts like a normal function, except that it cannot actually be |
| 2460 | /// called or have its address taken. Deleted functions are |
| 2461 | /// typically used in C++ overload resolution to attract arguments |
| 2462 | /// whose type or lvalue/rvalue-ness would permit the use of a |
| 2463 | /// different overload that would behave incorrectly. For example, |
| 2464 | /// one might use deleted functions to ban implicit conversion from |
| 2465 | /// a floating-point number to an Integer type: |
| 2466 | /// |
| 2467 | /// @code |
| 2468 | /// struct Integer { |
| 2469 | /// Integer(long); // construct from a long |
| 2470 | /// Integer(double) = delete; // no construction from float or double |
| 2471 | /// Integer(long double) = delete; // no construction from long double |
| 2472 | /// }; |
| 2473 | /// @endcode |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2474 | // If a function is deleted, its first declaration must be. |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2475 | bool isDeleted() const { |
| 2476 | return getCanonicalDecl()->FunctionDeclBits.IsDeleted; |
| 2477 | } |
| 2478 | |
| 2479 | bool isDeletedAsWritten() const { |
| 2480 | return FunctionDeclBits.IsDeleted && !isDefaulted(); |
| 2481 | } |
| 2482 | |
Sirraide | ef164ce | 2024-04-14 12:30:01 +0200 | [diff] [blame] | 2483 | void setDeletedAsWritten(bool D = true, StringLiteral *Message = nullptr); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2484 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2485 | /// Determines whether this function is "main", which is the |
John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2486 | /// entry point into an executable program. |
Douglas Gregor | 16618f2 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 2487 | bool isMain() const; |
Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2488 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2489 | /// Determines whether this function is a MSVCRT user defined entry |
David Majnemer | c729b0b | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 2490 | /// point. |
| 2491 | bool isMSVCRTEntryPoint() const; |
| 2492 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2493 | /// Determines whether this operator new or delete is one |
John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2494 | /// of the reserved global placement operators: |
| 2495 | /// void *operator new(size_t, void *); |
| 2496 | /// void *operator new[](size_t, void *); |
| 2497 | /// void operator delete(void *, void *); |
| 2498 | /// void operator delete[](void *, void *); |
| 2499 | /// These functions have special behavior under [new.delete.placement]: |
| 2500 | /// These functions are reserved, a C++ program may not define |
| 2501 | /// functions that displace the versions in the Standard C++ library. |
| 2502 | /// The provisions of [basic.stc.dynamic] do not apply to these |
| 2503 | /// reserved placement forms of operator new and operator delete. |
| 2504 | /// |
| 2505 | /// This function must be an allocation or deallocation function. |
| 2506 | bool isReservedGlobalPlacementOperator() const; |
| 2507 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2508 | /// Determines whether this function is one of the replaceable |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2509 | /// global allocation functions: |
| 2510 | /// void *operator new(size_t); |
| 2511 | /// void *operator new(size_t, const std::nothrow_t &) noexcept; |
| 2512 | /// void *operator new[](size_t); |
| 2513 | /// void *operator new[](size_t, const std::nothrow_t &) noexcept; |
| 2514 | /// void operator delete(void *) noexcept; |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2515 | /// void operator delete(void *, std::size_t) noexcept; [C++1y] |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2516 | /// void operator delete(void *, const std::nothrow_t &) noexcept; |
| 2517 | /// void operator delete[](void *) noexcept; |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2518 | /// void operator delete[](void *, std::size_t) noexcept; [C++1y] |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2519 | /// void operator delete[](void *, const std::nothrow_t &) noexcept; |
| 2520 | /// These functions have special behavior under C++1y [expr.new]: |
| 2521 | /// An implementation is allowed to omit a call to a replaceable global |
| 2522 | /// allocation function. [...] |
Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 2523 | /// |
| 2524 | /// If this function is an aligned allocation/deallocation function, return |
Roman Lebedev | 3dd5a29 | 2020-02-26 01:37:17 +0300 | [diff] [blame] | 2525 | /// the parameter number of the requested alignment through AlignmentParam. |
| 2526 | /// |
| 2527 | /// If this function is an allocation/deallocation function that takes |
| 2528 | /// the `std::nothrow_t` tag, return true through IsNothrow, |
| 2529 | bool isReplaceableGlobalAllocationFunction( |
Kazu Hirata | 6ad0788 | 2023-01-14 12:31:01 -0800 | [diff] [blame] | 2530 | std::optional<unsigned> *AlignmentParam = nullptr, |
Roman Lebedev | 3dd5a29 | 2020-02-26 01:37:17 +0300 | [diff] [blame] | 2531 | bool *IsNothrow = nullptr) const; |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2532 | |
serge-sans-paille | d437fba | 2020-01-16 19:54:38 +0100 | [diff] [blame] | 2533 | /// Determine if this function provides an inline implementation of a builtin. |
| 2534 | bool isInlineBuiltinDeclaration() const; |
| 2535 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2536 | /// Determine whether this is a destroying operator delete. |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 2537 | bool isDestroyingOperatorDelete() const; |
| 2538 | |
Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2539 | /// Compute the language linkage. |
| 2540 | LanguageLinkage getLanguageLinkage() const; |
| 2541 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2542 | /// Determines whether this function is a function with |
Rafael Espindola | 5bda63f | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 2543 | /// external, C linkage. |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2544 | bool isExternC() const; |
Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 2545 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2546 | /// Determines whether this function's context is, or is nested within, |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2547 | /// a C++ extern "C" linkage spec. |
| 2548 | bool isInExternCContext() const; |
| 2549 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2550 | /// Determines whether this function's context is, or is nested within, |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2551 | /// a C++ extern "C++" linkage spec. |
| 2552 | bool isInExternCXXContext() const; |
| 2553 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2554 | /// Determines whether this is a global function. |
Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 2555 | bool isGlobal() const; |
| 2556 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2557 | /// Determines whether this function is known to be 'noreturn', through |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 2558 | /// an attribute on its declaration or its type. |
| 2559 | bool isNoReturn() const; |
| 2560 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2561 | /// True if the function was a definition but its body was skipped. |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2562 | bool hasSkippedBody() const { return FunctionDeclBits.HasSkippedBody; } |
| 2563 | void setHasSkippedBody(bool Skipped = true) { |
| 2564 | FunctionDeclBits.HasSkippedBody = Skipped; |
| 2565 | } |
Argyrios Kyrtzidis | 1eb71a1 | 2012-12-06 18:59:10 +0000 | [diff] [blame] | 2566 | |
Justin Lebar | 2b42ccc | 2016-10-28 16:26:26 +0000 | [diff] [blame] | 2567 | /// True if this function will eventually have a body, once it's fully parsed. |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2568 | bool willHaveBody() const { return FunctionDeclBits.WillHaveBody; } |
| 2569 | void setWillHaveBody(bool V = true) { FunctionDeclBits.WillHaveBody = V; } |
Justin Lebar | 2b42ccc | 2016-10-28 16:26:26 +0000 | [diff] [blame] | 2570 | |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 2571 | /// True if this function is considered a multiversioned function. |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2572 | bool isMultiVersion() const { |
| 2573 | return getCanonicalDecl()->FunctionDeclBits.IsMultiVersion; |
| 2574 | } |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 2575 | |
| 2576 | /// Sets the multiversion state for this declaration and all of its |
| 2577 | /// redeclarations. |
| 2578 | void setIsMultiVersion(bool V = true) { |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2579 | getCanonicalDecl()->FunctionDeclBits.IsMultiVersion = V; |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
Erich Keane | babdef2 | 2022-08-19 13:57:45 -0700 | [diff] [blame] | 2582 | // Sets that this is a constrained friend where the constraint refers to an |
| 2583 | // enclosing template. |
| 2584 | void setFriendConstraintRefersToEnclosingTemplate(bool V = true) { |
| 2585 | getCanonicalDecl() |
| 2586 | ->FunctionDeclBits.FriendConstraintRefersToEnclosingTemplate = V; |
| 2587 | } |
| 2588 | // Indicates this function is a constrained friend, where the constraint |
| 2589 | // refers to an enclosing template for hte purposes of [temp.friend]p9. |
| 2590 | bool FriendConstraintRefersToEnclosingTemplate() const { |
| 2591 | return getCanonicalDecl() |
| 2592 | ->FunctionDeclBits.FriendConstraintRefersToEnclosingTemplate; |
| 2593 | } |
| 2594 | |
Richard Smith | a07abe2 | 2023-03-30 16:07:55 -0700 | [diff] [blame] | 2595 | /// Determine whether a function is a friend function that cannot be |
| 2596 | /// redeclared outside of its class, per C++ [temp.friend]p9. |
| 2597 | bool isMemberLikeConstrainedFriend() const; |
| 2598 | |
Erich Keane | 5c0d192 | 2018-11-28 18:34:14 +0000 | [diff] [blame] | 2599 | /// Gets the kind of multiversioning attribute this declaration has. Note that |
| 2600 | /// this can return a value even if the function is not multiversion, such as |
| 2601 | /// the case of 'target'. |
| 2602 | MultiVersionKind getMultiVersionKind() const; |
| 2603 | |
| 2604 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2605 | /// True if this function is a multiversioned dispatch function as a part of |
| 2606 | /// the cpu_specific/cpu_dispatch functionality. |
| 2607 | bool isCPUDispatchMultiVersion() const; |
| 2608 | /// True if this function is a multiversioned processor specific function as a |
| 2609 | /// part of the cpu_specific/cpu_dispatch functionality. |
| 2610 | bool isCPUSpecificMultiVersion() const; |
| 2611 | |
Erich Keane | 19a8adc | 2018-10-25 18:57:19 +0000 | [diff] [blame] | 2612 | /// True if this function is a multiversioned dispatch function as a part of |
| 2613 | /// the target functionality. |
| 2614 | bool isTargetMultiVersion() const; |
| 2615 | |
Jon Roelofs | 2095655 | 2024-02-09 08:14:09 -0800 | [diff] [blame] | 2616 | /// True if this function is the default version of a multiversioned dispatch |
| 2617 | /// function as a part of the target functionality. |
| 2618 | bool isTargetMultiVersionDefault() const; |
| 2619 | |
Erich Keane | fc53eb6 | 2021-11-29 06:28:02 -0800 | [diff] [blame] | 2620 | /// True if this function is a multiversioned dispatch function as a part of |
| 2621 | /// the target-clones functionality. |
| 2622 | bool isTargetClonesMultiVersion() const; |
| 2623 | |
Jon Roelofs | 99d7433 | 2024-02-09 08:13:15 -0800 | [diff] [blame] | 2624 | /// True if this function is a multiversioned dispatch function as a part of |
| 2625 | /// the target-version functionality. |
| 2626 | bool isTargetVersionMultiVersion() const; |
| 2627 | |
Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 2628 | /// \brief Get the associated-constraints of this function declaration. |
| 2629 | /// Currently, this will either be a vector of size 1 containing the |
| 2630 | /// trailing-requires-clause or an empty vector. |
| 2631 | /// |
| 2632 | /// Use this instead of getTrailingRequiresClause for concepts APIs that |
| 2633 | /// accept an ArrayRef of constraint expressions. |
| 2634 | void getAssociatedConstraints(SmallVectorImpl<const Expr *> &AC) const { |
| 2635 | if (auto *TRC = getTrailingRequiresClause()) |
| 2636 | AC.push_back(TRC); |
| 2637 | } |
| 2638 | |
Sirraide | ef164ce | 2024-04-14 12:30:01 +0200 | [diff] [blame] | 2639 | /// Get the message that indicates why this function was deleted. |
| 2640 | StringLiteral *getDeletedMessage() const { |
| 2641 | return FunctionDeclBits.HasDefaultedOrDeletedInfo |
| 2642 | ? DefaultedOrDeletedInfo->getDeletedMessage() |
| 2643 | : nullptr; |
| 2644 | } |
| 2645 | |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 2646 | void setPreviousDeclaration(FunctionDecl * PrevDecl); |
Eli Friedman | aee9e54 | 2008-05-27 05:07:37 +0000 | [diff] [blame] | 2647 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 2648 | FunctionDecl *getCanonicalDecl() override; |
Craig Topper | 3822774 | 2015-05-10 18:40:12 +0000 | [diff] [blame] | 2649 | const FunctionDecl *getCanonicalDecl() const { |
| 2650 | return const_cast<FunctionDecl*>(this)->getCanonicalDecl(); |
| 2651 | } |
Argyrios Kyrtzidis | 02dd4f9 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 2652 | |
Erik Pilkington | b6e16ea | 2019-03-18 19:23:45 +0000 | [diff] [blame] | 2653 | unsigned getBuiltinID(bool ConsiderWrapperFunctions = false) const; |
Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2654 | |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2655 | // ArrayRef interface to parameters. |
| 2656 | ArrayRef<ParmVarDecl *> parameters() const { |
| 2657 | return {ParamInfo, getNumParams()}; |
| 2658 | } |
| 2659 | MutableArrayRef<ParmVarDecl *> parameters() { |
| 2660 | return {ParamInfo, getNumParams()}; |
| 2661 | } |
| 2662 | |
Chris Lattner | 9b65d35 | 2007-12-19 23:58:25 +0000 | [diff] [blame] | 2663 | // Iterator access to formal parameters. |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 2664 | using param_iterator = MutableArrayRef<ParmVarDecl *>::iterator; |
| 2665 | using param_const_iterator = ArrayRef<ParmVarDecl *>::const_iterator; |
| 2666 | |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2667 | bool param_empty() const { return parameters().empty(); } |
| 2668 | param_iterator param_begin() { return parameters().begin(); } |
| 2669 | param_iterator param_end() { return parameters().end(); } |
| 2670 | param_const_iterator param_begin() const { return parameters().begin(); } |
| 2671 | param_const_iterator param_end() const { return parameters().end(); } |
| 2672 | size_t param_size() const { return parameters().size(); } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2673 | |
Erich Keane | 7670b4b | 2017-12-15 16:37:14 +0000 | [diff] [blame] | 2674 | /// Return the number of parameters this function must have based on its |
| 2675 | /// FunctionType. This is the length of the ParamInfo array after it has been |
| 2676 | /// created. |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 2677 | unsigned getNumParams() const; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2678 | |
Chris Lattner | 53621a5 | 2007-06-13 20:44:40 +0000 | [diff] [blame] | 2679 | const ParmVarDecl *getParamDecl(unsigned i) const { |
Chris Lattner | 2e89ebb | 2007-01-21 23:09:17 +0000 | [diff] [blame] | 2680 | assert(i < getNumParams() && "Illegal param #"); |
| 2681 | return ParamInfo[i]; |
| 2682 | } |
Chris Lattner | 53621a5 | 2007-06-13 20:44:40 +0000 | [diff] [blame] | 2683 | ParmVarDecl *getParamDecl(unsigned i) { |
| 2684 | assert(i < getNumParams() && "Illegal param #"); |
| 2685 | return ParamInfo[i]; |
| 2686 | } |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2687 | void setParams(ArrayRef<ParmVarDecl *> NewParamInfo) { |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2688 | setParams(getASTContext(), NewParamInfo); |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2689 | } |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 2690 | |
Erich Keane | 7670b4b | 2017-12-15 16:37:14 +0000 | [diff] [blame] | 2691 | /// Returns the minimum number of arguments needed to call this function. This |
| 2692 | /// may be fewer than the number of function parameters, if some of the |
| 2693 | /// parameters have default arguments (in C++). |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2694 | unsigned getMinRequiredArguments() const; |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 2695 | |
Corentin Jabot | af47517 | 2022-01-27 13:55:08 +0100 | [diff] [blame] | 2696 | /// Returns the minimum number of non-object arguments needed to call this |
| 2697 | /// function. This produces the same value as getMinRequiredArguments except |
| 2698 | /// it does not count the explicit object argument, if any. |
| 2699 | unsigned getMinRequiredExplicitArguments() const; |
| 2700 | |
| 2701 | bool hasCXXExplicitFunctionObjectParameter() const; |
| 2702 | |
| 2703 | unsigned getNumNonObjectParams() const; |
| 2704 | |
| 2705 | const ParmVarDecl *getNonObjectParameter(unsigned I) const { |
| 2706 | return getParamDecl(hasCXXExplicitFunctionObjectParameter() ? I + 1 : I); |
| 2707 | } |
| 2708 | |
| 2709 | ParmVarDecl *getNonObjectParameter(unsigned I) { |
| 2710 | return getParamDecl(hasCXXExplicitFunctionObjectParameter() ? I + 1 : I); |
| 2711 | } |
| 2712 | |
Richard Smith | b5f2c4e | 2020-06-02 10:42:36 -0700 | [diff] [blame] | 2713 | /// Determine whether this function has a single parameter, or multiple |
| 2714 | /// parameters where all but the first have default arguments. |
| 2715 | /// |
| 2716 | /// This notion is used in the definition of copy/move constructors and |
| 2717 | /// initializer list constructors. Note that, unlike getMinRequiredArguments, |
| 2718 | /// parameter packs are not treated specially here. |
| 2719 | bool hasOneParamOrDefaultArgs() const; |
| 2720 | |
Richard Smith | d052a578 | 2019-10-22 17:44:08 -0700 | [diff] [blame] | 2721 | /// Find the source location information for how the type of this function |
| 2722 | /// was written. May be absent (for example if the function was declared via |
| 2723 | /// a typedef) and may contain a different type from that of the function |
| 2724 | /// (for example if the function type was adjusted by an attribute). |
| 2725 | FunctionTypeLoc getFunctionTypeLoc() const; |
| 2726 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2727 | QualType getReturnType() const { |
Richard Smith | 4576a77 | 2018-09-10 06:35:32 +0000 | [diff] [blame] | 2728 | return getType()->castAs<FunctionType>()->getReturnType(); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 2729 | } |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 2730 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2731 | /// Attempt to compute an informative source range covering the |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 2732 | /// function return type. This may omit qualifiers and other information with |
| 2733 | /// limited representation in the AST. |
Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 2734 | SourceRange getReturnTypeSourceRange() const; |
| 2735 | |
Nicolas Manichon | cc3c935 | 2019-12-03 08:21:55 -0500 | [diff] [blame] | 2736 | /// Attempt to compute an informative source range covering the |
| 2737 | /// function parameters, including the ellipsis of a variadic function. |
| 2738 | /// The source range excludes the parentheses, and is invalid if there are |
| 2739 | /// no parameters and no ellipsis. |
| 2740 | SourceRange getParametersSourceRange() const; |
| 2741 | |
Richard Smith | 4576a77 | 2018-09-10 06:35:32 +0000 | [diff] [blame] | 2742 | /// Get the declared return type, which may differ from the actual return |
| 2743 | /// type if the return type is deduced. |
| 2744 | QualType getDeclaredReturnType() const { |
| 2745 | auto *TSI = getTypeSourceInfo(); |
| 2746 | QualType T = TSI ? TSI->getType() : getType(); |
| 2747 | return T->castAs<FunctionType>()->getReturnType(); |
| 2748 | } |
| 2749 | |
Erich Keane | d02f4a1 | 2019-05-30 17:31:54 +0000 | [diff] [blame] | 2750 | /// Gets the ExceptionSpecificationType as declared. |
| 2751 | ExceptionSpecificationType getExceptionSpecType() const { |
| 2752 | auto *TSI = getTypeSourceInfo(); |
| 2753 | QualType T = TSI ? TSI->getType() : getType(); |
| 2754 | const auto *FPT = T->getAs<FunctionProtoType>(); |
| 2755 | return FPT ? FPT->getExceptionSpecType() : EST_None; |
| 2756 | } |
| 2757 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2758 | /// Attempt to compute an informative source range covering the |
Malcolm Parsons | a3220ce | 2017-01-12 16:11:28 +0000 | [diff] [blame] | 2759 | /// function exception specification, if any. |
| 2760 | SourceRange getExceptionSpecSourceRange() const; |
| 2761 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2762 | /// Determine the type of an expression that calls this function. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 2763 | QualType getCallResultType() const { |
Richard Smith | 4576a77 | 2018-09-10 06:35:32 +0000 | [diff] [blame] | 2764 | return getType()->castAs<FunctionType>()->getCallResultType( |
| 2765 | getASTContext()); |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 2766 | } |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 2767 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2768 | /// Returns the storage class as written in the source. For the |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2769 | /// computed linkage of symbol, see getLinkage. |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2770 | StorageClass getStorageClass() const { |
| 2771 | return static_cast<StorageClass>(FunctionDeclBits.SClass); |
| 2772 | } |
| 2773 | |
| 2774 | /// Sets the storage class as written in the source. |
| 2775 | void setStorageClass(StorageClass SClass) { |
Thorsten Schütt | 2fd11e0 | 2021-01-04 23:17:45 +0100 | [diff] [blame] | 2776 | FunctionDeclBits.SClass = SClass; |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2777 | } |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2778 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2779 | /// Determine whether the "inline" keyword was specified for this |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 2780 | /// function. |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2781 | bool isInlineSpecified() const { return FunctionDeclBits.IsInlineSpecified; } |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 2782 | |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 2783 | /// Set whether the "inline" keyword was specified for this function. |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 2784 | void setInlineSpecified(bool I) { |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2785 | FunctionDeclBits.IsInlineSpecified = I; |
| 2786 | FunctionDeclBits.IsInline = I; |
Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2787 | } |
Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2788 | |
Melanie Blower | bc5b5ea | 2021-07-29 12:02:20 -0400 | [diff] [blame] | 2789 | /// Determine whether the function was declared in source context |
| 2790 | /// that requires constrained FP intrinsics |
| 2791 | bool UsesFPIntrin() const { return FunctionDeclBits.UsesFPIntrin; } |
| 2792 | |
| 2793 | /// Set whether the function was declared in source context |
| 2794 | /// that requires constrained FP intrinsics |
| 2795 | void setUsesFPIntrin(bool I) { FunctionDeclBits.UsesFPIntrin = I; } |
| 2796 | |
John McCall | 357d0f3 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 2797 | /// Flag that this function is implicitly inline. |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2798 | void setImplicitlyInline(bool I = true) { FunctionDeclBits.IsInline = I; } |
John McCall | 357d0f3 | 2010-12-15 04:00:32 +0000 | [diff] [blame] | 2799 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2800 | /// Determine whether this function should be inlined, because it is |
Richard Smith | 7cd25b2 | 2011-09-30 00:45:47 +0000 | [diff] [blame] | 2801 | /// either marked "inline" or "constexpr" or is a member function of a class |
| 2802 | /// that was defined in the class body. |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2803 | bool isInlined() const { return FunctionDeclBits.IsInline; } |
| 2804 | |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2805 | bool isInlineDefinitionExternallyVisible() const; |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2806 | |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 2807 | bool isMSExternInline() const; |
| 2808 | |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2809 | bool doesDeclarationForceExternallyVisibleDefinition() const; |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 2810 | |
Thorsten Schütt | 2fd11e0 | 2021-01-04 23:17:45 +0100 | [diff] [blame] | 2811 | bool isStatic() const { return getStorageClass() == SC_Static; } |
Djordje Todorovic | 0f65168 | 2019-06-27 06:44:44 +0000 | [diff] [blame] | 2812 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 2813 | /// Whether this function declaration represents an C++ overloaded |
| 2814 | /// operator, e.g., "operator+". |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2815 | bool isOverloadedOperator() const { |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2816 | return getOverloadedOperator() != OO_None; |
Daniel Dunbar | e017ecc | 2009-12-19 17:50:07 +0000 | [diff] [blame] | 2817 | } |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2818 | |
| 2819 | OverloadedOperatorKind getOverloadedOperator() const; |
| 2820 | |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 2821 | const IdentifierInfo *getLiteralIdentifier() const; |
| 2822 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2823 | /// If this function is an instantiation of a member function |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2824 | /// of a class template specialization, retrieves the function from |
| 2825 | /// which it was instantiated. |
| 2826 | /// |
| 2827 | /// This routine will return non-NULL for (non-templated) member |
| 2828 | /// functions of class templates and for instantiations of function |
| 2829 | /// templates. For example, given: |
| 2830 | /// |
| 2831 | /// \code |
| 2832 | /// template<typename T> |
| 2833 | /// struct X { |
| 2834 | /// void f(T); |
| 2835 | /// }; |
| 2836 | /// \endcode |
| 2837 | /// |
| 2838 | /// The declaration for X<int>::f is a (non-templated) FunctionDecl |
| 2839 | /// whose parent is the class template specialization X<int>. For |
| 2840 | /// this declaration, getInstantiatedFromFunction() will return |
| 2841 | /// the FunctionDecl X<T>::A. When a complete definition of |
| 2842 | /// X<int>::A is required, it will be instantiated from the |
| 2843 | /// declaration returned by getInstantiatedFromMemberFunction(). |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2844 | FunctionDecl *getInstantiatedFromMemberFunction() const; |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 2845 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2846 | /// What kind of templated function this is. |
Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 2847 | TemplatedKind getTemplatedKind() const; |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2848 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2849 | /// If this function is an instantiation of a member function of a |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2850 | /// class template specialization, retrieves the member specialization |
| 2851 | /// information. |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 2852 | MemberSpecializationInfo *getMemberSpecializationInfo() const; |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 2853 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2854 | /// Specify that this record is an instantiation of the |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2855 | /// member function FD. |
| 2856 | void setInstantiationOfMemberFunction(FunctionDecl *FD, |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2857 | TemplateSpecializationKind TSK) { |
| 2858 | setInstantiationOfMemberFunction(getASTContext(), FD, TSK); |
| 2859 | } |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2860 | |
Erich Keane | 3ff86f9 | 2022-07-22 12:30:47 -0700 | [diff] [blame] | 2861 | /// Specify that this function declaration was instantiated from a |
| 2862 | /// FunctionDecl FD. This is only used if this is a function declaration |
| 2863 | /// declared locally inside of a function template. |
| 2864 | void setInstantiatedFromDecl(FunctionDecl *FD); |
| 2865 | |
| 2866 | FunctionDecl *getInstantiatedFromDecl() const; |
| 2867 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2868 | /// Retrieves the function template that is described by this |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2869 | /// function declaration. |
| 2870 | /// |
| 2871 | /// Every function template is represented as a FunctionTemplateDecl |
| 2872 | /// and a FunctionDecl (or something derived from FunctionDecl). The |
| 2873 | /// former contains template properties (such as the template |
| 2874 | /// parameter lists) while the latter contains the actual |
| 2875 | /// description of the template's |
| 2876 | /// contents. FunctionTemplateDecl::getTemplatedDecl() retrieves the |
| 2877 | /// FunctionDecl that describes the function template, |
| 2878 | /// getDescribedFunctionTemplate() retrieves the |
| 2879 | /// FunctionTemplateDecl from a FunctionDecl. |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 2880 | FunctionTemplateDecl *getDescribedFunctionTemplate() const; |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2881 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 2882 | void setDescribedFunctionTemplate(FunctionTemplateDecl *Template); |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2883 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2884 | /// Determine whether this function is a function template |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 2885 | /// specialization. |
Krystian Stasiowski | 3a3b84b | 2023-10-07 02:55:31 -0400 | [diff] [blame] | 2886 | bool isFunctionTemplateSpecialization() const; |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2887 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2888 | /// If this function is actually a function template specialization, |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 2889 | /// retrieve information about this function template specialization. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2890 | /// Otherwise, returns NULL. |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 2891 | FunctionTemplateSpecializationInfo *getTemplateSpecializationInfo() const; |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2892 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2893 | /// Determines whether this function is a function template |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2894 | /// specialization or a member of a class template specialization that can |
| 2895 | /// be implicitly instantiated. |
| 2896 | bool isImplicitlyInstantiable() const; |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 2897 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2898 | /// Determines if the given function was instantiated from a |
Ted Kremenek | 85825ae | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 2899 | /// function template. |
| 2900 | bool isTemplateInstantiation() const; |
| 2901 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2902 | /// Retrieve the function declaration from which this function could |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2903 | /// be instantiated, if it is an instantiation (rather than a non-template |
| 2904 | /// or a specialization, for example). |
Richard Smith | a5569f0 | 2020-07-09 14:57:30 -0700 | [diff] [blame] | 2905 | /// |
| 2906 | /// If \p ForDefinition is \c false, explicit specializations will be treated |
| 2907 | /// as if they were implicit instantiations. This will then find the pattern |
| 2908 | /// corresponding to non-definition portions of the declaration, such as |
| 2909 | /// default arguments and the exception specification. |
| 2910 | FunctionDecl * |
| 2911 | getTemplateInstantiationPattern(bool ForDefinition = true) const; |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2912 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2913 | /// Retrieve the primary template that this function template |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 2914 | /// specialization either specializes or was instantiated from. |
| 2915 | /// |
| 2916 | /// If this function declaration is not a function template specialization, |
| 2917 | /// returns NULL. |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2918 | FunctionTemplateDecl *getPrimaryTemplate() const; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2919 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2920 | /// Retrieve the template arguments used to produce this function |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 2921 | /// template specialization from the primary template. |
| 2922 | /// |
| 2923 | /// If this function declaration is not a function template specialization, |
| 2924 | /// returns NULL. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2925 | const TemplateArgumentList *getTemplateSpecializationArgs() const; |
| 2926 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2927 | /// Retrieve the template argument list as written in the sources, |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 2928 | /// if any. |
| 2929 | /// |
| 2930 | /// If this function declaration is not a function template specialization |
| 2931 | /// or if it had no explicit template argument list, returns NULL. |
| 2932 | /// Note that it an explicit template argument list may be written empty, |
| 2933 | /// e.g., template<> void foo<>(char* s); |
Argyrios Kyrtzidis | e9a2443 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 2934 | const ASTTemplateArgumentListInfo* |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 2935 | getTemplateSpecializationArgsAsWritten() const; |
| 2936 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2937 | /// Specify that this function declaration is actually a function |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 2938 | /// template specialization. |
| 2939 | /// |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 2940 | /// \param Template the function template that this function template |
| 2941 | /// specialization specializes. |
| 2942 | /// |
| 2943 | /// \param TemplateArgs the template arguments that produced this |
| 2944 | /// function template specialization from the template. |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 2945 | /// |
| 2946 | /// \param InsertPos If non-NULL, the position in the function template |
| 2947 | /// specialization set where the function template specialization data will |
| 2948 | /// be inserted. |
| 2949 | /// |
| 2950 | /// \param TSK the kind of template specialization this is. |
Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 2951 | /// |
| 2952 | /// \param TemplateArgsAsWritten location info of template arguments. |
Argyrios Kyrtzidis | 927d8e0 | 2010-07-05 10:37:55 +0000 | [diff] [blame] | 2953 | /// |
| 2954 | /// \param PointOfInstantiation point at which the function template |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 2955 | /// specialization was first instantiated. |
Vlad Serebrennikov | 6677aef | 2024-05-17 17:49:50 +0400 | [diff] [blame] | 2956 | void setFunctionTemplateSpecialization( |
| 2957 | FunctionTemplateDecl *Template, TemplateArgumentList *TemplateArgs, |
| 2958 | void *InsertPos, |
| 2959 | TemplateSpecializationKind TSK = TSK_ImplicitInstantiation, |
| 2960 | TemplateArgumentListInfo *TemplateArgsAsWritten = nullptr, |
| 2961 | SourceLocation PointOfInstantiation = SourceLocation()) { |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2962 | setFunctionTemplateSpecialization(getASTContext(), Template, TemplateArgs, |
| 2963 | InsertPos, TSK, TemplateArgsAsWritten, |
| 2964 | PointOfInstantiation); |
| 2965 | } |
Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 2966 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2967 | /// Specifies that this function declaration is actually a |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 2968 | /// dependent function template specialization. |
Krystian Stasiowski | 3a3b84b | 2023-10-07 02:55:31 -0400 | [diff] [blame] | 2969 | void setDependentTemplateSpecialization( |
| 2970 | ASTContext &Context, const UnresolvedSetImpl &Templates, |
| 2971 | const TemplateArgumentListInfo *TemplateArgs); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 2972 | |
| 2973 | DependentFunctionTemplateSpecializationInfo * |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 2974 | getDependentSpecializationInfo() const; |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 2975 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2976 | /// Determine what kind of template instantiation this function |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2977 | /// represents. |
| 2978 | TemplateSpecializationKind getTemplateSpecializationKind() const; |
Douglas Gregor | e8925db | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 2979 | |
Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 2980 | /// Determine the kind of template specialization this function represents |
| 2981 | /// for the purpose of template instantiation. |
| 2982 | TemplateSpecializationKind |
| 2983 | getTemplateSpecializationKindForInstantiation() const; |
| 2984 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2985 | /// Determine what kind of template instantiation this function |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2986 | /// represents. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2987 | void setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 2988 | SourceLocation PointOfInstantiation = SourceLocation()); |
| 2989 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2990 | /// Retrieve the (first) point of instantiation of a function template |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2991 | /// specialization or a member of a class template specialization. |
| 2992 | /// |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 2993 | /// \returns the first point of instantiation, if this function was |
| 2994 | /// instantiated from a template; otherwise, returns an invalid source |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2995 | /// location. |
| 2996 | SourceLocation getPointOfInstantiation() const; |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 2997 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2998 | /// Determine whether this is or was instantiated from an out-of-line |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 2999 | /// definition of a member function. |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 3000 | bool isOutOfLine() const override; |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 3001 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3002 | /// Identify a memory copying or setting function. |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3003 | /// If the given function is a memory copy or setting function, returns |
| 3004 | /// the corresponding Builtin ID. If the function is not a memory function, |
| 3005 | /// returns 0. |
Anna Zaks | 28db7ce | 2012-01-18 02:45:01 +0000 | [diff] [blame] | 3006 | unsigned getMemoryFunctionKind() const; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3007 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3008 | /// Returns ODRHash of the function. This value is calculated and |
Richard Trieu | e6caa26 | 2017-12-23 00:41:01 +0000 | [diff] [blame] | 3009 | /// stored on first call, then the stored value returned on the other calls. |
| 3010 | unsigned getODRHash(); |
| 3011 | |
Richard Trieu | 27c1b1a | 2018-07-10 01:40:50 +0000 | [diff] [blame] | 3012 | /// Returns cached ODRHash of the function. This must have been previously |
| 3013 | /// computed and stored. |
| 3014 | unsigned getODRHash() const; |
| 3015 | |
Doug Wyatt | f03cb00 | 2024-06-24 00:51:31 -1000 | [diff] [blame] | 3016 | FunctionEffectsRef getFunctionEffects() const { |
| 3017 | // Effects may differ between declarations, but they should be propagated |
| 3018 | // from old to new on any redeclaration, so it suffices to look at |
| 3019 | // getMostRecentDecl(). |
| 3020 | if (const auto *FPT = |
| 3021 | getMostRecentDecl()->getType()->getAs<FunctionProtoType>()) |
| 3022 | return FPT->getFunctionEffects(); |
| 3023 | return {}; |
| 3024 | } |
| 3025 | |
Chris Lattner | da8aa7b | 2006-11-19 23:12:30 +0000 | [diff] [blame] | 3026 | // Implement isa/cast/dyncast/etc. |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 3027 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 3028 | static bool classofKind(Kind K) { |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 3029 | return K >= firstFunction && K <= lastFunction; |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 3030 | } |
Argyrios Kyrtzidis | 3768ad6 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 3031 | static DeclContext *castToDeclContext(const FunctionDecl *D) { |
| 3032 | return static_cast<DeclContext *>(const_cast<FunctionDecl*>(D)); |
| 3033 | } |
| 3034 | static FunctionDecl *castFromDeclContext(const DeclContext *DC) { |
| 3035 | return static_cast<FunctionDecl *>(const_cast<DeclContext*>(DC)); |
| 3036 | } |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 3037 | }; |
| 3038 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 3039 | /// Represents a member of a struct/union/class. |
Richard Smith | 0b87e07 | 2013-10-07 08:02:11 +0000 | [diff] [blame] | 3040 | class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> { |
Richard Smith | 3242934 | 2023-04-05 14:28:57 -0700 | [diff] [blame] | 3041 | /// The kinds of value we can store in StorageKind. |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3042 | /// |
John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 3043 | /// Note that this is compatible with InClassInitStyle except for |
| 3044 | /// ISK_CapturedVLAType. |
| 3045 | enum InitStorageKind { |
| 3046 | /// If the pointer is null, there's nothing special. Otherwise, |
| 3047 | /// this is a bitfield and the pointer is the Expr* storing the |
| 3048 | /// bit-width. |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3049 | ISK_NoInit = (unsigned) ICIS_NoInit, |
John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 3050 | |
| 3051 | /// The pointer is an (optional due to delayed parsing) Expr* |
| 3052 | /// holding the copy-initializer. |
| 3053 | ISK_InClassCopyInit = (unsigned) ICIS_CopyInit, |
| 3054 | |
| 3055 | /// The pointer is an (optional due to delayed parsing) Expr* |
| 3056 | /// holding the list-initializer. |
| 3057 | ISK_InClassListInit = (unsigned) ICIS_ListInit, |
| 3058 | |
| 3059 | /// The pointer is a VariableArrayType* that's been captured; |
| 3060 | /// the enclosing context is a lambda or captured statement. |
| 3061 | ISK_CapturedVLAType, |
| 3062 | }; |
| 3063 | |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 3064 | LLVM_PREFERRED_TYPE(bool) |
Richard Smith | 3242934 | 2023-04-05 14:28:57 -0700 | [diff] [blame] | 3065 | unsigned BitField : 1; |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 3066 | LLVM_PREFERRED_TYPE(bool) |
Richard Smith | 3242934 | 2023-04-05 14:28:57 -0700 | [diff] [blame] | 3067 | unsigned Mutable : 1; |
Vlad Serebrennikov | ad27848 | 2023-11-06 12:17:21 +0300 | [diff] [blame] | 3068 | LLVM_PREFERRED_TYPE(InitStorageKind) |
Richard Smith | 3242934 | 2023-04-05 14:28:57 -0700 | [diff] [blame] | 3069 | unsigned StorageKind : 2; |
| 3070 | mutable unsigned CachedFieldIndex : 28; |
| 3071 | |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3072 | /// If this is a bitfield with a default member initializer, this |
| 3073 | /// structure is used to represent the two expressions. |
Richard Smith | 3242934 | 2023-04-05 14:28:57 -0700 | [diff] [blame] | 3074 | struct InitAndBitWidthStorage { |
| 3075 | LazyDeclStmtPtr Init; |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3076 | Expr *BitWidth; |
| 3077 | }; |
| 3078 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3079 | /// Storage for either the bit-width, the in-class initializer, or |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3080 | /// both (via InitAndBitWidth), or the captured variable length array bound. |
John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 3081 | /// |
| 3082 | /// If the storage kind is ISK_InClassCopyInit or |
| 3083 | /// ISK_InClassListInit, but the initializer is null, then this |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3084 | /// field has an in-class initializer that has not yet been parsed |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3085 | /// and attached. |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3086 | // FIXME: Tail-allocate this to reduce the size of FieldDecl in the |
| 3087 | // overwhelmingly common case that we have none of these things. |
Richard Smith | 3242934 | 2023-04-05 14:28:57 -0700 | [diff] [blame] | 3088 | union { |
| 3089 | // Active member if ISK is not ISK_CapturedVLAType and BitField is false. |
| 3090 | LazyDeclStmtPtr Init; |
| 3091 | // Active member if ISK is ISK_NoInit and BitField is true. |
| 3092 | Expr *BitWidth; |
| 3093 | // Active member if ISK is ISK_InClass*Init and BitField is true. |
| 3094 | InitAndBitWidthStorage *InitAndBitWidth; |
| 3095 | // Active member if ISK is ISK_CapturedVLAType. |
| 3096 | const VariableArrayType *CapturedVLAType; |
| 3097 | }; |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3098 | |
Chris Lattner | d3f989c | 2008-03-15 23:04:32 +0000 | [diff] [blame] | 3099 | protected: |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3100 | FieldDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc, |
Bill Wendling | fca5191 | 2024-04-11 00:33:40 +0000 | [diff] [blame] | 3101 | SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, |
Richard Smith | 3242934 | 2023-04-05 14:28:57 -0700 | [diff] [blame] | 3102 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3103 | InClassInitStyle InitStyle) |
Richard Smith | 3242934 | 2023-04-05 14:28:57 -0700 | [diff] [blame] | 3104 | : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), BitField(false), |
| 3105 | Mutable(Mutable), StorageKind((InitStorageKind)InitStyle), |
| 3106 | CachedFieldIndex(0), Init() { |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3107 | if (BW) |
| 3108 | setBitWidth(BW); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3109 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3110 | |
Chris Lattner | ee1284a | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 3111 | public: |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3112 | friend class ASTDeclReader; |
| 3113 | friend class ASTDeclWriter; |
| 3114 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3115 | static FieldDecl *Create(const ASTContext &C, DeclContext *DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3116 | SourceLocation StartLoc, SourceLocation IdLoc, |
Bill Wendling | fca5191 | 2024-04-11 00:33:40 +0000 | [diff] [blame] | 3117 | const IdentifierInfo *Id, QualType T, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3118 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3119 | InClassInitStyle InitStyle); |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 3120 | |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 3121 | static FieldDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3122 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 3123 | /// Returns the index of this field within its record, |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3124 | /// as appropriate for passing to ASTRecordLayout::getFieldOffset. |
Reid Kleckner | 26aa20a | 2025-01-09 11:21:03 -0800 | [diff] [blame] | 3125 | unsigned getFieldIndex() const { |
| 3126 | const FieldDecl *Canonical = getCanonicalDecl(); |
| 3127 | if (Canonical->CachedFieldIndex == 0) { |
| 3128 | Canonical->setCachedFieldIndex(); |
| 3129 | assert(Canonical->CachedFieldIndex != 0); |
| 3130 | } |
| 3131 | return Canonical->CachedFieldIndex - 1; |
| 3132 | } |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3133 | |
Reid Kleckner | 26aa20a | 2025-01-09 11:21:03 -0800 | [diff] [blame] | 3134 | private: |
| 3135 | /// Set CachedFieldIndex to the index of this field plus one. |
| 3136 | void setCachedFieldIndex() const; |
| 3137 | |
| 3138 | public: |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 3139 | /// Determines whether this field is mutable (C++ only). |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3140 | bool isMutable() const { return Mutable; } |
| 3141 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 3142 | /// Determines whether this field is a bitfield. |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3143 | bool isBitField() const { return BitField; } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3144 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 3145 | /// Determines whether this is an unnamed bitfield. |
Timm Baeder | 3d56ea0 | 2024-04-18 07:39:29 +0200 | [diff] [blame] | 3146 | bool isUnnamedBitField() const { return isBitField() && !getDeclName(); } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 3147 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 3148 | /// Determines whether this field is a |
Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3149 | /// representative for an anonymous struct or union. Such fields are |
| 3150 | /// unnamed and are implicitly generated by the implementation to |
| 3151 | /// store the data for the anonymous union or struct. |
| 3152 | bool isAnonymousStructOrUnion() const; |
| 3153 | |
Timm Bäder | 05a11f3 | 2023-09-13 10:08:32 +0200 | [diff] [blame] | 3154 | /// Returns the expression that represents the bit width, if this field |
| 3155 | /// is a bit field. For non-bitfields, this returns \c nullptr. |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3156 | Expr *getBitWidth() const { |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3157 | if (!BitField) |
| 3158 | return nullptr; |
Richard Smith | 3242934 | 2023-04-05 14:28:57 -0700 | [diff] [blame] | 3159 | return hasInClassInitializer() ? InitAndBitWidth->BitWidth : BitWidth; |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3160 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3161 | |
Timm Bäder | 05a11f3 | 2023-09-13 10:08:32 +0200 | [diff] [blame] | 3162 | /// Computes the bit width of this field, if this is a bit field. |
| 3163 | /// May not be called on non-bitfields. |
Timm Baeder | cfe2635 | 2025-01-11 07:12:37 +0100 | [diff] [blame] | 3164 | /// Note that in order to successfully use this function, the bitwidth |
| 3165 | /// expression must be a ConstantExpr with a valid integer result set. |
| 3166 | unsigned getBitWidthValue() const; |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3167 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 3168 | /// Set the bit-field width for this member. |
Abramo Bagnara | b1cdde7 | 2012-07-02 20:35:48 +0000 | [diff] [blame] | 3169 | // Note: used by some clients (i.e., do not remove it). |
John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 3170 | void setBitWidth(Expr *Width) { |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3171 | assert(!hasCapturedVLAType() && !BitField && |
| 3172 | "bit width or captured type already set"); |
| 3173 | assert(Width && "no bit width specified"); |
Richard Smith | 3242934 | 2023-04-05 14:28:57 -0700 | [diff] [blame] | 3174 | if (hasInClassInitializer()) |
| 3175 | InitAndBitWidth = |
| 3176 | new (getASTContext()) InitAndBitWidthStorage{Init, Width}; |
| 3177 | else |
| 3178 | BitWidth = Width; |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3179 | BitField = true; |
John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 3182 | /// Remove the bit-field width from this member. |
Abramo Bagnara | b1cdde7 | 2012-07-02 20:35:48 +0000 | [diff] [blame] | 3183 | // Note: used by some clients (i.e., do not remove it). |
| 3184 | void removeBitWidth() { |
| 3185 | assert(isBitField() && "no bitfield width to remove"); |
Richard Smith | 3242934 | 2023-04-05 14:28:57 -0700 | [diff] [blame] | 3186 | if (hasInClassInitializer()) { |
| 3187 | // Read the old initializer before we change the active union member. |
| 3188 | auto ExistingInit = InitAndBitWidth->Init; |
| 3189 | Init = ExistingInit; |
| 3190 | } |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3191 | BitField = false; |
Abramo Bagnara | b1cdde7 | 2012-07-02 20:35:48 +0000 | [diff] [blame] | 3192 | } |
| 3193 | |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 3194 | /// Is this a zero-length bit-field? Such bit-fields aren't really bit-fields |
| 3195 | /// at all and instead act as a separator between contiguous runs of other |
| 3196 | /// bit-fields. |
Timm Baeder | cfe2635 | 2025-01-11 07:12:37 +0100 | [diff] [blame] | 3197 | bool isZeroLengthBitField() const; |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 3198 | |
Richard Smith | 78b239e | 2019-06-20 20:44:45 +0000 | [diff] [blame] | 3199 | /// Determine if this field is a subobject of zero size, that is, either a |
| 3200 | /// zero-length bit-field or a field of empty class type with the |
| 3201 | /// [[no_unique_address]] attribute. |
| 3202 | bool isZeroSize(const ASTContext &Ctx) const; |
| 3203 | |
Vladislav Dzhidzhoev | af6c0b6d | 2023-01-14 02:38:10 +0300 | [diff] [blame] | 3204 | /// Determine if this field is of potentially-overlapping class type, that |
| 3205 | /// is, subobject with the [[no_unique_address]] attribute |
| 3206 | bool isPotentiallyOverlapping() const; |
| 3207 | |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3208 | /// Get the kind of (C++11) default member initializer that this field has. |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3209 | InClassInitStyle getInClassInitStyle() const { |
Richard Smith | 3242934 | 2023-04-05 14:28:57 -0700 | [diff] [blame] | 3210 | return (StorageKind == ISK_CapturedVLAType ? ICIS_NoInit |
| 3211 | : (InClassInitStyle)StorageKind); |
Abramo Bagnara | 612f6dc | 2011-07-11 08:43:20 +0000 | [diff] [blame] | 3212 | } |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3213 | |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3214 | /// Determine whether this member has a C++11 default member initializer. |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3215 | bool hasInClassInitializer() const { |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3216 | return getInClassInitStyle() != ICIS_NoInit; |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3217 | } |
John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 3218 | |
Richard Smith | 3242934 | 2023-04-05 14:28:57 -0700 | [diff] [blame] | 3219 | /// Determine whether getInClassInitializer() would return a non-null pointer |
| 3220 | /// without deserializing the initializer. |
| 3221 | bool hasNonNullInClassInitializer() const { |
| 3222 | return hasInClassInitializer() && (BitField ? InitAndBitWidth->Init : Init); |
| 3223 | } |
| 3224 | |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3225 | /// Get the C++11 default member initializer for this member, or null if one |
| 3226 | /// has not been set. If a valid declaration has a default member initializer, |
| 3227 | /// but this returns null, then we have not parsed and attached it yet. |
Richard Smith | 3242934 | 2023-04-05 14:28:57 -0700 | [diff] [blame] | 3228 | Expr *getInClassInitializer() const; |
John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 3229 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 3230 | /// Set the C++11 in-class initializer for this member. |
Richard Smith | 3242934 | 2023-04-05 14:28:57 -0700 | [diff] [blame] | 3231 | void setInClassInitializer(Expr *NewInit); |
John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 3232 | |
Bill Wendling | 94b8b11 | 2024-08-15 05:27:19 +0000 | [diff] [blame] | 3233 | /// Find the FieldDecl specified in a FAM's "counted_by" attribute. Returns |
| 3234 | /// \p nullptr if either the attribute or the field doesn't exist. |
| 3235 | const FieldDecl *findCountedByField() const; |
| 3236 | |
Richard Smith | 3242934 | 2023-04-05 14:28:57 -0700 | [diff] [blame] | 3237 | private: |
| 3238 | void setLazyInClassInitializer(LazyDeclStmtPtr NewInit); |
| 3239 | |
| 3240 | public: |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 3241 | /// Remove the C++11 in-class initializer from this member. |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3242 | void removeInClassInitializer() { |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3243 | assert(hasInClassInitializer() && "no initializer to remove"); |
Richard Smith | 3242934 | 2023-04-05 14:28:57 -0700 | [diff] [blame] | 3244 | StorageKind = ISK_NoInit; |
| 3245 | if (BitField) { |
| 3246 | // Read the bit width before we change the active union member. |
| 3247 | Expr *ExistingBitWidth = InitAndBitWidth->BitWidth; |
| 3248 | BitWidth = ExistingBitWidth; |
| 3249 | } |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3250 | } |
Douglas Gregor | feb84b0 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 3251 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3252 | /// Determine whether this member captures the variable length array |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 3253 | /// type. |
John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 3254 | bool hasCapturedVLAType() const { |
Richard Smith | 3242934 | 2023-04-05 14:28:57 -0700 | [diff] [blame] | 3255 | return StorageKind == ISK_CapturedVLAType; |
John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3258 | /// Get the captured variable length array type. |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 3259 | const VariableArrayType *getCapturedVLAType() const { |
Richard Smith | 3242934 | 2023-04-05 14:28:57 -0700 | [diff] [blame] | 3260 | return hasCapturedVLAType() ? CapturedVLAType : nullptr; |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 3261 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3262 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3263 | /// Set the captured variable length array type for this field. |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 3264 | void setCapturedVLAType(const VariableArrayType *VLAType); |
| 3265 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 3266 | /// Returns the parent of this field declaration, which |
Malcolm Parsons | 2eac1d8 | 2017-05-08 16:43:29 +0000 | [diff] [blame] | 3267 | /// is the struct in which this field is defined. |
Haojian Wu | fcf0764 | 2020-05-19 15:26:42 +0200 | [diff] [blame] | 3268 | /// |
| 3269 | /// Returns null if this is not a normal class/struct field declaration, e.g. |
| 3270 | /// ObjCAtDefsFieldDecl, ObjCIvarDecl. |
Anders Carlsson | 5d8645b | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 3271 | const RecordDecl *getParent() const { |
Haojian Wu | fcf0764 | 2020-05-19 15:26:42 +0200 | [diff] [blame] | 3272 | return dyn_cast<RecordDecl>(getDeclContext()); |
Anders Carlsson | 5d8645b | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | RecordDecl *getParent() { |
Haojian Wu | fcf0764 | 2020-05-19 15:26:42 +0200 | [diff] [blame] | 3276 | return dyn_cast<RecordDecl>(getDeclContext()); |
Anders Carlsson | 5d8645b | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 3277 | } |
Abramo Bagnara | 20c9e24 | 2011-03-08 11:07:11 +0000 | [diff] [blame] | 3278 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 3279 | SourceRange getSourceRange() const override LLVM_READONLY; |
Abramo Bagnara | 20c9e24 | 2011-03-08 11:07:11 +0000 | [diff] [blame] | 3280 | |
Richard Smith | 0b87e07 | 2013-10-07 08:02:11 +0000 | [diff] [blame] | 3281 | /// Retrieves the canonical declaration of this field. |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 3282 | FieldDecl *getCanonicalDecl() override { return getFirstDecl(); } |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 3283 | const FieldDecl *getCanonicalDecl() const { return getFirstDecl(); } |
Richard Smith | 0b87e07 | 2013-10-07 08:02:11 +0000 | [diff] [blame] | 3284 | |
Chris Lattner | ce3a702 | 2007-01-24 02:10:37 +0000 | [diff] [blame] | 3285 | // Implement isa/cast/dyncast/etc. |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 3286 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 3287 | static bool classofKind(Kind K) { return K >= firstField && K <= lastField; } |
Podchishchaeva, Mariya | f4c886b | 2023-08-24 06:27:02 -0700 | [diff] [blame] | 3288 | |
| 3289 | void printName(raw_ostream &OS, const PrintingPolicy &Policy) const override; |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 3290 | }; |
| 3291 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 3292 | /// An instance of this object exists for each enum constant |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3293 | /// that is defined. For example, in "enum X {a,b}", each of a/b are |
| 3294 | /// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 3295 | /// TagType for the X EnumDecl. |
Craig Topper | 142f270 | 2024-01-16 12:49:11 -0800 | [diff] [blame] | 3296 | class EnumConstantDecl : public ValueDecl, |
| 3297 | public Mergeable<EnumConstantDecl>, |
| 3298 | public APIntStorage { |
Ted Kremenek | 8595668 | 2008-05-30 16:14:41 +0000 | [diff] [blame] | 3299 | Stmt *Init; // an integer constant expression |
Craig Topper | 142f270 | 2024-01-16 12:49:11 -0800 | [diff] [blame] | 3300 | bool IsUnsigned; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3301 | |
Chris Lattner | a7b3287 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 3302 | protected: |
Craig Topper | 142f270 | 2024-01-16 12:49:11 -0800 | [diff] [blame] | 3303 | EnumConstantDecl(const ASTContext &C, DeclContext *DC, SourceLocation L, |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 3304 | IdentifierInfo *Id, QualType T, Expr *E, |
Craig Topper | 142f270 | 2024-01-16 12:49:11 -0800 | [diff] [blame] | 3305 | const llvm::APSInt &V); |
Ted Kremenek | ce20e8f | 2008-05-20 00:43:19 +0000 | [diff] [blame] | 3306 | |
Chris Lattner | a7b3287 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 3307 | public: |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3308 | friend class StmtIteratorBase; |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 3309 | |
Chris Lattner | bec4134 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 3310 | static EnumConstantDecl *Create(ASTContext &C, EnumDecl *DC, |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 3311 | SourceLocation L, IdentifierInfo *Id, |
| 3312 | QualType T, Expr *E, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3313 | const llvm::APSInt &V); |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 3314 | static EnumConstantDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3315 | |
Ted Kremenek | 8595668 | 2008-05-30 16:14:41 +0000 | [diff] [blame] | 3316 | const Expr *getInitExpr() const { return (const Expr*) Init; } |
| 3317 | Expr *getInitExpr() { return (Expr*) Init; } |
Craig Topper | 142f270 | 2024-01-16 12:49:11 -0800 | [diff] [blame] | 3318 | llvm::APSInt getInitVal() const { |
| 3319 | return llvm::APSInt(getValue(), IsUnsigned); |
| 3320 | } |
Chris Lattner | 959bcdc | 2007-08-29 17:23:37 +0000 | [diff] [blame] | 3321 | |
Ted Kremenek | 8595668 | 2008-05-30 16:14:41 +0000 | [diff] [blame] | 3322 | void setInitExpr(Expr *E) { Init = (Stmt*) E; } |
Craig Topper | 142f270 | 2024-01-16 12:49:11 -0800 | [diff] [blame] | 3323 | void setInitVal(const ASTContext &C, const llvm::APSInt &V) { |
| 3324 | setValue(C, V); |
| 3325 | IsUnsigned = V.isUnsigned(); |
| 3326 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3327 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 3328 | SourceRange getSourceRange() const override LLVM_READONLY; |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 3329 | |
Richard Smith | 01a7337 | 2013-10-15 22:02:41 +0000 | [diff] [blame] | 3330 | /// Retrieves the canonical declaration of this enumerator. |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 3331 | EnumConstantDecl *getCanonicalDecl() override { return getFirstDecl(); } |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 3332 | const EnumConstantDecl *getCanonicalDecl() const { return getFirstDecl(); } |
Richard Smith | 01a7337 | 2013-10-15 22:02:41 +0000 | [diff] [blame] | 3333 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3334 | // Implement isa/cast/dyncast/etc. |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 3335 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 3336 | static bool classofKind(Kind K) { return K == EnumConstant; } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3337 | }; |
| 3338 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 3339 | /// Represents a field injected from an anonymous union/struct into the parent |
| 3340 | /// scope. These are always implicit. |
Richard Smith | 8cbd895 | 2015-08-04 02:05:09 +0000 | [diff] [blame] | 3341 | class IndirectFieldDecl : public ValueDecl, |
| 3342 | public Mergeable<IndirectFieldDecl> { |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3343 | NamedDecl **Chaining; |
Benjamin Kramer | 3959370 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 3344 | unsigned ChainingSize; |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3345 | |
Richard Smith | 9f95182 | 2016-01-06 22:49:11 +0000 | [diff] [blame] | 3346 | IndirectFieldDecl(ASTContext &C, DeclContext *DC, SourceLocation L, |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3347 | DeclarationName N, QualType T, |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3348 | MutableArrayRef<NamedDecl *> CH); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3349 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3350 | void anchor() override; |
| 3351 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3352 | public: |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3353 | friend class ASTDeclReader; |
| 3354 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3355 | static IndirectFieldDecl *Create(ASTContext &C, DeclContext *DC, |
Bill Wendling | fca5191 | 2024-04-11 00:33:40 +0000 | [diff] [blame] | 3356 | SourceLocation L, const IdentifierInfo *Id, |
| 3357 | QualType T, |
| 3358 | llvm::MutableArrayRef<NamedDecl *> CH); |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 3359 | |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 3360 | static IndirectFieldDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 3361 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3362 | using chain_iterator = ArrayRef<NamedDecl *>::const_iterator; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3363 | |
| 3364 | ArrayRef<NamedDecl *> chain() const { |
serge-sans-paille | a3c248d | 2023-01-06 16:56:23 +0100 | [diff] [blame] | 3365 | return llvm::ArrayRef(Chaining, ChainingSize); |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 3366 | } |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3367 | chain_iterator chain_begin() const { return chain().begin(); } |
| 3368 | chain_iterator chain_end() const { return chain().end(); } |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3369 | |
Benjamin Kramer | 3959370 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 3370 | unsigned getChainingSize() const { return ChainingSize; } |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3371 | |
| 3372 | FieldDecl *getAnonField() const { |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3373 | assert(chain().size() >= 2); |
| 3374 | return cast<FieldDecl>(chain().back()); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
| 3377 | VarDecl *getVarDecl() const { |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3378 | assert(chain().size() >= 2); |
| 3379 | return dyn_cast<VarDecl>(chain().front()); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3380 | } |
| 3381 | |
Richard Smith | 8cbd895 | 2015-08-04 02:05:09 +0000 | [diff] [blame] | 3382 | IndirectFieldDecl *getCanonicalDecl() override { return getFirstDecl(); } |
| 3383 | const IndirectFieldDecl *getCanonicalDecl() const { return getFirstDecl(); } |
| 3384 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3385 | // Implement isa/cast/dyncast/etc. |
| 3386 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3387 | static bool classofKind(Kind K) { return K == IndirectField; } |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3388 | }; |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3389 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 3390 | /// Represents a declaration of a type. |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 3391 | class TypeDecl : public NamedDecl { |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3392 | friend class ASTContext; |
Chuanqi Xu | 55cdb3c | 2024-08-29 12:37:56 +0800 | [diff] [blame] | 3393 | friend class ASTReader; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3394 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 3395 | /// This indicates the Type object that represents |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3396 | /// this TypeDecl. It is a cache maintained by |
| 3397 | /// ASTContext::getTypedefType, ASTContext::getTagDeclType, and |
| 3398 | /// ASTContext::getTemplateTypeParmType, and TemplateTypeParmDecl. |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3399 | mutable const Type *TypeForDecl = nullptr; |
| 3400 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 3401 | /// The start of the source range for this declaration. |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3402 | SourceLocation LocStart; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3403 | |
| 3404 | void anchor() override; |
Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 3405 | |
Chris Lattner | 0659f48 | 2007-01-26 02:12:16 +0000 | [diff] [blame] | 3406 | protected: |
Bill Wendling | fca5191 | 2024-04-11 00:33:40 +0000 | [diff] [blame] | 3407 | TypeDecl(Kind DK, DeclContext *DC, SourceLocation L, const IdentifierInfo *Id, |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3408 | SourceLocation StartL = SourceLocation()) |
Bill Wendling | fca5191 | 2024-04-11 00:33:40 +0000 | [diff] [blame] | 3409 | : NamedDecl(DK, DC, L, Id), LocStart(StartL) {} |
Argyrios Kyrtzidis | 2951e14 | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 3410 | |
Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 3411 | public: |
Douglas Gregor | 4b12ba0 | 2012-07-17 23:09:36 +0000 | [diff] [blame] | 3412 | // Low-level accessor. If you just want the type defined by this node, |
| 3413 | // check out ASTContext::getTypeDeclType or one of |
| 3414 | // ASTContext::getTypedefType, ASTContext::getRecordType, etc. if you |
| 3415 | // already know the specific kind of node this is. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3416 | const Type *getTypeForDecl() const { return TypeForDecl; } |
| 3417 | void setTypeForDecl(const Type *TD) { TypeForDecl = TD; } |
Douglas Gregor | b1fe2c9 | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 3418 | |
Stephen Kelly | 724e9e5 | 2018-08-09 20:05:03 +0000 | [diff] [blame] | 3419 | SourceLocation getBeginLoc() const LLVM_READONLY { return LocStart; } |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3420 | void setLocStart(SourceLocation L) { LocStart = L; } |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 3421 | SourceRange getSourceRange() const override LLVM_READONLY { |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3422 | if (LocStart.isValid()) |
| 3423 | return SourceRange(LocStart, getLocation()); |
| 3424 | else |
| 3425 | return SourceRange(getLocation()); |
| 3426 | } |
| 3427 | |
Chris Lattner | 0659f48 | 2007-01-26 02:12:16 +0000 | [diff] [blame] | 3428 | // Implement isa/cast/dyncast/etc. |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 3429 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 3430 | static bool classofKind(Kind K) { return K >= firstType && K <= lastType; } |
Chris Lattner | 0659f48 | 2007-01-26 02:12:16 +0000 | [diff] [blame] | 3431 | }; |
| 3432 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3433 | /// Base class for declarations which introduce a typedef-name. |
| 3434 | class TypedefNameDecl : public TypeDecl, public Redeclarable<TypedefNameDecl> { |
Richard Smith | 7027ffa | 2018-07-17 22:24:11 +0000 | [diff] [blame] | 3435 | struct alignas(8) ModedTInfo { |
Benjamin Kramer | 4bb3334 | 2018-01-26 20:01:13 +0000 | [diff] [blame] | 3436 | TypeSourceInfo *first; |
| 3437 | QualType second; |
| 3438 | }; |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 3439 | |
Benjamin Kramer | dfb730a | 2018-01-26 14:14:11 +0000 | [diff] [blame] | 3440 | /// If int part is 0, we have not computed IsTransparentTag. |
| 3441 | /// Otherwise, IsTransparentTag is (getInt() >> 1). |
| 3442 | mutable llvm::PointerIntPair< |
| 3443 | llvm::PointerUnion<TypeSourceInfo *, ModedTInfo *>, 2> |
| 3444 | MaybeModedTInfo; |
Argyrios Kyrtzidis | 3b25c91 | 2017-03-21 16:56:02 +0000 | [diff] [blame] | 3445 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3446 | void anchor() override; |
| 3447 | |
Douglas Gregor | ecd99b1 | 2010-05-25 19:53:14 +0000 | [diff] [blame] | 3448 | protected: |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3449 | TypedefNameDecl(Kind DK, ASTContext &C, DeclContext *DC, |
| 3450 | SourceLocation StartLoc, SourceLocation IdLoc, |
Bill Wendling | fca5191 | 2024-04-11 00:33:40 +0000 | [diff] [blame] | 3451 | const IdentifierInfo *Id, TypeSourceInfo *TInfo) |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3452 | : TypeDecl(DK, DC, IdLoc, Id, StartLoc), redeclarable_base(C), |
Benjamin Kramer | dfb730a | 2018-01-26 14:14:11 +0000 | [diff] [blame] | 3453 | MaybeModedTInfo(TInfo, 0) {} |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3454 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3455 | using redeclarable_base = Redeclarable<TypedefNameDecl>; |
| 3456 | |
Richard Smith | d7af8a3 | 2014-05-10 01:17:36 +0000 | [diff] [blame] | 3457 | TypedefNameDecl *getNextRedeclarationImpl() override { |
| 3458 | return getNextRedeclaration(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3459 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3460 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 3461 | TypedefNameDecl *getPreviousDeclImpl() override { |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3462 | return getPreviousDecl(); |
| 3463 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3464 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 3465 | TypedefNameDecl *getMostRecentDeclImpl() override { |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3466 | return getMostRecentDecl(); |
| 3467 | } |
Douglas Gregor | ecd99b1 | 2010-05-25 19:53:14 +0000 | [diff] [blame] | 3468 | |
Chris Lattner | a7b3287 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 3469 | public: |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3470 | using redecl_range = redeclarable_base::redecl_range; |
| 3471 | using redecl_iterator = redeclarable_base::redecl_iterator; |
| 3472 | |
Aaron Ballman | 211cd8c | 2014-03-07 00:10:58 +0000 | [diff] [blame] | 3473 | using redeclarable_base::redecls_begin; |
| 3474 | using redeclarable_base::redecls_end; |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 3475 | using redeclarable_base::redecls; |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3476 | using redeclarable_base::getPreviousDecl; |
| 3477 | using redeclarable_base::getMostRecentDecl; |
Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 3478 | using redeclarable_base::isFirstDecl; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 3479 | |
Benjamin Kramer | dfb730a | 2018-01-26 14:14:11 +0000 | [diff] [blame] | 3480 | bool isModed() const { |
Kazu Hirata | 2a825cd2 | 2024-12-12 01:15:39 -0800 | [diff] [blame] | 3481 | return isa<ModedTInfo *>(MaybeModedTInfo.getPointer()); |
Benjamin Kramer | dfb730a | 2018-01-26 14:14:11 +0000 | [diff] [blame] | 3482 | } |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3483 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3484 | TypeSourceInfo *getTypeSourceInfo() const { |
Kazu Hirata | 2a825cd2 | 2024-12-12 01:15:39 -0800 | [diff] [blame] | 3485 | return isModed() ? cast<ModedTInfo *>(MaybeModedTInfo.getPointer())->first |
| 3486 | : cast<TypeSourceInfo *>(MaybeModedTInfo.getPointer()); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3487 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3488 | |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3489 | QualType getUnderlyingType() const { |
Kazu Hirata | 2a825cd2 | 2024-12-12 01:15:39 -0800 | [diff] [blame] | 3490 | return isModed() ? cast<ModedTInfo *>(MaybeModedTInfo.getPointer())->second |
| 3491 | : cast<TypeSourceInfo *>(MaybeModedTInfo.getPointer()) |
Benjamin Kramer | dfb730a | 2018-01-26 14:14:11 +0000 | [diff] [blame] | 3492 | ->getType(); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3493 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3494 | |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3495 | void setTypeSourceInfo(TypeSourceInfo *newType) { |
Benjamin Kramer | dfb730a | 2018-01-26 14:14:11 +0000 | [diff] [blame] | 3496 | MaybeModedTInfo.setPointer(newType); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3497 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3498 | |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3499 | void setModedTypeSourceInfo(TypeSourceInfo *unmodedTSI, QualType modedTy) { |
Benjamin Kramer | dfb730a | 2018-01-26 14:14:11 +0000 | [diff] [blame] | 3500 | MaybeModedTInfo.setPointer(new (getASTContext(), 8) |
Benjamin Kramer | 4bb3334 | 2018-01-26 20:01:13 +0000 | [diff] [blame] | 3501 | ModedTInfo({unmodedTSI, modedTy})); |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3504 | /// Retrieves the canonical declaration of this typedef-name. |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 3505 | TypedefNameDecl *getCanonicalDecl() override { return getFirstDecl(); } |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 3506 | const TypedefNameDecl *getCanonicalDecl() const { return getFirstDecl(); } |
John McCall | 91f1a02 | 2009-12-30 00:31:22 +0000 | [diff] [blame] | 3507 | |
Richard Smith | a523022 | 2015-03-27 01:37:43 +0000 | [diff] [blame] | 3508 | /// Retrieves the tag declaration for which this is the typedef name for |
| 3509 | /// linkage purposes, if any. |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 3510 | /// |
| 3511 | /// \param AnyRedecl Look for the tag declaration in any redeclaration of |
| 3512 | /// this typedef declaration. |
| 3513 | TagDecl *getAnonDeclWithTypedefName(bool AnyRedecl = false) const; |
Richard Smith | a523022 | 2015-03-27 01:37:43 +0000 | [diff] [blame] | 3514 | |
Argyrios Kyrtzidis | 3b25c91 | 2017-03-21 16:56:02 +0000 | [diff] [blame] | 3515 | /// Determines if this typedef shares a name and spelling location with its |
| 3516 | /// underlying tag type, as is the case with the NS_ENUM macro. |
| 3517 | bool isTransparentTag() const { |
Benjamin Kramer | dfb730a | 2018-01-26 14:14:11 +0000 | [diff] [blame] | 3518 | if (MaybeModedTInfo.getInt()) |
| 3519 | return MaybeModedTInfo.getInt() & 0x2; |
Argyrios Kyrtzidis | 3b25c91 | 2017-03-21 16:56:02 +0000 | [diff] [blame] | 3520 | return isTransparentTagSlow(); |
| 3521 | } |
| 3522 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3523 | // Implement isa/cast/dyncast/etc. |
| 3524 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3525 | static bool classofKind(Kind K) { |
| 3526 | return K >= firstTypedefName && K <= lastTypedefName; |
| 3527 | } |
Argyrios Kyrtzidis | 3b25c91 | 2017-03-21 16:56:02 +0000 | [diff] [blame] | 3528 | |
| 3529 | private: |
| 3530 | bool isTransparentTagSlow() const; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3531 | }; |
| 3532 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 3533 | /// Represents the declaration of a typedef-name via the 'typedef' |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3534 | /// type specifier. |
| 3535 | class TypedefDecl : public TypedefNameDecl { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3536 | TypedefDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
Bill Wendling | fca5191 | 2024-04-11 00:33:40 +0000 | [diff] [blame] | 3537 | SourceLocation IdLoc, const IdentifierInfo *Id, |
| 3538 | TypeSourceInfo *TInfo) |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3539 | : TypedefNameDecl(Typedef, C, DC, StartLoc, IdLoc, Id, TInfo) {} |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3540 | |
| 3541 | public: |
| 3542 | static TypedefDecl *Create(ASTContext &C, DeclContext *DC, |
| 3543 | SourceLocation StartLoc, SourceLocation IdLoc, |
Bill Wendling | fca5191 | 2024-04-11 00:33:40 +0000 | [diff] [blame] | 3544 | const IdentifierInfo *Id, TypeSourceInfo *TInfo); |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 3545 | static TypedefDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 3546 | |
| 3547 | SourceRange getSourceRange() const override LLVM_READONLY; |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 3548 | |
Chris Lattner | 0659f48 | 2007-01-26 02:12:16 +0000 | [diff] [blame] | 3549 | // Implement isa/cast/dyncast/etc. |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 3550 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 3551 | static bool classofKind(Kind K) { return K == Typedef; } |
Chris Lattner | 0659f48 | 2007-01-26 02:12:16 +0000 | [diff] [blame] | 3552 | }; |
| 3553 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 3554 | /// Represents the declaration of a typedef-name via a C++11 |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3555 | /// alias-declaration. |
| 3556 | class TypeAliasDecl : public TypedefNameDecl { |
Richard Smith | 43ccec8e | 2014-08-26 03:52:16 +0000 | [diff] [blame] | 3557 | /// The template for which this is the pattern, if any. |
| 3558 | TypeAliasTemplateDecl *Template; |
| 3559 | |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3560 | TypeAliasDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
Bill Wendling | fca5191 | 2024-04-11 00:33:40 +0000 | [diff] [blame] | 3561 | SourceLocation IdLoc, const IdentifierInfo *Id, |
| 3562 | TypeSourceInfo *TInfo) |
Richard Smith | 43ccec8e | 2014-08-26 03:52:16 +0000 | [diff] [blame] | 3563 | : TypedefNameDecl(TypeAlias, C, DC, StartLoc, IdLoc, Id, TInfo), |
| 3564 | Template(nullptr) {} |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3565 | |
| 3566 | public: |
| 3567 | static TypeAliasDecl *Create(ASTContext &C, DeclContext *DC, |
| 3568 | SourceLocation StartLoc, SourceLocation IdLoc, |
Bill Wendling | fca5191 | 2024-04-11 00:33:40 +0000 | [diff] [blame] | 3569 | const IdentifierInfo *Id, TypeSourceInfo *TInfo); |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 3570 | static TypeAliasDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3571 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 3572 | SourceRange getSourceRange() const override LLVM_READONLY; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3573 | |
Richard Smith | 43ccec8e | 2014-08-26 03:52:16 +0000 | [diff] [blame] | 3574 | TypeAliasTemplateDecl *getDescribedAliasTemplate() const { return Template; } |
| 3575 | void setDescribedAliasTemplate(TypeAliasTemplateDecl *TAT) { Template = TAT; } |
| 3576 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3577 | // Implement isa/cast/dyncast/etc. |
| 3578 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3579 | static bool classofKind(Kind K) { return K == TypeAlias; } |
| 3580 | }; |
| 3581 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 3582 | /// Represents the declaration of a struct/union/class/enum. |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3583 | class TagDecl : public TypeDecl, |
| 3584 | public DeclContext, |
| 3585 | public Redeclarable<TagDecl> { |
| 3586 | // This class stores some data in DeclContext::TagDeclBits |
| 3587 | // to save some space. Use the provided accessors to access it. |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3588 | public: |
John McCall | fcc33b0 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 3589 | // This is really ugly. |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3590 | using TagKind = TagTypeKind; |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3591 | |
| 3592 | private: |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 3593 | SourceRange BraceRange; |
Argyrios Kyrtzidis | 575fa05 | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 3594 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3595 | // A struct representing syntactic qualifier info, |
| 3596 | // to be used for the (uncommon) case of out-of-line declarations. |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3597 | using ExtInfo = QualifierInfo; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3598 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3599 | /// If the (out-of-line) tag declaration name |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3600 | /// is qualified, it points to the qualifier info (nns and range); |
| 3601 | /// otherwise, if the tag declaration is anonymous and it is part of |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3602 | /// a typedef or alias, it points to the TypedefNameDecl (used for mangling); |
David Majnemer | 50ce835 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 3603 | /// otherwise, if the tag declaration is anonymous and it is used as a |
| 3604 | /// declaration specifier for variables, it points to the first VarDecl (used |
| 3605 | /// for mangling); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3606 | /// otherwise, it is a null (TypedefNameDecl) pointer. |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 3607 | llvm::PointerUnion<TypedefNameDecl *, ExtInfo *> TypedefNameDeclOrQualifier; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3608 | |
Kazu Hirata | 624cc70 | 2024-12-11 08:35:41 -0800 | [diff] [blame] | 3609 | bool hasExtInfo() const { return isa<ExtInfo *>(TypedefNameDeclOrQualifier); } |
| 3610 | ExtInfo *getExtInfo() { return cast<ExtInfo *>(TypedefNameDeclOrQualifier); } |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3611 | const ExtInfo *getExtInfo() const { |
Kazu Hirata | 624cc70 | 2024-12-11 08:35:41 -0800 | [diff] [blame] | 3612 | return cast<ExtInfo *>(TypedefNameDeclOrQualifier); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3613 | } |
| 3614 | |
Chris Lattner | 28743e2 | 2007-01-22 07:41:08 +0000 | [diff] [blame] | 3615 | protected: |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3616 | TagDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC, |
| 3617 | SourceLocation L, IdentifierInfo *Id, TagDecl *PrevDecl, |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3618 | SourceLocation StartL); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3619 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3620 | using redeclarable_base = Redeclarable<TagDecl>; |
| 3621 | |
Richard Smith | d7af8a3 | 2014-05-10 01:17:36 +0000 | [diff] [blame] | 3622 | TagDecl *getNextRedeclarationImpl() override { |
| 3623 | return getNextRedeclaration(); |
| 3624 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3625 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 3626 | TagDecl *getPreviousDeclImpl() override { |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3627 | return getPreviousDecl(); |
| 3628 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3629 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 3630 | TagDecl *getMostRecentDeclImpl() override { |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3631 | return getMostRecentDecl(); |
| 3632 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3633 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3634 | /// Completes the definition of this tag declaration. |
Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 3635 | /// |
| 3636 | /// This is a helper function for derived classes. |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 3637 | void completeDefinition(); |
| 3638 | |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3639 | /// True if this decl is currently being defined. |
| 3640 | void setBeingDefined(bool V = true) { TagDeclBits.IsBeingDefined = V; } |
| 3641 | |
| 3642 | /// Indicates whether it is possible for declarations of this kind |
| 3643 | /// to have an out-of-date definition. |
| 3644 | /// |
| 3645 | /// This option is only enabled when modules are enabled. |
| 3646 | void setMayHaveOutOfDateDef(bool V = true) { |
| 3647 | TagDeclBits.MayHaveOutOfDateDef = V; |
| 3648 | } |
| 3649 | |
Chris Lattner | 28743e2 | 2007-01-22 07:41:08 +0000 | [diff] [blame] | 3650 | public: |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3651 | friend class ASTDeclReader; |
| 3652 | friend class ASTDeclWriter; |
| 3653 | |
| 3654 | using redecl_range = redeclarable_base::redecl_range; |
| 3655 | using redecl_iterator = redeclarable_base::redecl_iterator; |
| 3656 | |
Aaron Ballman | 211cd8c | 2014-03-07 00:10:58 +0000 | [diff] [blame] | 3657 | using redeclarable_base::redecls_begin; |
| 3658 | using redeclarable_base::redecls_end; |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 3659 | using redeclarable_base::redecls; |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3660 | using redeclarable_base::getPreviousDecl; |
| 3661 | using redeclarable_base::getMostRecentDecl; |
Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 3662 | using redeclarable_base::isFirstDecl; |
Douglas Gregor | 0bc8a21 | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 3663 | |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 3664 | SourceRange getBraceRange() const { return BraceRange; } |
| 3665 | void setBraceRange(SourceRange R) { BraceRange = R; } |
Argyrios Kyrtzidis | 575fa05 | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 3666 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 3667 | /// Return SourceLocation representing start of source |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 3668 | /// range ignoring outer template declarations. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3669 | SourceLocation getInnerLocStart() const { return getBeginLoc(); } |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 3670 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 3671 | /// Return SourceLocation representing start of source |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 3672 | /// range taking into account any outer template declarations. |
| 3673 | SourceLocation getOuterLocStart() const; |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 3674 | SourceRange getSourceRange() const override LLVM_READONLY; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3675 | |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3676 | TagDecl *getCanonicalDecl() override; |
| 3677 | const TagDecl *getCanonicalDecl() const { |
John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 3678 | return const_cast<TagDecl*>(this)->getCanonicalDecl(); |
| 3679 | } |
Argyrios Kyrtzidis | 575fa05 | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 3680 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 3681 | /// Return true if this declaration is a completion definition of the type. |
| 3682 | /// Provided for consistency. |
John McCall | c265ad2 | 2010-08-31 22:21:26 +0000 | [diff] [blame] | 3683 | bool isThisDeclarationADefinition() const { |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3684 | return isCompleteDefinition(); |
John McCall | c265ad2 | 2010-08-31 22:21:26 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 3687 | /// Return true if this decl has its body fully specified. |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3688 | bool isCompleteDefinition() const { return TagDeclBits.IsCompleteDefinition; } |
| 3689 | |
| 3690 | /// True if this decl has its body fully specified. |
| 3691 | void setCompleteDefinition(bool V = true) { |
| 3692 | TagDeclBits.IsCompleteDefinition = V; |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 3693 | } |
Douglas Gregor | e362cea | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 3694 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3695 | /// Return true if this complete decl is |
David Blaikie | 48ad6dc | 2013-07-13 21:08:14 +0000 | [diff] [blame] | 3696 | /// required to be complete for some existing use. |
| 3697 | bool isCompleteDefinitionRequired() const { |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3698 | return TagDeclBits.IsCompleteDefinitionRequired; |
| 3699 | } |
| 3700 | |
| 3701 | /// True if this complete decl is |
| 3702 | /// required to be complete for some existing use. |
| 3703 | void setCompleteDefinitionRequired(bool V = true) { |
| 3704 | TagDeclBits.IsCompleteDefinitionRequired = V; |
David Blaikie | 48ad6dc | 2013-07-13 21:08:14 +0000 | [diff] [blame] | 3705 | } |
| 3706 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 3707 | /// Return true if this decl is currently being defined. |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3708 | bool isBeingDefined() const { return TagDeclBits.IsBeingDefined; } |
Sebastian Redl | 9d8854e | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 3709 | |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3710 | /// True if this tag declaration is "embedded" (i.e., defined or declared |
| 3711 | /// for the very first time) in the syntax of a declarator. |
Douglas Gregor | 5089c76 | 2010-02-12 17:40:34 +0000 | [diff] [blame] | 3712 | bool isEmbeddedInDeclarator() const { |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3713 | return TagDeclBits.IsEmbeddedInDeclarator; |
Douglas Gregor | 586d0f9 | 2010-02-08 22:07:33 +0000 | [diff] [blame] | 3714 | } |
| 3715 | |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3716 | /// True if this tag declaration is "embedded" (i.e., defined or declared |
| 3717 | /// for the very first time) in the syntax of a declarator. |
| 3718 | void setEmbeddedInDeclarator(bool isInDeclarator) { |
| 3719 | TagDeclBits.IsEmbeddedInDeclarator = isInDeclarator; |
Argyrios Kyrtzidis | 201d377 | 2011-09-30 22:11:31 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3722 | /// True if this tag is free standing, e.g. "struct foo;". |
| 3723 | bool isFreeStanding() const { return TagDeclBits.IsFreeStanding; } |
| 3724 | |
| 3725 | /// True if this tag is free standing, e.g. "struct foo;". |
| 3726 | void setFreeStanding(bool isFreeStanding = true) { |
| 3727 | TagDeclBits.IsFreeStanding = isFreeStanding; |
| 3728 | } |
| 3729 | |
| 3730 | /// Indicates whether it is possible for declarations of this kind |
| 3731 | /// to have an out-of-date definition. |
| 3732 | /// |
| 3733 | /// This option is only enabled when modules are enabled. |
| 3734 | bool mayHaveOutOfDateDef() const { return TagDeclBits.MayHaveOutOfDateDef; } |
| 3735 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3736 | /// Whether this declaration declares a type that is |
Douglas Gregor | e362cea | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 3737 | /// dependent, i.e., a type that somehow depends on template |
| 3738 | /// parameters. |
Douglas Gregor | 9e927ab | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 3739 | bool isDependentType() const { return isDependentContext(); } |
Douglas Gregor | e362cea | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 3740 | |
Volodymyr Sapsai | fa4a0f1 | 2022-02-01 15:03:59 -0800 | [diff] [blame] | 3741 | /// Whether this declaration was a definition in some module but was forced |
| 3742 | /// to be a declaration. |
| 3743 | /// |
| 3744 | /// Useful for clients checking if a module has a definition of a specific |
| 3745 | /// symbol and not interested in the final AST with deduplicated definitions. |
| 3746 | bool isThisDeclarationADemotedDefinition() const { |
| 3747 | return TagDeclBits.IsThisDeclarationADemotedDefinition; |
| 3748 | } |
| 3749 | |
| 3750 | /// Mark a definition as a declaration and maintain information it _was_ |
| 3751 | /// a definition. |
| 3752 | void demoteThisDefinitionToDeclaration() { |
| 3753 | assert(isCompleteDefinition() && |
| 3754 | "Should demote definitions only, not forward declarations"); |
| 3755 | setCompleteDefinition(false); |
| 3756 | TagDeclBits.IsThisDeclarationADemotedDefinition = true; |
| 3757 | } |
| 3758 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 3759 | /// Starts the definition of this tag declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3760 | /// |
Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 3761 | /// This method should be invoked at the beginning of the definition |
| 3762 | /// of this tag declaration. It will set the tag type into a state |
| 3763 | /// where it is in the process of being defined. |
| 3764 | void startDefinition(); |
| 3765 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 3766 | /// Returns the TagDecl that actually defines this |
Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 3767 | /// struct/union/class/enum. When determining whether or not a |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3768 | /// struct/union/class/enum has a definition, one should use this |
| 3769 | /// method as opposed to 'isDefinition'. 'isDefinition' indicates |
| 3770 | /// whether or not a specific TagDecl is defining declaration, not |
| 3771 | /// whether or not the struct/union/class/enum type is defined. |
| 3772 | /// This method returns NULL if there is no TagDecl that defines |
| 3773 | /// the struct/union/class/enum. |
| 3774 | TagDecl *getDefinition() const; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3775 | |
Alp Toker | a030cd0 | 2014-05-05 12:38:48 +0000 | [diff] [blame] | 3776 | StringRef getKindName() const { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3777 | return TypeWithKeyword::getTagTypeKindName(getTagKind()); |
Chris Lattner | fb072463 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 3778 | } |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3779 | |
| 3780 | TagKind getTagKind() const { |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3781 | return static_cast<TagKind>(TagDeclBits.TagDeclKind); |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 3782 | } |
| 3783 | |
Vlad Serebrennikov | edd690b | 2023-11-03 21:45:39 +0400 | [diff] [blame] | 3784 | void setTagKind(TagKind TK) { |
| 3785 | TagDeclBits.TagDeclKind = llvm::to_underlying(TK); |
| 3786 | } |
Douglas Gregor | 1daeb69 | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 3787 | |
Vlad Serebrennikov | edd690b | 2023-11-03 21:45:39 +0400 | [diff] [blame] | 3788 | bool isStruct() const { return getTagKind() == TagTypeKind::Struct; } |
| 3789 | bool isInterface() const { return getTagKind() == TagTypeKind::Interface; } |
| 3790 | bool isClass() const { return getTagKind() == TagTypeKind::Class; } |
| 3791 | bool isUnion() const { return getTagKind() == TagTypeKind::Union; } |
| 3792 | bool isEnum() const { return getTagKind() == TagTypeKind::Enum; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3793 | |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 3794 | /// Is this tag type named, either directly or via being defined in |
| 3795 | /// a typedef of this type? |
| 3796 | /// |
| 3797 | /// C++11 [basic.link]p8: |
| 3798 | /// A type is said to have linkage if and only if: |
| 3799 | /// - it is a class or enumeration type that is named (or has a |
| 3800 | /// name for linkage purposes) and the name has linkage; ... |
| 3801 | /// C++11 [dcl.typedef]p9: |
| 3802 | /// If the typedef declaration defines an unnamed class (or enum), |
| 3803 | /// the first typedef-name declared by the declaration to be that |
| 3804 | /// class type (or enum type) is used to denote the class type (or |
| 3805 | /// enum type) for linkage purposes only. |
| 3806 | /// |
| 3807 | /// C does not have an analogous rule, but the same concept is |
| 3808 | /// nonetheless useful in some places. |
| 3809 | bool hasNameForLinkage() const { |
| 3810 | return (getDeclName() || getTypedefNameForAnonDecl()); |
| 3811 | } |
| 3812 | |
David Majnemer | 50ce835 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 3813 | TypedefNameDecl *getTypedefNameForAnonDecl() const { |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 3814 | return hasExtInfo() ? nullptr |
Kazu Hirata | 624cc70 | 2024-12-11 08:35:41 -0800 | [diff] [blame] | 3815 | : cast<TypedefNameDecl *>(TypedefNameDeclOrQualifier); |
David Majnemer | 50ce835 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3818 | void setTypedefNameForAnonDecl(TypedefNameDecl *TDD); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 3819 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3820 | /// Retrieve the nested-name-specifier that qualifies the name of this |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3821 | /// declaration, if it was present in the source. |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3822 | NestedNameSpecifier *getQualifier() const { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3823 | return hasExtInfo() ? getExtInfo()->QualifierLoc.getNestedNameSpecifier() |
Craig Topper | 34b4c43 | 2014-05-06 06:48:52 +0000 | [diff] [blame] | 3824 | : nullptr; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3825 | } |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 3826 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3827 | /// Retrieve the nested-name-specifier (with source-location |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 3828 | /// information) that qualifies the name of this declaration, if it was |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3829 | /// present in the source. |
| 3830 | NestedNameSpecifierLoc getQualifierLoc() const { |
| 3831 | return hasExtInfo() ? getExtInfo()->QualifierLoc |
| 3832 | : NestedNameSpecifierLoc(); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3833 | } |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 3834 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3835 | void setQualifierInfo(NestedNameSpecifierLoc QualifierLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3836 | |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 3837 | unsigned getNumTemplateParameterLists() const { |
| 3838 | return hasExtInfo() ? getExtInfo()->NumTemplParamLists : 0; |
| 3839 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3840 | |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 3841 | TemplateParameterList *getTemplateParameterList(unsigned i) const { |
| 3842 | assert(i < getNumTemplateParameterLists()); |
| 3843 | return getExtInfo()->TemplParamLists[i]; |
| 3844 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3845 | |
Aaron Ballman | 0566791 | 2023-07-08 08:30:58 -0400 | [diff] [blame] | 3846 | using TypeDecl::printName; |
Aaron Ballman | 19e984e | 2022-10-14 08:17:16 -0400 | [diff] [blame] | 3847 | void printName(raw_ostream &OS, const PrintingPolicy &Policy) const override; |
| 3848 | |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 3849 | void setTemplateParameterListsInfo(ASTContext &Context, |
| 3850 | ArrayRef<TemplateParameterList *> TPLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 3851 | |
Chris Lattner | 28743e2 | 2007-01-22 07:41:08 +0000 | [diff] [blame] | 3852 | // Implement isa/cast/dyncast/etc. |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 3853 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 3854 | static bool classofKind(Kind K) { return K >= firstTag && K <= lastTag; } |
Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 3855 | |
| 3856 | static DeclContext *castToDeclContext(const TagDecl *D) { |
| 3857 | return static_cast<DeclContext *>(const_cast<TagDecl*>(D)); |
| 3858 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3859 | |
Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 3860 | static TagDecl *castFromDeclContext(const DeclContext *DC) { |
| 3861 | return static_cast<TagDecl *>(const_cast<DeclContext*>(DC)); |
| 3862 | } |
Chris Lattner | 28743e2 | 2007-01-22 07:41:08 +0000 | [diff] [blame] | 3863 | }; |
| 3864 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 3865 | /// Represents an enum. In C++11, enums can be forward-declared |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 3866 | /// with a fixed underlying type, and in C we allow them to be forward-declared |
| 3867 | /// with no underlying type as an extension. |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3868 | class EnumDecl : public TagDecl { |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3869 | // This class stores some data in DeclContext::EnumDeclBits |
| 3870 | // to save some space. Use the provided accessors to access it. |
| 3871 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 3872 | /// This represent the integer type that the enum corresponds |
Chris Lattner | 1c1f932 | 2007-08-28 18:24:31 +0000 | [diff] [blame] | 3873 | /// to for code generation purposes. Note that the enumerator constants may |
| 3874 | /// have a different type than this does. |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3875 | /// |
| 3876 | /// If the underlying integer type was explicitly stated in the source |
| 3877 | /// code, this is a TypeSourceInfo* for that type. Otherwise this type |
| 3878 | /// was automatically deduced somehow, and this is a Type*. |
| 3879 | /// |
| 3880 | /// Normally if IsFixed(), this would contain a TypeSourceInfo*, but in |
| 3881 | /// some cases it won't. |
| 3882 | /// |
| 3883 | /// The underlying type of an enumeration never has any qualifiers, so |
| 3884 | /// we can get away with just storing a raw Type*, and thus save an |
| 3885 | /// extra pointer when TypeSourceInfo is needed. |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3886 | llvm::PointerUnion<const Type *, TypeSourceInfo *> IntegerType; |
Douglas Gregor | 7a74938 | 2009-05-27 17:20:35 +0000 | [diff] [blame] | 3887 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 3888 | /// The integer type that values of this type should |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3889 | /// promote to. In C, enumerators are generally of an integer type |
| 3890 | /// directly, but gcc-style large enumerators (and all enumerators |
| 3891 | /// in C++) are of the enum type instead. |
| 3892 | QualType PromotionType; |
| 3893 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3894 | /// If this enumeration is an instantiation of a member enumeration |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 3895 | /// of a class template specialization, this is the member specialization |
| 3896 | /// information. |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3897 | MemberSpecializationInfo *SpecializationInfo = nullptr; |
Douglas Gregor | 7a74938 | 2009-05-27 17:20:35 +0000 | [diff] [blame] | 3898 | |
Richard Trieu | ab4d730 | 2018-07-25 22:52:05 +0000 | [diff] [blame] | 3899 | /// Store the ODRHash after first calculation. |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3900 | /// The corresponding flag HasODRHash is in EnumDeclBits |
| 3901 | /// and can be accessed with the provided accessors. |
Richard Trieu | ab4d730 | 2018-07-25 22:52:05 +0000 | [diff] [blame] | 3902 | unsigned ODRHash; |
| 3903 | |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3904 | EnumDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
| 3905 | SourceLocation IdLoc, IdentifierInfo *Id, EnumDecl *PrevDecl, |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3906 | bool Scoped, bool ScopedUsingClassTag, bool Fixed); |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 3907 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3908 | void anchor() override; |
| 3909 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 3910 | void setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, |
| 3911 | TemplateSpecializationKind TSK); |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3912 | |
| 3913 | /// Sets the width in bits required to store all the |
| 3914 | /// non-negative enumerators of this enum. |
| 3915 | void setNumPositiveBits(unsigned Num) { |
| 3916 | EnumDeclBits.NumPositiveBits = Num; |
| 3917 | assert(EnumDeclBits.NumPositiveBits == Num && "can't store this bitcount"); |
| 3918 | } |
| 3919 | |
| 3920 | /// Returns the width in bits required to store all the |
| 3921 | /// negative enumerators of this enum. (see getNumNegativeBits) |
| 3922 | void setNumNegativeBits(unsigned Num) { EnumDeclBits.NumNegativeBits = Num; } |
| 3923 | |
Adrian Prantl | 2b366e75 | 2020-03-03 12:54:04 -0800 | [diff] [blame] | 3924 | public: |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3925 | /// True if this tag declaration is a scoped enumeration. Only |
| 3926 | /// possible in C++11 mode. |
| 3927 | void setScoped(bool Scoped = true) { EnumDeclBits.IsScoped = Scoped; } |
| 3928 | |
| 3929 | /// If this tag declaration is a scoped enum, |
| 3930 | /// then this is true if the scoped enum was declared using the class |
| 3931 | /// tag, false if it was declared with the struct tag. No meaning is |
| 3932 | /// associated if this tag declaration is not a scoped enum. |
| 3933 | void setScopedUsingClassTag(bool ScopedUCT = true) { |
| 3934 | EnumDeclBits.IsScopedUsingClassTag = ScopedUCT; |
| 3935 | } |
| 3936 | |
| 3937 | /// True if this is an Objective-C, C++11, or |
| 3938 | /// Microsoft-style enumeration with a fixed underlying type. |
| 3939 | void setFixed(bool Fixed = true) { EnumDeclBits.IsFixed = Fixed; } |
| 3940 | |
Adrian Prantl | 2b366e75 | 2020-03-03 12:54:04 -0800 | [diff] [blame] | 3941 | private: |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3942 | /// True if a valid hash is stored in ODRHash. |
| 3943 | bool hasODRHash() const { return EnumDeclBits.HasODRHash; } |
| 3944 | void setHasODRHash(bool Hash = true) { EnumDeclBits.HasODRHash = Hash; } |
| 3945 | |
Chris Lattner | a7b3287 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 3946 | public: |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3947 | friend class ASTDeclReader; |
| 3948 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 3949 | EnumDecl *getCanonicalDecl() override { |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 3950 | return cast<EnumDecl>(TagDecl::getCanonicalDecl()); |
| 3951 | } |
John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 3952 | const EnumDecl *getCanonicalDecl() const { |
Rafael Espindola | 7b56f6c | 2013-10-19 16:55:03 +0000 | [diff] [blame] | 3953 | return const_cast<EnumDecl*>(this)->getCanonicalDecl(); |
John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 3954 | } |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 3955 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3956 | EnumDecl *getPreviousDecl() { |
Aaron Ballman | 5116a8e | 2013-11-06 22:39:46 +0000 | [diff] [blame] | 3957 | return cast_or_null<EnumDecl>( |
| 3958 | static_cast<TagDecl *>(this)->getPreviousDecl()); |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3959 | } |
Rafael Espindola | 7b56f6c | 2013-10-19 16:55:03 +0000 | [diff] [blame] | 3960 | const EnumDecl *getPreviousDecl() const { |
| 3961 | return const_cast<EnumDecl*>(this)->getPreviousDecl(); |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3962 | } |
Rafael Espindola | 7b56f6c | 2013-10-19 16:55:03 +0000 | [diff] [blame] | 3963 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3964 | EnumDecl *getMostRecentDecl() { |
Aaron Ballman | 5116a8e | 2013-11-06 22:39:46 +0000 | [diff] [blame] | 3965 | return cast<EnumDecl>(static_cast<TagDecl *>(this)->getMostRecentDecl()); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3966 | } |
Rafael Espindola | 7b56f6c | 2013-10-19 16:55:03 +0000 | [diff] [blame] | 3967 | const EnumDecl *getMostRecentDecl() const { |
| 3968 | return const_cast<EnumDecl*>(this)->getMostRecentDecl(); |
| 3969 | } |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3970 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 3971 | EnumDecl *getDefinition() const { |
| 3972 | return cast_or_null<EnumDecl>(TagDecl::getDefinition()); |
| 3973 | } |
| 3974 | |
Chris Lattner | bec4134 | 2008-04-22 18:39:57 +0000 | [diff] [blame] | 3975 | static EnumDecl *Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3976 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 3977 | IdentifierInfo *Id, EnumDecl *PrevDecl, |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3978 | bool IsScoped, bool IsScopedUsingClassTag, |
| 3979 | bool IsFixed); |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 3980 | static EnumDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3981 | |
Kadir Cetinkaya | ffa96f0 | 2021-10-06 20:25:26 +0200 | [diff] [blame] | 3982 | /// Overrides to provide correct range when there's an enum-base specifier |
| 3983 | /// with forward declarations. |
| 3984 | SourceRange getSourceRange() const override LLVM_READONLY; |
| 3985 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 3986 | /// When created, the EnumDecl corresponds to a |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3987 | /// forward-declared enum. This method is used to mark the |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 3988 | /// declaration as being defined; its enumerators have already been |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3989 | /// added (via DeclContext::addDecl). NewType is the new underlying |
| 3990 | /// type of the enumeration type. |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3991 | void completeDefinition(QualType NewType, |
John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 3992 | QualType PromotionType, |
| 3993 | unsigned NumPositiveBits, |
| 3994 | unsigned NumNegativeBits); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3995 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 3996 | // Iterates through the enumerators of this enumeration. |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3997 | using enumerator_iterator = specific_decl_iterator<EnumConstantDecl>; |
| 3998 | using enumerator_range = |
| 3999 | llvm::iterator_range<specific_decl_iterator<EnumConstantDecl>>; |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 4000 | |
| 4001 | enumerator_range enumerators() const { |
| 4002 | return enumerator_range(enumerator_begin(), enumerator_end()); |
| 4003 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4004 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4005 | enumerator_iterator enumerator_begin() const { |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 4006 | const EnumDecl *E = getDefinition(); |
Douglas Gregor | a46d661 | 2010-06-22 14:45:56 +0000 | [diff] [blame] | 4007 | if (!E) |
| 4008 | E = this; |
| 4009 | return enumerator_iterator(E->decls_begin()); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4010 | } |
| 4011 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4012 | enumerator_iterator enumerator_end() const { |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 4013 | const EnumDecl *E = getDefinition(); |
Douglas Gregor | a46d661 | 2010-06-22 14:45:56 +0000 | [diff] [blame] | 4014 | if (!E) |
| 4015 | E = this; |
| 4016 | return enumerator_iterator(E->decls_end()); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4017 | } |
| 4018 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 4019 | /// Return the integer type that enumerators should promote to. |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4020 | QualType getPromotionType() const { return PromotionType; } |
| 4021 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 4022 | /// Set the promotion type. |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4023 | void setPromotionType(QualType T) { PromotionType = T; } |
| 4024 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 4025 | /// Return the integer type this enum decl corresponds to. |
Richard Smith | 8bcc086 | 2014-01-08 01:16:19 +0000 | [diff] [blame] | 4026 | /// This returns a null QualType for an enum forward definition with no fixed |
| 4027 | /// underlying type. |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4028 | QualType getIntegerType() const { |
| 4029 | if (!IntegerType) |
| 4030 | return QualType(); |
Kazu Hirata | c969964 | 2025-01-16 08:44:15 -0800 | [diff] [blame] | 4031 | if (const Type *T = dyn_cast<const Type *>(IntegerType)) |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4032 | return QualType(T, 0); |
Kazu Hirata | 624cc70 | 2024-12-11 08:35:41 -0800 | [diff] [blame] | 4033 | return cast<TypeSourceInfo *>(IntegerType)->getType().getUnqualifiedType(); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4034 | } |
Douglas Gregor | 1daeb69 | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 4035 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4036 | /// Set the underlying integer type. |
Douglas Gregor | 0cdc832 | 2010-12-10 17:03:06 +0000 | [diff] [blame] | 4037 | void setIntegerType(QualType T) { IntegerType = T.getTypePtrOrNull(); } |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4038 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4039 | /// Set the underlying integer type source info. |
Richard Smith | 8bcc086 | 2014-01-08 01:16:19 +0000 | [diff] [blame] | 4040 | void setIntegerTypeSourceInfo(TypeSourceInfo *TInfo) { IntegerType = TInfo; } |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4041 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4042 | /// Return the type source info for the underlying integer type, |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4043 | /// if no type source info exists, return 0. |
Richard Smith | 8bcc086 | 2014-01-08 01:16:19 +0000 | [diff] [blame] | 4044 | TypeSourceInfo *getIntegerTypeSourceInfo() const { |
Kazu Hirata | 563c7c5 | 2025-01-25 14:05:01 -0800 | [diff] [blame] | 4045 | return dyn_cast_if_present<TypeSourceInfo *>(IntegerType); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4046 | } |
Douglas Gregor | 1daeb69 | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 4047 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4048 | /// Retrieve the source range that covers the underlying type if |
Alp Toker | b9fa512 | 2014-01-06 11:31:18 +0000 | [diff] [blame] | 4049 | /// specified. |
| 4050 | SourceRange getIntegerTypeRange() const LLVM_READONLY; |
| 4051 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4052 | /// Returns the width in bits required to store all the |
John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 4053 | /// non-negative enumerators of this enum. |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4054 | unsigned getNumPositiveBits() const { return EnumDeclBits.NumPositiveBits; } |
John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 4055 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4056 | /// Returns the width in bits required to store all the |
John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 4057 | /// negative enumerators of this enum. These widths include |
| 4058 | /// the rightmost leading 1; that is: |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 4059 | /// |
John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 4060 | /// MOST NEGATIVE ENUMERATOR PATTERN NUM NEGATIVE BITS |
| 4061 | /// ------------------------ ------- ----------------- |
| 4062 | /// -1 1111111 1 |
| 4063 | /// -10 1110110 5 |
| 4064 | /// -101 1001011 8 |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4065 | unsigned getNumNegativeBits() const { return EnumDeclBits.NumNegativeBits; } |
John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 4066 | |
Shafik Yaghmour | b3645353 | 2022-07-28 15:26:15 -0700 | [diff] [blame] | 4067 | /// Calculates the [Min,Max) values the enum can store based on the |
| 4068 | /// NumPositiveBits and NumNegativeBits. This matters for enums that do not |
| 4069 | /// have a fixed underlying type. |
| 4070 | void getValueRange(llvm::APInt &Max, llvm::APInt &Min) const; |
| 4071 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4072 | /// Returns true if this is a C++11 scoped enumeration. |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4073 | bool isScoped() const { return EnumDeclBits.IsScoped; } |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4074 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4075 | /// Returns true if this is a C++11 scoped enumeration. |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 4076 | bool isScopedUsingClassTag() const { |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4077 | return EnumDeclBits.IsScopedUsingClassTag; |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 4078 | } |
| 4079 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4080 | /// Returns true if this is an Objective-C, C++11, or |
Adrian Prantl | c60dc71 | 2013-04-19 19:56:39 +0000 | [diff] [blame] | 4081 | /// Microsoft-style enumeration with a fixed underlying type. |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4082 | bool isFixed() const { return EnumDeclBits.IsFixed; } |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4083 | |
Richard Trieu | ab4d730 | 2018-07-25 22:52:05 +0000 | [diff] [blame] | 4084 | unsigned getODRHash(); |
| 4085 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4086 | /// Returns true if this can be considered a complete type. |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4087 | bool isComplete() const { |
Reid Kleckner | b0a17ed | 2018-02-12 17:37:06 +0000 | [diff] [blame] | 4088 | // IntegerType is set for fixed type enums and non-fixed but implicitly |
| 4089 | // int-sized Microsoft enums. |
| 4090 | return isCompleteDefinition() || IntegerType; |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4091 | } |
| 4092 | |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 4093 | /// Returns true if this enum is either annotated with |
| 4094 | /// enum_extensibility(closed) or isn't annotated with enum_extensibility. |
| 4095 | bool isClosed() const; |
| 4096 | |
| 4097 | /// Returns true if this enum is annotated with flag_enum and isn't annotated |
| 4098 | /// with enum_extensibility(open). |
| 4099 | bool isClosedFlag() const; |
| 4100 | |
| 4101 | /// Returns true if this enum is annotated with neither flag_enum nor |
| 4102 | /// enum_extensibility(open). |
| 4103 | bool isClosedNonFlag() const; |
| 4104 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4105 | /// Retrieve the enum definition from which this enumeration could |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4106 | /// be instantiated, if it is an instantiation (rather than a non-template). |
| 4107 | EnumDecl *getTemplateInstantiationPattern() const; |
| 4108 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4109 | /// Returns the enumeration (declared within the template) |
Douglas Gregor | 7a74938 | 2009-05-27 17:20:35 +0000 | [diff] [blame] | 4110 | /// from which this enumeration type was instantiated, or NULL if |
| 4111 | /// this enumeration was not instantiated from any template. |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 4112 | EnumDecl *getInstantiatedFromMemberEnum() const; |
| 4113 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4114 | /// If this enumeration is a member of a specialization of a |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4115 | /// templated class, determine what kind of template specialization |
| 4116 | /// or instantiation this is. |
| 4117 | TemplateSpecializationKind getTemplateSpecializationKind() const; |
| 4118 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4119 | /// For an enumeration member that was instantiated from a member |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4120 | /// enumeration of a templated class, set the template specialiation kind. |
| 4121 | void setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 4122 | SourceLocation PointOfInstantiation = SourceLocation()); |
| 4123 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4124 | /// If this enumeration is an instantiation of a member enumeration of |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 4125 | /// a class template specialization, retrieves the member specialization |
| 4126 | /// information. |
| 4127 | MemberSpecializationInfo *getMemberSpecializationInfo() const { |
| 4128 | return SpecializationInfo; |
Douglas Gregor | 7a74938 | 2009-05-27 17:20:35 +0000 | [diff] [blame] | 4129 | } |
| 4130 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4131 | /// Specify that this enumeration is an instantiation of the |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 4132 | /// member enumeration ED. |
| 4133 | void setInstantiationOfMemberEnum(EnumDecl *ED, |
| 4134 | TemplateSpecializationKind TSK) { |
| 4135 | setInstantiationOfMemberEnum(getASTContext(), ED, TSK); |
| 4136 | } |
Douglas Gregor | 7a74938 | 2009-05-27 17:20:35 +0000 | [diff] [blame] | 4137 | |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 4138 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 4139 | static bool classofKind(Kind K) { return K == Enum; } |
Chris Lattner | 5f52150 | 2007-01-25 06:27:24 +0000 | [diff] [blame] | 4140 | }; |
| 4141 | |
Vlad Serebrennikov | 24228ae | 2023-11-01 19:47:06 +0400 | [diff] [blame] | 4142 | /// Enum that represents the different ways arguments are passed to and |
| 4143 | /// returned from function calls. This takes into account the target-specific |
| 4144 | /// and version-specific rules along with the rules determined by the |
| 4145 | /// language. |
Vlad Serebrennikov | a8ead56 | 2023-11-01 20:38:28 +0400 | [diff] [blame] | 4146 | enum class RecordArgPassingKind { |
Vlad Serebrennikov | 24228ae | 2023-11-01 19:47:06 +0400 | [diff] [blame] | 4147 | /// The argument of this type can be passed directly in registers. |
| 4148 | CanPassInRegs, |
| 4149 | |
| 4150 | /// The argument of this type cannot be passed directly in registers. |
| 4151 | /// Records containing this type as a subobject are not forced to be passed |
| 4152 | /// indirectly. This value is used only in C++. This value is required by |
| 4153 | /// C++ because, in uncommon situations, it is possible for a class to have |
| 4154 | /// only trivial copy/move constructors even when one of its subobjects has |
| 4155 | /// a non-trivial copy/move constructor (if e.g. the corresponding copy/move |
| 4156 | /// constructor in the derived class is deleted). |
| 4157 | CannotPassInRegs, |
| 4158 | |
| 4159 | /// The argument of this type cannot be passed directly in registers. |
| 4160 | /// Records containing this type as a subobject are forced to be passed |
| 4161 | /// indirectly. |
| 4162 | CanNeverPassInRegs |
| 4163 | }; |
| 4164 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 4165 | /// Represents a struct/union/class. For example: |
Chris Lattner | 90c26ba | 2007-09-30 08:13:22 +0000 | [diff] [blame] | 4166 | /// struct X; // Forward declaration, no "body". |
| 4167 | /// union Y { int A, B; }; // Has body with members A and B (FieldDecls). |
Steve Naroff | b5fc255 | 2008-02-11 21:52:37 +0000 | [diff] [blame] | 4168 | /// This decl will be marked invalid if *any* members are invalid. |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 4169 | class RecordDecl : public TagDecl { |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4170 | // This class stores some data in DeclContext::RecordDeclBits |
| 4171 | // to save some space. Use the provided accessors to access it. |
Akira Hatanaka | e6313ac | 2018-04-09 22:48:22 +0000 | [diff] [blame] | 4172 | public: |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4173 | friend class DeclContext; |
Volodymyr Sapsai | 160bc16 | 2022-12-01 18:39:23 -0800 | [diff] [blame] | 4174 | friend class ASTDeclReader; |
Akira Hatanaka | e6313ac | 2018-04-09 22:48:22 +0000 | [diff] [blame] | 4175 | |
Argyrios Kyrtzidis | 2951e14 | 2008-06-09 21:05:31 +0000 | [diff] [blame] | 4176 | protected: |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4177 | RecordDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4178 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 4179 | IdentifierInfo *Id, RecordDecl *PrevDecl); |
Argyrios Kyrtzidis | 6bd44af | 2008-08-08 14:08:55 +0000 | [diff] [blame] | 4180 | |
Chris Lattner | a7b3287 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 4181 | public: |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4182 | static RecordDecl *Create(const ASTContext &C, TagKind TK, DeclContext *DC, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4183 | SourceLocation StartLoc, SourceLocation IdLoc, |
Craig Topper | 34b4c43 | 2014-05-06 06:48:52 +0000 | [diff] [blame] | 4184 | IdentifierInfo *Id, RecordDecl* PrevDecl = nullptr); |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 4185 | static RecordDecl *CreateDeserialized(const ASTContext &C, GlobalDeclID ID); |
Argyrios Kyrtzidis | 69bc5ba | 2008-08-08 22:25:06 +0000 | [diff] [blame] | 4186 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 4187 | RecordDecl *getPreviousDecl() { |
Aaron Ballman | 5116a8e | 2013-11-06 22:39:46 +0000 | [diff] [blame] | 4188 | return cast_or_null<RecordDecl>( |
| 4189 | static_cast<TagDecl *>(this)->getPreviousDecl()); |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 4190 | } |
Rafael Espindola | 7b56f6c | 2013-10-19 16:55:03 +0000 | [diff] [blame] | 4191 | const RecordDecl *getPreviousDecl() const { |
| 4192 | return const_cast<RecordDecl*>(this)->getPreviousDecl(); |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 4193 | } |
Rafael Espindola | 7b56f6c | 2013-10-19 16:55:03 +0000 | [diff] [blame] | 4194 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 4195 | RecordDecl *getMostRecentDecl() { |
Aaron Ballman | 5116a8e | 2013-11-06 22:39:46 +0000 | [diff] [blame] | 4196 | return cast<RecordDecl>(static_cast<TagDecl *>(this)->getMostRecentDecl()); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 4197 | } |
Rafael Espindola | 7b56f6c | 2013-10-19 16:55:03 +0000 | [diff] [blame] | 4198 | const RecordDecl *getMostRecentDecl() const { |
| 4199 | return const_cast<RecordDecl*>(this)->getMostRecentDecl(); |
| 4200 | } |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 4201 | |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4202 | bool hasFlexibleArrayMember() const { |
| 4203 | return RecordDeclBits.HasFlexibleArrayMember; |
| 4204 | } |
| 4205 | |
| 4206 | void setHasFlexibleArrayMember(bool V) { |
| 4207 | RecordDeclBits.HasFlexibleArrayMember = V; |
| 4208 | } |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 4209 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 4210 | /// Whether this is an anonymous struct or union. To be an anonymous |
| 4211 | /// struct or union, it must have been declared without a name and |
| 4212 | /// there must be no objects of this type declared, e.g., |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 4213 | /// @code |
| 4214 | /// union { int i; float f; }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4215 | /// @endcode |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 4216 | /// is an anonymous union but neither of the following are: |
| 4217 | /// @code |
| 4218 | /// union X { int i; float f; }; |
| 4219 | /// union { int i; float f; } obj; |
| 4220 | /// @endcode |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4221 | bool isAnonymousStructOrUnion() const { |
| 4222 | return RecordDeclBits.AnonymousStructOrUnion; |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 4223 | } |
| 4224 | |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4225 | void setAnonymousStructOrUnion(bool Anon) { |
| 4226 | RecordDeclBits.AnonymousStructOrUnion = Anon; |
| 4227 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4228 | |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4229 | bool hasObjectMember() const { return RecordDeclBits.HasObjectMember; } |
| 4230 | void setHasObjectMember(bool val) { RecordDeclBits.HasObjectMember = val; } |
| 4231 | |
| 4232 | bool hasVolatileMember() const { return RecordDeclBits.HasVolatileMember; } |
| 4233 | |
| 4234 | void setHasVolatileMember(bool val) { |
| 4235 | RecordDeclBits.HasVolatileMember = val; |
| 4236 | } |
Adrian Prantl | 354bebd | 2015-12-01 19:54:07 +0000 | [diff] [blame] | 4237 | |
Adrian Prantl | d6ec3bc | 2015-12-01 20:19:44 +0000 | [diff] [blame] | 4238 | bool hasLoadedFieldsFromExternalStorage() const { |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4239 | return RecordDeclBits.LoadedFieldsFromExternalStorage; |
Adrian Prantl | d6ec3bc | 2015-12-01 20:19:44 +0000 | [diff] [blame] | 4240 | } |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4241 | |
| 4242 | void setHasLoadedFieldsFromExternalStorage(bool val) const { |
| 4243 | RecordDeclBits.LoadedFieldsFromExternalStorage = val; |
Adrian Prantl | d6ec3bc | 2015-12-01 20:19:44 +0000 | [diff] [blame] | 4244 | } |
Adrian Prantl | 354bebd | 2015-12-01 19:54:07 +0000 | [diff] [blame] | 4245 | |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 4246 | /// Functions to query basic properties of non-trivial C structs. |
| 4247 | bool isNonTrivialToPrimitiveDefaultInitialize() const { |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4248 | return RecordDeclBits.NonTrivialToPrimitiveDefaultInitialize; |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
Akira Hatanaka | 34fb264 | 2018-03-13 18:58:25 +0000 | [diff] [blame] | 4251 | void setNonTrivialToPrimitiveDefaultInitialize(bool V) { |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4252 | RecordDeclBits.NonTrivialToPrimitiveDefaultInitialize = V; |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 4253 | } |
| 4254 | |
| 4255 | bool isNonTrivialToPrimitiveCopy() const { |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4256 | return RecordDeclBits.NonTrivialToPrimitiveCopy; |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 4257 | } |
| 4258 | |
Akira Hatanaka | 34fb264 | 2018-03-13 18:58:25 +0000 | [diff] [blame] | 4259 | void setNonTrivialToPrimitiveCopy(bool V) { |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4260 | RecordDeclBits.NonTrivialToPrimitiveCopy = V; |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 4261 | } |
| 4262 | |
| 4263 | bool isNonTrivialToPrimitiveDestroy() const { |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4264 | return RecordDeclBits.NonTrivialToPrimitiveDestroy; |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 4265 | } |
| 4266 | |
Akira Hatanaka | 34fb264 | 2018-03-13 18:58:25 +0000 | [diff] [blame] | 4267 | void setNonTrivialToPrimitiveDestroy(bool V) { |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4268 | RecordDeclBits.NonTrivialToPrimitiveDestroy = V; |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 4269 | } |
| 4270 | |
Akira Hatanaka | 0905106 | 2019-09-07 00:34:43 +0000 | [diff] [blame] | 4271 | bool hasNonTrivialToPrimitiveDefaultInitializeCUnion() const { |
| 4272 | return RecordDeclBits.HasNonTrivialToPrimitiveDefaultInitializeCUnion; |
| 4273 | } |
| 4274 | |
| 4275 | void setHasNonTrivialToPrimitiveDefaultInitializeCUnion(bool V) { |
| 4276 | RecordDeclBits.HasNonTrivialToPrimitiveDefaultInitializeCUnion = V; |
| 4277 | } |
| 4278 | |
| 4279 | bool hasNonTrivialToPrimitiveDestructCUnion() const { |
| 4280 | return RecordDeclBits.HasNonTrivialToPrimitiveDestructCUnion; |
| 4281 | } |
| 4282 | |
| 4283 | void setHasNonTrivialToPrimitiveDestructCUnion(bool V) { |
| 4284 | RecordDeclBits.HasNonTrivialToPrimitiveDestructCUnion = V; |
| 4285 | } |
| 4286 | |
| 4287 | bool hasNonTrivialToPrimitiveCopyCUnion() const { |
| 4288 | return RecordDeclBits.HasNonTrivialToPrimitiveCopyCUnion; |
| 4289 | } |
| 4290 | |
| 4291 | void setHasNonTrivialToPrimitiveCopyCUnion(bool V) { |
| 4292 | RecordDeclBits.HasNonTrivialToPrimitiveCopyCUnion = V; |
| 4293 | } |
| 4294 | |
higher-performance | 1594413 | 2025-01-14 13:31:12 -0500 | [diff] [blame] | 4295 | bool hasUninitializedExplicitInitFields() const { |
| 4296 | return RecordDeclBits.HasUninitializedExplicitInitFields; |
| 4297 | } |
| 4298 | |
| 4299 | void setHasUninitializedExplicitInitFields(bool V) { |
| 4300 | RecordDeclBits.HasUninitializedExplicitInitFields = V; |
| 4301 | } |
| 4302 | |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 4303 | /// Determine whether this class can be passed in registers. In C++ mode, |
| 4304 | /// it must have at least one trivial, non-deleted copy or move constructor. |
| 4305 | /// FIXME: This should be set as part of completeDefinition. |
| 4306 | bool canPassInRegisters() const { |
Vlad Serebrennikov | a8ead56 | 2023-11-01 20:38:28 +0400 | [diff] [blame] | 4307 | return getArgPassingRestrictions() == RecordArgPassingKind::CanPassInRegs; |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
Vlad Serebrennikov | a8ead56 | 2023-11-01 20:38:28 +0400 | [diff] [blame] | 4310 | RecordArgPassingKind getArgPassingRestrictions() const { |
| 4311 | return static_cast<RecordArgPassingKind>( |
| 4312 | RecordDeclBits.ArgPassingRestrictions); |
Akira Hatanaka | e6313ac | 2018-04-09 22:48:22 +0000 | [diff] [blame] | 4313 | } |
| 4314 | |
Vlad Serebrennikov | a8ead56 | 2023-11-01 20:38:28 +0400 | [diff] [blame] | 4315 | void setArgPassingRestrictions(RecordArgPassingKind Kind) { |
Vlad Serebrennikov | b120fe8 | 2023-11-01 11:47:40 +0300 | [diff] [blame] | 4316 | RecordDeclBits.ArgPassingRestrictions = llvm::to_underlying(Kind); |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 4317 | } |
| 4318 | |
Akira Hatanaka | fcbe17c | 2018-03-28 21:13:14 +0000 | [diff] [blame] | 4319 | bool isParamDestroyedInCallee() const { |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4320 | return RecordDeclBits.ParamDestroyedInCallee; |
Akira Hatanaka | fcbe17c | 2018-03-28 21:13:14 +0000 | [diff] [blame] | 4321 | } |
| 4322 | |
| 4323 | void setParamDestroyedInCallee(bool V) { |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4324 | RecordDeclBits.ParamDestroyedInCallee = V; |
Akira Hatanaka | fcbe17c | 2018-03-28 21:13:14 +0000 | [diff] [blame] | 4325 | } |
| 4326 | |
Connor Kuehl | 7aa8c38 | 2022-04-08 23:36:51 -0700 | [diff] [blame] | 4327 | bool isRandomized() const { return RecordDeclBits.IsRandomized; } |
| 4328 | |
| 4329 | void setIsRandomized(bool V) { RecordDeclBits.IsRandomized = V; } |
| 4330 | |
Bill Wendling | 463790b | 2022-04-28 12:00:47 -0700 | [diff] [blame] | 4331 | void reorderDecls(const SmallVectorImpl<Decl *> &Decls); |
Connor Kuehl | 7aa8c38 | 2022-04-08 23:36:51 -0700 | [diff] [blame] | 4332 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4333 | /// Determines whether this declaration represents the |
Douglas Gregor | dfcad11 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 4334 | /// injected class name. |
| 4335 | /// |
| 4336 | /// The injected class name in C++ is the name of the class that |
| 4337 | /// appears inside the class itself. For example: |
| 4338 | /// |
| 4339 | /// \code |
| 4340 | /// struct C { |
| 4341 | /// // C is implicitly declared here as a synonym for the class name. |
| 4342 | /// }; |
| 4343 | /// |
| 4344 | /// C::C c; // same as "C c;" |
| 4345 | /// \endcode |
| 4346 | bool isInjectedClassName() const; |
| 4347 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4348 | /// Determine whether this record is a class describing a lambda |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 4349 | /// function object. |
| 4350 | bool isLambda() const; |
| 4351 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4352 | /// Determine whether this record is a record for captured variables in |
Alexey Bataev | 330de03 | 2014-10-29 12:21:55 +0000 | [diff] [blame] | 4353 | /// CapturedStmt construct. |
| 4354 | bool isCapturedRecord() const; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4355 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4356 | /// Mark the record as a record for captured variables in CapturedStmt |
Alexey Bataev | 330de03 | 2014-10-29 12:21:55 +0000 | [diff] [blame] | 4357 | /// construct. |
| 4358 | void setCapturedRecord(); |
| 4359 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 4360 | /// Returns the RecordDecl that actually defines |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4361 | /// this struct/union/class. When determining whether or not a |
| 4362 | /// struct/union/class is completely defined, one should use this |
| 4363 | /// method as opposed to 'isCompleteDefinition'. |
| 4364 | /// 'isCompleteDefinition' indicates whether or not a specific |
| 4365 | /// RecordDecl is a completed definition, not whether or not the |
| 4366 | /// record type is defined. This method returns NULL if there is |
| 4367 | /// no RecordDecl that defines the struct/union/tag. |
| 4368 | RecordDecl *getDefinition() const { |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4369 | return cast_or_null<RecordDecl>(TagDecl::getDefinition()); |
Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 4370 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4371 | |
Momchil Velikov | 102b410 | 2020-04-28 16:27:52 +0100 | [diff] [blame] | 4372 | /// Returns whether this record is a union, or contains (at any nesting level) |
| 4373 | /// a union member. This is used by CMSE to warn about possible information |
| 4374 | /// leaks. |
| 4375 | bool isOrContainsUnion() const; |
| 4376 | |
Douglas Gregor | e029561 | 2008-12-11 17:59:21 +0000 | [diff] [blame] | 4377 | // Iterator access to field members. The field iterator only visits |
| 4378 | // the non-static data members of this class, ignoring any static |
| 4379 | // data members, functions, constructors, destructors, etc. |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4380 | using field_iterator = specific_decl_iterator<FieldDecl>; |
| 4381 | using field_range = llvm::iterator_range<specific_decl_iterator<FieldDecl>>; |
Argyrios Kyrtzidis | cd5f3bd | 2008-08-08 13:54:06 +0000 | [diff] [blame] | 4382 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4383 | field_range fields() const { return field_range(field_begin(), field_end()); } |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4384 | field_iterator field_begin() const; |
| 4385 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4386 | field_iterator field_end() const { |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4387 | return field_iterator(decl_iterator()); |
Argyrios Kyrtzidis | cd5f3bd | 2008-08-08 13:54:06 +0000 | [diff] [blame] | 4388 | } |
| 4389 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 4390 | // Whether there are any fields (non-static data members) in this record. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4391 | bool field_empty() const { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4392 | return field_begin() == field_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4393 | } |
Douglas Gregor | 7a4fad1 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 4394 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 4395 | /// Note that the definition of this type is now complete. |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 4396 | virtual void completeDefinition(); |
Steve Naroff | cc32142 | 2007-03-26 23:09:51 +0000 | [diff] [blame] | 4397 | |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 4398 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 4399 | static bool classofKind(Kind K) { |
Alexis Hunt | ed05325 | 2010-05-30 07:21:58 +0000 | [diff] [blame] | 4400 | return K >= firstRecord && K <= lastRecord; |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 4401 | } |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4402 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4403 | /// Get whether or not this is an ms_struct which can |
Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 4404 | /// be turned on with an attribute, pragma, or -mms-bitfields |
| 4405 | /// commandline option. |
| 4406 | bool isMsStruct(const ASTContext &C) const; |
| 4407 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4408 | /// Whether we are allowed to insert extra padding between fields. |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 4409 | /// These padding are added to help AddressSanitizer detect |
| 4410 | /// intra-object-overflow bugs. |
| 4411 | bool mayInsertExtraPadding(bool EmitRemark = false) const; |
| 4412 | |
Evgeny Astigeevich | 665027d | 2014-12-12 16:17:46 +0000 | [diff] [blame] | 4413 | /// Finds the first data member which has a name. |
| 4414 | /// nullptr is returned if no named data member exists. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4415 | const FieldDecl *findFirstNamedDataMember() const; |
Evgeny Astigeevich | 665027d | 2014-12-12 16:17:46 +0000 | [diff] [blame] | 4416 | |
Volodymyr Sapsai | 160bc16 | 2022-12-01 18:39:23 -0800 | [diff] [blame] | 4417 | /// Get precomputed ODRHash or add a new one. |
| 4418 | unsigned getODRHash(); |
| 4419 | |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4420 | private: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4421 | /// Deserialize just the fields. |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4422 | void LoadFieldsFromExternalStorage() const; |
Volodymyr Sapsai | 160bc16 | 2022-12-01 18:39:23 -0800 | [diff] [blame] | 4423 | |
| 4424 | /// True if a valid hash is stored in ODRHash. |
| 4425 | bool hasODRHash() const { return RecordDeclBits.ODRHash; } |
| 4426 | void setODRHash(unsigned Hash) { RecordDeclBits.ODRHash = Hash; } |
Chris Lattner | 28743e2 | 2007-01-22 07:41:08 +0000 | [diff] [blame] | 4427 | }; |
| 4428 | |
Anders Carlsson | 5c6c059 | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 4429 | class FileScopeAsmDecl : public Decl { |
Anders Carlsson | 5c6c059 | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 4430 | StringLiteral *AsmString; |
Abramo Bagnara | 348823a | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 4431 | SourceLocation RParenLoc; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4432 | |
Abramo Bagnara | 348823a | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 4433 | FileScopeAsmDecl(DeclContext *DC, StringLiteral *asmstring, |
| 4434 | SourceLocation StartL, SourceLocation EndL) |
| 4435 | : Decl(FileScopeAsm, DC, StartL), AsmString(asmstring), RParenLoc(EndL) {} |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4436 | |
| 4437 | virtual void anchor(); |
| 4438 | |
Chris Lattner | ee1284a | 2008-03-16 00:16:02 +0000 | [diff] [blame] | 4439 | public: |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 4440 | static FileScopeAsmDecl *Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 348823a | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 4441 | StringLiteral *Str, SourceLocation AsmLoc, |
| 4442 | SourceLocation RParenLoc); |
| 4443 | |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 4444 | static FileScopeAsmDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4445 | |
Abramo Bagnara | 348823a | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 4446 | SourceLocation getAsmLoc() const { return getLocation(); } |
| 4447 | SourceLocation getRParenLoc() const { return RParenLoc; } |
| 4448 | void setRParenLoc(SourceLocation L) { RParenLoc = L; } |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 4449 | SourceRange getSourceRange() const override LLVM_READONLY { |
Abramo Bagnara | 348823a | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 4450 | return SourceRange(getAsmLoc(), getRParenLoc()); |
| 4451 | } |
Anders Carlsson | 5c6c059 | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 4452 | |
| 4453 | const StringLiteral *getAsmString() const { return AsmString; } |
| 4454 | StringLiteral *getAsmString() { return AsmString; } |
Douglas Gregor | a541485 | 2009-04-13 22:49:25 +0000 | [diff] [blame] | 4455 | void setAsmString(StringLiteral *Asm) { AsmString = Asm; } |
| 4456 | |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 4457 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 4458 | static bool classofKind(Kind K) { return K == FileScopeAsm; } |
Anders Carlsson | 5c6c059 | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 4459 | }; |
Chris Lattner | 38376f1 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 4460 | |
Vassil Vassilev | dc48893 | 2022-06-08 09:59:40 +0000 | [diff] [blame] | 4461 | /// A declaration that models statements at global scope. This declaration |
| 4462 | /// supports incremental and interactive C/C++. |
| 4463 | /// |
| 4464 | /// \note This is used in libInterpreter, clang -cc1 -fincremental-extensions |
| 4465 | /// and in tools such as clang-repl. |
Stefan Gränitz | 4b70d17 | 2024-03-07 14:27:04 +0100 | [diff] [blame] | 4466 | class TopLevelStmtDecl : public Decl, public DeclContext { |
Vassil Vassilev | dc48893 | 2022-06-08 09:59:40 +0000 | [diff] [blame] | 4467 | friend class ASTDeclReader; |
| 4468 | friend class ASTDeclWriter; |
| 4469 | |
| 4470 | Stmt *Statement = nullptr; |
Jun Zhang | 247fa04 | 2023-05-16 20:10:43 +0800 | [diff] [blame] | 4471 | bool IsSemiMissing = false; |
Vassil Vassilev | dc48893 | 2022-06-08 09:59:40 +0000 | [diff] [blame] | 4472 | |
| 4473 | TopLevelStmtDecl(DeclContext *DC, SourceLocation L, Stmt *S) |
Stefan Gränitz | 4b70d17 | 2024-03-07 14:27:04 +0100 | [diff] [blame] | 4474 | : Decl(TopLevelStmt, DC, L), DeclContext(TopLevelStmt), Statement(S) {} |
Vassil Vassilev | dc48893 | 2022-06-08 09:59:40 +0000 | [diff] [blame] | 4475 | |
| 4476 | virtual void anchor(); |
| 4477 | |
| 4478 | public: |
| 4479 | static TopLevelStmtDecl *Create(ASTContext &C, Stmt *Statement); |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 4480 | static TopLevelStmtDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); |
Vassil Vassilev | dc48893 | 2022-06-08 09:59:40 +0000 | [diff] [blame] | 4481 | |
| 4482 | SourceRange getSourceRange() const override LLVM_READONLY; |
| 4483 | Stmt *getStmt() { return Statement; } |
| 4484 | const Stmt *getStmt() const { return Statement; } |
Stefan Gränitz | 4b70d17 | 2024-03-07 14:27:04 +0100 | [diff] [blame] | 4485 | void setStmt(Stmt *S); |
Jun Zhang | 247fa04 | 2023-05-16 20:10:43 +0800 | [diff] [blame] | 4486 | bool isSemiMissing() const { return IsSemiMissing; } |
| 4487 | void setSemiMissing(bool Missing = true) { IsSemiMissing = Missing; } |
Vassil Vassilev | dc48893 | 2022-06-08 09:59:40 +0000 | [diff] [blame] | 4488 | |
| 4489 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 4490 | static bool classofKind(Kind K) { return K == TopLevelStmt; } |
Stefan Gränitz | 4b70d17 | 2024-03-07 14:27:04 +0100 | [diff] [blame] | 4491 | |
| 4492 | static DeclContext *castToDeclContext(const TopLevelStmtDecl *D) { |
| 4493 | return static_cast<DeclContext *>(const_cast<TopLevelStmtDecl *>(D)); |
| 4494 | } |
| 4495 | static TopLevelStmtDecl *castFromDeclContext(const DeclContext *DC) { |
| 4496 | return static_cast<TopLevelStmtDecl *>(const_cast<DeclContext *>(DC)); |
| 4497 | } |
Vassil Vassilev | dc48893 | 2022-06-08 09:59:40 +0000 | [diff] [blame] | 4498 | }; |
| 4499 | |
JF Bastien | 0919777 | 2019-02-12 20:19:16 +0000 | [diff] [blame] | 4500 | /// Represents a block literal declaration, which is like an |
Steve Naroff | 415d3d5 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 4501 | /// unnamed FunctionDecl. For example: |
| 4502 | /// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } |
Steve Naroff | 415d3d5 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 4503 | class BlockDecl : public Decl, public DeclContext { |
Erich Keane | c9d2990 | 2018-08-01 21:16:54 +0000 | [diff] [blame] | 4504 | // This class stores some data in DeclContext::BlockDeclBits |
| 4505 | // to save some space. Use the provided accessors to access it. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4506 | public: |
| 4507 | /// A class which contains all the information about a particular |
| 4508 | /// captured value. |
| 4509 | class Capture { |
| 4510 | enum { |
| 4511 | flag_isByRef = 0x1, |
| 4512 | flag_isNested = 0x2 |
| 4513 | }; |
| 4514 | |
| 4515 | /// The variable being captured. |
| 4516 | llvm::PointerIntPair<VarDecl*, 2> VariableAndFlags; |
| 4517 | |
| 4518 | /// The copy expression, expressed in terms of a DeclRef (or |
| 4519 | /// BlockDeclRef) to the captured variable. Only required if the |
| 4520 | /// variable has a C++ class type. |
| 4521 | Expr *CopyExpr; |
| 4522 | |
| 4523 | public: |
| 4524 | Capture(VarDecl *variable, bool byRef, bool nested, Expr *copy) |
| 4525 | : VariableAndFlags(variable, |
| 4526 | (byRef ? flag_isByRef : 0) | (nested ? flag_isNested : 0)), |
| 4527 | CopyExpr(copy) {} |
| 4528 | |
| 4529 | /// The variable being captured. |
| 4530 | VarDecl *getVariable() const { return VariableAndFlags.getPointer(); } |
| 4531 | |
| 4532 | /// Whether this is a "by ref" capture, i.e. a capture of a __block |
| 4533 | /// variable. |
| 4534 | bool isByRef() const { return VariableAndFlags.getInt() & flag_isByRef; } |
| 4535 | |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 4536 | bool isEscapingByref() const { |
| 4537 | return getVariable()->isEscapingByref(); |
| 4538 | } |
| 4539 | |
| 4540 | bool isNonEscapingByref() const { |
| 4541 | return getVariable()->isNonEscapingByref(); |
| 4542 | } |
| 4543 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4544 | /// Whether this is a nested capture, i.e. the variable captured |
| 4545 | /// is not from outside the immediately enclosing function/block. |
| 4546 | bool isNested() const { return VariableAndFlags.getInt() & flag_isNested; } |
| 4547 | |
Craig Topper | 34b4c43 | 2014-05-06 06:48:52 +0000 | [diff] [blame] | 4548 | bool hasCopyExpr() const { return CopyExpr != nullptr; } |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4549 | Expr *getCopyExpr() const { return CopyExpr; } |
| 4550 | void setCopyExpr(Expr *e) { CopyExpr = e; } |
| 4551 | }; |
| 4552 | |
| 4553 | private: |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 4554 | /// A new[]'d array of pointers to ParmVarDecls for the formal |
Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 4555 | /// parameters of this function. This is null if a prototype or if there are |
| 4556 | /// no formals. |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4557 | ParmVarDecl **ParamInfo = nullptr; |
| 4558 | unsigned NumParams = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4559 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4560 | Stmt *Body = nullptr; |
| 4561 | TypeSourceInfo *SignatureAsWritten = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4562 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4563 | const Capture *Captures = nullptr; |
| 4564 | unsigned NumCaptures = 0; |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 4565 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4566 | unsigned ManglingNumber = 0; |
| 4567 | Decl *ManglingContextDecl = nullptr; |
Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 4568 | |
Steve Naroff | 415d3d5 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 4569 | protected: |
Erich Keane | c9d2990 | 2018-08-01 21:16:54 +0000 | [diff] [blame] | 4570 | BlockDecl(DeclContext *DC, SourceLocation CaretLoc); |
Ted Kremenek | 1f1e756 | 2007-10-25 21:37:16 +0000 | [diff] [blame] | 4571 | |
Steve Naroff | 415d3d5 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 4572 | public: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4573 | static BlockDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L); |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 4574 | static BlockDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4575 | |
Steve Naroff | 415d3d5 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 4576 | SourceLocation getCaretLocation() const { return getLocation(); } |
| 4577 | |
Erich Keane | c9d2990 | 2018-08-01 21:16:54 +0000 | [diff] [blame] | 4578 | bool isVariadic() const { return BlockDeclBits.IsVariadic; } |
| 4579 | void setIsVariadic(bool value) { BlockDeclBits.IsVariadic = value; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4580 | |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 4581 | CompoundStmt *getCompoundBody() const { return (CompoundStmt*) Body; } |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 4582 | Stmt *getBody() const override { return (Stmt*) Body; } |
Ted Kremenek | 7398059 | 2009-03-12 18:33:24 +0000 | [diff] [blame] | 4583 | void setBody(CompoundStmt *B) { Body = (Stmt*) B; } |
Steve Naroff | 415d3d5 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 4584 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 4585 | void setSignatureAsWritten(TypeSourceInfo *Sig) { SignatureAsWritten = Sig; } |
| 4586 | TypeSourceInfo *getSignatureAsWritten() const { return SignatureAsWritten; } |
| 4587 | |
Ted Kremenek | 81541c5 | 2014-01-17 07:15:26 +0000 | [diff] [blame] | 4588 | // ArrayRef access to formal parameters. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 4589 | ArrayRef<ParmVarDecl *> parameters() const { |
| 4590 | return {ParamInfo, getNumParams()}; |
| 4591 | } |
| 4592 | MutableArrayRef<ParmVarDecl *> parameters() { |
| 4593 | return {ParamInfo, getNumParams()}; |
Ted Kremenek | 81541c5 | 2014-01-17 07:15:26 +0000 | [diff] [blame] | 4594 | } |
| 4595 | |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 4596 | // Iterator access to formal parameters. |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4597 | using param_iterator = MutableArrayRef<ParmVarDecl *>::iterator; |
| 4598 | using param_const_iterator = ArrayRef<ParmVarDecl *>::const_iterator; |
| 4599 | |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 4600 | bool param_empty() const { return parameters().empty(); } |
| 4601 | param_iterator param_begin() { return parameters().begin(); } |
| 4602 | param_iterator param_end() { return parameters().end(); } |
| 4603 | param_const_iterator param_begin() const { return parameters().begin(); } |
| 4604 | param_const_iterator param_end() const { return parameters().end(); } |
| 4605 | size_t param_size() const { return parameters().size(); } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4606 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 4607 | unsigned getNumParams() const { return NumParams; } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4608 | |
Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 4609 | const ParmVarDecl *getParamDecl(unsigned i) const { |
| 4610 | assert(i < getNumParams() && "Illegal param #"); |
| 4611 | return ParamInfo[i]; |
| 4612 | } |
| 4613 | ParmVarDecl *getParamDecl(unsigned i) { |
| 4614 | assert(i < getNumParams() && "Illegal param #"); |
| 4615 | return ParamInfo[i]; |
| 4616 | } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4617 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4618 | void setParams(ArrayRef<ParmVarDecl *> NewParamInfo); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4619 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 4620 | /// True if this block (or its nested blocks) captures |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 4621 | /// anything of local storage from its enclosing scopes. |
Erich Keane | c9d2990 | 2018-08-01 21:16:54 +0000 | [diff] [blame] | 4622 | bool hasCaptures() const { return NumCaptures || capturesCXXThis(); } |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 4623 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 4624 | /// Returns the number of captured variables. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4625 | /// Does not include an entry for 'this'. |
| 4626 | unsigned getNumCaptures() const { return NumCaptures; } |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 4627 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4628 | using capture_const_iterator = ArrayRef<Capture>::const_iterator; |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 4629 | |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 4630 | ArrayRef<Capture> captures() const { return {Captures, NumCaptures}; } |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 4631 | |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 4632 | capture_const_iterator capture_begin() const { return captures().begin(); } |
| 4633 | capture_const_iterator capture_end() const { return captures().end(); } |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 4634 | |
Erich Keane | c9d2990 | 2018-08-01 21:16:54 +0000 | [diff] [blame] | 4635 | bool capturesCXXThis() const { return BlockDeclBits.CapturesCXXThis; } |
| 4636 | void setCapturesCXXThis(bool B = true) { BlockDeclBits.CapturesCXXThis = B; } |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 4637 | |
Erich Keane | c9d2990 | 2018-08-01 21:16:54 +0000 | [diff] [blame] | 4638 | bool blockMissingReturnType() const { |
| 4639 | return BlockDeclBits.BlockMissingReturnType; |
| 4640 | } |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 4641 | |
Erich Keane | c9d2990 | 2018-08-01 21:16:54 +0000 | [diff] [blame] | 4642 | void setBlockMissingReturnType(bool val = true) { |
| 4643 | BlockDeclBits.BlockMissingReturnType = val; |
| 4644 | } |
| 4645 | |
| 4646 | bool isConversionFromLambda() const { |
| 4647 | return BlockDeclBits.IsConversionFromLambda; |
| 4648 | } |
| 4649 | |
| 4650 | void setIsConversionFromLambda(bool val = true) { |
| 4651 | BlockDeclBits.IsConversionFromLambda = val; |
| 4652 | } |
| 4653 | |
| 4654 | bool doesNotEscape() const { return BlockDeclBits.DoesNotEscape; } |
| 4655 | void setDoesNotEscape(bool B = true) { BlockDeclBits.DoesNotEscape = B; } |
Akira Hatanaka | 627586b8 | 2018-03-02 01:53:15 +0000 | [diff] [blame] | 4656 | |
Akira Hatanaka | c5792aa | 2019-02-27 18:17:16 +0000 | [diff] [blame] | 4657 | bool canAvoidCopyToHeap() const { |
| 4658 | return BlockDeclBits.CanAvoidCopyToHeap; |
| 4659 | } |
| 4660 | void setCanAvoidCopyToHeap(bool B = true) { |
| 4661 | BlockDeclBits.CanAvoidCopyToHeap = B; |
| 4662 | } |
| 4663 | |
John McCall | ce45f88 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 4664 | bool capturesVariable(const VarDecl *var) const; |
| 4665 | |
Benjamin Kramer | b40e4af | 2015-08-05 09:40:35 +0000 | [diff] [blame] | 4666 | void setCaptures(ASTContext &Context, ArrayRef<Capture> Captures, |
| 4667 | bool CapturesCXXThis); |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 4668 | |
Michael Liao | c752f5b | 2019-10-10 04:16:52 +0000 | [diff] [blame] | 4669 | unsigned getBlockManglingNumber() const { return ManglingNumber; } |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4670 | |
Michael Liao | c752f5b | 2019-10-10 04:16:52 +0000 | [diff] [blame] | 4671 | Decl *getBlockManglingContextDecl() const { return ManglingContextDecl; } |
Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 4672 | |
| 4673 | void setBlockMangling(unsigned Number, Decl *Ctx) { |
| 4674 | ManglingNumber = Number; |
| 4675 | ManglingContextDecl = Ctx; |
| 4676 | } |
| 4677 | |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 4678 | SourceRange getSourceRange() const override LLVM_READONLY; |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 4679 | |
Doug Wyatt | f03cb00 | 2024-06-24 00:51:31 -1000 | [diff] [blame] | 4680 | FunctionEffectsRef getFunctionEffects() const { |
| 4681 | if (const TypeSourceInfo *TSI = getSignatureAsWritten()) |
| 4682 | if (const auto *FPT = TSI->getType()->getAs<FunctionProtoType>()) |
| 4683 | return FPT->getFunctionEffects(); |
| 4684 | return {}; |
| 4685 | } |
| 4686 | |
Steve Naroff | 415d3d5 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 4687 | // Implement isa/cast/dyncast/etc. |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 4688 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
John McCall | 180ef09 | 2010-01-29 01:45:37 +0000 | [diff] [blame] | 4689 | static bool classofKind(Kind K) { return K == Block; } |
Argyrios Kyrtzidis | 3768ad6 | 2008-10-12 16:14:48 +0000 | [diff] [blame] | 4690 | static DeclContext *castToDeclContext(const BlockDecl *D) { |
| 4691 | return static_cast<DeclContext *>(const_cast<BlockDecl*>(D)); |
| 4692 | } |
| 4693 | static BlockDecl *castFromDeclContext(const DeclContext *DC) { |
| 4694 | return static_cast<BlockDecl *>(const_cast<DeclContext*>(DC)); |
| 4695 | } |
Steve Naroff | 415d3d5 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 4696 | }; |
| 4697 | |
Tom Honermann | 8fb4230 | 2025-01-22 16:39:08 -0500 | [diff] [blame] | 4698 | /// Represents a partial function definition. |
| 4699 | /// |
| 4700 | /// An outlined function declaration contains the parameters and body of |
| 4701 | /// a function independent of other function definition concerns such |
| 4702 | /// as function name, type, and calling convention. Such declarations may |
| 4703 | /// be used to hold a parameterized and transformed sequence of statements |
| 4704 | /// used to generate a target dependent function definition without losing |
| 4705 | /// association with the original statements. See SYCLKernelCallStmt as an |
| 4706 | /// example. |
| 4707 | class OutlinedFunctionDecl final |
| 4708 | : public Decl, |
| 4709 | public DeclContext, |
| 4710 | private llvm::TrailingObjects<OutlinedFunctionDecl, ImplicitParamDecl *> { |
| 4711 | private: |
| 4712 | /// The number of parameters to the outlined function. |
| 4713 | unsigned NumParams; |
| 4714 | |
| 4715 | /// The body of the outlined function. |
| 4716 | llvm::PointerIntPair<Stmt *, 1, bool> BodyAndNothrow; |
| 4717 | |
| 4718 | explicit OutlinedFunctionDecl(DeclContext *DC, unsigned NumParams); |
| 4719 | |
| 4720 | ImplicitParamDecl *const *getParams() const { |
| 4721 | return getTrailingObjects<ImplicitParamDecl *>(); |
| 4722 | } |
| 4723 | |
| 4724 | ImplicitParamDecl **getParams() { |
| 4725 | return getTrailingObjects<ImplicitParamDecl *>(); |
| 4726 | } |
| 4727 | |
| 4728 | public: |
| 4729 | friend class ASTDeclReader; |
| 4730 | friend class ASTDeclWriter; |
| 4731 | friend TrailingObjects; |
| 4732 | |
| 4733 | static OutlinedFunctionDecl *Create(ASTContext &C, DeclContext *DC, |
| 4734 | unsigned NumParams); |
| 4735 | static OutlinedFunctionDecl * |
| 4736 | CreateDeserialized(ASTContext &C, GlobalDeclID ID, unsigned NumParams); |
| 4737 | |
| 4738 | Stmt *getBody() const override; |
| 4739 | void setBody(Stmt *B); |
| 4740 | |
| 4741 | bool isNothrow() const; |
| 4742 | void setNothrow(bool Nothrow = true); |
| 4743 | |
| 4744 | unsigned getNumParams() const { return NumParams; } |
| 4745 | |
| 4746 | ImplicitParamDecl *getParam(unsigned i) const { |
| 4747 | assert(i < NumParams); |
| 4748 | return getParams()[i]; |
| 4749 | } |
| 4750 | void setParam(unsigned i, ImplicitParamDecl *P) { |
| 4751 | assert(i < NumParams); |
| 4752 | getParams()[i] = P; |
| 4753 | } |
| 4754 | |
| 4755 | // Range interface to parameters. |
| 4756 | using parameter_const_iterator = const ImplicitParamDecl *const *; |
| 4757 | using parameter_const_range = llvm::iterator_range<parameter_const_iterator>; |
| 4758 | parameter_const_range parameters() const { |
| 4759 | return {param_begin(), param_end()}; |
| 4760 | } |
| 4761 | parameter_const_iterator param_begin() const { return getParams(); } |
| 4762 | parameter_const_iterator param_end() const { return getParams() + NumParams; } |
| 4763 | |
| 4764 | // Implement isa/cast/dyncast/etc. |
| 4765 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 4766 | static bool classofKind(Kind K) { return K == OutlinedFunction; } |
| 4767 | static DeclContext *castToDeclContext(const OutlinedFunctionDecl *D) { |
| 4768 | return static_cast<DeclContext *>(const_cast<OutlinedFunctionDecl *>(D)); |
| 4769 | } |
| 4770 | static OutlinedFunctionDecl *castFromDeclContext(const DeclContext *DC) { |
| 4771 | return static_cast<OutlinedFunctionDecl *>(const_cast<DeclContext *>(DC)); |
| 4772 | } |
| 4773 | }; |
| 4774 | |
James Dennett | 31a5734 | 2018-02-02 21:38:22 +0000 | [diff] [blame] | 4775 | /// Represents the body of a CapturedStmt, and serves as its DeclContext. |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4776 | class CapturedDecl final |
| 4777 | : public Decl, |
| 4778 | public DeclContext, |
| 4779 | private llvm::TrailingObjects<CapturedDecl, ImplicitParamDecl *> { |
| 4780 | protected: |
| 4781 | size_t numTrailingObjects(OverloadToken<ImplicitParamDecl>) { |
| 4782 | return NumParams; |
| 4783 | } |
| 4784 | |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4785 | private: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4786 | /// The number of parameters to the outlined function. |
Ben Langmuir | 37943a7 | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 4787 | unsigned NumParams; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4788 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4789 | /// The position of context parameter in list of parameters. |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 4790 | unsigned ContextParam; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4791 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4792 | /// The body of the outlined function. |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 4793 | llvm::PointerIntPair<Stmt *, 1, bool> BodyAndNothrow; |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4794 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 4795 | explicit CapturedDecl(DeclContext *DC, unsigned NumParams); |
Ben Langmuir | 37943a7 | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 4796 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4797 | ImplicitParamDecl *const *getParams() const { |
| 4798 | return getTrailingObjects<ImplicitParamDecl *>(); |
| 4799 | } |
| 4800 | |
| 4801 | ImplicitParamDecl **getParams() { |
| 4802 | return getTrailingObjects<ImplicitParamDecl *>(); |
Ben Langmuir | 37943a7 | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 4803 | } |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4804 | |
| 4805 | public: |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4806 | friend class ASTDeclReader; |
| 4807 | friend class ASTDeclWriter; |
| 4808 | friend TrailingObjects; |
| 4809 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 4810 | static CapturedDecl *Create(ASTContext &C, DeclContext *DC, |
| 4811 | unsigned NumParams); |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 4812 | static CapturedDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID, |
Ben Langmuir | ce914fc | 2013-05-03 19:20:19 +0000 | [diff] [blame] | 4813 | unsigned NumParams); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4814 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 4815 | Stmt *getBody() const override; |
| 4816 | void setBody(Stmt *B); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 4817 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 4818 | bool isNothrow() const; |
| 4819 | void setNothrow(bool Nothrow = true); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4820 | |
Ben Langmuir | ce914fc | 2013-05-03 19:20:19 +0000 | [diff] [blame] | 4821 | unsigned getNumParams() const { return NumParams; } |
| 4822 | |
Ben Langmuir | 37943a7 | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 4823 | ImplicitParamDecl *getParam(unsigned i) const { |
| 4824 | assert(i < NumParams); |
| 4825 | return getParams()[i]; |
| 4826 | } |
| 4827 | void setParam(unsigned i, ImplicitParamDecl *P) { |
| 4828 | assert(i < NumParams); |
| 4829 | getParams()[i] = P; |
| 4830 | } |
| 4831 | |
Yaron Keren | fe81363 | 2016-06-29 18:55:53 +0000 | [diff] [blame] | 4832 | // ArrayRef interface to parameters. |
| 4833 | ArrayRef<ImplicitParamDecl *> parameters() const { |
| 4834 | return {getParams(), getNumParams()}; |
| 4835 | } |
| 4836 | MutableArrayRef<ImplicitParamDecl *> parameters() { |
| 4837 | return {getParams(), getNumParams()}; |
| 4838 | } |
| 4839 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4840 | /// Retrieve the parameter containing captured variables. |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 4841 | ImplicitParamDecl *getContextParam() const { |
| 4842 | assert(ContextParam < NumParams); |
| 4843 | return getParam(ContextParam); |
| 4844 | } |
| 4845 | void setContextParam(unsigned i, ImplicitParamDecl *P) { |
| 4846 | assert(i < NumParams); |
| 4847 | ContextParam = i; |
| 4848 | setParam(i, P); |
| 4849 | } |
| 4850 | unsigned getContextParamPosition() const { return ContextParam; } |
Ben Langmuir | 37943a7 | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 4851 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4852 | using param_iterator = ImplicitParamDecl *const *; |
| 4853 | using param_range = llvm::iterator_range<param_iterator>; |
Aaron Ballman | e7fbde8 | 2014-03-07 16:40:17 +0000 | [diff] [blame] | 4854 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4855 | /// Retrieve an iterator pointing to the first parameter decl. |
Ben Langmuir | 37943a7 | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 4856 | param_iterator param_begin() const { return getParams(); } |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4857 | /// Retrieve an iterator one past the last parameter decl. |
Ben Langmuir | 37943a7 | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 4858 | param_iterator param_end() const { return getParams() + NumParams; } |
| 4859 | |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4860 | // Implement isa/cast/dyncast/etc. |
| 4861 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 4862 | static bool classofKind(Kind K) { return K == Captured; } |
| 4863 | static DeclContext *castToDeclContext(const CapturedDecl *D) { |
| 4864 | return static_cast<DeclContext *>(const_cast<CapturedDecl *>(D)); |
| 4865 | } |
| 4866 | static CapturedDecl *castFromDeclContext(const DeclContext *DC) { |
| 4867 | return static_cast<CapturedDecl *>(const_cast<DeclContext *>(DC)); |
| 4868 | } |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4869 | }; |
| 4870 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4871 | /// Describes a module import declaration, which makes the contents |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4872 | /// of the named module visible in the current translation unit. |
| 4873 | /// |
| 4874 | /// An import declaration imports the named module (or submodule). For example: |
| 4875 | /// \code |
Douglas Gregor | c50d492 | 2012-12-11 22:11:52 +0000 | [diff] [blame] | 4876 | /// @import std.vector; |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4877 | /// \endcode |
| 4878 | /// |
Iain Sandoe | 69350e5 | 2021-02-07 01:00:33 +0000 | [diff] [blame] | 4879 | /// A C++20 module import declaration imports the named module or partition. |
| 4880 | /// Periods are permitted in C++20 module names, but have no semantic meaning. |
| 4881 | /// For example: |
| 4882 | /// \code |
| 4883 | /// import NamedModule; |
| 4884 | /// import :SomePartition; // Must be a partition of the current module. |
| 4885 | /// import Names.Like.this; // Allowed. |
| 4886 | /// import :and.Also.Partition.names; |
| 4887 | /// \endcode |
| 4888 | /// |
James Dennett | 1355bd1 | 2012-06-11 06:19:40 +0000 | [diff] [blame] | 4889 | /// Import declarations can also be implicitly generated from |
| 4890 | /// \#include/\#import directives. |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4891 | class ImportDecl final : public Decl, |
| 4892 | llvm::TrailingObjects<ImportDecl, SourceLocation> { |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4893 | friend class ASTContext; |
| 4894 | friend class ASTDeclReader; |
| 4895 | friend class ASTReader; |
| 4896 | friend TrailingObjects; |
| 4897 | |
Reid Kleckner | c915cb9 | 2020-02-27 18:13:54 -0800 | [diff] [blame] | 4898 | /// The imported module. |
| 4899 | Module *ImportedModule = nullptr; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4900 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4901 | /// The next import in the list of imports local to the translation |
Douglas Gregor | 0f2a360 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 4902 | /// unit being parsed (not loaded from an AST file). |
Reid Kleckner | c915cb9 | 2020-02-27 18:13:54 -0800 | [diff] [blame] | 4903 | /// |
| 4904 | /// Includes a bit that indicates whether we have source-location information |
| 4905 | /// for each identifier in the module name. |
| 4906 | /// |
| 4907 | /// When the bit is false, we only have a single source location for the |
| 4908 | /// end of the import declaration. |
| 4909 | llvm::PointerIntPair<ImportDecl *, 1, bool> NextLocalImportAndComplete; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4910 | |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4911 | ImportDecl(DeclContext *DC, SourceLocation StartLoc, Module *Imported, |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4912 | ArrayRef<SourceLocation> IdentifierLocs); |
| 4913 | |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4914 | ImportDecl(DeclContext *DC, SourceLocation StartLoc, Module *Imported, |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4915 | SourceLocation EndLoc); |
| 4916 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4917 | ImportDecl(EmptyShell Empty) : Decl(Import, Empty) {} |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4918 | |
Reid Kleckner | c915cb9 | 2020-02-27 18:13:54 -0800 | [diff] [blame] | 4919 | bool isImportComplete() const { return NextLocalImportAndComplete.getInt(); } |
| 4920 | |
| 4921 | void setImportComplete(bool C) { NextLocalImportAndComplete.setInt(C); } |
| 4922 | |
| 4923 | /// The next import in the list of imports local to the translation |
| 4924 | /// unit being parsed (not loaded from an AST file). |
| 4925 | ImportDecl *getNextLocalImport() const { |
| 4926 | return NextLocalImportAndComplete.getPointer(); |
| 4927 | } |
| 4928 | |
| 4929 | void setNextLocalImport(ImportDecl *Import) { |
| 4930 | NextLocalImportAndComplete.setPointer(Import); |
| 4931 | } |
| 4932 | |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4933 | public: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4934 | /// Create a new module import declaration. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4935 | static ImportDecl *Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4936 | SourceLocation StartLoc, Module *Imported, |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4937 | ArrayRef<SourceLocation> IdentifierLocs); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4938 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4939 | /// Create a new module import declaration for an implicitly-generated |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4940 | /// import. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4941 | static ImportDecl *CreateImplicit(ASTContext &C, DeclContext *DC, |
| 4942 | SourceLocation StartLoc, Module *Imported, |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4943 | SourceLocation EndLoc); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4944 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4945 | /// Create a new, deserialized module import declaration. |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 4946 | static ImportDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID, |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4947 | unsigned NumLocations); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4948 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4949 | /// Retrieve the module that was imported by the import declaration. |
Reid Kleckner | c915cb9 | 2020-02-27 18:13:54 -0800 | [diff] [blame] | 4950 | Module *getImportedModule() const { return ImportedModule; } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4951 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4952 | /// Retrieves the locations of each of the identifiers that make up |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4953 | /// the complete module name in the import declaration. |
| 4954 | /// |
| 4955 | /// This will return an empty array if the locations of the individual |
| 4956 | /// identifiers aren't available. |
| 4957 | ArrayRef<SourceLocation> getIdentifierLocs() const; |
Craig Topper | cbce6e9 | 2014-03-11 06:22:39 +0000 | [diff] [blame] | 4958 | |
| 4959 | SourceRange getSourceRange() const override LLVM_READONLY; |
| 4960 | |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4961 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4962 | static bool classofKind(Kind K) { return K == Import; } |
| 4963 | }; |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 4964 | |
Chuanqi Xu | 612f3ac | 2023-02-15 18:30:49 +0800 | [diff] [blame] | 4965 | /// Represents a standard C++ module export declaration. |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 4966 | /// |
| 4967 | /// For example: |
| 4968 | /// \code |
| 4969 | /// export void foo(); |
| 4970 | /// \endcode |
| 4971 | class ExportDecl final : public Decl, public DeclContext { |
| 4972 | virtual void anchor(); |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4973 | |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 4974 | private: |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4975 | friend class ASTDeclReader; |
| 4976 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4977 | /// The source location for the right brace (if valid). |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 4978 | SourceLocation RBraceLoc; |
| 4979 | |
| 4980 | ExportDecl(DeclContext *DC, SourceLocation ExportLoc) |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4981 | : Decl(Export, DC, ExportLoc), DeclContext(Export), |
| 4982 | RBraceLoc(SourceLocation()) {} |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 4983 | |
| 4984 | public: |
| 4985 | static ExportDecl *Create(ASTContext &C, DeclContext *DC, |
| 4986 | SourceLocation ExportLoc); |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 4987 | static ExportDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4988 | |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 4989 | SourceLocation getExportLoc() const { return getLocation(); } |
| 4990 | SourceLocation getRBraceLoc() const { return RBraceLoc; } |
| 4991 | void setRBraceLoc(SourceLocation L) { RBraceLoc = L; } |
| 4992 | |
Richard Smith | e181de7 | 2019-04-22 22:50:11 +0000 | [diff] [blame] | 4993 | bool hasBraces() const { return RBraceLoc.isValid(); } |
| 4994 | |
Stephen Kelly | 02a67ba | 2018-08-09 20:05:47 +0000 | [diff] [blame] | 4995 | SourceLocation getEndLoc() const LLVM_READONLY { |
Richard Smith | e181de7 | 2019-04-22 22:50:11 +0000 | [diff] [blame] | 4996 | if (hasBraces()) |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 4997 | return RBraceLoc; |
| 4998 | // No braces: get the end location of the (only) declaration in context |
| 4999 | // (if present). |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 5000 | return decls_empty() ? getLocation() : decls_begin()->getEndLoc(); |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 5001 | } |
| 5002 | |
| 5003 | SourceRange getSourceRange() const override LLVM_READONLY { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 5004 | return SourceRange(getLocation(), getEndLoc()); |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 5005 | } |
| 5006 | |
| 5007 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 5008 | static bool classofKind(Kind K) { return K == Export; } |
| 5009 | static DeclContext *castToDeclContext(const ExportDecl *D) { |
| 5010 | return static_cast<DeclContext *>(const_cast<ExportDecl*>(D)); |
| 5011 | } |
| 5012 | static ExportDecl *castFromDeclContext(const DeclContext *DC) { |
| 5013 | return static_cast<ExportDecl *>(const_cast<DeclContext*>(DC)); |
| 5014 | } |
| 5015 | }; |
| 5016 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 5017 | /// Represents an empty-declaration. |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 5018 | class EmptyDecl : public Decl { |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 5019 | EmptyDecl(DeclContext *DC, SourceLocation L) : Decl(Empty, DC, L) {} |
| 5020 | |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 5021 | virtual void anchor(); |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 5022 | |
| 5023 | public: |
| 5024 | static EmptyDecl *Create(ASTContext &C, DeclContext *DC, |
| 5025 | SourceLocation L); |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 5026 | static EmptyDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 5027 | |
| 5028 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 5029 | static bool classofKind(Kind K) { return K == Empty; } |
| 5030 | }; |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 5031 | |
Xiang Li | 782ac21 | 2022-07-15 10:45:57 -0700 | [diff] [blame] | 5032 | /// HLSLBufferDecl - Represent a cbuffer or tbuffer declaration. |
| 5033 | class HLSLBufferDecl final : public NamedDecl, public DeclContext { |
| 5034 | /// LBraceLoc - The ending location of the source range. |
| 5035 | SourceLocation LBraceLoc; |
| 5036 | /// RBraceLoc - The ending location of the source range. |
| 5037 | SourceLocation RBraceLoc; |
| 5038 | /// KwLoc - The location of the cbuffer or tbuffer keyword. |
| 5039 | SourceLocation KwLoc; |
| 5040 | /// IsCBuffer - Whether the buffer is a cbuffer (and not a tbuffer). |
| 5041 | bool IsCBuffer; |
Helena Kotas | 19af858 | 2025-02-20 10:32:14 -0800 | [diff] [blame] | 5042 | /// HasValidPackoffset - Whether the buffer has valid packoffset annotations |
| 5043 | // on all declarations |
| 5044 | bool HasValidPackoffset; |
| 5045 | // LayoutStruct - Layout struct for the buffer |
| 5046 | CXXRecordDecl *LayoutStruct; |
Xiang Li | 782ac21 | 2022-07-15 10:45:57 -0700 | [diff] [blame] | 5047 | |
| 5048 | HLSLBufferDecl(DeclContext *DC, bool CBuffer, SourceLocation KwLoc, |
| 5049 | IdentifierInfo *ID, SourceLocation IDLoc, |
| 5050 | SourceLocation LBrace); |
| 5051 | |
| 5052 | public: |
| 5053 | static HLSLBufferDecl *Create(ASTContext &C, DeclContext *LexicalParent, |
| 5054 | bool CBuffer, SourceLocation KwLoc, |
| 5055 | IdentifierInfo *ID, SourceLocation IDLoc, |
| 5056 | SourceLocation LBrace); |
Chuanqi Xu | d86cc73 | 2024-04-25 11:43:13 +0800 | [diff] [blame] | 5057 | static HLSLBufferDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID); |
Xiang Li | 782ac21 | 2022-07-15 10:45:57 -0700 | [diff] [blame] | 5058 | |
Xiang Li | a7e3de2 | 2022-09-21 10:55:51 -0700 | [diff] [blame] | 5059 | SourceRange getSourceRange() const override LLVM_READONLY { |
Xiang Li | 782ac21 | 2022-07-15 10:45:57 -0700 | [diff] [blame] | 5060 | return SourceRange(getLocStart(), RBraceLoc); |
| 5061 | } |
| 5062 | SourceLocation getLocStart() const LLVM_READONLY { return KwLoc; } |
| 5063 | SourceLocation getLBraceLoc() const { return LBraceLoc; } |
| 5064 | SourceLocation getRBraceLoc() const { return RBraceLoc; } |
| 5065 | void setRBraceLoc(SourceLocation L) { RBraceLoc = L; } |
| 5066 | bool isCBuffer() const { return IsCBuffer; } |
Helena Kotas | 19af858 | 2025-02-20 10:32:14 -0800 | [diff] [blame] | 5067 | void setHasValidPackoffset(bool PO) { HasValidPackoffset = PO; } |
| 5068 | bool hasValidPackoffset() const { return HasValidPackoffset; } |
| 5069 | const CXXRecordDecl *getLayoutStruct() const { return LayoutStruct; } |
| 5070 | void addLayoutStruct(CXXRecordDecl *LS); |
Xiang Li | 782ac21 | 2022-07-15 10:45:57 -0700 | [diff] [blame] | 5071 | |
| 5072 | // Implement isa/cast/dyncast/etc. |
| 5073 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
| 5074 | static bool classofKind(Kind K) { return K == HLSLBuffer; } |
| 5075 | static DeclContext *castToDeclContext(const HLSLBufferDecl *D) { |
| 5076 | return static_cast<DeclContext *>(const_cast<HLSLBufferDecl *>(D)); |
| 5077 | } |
| 5078 | static HLSLBufferDecl *castFromDeclContext(const DeclContext *DC) { |
| 5079 | return static_cast<HLSLBufferDecl *>(const_cast<DeclContext *>(DC)); |
| 5080 | } |
| 5081 | |
| 5082 | friend class ASTDeclReader; |
| 5083 | friend class ASTDeclWriter; |
| 5084 | }; |
| 5085 | |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 5086 | /// Insertion operator for diagnostics. This allows sending NamedDecl's |
| 5087 | /// into a diagnostic with <<. |
Yaxun (Sam) Liu | 7e561b6 | 2020-09-23 16:16:00 -0400 | [diff] [blame] | 5088 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, |
| 5089 | const NamedDecl *ND) { |
Arthur Eubanks | afdac5f | 2021-10-05 13:55:31 -0700 | [diff] [blame] | 5090 | PD.AddTaggedVal(reinterpret_cast<uint64_t>(ND), |
Benjamin Kramer | 7ec12c9 | 2012-02-07 22:29:24 +0000 | [diff] [blame] | 5091 | DiagnosticsEngine::ak_nameddecl); |
| 5092 | return PD; |
| 5093 | } |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 5094 | |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 5095 | template<typename decl_type> |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 5096 | void Redeclarable<decl_type>::setPreviousDecl(decl_type *PrevDecl) { |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 5097 | // Note: This routine is implemented here because we need both NamedDecl |
| 5098 | // and Redeclarable to be defined. |
Richard Trieu | 87a615d | 2018-07-09 22:09:33 +0000 | [diff] [blame] | 5099 | assert(RedeclLink.isFirst() && |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 5100 | "setPreviousDecl on a decl already in a redeclaration chain"); |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 5101 | |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 5102 | if (PrevDecl) { |
| 5103 | // Point to previous. Make sure that this is actually the most recent |
| 5104 | // redeclaration, or we can build invalid chains. If the most recent |
| 5105 | // redeclaration is invalid, it won't be PrevDecl, but we want it anyway. |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 5106 | First = PrevDecl->getFirstDecl(); |
Richard Trieu | 87a615d | 2018-07-09 22:09:33 +0000 | [diff] [blame] | 5107 | assert(First->RedeclLink.isFirst() && "Expected first"); |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 5108 | decl_type *MostRecent = First->getNextRedeclaration(); |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 5109 | RedeclLink = PreviousDeclLink(cast<decl_type>(MostRecent)); |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 5110 | |
| 5111 | // If the declaration was previously visible, a redeclaration of it remains |
| 5112 | // visible even if it wouldn't be visible by itself. |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 5113 | static_cast<decl_type*>(this)->IdentifierNamespace |= |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 5114 | MostRecent->getIdentifierNamespace() & |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 5115 | (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type); |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 5116 | } else { |
| 5117 | // Make this first. |
| 5118 | First = static_cast<decl_type*>(this); |
| 5119 | } |
David Blaikie | 21bfbf8 | 2011-11-09 06:07:30 +0000 | [diff] [blame] | 5120 | |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 5121 | // First one will point to this one as latest. |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 5122 | First->RedeclLink.setLatest(static_cast<decl_type*>(this)); |
| 5123 | |
Chandler Carruth | 5aa9d79 | 2013-03-14 11:17:20 +0000 | [diff] [blame] | 5124 | assert(!isa<NamedDecl>(static_cast<decl_type*>(this)) || |
| 5125 | cast<NamedDecl>(static_cast<decl_type*>(this))->isLinkageValid()); |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 5126 | } |
| 5127 | |
Daniel Dunbar | 4924882 | 2012-03-06 18:20:20 +0000 | [diff] [blame] | 5128 | // Inline function definitions. |
| 5129 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 5130 | /// Check if the given decl is complete. |
Daniel Dunbar | 4924882 | 2012-03-06 18:20:20 +0000 | [diff] [blame] | 5131 | /// |
| 5132 | /// We use this function to break a cycle between the inline definitions in |
| 5133 | /// Type.h and Decl.h. |
| 5134 | inline bool IsEnumDeclComplete(EnumDecl *ED) { |
| 5135 | return ED->isComplete(); |
| 5136 | } |
| 5137 | |
James Dennett | b8973efb | 2018-02-02 20:22:29 +0000 | [diff] [blame] | 5138 | /// Check if the given decl is scoped. |
Daniel Dunbar | d043733 | 2012-03-08 02:52:18 +0000 | [diff] [blame] | 5139 | /// |
| 5140 | /// We use this function to break a cycle between the inline definitions in |
| 5141 | /// Type.h and Decl.h. |
| 5142 | inline bool IsEnumDeclScoped(EnumDecl *ED) { |
| 5143 | return ED->isScoped(); |
| 5144 | } |
| 5145 | |
Johannes Doerfert | befb4be | 2020-02-25 14:04:06 -0800 | [diff] [blame] | 5146 | /// OpenMP variants are mangled early based on their OpenMP context selector. |
| 5147 | /// The new name looks likes this: |
| 5148 | /// <name> + OpenMPVariantManglingSeparatorStr + <mangled OpenMP context> |
| 5149 | static constexpr StringRef getOpenMPVariantManglingSeparatorStr() { |
Lukas Sommer | 8bd7e41 | 2020-06-03 16:32:49 -0400 | [diff] [blame] | 5150 | return "$ompvariant"; |
Johannes Doerfert | befb4be | 2020-02-25 14:04:06 -0800 | [diff] [blame] | 5151 | } |
| 5152 | |
Sander de Smalen | 6a6fcbf | 2024-05-07 15:03:26 +0000 | [diff] [blame] | 5153 | /// Returns whether the given FunctionDecl has an __arm[_locally]_streaming |
| 5154 | /// attribute. |
| 5155 | bool IsArmStreamingFunction(const FunctionDecl *FD, |
| 5156 | bool IncludeLocallyStreaming); |
| 5157 | |
Benjamin Maxwell | a7f4044 | 2025-01-28 12:08:54 +0000 | [diff] [blame] | 5158 | /// Returns whether the given FunctionDecl has Arm ZA state. |
| 5159 | bool hasArmZAState(const FunctionDecl *FD); |
| 5160 | |
| 5161 | /// Returns whether the given FunctionDecl has Arm ZT0 state. |
| 5162 | bool hasArmZT0State(const FunctionDecl *FD); |
| 5163 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 5164 | } // namespace clang |
Nate Begeman | f2a6e5f | 2008-12-16 19:57:09 +0000 | [diff] [blame] | 5165 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 5166 | #endif // LLVM_CLANG_AST_DECL_H |