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