improve portability to avoid conflicting with std::next in c++'0x.
[oota-llvm.git] / include / llvm / Attributes.h
1 //===-- llvm/Attributes.h - Container for Attributes ---*---------- 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 simple types necessary to represent the
11 // attributes associated with functions and their calls.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ATTRIBUTES_H
16 #define LLVM_ATTRIBUTES_H
17
18 #include "llvm/Support/MathExtras.h"
19 #include <cassert>
20 #include <string>
21
22 namespace llvm {
23 class Type;
24
25 /// Attributes - A bitset of attributes.
26 typedef unsigned Attributes;
27
28 namespace Attribute {
29
30 /// Function parameters and results can have attributes to indicate how they 
31 /// should be treated by optimizations and code generation. This enumeration 
32 /// lists the attributes that can be associated with parameters, function 
33 /// results or the function itself.
34 /// @brief Function attributes.
35
36 const Attributes None      = 0;     ///< No attributes have been set
37 const Attributes ZExt      = 1<<0;  ///< Zero extended before/after call
38 const Attributes SExt      = 1<<1;  ///< Sign extended before/after call
39 const Attributes NoReturn  = 1<<2;  ///< Mark the function as not returning
40 const Attributes InReg     = 1<<3;  ///< Force argument to be passed in register
41 const Attributes StructRet = 1<<4;  ///< Hidden pointer to structure to return
42 const Attributes NoUnwind  = 1<<5;  ///< Function doesn't unwind stack
43 const Attributes NoAlias   = 1<<6;  ///< Considered to not alias after call
44 const Attributes ByVal     = 1<<7;  ///< Pass structure by value
45 const Attributes Nest      = 1<<8;  ///< Nested function static chain
46 const Attributes ReadNone  = 1<<9;  ///< Function does not access memory
47 const Attributes ReadOnly  = 1<<10; ///< Function only reads from memory
48 const Attributes NoInline        = 1<<11; ///< inline=never 
49 const Attributes AlwaysInline    = 1<<12; ///< inline=always
50 const Attributes OptimizeForSize = 1<<13; ///< opt_size
51 const Attributes StackProtect    = 1<<14; ///< Stack protection.
52 const Attributes StackProtectReq = 1<<15; ///< Stack protection required.
53 const Attributes Alignment = 31<<16; ///< Alignment of parameter (5 bits)
54                                      // stored as log2 of alignment with +1 bias
55                                      // 0 means unaligned different from align 1
56 const Attributes NoCapture = 1<<21; ///< Function creates no aliases of pointer
57 const Attributes NoRedZone = 1<<22; /// disable redzone
58 const Attributes NoImplicitFloat = 1<<23; /// disable implicit floating point
59                                           /// instructions.
60 const Attributes Naked           = 1<<24; ///< Naked function
61 const Attributes InlineHint      = 1<<25; ///< source said inlining was desirable
62
63 /// @brief Attributes that only apply to function parameters.
64 const Attributes ParameterOnly = ByVal | Nest | StructRet | NoCapture;
65
66 /// @brief Attributes that may be applied to the function itself.  These cannot
67 /// be used on return values or function parameters.
68 const Attributes FunctionOnly = NoReturn | NoUnwind | ReadNone | ReadOnly | 
69   NoInline | AlwaysInline | OptimizeForSize | StackProtect | StackProtectReq |
70   NoRedZone | NoImplicitFloat | Naked | InlineHint;
71
72 /// @brief Parameter attributes that do not apply to vararg call arguments.
73 const Attributes VarArgsIncompatible = StructRet;
74
75 /// @brief Attributes that are mutually incompatible.
76 const Attributes MutuallyIncompatible[4] = {
77   ByVal | InReg | Nest | StructRet,
78   ZExt  | SExt,
79   ReadNone | ReadOnly,
80   NoInline | AlwaysInline
81 };
82
83 /// @brief Which attributes cannot be applied to a type.
84 Attributes typeIncompatible(const Type *Ty);
85
86 /// This turns an int alignment (a power of 2, normally) into the
87 /// form used internally in Attributes.
88 inline Attributes constructAlignmentFromInt(unsigned i) {
89   // Default alignment, allow the target to define how to align it.
90   if (i == 0)
91     return 0;
92
93   assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
94   assert(i <= 0x40000000 && "Alignment too large.");
95   return (Log2_32(i)+1) << 16;
96 }
97
98 /// This returns the alignment field of an attribute as a byte alignment value.
99 inline unsigned getAlignmentFromAttrs(Attributes A) {
100   Attributes Align = A & Attribute::Alignment;
101   if (Align == 0)
102     return 0;
103   
104   return 1U << ((Align >> 16) - 1);
105 }
106   
107   
108 /// The set of Attributes 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 attribute bits to text
112 std::string getAsString(Attributes Attrs);
113 } // end namespace Attribute
114
115 /// This is just a pair of values to associate a set of attributes
116 /// with an index. 
117 struct AttributeWithIndex {
118   Attributes Attrs; ///< The attributes that are set, or'd together.
119   unsigned Index; ///< Index of the parameter for which the attributes apply.
120                   ///< Index 0 is used for return value attributes.
121                   ///< Index ~0U is used for function attributes.
122   
123   static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
124     AttributeWithIndex P;
125     P.Index = Idx;
126     P.Attrs = Attrs;
127     return P;
128   }
129 };
130   
131 //===----------------------------------------------------------------------===//
132 // AttrListPtr Smart Pointer
133 //===----------------------------------------------------------------------===//
134
135 class AttributeListImpl;
136   
137 /// AttrListPtr - This class manages the ref count for the opaque 
138 /// AttributeListImpl object and provides accessors for it.
139 class AttrListPtr {
140   /// AttrList - The attributes that we are managing.  This can be null
141   /// to represent the empty attributes list.
142   AttributeListImpl *AttrList;
143 public:
144   AttrListPtr() : AttrList(0) {}
145   AttrListPtr(const AttrListPtr &P);
146   const AttrListPtr &operator=(const AttrListPtr &RHS);
147   ~AttrListPtr();
148   
149   //===--------------------------------------------------------------------===//
150   // Attribute List Construction and Mutation
151   //===--------------------------------------------------------------------===//
152   
153   /// get - Return a Attributes list with the specified parameter in it.
154   static AttrListPtr get(const AttributeWithIndex *Attr, unsigned NumAttrs);
155   
156   /// get - Return a Attribute list with the parameters specified by the
157   /// consecutive random access iterator range.
158   template <typename Iter>
159   static AttrListPtr get(const Iter &I, const Iter &E) {
160     if (I == E) return AttrListPtr();  // Empty list.
161     return get(&*I, static_cast<unsigned>(E-I));
162   }
163
164   /// addAttr - Add the specified attribute at the specified index to this
165   /// attribute list.  Since attribute lists are immutable, this
166   /// returns the new list.
167   AttrListPtr addAttr(unsigned Idx, Attributes Attrs) const;
168   
169   /// removeAttr - Remove the specified attribute at the specified index from
170   /// this attribute list.  Since attribute lists are immutable, this
171   /// returns the new list.
172   AttrListPtr removeAttr(unsigned Idx, Attributes Attrs) const;
173   
174   //===--------------------------------------------------------------------===//
175   // Attribute List Accessors
176   //===--------------------------------------------------------------------===//
177   /// getParamAttributes - The attributes for the specified index are
178   /// returned. 
179   Attributes getParamAttributes(unsigned Idx) const {
180     assert (Idx && Idx != ~0U && "Invalid parameter index!");
181     return getAttributes(Idx);
182   }
183
184   /// getRetAttributes - The attributes for the ret value are
185   /// returned. 
186   Attributes getRetAttributes() const {
187     return getAttributes(0);
188   }
189
190   /// getFnAttributes - The function attributes are returned.
191   Attributes getFnAttributes() const {
192     return getAttributes(~0U);
193   }
194   
195   /// paramHasAttr - Return true if the specified parameter index has the
196   /// specified attribute set.
197   bool paramHasAttr(unsigned Idx, Attributes Attr) const {
198     return getAttributes(Idx) & Attr;
199   }
200   
201   /// getParamAlignment - Return the alignment for the specified function
202   /// parameter.
203   unsigned getParamAlignment(unsigned Idx) const {
204     return Attribute::getAlignmentFromAttrs(getAttributes(Idx));
205   }
206   
207   /// hasAttrSomewhere - Return true if the specified attribute is set for at
208   /// least one parameter or for the return value.
209   bool hasAttrSomewhere(Attributes Attr) const;
210
211   /// operator==/!= - Provide equality predicates.
212   bool operator==(const AttrListPtr &RHS) const { return AttrList == RHS.AttrList; }
213   bool operator!=(const AttrListPtr &RHS) const { return AttrList != RHS.AttrList; }
214   
215   void dump() const;
216
217   //===--------------------------------------------------------------------===//
218   // Attribute List Introspection
219   //===--------------------------------------------------------------------===//
220   
221   /// getRawPointer - Return a raw pointer that uniquely identifies this
222   /// attribute list. 
223   void *getRawPointer() const {
224     return AttrList;
225   }
226   
227   // Attributes are stored as a dense set of slots, where there is one
228   // slot for each argument that has an attribute.  This allows walking over the
229   // dense set instead of walking the sparse list of attributes.
230   
231   /// isEmpty - Return true if there are no attributes.
232   ///
233   bool isEmpty() const {
234     return AttrList == 0;
235   }
236   
237   /// getNumSlots - Return the number of slots used in this attribute list. 
238   /// This is the number of arguments that have an attribute set on them
239   /// (including the function itself).
240   unsigned getNumSlots() const;
241   
242   /// getSlot - Return the AttributeWithIndex at the specified slot.  This
243   /// holds a index number plus a set of attributes.
244   const AttributeWithIndex &getSlot(unsigned Slot) const;
245   
246 private:
247   explicit AttrListPtr(AttributeListImpl *L);
248
249   /// getAttributes - The attributes for the specified index are
250   /// returned.  Attributes for the result are denoted with Idx = 0.
251   Attributes getAttributes(unsigned Idx) const;
252
253 };
254
255 } // End llvm namespace
256
257 #endif