I created a c program which uses nested functions from the gnu extension a lot and now I want to make them standard conform with ansi c.
What's the best way to transform nested functions, which access some outer vars to something different.
#define lambda(return_type, function_body) \
({ \
return_type __fn__ function_body \
__fn__; \
})
example usage
size_t var1;
size_t var2;
lambda(void, (...) {
// some code
lambda(void, (...) {
// ...
// do something with var1/var2
// ..
}
// ...
// do something with var1/var2
}
I thought about moving the vars to global scope, so they are known from each "lambda" which would maybe the easiest solution, but I dont want to polute the global scope and I'm not sure, if this is the cleanest way.
As asked by some commenters - here's a concrete example
/* fill itt*/
int n_method = 0;
void *add_method = lambda(void, (ir_entity *method) {
int itable_offset = n_method++;
const char *method_name = get_entity_name(method);
ir_entity *implementation = get_method_entity(klass, method_name);
if (implementation == NULL) {
walk_up_callback(klass, lambda(bool, (ir_type *st) {
implementation = get_method_entity(st, method_name);
if (implementation != NULL) {
insert_itable_method(implementation, itable_offset, interface, init);
}
return implementation == NULL;
}), NULL);
} else {
insert_itable_method(implementation, itable_offset, interface, init);
}
});
walk_up_callback(interface, NULL, lambda(void, (ir_type *klass) {
if (oo_get_class_is_interface(klass)) {
walk_table_methods_callback(add_method, klass);
}
}));
walk_table_methods_callback(add_method, interface);
It's a part of a compiler which creates some itables for efficient interface lookups
Aucun commentaire:
Enregistrer un commentaire