Fixed a lot of size_t related warnings, most of them due to array implementation...
[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 = (const tr_inh_trans_tp*)e1;
167         const tr_inh_trans_tp *ef2 = (const tr_inh_trans_tp*)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 = (tr_inh_trans_tp*)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 = (tr_inh_trans_tp*)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 = (tr_inh_trans_tp*)set_first(tr_inh_trans_set); elt != NULL;
412                      elt = (tr_inh_trans_tp*)set_next(tr_inh_trans_set)) {
413                         del_pset(elt->directions[d_up]);
414                         del_pset(elt->directions[d_down]);
415                 }
416                 del_set(tr_inh_trans_set);
417                 tr_inh_trans_set = NULL;
418         }
419         irp->inh_trans_closure_state = inh_transitive_closure_none;
420 }
421
422 /* - subtype ------------------------------------------------------------- */
423
424 ir_type *get_class_trans_subtype_first(const ir_type *tp)
425 {
426         assert_valid_state();
427         return (ir_type*)pset_first(get_type_map(tp, d_down));
428 }
429
430 ir_type *get_class_trans_subtype_next(const ir_type *tp)
431 {
432         assert_valid_state();
433         return (ir_type*)pset_next(get_type_map(tp, d_down));
434 }
435
436 int is_class_trans_subtype(const ir_type *tp, const ir_type *subtp)
437 {
438         assert_valid_state();
439         return (pset_find_ptr(get_type_map(tp, d_down), subtp) != NULL);
440 }
441
442 /* - supertype ----------------------------------------------------------- */
443
444 ir_type *get_class_trans_supertype_first(const ir_type *tp)
445 {
446         assert_valid_state();
447         return (ir_type*)pset_first(get_type_map(tp, d_up));
448 }
449
450 ir_type *get_class_trans_supertype_next(const ir_type *tp)
451 {
452         assert_valid_state();
453         return (ir_type*)pset_next(get_type_map(tp, d_up));
454 }
455
456 /* - overwrittenby ------------------------------------------------------- */
457
458 ir_entity *get_entity_trans_overwrittenby_first(const ir_entity *ent)
459 {
460         assert_valid_state();
461         return (ir_entity*)pset_first(get_entity_map(ent, d_down));
462 }
463
464 ir_entity *get_entity_trans_overwrittenby_next(const ir_entity *ent)
465 {
466         assert_valid_state();
467         return (ir_entity*)pset_next(get_entity_map(ent, d_down));
468 }
469
470 /* - overwrites ---------------------------------------------------------- */
471
472
473 /** Iterate over all transitive overwritten entities. */
474 ir_entity *get_entity_trans_overwrites_first(const ir_entity *ent)
475 {
476         assert_valid_state();
477         return (ir_entity*)pset_first(get_entity_map(ent, d_up));
478 }
479
480 ir_entity *get_entity_trans_overwrites_next(const ir_entity *ent)
481 {
482         assert_valid_state();
483         return (ir_entity*)pset_next(get_entity_map(ent, d_up));
484 }
485
486
487 /* ----------------------------------------------------------------------- */
488 /* Classify pairs of types/entities in the inheritance relations.          */
489 /* ----------------------------------------------------------------------- */
490
491 /** Returns true if low is subclass of high. */
492 static int check_is_SubClass_of(ir_type *low, ir_type *high)
493 {
494         int i, n_subtypes;
495
496         /* depth first search from high downwards. */
497         n_subtypes = get_class_n_subtypes(high);
498         for (i = 0; i < n_subtypes; i++) {
499                 ir_type *stp = get_class_subtype(high, i);
500                 if (low == stp) return 1;
501                 if (is_SubClass_of(low, stp))
502                         return 1;
503         }
504         return 0;
505 }
506
507 /* Returns true if low is subclass of high. */
508 int is_SubClass_of(ir_type *low, ir_type *high)
509 {
510         assert(is_Class_type(low) && is_Class_type(high));
511
512         if (low == high) return 1;
513
514         if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
515                 pset *m = get_type_map(high, d_down);
516                 return pset_find_ptr(m, low) ? 1 : 0;
517         }
518         return check_is_SubClass_of(low, high);
519 }
520
521
522 /* Subclass check for pointers to classes.
523  *
524  *  Dereferences at both types the same amount of pointer types (as
525  *  many as possible).  If the remaining types are both class types
526  *  and subclasses, returns true, else false.  Can also be called with
527  *  two class types.  */
528 int is_SubClass_ptr_of(ir_type *low, ir_type *high)
529 {
530         while (is_Pointer_type(low) && is_Pointer_type(high)) {
531                 low  = get_pointer_points_to_type(low);
532                 high = get_pointer_points_to_type(high);
533         }
534
535         if (is_Class_type(low) && is_Class_type(high))
536                 return is_SubClass_of(low, high);
537         return 0;
538 }
539
540 int is_overwritten_by(ir_entity *high, ir_entity *low)
541 {
542         int i, n_overwrittenby;
543         assert(is_entity(low) && is_entity(high));
544
545         if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
546                 pset *m = get_entity_map(high, d_down);
547                 return pset_find_ptr(m, low) ? 1 : 0;
548         }
549
550         /* depth first search from high downwards. */
551         n_overwrittenby = get_entity_n_overwrittenby(high);
552         for (i = 0; i < n_overwrittenby; i++) {
553                 ir_entity *ov = get_entity_overwrittenby(high, i);
554                 if (low == ov) return 1;
555                 if (is_overwritten_by(low, ov))
556                         return 1;
557         }
558         return 0;
559 }
560
561 /** Resolve polymorphy in the inheritance relation.
562  *
563  * Returns the dynamically referenced entity if the static entity and the
564  * dynamic type are given.
565  * Search downwards in overwritten tree.
566  *
567  * Need two routines because I want to assert the result.
568  */
569 static ir_entity *do_resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent)
570 {
571         int i, n_overwrittenby;
572
573         if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
574
575         n_overwrittenby = get_entity_n_overwrittenby(static_ent);
576         for (i = 0; i < n_overwrittenby; ++i) {
577                 ir_entity *ent = get_entity_overwrittenby(static_ent, i);
578                 ent = do_resolve_ent_polymorphy(dynamic_class, ent);
579                 if (ent) return ent;
580         }
581         return NULL;
582 }
583
584 /* Resolve polymorphy in the inheritance relation.
585  *
586  * Returns the dynamically referenced entity if the static entity and the
587  * dynamic type are given.
588  * Search downwards in overwritten tree.
589  */
590 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent)
591 {
592         ir_entity *res;
593         assert(static_ent && is_entity(static_ent));
594
595         res = do_resolve_ent_polymorphy(dynamic_class, static_ent);
596         assert(res);
597
598         return res;
599 }
600
601
602
603 /* ----------------------------------------------------------------------- */
604 /* Class cast state handling.                                              */
605 /* ----------------------------------------------------------------------- */
606
607 /* - State handling. ----------------------------------------- */
608
609 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s)
610 {
611         if (get_irp_class_cast_state() > s)
612                 set_irp_class_cast_state(s);
613         irg->class_cast_state = s;
614 }
615
616 ir_class_cast_state get_irg_class_cast_state(const ir_graph *irg)
617 {
618         return irg->class_cast_state;
619 }
620
621 void set_irp_class_cast_state(ir_class_cast_state s)
622 {
623 #ifndef NDEBUG
624         int i;
625         for (i = get_irp_n_irgs() - 1; i >= 0; --i)
626                 assert(get_irg_class_cast_state(get_irp_irg(i)) >= s);
627 #endif
628         irp->class_cast_state = s;
629 }
630
631 ir_class_cast_state get_irp_class_cast_state(void)
632 {
633         return irp->class_cast_state;
634 }
635
636 const char *get_class_cast_state_string(ir_class_cast_state s)
637 {
638 #define X(a)    case a: return #a
639         switch (s) {
640         X(ir_class_casts_any);
641         X(ir_class_casts_transitive);
642         X(ir_class_casts_normalized);
643         X(ir_class_casts_state_max);
644         default: return "invalid class cast state";
645         }
646 #undef X
647 }
648
649 /* - State verification. ------------------------------------- */
650
651 typedef struct ccs_env {
652         ir_class_cast_state expected_state;
653         ir_class_cast_state worst_situation;
654 } ccs_env;
655
656 /**
657  * Walker: check Casts.
658  */
659 static void verify_irn_class_cast_state(ir_node *n, void *env)
660 {
661         ccs_env             *ccs = (ccs_env *)env;
662         ir_class_cast_state this_state = ir_class_casts_any;
663         ir_type             *fromtype, *totype;
664         int                 ref_depth = 0;
665
666         if (!is_Cast(n)) return;
667
668         fromtype = get_irn_typeinfo_type(get_Cast_op(n));
669         totype   = get_Cast_type(n);
670
671         while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
672                 totype   = get_pointer_points_to_type(totype);
673                 fromtype = get_pointer_points_to_type(fromtype);
674                 ref_depth++;
675         }
676
677         if (!is_Class_type(totype)) return;
678
679         if (is_SubClass_of(totype, fromtype) ||
680                 is_SubClass_of(fromtype, totype)   ) {
681                 this_state = ir_class_casts_transitive;
682                 if ((get_class_supertype_index(totype, fromtype) != -1) ||
683                     (get_class_supertype_index(fromtype, totype) != -1) ||
684                     fromtype == totype) {
685                         /*   Das ist doch alt?  Aus dem cvs aufgetaucht ...
686                         if ((get_class_supertype_index(totype, fromtype) == -1) &&
687                             (get_class_supertype_index(fromtype, totype) == -1) ) {  */
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 }