shithub: scc

Download patch

ref: c3b98d1587e0da7811403997cabbaa5a425b1d7a
parent: 5c33e4a03fa4f581ffdfce4d5b294071611257f1
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue Nov 5 02:42:44 EST 2013

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.

--- a/decl.c
+++ b/decl.c
@@ -71,8 +71,15 @@
 	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,6 +133,8 @@
 static void
 structdcl(register struct ctype *tp)
 {
+	struct symbol *sym;
+
 	aggregate(tp);
 	if (nested_tags == NR_STRUCT_LEVEL)
 		error("too much nested structs/unions");
@@ -132,8 +141,6 @@
 
 	if (!accept('{'))
 		return;
-	if (!tp->forward)
-		error("struct/union already defined");
 
 	++nested_tags;
 	do
@@ -142,6 +149,8 @@
 	--nested_tags;
 
 	tp->forward = 0;
+	if (sym = tp->sym)
+		*sym->ctype = *tp;
 }
 
 static void
--