harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 5dd2f47cc59173ff36e3cf323e64e35902c8285a
parent ab0bd2f1708829d387db797af79458717c86abc4
Author: Drew DeVault <sir@cmpwn.com>
Date:   Thu,  1 Apr 2021 13:06:47 -0400

Remove str => *const char cast

Diffstat:
Mrt/Makefile | 1+
Mrt/abort.ha | 16++++++++--------
Mrt/compile.ha | 8++++++--
Art/cstrings.ha | 10++++++++++
Msrc/gen.c | 8++------
Msrc/types.c | 5+----
Mtests/04-strings.ha | 13+------------
7 files changed, 29 insertions(+), 32 deletions(-)

diff --git a/rt/Makefile b/rt/Makefile @@ -1,5 +1,6 @@ libhart_srcs+=\ rt/abort.ha \ + rt/cstrings.ha \ rt/compile.ha \ rt/ensure.ha \ rt/malloc.ha \ diff --git a/rt/abort.ha b/rt/abort.ha @@ -1,8 +1,8 @@ export @noreturn @symbol("rt.abort") fn _abort(msg: str) void = { const prefix = "Abort: "; - write(2, prefix: *const char, len(prefix)); - write(2, msg: *const char, len(msg)); - write(2, "\n": *const char, 1); + write(2, constchar(prefix), len(prefix)); + write(2, constchar(msg), len(msg)); + write(2, constchar("\n"), 1); kill(getpid(), SIGABRT); }; @@ -16,10 +16,10 @@ const reasons: [_]str = [ export @noreturn fn abort_fixed(loc: str, i: int) void = { const prefix = "Abort: "; const sep = ": "; - write(2, prefix: *const char, len(prefix)); - write(2, loc: *const char, len(loc)); - write(2, sep: *const char, len(sep)); - write(2, reasons[i]: *const char, len(reasons[i])); - write(2, "\n": *const char, 1); + write(2, constchar(prefix), len(prefix)); + write(2, constchar(loc), len(loc)); + write(2, constchar(sep), len(sep)); + write(2, constchar(reasons[i]), len(reasons[i])); + write(2, constchar("\n"), 1); kill(getpid(), SIGABRT); }; diff --git a/rt/compile.ha b/rt/compile.ha @@ -12,14 +12,18 @@ export fn compile(src: const str) int = { close(2); const path = "./harec"; - const argv: [_]nullable *const char = [path, "-", null]; + const argv: [_]nullable *const char = [ + constchar(path), + constchar("-"), + null + ]; const envp: [_]nullable *const char = [null]; execve(path, &argv, &envp); abort(); } else { close(pipefd[0]); - const buf = src: *const char: *const [*]u8; + const buf = constchar(src): *const [*]u8; for (let n = 0z; n < len(src)) { let m = write(pipefd[1], &buf[n], len(src) - n); assert(m > 0, "write(2) failed"); diff --git a/rt/cstrings.ha b/rt/cstrings.ha @@ -0,0 +1,10 @@ +type string = struct { + data: nullable *[*]u8, + length: size, + capacity: size, +}; + +fn constchar(s: str) *const char = { + let s = &s: *string; + return s.data: *const char; +}; diff --git a/src/gen.c b/src/gen.c @@ -1305,12 +1305,8 @@ gen_expr_cast(struct gen_context *ctx, struct qbe_value in = {0}, result = {0}; gen_temp(ctx, &result, qtype_for_type(ctx, to, false), "cast.out.%d"); - // Special case: str -> *const char; slice -> ptr - if ((to->storage == STORAGE_POINTER - && to->pointer.referent->storage == STORAGE_CHAR - && from->storage == STORAGE_STRING) - || (to->storage == STORAGE_POINTER - && from->storage == STORAGE_SLICE)) { + // Special case: slice -> ptr + if (to->storage == STORAGE_POINTER && from->storage == STORAGE_SLICE) { alloc_temp(ctx, &in, from, "cast.in.%d"); in.indirect = false; gen_expression(ctx, expr->cast.value, &in); diff --git a/src/types.c b/src/types.c @@ -705,11 +705,8 @@ type_is_castable(const struct type *to, const struct type *from) || (to->storage == STORAGE_POINTER && to->pointer.referent->storage == STORAGE_ARRAY && from->storage == STORAGE_SLICE); - case STORAGE_STRING: - return to->storage == STORAGE_POINTER - && to->pointer.referent->storage == STORAGE_CHAR - && to->pointer.referent->flags & TYPE_CONST; // Cannot be cast: + case STORAGE_STRING: case STORAGE_BOOL: case STORAGE_VOID: case STORAGE_FUNCTION: diff --git a/tests/04-strings.ha b/tests/04-strings.ha @@ -11,16 +11,6 @@ fn measurements() void = { assert(&x: uintptr: size % align == 0); }; -fn charptr() void = { - const x = "Hello!"; - const y = x: *const char; - const z = y: *[*]u8; - const expected = ['H', 'e', 'l', 'l', 'o', '!']; - for (let i = 0z; i < len(expected); i += 1) { - assert(z[i] == expected[i]: u32: u8); - }; -}; - fn storage() void = { const string = "こんにちは"; const ptr = &string: *struct { @@ -50,7 +40,7 @@ fn storage() void = { fn concat() void = { const s = "Hell" "o, " "wor" "ld!"; - const t = s: *const char: *[*]u8; + const t = *(&s: **[*]u8); const expected = [ 'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', @@ -69,7 +59,6 @@ fn equality() void = { export fn main() void = { measurements(); - charptr(); storage(); concat(); equality();