improve ast-walk infrastructure
[cparser] / walk.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #include <config.h>
21
22 #include "adt/error.h"
23 #include "ast_t.h"
24 #include "entity_t.h"
25 #include "type_t.h"
26 #include "walk.h"
27 #include <libfirm/adt/pset.h>
28
29 typedef struct walk_env_t {
30         pset *visited_types;
31         declaration_callback declaration_func;
32         statement_callback   statement_func;
33         expression_callback  expression_func;
34         void *env;
35 } walk_env_t;
36
37 static void walk_expression(expression_t *expr, const walk_env_t *const env);
38 static void walk_statement(statement_t *stmt, const walk_env_t *env);
39 static void walk_entity(entity_t *entity, const walk_env_t *env);
40 static void walk_designator(const designator_t *, const walk_env_t *);
41 static void walk_initializer(const initializer_t  *, const walk_env_t *);
42 static void walk_scope(const scope_t *const scope, const walk_env_t *const env);
43
44 static void walk_type(type_t *const type, const walk_env_t *const env)
45 {
46         if (pset_find_ptr(env->visited_types, type) != NULL)
47                 return;
48         pset_insert_ptr(env->visited_types, type);
49
50         switch (type->kind) {
51         case TYPE_ATOMIC:
52         case TYPE_COMPLEX:
53         case TYPE_IMAGINARY:
54         case TYPE_REFERENCE:
55         case TYPE_ERROR:
56                 return;
57         case TYPE_POINTER:
58                 walk_type(type->pointer.points_to, env);
59                 return;
60         case TYPE_ARRAY:
61                 walk_type(type->array.element_type, env);
62                 if (type->array.size_expression != NULL)
63                         walk_expression(type->array.size_expression, env);
64                 return;
65         case TYPE_FUNCTION:
66                 for (function_parameter_t *parameter = type->function.parameters;
67                      parameter != NULL; parameter = parameter->next) {
68                         walk_type(parameter->type, env);
69                 }
70                 walk_type(type->function.return_type, env);
71                 return;
72         case TYPE_TYPEOF:
73                 walk_expression(type->typeoft.expression, env);
74                 return;
75         case TYPE_TYPEDEF:
76                 walk_entity((entity_t*)type->typedeft.typedefe, env);
77                 return;
78         case TYPE_COMPOUND_STRUCT:
79         case TYPE_COMPOUND_UNION:
80                 walk_entity((entity_t*)type->compound.compound, env);
81                 return;
82         case TYPE_ENUM:
83                 walk_entity((entity_t*)type->enumt.enume, env);
84                 return;
85         case TYPE_INVALID:
86                 break;
87         }
88         panic("invalid type found");
89 }
90
91 static void walk_expression(expression_t *const expr,
92                             const walk_env_t *const env)
93 {
94         env->expression_func(expr, env->env);
95
96         switch (expr->base.kind) {
97         case EXPR_STATEMENT:
98                 walk_statement(expr->statement.statement, env);
99                 return;
100
101         EXPR_BINARY_CASES
102                 walk_expression(expr->binary.left, env);
103                 walk_expression(expr->binary.right, env);
104                 return;
105
106         EXPR_UNARY_CASES_OPTIONAL
107                 if (expr->unary.value == NULL)
108                         return;
109                 /* FALLTHROUGH */
110         EXPR_UNARY_CASES_MANDATORY
111                 walk_expression(expr->unary.value, env);
112                 return;
113
114         case EXPR_CALL:
115                 for (call_argument_t *arg = expr->call.arguments; arg != NULL;
116                      arg = arg->next) {
117                         walk_expression(arg->expression, env);
118                 }
119                 return;
120
121         case EXPR_COMPOUND_LITERAL:
122                 walk_initializer(expr->compound_literal.initializer, env);
123                 return;
124
125         case EXPR_CONDITIONAL:
126                 walk_expression(expr->conditional.condition, env);
127                 /* may be NULL because of gnu extension */
128                 if (expr->conditional.true_expression != NULL)
129                         walk_expression(expr->conditional.true_expression, env);
130                 walk_expression(expr->conditional.false_expression, env);
131                 return;
132
133         case EXPR_BUILTIN_CONSTANT_P:
134                 walk_expression(expr->builtin_constant.value, env);
135                 return;
136
137         case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
138                 walk_type(expr->builtin_types_compatible.left, env);
139                 walk_type(expr->builtin_types_compatible.right, env);
140                 return;
141
142         case EXPR_SELECT:
143                 walk_expression(expr->select.compound, env);
144                 return;
145
146         case EXPR_ARRAY_ACCESS:
147                 walk_expression(expr->array_access.array_ref, env);
148                 walk_expression(expr->array_access.index, env);
149                 return;
150
151         case EXPR_CLASSIFY_TYPE:
152                 walk_expression(expr->classify_type.type_expression, env);
153                 return;
154
155         case EXPR_SIZEOF:
156         case EXPR_ALIGNOF: {
157                 expression_t *tp_expression = expr->typeprop.tp_expression;
158                 if (tp_expression != NULL) {
159                         walk_expression(tp_expression, env);
160                 }
161                 return;
162         }
163
164         case EXPR_VA_START:
165                 walk_expression(expr->va_starte.ap, env);
166                 return;
167
168         case EXPR_VA_ARG:
169                 walk_expression(expr->va_arge.ap, env);
170                 return;
171
172         case EXPR_VA_COPY:
173                 walk_expression(expr->va_copye.src, env);
174                 walk_expression(expr->va_copye.dst, env);
175                 return;
176
177         case EXPR_OFFSETOF:
178                 walk_designator(expr->offsetofe.designator, env);
179                 return;
180
181         EXPR_LITERAL_CASES
182         case EXPR_REFERENCE:
183         case EXPR_REFERENCE_ENUM_VALUE:
184         case EXPR_STRING_LITERAL:
185         case EXPR_WIDE_STRING_LITERAL:
186         case EXPR_FUNCNAME:
187         case EXPR_LABEL_ADDRESS:
188                 return;
189         case EXPR_INVALID:
190                 break;
191         }
192         panic("invalid expr kind");
193 }
194
195 static void walk_designator(const designator_t *designator,
196                             const walk_env_t *const env)
197 {
198         for ( ; designator != NULL; designator = designator->next) {
199                 walk_expression(designator->array_index, env);
200         }
201 }
202
203 static void walk_initializer(const initializer_t  *initializer,
204                              const walk_env_t *const env)
205 {
206         switch(initializer->kind) {
207         case INITIALIZER_VALUE:
208                 walk_expression(initializer->value.value, env);
209                 return;
210         case INITIALIZER_LIST: {
211                 for (size_t i = 0; i < initializer->list.len; ++i) {
212                         walk_initializer(initializer->list.initializers[i], env);
213                 }
214                 return;
215         }
216         case INITIALIZER_DESIGNATOR:
217                 walk_designator(initializer->designator.designator, env);
218                 return;
219         case INITIALIZER_STRING:
220         case INITIALIZER_WIDE_STRING:
221                 return;
222         }
223 }
224
225 static void walk_entity(entity_t *entity, const walk_env_t *const env)
226 {
227         env->declaration_func(entity, env->env);
228
229         switch (entity->kind) {
230         case ENTITY_VARIABLE: {
231                 const variable_t    *variable    = &entity->variable;
232                 const initializer_t *initializer = variable->initializer;
233                 walk_type(entity->declaration.type, env);
234                 if (initializer != NULL) {
235                         walk_initializer(initializer, env);
236                 }
237                 return;
238         }
239         case ENTITY_ENUM_VALUE:
240                 walk_expression(entity->enum_value.value, env);
241                 return;
242         case ENTITY_TYPEDEF:
243                 walk_type(entity->typedefe.type, env);
244                 return;
245         case ENTITY_FUNCTION:
246                 walk_type(entity->declaration.type, env);
247                 if (entity->function.statement != NULL)
248                         walk_statement(entity->function.statement, env);
249                 return;
250         case ENTITY_COMPOUND_MEMBER:
251         case ENTITY_PARAMETER:
252                 walk_type(entity->declaration.type, env);
253                 return;
254         case ENTITY_CLASS:
255         case ENTITY_STRUCT:
256         case ENTITY_UNION:
257                 walk_scope(&entity->compound.members, env);
258                 return;
259         case ENTITY_NAMESPACE:
260                 walk_scope(&entity->namespacee.members, env);
261                 return;
262         case ENTITY_ENUM:
263                 for (entity = entity->base.next;
264                      entity != NULL && entity->kind == ENTITY_ENUM_VALUE;
265                          entity = entity->base.next) {
266                         walk_entity(entity, env);
267                 }
268                 return;
269         case ENTITY_LABEL:
270         case ENTITY_LOCAL_LABEL:
271                 return;
272         case ENTITY_INVALID:
273                 break;
274         }
275         panic("invalid entity found");
276 }
277
278 static void walk_declarations(entity_t*            entity,
279                               entity_t*      const last,
280                                                           const walk_env_t    *const env)
281 {
282         entity_t const *const end = last != NULL ? last->base.next : NULL;
283         for (; entity != end; entity = entity->base.next) {
284                 walk_entity(entity, env);
285         }
286 }
287
288 static void walk_scope(const scope_t *const scope, const walk_env_t *const env)
289 {
290         walk_declarations(scope->entities, NULL, env);
291 }
292
293 static void walk_statement(statement_t *const stmt, const walk_env_t *const env)
294 {
295         env->statement_func(stmt, env->env);
296
297         switch (stmt->kind) {
298         case STATEMENT_COMPOUND:
299                 for (statement_t *s = stmt->compound.statements; s != NULL;
300                      s = s->base.next) {
301                         walk_statement(s, env);
302                 }
303                 return;
304
305         case STATEMENT_FOR:
306                 walk_declarations(stmt->fors.scope.entities, NULL, env);
307                 if (stmt->fors.initialisation != NULL)
308                         walk_expression(stmt->fors.initialisation, env);
309                 if (stmt->fors.condition != NULL)
310                         walk_expression(stmt->fors.condition, env);
311                 if (stmt->fors.step != NULL)
312                         walk_expression(stmt->fors.step, env);
313                 walk_statement(stmt->fors.body, env);
314                 return;
315
316         case STATEMENT_IF:
317                 walk_expression(stmt->ifs.condition, env);
318                 walk_statement(stmt->ifs.true_statement, env);
319                 if (stmt->ifs.false_statement != NULL)
320                         walk_statement(stmt->ifs.false_statement, env);
321                 return;
322
323         case STATEMENT_SWITCH:
324                 walk_expression(stmt->switchs.expression, env);
325                 walk_statement(stmt->switchs.body, env);
326                 return;
327
328         case STATEMENT_LABEL:
329                 walk_statement(stmt->label.statement, env);
330                 return;
331
332         case STATEMENT_CASE_LABEL:
333                 walk_statement(stmt->case_label.statement, env);
334                 return;
335
336         case STATEMENT_WHILE:
337                 walk_expression(stmt->whiles.condition, env);
338                 walk_statement(stmt->whiles.body, env);
339                 return;
340
341         case STATEMENT_DO_WHILE:
342                 walk_statement(stmt->do_while.body, env);
343                 walk_expression(stmt->do_while.condition, env);
344                 return;
345
346         case STATEMENT_EXPRESSION:
347                 walk_expression(stmt->expression.expression, env);
348                 return;
349
350         case STATEMENT_RETURN:
351                 if (stmt->returns.value != NULL)
352                         walk_expression(stmt->returns.value, env);
353                 return;
354
355         case STATEMENT_DECLARATION:
356                 walk_declarations(stmt->declaration.declarations_begin,
357                                 stmt->declaration.declarations_end, env);
358                 return;
359
360         case STATEMENT_MS_TRY:
361                 walk_statement(stmt->ms_try.try_statement, env);
362                 walk_statement(stmt->ms_try.final_statement, env);
363                 return;
364
365         case STATEMENT_INVALID:
366         case STATEMENT_EMPTY:
367         case STATEMENT_CONTINUE:
368         case STATEMENT_BREAK:
369         case STATEMENT_GOTO:
370         case STATEMENT_ASM:
371         case STATEMENT_LEAVE:
372                 return;
373         }
374
375         panic("unhandled statement");
376 }
377
378 static void null_declaration_func(entity_t *entity, void *env)
379 {
380         (void) entity;
381         (void) env;
382 }
383
384 static void null_statement_func(statement_t *statement, void *env)
385 {
386         (void) statement;
387         (void) env;
388 }
389
390 static void null_expression_func(expression_t *expression, void *env)
391 {
392         (void) expression;
393         (void) env;
394 }
395
396 void walk_translation_unit(translation_unit_t *unit,
397                            declaration_callback declaration_func,
398                                                    statement_callback statement_func,
399                                                    expression_callback expression_func, void *env)
400 {
401         walk_env_t walk_env = {
402                 pset_new_ptr_default(),
403                 declaration_func != NULL ? declaration_func : null_declaration_func,
404                 statement_func != NULL ? statement_func : null_statement_func,
405                 expression_func != NULL ? expression_func : null_expression_func,
406                 env
407         };
408         walk_scope(&unit->scope, &walk_env);
409         del_pset(walk_env.visited_types);
410 }
411
412 void walk_statements(statement_t *statement, statement_callback func, void *env)
413 {
414         walk_env_t walk_env
415                 = { pset_new_ptr_default(),
416                     null_declaration_func, func, null_expression_func, env };
417         walk_statement(statement, &walk_env);
418         del_pset(walk_env.visited_types);
419 }