2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Representation of types -- private header.
23 * @author Goetz Lindenmaier, Michael Beck
25 * @see type.h tpop_t.h tpop.h
27 #ifndef FIRM_TR_TYPE_T_H
28 #define FIRM_TR_TYPE_T_H
33 #include "firm_common.h"
39 cf_none = 0, /**< No flags. */
40 cf_final_class = 1, /**< Set if a class is an final class */
41 cf_interface_class = 2, /**< Set if a class is an "interface" */
42 cf_absctract_class = 4, /**< Set if a class is "abstract" */
45 /** Class type attributes. */
47 ir_entity **members; /**< Array containing the fields and methods of this class. */
48 ir_type **subtypes; /**< Array containing the direct subtypes. */
49 ir_type **supertypes; /**< Array containing the direct supertypes */
50 ir_peculiarity peculiarity; /**< The peculiarity of this class. */
51 ir_entity *type_info; /**< An ir_entity representing this class, used for type info. */
52 int dfn; /**< A number that can be used for 'instanceof' operator. */
53 unsigned vtable_size; /**< The size of the vtable for this class. */
54 unsigned clss_flags; /**< Additional class flags. */
57 /** Struct type attributes. */
59 ir_entity **members; /**< Fields of this struct. No method entities allowed. */
62 /** A (type, ir_entity) pair. */
64 ir_type *tp; /**< A type. */
65 ir_entity *ent; /**< An ir_entity. */
66 ident *param_name; /**< For debugging purposes: the name of the parameter */
69 /** Method type attributes. */
71 int n_params; /**< Number of parameters. */
72 tp_ent_pair *params; /**< Array of parameter type/value entities pairs. */
73 ir_type *value_params; /**< A type whose entities represent copied value arguments. */
74 int n_res; /**< Number of results. */
75 tp_ent_pair *res_type; /**< Array of result type/value ir_entity pairs. */
76 ir_type *value_ress; /**< A type whose entities represent copied value results. */
77 ir_variadicity variadicity; /**< The variadicity of the method. */
78 int first_variadic_param; /**< The index of the first variadic parameter or -1 if non-variadic .*/
79 unsigned additional_properties; /**< Set of additional method properties. */
80 unsigned irg_calling_conv; /**< A set of calling convention flags. */
83 /** Union type attributes. */
85 ir_entity **members; /**< Fields of this union. No method entities allowed. */
88 /** Array type attributes. */
90 int n_dimensions; /**< Number of array dimensions. */
91 ir_node **lower_bound; /**< Lower bounds of dimensions. Usually all 0. */
92 ir_node **upper_bound; /**< Upper bounds or dimensions. */
93 int *order; /**< Ordering of dimensions. */
94 ir_type *element_type; /**< The type of the array elements. */
95 ir_entity *element_ent; /**< entity for the array elements, to be used for
96 element selection with a Sel node. */
99 /** An enumerator constant. */
100 struct ir_enum_const {
101 tarval *value; /**< The constants that represents this enumerator identifier. */
102 ident *nameid; /**< The name of the enumerator identifier. */
103 ir_type *owner; /**< owner type of this enumerator constant. */
106 /** Enum type attributes. */
108 ir_enum_const *enumer; /**< Contains all enumerator constants that represent a member
109 of the enum -- enumerators. */
112 /** Pointer type attributes. */
114 ir_type *points_to; /**< The type of the ir_entity the pointer points to. */
117 /** Primitive type attributes. */
119 ir_type *base_type; /**< For bitfield types: The base primitive type, NULL else. */
124 typedef struct { * No private attr, must be smaller than others! *
128 /** General type attributes. */
130 cls_attr ca; /**< Attributes of a class type */
131 stc_attr sa; /**< Attributes of a struct type */
132 mtd_attr ma; /**< Attributes of a method type */
133 uni_attr ua; /**< Attributes of an union type */
134 arr_attr aa; /**< Attributes of an array type */
135 enm_attr ea; /**< Attributes of an enumeration type */
136 ptr_attr pa; /**< Attributes of a pointer type */
137 pri_attr ba; /**< Attributes of a primitive bitfield type */
140 /** Additional type flags. */
142 tf_none = 0, /**< No flags. */
143 tf_frame_type = 1, /**< Set if this is a frame type. */
144 tf_value_param_type = 2, /**< Set if this is a value param type. */
145 tf_lowered_type = 4, /**< Set if this is a lowered type. */
146 tf_layout_fixed = 8, /**< Set if the layout of a type is fixed */
147 tf_global_type = 16, /**< Set only for the global type */
148 tf_tls_type = 32, /**< Set only for the tls type */
152 * An abstract data type to represent types.
154 * This is the abstract data type with which any type known in the
155 * compiled program can be represented. This includes types specified
156 * in the program as well as types defined by the language. In the
157 * view of the intermediate representation there is no difference
158 * between these types.
160 * There exist several kinds of types, arranged by the structure of
161 * the type. These are distinguished by a type opcode.
162 * A type is described by a set of attributes. Some of these attributes
163 * are common to all types, others depend on the kind of the type.
165 * The following describes the common attributes. They can only be
166 * accessed by the functions given below.
168 * The common fields are:
170 * - firm_kind: A firm_kind tag containing k_type. This is useful
171 * for dynamically checking whether a node is a type node.
172 * - type_op: A tp_op specifying the kind of the type.
173 * - name: An identifier specifying the name of the type. To be
174 * set by the frontend.
175 * - visibility: The visibility of this type.
176 * - size: The size of the type, i.e. an entity of this type will
177 * occupy size bits in memory. In several cases this is
178 * determined when fixing the layout of this type (class,
179 * struct, union, array, enumeration).
180 * - alignment The alignment of the type, i.e. an entity of this type will
181 * be allocated an an address in memory with this alignment.
182 * In several cases this is determined when fixing the layout
183 * of this type (class, struct, union, array)
184 * - mode: The mode to be used to represent the type on a machine.
185 * - state: The state of the type. The state represents whether the
186 * layout of the type is undefined or fixed (values: layout_undefined
187 * or layout_fixed). Compound types can have an undefined
188 * layout. The layout of the basic types primitive and pointer
189 * is always layout_fixed. If the layout of
190 * compound types is fixed all entities must have an offset
191 * and the size of the type must be set.
192 * A fixed layout for enumeration types means that each enumeration
193 * is associated with an implementation value.
194 * - assoc_type: The associated lowered/upper type.
195 * - visit: A counter for walks of the type information.
196 * - link: A void* to associate some additional information with the type.
198 * These fields can only be accessed via access functions.
200 * Depending on the value of @c type_op, i.e., depending on the kind of the
201 * type the adt contains further attributes. These are documented below.
205 * @link class_type class @endlink, @link struct_type struct @endlink,
206 * @link method_type method @endlink, @link union_type union @endlink,
207 * @link array_type array @endlink, @link enumeration_type enumeration @endlink,
208 * @link pointer_type pointer @endlink, @link primitive_type primitive @endlink
211 * mode maybe not global field??
214 firm_kind kind; /**< the firm kind, must be k_type */
215 const tp_op *type_op; /**< the type operation of the type */
216 ident *name; /**< The name of the type */
217 ir_visibility visibility;/**< Visibility of entities of this type. */
218 unsigned flags; /**< Type flags, a bitmask of enum type_flags. */
219 unsigned size; /**< Size of an ir_entity of this type. This is determined
220 when fixing the layout of this class. Size must be
222 unsigned align; /**< Alignment of an ir_entity of this type. This should be
223 set according to the source language needs. If not set it's
224 calculated automatically by get_type_alignment().
225 Alignment must be given in bytes. */
226 ir_mode *mode; /**< The mode for atomic types */
227 ir_visited_t visit; /**< visited counter for walks of the type information */
228 void *link; /**< holds temporary data - like in irnode_t.h */
229 struct dbg_info *dbi; /**< A pointer to information for debug support. */
230 ir_type *assoc_type; /**< The associated lowered/unlowered type */
232 /* ------------- fields for analyses ---------------*/
235 long nr; /**< An unique node number for each node to make output
238 tp_attr attr; /**< Type kind specific fields. This must be the last
239 entry in this struct! Varying size! */
243 * Creates a new type representation:
245 * @param type_op the kind of this type. May not be type_id.
246 * @param mode the mode to be used for this type, may be NULL
247 * @param name an ident for the name of this type.
248 * @param db debug info
250 * @return A new type of the given type. The remaining private attributes are not
251 * initialized. The type is in state layout_undefined.
254 new_type(tp_op *type_op, ir_mode *mode, ident *name, dbg_info *db);
255 void free_type_attrs (ir_type *tp);
257 void free_class_entities (ir_type *clss);
258 void free_struct_entities (ir_type *strct);
259 void free_method_entities (ir_type *method);
260 void free_union_entities (ir_type *uni);
261 void free_array_entities (ir_type *array);
262 void free_enumeration_entities(ir_type *enumeration);
263 void free_pointer_entities (ir_type *pointer);
265 void free_array_automatic_entities(ir_type *array);
267 void free_class_attrs (ir_type *clss);
268 void free_struct_attrs (ir_type *strct);
269 void free_method_attrs (ir_type *method);
270 void free_union_attrs (ir_type *uni);
271 void free_array_attrs (ir_type *array);
272 void free_enumeration_attrs(ir_type *enumeration);
273 void free_pointer_attrs (ir_type *pointer);
275 void set_class_mode(ir_type *tp, ir_mode *mode);
276 void set_struct_mode(ir_type *tp, ir_mode *mode);
277 void set_pointer_mode(ir_type *tp, ir_mode *mode);
278 void set_primitive_mode(ir_type *tp, ir_mode *mode);
279 void set_enumeration_mode(ir_type *tp, ir_mode *mode);
281 void set_class_size(ir_type *tp, unsigned bytes);
282 void set_struct_size(ir_type *tp, unsigned bytes);
283 void set_union_size(ir_type *tp, unsigned bytes);
284 void set_array_size(ir_type *tp, unsigned bytes);
285 void set_default_size(ir_type *tp, unsigned bytes);
288 * Initialize the type module.
290 * @param builtin_db debug info for built-in objects
291 * @param default_cc_mask default calling conventions for methods
293 void firm_init_type(dbg_info *builtin_db, unsigned default_cc_mask);
296 /* ------------------- *
298 * ------------------- */
300 extern ir_visited_t firm_type_visited;
302 static inline void _set_master_type_visited(ir_visited_t val) { firm_type_visited = val; }
303 static inline ir_visited_t _get_master_type_visited(void) { return firm_type_visited; }
304 static inline void _inc_master_type_visited(void) { ++firm_type_visited; }
307 _get_type_link(const ir_type *tp) {
308 assert(tp && tp->kind == k_type);
313 _set_type_link(ir_type *tp, void *l) {
314 assert(tp && tp->kind == k_type);
318 static inline const tp_op*
319 _get_type_tpop(const ir_type *tp) {
320 assert(tp && tp->kind == k_type);
325 _get_type_tpop_nameid(const ir_type *tp) {
326 assert(tp && tp->kind == k_type);
327 return get_tpop_ident(tp->type_op);
330 static inline tp_opcode
331 _get_type_tpop_code(const ir_type *tp) {
332 assert(tp && tp->kind == k_type);
333 return get_tpop_code(tp->type_op);
336 static inline ir_mode *
337 _get_type_mode(const ir_type *tp) {
338 assert(tp && tp->kind == k_type);
342 static inline ident *
343 _get_type_ident(const ir_type *tp) {
344 assert(tp && tp->kind == k_type);
349 _set_type_ident(ir_type *tp, ident* id) {
350 assert(tp && tp->kind == k_type);
354 static inline unsigned
355 _get_type_size_bytes(const ir_type *tp) {
356 assert(tp && tp->kind == k_type);
360 static inline ir_type_state
361 _get_type_state(const ir_type *tp) {
362 assert(tp && tp->kind == k_type);
363 return tp->flags & tf_layout_fixed ? layout_fixed : layout_undefined;
366 static inline ir_visited_t
367 _get_type_visited(const ir_type *tp) {
368 assert(tp && tp->kind == k_type);
373 _set_type_visited(ir_type *tp, ir_visited_t num) {
374 assert(tp && tp->kind == k_type);
379 _mark_type_visited(ir_type *tp) {
380 assert(tp && tp->kind == k_type);
381 assert(tp->visit < firm_type_visited);
382 tp->visit = firm_type_visited;
386 _type_visited(const ir_type *tp) {
387 assert(tp && tp->kind == k_type);
388 return tp->visit >= firm_type_visited;
392 _type_not_visited(const ir_type *tp) {
393 assert(tp && tp->kind == k_type);
394 return tp->visit < firm_type_visited;
397 static inline dbg_info *
398 _get_type_dbg_info(const ir_type *tp) {
403 _set_type_dbg_info(ir_type *tp, dbg_info *db) {
408 _is_type(const void *thing) {
409 return (get_kind(thing) == k_type);
413 _is_class_type(const ir_type *clss) {
415 return (clss->type_op == type_class);
419 _get_class_n_members (const ir_type *clss) {
420 assert(clss && (clss->type_op == type_class));
421 return (ARR_LEN (clss->attr.ca.members));
424 static inline ir_entity *
425 _get_class_member (const ir_type *clss, int pos) {
426 assert(clss && (clss->type_op == type_class));
427 assert(pos >= 0 && pos < _get_class_n_members(clss));
428 return clss->attr.ca.members[pos];
431 static inline unsigned
432 _get_class_vtable_size(const ir_type *clss) {
433 assert(clss && (clss->type_op == type_class));
434 return clss->attr.ca.vtable_size;
438 _set_class_vtable_size(ir_type *clss, unsigned vtable_size) {
439 assert(clss && (clss->type_op == type_class));
440 clss->attr.ca.vtable_size = vtable_size;
444 _is_class_final(const ir_type *clss) {
445 assert(clss && (clss->type_op == type_class));
446 return clss->attr.ca.clss_flags & cf_final_class;
450 _set_class_final(ir_type *clss, int final) {
451 assert(clss && (clss->type_op == type_class));
453 clss->attr.ca.clss_flags |= cf_final_class;
455 clss->attr.ca.clss_flags &= ~cf_final_class;
459 _is_class_interface(const ir_type *clss) {
460 assert(clss && (clss->type_op == type_class));
461 return clss->attr.ca.clss_flags & cf_interface_class;
465 _set_class_interface(ir_type *clss, int final) {
466 assert(clss && (clss->type_op == type_class));
468 clss->attr.ca.clss_flags |= cf_interface_class;
470 clss->attr.ca.clss_flags &= ~cf_interface_class;
474 _is_class_abstract(const ir_type *clss) {
475 assert(clss && (clss->type_op == type_class));
476 return clss->attr.ca.clss_flags & cf_absctract_class;
480 _set_class_abstract(ir_type *clss, int final) {
481 assert(clss && (clss->type_op == type_class));
483 clss->attr.ca.clss_flags |= cf_absctract_class;
485 clss->attr.ca.clss_flags &= ~cf_absctract_class;
489 _is_struct_type(const ir_type *strct) {
491 return (strct->type_op == type_struct);
495 _is_method_type(const ir_type *method) {
497 return (method->type_op == type_method);
501 _is_union_type(const ir_type *uni) {
503 return (uni->type_op == type_union);
507 _is_array_type(const ir_type *array) {
509 return (array->type_op == type_array);
513 _is_enumeration_type(const ir_type *enumeration) {
515 return (enumeration->type_op == type_enumeration);
519 _is_pointer_type(const ir_type *pointer) {
521 return (pointer->type_op == type_pointer);
524 /** Returns true if a type is a primitive type. */
526 _is_primitive_type(const ir_type *primitive) {
527 assert(primitive && primitive->kind == k_type);
528 return (primitive->type_op == type_primitive);
532 _is_atomic_type(const ir_type *tp) {
533 assert(tp && tp->kind == k_type);
534 return (_is_primitive_type(tp) || _is_pointer_type(tp) ||
535 _is_enumeration_type(tp));
539 _get_method_n_params(const ir_type *method) {
540 assert(method && (method->type_op == type_method));
541 return method->attr.ma.n_params;
545 _get_method_n_ress(const ir_type *method) {
546 assert(method && (method->type_op == type_method));
547 return method->attr.ma.n_res;
550 static inline unsigned
551 _get_method_additional_properties(const ir_type *method) {
552 assert(method && (method->type_op == type_method));
553 return method->attr.ma.additional_properties;
557 _set_method_additional_properties(ir_type *method, unsigned mask) {
558 assert(method && (method->type_op == type_method));
560 /* do not allow to set the mtp_property_inherited flag or
561 * the automatic inheritance of flags will not work */
562 method->attr.ma.additional_properties = mask & ~mtp_property_inherited;
566 _set_method_additional_property(ir_type *method, mtp_additional_property flag) {
567 assert(method && (method->type_op == type_method));
569 /* do not allow to set the mtp_property_inherited flag or
570 * the automatic inheritance of flags will not work */
571 method->attr.ma.additional_properties |= flag & ~mtp_property_inherited;
574 static inline unsigned
575 _get_method_calling_convention(const ir_type *method) {
576 assert(method && (method->type_op == type_method));
577 return method->attr.ma.irg_calling_conv;
581 _set_method_calling_convention(ir_type *method, unsigned cc_mask) {
582 assert(method && (method->type_op == type_method));
583 method->attr.ma.irg_calling_conv = cc_mask;
587 #define set_master_type_visited(val) _set_master_type_visited(val)
588 #define get_master_type_visited() _get_master_type_visited()
589 #define inc_master_type_visited() _inc_master_type_visited()
590 #define get_type_link(tp) _get_type_link(tp)
591 #define set_type_link(tp, l) _set_type_link(tp, l)
592 #define get_type_tpop(tp) _get_type_tpop(tp)
593 #define get_type_tpop_nameid(tp) _get_type_tpop_nameid(tp)
594 #define get_type_tpop_code(tp) _get_type_tpop_code(tp)
595 #define get_type_mode(tp) _get_type_mode(tp)
596 #define get_type_ident(tp) _get_type_ident(tp)
597 #define set_type_ident(tp, id) _set_type_ident(tp, id)
598 #define get_type_size_bytes(tp) _get_type_size_bytes(tp)
599 #define get_type_state(tp) _get_type_state(tp)
600 #define get_type_visited(tp) _get_type_visited(tp)
601 #define set_type_visited(tp, num) _set_type_visited(tp, num)
602 #define mark_type_visited(tp) _mark_type_visited(tp)
603 #define type_visited(tp) _type_visited(tp)
604 #define type_not_visited(tp) _type_not_visited(tp)
605 #define get_type_dbg_info(tp) _get_type_dbg_info(tp)
606 #define set_type_dbg_info(tp, db) _set_type_dbg_info(tp, db)
607 #define is_type(thing) _is_type(thing)
608 #define is_Class_type(clss) _is_class_type(clss)
609 #define get_class_n_members(clss) _get_class_n_members(clss)
610 #define get_class_member(clss, pos) _get_class_member(clss, pos)
611 #define get_class_vtable_size(clss) _get_class_vtable_size(clss)
612 #define set_class_vtable_size(clss, size) _set_class_vtable_size(clss, size)
613 #define is_class_final(clss) _is_class_final(clss)
614 #define set_class_final(clss, flag) _set_class_final(clss, flag)
615 #define is_class_interface(clss) _is_class_interface(clss)
616 #define set_class_interface(clss, flag) _set_class_interface(clss, flag)
617 #define is_class_abstract(clss) _is_class_abstract(clss)
618 #define set_class_abstract(clss, flag) _set_class_abstract(clss, flag)
619 #define is_Struct_type(strct) _is_struct_type(strct)
620 #define is_Method_type(method) _is_method_type(method)
621 #define is_Union_type(uni) _is_union_type(uni)
622 #define is_Array_type(array) _is_array_type(array)
623 #define is_Enumeration_type(enumeration) _is_enumeration_type(enumeration)
624 #define is_Pointer_type(pointer) _is_pointer_type(pointer)
625 #define is_Primitive_type(primitive) _is_primitive_type(primitive)
626 #define is_atomic_type(tp) _is_atomic_type(tp)
627 #define get_method_n_params(method) _get_method_n_params(method)
628 #define get_method_n_ress(method) _get_method_n_ress(method)
629 #define get_method_additional_properties(method) _get_method_additional_properties(method)
630 #define set_method_additional_properties(method, mask) _set_method_additional_properties(method, mask)
631 #define set_method_additional_property(method, flag) _set_method_additional_property(method, flag)
632 #define get_method_calling_convention(method) _get_method_calling_convention(method)
633 #define set_method_calling_convention(method, cc_mask) _set_method_calling_convention(method, cc_mask)
635 #endif /* FIRM_TR_TYPE_T_H */