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