LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AttributeImpl.h
Go to the documentation of this file.
1 //===-- AttributeImpl.h - Attribute Internals -------------------*- 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 /// \file
11 /// \brief This file defines various helper methods and classes used by
12 /// LLVMContextImpl for creating and managing attributes.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_ATTRIBUTESIMPL_H
17 #define LLVM_ATTRIBUTESIMPL_H
18 
19 #include "llvm/ADT/FoldingSet.h"
20 #include "llvm/IR/Attributes.h"
21 #include <string>
22 
23 namespace llvm {
24 
25 class Constant;
26 class LLVMContext;
27 
28 //===----------------------------------------------------------------------===//
29 /// \class
30 /// \brief This class represents a single, uniqued attribute. That attribute
31 /// could be a single enum, a tuple, or a string.
32 class AttributeImpl : public FoldingSetNode {
33  unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
34 
35  // AttributesImpl is uniqued, these should not be publicly available.
36  void operator=(const AttributeImpl &) LLVM_DELETED_FUNCTION;
38 
39 protected:
44  };
45 
46  AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
47 
48 public:
49  virtual ~AttributeImpl();
50 
51  bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
52  bool isAlignAttribute() const { return KindID == AlignAttrEntry; }
53  bool isStringAttribute() const { return KindID == StringAttrEntry; }
54 
56  bool hasAttribute(StringRef Kind) const;
57 
59  uint64_t getValueAsInt() const;
60 
61  StringRef getKindAsString() const;
63 
64  /// \brief Used when sorting the attributes.
65  bool operator<(const AttributeImpl &AI) const;
66 
67  void Profile(FoldingSetNodeID &ID) const {
68  if (isEnumAttribute())
69  Profile(ID, getKindAsEnum(), 0);
70  else if (isAlignAttribute())
72  else
74  }
76  uint64_t Val) {
77  ID.AddInteger(Kind);
78  if (Val) ID.AddInteger(Val);
79  }
81  ID.AddString(Kind);
82  if (!Values.empty()) ID.AddString(Values);
83  }
84 
85  // FIXME: Remove this!
86  static uint64_t getAttrMask(Attribute::AttrKind Val);
87 };
88 
89 //===----------------------------------------------------------------------===//
90 /// \class
91 /// \brief A set of classes that contain the value of the
92 /// attribute object. There are three main categories: enum attribute entries,
93 /// represented by Attribute::AttrKind; alignment attribute entries; and string
94 /// attribute enties, which are for target-dependent attributes.
95 
97  virtual void anchor();
99 
100 protected:
102  : AttributeImpl(ID), Kind(Kind) {}
103 
104 public:
106  : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
107 
108  Attribute::AttrKind getEnumKind() const { return Kind; }
109 };
110 
112  virtual void anchor();
113  unsigned Align;
114 
115 public:
117  : EnumAttributeImpl(AlignAttrEntry, Kind), Align(Align) {
118  assert(
119  (Kind == Attribute::Alignment || Kind == Attribute::StackAlignment) &&
120  "Wrong kind for alignment attribute!");
121  }
122 
123  unsigned getAlignment() const { return Align; }
124 };
125 
127  virtual void anchor();
128  std::string Kind;
129  std::string Val;
130 
131 public:
133  : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
134 
135  StringRef getStringKind() const { return Kind; }
136  StringRef getStringValue() const { return Val; }
137 };
138 
139 //===----------------------------------------------------------------------===//
140 /// \class
141 /// \brief This class represents a group of attributes that apply to one
142 /// element: function, return type, or parameter.
144  unsigned NumAttrs; ///< Number of attributes in this node.
145 
146  AttributeSetNode(ArrayRef<Attribute> Attrs) : NumAttrs(Attrs.size()) {
147  // There's memory after the node where we can store the entries in.
148  std::copy(Attrs.begin(), Attrs.end(),
149  reinterpret_cast<Attribute *>(this + 1));
150  }
151 
152  // AttributesSetNode is uniqued, these should not be publicly available.
153  void operator=(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
155 public:
156  static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
157 
159  bool hasAttribute(StringRef Kind) const;
160  bool hasAttributes() const { return NumAttrs != 0; }
161 
163  Attribute getAttribute(StringRef Kind) const;
164 
165  unsigned getAlignment() const;
166  unsigned getStackAlignment() const;
167  std::string getAsString(bool InAttrGrp) const;
168 
169  typedef const Attribute *iterator;
170  iterator begin() const { return reinterpret_cast<iterator>(this + 1); }
171  iterator end() const { return begin() + NumAttrs; }
172 
173  void Profile(FoldingSetNodeID &ID) const {
174  Profile(ID, makeArrayRef(begin(), end()));
175  }
176  static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
177  for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
178  AttrList[I].Profile(ID);
179  }
180 };
181 
182 //===----------------------------------------------------------------------===//
183 /// \class
184 /// \brief This class represents a set of attributes that apply to the function,
185 /// return type, and parameters.
187  friend class AttributeSet;
188 
189  LLVMContext &Context;
190 
191  typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair;
192  unsigned NumAttrs; ///< Number of entries in this set.
193 
194  /// \brief Return a pointer to the IndexAttrPair for the specified slot.
195  const IndexAttrPair *getNode(unsigned Slot) const {
196  return reinterpret_cast<const IndexAttrPair *>(this + 1) + Slot;
197  }
198 
199  // AttributesSet is uniqued, these should not be publicly available.
200  void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
201  AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
202 public:
204  ArrayRef<std::pair<unsigned, AttributeSetNode *> > Attrs)
205  : Context(C), NumAttrs(Attrs.size()) {
206 #ifndef NDEBUG
207  if (Attrs.size() >= 2) {
208  for (const std::pair<unsigned, AttributeSetNode *> *i = Attrs.begin() + 1,
209  *e = Attrs.end();
210  i != e; ++i) {
211  assert((i-1)->first <= i->first && "Attribute set not ordered!");
212  }
213  }
214 #endif
215  // There's memory after the node where we can store the entries in.
216  std::copy(Attrs.begin(), Attrs.end(),
217  reinterpret_cast<IndexAttrPair *>(this + 1));
218  }
219 
220  /// \brief Get the context that created this AttributeSetImpl.
221  LLVMContext &getContext() { return Context; }
222 
223  /// \brief Return the number of attributes this AttributeSet contains.
224  unsigned getNumAttributes() const { return NumAttrs; }
225 
226  /// \brief Get the index of the given "slot" in the AttrNodes list. This index
227  /// is the index of the return, parameter, or function object that the
228  /// attributes are applied to, not the index into the AttrNodes list where the
229  /// attributes reside.
230  unsigned getSlotIndex(unsigned Slot) const {
231  return getNode(Slot)->first;
232  }
233 
234  /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
235  /// \p Slot is an index into the AttrNodes list, not the index of the return /
236  /// parameter/ function which the attributes apply to.
237  AttributeSet getSlotAttributes(unsigned Slot) const {
238  return AttributeSet::get(Context, *getNode(Slot));
239  }
240 
241  /// \brief Retrieve the attribute set node for the given "slot" in the
242  /// AttrNode list.
243  AttributeSetNode *getSlotNode(unsigned Slot) const {
244  return getNode(Slot)->second;
245  }
246 
248  iterator begin(unsigned Slot) const { return getSlotNode(Slot)->begin(); }
249  iterator end(unsigned Slot) const { return getSlotNode(Slot)->end(); }
250 
251  void Profile(FoldingSetNodeID &ID) const {
252  Profile(ID, makeArrayRef(getNode(0), getNumAttributes()));
253  }
255  ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
256  for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
257  ID.AddInteger(Nodes[i].first);
258  ID.AddPointer(Nodes[i].second);
259  }
260  }
261 
262  // FIXME: This atrocity is temporary.
263  uint64_t Raw(unsigned Index) const;
264 
265  void dump() const;
266 };
267 
268 } // end llvm namespace
269 
270 #endif
void AddPointer(const void *Ptr)
Definition: FoldingSet.cpp:52
std::string getAsString(bool InAttrGrp) const
Definition: Attributes.cpp:475
iterator begin() const
iterator end() const
Definition: ArrayRef.h:98
AttributeSet getSlotAttributes(unsigned Slot) const
Retrieve the attributes for the given "slot" in the AttrNode list. Slot is an index into the AttrNode...
StringRef getStringValue() const
iterator end() const
StringAttributeImpl(StringRef Kind, StringRef Val=StringRef())
void Profile(FoldingSetNodeID &ID) const
static void Profile(FoldingSetNodeID &ID, ArrayRef< Attribute > AttrList)
bool hasAttributes() const
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:261
This file contains the simple types necessary to represent the attributes associated with functions a...
Attribute::AttrKind getKindAsEnum() const
Definition: Attributes.cpp:305
void AddInteger(signed I)
Definition: FoldingSet.cpp:60
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
bool operator<(const AttributeImpl &AI) const
Used when sorting the attributes.
Definition: Attributes.cpp:325
EnumAttributeImpl(Attribute::AttrKind Kind)
unsigned getAlignment() const
iterator end(unsigned Slot) const
AlignAttributeImpl(Attribute::AttrKind Kind, unsigned Align)
AttributeSetNode::iterator iterator
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:109
StringRef getKindAsString() const
Definition: Attributes.cpp:315
bool hasAttribute(Attribute::AttrKind Kind) const
Definition: Attributes.cpp:433
StringRef getStringKind() const
iterator begin(unsigned Slot) const
bool isStringAttribute() const
Definition: AttributeImpl.h:53
unsigned getSlotIndex(unsigned Slot) const
Get the index of the given "slot" in the AttrNodes list. This index is the index of the return...
unsigned getAlignment() const
Definition: Attributes.cpp:461
uint64_t Raw(unsigned Index) const
Definition: Attributes.cpp:489
iterator begin() const
Definition: ArrayRef.h:97
StringRef getValueAsString() const
Definition: Attributes.cpp:320
bool hasAttribute(Attribute::AttrKind A) const
Definition: Attributes.cpp:295
uint64_t getValueAsInt() const
Definition: Attributes.cpp:310
Attribute::AttrKind getEnumKind() const
bool isAlignAttribute() const
Definition: AttributeImpl.h:52
static uint64_t getAttrMask(Attribute::AttrKind Val)
Definition: Attributes.cpp:347
AttributeSetNode * getSlotNode(unsigned Slot) const
Retrieve the attribute set node for the given "slot" in the AttrNode list.
const Attribute * iterator
#define LLVM_DELETED_FUNCTION
Definition: Compiler.h:137
LLVMContext & getContext()
Get the context that created this AttributeSetImpl.
unsigned getStackAlignment() const
Definition: Attributes.cpp:468
AttributeImpl(AttrEntryKind KindID)
Definition: AttributeImpl.h:46
static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, uint64_t Val)
Definition: AttributeImpl.h:75
#define I(x, y, z)
Definition: MD5.cpp:54
bool isEnumAttribute() const
Definition: AttributeImpl.h:51
void AddString(StringRef String)
Definition: FoldingSet.cpp:87
static void Profile(FoldingSetNodeID &ID, ArrayRef< std::pair< unsigned, AttributeSetNode * > > Nodes)
unsigned getNumAttributes() const
Return the number of attributes this AttributeSet contains.
void Profile(FoldingSetNodeID &ID) const
void Profile(FoldingSetNodeID &ID) const
Definition: AttributeImpl.h:67
virtual ~AttributeImpl()
Definition: Attributes.cpp:290
Attribute getAttribute(Attribute::AttrKind Kind) const
Definition: Attributes.cpp:447
static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values)
Definition: AttributeImpl.h:80
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110