differentiate between EXPR_INVALID and EXPR_ERROR
[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         case EXPR_ERROR:
189                 return;
190         case EXPR_INVALID:
191                 break;
192         }
193         panic("invalid expr kind");
194 }
195
196 static void walk_designator(const designator_t *designator,
197                             const walk_env_t *const env)
198 {
199         for ( ; designator != NULL; designator = designator->next) {
200                 if (designator->array_index != NULL)
201                         walk_expression(designator->array_index, env);
202         }
203 }
204
205 static void walk_initializer(const initializer_t  *initializer,
206                              const walk_env_t *const env)
207 {
208         switch(initializer->kind) {
209         case INITIALIZER_VALUE:
210                 walk_expression(initializer->value.value, env);
211                 return;
212         case INITIALIZER_LIST: {
213                 for (size_t i = 0; i < initializer->list.len; ++i) {
214                         walk_initializer(initializer->list.initializers[i], env);
215                 }
216                 return;
217         }
218         case INITIALIZER_DESIGNATOR:
219                 walk_designator(initializer->designator.designator, env);
220                 return;
221         case INITIALIZER_STRING:
222         case INITIALIZER_WIDE_STRING:
223                 return;
224         }
225 }
226
227 static void walk_entity(entity_t *entity, const walk_env_t *const env)
228 {
229         env->declaration_func(entity, env->env);
230
231         switch (entity->kind) {
232         case ENTITY_VARIABLE: {
233                 const variable_t    *variable    = &entity->variable;
234                 const initializer_t *initializer = variable->initializer;
235                 walk_type(entity->declaration.type, env);
236                 if (initializer != NULL) {
237                         walk_initializer(initializer, env);
238                 }
239                 return;
240         }
241         case ENTITY_ENUM_VALUE:
242                 if (entity->enum_value.value != NULL)
243                         walk_expression(entity->enum_value.value, env);
244                 return;
245         case ENTITY_TYPEDEF:
246                 walk_type(entity->typedefe.type, env);
247                 return;
248         case ENTITY_FUNCTION:
249                 walk_type(entity->declaration.type, env);
250                 if (entity->function.statement != NULL)
251                         walk_statement(entity->function.statement, env);
252                 return;
253         case ENTITY_COMPOUND_MEMBER:
254         case ENTITY_PARAMETER:
255                 walk_type(entity->declaration.type, env);
256                 return;
257         case ENTITY_CLASS:
258         case ENTITY_STRUCT:
259         case ENTITY_UNION:
260                 walk_scope(&entity->compound.members, env);
261                 return;
262         case ENTITY_NAMESPACE:
263                 walk_scope(&entity->namespacee.members, env);
264                 return;
265         case ENTITY_ENUM:
266                 for (entity = entity->base.next;
267                      entity != NULL && entity->kind == ENTITY_ENUM_VALUE;
268                          entity = entity->base.next) {
269                         walk_entity(entity, env);
270                 }
271                 return;
272         case ENTITY_LABEL:
273         case ENTITY_LOCAL_LABEL:
274                 return;
275         case ENTITY_INVALID:
276                 break;
277         }
278         panic("invalid entity found");
279 }
280
281 static void walk_declarations(entity_t*            entity,
282                               entity_t*      const last,
283                                                           const walk_env_t    *const env)
284 {
285         entity_t const *const end = last != NULL ? last->base.next : NULL;
286         for (; entity != end; entity = entity->base.next) {
287                 walk_entity(entity, env);
288         }
289 }
290
291 static void walk_scope(const scope_t *const scope, const walk_env_t *const env)
292 {
293         walk_declarations(scope->entities, NULL, env);
294 }
295
296 static void walk_statement(statement_t *const stmt, const walk_env_t *const env)
297 {
298         env->statement_func(stmt, env->env);
299
300         switch (stmt->kind) {
301         case STATEMENT_COMPOUND:
302                 for (statement_t *s = stmt->compound.statements; s != NULL;
303                      s = s->base.next) {
304                         walk_statement(s, env);
305                 }
306                 return;
307
308         case STATEMENT_FOR:
309                 walk_declarations(stmt->fors.scope.entities, NULL, env);
310                 if (stmt->fors.initialisation != NULL)
311                         walk_expression(stmt->fors.initialisation, env);
312                 if (stmt->fors.condition != NULL)
313                         walk_expression(stmt->fors.condition, env);
314                 if (stmt->fors.step != NULL)
315                         walk_expression(stmt->fors.step, env);
316                 walk_statement(stmt->fors.body, env);
317                 return;
318
319         case STATEMENT_IF:
320                 walk_expression(stmt->ifs.condition, env);
321                 walk_statement(stmt->ifs.true_statement, env);
322                 if (stmt->ifs.false_statement != NULL)
323                         walk_statement(stmt->ifs.false_statement, env);
324                 return;
325
326         case STATEMENT_SWITCH:
327                 walk_expression(stmt->switchs.expression, env);
328                 walk_statement(stmt->switchs.body, env);
329                 return;
330
331         case STATEMENT_LABEL:
332                 walk_statement(stmt->label.statement, env);
333                 return;
334
335         case STATEMENT_CASE_LABEL:
336                 walk_statement(stmt->case_label.statement, env);
337                 return;
338
339         case STATEMENT_WHILE:
340                 walk_expression(stmt->whiles.condition, env);
341                 walk_statement(stmt->whiles.body, env);
342                 return;
343
344         case STATEMENT_DO_WHILE:
345                 walk_statement(stmt->do_while.body, env);
346                 walk_expression(stmt->do_while.condition, env);
347                 return;
348
349         case STATEMENT_EXPRESSION:
350                 walk_expression(stmt->expression.expression, env);
351                 return;
352
353         case STATEMENT_RETURN:
354                 if (stmt->returns.value != NULL)
355                         walk_expression(stmt->returns.value, env);
356                 return;
357
358         case STATEMENT_DECLARATION:
359                 walk_declarations(stmt->declaration.declarations_begin,
360                                 stmt->declaration.declarations_end, env);
361                 return;
362
363         case STATEMENT_MS_TRY:
364                 walk_statement(stmt->ms_try.try_statement, env);
365                 walk_statement(stmt->ms_try.final_statement, env);
366                 return;
367
368         case STATEMENT_INVALID:
369         case STATEMENT_EMPTY:
370         case STATEMENT_CONTINUE:
371         case STATEMENT_BREAK:
372         case STATEMENT_GOTO:
373         case STATEMENT_ASM:
374         case STATEMENT_LEAVE:
375                 return;
376         }
377
378         panic("unhandled statement");
379 }
380
381 static void null_declaration_func(entity_t *entity, void *env)
382 {
383         (void) entity;
384         (void) env;
385 }
386
387 static void null_statement_func(statement_t *statement, void *env)
388 {
389         (void) statement;
390         (void) env;
391 }
392
393 static void null_expression_func(expression_t *expression, void *env)
394 {
395         (void) expression;
396         (void) env;
397 }
398
399 void walk_translation_unit(translation_unit_t *unit,
400                            declaration_callback declaration_func,
401                                                    statement_callback statement_func,
402                                                    expression_callback expression_func, void *env)
403 {
404         walk_env_t walk_env = {
405                 pset_new_ptr_default(),
406                 declaration_func != NULL ? declaration_func : null_declaration_func,
407                 statement_func != NULL ? statement_func : null_statement_func,
408                 expression_func != NULL ? expression_func : null_expression_func,
409                 env
410         };
411         walk_scope(&unit->scope, &walk_env);
412         del_pset(walk_env.visited_types);
413 }
414
415 void walk_statements(statement_t *statement, statement_callback func, void *env)
416 {
417         walk_env_t walk_env
418                 = { pset_new_ptr_default(),
419                     null_declaration_func, func, null_expression_func, env };
420         walk_statement(statement, &walk_env);
421         del_pset(walk_env.visited_types);
422 }