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 2013/02/06 23:08:26 UTC

[lucy-commits] [4/4] git commit: refs/heads/c-bindings-cfc - Write dummmy callbacks.h for C bindings

Updated Branches:
  refs/heads/c-bindings-cfc [created] 4cbd991ec


Write dummmy callbacks.h for C bindings


Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/19cb392c
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/19cb392c
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/19cb392c

Branch: refs/heads/c-bindings-cfc
Commit: 19cb392c4b80d54795aef50ebdac9fd6a659da62
Parents: b7548c4
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Mon Dec 24 00:55:35 2012 +0100
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Wed Feb 6 23:03:54 2013 +0100

----------------------------------------------------------------------
 clownfish/compiler/src/CFCC.c      |  116 +++++++++++++++++++++++++++++++
 clownfish/compiler/src/CFCC.h      |   57 +++++++++++++++
 clownfish/compiler/src/CFCCClass.c |   43 +++++++++++
 clownfish/compiler/src/CFCCClass.h |   36 ++++++++++
 clownfish/compiler/src/CFCClass.h  |    2 +
 5 files changed, 254 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/19cb392c/clownfish/compiler/src/CFCC.c
----------------------------------------------------------------------
diff --git a/clownfish/compiler/src/CFCC.c b/clownfish/compiler/src/CFCC.c
new file mode 100644
index 0000000..0a1b9cb
--- /dev/null
+++ b/clownfish/compiler/src/CFCC.c
@@ -0,0 +1,116 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "charmony.h"
+
+#include <stdio.h>
+#include <string.h>
+
+#define CFC_NEED_BASE_STRUCT_DEF
+#include "CFCBase.h"
+#include "CFCC.h"
+#include "CFCCClass.h"
+#include "CFCClass.h"
+#include "CFCHierarchy.h"
+#include "CFCUtil.h"
+
+struct CFCC {
+    CFCBase base;
+    CFCHierarchy *hierarchy;
+    char         *header;
+    char         *footer;
+};
+
+const static CFCMeta CFCC_META = {
+    "Clownfish::CFC::Binding::C",
+    sizeof(CFCC),
+    (CFCBase_destroy_t)CFCC_destroy
+};
+
+CFCC*
+CFCC_new(CFCHierarchy *hierarchy, const char *header, const char *footer) {
+    CFCC *self = (CFCC*)CFCBase_allocate(&CFCC_META);
+    return CFCC_init(self, hierarchy, header, footer);
+}
+
+CFCC*
+CFCC_init(CFCC *self, CFCHierarchy *hierarchy, const char *header,
+          const char *footer) {
+    CFCUTIL_NULL_CHECK(hierarchy);
+    CFCUTIL_NULL_CHECK(header);
+    CFCUTIL_NULL_CHECK(footer);
+    self->hierarchy = (CFCHierarchy*)CFCBase_incref((CFCBase*)hierarchy);
+    self->header    = CFCUtil_strdup(header);
+    self->footer    = CFCUtil_strdup(footer);
+    return self;
+}
+
+void
+CFCC_destroy(CFCC *self) {
+    CFCBase_decref((CFCBase*)self->hierarchy);
+    FREEMEM(self->header);
+    FREEMEM(self->footer);
+    CFCBase_destroy((CFCBase*)self);
+}
+
+/* Write "callbacks.h" with NULL callbacks.
+ */
+void
+CFCC_write_callbacks(CFCC *self) {
+    CFCHierarchy  *hierarchy   = self->hierarchy;
+    CFCClass     **ordered     = CFCHierarchy_ordered_classes(hierarchy);
+    char          *all_cb_decs = CFCUtil_strdup("");
+
+    for (int i = 0; ordered[i] != NULL; i++) {
+        CFCClass *klass = ordered[i];
+
+        if (!CFCClass_included(klass)) {
+            char *cb_decs = CFCCClass_callback_decs(klass);
+            all_cb_decs = CFCUtil_cat(all_cb_decs, cb_decs, NULL);
+            FREEMEM(cb_decs);
+        }
+    }
+
+    FREEMEM(ordered);
+
+    const char pattern[] =
+        "%s\n"
+        "#ifndef CFCCALLBACKS_H\n"
+        "#define CFCCALLBACKS_H 1\n"
+        "\n"
+        "#include <stddef.h>\n"
+        "\n"
+        "%s"
+        "\n"
+        "#endif /* CFCCALLBACKS_H */\n"
+        "\n"
+        "%s\n"
+        "\n";
+    char *file_content = CFCUtil_sprintf(pattern, self->header, all_cb_decs,
+                                         self->footer);
+
+    // Unlink then write file.
+    const char *inc_dest = CFCHierarchy_get_include_dest(hierarchy);
+    char *filepath = CFCUtil_sprintf("%s" CHY_DIR_SEP "callbacks.h", inc_dest);
+    remove(filepath);
+    CFCUtil_write_file(filepath, file_content, strlen(file_content));
+    FREEMEM(filepath);
+
+    FREEMEM(all_cb_decs);
+    FREEMEM(file_content);
+}
+
+

