From 302e24671942051d70707586cf8c605a5815edac Mon Sep 17 00:00:00 2001 From: lemon Date: Mon, 15 Dec 2025 22:39:52 +0100 Subject: create distinct interned string type Interned strings are used pervasively, so it's a good idea to add a layer of type safety to differentiate them from general cstrs and avoid potential bugs from comparing non-interned and interned strings. Not that that's happened so far that I can remember, but it could. I'm 90% sure it's legal to alias `struct {char c;}` pointers with `char` pointers. This specific typedef gives type safety but with a simple one-way `internstr -> const char *` typecast (with `&istr->c`). Converting the other way around is more intentional: a straight up cast `(internstr)cstr` which sticks out as unchecked and probably wrong, or calling the intern(cstr) function, which is the right way. --- type.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'type.c') diff --git a/type.c b/type.c index b18feb2..e8a5b1e 100644 --- a/type.c +++ b/type.c @@ -1,7 +1,7 @@ #include "type.h" struct typedata typedata[1<<13]; -const char *ttypenames[1<<10]; +internstr ttypenames[1<<10]; static ushort hashtd(const struct typedata *td) @@ -183,7 +183,7 @@ mkfntype(union type ret, uint n, const union type *par, bool kandr, bool variadi } union type -completetype(const char *name, int id, struct typedata *td) +completetype(internstr name, int id, struct typedata *td) { assert(td->t == TYENUM || td->t == TYSTRUCT || td->t == TYUNION); td->id = id; @@ -196,14 +196,14 @@ completetype(const char *name, int id, struct typedata *td) } union type -mktagtype(const char *name, struct typedata *td) +mktagtype(internstr name, struct typedata *td) { static int id; return completetype(name, id++, td); } static bool -getfieldrec(struct fielddata *res, uint off, const struct typedata *td, const char *name) +getfieldrec(struct fielddata *res, uint off, const struct typedata *td, internstr name) { Begin: for (int i = 0; i < td->nmemb; ++i) { @@ -227,7 +227,7 @@ Begin: } bool -getfield(struct fielddata *res, union type ty, const char *name) +getfield(struct fielddata *res, union type ty, internstr name) { assert(isagg(ty)); return getfieldrec(res, 0, &typedata[ty.dat], name); -- cgit v1.2.3