clang  9.0.0svn
Attr.h
Go to the documentation of this file.
1 //===--- Attr.h - Classes for representing attributes ----------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the Attr interface and subclasses.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_AST_ATTR_H
15 #define LLVM_CLANG_AST_ATTR_H
16 
17 #include "clang/AST/ASTContextAllocate.h" // For Attrs.inc
18 #include "clang/AST/AttrIterator.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/Type.h"
22 #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 IdentifierInfo;
37  class ObjCInterfaceDecl;
38  class Expr;
39  class QualType;
40  class FunctionDecl;
41  class TypeSourceInfo;
42 
43 /// Attr - This represents one attribute.
44 class Attr {
45 private:
46  SourceRange Range;
47  unsigned AttrKind : 16;
48 
49 protected:
50  /// An index into the spelling list of an
51  /// attribute defined in Attr.td file.
52  unsigned SpellingListIndex : 4;
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:
80  bool IsLateParsed)
81  : Range(R), AttrKind(AK), SpellingListIndex(SpellingListIndex),
83  IsLateParsed(IsLateParsed), InheritEvenIfAlreadyPresent(false) {}
84 
85 public:
86 
87  attr::Kind getKind() const {
88  return static_cast<attr::Kind>(AttrKind);
89  }
90 
91  unsigned getSpellingListIndex() const { return SpellingListIndex; }
92  const char *getSpelling() const;
93 
94  SourceLocation getLocation() const { return Range.getBegin(); }
95  SourceRange getRange() const { return Range; }
96  void setRange(SourceRange R) { Range = R; }
97 
98  bool isInherited() const { return Inherited; }
99 
100  /// Returns true if the attribute has been implicitly created instead
101  /// of explicitly written by the user.
102  bool isImplicit() const { return Implicit; }
103  void setImplicit(bool I) { Implicit = I; }
104 
105  void setPackExpansion(bool PE) { IsPackExpansion = PE; }
106  bool isPackExpansion() const { return IsPackExpansion; }
107 
108  // Clone this attribute.
109  Attr *clone(ASTContext &C) const;
110 
111  bool isLateParsed() const { return IsLateParsed; }
112 
113  // Pretty print this attribute.
114  void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const;
115 };
116 
117 class TypeAttr : public Attr {
118 protected:
120  bool IsLateParsed)
121  : Attr(AK, R, SpellingListIndex, IsLateParsed) {}
122 
123 public:
124  static bool classof(const Attr *A) {
125  return A->getKind() >= attr::FirstTypeAttr &&
126  A->getKind() <= attr::LastTypeAttr;
127  }
128 };
129 
130 class StmtAttr : public Attr {
131 protected:
133  bool IsLateParsed)
134  : Attr(AK, R, SpellingListIndex, IsLateParsed) {}
135 
136 public:
137  static bool classof(const Attr *A) {
138  return A->getKind() >= attr::FirstStmtAttr &&
139  A->getKind() <= attr::LastStmtAttr;
140  }
141 };
142 
143 class InheritableAttr : public Attr {
144 protected:
147  : Attr(AK, R, SpellingListIndex, 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:
171  : InheritableAttr(AK, R, SpellingListIndex, IsLateParsed,
172  InheritEvenIfAlreadyPresent) {}
173 
174 public:
175  // Implement isa/cast/dyncast/etc.
176  static bool classof(const Attr *A) {
177  return A->getKind() >= attr::FirstInheritableParamAttr &&
178  A->getKind() <= attr::LastInheritableParamAttr;
179  }
180 };
181 
182 /// A parameter attribute which changes the argument-passing ABI rule
183 /// for the parameter.
185 protected:
187  unsigned SpellingListIndex, bool IsLateParsed,
189  : InheritableParamAttr(AK, R, SpellingListIndex, IsLateParsed,
190  InheritEvenIfAlreadyPresent) {}
191 
192 public:
194  switch (getKind()) {
195  case attr::SwiftContext:
197  case attr::SwiftErrorResult:
199  case attr::SwiftIndirectResult:
201  default:
202  llvm_unreachable("bad parameter ABI attribute kind");
203  }
204  }
205 
206  static bool classof(const Attr *A) {
207  return A->getKind() >= attr::FirstParameterABIAttr &&
208  A->getKind() <= attr::LastParameterABIAttr;
209  }
210 };
211 
212 /// A single parameter index whose accessors require each use to make explicit
213 /// the parameter index encoding needed.
214 class ParamIdx {
215  // Idx is exposed only via accessors that specify specific encodings.
216  unsigned Idx : 30;
217  unsigned HasThis : 1;
218  unsigned IsValid : 1;
219 
220  void assertComparable(const ParamIdx &I) const {
221  assert(isValid() && I.isValid() &&
222  "ParamIdx must be valid to be compared");
223  // It's possible to compare indices from separate functions, but so far
224  // it's not proven useful. Moreover, it might be confusing because a
225  // comparison on the results of getASTIndex might be inconsistent with a
226  // comparison on the ParamIdx objects themselves.
227  assert(HasThis == I.HasThis &&
228  "ParamIdx must be for the same function to be compared");
229  }
230 
231 public:
232  /// Construct an invalid parameter index (\c isValid returns false and
233  /// accessors fail an assert).
234  ParamIdx() : Idx(0), HasThis(false), IsValid(false) {}
235 
236  /// \param Idx is the parameter index as it is normally specified in
237  /// attributes in the source: one-origin including any C++ implicit this
238  /// parameter.
239  ///
240  /// \param D is the declaration containing the parameters. It is used to
241  /// determine if there is a C++ implicit this parameter.
242  ParamIdx(unsigned Idx, const Decl *D)
243  : Idx(Idx), HasThis(false), IsValid(true) {
244  assert(Idx >= 1 && "Idx must be one-origin");
245  if (const auto *FD = dyn_cast<FunctionDecl>(D))
246  HasThis = FD->isCXXInstanceMember();
247  }
248 
249  /// A type into which \c ParamIdx can be serialized.
250  ///
251  /// A static assertion that it's of the correct size follows the \c ParamIdx
252  /// class definition.
253  typedef uint32_t SerialType;
254 
255  /// Produce a representation that can later be passed to \c deserialize to
256  /// construct an equivalent \c ParamIdx.
257  SerialType serialize() const {
258  return *reinterpret_cast<const SerialType *>(this);
259  }
260 
261  /// Construct from a result from \c serialize.
262  static ParamIdx deserialize(SerialType S) {
263  ParamIdx P(*reinterpret_cast<ParamIdx *>(&S));
264  assert((!P.IsValid || P.Idx >= 1) && "valid Idx must be one-origin");
265  return P;
266  }
267 
268  /// Is this parameter index valid?
269  bool isValid() const { return IsValid; }
270 
271  /// Get the parameter index as it would normally be encoded for attributes at
272  /// the source level of representation: one-origin including any C++ implicit
273  /// this parameter.
274  ///
275  /// This encoding thus makes sense for diagnostics, pretty printing, and
276  /// constructing new attributes from a source-like specification.
277  unsigned getSourceIndex() const {
278  assert(isValid() && "ParamIdx must be valid");
279  return Idx;
280  }
281 
282  /// Get the parameter index as it would normally be encoded at the AST level
283  /// of representation: zero-origin not including any C++ implicit this
284  /// parameter.
285  ///
286  /// This is the encoding primarily used in Sema. However, in diagnostics,
287  /// Sema uses \c getSourceIndex instead.
288  unsigned getASTIndex() const {
289  assert(isValid() && "ParamIdx must be valid");
290  assert(Idx >= 1 + HasThis &&
291  "stored index must be base-1 and not specify C++ implicit this");
292  return Idx - 1 - HasThis;
293  }
294 
295  /// Get the parameter index as it would normally be encoded at the LLVM level
296  /// of representation: zero-origin including any C++ implicit this parameter.
297  ///
298  /// This is the encoding primarily used in CodeGen.
299  unsigned getLLVMIndex() const {
300  assert(isValid() && "ParamIdx must be valid");
301  assert(Idx >= 1 && "stored index must be base-1");
302  return Idx - 1;
303  }
304 
305  bool operator==(const ParamIdx &I) const {
306  assertComparable(I);
307  return Idx == I.Idx;
308  }
309  bool operator!=(const ParamIdx &I) const {
310  assertComparable(I);
311  return Idx != I.Idx;
312  }
313  bool operator<(const ParamIdx &I) const {
314  assertComparable(I);
315  return Idx < I.Idx;
316  }
317  bool operator>(const ParamIdx &I) const {
318  assertComparable(I);
319  return Idx > I.Idx;
320  }
321  bool operator<=(const ParamIdx &I) const {
322  assertComparable(I);
323  return Idx <= I.Idx;
324  }
325  bool operator>=(const ParamIdx &I) const {
326  assertComparable(I);
327  return Idx >= I.Idx;
328  }
329 };
330 
331 static_assert(sizeof(ParamIdx) == sizeof(ParamIdx::SerialType),
332  "ParamIdx does not fit its serialization type");
333 
334 #include "clang/AST/Attrs.inc"
335 
337  const Attr *At) {
338  DB.AddTaggedVal(reinterpret_cast<intptr_t>(At),
340  return DB;
341 }
342 
344  const Attr *At) {
345  PD.AddTaggedVal(reinterpret_cast<intptr_t>(At),
347  return PD;
348 }
349 } // end namespace clang
350 
351 #endif
const char * getSpelling() const
Attr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex, bool IsLateParsed)
Definition: Attr.h:79
ParamIdx()
Construct an invalid parameter index (isValid returns false and accessors fail an assert)...
Definition: Attr.h:234
C Language Family Type Representation.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:87
void AddTaggedVal(intptr_t V, DiagnosticsEngine::ArgumentKind Kind) const
unsigned Implicit
Definition: Attr.h:55
StringRef P
const DiagnosticBuilder & operator<<(const DiagnosticBuilder &DB, const Attr *At)
Definition: Attr.h:336
unsigned Inherited
Definition: Attr.h:53
InheritableAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:145
static bool classof(const Attr *A)
Definition: Attr.h:206
bool operator<=(const ParamIdx &I) const
Definition: Attr.h:321
Describes how types, statements, expressions, and declarations should be printed. ...
Definition: PrettyPrinter.h:38
static StringRef bytes(const std::vector< T, Allocator > &v)
Definition: ASTWriter.cpp:120
bool isValid() const
Is this parameter index valid?
Definition: Attr.h:269
Defines the clang::SanitizerKind enum.
void AddTaggedVal(intptr_t V, DiagnosticsEngine::ArgumentKind Kind) const
Definition: Diagnostic.h:1156
unsigned InheritEvenIfAlreadyPresent
Definition: Attr.h:59
bool isImplicit() const
Returns true if the attribute has been implicitly created instead of explicitly written by the user...
Definition: Attr.h:102
ParameterABI getABI() const
Definition: Attr.h:193
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:155
unsigned getSourceIndex() const
Get the parameter index as it would normally be encoded for attributes at the source level of represe...
Definition: Attr.h:277
Defines the clang::attr::Kind enum.
Defines some OpenMP-specific enums and functions.
This parameter (which must have pointer type) uses the special Swift context-pointer ABI treatment...
void setRange(SourceRange R)
Definition: Attr.h:96
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
static bool classof(const Attr *A)
Definition: Attr.h:137
This parameter (which must have pointer-to-pointer type) uses the special Swift error-result ABI trea...
InheritableParamAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:169
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:1043
static bool classof(const Attr *A)
Definition: Attr.h:161
unsigned getLLVMIndex() const
Get the parameter index as it would normally be encoded at the LLVM level of representation: zero-ori...
Definition: Attr.h:299
bool operator<(const ParamIdx &I) const
Definition: Attr.h:313
bool operator!=(const ParamIdx &I) const
Definition: Attr.h:309
A single parameter index whose accessors require each use to make explicit the parameter index encodi...
Definition: Attr.h:214
TypeAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex, bool IsLateParsed)
Definition: Attr.h:119
bool isPackExpansion() const
Definition: Attr.h:106
unsigned IsPackExpansion
Definition: Attr.h:54
bool shouldInheritEvenIfAlreadyPresent() const
Should this attribute be inherited from a prior declaration even if it&#39;s explicitly provided in the c...
Definition: Attr.h:156
bool operator==(const ParamIdx &I) const
Definition: Attr.h:305
static bool classof(const Attr *A)
Definition: Attr.h:124
bool operator>(const ParamIdx &I) const
Definition: Attr.h:317
unsigned getASTIndex() const
Get the parameter index as it would normally be encoded at the AST level of representation: zero-orig...
Definition: Attr.h:288
A parameter attribute which changes the argument-passing ABI rule for the parameter.
Definition: Attr.h:184
void setImplicit(bool I)
Definition: Attr.h:103
#define false
Definition: stdbool.h:33
Encodes a location in the source.
Attr * clone(ASTContext &C) const
unsigned getSpellingListIndex() const
Definition: Attr.h:91
void setPackExpansion(bool PE)
Definition: Attr.h:105
StmtAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex, bool IsLateParsed)
Definition: Attr.h:132
ParamIdx(unsigned Idx, const Decl *D)
Definition: Attr.h:242
SourceLocation getLocation() const
Definition: Attr.h:94
void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const
ParameterABI
Kinds of parameter ABI.
Definition: Specifiers.h:309
SourceRange getRange() const
Definition: Attr.h:95
Dataflow Directional Tag Classes.
static ParamIdx deserialize(SerialType S)
Construct from a result from serialize.
Definition: Attr.h:262
unsigned SpellingListIndex
An index into the spelling list of an attribute defined in Attr.td file.
Definition: Attr.h:52
bool isLateParsed() const
Definition: Attr.h:111
SerialType serialize() const
Produce a representation that can later be passed to deserialize to construct an equivalent ParamIdx...
Definition: Attr.h:257
uint32_t SerialType
A type into which ParamIdx can be serialized.
Definition: Attr.h:253
void setInherited(bool I)
Definition: Attr.h:152
static bool classof(const Attr *A)
Definition: Attr.h:176
Defines the clang::SourceLocation class and associated facilities.
bool isInherited() const
Definition: Attr.h:98
bool operator>=(const ParamIdx &I) const
Definition: Attr.h:325
ParameterABIAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:186
#define true
Definition: stdbool.h:32
A trivial tuple used to represent a source range.
unsigned IsLateParsed
Definition: Attr.h:58
attr::Kind getKind() const
Definition: Attr.h:87
SourceLocation getBegin() const
Attr - This represents one attribute.
Definition: Attr.h:44
This parameter (which must have pointer type) is a Swift indirect result parameter.