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