2e042704a2b4ad7e33564a65ac6be7cd200d6630
[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  * Checks a type.
76  *
77  * Currently checks class types only.
78  */
79 static int check_type(type *tp) {
80   switch (get_type_tpop_code(tp)) {
81   case tpo_class:
82     return check_class(tp);
83   case tpo_array:
84     return check_array(tp);
85   default: break;
86   }
87   return 0;
88 }
89
90 /**
91  * helper environment struct for constant_on_wrong_obstack()
92  */
93 struct myenv {
94   int res;
95   ir_graph *irg;
96 };
97
98 /**
99  * called by the walker
100  */
101 static void on_irg_storage(ir_node *n, void *env) {
102   struct myenv * myenv = env;
103
104   myenv->res = node_is_in_irgs_storage(myenv->irg, n);
105
106   /* We also test whether the setting of the visited flag is legal. */
107   assert(get_irn_visited(n) <= get_irg_visited(myenv->irg) &&
108          "Visited flag of node is larger than that of corresponding irg.");
109 }
110
111 /**
112  * checks wheater a given constant IR node is NOT on the
113  * constant IR graph.
114  */
115 static int constant_on_wrong_irg(ir_node *n) {
116   struct myenv env;
117
118   env.res = 1;  /* on right obstack */
119   env.irg = get_const_code_irg();
120
121   irg_walk(n, on_irg_storage, NULL, (void *)&env);
122   return ! env.res;
123 }
124
125 /*
126  * Check if constants node are NOT on the constant IR graph.
127  */
128 static int constants_on_wrong_irg(entity *ent) {
129   if (get_entity_variability(ent) == variability_uninitialized) return 0;
130
131   if (is_compound_entity(ent)) {
132     int i;
133     for (i = 0; i < get_compound_ent_n_values(ent); i++) {
134       if (constant_on_wrong_irg(get_compound_ent_value(ent, i)))
135         return 1;
136     }
137   } else {
138     /* Might not be set if entity belongs to a description or is external allocated. */
139     if (get_atomic_ent_value(ent))
140       return constant_on_wrong_irg(get_atomic_ent_value(ent));
141     else if (get_entity_visibility(ent) != visibility_external_allocated)
142       assert((is_class_type(get_entity_owner(ent)) &&
143               get_class_peculiarity(get_entity_owner(ent)) == peculiarity_description) &&
144              "Value in constant atomic entity not set.");
145   }
146   return 0;
147 }
148
149 /*
150  * Check an entity. Currently, we check only if initialized constants
151  * are build on the const irg graph.
152  *
153  * @return
154  *      0       if no error encountered
155  *      != 0    else
156  */
157 static int check_entity(entity *ent) {
158   current_ir_graph =  get_const_code_irg();
159   if (constants_on_wrong_irg(ent)) {
160     assert(0 && "Contants placed on wrong IRG");
161     return error_const_on_wrong_irg;
162   }
163
164   if ((get_entity_peculiarity(ent) == peculiarity_existent) &&
165       (get_entity_visibility(ent) != visibility_external_allocated) &&
166       (is_method_type(get_entity_type(ent)))                &&
167       (!get_entity_irg(ent) || !(is_ir_graph(get_entity_irg(ent))))) {
168     assert(0 && "Method ents with pec_exist must have an irg");
169     return error_existent_entity_without_irg;
170   }
171
172   return 0;
173 }
174
175 /*
176  * check types and entities
177  */
178 static void check_tore(type_or_ent *tore, void *env) {
179   int *res = env;
180   assert(tore);
181   if (is_type(tore)) {
182     *res = check_type((type *)tore);
183   } else {
184     assert(is_entity(tore));
185     *res = check_entity((entity *)tore);
186   }
187 }
188
189 /*
190  * Verify types and entities.
191  */
192 int tr_vrfy(void) {
193   int res;
194
195   type_walk(check_tore, NULL, &res);
196   return res;
197 }