clang  16.0.0git
Attr.h
Go to the documentation of this file.
1 //===--- Attr.h - Classes for representing attributes ----------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the Attr interface and subclasses.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_AST_ATTR_H
14 #define LLVM_CLANG_AST_ATTR_H
15 
16 #include "clang/AST/ASTFwd.h"
17 #include "clang/AST/AttrIterator.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/Type.h"
20 #include "clang/Basic/AttrKinds.h"
23 #include "clang/Basic/LLVM.h"
25 #include "clang/Basic/Sanitizers.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/VersionTuple.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include <algorithm>
31 #include <cassert>
32 
33 namespace clang {
34 class ASTContext;
35 class AttributeCommonInfo;
36 class FunctionDecl;
37 class OMPTraitInfo;
38 
39 /// Attr - This represents one attribute.
40 class Attr : public AttributeCommonInfo {
41 private:
42  unsigned AttrKind : 16;
43 
44 protected:
45  /// An index into the spelling list of an
46  /// attribute defined in Attr.td file.
47  unsigned Inherited : 1;
48  unsigned IsPackExpansion : 1;
49  unsigned Implicit : 1;
50  // FIXME: These are properties of the attribute kind, not state for this
51  // instance of the attribute.
52  unsigned IsLateParsed : 1;
54 
55  void *operator new(size_t bytes) noexcept {
56  llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
57  }
58  void operator delete(void *data) noexcept {
59  llvm_unreachable("Attrs cannot be released with regular 'delete'.");
60  }
61 
62 public:
63  // Forward so that the regular new and delete do not hide global ones.
64  void *operator new(size_t Bytes, ASTContext &C,
65  size_t Alignment = 8) noexcept {
66  return ::operator new(Bytes, C, Alignment);
67  }
68  void operator delete(void *Ptr, ASTContext &C, size_t Alignment) noexcept {
69  return ::operator delete(Ptr, C, Alignment);
70  }
71 
72 protected:
73  Attr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
74  attr::Kind AK, bool IsLateParsed)
75  : AttributeCommonInfo(CommonInfo), AttrKind(AK), Inherited(false),
78 
79 public:
80  attr::Kind getKind() const { return static_cast<attr::Kind>(AttrKind); }
81 
82  unsigned getSpellingListIndex() const {
84  }
85  const char *getSpelling() const;
86 
87  SourceLocation getLocation() const { return getRange().getBegin(); }
88 
89  bool isInherited() const { return Inherited; }
90 
91  /// Returns true if the attribute has been implicitly created instead
92  /// of explicitly written by the user.
93  bool isImplicit() const { return Implicit; }
94  void setImplicit(bool I) { Implicit = I; }
95 
96  void setPackExpansion(bool PE) { IsPackExpansion = PE; }
97  bool isPackExpansion() const { return IsPackExpansion; }
98 
99  // Clone this attribute.
100  Attr *clone(ASTContext &C) const;
101 
102  bool isLateParsed() const { return IsLateParsed; }
103 
104  // Pretty print this attribute.
105  void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const;
106 
107  static StringRef getDocumentation(attr::Kind);
108 };
109 
110 class TypeAttr : public Attr {
111 protected:
112  TypeAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
113  attr::Kind AK, bool IsLateParsed)
114  : Attr(Context, CommonInfo, AK, IsLateParsed) {}
115 
116 public:
117  static bool classof(const Attr *A) {
118  return A->getKind() >= attr::FirstTypeAttr &&
119  A->getKind() <= attr::LastTypeAttr;
120  }
121 };
122 
123 class StmtAttr : public Attr {
124 protected:
125  StmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
126  attr::Kind AK, bool IsLateParsed)
127  : Attr(Context, CommonInfo, AK, IsLateParsed) {}
128 
129 public:
130  static bool classof(const Attr *A) {
131  return A->getKind() >= attr::FirstStmtAttr &&
132  A->getKind() <= attr::LastStmtAttr;
133  }
134 };
135 
136 class InheritableAttr : public Attr {
137 protected:
138  InheritableAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
139  attr::Kind AK, bool IsLateParsed,
141  : Attr(Context, CommonInfo, AK, IsLateParsed) {
142  this->InheritEvenIfAlreadyPresent = InheritEvenIfAlreadyPresent;
143  }
144 
145 public:
146  void setInherited(bool I) { Inherited = I; }
147 
148  /// Should this attribute be inherited from a prior declaration even if it's
149  /// explicitly provided in the current declaration?
152  }
153 
154  // Implement isa/cast/dyncast/etc.
155  static bool classof(const Attr *A) {
156  return A->getKind() >= attr::FirstInheritableAttr &&
157  A->getKind() <= attr::LastInheritableAttr;
158  }
159 };
160 
162 protected:
163  DeclOrStmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
164  attr::Kind AK, bool IsLateParsed,
166  : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
168 
169 public:
170  static bool classof(const Attr *A) {
171  return A->getKind() >= attr::FirstDeclOrStmtAttr &&
172  A->getKind() <= attr::LastDeclOrStmtAttr;
173  }
174 };
175 
177 protected:
179  const AttributeCommonInfo &CommonInfo, attr::Kind AK,
181  : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
183 
184 public:
185  // Implement isa/cast/dyncast/etc.
186  static bool classof(const Attr *A) {
187  return A->getKind() >= attr::FirstInheritableParamAttr &&
188  A->getKind() <= attr::LastInheritableParamAttr;
189  }
190 };
191 
193 protected:
194  HLSLAnnotationAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
195  attr::Kind AK, bool IsLateParsed,
197  : InheritableAttr(Context, CommonInfo, AK, IsLateParsed,
199 
200 public:
201  // Implement isa/cast/dyncast/etc.
202  static bool classof(const Attr *A) {
203  return A->getKind() >= attr::FirstHLSLAnnotationAttr &&
204  A->getKind() <= attr::LastHLSLAnnotationAttr;
205  }
206 };
207 
208 /// A parameter attribute which changes the argument-passing ABI rule
209 /// for the parameter.
211 protected:
212  ParameterABIAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
213  attr::Kind AK, bool IsLateParsed,
215  : InheritableParamAttr(Context, CommonInfo, AK, IsLateParsed,
217 
218 public:
220  switch (getKind()) {
221  case attr::SwiftContext:
223  case attr::SwiftAsyncContext:
225  case attr::SwiftErrorResult:
227  case attr::SwiftIndirectResult:
229  default:
230  llvm_unreachable("bad parameter ABI attribute kind");
231  }
232  }
233 
234  static bool classof(const Attr *A) {
235  return A->getKind() >= attr::FirstParameterABIAttr &&
236  A->getKind() <= attr::LastParameterABIAttr;
237  }
238 };
239 
240 /// A single parameter index whose accessors require each use to make explicit
241 /// the parameter index encoding needed.
242 class ParamIdx {
243  // Idx is exposed only via accessors that specify specific encodings.
244  unsigned Idx : 30;
245  unsigned HasThis : 1;
246  unsigned IsValid : 1;
247 
248  void assertComparable(const ParamIdx &I) const {
249  assert(isValid() && I.isValid() &&
250  "ParamIdx must be valid to be compared");
251  // It's possible to compare indices from separate functions, but so far
252  // it's not proven useful. Moreover, it might be confusing because a
253  // comparison on the results of getASTIndex might be inconsistent with a
254  // comparison on the ParamIdx objects themselves.
255  assert(HasThis == I.HasThis &&
256  "ParamIdx must be for the same function to be compared");
257  }
258 
259 public:
260  /// Construct an invalid parameter index (\c isValid returns false and
261  /// accessors fail an assert).
262  ParamIdx() : Idx(0), HasThis(false), IsValid(false) {}
263 
264  /// \param Idx is the parameter index as it is normally specified in
265  /// attributes in the source: one-origin including any C++ implicit this
266  /// parameter.
267  ///
268  /// \param D is the declaration containing the parameters. It is used to
269  /// determine if there is a C++ implicit this parameter.
270  ParamIdx(unsigned Idx, const Decl *D)
271  : Idx(Idx), HasThis(false), IsValid(true) {
272  assert(Idx >= 1 && "Idx must be one-origin");
273  if (const auto *FD = dyn_cast<FunctionDecl>(D))
274  HasThis = FD->isCXXInstanceMember();
275  }
276 
277  /// A type into which \c ParamIdx can be serialized.
278  ///
279  /// A static assertion that it's of the correct size follows the \c ParamIdx
280  /// class definition.
281  typedef uint32_t SerialType;
282 
283  /// Produce a representation that can later be passed to \c deserialize to
284  /// construct an equivalent \c ParamIdx.
286  return *reinterpret_cast<const SerialType *>(this);
287  }
288 
289  /// Construct from a result from \c serialize.
291  // Using this two-step static_cast via void * instead of reinterpret_cast
292  // silences a -Wstrict-aliasing false positive from GCC7 and earlier.
293  void *ParamIdxPtr = static_cast<void *>(&S);
294  ParamIdx P(*static_cast<ParamIdx *>(ParamIdxPtr));
295  assert((!P.IsValid || P.Idx >= 1) && "valid Idx must be one-origin");
296  return P;
297  }
298 
299  /// Is this parameter index valid?
300  bool isValid() const { return IsValid; }
301 
302  /// Get the parameter index as it would normally be encoded for attributes at
303  /// the source level of representation: one-origin including any C++ implicit
304  /// this parameter.
305  ///
306  /// This encoding thus makes sense for diagnostics, pretty printing, and
307  /// constructing new attributes from a source-like specification.
308  unsigned getSourceIndex() const {
309  assert(isValid() && "ParamIdx must be valid");
310  return Idx;
311  }
312 
313  /// Get the parameter index as it would normally be encoded at the AST level
314  /// of representation: zero-origin not including any C++ implicit this
315  /// parameter.
316  ///
317  /// This is the encoding primarily used in Sema. However, in diagnostics,
318  /// Sema uses \c getSourceIndex instead.
319  unsigned getASTIndex() const {
320  assert(isValid() && "ParamIdx must be valid");
321  assert(Idx >= 1 + HasThis &&
322  "stored index must be base-1 and not specify C++ implicit this");
323  return Idx - 1 - HasThis;
324  }
325 
326  /// Get the parameter index as it would normally be encoded at the LLVM level
327  /// of representation: zero-origin including any C++ implicit this parameter.
328  ///
329  /// This is the encoding primarily used in CodeGen.
330  unsigned getLLVMIndex() const {
331  assert(isValid() && "ParamIdx must be valid");
332  assert(Idx >= 1 && "stored index must be base-1");
333  return Idx - 1;
334  }
335 
336  bool operator==(const ParamIdx &I) const {
337  assertComparable(I);
338  return Idx == I.Idx;
339  }
340  bool operator!=(const ParamIdx &I) const {
341  assertComparable(I);
342  return Idx != I.Idx;
343  }
344  bool operator<(const ParamIdx &I) const {
345  assertComparable(I);
346  return Idx < I.Idx;
347  }
348  bool operator>(const ParamIdx &I) const {
349  assertComparable(I);
350  return Idx > I.Idx;
351  }
352  bool operator<=(const ParamIdx &I) const {
353  assertComparable(I);
354  return Idx <= I.Idx;
355  }
356  bool operator>=(const ParamIdx &I) const {
357  assertComparable(I);
358  return Idx >= I.Idx;
359  }
360 };
361 
362 static_assert(sizeof(ParamIdx) == sizeof(ParamIdx::SerialType),
363  "ParamIdx does not fit its serialization type");
364 
365 #include "clang/AST/Attrs.inc"
366 
368  const Attr *At) {
369  DB.AddTaggedVal(reinterpret_cast<uint64_t>(At), DiagnosticsEngine::ak_attr);
370  return DB;
371 }
372 } // end namespace clang
373 
374 #endif
clang::HLSLAnnotationAttr::HLSLAnnotationAttr
HLSLAnnotationAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:194
clang::Attr::IsPackExpansion
unsigned IsPackExpansion
Definition: Attr.h:48
clang::ParamIdx::isValid
bool isValid() const
Is this parameter index valid?
Definition: Attr.h:300
clang::Attr::isImplicit
bool isImplicit() const
Returns true if the attribute has been implicitly created instead of explicitly written by the user.
Definition: Attr.h:93
AttributeCommonInfo.h
clang::ParameterABI::SwiftAsyncContext
@ SwiftAsyncContext
This parameter (which must have pointer type) uses the special Swift asynchronous context-pointer ABI...
clang::TypeAttr::TypeAttr
TypeAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed)
Definition: Attr.h:112
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::StmtAttr
Definition: Attr.h:123
clang::ParamIdx::ParamIdx
ParamIdx()
Construct an invalid parameter index (isValid returns false and accessors fail an assert).
Definition: Attr.h:262
clang::SourceRange::getBegin
SourceLocation getBegin() const
Definition: SourceLocation.h:219
clang::InheritableAttr::shouldInheritEvenIfAlreadyPresent
bool shouldInheritEvenIfAlreadyPresent() const
Should this attribute be inherited from a prior declaration even if it's explicitly provided in the c...
Definition: Attr.h:150
clang::Attr::getLocation
SourceLocation getLocation() const
Definition: Attr.h:87
clang::DeclOrStmtAttr
Definition: Attr.h:161
clang::ParameterABIAttr::getABI
ParameterABI getABI() const
Definition: Attr.h:219
AttrIterator.h
clang::PrintingPolicy
Describes how types, statements, expressions, and declarations should be printed.
Definition: PrettyPrinter.h:57
clang::Attr::InheritEvenIfAlreadyPresent
unsigned InheritEvenIfAlreadyPresent
Definition: Attr.h:53
clang::Attr::Attr
Attr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed)
Definition: Attr.h:73
ASTFwd.h
clang::StreamingDiagnostic::AddTaggedVal
void AddTaggedVal(uint64_t V, DiagnosticsEngine::ArgumentKind Kind) const
Definition: Diagnostic.h:1184
clang::InheritableAttr::classof
static bool classof(const Attr *A)
Definition: Attr.h:155
clang::ParamIdx::operator<=
bool operator<=(const ParamIdx &I) const
Definition: Attr.h:352
clang::TypeAttr
Definition: Attr.h:110
Decl.h
clang::InheritableAttr
Definition: Attr.h:136
clang::ParamIdx::operator<
bool operator<(const ParamIdx &I) const
Definition: Attr.h:344
clang::ParamIdx::getSourceIndex
unsigned getSourceIndex() const
Get the parameter index as it would normally be encoded for attributes at the source level of represe...
Definition: Attr.h:308
clang::ParameterABI
ParameterABI
Kinds of parameter ABI.
Definition: Specifiers.h:348
clang::InheritableAttr::setInherited
void setInherited(bool I)
Definition: Attr.h:146
clang::InheritableAttr::InheritableAttr
InheritableAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:138
hlsl::uint64_t
unsigned long uint64_t
Definition: hlsl_basic_types.h:25
clang::ParameterABIAttr::ParameterABIAttr
ParameterABIAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:212
clang::ParamIdx
A single parameter index whose accessors require each use to make explicit the parameter index encodi...
Definition: Attr.h:242
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:209
clang::StreamingDiagnostic
The streaming interface shared between DiagnosticBuilder and PartialDiagnostic.
Definition: Diagnostic.h:1110
LangOptions.h
clang::InheritableParamAttr::InheritableParamAttr
InheritableParamAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:178
clang::DiagnosticsEngine::ak_attr
@ ak_attr
Attr *.
Definition: Diagnostic.h:248
OpenMPKinds.h
clang::TypeAttr::classof
static bool classof(const Attr *A)
Definition: Attr.h:117
clang::ParameterABI::SwiftIndirectResult
@ SwiftIndirectResult
This parameter (which must have pointer type) is a Swift indirect result parameter.
clang::Attr::getKind
attr::Kind getKind() const
Definition: Attr.h:80
clang::RISCV::Policy
Policy
Definition: RISCVVIntrinsicUtils.h:96
clang::ParamIdx::getASTIndex
unsigned getASTIndex() const
Get the parameter index as it would normally be encoded at the AST level of representation: zero-orig...
Definition: Attr.h:319
clang::Attr::setPackExpansion
void setPackExpansion(bool PE)
Definition: Attr.h:96
Type.h
clang::ParamIdx::operator==
bool operator==(const ParamIdx &I) const
Definition: Attr.h:336
clang::StmtAttr::classof
static bool classof(const Attr *A)
Definition: Attr.h:130
clang::Attr::IsLateParsed
unsigned IsLateParsed
Definition: Attr.h:52
clang::Attr::Inherited
unsigned Inherited
An index into the spelling list of an attribute defined in Attr.td file.
Definition: Attr.h:47
clang::Attr::getDocumentation
static StringRef getDocumentation(attr::Kind)
Definition: AttrDocTable.cpp:23
clang::operator<<
const StreamingDiagnostic & operator<<(const StreamingDiagnostic &DB, const ASTContext::SectionInfo &Section)
Insertion operator for diagnostics.
Definition: ASTContext.cpp:13400
clang::ParamIdx::getLLVMIndex
unsigned getLLVMIndex() const
Get the parameter index as it would normally be encoded at the LLVM level of representation: zero-ori...
Definition: Attr.h:330
clang::ParamIdx::operator!=
bool operator!=(const ParamIdx &I) const
Definition: Attr.h:340
clang::ParamIdx::ParamIdx
ParamIdx(unsigned Idx, const Decl *D)
Definition: Attr.h:270
SourceLocation.h
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
clang::DeclOrStmtAttr::DeclOrStmtAttr
DeclOrStmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:163
clang::Attr::isInherited
bool isInherited() const
Definition: Attr.h:89
clang::Attr::clone
Attr * clone(ASTContext &C) const
clang::AttributeCommonInfo::getRange
SourceRange getRange() const
Definition: AttributeCommonInfo.h:133
clang::ParamIdx::operator>
bool operator>(const ParamIdx &I) const
Definition: Attr.h:348
clang::attr::Kind
Kind
Definition: AttrKinds.h:22
false
#define false
Definition: stdbool.h:22
clang::Attr::isPackExpansion
bool isPackExpansion() const
Definition: Attr.h:97
clang::ParameterABIAttr::classof
static bool classof(const Attr *A)
Definition: Attr.h:234
clang::HLSLAnnotationAttr::classof
static bool classof(const Attr *A)
Definition: Attr.h:202
clang::DeclOrStmtAttr::classof
static bool classof(const Attr *A)
Definition: Attr.h:170
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
clang::AttributeCommonInfo
Definition: AttributeCommonInfo.h:22
LLVM.h
clang::InheritableParamAttr::classof
static bool classof(const Attr *A)
Definition: Attr.h:186
bytes
static StringRef bytes(const std::vector< T, Allocator > &v)
Definition: ASTWriter.cpp:123
Sanitizers.h
clang::ParamIdx::SerialType
uint32_t SerialType
A type into which ParamIdx can be serialized.
Definition: Attr.h:281
clang::Attr::getSpelling
const char * getSpelling() const
clang
Definition: CalledOnceCheck.h:17
clang::ParameterABI::SwiftErrorResult
@ SwiftErrorResult
This parameter (which must have pointer-to-pointer type) uses the special Swift error-result ABI trea...
clang::Attr::isLateParsed
bool isLateParsed() const
Definition: Attr.h:102
clang::StmtAttr::StmtAttr
StmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, attr::Kind AK, bool IsLateParsed)
Definition: Attr.h:125
clang::Attr
Attr - This represents one attribute.
Definition: Attr.h:40
clang::ParameterABI::SwiftContext
@ SwiftContext
This parameter (which must have pointer type) uses the special Swift context-pointer ABI treatment.
clang::ParamIdx::deserialize
static ParamIdx deserialize(SerialType S)
Construct from a result from serialize.
Definition: Attr.h:290
clang::Attr::setImplicit
void setImplicit(bool I)
Definition: Attr.h:94
clang::Attr::getSpellingListIndex
unsigned getSpellingListIndex() const
Definition: Attr.h:82
clang::ParamIdx::serialize
SerialType serialize() const
Produce a representation that can later be passed to deserialize to construct an equivalent ParamIdx.
Definition: Attr.h:285
clang::ParameterABIAttr
A parameter attribute which changes the argument-passing ABI rule for the parameter.
Definition: Attr.h:210
AttrKinds.h
clang::InheritableParamAttr
Definition: Attr.h:176
true
#define true
Definition: stdbool.h:21
clang::AttributeCommonInfo::getAttributeSpellingListIndex
unsigned getAttributeSpellingListIndex() const
Definition: AttributeCommonInfo.h:193
clang::Attr::printPretty
void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const
clang::Attr::Implicit
unsigned Implicit
Definition: Attr.h:49
clang::HLSLAnnotationAttr
Definition: Attr.h:192
clang::ParamIdx::operator>=
bool operator>=(const ParamIdx &I) const
Definition: Attr.h:356