7bb1fccba0a6cac92c95c039a70fab1fc61c3937
[oota-llvm.git] / 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_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 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.
34 class AttributeEntry {
35   unsigned char KindID;
36 protected:
37   enum AttrEntryKind {
38     EnumAttrEntry,
39     AlignAttrEntry,
40     StringAttrEntry
41   };
42 public:
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
52 class EnumAttributeEntry : public AttributeEntry {
53   Attribute::AttrKind Kind;
54 public:
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
66 class AlignAttributeEntry : public AttributeEntry {
67   Attribute::AttrKind Kind;
68   unsigned Align;
69 public:
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
82 class StringAttributeEntry : public AttributeEntry {
83   std::string Kind;
84   std::string Val;
85 public:
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
100 /// \brief This class represents a single, uniqued attribute. That attribute
101 /// could be a single enum, a tuple, or a string.
102 class AttributeImpl : public FoldingSetNode {
103   LLVMContext &Context;  ///< Global context for uniquing objects
104
105   AttributeEntry *Entry; ///< Holds the kind and value of the attribute
106
107   // AttributesImpl is uniqued, these should not be publicly available.
108   void operator=(const AttributeImpl &) LLVM_DELETED_FUNCTION;
109   AttributeImpl(const AttributeImpl &) LLVM_DELETED_FUNCTION;
110 public:
111   AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind);
112   AttributeImpl(LLVMContext &C, Attribute::AttrKind Kind, unsigned Align);
113   AttributeImpl(LLVMContext &C, StringRef Kind, StringRef Val = StringRef());
114   ~AttributeImpl();
115
116   LLVMContext &getContext() { return Context; }
117
118   bool isEnumAttribute() const;
119   bool isAlignAttribute() const;
120   bool isStringAttribute() const;
121
122   bool hasAttribute(Attribute::AttrKind A) const;
123   bool hasAttribute(StringRef Kind) const;
124
125   Attribute::AttrKind getKindAsEnum() const;
126   uint64_t getValueAsInt() const;
127
128   StringRef getKindAsString() const;
129   StringRef getValueAsString() const;
130
131   /// \brief Used when sorting the attributes.
132   bool operator<(const AttributeImpl &AI) const;
133
134   void Profile(FoldingSetNodeID &ID) const {
135     if (isEnumAttribute())
136       Profile(ID, getKindAsEnum(), 0);
137     else if (isAlignAttribute())
138       Profile(ID, getKindAsEnum(), getValueAsInt());
139     else
140       Profile(ID, getKindAsString(), getValueAsString());
141   }
142   static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
143                       uint64_t Val) {
144     ID.AddInteger(Kind);
145     if (Val) ID.AddInteger(Val);
146   }
147   static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
148     ID.AddString(Kind);
149     ID.AddString(Values);
150   }
151
152   // FIXME: Remove this!
153   static uint64_t getAttrMask(Attribute::AttrKind Val);
154 };
155
156 //===----------------------------------------------------------------------===//
157 /// \class
158 /// \brief This class represents a group of attributes that apply to one
159 /// element: function, return type, or parameter.
160 class AttributeSetNode : public FoldingSetNode {
161   SmallVector<Attribute, 4> AttrList;
162
163   AttributeSetNode(ArrayRef<Attribute> Attrs)
164     : AttrList(Attrs.begin(), Attrs.end()) {}
165
166   // AttributesSetNode is uniqued, these should not be publicly available.
167   void operator=(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
168   AttributeSetNode(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
169 public:
170   static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
171
172   bool hasAttribute(Attribute::AttrKind Kind) const;
173   bool hasAttributes() const { return !AttrList.empty(); }
174
175   unsigned getAlignment() const;
176   unsigned getStackAlignment() const;
177   std::string getAsString(bool InAttrGrp) const;
178
179   typedef SmallVectorImpl<Attribute>::iterator       iterator;
180   typedef SmallVectorImpl<Attribute>::const_iterator const_iterator;
181
182   iterator begin() { return AttrList.begin(); }
183   iterator end()   { return AttrList.end(); }
184
185   const_iterator begin() const { return AttrList.begin(); }
186   const_iterator end() const   { return AttrList.end(); }
187
188   void Profile(FoldingSetNodeID &ID) const {
189     Profile(ID, AttrList);
190   }
191   static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
192     for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
193       AttrList[I].Profile(ID);
194   }
195 };
196
197 //===----------------------------------------------------------------------===//
198 /// \class
199 /// \brief This class represents a set of attributes that apply to the function,
200 /// return type, and parameters.
201 class AttributeSetImpl : public FoldingSetNode {
202   friend class AttributeSet;
203
204   LLVMContext &Context;
205
206   typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair;
207   SmallVector<IndexAttrPair, 4> AttrNodes;
208
209   // AttributesSet is uniqued, these should not be publicly available.
210   void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
211   AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
212 public:
213   AttributeSetImpl(LLVMContext &C,
214                    ArrayRef<std::pair<unsigned, AttributeSetNode*> > attrs)
215     : Context(C), AttrNodes(attrs.begin(), attrs.end()) {}
216
217   /// \brief Get the context that created this AttributeSetImpl.
218   LLVMContext &getContext() { return Context; }
219
220   /// \brief Return the number of attributes this AttributeSet contains.
221   unsigned getNumAttributes() const { return AttrNodes.size(); }
222
223   /// \brief Get the index of the given "slot" in the AttrNodes list. This index
224   /// is the index of the return, parameter, or function object that the
225   /// attributes are applied to, not the index into the AttrNodes list where the
226   /// attributes reside.
227   uint64_t getSlotIndex(unsigned Slot) const {
228     return AttrNodes[Slot].first;
229   }
230
231   /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
232   /// \p Slot is an index into the AttrNodes list, not the index of the return /
233   /// parameter/ function which the attributes apply to.
234   AttributeSet getSlotAttributes(unsigned Slot) const {
235     return AttributeSet::get(Context, AttrNodes[Slot]);
236   }
237
238   /// \brief Retrieve the attribute set node for the given "slot" in the
239   /// AttrNode list.
240   AttributeSetNode *getSlotNode(unsigned Slot) const {
241     return AttrNodes[Slot].second;
242   }
243
244   typedef AttributeSetNode::iterator       iterator;
245   typedef AttributeSetNode::const_iterator const_iterator;
246
247   iterator begin(unsigned Idx)
248     { return AttrNodes[Idx].second->begin(); }
249   iterator end(unsigned Idx)
250     { return AttrNodes[Idx].second->end(); }
251
252   const_iterator begin(unsigned Idx) const
253     { return AttrNodes[Idx].second->begin(); }
254   const_iterator end(unsigned Idx) const
255     { return AttrNodes[Idx].second->end(); }
256
257   void Profile(FoldingSetNodeID &ID) const {
258     Profile(ID, AttrNodes);
259   }
260   static void Profile(FoldingSetNodeID &ID,
261                       ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
262     for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
263       ID.AddInteger(Nodes[i].first);
264       ID.AddPointer(Nodes[i].second);
265     }
266   }
267
268   // FIXME: This atrocity is temporary.
269   uint64_t Raw(uint64_t Index) const;
270 };
271
272 } // end llvm namespace
273
274 #endif