blob: 5f9e3e70316284ab2eadd99a54c62a7109c39e8d [file] [log] [blame]
Bill Wendlingf6670722012-12-20 01:36:59 +00001//===-- AttributeImpl.h - Attribute Internals -------------------*- C++ -*-===//
Bill Wendling2c79ecb2012-09-26 21:07:29 +00002//
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//===----------------------------------------------------------------------===//
Bill Wendling27107f62012-12-20 21:28:43 +00009///
10/// \file
11/// \brief This file defines various helper methods and classes used by
12/// LLVMContextImpl for creating and managing attributes.
13///
Bill Wendling2c79ecb2012-09-26 21:07:29 +000014//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_ATTRIBUTESIMPL_H
17#define LLVM_ATTRIBUTESIMPL_H
18
19#include "llvm/ADT/FoldingSet.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Attributes.h"
Bill Wendling606c8e32013-01-29 03:20:31 +000021#include <string>
Bill Wendling2c79ecb2012-09-26 21:07:29 +000022
23namespace llvm {
24
Bill Wendling7c1683d2012-12-29 12:29:38 +000025class Constant;
Bill Wendling5f93e2b2012-12-19 23:55:43 +000026class LLVMContext;
27
Bill Wendling27107f62012-12-20 21:28:43 +000028//===----------------------------------------------------------------------===//
29/// \class
Bill Wendling8c74ecf2013-02-05 22:37:24 +000030/// \brief A set of classes that contain the kind and (optional) value of the
31/// attribute object. There are three main categories: enum attribute entries,
32/// represented by Attribute::AttrKind; alignment attribute entries; and string
33/// attribute enties, which are for target-dependent attributes.
34class AttributeEntry {
35 unsigned char KindID;
36protected:
37 enum AttrEntryKind {
38 EnumAttrEntry,
39 AlignAttrEntry,
40 StringAttrEntry
41 };
42public:
43 AttributeEntry(AttrEntryKind Kind)
44 : KindID(Kind) {}
45 virtual ~AttributeEntry() {}
46
47 unsigned getKindID() const { return KindID; }
48
49 static inline bool classof(const AttributeEntry *) { return true; }
50};
51
52class EnumAttributeEntry : public AttributeEntry {
53 Attribute::AttrKind Kind;
54public:
55 EnumAttributeEntry(Attribute::AttrKind Kind)
56 : AttributeEntry(EnumAttrEntry), Kind(Kind) {}
57
58 Attribute::AttrKind getEnumKind() const { return Kind; }
59
60 static inline bool classof(const AttributeEntry *AE) {
61 return AE->getKindID() == EnumAttrEntry;
62 }
63 static inline bool classof(const EnumAttributeEntry *) { return true; }
64};
65
66class AlignAttributeEntry : public AttributeEntry {
67 Attribute::AttrKind Kind;
68 unsigned Align;
69public:
70 AlignAttributeEntry(Attribute::AttrKind Kind, unsigned Align)
71 : AttributeEntry(AlignAttrEntry), Kind(Kind), Align(Align) {}
72
73 Attribute::AttrKind getEnumKind() const { return Kind; }
74 unsigned getAlignment() const { return Align; }
75
76 static inline bool classof(const AttributeEntry *AE) {
77 return AE->getKindID() == AlignAttrEntry;
78 }
79 static inline bool classof(const AlignAttributeEntry *) { return true; }
80};
81
82class StringAttributeEntry : public AttributeEntry {
83 std::string Kind;
84 std::string Val;
85public:
86 StringAttributeEntry(StringRef Kind, StringRef Val = StringRef())
87 : AttributeEntry(StringAttrEntry), Kind(Kind), Val(Val) {}
88
89 StringRef getStringKind() const { return Kind; }
90 StringRef getStringValue() const { return Val; }
91
92 static inline bool classof(const AttributeEntry *AE) {
93 return AE->getKindID() == StringAttrEntry;
94 }
95 static inline bool classof(const StringAttributeEntry *) { return true; }
96};
97
98//===----------------------------------------------------------------------===//
99/// \class
Bill Wendling27107f62012-12-20 21:28:43 +0000100/// \brief This class represents a single, uniqued attribute. That attribute
Bill Wendling979aff62012-12-30 02:22:16 +0000101/// could be a single enum, a tuple, or a string.
Bill Wendlingf6670722012-12-20 01:36:59 +0000102class AttributeImpl : public FoldingSetNode {
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000103 LLVMContext &Context; ///< Global context for uniquing objects
104 Constant *Kind; ///< Kind of attribute: enum or string
105
106 AttributeEntry *Entry; ///< Holds the kind and value of the attribute
Bill Wendling1cc0d5a2013-01-27 21:38:03 +0000107
108 // AttributesImpl is uniqued, these should not be publicly available.
109 void operator=(const AttributeImpl &) LLVM_DELETED_FUNCTION;
110 AttributeImpl(const AttributeImpl &) LLVM_DELETED_FUNCTION;
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000111public:
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000112 AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind);
113 AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind, unsigned Align);
114 AttributeImpl(LLVMContext &C, StringRef Kind, StringRef Val = StringRef());
115 ~AttributeImpl();
Bill Wendling979aff62012-12-30 02:22:16 +0000116
Bill Wendling14292a62013-01-31 20:59:05 +0000117 LLVMContext &getContext() { return Context; }
118
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000119 bool isEnumAttribute() const;
120 bool isAlignAttribute() const;
121 bool isStringAttribute() const;
122
Bill Wendling60507d52013-01-04 20:54:35 +0000123 bool hasAttribute(Attribute::AttrKind A) const;
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000124 bool hasAttribute(StringRef Kind) const;
Bill Wendling8e635db2012-10-08 21:47:17 +0000125
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000126 Attribute::AttrKind getKindAsEnum() const;
127 uint64_t getValueAsInt() const;
Bill Wendling9f175f82013-01-29 20:37:10 +0000128
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000129 StringRef getKindAsString() const;
130 StringRef getValueAsString() const;
Bill Wendling529ec712012-12-30 01:38:39 +0000131
Bill Wendling14292a62013-01-31 20:59:05 +0000132 /// \brief Used when sorting the attributes.
Bill Wendling3467e302013-01-24 00:06:56 +0000133 bool operator<(const AttributeImpl &AI) const;
134
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000135 void Profile(FoldingSetNodeID &ID) const {
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000136 if (isEnumAttribute())
137 Profile(ID, getKindAsEnum(), 0);
138 else if (isAlignAttribute())
139 Profile(ID, getKindAsEnum(), getValueAsInt());
140 else
141 Profile(ID, getKindAsString(), getValueAsString());
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000142 }
Bill Wendling8c74ecf2013-02-05 22:37:24 +0000143 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
144 uint64_t Val) {
145 ID.AddInteger(Kind);
146 if (Val) ID.AddInteger(Val);
147 }
148 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
149 ID.AddString(Kind);
150 ID.AddString(Values);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000151 }
152
Bill Wendlingf715dbd2013-02-01 00:48:14 +0000153 // FIXME: Remove this!
Bill Wendlingc22f4aa2013-01-29 00:34:06 +0000154 static uint64_t getAttrMask(Attribute::AttrKind Val);
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000155};
156
Bill Wendling27107f62012-12-20 21:28:43 +0000157//===----------------------------------------------------------------------===//
158/// \class
Bill Wendling3467e302013-01-24 00:06:56 +0000159/// \brief This class represents a group of attributes that apply to one
160/// element: function, return type, or parameter.
161class AttributeSetNode : public FoldingSetNode {
162 SmallVector<Attribute, 4> AttrList;
163
164 AttributeSetNode(ArrayRef<Attribute> Attrs)
165 : AttrList(Attrs.begin(), Attrs.end()) {}
Bill Wendling1cc0d5a2013-01-27 21:38:03 +0000166
167 // AttributesSetNode is uniqued, these should not be publicly available.
168 void operator=(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
169 AttributeSetNode(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
Bill Wendling3467e302013-01-24 00:06:56 +0000170public:
171 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
172
Bill Wendling606c8e32013-01-29 03:20:31 +0000173 bool hasAttribute(Attribute::AttrKind Kind) const;
174 bool hasAttributes() const { return !AttrList.empty(); }
175
176 unsigned getAlignment() const;
177 unsigned getStackAlignment() const;
178 std::string getAsString() const;
179
Bill Wendling3467e302013-01-24 00:06:56 +0000180 typedef SmallVectorImpl<Attribute>::iterator iterator;
181 typedef SmallVectorImpl<Attribute>::const_iterator const_iterator;
182
183 iterator begin() { return AttrList.begin(); }
184 iterator end() { return AttrList.end(); }
185
186 const_iterator begin() const { return AttrList.begin(); }
187 const_iterator end() const { return AttrList.end(); }
188
189 void Profile(FoldingSetNodeID &ID) const {
190 Profile(ID, AttrList);
191 }
192 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
193 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
194 AttrList[I].Profile(ID);
195 }
196};
197
198//===----------------------------------------------------------------------===//
199/// \class
200/// \brief This class represents a set of attributes that apply to the function,
201/// return type, and parameters.
Bill Wendling18e72112012-12-19 22:42:22 +0000202class AttributeSetImpl : public FoldingSetNode {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000203 friend class AttributeSet;
204
Bill Wendling60507d52013-01-04 20:54:35 +0000205 LLVMContext &Context;
Bill Wendling60507d52013-01-04 20:54:35 +0000206
Bill Wendling6bdbf062013-01-28 22:33:39 +0000207 typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair;
Bill Wendling73bc4522013-01-28 00:21:34 +0000208 SmallVector<IndexAttrPair, 4> AttrNodes;
Bill Wendlingbb085932013-01-24 01:01:34 +0000209
Bill Wendling831737d2012-12-30 10:32:01 +0000210 // AttributesSet is uniqued, these should not be publicly available.
Bill Wendling18e72112012-12-19 22:42:22 +0000211 void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
212 AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
Bill Wendling0976e002012-11-20 05:09:20 +0000213public:
Bill Wendling87e10df2013-01-28 21:55:20 +0000214 AttributeSetImpl(LLVMContext &C,
Bill Wendling6bdbf062013-01-28 22:33:39 +0000215 ArrayRef<std::pair<unsigned, AttributeSetNode*> > attrs)
Bill Wendling87e10df2013-01-28 21:55:20 +0000216 : Context(C), AttrNodes(attrs.begin(), attrs.end()) {}
Bill Wendling60507d52013-01-04 20:54:35 +0000217
Bill Wendling893eac12013-01-27 21:32:11 +0000218 /// \brief Get the context that created this AttributeSetImpl.
Bill Wendling60507d52013-01-04 20:54:35 +0000219 LLVMContext &getContext() { return Context; }
Bill Wendling893eac12013-01-27 21:32:11 +0000220
Bill Wendling893eac12013-01-27 21:32:11 +0000221 /// \brief Return the number of attributes this AttributeSet contains.
222 unsigned getNumAttributes() const { return AttrNodes.size(); }
223
224 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
225 /// is the index of the return, parameter, or function object that the
226 /// attributes are applied to, not the index into the AttrNodes list where the
227 /// attributes reside.
Bill Wendling73bc4522013-01-28 00:21:34 +0000228 uint64_t getSlotIndex(unsigned Slot) const {
229 return AttrNodes[Slot].first;
230 }
Bill Wendling893eac12013-01-27 21:32:11 +0000231
232 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
233 /// \p Slot is an index into the AttrNodes list, not the index of the return /
234 /// parameter/ function which the attributes apply to.
Bill Wendling8e47daf2013-01-25 23:09:36 +0000235 AttributeSet getSlotAttributes(unsigned Slot) const {
Bill Wendling87e10df2013-01-28 21:55:20 +0000236 return AttributeSet::get(Context, AttrNodes[Slot]);
Bill Wendling8e47daf2013-01-25 23:09:36 +0000237 }
Bill Wendling0976e002012-11-20 05:09:20 +0000238
Bill Wendling606c8e32013-01-29 03:20:31 +0000239 /// \brief Retrieve the attribute set node for the given "slot" in the
240 /// AttrNode list.
241 AttributeSetNode *getSlotNode(unsigned Slot) const {
242 return AttrNodes[Slot].second;
243 }
244
Bill Wendling73bc4522013-01-28 00:21:34 +0000245 typedef AttributeSetNode::iterator iterator;
246 typedef AttributeSetNode::const_iterator const_iterator;
247
248 iterator begin(unsigned Idx)
249 { return AttrNodes[Idx].second->begin(); }
250 iterator end(unsigned Idx)
251 { return AttrNodes[Idx].second->end(); }
252
253 const_iterator begin(unsigned Idx) const
254 { return AttrNodes[Idx].second->begin(); }
255 const_iterator end(unsigned Idx) const
256 { return AttrNodes[Idx].second->end(); }
257
Bill Wendling0976e002012-11-20 05:09:20 +0000258 void Profile(FoldingSetNodeID &ID) const {
Bill Wendling87e10df2013-01-28 21:55:20 +0000259 Profile(ID, AttrNodes);
Bill Wendling0976e002012-11-20 05:09:20 +0000260 }
Bill Wendling60507d52013-01-04 20:54:35 +0000261 static void Profile(FoldingSetNodeID &ID,
Bill Wendling6bdbf062013-01-28 22:33:39 +0000262 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
Bill Wendlingbb085932013-01-24 01:01:34 +0000263 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
264 ID.AddInteger(Nodes[i].first);
265 ID.AddPointer(Nodes[i].second);
266 }
267 }
Bill Wendlingd05204a2013-01-27 23:41:29 +0000268
269 // FIXME: This atrocity is temporary.
270 uint64_t Raw(uint64_t Index) const;
Bill Wendling0976e002012-11-20 05:09:20 +0000271};
272
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000273} // end llvm namespace
274
275#endif