# HG changeset patch # User Paul Boddie # Date 1486825480 -3600 # Node ID b76a0be09cb2822382003ba3e8cc9d1ea7d4f89e # Parent 7a6d39d57799626081fa524891dedae9bfa09634 Removed the size member from __attr, requiring strlen to be used to get the size where it is needed in the native functions. diff -r 7a6d39d57799 -r b76a0be09cb2 generator.py --- a/generator.py Sat Feb 11 15:34:30 2017 +0100 +++ b/generator.py Sat Feb 11 16:04:40 2017 +0100 @@ -902,8 +902,7 @@ # Special internal data member. elif attrname == "__data__": - structure.append("{{.size=%d, .%s=%s}}" % ( - encode_literal_constant_size(attr), + structure.append("{.%s=%s}" % ( encode_literal_constant_member(attr), encode_literal_constant_value(attr))) continue diff -r 7a6d39d57799 -r b76a0be09cb2 templates/native/buffer.c --- a/templates/native/buffer.c Sat Feb 11 15:34:30 2017 +0100 +++ b/templates/native/buffer.c Sat Feb 11 16:04:40 2017 +0100 @@ -1,6 +1,6 @@ /* Native functions for buffer operations. -Copyright (C) 2016 Paul Boddie +Copyright (C) 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -37,7 +37,7 @@ /* Calculate the size of the string. */ for (i = 0; i < data->size; i++) - size += __load_via_object(data->attrs[i].value, __pos___data__).size; + size += strlen(__load_via_object(data->attrs[i].value, __pos___data__).strvalue); /* Reserve space for a new string. */ s = (char *) __ALLOCATE(size + 1, sizeof(char)); @@ -46,13 +46,13 @@ for (i = 0, j = 0; i < data->size; i++) { o = __load_via_object(data->attrs[i].value, __pos___data__); - n = o.size; + n = strlen(o.strvalue); memcpy(s + j, o.strvalue, n); /* does not null terminate but final byte should be zero */ j += n; } /* Return a new string. */ - return __new_str(s, size); + return __new_str(s); } /* Module initialisation. */ diff -r 7a6d39d57799 -r b76a0be09cb2 templates/native/common.c --- a/templates/native/common.c Sat Feb 11 15:34:30 2017 +0100 +++ b/templates/native/common.c Sat Feb 11 16:04:40 2017 +0100 @@ -1,6 +1,6 @@ /* Common operations for native functions. -Copyright (C) 2016 Paul Boddie +Copyright (C) 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -34,11 +34,10 @@ return attr; } -__attr __new_str(char *s, size_t size) +__attr __new_str(char *s) { /* Create a new string and mutate the __data__ and __key__ attributes. */ __attr attr = __new(&__InstanceTable___builtins___str_string, &__builtins___str_string, sizeof(__obj___builtins___str_string)); - attr.value->attrs[__pos___data__].size = size; attr.value->attrs[__pos___data__].strvalue = s; attr.value->attrs[__pos___key__] = __NULL; return attr; diff -r 7a6d39d57799 -r b76a0be09cb2 templates/native/common.h --- a/templates/native/common.h Sat Feb 11 15:34:30 2017 +0100 +++ b/templates/native/common.h Sat Feb 11 16:04:40 2017 +0100 @@ -1,6 +1,6 @@ /* Common operations for native functions. -Copyright (C) 2016 Paul Boddie +Copyright (C) 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -24,7 +24,7 @@ /* Utility functions. */ __attr __new_int(int i); -__attr __new_str(char *s, size_t size); +__attr __new_str(char *s); __attr __new_list(__fragment *f); __fragment *__fragment_append(__fragment *data, __attr * const value); diff -r 7a6d39d57799 -r b76a0be09cb2 templates/native/iconv.c --- a/templates/native/iconv.c Sat Feb 11 15:34:30 2017 +0100 +++ b/templates/native/iconv.c Sat Feb 11 16:04:40 2017 +0100 @@ -1,6 +1,6 @@ /* Native functions for character set conversion. -Copyright (C) 2016 Paul Boddie +Copyright (C) 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -99,9 +99,9 @@ /* Incomplete sequence: raise the string in an OSError instead. */ if (errno == EINVAL) - __raise_incomplete_sequence_error(__new_int(errno), __new_str(resultbuf, outbytestotal)); + __raise_incomplete_sequence_error(__new_int(errno), __new_str(resultbuf)); - return __new_str(resultbuf, outbytestotal); + return __new_str(resultbuf); } /* Invalid sequence. */ @@ -110,7 +110,7 @@ { resultbuf = __ALLOCATE(inbytesleft + 1, sizeof(char)); memcpy(resultbuf, inbuf, inbytesleft); - __raise_invalid_sequence_error(__new_int(errno), __new_str(resultbuf, inbytesleft)); + __raise_invalid_sequence_error(__new_int(errno), __new_str(resultbuf)); } /* General failure. */ diff -r 7a6d39d57799 -r b76a0be09cb2 templates/native/int.c --- a/templates/native/int.c Sat Feb 11 15:34:30 2017 +0100 +++ b/templates/native/int.c Sat Feb 11 16:04:40 2017 +0100 @@ -1,6 +1,6 @@ /* Native functions for integer operations. -Copyright (C) 2016 Paul Boddie +Copyright (C) 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -268,7 +268,7 @@ snprintf(s, n, "%d", i); /* Return a new string. */ - return __new_str(s, strlen(s)); + return __new_str(s); } /* Module initialisation. */ diff -r 7a6d39d57799 -r b76a0be09cb2 templates/native/io.c --- a/templates/native/io.c Sat Feb 11 15:34:30 2017 +0100 +++ b/templates/native/io.c Sat Feb 11 16:04:40 2017 +0100 @@ -1,6 +1,6 @@ /* Native functions for input/output. -Copyright (C) 2016 Paul Boddie +Copyright (C) 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -150,7 +150,7 @@ s = __ALLOCATE(have_read + 1, sizeof(char)); memcpy(s, (char *) buf, have_read); /* does not null terminate but final byte should be zero */ - return __new_str(s, have_read); + return __new_str(s); } __attr __fn_native_io_fwrite(__attr __args[]) @@ -162,7 +162,7 @@ /* str.__data__ interpreted as string */ __attr sa = __load_via_object(str->value, __pos___data__); char *s = sa.strvalue; - size_t to_write = sa.size; + size_t to_write = strlen(sa.strvalue); size_t have_written = fwrite(s, sizeof(char), to_write, f); int error; @@ -212,7 +212,7 @@ s = __ALLOCATE(have_read + 1, 1); memcpy(s, (char *) buf, have_read); /* does not null terminate but final byte should be zero */ - return __new_str(s, have_read); + return __new_str(s); } __attr __fn_native_io_write(__attr __args[]) @@ -227,7 +227,7 @@ ssize_t have_written; errno = 0; - have_written = write(i, s, sizeof(char) * sa.size); + have_written = write(i, s, sizeof(char) * strlen(sa.strvalue)); if (have_written == -1) __raise_io_error(__new_int(errno)); diff -r 7a6d39d57799 -r b76a0be09cb2 templates/native/list.c --- a/templates/native/list.c Sat Feb 11 15:34:30 2017 +0100 +++ b/templates/native/list.c Sat Feb 11 16:04:40 2017 +0100 @@ -32,7 +32,7 @@ __attr * const size = &__args[1]; /* size.__data__ interpreted as int */ unsigned int n = __load_via_object(size->value, __pos___data__).intvalue; - __attr attr = {{.size=0, .seqvalue=__new_fragment(n)}}; + __attr attr = {.seqvalue=__new_fragment(n)}; /* Return the __data__ attribute. */ return attr; @@ -61,7 +61,7 @@ /* Replace the __data__ attribute if appropriate. */ if (newdata != data) - __store_via_object(self->value, __pos___data__, ((__attr) {{.size=0, .seqvalue=newdata}})); + __store_via_object(self->value, __pos___data__, ((__attr) {.seqvalue=newdata})); return __builtins___none_None; } @@ -92,7 +92,7 @@ /* Replace the __data__ attribute if appropriate. */ if (newdata != data) - __store_via_object(self->value, __pos___data__, ((__attr) {{.size=0, .seqvalue=newdata}})); + __store_via_object(self->value, __pos___data__, ((__attr) {.seqvalue=newdata})); return __builtins___none_None; } diff -r 7a6d39d57799 -r b76a0be09cb2 templates/native/locale.c --- a/templates/native/locale.c Sat Feb 11 15:34:30 2017 +0100 +++ b/templates/native/locale.c Sat Feb 11 16:04:40 2017 +0100 @@ -46,7 +46,7 @@ out = __ALLOCATE(length + 1, sizeof(char)); strncpy(out, result, length); - return __new_str(result, length); + return __new_str(result); } __attr __fn_native_locale_setlocale(__attr __args[]) @@ -69,7 +69,7 @@ out = __ALLOCATE(length + 1, sizeof(char)); strncpy(out, result, length); - return __new_str(result, length); + return __new_str(result); } /* Module initialisation. */ diff -r 7a6d39d57799 -r b76a0be09cb2 templates/native/str.c --- a/templates/native/str.c Sat Feb 11 15:34:30 2017 +0100 +++ b/templates/native/str.c Sat Feb 11 16:04:40 2017 +0100 @@ -35,14 +35,15 @@ /* _data, other interpreted as string */ char *s = _data->strvalue; char *o = other->strvalue; - int n = _data->size + other->size; + size_t ss = strlen(_data->strvalue), os = strlen(other->strvalue); + int n = ss + os; char *r = (char *) __ALLOCATE(n + 1, sizeof(char)); - memcpy(r, s, _data->size); - memcpy(r + _data->size, o, other->size); + memcpy(r, s, ss); + memcpy(r + ss, o, os); /* Return a new string. */ - return __new_str(r, n); + return __new_str(r); } __attr __fn_native_str_str_chr(__attr __args[]) @@ -53,7 +54,7 @@ char *s = (char *) __ALLOCATE(2, sizeof(char)); s[0] = (char) n; - return __new_str(s, 1); + return __new_str(s); } __attr __fn_native_str_str_lt(__attr __args[]) @@ -97,14 +98,14 @@ __attr * const _data = &__args[1]; /* Return the new integer. */ - return __new_int(_data->size); + return __new_int(strlen(_data->strvalue)); } __attr __fn_native_str_str_nonempty(__attr __args[]) { __attr * const _data = &__args[1]; - return _data->size ? __builtins___boolean_True : __builtins___boolean_False; + return _data->strvalue[0] ? __builtins___boolean_True : __builtins___boolean_False; } __attr __fn_native_str_str_ord(__attr __args[]) @@ -146,7 +147,7 @@ for (from = istart, to = 0; from > iend; from += istep, to++) sub[to] = s[from]; - return __new_str(sub, resultsize); + return __new_str(sub); } /* Module initialisation. */ diff -r 7a6d39d57799 -r b76a0be09cb2 templates/native/unicode.c --- a/templates/native/unicode.c Sat Feb 11 15:34:30 2017 +0100 +++ b/templates/native/unicode.c Sat Feb 11 16:04:40 2017 +0100 @@ -76,7 +76,7 @@ char *s = _data->strvalue; unsigned int i, c = 0; - for (i = 0; i < _data->size; i++) + for (i = 0; s[i] != 0; i++) if (boundary(s[i])) c++; @@ -91,7 +91,7 @@ char *s = _data->strvalue; unsigned int i, c = 0, v; - for (i = 0; i < _data->size; i++) + for (i = 0; s[i] != 0; i++) { /* Evaluate the current character as a boundary. */ @@ -137,14 +137,14 @@ unsigned int indexes[nchar]; unsigned int c, d, i, to, from, lastbyte = 0; - size_t resultsize = 0; + size_t resultsize = 0, ss = strlen(_data->strvalue); /* Find the indexes of the characters. */ if (istep > 0) { /* Get the first byte position. */ for (c = 0; c < istart; c++) - lastbyte = nextpos(s, _data->size, lastbyte); + lastbyte = nextpos(s, ss, lastbyte); /* Get each subsequent byte position. */ for (c = istart, i = 0; i < nchar; c += istep, i++) @@ -152,17 +152,17 @@ indexes[i] = lastbyte; /* Add the character size to the result size. */ - resultsize += nextpos(s, _data->size, lastbyte) - lastbyte; + resultsize += nextpos(s, ss, lastbyte) - lastbyte; for (d = c; d < c + istep; d++) - lastbyte = nextpos(s, _data->size, lastbyte); + lastbyte = nextpos(s, ss, lastbyte); } } else { /* Get the first byte position. */ for (c = 0; c < istart; c++) - lastbyte = nextpos(s, _data->size, lastbyte); + lastbyte = nextpos(s, ss, lastbyte); /* Get each subsequent byte position. */ for (c = istart, i = 0; i < nchar; c += istep, i++) @@ -170,7 +170,7 @@ indexes[i] = lastbyte; /* Add the character size to the result size. */ - resultsize += nextpos(s, _data->size, lastbyte) - lastbyte; + resultsize += nextpos(s, ss, lastbyte) - lastbyte; for (d = c; d > c + istep; d--) lastbyte = prevpos(s, lastbyte); @@ -190,7 +190,7 @@ } while (!boundary(s[from])); } - return __new_str(sub, resultsize); + return __new_str(sub); } /* Module initialisation. */ diff -r 7a6d39d57799 -r b76a0be09cb2 templates/progops.c --- a/templates/progops.c Sat Feb 11 15:34:30 2017 +0100 +++ b/templates/progops.c Sat Feb 11 16:04:40 2017 +0100 @@ -57,7 +57,7 @@ /* Calculate the size of the fragment. */ __fragment *data = __new_fragment(number); - __attr attr = {{.size=0, .seqvalue=data}}; + __attr attr = {.seqvalue=data}; unsigned int i, j; /* Copy the given number of values, starting from the second element. */ diff -r 7a6d39d57799 -r b76a0be09cb2 templates/progops.h --- a/templates/progops.h Sat Feb 11 15:34:30 2017 +0100 +++ b/templates/progops.h Sat Feb 11 16:04:40 2017 +0100 @@ -1,6 +1,6 @@ /* Operations depending on program specifics. -Copyright (C) 2015, 2016 Paul Boddie +Copyright (C) 2015, 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -19,6 +19,7 @@ #ifndef __PROGOPS_H__ #define __PROGOPS_H__ +#include /* size_t */ #include "types.h" /* Generic instantiation operations, defining common members. */ diff -r 7a6d39d57799 -r b76a0be09cb2 templates/types.h --- a/templates/types.h Sat Feb 11 15:34:30 2017 +0100 +++ b/templates/types.h Sat Feb 11 16:04:40 2017 +0100 @@ -19,8 +19,6 @@ #ifndef __TYPES_H__ #define __TYPES_H__ -#include /* size_t */ - /* Define code and position types, populated by enum values defined for each program specifically. */ @@ -82,16 +80,13 @@ struct __attr (*inv)(); /* unbound callable details */ struct __attr (*fn)(); /* callable details */ }; - struct { - size_t size; /* size of value */ - union - { - int intvalue; /* integer value */ - double floatvalue; /* floating point value */ - char * strvalue; /* string value */ - __fragment * seqvalue; /* sequence data */ - void * datavalue; /* object-specific data */ - }; + union + { + int intvalue; /* integer value */ + double floatvalue; /* floating point value */ + char * strvalue; /* string value */ + __fragment * seqvalue; /* sequence data */ + void * datavalue; /* object-specific data */ }; }; } __attr;