def __generate(self): self.__process() for namespace in self.api_description.namespaces: Capi.__substitute_implementation_class_name(namespace) namespace_generators = create_namespace_generators( self.api_description, self.params_description) by_first_argument_exception_traits = ExceptionTraits.ByFirstArgument( self.params_description, namespace_generators) no_handling_exception_traits = ExceptionTraits.NoHandling() if self.params_description.exception_handling_mode == TExceptionHandlingMode.by_first_argument: main_exception_traits = by_first_argument_exception_traits else: main_exception_traits = no_handling_exception_traits capi_generator = CapiGenerator(main_exception_traits, no_handling_exception_traits, self.params_description, self.api_description) file_cache = FileCache(self.params_description) for namespace_generator in namespace_generators: namespace_generator.generate(file_cache, capi_generator) capi_generator.generate(file_cache) self.__generate_root_header(namespace_generators, file_cache) if self.unit_tests_generator: self.unit_tests_generator.generate(namespace_generators)
def generate_c_function(self, capi_generator: CapiGenerator): c_arguments_list = [ argument_generator.c_argument_declaration() for argument_generator in self.argument_generators ] self.exception_traits.modify_c_arguments(c_arguments_list) c_arguments = ', '.join(c_arguments_list) implementation_arguments = ', '.join([ argument_generator.c_2_implementation() for argument_generator in self.argument_generators ]) c_function_body = FileGenerator(None) with IndentScope(c_function_body): function_name = self.function_object.implementation_name if not function_name: function_name = self.function_object.name implementation_call = '{function_name}({arguments})'.format( function_name=function_name, arguments=implementation_arguments) calling_instructions, return_expression = self.return_type_generator.implementation_2_c_var( '', implementation_call) if return_expression: calling_instructions.append( 'return {0};'.format(return_expression)) self.exception_traits.generate_implementation_call( c_function_body, self.return_type_generator, calling_instructions) capi_generator.add_c_function( self.parent_namespace_generator.full_name_array, self.return_type_generator.c_argument_declaration(), self.full_c_name, c_arguments, c_function_body)
def generate_c_function(self, capi_generator: CapiGenerator): argument_declaration_list = [ argument_generator.c_argument_declaration() for argument_generator in self.argument_generators ] self.exception_traits.modify_c_arguments(argument_declaration_list) arguments_declaration = ', '.join(argument_declaration_list) implementation_arguments = ', '.join([ argument_generator.c_2_implementation() for argument_generator in self.argument_generators ]) c_function_body = FileGenerator(None) with IndentScope(c_function_body): if self.constructor_object.implementation_codes: calling_instructions = generate_custom_implementation_code( self.constructor_object.implementation_codes[0], self.argument_generators, self.parent_class_as_argument_type) else: implementation_call = 'return new {impl_class}({arguments});'.format( impl_class=self.parent_class_generator.class_object. implementation_class_name, arguments=implementation_arguments) calling_instructions = [implementation_call] self.exception_traits.generate_implementation_call( c_function_body, self.parent_class_as_argument_type, calling_instructions) capi_generator.add_c_function( self.parent_class_generator.full_name_array, self.parent_class_as_argument_type.c_argument_declaration(), self.full_c_name, arguments_declaration, c_function_body)
def generate_c_function(self, capi_generator: CapiGenerator): argument_declaration_list = [ argument_generator.c_argument_declaration() for argument_generator in self.c_arguments_list ] self.exception_traits.modify_c_arguments(argument_declaration_list) arguments_declaration = ', '.join(argument_declaration_list) implementation_arguments = ', '.join([ argument_generator.c_2_implementation() for argument_generator in self.argument_generators ]) c_function_body = FileGenerator(None) with IndentScope(c_function_body): c_function_body.put_line( '{const}{self_impl_class}* self = {to_impl_cast};'.format( const='const ' if self.method_object.const else '', self_impl_class=self.parent_class_generator.class_object. implementation_class_name, to_impl_cast=self.c_arguments_list[0].c_2_implementation( ))) if self.method_object.implementation_codes: calling_instructions = generate_custom_implementation_code( self.method_object.implementation_codes[0], self.argument_generators, self.return_type_generator) else: self_access = 'self->' if self.parent_class_generator.class_object.pointer_access: self_access = '(*self)->' method_name = self.method_object.implementation_name if not method_name: method_name = self.method_object.name if self.method_object.getter_field_name_filled: implementation_call = '{self_access}{field_name}'.format( self_access=self_access, field_name=self.method_object.getter_field_name) elif self.method_object.setter_field_name_filled: implementation_call = '{self_access}{field_name} = {arguments}'.format( self_access=self_access, field_name=self.method_object.setter_field_name, arguments=implementation_arguments) else: implementation_call = '{self_access}{method_name}({arguments})'.format( self_access=self_access, method_name=method_name, arguments=implementation_arguments) calling_instructions, return_expression = self.return_type_generator.implementation_2_c_var( '', implementation_call) if return_expression: calling_instructions.append( 'return {0};'.format(return_expression)) self.exception_traits.generate_implementation_call( c_function_body, self.return_type_generator, calling_instructions) capi_generator.add_c_function( self.parent_class_generator.full_name_array, self.return_type_generator.c_argument_declaration(), self.full_c_name, arguments_declaration, c_function_body)
def generate_wrap_definition(self, out: FileGenerator, capi_generator: CapiGenerator): self.exception_traits = capi_generator.get_exception_traits( self.constructor_object.noexcept) arguments = ', '.join([ argument_generator.wrap_argument_declaration() for argument_generator in self.argument_generators ]) arguments_call = [ argument_generator.wrap_2_c() for argument_generator in self.argument_generators ] out.put_line( 'inline {namespace}::{class_name}({arguments}){base_init}'.format( namespace=self.parent_class_generator.full_wrap_name, class_name=self.parent_class_generator.wrap_short_name, arguments=arguments, base_init=get_base_init(self.parent_class_generator))) with IndentScope(out): result_expression = self.exception_traits.generate_c_call( out, self.parent_class_as_argument_type, self.full_c_name, arguments_call) out.put_line('SetObject({result_expression}.{detach}());'.format( result_expression=result_expression, detach=self.params.detach_method_name))
def wrap_declaration(self, capi_generator: CapiGenerator) -> str: self.exception_traits = capi_generator.get_exception_traits( self.method_object.noexcept) arguments = ', '.join([ argument_generator.wrap_argument_declaration() for argument_generator in self.argument_generators ]) return 'inline {return_type} {name}({arguments}){const}'.format( return_type=self.return_type_generator.wrap_return_type(), name=self.method_object.name, arguments=arguments, const=' const' if self.method_object.const else '')
def generate_wrap_definition(self, out: FileGenerator, capi_generator: CapiGenerator): self.exception_traits = capi_generator.get_exception_traits( self.function_object.noexcept) arguments = ', '.join([ argument_generator.wrap_argument_declaration() for argument_generator in self.argument_generators ]) arguments_call = [ argument_generator.wrap_2_c() for argument_generator in self.argument_generators ] out.put_line('inline {return_type} {name}({arguments})'.format( return_type=self.return_type_generator.wrap_return_type(), name=self.wrap_name, arguments=arguments)) with IndentScope(out): self.return_type_generator.copy_or_add_ref_when_c_2_wrap = self.function_object.return_copy_or_add_ref return_expression = self.exception_traits.generate_c_call( out, self.return_type_generator, self.full_c_name, arguments_call) out.put_return_cpp_statement(return_expression)