clang  6.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"
27 #include "llvm/ADT/StringSwitch.h"
28 #include "llvm/Support/ErrorHandling.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  unsigned IsLateParsed : 1;
56  unsigned DuplicatesAllowed : 1;
57 
58  void *operator new(size_t bytes) noexcept {
59  llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
60  }
61  void operator delete(void *data) noexcept {
62  llvm_unreachable("Attrs cannot be released with regular 'delete'.");
63  }
64 
65 public:
66  // Forward so that the regular new and delete do not hide global ones.
67  void *operator new(size_t Bytes, ASTContext &C,
68  size_t Alignment = 8) noexcept {
69  return ::operator new(Bytes, C, Alignment);
70  }
71  void operator delete(void *Ptr, ASTContext &C, size_t Alignment) noexcept {
72  return ::operator delete(Ptr, C, Alignment);
73  }
74 
75 protected:
78  : Range(R), AttrKind(AK), SpellingListIndex(SpellingListIndex),
80  IsLateParsed(IsLateParsed), DuplicatesAllowed(DuplicatesAllowed) {}
81 
82 public:
83 
84  attr::Kind getKind() const {
85  return static_cast<attr::Kind>(AttrKind);
86  }
87 
88  unsigned getSpellingListIndex() const { return SpellingListIndex; }
89  const char *getSpelling() const;
90 
91  SourceLocation getLocation() const { return Range.getBegin(); }
92  SourceRange getRange() const { return Range; }
93  void setRange(SourceRange R) { Range = R; }
94 
95  bool isInherited() const { return Inherited; }
96 
97  /// \brief 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  /// \brief By default, attributes cannot be duplicated when being merged;
114  /// however, an attribute can override this. Returns true if the attribute
115  /// can be duplicated when merging.
116  bool duplicatesAllowed() const { return DuplicatesAllowed; }
117 };
118 
119 class StmtAttr : public Attr {
120 protected:
122  bool IsLateParsed, bool DuplicatesAllowed)
123  : Attr(AK, R, SpellingListIndex, IsLateParsed, DuplicatesAllowed) {}
124 
125 public:
126  static bool classof(const Attr *A) {
127  return A->getKind() >= attr::FirstStmtAttr &&
128  A->getKind() <= attr::LastStmtAttr;
129  }
130 };
131 
132 class InheritableAttr : public Attr {
133 protected:
135  bool IsLateParsed, bool DuplicatesAllowed)
136  : Attr(AK, R, SpellingListIndex, IsLateParsed, DuplicatesAllowed) {}
137 
138 public:
139  void setInherited(bool I) { Inherited = I; }
140 
141  // Implement isa/cast/dyncast/etc.
142  static bool classof(const Attr *A) {
143  return A->getKind() >= attr::FirstInheritableAttr &&
144  A->getKind() <= attr::LastInheritableAttr;
145  }
146 };
147 
149 protected:
151  bool IsLateParsed, bool DuplicatesAllowed)
152  : InheritableAttr(AK, R, SpellingListIndex, IsLateParsed,
153  DuplicatesAllowed) {}
154 
155 public:
156  // Implement isa/cast/dyncast/etc.
157  static bool classof(const Attr *A) {
158  return A->getKind() >= attr::FirstInheritableParamAttr &&
159  A->getKind() <= attr::LastInheritableParamAttr;
160  }
161 };
162 
163 /// A parameter attribute which changes the argument-passing ABI rule
164 /// for the parameter.
166 protected:
168  unsigned SpellingListIndex, bool IsLateParsed,
169  bool DuplicatesAllowed)
170  : InheritableParamAttr(AK, R, SpellingListIndex, IsLateParsed,
171  DuplicatesAllowed) {}
172 
173 public:
175  switch (getKind()) {
176  case attr::SwiftContext:
178  case attr::SwiftErrorResult:
180  case attr::SwiftIndirectResult:
182  default:
183  llvm_unreachable("bad parameter ABI attribute kind");
184  }
185  }
186 
187  static bool classof(const Attr *A) {
188  return A->getKind() >= attr::FirstParameterABIAttr &&
189  A->getKind() <= attr::LastParameterABIAttr;
190  }
191 };
192 
193 #include "clang/AST/Attrs.inc"
194 
196  const Attr *At) {
197  DB.AddTaggedVal(reinterpret_cast<intptr_t>(At),
199  return DB;
200 }
201 
203  const Attr *At) {
204  PD.AddTaggedVal(reinterpret_cast<intptr_t>(At),
206  return PD;
207 }
208 } // end namespace clang
209 
210 #endif
const char * getSpelling() const
C Language Family Type Representation.
void AddTaggedVal(intptr_t V, DiagnosticsEngine::ArgumentKind Kind) const
unsigned Implicit
Definition: Attr.h:54
const DiagnosticBuilder & operator<<(const DiagnosticBuilder &DB, const Attr *At)
Definition: Attr.h:195
unsigned Inherited
Definition: Attr.h:52
static bool classof(const Attr *A)
Definition: Attr.h:187
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:100
Defines the clang::SanitizerKind enum.
void AddTaggedVal(intptr_t V, DiagnosticsEngine::ArgumentKind Kind) const
Definition: Diagnostic.h:1067
bool isImplicit() const
Returns true if the attribute has been implicitly created instead of explicitly written by the user...
Definition: Attr.h:99
ParameterABI getABI() const
Definition: Attr.h:174
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:128
Defines the clang::attr::Kind enum.
Defines some OpenMP-specific enums and functions.
InheritableParamAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex, bool IsLateParsed, bool DuplicatesAllowed)
Definition: Attr.h:150
This parameter (which must have pointer type) uses the special Swift context-pointer ABI treatment...
void setRange(SourceRange R)
Definition: Attr.h:93
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
Attr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex, bool IsLateParsed, bool DuplicatesAllowed)
Definition: Attr.h:76
static bool classof(const Attr *A)
Definition: Attr.h:126
This parameter (which must have pointer-to-pointer type) uses the special Swift error-result ABI trea...
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:953
static bool classof(const Attr *A)
Definition: Attr.h:142
bool isPackExpansion() const
Definition: Attr.h:103
unsigned DuplicatesAllowed
Definition: Attr.h:56
unsigned IsPackExpansion
Definition: Attr.h:53
A parameter attribute which changes the argument-passing ABI rule for the parameter.
Definition: Attr.h:165
void setImplicit(bool I)
Definition: Attr.h:100
#define false
Definition: stdbool.h:33
Encodes a location in the source.
bool duplicatesAllowed() const
By default, attributes cannot be duplicated when being merged; however, an attribute can override thi...
Definition: Attr.h:116
Attr * clone(ASTContext &C) const
unsigned getSpellingListIndex() const
Definition: Attr.h:88
void setPackExpansion(bool PE)
Definition: Attr.h:102
SourceLocation getLocation() const
Definition: Attr.h:91
void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const
ParameterABI
Kinds of parameter ABI.
Definition: Specifiers.h:299
SourceRange getRange() const
Definition: Attr.h:92
Dataflow Directional Tag Classes.
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:108
void setInherited(bool I)
Definition: Attr.h:139
static bool classof(const Attr *A)
Definition: Attr.h:157
Defines the clang::SourceLocation class and associated facilities.
InheritableAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex, bool IsLateParsed, bool DuplicatesAllowed)
Definition: Attr.h:134
bool isInherited() const
Definition: Attr.h:95
StmtAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex, bool IsLateParsed, bool DuplicatesAllowed)
Definition: Attr.h:121
Defines the clang::VersionTuple class, which represents a version in the form major[.minor[.subminor]].
ParameterABIAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex, bool IsLateParsed, bool DuplicatesAllowed)
Definition: Attr.h:167
A trivial tuple used to represent a source range.
unsigned IsLateParsed
Definition: Attr.h:55
attr::Kind getKind() const
Definition: Attr.h:84
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.