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/03/01 04:03:14 UTC

[06/22] lucy-clownfish git commit: Gen CFBindArg declarations.

Gen CFBindArg declarations.

Generate the declarations for the CFBindArg wrapper structs which are
required by some Python argument conversion routines.


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

Branch: refs/heads/master
Commit: 1853cbfd000430937312142034090de25de8039f
Parents: f11b123
Author: Marvin Humphrey <ma...@rectangular.com>
Authored: Tue Feb 2 13:38:19 2016 -0800
Committer: Marvin Humphrey <ma...@rectangular.com>
Committed: Wed Feb 24 15:24:52 2016 -0800

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


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/1853cbfd/compiler/src/CFCPyMethod.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCPyMethod.c b/compiler/src/CFCPyMethod.c
index dc98fb4..48ef443 100644
--- a/compiler/src/CFCPyMethod.c
+++ b/compiler/src/CFCPyMethod.c
@@ -81,6 +81,58 @@ S_gen_decs(CFCParamList *param_list, int first_tick) {
     return decs;
 }
 
+/* Some of the ParseTuple conversion routines provided by the Python-flavored
+ * CFBind module accept a CFBindArg instead of just a pointer to the value
+ * itself.  This routine generates the declarations for those CFBindArg
+ * variables, as well as handling some default values.
+ */
+static char*
+S_gen_declaration(CFCVariable *var, const char *val) {
+    CFCType *type = CFCVariable_get_type(var);
+    const char *var_name = CFCVariable_get_name(var);
+    const char *type_str = CFCType_to_c(type);
+    char *result = NULL;
+
+    if (CFCType_is_object(type)) {
+        const char *specifier = CFCType_get_specifier(type);
+        if (strcmp(specifier, "cfish_String") == 0) {
+            if (val && strcmp(val, "NULL") != 0) {
+                const char pattern[] =
+                    "    const char arg_%s_DEFAULT[] = %s;\n"
+                    "    %s_ARG = CFISH_SSTR_WRAP_UTF8(\n"
+                    "        arg_%s_DEFAULT, sizeof(arg_%s_DEFAULT) - 1);\n"
+                    ;
+                result = CFCUtil_sprintf(pattern, var_name, val, var_name,
+                                         var_name, var_name);
+            }
+        }
+        else {
+            if (val && strcmp(val, "NULL") != 0) {
+                CFCUtil_die("Can't assign a default of '%s' to a %s",
+                            val, type_str);
+            }
+            if (strcmp(specifier, "cfish_Hash") != 0
+                && strcmp(specifier, "cfish_Vector") != 0
+                ) {
+                const char *class_var = CFCType_get_class_var(type);
+                char pattern[] =
+                    "    CFBindArg wrap_arg_%s = {%s, &%s_ARG};\n"
+                    ;
+                result = CFCUtil_sprintf(pattern, var_name, class_var,
+                                         var_name);
+            }
+        }
+    }
+    else if (CFCType_is_primitive(type)) {
+        ;
+    }
+    else {
+        CFCUtil_die("Unexpected type, can't gen declaration: %s", type_str);
+    }
+
+    return result;
+}
+
 /* Generate the code which parses arguments passed from Python and converts
  * them to Clownfish-flavored C values.
  */
@@ -99,7 +151,9 @@ S_gen_arg_parsing(CFCParamList *param_list, int first_tick, char **error) {
     int optional_started = 0;
 
     for (int i = first_tick; i < num_vars; i++) {
+        CFCVariable *var  = vars[i];
         const char  *val  = vals[i];
+
         if (val == NULL) {
             if (optional_started) { // problem!
                 *error = "Required after optional param";
@@ -111,6 +165,10 @@ S_gen_arg_parsing(CFCParamList *param_list, int first_tick, char **error) {
                 optional_started = 1;
             }
         }
+
+        char *declaration = S_gen_declaration(var, val);
+        declarations = CFCUtil_cat(declarations, declaration, NULL);
+        FREEMEM(declaration);
     }
 
     char parse_pattern[] =