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