cleanup: Remove pointless assert(is_${NODE}(x)) just before get_${NODE}_${FOO}(x...
[libfirm] / ir / tr / tr_inheritance.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    tr_inheritance.c
22  * @brief   Utility routines for inheritance representation
23  * @author  Goetz Lindenmaier
24  */
25 #include "config.h"
26
27 #include "debug.h"
28 #include "typerep.h"
29 #include "irgraph_t.h"
30 #include "irprog_t.h"
31 #include "irprintf.h"
32 #include "pset.h"
33 #include "set.h"
34 #include "irgwalk.h"
35 #include "irflag.h"
36
37 /* ----------------------------------------------------------------------- */
38 /* Resolve implicit inheritance.                                           */
39 /* ----------------------------------------------------------------------- */
40
41 ident *default_mangle_inherited_name(const ir_entity *super, const ir_type *clss)
42 {
43         return id_mangle_u(new_id_from_str("inh"), id_mangle_u(get_class_ident(clss), get_entity_ident(super)));
44 }
45
46 /** Replicates all entities in all super classes that are not overwritten
47     by an entity of this class. */
48 static void copy_entities_from_superclass(ir_type *clss, void *env)
49 {
50         size_t i;
51         size_t j;
52         size_t k;
53         size_t l;
54         int overwritten;
55         ir_type *super;
56         ir_entity *inhent, *thisent;
57         mangle_inherited_name_func *mfunc = *(mangle_inherited_name_func **)env;
58
59         for (i = 0; i < get_class_n_supertypes(clss); i++) {
60                 super = get_class_supertype(clss, i);
61                 for (j = 0; j < get_class_n_members(super); j++) {
62                         inhent = get_class_member(super, j);
63                         /* check whether inhent is already overwritten */
64                         overwritten = 0;
65                         for (k = 0; (k < get_class_n_members(clss)) && (overwritten == 0); k++) {
66                                 thisent = get_class_member(clss, k);
67                                 for (l = 0; l < get_entity_n_overwrites(thisent); l++) {
68                                         if (inhent == get_entity_overwrites(thisent, l)) {
69                                                 /* overwritten - do not copy */
70                                                 overwritten = 1;
71                                                 break;
72                                         }
73                                 }
74                         }
75                         /* Inherit entity */
76                         if (!overwritten) {
77                                 thisent = copy_entity_own(inhent, clss);
78                                 add_entity_overwrites(thisent, inhent);
79                                 if (get_entity_peculiarity(inhent) == peculiarity_existent)
80                                         set_entity_peculiarity(thisent, peculiarity_inherited);
81                                 set_entity_ld_ident(thisent, mfunc(inhent, clss));
82                                 if (get_entity_linkage(inhent) & IR_LINKAGE_CONSTANT) {
83                                         assert(is_atomic_entity(inhent) &&  /* @@@ */
84                                                 "Inheritance of constant, compound entities not implemented");
85                                         add_entity_linkage(thisent, IR_LINKAGE_CONSTANT);
86                                         set_atomic_ent_value(thisent, get_atomic_ent_value(inhent));
87                                 }
88                         }
89                 }
90         }
91 }
92
93 void resolve_inheritance(mangle_inherited_name_func *mfunc)
94 {
95         if (!mfunc)
96                 mfunc = default_mangle_inherited_name;
97         class_walk_super2sub(copy_entities_from_superclass, NULL, (void *)&mfunc);
98 }
99
100
101 /* ----------------------------------------------------------------------- */
102 /* The transitive closure of the subclass/superclass and                   */
103 /* overwrites/overwrittenby relation.                                      */
104 /*                                                                         */
105 /* A walk over the ir (O(#types+#entities)) computes the transitive        */
106 /* closure.  Adding a new type/entity or changing the basic relations in   */
107 /* some other way invalidates the transitive closure, i.e., it is not      */
108 /* updated by the basic functions.                                         */
109 /*                                                                         */
110 /* All functions are named as their counterparts for the basic relations,  */
111 /* adding the infix 'trans_'.                                              */
112 /* ----------------------------------------------------------------------- */
113
114 void                        set_irp_inh_transitive_closure_state(inh_transitive_closure_state s)
115 {
116         irp->inh_trans_closure_state = s;
117 }
118 void                        invalidate_irp_inh_transitive_closure_state(void)
119 {
120         if (irp->inh_trans_closure_state == inh_transitive_closure_valid)
121                 irp->inh_trans_closure_state = inh_transitive_closure_invalid;
122 }
123 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void)
124 {
125         return irp->inh_trans_closure_state;
126 }
127
128 static void assert_valid_state(void)
129 {
130         assert(irp->inh_trans_closure_state == inh_transitive_closure_valid ||
131                irp->inh_trans_closure_state == inh_transitive_closure_invalid);
132 }
133
134 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
135 /* There is a set that extends each entity/type with two new               */
136 /* fields:  one for the upwards directed relation: 'up' (supertype,        */
137 /* overwrites) and one for the downwards directed relation: 'down' (sub-   */
138 /* type, overwrittenby.  These fields contain psets (and maybe later       */
139 /* arrays) listing all subtypes...                                         */
140 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
141
142 typedef enum {
143         d_up   = 0,
144         d_down = 1,
145 } dir;
146
147 typedef struct {
148         const firm_kind *kind;   /**< An entity or type. */
149         pset *directions[2];
150 } tr_inh_trans_tp;
151
152 /* We use this set for all types and entities.  */
153 static set *tr_inh_trans_set = NULL;
154
155 /**
156  * Compare two tr_inh_trans_tp entries.
157  */
158 static int tr_inh_trans_cmp(const void *e1, const void *e2, size_t size)
159 {
160         const tr_inh_trans_tp *ef1 = (const tr_inh_trans_tp*)e1;
161         const tr_inh_trans_tp *ef2 = (const tr_inh_trans_tp*)e2;
162         (void) size;
163
164         return ef1->kind != ef2->kind;
165 }
166
167 /**
168  * calculate the hash value of an tr_inh_trans_tp
169  */
170 static inline unsigned int tr_inh_trans_hash(const tr_inh_trans_tp *v)
171 {
172         return hash_ptr(v->kind);
173 }
174
175 /* This always completes successfully. */
176 static tr_inh_trans_tp *get_firm_kind_entry(const firm_kind *k)
177 {
178         tr_inh_trans_tp a, *found;
179         a.kind = k;
180
181         if (!tr_inh_trans_set) tr_inh_trans_set = new_set(tr_inh_trans_cmp, 128);
182
183         found = set_find(tr_inh_trans_tp, tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
184         if (!found) {
185                 a.directions[d_up]   = pset_new_ptr(16);
186                 a.directions[d_down] = pset_new_ptr(16);
187                 found = set_insert(tr_inh_trans_tp, tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
188         }
189         return found;
190 }
191
192 static pset *get_entity_map(const ir_entity *ent, dir d)
193 {
194         tr_inh_trans_tp *found;
195
196         assert(is_entity(ent));
197         found = get_firm_kind_entry((const firm_kind *)ent);
198         return found->directions[d];
199 }
200
201 static pset *get_type_map(const ir_type *tp, dir d)
202 {
203         tr_inh_trans_tp *found;
204
205         assert(is_type(tp));
206         found = get_firm_kind_entry((const firm_kind *)tp);
207         return found->directions[d];
208 }
209
210
211 /**
212  * Walk over all types reachable from tp in the sub/supertype
213  * relation and compute the closure for the two downwards directed
214  * relations.
215  *
216  * The walk in the dag formed by the relation is tricky:  We must visit
217  * all subtypes before visiting the supertypes.  So we first walk down.
218  * Then we can compute the closure for this type.  Then we walk up.
219  * As we call ourselves recursive, and walk in both directions, there
220  * can be cycles.  So we have to make sure, that if we visit a node
221  * a second time (in a walk up) we do nothing.  For this we increment
222  * the master visited flag twice.
223  * If the type is marked with master_flag_visited-1 it is on the stack.
224  * If it is marked with master_flag_visited it is fully processed.
225  *
226  * Well, we still miss some candidates ... */
227 static void compute_down_closure(ir_type *tp)
228 {
229         pset *myset, *subset;
230         size_t i, n_subtypes, n_members, n_supertypes;
231         ir_visited_t master_visited = get_master_type_visited();
232
233         set_type_visited(tp, master_visited-1);
234
235         /* Recursive descend. */
236         n_subtypes = get_class_n_subtypes(tp);
237         for (i = 0; i < n_subtypes; ++i) {
238                 ir_type *stp = get_class_subtype(tp, i);
239                 if (get_type_visited(stp) < master_visited-1) {
240                         compute_down_closure(stp);
241                 }
242         }
243
244         /* types */
245         myset = get_type_map(tp, d_down);
246         for (i = 0; i < n_subtypes; ++i) {
247                 ir_type *stp = get_class_subtype(tp, i);
248                 subset = get_type_map(stp, d_down);
249                 pset_insert_ptr(myset, stp);
250                 pset_insert_pset_ptr(myset, subset);
251         }
252
253         /* entities */
254         n_members = get_class_n_members(tp);
255         for (i = 0; i < n_members; ++i) {
256                 ir_entity *mem = get_class_member(tp, i);
257                 size_t j, n_overwrittenby = get_entity_n_overwrittenby(mem);
258
259                 myset = get_entity_map(mem, d_down);
260                 for (j = 0; j < n_overwrittenby; ++j) {
261                         ir_entity *ov = get_entity_overwrittenby(mem, j);
262                         subset = get_entity_map(ov, d_down);
263                         pset_insert_ptr(myset, ov);
264                         pset_insert_pset_ptr(myset, subset);
265                 }
266         }
267
268         mark_type_visited(tp);
269
270         /* Walk up. */
271         n_supertypes = get_class_n_supertypes(tp);
272         for (i = 0; i < n_supertypes; ++i) {
273                 ir_type *stp = get_class_supertype(tp, i);
274                 if (get_type_visited(stp) < master_visited-1) {
275                         compute_down_closure(stp);
276                 }
277         }
278 }
279
280 static void compute_up_closure(ir_type *tp)
281 {
282         pset *myset, *subset;
283         size_t i, n_subtypes, n_members, n_supertypes;
284         ir_visited_t master_visited = get_master_type_visited();
285
286         set_type_visited(tp, master_visited-1);
287
288         /* Recursive descend. */
289         n_supertypes = get_class_n_supertypes(tp);
290         for (i = 0; i < n_supertypes; ++i) {
291                 ir_type *stp = get_class_supertype(tp, i);
292                 if (get_type_visited(stp) < get_master_type_visited()-1) {
293                         compute_up_closure(stp);
294                 }
295         }
296
297         /* types */
298         myset = get_type_map(tp, d_up);
299         for (i = 0; i < n_supertypes; ++i) {
300                 ir_type *stp = get_class_supertype(tp, i);
301                 subset = get_type_map(stp, d_up);
302                 pset_insert_ptr(myset, stp);
303                 pset_insert_pset_ptr(myset, subset);
304         }
305
306         /* entities */
307         n_members = get_class_n_members(tp);
308         for (i = 0; i < n_members; ++i) {
309                 ir_entity *mem = get_class_member(tp, i);
310                 size_t j, n_overwrites = get_entity_n_overwrites(mem);
311
312                 myset = get_entity_map(mem, d_up);
313                 for (j = 0; j < n_overwrites; ++j) {
314                         ir_entity *ov = get_entity_overwrites(mem, j);
315                         subset = get_entity_map(ov, d_up);
316                         pset_insert_pset_ptr(myset, subset);
317                         pset_insert_ptr(myset, ov);
318                 }
319         }
320
321         mark_type_visited(tp);
322
323         /* Walk down. */
324         n_subtypes = get_class_n_subtypes(tp);
325         for (i = 0; i < n_subtypes; ++i) {
326                 ir_type *stp = get_class_subtype(tp, i);
327                 if (get_type_visited(stp) < master_visited-1) {
328                         compute_up_closure(stp);
329                 }
330         }
331 }
332
333 void compute_inh_transitive_closure(void)
334 {
335         size_t i, n_types = get_irp_n_types();
336         free_inh_transitive_closure();
337
338         /* The 'down' relation */
339         irp_reserve_resources(irp, IRP_RESOURCE_TYPE_VISITED);
340         inc_master_type_visited();  /* Inc twice: one if on stack, second if values computed. */
341         inc_master_type_visited();
342         for (i = 0; i < n_types; ++i) {
343                 ir_type *tp = get_irp_type(i);
344                 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
345                         size_t j, n_subtypes = get_class_n_subtypes(tp);
346                         int has_unmarked_subtype = 0;
347
348                         assert(get_type_visited(tp) < get_master_type_visited()-1);
349                         for (j = 0; j < n_subtypes; ++j) {
350                                 ir_type *stp = get_class_subtype(tp, j);
351                                 if (type_not_visited(stp)) {
352                                         has_unmarked_subtype = 1;
353                                         break;
354                                 }
355                         }
356
357                         /* This is a good starting point. */
358                         if (!has_unmarked_subtype)
359                                 compute_down_closure(tp);
360                 }
361         }
362
363         /* The 'up' relation */
364         inc_master_type_visited();
365         inc_master_type_visited();
366         for (i = 0; i < n_types; ++i) {
367                 ir_type *tp = get_irp_type(i);
368                 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
369                         size_t j, n_supertypes = get_class_n_supertypes(tp);
370                         int has_unmarked_supertype = 0;
371
372                         assert(get_type_visited(tp) < get_master_type_visited()-1);
373                         for (j = 0; j < n_supertypes; ++j) {
374                                 ir_type *stp = get_class_supertype(tp, j);
375                                 if (type_not_visited(stp)) {
376                                         has_unmarked_supertype = 1;
377                                         break;
378                                 }
379                         }
380
381                         /* This is a good starting point. */
382                         if (!has_unmarked_supertype)
383                                 compute_up_closure(tp);
384                 }
385         }
386
387         irp->inh_trans_closure_state = inh_transitive_closure_valid;
388         irp_free_resources(irp, IRP_RESOURCE_TYPE_VISITED);
389 }
390
391 void free_inh_transitive_closure(void)
392 {
393         if (tr_inh_trans_set) {
394                 foreach_set(tr_inh_trans_set, tr_inh_trans_tp, elt) {
395                         del_pset(elt->directions[d_up]);
396                         del_pset(elt->directions[d_down]);
397                 }
398                 del_set(tr_inh_trans_set);
399                 tr_inh_trans_set = NULL;
400         }
401         irp->inh_trans_closure_state = inh_transitive_closure_none;
402 }
403
404 /* - subtype ------------------------------------------------------------- */
405
406 ir_type *get_class_trans_subtype_first(const ir_type *tp)
407 {
408         assert_valid_state();
409         return pset_first(ir_type, get_type_map(tp, d_down));
410 }
411
412 ir_type *get_class_trans_subtype_next(const ir_type *tp)
413 {
414         assert_valid_state();
415         return pset_next(ir_type, get_type_map(tp, d_down));
416 }
417
418 int is_class_trans_subtype(const ir_type *tp, const ir_type *subtp)
419 {
420         assert_valid_state();
421         return (pset_find_ptr(get_type_map(tp, d_down), subtp) != NULL);
422 }
423
424 /* - supertype ----------------------------------------------------------- */
425
426 ir_type *get_class_trans_supertype_first(const ir_type *tp)
427 {
428         assert_valid_state();
429         return pset_first(ir_type, get_type_map(tp, d_up));
430 }
431
432 ir_type *get_class_trans_supertype_next(const ir_type *tp)
433 {
434         assert_valid_state();
435         return pset_next(ir_type, get_type_map(tp, d_up));
436 }
437
438 /* - overwrittenby ------------------------------------------------------- */
439
440 ir_entity *get_entity_trans_overwrittenby_first(const ir_entity *ent)
441 {
442         assert_valid_state();
443         return pset_first(ir_entity, get_entity_map(ent, d_down));
444 }
445
446 ir_entity *get_entity_trans_overwrittenby_next(const ir_entity *ent)
447 {
448         assert_valid_state();
449         return pset_next(ir_entity, get_entity_map(ent, d_down));
450 }
451
452 /* - overwrites ---------------------------------------------------------- */
453
454
455 ir_entity *get_entity_trans_overwrites_first(const ir_entity *ent)
456 {
457         assert_valid_state();
458         return pset_first(ir_entity, get_entity_map(ent, d_up));
459 }
460
461 ir_entity *get_entity_trans_overwrites_next(const ir_entity *ent)
462 {
463         assert_valid_state();
464         return pset_next(ir_entity, get_entity_map(ent, d_up));
465 }
466
467
468 /* ----------------------------------------------------------------------- */
469 /* Classify pairs of types/entities in the inheritance relations.          */
470 /* ----------------------------------------------------------------------- */
471
472 /** Returns true if low is subclass of high. */
473 static int check_is_SubClass_of(ir_type *low, ir_type *high)
474 {
475         size_t i, n_subtypes;
476
477         /* depth first search from high downwards. */
478         n_subtypes = get_class_n_subtypes(high);
479         for (i = 0; i < n_subtypes; i++) {
480                 ir_type *stp = get_class_subtype(high, i);
481                 if (low == stp) return 1;
482                 if (is_SubClass_of(low, stp))
483                         return 1;
484         }
485         return 0;
486 }
487
488 int is_SubClass_of(ir_type *low, ir_type *high)
489 {
490         assert(is_Class_type(low) && is_Class_type(high));
491
492         if (low == high) return 1;
493
494         if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
495                 pset *m = get_type_map(high, d_down);
496                 return pset_find_ptr(m, low) ? 1 : 0;
497         }
498         return check_is_SubClass_of(low, high);
499 }
500
501 int is_SubClass_ptr_of(ir_type *low, ir_type *high)
502 {
503         while (is_Pointer_type(low) && is_Pointer_type(high)) {
504                 low  = get_pointer_points_to_type(low);
505                 high = get_pointer_points_to_type(high);
506         }
507
508         if (is_Class_type(low) && is_Class_type(high))
509                 return is_SubClass_of(low, high);
510         return 0;
511 }
512
513 int is_overwritten_by(ir_entity *high, ir_entity *low)
514 {
515         size_t i, n_overwrittenby;
516         assert(is_entity(low) && is_entity(high));
517
518         if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
519                 pset *m = get_entity_map(high, d_down);
520                 return pset_find_ptr(m, low) ? 1 : 0;
521         }
522
523         /* depth first search from high downwards. */
524         n_overwrittenby = get_entity_n_overwrittenby(high);
525         for (i = 0; i < n_overwrittenby; i++) {
526                 ir_entity *ov = get_entity_overwrittenby(high, i);
527                 if (low == ov) return 1;
528                 if (is_overwritten_by(low, ov))
529                         return 1;
530         }
531         return 0;
532 }
533
534 /** Resolve polymorphy in the inheritance relation.
535  *
536  * Returns the dynamically referenced entity if the static entity and the
537  * dynamic type are given.
538  * Search downwards in overwritten tree.
539  *
540  * Need two routines because I want to assert the result.
541  */
542 static ir_entity *do_resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent)
543 {
544         size_t i, n_overwrittenby;
545
546         ir_type *owner = get_entity_owner(static_ent);
547         if (owner == dynamic_class) return static_ent;
548
549         // if the owner of the static_ent already is more special than the dynamic
550         // type to check against - stop here.
551         if (! is_SubClass_of(dynamic_class, owner)) return NULL;
552
553         n_overwrittenby = get_entity_n_overwrittenby(static_ent);
554         for (i = 0; i < n_overwrittenby; ++i) {
555                 ir_entity *ent = get_entity_overwrittenby(static_ent, i);
556                 ent = do_resolve_ent_polymorphy(dynamic_class, ent);
557                 if (ent) return ent;
558         }
559
560         // No further specialization of static_ent has been found
561         return static_ent;
562 }
563
564 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent)
565 {
566         ir_entity *res;
567         assert(static_ent && is_entity(static_ent));
568
569         res = do_resolve_ent_polymorphy(dynamic_class, static_ent);
570         assert(res);
571
572         return res;
573 }
574
575
576
577 /* ----------------------------------------------------------------------- */
578 /* Class cast state handling.                                              */
579 /* ----------------------------------------------------------------------- */
580
581 /* - State handling. ----------------------------------------- */
582
583 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s)
584 {
585         if (get_irp_class_cast_state() > s)
586                 set_irp_class_cast_state(s);
587         irg->class_cast_state = s;
588 }
589
590 ir_class_cast_state get_irg_class_cast_state(const ir_graph *irg)
591 {
592         return irg->class_cast_state;
593 }
594
595 void set_irp_class_cast_state(ir_class_cast_state s)
596 {
597 #ifndef NDEBUG
598         size_t i, n;
599         for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
600                 assert(get_irg_class_cast_state(get_irp_irg(i)) >= s);
601 #endif
602         irp->class_cast_state = s;
603 }
604
605 ir_class_cast_state get_irp_class_cast_state(void)
606 {
607         return irp->class_cast_state;
608 }