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