- add support for statistics and merge debug info
[libfirm] / include / libfirm / typerep.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19 #ifndef FIRM_TYPEREP_H
20 #define FIRM_TYPEREP_H
21
22 #include "firm_types.h"
23
24 /**
25  * @page entity       Entity representation
26  *
27  * An entity is the representation of program known objects in Firm.
28  * The primary concept of entities is to represent members of complex
29  * types, i.e., fields and methods of classes.  As not all programming
30  * language model all variables and methods as members of some class,
31  * the concept of entities is extended to cover also local and global
32  * variables, and arbitrary procedures.
33  *
34  * An entity always specifies the type of the object it represents and
35  * the type of the object it is a part of, the owner of the entity.
36  * Originally this is the type of the class of which the entity is a
37  * member.
38  * The owner of local variables is the procedure they are defined in.
39  * The owner of global variables and procedures visible in the whole
40  * program is a universally defined class type "GlobalType".  The owner
41  * of procedures defined in the scope of an other procedure is the
42  * enclosing procedure.
43  *
44  * The type ir_entity is an abstract data type to represent program entities.
45  * If contains the following attributes:
46  *
47  *   - owner:      A compound type this entity is a part of.
48  *   - type:       The type of this entity.
49  *   - name:       The string that represents this entity in the source program.
50  *   - allocation: A flag saying whether the entity is dynamically or statically
51  *                 allocated (values: dynamic_allocated,  static_allocated,
52  *                 automatic_allocated).
53  *   - visibility: A flag indicating the visibility of this entity (values: local,
54  *                 external_visible,  external_allocated)
55  *   - variability: A flag indicating the variability of this entity (values:
56  *                  uninitialized, initialized, part_constant, constant)
57  *   - volatility: @@@
58  *   - offset:     The offset of the entity within the compound object in bytes.  Only set
59  *                 if the owner in the state "layout_fixed".
60  *   - offset_bits_remainder:   The offset bit remainder of a bitfield entity (in a compound)
61  *                 in bits.  Only set if the owner in the state "layout_fixed".
62  *   - overwrites: A list of entities overwritten by this entity.  This list is only
63  *                 existent if the owner of this entity is a class.  The members in
64  *                 this list must be entities of super classes.
65  *   - overwrittenby: A list of entities that overwrite this entity.  This list is only
66  *                 existent if the owner of this entity is a class.  The members in
67  *                 this list must be entities of sub classes.
68  *   - link:       A void* to associate some additional information with the entity.
69  *   - irg:        If the entity is a method this is the ir graph that represents the
70  *                 code of the method.
71  *   - peculiarity: The peculiarity of the entity.  If the entity is a method this
72  *                 indicates whether the entity represents
73  *                 a real method or whether it only exists to describe an interface.
74  *                 In that case there nowhere exists code for this entity and this entity
75  *                 is never dynamically used in the code.
76  *                 Values: description, existent.  Default: existent.
77  *   - visited:    visited flag.  Master flag is type_visited.
78  *
79  * These fields can only be accessed via access functions.
80  *
81  * @see  ir_type, ir_entity
82  */
83
84 /** This enumeration flags the visibility of entities and types.
85  *
86  * This is necessary for partial compilation.
87  * We rely on the ordering of the flags.
88  */
89 typedef enum {
90         visibility_local,              /**< The entity is only visible locally.  This is the default for
91                                             entities.
92                                             The type is only visible locally.  All instances are allocated
93                                             locally, and no pointer to entities of this type are passed
94                                             out of this compilation unit. */
95         visibility_external_visible,   /**< The entity is visible to other external program parts, but
96                                             it is defined here.  It may not be optimized away.  The entity must
97                                             be static_allocated.
98                                             For types:  entities of this type can be accessed externally.  No
99                                             instances of this type are allocated externally.  */
100         visibility_external_allocated  /**< The entity is defined and allocated externally.  This compilation
101                                             must not allocate memory for this entity. The entity must
102                                             be static_allocated.  This can also be an external defined
103                                             method.
104                                             For types:  entities of this type are allocated and accessed from
105                                             external code.  Default for types.  */
106 } ir_visibility;
107
108 /** This enumeration flags the peculiarity of entities and types. */
109 typedef enum {
110         peculiarity_description,     /**< Represents only a description.  The entity/type is never
111                                   allocated, no code/data exists for this entity/type.
112                               @@@ eventually rename to descriptive (adjective as the others!)*/
113         peculiarity_inherited,       /**< Describes explicitly that other entities are
114                                   inherited to the owner of this entity.
115                                   Overwrites must refer to at least one other
116                                   entity.  If this is a method entity there exists
117                                   no irg for this entity, only for one of the
118                                   overwritten ones.
119                               Only for entity. */
120         peculiarity_existent         /**< The entity/type (can) exist.
121                           @@@ eventually rename to 'real' i.e., 'echt'
122                               This serves better as opposition to description _and_ inherited.*/
123 } ir_peculiarity;
124
125 /**
126  * Creates a new entity.
127  *
128  * Automatically inserts the entity as a member of owner.
129  * Entity is automatic_allocated and uninitialized except if the type
130  * is type_method, then it is static_allocated and constant.  The constant
131  * value is a pointer to the method.
132  * Visibility is local, offset -1, and it is not volatile.
133  */
134 ir_entity *new_entity(ir_type *owner, ident *name, ir_type *tp);
135
136 /**
137  * Creates a new entity.
138  *
139  * Automatically inserts the entity as a member of owner.
140  * The entity is automatic allocated and uninitialized except if the type
141  * is type_method, then it is static allocated and constant.  The constant
142  * value is a pointer to the method.
143  * Visibility is local, offset -1, and it is not volatile.
144  */
145 ir_entity *new_d_entity(ir_type *owner, ident *name, ir_type *tp, dbg_info *db);
146
147 /**
148  * Copies the entity if the new_owner is different from the
149  * owner of the old entity,  else returns the old entity.
150  *
151  * Automatically inserts the new entity as a member of owner.
152  * Resets the overwrites/overwritten_by fields.
153  * Keeps the old atomic value.
154  *   @@@ Maybe we should change this.  If peculiarity of a method
155  *       is existent, we should add a new SymConst that points to
156  *       itself and not to the origin.  Right now we have to change
157  *       the peculiarity and then set a new atomic value by hand.
158  */
159 ir_entity *copy_entity_own(ir_entity *old, ir_type *new_owner);
160
161 /**
162  * Copies the entity if the new_name is different from the
163  * name of the old entity, else returns the old entity.
164  *
165  * Automatically inserts the new entity as a member of owner.
166  * The mangled name ld_name is set to NULL.
167  * Overwrites relation is copied from old.
168  */
169 ir_entity *copy_entity_name(ir_entity *old, ident *new_name);
170
171 /**
172  * Frees the entity.
173  *
174  * The owner will still contain the pointer to this
175  * entity, as well as all other references!
176  */
177 void free_entity(ir_entity *ent);
178
179 /** Returns the name of an entity. */
180 const char *get_entity_name(const ir_entity *ent);
181
182 /** Returns the ident of an entity. */
183 ident *get_entity_ident(const ir_entity *ent);
184
185 /** Sets the ident of the entity. */
186 void set_entity_ident(ir_entity *ent, ident *id);
187
188 /** Returns the mangled name of the entity.
189  *
190  * If the mangled name is set it returns the existing name.
191  * Else it generates a name with mangle_entity()
192  * and remembers this new name internally.
193  */
194 ident *get_entity_ld_ident(ir_entity *ent);
195
196 /** Sets the mangled name of the entity. */
197 void set_entity_ld_ident(ir_entity *ent, ident *ld_ident);
198
199 /** Returns the mangled name of the entity as a string. */
200 const char *get_entity_ld_name(ir_entity *ent);
201
202 /** Returns the owner of the entity. */
203 ir_type *get_entity_owner(ir_entity *ent);
204
205 /** Sets the owner field in entity to owner.  Don't forget to add
206    ent to owner!! */
207 void set_entity_owner(ir_entity *ent, ir_type *owner);
208
209 /** Returns the type of an entity. */
210 ir_type *get_entity_type(ir_entity *ent);
211
212 /** Sets the type of an entity. */
213 void set_entity_type(ir_entity *ent, ir_type *tp);
214
215 /** The allocation type. */
216 typedef enum {
217         allocation_automatic, /**< The entity is allocated during runtime, implicitly
218                                    as component of a compound type.   This is the default. */
219         allocation_parameter, /**< The entity is a parameter.  It is also automatic allocated.
220                                    We distinguish the allocation of parameters from the allocation
221                                    of local variables as their placement depends on the calling
222                                    conventions. */
223         allocation_dynamic,   /**< The entity is allocated during runtime, explicitly
224                                    by an Alloc node. */
225         allocation_static     /**< The entity is allocated statically.  We can use a
226                                    Const as address of the entity.  This is the default for methods. */
227 } ir_allocation;
228
229 /** Returns the allocation type of an entity. */
230 ir_allocation get_entity_allocation(const ir_entity *ent);
231
232 /** Sets the allocation type of an entity. */
233 void set_entity_allocation(ir_entity *ent, ir_allocation al);
234
235 /** Return the name of the allocation type. */
236 const char *get_allocation_name(ir_allocation vis);
237
238 /** Returns the visibility of an entity. */
239 ir_visibility get_entity_visibility(const ir_entity *ent);
240
241 /** Sets the visibility of an entity. */
242 void set_entity_visibility(ir_entity *ent, ir_visibility vis);
243
244 /** Return the name of the visibility */
245 const char *get_visibility_name(ir_visibility vis);
246
247 /** This enumeration flags the variability of entities. */
248 typedef enum {
249         variability_uninitialized,    /**< The content of the entity is completely unknown. Default. */
250         variability_initialized,      /**< After allocation the entity is initialized with the
251                                            value given somewhere in the entity. */
252         variability_part_constant,    /**< For entities of compound types.
253                                            The members of the entity are mixed constant,
254                                            initialized or uninitialized. */
255         variability_constant          /**< The entity is constant. */
256 } ir_variability;
257
258 /** Returns the variability of an entity. */
259 ir_variability get_entity_variability(const ir_entity *ent);
260
261 /** Sets the variability of an entity. */
262 void set_entity_variability(ir_entity *ent, ir_variability var);
263
264 /** Return the name of the variability. */
265 const char *get_variability_name(ir_variability var);
266
267 /** This enumeration flags the volatility of entities and Loads/Stores. */
268 typedef enum {
269         volatility_non_volatile,    /**< The entity is not volatile. Default. */
270         volatility_is_volatile      /**< The entity is volatile. */
271 } ir_volatility;
272
273 /** Returns the volatility of an entity. */
274 ir_volatility get_entity_volatility(const ir_entity *ent);
275
276 /** Sets the volatility of an entity. */
277 void set_entity_volatility(ir_entity *ent, ir_volatility vol);
278
279 /** Return the name of the volatility. */
280 const char *get_volatility_name(ir_volatility var);
281
282 /** This enumeration flags the align of Loads/Stores. */
283 typedef enum {
284         align_non_aligned,    /**< The entity is not aligned. */
285         align_is_aligned      /**< The entity is aligned. Default */
286 } ir_align;
287
288 /** Returns the alignment of an entity. */
289 ir_align get_entity_align(const ir_entity *ent);
290
291 /** Sets the alignment of an entity. */
292 void  set_entity_align(ir_entity *ent, ir_align a);
293
294 /** Return the name of the alignment. */
295 const char *get_align_name(ir_align a);
296
297 /** This enumeration flags the stickyness of an entity. */
298 typedef enum {
299         stickyness_unsticky,  /**< The entity can be removed from
300                                    the program, unless contraindicated
301                                    by other attributes. Default. */
302         stickyness_sticky     /**< The entity must remain in the
303                                    program in any case. There might be external
304                                    callers. */
305 } ir_stickyness;
306
307 /** Get the entity's stickyness. */
308 ir_stickyness get_entity_stickyness(const ir_entity *ent);
309
310 /** Set the entity's stickyness. */
311 void set_entity_stickyness(ir_entity *ent, ir_stickyness stickyness);
312
313 /** Returns the offset of an entity (in a compound) in bytes. Only set if layout = fixed. */
314 int get_entity_offset(const ir_entity *ent);
315
316 /** Sets the offset of an entity (in a compound) in bytes. */
317 void set_entity_offset(ir_entity *ent, int offset);
318
319 /** Returns the offset bit remainder of a bitfield entity (in a compound) in bits. Only set if layout = fixed. */
320 unsigned char get_entity_offset_bits_remainder(const ir_entity *ent);
321
322 /** Sets the offset bit remainder of a bitfield entity (in a compound) in bits. */
323 void set_entity_offset_bits_remainder(ir_entity *ent, unsigned char offset);
324
325 /** Returns the stored intermediate information. */
326 void *get_entity_link(const ir_entity *ent);
327
328 /** Stores new intermediate information. */
329 void set_entity_link(ir_entity *ent, void *l);
330
331 /* -- Fields of method entities -- */
332 /** The entity knows the corresponding irg if the entity is a method.
333    This allows to get from a Call to the called irg.
334    Only entities of peculiarity "existent" can have a corresponding irg,
335    else the field is fixed to NULL.  (Get returns NULL, set asserts.) */
336 ir_graph *get_entity_irg(const ir_entity *ent);
337 void set_entity_irg(ir_entity *ent, ir_graph *irg);
338
339 /** Gets the entity vtable number. */
340 unsigned get_entity_vtable_number(const ir_entity *ent);
341
342 /** Sets the entity vtable number. */
343 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number);
344
345 /** Return the peculiarity of an entity. */
346 ir_peculiarity get_entity_peculiarity(const ir_entity *ent);
347
348 /** Sets the peculiarity of an entity. */
349 void set_entity_peculiarity(ir_entity *ent, ir_peculiarity pec);
350
351 /** Checks if an entity cannot be overridden anymore. */
352 int is_entity_final(const ir_entity *ent);
353
354 /** Sets/resets the final flag of an entity. */
355 void set_entity_final(ir_entity *ent, int final);
356
357 /** Checks if an entity is compiler generated. */
358 int is_entity_compiler_generated(const ir_entity *ent);
359
360 /** Sets/resets the compiler generated flag. */
361 void set_entity_compiler_generated(ir_entity *ent, int flag);
362
363 /** Checks if an entity is marked by the backend. */
364 int is_entity_backend_marked(const ir_entity *ent);
365
366 /** Sets/resets the backend marker flag. */
367 void set_entity_backend_marked(ir_entity *ent, int flag);
368
369 /**
370  * Bitfield type indicating the way an entity is used.
371  */
372 typedef enum {
373         ir_usage_none             = 0,      /**< This entity is unused. */
374         ir_usage_address_taken    = 1 << 0, /**< The address of this entity was taken. */
375         ir_usage_write            = 1 << 1, /**< The entity was written to. */
376         ir_usage_read             = 1 << 2, /**< The entity was read. */
377         ir_usage_reinterpret_cast = 1 << 3, /**< The entity was read but with a wrong mode
378                                                  (an implicit reinterpret cast) */
379         /** Unknown access */
380         ir_usage_unknown
381                 = ir_usage_address_taken | ir_usage_write | ir_usage_read
382                 | ir_usage_reinterpret_cast
383 } ir_entity_usage;
384
385 /** Return the entity usage */
386 ir_entity_usage get_entity_usage(const ir_entity *ent);
387
388 /** Sets/resets the state of the address taken flag of an entity. */
389 void set_entity_usage(ir_entity *ent, ir_entity_usage flag);
390
391 /**
392  * Returns the debug information of an entity.
393  *
394  * @param ent The entity.
395  */
396 dbg_info *get_entity_dbg_info(const ir_entity *ent);
397
398 /**
399  * Sets the debug information of an entity.
400  *
401  * @param ent The entity.
402  * @param db  The debug info.
403  */
404 void set_entity_dbg_info(ir_entity *ent, dbg_info *db);
405
406 /* -- Representation of constant values of entities -- */
407 /**
408  * Returns true if the the node is representable as code on
409  * const_code_irg.
410  *
411  * @deprecated This function is not used by libFirm and stays here
412  *             only as a helper for the old Jack frontend.
413  */
414 int is_irn_const_expression(ir_node *n);
415
416 /**
417  * Copies a Firm subgraph that complies to the restrictions for
418  * constant expressions to current_block in current_ir_graph.
419  *
420  * @param dbg  debug info for all newly created nodes
421  * @param n    the node
422  *
423  * Set current_ir_graph to get_const_code_irg() to generate a constant
424  * expression.
425  */
426 ir_node *copy_const_value(dbg_info *dbg, ir_node *n);
427
428 /* Set has no effect for existent entities of type method. */
429 ir_node *get_atomic_ent_value(ir_entity *ent);
430 void set_atomic_ent_value(ir_entity *ent, ir_node *val);
431
432 /** the kind (type) of an initializer */
433 typedef enum ir_initializer_kind_t {
434         /** initializer containing an ir_node from the const-code irg */
435         IR_INITIALIZER_CONST,
436         /** initializer containing a tarval */
437         IR_INITIALIZER_TARVAL,
438         /** initializes type with default values (usually 0) */
439         IR_INITIALIZER_NULL,
440         /** list of initializers used to initializer a compound or array type */
441         IR_INITIALIZER_COMPOUND
442 } ir_initializer_kind_t;
443
444 /** returns kind of an initializer */
445 ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer);
446
447 /**
448  * returns the null initializer (there's only one instance of it in a program )
449  */
450 ir_initializer_t *get_initializer_null(void);
451
452 /**
453  * creates an initializer containing a reference to a node on the const-code
454  * irg.
455  */
456 ir_initializer_t *create_initializer_const(ir_node *value);
457
458 /** creates an initializer containing a single tarval value */
459 ir_initializer_t *create_initializer_tarval(tarval *tv);
460
461 /** return value contained in a const initializer */
462 ir_node *get_initializer_const_value(const ir_initializer_t *initializer);
463
464 /** return value contained in a tarval initializer */
465 tarval *get_initializer_tarval_value(const ir_initializer_t *initialzier);
466
467 /** creates a compound initializer which holds @p n_entries entries */
468 ir_initializer_t *create_initializer_compound(unsigned n_entries);
469
470 /** returns the number of entries in a compound initializer */
471 unsigned get_initializer_compound_n_entries(const ir_initializer_t *initializer);
472
473 /** sets entry with index @p index to the initializer @p value */
474 void set_initializer_compound_value(ir_initializer_t *initializer,
475                                     unsigned index, ir_initializer_t *value);
476
477 /** returns the value with index @p index of a compound initializer */
478 ir_initializer_t *get_initializer_compound_value(
479                 const ir_initializer_t *initializer, unsigned index);
480
481 /** Creates a new compound graph path of given length. */
482 compound_graph_path *new_compound_graph_path(ir_type *tp, int length);
483
484 /** Returns non-zero if an object is a compound graph path */
485 int is_compound_graph_path(const void *thing);
486
487 /** Frees a graph path object */
488 void free_compound_graph_path(compound_graph_path *gr);
489
490 /** Returns the length of a graph path */
491 int get_compound_graph_path_length(const compound_graph_path *gr);
492
493 /** Get the entity node of an compound graph path at position pos. */
494 ir_entity *get_compound_graph_path_node(const compound_graph_path *gr, int pos);
495 /** Set the entity node of an compound graph path at position pos. */
496 void      set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node);
497 /** Get the index of an compound graph path at position pos. */
498 int       get_compound_graph_path_array_index(const compound_graph_path *gr, int pos);
499 /** Set the index of an compound graph path at position pos. */
500 void      set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index);
501 /** Get the type of an compound graph path. */
502 ir_type   *get_compound_graph_path_type(const compound_graph_path *gr);
503
504 /** Checks whether the path up to pos is correct. If the path contains a NULL,
505  *  assumes the path is not complete and returns non-zero. */
506 int is_proper_compound_graph_path(compound_graph_path *gr, int pos);
507
508 /* A value of a compound entity is a pair of a value and the description of the
509    corresponding access path to the member of the compound.  */
510 void add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path);
511 void set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos);
512
513 /** Returns the number of constant values needed to initialize the entity.
514  *
515  *  Asserts if the entity has variability_uninitialized.
516  * */
517 int get_compound_ent_n_values(ir_entity *ent);
518 /** Returns a constant value given the position. */
519 ir_node *get_compound_ent_value(ir_entity *ent, int pos);
520 /** Returns the access path for value at position pos. */
521 compound_graph_path *get_compound_ent_value_path(ir_entity *ent, int pos);
522 /** Returns a constant value given the access path.
523  *  The path must contain array indices for all array element entities. */
524 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path);
525
526 /** Removes all constant entries where the path ends at value_ent. Does not
527    free the memory of the paths.  (The same path might be used for several
528    constant entities. */
529 void remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent);
530
531 /* Some languages support only trivial access paths, i.e., the member is a
532    direct, atomic member of the constant entities type. In this case the
533    corresponding entity can be accessed directly.  The following functions
534    allow direct access. */
535
536 /** Generates a Path with length 1.
537     Beware: Has a bad runtime for array elements (O(|array|) and should be
538     avoided there. Use add_compound_ent_value_w_path() instead and create
539     the path manually. */
540 void add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member);
541
542 /** Returns the last member in the path */
543 ir_entity *get_compound_ent_value_member(ir_entity *ent, int pos);
544
545 /** Sets the path at pos 0 */
546 void set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos);
547
548 /** Sets the new style initializers of an entity. */
549 void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer);
550
551 /** Return the new style initializers of an entity. */
552 ir_initializer_t *get_entity_initializer(const ir_entity *entity);
553
554 /** Initializes the entity ent which must be of a one dimensional
555    array type with the values given in the values array.
556    The array must have a lower and an upper bound.  Keeps the
557    order of values. Does not test whether the number of values
558    fits into the given array size.  Does not test whether the
559    values have the proper mode for the array. */
560 void set_array_entity_values(ir_entity *ent, tarval **values, int num_vals);
561
562 /**
563  * Return the offset in bits from the last byte address.
564  *
565  * This requires that the layout of all concerned types is fixed.
566  *
567  * @param ent Any entity of compound type with at least pos initialization values.
568  * @param pos The position of the value for which the offset is requested.
569  */
570 unsigned get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos);
571
572 /** Return the overall offset of value at position pos in bytes.
573  *
574  * This requires that the layout of all concerned types is fixed.
575  * Asserts if bit offset is not byte aligned.
576  *
577  * @param ent Any entity of compound type with at least pos initialization values.
578  * @param pos The position of the value for which the offset is requested.
579  */
580 unsigned get_compound_ent_value_offset_bytes(ir_entity *ent, int pos);
581
582 /* --- Fields of entities with a class type as owner --- */
583 /* Overwrites is a field that specifies that an access to the overwritten
584    entity in the supertype must use this entity.  It's a list as with
585    multiple inheritance several entities can be overwritten.  This field
586    is mostly useful for method entities.
587    If a Sel node selects an entity that is overwritten by other entities it
588    must return a pointer to the entity of the dynamic type of the pointer
589    that is passed to it.  Lowering of the Sel node must assure this.
590    Overwrittenby is the inverse of overwrites.  Both add routines add
591    both relations, they only differ in the order of arguments. */
592 void add_entity_overwrites(ir_entity *ent, ir_entity *overwritten);
593 int get_entity_n_overwrites(ir_entity *ent);
594 int get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten);
595 ir_entity *get_entity_overwrites(ir_entity *ent, int pos);
596 void set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten);
597 void remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten);
598
599 void add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites);
600 int get_entity_n_overwrittenby(ir_entity *ent);
601 int get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites);
602 ir_entity *get_entity_overwrittenby(ir_entity *ent, int pos);
603 void set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites);
604 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites);
605
606 /**
607  *   Checks whether a pointer points to an entity.
608  *
609  *   @param thing     an arbitrary pointer
610  *
611  *   @return
612  *       true if the thing is an entity, else false
613  */
614 int is_entity(const void *thing);
615
616 /** Returns true if the type of the entity is a primitive, pointer
617  * enumeration or method type.
618  *
619  * @Note This is a different classification than from is_primitive_type().
620  */
621 int is_atomic_entity(ir_entity *ent);
622 /** Returns true if the type of the entity is a class, structure,
623    array or union type. */
624 int is_compound_entity(ir_entity *ent);
625 /** Returns true if the type of the entity is a Method type. */
626 int is_method_entity(ir_entity *ent);
627
628 /** Returns non-zero if ent1 and ent2 have are equal except for their owner.
629    Two entities are equal if
630     - they have the same type (the same C-struct)
631     - ...?
632 */
633 int equal_entity(ir_entity *ent1, ir_entity *ent2);
634
635 /** Outputs a unique number for this entity if libfirm is compiled for
636  *  debugging, (configure with --enable-debug) else returns the address
637  *  of the type cast to long.
638  */
639 long get_entity_nr(const ir_entity *ent);
640
641 /** Returns the entities visited count. */
642 ir_visited_t get_entity_visited(ir_entity *ent);
643
644 /** Sets the entities visited count. */
645 void set_entity_visited(ir_entity *ent, ir_visited_t num);
646
647 /** Sets visited field in entity to entity_visited. */
648 void mark_entity_visited(ir_entity *ent);
649
650 /** Returns true if this entity was visited. */
651 int entity_visited(ir_entity *ent);
652
653 /** Returns true if this entity was not visited. */
654 int entity_not_visited(ir_entity *ent);
655
656 /**
657  * Returns the mask of the additional entity properties.
658  * The properties are automatically inherited from the irg if available
659  * or from the method type if they were not set using
660  * set_entity_additional_properties() or
661  * set_entity_additional_property().
662  */
663 unsigned get_entity_additional_properties(ir_entity *ent);
664
665 /** Sets the mask of the additional graph properties. */
666 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask);
667
668 /** Sets one additional graph property. */
669 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag);
670
671 /** Returns the class type that this type info entity represents or NULL
672     if ent is no type info entity. */
673 ir_type *get_entity_repr_class(const ir_entity *ent);
674
675 /**
676  * @page unknown_entity  The Unknown entity
677  *
678  *  This entity is an auxiliary entity dedicated to support analyses.
679  *
680  *  The unknown entity represents that there could be an entity, but it is not
681  *  known.  This entity can be used to initialize fields before an analysis (not known
682  *  yet) or to represent the top of a lattice (could not be determined).  There exists
683  *  exactly one entity unknown. This entity has as owner and as type the unknown type. It is
684  *  allocated when initializing the entity module.
685  *
686  *  The entity can take the role of any entity, also methods.  It returns default
687  *  values in these cases.
688  *
689  *  The following values are set:
690  *
691  * - name          = "unknown_entity"
692  * - ld_name       = "unknown_entity"
693  * - owner         = unknown_type
694  * - type          = unknown_type
695  * - allocation    = allocation_automatic
696  * - visibility    = visibility_external_allocated
697  * - offset        = -1
698  * - variability   = variability_uninitialized
699  * - value         = SymConst(unknown_entity)
700  * - values        = NULL
701  * - val_paths     = NULL
702  * - peculiarity   = peculiarity_existent
703  * - volatility    = volatility_non_volatile
704  * - stickyness    = stickyness_unsticky
705  * - ld_name       = NULL
706  * - overwrites    = NULL
707  * - overwrittenby = NULL
708  * - irg           = NULL
709  * - link          = NULL
710  */
711 /* A variable that contains the only unknown entity. */
712 extern ir_entity *unknown_entity;
713
714 /** Returns the @link unknown_entity unknown entity @endlink. */
715 ir_entity *get_unknown_entity(void);
716
717 /** Encodes how a pointer parameter is accessed. */
718 typedef enum acc_bits {
719         ptr_access_none  = 0,                                 /**< no access */
720         ptr_access_read  = 1,                                 /**< read access */
721         ptr_access_write = 2,                                 /**< write access */
722         ptr_access_rw    = ptr_access_read|ptr_access_write,  /**< read AND write access */
723         ptr_access_store = 4,                                 /**< the pointer is stored */
724         ptr_access_all   = ptr_access_rw|ptr_access_store     /**< all possible access */
725 } ptr_access_kind;
726
727 #define IS_READ(a)     ((a) & ptr_access_read)
728 #define IS_WRITTEN(a)  ((a) & ptr_access_write)
729 #define IS_STORED(a)   ((a) & ptr_access_store)
730
731 /**
732  * @page tyop  type operations
733  *  This module specifies the kinds of types available in firm.
734  *
735  *  They are called type opcodes. These include classes, structs, methods, unions,
736  *  arrays, enumerations, pointers and primitive types.
737  *  Special types with own opcodes are the id type, a type representing an unknown
738  *  type and a type used to specify that something has no type.
739  *
740  *  @see type.h
741  */
742
743 /**
744  *  An enum for the type kinds.
745  *  For each type kind exists a typecode to identify it.
746  */
747 typedef enum {
748         tpo_uninitialized = 0,   /* not a type opcode */
749         tpo_class,               /**< A class type. */
750         tpo_struct,              /**< A struct type. */
751         tpo_method,              /**< A method type. */
752         tpo_union,               /**< An union type. */
753         tpo_array,               /**< An array type. */
754         tpo_enumeration,         /**< An enumeration type. */
755         tpo_pointer,             /**< A pointer type. */
756         tpo_primitive,           /**< A primitive type. */
757         tpo_id,                  /**< Special Id tag used for type replacement. */
758         tpo_none,                /**< Special type for the None type. */
759         tpo_unknown,             /**< Special code for the Unknown type. */
760         tpo_max                  /* not a type opcode */
761 } tp_opcode;
762
763 /**
764  * A structure containing information about a kind of type.
765  * A structure containing information about a kind of type.  So far
766  * this is only the kind name, an enum for case-switching and some
767  * internal values.
768  *
769  * @see  get_tpop_name(), get_tpop_code(), get_tpop_ident()
770  */
771 typedef struct tp_op tp_op;
772
773
774 /**
775  * Returns the string for the type opcode.
776  *
777  * @param op  The type opcode to get the string from.
778  * @return a string.  (@todo Null terminated?)
779  */
780 const char *get_tpop_name(const tp_op *op);
781
782 /**
783  * Returns an enum for the type opcode.
784  *
785  * @param op   The type opcode to get the enum from.
786  * @return the enum.
787  */
788 tp_opcode get_tpop_code(const tp_op *op);
789
790 /**
791  * Returns the ident for the type opcode.
792  *
793  * @param op   The type opcode to get the ident from.
794  * @return The ident.
795  */
796 ident *get_tpop_ident(const tp_op *op);
797
798 /**
799  * This type opcode marks that the corresponding type is a class type.
800  *
801  * Consequently the type refers to supertypes, subtypes and entities.
802  * Entities can be any fields, but also methods.
803  * @@@ value class or not???
804  * This struct is dynamically allocated but constant for the lifetime
805  * of the library.
806  */
807 extern tp_op *type_class;
808 tp_op *get_tpop_class(void);
809
810 /**
811  * This type opcode marks that the corresponding type is a compound type
812  * as a struct in C.
813  *
814  * Consequently the type refers to a list of entities
815  * which may not be methods (but pointers to methods).
816  * This struct is dynamically allocated but constant for the lifetime
817  * of the library.
818  */
819 extern tp_op *type_struct;
820 tp_op *get_tpop_struct(void);
821
822 /**
823  * This type opcode marks that the corresponding type is a method type.
824  *
825  * Consequently it refers to a list of arguments and results.
826  * This struct is dynamically allocated but constant for the lifetime
827  * of the library.
828  */
829 extern tp_op *type_method;
830 tp_op *get_tpop_method(void);
831
832 /**
833  * This type opcode marks that the corresponding type is a union type.
834  *
835  * Consequently it refers to a list of unioned types.
836  * This struct is dynamically allocated but constant for the lifetime
837  * of the library.
838  */
839 extern tp_op *type_union;
840 tp_op *get_tpop_union(void);
841
842 /**
843  * This type opcode marks that the corresponding type is an array type.
844  *
845  * Consequently it contains a list of dimensions (lower and upper bounds)
846  * and an element type.
847  * This struct is dynamically allocated but constant for the lifetime
848  * of the library.
849  */
850 extern tp_op *type_array;
851 tp_op *get_tpop_array(void);
852
853 /**
854  * This type opcode marks that the corresponding type is an enumeration type.
855  *
856  * Consequently it contains a list of idents for the enumeration identifiers
857  * and a list of target values that are the constants used to implement
858  * the enumerators.
859  * This struct is dynamically allocated but constant for the lifetime
860  * of the library.
861  */
862 extern tp_op *type_enumeration;
863 tp_op *get_tpop_enumeration(void);
864
865 /**
866  * This type opcode marks that the corresponding type is a pointer type.
867  *
868  * It contains a reference to the type the pointer points to.
869  * This struct is dynamically allocated but constant for the lifetime
870  * of the library.
871  */
872 extern tp_op *type_pointer;
873 tp_op *get_tpop_pointer(void);
874
875 /**
876  * This type opcode marks that the corresponding type is a primitive type.
877  *
878  * Primitive types are types that are directly mapped to target machine
879  * modes.
880  * This struct is dynamically allocated but constant for the lifetime
881  * of the library.
882  */
883 extern tp_op *type_primitive;
884 tp_op *get_tpop_primitive(void);
885
886 /**
887  * This type opcode is an auxiliary opcode dedicated to support transformations
888  * of the type structure.
889  *
890  * If a type is changed to another type with another
891  * opcode the new type will be allocated with new memory.  All nodes refering
892  * to the old type need to be changed to refer to the new one.  This is simplified
893  * by turning the old type into an id type that merely forwards to the new type
894  * that now replaces the old one.
895  * type_ids should never be visible out of the type module.  All access routines
896  * should automatically check for type_id and eventually follow the forward in
897  * type_id.  Two types are exchanged by a call to exchange_types.
898  * If a type_id is visible externally report this as bug.  If it is assured that
899  * this never happens this extern variable can be moved to tpop_t.h.
900  * This struct is dynamically allocated but constant for the lifetime
901  * of the library.
902  */
903 extern tp_op *type_id;
904 tp_op *get_tpop_id(void);
905
906 /**
907  * This type opcode is an auxiliary opcode dedicated to support type analyses.
908  *
909  * Types with this opcode represents that there is no type.
910  * The type can be used to initialize fields of the type* that actually can not
911  * contain a type or that are initialized for an analysis. There exists exactly
912  * one type with this opcode.
913  */
914 extern tp_op *tpop_none;
915 tp_op *get_tpop_none(void);
916
917 /**
918  * This type opcode is an auxiliary opcode dedicated to support type analyses.
919  *
920  * Types with this opcode represents that there could be a type, but it is not
921  * known.  This type can be used to initialize fields before an analysis (not known
922  * yet) or to represent the top of a lattice (could not be determined).  There exists
923  * exactly one type with this opcode.
924  */
925 extern tp_op *tpop_unknown;
926 tp_op *get_tpop_unknown(void);
927
928 /* ----------------------------------------------------------------------- */
929 /* Classify pairs of types/entities in the inheritance relations.          */
930 /* ----------------------------------------------------------------------- */
931
932 /** Returns true if low is subclass of high.
933  *
934  *  Low is a subclass of high if low == high or if low is a subclass of
935  *  a subclass of high.  I.e, we search in all subtypes of high for low.
936  *  @@@ this can be implemented more efficient if we know the set of all
937  *  subclasses of high.  */
938 int is_SubClass_of(ir_type *low, ir_type *high);
939
940 /** Subclass check for pointers to classes.
941  *
942  *  Dereferences at both types the same amount of pointer types (as
943  *  many as possible).  If the remaining types are both class types
944  *  and subclasses, returns true, else false.  Can also be called with
945  *  two class types.  */
946 int is_SubClass_ptr_of(ir_type *low, ir_type *high);
947
948 /** Returns true if high is superclass of low.
949  *
950  *  Low is a subclass of high if low == high or if low is a subclass of
951  *  a subclass of high.  I.e, we search in all subtypes of high for low.
952  *  @@@ this can be implemented more efficient if we know the set of all
953  *  subclasses of high.  */
954 #define is_SuperClass_of(high, low) is_SubClass_of(low, high)
955
956 /** Superclass check for pointers to classes.
957  *
958  *  Dereferences at both types the same amount of pointer types (as
959  *  many as possible).  If the remaining types are both class types
960  *  and superclasses, returns true, else false.  Can also be called with
961  *  two class types.  */
962 #define is_SuperClass_ptr_of(low, high) is_SubClass_ptr_of(high, low)
963
964 /** Returns true if high is (transitive) overwritten by low.
965  *
966  *  Returns false if high == low. */
967 int is_overwritten_by(ir_entity *high, ir_entity *low);
968
969 /** Resolve polymorphism in the inheritance relation.
970  *
971  *  Returns the dynamically referenced entity if the static entity and the
972  *  dynamic type are given.
973  *  Searches downwards in overwritten tree. */
974 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity* static_ent);
975
976 /* ----------------------------------------------------------------------- */
977 /* Resolve implicit inheritance.                                           */
978 /* ----------------------------------------------------------------------- */
979
980 /** Default name mangling for inherited entities.
981  *
982  *  Returns an ident that consists of the name of type followed by an
983  *  underscore and the name (not ld_name) of the entity. */
984 ident *default_mangle_inherited_name(ir_entity *ent, ir_type *clss);
985
986 /** Type of argument functions for inheritance resolver.
987  *
988  * @param ent     The entity in the super type that will be overwritten
989  *                by the newly generated entity, for which this name is
990  *                used.
991  * @param clss    The class type in which the new entity will be placed.
992  */
993 typedef ident *mangle_inherited_name_func(ir_entity *ent, ir_type *clss);
994
995 /** Resolve implicit inheritance.
996  *
997  *  Resolves the implicit inheritance supplied by firm.  Firm defines,
998  *  that each entity that is not overwritten in a subclass is
999  *  inherited to this subclass without change implicitly.  This
1000  *  function generates entities that explicitly represent this
1001  *  inheritance.  It generates for each entity overwriting entities in
1002  *  all subclasses of the owner of the entity, if the entity is not
1003  *  overwritten in that subclass.
1004  *
1005  *  The name of the new entity is generated with the function passed.
1006  *  If the function is NULL, the default_mangle_inherited_name() is
1007  *  used.
1008  *
1009  *  This function was moved here from firmlower 3/2005.
1010  */
1011 void resolve_inheritance(mangle_inherited_name_func *mfunc);
1012
1013
1014 /* ----------------------------------------------------------------------- */
1015 /* The transitive closure of the subclass/superclass and                   */
1016 /* overwrites/overwrittenby relation.                                      */
1017 /*                                                                         */
1018 /* A walk over the ir (O(#types+#entities)) computes the transitive        */
1019 /* closure.  Adding a new type/entity or changing the basic relations in   */
1020 /* some other way invalidates the transitive closure, i.e., it is not      */
1021 /* updated by the basic functions.                                         */
1022 /*                                                                         */
1023 /* The transitive edges are held in a set, not in an array as the          */
1024 /* underlying relation.                                                    */
1025 /*                                                                         */
1026 /* Do the sets contain the node itself?  I assume NOT!                     */
1027 /* ----------------------------------------------------------------------- */
1028
1029 /** The state of the transitive closure.
1030  *
1031  *  @todo: we could manage the state for each relation separately.  Invalidating
1032  *  the entity relations does not mean invalidating the class relation. */
1033 typedef enum {
1034         inh_transitive_closure_none,       /**<  Closure is not computed, can not be accessed. */
1035         inh_transitive_closure_valid,      /**<  Closure computed and valid. */
1036         inh_transitive_closure_invalid,    /**<  Closure invalid, but can be accessed. */
1037         inh_transitive_closure_max         /**<  Invalid value. */
1038 } inh_transitive_closure_state;
1039
1040 void                         set_irp_inh_transitive_closure_state(inh_transitive_closure_state s);
1041 void                         invalidate_irp_inh_transitive_closure_state(void);
1042 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void);
1043
1044
1045 /** Compute transitive closure of the subclass/superclass and
1046  * overwrites/overwrittenby relation.
1047  *
1048  * This function walks over the ir (O(#types+#entities)) to compute the
1049  * transitive closure.    */
1050 void compute_inh_transitive_closure(void);
1051
1052 /** Free memory occupied by the transitive closure information. */
1053 void free_inh_transitive_closure(void);
1054
1055
1056 /* - subtype ------------------------------------------------------------- */
1057
1058 /** Iterate over all transitive subtypes. */
1059 ir_type *get_class_trans_subtype_first(const ir_type *tp);
1060 ir_type *get_class_trans_subtype_next(const ir_type *tp);
1061 int is_class_trans_subtype(const ir_type *tp, const ir_type *subtp);
1062
1063 /* - supertype ----------------------------------------------------------- */
1064
1065 /** Iterate over all transitive supertypes. */
1066 ir_type *get_class_trans_supertype_first(const ir_type *tp);
1067 ir_type *get_class_trans_supertype_next(const ir_type *tp);
1068
1069 /* - overwrittenby ------------------------------------------------------- */
1070
1071 /** Iterate over all entities that transitive overwrite this entities. */
1072 ir_entity *get_entity_trans_overwrittenby_first(const ir_entity *ent);
1073 ir_entity *get_entity_trans_overwrittenby_next(const ir_entity *ent);
1074
1075 /* - overwrites ---------------------------------------------------------- */
1076
1077 /** Iterate over all transitive overwritten entities. */
1078 ir_entity *get_entity_trans_overwrites_first(const ir_entity *ent);
1079 ir_entity *get_entity_trans_overwrites_next(const ir_entity *ent);
1080
1081
1082 /* ----------------------------------------------------------------------- */
1083 /** The state of Cast operations that cast class types or pointers to class
1084  *  types.
1085  *
1086  * The state expresses, how far Cast operations conform with the class
1087  * hierarchy.
1088  *
1089  *   class A {}
1090  *   class B1 extends A {}
1091  *   class B2 extends A {}
1092  *   class C  extends B1 {}
1093  * normalized:  Cast operations conform with the inheritance relation.
1094  *   I.e., the type of the operand of a Cast is either a super= or a sub-
1095  *   type of the type casted to. Example: (A)((B2) (new C())).
1096  * transitive:  Cast operations conform with the transitive inheritance
1097  *   relation. Example: (A)(new C()).
1098  * any:  Cast operations do not conform with the transitive inheritance
1099  *   relation.  Example: (B2)(new B1())
1100  *
1101  *  @see: tropt.h
1102  */
1103 /* ----------------------------------------------------------------------- */
1104
1105 /** Flags for class cast state.
1106  *
1107  * The state in irp is always smaller or equal to the state of any
1108  * irg.
1109  *
1110  * We rely on the ordering of the enum. */
1111 typedef enum {
1112         ir_class_casts_any        = 0, /**< There are class casts that do not cast in conformance with
1113                                             the class hierarchy.  @@@ So far this does not happen in Firm. */
1114         ir_class_casts_transitive = 1, /**< Class casts conform to transitive inheritance edges. Default. */
1115         ir_class_casts_normalized = 2, /**< Class casts conform to inheritance edges. */
1116         ir_class_casts_state_max
1117 } ir_class_cast_state;
1118 char *get_class_cast_state_string(ir_class_cast_state s);
1119
1120 void                set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s);
1121 ir_class_cast_state get_irg_class_cast_state(ir_graph *irg);
1122 void                set_irp_class_cast_state(ir_class_cast_state s);
1123 ir_class_cast_state get_irp_class_cast_state(void);
1124
1125 /** Verify the class cast state of an irg.
1126  *
1127  *  Asserts if state is to high, outputs warning if state is to low
1128  *  and firm verbosity is set.
1129  */
1130 void verify_irg_class_cast_state(ir_graph *irg);
1131
1132 /**
1133  * possible trvrfy() error codes
1134  */
1135 enum trvrfy_error_codes {
1136         no_error = 0,                      /**< no error */
1137         error_ent_not_cont,                /**< overwritten entity not in superclass */
1138         error_null_mem,                    /**< compound contains NULL member */
1139         error_const_on_wrong_irg,          /**< constant placed on wrong IRG */
1140         error_existent_entity_without_irg, /**< Method entities with pecularity_exist must have an irg */
1141         error_wrong_ent_overwrites,        /**< number of entity overwrites exceeds number of class overwrites */
1142         error_inherited_ent_without_const, /**< inherited method entity not pointing to existent entity */
1143         error_glob_ent_allocation,         /**< wrong allocation of a global entity */
1144         error_ent_const_mode,              /**< Mode of constant in entity did not match entities type. */
1145         error_ent_wrong_owner              /**< Mode of constant in entity did not match entities type. */
1146 };
1147
1148 /**
1149  * Checks a type.
1150  *
1151  * @return
1152  *  0   if no error encountered
1153  */
1154 int check_type(ir_type *tp);
1155
1156 /**
1157  * Check an entity. Currently, we check only if initialized constants
1158  * are build on the const irg graph.
1159  *
1160  * @return
1161  *  0   if no error encountered
1162  *  != 0    a trvrfy_error_codes code
1163  */
1164 int check_entity(ir_entity *ent);
1165
1166 /**
1167  * Walks the type information and performs a set of sanity checks.
1168  *
1169  * Currently, the following checks are executed:
1170  * - values of initialized entities must be allocated on the constant IRG
1171  * - class types: doesn't have NULL members
1172  * - class types: all overwrites are existent in the super type
1173  *
1174  * @return
1175  *    0 if graph is correct
1176  *    else error code.
1177  */
1178 int tr_vrfy(void);
1179
1180 /**
1181  * If NDEBUG is defined performs nothing, else calls the tr_vrfy() function.
1182  */
1183 #ifdef NDEBUG
1184 #define TR_VRFY()       0
1185 #else
1186 #define TR_VRFY()       tr_vrfy()
1187 #endif
1188
1189 /**
1190  *
1191  * @file typegmod.h
1192  *  This module supplies routines that support changing the type graph.
1193  */
1194
1195 /** Replaces one type by the other.
1196  *
1197  *  Old type is replaced by new_type.  All references to old_type
1198  *  now point to new_type.  The memory for the old type is destroyed,
1199  *  but still used.  Therefore it is not freed.
1200  *  All referenced to this memory will be lost after a certain while.
1201  *  An exception is the list of types in irp (irprog.h).
1202  *  In the future there might be a routine to recover the memory, but
1203  *  this will be at considerable runtime cost.
1204  *
1205  *  @param old_type  - The old type that shall be replaced by the new type.
1206  *  @param new_type  - The new type that will replace old_type.
1207  *
1208  */
1209 void exchange_types(ir_type *old_type, ir_type *new_type);
1210
1211 /** Skip id types until a useful type is reached.
1212  *
1213  *  @param tp - A type of arbitrary kind.
1214  *
1215  *  @return
1216  *    tp if it is not an id type.
1217  *    If tp is an id type returns the real type it stands for.
1218  */
1219 ir_type *skip_tid(ir_type *tp);
1220
1221 /**
1222  * @page type   representation of types
1223  *
1224  *  Datastructure to hold type information.
1225  *
1226  *  This module supplies a datastructure to represent all types
1227  *  known in the compiled program.  This includes types specified
1228  *  in the program as well as types defined by the language.  In the
1229  *  view of the intermediate representation there is no difference
1230  *  between these types.  Finally it specifies some auxiliary types.
1231  *
1232  *  There exist several kinds of types, arranged by the structure of
1233  *  the type.  A type is described by a set of attributes.  Some of
1234  *  these attributes are common to all types, others depend on the
1235  *  kind of the type.
1236  *
1237  *  Types are different from the modes defined in irmode:  Types are
1238  *  on the level of the programming language, modes at the level of
1239  *  the target processor.
1240  *
1241  *  @see  tpop.h
1242  */
1243
1244 #include "typerep.h"
1245
1246 /** Frees all entities associated with a type.
1247  *  Does not free the array entity.
1248  *  Warning: ensure these entities are not referenced anywhere else.
1249  */
1250 void free_type_entities(ir_type *tp);
1251
1252 /** Frees the memory used by the type.
1253  *
1254  * Removes the type from the type list. Does not free the entities
1255  * belonging to the type, except for the array element entity.  Does
1256  * not free if tp is "none" or "unknown".  Frees entities in value
1257  * param subtypes of method types!!! Make sure these are not
1258  * referenced any more.  Further make sure there is no pointer type
1259  * that refers to this type.                           */
1260 void free_type(ir_type *tp);
1261
1262 const tp_op *get_type_tpop(const ir_type *tp);
1263 ident *get_type_tpop_nameid(const ir_type *tp);
1264 const char *get_type_tpop_name(const ir_type *tp);
1265 tp_opcode get_type_tpop_code(const ir_type *tp);
1266
1267 ident *get_type_ident(const ir_type *tp);
1268 void set_type_ident(ir_type *tp, ident* id);
1269 const char *get_type_name(const ir_type *tp);
1270
1271 /** The visibility of a type.
1272  *
1273  *  The visibility of a type indicates, whether entities of this type
1274  *  are accessed or allocated in external code.
1275  *
1276  *  An entity of a type is allocated in external code, if the external
1277  *  code declares a variable of this type, or dynamically allocates
1278  *  an entity of this type.  If the external code declares a (compound)
1279  *  type, that contains entities of this type, the visibility also
1280  *  must be external_allocated.
1281  *
1282  *  The visibility must be higher than that of all entities, if the
1283  *  type is a compound.  Here it is questionable, what happens with
1284  *  static entities.  If these are accessed external by direct reference,
1285  *  (a static call to a method, that is also in the dispatch table)
1286  *  it should not affect the visibility of the type.
1287  *
1288  *
1289  * @@@ Do we need a visibility for types?
1290  * I change the layout of types radically when doing type splitting.
1291  * I need to know, which fields of classes are accessed in the RTS,
1292  * e.g., [_length.  I may not move [_length to the split part.
1293  * The layout though, is a property of the type.
1294  *
1295  * One could also think of changing the mode of a type ...
1296  *
1297  * But, we could also output macros to access the fields, e.g.,
1298  *  ACCESS_[_length (X)   X->length              // conventional
1299  *  ACCESS_[_length (X)   X->_split_ref->length  // with type splitting
1300  *
1301  * For now I implement this function, that returns the visibility
1302  * based on the visibility of the entities of a compound ...
1303  *
1304  * This function returns visibility_external_visible if one or more
1305  * entities of a compound type have visibility_external_visible.
1306  * Entities of types are never visibility_external_allocated (right?).
1307  * Else returns visibility_local.
1308  */
1309 ir_visibility get_type_visibility(const ir_type *tp);
1310 void          set_type_visibility(ir_type *tp, ir_visibility v);
1311
1312
1313
1314 /** The state of the type layout. */
1315 typedef enum {
1316         layout_undefined,    /**< The layout of this type is not defined.
1317                                   Address computation to access fields is not
1318                                   possible, fields must be accessed by Sel
1319                                   nodes.  Enumeration constants might be undefined.
1320                                   This is the default value except for
1321                                   pointer, primitive and method types. */
1322         layout_fixed         /**< The layout is fixed, all component/member entities
1323                                   have an offset assigned.  Size of the type is known.
1324                                   Arrays can be accessed by explicit address
1325                                   computation.  Enumeration constants must be defined.
1326                                   Default for pointer, primitive and method types. */
1327 } ir_type_state;
1328
1329 /** Returns a human readable string for the enum entry. */
1330 const char *get_type_state_name(ir_type_state s);
1331
1332 /** Returns the type layout state of a type. */
1333 ir_type_state get_type_state(const ir_type *tp);
1334
1335 /** Sets the type layout state of a type.
1336  *
1337  * For primitives, pointer and method types the layout is always fixed.
1338  * This call is legal but has no effect.
1339  */
1340 void set_type_state(ir_type *tp, ir_type_state state);
1341
1342 /** Returns the mode of a type.
1343  *
1344  * Returns NULL for all non atomic types.
1345  */
1346 ir_mode *get_type_mode(const ir_type *tp);
1347
1348 /** Sets the mode of a type.
1349  *
1350  * Only has an effect on primitive, enumeration and pointer types.
1351  */
1352 void set_type_mode(ir_type *tp, ir_mode* m);
1353
1354 /** Returns the size of a type in bytes. */
1355 unsigned get_type_size_bytes(const ir_type *tp);
1356
1357 /** Sets the size of a type in bytes.
1358  *
1359  * For primitive, enumeration, pointer and method types the size
1360  * is always fixed. This call is legal but has no effect.
1361  */
1362 void set_type_size_bytes(ir_type *tp, unsigned size);
1363
1364 /** Returns the alignment of a type in bytes. */
1365 unsigned get_type_alignment_bytes(ir_type *tp);
1366
1367 /** Returns the alignment of a type in bits.
1368  *
1369  *  If the alignment of a type is
1370  *  not set, it is calculated here according to the following rules:
1371  *  -#.) if a type has a mode, the alignment is the mode size.
1372  *  -#.) compound types have the alignment of there biggest member.
1373  *  -#.) array types have the alignment of there element type.
1374  *  -#.) method types return 0 here.
1375  *  -#.) all other types return 1 here (i.e. aligned at byte).
1376  */
1377 void set_type_alignment_bytes(ir_type *tp, unsigned align);
1378
1379 /** Returns the visited count of a type. */
1380 ir_visited_t get_type_visited(const ir_type *tp);
1381 /** Sets the visited count of a type to num. */
1382 void set_type_visited(ir_type *tp, ir_visited_t num);
1383 /** Sets visited field in type to type_visited. */
1384 void mark_type_visited(ir_type *tp);
1385 /** Returns non-zero if the type is already visited */
1386 int type_visited(const ir_type *tp);
1387 /** Returns non-zero if the type is not yet visited */
1388 int type_not_visited(const ir_type *tp);
1389
1390 /** Returns the associated link field of a type. */
1391 void *get_type_link(const ir_type *tp);
1392 /** Sets the associated link field of a type. */
1393 void set_type_link(ir_type *tp, void *l);
1394
1395 /**
1396  * Visited flag to traverse the type information.
1397  *
1398  * Increase this flag by one before traversing the type information
1399  * using inc_master_type_visited().
1400  * Mark type nodes as visited by mark_type_visited(ir_type).
1401  * Check whether node was already visited by type_visited(ir_type)
1402  * and type_not_visited(ir_type).
1403  * Or use the function to walk all types.
1404  *
1405  * @see  typewalk
1406  */
1407 void         set_master_type_visited(ir_visited_t val);
1408 ir_visited_t get_master_type_visited(void);
1409 void         inc_master_type_visited(void);
1410
1411 /**
1412  * Sets the debug information of a type.
1413  *
1414  * @param tp  The type.
1415  * @param db  The debug info.
1416  */
1417 void set_type_dbg_info(ir_type *tp, dbg_info *db);
1418
1419 /**
1420  * Returns the debug information of a type.
1421  *
1422  * @param tp  The type.
1423  */
1424 dbg_info *get_type_dbg_info(const ir_type *tp);
1425
1426 /**
1427  * Checks whether a pointer points to a type.
1428  *
1429  * @param thing     an arbitrary pointer
1430  *
1431  * @return
1432  *     true if the thing is a type, else false
1433  */
1434 int is_type(const void *thing);
1435
1436 /**
1437  *   Checks whether two types are structurally equal.
1438  *
1439  *   @param typ1  the first type
1440  *   @param typ2  the second type
1441  *
1442  *   @return
1443  *    true if the types are equal, else false.
1444  *
1445  *   Types are equal if :
1446  *    - they are the same type kind
1447  *    - they have the same name
1448  *    - they have the same mode (if applicable)
1449  *    - they have the same type_state and, ev., the same size
1450  *    - they are class types and have:
1451  *      - the same members (see same_entity in entity.h)
1452  *      - the same supertypes -- the C-pointers are compared --> no recursive call.
1453  *      - the same number of subtypes.  Subtypes are not compared,
1454  *        as this could cause a cyclic test.
1455  *      - the same peculiarity
1456  *    - they are structure types and have the same members
1457  *    - they are method types and have
1458  *      - the same parameter types
1459  *      - the same result types
1460  *    - they are union types and have the same members
1461  *    - they are array types and have
1462  *      - the same number of dimensions
1463  *      - the same dimension bounds
1464  *      - the same dimension order
1465  *      - the same element type
1466  *    - they are enumeration types and have the same enumerator names
1467  *    - they are pointer types and have the identical points_to type
1468  *      (i.e., the same C-struct to represent the type, type_id is skipped.
1469  *       This is to avoid endless recursions; with pointer types cyclic
1470  *       type graphs are possible.)
1471  */
1472 int equal_type(ir_type *typ1, ir_type *typ2);
1473
1474 /**
1475  *   Checks whether two types are structural comparable.
1476  *
1477  *   @param st pointer type
1478  *   @param lt pointer type
1479  *
1480  *   @return
1481  *    true if type st is smaller than type lt, i.e. whenever
1482  *    lt is expected a st can be used.
1483  *    This is true if
1484  *    - they are the same type kind
1485  *    - mode(st) < mode (lt)  (if applicable)
1486  *    - they are class types and st is (transitive) subtype of lt,
1487  *    - they are structure types and
1488  *       - the members of st have exactly one counterpart in lt with the same name,
1489  *       - the counterpart has a bigger type.
1490  *    - they are method types and have
1491  *      - the same number of parameter and result types,
1492  *      - the parameter types of st are smaller than those of lt,
1493  *      - the result types of st are smaller than those of lt
1494  *    - they are union types and have the members of st have exactly one
1495  *      @return counterpart in lt and the type is smaller
1496  *    - they are array types and have
1497  *      - the same number of dimensions
1498  *      - all bounds of lt are bound of st
1499  *      - the same dimension order
1500  *      - the same element type
1501  *      @return or
1502  *      - the element type of st is smaller than that of lt
1503  *      - the element types have the same size and fixed layout.
1504  *    - they are enumeration types and have the same enumerator names
1505  *    - they are pointer types and have the points_to type of st is
1506  *      @return smaller than the points_to type of lt.
1507  *
1508  */
1509 int smaller_type(ir_type *st, ir_type *lt);
1510
1511 /**
1512  *  @page class_type    Representation of a class type
1513  *
1514  *  If the type opcode is set to type_class the type represents class
1515  *  types.  A list of fields and methods is associated with a class.
1516  *  Further a class can inherit from and bequest to other classes.
1517  *
1518  *  The following attributes are private to this type kind:
1519  *  - member:     All entities belonging to this class.  This are method entities
1520  *                which have type_method or fields that can have any of the
1521  *                following type kinds: type_class, type_struct, type_union,
1522  *                type_array, type_enumeration, type_pointer, type_primitive.
1523  *
1524  *  The following two are dynamic lists that can be grown with an "add_" function,
1525  *  but not shrinked:
1526  *
1527  *  - subtypes:    A list of direct subclasses.
1528  *
1529  *  - supertypes:  A list of direct superclasses.
1530  *
1531  *  - peculiarity: The peculiarity of this class.  If the class is of peculiarity
1532  *                 "description" it only is a description of requirements to a class,
1533  *                 as, e.g., a Java interface.  The class will never be allocated.
1534  *                 Peculiarity inherited is only possible for entities.  An entity
1535  *                 is of peculiarity inherited if the compiler generated the entity
1536  *                 to explicitly resolve inheritance.  An inherited method entity has
1537  *                 no value for irg.
1538  *                 Values: description, existent, inherited.  Default: existent.
1539  *
1540  *  - type_info:   An entity representing the type information of this class.
1541  *                 This entity can be of arbitrari type, Firm did not use it yet.
1542  *                 It allows to express the coupling of a type with an entity
1543  *                 representing this type.  This information is useful for lowering
1544  *                 of InstOf and TypeChk nodes.  Default: NULL
1545  *
1546  *  - vtable_size: The size of this class virtual function table.
1547  *                 Default:  0
1548  *
1549  *  - final:       A final class is always a leaf in the class hierarchy.  Final
1550  *                 classes cannot be super classes of other ones.  As this information
1551  *                 can only be computed in whole world compilations, we allow to
1552  *                 set this flag.  It is used in optimizations if get_opt_closed_world()
1553  *                 is false.  Default:  false
1554  *
1555  *  - interface:   The class represents an interface.  This flag can be set to distinguish
1556  *                 between interfaces, abstract classes and other classes that all may
1557  *                 have the peculiarity peculiarity_description.  Depending on this flag
1558  *                 the lowering might do different actions.  Default:  false
1559  *
1560  *  - abstract :   The class represents an abstract class.  This flag can be set to distinguish
1561  *                 between interfaces, abstract classes and other classes that all may
1562  *                 have the peculiarity peculiarity_description.  Depending on this flag
1563  *                 the lowering might do different actions.  Default:  false
1564  */
1565
1566 /** Creates a new class type. */
1567 ir_type *new_type_class(ident *name);
1568
1569 /** Creates a new class type with debug information. */
1570 ir_type *new_d_type_class(ident *name, dbg_info *db);
1571
1572 /* --- manipulate private fields of class type  --- */
1573
1574 /** Adds the entity as member of the class.  */
1575 void add_class_member(ir_type *clss, ir_entity *member);
1576
1577 /** Returns the number of members of this class. */
1578 int get_class_n_members(const ir_type *clss);
1579
1580 /** Returns the member at position pos, 0 <= pos < n_member */
1581 ir_entity *get_class_member(const ir_type *clss, int pos);
1582
1583 /** Returns index of mem in clss, -1 if not contained. */
1584 int get_class_member_index(const ir_type *clss, ir_entity *mem);
1585
1586 /** Finds the member with name 'name'. If several members with the same
1587  *  name returns one of them.  Returns NULL if no member found. */
1588 ir_entity *get_class_member_by_name(ir_type *clss, ident *name);
1589
1590 /** Overwrites the member at position pos, 0 <= pos < n_member with
1591  *  the passed entity. */
1592 void set_class_member(ir_type *clss, ir_entity *member, int pos);
1593
1594 /** Replaces complete member list in class type by the list passed.
1595  *
1596  *  Copies the list passed. This function is necessary to reduce the number of members.
1597  *  members is an array of entities, num the size of this array.  Sets all
1598  *  owners of the members passed to clss. */
1599 void set_class_members(ir_type *clss, ir_entity *members[], int arity);
1600
1601 /** Finds member in the list of members and removes it.
1602  *
1603  *  Shrinks the member list, so iterate from the end!!!
1604  *  Does not deallocate the entity.  */
1605 void remove_class_member(ir_type *clss, ir_entity *member);
1606
1607
1608 /** Adds subtype as subtype to clss.
1609  *
1610  *  Checks whether clss is a supertype of subtype.  If not
1611  *  adds also clss as supertype to subtype.  */
1612 void add_class_subtype(ir_type *clss, ir_type *subtype);
1613
1614 /** Returns the number of subtypes */
1615 int get_class_n_subtypes(const ir_type *clss);
1616
1617 /** Gets the subtype at position pos, 0 <= pos < n_subtype. */
1618 ir_type *get_class_subtype(ir_type *clss, int pos);
1619
1620 /** Returns the index to access subclass as subtype of class.
1621  *
1622  *  If subclass is no direct subtype of class returns -1.
1623  */
1624 int get_class_subtype_index(ir_type *clss, const ir_type *subclass);
1625
1626 /** Sets the subtype at position pos, 0 <= pos < n_subtype.
1627  *
1628  *  Does not set the corresponding supertype relation for subtype: this might
1629  *  be a different position! */
1630 void set_class_subtype(ir_type *clss, ir_type *subtype, int pos);
1631
1632 /** Finds subtype in the list of subtypes and removes it  */
1633 void remove_class_subtype(ir_type *clss, ir_type *subtype);
1634
1635 /* Convenience macros */
1636 #define add_class_derived_type(clss, drvtype)       add_class_subtype(clss, drvtype)
1637 #define get_class_n_derived_types(clss)             get_class_n_subtypes(clss)
1638 #define get_class_derived_type(clss, pos)           get_class_subtype(clss, pos)
1639 #define get_class_derived_type_index(clss, drvtype) get_class_subtype_index(clss, drvtype)
1640 #define set_class_derived_type(clss, drvtype, pos)  set_class_subtype(clss, drvtype, pos)
1641 #define remove_class_derived_type(clss, drvtype)    remove_class_subtype(clss, drvtype)
1642
1643 /** Adds supertype as supertype to class.
1644  *
1645  *  Checks whether clss is a subtype of supertype.  If not
1646  *  adds also clss as subtype to supertype.  */
1647 void add_class_supertype(ir_type *clss, ir_type *supertype);
1648
1649 /** Returns the number of supertypes */
1650 int get_class_n_supertypes(const ir_type *clss);
1651
1652 /** Returns the index to access superclass as supertype of class.
1653  *
1654  *  If superclass is no direct supertype of class returns -1.
1655  */
1656 int get_class_supertype_index(ir_type *clss, ir_type *super_clss);
1657
1658 /** Gets the supertype at position pos,  0 <= pos < n_supertype. */
1659 ir_type *get_class_supertype(ir_type *clss, int pos);
1660
1661 /** Sets the supertype at position pos, 0 <= pos < n_supertype.
1662  *
1663  *  Does not set the corresponding subtype relation for supertype: this might
1664  *  be at a different position! */
1665 void set_class_supertype(ir_type *clss, ir_type *supertype, int pos);
1666
1667 /** Finds supertype in the list of supertypes and removes it */
1668 void remove_class_supertype(ir_type *clss, ir_type *supertype);
1669
1670 /** Convenience macro */
1671 #define add_class_base_type(clss, basetype)        add_class_supertype(clss, basetype)
1672 #define get_class_n_base_types(clss)               get_class_n_supertypes(clss)
1673 #define get_class_base_type_index(clss, base_clss) get_class_supertype_index(clss, base_clss)
1674 #define get_class_base_type(clss, pos)             get_class_supertype(clss, pos)
1675 #define set_class_base_type(clss, basetype, pos)   set_class_supertype(clss, basetype, pos)
1676 #define remove_class_base_type(clss, basetype)     remove_class_supertype(clss, basetype)
1677
1678 /** Returns a human readable string for a peculiarity. */
1679 const char *get_peculiarity_name(ir_peculiarity p);
1680
1681 /** Returns the peculiarity of the class. */
1682 ir_peculiarity get_class_peculiarity(const ir_type *clss);
1683 /** Sets the peculiarity of the class. */
1684 void set_class_peculiarity(ir_type *clss, ir_peculiarity pec);
1685
1686 /** Returns the type info entity of a class. */
1687 ir_entity *get_class_type_info(const ir_type *clss);
1688
1689 /** Set a type info entity for the class. */
1690 void set_class_type_info(ir_type *clss, ir_entity *ent);
1691
1692 /** Returns the size of the virtual function table. */
1693 unsigned get_class_vtable_size(const ir_type *clss);
1694
1695 /** Sets a new size of the virtual function table. */
1696 void set_class_vtable_size(ir_type *clss, unsigned size);
1697
1698 /** Returns non-zero if a class is final. */
1699 int is_class_final(const ir_type *clss);
1700
1701 /** Sets the class final flag. */
1702 void set_class_final(ir_type *clss, int flag);
1703
1704 /** Return non-zero if a class is an interface */
1705 int is_class_interface(const ir_type *clss);
1706
1707 /** Sets the class interface flag. */
1708 void set_class_interface(ir_type *clss, int flag);
1709
1710 /** Return non-zero if a class is an abstract class. */
1711 int is_class_abstract(const ir_type *clss);
1712
1713 /** Sets the class abstract flag. */
1714 void set_class_abstract(ir_type *clss, int flag);
1715
1716 /** Set and get a class' dfn --
1717    @todo This is an undocumented field, subject to change! */
1718 void set_class_dfn(ir_type *clss, int dfn);
1719 int  get_class_dfn(const ir_type *clss);
1720
1721 /** Returns true if a type is a class type. */
1722 int is_Class_type(const ir_type *clss);
1723
1724 /**
1725  *  @page struct_type   Representation of a struct type
1726  *
1727  *  A struct type represents aggregate types that consist of a list
1728  *  of fields.
1729  *
1730  *  The following attributes are private to this type kind:
1731  *  - member:  All entities belonging to this class.  This are the fields
1732  *             that can have any of the following types:  type_class,
1733  *             type_struct, type_union, type_array, type_enumeration,
1734  *             type_pointer, type_primitive.
1735  *             This is a dynamic list that can be grown with an "add_" function,
1736  *             but not shrinked.
1737  *             This is a dynamic list that can be grown with an "add_" function,
1738  *             but not shrinked.
1739  */
1740 /** Creates a new type struct */
1741 ir_type *new_type_struct(ident *name);
1742 /** Creates a new type struct with debug information. */
1743 ir_type *new_d_type_struct(ident *name, dbg_info* db);
1744
1745 /* --- manipulate private fields of struct --- */
1746
1747 /** Adds the entity as member of the struct.  */
1748 void add_struct_member(ir_type *strct, ir_entity *member);
1749
1750 /** Returns the number of members of this struct. */
1751 int get_struct_n_members(const ir_type *strct);
1752
1753 /** Returns the member at position pos, 0 <= pos < n_member */
1754 ir_entity *get_struct_member(const ir_type *strct, int pos);
1755
1756 /** Returns index of member in strct, -1 if not contained. */
1757 int get_struct_member_index(const ir_type *strct, ir_entity *member);
1758
1759 /** Overwrites the member at position pos, 0 <= pos < n_member with
1760    the passed entity. */
1761 void set_struct_member(ir_type *strct, int pos, ir_entity *member);
1762
1763 /** Finds member in the list of members and removes it. */
1764 void remove_struct_member(ir_type *strct, ir_entity *member);
1765
1766 /** Returns true if a type is a struct type. */
1767 int is_Struct_type(const ir_type *strct);
1768
1769 /**
1770  * @page method_type    Representation of a method type
1771  *
1772  * A method type represents a method, function or procedure type.
1773  * It contains a list of the parameter and result types, as these
1774  * are part of the type description.  These lists should not
1775  * be changed by a optimization, as a change creates a new method
1776  * type.  Therefore optimizations should allocated new method types.
1777  * The set_ routines are only for construction by a frontend.
1778  *
1779  * - n_params:   Number of parameters to the procedure.
1780  *               A procedure in FIRM has only call by value parameters.
1781  *
1782  * - param_type: A list with the types of parameters.  This list is ordered.
1783  *               The nth type in this list corresponds to the nth element
1784  *               in the parameter tuple that is a result of the start node.
1785  *               (See ircons.h for more information.)
1786  *
1787  * - value_param_ents
1788  *               A list of entities (whose owner is a struct private to the
1789  *               method type) that represent parameters passed by value.
1790  *
1791  * - n_res:      The number of results of the method.  In general, procedures
1792  *               have zero results, functions one.
1793  *
1794  * - res_type:   A list with the types of parameters.  This list is ordered.
1795  *               The nth type in this list corresponds to the nth input to
1796  *               Return nodes.  (See ircons.h for more information.)
1797  *
1798  * - value_res_ents
1799  *               A list of entities (whose owner is a struct private to the
1800  *               method type) that represent results passed by value.
1801  */
1802
1803 /* These macros define the suffixes for the types and entities used
1804    to represent value parameters / results. */
1805 #define VALUE_PARAMS_SUFFIX  "val_param"
1806 #define VALUE_RESS_SUFFIX    "val_res"
1807
1808 /** Create a new method type.
1809  *
1810  * @param name      the name (ident) of this type
1811  * @param n_param   the number of parameters
1812  * @param n_res     the number of results
1813  *
1814  * The arrays for the parameter and result types are not initialized by
1815  * the constructor.
1816  */
1817 ir_type *new_type_method(ident *name, int n_param, int n_res);
1818
1819 /** Create a new method type with debug information.
1820  *
1821  * @param name      the name (ident) of this type
1822  * @param n_param   the number of parameters
1823  * @param n_res     the number of results
1824  * @param db        user defined debug information
1825  *
1826  * The arrays for the parameter and result types are not initialized by
1827  * the constructor.
1828  */
1829 ir_type *new_d_type_method(ident *name, int n_param, int n_res, dbg_info *db);
1830
1831 /** Clone an existing method type.
1832  *
1833  * @param tp      the method type to clone.
1834  * @param prefix  if non-null, will be the prefix for the name of
1835  *                the cloned type
1836  *
1837  * @return the cloned method type.
1838  */
1839 ir_type *clone_type_method(ir_type *tp, ident *prefix);
1840
1841 /* -- manipulate private fields of method. -- */
1842
1843 /** Returns the number of parameters of this method. */
1844 int get_method_n_params(const ir_type *method);
1845
1846 /** Returns the type of the parameter at position pos of a method. */
1847 ir_type *get_method_param_type(ir_type *method, int pos);
1848 /** Sets the type of the parameter at position pos of a method.
1849     Also changes the type in the pass-by-value representation by just
1850     changing the type of the corresponding entity if the representation is constructed. */
1851 void set_method_param_type(ir_type *method, int pos, ir_type *tp);
1852 /** Returns an entity that represents the copied value argument.  Only necessary
1853    for compounds passed by value. This information is constructed only on demand. */
1854 ir_entity *get_method_value_param_ent(ir_type *method, int pos);
1855 /**
1856  * Returns a type that represents the copied value arguments if one
1857  * was allocated, else NULL.
1858  */
1859 ir_type *get_method_value_param_type(const ir_type *method);
1860 /** Returns an ident representing the parameters name. Returns NULL if not set.
1861     For debug support only. */
1862 ident *get_method_param_ident(ir_type *method, int pos);
1863 /** Returns a string representing the parameters name. Returns NULL if not set.
1864     For debug support only. */
1865 const char *get_method_param_name(ir_type *method, int pos);
1866 /** Sets an ident representing the parameters name. For debug support only. */
1867 void set_method_param_ident(ir_type *method, int pos, ident *id);
1868
1869 /** Returns the number of results of a method type. */
1870 int get_method_n_ress(const ir_type *method);
1871 /** Returns the return type of a method type at position pos. */
1872 ir_type *get_method_res_type(ir_type *method, int pos);
1873 /** Sets the type of the result at position pos of a method.
1874     Also changes the type in the pass-by-value representation by just
1875     changing the type of the corresponding entity if the representation is constructed. */
1876 void set_method_res_type(ir_type *method, int pos, ir_type *tp);
1877 /** Returns an entity that represents the copied value result.  Only necessary
1878    for compounds passed by value. This information is constructed only on demand. */
1879 ir_entity *get_method_value_res_ent(ir_type *method, int pos);
1880
1881 /**
1882  * Returns a type that represents the copied value results.
1883  */
1884 ir_type *get_method_value_res_type(const ir_type *method);
1885
1886 /**
1887  * This enum flags the variadicity of methods (methods with a
1888  * variable amount of arguments (e.g. C's printf). Default is
1889  * non_variadic.
1890  */
1891 typedef enum variadicity {
1892   variadicity_non_variadic, /**< non variadic */
1893   variadicity_variadic      /**< variadic */
1894 } variadicity;
1895
1896 /** Returns the null-terminated name of this variadicity. */
1897 const char *get_variadicity_name(variadicity vari);
1898
1899 /** Returns the variadicity of a method. */
1900 variadicity get_method_variadicity(const ir_type *method);
1901
1902 /** Sets the variadicity of a method. */
1903 void set_method_variadicity(ir_type *method, variadicity vari);
1904
1905 /**
1906  * Returns the first variadic parameter index of a type.
1907  * If this index was NOT set, the index of the last parameter
1908  * of the method type plus one is returned for variadic functions.
1909  * Non-variadic function types always return -1 here.
1910  */
1911 int get_method_first_variadic_param_index(const ir_type *method);
1912
1913 /**
1914  * Sets the first variadic parameter index. This allows to specify
1915  * a complete call type (containing the type of all parameters)
1916  * but still have the knowledge, which parameter must be passed as
1917  * variadic one.
1918  */
1919 void set_method_first_variadic_param_index(ir_type *method, int index);
1920
1921 /** Returns the mask of the additional graph properties. */
1922 unsigned get_method_additional_properties(const ir_type *method);
1923
1924 /** Sets the mask of the additional graph properties. */
1925 void set_method_additional_properties(ir_type *method, unsigned property_mask);
1926
1927 /** Sets one additional graph property. */
1928 void set_method_additional_property(ir_type *method, mtp_additional_property flag);
1929
1930 /**
1931  * Calling conventions: lower 24 bits are the number of register parameters,
1932  * upper 8 encode the calling conventions.
1933  */
1934 typedef enum {
1935         cc_reg_param           = 0x01000000, /**< Transmit parameters in registers, else the stack is used.
1936                                                   This flag may be set as default on some architectures. */
1937         cc_last_on_top         = 0x02000000, /**< The last non-register parameter is transmitted on top of
1938                                                   the stack. This is equivalent to the pascal
1939                                                   calling convention. If this flag is not set, the first
1940                                                   non-register parameter is used (stdcall or cdecl
1941                                                   calling convention) */
1942         cc_callee_clear_stk    = 0x04000000, /**< The callee clears the stack. This forbids variadic
1943                                                   function calls (stdcall). */
1944         cc_this_call           = 0x08000000, /**< The first parameter is a this pointer and is transmitted
1945                                                   in a special way. */
1946         cc_compound_ret        = 0x10000000, /**< The method returns a compound type. */
1947         cc_frame_on_caller_stk = 0x20000000, /**< The method did not allocate an own stack frame, instead the
1948                                                   caller must reserve size on its own stack. */
1949         cc_fpreg_param         = 0x40000000, /**< Transmit floating point parameters in registers, else the stack is used. */
1950         cc_bits                = (0xFF << 24)/**< The calling convention bits. */
1951 } calling_convention;
1952
1953 /* some often used cases: made as defines because firmjni cannot handle two
1954    equal enum values. */
1955
1956 /** cdecl calling convention */
1957 #define cc_cdecl_set    (0)
1958 /** stdcall calling convention */
1959 #define cc_stdcall_set  cc_callee_clear_stk
1960 /** fastcall calling convention */
1961 #define cc_fastcall_set (cc_reg_param|cc_callee_clear_stk)
1962
1963 /** Returns the default calling convention for method types. */
1964 unsigned get_default_cc_mask(void);
1965
1966 /**
1967  * check for the CDECL calling convention
1968  */
1969 #define IS_CDECL(cc_mask)     (((cc_mask) & cc_bits) == cc_cdecl_set)
1970
1971 /**
1972  * check for the STDCALL calling convention
1973  */
1974 #define IS_STDCALL(cc_mask)   (((cc_mask) & cc_bits) == cc_stdcall_set)
1975
1976 /**
1977  * check for the FASTCALL calling convention
1978  */
1979 #define IS_FASTCALL(cc_mask)  (((cc_mask) & cc_bits) == cc_fastcall_set)
1980
1981 /**
1982  * Sets the CDECL convention bits.
1983  */
1984 #define SET_CDECL(cc_mask)    (((cc_mask) & ~cc_bits) | cc_cdecl_set)
1985
1986 /**
1987  * Set. the STDCALL convention bits.
1988  */
1989 #define SET_STDCALL(cc_mask)  (((cc_mask) & ~cc_bits) | cc_stdcall_set)
1990
1991 /**
1992  * Sets the FASTCALL convention bits.
1993  */
1994 #define SET_FASTCALL(cc_mask) (((cc_mask) & ~cc_bits) | cc_fastcall_set)
1995
1996 /** Returns the calling convention of an entities graph. */
1997 unsigned get_method_calling_convention(const ir_type *method);
1998
1999 /** Sets the calling convention of an entities graph. */
2000 void set_method_calling_convention(ir_type *method, unsigned cc_mask);
2001
2002 /** Returns the number of registers parameters, 0 means default. */
2003 unsigned get_method_n_regparams(ir_type *method);
2004
2005 /** Sets the number of registers parameters, 0 means default. */
2006 void set_method_n_regparams(ir_type *method, unsigned n_regs);
2007
2008 /** Returns true if a type is a method type. */
2009 int is_Method_type(const ir_type *method);
2010
2011 /**
2012  *   @page union_type   Representation of a union (variant) type.
2013  *
2014  *   The union type represents union types.  Note that this representation
2015  *   resembles the C union type.  For tagged variant types like in Pascal or Modula
2016  *   a combination of a struct and a union type must be used.
2017  *
2018  *   - n_types:     Number of unioned types.
2019  *   - members:     Entities for unioned types.  Fixed length array.
2020  *                  This is a dynamic list that can be grown with an "add_" function,
2021  *                  but not shrinked.
2022  */
2023 /** Creates a new type union. */
2024 ir_type *new_type_union(ident *name);
2025
2026 /** Creates a new type union with debug information. */
2027 ir_type *new_d_type_union(ident *name, dbg_info* db);
2028
2029 /* --- manipulate private fields of struct --- */
2030
2031 /** Returns the number of unioned types of this union */
2032 int get_union_n_members(const ir_type *uni);
2033
2034 /** Adds a new entity to a union type */
2035 void add_union_member(ir_type *uni, ir_entity *member);
2036
2037 /** Returns the entity at position pos of a union */
2038 ir_entity *get_union_member(const ir_type *uni, int pos);
2039
2040 /** Returns index of member in uni, -1 if not contained. */
2041 int get_union_member_index(const ir_type *uni, ir_entity *member);
2042
2043 /** Overwrites a entity at position pos in a union type. */
2044 void set_union_member(ir_type *uni, int pos, ir_entity *member);
2045
2046 /** Finds member in the list of members and removes it. */
2047 void remove_union_member(ir_type *uni, ir_entity *member);
2048
2049 /** Returns true if a type is a union type. */
2050 int is_Union_type(const ir_type *uni);
2051
2052 /**
2053  * @page array_type Representation of an array type
2054  *
2055  * The array type represents rectangular multi dimensional arrays.
2056  * The constants representing the bounds must be allocated to
2057  * get_const_code_irg() by setting current_ir_graph accordingly.
2058  *
2059  * - n_dimensions:    Number of array dimensions.
2060  * - *lower_bound:    Lower bounds of dimensions.  Usually all 0.
2061  * - *upper_bound:    Upper bounds or dimensions.
2062  * - *element_type:   The type of the array elements.
2063  * - *element_ent:    An entity for the array elements to be used for
2064  *                      element selection with Sel.
2065  * @todo
2066  *   Do we need several entities?  One might want
2067  *   to select a dimension and not a single element in case of multi
2068  *   dimensional arrays.
2069  */
2070
2071 /** Create a new type array.
2072  *
2073  * Sets n_dimension to dimension and all dimension entries to NULL.
2074  * Initializes order to the order of the dimensions.
2075  * The entity for array elements is built automatically.
2076  * Set dimension sizes after call to constructor with set_* routines.
2077  */
2078 ir_type *new_type_array(ident *name, int n_dims, ir_type *element_type);
2079
2080 /** Create a new type array with debug information.
2081  *
2082  * Sets n_dimension to dimension and all dimension entries to NULL.
2083  * Initializes order to the order of the dimensions.
2084  * The entity for array elements is built automatically.
2085  * Set dimension sizes after call to constructor with set_* routines.
2086  * A legal array type must have at least one dimension set.
2087  */
2088 ir_type *new_d_type_array(ident *name, int n_dims, ir_type *element_type, dbg_info* db);
2089
2090 /* --- manipulate private fields of array type --- */
2091
2092 /** Returns the number of array dimensions of this type. */
2093 int get_array_n_dimensions(const ir_type *array);
2094
2095 /**
2096  * Allocates Const nodes of mode_Is for one array dimension.
2097  * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
2098  */
2099 void set_array_bounds_int(ir_type *array, int dimension, int lower_bound,
2100                                                          int upper_bound);
2101 /**
2102  * Sets the bounds for one array dimension.
2103  * Upper bound in Firm is the element next to the last, i.e. [lower,upper[
2104  */
2105 void set_array_bounds(ir_type *array, int dimension, ir_node *lower_bound,
2106                                                      ir_node *upper_bound);
2107 /** Sets the lower bound for one array dimension, i.e. [lower,upper[ */
2108 void set_array_lower_bound(ir_type *array, int dimension, ir_node *lower_bound);
2109
2110 /** Allocates Const nodes of mode_Is for the lower bound of an array
2111     dimension, i.e. [lower,upper[ */
2112 void set_array_lower_bound_int(ir_type *array, int dimension, int lower_bound);
2113
2114 /** Sets the upper bound for one array dimension, i.e. [lower,upper[ */
2115 void set_array_upper_bound(ir_type *array, int dimension, ir_node *upper_bound);
2116
2117 /** Allocates Const nodes of mode_Is for the upper bound of an array
2118     dimension, i.e. [lower,upper[. */
2119 void set_array_upper_bound_int(ir_type *array, int dimension, int upper_bound);
2120
2121 /** Returns true if lower bound != Unknown. */
2122 int has_array_lower_bound(const ir_type *array, int dimension);
2123 /** Returns the lower bound of an array. */
2124 ir_node *get_array_lower_bound(const ir_type *array, int dimension);
2125 /** Works only if bound is Const node with tarval that can be converted to long. */
2126 long get_array_lower_bound_int(const ir_type *array, int dimension);
2127 /** returns true if lower bound != Unknown */
2128 int has_array_upper_bound(const ir_type *array, int dimension);
2129 /** Returns the upper bound of an array. */
2130 ir_node *get_array_upper_bound(const ir_type *array, int dimension);
2131 /** Works only if bound is Const node with tarval that can be converted to long. */
2132 long get_array_upper_bound_int(const ir_type *array, int dimension);
2133
2134 /** Sets an array dimension to a specific order. */
2135 void set_array_order(ir_type *array, int dimension, int order);
2136
2137 /** Returns the order of an array dimension. */
2138 int get_array_order(const ir_type *array, int dimension);
2139
2140 /** Find the array dimension that is placed at order order. */
2141 int find_array_dimension(const ir_type *array, int order);
2142
2143 /** Sets the array element type. */
2144 void set_array_element_type(ir_type *array, ir_type* tp);
2145
2146 /** Gets the array element type. */
2147 ir_type *get_array_element_type(ir_type *array);
2148
2149 /** Sets the array element entity. */
2150 void set_array_element_entity(ir_type *array, ir_entity *ent);
2151
2152 /** Get the array element entity. */
2153 ir_entity *get_array_element_entity(const ir_type *array);
2154
2155 /** Returns true if a type is an array type. */
2156 int is_Array_type(const ir_type *array);
2157
2158 /**
2159  * @page enumeration_type   Representation of an enumeration type
2160  *
2161  * Enumeration types need not necessarily be represented explicitly
2162  * by Firm types, as the frontend can lower them to integer constants as
2163  * well.  For debugging purposes or similar tasks this information is useful.
2164  * The type state layout_fixed is set, if all enumeration constant have
2165  * there tarvals assigned.  Until then
2166  *
2167  * - *const:        The target values representing the constants used to
2168  *                  represent individual enumerations.
2169  */
2170
2171 /** Create a new type enumeration -- set the enumerators independently. */
2172 ir_type *new_type_enumeration(ident *name, int n_enums);
2173
2174 /** Create a new type enumeration with debug information -- set the enumerators independently. */
2175 ir_type *new_d_type_enumeration(ident *name, int n_enums, dbg_info *db);
2176
2177 /* --- manipulate fields of enumeration type. --- */
2178
2179 /** Set an enumeration constant to a enumeration type at a given position. */
2180 void set_enumeration_const(ir_type *enumeration, int pos, ident *nameid, tarval *con);
2181
2182 /** Returns the number of enumeration values of this enumeration */
2183 int get_enumeration_n_enums(const ir_type *enumeration);
2184
2185 /** Returns the enumeration constant at a given position. */
2186 ir_enum_const *get_enumeration_const(const ir_type *enumeration, int pos);
2187
2188 /** Returns the enumeration type owner of an enumeration constant. */
2189 ir_type *get_enumeration_owner(const ir_enum_const *enum_cnst);
2190
2191 /** Sets the enumeration constant value. */
2192 void set_enumeration_value(ir_enum_const *enum_cnst, tarval *con);
2193
2194 /** Returns the enumeration constant value. */
2195 tarval *get_enumeration_value(const ir_enum_const *enum_cnst);
2196
2197 /** Assign an ident to an enumeration constant. */
2198 void set_enumeration_nameid(ir_enum_const *enum_cnst, ident *id);
2199
2200 /** Returns the assigned ident of an enumeration constant. */
2201 ident *get_enumeration_nameid(const ir_enum_const *enum_cnst);
2202
2203 /** Returns the assigned name of an enumeration constant. */
2204 const char *get_enumeration_name(const ir_enum_const *enum_cnst);
2205
2206 /** Returns true if a type is a enumeration type. */
2207 int is_Enumeration_type(const ir_type *enumeration);
2208
2209 /**
2210  * @page pointer_type   Representation of a pointer type
2211  *
2212  * The mode of the pointer type must be a reference mode.
2213  *
2214  * Pointer types:
2215  * - points_to:      The type of the entity this pointer points to.
2216  */
2217
2218 /** Creates a new type pointer. */
2219 ir_type *new_type_pointer(ident *name, ir_type *points_to, ir_mode *ptr_mode);
2220
2221 /** Creates a new type pointer with debug information. */
2222 ir_type *new_d_type_pointer(ident *name, ir_type *points_to, ir_mode *ptr_mode, dbg_info* db);
2223
2224 /* --- manipulate fields of type_pointer --- */
2225
2226 /** Sets the type to which a pointer points to. */
2227 void  set_pointer_points_to_type(ir_type *pointer, ir_type *tp);
2228
2229 /** Returns the type to which a pointer points to. */
2230 ir_type *get_pointer_points_to_type(ir_type *pointer);
2231
2232 /** Returns true if a type is a pointer type. */
2233 int is_Pointer_type(const ir_type *pointer);
2234
2235 /** Returns the first pointer type that has as points_to tp.
2236  *  Not efficient: O(#types).
2237  *  If not found returns firm_unknown_type. */
2238 ir_type *find_pointer_type_to_type(ir_type *tp);
2239
2240 /**
2241  * @page primitive_type Representation of a primitive type
2242  *
2243  * Primitive types are types that represent atomic data values that
2244  * map directly to modes.  They don't have private attributes.  The
2245  * important information they carry is held in the common mode field.
2246  */
2247 /** Creates a new primitive type. */
2248 ir_type *new_type_primitive(ident *name, ir_mode *mode);
2249
2250 /** Creates a new primitive type with debug information. */
2251 ir_type *new_d_type_primitive(ident *name, ir_mode *mode, dbg_info* db);
2252
2253 /** Returns true if a type is a primitive type. */
2254 int is_Primitive_type(const ir_type *primitive);
2255
2256 /** Return the base type of a primitive (bitfield) type or NULL if none. */
2257 ir_type *get_primitive_base_type(ir_type *tp);
2258
2259 /** Sets the base type of a primitive (bitfield) type. */
2260 void set_primitive_base_type(ir_type *tp, ir_type *base_tp);
2261
2262 /**
2263  * @page none_type The None type
2264  *
2265  *  This type is an auxiliary type dedicated to support type analyses.
2266  *
2267  *  The none type represents that there is no type.  The type can be used to
2268  *  initialize fields of type* that actually can not contain a type or that
2269  *  are initialized for an analysis. There exists exactly one type none.
2270  *  This type is not on the type list in ir_prog. It is
2271  *  allocated when initializing the type module.
2272  *
2273  *  The following values are set:
2274  *    - mode:  mode_BAD
2275  *    - name:  "type_none"
2276  *    - state: layout_fixed
2277  *    - size:  0
2278  */
2279 /** A variable that contains the only none type. */
2280 extern ir_type *firm_none_type;
2281
2282 /** Returns the none type. */
2283 ir_type *get_none_type(void);
2284
2285 /**
2286  * @page unknown_type  The Unknown type
2287  *
2288  *  This type is an auxiliary type dedicated to support type analyses.
2289  *
2290  *  The unknown type represents that there could be a type, but it is not
2291  *  known.  This type can be used to initialize fields before an analysis (not known
2292  *  yet) or to represent the top of a lattice (could not be determined).  There exists
2293  *  exactly one type unknown. This type is not on the type list in ir_prog.  It is
2294  *  allocated when initializing the type module.
2295  *
2296  *  The following values are set:
2297  *    - mode:  mode_ANY
2298  *    - name:  "type_unknown"
2299  *    - state: layout_fixed
2300  *    - size:  0
2301  */
2302 /** A variable that contains the only unknown type. */
2303 extern ir_type *firm_unknown_type;
2304
2305 /** Returns the unknown type. */
2306 ir_type *get_unknown_type(void);
2307
2308
2309 /**
2310  *  Checks whether a type is atomic.
2311  *  @param tp   any type
2312  *  @return true if type is primitive, pointer or enumeration
2313  */
2314 int is_atomic_type(const ir_type *tp);
2315
2316 /* --- Support for compound types --- */
2317
2318 /**
2319  * Gets the number of elements in a Firm compound type.
2320  *
2321  * This is just a comfortability function, because structs and
2322  * classes can often be treated be the same code, but they have
2323  * different access functions to their members.
2324  *
2325  * @param tp  The type (must be struct, union or class).
2326  *
2327  * @return Number of members in the compound type.
2328  */
2329 int get_compound_n_members(const ir_type *tp);
2330
2331 /**
2332  * Gets the member of a Firm compound type at position pos.
2333  *
2334  * @param tp  The type (must be struct, union or class).
2335  * @param pos The number of the member.
2336  *
2337  * @return The member entity at position pos.
2338  *
2339  * @see get_compound_n_members() for justification of existence.
2340  */
2341 ir_entity *get_compound_member(const ir_type *tp, int pos);
2342
2343 /** Returns index of member in tp, -1 if not contained. */
2344 int get_compound_member_index(const ir_type *tp, ir_entity *member);
2345
2346 /**
2347  * Checks whether a type is a compound type.
2348  *
2349  * @param tp - any type
2350  *
2351  * @return true if the type is class, structure, union or array type.
2352  */
2353 int is_compound_type(const ir_type *tp);
2354
2355 /**
2356  * Checks, whether a type is a frame type.
2357  */
2358 int is_frame_type(const ir_type *tp);
2359
2360 /**
2361  * Checks, whether a type is a value parameter type.
2362  */
2363 int is_value_param_type(const ir_type *tp);
2364
2365 /**
2366  * Checks, whether a type is a lowered type.
2367  */
2368 int is_lowered_type(const ir_type *tp);
2369
2370 /**
2371  * Makes a new frame type. Frame types are class types,
2372  * so all class access functions work.
2373  * Frame types are not in the global list of types.
2374  */
2375 ir_type *new_type_frame(ident *name);
2376
2377 /**
2378  * Makes a clone of a frame type.
2379  * Sets entity links from old frame entities to new onces and
2380  * vice versa.
2381  */
2382 ir_type *clone_frame_type(ir_type *type);
2383
2384 /**
2385  * Sets a lowered type for a type. This sets both associations
2386  * and marks lowered_type as a "lowered" one.
2387  */
2388 void set_lowered_type(ir_type *tp, ir_type *lowered_type);
2389
2390 /**
2391  * Gets the lowered/unlowered type of a type or NULL if this type
2392  * has no lowered/unlowered one.
2393  */
2394 ir_type *get_associated_type(const ir_type *tp);
2395
2396 /**
2397  * Allocate an area of size bytes aligned at alignment
2398  * at the start or the end of a frame type.
2399  * The frame type must already have a fixed layout.
2400  *
2401  * @param frame_type a frame type
2402  * @param size       the size of the entity
2403  * @param alignment  the alignment of the entity
2404  * @param at_start   if true, put the area at the frame type's start, else at end
2405  *
2406  * @return the entity representing the area
2407  */
2408 ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment, int at_start);
2409
2410 /*-----------------------------------------------------------------*/
2411 /** Debug aides                                                   **/
2412 /*-----------------------------------------------------------------*/
2413
2414 /**
2415  *  Outputs a unique number for this type if libfirm is compiled for
2416  *  debugging, (configure with --enable-debug) else returns the address
2417  *  of the type cast to long.
2418  */
2419 long get_type_nr(const ir_type *tp);
2420
2421 /* ------------------------------------------------------------------------ */
2422
2423 /**  Type for a function that compares two types.
2424  *
2425  *   @param tp1  The first type to compare.
2426  *   @param tp2  The second type to compare.
2427  */
2428 typedef int (compare_types_func_t)(const void *tp1, const void *tp2);
2429
2430 /** Compares two types by their name.
2431  *
2432  * Compares the opcode and the name of the types. If these are
2433  * equal returns 0, else non-zero.
2434  */
2435 int compare_names(const void *tp1, const void *tp2);
2436
2437 /** Compares two types strict.
2438  *
2439  * returns 0 if tp1 == tp2, else non-zero
2440  */
2441 int compare_strict(const void *tp1, const void *tp2);
2442
2443 /* ------------------------------------------------------------------------ */
2444
2445 /**  Type for a function that computes a hash value for a type.
2446  *
2447  *   @param tp The type to compute a hash for.
2448  */
2449 typedef int (hash_types_func_t)(ir_type *tp);
2450
2451 /** Computes a hash value by the type name.
2452  *
2453  * Uses the name of the type and the type opcode to compute the hash.
2454  */
2455 int firm_hash_name(ir_type *tp);
2456
2457 /* ------------------------------------------------------------------------ */
2458
2459 /** Finalize type construction.
2460  *
2461  * Indicate that a type is so far completed that it can be
2462  * distinguished from other types.  Mature_type hashes the type into a
2463  * table.  It uses the function in compare_types_func to compare the
2464  * types.
2465  *
2466  * If it finds a type identical to tp it returns this type.  It turns
2467  * tp into the Id type.  All places formerly pointing to tp will now
2468  * point to the found type.  All entities of tp now refer to the found
2469  * type as their owner, but they are not a member of this type.  This
2470  * is invalid firm -- the entities must be replaced by entities of the
2471  * found type.  The Id type will be removed from the representation
2472  * automatically, but within an unknown time span.  It occupies memory
2473  * for this time.
2474  *
2475  * @param tp     The type to mature.
2476  */
2477 ir_type *mature_type(ir_type *tp);
2478
2479 /** Finalize type construction.
2480  *
2481  * Indicate that a type is so far completed that it can be
2482  * distinguished from other types.  mature_type() hashes the type into a
2483  * table.  It uses the function in compare_types_func to compare the
2484  * types.
2485  *
2486  * If it finds a type identical to tp it returns this type.  It frees
2487  * type tp and all its entities.
2488  *
2489  * @param tp     The type to mature.
2490  */
2491 ir_type *mature_type_free(ir_type *tp);
2492
2493 /** Finalize type construction.
2494  *
2495  * Indicate that a type is so far completed that it can be
2496  * distinguished from other types.  Mature_type hashes the type into a
2497  * table.  It uses the function in compare_types_func to compare the
2498  * types.
2499  *
2500  * If it find a type identical to tp it returns this type.  It frees
2501  * the entities and turns the type into an Id type.  All places
2502  * formerly pointing to tp will now point to the found type.  The Id
2503  * type will be removed from the representation automatically, but
2504  * within an unknown time span.  It occupies memory for this time.
2505  *
2506  * @param tp     The type to mature.
2507  */
2508 ir_type *mature_type_free_entities(ir_type *tp);
2509
2510 /**
2511  * The interface type for the type identify module;
2512  */
2513 struct type_identify_if_t {
2514         compare_types_func_t *cmp;    /**< The function that should be used to compare two types.
2515                                            If NULL, compare_strict() will be used. */
2516         hash_types_func_t *hash;      /**< The function that should be used to calculate a hash
2517                                            value of a type. If NULL, hash_name() will be used. */
2518 };
2519
2520 /**
2521  * Initialise the type identifier module.
2522  *
2523  * @param ti_if    The interface functions for this module.
2524  *
2525  * If the parameter ti_if is NULL, the default functions compare_strict() and
2526  * firm_hash_name() will be used.
2527  */
2528 void init_type_identify(type_identify_if_t *ti_if);
2529
2530 /** A data type to treat types and entities as the same. */
2531 typedef union {
2532         ir_type   *typ;   /**< points to a type */
2533         ir_entity *ent;   /**< points to an entity */
2534 } type_or_ent;
2535
2536 /** Type of argument functions for type walkers.
2537  *
2538  * @param tore    points to the visited type or entity
2539  * @param env     free environment pointer
2540  */
2541 typedef void type_walk_func(type_or_ent tore, void *env);
2542
2543 /**  The class walk function
2544  *
2545  * @param clss    points to the visited class
2546  * @param env     free environment pointer
2547  */
2548 typedef void class_walk_func(ir_type *clss, void *env);
2549
2550 /** Touches every type and entity in unspecified order.  If new
2551  *  types/entities are created during the traversal these will
2552  *  be visited, too.
2553  *  Does not touch frame types or types for value params ... */
2554 void type_walk(type_walk_func *pre, type_walk_func *post, void *env);
2555
2556 /** Walks over all type information reachable from an ir graph.
2557  *
2558  *  Walks over all type information reachable from irg, i.e., starts a
2559  *  type walk at the irgs entity, the irgs frame type and all types and
2560  *  entities that are attributes to firm nodes. */
2561 void type_walk_irg(ir_graph *irg, type_walk_func *pre, type_walk_func *post,
2562                    void *env);
2563
2564 /**
2565     Touches every class in specified order:
2566     - first the super class
2567     - second the class itself
2568     - third the sub classes.  If new classes are created
2569     during the traversal these will be visited, too.
2570
2571     @todo should be named class-walk
2572
2573     @deprecated will be removed?
2574 */
2575 void type_walk_super2sub(type_walk_func *pre, type_walk_func *post, void *env);
2576
2577 /** Walker for class types in inheritance order.
2578  *
2579  *  Touches every class in specified order:
2580  *   - first the super class
2581  *   - second the class itself
2582  *   If new classes are created during the traversal these
2583  *   will be visited, too.
2584  * Starts the walk at arbitrary classes.
2585  * Executes pre when first visiting a class.  Executes post after
2586  * visiting all superclasses.
2587  *
2588  * The arguments pre, post, env may be NULL. */
2589 void type_walk_super(type_walk_func *pre, type_walk_func *post, void *env);
2590
2591 /** Same as type_walk_super2sub, but visits only class types.
2592    Executes pre for a class if all superclasses have been visited.
2593    Then iterates to subclasses.  Executes post after return from
2594    subclass.
2595    Does not visit global type, frame types.
2596
2597    @bug ?? something is wrong with this.
2598 */
2599 void class_walk_super2sub(class_walk_func *pre, class_walk_func *post,
2600                           void *env);
2601
2602 /**
2603  * the entity walk function.  A function type for entity walkers.
2604  *
2605  * @param ent     points to the visited entity
2606  * @param env     free environment pointer
2607  */
2608 typedef void entity_walk_func(ir_entity *ent, void *env);
2609
2610 /**
2611  * Walks over all entities in the type.
2612  *
2613  * @param tp    the type
2614  * @param doit  the entity walker function
2615  * @param env   environment, will be passed to the walker function
2616  */
2617 void walk_types_entities(ir_type *tp, entity_walk_func *doit, void *env);
2618
2619 /**
2620  * If we have the closed world assumption, we can calculate the
2621  * finalization of classes and entities by inspecting the class hierarchy.
2622  * After this is done, all classes and entities that are not overridden
2623  * anymore have the final property set.
2624  */
2625 void types_calc_finalization(void);
2626
2627 #endif