b33e0f8706b86c1d8a04f62602176323315c7089
[libfirm] / ir / tr / trvrfy.c
1
2 #include "trvrfy.h"
3 #include "irgraph_t.h"  /* for checking whether constant code is allocated
4                            on proper obstack */
5
6 static int check_class(type *tp) {
7   int i, j, k;
8   int found;
9
10   //printf("\n"); DDMT(tp);
11
12   for (i = 0; i < get_class_n_members(tp); i++) {
13
14     entity *mem = get_class_member(tp, i);
15     assert(mem && "NULL members not allowed");
16     //printf(" %d, %d", get_entity_n_overwrites(mem), get_class_n_supertypes(tp)); DDME(mem);
17     if (!mem) return error_null_mem;
18     assert(get_entity_n_overwrites(mem) <= get_class_n_supertypes(tp));
19     for (j = 0; j < get_entity_n_overwrites(mem); j++) {
20       entity *ovw = get_entity_overwrites(mem, j);
21       //printf(" overwrites: "); DDME(ovw);
22       /* Check whether ovw is member of one of tp's supertypes. If so,
23          the representation is correct. */
24       found = false;
25       for (k = 0; k < get_class_n_supertypes(tp); k++) {
26         if (get_class_member_index(get_class_supertype(tp, k), ovw) >= 0) {
27           found = true;
28           break;
29         }
30       }
31       if (!found) {
32         DDMT(tp); DDME(mem);
33         assert(found && "overwrites an entity not contained in direct supertype");
34         return error_ent_not_cont;
35       }
36     }
37
38   }
39   return 0;
40 }
41
42 static int check_type(type *tp) {
43   switch (get_type_tpop_code(tp)) {
44   case tpo_class:
45     return check_class(tp);
46   default: break;
47   }
48   return 0;
49 }
50
51 struct myenv {
52   int res;
53   struct obstack *obst;
54 };
55
56 static void on_obstack(ir_node *n, void *env) {
57   struct obstack *obst = ((struct myenv *)env)->obst;
58
59   /* n must be on the obstack obst. */
60
61   ((struct myenv *)env)->res = 0;
62 }
63
64 static int constant_on_wrong_obstack(ir_node *n) {
65   struct myenv env;
66   env.res = 0;  /* false, not on wrong obstack */
67   env.obst = get_irg_obstack(get_const_code_irg());
68   irg_walk(n, on_obstack, NULL, (void *)&env);
69   return env.res;
70 }
71
72 static int constants_on_wrong_obstack(entity *ent) {
73   if (get_entity_variability(ent) == uninitialized) return 0;
74
75   if (is_compound_entity(ent)) {
76     int i;
77     for (i = 0; i < get_compound_ent_n_values(ent); i++) {
78       if (constant_on_wrong_obstack(get_compound_ent_value(ent, i)));
79         return 1;
80     }
81   } else {
82     return constant_on_wrong_obstack(get_atomic_ent_value(ent));
83   }
84   return 0;
85 }
86
87 static int check_entity(entity *ent) {
88   if (constants_on_wrong_obstack(ent))
89     return error_const_on_wrong_obstack;
90   return 0;
91 }
92
93 static void check_tore(type_or_ent *tore, void *env) {
94   int *res = env;
95   if (is_type(tore)) {
96     *res = check_type((type *)tore);
97   } else {
98     assert(is_entity(tore));
99     *res = check_entity((entity *)tore);
100   }
101 }
102
103
104 int tr_vrfy(void) {
105   int res;
106
107   type_walk(check_tore, NULL, &res);
108   return res;
109 }