aboutsummaryrefslogtreecommitdiffhomepage
path: root/type.h
diff options
context:
space:
mode:
authorlemon <lsof@mailbox.org>2025-12-15 22:39:52 +0100
committerlemon <lsof@mailbox.org>2025-12-15 22:39:52 +0100
commit302e24671942051d70707586cf8c605a5815edac (patch)
tree51e25fb6cd7e828c82ce5f17ffc775117121acee /type.h
parentc6c0f2ef35175075e91169113cfe856f29b3eb9a (diff)
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.
Diffstat (limited to 'type.h')
-rw-r--r--type.h12
1 files changed, 6 insertions, 6 deletions
diff --git a/type.h b/type.h
index decef3c..c3dffb7 100644
--- a/type.h
+++ b/type.h
@@ -66,7 +66,7 @@ union type {
#define mktype(...) ((union type) {{ __VA_ARGS__ }})
struct enumvar {
- const char *name;
+ internstr name;
union { vlong i; uvlong u; };
};
@@ -78,7 +78,7 @@ struct fielddata {
qual : 2;
};
struct namedfield {
- const char *name;
+ internstr name;
struct fielddata f;
};
@@ -118,7 +118,7 @@ struct typedata {
};
extern struct typedata typedata[];
-extern const char *ttypenames[/*id*/];
+extern internstr ttypenames[/*id*/];
bool isincomplete(union type);
uint typesize(union type);
@@ -126,9 +126,9 @@ uint typealign(union type);
union type mkptrtype(union type, int qual);
union type mkarrtype(union type t, int qual, uint n);
union type mkfntype(union type ret, uint n, const union type *, bool kandr, bool variadic);
-union type mktagtype(const char *name, struct typedata *td);
-bool getfield(struct fielddata *res, union type, const char *);
-union type completetype(const char *name, int id, struct typedata *td);
+union type mktagtype(internstr name, struct typedata *td);
+bool getfield(struct fielddata *res, union type, internstr);
+union type completetype(internstr name, int id, struct typedata *td);
union type typedecay(union type);
bool assigncompat(union type dst, union type src);
enum typetag intpromote(enum typetag);