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