2 * Copyright (C) 1995-2011 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
24 * @see type.h tpop_t.h tpop.h
26 #ifndef FIRM_TR_TYPE_T_H
27 #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. */
68 /** Method type attributes. */
70 size_t n_params; /**< Number of parameters. */
71 tp_ent_pair *params; /**< Array of parameter type/value entities pairs. */
72 size_t n_res; /**< Number of results. */
73 tp_ent_pair *res_type; /**< Array of result type/value ir_entity pairs. */
74 ir_variadicity variadicity; /**< The variadicity of the method. */
75 mtp_additional_properties additional_properties; /**< Set of additional method properties. */
76 unsigned irg_calling_conv; /**< A set of calling convention flags. */
79 /** Union type attributes. */
81 ir_entity **members; /**< Fields of this union. No method entities allowed. */
84 /** Array type attributes. */
86 size_t n_dimensions; /**< Number of array dimensions. */
87 ir_node **lower_bound; /**< Lower bounds of dimensions. Usually all 0. */
88 ir_node **upper_bound; /**< Upper bounds or dimensions. */
89 size_t *order; /**< Ordering of dimensions. */
90 ir_type *element_type; /**< The type of the array elements. */
91 ir_entity *element_ent; /**< entity for the array elements, to be used for
92 element selection with a Sel node. */
95 /** An enumerator constant. */
96 struct ir_enum_const {
97 ir_tarval *value; /**< The constants that represents this enumerator identifier. */
98 ident *nameid; /**< The name of the enumerator identifier. */
99 ir_type *owner; /**< owner type of this enumerator constant. */
102 /** Enum type attributes. */
104 ir_enum_const *enumer; /**< Contains all enumerator constants that represent a member
105 of the enum -- enumerators. */
108 /** Pointer type attributes. */
110 ir_type *points_to; /**< The type of the ir_entity the pointer points to. */
113 /** Primitive type attributes. */
115 ir_type *base_type; /**< For bitfield types: The base primitive type, NULL else. */
118 /** General type attributes. */
120 cls_attr ca; /**< Attributes of a class type */
121 stc_attr sa; /**< Attributes of a struct type */
122 mtd_attr ma; /**< Attributes of a method type */
123 uni_attr ua; /**< Attributes of an union type */
124 arr_attr aa; /**< Attributes of an array type */
125 enm_attr ea; /**< Attributes of an enumeration type */
126 ptr_attr pa; /**< Attributes of a pointer type */
127 pri_attr ba; /**< Attributes of a primitive bitfield type */
130 /** Additional type flags. */
132 tf_none = 0, /**< No flags. */
133 tf_lowered_type = 1U << 0, /**< Set if this is a lowered type. */
134 tf_layout_fixed = 1U << 1, /**< Set if the layout of a type is fixed */
136 tf_frame_type = 1U << 2, /**< Set if this is a frame type. */
137 tf_segment = 1U << 3, /**< type represents a linker segment */
138 tf_global_type = 1U << 4, /**< Set only for the global type */
139 tf_tls_type = 1U << 5, /**< Set only for the tls type */
140 tf_constructors = 1U << 6, /**< Set only for the constructors segment type */
141 tf_destructors = 1U << 7, /**< Set only for the destructors segment type */
143 ENUM_BITSET(type_flags)
146 * An abstract data type to represent types.
148 * This is the abstract data type with which any type known in the
149 * compiled program can be represented. This includes types specified
150 * in the program as well as types defined by the language. In the
151 * view of the intermediate representation there is no difference
152 * between these types.
154 * There exist several kinds of types, arranged by the structure of
155 * the type. These are distinguished by a type opcode.
156 * A type is described by a set of attributes. Some of these attributes
157 * are common to all types, others depend on the kind of the type.
160 firm_kind kind; /**< the firm kind, must be k_type */
161 const tp_op *type_op; /**< the type operation of the type */
162 ident *name; /**< The name of the type */
163 ir_visibility visibility;/**< Visibility of entities of this type. */
164 unsigned flags; /**< Type flags, a bitmask of enum type_flags. */
165 unsigned size; /**< Size of an ir_entity of this type. This is
166 determined when fixing the layout of this
167 class. Size must be given in bytes. */
168 unsigned align; /**< Alignment of an ir_entity of this type. This
169 should be set according to the source
170 language needs. If not set, it's calculated
171 automatically by get_type_alignment().
172 Alignment must be given in bytes. */
173 ir_mode *mode; /**< The mode for atomic types */
174 ir_visited_t visit; /**< visited counter for walks of the type information */
175 void *link; /**< holds temporary data - like in irnode_t.h */
176 type_dbg_info *dbi; /**< A pointer to information for debug support. */
177 ir_type *higher_type; /**< link to highlevel type in case of lowered
181 long nr; /**< An unique node number for each node to make
184 tp_attr attr; /**< Type kind specific fields. This must be the
185 last entry in this struct! Varying size! */
189 * Creates a new type representation:
191 * @param type_op the kind of this type. May not be type_id.
192 * @param mode the mode to be used for this type, may be NULL
193 * @param db debug info
195 * @return A new type of the given type. The remaining private attributes are not
196 * initialized. The type is in state layout_undefined.
198 ir_type *new_type(const tp_op *type_op, ir_mode *mode, type_dbg_info *db);
200 void free_type_entities(ir_type *tp);
202 void free_class_entities (ir_type *clss);
203 void free_struct_entities (ir_type *strct);
204 void free_method_entities (ir_type *method);
205 void free_union_entities (ir_type *uni);
206 void free_array_entities (ir_type *array);
207 void free_enumeration_entities(ir_type *enumeration);
208 void free_pointer_entities (ir_type *pointer);
210 void free_array_automatic_entities(ir_type *array);
212 void free_class_attrs (ir_type *clss);
213 void free_struct_attrs (ir_type *strct);
214 void free_method_attrs (ir_type *method);
215 void free_union_attrs (ir_type *uni);
216 void free_array_attrs (ir_type *array);
217 void free_enumeration_attrs(ir_type *enumeration);
218 void free_pointer_attrs (ir_type *pointer);
220 void set_class_mode(ir_type *tp, ir_mode *mode);
221 void set_struct_mode(ir_type *tp, ir_mode *mode);
222 void set_pointer_mode(ir_type *tp, ir_mode *mode);
223 void set_primitive_mode(ir_type *tp, ir_mode *mode);
224 void set_enumeration_mode(ir_type *tp, ir_mode *mode);
226 void set_class_size(ir_type *tp, unsigned bytes);
227 void set_struct_size(ir_type *tp, unsigned bytes);
228 void set_union_size(ir_type *tp, unsigned bytes);
229 void set_array_size(ir_type *tp, unsigned bytes);
230 void set_default_size(ir_type *tp, unsigned bytes);
232 /** Set and get a class' dfn --
233 * This is an undocumented field, subject to change! */
234 void set_class_dfn(ir_type *clss, int dfn);
235 int get_class_dfn(const ir_type *clss);
237 void add_compound_member(ir_type *compound, ir_entity *entity);
239 /** Initialize the type module. */
240 void ir_init_type(ir_prog *irp);
242 /** free internal datastructures of type module */
243 void ir_finish_type(ir_prog *irp);
245 /** Clone an existing method type.
247 * @param tp the method type to clone.
248 * @param prefix if non-null, will be the prefix for the name of
251 * @return the cloned method type.
253 ir_type *clone_type_method(ir_type *tp);
255 extern ir_visited_t firm_type_visited;
257 static inline void _set_master_type_visited(ir_visited_t val)
259 firm_type_visited = val;
262 static inline ir_visited_t _get_master_type_visited(void)
264 return firm_type_visited;
267 static inline void _inc_master_type_visited(void)
272 static inline int is_lowered_type(const ir_type *tp)
274 return tp->flags & tf_lowered_type;
277 static inline ir_type *get_higher_type(const ir_type *tp)
279 return tp->higher_type;
282 static inline void set_higher_type(ir_type *tp, ir_type *higher_type)
284 tp->flags |= tf_lowered_type;
285 tp->higher_type = higher_type;
288 static inline void *_get_type_link(const ir_type *tp)
290 assert(tp && tp->kind == k_type);
294 static inline void _set_type_link(ir_type *tp, void *l)
296 assert(tp && tp->kind == k_type);
300 static inline const tp_op *_get_type_tpop(const ir_type *tp)
302 assert(tp && tp->kind == k_type);
306 static inline ident *_get_type_tpop_nameid(const ir_type *tp)
308 assert(tp && tp->kind == k_type);
309 return get_tpop_ident(tp->type_op);
312 static inline tp_opcode _get_type_tpop_code(const ir_type *tp)
314 assert(tp && tp->kind == k_type);
315 return get_tpop_code(tp->type_op);
318 static inline ir_mode *_get_type_mode(const ir_type *tp)
320 assert(tp && tp->kind == k_type);
324 static inline unsigned _get_type_size_bytes(const ir_type *tp)
326 assert(tp && tp->kind == k_type);
330 static inline ir_type_state _get_type_state(const ir_type *tp)
332 assert(tp && tp->kind == k_type);
333 return tp->flags & tf_layout_fixed ? layout_fixed : layout_undefined;
336 static inline ir_visited_t _get_type_visited(const ir_type *tp)
338 assert(tp && tp->kind == k_type);
342 static inline void _set_type_visited(ir_type *tp, ir_visited_t num)
344 assert(tp && tp->kind == k_type);
348 static inline void _mark_type_visited(ir_type *tp)
350 assert(tp && tp->kind == k_type);
351 assert(tp->visit < firm_type_visited);
352 tp->visit = firm_type_visited;
355 static inline int _type_visited(const ir_type *tp)
357 assert(tp && tp->kind == k_type);
358 return tp->visit >= firm_type_visited;
361 static inline int _type_not_visited(const ir_type *tp)
363 assert(tp && tp->kind == k_type);
364 return tp->visit < firm_type_visited;
367 static inline type_dbg_info *_get_type_dbg_info(const ir_type *tp)
372 static inline void _set_type_dbg_info(ir_type *tp, type_dbg_info *db)
377 static inline int _is_type(const void *thing)
379 return get_kind(thing) == k_type;
382 static inline int _is_class_type(const ir_type *clss)
384 return clss->type_op == type_class;
387 static inline size_t _get_class_n_members(const ir_type *clss)
389 assert(clss->type_op == type_class);
390 return ARR_LEN(clss->attr.ca.members);
393 static inline ir_entity *_get_class_member(const ir_type *clss, size_t pos)
395 assert(clss->type_op == type_class);
396 assert(pos < _get_class_n_members(clss));
397 return clss->attr.ca.members[pos];
400 static inline unsigned _get_class_vtable_size(const ir_type *clss)
402 assert(clss->type_op == type_class);
403 return clss->attr.ca.vtable_size;
406 static inline void _set_class_vtable_size(ir_type *clss, unsigned vtable_size)
408 assert(clss->type_op == type_class);
409 clss->attr.ca.vtable_size = vtable_size;
412 static inline int _is_class_final(const ir_type *clss)
414 assert(clss->type_op == type_class);
415 return clss->attr.ca.clss_flags & cf_final_class;
418 static inline void _set_class_final(ir_type *clss, int final)
420 assert(clss->type_op == type_class);
422 clss->attr.ca.clss_flags |= cf_final_class;
424 clss->attr.ca.clss_flags &= ~cf_final_class;
427 static inline int _is_class_interface(const ir_type *clss)
429 assert(clss->type_op == type_class);
430 return clss->attr.ca.clss_flags & cf_interface_class;
433 static inline void _set_class_interface(ir_type *clss, int final)
435 assert(clss->type_op == type_class);
437 clss->attr.ca.clss_flags |= cf_interface_class;
439 clss->attr.ca.clss_flags &= ~cf_interface_class;
442 static inline int _is_class_abstract(const ir_type *clss)
444 assert(clss->type_op == type_class);
445 return clss->attr.ca.clss_flags & cf_absctract_class;
448 static inline void _set_class_abstract(ir_type *clss, int final)
450 assert(clss->type_op == type_class);
452 clss->attr.ca.clss_flags |= cf_absctract_class;
454 clss->attr.ca.clss_flags &= ~cf_absctract_class;
457 static inline int _is_struct_type(const ir_type *strct)
459 return (strct->type_op == type_struct);
462 static inline int _is_method_type(const ir_type *method)
464 return (method->type_op == type_method);
467 static inline int _is_union_type(const ir_type *uni)
469 return (uni->type_op == type_union);
472 static inline int _is_array_type(const ir_type *array)
474 return (array->type_op == type_array);
477 static inline int _is_enumeration_type(const ir_type *enumeration)
479 return (enumeration->type_op == type_enumeration);
482 static inline int _is_pointer_type(const ir_type *pointer)
484 return (pointer->type_op == type_pointer);
487 /** Returns true if a type is a primitive type. */
488 static inline int _is_primitive_type(const ir_type *primitive)
490 assert(primitive->kind == k_type);
491 return (primitive->type_op == type_primitive);
494 static inline int _is_atomic_type(const ir_type *tp)
496 assert(tp->kind == k_type);
497 return (_is_primitive_type(tp) || _is_pointer_type(tp) ||
498 _is_enumeration_type(tp));
501 static inline size_t _get_method_n_params(const ir_type *method)
503 assert(method->type_op == type_method);
504 return method->attr.ma.n_params;
507 static inline size_t _get_method_n_ress(const ir_type *method)
509 assert(method->type_op == type_method);
510 return method->attr.ma.n_res;
513 static inline mtp_additional_properties _get_method_additional_properties(const ir_type *method)
515 assert(method->type_op == type_method);
516 return method->attr.ma.additional_properties;
519 static inline void _set_method_additional_properties(ir_type *method, mtp_additional_properties mask)
521 assert(method->type_op == type_method);
523 /* do not allow to set the mtp_property_inherited flag or
524 * the automatic inheritance of flags will not work */
525 method->attr.ma.additional_properties = mask & ~mtp_property_inherited;
528 static inline void _add_method_additional_properties(ir_type *method, mtp_additional_properties flag)
530 assert(method->type_op == type_method);
532 /* do not allow to set the mtp_property_inherited flag or
533 * the automatic inheritance of flags will not work */
534 method->attr.ma.additional_properties |= flag & ~mtp_property_inherited;
537 static inline unsigned _get_method_calling_convention(const ir_type *method)
539 assert(method->type_op == type_method);
540 return method->attr.ma.irg_calling_conv;
543 static inline void _set_method_calling_convention(ir_type *method, unsigned cc_mask)
545 assert(method->type_op == type_method);
546 method->attr.ma.irg_calling_conv = cc_mask;
550 #define set_master_type_visited(val) _set_master_type_visited(val)
551 #define get_master_type_visited() _get_master_type_visited()
552 #define inc_master_type_visited() _inc_master_type_visited()
553 #define get_type_link(tp) _get_type_link(tp)
554 #define set_type_link(tp, l) _set_type_link(tp, l)
555 #define get_type_tpop(tp) _get_type_tpop(tp)
556 #define get_type_tpop_nameid(tp) _get_type_tpop_nameid(tp)
557 #define get_type_tpop_code(tp) _get_type_tpop_code(tp)
558 #define get_type_mode(tp) _get_type_mode(tp)
559 #define get_type_size_bytes(tp) _get_type_size_bytes(tp)
560 #define get_type_state(tp) _get_type_state(tp)
561 #define get_type_visited(tp) _get_type_visited(tp)
562 #define set_type_visited(tp, num) _set_type_visited(tp, num)
563 #define mark_type_visited(tp) _mark_type_visited(tp)
564 #define type_visited(tp) _type_visited(tp)
565 #define type_not_visited(tp) _type_not_visited(tp)
566 #define get_type_dbg_info(tp) _get_type_dbg_info(tp)
567 #define set_type_dbg_info(tp, db) _set_type_dbg_info(tp, db)
568 #define is_type(thing) _is_type(thing)
569 #define is_Class_type(clss) _is_class_type(clss)
570 #define get_class_n_members(clss) _get_class_n_members(clss)
571 #define get_class_member(clss, pos) _get_class_member(clss, pos)
572 #define get_class_vtable_size(clss) _get_class_vtable_size(clss)
573 #define set_class_vtable_size(clss, size) _set_class_vtable_size(clss, size)
574 #define is_class_final(clss) _is_class_final(clss)
575 #define set_class_final(clss, flag) _set_class_final(clss, flag)
576 #define is_class_interface(clss) _is_class_interface(clss)
577 #define set_class_interface(clss, flag) _set_class_interface(clss, flag)
578 #define is_class_abstract(clss) _is_class_abstract(clss)
579 #define set_class_abstract(clss, flag) _set_class_abstract(clss, flag)
580 #define is_Struct_type(strct) _is_struct_type(strct)
581 #define is_Method_type(method) _is_method_type(method)
582 #define is_Union_type(uni) _is_union_type(uni)
583 #define is_Array_type(array) _is_array_type(array)
584 #define is_Enumeration_type(enumeration) _is_enumeration_type(enumeration)
585 #define is_Pointer_type(pointer) _is_pointer_type(pointer)
586 #define is_Primitive_type(primitive) _is_primitive_type(primitive)
587 #define is_atomic_type(tp) _is_atomic_type(tp)
588 #define get_method_n_params(method) _get_method_n_params(method)
589 #define get_method_n_ress(method) _get_method_n_ress(method)
590 #define get_method_additional_properties(method) _get_method_additional_properties(method)
591 #define set_method_additional_properties(method, mask) _set_method_additional_properties(method, mask)
592 #define add_method_additional_properties(method, flag) _add_method_additional_properties(method, flag)
593 #define get_method_calling_convention(method) _get_method_calling_convention(method)
594 #define set_method_calling_convention(method, cc_mask) _set_method_calling_convention(method, cc_mask)
596 #endif /* FIRM_TR_TYPE_T_H */