removed entity_visited flag - use type_visited instead
[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 information 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 /* Frees the memory used by the type.   Does not free the entities
112    belonging to the type, except for the array element entity.  */
113 void        free_type(type *tp);
114
115 tp_op*      get_type_tpop(type *tp);
116 ident*      get_type_tpop_nameid(type *tp);
117 const char* get_type_tpop_name(type *tp);
118 tp_opcode   get_type_tpop_code(type *tp);
119
120 ident*      get_type_ident(type *tp);
121 void        set_type_ident(type *tp, ident* id);
122 const char* get_type_name(type *tp);
123
124 typedef enum {
125   layout_undefined,    /* The layout of this type is not defined.
126                           Address computation to access fields is not
127                           possible, fields must be accessed by Sel
128                           nodes.  This is the default value except for
129                           pointer, primitive and method types. */
130   layout_fixed         /* The layout is fixed, all component/member entities
131                           have an offset assigned.  Size of the type is known.
132                           Arrays can be accessed by explicit address
133                           computation. Default for pointer, primitive ane method
134                           types.  */
135 } type_state;
136 type_state  get_type_state(type *tp);
137 /* For primitives, pointer and method 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 /* Returns NULL for all non atomic types. */
142 ir_mode*    get_type_mode(type *tp);
143 /* Only has an effect on primitive and enumeration types */
144 void        set_type_mode(type *tp, ir_mode* m);
145
146 int         get_type_size(type *tp);
147 /* For primitive, enumeration, pointer and method types the size
148    is always fixed. This call is legal but has no effect. */
149 void        set_type_size(type *tp, int size);
150
151
152 unsigned long get_type_visited(type *tp);
153 void        set_type_visited(type *tp, unsigned long num);
154 /* Sets visited field in type to type_visited. */
155 void        mark_type_visited(type *tp);
156
157 void*       get_type_link(type *tp);
158 void        set_type_link(type *tp, void *l);
159 /*****/
160
161 /****v* type/visited
162  *
163  * NAME
164  *   type_visited -  visited flag to traverse the type information
165  * PURPOSE
166  *   Increase this flag by one before traversing the type information.
167  *   Mark type nodes as visited by set_type_visited(type, type_visited).
168  *   Check whether node was already visited by comparing get_type_visited(type)
169  *   and type_visited.
170  *   Or use the function to walk all types.
171  * SEE ALSO
172  *   typewalk
173  * SOURCE
174  */
175 extern unsigned long type_visited;
176 /*****/
177
178 /****f* type/is_type
179  *
180  * NAME
181  *   is_type - Checks whether a pointer points to a type.
182  * SYNOPSIS
183  *   bool is_type            (void *thing);
184  * INPUTS
185  *   thing - a pointer
186  * RESULT
187  *   true if the thing is a type, else false
188  ***
189  */
190 int is_type            (void *thing);
191
192 /****f* type/equal_types
193  *
194  * NAME
195  *   equal_type - Checks whether two types are structural equal.
196  * SYNOPSIS
197  *   bool equal_types   (type *typ1, type *typ2);
198  * INPUTS
199  *   two pointer types
200  * RESULT
201  *   true if the types are equal, else false.
202  *   Types are equal if
203  *    - they are the same type kind
204  *    - they have the same name
205  *    - they have the same mode (if applicable)
206  *    - they have the same type_state and, ev., the same size
207  *    - they are class types and have
208  *      - the same members (see same_entity in entity.h)
209  *      - the same supertypes -- the C-pointers are compared --> no recursive call.
210  *      - the same number of subtypes.  Subtypes are not compared,
211  *        as this could cause a cyclic test.
212  *      - the same peculiarity
213  *    - they are structure types and have the same members
214  *    - they are method types and have
215  *      - the same parameter types
216  *      - the same result types
217  *    - they are union types and have the same members
218  *    - they are array types and have
219  *      - the same number of dimensions
220  *      - the same dimension bounds
221  *      - the same dimension order
222  *      - the same element type
223  *    - they are enumeration types and have the same enumerator names
224  *    - they are pointer types and have the identical points_to type
225  *      (i.e., the same C-struct to represent the type, type_id is skipped.
226  *       This is to avoid endless recursions; with pointer types circlic
227  *       type graphs are possible.)
228  *
229  ***
230  */
231 bool equal_type(type *tpy1, type *typ2);
232
233 /****f* type/smaller_type
234  *
235  * NAME
236  *   smaller_type - Checks whether two types are structural comparable.
237  * SYNOPSIS
238  *   bool smaller_type   (type *st, type *lt);
239  * INPUTS
240  *   two pointer type
241  * RESULT
242  *   true if type st is smaller than type lt, i.e. whenever
243  *   lt is expected a st can be used.
244  *   This is true if
245  *    - they are the same type kind
246  *    - mode(st) < mode (lt)  (if applicable)
247  *    - they are class types and st is (transitive) subtype of lt,
248  *    - they are structure types and
249  *       - the members of st have exactly one counterpart in lt with the same name,
250  *       - the counterpart has a bigger type.
251  *    - they are method types and have
252  *      - the same number of parameter and result types,
253  *      - the parameter types of st are smaller than those of lt,
254  *      - the result types of st are smaller than those of lt
255  *    - they are union types and have the members of st have exactly one
256  *      counterpart in lt and the type is smaller
257  *    - they are array types and have
258  *      - the same number of dimensions
259  *      - all bounds of lt are bound of st
260  *      - the same dimension order
261  *      - the same element type
262  *      or
263  *      - the element type of st is smaller than that of lt
264  *      - the element types have the same size and fixed layout.
265  *    - they are enumeration types and have the same enumerator names
266  *    - they are pointer types and have the points_to type of st is
267  *      smaller than the points_to type of lt.
268  ***
269  */
270 bool smaller_type (type *st, type *lt);
271
272 /****** type/class
273  * NAME
274  *  Representation of a class type.
275  * NOTE
276  *  If the type opcode is set to type_class the type represents class
277  *  types.  A list of fields and methods is associated with a class.
278  *  Further a class can inherit from and bequest to other classes.
279  *  @@@ value class???
280  * ATTRIBUTES
281  *  The following attributes are private to this type kind.
282  *  member     All entities belonging to this class.  This are methode entities
283  *             which have type_method or fields that can have any of the
284  *             following type kinds: type_class, type_struct, type_union,
285  *             type_array, type_enumeration, type_pointer, type_primitive.
286  *
287  *  subtypes   A list of direct subclasses.
288  *
289  *  supertypes A list of direct superclasses.
290  *
291  *  These are dynamic lists that can be grown with an "add_" function,
292  *  but not shrinked.
293  *
294  *  peculiarity The peculiarity of this class.  If the class is of peculiarity
295  *             "description" it only is a description of requirememts to a class,
296  *             as, e.g., a Java interface.  The class will never be allocated.
297  *             Peculiatity inherited is only possible for entities.  An entity
298  *             is of peculiarity inherited if the compiler generated the entity
299  *             to explicitly resolve inheritance.  An inherited method entity has
300  *             no value for irg.
301  *             Values: description, existent, inherited.  Default: existent.
302  *
303  * SOURCE
304  */
305 /* create a new class type */
306 type   *new_type_class (ident *name);
307
308 /** manipulate private fields of class type  **/
309 /* Adds the entity as member of the class.  */
310 void    add_class_member   (type *clss, entity *member);
311 /* Returns the number of members of this class. */
312 int     get_class_n_members (type *clss);
313 /* Returns the member at position pos, 0 <= pos < n_member */
314 entity *get_class_member   (type *clss, int pos);
315 /* Overwrites the member at position pos, 0 <= pos < n_member with
316    the passed entity. */
317 void    set_class_member   (type *clss, entity *member, int pos);
318 /* Replaces complete member list in class type by the list passed.  Copies the
319    list passed. This function is necessary to reduce the number of members.
320    members is an array of entities, num the size of this array.  Sets all
321    owners of the members passed to clss. */
322 void    set_class_members  (type *clss, entity **members, int arity);
323 /* Finds member in the list of members and removes it.
324    Shrinks the member list, so iterate from the end!!!
325    Does not deallocate the entity.  */
326 void    remove_class_member(type *clss, entity *member);
327
328
329 /* Adds subtype as subtype to clss.
330    Checks whether clss is a supertype of subtype.  If not
331    adds also clss as supertype to subtype.  */
332 void    add_class_subtype   (type *clss, type *subtype);
333 /* Returns the number of subtypes */
334 int     get_class_n_subtypes (type *clss);
335 /* Gets the subtype at position pos, 0 <= pos < n_subtype. */
336 type   *get_class_subtype   (type *clss, int pos);
337 /* Sets the subtype at positioin pos, 0 <= pos < n_subtype.  Does not
338    set the corresponding supertype relation for subtype: this might
339    be a different position! */
340 void    set_class_subtype   (type *clss, type *subtype, int pos);
341 /* Finds subtype in the list of subtypes and removes it  */
342 void    remove_class_subtype(type *clss, type *subtype);
343
344
345 /* Adds supertype as supertype to class.
346    Checks whether clss is a subtype of supertype.  If not
347    adds also clss as subtype to supertype.  */
348 void    add_class_supertype   (type *clss, type *supertype);
349 /* Returns the number of supertypes */
350 int     get_class_n_supertypes (type *clss);
351 /* Gets the supertype at position pos,  0 <= pos < n_supertype. */
352 type   *get_class_supertype   (type *clss, int pos);
353 /* Sets the supertype at postition pos, 0 <= pos < n_subtype.  Does not
354    set the corresponding subtype relation for supertype: this might
355    be a different position! */
356 void    set_class_supertype   (type *clss, type *supertype, int pos);
357 /* Finds supertype in the list of supertypes and removes it */
358 void    remove_class_supertype(type *clss, type *supertype);
359
360 /* This enumeration flags the peculiarity of entities and types. */
361 typedef enum peculiarity {
362   description,     /* Represents only a description.  The entity/type is never
363                       allocated, no code/data exists for this entity/type. */
364   inherited,       /* Describes explicitly that other entities are
365                       inherited to the owner of this entity.
366                       Overwrites must refer to at least one other
367                       entity.  If this is a method entity there exists
368                       no irg for this entity, only for one of the
369                       overwritten ones. */
370   existent         /* The entity/type (can) exist. */
371 } peculiarity;
372
373 /* The peculiarity of the class.  The enumeration peculiarity is defined
374    in entity.h */
375 INLINE peculiarity get_class_peculiarity (type *clss);
376 INLINE void        set_class_peculiarity (type *clss, peculiarity pec);
377
378 /* Set and get a class' dfn --
379    @@@ This is an undocumented field, subject to change! */
380 void set_class_dfn (type*, int);
381 int  get_class_dfn (type*);
382
383 /* typecheck */
384 bool is_class_type(type *clss);
385 /* Returns true if low is subclass of high. */
386 bool is_subclass_of(type *low, type *high);
387 /*****/
388
389 /****** type/struct
390  * NAME
391  *  Representation of a struct type.
392  * NOTE
393  *  Type_strct represents aggregate types that consist of a list
394  *  of fields.
395  * ATTRIBUTES
396  *  member   All entities belonging to this class.  This are the fields
397  *           that can have any of the following types:  type_class,
398  *           type_struct, type_union, type_array, type_enumeration,
399  *           type_pointer, type_primitive.
400  *           This is a dynamic list that can be grown with an "add_" function,
401  *           but not shrinked.
402  *           This is a dynamic list that can be grown with an "add_" function,
403  *           but not shrinked.
404  * SOURCE
405  */
406 /* create a new type struct */
407 type   *new_type_struct (ident *name);
408
409 /* manipulate private fields of struct */
410 void    add_struct_member   (type *strct, entity *member);
411 int     get_struct_n_members (type *strct);
412 entity *get_struct_member   (type *strct, int pos);
413 void    set_struct_member   (type *strct, int pos, entity *member);
414 /* Finds member in the list of memberss and removees it */
415 void    remove_struct_member (type *strct, entity *member);
416
417 /* typecheck */
418 bool    is_struct_type(type *strct);
419 /*****/
420
421 /****** type/method
422  * NAME
423  *  Representation of a method type.
424  * NOTE
425  *  A method type represents a method, function or procedure type.
426  *  It contains a list of the parameter and result types, as these
427  *  are part of the type description.  These lists should not
428  *  be changed by a optimization, as a change creates a new method
429  *  type.  Therefore optimizations should allocated new method types.
430  *  The set_ routines are only for construction by a frontend.
431  * ATTRIBUTES
432  *  n_params    Number of parameters to the procedure.
433  *              A procedure in FIRM has only call by value parameters.
434  *
435  *  param_type  A list with the types of parameters.  This list is ordered.
436  *              The nth type in this list corresponds to the nth element
437  *              in the parameter tuple that is a result of the start node.
438  *              (See ircons.h for more information.)
439  *
440  *  n_res       The number of results of the method.  In general, procedures
441  *              have zero results, functions one.
442  *
443  *  res_type    A list with the types of parameters.  This list is ordered.
444  *              The nth type in this list corresponds to the nth input to
445  *              Return nodes.  (See ircons.h for more information.)
446  * SOURCE
447  */
448
449 /* Create a new method type.
450    N_param is the number of parameters, n_res the number of results.
451    The arrays for the parameter and result types are not initialized by
452    the constructor. */
453 type *new_type_method (ident *name, int n_param, int n_res);
454
455 /* manipulate private fields of method. */
456 int   get_method_n_params  (type *method);
457 type *get_method_param_type(type *method, int pos);
458 void  set_method_param_type(type *method, int pos, type* type);
459
460 int   get_method_n_ress   (type *method);
461 type *get_method_res_type(type *method, int pos);
462 void  set_method_res_type(type *method, int pos, type* type);
463
464 /* typecheck */
465 bool  is_method_type     (type *method);
466 /*****/
467
468 /****** type/union
469  * NAME
470  *   Representation of a union type.
471  * NOTE
472  *   The union type represents union types.
473  * ATTRIBUTES
474  *   n_types        Number of unioned types.
475  *   members        Entities for unioned types.  Fixed length array.
476  *                  This is a dynamic list that can be grown with an "add_" function,
477  *                  but not shrinked.
478  * SOURCE
479  */
480 /* create a new type union  */
481 type   *new_type_union (ident *name);
482
483 /* manipulate private fields of struct */
484 int     get_union_n_members      (type *uni);
485 void    add_union_member (type *uni, entity *member);
486 entity *get_union_member (type *uni, int pos);
487 void    set_union_member (type *uni, int pos, entity *member);
488 /* Finds member in the list of members and removes it. */
489 void    remove_union_member (type *uni, entity *member);
490
491 /* typecheck */
492 bool    is_union_type          (type *uni);
493 /*****/
494
495 #if 0
496 /* We don't need these if the union has entities, which it now
497    does. The entities are necessary for the analysis algorithms. */
498 type  *get_union_unioned_type (type *uni, int pos);
499 void   set_union_unioned_type (type *uni, int pos, type *type);
500
501 ident *get_union_delim_nameid (type *uni, int pos);
502 const char *get_union_delim_name (type *uni, int pos);
503 void   set_union_delim_nameid (type *uni, int pos, ident *id);
504 #endif
505
506 /****** type/array
507  * NAME
508  *   Representation of an array type.
509  * NOTE
510  *   The array type represents rectangular multi dimensional arrays.
511  *   The constants representing the bounds must be allocated to
512  *   get_const_code_irg() by setting current_ir_graph accordingly.
513  * ATTRIBUTES
514  *   n_dimensions     Number of array dimensions.
515  *   *lower_bound     Lower bounds of dimensions.  Usually all 0.
516  *   *upper_bound     Upper bounds or dimensions.
517  *   *element_type    The type of the array elements.
518  *   *element_ent     An entity for the array elements to be used for
519  *                    element selection with Sel.
520  *                    @@@ Do we need several entities?  One might want
521  *                    to select a dimension and not a single element in
522  *                    case of multidim arrays.
523  * SOURCE
524  */
525 /* create a new type array --
526    Sets n_dimension to dimension and all dimension entries to NULL.
527    Initializes order to the order of the dimensions.
528    The entity for array elements is built automatically.
529    Set dimension sizes after call to constructor with set_* routines. */
530 type *new_type_array         (ident *name, int n_dimensions,
531                               type *element_type);
532
533 /* manipulate private fields of array type */
534 int   get_array_n_dimensions (type *array);
535 /* Allocates Const nodes of mode_I for the array dimensions */
536 void  set_array_bounds_int   (type *array, int dimension, int lower_bound,
537                                                           int upper_bound);
538 void  set_array_bounds       (type *array, int dimension, ir_node *lower_bound,
539                                                           ir_node *upper_bound);
540 void  set_array_lower_bound  (type *array, int dimension, ir_node *lower_bound);
541 void  set_array_lower_bound_int (type *array, int dimension, int lower_bound);
542 void  set_array_upper_bound  (type *array, int dimension, ir_node *upper_bound);
543 void  set_array_upper_bound_int (type *array, int dimension, int lower_bound);
544 ir_node * get_array_lower_bound  (type *array, int dimension);
545 ir_node * get_array_upper_bound  (type *array, int dimension);
546
547 void set_array_order (type *array, int dimension, int order);
548 int  get_array_order (type *array, int dimension);
549
550 void  set_array_element_type (type *array, type *type);
551 type *get_array_element_type (type *array);
552
553 void  set_array_element_entity (type *array, entity *ent);
554 entity *get_array_element_entity (type *array);
555
556 /* typecheck */
557 bool   is_array_type         (type *array);
558 /*****/
559
560 /****** type/enumeration
561  * NAME
562  *  Representation of an enumeration type.
563  * NOTE
564  *  Enumeration types need not necessarily be represented explicitly
565  *  by Firm types, as the frontend can lower them to integer constants as
566  *  well.  For debugging purposes or similar tasks this information is useful.
567  * ATTRIBUTES
568  *   *enum           The target values representing the constants used to
569  *                   represent individual enumerations.
570  *   *enum_nameid    Idents containing the source program name of the enumeration
571  *                   constants
572  *
573 *****
574 */
575 /* create a new type enumeration -- set the enumerators independently */
576 type   *new_type_enumeration    (ident *name, int n_enums);
577
578 /* manipulate fields of enumeration type. */
579 int     get_enumeration_n_enums (type *enumeration);
580
581 void    set_enumeration_enum    (type *enumeration, int pos, tarval *con);
582 tarval *get_enumeration_enum    (type *enumeration, int pos);
583
584 void    set_enumeration_nameid  (type *enumeration, int pos, ident *id);
585 ident  *get_enumeration_nameid  (type *enumeration, int pos);
586 const char *get_enumeration_name(type *enumeration, int pos);
587
588 /* typecheck */
589 bool    is_enumeration_type     (type *enumeration);
590 /*****/
591
592 /****** type/pointer
593  * NAME
594  *   Representation of a pointer type.
595  * NOTE
596  *   Pointer types.
597  * ATTRIBUTES
598  *   points_to       The type of the entity this pointer points to.
599  * SOURCE
600  */
601 /* Create a new type pointer */
602 type *new_type_pointer           (ident *name, type *points_to);
603
604 /* manipulate fields of type_pointer */
605 void  set_pointer_points_to_type (type *pointer, type *type);
606 type *get_pointer_points_to_type (type *pointer);
607
608 /* typecheck */
609 bool  is_pointer_type            (type *pointer);
610 /*****/
611
612 /****** type/primitive
613  * NAME
614  *   Representation of a primitive type.
615  * NOTE
616  *   Primitive types are types that represent indivisible data values that
617  *   map directly to modes.  They don't have a private attribute.  The
618  *   important information they carry is held in the common mode field.
619  * SOURCE
620 */
621 /* create a new type primitive */
622 type *new_type_primitive (ident *name, ir_mode *mode);
623
624 /* typecheck */
625 bool  is_primitive_type  (type *primitive);
626 /*****/
627
628
629
630 /****f* type/is_atomic_type
631  *
632  * NAME
633  *   is_atomic_type - Checks whether a type is atomic.
634  * SYNOPSIS
635  *   int is_atomic_type(type *tp);
636  * INPUTS
637  *   tp - any type
638  * RESULT
639  *   true if type is primitive, pointer or enumeration
640  ***
641  */
642 int is_atomic_type(type *tp);
643
644 /****f* type/is_compound_type
645  *
646  * NAME
647  *   is_compound_type - Checks whether a type is compound.
648  * SYNOPSIS
649  *   int is_compound_type(type *tp)
650  * INPUTS
651  *   tp - any type
652  * RESULT
653  *   true if the type is class, structure, union or array type.
654  ***
655  */
656 int is_compound_type(type *tp);
657
658 # endif /* _TYPE_H_ */