# HG changeset patch # User Paul Boddie # Date 1488906600 -3600 # Node ID d3e1284d1590182a302d6b3c5b57b4ebb1a45740 # Parent 7d209b6bcfcd3558c65c6d1fd985ef3252be94aa Changed instantiators to use normal function parameters, simplifying their implementation by making initialisers return self. Made literal instantiators macros. diff -r 7d209b6bcfcd -r d3e1284d1590 generator.py --- a/generator.py Tue Mar 07 17:32:14 2017 +0100 +++ b/generator.py Tue Mar 07 18:10:00 2017 +0100 @@ -1183,60 +1183,52 @@ NOTE: where __call__ is provided by the class. """ - parameters = self.importer.function_parameters[init_ref.get_origin()] initialiser = init_ref.get_origin() + parameters = self.importer.function_parameters[initialiser] argmin, argmax = self.get_argument_limits(initialiser) + l = [] + for name in parameters: + l.append("__attr %s" % name) + print >>f_code, """\ -__attr %s(__attr __args[]) +__attr %s(__attr __self%s) { - /* Allocate the structure. */ - __args[0] = __NEWINSTANCE(%s); - - /* Call the initialiser. */ - %s(%s, __args); - - /* Return the allocated object details. */ - return __args[0]; + return %s(__NEWINSTANCE(%s)%s); } """ % ( - encode_instantiator_pointer(path), - encode_path(path), - "__CALL%d" % argmax, - encode_function_pointer(initialiser) - ) + encode_instantiator_pointer(path), + l and ", %s" % ",".join(l) or "", + encode_function_pointer(initialiser), + encode_path(path), + parameters and ", %s" % ", ".join(parameters) or "" + ) + + # Signature: __new_typename(__attr __self, ...) + + print >>f_signatures, "__attr %s(__attr __self%s);" % ( + encode_instantiator_pointer(path), + l and ", %s" % ", ".join(l) or "" + ) print >>f_signatures, "#define __HAVE_%s" % encode_path(path) - print >>f_signatures, "__attr %s(__attr[]);" % encode_instantiator_pointer(path) # Write additional literal instantiators. These do not call the # initialisers but instead populate the structures directly. + # Signature: __newliteral_sequence(ARGS, NUM) + if path in self.literal_instantiator_types: if path in self.literal_mapping_types: style = "mapping" else: style = "sequence" - print >>f_code, """\ -__attr %s(__attr __args[], __pos number) -{ - /* Allocate the structure. */ - __args[0] = __NEWINSTANCE(%s); - - /* Allocate a structure for the data and set it on the __data__ attribute. */ - %s(__args, number); - - /* Return the allocated object details. */ - return __args[0]; -} -""" % ( - encode_literal_instantiator(path), - encode_path(path), - encode_literal_data_initialiser(style) - ) - - print >>f_signatures, "__attr %s(__attr[], __pos);" % encode_literal_instantiator(path) + print >>f_signatures, "#define %s(ARGS, NUM) (%s(__NEWINSTANCE(%s), ARGS, NUM))" % ( + encode_literal_instantiator(path), + encode_literal_data_initialiser(style), + encode_path(path) + ) def write_main_program(self, f_code, f_signatures): diff -r 7d209b6bcfcd -r d3e1284d1590 templates/native/iconv.c --- a/templates/native/iconv.c Tue Mar 07 17:32:14 2017 +0100 +++ b/templates/native/iconv.c Tue Mar 07 18:10:00 2017 +0100 @@ -33,18 +33,14 @@ static void __raise_incomplete_sequence_error(__attr value, __attr arg) { #ifdef __HAVE_posix_iconv_IncompleteSequenceError - __attr args[3] = {__NULL, value, arg}; - __attr exc = __new_posix_iconv_IncompleteSequenceError(args); - __Raise(exc); + __Raise(__new_posix_iconv_IncompleteSequenceError(__NULL, value, arg)); #endif /* __HAVE_posix_iconv_IncompleteSequenceError */ } static void __raise_invalid_sequence_error(__attr value, __attr arg) { #ifdef __HAVE_posix_iconv_InvalidSequenceError - __attr args[3] = {__NULL, value, arg}; - __attr exc = __new_posix_iconv_InvalidSequenceError(args); - __Raise(exc); + __Raise(__new_posix_iconv_InvalidSequenceError(__NULL, value, arg)); #endif /* __HAVE_posix_iconv_InvalidSequenceError */ } diff -r 7d209b6bcfcd -r d3e1284d1590 templates/progops.c --- a/templates/progops.c Tue Mar 07 17:32:14 2017 +0100 +++ b/templates/progops.c Tue Mar 07 18:10:00 2017 +0100 @@ -33,13 +33,13 @@ __ref obj = (__ref) __ALLOCATE(1, size); obj->table = table; obj->pos = __INSTANCEPOS; - __store_via_object(obj, __class__, (__attr) {.value=cls}); + __store_via_object(obj, __class__, __ATTRVALUE(cls)); return (__attr) {.value=obj}; } __attr __new_wrapper(__ref context, __attr attr) { - return __new___builtins___core_wrapper((__attr[]) {__NULL, {.value=context}, attr}); + return __new___builtins___core_wrapper(__NULL, __ATTRVALUE(context), attr); } /* Generic internal data allocation. */ @@ -57,43 +57,40 @@ return data; } -void __newdata_sequence(__attr args[], unsigned int number) +__attr __newdata_sequence(__attr self, __attr args[], unsigned int number) { /* Calculate the size of the fragment. */ __fragment *data = __new_fragment(number); __attr attr = {.seqvalue=data}; - unsigned int i, j; + unsigned int i; - /* Copy the given number of values, starting from the second element. */ + /* Copy the given number of values. */ - for (i = 1, j = 0; i <= number; i++, j++) - data->attrs[j] = args[i]; + for (i = 0; i <= number; i++) + data->attrs[i] = args[i]; data->size = number; /* Store a reference to the data in the object's __data__ attribute. */ - __store_via_object(args[0].value, __data__, attr); + __store_via_object(self.value, __data__, attr); + return self; } #ifdef __HAVE___builtins___dict_dict -void __newdata_mapping(__attr args[], unsigned int number) +__attr __newdata_mapping(__attr self, __attr args[], unsigned int number) { - __attr dict = args[0]; __attr callargs[2]; /* Create a temporary list using the arguments. */ - __newliteral___builtins___list_list(args, number); + __attr tmp = __newliteral___builtins___list_list(args, number); /* Call __init__ with the dict object and list argument. */ - callargs[0] = dict; - callargs[1] = args[0]; - - __fn___builtins___dict_dict___init__(callargs); - args[0] = dict; + __fn___builtins___dict_dict___init__(self, tmp); + return self; } #endif /* __HAVE___builtins___dict_dict */ @@ -102,56 +99,42 @@ void __raise_eof_error() { #ifdef __HAVE___builtins___exception_io_EOFError - __attr args[1]; - __attr exc = __new___builtins___exception_io_EOFError(args); - __Raise(exc); + __Raise(__new___builtins___exception_io_EOFError(__NULL)); #endif /* __HAVE___builtins___exception_io_EOFError */ } void __raise_io_error(__attr value) { #ifdef __HAVE___builtins___exception_io_IOError - __attr args[2] = {__NULL, value}; - __attr exc = __new___builtins___exception_io_IOError(args); - __Raise(exc); + __Raise(__new___builtins___exception_io_IOError(__NULL, value)); #endif /* __HAVE___builtins___exception_io_IOError */ } void __raise_memory_error() { - __attr args[1]; - __attr exc = __new___builtins___core_MemoryError(args); - __Raise(exc); + __Raise(__new___builtins___core_MemoryError(__NULL)); } void __raise_os_error(__attr value, __attr arg) { #ifdef __HAVE___builtins___exception_system_OSError - __attr args[3] = {__NULL, value, arg}; - __attr exc = __new___builtins___exception_system_OSError(args); - __Raise(exc); + __Raise(__new___builtins___exception_system_OSError(__NULL, value, arg)); #endif /* __HAVE___builtins___exception_system_OSError */ } void __raise_overflow_error() { - __attr args[1]; - __attr exc = __new___builtins___core_OverflowError(args); - __Raise(exc); + __Raise(__new___builtins___core_OverflowError(__NULL)); } void __raise_type_error() { - __attr args[1]; - __attr exc = __new___builtins___core_TypeError(args); - __Raise(exc); + __Raise(__new___builtins___core_TypeError(__NULL)); } void __raise_zero_division_error() { - __attr args[1]; - __attr exc = __new___builtins___core_ZeroDivisionError(args); - __Raise(exc); + __Raise(__new___builtins___core_ZeroDivisionError(__NULL)); } /* Helper for raising exception instances. */ @@ -266,9 +249,7 @@ __attr __unbound_method(__attr __self) { - __attr excargs[1]; - __attr exc = __new___builtins___core_UnboundMethodInvocation(excargs); - __Raise(exc); + __Raise(__new___builtins___core_UnboundMethodInvocation(__NULL)); return __builtins___none_None; /* superfluous */ } diff -r 7d209b6bcfcd -r d3e1284d1590 templates/progops.h --- a/templates/progops.h Tue Mar 07 17:32:14 2017 +0100 +++ b/templates/progops.h Tue Mar 07 18:10:00 2017 +0100 @@ -31,10 +31,10 @@ __fragment *__new_fragment(unsigned int n); -void __newdata_sequence(__attr args[], unsigned int number); +__attr __newdata_sequence(__attr self, __attr args[], unsigned int number); #ifdef __HAVE___builtins___dict_dict -void __newdata_mapping(__attr args[], unsigned int number); +__attr __newdata_mapping(__attr self, __attr args[], unsigned int number); #endif /* __HAVE___builtins___dict_dict */ /* Helpers for raising errors within common operations. */ diff -r 7d209b6bcfcd -r d3e1284d1590 translator.py --- a/translator.py Tue Mar 07 17:32:14 2017 +0100 +++ b/translator.py Tue Mar 07 18:10:00 2017 +0100 @@ -811,7 +811,12 @@ # Produce the body and any additional return statement. - expr = self.process_structure_node(n.code) or PredefinedConstantRef("None") + expr = self.process_structure_node(n.code) or \ + self.in_method() and \ + function_name.rsplit(".", 1)[-1] == "__init__" and \ + TrResolvedNameRef("self", self.importer.function_locals[function_name]["self"]) or \ + PredefinedConstantRef("None") + if not isinstance(expr, ReturnRef): self.writestmt("return %s;" % expr) @@ -847,7 +852,6 @@ # Produce an appropriate access to an attribute's value. - parameters = self.importer.function_parameters.get(self.get_namespace_path()) name_to_value = "%s.value" % name # Write a test that raises a TypeError upon failure. @@ -1184,18 +1188,17 @@ # Encode the arguments. - argstr = ", ".join(args) + # Where literal instantiation is occurring, add an argument indicating + # the number of values. The context is excluded. + + if literal_instantiation: + argstr = "__ARGS(%s), %d" % (", ".join(args[1:]), len(args) - 1) + else: + argstr = ", ".join(args) + kwargstr = kwargs and ("__ARGS(%s)" % ", ".join(kwargs)) or "0" kwcodestr = kwcodes and ("__KWARGS(%s)" % ", ".join(kwcodes)) or "0" - # Where literal instantiation is occurring, add an argument indicating - # the number of values. - - if literal_instantiation: - argstr = "__ARGS(%s), %d" % (argstr, len(args) - 1) - elif instantiation: - argstr = "__ARGS(%s)" % argstr - # First, the invocation expression is presented. stages = []