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