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