Repository: lucy-clownfish Updated Branches: refs/heads/master 6090c3153 -> 06a7e809e
Auto-generate Go bindings for constructors. Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/0728625d Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/0728625d Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/0728625d Branch: refs/heads/master Commit: 0728625d2608a4455427241f517631baea5d0d55 Parents: 611b0d4 Author: Marvin Humphrey <[email protected]> Authored: Mon Jul 13 19:03:41 2015 -0700 Committer: Marvin Humphrey <[email protected]> Committed: Mon Jul 20 13:40:36 2015 -0700 ---------------------------------------------------------------------- compiler/src/CFCGoClass.c | 48 +++++++++++++++++++++++++++++++++- compiler/src/CFCGoClass.h | 3 +++ compiler/src/CFCGoFunc.c | 12 +++++++++ compiler/src/CFCGoFunc.h | 12 +++++++++ runtime/go/clownfish/clownfish.go | 8 ------ 5 files changed, 74 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/0728625d/compiler/src/CFCGoClass.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCGoClass.c b/compiler/src/CFCGoClass.c index 28d00d8..d02aeb8 100644 --- a/compiler/src/CFCGoClass.c +++ b/compiler/src/CFCGoClass.c @@ -17,6 +17,11 @@ #include <string.h> #include <stdlib.h> +#ifndef true +#define true 1 +#define false 0 +#endif + #define CFC_NEED_BASE_STRUCT_DEF #include "CFCBase.h" #include "CFCGoClass.h" @@ -29,6 +34,7 @@ #include "CFCSymbol.h" #include "CFCVariable.h" #include "CFCType.h" +#include "CFCGoFunc.h" #include "CFCGoMethod.h" #include "CFCGoTypeMap.h" @@ -40,6 +46,7 @@ struct CFCGoClass { CFCGoMethod **method_bindings; size_t num_bound; int suppress_struct; + int suppress_ctor; }; static CFCGoClass **registry = NULL; @@ -259,9 +266,43 @@ CFCGoClass_boilerplate_funcs(CFCGoClass *self) { return content; } + char* CFCGoClass_gen_ctors(CFCGoClass *self) { - return CFCUtil_strdup(""); + CFCFunction *ctor_func = CFCClass_function(self->client, "new"); + if (self->suppress_ctor + || !ctor_func + || !CFCFunction_can_be_bound(ctor_func) + ) { + return CFCUtil_strdup(""); + } + CFCParcel *parcel = CFCClass_get_parcel(self->client); + CFCParamList *param_list = CFCFunction_get_param_list(ctor_func); + CFCType *ret_type = CFCFunction_get_return_type(ctor_func); + const char *struct_sym = CFCClass_get_struct_sym(self->client); + char *name = CFCUtil_sprintf("New%s", struct_sym); + char *cfunc = CFCFunction_full_func_sym(ctor_func, self->client); + char *cfargs = CFCGoFunc_ctor_cfargs(parcel, param_list); + char *first_line + = CFCGoFunc_ctor_start(parcel, name, param_list, ret_type); + char *ret_statement + = CFCGoFunc_return_statement(parcel, ret_type, "retvalCF"); + + char pattern[] = + "%s" + "\tretvalCF := C.%s(%s)\n" + "%s" + "}\n" + ; + char *content = CFCUtil_sprintf(pattern, first_line, cfunc, + cfargs, ret_statement); + + FREEMEM(ret_statement); + FREEMEM(cfargs); + FREEMEM(cfunc); + FREEMEM(first_line); + FREEMEM(name); + return content; } static void @@ -364,3 +405,8 @@ CFCGoClass_set_suppress_struct(CFCGoClass *self, int suppress_struct) { self->suppress_struct = !!suppress_struct; } +void +CFCGoClass_set_suppress_ctor(CFCGoClass *self, int suppress_ctor) { + self->suppress_ctor = !!suppress_ctor; +} + http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/0728625d/compiler/src/CFCGoClass.h ---------------------------------------------------------------------- diff --git a/compiler/src/CFCGoClass.h b/compiler/src/CFCGoClass.h index bfe88ca..6a2db34 100644 --- a/compiler/src/CFCGoClass.h +++ b/compiler/src/CFCGoClass.h @@ -82,6 +82,9 @@ CFCGoClass_spec_method(CFCGoClass *self, const char *name, const char *sig); void CFCGoClass_set_suppress_struct(CFCGoClass *self, int suppress_struct); +void +CFCGoClass_set_suppress_ctor(CFCGoClass *self, int suppress_ctor); + #ifdef __cplusplus } #endif http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/0728625d/compiler/src/CFCGoFunc.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCGoFunc.c b/compiler/src/CFCGoFunc.c index 3bde6b1..b9177b3 100644 --- a/compiler/src/CFCGoFunc.c +++ b/compiler/src/CFCGoFunc.c @@ -113,6 +113,13 @@ CFCGoFunc_meth_start(CFCParcel *parcel, const char *name, CFCClass *invoker, IS_METHOD); } +char* +CFCGoFunc_ctor_start(CFCParcel *parcel, const char *name, + CFCParamList *param_list, CFCType *return_type) { + return S_prep_start(parcel, name, NULL, param_list, return_type, + IS_CTOR); +} + static char* S_prep_cfargs(CFCParcel *parcel, CFCClass *invoker, CFCParamList *param_list, int targ) { @@ -180,6 +187,11 @@ CFCGoFunc_meth_cfargs(CFCParcel *parcel, CFCClass *invoker, } char* +CFCGoFunc_ctor_cfargs(CFCParcel *parcel, CFCParamList *param_list) { + return S_prep_cfargs(parcel, NULL, param_list, IS_CTOR); +} + +char* CFCGoFunc_return_statement(CFCParcel *parcel, CFCType *return_type, const char *cf_retval) { char *statement = NULL; http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/0728625d/compiler/src/CFCGoFunc.h ---------------------------------------------------------------------- diff --git a/compiler/src/CFCGoFunc.h b/compiler/src/CFCGoFunc.h index a2b71ee..b22276a 100644 --- a/compiler/src/CFCGoFunc.h +++ b/compiler/src/CFCGoFunc.h @@ -37,6 +37,11 @@ CFCGoFunc_meth_start(struct CFCParcel *parcel, const char *name, struct CFCParamList *param_list, struct CFCType *return_type); +char* +CFCGoFunc_ctor_start(struct CFCParcel *parcel, const char *name, + struct CFCParamList *param_list, + struct CFCType *return_type); + /** Convert Go method arguments to comma-separated Clownfish-flavored C * arguments, to be passed to a Clownfish method. */ @@ -44,6 +49,13 @@ char* CFCGoFunc_meth_cfargs(struct CFCParcel *parcel, struct CFCClass *invoker, struct CFCParamList *param_list); +/** Convert Go method arguments to comma-separated Clownfish-flavored C + * arguments, to be passed to a Clownfish `new` function. + */ +char* +CFCGoFunc_ctor_cfargs(struct CFCParcel *parcel, + struct CFCParamList *param_list); + /** Generate a Go return statement which maps from a CGO Clownfish type to a * Go type. * http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/0728625d/runtime/go/clownfish/clownfish.go ---------------------------------------------------------------------- diff --git a/runtime/go/clownfish/clownfish.go b/runtime/go/clownfish/clownfish.go index 83f38ab..9a13cff 100644 --- a/runtime/go/clownfish/clownfish.go +++ b/runtime/go/clownfish/clownfish.go @@ -106,14 +106,6 @@ func CFStringToGo(ptr unsafe.Pointer) string { return C.GoStringN(data, size) } -func NewErr(mess string) Err { - str := C.CString(mess) - len := C.size_t(len(mess)) - messC := C.cfish_Str_new_steal_utf8(str, len) - cfObj := C.cfish_Err_new(messC) - return WRAPErr(unsafe.Pointer(cfObj)) -} - func (e *ErrIMP) Error() string { mess := C.CFISH_Err_Get_Mess((*C.cfish_Err)(unsafe.Pointer(e.ref))) return CFStringToGo(unsafe.Pointer(mess))
