Function parameter and return type check Move parameter and return type check from a static function in CFCPerlClass to CFCPerlSub. Allow types which are either objects or primitives.
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/3b924b5c Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/3b924b5c Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/3b924b5c Branch: refs/heads/master Commit: 3b924b5c0f4cd6c119e5bc3961b7b66d205b7a25 Parents: 6de3da6 Author: Nick Wellnhofer <[email protected]> Authored: Thu Jul 31 13:50:52 2014 +0200 Committer: Nick Wellnhofer <[email protected]> Committed: Mon Sep 1 13:30:04 2014 +0200 ---------------------------------------------------------------------- compiler/src/CFCPerlClass.c | 39 ++++++++------------------------------- compiler/src/CFCPerlSub.c | 25 +++++++++++++++++++++++++ compiler/src/CFCPerlSub.h | 6 ++++++ 3 files changed, 39 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/3b924b5c/compiler/src/CFCPerlClass.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCPerlClass.c b/compiler/src/CFCPerlClass.c index 7c018de..1fb2b7c 100644 --- a/compiler/src/CFCPerlClass.c +++ b/compiler/src/CFCPerlClass.c @@ -32,6 +32,7 @@ #include "CFCVariable.h" #include "CFCType.h" #include "CFCPerlPod.h" +#include "CFCPerlSub.h" #include "CFCPerlMethod.h" #include "CFCPerlConstructor.h" #include "CFCPerlTypeMap.h" @@ -226,26 +227,6 @@ CFCPerlClass_exclude_constructor(CFCPerlClass *self) { self->exclude_cons = 1; } -static int -S_can_be_bound(CFCParamList *param_list, CFCType *return_type) { - int success = 1; - CFCVariable **arg_vars = CFCParamList_get_variables(param_list); - - for (size_t i = 0; arg_vars[i] != NULL; i++) { - CFCType *type = CFCVariable_get_type(arg_vars[i]); - char *conversion = CFCPerlTypeMap_from_perl(type, "foo"); - if (conversion) { FREEMEM(conversion); } - else { success = 0; } - } - if (!CFCType_is_void(return_type)) { - char *conversion = CFCPerlTypeMap_to_perl(return_type, "foo"); - if (conversion) { FREEMEM(conversion); } - else { success = 0; } - } - - return success; -} - CFCPerlMethod** CFCPerlClass_method_bindings(CFCClass *klass) { CFCClass *parent = CFCClass_get_parent(klass); @@ -267,9 +248,7 @@ CFCPerlClass_method_bindings(CFCClass *klass) { } // Skip methods with types which cannot be mapped automatically. - CFCParamList *param_list = CFCMethod_get_param_list(method); - CFCType *return_type = CFCMethod_get_return_type(method); - if (!S_can_be_bound(param_list, return_type)) { + if (!CFCPerlSub_can_be_bound((CFCFunction*)method)) { continue; } @@ -307,17 +286,15 @@ CFCPerlClass_constructor_bindings(CFCClass *klass) { // Iterate over the list of possible initialization functions. for (size_t i = 0; functions[i] != NULL; i++) { - CFCFunction *function = functions[i]; - const char *micro_sym = CFCFunction_micro_sym(function); - CFCParamList *param_list = CFCFunction_get_param_list(function); - CFCType *return_type = CFCFunction_get_return_type(function); - const char *alias = NULL; + CFCFunction *function = functions[i]; + const char *micro_sym = CFCFunction_micro_sym(function); + const char *alias = NULL; // Find user-specified alias. if (perl_class == NULL) { // Bind init() to new() when possible. if (strcmp(micro_sym, "init") == 0 - && S_can_be_bound(param_list, return_type) + && CFCPerlSub_can_be_bound(function) ) { alias = NEW; } @@ -326,7 +303,7 @@ CFCPerlClass_constructor_bindings(CFCClass *klass) { for (size_t j = 0; j < perl_class->num_cons; j++) { if (strcmp(micro_sym, perl_class->cons_inits[j]) == 0) { alias = perl_class->cons_aliases[j]; - if (!S_can_be_bound(param_list, return_type)) { + if (!CFCPerlSub_can_be_bound(function)) { CFCUtil_die("Can't bind %s as %s" " -- types can't be mapped", micro_sym, alias); @@ -339,7 +316,7 @@ CFCPerlClass_constructor_bindings(CFCClass *klass) { if (!alias && !perl_class->exclude_cons && strcmp(micro_sym, "init") == 0 - && S_can_be_bound(param_list, return_type) + && CFCPerlSub_can_be_bound(function) ) { int saw_new = 0; for (size_t j = 0; j < perl_class->num_cons; j++) { http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/3b924b5c/compiler/src/CFCPerlSub.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCPerlSub.c b/compiler/src/CFCPerlSub.c index 063d59a..fd508cc 100644 --- a/compiler/src/CFCPerlSub.c +++ b/compiler/src/CFCPerlSub.c @@ -20,6 +20,7 @@ #define CFC_NEED_PERLSUB_STRUCT_DEF #include "CFCPerlSub.h" #include "CFCBase.h" +#include "CFCFunction.h" #include "CFCUtil.h" #include "CFCParamList.h" #include "CFCVariable.h" @@ -72,6 +73,30 @@ CFCPerlSub_destroy(CFCPerlSub *self) { CFCBase_destroy((CFCBase*)self); } +int +CFCPerlSub_can_be_bound(CFCFunction *function) { + // Test whether parameters can be mapped automatically. + CFCParamList *param_list = CFCFunction_get_param_list(function); + CFCVariable **arg_vars = CFCParamList_get_variables(param_list); + for (size_t i = 0; arg_vars[i] != NULL; i++) { + CFCType *type = CFCVariable_get_type(arg_vars[i]); + if (!CFCType_is_object(type) && !CFCType_is_primitive(type)) { + return false; + } + } + + // Test whether return type can be mapped automatically. + CFCType *return_type = CFCFunction_get_return_type(function); + if (!CFCType_is_void(return_type) + && !CFCType_is_object(return_type) + && !CFCType_is_primitive(return_type) + ) { + return false; + } + + return true; +} + char* CFCPerlSub_params_hash_def(CFCPerlSub *self) { if (!self->use_labeled_params) { http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/3b924b5c/compiler/src/CFCPerlSub.h ---------------------------------------------------------------------- diff --git a/compiler/src/CFCPerlSub.h b/compiler/src/CFCPerlSub.h index 54d1917..8582818 100644 --- a/compiler/src/CFCPerlSub.h +++ b/compiler/src/CFCPerlSub.h @@ -22,6 +22,7 @@ extern "C" { #endif typedef struct CFCPerlSub CFCPerlSub; +struct CFCFunction; struct CFCParamList; struct CFCType; @@ -64,6 +65,11 @@ CFCPerlSub_init(CFCPerlSub *self, struct CFCParamList *param_list, void CFCPerlSub_destroy(CFCPerlSub *self); +/** Test whether bindings can be generated for a function. + */ +int +CFCPerlSub_can_be_bound(struct CFCFunction *function); + /** Return Perl code initializing a package-global hash where all the keys are * the names of labeled params. The hash's name consists of the the binding's * perl_name() plus "_PARAMS".
