You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by nw...@apache.org on 2012/12/27 18:49:16 UTC

[lucy-commits] [2/7] git commit: refs/heads/c-bindings-wip1 - Autogenerate callbacks.h

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/2d572a69
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/2d572a69
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/2d572a69

Branch: refs/heads/c-bindings-wip1
Commit: 2d572a691dcaac1b075c51749cda2066824caccc
Parents: 272715c
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Sun Dec 16 17:27:43 2012 +0100
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Mon Dec 24 01:00:07 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/2d572a69/clownfish/compiler/src/CFCBindClass.c
----------------------------------------------------------------------
diff --git a/clownfish/compiler/src/CFCBindClass.c b/clownfish/compiler/src/CFCBindClass.c
index 0301910..bb0511c 100644
--- a/clownfish/compiler/src/CFCBindClass.c
+++ b/clownfish/compiler/src/CFCBindClass.c
@@ -299,9 +299,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];
@@ -336,17 +336,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);
@@ -369,8 +362,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"
@@ -394,18 +386,18 @@ CFCBindClass_to_c_data(CFCBindClass *self) {
     size_t size = sizeof(pattern)
                   + strlen(include_h)
                   + 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, include_h, offsets, cb_funcs, ms_var, vt_var,
+    sprintf(code, pattern, include_h, offsets, method_defs, ms_var, vt_var,
             autocode);
 
     FREEMEM(fresh_methods);
     FREEMEM(offsets);
-    FREEMEM(cb_funcs);
+    FREEMEM(method_defs);
     FREEMEM(ms_var);
     return code;
 }
@@ -497,6 +489,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/2d572a69/clownfish/compiler/src/CFCBindClass.h
----------------------------------------------------------------------
diff --git a/clownfish/compiler/src/CFCBindClass.h b/clownfish/compiler/src/CFCBindClass.h
index e14c94e..666e46b 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);
+
 /** Return the man page for the class.
  */
 char*

http://git-wip-us.apache.org/repos/asf/lucy/blob/2d572a69/clownfish/compiler/src/CFCBindCore.c
----------------------------------------------------------------------
diff --git a/clownfish/compiler/src/CFCBindCore.c b/clownfish/compiler/src/CFCBindCore.c
index 84c6165..c41affa 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;
@@ -333,6 +340,7 @@ S_write_parcel_c(CFCBindCore *self) {
         "#include \"parcel.h\"\n"
         "#include \"Clownfish/VTable.h\"\n"
         "%s\n"
+        "#include \"callbacks.h\"\n"
         "\n"
         "typedef struct cfish_MethodSpec {\n"
         "    int             is_novel;\n"
@@ -436,6 +444,79 @@ S_write_parcel_c(CFCBindCore *self) {
     FREEMEM(vt_specs);
 }
 
+/* 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);
+}
+
 void
 CFCBindCore_write_man_pages(CFCBindCore *self) {
     CFCHierarchy  *hierarchy = self->hierarchy;

http://git-wip-us.apache.org/repos/asf/lucy/blob/2d572a69/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"