http://git-wip-us.apache.org/repos/asf/lucy/blob/19cb392c/clownfish/compiler/src/CFCC.h
----------------------------------------------------------------------
diff --git a/clownfish/compiler/src/CFCC.h b/clownfish/compiler/src/CFCC.h
new file mode 100644
index 0000000..694b4e7
--- /dev/null
+++ b/clownfish/compiler/src/CFCC.h
@@ -0,0 +1,57 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/** Clownfish::CFC::Binding::C - Generate code for C bindings.
+ */
+#ifndef H_CFCC
+#define H_CFCC
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct CFCC CFCC;
+struct CFCHierarchy;
+
+/**
+ * @param hierarchy A L<Clownfish::CFC::Model::Hierarchy>.
+ * @param header Text which will be prepended to each generated C file --
+ * typically, an "autogenerated file" warning.
+ * @param footer Text to be appended to the end of each generated C file --
+ * typically copyright information.
+ */
+CFCC*
+CFCC_new(struct CFCHierarchy *hierarchy, const char *header,
+         const char *footer);
+
+CFCC*
+CFCC_init(CFCC *self, struct CFCHierarchy *hierarchy, const char *header,
+          const char *footer);
+
+void
+CFCC_destroy(CFCC *self);
+
+/** Write the "callbacks.h" header file with dummy callbacks.
+ */
+void
+CFCC_write_callbacks(CFCC *self);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* H_CFCC */
+

http://git-wip-us.apache.org/repos/asf/lucy/blob/19cb392c/clownfish/compiler/src/CFCCClass.c
----------------------------------------------------------------------
diff --git a/clownfish/compiler/src/CFCCClass.c b/clownfish/compiler/src/CFCCClass.c
new file mode 100644
index 0000000..b694ba7
--- /dev/null
+++ b/clownfish/compiler/src/CFCCClass.c
@@ -0,0 +1,43 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "CFCCClass.h"
+#include "CFCClass.h"
+#include "CFCMethod.h"
+#include "CFCUtil.h"
+
+// Declare dummy host callbacks.
+char*
+CFCCClass_callback_decs(CFCClass *klass) {
+    CFCMethod **fresh_methods = CFCClass_fresh_methods(klass);
+    char       *cb_decs       = CFCUtil_strdup("");
+
+    for (int meth_num = 0; fresh_methods[meth_num] != NULL; meth_num++) {
+        CFCMethod *method = fresh_methods[meth_num];
+
+        // Define callback to NULL.
+        if (CFCMethod_novel(method) && !CFCMethod_final(method)) {
+            const char *override_sym = CFCMethod_full_override_sym(method);
+            cb_decs = CFCUtil_cat(cb_decs, "#define ", override_sym, " NULL\n",
+                                  NULL);
+        }
+    }
+
+    FREEMEM(fresh_methods);
+
+    return cb_decs;
+}
+

http://git-wip-us.apache.org/repos/asf/lucy/blob/19cb392c/clownfish/compiler/src/CFCCClass.h
----------------------------------------------------------------------
diff --git a/clownfish/compiler/src/CFCCClass.h b/clownfish/compiler/src/CFCCClass.h
new file mode 100644
index 0000000..c701d2f
--- /dev/null
+++ b/clownfish/compiler/src/CFCCClass.h
@@ -0,0 +1,36 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef H_CFCCCLASS
+#define H_CFCCCLASS
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct CFCClass;
+
+/* Return dummy host callbacks defined to NULL.
+ */
+char*
+CFCCClass_callback_decs(struct CFCClass *klass);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* H_CFCCCLASS */
+

http://git-wip-us.apache.org/repos/asf/lucy/blob/19cb392c/clownfish/compiler/src/CFCClass.h
----------------------------------------------------------------------
diff --git a/clownfish/compiler/src/CFCClass.h b/clownfish/compiler/src/CFCClass.h
index ab4e5a0..fd38149 100644
--- a/clownfish/compiler/src/CFCClass.h
+++ b/clownfish/compiler/src/CFCClass.h
@@ -28,6 +28,8 @@
 extern "C" {
 #endif
 
+#include <stddef.h>
+
 typedef struct CFCClass CFCClass;
 struct CFCParcel;
 struct CFCDocuComment;