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