From 3ee275dbb8bcf5df7f54e7659b4a9a8f4f49911d Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Tue, 3 Nov 2026 00:16:51 +0100 Subject: Add initial support for opaque types --- src/prc1/from.c | 1 + src/prc1/kw.c | 2 +- src/prc1/parse.h | 2 +- src/prc1/print.c | 1 + src/prc1/sym.c | 19 ++++++++++--------- src/prc1/td.c | 38 ++++++++++++++++++++++++++++++++++++++ src/prc1/type.c | 2 ++ 7 files changed, 54 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/prc1/from.c b/src/prc1/from.c index 8561b8f..3a8892a 100644 --- a/src/prc1/from.c +++ b/src/prc1/from.c @@ -37,6 +37,7 @@ static int checksym(const struct fn *fn, const struct tk *tk, case S: case P: case T: + case OP: case PTR: case ARY: { diff --git a/src/prc1/kw.c b/src/prc1/kw.c index 8531c52..8065be5 100644 --- a/src/prc1/kw.c +++ b/src/prc1/kw.c @@ -9,7 +9,7 @@ static const char *kws[] = /* sections */ "types", "externals", "procedure", "locals", "globals", "imports", /* types */ - "union", "struct", "constant", "alias", "prototype", + "union", "struct", "constant", "alias", "prototype", "opaque", /* operators */ "add", "subtract", "multiply", "divide", "negate", "xor", "bitor", "bitand", "leftshift", "rightshift", "call", "set", diff --git a/src/prc1/parse.h b/src/prc1/parse.h index f0ae691..dc00f4b 100644 --- a/src/prc1/parse.h +++ b/src/prc1/parse.h @@ -49,7 +49,7 @@ struct builtin struct type { - enum ttype {U, S, C, P, T, PTR, ARY, BUILTIN} type; + enum ttype {U, S, C, P, T, OP, PTR, ARY, BUILTIN} type; const struct tk *tk; size_t sz, align; int sign, readonly; diff --git a/src/prc1/print.c b/src/prc1/print.c index 604c2a3..c40672d 100644 --- a/src/prc1/print.c +++ b/src/prc1/print.c @@ -87,6 +87,7 @@ static int settype(struct prv *p, const struct sym *s, struct prisym *ps) case U: case S: case T: + case OP: case ARY: return invalid(s, t); case C: diff --git a/src/prc1/sym.c b/src/prc1/sym.c index cc8c129..f4056de 100644 --- a/src/prc1/sym.c +++ b/src/prc1/sym.c @@ -333,15 +333,16 @@ static int invalid(const struct tk *tk, const struct type *a, { static const int t[BUILTIN + 1][BUILTIN + 1] = { - /* U S C P T PTR ARY BUILTIN */ - /* U */ {1, 0, 0, 0, 0, 0, 0, 0}, - /* S */ {0, 1, 0, 0, 0, 0, 0, 0}, - /* C */ {0, 0, 1, 0, 0, 0, 0, 0}, - /* P */ {0, 0, 0, 1, 0, 1, 0, 0}, - /* T */ {0, 0, 0, 0, 1, 0, 0, 0}, - /* PTR */ {0, 0, 0, 0, 0, 1, 1, 0}, - /* ARY */ {0, 0, 0, 0, 0, 0, 1, 0}, - /* BUILTIN */ {0, 0, 0, 0, 0, 0, 0, 1} + /* U S C P T OP PTR ARY BUILTIN */ + /* U */ {1, 0, 0, 0, 0, 0, 0, 0, 0}, + /* S */ {0, 1, 0, 0, 0, 0, 0, 0, 0}, + /* C */ {0, 0, 1, 0, 0, 0, 0, 0, 0}, + /* P */ {0, 0, 0, 1, 0, 0, 1, 0, 0}, + /* T */ {0, 0, 0, 0, 1, 0, 0, 0, 0}, + /* OP */ {0, 0, 0, 0, 0, 1, 0, 0, 0}, + /* PTR */ {0, 0, 0, 0, 0, 0, 1, 1, 1}, + /* ARY */ {0, 0, 0, 0, 0, 0, 0, 1, 0}, + /* BUILTIN */ {0, 0, 0, 0, 0, 0, 1, 0, 1} }; int valid = t[a->type][b->type]; diff --git a/src/prc1/td.c b/src/prc1/td.c index 0c455f2..b38a9d3 100644 --- a/src/prc1/td.c +++ b/src/prc1/td.c @@ -24,6 +24,7 @@ static int s(const struct lex *, struct prv *); static int c(const struct lex *, struct prv *); static int a(const struct lex *, struct prv *); static int p(const struct lex *, struct prv *); +static int op(const struct lex *, struct prv *); static const struct seq seqs[] = { @@ -39,6 +40,7 @@ static const struct seq seqs[] = {(struct step[]){{ID}, {ID, "struct"}, {0}}, .fn = s}, {(struct step[]){{ID}, {ID, "constant"}, {0}}, .fn = c}, {(struct step[]){{ID}, {ID, "prototype"}, {0}}, .fn = p}, + {(struct step[]){{ID}, {ID, "opaque"}, {0}}, .fn = op}, {0} }; @@ -359,6 +361,42 @@ failure: return -1; } +static int op(const struct lex *l, struct prv *p) +{ + const struct tk *id = p->stk; + const struct loc *loc = &id->loc; + const char *tr = triplet(p); + struct fn *fn = fn_cur(p); + struct td *td = fn->td; + struct type *nt; + const struct type *t = type_find(fn, tr, id->s); + + if (t) + { + const struct loc *loc = &t->tk->loc; + + errloc(id, "type \"%s\" already defined at %s:%d:%d", id->s, loc->f, + loc->line, loc->col); + return -1; + } + else if (!(nt = malloc(sizeof *nt))) + { + perror("realloc(3)"); + return -1; + } + + *nt = (struct type) + { + .type = OP, + .tk = id + }; + + insert(td, nt); + dbg("\tadd opaque type %s (%d:%d)\n", id->s, loc->line, loc->col); + reset(p); + return 0; +} + static int ends(struct type *t) { const struct s *s = &t->u.s; diff --git a/src/prc1/type.c b/src/prc1/type.c index 9267628..72940c8 100644 --- a/src/prc1/type.c +++ b/src/prc1/type.c @@ -71,6 +71,7 @@ void type_free(struct type *t) break; case T: + case OP: case ARY: case PTR: case BUILTIN: @@ -144,6 +145,7 @@ char *type_name(const struct type *t) case C: case P: case T: + case OP: n = append(t->tk->s, &ret); break; } -- cgit v1.2.3