Change the AttributeImpl to hold a single Constant* for the values.
[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 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   LLVMContext &Context; ///< Global context for uniquing objects
34   Constant *Kind;       ///< Kind of attribute: enum or string
35   Constant *Values;     ///< Values associated with the attribute
36
37   // AttributesImpl is uniqued, these should not be publicly available.
38   void operator=(const AttributeImpl &) LLVM_DELETED_FUNCTION;
39   AttributeImpl(const AttributeImpl &) LLVM_DELETED_FUNCTION;
40 public:
41   AttributeImpl(LLVMContext &C, Constant *Kind, Constant *Values = 0)
42     : Context(C), Kind(Kind), Values(Values) {}
43
44   LLVMContext &getContext() { return Context; }
45
46   bool hasAttribute(Attribute::AttrKind A) const;
47
48   Constant *getAttributeKind() const { return Kind; }
49   Constant *getAttributeValues() const { return Values; }
50
51   uint64_t getAlignment() const;
52   uint64_t getStackAlignment() const;
53
54   /// \brief Equality and non-equality comparison operators.
55   bool operator==(Attribute::AttrKind Kind) const;
56   bool operator!=(Attribute::AttrKind Kind) const;
57
58   bool operator==(StringRef Kind) const;
59   bool operator!=(StringRef Kind) const;
60
61   /// \brief Used when sorting the attributes.
62   bool operator<(const AttributeImpl &AI) const;
63
64   void Profile(FoldingSetNodeID &ID) const {
65     Profile(ID, Kind, Values);
66   }
67   static void Profile(FoldingSetNodeID &ID, Constant *Kind, Constant *Values) {
68     ID.AddPointer(Kind);
69     if (Values)
70       ID.AddPointer(Values);
71   }
72
73   // FIXME: Remove this!
74   static uint64_t getAttrMask(Attribute::AttrKind Val);
75 };
76
77 //===----------------------------------------------------------------------===//
78 /// \class
79 /// \brief This class represents a group of attributes that apply to one
80 /// element: function, return type, or parameter.
81 class AttributeSetNode : public FoldingSetNode {
82   SmallVector<Attribute, 4> AttrList;
83
84   AttributeSetNode(ArrayRef<Attribute> Attrs)
85     : AttrList(Attrs.begin(), Attrs.end()) {}
86
87   // AttributesSetNode is uniqued, these should not be publicly available.
88   void operator=(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
89   AttributeSetNode(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
90 public:
91   static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
92
93   bool hasAttribute(Attribute::AttrKind Kind) const;
94   bool hasAttributes() const { return !AttrList.empty(); }
95
96   unsigned getAlignment() const;
97   unsigned getStackAlignment() const;
98   std::string getAsString() const;
99
100   typedef SmallVectorImpl<Attribute>::iterator       iterator;
101   typedef SmallVectorImpl<Attribute>::const_iterator const_iterator;
102
103   iterator begin() { return AttrList.begin(); }
104   iterator end()   { return AttrList.end(); }
105
106   const_iterator begin() const { return AttrList.begin(); }
107   const_iterator end() const   { return AttrList.end(); }
108
109   void Profile(FoldingSetNodeID &ID) const {
110     Profile(ID, AttrList);
111   }
112   static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
113     for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
114       AttrList[I].Profile(ID);
115   }
116 };
117
118 //===----------------------------------------------------------------------===//
119 /// \class
120 /// \brief This class represents a set of attributes that apply to the function,
121 /// return type, and parameters.
122 class AttributeSetImpl : public FoldingSetNode {
123   friend class AttributeSet;
124
125   LLVMContext &Context;
126
127   typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair;
128   SmallVector<IndexAttrPair, 4> AttrNodes;
129
130   // AttributesSet is uniqued, these should not be publicly available.
131   void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
132   AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
133 public:
134   AttributeSetImpl(LLVMContext &C,
135                    ArrayRef<std::pair<unsigned, AttributeSetNode*> > attrs)
136     : Context(C), AttrNodes(attrs.begin(), attrs.end()) {}
137
138   /// \brief Get the context that created this AttributeSetImpl.
139   LLVMContext &getContext() { return Context; }
140
141   /// \brief Return the number of attributes this AttributeSet contains.
142   unsigned getNumAttributes() const { return AttrNodes.size(); }
143
144   /// \brief Get the index of the given "slot" in the AttrNodes list. This index
145   /// is the index of the return, parameter, or function object that the
146   /// attributes are applied to, not the index into the AttrNodes list where the
147   /// attributes reside.
148   uint64_t getSlotIndex(unsigned Slot) const {
149     return AttrNodes[Slot].first;
150   }
151
152   /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
153   /// \p Slot is an index into the AttrNodes list, not the index of the return /
154   /// parameter/ function which the attributes apply to.
155   AttributeSet getSlotAttributes(unsigned Slot) const {
156     // FIXME: This needs to use AttrNodes instead.
157     return AttributeSet::get(Context, AttrNodes[Slot]);
158   }
159
160   /// \brief Retrieve the attribute set node for the given "slot" in the
161   /// AttrNode list.
162   AttributeSetNode *getSlotNode(unsigned Slot) const {
163     return AttrNodes[Slot].second;
164   }
165
166   typedef AttributeSetNode::iterator       iterator;
167   typedef AttributeSetNode::const_iterator const_iterator;
168
169   iterator begin(unsigned Idx)
170     { return AttrNodes[Idx].second->begin(); }
171   iterator end(unsigned Idx)
172     { return AttrNodes[Idx].second->end(); }
173
174   const_iterator begin(unsigned Idx) const
175     { return AttrNodes[Idx].second->begin(); }
176   const_iterator end(unsigned Idx) const
177     { return AttrNodes[Idx].second->end(); }
178
179   void Profile(FoldingSetNodeID &ID) const {
180     Profile(ID, AttrNodes);
181   }
182   static void Profile(FoldingSetNodeID &ID,
183                       ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
184     for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
185       ID.AddInteger(Nodes[i].first);
186       ID.AddPointer(Nodes[i].second);
187     }
188   }
189
190   // FIXME: This atrocity is temporary.
191   uint64_t Raw(uint64_t Index) const;
192 };
193
194 } // end llvm namespace
195
196 #endif