scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit c3b98d1587e0da7811403997cabbaa5a425b1d7a
parent 5c33e4a03fa4f581ffdfce4d5b294071611257f1
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue,  5 Nov 2013 07:42:44 +0100

Fix struct type initialzation

aggregate() receives tp,a pointer to an automatic variable, so this
pointer cannot be used in a type struct, because the new type
will have a longer life than the function were was defined the
value pointed by tp. It is necessary allocate a new type and
assign it later.

Diffstat:
decl.c | 15++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/decl.c b/decl.c @@ -71,8 +71,15 @@ aggregate(register struct ctype *tp) struct symbol *sym = NULL; if (yytoken == IDEN) { + register struct ctype *aux; + sym = lookup(yytext, NS_TAG); - sym->ctype = tp; + if (aux = sym->ctype) { + if (!aux->forward) + error("struct/union already defined"); + delctype(aux); + } + sym->ctype = xmalloc(sizeof(*tp)); next(); } if (nr_tags == NS_TAG + NR_MAXSTRUCTS) @@ -126,14 +133,14 @@ fielddcl(unsigned char ns) static void structdcl(register struct ctype *tp) { + struct symbol *sym; + aggregate(tp); if (nested_tags == NR_STRUCT_LEVEL) error("too much nested structs/unions"); if (!accept('{')) return; - if (!tp->forward) - error("struct/union already defined"); ++nested_tags; do @@ -142,6 +149,8 @@ structdcl(register struct ctype *tp) --nested_tags; tp->forward = 0; + if (sym = tp->sym) + *sym->ctype = *tp; } static void