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