diff options
| author | 2025-12-15 22:39:52 +0100 | |
|---|---|---|
| committer | 2025-12-15 22:39:52 +0100 | |
| commit | 302e24671942051d70707586cf8c605a5815edac (patch) | |
| tree | 51e25fb6cd7e828c82ce5f17ffc775117121acee /ir/ir.h | |
| parent | c6c0f2ef35175075e91169113cfe856f29b3eb9a (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 'ir/ir.h')
| -rw-r--r-- | ir/ir.h | 12 |
1 files changed, 6 insertions, 6 deletions
@@ -22,14 +22,14 @@ struct irdat { union type ctype; uint siz; uint off; - const char *name; + internstr name; }; struct xcon { bool issym, isdat, isfunc, deref; uchar cls; union { - const char *sym; + internstr sym; int dat; vlong i; double f; @@ -163,7 +163,7 @@ enum { }; struct function { struct arena **arena; - const char *name; + internstr name; struct block *entry, *curblk; struct use *use; short *nuse; @@ -250,9 +250,9 @@ union ref mkfltcon(enum irclass, double); #define isaddrcon(r,derefok) ((r).t == RXCON && !conht[(r).i].cls && (derefok || !conht[(r).i].deref)) #define intconval(r) ((r).t == RICON ? (r).i : conht[(r).i].i) #define fltconval(r) ((r).t == RICON ? (r).i : conht[(r).i].f) -union ref mksymref(const char *, bool isfunc); -union ref mkdatref(const char *name, union type ctype, uint siz, uint align, const void *, uint n, bool deref); -const char *xcon2sym(int ref); +union ref mksymref(internstr, bool isfunc); +union ref mkdatref(internstr name, union type ctype, uint siz, uint align, const void *, uint n, bool deref); +internstr xcon2sym(int ref); struct instr mkalloca(uint siz, uint align); union ref mkcallarg(union irtype ret, uint narg, int vararg); #define mkintrin(B, C, N) mkinstr(Ointrin, C, {{.t=RICON,B}}, mkcallarg((union irtype){{0}},N,-1)) |