+/* a walker for irn's */
+static void irn_type_walker(
+ ir_node *node, type_walk_func *pre, type_walk_func *post, void *env);
+
+static void walk_initializer(ir_initializer_t *initializer,
+ type_walk_func *pre, type_walk_func *post,
+ void *env)
+{
+ switch (initializer->kind) {
+ case IR_INITIALIZER_CONST:
+ irn_type_walker(initializer->consti.value, pre, post, env);
+ return;
+ case IR_INITIALIZER_TARVAL:
+ case IR_INITIALIZER_NULL:
+ return;
+
+ case IR_INITIALIZER_COMPOUND: {
+ size_t i;
+ for (i = 0; i < initializer->compound.n_initializers; ++i) {
+ ir_initializer_t *subinitializer
+ = initializer->compound.initializers[i];
+ walk_initializer(subinitializer, pre, post, env);
+ }
+ return;
+ }
+ }
+ panic("invalid initializer found");
+}
+
+/**
+ * Main walker: walks over all used types/entities of a
+ * type entity.
+ */
+static void do_type_walk(type_or_ent tore,
+ type_walk_func *pre,
+ type_walk_func *post,
+ void *env)
+{
+ size_t i, n_types, n_mem;
+ ir_entity *ent = NULL;
+ ir_type *tp = NULL;
+ ir_node *n;
+ type_or_ent cont;
+
+ /* marked? */
+ switch (get_kind(tore.ent)) {
+ case k_entity:
+ ent = tore.ent;
+ if (entity_visited(ent))
+ return;
+ mark_entity_visited(ent);
+ break;
+ case k_type:
+ tp = tore.typ;
+ if (type_visited(tp))
+ return;
+ mark_type_visited(tp);
+ break;
+ default:
+ break;
+ }
+
+ /* execute pre method */
+ if (pre)
+ pre(tore, env);
+
+ /* iterate */
+ switch (get_kind(tore.ent)) {
+ case k_entity:
+ cont.typ = get_entity_owner(ent);
+ do_type_walk(cont, pre, post, env);
+ cont.typ = get_entity_type(ent);
+ do_type_walk(cont, pre, post, env);
+
+ /* walk over the value types */
+ if (ent->initializer != NULL) {
+ walk_initializer(ent->initializer, pre, post, env);
+ } else if (entity_has_compound_ent_values(ent)) {
+ size_t i, n_mem = get_compound_ent_n_values(ent);
+ for (i = 0; i < n_mem; ++i) {
+ n = get_compound_ent_value(ent, i);
+ irn_type_walker(n, pre, post, env);
+ }
+ }
+ break;
+ case k_type:
+ switch (get_type_tpop_code(tp)) {
+ case tpo_class:
+ n_types = get_class_n_supertypes(tp);
+ for (i = 0; i < n_types; ++i) {
+ cont.typ = get_class_supertype(tp, i);
+ do_type_walk(cont, pre, post, env);
+ }
+ n_mem = get_class_n_members(tp);
+ for (i = 0; i < n_mem; ++i) {
+ cont.ent = get_class_member(tp, i);
+ do_type_walk(cont, pre, post, env);
+ }
+ n_types = get_class_n_subtypes(tp);
+ for (i = 0; i < n_types; ++i) {
+ cont.typ = get_class_subtype(tp, i);
+ do_type_walk(cont, pre, post, env);
+ }
+ break;
+
+ case tpo_struct:
+ n_mem = get_struct_n_members(tp);
+ for (i = 0; i < n_mem; ++i) {
+ cont.ent = get_struct_member(tp, i);
+ do_type_walk(cont, pre, post, env);
+ }
+ break;
+
+ case tpo_method:
+ n_mem = get_method_n_params(tp);
+ for (i = 0; i < n_mem; ++i) {
+ cont.typ = get_method_param_type(tp, i);
+ do_type_walk(cont, pre, post, env);
+ }
+ n_mem = get_method_n_ress(tp);
+ for (i = 0; i < n_mem; ++i) {
+ cont.typ = get_method_res_type(tp, i);
+ do_type_walk(cont, pre, post, env);
+ }
+ break;
+
+ case tpo_union:
+ n_mem = get_union_n_members(tp);
+ for (i = 0; i < n_mem; ++i) {
+ cont.ent = get_union_member(tp, i);
+ do_type_walk(cont, pre, post, env);
+ }
+ break;
+
+ case tpo_array:
+ cont.typ = get_array_element_type(tp);
+ do_type_walk(cont, pre, post, env);
+ cont.ent = get_array_element_entity(tp);
+ do_type_walk(cont, pre, post, env);
+ break;
+
+ case tpo_enumeration:
+ /* a leave */
+ break;
+
+ case tpo_pointer:
+ cont.typ = get_pointer_points_to_type(tp);
+ do_type_walk(cont, pre, post, env);
+ break;
+
+ case tpo_code:
+ case tpo_primitive:
+ case tpo_none:
+ case tpo_unknown:
+ /* a leave. */
+ break;
+ case tpo_uninitialized:
+ assert(0 && "Faulty type");
+ break;
+ }
+ break; /* end case k_type */
+
+ default:
+ printf(" *** Faulty type or entity! \n");
+ break;
+ }
+
+ /* execute post method */
+ if (post)
+ post(tore, env);
+}
+
+/** Check whether node contains types or entities as an attribute.
+ If so start a walk over that information. */
+static void irn_type_walker(
+ ir_node *node, type_walk_func *pre, type_walk_func *post, void *env)
+{
+ type_or_ent cont;
+
+ assert(node);
+
+ cont.ent = get_irn_entity_attr(node);
+ if (cont.ent)
+ do_type_walk(cont, pre, post, env);
+ cont.typ = get_irn_type_attr(node);
+ if (cont.typ)
+ do_type_walk(cont, pre, post, env);
+}
+
+/** Check whether node contains types or entities as an attribute.
+ If so start a walk over that information. */
+static void start_type_walk(ir_node *node, void *ctx)
+{
+ type_walk_env *env = (type_walk_env*)ctx;
+ type_walk_func *pre;
+ type_walk_func *post;
+ void *envi;
+
+ pre = env->pre;
+ post = env->post;
+ envi = env->env;
+
+ irn_type_walker(node, pre, post, envi);
+}
+
+void type_walk(type_walk_func *pre, type_walk_func *post, void *env)
+{
+ size_t i, n_types = get_irp_n_types();
+ type_or_ent cont;
+
+ irp_reserve_resources(irp, IRP_RESOURCE_TYPE_VISITED);
+ inc_master_type_visited();
+ for (i = 0; i < n_types; ++i) {
+ cont.typ = get_irp_type(i);
+ do_type_walk(cont, pre, post, env);
+ }
+ cont.typ = get_glob_type();
+ do_type_walk(cont, pre, post, env);
+ irp_free_resources(irp, IRP_RESOURCE_TYPE_VISITED);
+}
+
+void type_walk_irg(ir_graph *irg,
+ type_walk_func *pre,
+ type_walk_func *post,
+ void *env)
+{
+ ir_graph *rem = current_ir_graph;
+ /* this is needed to pass the parameters to the walker that actually
+ walks the type information */
+ type_walk_env type_env;
+ type_or_ent cont;
+
+ type_env.pre = pre;
+ type_env.post = post;
+ type_env.env = env;
+
+ current_ir_graph = irg;