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.
43 #ifndef _ENTITY_TYPEDEF_
44 #define _ENTITY_TYPEDEF_
45 /* to resolve recursion between entity.h and type.h */
46 typedef struct entity entity;
49 #ifndef _IR_NODE_TYPEDEF_
50 #define _IR_NODE_TYPEDEF_
51 typedef struct ir_node ir_node;
57 * type - An abstract data type to represent types.
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.
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.
70 * The following describes the common attributes. They can only be
71 * accessed by the functions given below.
74 * The common fields are:
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.
99 * These fields can only be accessed via access functions.
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.
104 * class, struct, method, union, array, enumeration, pointer, primitive
107 typedef struct type type;
109 # include "type_or_entity.h"
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);
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);
120 ident* get_type_ident(type *tp);
121 void set_type_ident(type *tp, ident* id);
122 const char* get_type_name(type *tp);
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
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);
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);
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);
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);
157 void* get_type_link(type *tp);
158 void set_type_link(type *tp, void *l);
164 * type_visited - visited flag to traverse the type information
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)
170 * Or use the function to walk all types.
175 extern unsigned long type_visited;
181 * is_type - Checks whether a pointer points to a type.
183 * bool is_type (void *thing);
187 * true if the thing is a type, else false
190 int is_type (void *thing);
192 /****f* type/equal_types
195 * equal_type - Checks whether two types are structural equal.
197 * bool equal_types (type *typ1, type *typ2);
201 * true if the types are equal, else false.
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.)
231 bool equal_type(type *tpy1, type *typ2);
233 /****f* type/smaller_type
236 * smaller_type - Checks whether two types are structural comparable.
238 * bool smaller_type (type *st, type *lt);
242 * true if type st is smaller than type lt, i.e. whenever
243 * lt is expected a st can be used.
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
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.
270 bool smaller_type (type *st, type *lt);
274 * Representation of a class type.
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.
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.
287 * subtypes A list of direct subclasses.
289 * supertypes A list of direct superclasses.
291 * These are dynamic lists that can be grown with an "add_" function,
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
301 * Values: description, existent, inherited. Default: existent.
305 /* create a new class type */
306 type *new_type_class (ident *name);
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 overwrites it with NULL
324 @@@ Doesn't work properly. */
325 void remove_class_member(type *clss, entity *member);
328 /* Adds subtype as subtype to clss.
329 Checks whether clss is a supertype of subtype. If not
330 adds also clss as supertype to subtype. */
331 void add_class_subtype (type *clss, type *subtype);
332 /* Returns the number of subtypes */
333 int get_class_n_subtypes (type *clss);
334 /* Gets the subtype at position pos, 0 <= pos < n_subtype. */
335 type *get_class_subtype (type *clss, int pos);
336 /* Sets the subtype at positioin pos, 0 <= pos < n_subtype. Does not
337 set the corresponding supertype relation for subtype: this might
338 be a different position! */
339 void set_class_subtype (type *clss, type *subtype, int pos);
340 /* Finds subtype in the list of subtypes and overwrites it with NULL
341 @@@ Doesn't work properly. */
342 void remove_class_subtype(type *clss, type *subtype);
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 overwrites it with NULL
358 @@@ Doesn't work properly. */
359 void remove_class_supertype(type *clss, type *supertype);
361 /* This enumeration flags the peculiarity of entities and types. */
362 typedef enum peculiarity {
363 description, /* Represents only a description. The entity/type is never
364 allocated, no code/data exists for this entity/type. */
365 inherited, /* Describes explicitly that other entities are inherited
366 to the owner of this entity. Overwrites must refer to
367 at least one other entity. If this is a method entity
368 there exists no irg for this entity, only for one of
369 the overwritten ones. */
370 existent /* The entity/type (can) exist. */
373 /* The peculiarity of the class. The enumeration peculiarity is defined
375 INLINE peculiarity get_class_peculiarity (type *clss);
376 INLINE void set_class_peculiarity (type *clss, peculiarity pec);
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*);
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);
391 * Representation of a struct type.
393 * Type_strct represents aggregate types that consist of a list
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,
402 * This is a dynamic list that can be grown with an "add_" function,
406 /* create a new type struct */
407 type *new_type_struct (ident *name);
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 overwrites it with NULL
415 @@@ Doesn't work properly. */
416 void remove_struct_member (type *strct, entity *member);
419 bool is_struct_type(type *strct);
424 * Representation of a method type.
426 * A method type represents a method, function or procedure type.
427 * It contains a list of the parameter and result types, as these
428 * are part of the type description. These lists should not
429 * be changed by a optimization, as a change creates a new method
430 * type. Therefore optimizations should allocated new method types.
431 * The set_ routines are only for construction by a frontend.
433 * n_params Number of parameters to the procedure.
434 * A procedure in FIRM has only call by value parameters.
436 * param_type A list with the types of parameters. This list is ordered.
437 * The nth type in this list corresponds to the nth element
438 * in the parameter tuple that is a result of the start node.
439 * (See ircons.h for more information.)
441 * n_res The number of results of the method. In general, procedures
442 * have zero results, functions one.
444 * res_type A list with the types of parameters. This list is ordered.
445 * The nth type in this list corresponds to the nth input to
446 * Return nodes. (See ircons.h for more information.)
450 /* Create a new method type.
451 N_param is the number of parameters, n_res the number of results.
452 The arrays for the parameter and result types are not initialized by
454 type *new_type_method (ident *name, int n_param, int n_res);
456 /* manipulate private fields of method. */
457 int get_method_n_params (type *method);
458 type *get_method_param_type(type *method, int pos);
459 void set_method_param_type(type *method, int pos, type* type);
461 int get_method_n_ress (type *method);
462 type *get_method_res_type(type *method, int pos);
463 void set_method_res_type(type *method, int pos, type* type);
466 bool is_method_type (type *method);
471 * Representation of a union type.
473 * The union type represents union types.
475 * n_types Number of unioned types.
476 * members Entities for unioned types. Fixed length array.
477 * This is a dynamic list that can be grown with an "add_" function,
481 /* create a new type union */
482 type *new_type_union (ident *name);
484 /* manipulate private fields of struct */
485 int get_union_n_members (type *uni);
486 void add_union_member (type *uni, entity *member);
487 entity *get_union_member (type *uni, int pos);
488 void set_union_member (type *uni, int pos, entity *member);
489 /* Finds member in the list of members and overwrites it with NULL
490 @@@ Doesn't work properly. */
491 void remove_union_member (type *uni, entity *member);
494 bool is_union_type (type *uni);
498 /* We don't need these if the union has entities, which it now
499 does. The entities are necessary for the analysis algorithms. */
500 type *get_union_unioned_type (type *uni, int pos);
501 void set_union_unioned_type (type *uni, int pos, type *type);
503 ident *get_union_delim_nameid (type *uni, int pos);
504 const char *get_union_delim_name (type *uni, int pos);
505 void set_union_delim_nameid (type *uni, int pos, ident *id);
510 * Representation of an array type.
512 * The array type represents rectangular multi dimensional arrays.
513 * The constants representing the bounds must be allocated to
514 * get_const_code_irg() by setting current_ir_graph accordingly.
516 * n_dimensions Number of array dimensions.
517 * *lower_bound Lower bounds of dimensions. Usually all 0.
518 * *upper_bound Upper bounds or dimensions.
519 * *element_type The type of the array elements.
520 * *element_ent An entity for the array elements to be used for
521 * element selection with Sel.
522 * @@@ Do we need several entities? One might want
523 * to select a dimension and not a single element in
524 * case of multidim arrays.
527 /* create a new type array --
528 Sets n_dimension to dimension and all dimension entries to NULL.
529 Initializes order to the order of the dimensions.
530 The entity for array elements is built automatically.
531 Set dimension sizes after call to constructor with set_* routines. */
532 type *new_type_array (ident *name, int n_dimensions,
535 /* manipulate private fields of array type */
536 int get_array_n_dimensions (type *array);
537 /* Allocates Const nodes of mode_I for the array dimensions */
538 void set_array_bounds_int (type *array, int dimension, int lower_bound,
540 void set_array_bounds (type *array, int dimension, ir_node *lower_bound,
541 ir_node *upper_bound);
542 void set_array_lower_bound (type *array, int dimension, ir_node *lower_bound);
543 void set_array_lower_bound_int (type *array, int dimension, int lower_bound);
544 void set_array_upper_bound (type *array, int dimension, ir_node *upper_bound);
545 void set_array_upper_bound_int (type *array, int dimension, int lower_bound);
546 ir_node * get_array_lower_bound (type *array, int dimension);
547 ir_node * get_array_upper_bound (type *array, int dimension);
549 void set_array_order (type *array, int dimension, int order);
550 int get_array_order (type *array, int dimension);
552 void set_array_element_type (type *array, type *type);
553 type *get_array_element_type (type *array);
555 void set_array_element_entity (type *array, entity *ent);
556 entity *get_array_element_entity (type *array);
559 bool is_array_type (type *array);
562 /****** type/enumeration
564 * Representation of an enumeration type.
566 * Enumeration types need not necessarily be represented explicitly
567 * by Firm types, as the frontend can lower them to integer constants as
568 * well. For debugging purposes or similar tasks this information is useful.
570 * *enum The target values representing the constants used to
571 * represent individual enumerations.
572 * *enum_nameid Idents containing the source program name of the enumeration
577 /* create a new type enumeration -- set the enumerators independently */
578 type *new_type_enumeration (ident *name, int n_enums);
580 /* manipulate fields of enumeration type. */
581 int get_enumeration_n_enums (type *enumeration);
583 void set_enumeration_enum (type *enumeration, int pos, tarval *con);
584 tarval *get_enumeration_enum (type *enumeration, int pos);
586 void set_enumeration_nameid (type *enumeration, int pos, ident *id);
587 ident *get_enumeration_nameid (type *enumeration, int pos);
588 const char *get_enumeration_name(type *enumeration, int pos);
591 bool is_enumeration_type (type *enumeration);
596 * Representation of a pointer type.
600 * points_to The type of the entity this pointer points to.
603 /* Create a new type pointer */
604 type *new_type_pointer (ident *name, type *points_to);
606 /* manipulate fields of type_pointer */
607 void set_pointer_points_to_type (type *pointer, type *type);
608 type *get_pointer_points_to_type (type *pointer);
611 bool is_pointer_type (type *pointer);
614 /****** type/primitive
616 * Representation of a primitive type.
618 * Primitive types are types that represent indivisible data values that
619 * map directly to modes. They don't have a private attribute. The
620 * important information they carry is held in the common mode field.
623 /* create a new type primitive */
624 type *new_type_primitive (ident *name, ir_mode *mode);
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_ */