Make enum-valued bitfield large enough to avoid interpretation as negative values...
[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 was developed by Reid Spencer and is distributed under the 
6 // University of Illinois Open Source 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
22 namespace llvm {
23
24 /// Function parameters can have attributes to indicate how they should be
25 /// treated by optimizations and code generation. This enumeration lists the
26 /// attributes that can be associated with parameters or function results.
27 /// @brief Function parameter attributes.
28 namespace ParamAttr {
29
30 enum Attributes {
31   None       = 0,      ///< No attributes have been set
32   ZExt       = 1 << 0, ///< zero extended before/after call
33   SExt       = 1 << 1, ///< sign extended before/after call
34   NoReturn   = 1 << 2, ///< mark the function as not returning
35   InReg      = 1 << 3, ///< force argument to be passed in register
36   StructRet  = 1 << 4, ///< hidden pointer to structure to return
37   NoUnwind   = 1 << 5  ///< Function doesn't unwind stack
38 };
39
40 }
41
42 typedef ParamAttr::Attributes ParameterAttributes;
43
44 /// This class is used by Function and CallInst to represent the set of 
45 /// parameter attributes used. It represents a list of pairs of uint16_t, one
46 /// for the parameter index, and one a set of ParameterAttributes bits.
47 /// Parameters that have no attributes are not present in the list. The list
48 /// may also be empty, but this doesn't occur in practice.  The list constructs
49 /// as empty and is filled by the insert method. The list can be turned into 
50 /// a string of mnemonics suitable for LLVM Assembly output. Various accessors
51 /// are provided to obtain information about the attributes.
52 /// @brief A List of ParameterAttributes.
53 class ParamAttrsList {
54   //void operator=(const ParamAttrsList &); // Do not implement
55   //ParamAttrsList(const ParamAttrsList &); // Do not implement
56
57   /// @name Types
58   /// @{
59   public:
60     /// This is an internal structure used to associate the ParameterAttributes
61     /// with a parameter index. 
62     /// @brief ParameterAttributes with a parameter index.
63     struct ParamAttrsWithIndex {
64       uint16_t attrs; ///< The attributes that are set, |'d together
65       uint16_t index; ///< Index of the parameter for which the attributes apply
66     };
67
68     /// @brief A vector of attribute/index pairs.
69     typedef SmallVector<ParamAttrsWithIndex,4> ParamAttrsVector;
70
71   /// @}
72   /// @name Construction
73   /// @{
74   public:
75     /// @brief Construct an empty ParamAttrsList
76     ParamAttrsList() {}
77
78     /// This method ensures the uniqueness of ParamAttrsList instances. The
79     /// argument is a vector of attribute/index pairs as represented by the
80     /// ParamAttrsWithIndex structure. The vector is used in the construction of
81     /// the ParamAttrsList instance. If an instance with identical vector pairs
82     /// exists, it will be returned instead of creating a new instance.
83     /// @brief Get a ParamAttrsList instance.
84     ParamAttrsList *get(const ParamAttrsVector &attrVec);
85
86   /// @}
87   /// @name Accessors
88   /// @{
89   public:
90     /// The parameter attributes for the \p indexth parameter are returned. 
91     /// The 0th parameter refers to the return type of the function. Note that
92     /// the \p param_index is an index into the function's parameters, not an
93     /// index into this class's list of attributes. The result of getParamIndex
94     /// is always suitable input to this function.
95     /// @returns The all the ParameterAttributes for the \p indexth parameter
96     /// as a uint16_t of enumeration values OR'd together.
97     /// @brief Get the attributes for a parameter
98     uint16_t getParamAttrs(uint16_t param_index) const;
99
100     /// This checks to see if the \p ith function parameter has the parameter
101     /// attribute given by \p attr set.
102     /// @returns true if the parameter attribute is set
103     /// @brief Determine if a ParameterAttributes is set
104     bool paramHasAttr(uint16_t i, ParameterAttributes attr) const {
105       return getParamAttrs(i) & attr;
106     }
107
108     /// The set of ParameterAttributes set in Attributes is converted to a
109     /// string of equivalent mnemonics. This is, presumably, for writing out
110     /// the mnemonics for the assembly writer. 
111     /// @brief Convert parameter attribute bits to text
112     static std::string getParamAttrsText(uint16_t Attributes);
113
114     /// The \p Indexth parameter attribute is converted to string.
115     /// @brief Get the text for the parmeter attributes for one parameter.
116     std::string getParamAttrsTextByIndex(uint16_t Index) const {
117       return getParamAttrsText(getParamAttrs(Index));
118     }
119
120     /// @brief Comparison operator for ParamAttrsList
121     bool operator < (const ParamAttrsList& that) const {
122       if (this->attrs.size() < that.attrs.size())
123         return true;
124       if (this->attrs.size() > that.attrs.size())
125         return false;
126       for (unsigned i = 0; i < attrs.size(); ++i) {
127         if (attrs[i].index < that.attrs[i].index)
128           return true;
129         if (attrs[i].index > that.attrs[i].index)
130           return false;
131         if (attrs[i].attrs < that.attrs[i].attrs)
132           return true;
133         if (attrs[i].attrs > that.attrs[i].attrs)
134           return false;
135       }
136       return false;
137     }
138
139     /// Returns the parameter index of a particular parameter attribute in this
140     /// list of attributes. Note that the attr_index is an index into this 
141     /// class's list of attributes, not the index of a parameter. The result
142     /// is the index of the parameter. Clients generally should not use this
143     /// method. It is used internally by LLVM.
144     /// @brief Get a parameter index
145     uint16_t getParamIndex(unsigned attr_index) const {
146       return attrs[attr_index].index;
147     }
148
149     /// Determines how many parameter attributes are set in this ParamAttrsList.
150     /// This says nothing about how many parameters the function has. It also
151     /// says nothing about the highest parameter index that has attributes. 
152     /// Clients generally should not use this method. It is used internally by
153     /// LLVM.
154     /// @returns the number of parameter attributes in this ParamAttrsList.
155     /// @brief Return the number of parameter attributes this type has.
156     unsigned size() const { return attrs.size(); }
157
158     /// Clients generally should not use this method. It is used internally by
159     /// LLVM.
160     /// @returns true if this ParamAttrsList is empty.
161     /// @brief Determine emptiness of ParamAttrsList.
162     unsigned empty() const { return attrs.empty(); }
163
164   /// @}
165   /// @name Mutators
166   /// @{
167   public:
168     /// This method will add the \p attrs to the parameter with index
169     /// \p param_index. If the parameter index does not exist it will be created
170     /// and the \p attrs will be the only attributes set. Otherwise, any 
171     /// existing attributes for the specified parameter remain set and the 
172     /// attributes given by \p attrs are also set.
173     /// @brief Add ParameterAttributes.
174     void addAttributes(uint16_t param_index, uint16_t attrs);
175
176     /// This method will remove the \p attrs to the parameter with index
177     /// \p param_index. If the parameter index does not exist in the list,  
178     /// an assertion will occur. If the specified attributes are the last 
179     /// attributes set for the specified parameter index, the attributes for 
180     /// that index are removed completely from the list (size is decremented).
181     /// Otherwise, the specified attributes are removed from the set of 
182     /// attributes for the given index, retaining any others.
183     /// @brief Remove a single ParameterAttribute
184     void removeAttributes(uint16_t param_index, uint16_t attrs);
185
186   /// @}
187   /// @name Data
188   /// @{
189   private:
190     ParamAttrsVector attrs; ///< The list of attributes
191   /// @}
192 };
193
194 } // End llvm namespace
195
196 #endif