shithub: scc

Download patch

ref: f49504fa77710aaef3b063275b262ec8b2db73ac
parent: 0dc8403f3a7a439033d22d7f181d4798f9f1dc7a
author: Roberto E. Vargas Caballero <[email protected]>
date: Tue Oct 29 07:23:45 EDT 2013

Use a different namespace for each struct/union

The code had a bug and it was using the same namespace for all
the fields of all the structs, so it was causing problems. This
patch fixes this problem, and also remove the non used structbuf
and structp (the value of the namespace is saved in the stack
due to the recursion).

--- a/decl.c
+++ b/decl.c
@@ -12,7 +12,7 @@
 
 static struct symbol *cursym;
 static unsigned char nr_structs = NS_STRUCT;
-static unsigned char structbuf[NR_STRUCT_LEVEL], *structp = &structbuf[0];
+static unsigned char nested_struct;
 
 static void declarator(struct ctype *tp, unsigned char ns);
 
@@ -59,7 +59,7 @@
 }
 
 static void
-newstruct(register struct ctype *tp)
+aggregate(register struct ctype *tp)
 {
 	struct symbol *sym = NULL;
 
@@ -68,11 +68,9 @@
 		sym->ctype = tp;
 		next();
 	}
-	if (nr_structs == NR_MAXSTRUCTS)
+	if (nr_structs == NS_STRUCT + NR_MAXSTRUCTS)
 		error("too much structs/unions/enum defined");
-	if (structp == &structbuf[NR_STRUCT_LEVEL])
-		error("too much nested structs/unions");
-	tp->ns = *structp++ = nr_structs;
+	tp->ns = ++nr_structs;
 	tp->forward = 1;
 	tp->sym = sym;
 }
@@ -119,8 +117,11 @@
 static struct ctype *
 structdcl(register struct ctype *tp)
 {
-	newstruct(tp);
+	aggregate(tp);
+	if (nested_struct == NR_STRUCT_LEVEL)
+		error("too much nested structs/unions");
 
+	++nested_struct;
 	if (!accept('{'))
 		return tp;
 	if (!tp->forward)
@@ -129,6 +130,7 @@
 	do
 		fielddcl(tp->ns);
 	while (!accept('}'));
+	--nested_struct;
 
 	tp->forward = 0;
 	return tp;
@@ -139,7 +141,7 @@
 {
 	short val = 0;
 
-	newstruct(base);
+	aggregate(base);
 	if (!accept('{'))
 		return base;