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