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