3 * file type.h - datastructure to hold type information.
4 * (C) 2001 by Universitaet Karlsruhe
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.
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
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.
31 # include "firm_common.h"
38 /* to resolve recursion between entity.h and type.h */
39 #ifndef _ENTITY_TYPEDEF_
40 #define _ENTITY_TYPEDEF_
41 typedef struct entity entity;
44 #ifndef _IR_NODE_TYPEDEF_
45 #define _IR_NODE_TYPEDEF_
46 typedef struct ir_node ir_node;
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.
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.
63 * The following describes the common attributes. They can only be
64 * accessed by the functions given below.
66 * @param The common fields are:
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.
91 * @param These fields can only be accessed via access functions.
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
97 #ifndef _TYPE_TYPEDEF_
98 #define _TYPE_TYPEDEF_
99 typedef struct type type;
102 # include "type_or_entity.h"
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);
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);
113 ident* get_type_ident(type *tp);
114 void set_type_ident(type *tp, ident* id);
115 const char* get_type_name(type *tp);
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
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);
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);
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);
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);
150 void* get_type_link(type *tp);
151 void set_type_link(type *tp, void *l);
155 * visited flag to traverse the type information
156 * Increase this flag by one before traversing the type information.
157 * Mark type nodes as visited by set_type_visited(type, type_visited).
158 * Check whether node was already visited by comparing get_type_visited(type)
160 * Or use the function to walk all types.
163 extern unsigned long type_visited;
164 void set_master_type_visited(unsigned long val);
165 unsigned long get_master_type_visited();
166 void inc_master_type_visited();
170 * Checks whether a pointer points to a type.
171 * @param thing - a pointer
172 * @return true if the thing is a type, else false
175 int is_type (void *thing);
179 * Checks whether two types are structural equal.
180 * @param two pointer types
181 * @return true if the types are equal, else false.
182 * @return Types are equal if
183 * - they are the same type kind
184 * - they have the same name
185 * - they have the same mode (if applicable)
186 * - they have the same type_state and, ev., the same size
187 * - they are class types and have
188 * - the same members (see same_entity in entity.h)
189 * - the same supertypes -- the C-pointers are compared --> no recursive call.
190 * - the same number of subtypes. Subtypes are not compared,
191 * as this could cause a cyclic test.
192 * - the same peculiarity
193 * - they are structure types and have the same members
194 * - they are method types and have
195 * - the same parameter types
196 * - the same result types
197 * - they are union types and have the same members
198 * - they are array types and have
199 * - the same number of dimensions
200 * - the same dimension bounds
201 * - the same dimension order
202 * - the same element type
203 * - they are enumeration types and have the same enumerator names
204 * - they are pointer types and have the identical points_to type
205 * (i.e., the same C-struct to represent the type, type_id is skipped.
206 * This is to avoid endless recursions; with pointer types circlic
207 * type graphs are possible.)
211 bool equal_type(type *tpy1, type *typ2);
215 * Checks whether two types are structural comparable.
216 * @param two pointer type
217 * @return true if type st is smaller than type lt, i.e. whenever
218 * @return lt is expected a st can be used.
219 * @return This is true if
220 * - they are the same type kind
221 * - mode(st) < mode (lt) (if applicable)
222 * - they are class types and st is (transitive) subtype of lt,
223 * - they are structure types and
224 * - the members of st have exactly one counterpart in lt with the same name,
225 * - the counterpart has a bigger type.
226 * - they are method types and have
227 * - the same number of parameter and result types,
228 * - the parameter types of st are smaller than those of lt,
229 * - the result types of st are smaller than those of lt
230 * - they are union types and have the members of st have exactly one
231 * @return counterpart in lt and the type is smaller
232 * - they are array types and have
233 * - the same number of dimensions
234 * - all bounds of lt are bound of st
235 * - the same dimension order
236 * - the same element type
238 * - the element type of st is smaller than that of lt
239 * - the element types have the same size and fixed layout.
240 * - they are enumeration types and have the same enumerator names
241 * - they are pointer types and have the points_to type of st is
242 * @return smaller than the points_to type of lt.
245 bool smaller_type (type *st, type *lt);
248 * Representation of a class type.
249 * If the type opcode is set to type_class the type represents class
250 * types. A list of fields and methods is associated with a class.
251 * Further a class can inherit from and bequest to other classes.
253 * @param The following attributes are private to this type kind.
254 * @param member All entities belonging to this class. This are methode entities
255 * which have type_method or fields that can have any of the
256 * following type kinds: type_class, type_struct, type_union,
257 * type_array, type_enumeration, type_pointer, type_primitive.
259 * @param subtypes A list of direct subclasses.
261 * @param supertypes A list of direct superclasses.
263 * @param These are dynamic lists that can be grown with an "add_" function,
264 * @param but not shrinked.
266 * @param peculiarity The peculiarity of this class. If the class is of peculiarity
267 * "description" it only is a description of requirememts to a class,
268 * as, e.g., a Java interface. The class will never be allocated.
269 * Peculiatity inherited is only possible for entities. An entity
270 * is of peculiarity inherited if the compiler generated the entity
271 * to explicitly resolve inheritance. An inherited method entity has
273 * Values: description, existent, inherited. Default: existent.
276 /* create a new class type */
277 type *new_type_class (ident *name);
278 type *new_d_type_class (ident *name, dbg_info *db);
280 /** manipulate private fields of class type **/
281 /* Adds the entity as member of the class. */
282 void add_class_member (type *clss, entity *member);
283 /* Returns the number of members of this class. */
284 int get_class_n_members (type *clss);
285 /* Returns the member at position pos, 0 <= pos < n_member */
286 entity *get_class_member (type *clss, int pos);
287 /* Overwrites the member at position pos, 0 <= pos < n_member with
288 the passed entity. */
289 void set_class_member (type *clss, entity *member, int pos);
290 /* Replaces complete member list in class type by the list passed. Copies the
291 list passed. This function is necessary to reduce the number of members.
292 members is an array of entities, num the size of this array. Sets all
293 owners of the members passed to clss. */
294 void set_class_members (type *clss, entity *members[], int arity);
295 /* Finds member in the list of members and removes it.
296 Shrinks the member list, so iterate from the end!!!
297 Does not deallocate the entity. */
298 void remove_class_member(type *clss, entity *member);
301 /* Adds subtype as subtype to clss.
302 Checks whether clss is a supertype of subtype. If not
303 adds also clss as supertype to subtype. */
304 void add_class_subtype (type *clss, type *subtype);
305 /* Returns the number of subtypes */
306 int get_class_n_subtypes (type *clss);
307 /* Gets the subtype at position pos, 0 <= pos < n_subtype. */
308 type *get_class_subtype (type *clss, int pos);
309 /* Sets the subtype at positioin pos, 0 <= pos < n_subtype. Does not
310 set the corresponding supertype relation for subtype: this might
311 be a different position! */
312 void set_class_subtype (type *clss, type *subtype, int pos);
313 /* Finds subtype in the list of subtypes and removes it */
314 void remove_class_subtype(type *clss, type *subtype);
317 /* Adds supertype as supertype to class.
318 Checks whether clss is a subtype of supertype. If not
319 adds also clss as subtype to supertype. */
320 void add_class_supertype (type *clss, type *supertype);
321 /* Returns the number of supertypes */
322 int get_class_n_supertypes (type *clss);
323 /* Gets the supertype at position pos, 0 <= pos < n_supertype. */
324 type *get_class_supertype (type *clss, int pos);
325 /* Sets the supertype at postition pos, 0 <= pos < n_subtype. Does not
326 set the corresponding subtype relation for supertype: this might
327 be a different position! */
328 void set_class_supertype (type *clss, type *supertype, int pos);
329 /* Finds supertype in the list of supertypes and removes it */
330 void remove_class_supertype(type *clss, type *supertype);
332 /* This enumeration flags the peculiarity of entities and types. */
333 typedef enum peculiarity {
334 description, /* Represents only a description. The entity/type is never
335 allocated, no code/data exists for this entity/type. */
336 inherited, /* Describes explicitly that other entities are
337 inherited to the owner of this entity.
338 Overwrites must refer to at least one other
339 entity. If this is a method entity there exists
340 no irg for this entity, only for one of the
342 existent /* The entity/type (can) exist. */
345 /* The peculiarity of the class. The enumeration peculiarity is defined
347 INLINE peculiarity get_class_peculiarity (type *clss);
348 INLINE void set_class_peculiarity (type *clss, peculiarity pec);
350 /* Set and get a class' dfn --
351 @@@ This is an undocumented field, subject to change! */
352 void set_class_dfn (type *clss, int dfn);
353 int get_class_dfn (type *clss);
356 bool is_class_type(type *clss);
357 /* Returns true if low is subclass of high. */
358 bool is_subclass_of(type *low, type *high);
361 * Representation of a struct type.
362 * Type_strct represents aggregate types that consist of a list
364 * @param member All entities belonging to this class. This are the fields
365 * that can have any of the following types: type_class,
366 * type_struct, type_union, type_array, type_enumeration,
367 * type_pointer, type_primitive.
368 * This is a dynamic list that can be grown with an "add_" function,
370 * This is a dynamic list that can be grown with an "add_" function,
373 /* create a new type struct */
374 type *new_type_struct (ident *name);
375 type *new_d_type_struct (ident *name, dbg_info* db);
377 /* manipulate private fields of struct */
378 void add_struct_member (type *strct, entity *member);
379 int get_struct_n_members (type *strct);
380 entity *get_struct_member (type *strct, int pos);
381 void set_struct_member (type *strct, int pos, entity *member);
382 /* Finds member in the list of memberss and removees it */
383 void remove_struct_member (type *strct, entity *member);
386 bool is_struct_type(type *strct);
389 * Representation of a method type.
390 * A method type represents a method, function or procedure type.
391 * It contains a list of the parameter and result types, as these
392 * are part of the type description. These lists should not
393 * be changed by a optimization, as a change creates a new method
394 * type. Therefore optimizations should allocated new method types.
395 * The set_ routines are only for construction by a frontend.
396 * @param n_params Number of parameters to the procedure.
397 * A procedure in FIRM has only call by value parameters.
399 * @param param_type A list with the types of parameters. This list is ordered.
400 * The nth type in this list corresponds to the nth element
401 * in the parameter tuple that is a result of the start node.
402 * (See ircons.h for more information.)
404 * @param n_res The number of results of the method. In general, procedures
405 * have zero results, functions one.
407 * @param res_type A list with the types of parameters. This list is ordered.
408 * The nth type in this list corresponds to the nth input to
409 * Return nodes. (See ircons.h for more information.)
412 /* Create a new method type.
413 N_param is the number of parameters, n_res the number of results.
414 The arrays for the parameter and result types are not initialized by
416 type *new_type_method (ident *name, int n_param, int n_res);
417 type *new_d_type_method (ident *name, int n_param, int n_res, dbg_info* db);
419 /* manipulate private fields of method. */
420 int get_method_n_params (type *method);
421 type *get_method_param_type(type *method, int pos);
422 void set_method_param_type(type *method, int pos, type* tp);
424 int get_method_n_ress (type *method);
425 type *get_method_res_type(type *method, int pos);
426 void set_method_res_type(type *method, int pos, type* tp);
429 bool is_method_type (type *method);
432 * Representation of a union type.
433 * The union type represents union types.
434 * @param n_types Number of unioned types.
435 * @param members Entities for unioned types. Fixed length array.
436 * This is a dynamic list that can be grown with an "add_" function,
439 /* create a new type union */
440 type *new_type_union (ident *name);
441 type *new_d_type_union (ident *name, dbg_info* db);
443 /* manipulate private fields of struct */
444 int get_union_n_members (type *uni);
445 void add_union_member (type *uni, entity *member);
446 entity *get_union_member (type *uni, int pos);
447 void set_union_member (type *uni, int pos, entity *member);
448 /* Finds member in the list of members and removes it. */
449 void remove_union_member (type *uni, entity *member);
452 bool is_union_type (type *uni);
455 * Representation of an array type.
456 * The array type represents rectangular multi dimensional arrays.
457 * The constants representing the bounds must be allocated to
458 * get_const_code_irg() by setting current_ir_graph accordingly.
459 * @param n_dimensions Number of array dimensions.
460 * @param *lower_bound Lower bounds of dimensions. Usually all 0.
461 * @param *upper_bound Upper bounds or dimensions.
462 * @param *element_type The type of the array elements.
463 * @param *element_ent An entity for the array elements to be used for
464 * element selection with Sel.
465 * @@@ Do we need several entities? One might want
466 * to select a dimension and not a single element in
467 * case of multidim arrays.
469 /* create a new type array --
470 Sets n_dimension to dimension and all dimension entries to NULL.
471 Initializes order to the order of the dimensions.
472 The entity for array elements is built automatically.
473 Set dimension sizes after call to constructor with set_* routines. */
474 type *new_type_array (ident *name, int n_dimensions,
476 type *new_d_type_array (ident *name, int n_dimensions,
477 type *element_type, dbg_info* db);
479 /* manipulate private fields of array type */
480 int get_array_n_dimensions (type *array);
481 /* Allocates Const nodes of mode_I for the array dimensions */
482 void set_array_bounds_int (type *array, int dimension, int lower_bound,
484 void set_array_bounds (type *array, int dimension, ir_node *lower_bound,
485 ir_node *upper_bound);
486 void set_array_lower_bound (type *array, int dimension, ir_node *lower_bound);
487 void set_array_lower_bound_int (type *array, int dimension, int lower_bound);
488 void set_array_upper_bound (type *array, int dimension, ir_node *upper_bound);
489 void set_array_upper_bound_int (type *array, int dimension, int lower_bound);
490 ir_node * get_array_lower_bound (type *array, int dimension);
491 ir_node * get_array_upper_bound (type *array, int dimension);
493 void set_array_order (type *array, int dimension, int order);
494 int get_array_order (type *array, int dimension);
496 void set_array_element_type (type *array, type *tp);
497 type *get_array_element_type (type *array);
499 void set_array_element_entity (type *array, entity *ent);
500 entity *get_array_element_entity (type *array);
503 bool is_array_type (type *array);
506 * Representation of an enumeration type.
507 * Enumeration types need not necessarily be represented explicitly
508 * by Firm types, as the frontend can lower them to integer constants as
509 * well. For debugging purposes or similar tasks this information is useful.
510 * @param *enum The target values representing the constants used to
511 * represent individual enumerations.
512 * @param *enum_nameid Idents containing the source program name of the enumeration
516 /* create a new type enumeration -- set the enumerators independently */
517 type *new_type_enumeration (ident *name, int n_enums);
518 type *new_d_type_enumeration (ident *name, int n_enums, dbg_info* db);
520 /* manipulate fields of enumeration type. */
521 int get_enumeration_n_enums (type *enumeration);
523 void set_enumeration_enum (type *enumeration, int pos, tarval *con);
524 tarval *get_enumeration_enum (type *enumeration, int pos);
526 void set_enumeration_nameid (type *enumeration, int pos, ident *id);
527 ident *get_enumeration_nameid (type *enumeration, int pos);
528 const char *get_enumeration_name(type *enumeration, int pos);
531 bool is_enumeration_type (type *enumeration);
534 * Representation of a pointer type.
536 * @param points_to The type of the entity this pointer points to.
538 /* Create a new type pointer */
539 type *new_type_pointer (ident *name, type *points_to);
540 type *new_d_type_pointer (ident *name, type *points_to, dbg_info* db);
542 /* manipulate fields of type_pointer */
543 void set_pointer_points_to_type (type *pointer, type *tp);
544 type *get_pointer_points_to_type (type *pointer);
547 bool is_pointer_type (type *pointer);
550 * Representation of a primitive type.
551 * Primitive types are types that represent indivisible data values that
552 * map directly to modes. They don't have a private attribute. The
553 * important information they carry is held in the common mode field.
555 /* create a new type primitive */
556 type *new_type_primitive (ident *name, ir_mode *mode);
557 type *new_d_type_primitive (ident *name, ir_mode *mode, dbg_info* db);
560 bool is_primitive_type (type *primitive);
566 * Checks whether a type is atomic.
567 * @param tp - any type
568 * @return true if type is primitive, pointer or enumeration
571 int is_atomic_type(type *tp);
575 * Checks whether a type is compound.
576 * @param tp - any type
577 * @return true if the type is class, structure, union or array type.
580 int is_compound_type(type *tp);
582 # endif /* _TYPE_H_ */