clang  3.9.0
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/SmallVector.h"
28 #include "llvm/ADT/StringRef.h"
29 #include "llvm/ADT/StringSwitch.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include <algorithm>
33 #include <cassert>
34 
35 namespace clang {
36  class ASTContext;
37  class IdentifierInfo;
38  class ObjCInterfaceDecl;
39  class Expr;
40  class QualType;
41  class FunctionDecl;
42  class TypeSourceInfo;
43 
44 /// Attr - This represents one attribute.
45 class Attr {
46 private:
47  SourceRange Range;
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 SpellingListIndex : 4;
54  unsigned Inherited : 1;
55  unsigned IsPackExpansion : 1;
56  unsigned Implicit : 1;
57  unsigned IsLateParsed : 1;
58  unsigned DuplicatesAllowed : 1;
59 
60  void *operator new(size_t bytes) LLVM_NOEXCEPT {
61  llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
62  }
63  void operator delete(void *data) LLVM_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) LLVM_NOEXCEPT {
71  return ::operator new(Bytes, C, Alignment);
72  }
73  void operator delete(void *Ptr, ASTContext &C,
74  size_t Alignment) LLVM_NOEXCEPT {
75  return ::operator delete(Ptr, C, Alignment);
76  }
77 
78 protected:
81  : Range(R), AttrKind(AK), SpellingListIndex(SpellingListIndex),
83  IsLateParsed(IsLateParsed), DuplicatesAllowed(DuplicatesAllowed) {}
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  /// \brief 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  /// \brief By default, attributes cannot be duplicated when being merged;
117  /// however, an attribute can override this. Returns true if the attribute
118  /// can be duplicated when merging.
119  bool duplicatesAllowed() const { return DuplicatesAllowed; }
120 };
121 
122 class StmtAttr : public Attr {
123 protected:
125  bool IsLateParsed, bool DuplicatesAllowed)
126  : Attr(AK, R, SpellingListIndex, IsLateParsed, DuplicatesAllowed) {}
127 
128 public:
129  static bool classof(const Attr *A) {
130  return A->getKind() >= attr::FirstStmtAttr &&
131  A->getKind() <= attr::LastStmtAttr;
132  }
133 };
134 
135 class InheritableAttr : public Attr {
136 protected:
138  bool IsLateParsed, bool DuplicatesAllowed)
139  : Attr(AK, R, SpellingListIndex, IsLateParsed, DuplicatesAllowed) {}
140 
141 public:
142  void setInherited(bool I) { Inherited = I; }
143 
144  // Implement isa/cast/dyncast/etc.
145  static bool classof(const Attr *A) {
146  return A->getKind() >= attr::FirstInheritableAttr &&
147  A->getKind() <= attr::LastInheritableAttr;
148  }
149 };
150 
152 protected:
154  bool IsLateParsed, bool DuplicatesAllowed)
155  : InheritableAttr(AK, R, SpellingListIndex, IsLateParsed,
156  DuplicatesAllowed) {}
157 
158 public:
159  // Implement isa/cast/dyncast/etc.
160  static bool classof(const Attr *A) {
161  return A->getKind() >= attr::FirstInheritableParamAttr &&
162  A->getKind() <= attr::LastInheritableParamAttr;
163  }
164 };
165 
166 /// A parameter attribute which changes the argument-passing ABI rule
167 /// for the parameter.
169 protected:
171  unsigned SpellingListIndex, bool IsLateParsed,
172  bool DuplicatesAllowed)
173  : InheritableParamAttr(AK, R, SpellingListIndex, IsLateParsed,
174  DuplicatesAllowed) {}
175 
176 public:
178  switch (getKind()) {
179  case attr::SwiftContext:
181  case attr::SwiftErrorResult:
183  case attr::SwiftIndirectResult:
185  default:
186  llvm_unreachable("bad parameter ABI attribute kind");
187  }
188  }
189 
190  static bool classof(const Attr *A) {
191  return A->getKind() >= attr::FirstParameterABIAttr &&
192  A->getKind() <= attr::LastParameterABIAttr;
193  }
194 };
195 
196 #include "clang/AST/Attrs.inc"
197 
199  const Attr *At) {
200  DB.AddTaggedVal(reinterpret_cast<intptr_t>(At),
202  return DB;
203 }
204 
206  const Attr *At) {
207  PD.AddTaggedVal(reinterpret_cast<intptr_t>(At),
209  return PD;
210 }
211 } // end namespace clang
212 
213 #endif
C Language Family Type Representation.
unsigned Implicit
Definition: Attr.h:56
const DiagnosticBuilder & operator<<(const DiagnosticBuilder &DB, const Attr *At)
Definition: Attr.h:198
unsigned Inherited
Definition: Attr.h:54
static bool classof(const Attr *A)
Definition: Attr.h:190
void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const
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:69
void AddTaggedVal(intptr_t V, DiagnosticsEngine::ArgumentKind Kind) const
Definition: Diagnostic.h:986
Defines the clang::SanitizerKind enum.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:92
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:153
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...
Attr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex, bool IsLateParsed, bool DuplicatesAllowed)
Definition: Attr.h:79
static bool classof(const Attr *A)
Definition: Attr.h:129
SourceLocation getLocation() const
Definition: Attr.h:94
detail::InMemoryDirectory::const_iterator I
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:873
static bool classof(const Attr *A)
Definition: Attr.h:145
SourceRange getRange() const
Definition: Attr.h:95
friend class ASTContext
Definition: Type.h:4178
bool isInherited() const
Definition: Attr.h:98
unsigned DuplicatesAllowed
Definition: Attr.h:58
unsigned IsPackExpansion
Definition: Attr.h:55
unsigned getSpellingListIndex() const
Definition: Attr.h:91
A parameter attribute which changes the argument-passing ABI rule for the parameter.
Definition: Attr.h:168
void setImplicit(bool I)
Definition: Attr.h:103
#define false
Definition: stdbool.h:33
const char * getSpelling() const
ParameterABI getABI() const
Definition: Attr.h:177
Encodes a location in the source.
void setPackExpansion(bool PE)
Definition: Attr.h:105
void AddTaggedVal(intptr_t V, DiagnosticsEngine::ArgumentKind Kind) const
bool isLateParsed() const
Definition: Attr.h:111
SourceLocation getBegin() const
Attr * clone(ASTContext &C) const
ParameterABI
Kinds of parameter ABI.
Definition: Specifiers.h:295
attr::Kind getKind() const
Definition: Attr.h:87
bool isImplicit() const
Returns true if the attribute has been implicitly created instead of explicitly written by the user...
Definition: Attr.h:102
unsigned SpellingListIndex
An index into the spelling list of an attribute defined in Attr.td file.
Definition: Attr.h:53
void setInherited(bool I)
Definition: Attr.h:142
static bool classof(const Attr *A)
Definition: Attr.h:160
Defines the clang::SourceLocation class and associated facilities.
InheritableAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex, bool IsLateParsed, bool DuplicatesAllowed)
Definition: Attr.h:137
bool duplicatesAllowed() const
By default, attributes cannot be duplicated when being merged; however, an attribute can override thi...
Definition: Attr.h:119
StmtAttr(attr::Kind AK, SourceRange R, unsigned SpellingListIndex, bool IsLateParsed, bool DuplicatesAllowed)
Definition: Attr.h:124
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:170
A trivial tuple used to represent a source range.
bool isPackExpansion() const
Definition: Attr.h:106
unsigned IsLateParsed
Definition: Attr.h:57
Attr - This represents one attribute.
Definition: Attr.h:45
This parameter (which must have pointer type) is a Swift indirect result parameter.