2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
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.
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.
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
22 #include "adt/error.h"
27 #include <libfirm/adt/pset.h>
29 typedef struct walk_env_t {
31 declaration_callback declaration_func;
32 statement_callback statement_func;
33 expression_callback expression_func;
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);
44 static void walk_type(type_t *const type, const walk_env_t *const env)
46 if (pset_find_ptr(env->visited_types, type) != NULL)
48 pset_insert_ptr(env->visited_types, type);
58 walk_type(type->pointer.points_to, env);
61 walk_type(type->array.element_type, env);
62 if (type->array.size_expression != NULL)
63 walk_expression(type->array.size_expression, env);
66 for (function_parameter_t *parameter = type->function.parameters;
67 parameter != NULL; parameter = parameter->next) {
68 walk_type(parameter->type, env);
70 walk_type(type->function.return_type, env);
73 walk_expression(type->typeoft.expression, env);
76 walk_entity((entity_t*)type->typedeft.typedefe, env);
78 case TYPE_COMPOUND_STRUCT:
79 case TYPE_COMPOUND_UNION:
80 walk_entity((entity_t*)type->compound.compound, env);
83 walk_entity((entity_t*)type->enumt.enume, env);
88 panic("invalid type found");
91 static void walk_expression(expression_t *const expr,
92 const walk_env_t *const env)
94 env->expression_func(expr, env->env);
96 switch (expr->base.kind) {
98 walk_statement(expr->statement.statement, env);
102 walk_expression(expr->binary.left, env);
103 walk_expression(expr->binary.right, env);
106 EXPR_UNARY_CASES_OPTIONAL
107 if (expr->unary.value == NULL)
110 EXPR_UNARY_CASES_MANDATORY
111 walk_expression(expr->unary.value, env);
115 for (call_argument_t *arg = expr->call.arguments; arg != NULL;
117 walk_expression(arg->expression, env);
121 case EXPR_COMPOUND_LITERAL:
122 walk_initializer(expr->compound_literal.initializer, env);
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);
133 case EXPR_BUILTIN_CONSTANT_P:
134 walk_expression(expr->builtin_constant.value, env);
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);
143 walk_expression(expr->select.compound, env);
146 case EXPR_ARRAY_ACCESS:
147 walk_expression(expr->array_access.array_ref, env);
148 walk_expression(expr->array_access.index, env);
151 case EXPR_CLASSIFY_TYPE:
152 walk_expression(expr->classify_type.type_expression, env);
157 expression_t *tp_expression = expr->typeprop.tp_expression;
158 if (tp_expression != NULL) {
159 walk_expression(tp_expression, env);
165 walk_expression(expr->va_starte.ap, env);
169 walk_expression(expr->va_arge.ap, env);
173 walk_expression(expr->va_copye.src, env);
174 walk_expression(expr->va_copye.dst, env);
178 walk_designator(expr->offsetofe.designator, env);
183 case EXPR_REFERENCE_ENUM_VALUE:
184 case EXPR_STRING_LITERAL:
185 case EXPR_WIDE_STRING_LITERAL:
187 case EXPR_LABEL_ADDRESS:
192 panic("invalid expr kind");
195 static void walk_designator(const designator_t *designator,
196 const walk_env_t *const env)
198 for ( ; designator != NULL; designator = designator->next) {
199 walk_expression(designator->array_index, env);
203 static void walk_initializer(const initializer_t *initializer,
204 const walk_env_t *const env)
206 switch(initializer->kind) {
207 case INITIALIZER_VALUE:
208 walk_expression(initializer->value.value, env);
210 case INITIALIZER_LIST: {
211 for (size_t i = 0; i < initializer->list.len; ++i) {
212 walk_initializer(initializer->list.initializers[i], env);
216 case INITIALIZER_DESIGNATOR:
217 walk_designator(initializer->designator.designator, env);
219 case INITIALIZER_STRING:
220 case INITIALIZER_WIDE_STRING:
225 static void walk_entity(entity_t *entity, const walk_env_t *const env)
227 env->declaration_func(entity, env->env);
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);
239 case ENTITY_ENUM_VALUE:
240 walk_expression(entity->enum_value.value, env);
243 walk_type(entity->typedefe.type, env);
245 case ENTITY_FUNCTION:
246 walk_type(entity->declaration.type, env);
247 if (entity->function.statement != NULL)
248 walk_statement(entity->function.statement, env);
250 case ENTITY_COMPOUND_MEMBER:
251 case ENTITY_PARAMETER:
252 walk_type(entity->declaration.type, env);
257 walk_scope(&entity->compound.members, env);
259 case ENTITY_NAMESPACE:
260 walk_scope(&entity->namespacee.members, env);
263 for (entity = entity->base.next;
264 entity != NULL && entity->kind == ENTITY_ENUM_VALUE;
265 entity = entity->base.next) {
266 walk_entity(entity, env);
270 case ENTITY_LOCAL_LABEL:
275 panic("invalid entity found");
278 static void walk_declarations(entity_t* entity,
279 entity_t* const last,
280 const walk_env_t *const env)
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);
288 static void walk_scope(const scope_t *const scope, const walk_env_t *const env)
290 walk_declarations(scope->entities, NULL, env);
293 static void walk_statement(statement_t *const stmt, const walk_env_t *const env)
295 env->statement_func(stmt, env->env);
297 switch (stmt->kind) {
298 case STATEMENT_COMPOUND:
299 for (statement_t *s = stmt->compound.statements; s != NULL;
301 walk_statement(s, env);
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);
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);
323 case STATEMENT_SWITCH:
324 walk_expression(stmt->switchs.expression, env);
325 walk_statement(stmt->switchs.body, env);
328 case STATEMENT_LABEL:
329 walk_statement(stmt->label.statement, env);
332 case STATEMENT_CASE_LABEL:
333 walk_statement(stmt->case_label.statement, env);
336 case STATEMENT_WHILE:
337 walk_expression(stmt->whiles.condition, env);
338 walk_statement(stmt->whiles.body, env);
341 case STATEMENT_DO_WHILE:
342 walk_statement(stmt->do_while.body, env);
343 walk_expression(stmt->do_while.condition, env);
346 case STATEMENT_EXPRESSION:
347 walk_expression(stmt->expression.expression, env);
350 case STATEMENT_RETURN:
351 if (stmt->returns.value != NULL)
352 walk_expression(stmt->returns.value, env);
355 case STATEMENT_DECLARATION:
356 walk_declarations(stmt->declaration.declarations_begin,
357 stmt->declaration.declarations_end, env);
360 case STATEMENT_MS_TRY:
361 walk_statement(stmt->ms_try.try_statement, env);
362 walk_statement(stmt->ms_try.final_statement, env);
365 case STATEMENT_INVALID:
366 case STATEMENT_EMPTY:
367 case STATEMENT_CONTINUE:
368 case STATEMENT_BREAK:
371 case STATEMENT_LEAVE:
375 panic("unhandled statement");
378 static void null_declaration_func(entity_t *entity, void *env)
384 static void null_statement_func(statement_t *statement, void *env)
390 static void null_expression_func(expression_t *expression, void *env)
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)
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,
408 walk_scope(&unit->scope, &walk_env);
409 del_pset(walk_env.visited_types);
412 void walk_statements(statement_t *statement, statement_callback func, void *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);