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