Use proper return type for attribute index.
[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   SmallVector<AttributeWithIndex, 4> AttrList;
119
120   SmallVector<std::pair<uint64_t, AttributeSetNode*>, 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, ArrayRef<AttributeWithIndex> attrs);
127
128   /// \brief Get the context that created this AttributeSetImpl.
129   LLVMContext &getContext() { return Context; }
130
131   ArrayRef<AttributeWithIndex> getAttributes() const { return AttrList; }
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 { return AttrNodes[Slot].first; }
141
142   /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
143   /// \p Slot is an index into the AttrNodes list, not the index of the return /
144   /// parameter/ function which the attributes apply to.
145   AttributeSet getSlotAttributes(unsigned Slot) const {
146     // FIXME: This needs to use AttrNodes instead.
147     return AttributeSet::get(Context, AttrList[Slot]);
148   }
149
150   void Profile(FoldingSetNodeID &ID) const {
151     Profile(ID, AttrList);
152   }
153   static void Profile(FoldingSetNodeID &ID,
154                       ArrayRef<AttributeWithIndex> AttrList) {
155     for (unsigned i = 0, e = AttrList.size(); i != e; ++i) {
156       ID.AddInteger(AttrList[i].Index);
157       ID.AddInteger(AttrList[i].Attrs.Raw());
158     }
159   }
160
161   static void Profile(FoldingSetNodeID &ID,
162                       ArrayRef<std::pair<uint64_t, AttributeSetNode*> > Nodes) {
163     for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
164       ID.AddInteger(Nodes[i].first);
165       ID.AddPointer(Nodes[i].second);
166     }
167   }
168
169   // FIXME: This atrocity is temporary.
170   uint64_t Raw(uint64_t Index) const;
171 };
172
173 } // end llvm namespace
174
175 #endif