clang  7.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/AttrIterator.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/Type.h"
21 #include "clang/Basic/AttrKinds.h"
22 #include "clang/Basic/LLVM.h"
24 #include "clang/Basic/Sanitizers.h"
26 #include "llvm/ADT/StringSwitch.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 IdentifierInfo;
36  class ObjCInterfaceDecl;
37  class Expr;
38  class QualType;
39  class FunctionDecl;
40  class TypeSourceInfo;
41 
42 /// Attr - This represents one attribute.
43 class Attr {
44 private:
45  SourceRange Range;
46  unsigned AttrKind : 16;
47 
48 protected:
49  /// An index into the spelling list of an
50  /// attribute defined in Attr.td file.
51  unsigned SpellingListIndex : 4;
52  unsigned Inherited : 1;
53  unsigned IsPackExpansion : 1;
54  unsigned Implicit : 1;
55  // FIXME: These are properties of the attribute kind, not state for this
56  // instance of the attribute.
57  unsigned IsLateParsed : 1;
59 
60  void *operator new(size_t bytes) noexcept {
61  llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
62  }
63  void operator delete(void *data) noexcept {
64  llvm_unreachable("Attrs cannot be released with regular 'delete'.");
65  }
66 
67 public:
68  // Forward so that the regular new and delete do not hide global ones.
69  void *operator new(size_t Bytes, ASTContext &C,
70  size_t Alignment = 8) noexcept {
71  return ::operator new(Bytes, C, Alignment);
72  }
73  void operator delete(void *Ptr, ASTContext &C, size_t Alignment) noexcept {
74  return ::operator delete(Ptr, C, Alignment);
75  }
76 
77 protected:
79  bool IsLateParsed)
80  : Range(R), AttrKind(AK), SpellingListIndex(SpellingListIndex),
82  IsLateParsed(IsLateParsed), InheritEvenIfAlreadyPresent(false) {}
83 
84 public:
85 
86  attr::Kind getKind() const {
87  return static_cast<attr::Kind>(AttrKind);
88  }
89 
90  unsigned getSpellingListIndex() const { return SpellingListIndex; }
91  const char *getSpelling() const;
92 
93  SourceLocation getLocation() const { return Range.getBegin(); }
94  SourceRange getRange() const { return Range; }
95  void setRange(SourceRange R) { Range = R; }
96 
97  bool isInherited() const { return Inherited; }
98 
99  /// Returns true if the attribute has been implicitly created instead
100  /// of explicitly written by the user.
101  bool isImplicit() const { return Implicit; }
102  void setImplicit(bool I) { Implicit = I; }
103 
104  void setPackExpansion(bool PE) { IsPackExpansion = PE; }
105  bool isPackExpansion() const { return IsPackExpansion; }
106 
107  // Clone this attribute.
108  Attr *clone(ASTContext &C) const;
109 
110  bool isLateParsed() const { return IsLateParsed; }
111 
112  // Pretty print this attribute.
113  void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const;
114 };
115 
116 class StmtAttr : public Attr {
117 protected:
119  bool IsLateParsed)
120  : Attr(AK, R, SpellingListIndex, IsLateParsed) {}
121 
122 public:
123  static bool classof(const Attr *A) {
124  return A->getKind() >= attr::FirstStmtAttr &&
125  A->getKind() <= attr::LastStmtAttr;
126  }
127 };
128 
129 class InheritableAttr : public Attr {
130 protected:
133  : Attr(AK, R, SpellingListIndex, IsLateParsed) {
134  this->InheritEvenIfAlreadyPresent = InheritEvenIfAlreadyPresent;
135  }
136 
137 public:
138  void setInherited(bool I) { Inherited = I; }
139 
140  /// Should this attribute be inherited from a prior declaration even if it's
141  /// explicitly provided in the current declaration?
144  }
145 
146  // Implement isa/cast/dyncast/etc.
147  static bool classof(const Attr *A) {
148  return A->getKind() >= attr::FirstInheritableAttr &&
149  A->getKind() <= attr::LastInheritableAttr;
150  }
151 };
152 
154 protected:
157  : InheritableAttr(AK, R, SpellingListIndex, IsLateParsed,
158  InheritEvenIfAlreadyPresent) {}
159 
160 public:
161  // Implement isa/cast/dyncast/etc.
162  static bool classof(const Attr *A) {
163  return A->getKind() >= attr::FirstInheritableParamAttr &&
164  A->getKind() <= attr::LastInheritableParamAttr;
165  }
166 };
167 
168 /// A parameter attribute which changes the argument-passing ABI rule
169 /// for the parameter.
171 protected:
173  unsigned SpellingListIndex, bool IsLateParsed,
175  : InheritableParamAttr(AK, R, SpellingListIndex, IsLateParsed,
176  InheritEvenIfAlreadyPresent) {}
177 
178 public:
180  switch (getKind()) {
181  case attr::SwiftContext:
183  case attr::SwiftErrorResult:
185  case attr::SwiftIndirectResult:
187  default:
188  llvm_unreachable("bad parameter ABI attribute kind");
189  }
190  }
191 
192  static bool classof(const Attr *A) {
193  return A->getKind() >= attr::FirstParameterABIAttr &&
194  A->getKind() <= attr::LastParameterABIAttr;
195  }
196 };
197 
198 /// A single parameter index whose accessors require each use to make explicit
199 /// the parameter index encoding needed.
200 class ParamIdx {
201  // Idx is exposed only via accessors that specify specific encodings.
202  unsigned Idx : 30;
203  unsigned HasThis : 1;
204  unsigned IsValid : 1;
205 
206  void assertComparable(const ParamIdx &I) const {
207  assert(isValid() && I.isValid() &&
208  "ParamIdx must be valid to be compared");
209  // It's possible to compare indices from separate functions, but so far
210  // it's not proven useful. Moreover, it might be confusing because a
211  // comparison on the results of getASTIndex might be inconsistent with a
212  // comparison on the ParamIdx objects themselves.
213  assert(HasThis == I.HasThis &&
214  "ParamIdx must be for the same function to be compared");
215  }
216 
217 public:
218  /// Construct an invalid parameter index (\c isValid returns false and
219  /// accessors fail an assert).
220  ParamIdx() : Idx(0), HasThis(false), IsValid(false) {}
221 
222  /// \param Idx is the parameter index as it is normally specified in
223  /// attributes in the source: one-origin including any C++ implicit this
224  /// parameter.
225  ///
226  /// \param D is the declaration containing the parameters. It is used to
227  /// determine if there is a C++ implicit this parameter.
228  ParamIdx(unsigned Idx, const Decl *D)
229  : Idx(Idx), HasThis(false), IsValid(true) {
230  assert(Idx >= 1 && "Idx must be one-origin");
231  if (const auto *FD = dyn_cast<FunctionDecl>(D))
232  HasThis = FD->isCXXInstanceMember();
233  }
234 
235  /// A type into which \c ParamIdx can be serialized.
236  ///
237  /// A static assertion that it's of the correct size follows the \c ParamIdx
238  /// class definition.
239  typedef uint32_t SerialType;
240 
241  /// Produce a representation that can later be passed to \c deserialize to
242  /// construct an equivalent \c ParamIdx.
243  SerialType serialize() const {
244  return *reinterpret_cast<const SerialType *>(this);
245  }
246 
247  /// Construct from a result from \c serialize.
248  static ParamIdx deserialize(SerialType S) {
249  ParamIdx P(*reinterpret_cast<ParamIdx *>(&S));
250  assert((!P.IsValid || P.Idx >= 1) && "valid Idx must be one-origin");
251  return P;
252  }
253 
254  /// Is this parameter index valid?
255  bool isValid() const { return IsValid; }
256 
257  /// Get the parameter index as it would normally be encoded for attributes at
258  /// the source level of representation: one-origin including any C++ implicit
259  /// this parameter.
260  ///
261  /// This encoding thus makes sense for diagnostics, pretty printing, and
262  /// constructing new attributes from a source-like specification.
263  unsigned getSourceIndex() const {
264  assert(isValid() && "ParamIdx must be valid");
265  return Idx;
266  }
267 
268  /// Get the parameter index as it would normally be encoded at the AST level
269  /// of representation: zero-origin not including any C++ implicit this
270  /// parameter.
271  ///
272  /// This is the encoding primarily used in Sema. However, in diagnostics,
273  /// Sema uses \c getSourceIndex instead.
274  unsigned getASTIndex() const {
275  assert(isValid() && "ParamIdx must be valid");
276  assert(Idx >= 1 + HasThis &&
277  "stored index must be base-1 and not specify C++ implicit this");
278  return Idx - 1 - HasThis;
279  }
280 
281  /// Get the parameter index as it would normally be encoded at the LLVM level
282  /// of representation: zero-origin including any C++ implicit this parameter.
283  ///
284  /// This is the encoding primarily used in CodeGen.
285  unsigned getLLVMIndex() const {
286  assert(isValid() && "ParamIdx must be valid");
287  assert(Idx >= 1 && "stored index must be base-1");
288  return Idx - 1;
289  }
290 
291  bool operator==(const ParamIdx &I) const {
292  assertComparable(I);
293  return Idx == I.Idx;
294  }
295  bool operator!=(const ParamIdx &I) const {
296  assertComparable(I);
297  return Idx != I.Idx;
298  }
299  bool operator<(const ParamIdx &I) const {
300  assertComparable(I);
301  return Idx < I.Idx;
302  }
303  bool operator>(const ParamIdx &I) const {
304  assertComparable(I);
305  return Idx > I.Idx;
306  }
307  bool operator<=(const ParamIdx &I) const {
308  assertComparable(I);
309  return Idx <= I.Idx;
310  }
311  bool operator>=(const ParamIdx &I) const {
312  assertComparable(I);
313  return Idx >= I.Idx;
314  }
315 };
316 
317 static_assert(sizeof(ParamIdx) == sizeof(ParamIdx::SerialType),
318  "ParamIdx does not fit its serialization type");
319 
320 #include "clang/AST/Attrs.inc"
321 
323  const Attr *At) {
324  DB.AddTaggedVal(reinterpret_cast<intptr_t>(At),
326  return DB;
327 }
328 
330  const Attr *At) {
331  PD.AddTaggedVal(reinterpret_cast<intptr_t>(At),
333  return PD;
334 }
335 } // end namespace clang
336 
337 #endif
const char * getSpelling() const
Attr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex, bool IsLateParsed)
Definition: Attr.h:78
ParamIdx()
Construct an invalid parameter index (isValid returns false and accessors fail an assert)...
Definition: Attr.h:220
C Language Family Type Representation.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
void AddTaggedVal(intptr_t V, DiagnosticsEngine::ArgumentKind Kind) const
unsigned Implicit
Definition: Attr.h:54
StringRef P
const DiagnosticBuilder & operator<<(const DiagnosticBuilder &DB, const Attr *At)
Definition: Attr.h:322
unsigned Inherited
Definition: Attr.h:52
InheritableAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:131
static bool classof(const Attr *A)
Definition: Attr.h:192
bool operator<=(const ParamIdx &I) const
Definition: Attr.h:307
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:119
bool isValid() const
Is this parameter index valid?
Definition: Attr.h:255
Defines the clang::SanitizerKind enum.
void AddTaggedVal(intptr_t V, DiagnosticsEngine::ArgumentKind Kind) const
Definition: Diagnostic.h:1155
unsigned InheritEvenIfAlreadyPresent
Definition: Attr.h:58
bool isImplicit() const
Returns true if the attribute has been implicitly created instead of explicitly written by the user...
Definition: Attr.h:101
ParameterABI getABI() const
Definition: Attr.h:179
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:150
unsigned getSourceIndex() const
Get the parameter index as it would normally be encoded for attributes at the source level of represe...
Definition: Attr.h:263
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:95
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
static bool classof(const Attr *A)
Definition: Attr.h:123
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:155
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:1042
static bool classof(const Attr *A)
Definition: Attr.h:147
unsigned getLLVMIndex() const
Get the parameter index as it would normally be encoded at the LLVM level of representation: zero-ori...
Definition: Attr.h:285
bool operator<(const ParamIdx &I) const
Definition: Attr.h:299
bool operator!=(const ParamIdx &I) const
Definition: Attr.h:295
A single parameter index whose accessors require each use to make explicit the parameter index encodi...
Definition: Attr.h:200
bool isPackExpansion() const
Definition: Attr.h:105
unsigned IsPackExpansion
Definition: Attr.h:53
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:142
bool operator==(const ParamIdx &I) const
Definition: Attr.h:291
bool operator>(const ParamIdx &I) const
Definition: Attr.h:303
unsigned getASTIndex() const
Get the parameter index as it would normally be encoded at the AST level of representation: zero-orig...
Definition: Attr.h:274
A parameter attribute which changes the argument-passing ABI rule for the parameter.
Definition: Attr.h:170
void setImplicit(bool I)
Definition: Attr.h:102
#define false
Definition: stdbool.h:33
Encodes a location in the source.
Attr * clone(ASTContext &C) const
unsigned getSpellingListIndex() const
Definition: Attr.h:90
void setPackExpansion(bool PE)
Definition: Attr.h:104
StmtAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex, bool IsLateParsed)
Definition: Attr.h:118
ParamIdx(unsigned Idx, const Decl *D)
Definition: Attr.h:228
SourceLocation getLocation() const
Definition: Attr.h:93
void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const
ParameterABI
Kinds of parameter ABI.
Definition: Specifiers.h:302
SourceRange getRange() const
Definition: Attr.h:94
Dataflow Directional Tag Classes.
static ParamIdx deserialize(SerialType S)
Construct from a result from serialize.
Definition: Attr.h:248
unsigned SpellingListIndex
An index into the spelling list of an attribute defined in Attr.td file.
Definition: Attr.h:51
bool isLateParsed() const
Definition: Attr.h:110
SerialType serialize() const
Produce a representation that can later be passed to deserialize to construct an equivalent ParamIdx...
Definition: Attr.h:243
uint32_t SerialType
A type into which ParamIdx can be serialized.
Definition: Attr.h:239
void setInherited(bool I)
Definition: Attr.h:138
static bool classof(const Attr *A)
Definition: Attr.h:162
Defines the clang::SourceLocation class and associated facilities.
bool isInherited() const
Definition: Attr.h:97
bool operator>=(const ParamIdx &I) const
Definition: Attr.h:311
ParameterABIAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex, bool IsLateParsed, bool InheritEvenIfAlreadyPresent)
Definition: Attr.h:172
#define true
Definition: stdbool.h:32
A trivial tuple used to represent a source range.
unsigned IsLateParsed
Definition: Attr.h:57
attr::Kind getKind() const
Definition: Attr.h:86
SourceLocation getBegin() const
Attr - This represents one attribute.
Definition: Attr.h:43
This parameter (which must have pointer type) is a Swift indirect result parameter.