[weak vtables] Remove a bunch of weak vtables
[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   unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
34
35   // AttributesImpl is uniqued, these should not be publicly available.
36   void operator=(const AttributeImpl &) LLVM_DELETED_FUNCTION;
37   AttributeImpl(const AttributeImpl &) LLVM_DELETED_FUNCTION;
38
39 protected:
40   enum AttrEntryKind {
41     EnumAttrEntry,
42     AlignAttrEntry,
43     StringAttrEntry
44   };
45
46   AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
47
48 public:
49   bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
50   bool isAlignAttribute() const { return KindID == AlignAttrEntry; }
51   bool isStringAttribute() const { return KindID == StringAttrEntry; }
52
53   bool hasAttribute(Attribute::AttrKind A) const;
54   bool hasAttribute(StringRef Kind) const;
55
56   Attribute::AttrKind getKindAsEnum() const;
57   uint64_t getValueAsInt() const;
58
59   StringRef getKindAsString() const;
60   StringRef getValueAsString() const;
61
62   /// \brief Used when sorting the attributes.
63   bool operator<(const AttributeImpl &AI) const;
64
65   void Profile(FoldingSetNodeID &ID) const {
66     if (isEnumAttribute())
67       Profile(ID, getKindAsEnum(), 0);
68     else if (isAlignAttribute())
69       Profile(ID, getKindAsEnum(), getValueAsInt());
70     else
71       Profile(ID, getKindAsString(), getValueAsString());
72   }
73   static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
74                       uint64_t Val) {
75     ID.AddInteger(Kind);
76     if (Val) ID.AddInteger(Val);
77   }
78   static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
79     ID.AddString(Kind);
80     if (!Values.empty()) ID.AddString(Values);
81   }
82
83   // FIXME: Remove this!
84   static uint64_t getAttrMask(Attribute::AttrKind Val);
85 };
86
87 //===----------------------------------------------------------------------===//
88 /// \class
89 /// \brief A set of classes that contain the value of the
90 /// attribute object. There are three main categories: enum attribute entries,
91 /// represented by Attribute::AttrKind; alignment attribute entries; and string
92 /// attribute enties, which are for target-dependent attributes.
93
94 class EnumAttributeImpl : public AttributeImpl {
95   Attribute::AttrKind Kind;
96
97 protected:
98   EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
99       : AttributeImpl(ID), Kind(Kind) {}
100
101 public:
102   EnumAttributeImpl(Attribute::AttrKind Kind)
103       : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
104
105   Attribute::AttrKind getEnumKind() const { return Kind; }
106 };
107
108 class AlignAttributeImpl : public EnumAttributeImpl {
109   unsigned Align;
110
111 public:
112   AlignAttributeImpl(Attribute::AttrKind Kind, unsigned Align)
113       : EnumAttributeImpl(AlignAttrEntry, Kind), Align(Align) {
114     assert(
115         (Kind == Attribute::Alignment || Kind == Attribute::StackAlignment) &&
116         "Wrong kind for alignment attribute!");
117   }
118
119   unsigned getAlignment() const { return Align; }
120 };
121
122 class StringAttributeImpl : public AttributeImpl {
123   std::string Kind;
124   std::string Val;
125
126 public:
127   StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
128       : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
129
130   StringRef getStringKind() const { return Kind; }
131   StringRef getStringValue() const { return Val; }
132 };
133
134 //===----------------------------------------------------------------------===//
135 /// \class
136 /// \brief This class represents a group of attributes that apply to one
137 /// element: function, return type, or parameter.
138 class AttributeSetNode : public FoldingSetNode {
139   unsigned NumAttrs; ///< Number of attributes in this node.
140
141   AttributeSetNode(ArrayRef<Attribute> Attrs) : NumAttrs(Attrs.size()) {
142     // There's memory after the node where we can store the entries in.
143     std::copy(Attrs.begin(), Attrs.end(),
144               reinterpret_cast<Attribute *>(this + 1));
145   }
146
147   // AttributesSetNode is uniqued, these should not be publicly available.
148   void operator=(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
149   AttributeSetNode(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
150 public:
151   static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
152
153   bool hasAttribute(Attribute::AttrKind Kind) const;
154   bool hasAttribute(StringRef Kind) const;
155   bool hasAttributes() const { return NumAttrs != 0; }
156
157   Attribute getAttribute(Attribute::AttrKind Kind) const;
158   Attribute getAttribute(StringRef Kind) const;
159
160   unsigned getAlignment() const;
161   unsigned getStackAlignment() const;
162   std::string getAsString(bool InAttrGrp) const;
163
164   typedef const Attribute *iterator;
165   iterator begin() const { return reinterpret_cast<iterator>(this + 1); }
166   iterator end() const { return begin() + NumAttrs; }
167
168   void Profile(FoldingSetNodeID &ID) const {
169     Profile(ID, makeArrayRef(begin(), end()));
170   }
171   static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
172     for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
173       AttrList[I].Profile(ID);
174   }
175 };
176
177 //===----------------------------------------------------------------------===//
178 /// \class
179 /// \brief This class represents a set of attributes that apply to the function,
180 /// return type, and parameters.
181 class AttributeSetImpl : public FoldingSetNode {
182   friend class AttributeSet;
183
184   LLVMContext &Context;
185
186   typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair;
187   unsigned NumAttrs; ///< Number of entries in this set.
188
189   /// \brief Return a pointer to the IndexAttrPair for the specified slot.
190   const IndexAttrPair *getNode(unsigned Slot) const {
191     return reinterpret_cast<const IndexAttrPair *>(this + 1) + Slot;
192   }
193
194   // AttributesSet is uniqued, these should not be publicly available.
195   void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
196   AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
197 public:
198   AttributeSetImpl(LLVMContext &C,
199                    ArrayRef<std::pair<unsigned, AttributeSetNode *> > Attrs)
200       : Context(C), NumAttrs(Attrs.size()) {
201 #ifndef NDEBUG
202     if (Attrs.size() >= 2) {
203       for (const std::pair<unsigned, AttributeSetNode *> *i = Attrs.begin() + 1,
204                                                          *e = Attrs.end();
205            i != e; ++i) {
206         assert((i-1)->first <= i->first && "Attribute set not ordered!");
207       }
208     }
209 #endif
210     // There's memory after the node where we can store the entries in.
211     std::copy(Attrs.begin(), Attrs.end(),
212               reinterpret_cast<IndexAttrPair *>(this + 1));
213   }
214
215   /// \brief Get the context that created this AttributeSetImpl.
216   LLVMContext &getContext() { return Context; }
217
218   /// \brief Return the number of attributes this AttributeSet contains.
219   unsigned getNumAttributes() const { return NumAttrs; }
220
221   /// \brief Get the index of the given "slot" in the AttrNodes list. This index
222   /// is the index of the return, parameter, or function object that the
223   /// attributes are applied to, not the index into the AttrNodes list where the
224   /// attributes reside.
225   unsigned getSlotIndex(unsigned Slot) const {
226     return getNode(Slot)->first;
227   }
228
229   /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
230   /// \p Slot is an index into the AttrNodes list, not the index of the return /
231   /// parameter/ function which the attributes apply to.
232   AttributeSet getSlotAttributes(unsigned Slot) const {
233     return AttributeSet::get(Context, *getNode(Slot));
234   }
235
236   /// \brief Retrieve the attribute set node for the given "slot" in the
237   /// AttrNode list.
238   AttributeSetNode *getSlotNode(unsigned Slot) const {
239     return getNode(Slot)->second;
240   }
241
242   typedef AttributeSetNode::iterator iterator;
243   iterator begin(unsigned Slot) const { return getSlotNode(Slot)->begin(); }
244   iterator end(unsigned Slot) const { return getSlotNode(Slot)->end(); }
245
246   void Profile(FoldingSetNodeID &ID) const {
247     Profile(ID, makeArrayRef(getNode(0), getNumAttributes()));
248   }
249   static void Profile(FoldingSetNodeID &ID,
250                       ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
251     for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
252       ID.AddInteger(Nodes[i].first);
253       ID.AddPointer(Nodes[i].second);
254     }
255   }
256
257   // FIXME: This atrocity is temporary.
258   uint64_t Raw(unsigned Index) const;
259
260   void dump() const;
261 };
262
263 } // end llvm namespace
264
265 #endif