Do not allow attributes beyond a function's last
[oota-llvm.git] / include / llvm / ParameterAttributes.h
1 //===-- llvm/ParameterAttributes.h - Container for ParamAttrs ---*- 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 // This file contains the types necessary to represent the parameter attributes
11 // associated with functions and their calls.
12 //
13 // The implementations of these classes live in lib/VMCore/Function.cpp.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_PARAMETER_ATTRIBUTES_H
18 #define LLVM_PARAMETER_ATTRIBUTES_H
19
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include <cassert>
23
24 namespace llvm {
25 class Type;
26
27 namespace ParamAttr {
28
29 /// Function parameters and results can have attributes to indicate how they 
30 /// should be treated by optimizations and code generation. This enumeration 
31 /// lists the attributes that can be associated with parameters or function 
32 /// results.
33 /// @brief Function parameter attributes.
34 enum Attributes {
35   None       = 0,       ///< No attributes have been set
36   ZExt       = 1 << 0,  ///< Zero extended before/after call
37   SExt       = 1 << 1,  ///< Sign extended before/after call
38   NoReturn   = 1 << 2,  ///< Mark the function as not returning
39   InReg      = 1 << 3,  ///< Force argument to be passed in register
40   StructRet  = 1 << 4,  ///< Hidden pointer to structure to return
41   NoUnwind   = 1 << 5,  ///< Function doesn't unwind stack
42   NoAlias    = 1 << 6,  ///< Considered to not alias after call
43   ByVal      = 1 << 7,  ///< Pass structure by value
44   Nest       = 1 << 8,  ///< Nested function static chain
45   ReadNone   = 1 << 9,  ///< Function does not access memory
46   ReadOnly   = 1 << 10  ///< Function only reads from memory
47 };
48
49 /// @brief Attributes that only apply to function parameters.
50 const uint16_t ParameterOnly = ByVal | InReg | Nest | StructRet;
51
52 /// @brief Attributes that only apply to function return values.
53 const uint16_t ReturnOnly = NoReturn | NoUnwind | ReadNone | ReadOnly;
54
55 /// @brief Attributes that can apply to vararg call arguments.
56 const uint16_t VarArgsCompatible = ByVal;
57
58 /// @brief Attributes that are mutually incompatible.
59 const uint16_t MutuallyIncompatible[3] = {
60   ByVal | InReg | Nest  | StructRet,
61   ZExt  | SExt,
62   ReadNone | ReadOnly
63 };
64
65 /// @brief Which attributes cannot be applied to a type.
66 uint16_t typeIncompatible (const Type *Ty);
67
68 } // end namespace ParamAttr
69
70 /// This is just a pair of values to associate a set of parameter attributes
71 /// with a parameter index. 
72 /// @brief ParameterAttributes with a parameter index.
73 struct ParamAttrsWithIndex {
74   uint16_t attrs; ///< The attributes that are set, or'd together
75   uint16_t index; ///< Index of the parameter for which the attributes apply
76   
77   static ParamAttrsWithIndex get(uint16_t idx, uint16_t attrs) {
78     ParamAttrsWithIndex P;
79     P.index = idx;
80     P.attrs = attrs;
81     return P;
82   }
83 };
84
85 /// @brief A vector of attribute/index pairs.
86 typedef SmallVector<ParamAttrsWithIndex,4> ParamAttrsVector;
87
88 /// @brief A more friendly way to reference the attributes.
89 typedef ParamAttr::Attributes ParameterAttributes;
90
91 /// This class represents a list of attribute/index pairs for parameter 
92 /// attributes. Each entry in the list contains the index of a function 
93 /// parameter and the associated ParameterAttributes. If a parameter's index is
94 /// not present in the list, then no attributes are set for that parameter. The
95 /// list may also be empty, but this does not occur in practice. An item in
96 /// the list with an index of 0 refers to the function as a whole or its result.
97 /// To construct a ParamAttrsList, you must first fill a ParamAttrsVector with 
98 /// the attribute/index pairs you wish to set.  The list of attributes can be 
99 /// turned into a string of mnemonics suitable for LLVM Assembly output. 
100 /// Various accessors are provided to obtain information about the attributes.
101 /// Note that objects of this class are "uniqued". The \p get method can return
102 /// the pointer of an existing and identical instance. Consequently, reference
103 /// counting is necessary in order to determine when the last reference to a 
104 /// ParamAttrsList of a given shape is dropped. Users of this class should use
105 /// the addRef and dropRef methods to add/drop references. When the reference
106 /// count goes to zero, the ParamAttrsList object is deleted.
107 /// This class is used by Function, CallInst and InvokeInst to represent their
108 /// sets of parameter attributes. 
109 /// @brief A List of ParameterAttributes.
110 class ParamAttrsList : public FoldingSetNode {
111   /// @name Construction
112   /// @{
113   private:
114     // ParamAttrsList is uniqued, these should not be publicly available
115     void operator=(const ParamAttrsList &); // Do not implement
116     ParamAttrsList(const ParamAttrsList &); // Do not implement
117     ~ParamAttrsList();                      // Private implementation
118
119     /// Only the \p get method can invoke this when it wants to create a
120     /// new instance.
121     /// @brief Construct an ParamAttrsList from a ParamAttrsVector
122     explicit ParamAttrsList(const ParamAttrsVector &attrVec);
123
124   public:
125     /// This method ensures the uniqueness of ParamAttrsList instances.  The
126     /// argument is a vector of attribute/index pairs as represented by the
127     /// ParamAttrsWithIndex structure.  The index values must be in strictly
128     /// increasing order and ParamAttr::None is not allowed.  The vector is
129     /// used to construct the ParamAttrsList instance.  If an instance with
130     /// identical vector pairs exists, it will be returned instead of creating
131     /// a new instance.
132     /// @brief Get a ParamAttrsList instance.
133     static const ParamAttrsList *get(const ParamAttrsVector &attrVec);
134
135     /// Returns the ParamAttrsList obtained by modifying PAL using the supplied
136     /// list of attribute/index pairs.  Any existing attributes for the given
137     /// index are replaced by the given attributes.  If there were no attributes
138     /// then the new ones are inserted.  Attributes can be deleted by replacing
139     /// them with ParamAttr::None.  Index values must be strictly increasing.
140     /// @brief Get a new ParamAttrsList instance by modifying an existing one.
141     static const ParamAttrsList *getModified(const ParamAttrsList *PAL,
142                                              const ParamAttrsVector &modVec);
143
144     /// @brief Add the specified attributes to those in PAL at index idx.
145     static const ParamAttrsList *includeAttrs(const ParamAttrsList *PAL,
146                                               uint16_t idx, uint16_t attrs);
147
148     /// @brief Remove the specified attributes from those in PAL at index idx.
149     static const ParamAttrsList *excludeAttrs(const ParamAttrsList *PAL,
150                                               uint16_t idx, uint16_t attrs);
151
152   /// @}
153   /// @name Accessors
154   /// @{
155   public:
156     /// The parameter attributes for the \p indexth parameter are returned. 
157     /// The 0th parameter refers to the return type of the function. Note that
158     /// the \p param_index is an index into the function's parameters, not an
159     /// index into this class's list of attributes. The result of getParamIndex
160     /// is always suitable input to this function.
161     /// @returns The all the ParameterAttributes for the \p indexth parameter
162     /// as a uint16_t of enumeration values OR'd together.
163     /// @brief Get the attributes for a parameter
164     uint16_t getParamAttrs(uint16_t param_index) const;
165
166     /// This checks to see if the \p ith function parameter has the parameter
167     /// attribute given by \p attr set.
168     /// @returns true if the parameter attribute is set
169     /// @brief Determine if a ParameterAttributes is set
170     bool paramHasAttr(uint16_t i, ParameterAttributes attr) const {
171       return getParamAttrs(i) & attr;
172     }
173
174     /// The set of ParameterAttributes set in Attributes is converted to a
175     /// string of equivalent mnemonics. This is, presumably, for writing out
176     /// the mnemonics for the assembly writer. 
177     /// @brief Convert parameter attribute bits to text
178     static std::string getParamAttrsText(uint16_t Attributes);
179
180     /// The \p Indexth parameter attribute is converted to string.
181     /// @brief Get the text for the parmeter attributes for one parameter.
182     std::string getParamAttrsTextByIndex(uint16_t Index) const {
183       return getParamAttrsText(getParamAttrs(Index));
184     }
185
186     /// @brief Comparison operator for ParamAttrsList
187     bool operator < (const ParamAttrsList& that) const {
188       if (this->attrs.size() < that.attrs.size())
189         return true;
190       if (this->attrs.size() > that.attrs.size())
191         return false;
192       for (unsigned i = 0; i < attrs.size(); ++i) {
193         if (attrs[i].index < that.attrs[i].index)
194           return true;
195         if (attrs[i].index > that.attrs[i].index)
196           return false;
197         if (attrs[i].attrs < that.attrs[i].attrs)
198           return true;
199         if (attrs[i].attrs > that.attrs[i].attrs)
200           return false;
201       }
202       return false;
203     }
204
205     /// Returns the parameter index of a particular parameter attribute in this
206     /// list of attributes. Note that the attr_index is an index into this 
207     /// class's list of attributes, not the index of a parameter. The result
208     /// is the index of the parameter. Clients generally should not use this
209     /// method. It is used internally by LLVM.
210     /// @brief Get a parameter index
211     uint16_t getParamIndex(unsigned attr_index) const {
212       return attrs[attr_index].index;
213     }
214
215     uint16_t getParamAttrsAtIndex(unsigned attr_index) const {
216       return attrs[attr_index].attrs;
217     }
218     
219     /// Determines how many parameter attributes are set in this ParamAttrsList.
220     /// This says nothing about how many parameters the function has. It also
221     /// says nothing about the highest parameter index that has attributes. 
222     /// Clients generally should not use this method. It is used internally by
223     /// LLVM.
224     /// @returns the number of parameter attributes in this ParamAttrsList.
225     /// @brief Return the number of parameter attributes this type has.
226     unsigned size() const { return attrs.size(); }
227
228     /// @brief Return the number of references to this ParamAttrsList.
229     unsigned numRefs() const { return refCount; }
230
231   /// @}
232   /// @name Mutators
233   /// @{
234   public:
235     /// Classes retaining references to ParamAttrsList objects should call this
236     /// method to increment the reference count. This ensures that the
237     /// ParamAttrsList object will not disappear until the class drops it.
238     /// @brief Add a reference to this instance.
239     void addRef() const { refCount++; }
240
241     /// Classes retaining references to ParamAttrsList objects should call this
242     /// method to decrement the reference count and possibly delete the 
243     /// ParamAttrsList object. This ensures that ParamAttrsList objects are 
244     /// cleaned up only when the last reference to them is dropped.
245     /// @brief Drop a reference to this instance.
246     void dropRef() const { 
247       assert(refCount != 0 && "dropRef without addRef");
248       if (--refCount == 0) 
249         delete this; 
250     }
251
252   /// @}
253   /// @name Implementation Details
254   /// @{
255   public:
256     void Profile(FoldingSetNodeID &ID) const {
257       Profile(ID, attrs);
258     }
259     static void Profile(FoldingSetNodeID &ID, const ParamAttrsVector &Attrs);
260     void dump() const;
261
262   /// @}
263   /// @name Data
264   /// @{
265   private:
266     ParamAttrsVector attrs;     ///< The list of attributes
267     mutable unsigned refCount;  ///< The number of references to this object
268   /// @}
269 };
270
271 } // End llvm namespace
272
273 #endif