removed a bug
[libfirm] / ir / tr / type.h
1 /****h* libfirm/type
2  *
3  * NAME
4  *   file type.h - datastructure to hold type information.
5  * COPYRIGHT
6  *  (C) 2001 by Universitaet Karlsruhe
7  * AUTHORS
8  *  Goetz Lindenmaier
9  *
10  * NOTES
11  *  This module supplies a datastructure to represent all types
12  *  known in the compiled program.  This includes types specified
13  *  in the program as well as types defined by the language.  In the
14  *  view of the intermediate representation there is no difference
15  *  between these types.
16  *
17  *  There exist several kinds of types, arranged by the structure of
18  *  the type.  A type is described by a set of attributes.  Some of
19  *  these attributes are common to all types, others depend on the
20  *  kind of the type.
21  *
22  *  Types are different from the modes defined in irmode:  Types are
23  *  on the level of the programming language, modes at the level of
24  *  the target processor.
25  *
26  * SEE ALSO
27  *   tpop.h
28  *****
29  */
30 # ifndef _TYPE_H_
31 # define _TYPE_H_
32
33 # include "tpop.h"
34 # include "common.h"
35 # include "ident.h"
36 # include "irmode.h"
37 # include "bool.h"
38
39 #ifndef _ENTITY_TYPEDEF_
40 #define _ENTITY_TYPEDEF_
41 /* to resolve recursion between entity.h and type.h */
42 typedef struct entity entity;
43 #endif
44
45 #ifndef _IR_NODE_TYPEDEF_
46 #define _IR_NODE_TYPEDEF_
47 typedef struct ir_node ir_node;
48 #endif
49
50 /****s* type/type
51  *
52  * NAME
53  *   type - An abstract data type to represent types.
54  * NOTE
55  *  This is the abstract data type with which any type known in the
56  *  compiled program can be represented.  This includes types specified
57  *  in the program as well as types defined by the language.  In the
58  *  view of the intermediate representation there is no difference
59  *  between these types.
60  *
61  *  There exist several kinds of types, arranged by the structure of
62  *  the type.  These are distinguished by a type opcode.
63  *  A type is described by a set of attributes.  Some of these attributes
64  *  are common to all types, others depend on the kind of the type.
65  *
66  *  The following describes the common attributes.  They can only be
67  *  accessed by the functions given below.
68  *
69  * ATTRIBUTES
70  *  The common fields are:
71  *
72  *  firm_kind    A firm_kind tag containing k_type.  This is useful
73  *               for dynamically checking whether a node is a type node.
74  *  type_op      A tp_op specifying the kind of the type.
75  *  mode         The mode to be used to represent the type on a machine.
76  *               @@@ maybe not global field??
77  *  name         An identifier specifying the name of the type.  To be
78  *               set by the frontend.
79  *  size         The size of the type, i.e. an entity of this type will
80  *               occupy size bytes in memory.  In several cases this is
81  *               determined when fixing the layout of this type (class,
82  *               struct, union, array, enumeration).
83  *  state        The state of the type.  The state represents whether the
84  *               layout of the type is undefined or fixed (values: layout_undefined
85  *               or layout_fixed).  Compound types can have an undefined
86  *               layout.  The layout of the basic types primitive and pointer
87  *               is always layout_fixed.  If the layout of
88  *               compound types is fixed all entities must have an offset
89  *               and the size of the type must be set.
90  *               A fixed layout for enumeration types means that each enumeration
91  *               is associated with an implementation value.
92  *  visit        A counter for walks of the type information.
93  *
94  *  These fields can only be accessed via access functions.
95  *
96  *  Depending on the value of type_op, i.e., depending on the kind of the
97  *  type the adt contains further attributes.  These are documented below.
98  * SEE ALSO
99  *   class, struct, method, union, array, enumeration, pointer, primitive
100  * SOURCE
101  */
102 typedef struct type type;
103
104 void*       get_type_link(type *tp);
105 void        set_type_link(type *tp, void *l);
106 tp_op*      get_type_tpop(type *tp);
107 ident*      get_type_tpop_nameid(type *tp);
108 const char* get_type_tpop_name(type *tp);
109 tp_opcode   get_type_tpop_code(type *tp);
110
111 ir_mode*    get_type_mode(type *tp);
112 void        set_type_mode(type *tp, ir_mode* m);
113
114 ident*      get_type_ident(type *tp);
115 void        set_type_ident(type *tp, ident* id);
116 const char* get_type_name(type *tp);
117
118 int         get_type_size(type *tp);
119 /* For primitives and pointer types the size is always fixed.
120    This call is legal but has no effect. */
121 void        set_type_size(type *tp, int size);
122
123 typedef enum {
124   layout_undefined,    /* The layout of this type is not defined.
125                           Address computation to access fields is not
126                           possible, fields must be accessed by Sel nodes.
127                           This is the default value except for pointer and
128                           primitive types. */
129   layout_fixed         /* The layout is fixed, all component/member entities
130                           have an offset assigned.  Size of the type is known.
131                           Arrays can be accessed by explicit address
132                           computation. Default for pointer and primitive types.
133                        */
134 } type_state;
135
136 type_state  get_type_state(type *tp);
137 /* For primitives and pointer types the layout is always fixed.
138    This call is legal but has no effect. */
139 void        set_type_state(type *tp, type_state state);
140
141 unsigned long get_type_visited(type *tp);
142 void        set_type_visited(type *tp, unsigned long num);
143 /* Sets visited field in type to type_visited. */
144 void        mark_type_visited(type *tp);
145 /*****/
146
147 /****v* type/visited
148  *
149  * NAME
150  *   type_visited -  visited flag to traverse the type information
151  * PURPOSE
152  *   Increase this flag by one before traversing the type information.
153  *   Mark type nodes as visited by set_type_visited(type, type_visited).
154  *   Check whether node was already visited by comparing get_type_visited(type)
155  *   and type_visited.
156  *   Or use the function to walk all types.
157  * SEE ALSO
158  *   typewalk
159  * SOURCE
160  */
161 extern unsigned long type_visited;
162 /*****/
163
164 /****f* type/is_type
165  *
166  * NAME
167  *   is_type - Checks whether a pointer points to a type.
168  * SYNOPSIS
169  *   bool is_type            (void *thing);
170  * INPUTS
171  *   thing - a pointer
172  * RESULT
173  *   true if the thing is a type, else false
174  ***
175  */
176 int is_type            (void *thing);
177
178 /****** type/class
179  * NAME
180  *  Representation of a class type.
181  * NOTE
182  *  If the type opcode is set to type_class the type represents class
183  *  types.  A list of fields and methods is associated with a class.
184  *  Further a class can inherit from and bequest to other classes.
185  *  @@@ value class???
186  * ATTRIBUTES
187  *  The following attributes are private to this type kind.
188  *  member     All entities belonging to this class.  This are methode entities
189  *             which have type_method or fields that can have any of the
190  *             following type kinds: type_class, type_struct, type_union,
191  *             type_array, type_enumeration, type_pointer, type_primitive.
192  *
193  *  subtypes   A list of direct subclasses.
194  *
195  *  supertypes A list of direct superclasses.
196  *
197  *  These are dynamic lists that can be grown with an "add_" function,
198  *  but not shrinked.
199  * SOURCE
200  */
201 /* create a new class type */
202 type   *new_type_class (ident *name);
203
204 /** manipulate private fields of class type  **/
205 /* Adds the entity as member of the class.  */
206 void    add_class_member   (type *clss, entity *member);
207 /* Returns the number of members of this class. */
208 int     get_class_n_member (type *clss);
209 /* Returns the member at position pos, 0 <= pos < n_member */
210 entity *get_class_member   (type *clss, int pos);
211 /* Overwrites the member at position pos, 0 <= pos < n_member with
212    the passed entity. */
213 void    set_class_member   (type *clss, entity *member, int pos);
214 /* Finds member in the list of members and overwrites it with NULL
215  @@@ Doesn't work properly. */
216 void    remove_class_member(type *clss, entity *member);
217
218
219 /* Adds subtype as subtype to clss.
220    Checks whether clss is a supertype of subtype.  If not
221    adds also clss as supertype to subtype.  */
222 void    add_class_subtype   (type *clss, type *subtype);
223 /* Returns the number of subtypes */
224 int     get_class_n_subtype (type *clss);
225 /* Gets the subtype at position pos, 0 <= pos < n_subtype. */
226 type   *get_class_subtype   (type *clss, int pos);
227 /* Sets the subtype at positioin pos, 0 <= pos < n_subtype.  Does not
228    set the corresponding supertype relation for subtype: this might
229    be a different position! */
230 void    set_class_subtype   (type *clss, type *subtype, int pos);
231 /* Finds subtype in the list of subtypes and overwrites it with NULL
232  @@@ Doesn't work properly. */
233 void    remove_class_subtype(type *clss, type *subtype);
234
235
236 /* Adds supertype as supertype to class.
237    Checks whether clss is a subtype of supertype.  If not
238    adds also clss as subtype to supertype.  */
239 void    add_class_supertype   (type *clss, type *supertype);
240 /* Returns the number of supertypes */
241 int     get_class_n_supertype (type *clss);
242 /* Gets the supertype at position pos,  0 <= pos < n_supertype. */
243 type   *get_class_supertype   (type *clss, int pos);
244 /* Sets the supertype at postition pos, 0 <= pos < n_subtype.  Does not
245    set the corresponding subtype relation for supertype: this might
246    be a different position! */
247 void    set_class_supertype   (type *clss, type *supertype, int pos);
248 /* Finds supertype in the list of supertypes and overwrites it with NULL
249  @@@ Doesn't work properly. */
250 void    remove_class_supertype(type *clss, type *supertype);
251
252 /* typecheck */
253 bool    is_class_type(type *clss);
254 /*****/
255
256 /****** type/struct
257  * NAME
258  *  Representation of a struct type.
259  * NOTE
260  *  Type_strct represents aggregate types that consist of a list
261  *  of fields.
262  * ATTRIBUTES
263  *  member   All entities belonging to this class.  This are the fields
264  *           that can have any of the following types:  type_class,
265  *           type_struct, type_union, type_array, type_enumeration,
266  *           type_pointer, type_primitive.
267  *           This is a dynamic list that can be grown with an "add_" function,
268  *           but not shrinked.
269  *           This is a dynamic list that can be grown with an "add_" function,
270  *           but not shrinked.
271  * SOURCE
272  */
273 /* create a new type struct */
274 type   *new_type_struct (ident *name);
275
276 /* manipulate private fields of struct */
277 void    add_struct_member   (type *strct, entity *member);
278 int     get_struct_n_member (type *strct);
279 entity *get_struct_member   (type *strct, int pos);
280 void    set_struct_member   (type *strct, int pos, entity *member);
281 /* Finds member in the list of memberss and overwrites it with NULL
282  @@@ Doesn't work properly. */
283 void    remove_struct_member (type *strct, entity *member);
284
285 /* typecheck */
286 bool    is_struct_type(type *strct);
287 /*****/
288
289 /****** type/method
290  * NAME
291  *  Representation of a method type.
292  * NOTE
293  *  A method type represents a method, function or procedure type.
294  *  It contains a list of the parameter and result types, as these
295  *  are part of the type description.  These lists should not
296  *  be changed by a optimization, as a change creates a new method
297  *  type.  Therefore optimizations should allocated new method types.
298  *  The set_ routines are only for construction by a frontend.
299  * ATTRIBUTES
300  *  n_params    Number of parameters to the procedure.
301  *              A procedure in FIRM has only call by value parameters.
302  *
303  *  param_type  A list with the types of parameters.  This list is ordered.
304  *              The nth type in this list corresponds to the nth element
305  *              in the parameter tuple that is a result of the start node.
306  *              (See ircons.h for more information.)
307  *
308  *  n_res       The number of results of the method.  In general, procedures
309  *              have zero results, functions one.
310  *
311  *  res_type    A list with the types of parameters.  This list is ordered.
312  *              The nth type in this list corresponds to the nth input to
313  *              Return nodes.  (See ircons.h for more information.)
314  * SOURCE
315  */
316
317 /* Create a new method type.
318    N_param is the number of parameters, n_res the number of results.
319    The arrays for the parameter and result types are not initialized by
320    the constructor. */
321 type *new_type_method (ident *name, int n_param, int n_res);
322
323 /* manipulate private fields of method. */
324 int   get_method_n_params  (type *method);
325 type *get_method_param_type(type *method, int pos);
326 void  set_method_param_type(type *method, int pos, type* type);
327
328 int   get_method_n_res   (type *method);
329 type *get_method_res_type(type *method, int pos);
330 void  set_method_res_type(type *method, int pos, type* type);
331
332 /* typecheck */
333 bool  is_method_type     (type *method);
334 /*****/
335
336 /****** type/union
337  * NAME
338  *   Representation of a union type.
339  * NOTE
340  *   The union type represents union types.
341  * ATTRIBUTES
342  *   n_types        Number of unioned types.
343  *   members        Entities for unioned types.  Fixed length array.
344  *                  This is a dynamic list that can be grown with an "add_" function,
345  *                  but not shrinked.
346  * SOURCE
347  */
348 /* create a new type union  */
349 type   *new_type_union (ident *name);
350
351 /* manipulate private fields of struct */
352 int     get_union_n_members      (type *uni);
353 void    add_union_member (type *uni, entity *member);
354 entity *get_union_member (type *uni, int pos);
355 void    set_union_member (type *uni, int pos, entity *member);
356 /* Finds member in the list of members and overwrites it with NULL
357    @@@ Doesn't work properly. */
358 void    remove_union_member (type *uni, entity *member);
359
360 /* typecheck */
361 bool    is_union_type          (type *uni);
362 /*****/
363
364 #if 0
365 /* We don't need these if the union has entities, which it now
366    does. The entities are necessary for the analysis algorithms. */
367 type  *get_union_unioned_type (type *uni, int pos);
368 void   set_union_unioned_type (type *uni, int pos, type *type);
369
370 ident *get_union_delim_nameid (type *uni, int pos);
371 const char *get_union_delim_name (type *uni, int pos);
372 void   set_union_delim_nameid (type *uni, int pos, ident *id);
373 #endif
374
375 /****** type/array
376  * NAME
377  *   Representation of an array type.
378  * NOTE
379  *   The array type represents rectangular multi dimensional arrays.
380  * ATTRIBUTES
381  *   n_dimensions     Number of array dimensions.
382  *   *lower_bound     Lower bounds of dimensions.  Usually all 0.
383  *   *upper_bound     Upper bounds or dimensions.
384  *   *element_type    The type of the array elements.
385  *   *element_ent     An entity for the array elements to be used for
386  *                    element selection with Sel.
387  *                    @@@ Do we need several entities?  One might want
388  *                    to select a dimension and not a single element in
389  *                    case of multidim arrays.
390  * SOURCE
391  */
392 /* create a new type array --
393    Set dimension sizes after call to constructor with set_* routines.
394    Entity for array elements is built automatically. */
395 type *new_type_array         (ident *name, int n_dimensions,
396                               type *element_type);
397
398 /* manipulate private fields of array type */
399 int   get_array_n_dimensions (type *array);
400 void  set_array_bounds       (type *array, int dimension, ir_node *lower_bound,
401                                                           ir_node *upper_bound);
402 void  set_array_lower_bound  (type *array, int dimension, ir_node *lower_bound);
403 void  set_array_upper_bound  (type *array, int dimension, ir_node *upper_bound);
404 ir_node * get_array_lower_bound  (type *array, int dimension);
405 ir_node * get_array_upper_bound  (type *array, int dimension);
406
407 void  set_array_element_type (type *array, type *type);
408 type *get_array_element_type (type *array);
409
410 void  set_array_element_entity (type *array, entity *ent);
411 entity *get_array_element_entity (type *array);
412
413 /* typecheck */
414 bool   is_array_type         (type *array);
415 /*****/
416
417 /****** type/enumeration
418  * NAME
419  *  Representation of an enumeration type.
420  * NOTE
421  *  Enumeration types need not necessarily be represented explicitly
422  *  by Firm types, as the frontend can lower them to integer constants as
423  *  well.  For debugging purposes or similar tasks this information is useful.
424  * ATTRIBUTES
425  *   *enum           The target values representing the constants used to
426  *                   represent individual enumerations.
427  *   *enum_nameid    Idents containing the source program name of the enumeration
428  *                   constants
429  *
430 *****
431 */
432 /* create a new type enumeration -- set the enumerators independently */
433 type   *new_type_enumeration    (ident *name, int n_enums);
434
435 /* manipulate fields of enumeration type. */
436 int     get_enumeration_n_enums (type *enumeration);
437
438 void    set_enumeration_enum    (type *enumeration, int pos, tarval *con);
439 tarval *get_enumeration_enum    (type *enumeration, int pos);
440
441 void    set_enumeration_nameid  (type *enumeration, int pos, ident *id);
442 ident  *get_enumeration_nameid  (type *enumeration, int pos);
443 const char *get_enumeration_name(type *enumeration, int pos);
444
445 /* typecheck */
446 bool    is_enumeration_type     (type *enumeration);
447 /*****/
448
449 /****** type/pointer
450  * NAME
451  *   Representation of a pointer type.
452  * NOTE
453  *   Pointer types.
454  * ATTRIBUTES
455  *   points_to       The type of the entity this pointer points to.
456  * SOURCE
457  */
458 /* Create a new type pointer */
459 type *new_type_pointer           (ident *name, type *points_to);
460
461 /* manipulate fields of type_pointer */
462 void  set_pointer_points_to_type (type *pointer, type *type);
463 type *get_pointer_points_to_type (type *pointer);
464
465 /* typecheck */
466 bool  is_pointer_type            (type *pointer);
467 /*****/
468
469 /****** type/primitive
470  * NAME
471  *   Representation of a primitive type.
472  * NOTE
473  *   Primitive types are types that represent indivisible data values that
474  *   map directly to modes.  They don't have a private attribute.  The
475  *   important information they carry is held in the common mode field.
476  * SOURCE
477 */
478 /* create a new type primitive */
479 type *new_type_primitive (ident *name, ir_mode *mode);
480
481 /* typecheck */
482 bool  is_primitive_type  (type *primitive);
483 /*****/
484
485 # endif /* _TYPE_H_ */