convert opts to use the opt_manage framework
[libfirm] / ir / tr / entity.c
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   Representation of all program known entities.
23  * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stddef.h>
31
32 #include "xmalloc.h"
33 #include "entity_t.h"
34 #include "array.h"
35 #include "irtools.h"
36 #include "irhooks.h"
37 #include "irprintf.h"
38
39 #include "irprog_t.h"
40 #include "ircons.h"
41 #include "tv_t.h"
42 #include "irdump.h"
43 #include "irgraph_t.h"
44 #include "callgraph.h"
45 #include "error.h"
46 #include "compound_path.h"
47
48 /*-----------------------------------------------------------------*/
49 /** general                                                       **/
50 /*-----------------------------------------------------------------*/
51
52 ir_entity *unknown_entity = NULL;
53
54 ir_entity *get_unknown_entity(void) { return unknown_entity; }
55
56 /** The name of the unknown entity. */
57 #define UNKNOWN_ENTITY_NAME "unknown_entity"
58
59 /*-----------------------------------------------------------------*/
60 /* ENTITY                                                          */
61 /*-----------------------------------------------------------------*/
62
63 static ir_entity *intern_new_entity(ir_type *owner, ident *name, ir_type *type,
64                                     dbg_info *dbgi)
65 {
66         ir_entity *res;
67
68         res = XMALLOCZ(ir_entity);
69
70         res->kind    = k_entity;
71         res->name    = name;
72         res->ld_name = NULL;
73         res->type    = type;
74         res->owner   = owner;
75
76         res->volatility           = volatility_non_volatile;
77         res->aligned              = align_is_aligned;
78         res->usage                = ir_usage_unknown;
79         res->compiler_gen         = 0;
80         res->visibility           = ir_visibility_default;
81         res->offset               = -1;
82         res->offset_bit_remainder = 0;
83         res->alignment            = 0;
84         res->link                 = NULL;
85         res->repr_class           = NULL;
86 #ifdef DEBUG_libfirm
87         res->nr = get_irp_new_node_nr();
88 #endif
89
90         /* Remember entity in its owner. */
91         if (owner != NULL)
92                 add_compound_member(owner, res);
93
94         res->visit = 0;
95         set_entity_dbg_info(res, dbgi);
96
97         return res;
98 }
99
100 ir_entity *new_d_entity(ir_type *owner, ident *name, ir_type *type,
101                         dbg_info *db)
102 {
103         ir_entity *res = intern_new_entity(owner, name, type, db);
104
105         if (is_Method_type(type)) {
106                 ir_graph *irg = get_const_code_irg();
107                 symconst_symbol sym;
108                 ir_mode *mode = is_Method_type(type) ? mode_P_code : mode_P_data;
109                 sym.entity_p            = res;
110                 set_atomic_ent_value(res, new_r_SymConst(irg, mode, sym, symconst_addr_ent));
111                 res->linkage            = IR_LINKAGE_CONSTANT;
112                 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
113                 res->attr.mtd_attr.vtable_number      = IR_VTABLE_NUM_NOT_SET;
114                 res->attr.mtd_attr.param_access       = NULL;
115                 res->attr.mtd_attr.param_weight       = NULL;
116                 res->attr.mtd_attr.irg                = NULL;
117         } else if (is_compound_type(type)) {
118                 res->attr.cmpd_attr.values    = NULL;
119                 res->attr.cmpd_attr.val_paths = NULL;
120         } else if (is_code_type(type)) {
121                 res->attr.code_attr.label = (ir_label_t) -1;
122         }
123
124         hook_new_entity(res);
125         return res;
126 }
127
128 ir_entity *new_entity(ir_type *owner, ident *name, ir_type *type)
129 {
130         return new_d_entity(owner, name, type, NULL);
131 }
132
133 static ident *make_parameter_entity_name(size_t pos)
134 {
135         char buf[64];
136         snprintf(buf, sizeof(buf), "parameter.%lu", (unsigned long) pos);
137         return new_id_from_str(buf);
138 }
139
140 ir_entity *new_d_parameter_entity(ir_type *owner, size_t pos, ir_type *type,
141                                   dbg_info *dbgi)
142 {
143         ident     *name            = make_parameter_entity_name(pos);
144         ir_entity *res             = intern_new_entity(owner, name, type, dbgi);
145         res->is_parameter          = true;
146         res->attr.parameter.number = pos;
147         hook_new_entity(res);
148         return res;
149 }
150
151 ir_entity *new_parameter_entity(ir_type *owner, size_t pos, ir_type *type)
152 {
153         return new_d_parameter_entity(owner, pos, type, NULL);
154 }
155
156 /**
157  * Free entity attributes.
158  *
159  * @param ent  the entity
160  */
161 static void free_entity_attrs(ir_entity *ent)
162 {
163         if (ent->overwrites != NULL) {
164                 DEL_ARR_F(ent->overwrites);
165                 ent->overwrites = NULL;
166         }
167         if (ent->overwrittenby != NULL) {
168                 DEL_ARR_F(ent->overwrittenby);
169                 ent->overwrittenby = NULL;
170         }
171
172         if (ent->initializer != NULL) {
173                 /* TODO: free initializers */
174         } else if (entity_has_compound_ent_values(ent)) {
175                 /* can't free compound graph path as it might be used
176                  * multiple times */
177                 ent->attr.cmpd_attr.val_paths = NULL;
178         }
179         if (is_compound_entity(ent)) {
180                 ent->attr.cmpd_attr.values = NULL;
181         } else if (is_method_entity(ent)) {
182                 if (ent->attr.mtd_attr.param_access) {
183                         DEL_ARR_F(ent->attr.mtd_attr.param_access);
184                         ent->attr.mtd_attr.param_access = NULL;
185                 }
186                 if (ent->attr.mtd_attr.param_weight) {
187                         DEL_ARR_F(ent->attr.mtd_attr.param_weight);
188                         ent->attr.mtd_attr.param_weight = NULL;
189                 }
190         }
191 }
192
193 /**
194  * Creates a deep copy of an entity.
195  */
196 static ir_entity *deep_entity_copy(ir_entity *old)
197 {
198         ir_entity *newe = XMALLOC(ir_entity);
199
200         *newe = *old;
201         if (old->initializer != NULL) {
202                 /* FIXME: the initializers are NOT copied */
203         } else if (entity_has_compound_ent_values(old)) {
204                 newe->attr.cmpd_attr.values    = NULL;
205                 newe->attr.cmpd_attr.val_paths = NULL;
206                 if (old->attr.cmpd_attr.values)
207                         newe->attr.cmpd_attr.values = DUP_ARR_F(ir_node *, old->attr.cmpd_attr.values);
208
209                 /* FIXME: the compound graph paths are NOT copied */
210                 if (old->attr.cmpd_attr.val_paths)
211                         newe->attr.cmpd_attr.val_paths = DUP_ARR_F(compound_graph_path *, old->attr.cmpd_attr.val_paths);
212         } else if (is_method_entity(old)) {
213                 /* do NOT copy them, reanalyze. This might be the best solution */
214                 newe->attr.mtd_attr.param_access = NULL;
215                 newe->attr.mtd_attr.param_weight = NULL;
216         }
217         newe->overwrites    = NULL;
218         newe->overwrittenby = NULL;
219
220 #ifdef DEBUG_libfirm
221         newe->nr = get_irp_new_node_nr();
222 #endif
223         hook_new_entity(newe);
224         return newe;
225 }
226
227 /*
228  * Copies the entity if the new_owner is different from the
229  * owner of the old entity,  else returns the old entity.
230  */
231 ir_entity *copy_entity_own(ir_entity *old, ir_type *new_owner)
232 {
233         ir_entity *newe;
234         assert(is_entity(old));
235         assert(is_compound_type(new_owner));
236         assert(get_type_state(new_owner) != layout_fixed);
237
238         if (old->owner == new_owner)
239                 return old;
240
241         /* create a deep copy so we are safe of aliasing and double-freeing. */
242         newe        = deep_entity_copy(old);
243         newe->owner = new_owner;
244         add_compound_member(new_owner, newe);
245
246         return newe;
247 }
248
249 ir_entity *copy_entity_name(ir_entity *old, ident *new_name)
250 {
251         ir_entity *newe;
252         assert(old && old->kind == k_entity);
253
254         if (old->name == new_name)
255                 return old;
256
257         newe       = deep_entity_copy(old);
258         newe->name = new_name;
259         newe->ld_name = NULL;
260         add_compound_member(old->owner, newe);
261
262         return newe;
263 }
264
265 void free_entity(ir_entity *ent)
266 {
267         if (ent->owner != NULL && !is_Array_type(ent->owner))
268                 remove_compound_member(ent->owner, ent);
269
270         assert(ent && ent->kind == k_entity);
271         free_entity_attrs(ent);
272         ent->kind = k_BAD;
273         xfree(ent);
274 }
275
276 /* Outputs a unique number for this node */
277 long get_entity_nr(const ir_entity *ent)
278 {
279         assert(ent && ent->kind == k_entity);
280 #ifdef DEBUG_libfirm
281         return ent->nr;
282 #else
283         return (long)PTR_TO_INT(ent);
284 #endif
285 }
286
287 const char *(get_entity_name)(const ir_entity *ent)
288 {
289         return _get_entity_name(ent);
290 }
291
292 ident *(get_entity_ident)(const ir_entity *ent)
293 {
294         return _get_entity_ident(ent);
295 }
296
297 void (set_entity_ident)(ir_entity *ent, ident *id)
298 {
299         _set_entity_ident(ent, id);
300 }
301
302 ir_type *(get_entity_owner)(const ir_entity *ent)
303 {
304         return _get_entity_owner(ent);
305 }
306
307 void set_entity_owner(ir_entity *ent, ir_type *owner)
308 {
309         assert(is_entity(ent));
310         assert(is_compound_type(owner));
311
312         remove_compound_member(ent->owner, ent);
313         add_compound_member(owner, ent);
314         ent->owner = owner;
315 }
316
317 ident *(get_entity_ld_ident)(const ir_entity *ent)
318 {
319         return _get_entity_ld_ident(ent);
320 }
321
322 void (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident)
323 {
324         _set_entity_ld_ident(ent, ld_ident);
325 }
326
327 const char *(get_entity_ld_name)(const ir_entity *ent)
328 {
329         return _get_entity_ld_name(ent);
330 }
331
332 int entity_has_ld_ident(const ir_entity *entity)
333 {
334         return entity->ld_name != NULL;
335 }
336
337 ir_type *(get_entity_type)(const ir_entity *ent)
338 {
339         return _get_entity_type(ent);
340 }
341
342 void (set_entity_type)(ir_entity *ent, ir_type *type)
343 {
344         _set_entity_type(ent, type);
345 }
346
347 ir_volatility (get_entity_volatility)(const ir_entity *ent)
348 {
349         return _get_entity_volatility(ent);
350 }
351
352 void (set_entity_volatility)(ir_entity *ent, ir_volatility vol)
353 {
354         _set_entity_volatility(ent, vol);
355 }
356
357 /* Return the name of the volatility. */
358 const char *get_volatility_name(ir_volatility var)
359 {
360 #define X(a)    case a: return #a
361         switch (var) {
362         X(volatility_non_volatile);
363         X(volatility_is_volatile);
364     default: return "BAD VALUE";
365         }
366 #undef X
367 }
368
369 ir_align (get_entity_aligned)(const ir_entity *ent)
370 {
371         return _get_entity_aligned(ent);
372 }
373
374 void (set_entity_aligned)(ir_entity *ent, ir_align a)
375 {
376         _set_entity_aligned(ent, a);
377 }
378
379 unsigned (get_entity_alignment)(const ir_entity *ent)
380 {
381         return _get_entity_alignment(ent);
382 }
383
384 void (set_entity_alignment)(ir_entity *ent, unsigned alignment)
385 {
386         _set_entity_alignment(ent, alignment);
387 }
388
389 /* Return the name of the alignment. */
390 const char *get_align_name(ir_align a)
391 {
392 #define X(a)    case a: return #a
393         switch (a) {
394         X(align_non_aligned);
395         X(align_is_aligned);
396         default: return "BAD VALUE";
397         }
398 #undef X
399 }
400
401 void set_entity_label(ir_entity *ent, ir_label_t label)
402 {
403         ent->attr.code_attr.label = label;
404 }
405
406 ir_label_t get_entity_label(const ir_entity *ent)
407 {
408         return ent->attr.code_attr.label;
409 }
410
411 static void verify_visibility(const ir_entity *entity)
412 {
413         if (get_entity_visibility(entity) == ir_visibility_external
414                         && !is_method_entity(entity)) {
415                 assert(!entity_has_definition(entity));
416         }
417 }
418
419 void set_entity_visibility(ir_entity *entity, ir_visibility visibility)
420 {
421         entity->visibility = visibility;
422         verify_visibility(entity);
423 }
424
425 ir_visibility get_entity_visibility(const ir_entity *entity)
426 {
427         return (ir_visibility)entity->visibility;
428 }
429
430 void set_entity_linkage(ir_entity *entity, ir_linkage linkage)
431 {
432         entity->linkage = linkage;
433 }
434
435 ir_linkage (get_entity_linkage)(const ir_entity *entity)
436 {
437         return get_entity_linkage(entity);
438 }
439
440 void add_entity_linkage(ir_entity *entity, ir_linkage linkage)
441 {
442         entity->linkage |= linkage;
443 }
444
445 void remove_entity_linkage(ir_entity *entity, ir_linkage linkage)
446 {
447         entity->linkage &= ~linkage;
448 }
449
450 /* Checks if an entity is compiler generated */
451 int (is_entity_compiler_generated)(const ir_entity *ent)
452 {
453         return _is_entity_compiler_generated(ent);
454 }
455
456 /* Sets/resets the compiler generated flag */
457 void (set_entity_compiler_generated)(ir_entity *ent, int flag)
458 {
459         _set_entity_compiler_generated(ent, flag);
460 }
461
462 ir_entity_usage (get_entity_usage)(const ir_entity *ent)
463 {
464         return _get_entity_usage(ent);
465 }
466
467 void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags)
468 {
469         _set_entity_usage(ent, flags);
470 }
471
472 /* Set has no effect for existent entities of type method. */
473 ir_node *get_atomic_ent_value(ir_entity *entity)
474 {
475         ir_initializer_t *initializer = get_entity_initializer(entity);
476
477         assert(entity && is_atomic_entity(entity));
478         if (initializer == NULL) {
479                 ir_type *type = get_entity_type(entity);
480                 return new_r_Unknown(get_const_code_irg(), get_type_mode(type));
481         }
482
483         switch (get_initializer_kind(initializer)) {
484         case IR_INITIALIZER_NULL: {
485                 ir_type *type = get_entity_type(entity);
486                 ir_mode *mode = get_type_mode(type);
487                 return new_r_Const(get_const_code_irg(), get_mode_null(mode));
488         }
489         case IR_INITIALIZER_TARVAL: {
490                 ir_tarval *tv = get_initializer_tarval_value(initializer);
491                 return new_r_Const(get_const_code_irg(), tv);
492         }
493         case IR_INITIALIZER_CONST:
494                 return get_initializer_const_value(initializer);
495         case IR_INITIALIZER_COMPOUND:
496                 panic("compound initializer in atomic entity not allowed (%+F)", entity);
497         }
498
499         panic("invalid initializer kind in get_atomic_ent_value(%+F)", entity);
500 }
501
502 void set_atomic_ent_value(ir_entity *entity, ir_node *val)
503 {
504         ir_initializer_t *initializer;
505
506         assert(is_atomic_entity(entity));
507
508         assert(is_Dummy(val) || get_irn_mode(val) == get_type_mode(entity->type));
509         initializer = create_initializer_const(val);
510         entity->initializer = initializer;
511 }
512
513 /* Returns true if the the node is representable as code on
514  *  const_code_irg. */
515 int is_irn_const_expression(ir_node *n)
516 {
517         /* we are in danger iff an exception will arise. TODO: be more precisely,
518          * for instance Div. will NOT rise if divisor != 0
519          */
520         if (is_binop(n) && !is_fragile_op(n))
521                 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
522
523         switch (get_irn_opcode(n)) {
524         case iro_Const:
525         case iro_SymConst:
526         case iro_Unknown:
527                 return 1;
528         case iro_Conv:
529         case iro_Cast:
530                 return is_irn_const_expression(get_irn_n(n, 0));
531         default:
532                 break;
533         }
534         return 0;
535 }
536
537 /*
538  * Copies a firm subgraph that complies to the restrictions for
539  * constant expressions to block.
540  */
541 ir_node *copy_const_value(dbg_info *dbg, ir_node *n, ir_node *block)
542 {
543         ir_graph *irg = get_irn_irg(block);
544         ir_node *nn;
545         ir_mode *m;
546
547         /* @@@ GL I think  we should implement this using the routines from irgopt for
548                dead node elimination/inlineing. */
549
550         m = get_irn_mode(n);
551         switch (get_irn_opcode(n)) {
552         case iro_Const:
553                 nn = new_rd_Const(dbg, irg, get_Const_tarval(n));
554                 break;
555         case iro_SymConst:
556                 nn = new_rd_SymConst(dbg, irg, get_irn_mode(n), get_SymConst_symbol(n), get_SymConst_kind(n));
557                 break;
558         case iro_Add:
559                 nn = new_rd_Add(dbg, block,
560                                 copy_const_value(dbg, get_Add_left(n), block),
561                                 copy_const_value(dbg, get_Add_right(n), block), m);
562                 break;
563         case iro_Sub:
564                 nn = new_rd_Sub(dbg, block,
565                                 copy_const_value(dbg, get_Sub_left(n), block),
566                                 copy_const_value(dbg, get_Sub_right(n), block), m);
567                 break;
568         case iro_Mul:
569                 nn = new_rd_Mul(dbg, block,
570                                 copy_const_value(dbg, get_Mul_left(n), block),
571                                 copy_const_value(dbg, get_Mul_right(n), block), m);
572                 break;
573         case iro_And:
574                 nn = new_rd_And(dbg, block,
575                                 copy_const_value(dbg, get_And_left(n), block),
576                                 copy_const_value(dbg, get_And_right(n), block), m);
577                 break;
578         case iro_Or:
579                 nn = new_rd_Or(dbg, block,
580                                copy_const_value(dbg, get_Or_left(n), block),
581                                copy_const_value(dbg, get_Or_right(n), block), m);
582                 break;
583         case iro_Eor:
584                 nn = new_rd_Eor(dbg, block,
585                                 copy_const_value(dbg, get_Eor_left(n), block),
586                                 copy_const_value(dbg, get_Eor_right(n), block), m);
587                 break;
588         case iro_Cast:
589                 nn = new_rd_Cast(dbg, block,
590                                  copy_const_value(dbg, get_Cast_op(n), block),
591                                  get_Cast_type(n));
592                 break;
593         case iro_Conv:
594                 nn = new_rd_Conv(dbg, block,
595                                  copy_const_value(dbg, get_Conv_op(n), block), m);
596                 break;
597         case iro_Unknown:
598                 nn = new_r_Unknown(irg, m); break;
599         default:
600                 panic("opcode invalid or not implemented");
601         }
602         return nn;
603 }
604
605 /** Return the name of the initializer kind. */
606 const char *get_initializer_kind_name(ir_initializer_kind_t ini)
607 {
608 #define X(a)    case a: return #a
609         switch (ini) {
610         X(IR_INITIALIZER_CONST);
611         X(IR_INITIALIZER_TARVAL);
612         X(IR_INITIALIZER_NULL);
613         X(IR_INITIALIZER_COMPOUND);
614     default: return "BAD VALUE";
615         }
616 #undef X
617 }
618
619 static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };
620
621 ir_initializer_t *get_initializer_null(void)
622 {
623         return &null_initializer;
624 }
625
626 ir_initializer_t *create_initializer_const(ir_node *value)
627 {
628         struct obstack *obst = get_irg_obstack(get_const_code_irg());
629
630         ir_initializer_t *initializer
631                 = (ir_initializer_t*)OALLOC(obst, ir_initializer_const_t);
632         initializer->kind         = IR_INITIALIZER_CONST;
633         initializer->consti.value = value;
634
635         return initializer;
636 }
637
638 ir_initializer_t *create_initializer_tarval(ir_tarval *tv)
639 {
640         struct obstack *obst = get_irg_obstack(get_const_code_irg());
641
642         ir_initializer_t *initializer
643                 = (ir_initializer_t*)OALLOC(obst, ir_initializer_tarval_t);
644         initializer->kind         = IR_INITIALIZER_TARVAL;
645         initializer->tarval.value = tv;
646
647         return initializer;
648 }
649
650 ir_initializer_t *create_initializer_compound(size_t n_entries)
651 {
652         struct obstack *obst = get_irg_obstack(get_const_code_irg());
653
654         size_t i;
655         size_t size  = sizeof(ir_initializer_compound_t)
656                      + n_entries * sizeof(ir_initializer_t*)
657                      - sizeof(ir_initializer_t*);
658
659         ir_initializer_t *initializer
660                 = (ir_initializer_t*)obstack_alloc(obst, size);
661         initializer->kind                    = IR_INITIALIZER_COMPOUND;
662         initializer->compound.n_initializers = n_entries;
663
664         for (i = 0; i < n_entries; ++i) {
665                 initializer->compound.initializers[i] = get_initializer_null();
666         }
667
668         return initializer;
669 }
670
671 ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
672 {
673         assert(initializer->kind == IR_INITIALIZER_CONST);
674         return skip_Id(initializer->consti.value);
675 }
676
677 ir_tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
678 {
679         assert(initializer->kind == IR_INITIALIZER_TARVAL);
680         return initializer->tarval.value;
681 }
682
683 size_t get_initializer_compound_n_entries(const ir_initializer_t *initializer)
684 {
685         assert(initializer->kind == IR_INITIALIZER_COMPOUND);
686         return initializer->compound.n_initializers;
687 }
688
689 void set_initializer_compound_value(ir_initializer_t *initializer,
690                                     size_t index, ir_initializer_t *value)
691 {
692         assert(initializer->kind == IR_INITIALIZER_COMPOUND);
693         assert(index < initializer->compound.n_initializers);
694
695         initializer->compound.initializers[index] = value;
696 }
697
698 ir_initializer_t *get_initializer_compound_value(
699                 const ir_initializer_t *initializer, size_t index)
700 {
701         assert(initializer->kind == IR_INITIALIZER_COMPOUND);
702         assert(index < initializer->compound.n_initializers);
703
704         return initializer->compound.initializers[index];
705 }
706
707 ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
708 {
709         return initializer->kind;
710 }
711
712 static void check_entity_initializer(ir_entity *entity)
713 {
714 #ifndef NDEBUG
715         ir_initializer_t *initializer = entity->initializer;
716         ir_type          *entity_tp   = get_entity_type(entity);
717         switch (initializer->kind) {
718         case IR_INITIALIZER_COMPOUND:
719                 assert(is_compound_type(entity_tp) || is_Array_type(entity_tp));
720                 break;
721         case IR_INITIALIZER_CONST:
722                 /* methods are initialized by a SymConst */
723                 assert(is_atomic_type(entity_tp) || is_Method_type(entity_tp));
724                 break;
725         case IR_INITIALIZER_TARVAL:
726                 assert(is_atomic_type(entity_tp));
727                 break;
728         case IR_INITIALIZER_NULL:
729                 break;
730         }
731 #endif
732 }
733
734 void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
735 {
736         entity->initializer = initializer;
737         check_entity_initializer(entity);
738 }
739
740 int has_entity_initializer(const ir_entity *entity)
741 {
742         return entity->initializer != NULL;
743 }
744
745 ir_initializer_t *get_entity_initializer(const ir_entity *entity)
746 {
747         return entity->initializer;
748 }
749
750 int (get_entity_offset)(const ir_entity *ent)
751 {
752         return _get_entity_offset(ent);
753 }
754
755 void (set_entity_offset)(ir_entity *ent, int offset)
756 {
757         _set_entity_offset(ent, offset);
758 }
759
760 unsigned char (get_entity_offset_bits_remainder)(const ir_entity *ent)
761 {
762         return _get_entity_offset_bits_remainder(ent);
763 }
764
765 void (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset)
766 {
767         _set_entity_offset_bits_remainder(ent, offset);
768 }
769
770 void add_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
771 {
772         if (ent->overwrites == NULL) {
773                 ent->overwrites = NEW_ARR_F(ir_entity*, 0);
774         }
775         ARR_APP1(ir_entity *, ent->overwrites, overwritten);
776         if (overwritten->overwrittenby == NULL) {
777                 overwritten->overwrittenby = NEW_ARR_F(ir_entity*, 0);
778         }
779         ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
780 }
781
782 size_t get_entity_n_overwrites(const ir_entity *ent)
783 {
784         if (ent->overwrites == NULL)
785                 return 0;
786         return ARR_LEN(ent->overwrites);
787 }
788
789 size_t get_entity_overwrites_index(const ir_entity *ent, ir_entity *overwritten)
790 {
791         size_t i;
792         size_t n = get_entity_n_overwrites(ent);
793         for (i = 0; i < n; ++i) {
794                 if (get_entity_overwrites(ent, i) == overwritten)
795                         return i;
796         }
797         return (size_t)-1;
798 }
799
800 ir_entity *get_entity_overwrites(const ir_entity *ent, size_t pos)
801 {
802         assert(pos < get_entity_n_overwrites(ent));
803         return ent->overwrites[pos];
804 }
805
806 void set_entity_overwrites(ir_entity *ent, size_t pos, ir_entity *overwritten)
807 {
808         assert(pos < get_entity_n_overwrites(ent));
809         ent->overwrites[pos] = overwritten;
810 }
811
812 void remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
813 {
814         size_t i;
815         size_t n = get_entity_n_overwrites(ent);
816         for (i = 0; i < n; ++i) {
817                 if (ent->overwrites[i] == overwritten) {
818                         for (; i < n - 1; i++)
819                                 ent->overwrites[i] = ent->overwrites[i+1];
820                         ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
821                         break;
822                 }
823         }
824 }
825
826
827 size_t get_entity_n_overwrittenby(const ir_entity *ent)
828 {
829         if (ent->overwrittenby == NULL)
830                 return 0;
831         return ARR_LEN(ent->overwrittenby);
832 }
833
834 size_t get_entity_overwrittenby_index(const ir_entity *ent,
835                                       ir_entity *overwrites)
836 {
837         size_t i;
838         size_t n = get_entity_n_overwrittenby(ent);
839         for (i = 0; i < n; ++i) {
840                 if (get_entity_overwrittenby(ent, i) == overwrites)
841                         return i;
842         }
843         return (size_t)-1;
844 }
845
846 ir_entity *get_entity_overwrittenby(const ir_entity *ent, size_t pos)
847 {
848         assert(pos < get_entity_n_overwrittenby(ent));
849         return ent->overwrittenby[pos];
850 }
851
852 void set_entity_overwrittenby(ir_entity *ent, size_t pos, ir_entity *overwrites)
853 {
854         assert(pos < get_entity_n_overwrittenby(ent));
855         ent->overwrittenby[pos] = overwrites;
856 }
857
858 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites)
859 {
860         size_t i;
861         size_t n = get_entity_n_overwrittenby(ent);
862         for (i = 0; i < n; ++i) {
863                 if (ent->overwrittenby[i] == overwrites) {
864                         for (; i < n - 1; ++i)
865                                 ent->overwrittenby[i] = ent->overwrittenby[i+1];
866                         ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
867                         break;
868                 }
869         }
870 }
871
872 void *(get_entity_link)(const ir_entity *ent)
873 {
874         return _get_entity_link(ent);
875 }
876
877 void (set_entity_link)(ir_entity *ent, void *l)
878 {
879         _set_entity_link(ent, l);
880 }
881
882 ir_graph *(get_entity_irg)(const ir_entity *ent)
883 {
884         return _get_entity_irg(ent);
885 }
886
887 void set_entity_irg(ir_entity *ent, ir_graph *irg)
888 {
889         assert(is_method_entity(ent));
890         assert(get_entity_peculiarity(ent) == peculiarity_existent);
891         ent->attr.mtd_attr.irg = irg;
892 }
893
894 int (is_parameter_entity)(const ir_entity *entity)
895 {
896         return _is_parameter_entity(entity);
897 }
898
899 size_t (get_entity_parameter_number)(const ir_entity *entity)
900 {
901         return _get_entity_parameter_number(entity);
902 }
903
904 void set_entity_parameter_number(ir_entity *entity, size_t n)
905 {
906         assert(is_parameter_entity(entity));
907         entity->attr.parameter.number = n;
908 }
909
910 unsigned get_entity_vtable_number(const ir_entity *ent)
911 {
912         assert(is_method_entity((ir_entity *)ent));
913         return ent->attr.mtd_attr.vtable_number;
914 }
915
916 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number)
917 {
918         assert(is_method_entity(ent));
919         ent->attr.mtd_attr.vtable_number = vtable_number;
920 }
921
922 int (is_entity)(const void *thing)
923 {
924         return _is_entity(thing);
925 }
926
927 int is_atomic_entity(const ir_entity *ent)
928 {
929         ir_type *t      = get_entity_type(ent);
930         const tp_op *op = get_type_tpop(t);
931         return (op == type_primitive || op == type_pointer ||
932                 op == type_enumeration || op == type_method);
933 }
934
935 int is_compound_entity(const ir_entity *ent)
936 {
937         ir_type     *t  = get_entity_type(ent);
938         const tp_op *op = get_type_tpop(t);
939         return (op == type_class || op == type_struct ||
940                 op == type_array || op == type_union);
941 }
942
943 int is_method_entity(const ir_entity *ent)
944 {
945         ir_type *t = get_entity_type(ent);
946         return is_Method_type(t);
947 }
948
949 ir_visited_t (get_entity_visited)(const ir_entity *ent)
950 {
951         return _get_entity_visited(ent);
952 }
953
954 void (set_entity_visited)(ir_entity *ent, ir_visited_t num)
955 {
956         _set_entity_visited(ent, num);
957 }
958
959 void (mark_entity_visited)(ir_entity *ent)
960 {
961         _mark_entity_visited(ent);
962 }
963
964 int (entity_visited)(const ir_entity *ent)
965 {
966         return _entity_visited(ent);
967 }
968
969 int (entity_not_visited)(const ir_entity *ent)
970 {
971         return _entity_not_visited(ent);
972 }
973
974 mtp_additional_properties get_entity_additional_properties(const ir_entity *ent)
975 {
976         ir_graph *irg;
977
978         assert(is_method_entity(ent));
979
980         /* first check, if the graph has additional properties */
981         irg = get_entity_irg(ent);
982
983         if (irg)
984                 return get_irg_additional_properties(irg);
985
986         if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
987                 return get_method_additional_properties(get_entity_type(ent));
988
989         return ent->attr.mtd_attr.irg_add_properties;
990 }
991
992 void set_entity_additional_properties(ir_entity *ent, mtp_additional_properties property_mask)
993 {
994         ir_graph *irg;
995
996         assert(is_method_entity(ent));
997
998         /* first check, if the graph exists */
999         irg = get_entity_irg(ent);
1000         if (irg)
1001                 set_irg_additional_properties(irg, property_mask);
1002         else {
1003                 /* do not allow to set the mtp_property_inherited flag or
1004                  * the automatic inheritance of flags will not work */
1005                 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1006         }
1007 }
1008
1009 void add_entity_additional_properties(ir_entity *ent, mtp_additional_properties properties)
1010 {
1011         ir_graph *irg;
1012
1013         assert(is_method_entity(ent));
1014
1015         /* first check, if the graph exists */
1016         irg = get_entity_irg(ent);
1017         if (irg)
1018                 add_irg_additional_properties(irg, properties);
1019         else {
1020                 mtp_additional_properties mask = ent->attr.mtd_attr.irg_add_properties;
1021
1022                 if (mask & mtp_property_inherited)
1023                         mask = get_method_additional_properties(get_entity_type(ent));
1024
1025                 /* do not allow to set the mtp_property_inherited flag or
1026                  * the automatic inheritance of flags will not work */
1027                 ent->attr.mtd_attr.irg_add_properties = mask | (properties & ~mtp_property_inherited);
1028         }
1029 }
1030
1031 /* Returns the class type that this type info entity represents or NULL
1032    if ent is no type info entity. */
1033 ir_type *(get_entity_repr_class)(const ir_entity *ent)
1034 {
1035         return _get_entity_repr_class(ent);
1036 }
1037
1038 dbg_info *(get_entity_dbg_info)(const ir_entity *ent)
1039 {
1040         return _get_entity_dbg_info(ent);
1041 }
1042
1043 void (set_entity_dbg_info)(ir_entity *ent, dbg_info *db)
1044 {
1045         _set_entity_dbg_info(ent, db);
1046 }
1047
1048 int entity_is_externally_visible(const ir_entity *entity)
1049 {
1050         return get_entity_visibility(entity) != ir_visibility_local
1051                 || (get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER);
1052 }
1053
1054 int entity_has_definition(const ir_entity *entity)
1055 {
1056         return entity->initializer != NULL
1057                 || get_entity_irg(entity) != NULL
1058                 || entity_has_compound_ent_values(entity);
1059 }
1060
1061 void ir_init_entity(void)
1062 {
1063         assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1064         assert(!unknown_entity && "Call firm_init_entity() only once!");
1065
1066         unknown_entity = new_d_entity(NULL, new_id_from_str(UNKNOWN_ENTITY_NAME),
1067                                       firm_unknown_type, NULL);
1068         set_entity_visibility(unknown_entity, ir_visibility_external);
1069         set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1070 }
1071
1072 void ir_finish_entity(void)
1073 {
1074         if (unknown_entity != NULL) {
1075                 free_entity(unknown_entity);
1076                 unknown_entity = NULL;
1077         }
1078 }
1079
1080 ir_allocation get_entity_allocation(const ir_entity *entity)
1081 {
1082         return (ir_allocation)entity->allocation;
1083 }
1084
1085 void set_entity_allocation(ir_entity *entity, ir_allocation allocation)
1086 {
1087         entity->allocation = allocation;
1088 }
1089
1090 ir_peculiarity get_entity_peculiarity(const ir_entity *entity)
1091 {
1092         return (ir_peculiarity)entity->peculiarity;
1093 }
1094
1095 void set_entity_peculiarity(ir_entity *entity, ir_peculiarity peculiarity)
1096 {
1097         entity->peculiarity = peculiarity;
1098 }
1099
1100 void set_entity_final(ir_entity *entity, int final)
1101 {
1102         entity->final = final;
1103 }
1104
1105 int is_entity_final(const ir_entity *entity)
1106 {
1107         return entity->final;
1108 }