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