1 /****h* libfirm/type6 2002/03/19 13:08:33
4 * file type.h - datastructure to hold type information.
6 * (C) 2001 by Universitaet Karlsruhe
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.
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
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.
44 #ifndef _ENTITY_TYPEDEF_
45 #define _ENTITY_TYPEDEF_
46 /* to resolve recursion between entity.h and type.h */
47 typedef struct entity entity;
50 #ifndef _IR_NODE_TYPEDEF_
51 #define _IR_NODE_TYPEDEF_
52 typedef struct ir_node ir_node;
58 * type - An abstract data type to represent types.
60 * This is the abstract data type with which any type known in the
61 * compiled program can be represented. This includes types specified
62 * in the program as well as types defined by the language. In the
63 * view of the intermediate representation there is no difference
64 * between these types.
66 * There exist several kinds of types, arranged by the structure of
67 * the type. These are distinguished by a type opcode.
68 * A type is described by a set of attributes. Some of these attributes
69 * are common to all types, others depend on the kind of the type.
71 * The following describes the common attributes. They can only be
72 * accessed by the functions given below.
75 * The common fields are:
77 * firm_kind A firm_kind tag containing k_type. This is useful
78 * for dynamically checking whether a node is a type node.
79 * type_op A tp_op specifying the kind of the type.
80 * mode The mode to be used to represent the type on a machine.
81 * @@@ maybe not global field??
82 * name An identifier specifying the name of the type. To be
83 * set by the frontend.
84 * size The size of the type, i.e. an entity of this type will
85 * occupy size bytes in memory. In several cases this is
86 * determined when fixing the layout of this type (class,
87 * struct, union, array, enumeration).
88 * state The state of the type. The state represents whether the
89 * layout of the type is undefined or fixed (values: layout_undefined
90 * or layout_fixed). Compound types can have an undefined
91 * layout. The layout of the basic types primitive and pointer
92 * is always layout_fixed. If the layout of
93 * compound types is fixed all entities must have an offset
94 * and the size of the type must be set.
95 * A fixed layout for enumeration types means that each enumeration
96 * is associated with an implementation value.
97 * visit A counter for walks of the type information.
98 * link A void* to associate some additional information with the type.
100 * These fields can only be accessed via access functions.
102 * Depending on the value of type_op, i.e., depending on the kind of the
103 * type the adt contains further attributes. These are documented below.
105 * class, struct, method, union, array, enumeration, pointer, primitive
108 #ifndef _TYPE_TYPEDEF_
109 #define _TYPE_TYPEDEF_
110 typedef struct type type;
113 # include "type_or_entity.h"
115 /* Frees the memory used by the type. Does not free the entities
116 belonging to the type, except for the array element entity. */
117 void free_type(type *tp);
119 tp_op* get_type_tpop(type *tp);
120 ident* get_type_tpop_nameid(type *tp);
121 const char* get_type_tpop_name(type *tp);
122 tp_opcode get_type_tpop_code(type *tp);
124 ident* get_type_ident(type *tp);
125 void set_type_ident(type *tp, ident* id);
126 const char* get_type_name(type *tp);
129 layout_undefined, /* The layout of this type is not defined.
130 Address computation to access fields is not
131 possible, fields must be accessed by Sel
132 nodes. This is the default value except for
133 pointer, primitive and method types. */
134 layout_fixed /* The layout is fixed, all component/member entities
135 have an offset assigned. Size of the type is known.
136 Arrays can be accessed by explicit address
137 computation. Default for pointer, primitive ane method
140 type_state get_type_state(type *tp);
141 /* For primitives, pointer and method 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);
145 /* Returns NULL for all non atomic types. */
146 ir_mode* get_type_mode(type *tp);
147 /* Only has an effect on primitive and enumeration types */
148 void set_type_mode(type *tp, ir_mode* m);
150 int get_type_size(type *tp);
151 /* For primitive, enumeration, pointer and method types the size
152 is always fixed. This call is legal but has no effect. */
153 void set_type_size(type *tp, int size);
156 unsigned long get_type_visited(type *tp);
157 void set_type_visited(type *tp, unsigned long num);
158 /* Sets visited field in type to type_visited. */
159 void mark_type_visited(type *tp);
161 void* get_type_link(type *tp);
162 void set_type_link(type *tp, void *l);
168 * type_visited - visited flag to traverse the type information
170 * Increase this flag by one before traversing the type information.
171 * Mark type nodes as visited by set_type_visited(type, type_visited).
172 * Check whether node was already visited by comparing get_type_visited(type)
174 * Or use the function to walk all types.
179 extern unsigned long type_visited;
185 * is_type - Checks whether a pointer points to a type.
187 * bool is_type (void *thing);
191 * true if the thing is a type, else false
194 int is_type (void *thing);
196 /****f* type/equal_types
199 * equal_type - Checks whether two types are structural equal.
201 * bool equal_types (type *typ1, type *typ2);
205 * true if the types are equal, else false.
207 * - they are the same type kind
208 * - they have the same name
209 * - they have the same mode (if applicable)
210 * - they have the same type_state and, ev., the same size
211 * - they are class types and have
212 * - the same members (see same_entity in entity.h)
213 * - the same supertypes -- the C-pointers are compared --> no recursive call.
214 * - the same number of subtypes. Subtypes are not compared,
215 * as this could cause a cyclic test.
216 * - the same peculiarity
217 * - they are structure types and have the same members
218 * - they are method types and have
219 * - the same parameter types
220 * - the same result types
221 * - they are union types and have the same members
222 * - they are array types and have
223 * - the same number of dimensions
224 * - the same dimension bounds
225 * - the same dimension order
226 * - the same element type
227 * - they are enumeration types and have the same enumerator names
228 * - they are pointer types and have the identical points_to type
229 * (i.e., the same C-struct to represent the type, type_id is skipped.
230 * This is to avoid endless recursions; with pointer types circlic
231 * type graphs are possible.)
235 bool equal_type(type *tpy1, type *typ2);
237 /****f* type/smaller_type
240 * smaller_type - Checks whether two types are structural comparable.
242 * bool smaller_type (type *st, type *lt);
246 * true if type st is smaller than type lt, i.e. whenever
247 * lt is expected a st can be used.
249 * - they are the same type kind
250 * - mode(st) < mode (lt) (if applicable)
251 * - they are class types and st is (transitive) subtype of lt,
252 * - they are structure types and
253 * - the members of st have exactly one counterpart in lt with the same name,
254 * - the counterpart has a bigger type.
255 * - they are method types and have
256 * - the same number of parameter and result types,
257 * - the parameter types of st are smaller than those of lt,
258 * - the result types of st are smaller than those of lt
259 * - they are union types and have the members of st have exactly one
260 * counterpart in lt and the type is smaller
261 * - they are array types and have
262 * - the same number of dimensions
263 * - all bounds of lt are bound of st
264 * - the same dimension order
265 * - the same element type
267 * - the element type of st is smaller than that of lt
268 * - the element types have the same size and fixed layout.
269 * - they are enumeration types and have the same enumerator names
270 * - they are pointer types and have the points_to type of st is
271 * smaller than the points_to type of lt.
274 bool smaller_type (type *st, type *lt);
278 * Representation of a class type.
280 * If the type opcode is set to type_class the type represents class
281 * types. A list of fields and methods is associated with a class.
282 * Further a class can inherit from and bequest to other classes.
285 * The following attributes are private to this type kind.
286 * member All entities belonging to this class. This are methode entities
287 * which have type_method or fields that can have any of the
288 * following type kinds: type_class, type_struct, type_union,
289 * type_array, type_enumeration, type_pointer, type_primitive.
291 * subtypes A list of direct subclasses.
293 * supertypes A list of direct superclasses.
295 * These are dynamic lists that can be grown with an "add_" function,
298 * peculiarity The peculiarity of this class. If the class is of peculiarity
299 * "description" it only is a description of requirememts to a class,
300 * as, e.g., a Java interface. The class will never be allocated.
301 * Peculiatity inherited is only possible for entities. An entity
302 * is of peculiarity inherited if the compiler generated the entity
303 * to explicitly resolve inheritance. An inherited method entity has
305 * Values: description, existent, inherited. Default: existent.
309 /* create a new class type */
310 type *new_type_class (ident *name);
311 type *new_d_type_class (ident *name, dbg_info *db);
313 /** manipulate private fields of class type **/
314 /* Adds the entity as member of the class. */
315 void add_class_member (type *clss, entity *member);
316 /* Returns the number of members of this class. */
317 int get_class_n_members (type *clss);
318 /* Returns the member at position pos, 0 <= pos < n_member */
319 entity *get_class_member (type *clss, int pos);
320 /* Overwrites the member at position pos, 0 <= pos < n_member with
321 the passed entity. */
322 void set_class_member (type *clss, entity *member, int pos);
323 /* Replaces complete member list in class type by the list passed. Copies the
324 list passed. This function is necessary to reduce the number of members.
325 members is an array of entities, num the size of this array. Sets all
326 owners of the members passed to clss. */
327 void set_class_members (type *clss, entity **members, int arity);
328 /* Finds member in the list of members and removes it.
329 Shrinks the member list, so iterate from the end!!!
330 Does not deallocate the entity. */
331 void remove_class_member(type *clss, entity *member);
334 /* Adds subtype as subtype to clss.
335 Checks whether clss is a supertype of subtype. If not
336 adds also clss as supertype to subtype. */
337 void add_class_subtype (type *clss, type *subtype);
338 /* Returns the number of subtypes */
339 int get_class_n_subtypes (type *clss);
340 /* Gets the subtype at position pos, 0 <= pos < n_subtype. */
341 type *get_class_subtype (type *clss, int pos);
342 /* Sets the subtype at positioin pos, 0 <= pos < n_subtype. Does not
343 set the corresponding supertype relation for subtype: this might
344 be a different position! */
345 void set_class_subtype (type *clss, type *subtype, int pos);
346 /* Finds subtype in the list of subtypes and removes it */
347 void remove_class_subtype(type *clss, type *subtype);
350 /* Adds supertype as supertype to class.
351 Checks whether clss is a subtype of supertype. If not
352 adds also clss as subtype to supertype. */
353 void add_class_supertype (type *clss, type *supertype);
354 /* Returns the number of supertypes */
355 int get_class_n_supertypes (type *clss);
356 /* Gets the supertype at position pos, 0 <= pos < n_supertype. */
357 type *get_class_supertype (type *clss, int pos);
358 /* Sets the supertype at postition pos, 0 <= pos < n_subtype. Does not
359 set the corresponding subtype relation for supertype: this might
360 be a different position! */
361 void set_class_supertype (type *clss, type *supertype, int pos);
362 /* Finds supertype in the list of supertypes and removes it */
363 void remove_class_supertype(type *clss, type *supertype);
365 /* This enumeration flags the peculiarity of entities and types. */
366 typedef enum peculiarity {
367 description, /* Represents only a description. The entity/type is never
368 allocated, no code/data exists for this entity/type. */
369 inherited, /* Describes explicitly that other entities are
370 inherited to the owner of this entity.
371 Overwrites must refer to at least one other
372 entity. If this is a method entity there exists
373 no irg for this entity, only for one of the
375 existent /* The entity/type (can) exist. */
378 /* The peculiarity of the class. The enumeration peculiarity is defined
380 INLINE peculiarity get_class_peculiarity (type *clss);
381 INLINE void set_class_peculiarity (type *clss, peculiarity pec);
383 /* Set and get a class' dfn --
384 @@@ This is an undocumented field, subject to change! */
385 void set_class_dfn (type*, int);
386 int get_class_dfn (type*);
389 bool is_class_type(type *clss);
390 /* Returns true if low is subclass of high. */
391 bool is_subclass_of(type *low, type *high);
396 * Representation of a struct type.
398 * Type_strct represents aggregate types that consist of a list
401 * member All entities belonging to this class. This are the fields
402 * that can have any of the following types: type_class,
403 * type_struct, type_union, type_array, type_enumeration,
404 * type_pointer, type_primitive.
405 * This is a dynamic list that can be grown with an "add_" function,
407 * This is a dynamic list that can be grown with an "add_" function,
411 /* create a new type struct */
412 type *new_type_struct (ident *name);
413 type *new_d_type_struct (ident *name, dbg_info* db);
415 /* manipulate private fields of struct */
416 void add_struct_member (type *strct, entity *member);
417 int get_struct_n_members (type *strct);
418 entity *get_struct_member (type *strct, int pos);
419 void set_struct_member (type *strct, int pos, entity *member);
420 /* Finds member in the list of memberss and removees it */
421 void remove_struct_member (type *strct, entity *member);
424 bool is_struct_type(type *strct);
429 * Representation of a method type.
431 * A method type represents a method, function or procedure type.
432 * It contains a list of the parameter and result types, as these
433 * are part of the type description. These lists should not
434 * be changed by a optimization, as a change creates a new method
435 * type. Therefore optimizations should allocated new method types.
436 * The set_ routines are only for construction by a frontend.
438 * n_params Number of parameters to the procedure.
439 * A procedure in FIRM has only call by value parameters.
441 * param_type A list with the types of parameters. This list is ordered.
442 * The nth type in this list corresponds to the nth element
443 * in the parameter tuple that is a result of the start node.
444 * (See ircons.h for more information.)
446 * n_res The number of results of the method. In general, procedures
447 * have zero results, functions one.
449 * res_type A list with the types of parameters. This list is ordered.
450 * The nth type in this list corresponds to the nth input to
451 * Return nodes. (See ircons.h for more information.)
455 /* Create a new method type.
456 N_param is the number of parameters, n_res the number of results.
457 The arrays for the parameter and result types are not initialized by
459 type *new_type_method (ident *name, int n_param, int n_res);
460 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db);
462 /* manipulate private fields of method. */
463 int get_method_n_params (type *method);
464 type *get_method_param_type(type *method, int pos);
465 void set_method_param_type(type *method, int pos, type* type);
467 int get_method_n_ress (type *method);
468 type *get_method_res_type(type *method, int pos);
469 void set_method_res_type(type *method, int pos, type* type);
472 bool is_method_type (type *method);
477 * Representation of a union type.
479 * The union type represents union types.
481 * n_types Number of unioned types.
482 * members Entities for unioned types. Fixed length array.
483 * This is a dynamic list that can be grown with an "add_" function,
487 /* create a new type union */
488 type *new_type_union (ident *name);
489 type *new_d_type_union (ident *name, dbg_info* db);
491 /* manipulate private fields of struct */
492 int get_union_n_members (type *uni);
493 void add_union_member (type *uni, entity *member);
494 entity *get_union_member (type *uni, int pos);
495 void set_union_member (type *uni, int pos, entity *member);
496 /* Finds member in the list of members and removes it. */
497 void remove_union_member (type *uni, entity *member);
500 bool is_union_type (type *uni);
505 * Representation of an array type.
507 * The array type represents rectangular multi dimensional arrays.
508 * The constants representing the bounds must be allocated to
509 * get_const_code_irg() by setting current_ir_graph accordingly.
511 * n_dimensions Number of array dimensions.
512 * *lower_bound Lower bounds of dimensions. Usually all 0.
513 * *upper_bound Upper bounds or dimensions.
514 * *element_type The type of the array elements.
515 * *element_ent An entity for the array elements to be used for
516 * element selection with Sel.
517 * @@@ Do we need several entities? One might want
518 * to select a dimension and not a single element in
519 * case of multidim arrays.
522 /* create a new type array --
523 Sets n_dimension to dimension and all dimension entries to NULL.
524 Initializes order to the order of the dimensions.
525 The entity for array elements is built automatically.
526 Set dimension sizes after call to constructor with set_* routines. */
527 type *new_type_array (ident *name, int n_dimensions,
529 type *new_d_type_array (ident *name, int n_dimensions,
530 type *element_type, dbg_info* db);
532 /* manipulate private fields of array type */
533 int get_array_n_dimensions (type *array);
534 /* Allocates Const nodes of mode_I for the array dimensions */
535 void set_array_bounds_int (type *array, int dimension, int lower_bound,
537 void set_array_bounds (type *array, int dimension, ir_node *lower_bound,
538 ir_node *upper_bound);
539 void set_array_lower_bound (type *array, int dimension, ir_node *lower_bound);
540 void set_array_lower_bound_int (type *array, int dimension, int lower_bound);
541 void set_array_upper_bound (type *array, int dimension, ir_node *upper_bound);
542 void set_array_upper_bound_int (type *array, int dimension, int lower_bound);
543 ir_node * get_array_lower_bound (type *array, int dimension);
544 ir_node * get_array_upper_bound (type *array, int dimension);
546 void set_array_order (type *array, int dimension, int order);
547 int get_array_order (type *array, int dimension);
549 void set_array_element_type (type *array, type *type);
550 type *get_array_element_type (type *array);
552 void set_array_element_entity (type *array, entity *ent);
553 entity *get_array_element_entity (type *array);
556 bool is_array_type (type *array);
559 /****** type/enumeration
561 * Representation of an enumeration type.
563 * Enumeration types need not necessarily be represented explicitly
564 * by Firm types, as the frontend can lower them to integer constants as
565 * well. For debugging purposes or similar tasks this information is useful.
567 * *enum The target values representing the constants used to
568 * represent individual enumerations.
569 * *enum_nameid Idents containing the source program name of the enumeration
574 /* create a new type enumeration -- set the enumerators independently */
575 type *new_type_enumeration (ident *name, int n_enums);
576 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db);
578 /* manipulate fields of enumeration type. */
579 int get_enumeration_n_enums (type *enumeration);
581 void set_enumeration_enum (type *enumeration, int pos, tarval *con);
582 tarval *get_enumeration_enum (type *enumeration, int pos);
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);
589 bool is_enumeration_type (type *enumeration);
594 * Representation of a pointer type.
598 * points_to The type of the entity this pointer points to.
601 /* Create a new type pointer */
602 type *new_type_pointer (ident *name, type *points_to);
603 type *new_d_type_pointer (ident *name, type *points_to, dbg_info* db);
605 /* manipulate fields of type_pointer */
606 void set_pointer_points_to_type (type *pointer, type *type);
607 type *get_pointer_points_to_type (type *pointer);
610 bool is_pointer_type (type *pointer);
613 /****** type/primitive
615 * Representation of a primitive type.
617 * Primitive types are types that represent indivisible data values that
618 * map directly to modes. They don't have a private attribute. The
619 * important information they carry is held in the common mode field.
622 /* create a new type primitive */
623 type *new_type_primitive (ident *name, ir_mode *mode);
624 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db);
627 bool is_primitive_type (type *primitive);
632 /****f* type/is_atomic_type
635 * is_atomic_type - Checks whether a type is atomic.
637 * int is_atomic_type(type *tp);
641 * true if type is primitive, pointer or enumeration
644 int is_atomic_type(type *tp);
646 /****f* type/is_compound_type
649 * is_compound_type - Checks whether a type is compound.
651 * int is_compound_type(type *tp)
655 * true if the type is class, structure, union or array type.
658 int is_compound_type(type *tp);
660 # endif /* _TYPE_H_ */