shithub: scc

Download patch

ref: 4e770c4791211178f97399cb864a99e319eccbdf
parent: 529349a0c7457739a1d78497a0ccc7e7d7b20dd7
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Oct 31 04:20:41 EDT 2013

Use a stack for symbol definition

We have nested declarations due to the functions and to the structs,
so cursym can change the value before of the end of the declaration,
and it can cause a lot of problems (we are assigning the type
to other symbol). The correct solution is using a stack.

--- a/decl.c
+++ b/decl.c
@@ -9,7 +9,13 @@
 #include "symbol.h"
 
 char parser_out_home;
-
+/*
+ * Number of nested declarations:
+ * Number of nested struct declarations
+ * +1 for the function declaration
+ * +1 for the field declaration
+ */
+static struct symbol *symstack[NR_STRUCT_LEVEL + 1 + 1], **symstackp = symstack;
 static struct symbol *cursym;
 static unsigned char nr_tags = NS_TAG;
 static unsigned char nested_tags;
@@ -23,7 +29,8 @@
 		declarator(tp, ns);
 		expect(')');
 	} else if (yytoken == IDEN) {
-		cursym = lookup(yytext, ns);
+		/* we can't overflow due to the check in structdcl */
+		*symstackp++ = cursym = lookup(yytext, ns);
 		if (!cursym->ctype)
 			cursym->ctx = curctx;
 		else if (cursym->ctx == curctx)
@@ -308,6 +315,7 @@
 		fun = tp->type == FTN && yytoken == '{';
 		aux = fun ? function(cursym) : initializer(tp);
 		addstmt(&c, node(ODEF, np, aux));
+		cursym = *--symstackp;
 	} while (!fun && accept(','));
 
 	if (!fun)