whitespace
[libfirm] / ir / tr / trvrfy.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/tr/trvrfy.c
4  * Purpose:     Check types and entities for correctness.
5  * Author:      Michael Beck, Goetz Lindenmaier
6  * Modified by:
7  * Created:     29.1.2003
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include "trvrfy.h"
17 #include "irgraph_t.h"  /* for checking whether constant code is allocated
18                on proper obstack */
19
20 /**
21  * Check a class
22  */
23 static int check_class(type *tp) {
24   int i, j, k;
25   int found;
26
27   /*printf("\n"); DDMT(tp);*/
28
29   for (i = 0; i < get_class_n_members(tp); i++) {
30
31     entity *mem = get_class_member(tp, i);
32     assert(mem && "NULL members not allowed");
33     /*printf(" %d, %d", get_entity_n_overwrites(mem), get_class_n_supertypes(tp)); DDME(mem);*/
34     if (!mem) return error_null_mem;
35
36     if (get_entity_n_overwrites(mem) > get_class_n_supertypes(tp)) {
37       DDMT(tp); DDME(mem);
38       assert(get_entity_n_overwrites(mem) <= get_class_n_supertypes(tp));
39     }
40     for (j = 0; j < get_entity_n_overwrites(mem); j++) {
41       entity *ovw = get_entity_overwrites(mem, j);
42       /*printf(" overwrites: "); DDME(ovw);*/
43       /* Check whether ovw is member of one of tp's supertypes. If so,
44      the representation is correct. */
45       found = false;
46       for (k = 0; k < get_class_n_supertypes(tp); k++) {
47     if (get_class_member_index(get_class_supertype(tp, k), ovw) >= 0) {
48       found = true;
49       break;
50     }
51       }
52       if (!found) {
53     DDMT(tp); DDME(mem);
54     assert(found && "overwrites an entity not contained in direct supertype");
55     return error_ent_not_cont;
56       }
57     }
58
59   }
60   return 0;
61 }
62
63 /**
64  * Check an array.
65  */
66 static int check_array(type *tp) {
67   int i, n_dim = get_array_n_dimensions(tp);
68   for (i = 0; i < n_dim; ++i)
69     assert(has_array_lower_bound(tp, i) || has_array_upper_bound(tp, i));
70
71   return 0;
72 }
73
74
75 /**
76  * Check a primitive.
77  */
78 static int check_primitive(type *tp) {
79   assert(is_mode(get_type_mode(tp)));
80
81   return 0;
82 }
83
84
85 /**
86  * Checks a type.
87  *
88  * Currently checks class types only.
89  */
90 static int check_type(type *tp) {
91   switch (get_type_tpop_code(tp)) {
92   case tpo_class:
93     return check_class(tp);
94   case tpo_array:
95     return check_array(tp);
96   case tpo_primitive:
97     return check_primitive(tp);
98   default: break;
99   }
100   return 0;
101 }
102
103 /**
104  * helper environment struct for constant_on_wrong_obstack()
105  */
106 struct myenv {
107   int res;
108   ir_graph *irg;
109 };
110
111 /**
112  * called by the walker
113  */
114 static void on_irg_storage(ir_node *n, void *env) {
115   struct myenv * myenv = env;
116
117   myenv->res = node_is_in_irgs_storage(myenv->irg, n);
118
119   /* We also test whether the setting of the visited flag is legal. */
120   assert(get_irn_visited(n) <= get_irg_visited(myenv->irg) &&
121      "Visited flag of node is larger than that of corresponding irg.");
122 }
123
124 /**
125  * checks wheater a given constant IR node is NOT on the
126  * constant IR graph.
127  */
128 static int constant_on_wrong_irg(ir_node *n) {
129   struct myenv env;
130
131   env.res = 1;  /* on right obstack */
132   env.irg = get_const_code_irg();
133
134   irg_walk(n, on_irg_storage, NULL, (void *)&env);
135   return ! env.res;
136 }
137
138 /*
139  * Check if constants node are NOT on the constant IR graph.
140  */
141 static int constants_on_wrong_irg(entity *ent) {
142   if (get_entity_variability(ent) == variability_uninitialized) return 0;
143
144   if (is_compound_entity(ent)) {
145     int i;
146     for (i = 0; i < get_compound_ent_n_values(ent); i++) {
147       if (constant_on_wrong_irg(get_compound_ent_value(ent, i)))
148     return 1;
149     }
150   } else {
151     /* Might not be set if entity belongs to a description or is external allocated. */
152     if (get_atomic_ent_value(ent))
153       return constant_on_wrong_irg(get_atomic_ent_value(ent));
154     else if (get_entity_visibility(ent) != visibility_external_allocated)
155       assert((is_class_type(get_entity_owner(ent)) &&
156           get_class_peculiarity(get_entity_owner(ent)) == peculiarity_description) &&
157          "Value in constant atomic entity not set.");
158   }
159   return 0;
160 }
161
162 /*
163  * Check an entity. Currently, we check only if initialized constants
164  * are build on the const irg graph.
165  *
166  * @return
167  *  0   if no error encountered
168  *  != 0    else
169  */
170 static int check_entity(entity *ent) {
171   current_ir_graph =  get_const_code_irg();
172   if (constants_on_wrong_irg(ent)) {
173     assert(0 && "Contants placed on wrong IRG");
174     return error_const_on_wrong_irg;
175   }
176
177   if ((get_entity_peculiarity(ent) == peculiarity_existent) &&
178       (get_entity_visibility(ent) != visibility_external_allocated) &&
179       (is_method_type(get_entity_type(ent)))                &&
180       (!get_entity_irg(ent) || !(is_ir_graph(get_entity_irg(ent))))) {
181     assert(0 && "Method ents with pec_exist must have an irg");
182     return error_existent_entity_without_irg;
183   }
184
185   if (get_entity_peculiarity(ent) == peculiarity_inherited) {
186     entity *impl = get_SymConst_entity(get_atomic_ent_value(ent));
187     assert(get_entity_peculiarity(impl) == peculiarity_existent &&
188            "inherited entities must have constant pointing to existent entity.");
189   }
190
191   return 0;
192 }
193
194 /*
195  * check types and entities
196  */
197 static void check_tore(type_or_ent *tore, void *env) {
198   int *res = env;
199   assert(tore);
200   if (is_type(tore)) {
201     *res = check_type((type *)tore);
202   } else {
203     assert(is_entity(tore));
204     *res = check_entity((entity *)tore);
205   }
206 }
207
208 /*
209  * Verify types and entities.
210  */
211 int tr_vrfy(void) {
212   int res;
213
214   type_walk(check_tore, NULL, &res);
215   return res;
216 }