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