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