shithub: scc

Download patch

ref: 6e980d9048da9c1aaaafa68ee7835ad83423a07a
parent: 687959e5e85e73b4fbdd9871eea90d5b56c3f577
author: Roberto E. Vargas Caballero <[email protected]>
date: Mon Jan 18 09:11:21 EST 2016

Return correct node iin initlist()

This function must generate a node, because one initializator
can be the expression used to initialize a field of another
aggregate.

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -17,6 +17,7 @@
 typedef struct caselist Caselist;
 typedef struct node Node;
 typedef struct input Input;
+typedef struct init Init;
 
 struct limits {
 	union {
@@ -61,6 +62,14 @@
 	} n;
 };
 
+struct designator;
+
+struct init {
+	Type *type;
+	TUINT pos;
+	struct designator *head;
+};
+
 struct symbol {
 	char *name;
 	Type *type;
@@ -75,6 +84,7 @@
 		TFLOAT f;
 		char *s;
 		unsigned char token;
+		Init *init;
 		Symbol **pars;
 	} u;
 	struct symbol *next;
--- a/cc1/init.c
+++ b/cc1/init.c
@@ -6,7 +6,6 @@
 #include "../inc/sizes.h"
 #include "cc1.h"
 
-typedef struct inititlizer Init;
 
 struct designator {
 	TINT pos;
@@ -14,11 +13,6 @@
 	struct designator *next;
 };
 
-struct inititlizer {
-	Type *type;
-	TUINT curpos;
-	struct designator *head;
-};
 
 static TINT
 arydesig(Init *ip)
@@ -79,7 +73,7 @@
 	default:  return ip;
 	}
 
-	ip->curpos  = (*fun)(ip);
+	ip->pos  = (*fun)(ip);
 	expect('=');
 	return ip;
 }
@@ -87,32 +81,34 @@
 static Node *
 initlist(Symbol *sym, Type *tp)
 {
-	struct inititlizer *ip;
+	Init *ip;
+	Symbol *nsym;
 	struct designator *dp;
 	int toomany = 0;
-	TINT n;
 	Type *newtp;
 
 	ip = xmalloc(sizeof(*ip));
 	ip->head = NULL;
+	ip->pos = 0;
+	ip->type = tp;
+
 	if (accept('}'))
-		return NULL;
+		goto end_of_initializer;
 
-	for (ip->curpos = 0; ; ++ip->curpos) {
+	for (ip->pos = 1; ; ++ip->pos) {
 		designation(ip);
 		switch (tp->op) {
 		case ARY:
 			newtp = tp->type;
-			if (!tp->defined || n < tp->n.elem)
+			if (!tp->defined || ip->pos < tp->n.elem)
 				break;
 			if (!toomany)
 				warn("excess elements in array initializer");
 			toomany = 1;
-			sym = NULL;
 			break;
 		case STRUCT:
-			if (n < tp->n.elem) {
-				sym = tp->p.fields[n];
+			if (ip->pos < tp->n.elem) {
+				sym = tp->p.fields[ip->pos];
 				newtp = sym->type;
 				break;
 			}
@@ -119,21 +115,19 @@
 			if (!toomany)
 				warn("excess elements in struct initializer");
 			toomany = 1;
-			sym = NULL;
 			break;
 		default:
 			newtp = tp;
 			warn("braces around scalar initializer");
-			if (n <= 0)
+			if (ip->pos <= 0)
 				break;
 			if (!toomany)
 				warn("excess elements in scalar initializer");
 			toomany = 1;
-			sym = NULL;
 			break;
 		}
 		dp = ip->head;
-		dp->pos = ip->curpos;
+		dp->pos = ip->pos;
 		/* TODO: pass the correct parameters to initlist */
 		dp->expr = (accept('{')) ? initlist(sym, tp) : assign(NULL);
 
@@ -142,11 +136,14 @@
 	}
 	expect('}');
 
+end_of_initializer:
 	if (tp->op == ARY && !tp->defined) {
-		tp->n.elem = n + 1;
+		tp->n.elem = ip->pos;
 		tp->defined = 1;
 	}
-	return NULL;
+	nsym = newsym(NS_IDEN);
+	nsym->u.init = ip;
+	return constnode(nsym);
 }
 
 void