1c4744f5866fb2fff8312afa6ec1ec4635d0eb9b
[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 /**
7  * Check a class
8  */
9 static int check_class(type *tp) {
10   int i, j, k;
11   int found;
12
13   /*printf("\n"); DDMT(tp);*/
14
15   for (i = 0; i < get_class_n_members(tp); i++) {
16
17     entity *mem = get_class_member(tp, i);
18     assert(mem && "NULL members not allowed");
19     /*printf(" %d, %d", get_entity_n_overwrites(mem), get_class_n_supertypes(tp)); DDME(mem);*/
20     if (!mem) return error_null_mem;
21
22     assert(get_entity_n_overwrites(mem) <= get_class_n_supertypes(tp));
23     for (j = 0; j < get_entity_n_overwrites(mem); j++) {
24       entity *ovw = get_entity_overwrites(mem, j);
25       /*printf(" overwrites: "); DDME(ovw);*/
26       /* Check whether ovw is member of one of tp's supertypes. If so,
27          the representation is correct. */
28       found = false;
29       for (k = 0; k < get_class_n_supertypes(tp); k++) {
30         if (get_class_member_index(get_class_supertype(tp, k), ovw) >= 0) {
31           found = true;
32           break;
33         }
34       }
35       if (!found) {
36         DDMT(tp); DDME(mem);
37         assert(found && "overwrites an entity not contained in direct supertype");
38         return error_ent_not_cont;
39       }
40     }
41
42   }
43   return 0;
44 }
45
46 /**
47  * Checks a type.
48  *
49  * Currently checks class types only.
50  */
51 static int check_type(type *tp) {
52   switch (get_type_tpop_code(tp)) {
53   case tpo_class:
54     return check_class(tp);
55   default: break;
56   }
57   return 0;
58 }
59
60 /**
61  * helper environment struct for constant_on_wrong_obstack()
62  */
63 struct myenv {
64   int res;
65   ir_graph *irg;
66 };
67
68 /**
69  * called by the walker
70  */
71 static void on_irg_storage(ir_node *n, void *env) {
72   struct myenv * myenv = env;
73
74   myenv->res = node_is_in_irgs_storage(myenv->irg, n);
75 }
76
77 /**
78  * checks wheater a given constant IR node is NOT on the
79  * constant IR graph.
80  */
81 static int constant_on_wrong_irg(ir_node *n) {
82   struct myenv env;
83
84   env.res = 1;  /* on right obstack */
85   env.irg = get_const_code_irg();
86
87   irg_walk(n, on_irg_storage, NULL, (void *)&env);
88   return ! env.res;
89 }
90
91 /*
92  * Check if constants node are NOT on the constant IR graph.
93  */
94 static int constants_on_wrong_irg(entity *ent) {
95   if (get_entity_variability(ent) == uninitialized) return 0;
96
97   if (is_compound_entity(ent)) {
98     int i;
99     for (i = 0; i < get_compound_ent_n_values(ent); i++) {
100       if (constant_on_wrong_irg(get_compound_ent_value(ent, i)))
101         return 1;
102     }
103   } else {
104     /* Might not be set if entity belongs to a description. */
105     if (get_atomic_ent_value(ent))
106       return constant_on_wrong_irg(get_atomic_ent_value(ent));
107     else
108       assert((is_class_type(get_entity_owner(ent)) &&
109               get_class_peculiarity(get_entity_owner(ent)) == description) &&
110              "Value in constant atomic entity not set.");
111   }
112   return 0;
113 }
114
115 /*
116  * Check an entity. Currently, we check only if initialized constants
117  * are build on the const irg graph.
118  *
119  * @return
120  *      0       if no error encountered
121  *      != 0    else
122  */
123 static int check_entity(entity *ent) {
124   if (constants_on_wrong_irg(ent)) {
125     assert(0 && "Contants placed on wrong IRG");
126     return error_const_on_wrong_irg;
127   }
128   return 0;
129 }
130
131 /*
132  * check types and entities
133  */
134 static void check_tore(type_or_ent *tore, void *env) {
135   int *res = env;
136   if (is_type(tore)) {
137     *res = check_type((type *)tore);
138   } else {
139     assert(is_entity(tore));
140     *res = check_entity((entity *)tore);
141   }
142 }
143
144 /*
145  * Verify types and entities.
146  */
147 int tr_vrfy(void) {
148   int res;
149
150   type_walk(check_tore, NULL, &res);
151   return res;
152 }