]> git.proxmox.com Git - rustc.git/blob - src/llvm/lib/IR/AttributeImpl.h
Imported Upstream version 1.0.0+dfsg1
[rustc.git] / src / llvm / lib / IR / AttributeImpl.h
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_LIB_IR_ATTRIBUTEIMPL_H
17 #define LLVM_LIB_IR_ATTRIBUTEIMPL_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;
37 AttributeImpl(const AttributeImpl &) LLVM_DELETED_FUNCTION;
38
39 protected:
40 enum AttrEntryKind {
41 EnumAttrEntry,
42 IntAttrEntry,
43 StringAttrEntry
44 };
45
46 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
47
48 public:
49 virtual ~AttributeImpl();
50
51 bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
52 bool isIntAttribute() const { return KindID == IntAttrEntry; }
53 bool isStringAttribute() const { return KindID == StringAttrEntry; }
54
55 bool hasAttribute(Attribute::AttrKind A) const;
56 bool hasAttribute(StringRef Kind) const;
57
58 Attribute::AttrKind getKindAsEnum() const;
59 uint64_t getValueAsInt() const;
60
61 StringRef getKindAsString() const;
62 StringRef getValueAsString() 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 (isIntAttribute())
71 Profile(ID, getKindAsEnum(), getValueAsInt());
72 else
73 Profile(ID, getKindAsString(), getValueAsString());
74 }
75 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
76 uint64_t Val) {
77 ID.AddInteger(Kind);
78 if (Val) ID.AddInteger(Val);
79 }
80 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
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
96 class EnumAttributeImpl : public AttributeImpl {
97 virtual void anchor();
98 Attribute::AttrKind Kind;
99
100 protected:
101 EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
102 : AttributeImpl(ID), Kind(Kind) {}
103
104 public:
105 EnumAttributeImpl(Attribute::AttrKind Kind)
106 : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
107
108 Attribute::AttrKind getEnumKind() const { return Kind; }
109 };
110
111 class IntAttributeImpl : public EnumAttributeImpl {
112 void anchor() override;
113 uint64_t Val;
114
115 public:
116 IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
117 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
118 assert(
119 (Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
120 Kind == Attribute::Dereferenceable) &&
121 "Wrong kind for int attribute!");
122 }
123
124 uint64_t getValue() const { return Val; }
125 };
126
127 class StringAttributeImpl : public AttributeImpl {
128 virtual void anchor();
129 std::string Kind;
130 std::string Val;
131
132 public:
133 StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
134 : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
135
136 StringRef getStringKind() const { return Kind; }
137 StringRef getStringValue() const { return Val; }
138 };
139
140 //===----------------------------------------------------------------------===//
141 /// \class
142 /// \brief This class represents a group of attributes that apply to one
143 /// element: function, return type, or parameter.
144 class AttributeSetNode : public FoldingSetNode {
145 unsigned NumAttrs; ///< Number of attributes in this node.
146
147 AttributeSetNode(ArrayRef<Attribute> Attrs) : NumAttrs(Attrs.size()) {
148 // There's memory after the node where we can store the entries in.
149 std::copy(Attrs.begin(), Attrs.end(),
150 reinterpret_cast<Attribute *>(this + 1));
151 }
152
153 // AttributesSetNode is uniqued, these should not be publicly available.
154 void operator=(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
155 AttributeSetNode(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
156 public:
157 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
158
159 bool hasAttribute(Attribute::AttrKind Kind) const;
160 bool hasAttribute(StringRef Kind) const;
161 bool hasAttributes() const { return NumAttrs != 0; }
162
163 Attribute getAttribute(Attribute::AttrKind Kind) const;
164 Attribute getAttribute(StringRef Kind) const;
165
166 unsigned getAlignment() const;
167 unsigned getStackAlignment() const;
168 uint64_t getDereferenceableBytes() const;
169 std::string getAsString(bool InAttrGrp) const;
170
171 typedef const Attribute *iterator;
172 iterator begin() const { return reinterpret_cast<iterator>(this + 1); }
173 iterator end() const { return begin() + NumAttrs; }
174
175 void Profile(FoldingSetNodeID &ID) const {
176 Profile(ID, makeArrayRef(begin(), end()));
177 }
178 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
179 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
180 AttrList[I].Profile(ID);
181 }
182 };
183
184 //===----------------------------------------------------------------------===//
185 /// \class
186 /// \brief This class represents a set of attributes that apply to the function,
187 /// return type, and parameters.
188 class AttributeSetImpl : public FoldingSetNode {
189 friend class AttributeSet;
190
191 LLVMContext &Context;
192
193 typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair;
194 unsigned NumAttrs; ///< Number of entries in this set.
195
196 /// \brief Return a pointer to the IndexAttrPair for the specified slot.
197 const IndexAttrPair *getNode(unsigned Slot) const {
198 return reinterpret_cast<const IndexAttrPair *>(this + 1) + Slot;
199 }
200
201 // AttributesSet is uniqued, these should not be publicly available.
202 void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
203 AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
204 public:
205 AttributeSetImpl(LLVMContext &C,
206 ArrayRef<std::pair<unsigned, AttributeSetNode *> > Attrs)
207 : Context(C), NumAttrs(Attrs.size()) {
208 #ifndef NDEBUG
209 if (Attrs.size() >= 2) {
210 for (const std::pair<unsigned, AttributeSetNode *> *i = Attrs.begin() + 1,
211 *e = Attrs.end();
212 i != e; ++i) {
213 assert((i-1)->first <= i->first && "Attribute set not ordered!");
214 }
215 }
216 #endif
217 // There's memory after the node where we can store the entries in.
218 std::copy(Attrs.begin(), Attrs.end(),
219 reinterpret_cast<IndexAttrPair *>(this + 1));
220 }
221
222 /// \brief Get the context that created this AttributeSetImpl.
223 LLVMContext &getContext() { return Context; }
224
225 /// \brief Return the number of attributes this AttributeSet contains.
226 unsigned getNumAttributes() const { return NumAttrs; }
227
228 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
229 /// is the index of the return, parameter, or function object that the
230 /// attributes are applied to, not the index into the AttrNodes list where the
231 /// attributes reside.
232 unsigned getSlotIndex(unsigned Slot) const {
233 return getNode(Slot)->first;
234 }
235
236 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
237 /// \p Slot is an index into the AttrNodes list, not the index of the return /
238 /// parameter/ function which the attributes apply to.
239 AttributeSet getSlotAttributes(unsigned Slot) const {
240 return AttributeSet::get(Context, *getNode(Slot));
241 }
242
243 /// \brief Retrieve the attribute set node for the given "slot" in the
244 /// AttrNode list.
245 AttributeSetNode *getSlotNode(unsigned Slot) const {
246 return getNode(Slot)->second;
247 }
248
249 typedef AttributeSetNode::iterator iterator;
250 iterator begin(unsigned Slot) const { return getSlotNode(Slot)->begin(); }
251 iterator end(unsigned Slot) const { return getSlotNode(Slot)->end(); }
252
253 void Profile(FoldingSetNodeID &ID) const {
254 Profile(ID, makeArrayRef(getNode(0), getNumAttributes()));
255 }
256 static void Profile(FoldingSetNodeID &ID,
257 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
258 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
259 ID.AddInteger(Nodes[i].first);
260 ID.AddPointer(Nodes[i].second);
261 }
262 }
263
264 // FIXME: This atrocity is temporary.
265 uint64_t Raw(unsigned Index) const;
266
267 void dump() const;
268 };
269
270 } // end llvm namespace
271
272 #endif