Updated Branches: refs/heads/callbacks-header [created] 8f337a013
Autogenerate callbacks.h Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/8f337a01 Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/8f337a01 Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/8f337a01 Branch: refs/heads/callbacks-header Commit: 8f337a013a3b51f49bb2cf9f19a515752b32ec65 Parents: 377630c Author: Nick Wellnhofer <[email protected]> Authored: Sun Dec 16 17:27:43 2012 +0100 Committer: Nick Wellnhofer <[email protected]> Committed: Tue Dec 18 18:54:09 2012 +0100 ---------------------------------------------------------------------- clownfish/compiler/src/CFCBindClass.c | 47 +++++++++++------ clownfish/compiler/src/CFCBindClass.h | 5 ++ clownfish/compiler/src/CFCBindCore.c | 81 ++++++++++++++++++++++++++++ clownfish/compiler/src/CFCPerl.c | 1 + 4 files changed, 118 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/8f337a01/clownfish/compiler/src/CFCBindClass.c ---------------------------------------------------------------------- diff --git a/clownfish/compiler/src/CFCBindClass.c b/clownfish/compiler/src/CFCBindClass.c index 52aa5a5..af79aae 100644 --- a/clownfish/compiler/src/CFCBindClass.c +++ b/clownfish/compiler/src/CFCBindClass.c @@ -278,9 +278,9 @@ CFCBindClass_to_c_data(CFCBindClass *self) { CFCMethod **methods = CFCClass_methods(client); CFCMethod **fresh_methods = CFCClass_fresh_methods(client); - char *offsets = CFCUtil_strdup(""); - char *cb_funcs = CFCUtil_strdup(""); - char *ms_var = CFCUtil_strdup(""); + char *offsets = CFCUtil_strdup(""); + char *method_defs = CFCUtil_strdup(""); + char *ms_var = CFCUtil_strdup(""); for (int meth_num = 0; methods[meth_num] != NULL; meth_num++) { CFCMethod *method = methods[meth_num]; @@ -315,17 +315,10 @@ CFCBindClass_to_c_data(CFCBindClass *self) { // Create a default implementation for abstract methods. if (CFCMethod_abstract(method)) { char *method_def = CFCBindMeth_abstract_method_def(method); - cb_funcs = CFCUtil_cat(cb_funcs, method_def, "\n", NULL); + method_defs = CFCUtil_cat(method_defs, method_def, "\n", NULL); FREEMEM(method_def); } - // Declare (but don't define) callback. - if (CFCMethod_novel(method) && !CFCMethod_final(method)) { - char *cb_dec = CFCBindMeth_callback_dec(method); - cb_funcs = CFCUtil_cat(cb_funcs, cb_dec, "\n", NULL); - FREEMEM(cb_dec); - } - // Define MethodSpec struct. if (meth_num != 0) { ms_var = CFCUtil_cat(ms_var, ",\n", NULL); @@ -346,8 +339,7 @@ CFCBindClass_to_c_data(CFCBindClass *self) { "\n" "%s\n" "\n" - "/* Define functions which implement host callbacks for the methods\n" - " * of this class which can be overridden via the host.\n" + "/* Define abstract methods of this class.\n" " */\n" "\n" "%s\n" @@ -370,17 +362,17 @@ CFCBindClass_to_c_data(CFCBindClass *self) { "\n"; size_t size = sizeof(pattern) + strlen(offsets) - + strlen(cb_funcs) + + strlen(method_defs) + strlen(ms_var) + strlen(vt_var) + strlen(autocode) + 100; char *code = (char*)MALLOCATE(size); - sprintf(code, pattern, offsets, cb_funcs, ms_var, vt_var, autocode); + sprintf(code, pattern, offsets, method_defs, ms_var, vt_var, autocode); FREEMEM(fresh_methods); FREEMEM(offsets); - FREEMEM(cb_funcs); + FREEMEM(method_defs); FREEMEM(ms_var); return code; } @@ -472,6 +464,29 @@ CFCBindClass_spec_def(CFCBindClass *self) { return code; } +// Declare host callbacks. +char* +CFCBindClass_callback_decs(CFCBindClass *self) { + CFCClass *client = self->client; + CFCMethod **fresh_methods = CFCClass_fresh_methods(client); + char *cb_decs = CFCUtil_strdup(""); + + for (int meth_num = 0; fresh_methods[meth_num] != NULL; meth_num++) { + CFCMethod *method = fresh_methods[meth_num]; + + // Declare callback. + if (CFCMethod_novel(method) && !CFCMethod_final(method)) { + char *cb_dec = CFCBindMeth_callback_dec(method); + cb_decs = CFCUtil_cat(cb_decs, cb_dec, "\n", NULL); + FREEMEM(cb_dec); + } + } + + FREEMEM(fresh_methods); + + return cb_decs; +} + // Declare typedefs for every method, to ease casting. static char* S_method_typedefs(CFCBindClass *self) { http://git-wip-us.apache.org/repos/asf/lucy/blob/8f337a01/clownfish/compiler/src/CFCBindClass.h ---------------------------------------------------------------------- diff --git a/clownfish/compiler/src/CFCBindClass.h b/clownfish/compiler/src/CFCBindClass.h index c84954e..6cb9347 100644 --- a/clownfish/compiler/src/CFCBindClass.h +++ b/clownfish/compiler/src/CFCBindClass.h @@ -59,6 +59,11 @@ CFCBindClass_to_c_data(CFCBindClass *self); char* CFCBindClass_spec_def(CFCBindClass *self); +/* Return the declarations of the host callbacks of this class. + */ +char* +CFCBindClass_callback_decs(CFCBindClass *self); + #ifdef __cplusplus } #endif http://git-wip-us.apache.org/repos/asf/lucy/blob/8f337a01/clownfish/compiler/src/CFCBindCore.c ---------------------------------------------------------------------- diff --git a/clownfish/compiler/src/CFCBindCore.c b/clownfish/compiler/src/CFCBindCore.c index 0cf7e30..300cd5a 100644 --- a/clownfish/compiler/src/CFCBindCore.c +++ b/clownfish/compiler/src/CFCBindCore.c @@ -47,6 +47,12 @@ S_write_parcel_h(CFCBindCore *self); static void S_write_parcel_c(CFCBindCore *self); +/* Write the "callbacks.h" header file, which contains declarations of host + * callbacks. + */ +static void +S_write_callbacks_h(CFCBindCore *self); + const static CFCMeta CFCBINDCORE_META = { "Clownfish::CFC::Binding::Core", sizeof(CFCBindCore), @@ -104,6 +110,7 @@ CFCBindCore_write_all_modified(CFCBindCore *self, int modified) { if (modified) { S_write_parcel_h(self); S_write_parcel_c(self); + S_write_callbacks_h(self); } return modified; @@ -334,6 +341,7 @@ S_write_parcel_c(CFCBindCore *self) { "#include \"parcel.h\"\n" "#include \"Clownfish/VTable.h\"\n" "%s\n" + "#include \"callbacks.h\"\n" "\n" "%s\n" "\n" @@ -378,3 +386,76 @@ S_write_parcel_c(CFCBindCore *self) { FREEMEM(file_content); } +/* Write the "callbacks.h" header file, which contains declarations of host + * callbacks. + */ +static void +S_write_callbacks_h(CFCBindCore *self) { + CFCHierarchy *hierarchy = self->hierarchy; + CFCClass **ordered = CFCHierarchy_ordered_classes(hierarchy); + char *includes = CFCUtil_strdup(""); + char *all_cb_decs = CFCUtil_strdup(""); + + for (int i = 0; ordered[i] != NULL; i++) { + CFCClass *klass = ordered[i]; + + const char *include_h = CFCClass_include_h(klass); + includes = CFCUtil_cat(includes, "#include \"", include_h, "\"\n", + NULL); + + if (!CFCClass_included(klass)) { + CFCBindClass *class_binding = CFCBindClass_new(klass); + char *cb_decs = CFCBindClass_callback_decs(class_binding); + all_cb_decs = CFCUtil_cat(all_cb_decs, cb_decs, NULL); + FREEMEM(cb_decs); + CFCBase_decref((CFCBase*)class_binding); + } + } + + FREEMEM(ordered); + + const char pattern[] = + "%s\n" + "#ifndef CFCCALLBACKS_H\n" + "#define CFCCALLBACKS_H 1\n" + "\n" + "#ifdef __cplusplus\n" + "extern \"C\" {\n" + "#endif\n" + "\n" + "#include \"parcel.h\"\n" + "%s" + "\n" + "%s" + "\n" + "#ifdef __cplusplus\n" + "}\n" + "#endif\n" + "\n" + "#endif /* CFCCALLBACKS_H */\n" + "\n" + "%s\n" + "\n"; + size_t size = sizeof(pattern) + + strlen(self->header) + + strlen(includes) + + strlen(all_cb_decs) + + strlen(self->footer) + + 50; + char *file_content = (char*)MALLOCATE(size); + sprintf(file_content, pattern, self->header, includes, all_cb_decs, + self->footer); + + // Unlink then write file. + const char *inc_dest = CFCHierarchy_get_include_dest(hierarchy); + char *filepath = CFCUtil_cat(CFCUtil_strdup(""), inc_dest, + CFCUTIL_PATH_SEP, "callbacks.h", NULL); + remove(filepath); + CFCUtil_write_file(filepath, file_content, strlen(file_content)); + FREEMEM(filepath); + + FREEMEM(includes); + FREEMEM(all_cb_decs); + FREEMEM(file_content); +} + http://git-wip-us.apache.org/repos/asf/lucy/blob/8f337a01/clownfish/compiler/src/CFCPerl.c ---------------------------------------------------------------------- diff --git a/clownfish/compiler/src/CFCPerl.c b/clownfish/compiler/src/CFCPerl.c index 31a1a45..ed35dab 100644 --- a/clownfish/compiler/src/CFCPerl.c +++ b/clownfish/compiler/src/CFCPerl.c @@ -521,6 +521,7 @@ CFCPerl_write_callbacks(CFCPerl *self) { " */\n" "\n" "#include \"XSBind.h\"\n" + "#include \"callbacks.h\"\n" "#include \"parcel.h\"\n" "\n" "static void\n"
