You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2016/02/25 00:07:44 UTC

[17/20] lucy-clownfish git commit: Generate OVERRIDEs for non-overrideable meths.

Generate OVERRIDEs for non-overrideable meths.

Generate error-throwing OVERRIDE routines for methods which cannot be
overridden.


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

Branch: refs/heads/master
Commit: 02617ac057b70a2358233d9f65fb205123b308e1
Parents: 91622db
Author: Marvin Humphrey <ma...@rectangular.com>
Authored: Wed Jan 27 19:14:25 2016 -0800
Committer: Marvin Humphrey <ma...@rectangular.com>
Committed: Tue Feb 23 18:22:05 2016 -0800

----------------------------------------------------------------------
 compiler/src/CFCPyMethod.c | 76 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 75 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/02617ac0/compiler/src/CFCPyMethod.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCPyMethod.c b/compiler/src/CFCPyMethod.c
index 2892b86..3cc7b96 100644
--- a/compiler/src/CFCPyMethod.c
+++ b/compiler/src/CFCPyMethod.c
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <string.h>
 
 #include "CFCPyMethod.h"
 #include "CFCPyTypeMap.h"
@@ -32,7 +33,80 @@
 #define false 0
 #endif
 
+/* Take a NULL-terminated list of CFCVariables and build up a string of
+ * directives like:
+ *
+ *     UNUSED_VAR(var1);
+ *     UNUSED_VAR(var2);
+ */
+static char*
+S_build_unused_vars(CFCVariable **vars);
+
+/* Create an unreachable return statement if necessary, in order to thwart
+ * compiler warnings. */
+static char*
+S_maybe_unreachable(CFCType *return_type);
+
 char*
 CFCPyMethod_callback_def(CFCMethod *method, CFCClass *invoker) {
-    return CFCUtil_strdup("");
+    CFCParamList *param_list   = CFCMethod_get_param_list(method);
+    CFCVariable **vars         = CFCParamList_get_variables(param_list);
+    CFCType      *return_type  = CFCMethod_get_return_type(method);
+    const char   *ret_type_str = CFCType_to_c(return_type);
+    const char   *params       = CFCParamList_to_c(param_list);
+    char         *override_sym = CFCMethod_full_override_sym(method, invoker);
+    char *content;
+
+    //if (CFCMethod_can_be_bound(method)) {
+    if (false) {
+    }
+    else {
+        char *unused = S_build_unused_vars(vars);
+        char *unreachable = S_maybe_unreachable(return_type);
+        char *meth_sym = CFCMethod_full_method_sym(method, invoker);
+        const char pattern[] =
+            "%s\n"
+            "%s(%s) {%s\n"
+            "    CFISH_THROW(CFISH_ERR, \"Can't override %s via binding\");%s\n"
+            "}\n";
+        content = CFCUtil_sprintf(pattern, ret_type_str, override_sym,
+                                  params, unused, meth_sym, unreachable);
+        FREEMEM(meth_sym);
+        FREEMEM(unused);
+        FREEMEM(unreachable);
+    }
+
+    FREEMEM(override_sym);
+    return content;
 }
+
+static char*
+S_build_unused_vars(CFCVariable **vars) {
+    char *unused = CFCUtil_strdup("");
+
+    for (int i = 0; vars[i] != NULL; i++) {
+        const char *var_name = CFCVariable_get_name(vars[i]);
+        size_t size = strlen(unused) + strlen(var_name) + 80;
+        unused = (char*)REALLOCATE(unused, size);
+        strcat(unused, "\n    CFISH_UNUSED_VAR(");
+        strcat(unused, var_name);
+        strcat(unused, ");");
+    }
+
+    return unused;
+}
+
+static char*
+S_maybe_unreachable(CFCType *return_type) {
+    char *return_statement;
+    if (CFCType_is_void(return_type)) {
+        return_statement = CFCUtil_strdup("");
+    }
+    else {
+        const char *ret_type_str = CFCType_to_c(return_type);
+        char pattern[] = "\n    CFISH_UNREACHABLE_RETURN(%s);";
+        return_statement = CFCUtil_sprintf(pattern, ret_type_str);
+    }
+    return return_statement;
+}
+