Repository: lucy-clownfish Updated Branches: refs/heads/master dbb139416 -> 505750771
Common base class CFCCallable for functions and methods Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/ed4ce8b8 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/ed4ce8b8 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/ed4ce8b8 Branch: refs/heads/master Commit: ed4ce8b840c357669eebcf3b895d594ef1eb57d6 Parents: dbb1394 Author: Nick Wellnhofer <[email protected]> Authored: Tue Apr 21 12:28:18 2015 +0200 Committer: Nick Wellnhofer <[email protected]> Committed: Thu May 7 21:08:51 2015 +0200 ---------------------------------------------------------------------- compiler/include/CFC.h | 1 + compiler/src/CFCCallable.c | 94 +++++++++++++++++++++++++++++++++++++++++ compiler/src/CFCCallable.h | 86 +++++++++++++++++++++++++++++++++++++ compiler/src/CFCFunction.c | 57 ++++++++----------------- compiler/src/CFCFunction.h | 12 ------ compiler/src/CFCMethod.c | 32 +++++++------- compiler/src/CFCMethod.h | 2 +- 7 files changed, 216 insertions(+), 68 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ed4ce8b8/compiler/include/CFC.h ---------------------------------------------------------------------- diff --git a/compiler/include/CFC.h b/compiler/include/CFC.h index 0e76090..486aec0 100644 --- a/compiler/include/CFC.h +++ b/compiler/include/CFC.h @@ -16,6 +16,7 @@ #include "CFCBase.h" #include "CFCCBlock.h" +#include "CFCCallable.h" #include "CFCClass.h" #include "CFCDocuComment.h" #include "CFCFile.h" http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ed4ce8b8/compiler/src/CFCCallable.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCCallable.c b/compiler/src/CFCCallable.c new file mode 100644 index 0000000..f787324 --- /dev/null +++ b/compiler/src/CFCCallable.c @@ -0,0 +1,94 @@ +/* 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 <string.h> +#include <ctype.h> + +#ifndef true + #define true 1 + #define false 0 +#endif + +#define CFC_NEED_CALLABLE_STRUCT_DEF +#include "CFCCallable.h" +#include "CFCClass.h" +#include "CFCParcel.h" +#include "CFCType.h" +#include "CFCParamList.h" +#include "CFCVariable.h" +#include "CFCDocuComment.h" +#include "CFCUtil.h" + +static const CFCMeta CFCCALLABLE_META = { + "Clownfish::CFC::Model::Callable", + sizeof(CFCCallable), + (CFCBase_destroy_t)CFCCallable_destroy +}; + +CFCCallable* +CFCCallable_init(CFCCallable *self, CFCParcel *parcel, const char *exposure, + const char *class_name, const char *class_nickname, + const char *micro_sym, CFCType *return_type, + CFCParamList *param_list, CFCDocuComment *docucomment) { + + exposure = exposure ? exposure : "parcel"; + CFCUTIL_NULL_CHECK(class_name); + CFCUTIL_NULL_CHECK(return_type); + CFCUTIL_NULL_CHECK(param_list); + CFCSymbol_init((CFCSymbol*)self, parcel, exposure, class_name, + class_nickname, micro_sym); + self->return_type = (CFCType*)CFCBase_incref((CFCBase*)return_type); + self->param_list = (CFCParamList*)CFCBase_incref((CFCBase*)param_list); + self->docucomment = (CFCDocuComment*)CFCBase_incref((CFCBase*)docucomment); + return self; +} + +void +CFCCallable_destroy(CFCCallable *self) { + CFCBase_decref((CFCBase*)self->return_type); + CFCBase_decref((CFCBase*)self->param_list); + CFCBase_decref((CFCBase*)self->docucomment); + CFCSymbol_destroy((CFCSymbol*)self); +} + +void +CFCCallable_resolve_types(CFCCallable *self) { + CFCType_resolve(self->return_type); + CFCParamList_resolve_types(self->param_list); +} + +int +CFCCallable_can_be_bound(CFCCallable *self) { + // Test whether parameters can be mapped automatically. + CFCVariable **arg_vars = CFCParamList_get_variables(self->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. + if (!CFCType_is_void(self->return_type) + && !CFCType_is_object(self->return_type) + && !CFCType_is_primitive(self->return_type) + ) { + return false; + } + + return true; +} + http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ed4ce8b8/compiler/src/CFCCallable.h ---------------------------------------------------------------------- diff --git a/compiler/src/CFCCallable.h b/compiler/src/CFCCallable.h new file mode 100644 index 0000000..6e9a349 --- /dev/null +++ b/compiler/src/CFCCallable.h @@ -0,0 +1,86 @@ +/* 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::Model::Callable - Base class for functions and methods. + */ + +#ifndef H_CFCCALLABLE +#define H_CFCCALLABLE + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct CFCCallable CFCCallable; +struct CFCParcel; +struct CFCType; +struct CFCDocuComment; +struct CFCParamList; +struct CFCClass; + +#ifdef CFC_NEED_CALLABLE_STRUCT_DEF +#define CFC_NEED_SYMBOL_STRUCT_DEF +#include "CFCSymbol.h" +struct CFCCallable { + CFCSymbol symbol; + struct CFCType *return_type; + struct CFCParamList *param_list; + struct CFCDocuComment *docucomment; +}; +#endif + +/** + * @param parcel A Clownfish::CFC::Model::Parcel. + * @param exposure The callable's exposure (see + * L<Clownfish::CFC::Model::Symbol>). + * @param class_name The full name of the class in whose namespace the + * function resides. + * @param class_nickname The C nickname for the class. + * @param micro_sym The lower case name of the callable, without any + * namespacing prefixes. + * @param return_type A Clownfish::CFC::Model::Type representing the + * callable's return type. + * @param param_list A Clownfish::CFC::Model::ParamList representing the + * callable's argument list. + * @param docucomment A Clownfish::CFC::Model::DocuComment describing the + * callable. + */ +CFCCallable* +CFCCallable_init(CFCCallable *self, struct CFCParcel *parcel, + const char *exposure, const char *class_name, + const char *class_nickname, const char *micro_sym, + struct CFCType *return_type, struct CFCParamList *param_list, + struct CFCDocuComment *docucomment); + +void +CFCCallable_destroy(CFCCallable *self); + +/** Find the actual class of all unprefixed object types. + */ +void +CFCCallable_resolve_types(CFCCallable *self); + +/** Test whether bindings can be generated for a callable. + */ +int +CFCCallable_can_be_bound(CFCCallable *self); + +#ifdef __cplusplus +} +#endif + +#endif /* H_CFCCALLABLE */ + http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ed4ce8b8/compiler/src/CFCFunction.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCFunction.c b/compiler/src/CFCFunction.c index c875507..c5d3658 100644 --- a/compiler/src/CFCFunction.c +++ b/compiler/src/CFCFunction.c @@ -22,7 +22,8 @@ #define false 0 #endif -#define CFC_NEED_FUNCTION_STRUCT_DEF +#define CFC_NEED_CALLABLE_STRUCT_DEF +#include "CFCCallable.h" #include "CFCFunction.h" #include "CFCParcel.h" #include "CFCType.h" @@ -31,6 +32,11 @@ #include "CFCDocuComment.h" #include "CFCUtil.h" +struct CFCFunction { + CFCCallable callable; + int is_inline; +}; + static const CFCMeta CFCFUNCTION_META = { "Clownfish::CFC::Model::Function", sizeof(CFCFunction), @@ -67,72 +73,45 @@ CFCFunction_init(CFCFunction *self, CFCParcel *parcel, const char *exposure, CFCParamList *param_list, CFCDocuComment *docucomment, int is_inline) { - exposure = exposure ? exposure : "parcel"; - CFCUTIL_NULL_CHECK(class_name); - CFCUTIL_NULL_CHECK(return_type); - CFCUTIL_NULL_CHECK(param_list); if (!S_validate_micro_sym(micro_sym)) { CFCBase_decref((CFCBase*)self); CFCUtil_die("Invalid micro_sym: '%s'", micro_sym); } - CFCSymbol_init((CFCSymbol*)self, parcel, exposure, class_name, - class_nickname, micro_sym); - self->return_type = (CFCType*)CFCBase_incref((CFCBase*)return_type); - self->param_list = (CFCParamList*)CFCBase_incref((CFCBase*)param_list); - self->docucomment = (CFCDocuComment*)CFCBase_incref((CFCBase*)docucomment); - self->is_inline = is_inline; + CFCCallable_init((CFCCallable*)self, parcel, exposure, class_name, + class_nickname, micro_sym, return_type, param_list, + docucomment); + self->is_inline = is_inline; return self; } void CFCFunction_resolve_types(CFCFunction *self) { - CFCType_resolve(self->return_type); - CFCParamList_resolve_types(self->param_list); + CFCCallable_resolve_types(&self->callable); } void CFCFunction_destroy(CFCFunction *self) { - CFCBase_decref((CFCBase*)self->return_type); - CFCBase_decref((CFCBase*)self->param_list); - CFCBase_decref((CFCBase*)self->docucomment); - CFCSymbol_destroy((CFCSymbol*)self); + CFCCallable_destroy((CFCCallable*)self); } int CFCFunction_can_be_bound(CFCFunction *self) { - // Test whether parameters can be mapped automatically. - CFCVariable **arg_vars = CFCParamList_get_variables(self->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. - if (!CFCType_is_void(self->return_type) - && !CFCType_is_object(self->return_type) - && !CFCType_is_primitive(self->return_type) - ) { - return false; - } - - return true; + return CFCCallable_can_be_bound((CFCCallable*)self); } CFCType* CFCFunction_get_return_type(CFCFunction *self) { - return self->return_type; + return self->callable.return_type; } CFCParamList* CFCFunction_get_param_list(CFCFunction *self) { - return self->param_list; + return self->callable.param_list; } CFCDocuComment* CFCFunction_get_docucomment(CFCFunction *self) { - return self->docucomment; + return self->callable.docucomment; } int @@ -142,7 +121,7 @@ CFCFunction_inline(CFCFunction *self) { int CFCFunction_void(CFCFunction *self) { - return CFCType_is_void(self->return_type); + return CFCType_is_void(self->callable.return_type); } const char* http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ed4ce8b8/compiler/src/CFCFunction.h ---------------------------------------------------------------------- diff --git a/compiler/src/CFCFunction.h b/compiler/src/CFCFunction.h index f2b6794..402a214 100644 --- a/compiler/src/CFCFunction.h +++ b/compiler/src/CFCFunction.h @@ -31,18 +31,6 @@ struct CFCDocuComment; struct CFCParamList; struct CFCClass; -#ifdef CFC_NEED_FUNCTION_STRUCT_DEF -#define CFC_NEED_SYMBOL_STRUCT_DEF -#include "CFCSymbol.h" -struct CFCFunction { - CFCSymbol symbol; - struct CFCType *return_type; - struct CFCParamList *param_list; - struct CFCDocuComment *docucomment; - int is_inline; -}; -#endif - /** * @param parcel A Clownfish::CFC::Model::Parcel. * @param exposure The function's exposure (see http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ed4ce8b8/compiler/src/CFCMethod.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCMethod.c b/compiler/src/CFCMethod.c index 5f8c951..ba5399d 100644 --- a/compiler/src/CFCMethod.c +++ b/compiler/src/CFCMethod.c @@ -18,8 +18,8 @@ #include <ctype.h> #include <stdio.h> -#define CFC_NEED_FUNCTION_STRUCT_DEF -#include "CFCFunction.h" +#define CFC_NEED_CALLABLE_STRUCT_DEF +#include "CFCCallable.h" #include "CFCMethod.h" #include "CFCType.h" #include "CFCClass.h" @@ -35,7 +35,7 @@ #endif struct CFCMethod { - CFCFunction function; + CFCCallable callable; CFCMethod *novel_method; char *macro_sym; char *full_override_sym; @@ -105,9 +105,9 @@ CFCMethod_init(CFCMethod *self, CFCParcel *parcel, const char *exposure, } // Super-init and clean up derived micro_sym. - CFCFunction_init((CFCFunction*)self, parcel, exposure, class_name, + CFCCallable_init((CFCCallable*)self, parcel, exposure, class_name, class_nickname, micro_sym, return_type, param_list, - docucomment, false); + docucomment); FREEMEM(micro_sym); // Verify that the first element in the arg list is a self. @@ -152,7 +152,7 @@ CFCMethod_init(CFCMethod *self, CFCParcel *parcel, const char *exposure, void CFCMethod_resolve_types(CFCMethod *self) { - CFCFunction_resolve_types((CFCFunction*)self); + CFCCallable_resolve_types((CFCCallable*)self); } void @@ -163,7 +163,7 @@ CFCMethod_destroy(CFCMethod *self) { FREEMEM(self->host_alias); FREEMEM(self->short_imp_func); FREEMEM(self->imp_func); - CFCFunction_destroy((CFCFunction*)self); + CFCCallable_destroy((CFCCallable*)self); } int @@ -175,8 +175,8 @@ CFCMethod_compatible(CFCMethod *self, CFCMethod *other) { if (!!my_public != !!other_public) { return false; } // Check arguments and initial values. - CFCParamList *my_param_list = self->function.param_list; - CFCParamList *other_param_list = other->function.param_list; + CFCParamList *my_param_list = self->callable.param_list; + CFCParamList *other_param_list = other->callable.param_list; CFCVariable **my_args = CFCParamList_get_variables(my_param_list); CFCVariable **other_args = CFCParamList_get_variables(other_param_list); const char **my_vals = CFCParamList_get_initial_values(my_param_list); @@ -251,9 +251,9 @@ CFCMethod_finalize(CFCMethod *self) { const char *class_nickname = CFCMethod_get_class_nickname(self); CFCMethod *finalized = CFCMethod_new(parcel, exposure, class_name, class_nickname, - self->macro_sym, self->function.return_type, - self->function.param_list, - self->function.docucomment, true, + self->macro_sym, self->callable.return_type, + self->callable.param_list, + self->callable.docucomment, true, self->is_abstract); finalized->novel_method = (CFCMethod*)CFCBase_incref((CFCBase*)self->novel_method); @@ -269,7 +269,7 @@ CFCMethod_can_be_bound(CFCMethod *method) { * - methods with types which cannot be mapped automatically */ return !CFCSymbol_private((CFCSymbol*)method) - && CFCFunction_can_be_bound((CFCFunction*)method); + && CFCCallable_can_be_bound((CFCCallable*)method); } void @@ -408,7 +408,7 @@ CFCMethod_novel(CFCMethod *self) { CFCType* CFCMethod_self_type(CFCMethod *self) { - CFCVariable **vars = CFCParamList_get_variables(self->function.param_list); + CFCVariable **vars = CFCParamList_get_variables(self->callable.param_list); return CFCVariable_get_type(vars[0]); } @@ -454,12 +454,12 @@ CFCMethod_public(CFCMethod *self) { CFCType* CFCMethod_get_return_type(CFCMethod *self) { - return CFCFunction_get_return_type((CFCFunction*)self); + return self->callable.return_type; } CFCParamList* CFCMethod_get_param_list(CFCMethod *self) { - return CFCFunction_get_param_list((CFCFunction*)self); + return self->callable.param_list; } const char* http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ed4ce8b8/compiler/src/CFCMethod.h ---------------------------------------------------------------------- diff --git a/compiler/src/CFCMethod.h b/compiler/src/CFCMethod.h index deff2a3..3549214 100644 --- a/compiler/src/CFCMethod.h +++ b/compiler/src/CFCMethod.h @@ -41,7 +41,7 @@ struct CFCDocuComment; /** * @param parcel See Clownfish::CFC::Model::Function. - * @param exposure See Clownfish::CFC::Model::Function. Defaults to "parcel" + * @param exposure See Clownfish::CFC::Model::Symbol. Defaults to "parcel" * if not supplied. * @param class_name See Clownfish::CFC::Model::Function. * @param class_nickname See Clownfish::CFC::Model::Function.
