shithub: scc

Download patch

ref: 511baa28dc19e114dc4ddb08c865e68cbfc8a624
parent: 9f2aff7690c4222d1ad28eef68b042f9e6e61c27
author: Roberto E. Vargas Caballero <[email protected]>
date: Sat Nov 15 07:40:27 EST 2014

Unify rank and nelemt

These fields are not going to be used at the same time ever, so they
can be joined in a union. In the same patch we remove the union of pars,
because an union of only one element is a bit silly.

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -46,11 +46,11 @@
 	bool sign : 1;        /* sign type */
 	struct ctype *type;   /* base type */
 	struct ctype *next;   /* next element in the hash */
-	short nelem;          /* number of elements in ary/ftn/strct/union */
-	union typeval {
-		unsigned char rank;   /* convertion rank */
-		void *pars;           /* parameters */
-	} u;
+	union {
+		unsigned char rank;  /* convertion rank */
+		short elem;          /* number of type parameters */
+	} n;
+	void *pars;           /* type parameters */
 };
 
 
--- a/cc1/decl.c
+++ b/cc1/decl.c
@@ -258,7 +258,7 @@
 	register char *s, *t;
 
 	s = sym->name;
-	for (q = p = tp->u.pars; p; q = p, p = p->next) {
+	for (q = p = tp->pars; p; q = p, p = p->next) {
 		t = p->name;
 		if (*s == *t && !strcmp(s, t))
 			error("duplicated fields '%s' and '%s'", s, t);
@@ -273,7 +273,7 @@
 	p->id = sym->id;
 	p->type = sym->type;
 	if (!q)
-		tp->u.pars= p;
+		tp->pars= p;
 	else
 		q->next = p;
 
--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -32,7 +32,7 @@
 	if (options.npromote)
 		return np;
 	tp = np->type;
-	r = tp->u.rank;
+	r = tp->n.rank;
 	if  (r > RANK_UINT || tp == inttype || tp == uinttype)
 		return np;
 	return castcode(np, (r == RANK_UINT) ? uinttype : inttype);
@@ -51,7 +51,7 @@
 	tp1 = np1->type;
 	tp2 = np2->type;
 	if (tp1 != tp2) {
-		if ((n = tp1->u.rank - tp2->u.rank) > 0)
+		if ((n = tp1->n.rank - tp2->n.rank) > 0)
 			np2 = castcode(np2, tp1);
 		else if (n < 0)
 			np1 = castcode(np1, tp2);
@@ -298,7 +298,7 @@
 		unexpected();
 	switch (np->typeop) {
 	case STRUCT: case UNION:
-		for (fp = np->type->u.pars; fp; fp = fp->next) {
+		for (fp = np->type->pars; fp; fp = fp->next) {
 			if (!strcmp(fp->name, yytext)) {
 				next();
 				return fieldcode(np, fp);
--- a/cc1/types.c
+++ b/cc1/types.c
@@ -23,13 +23,13 @@
 		.op = INT,
 		.letter = L_BOOL,
 		.defined = 1,
-		.u.rank = RANK_BOOL
+		.n.rank = RANK_BOOL
 	},
 	*schartype = &(Type) {
 		.op = INT,
 		.letter = L_SCHAR,
 		.defined = 1,
-		.u.rank = RANK_SCHAR
+		.n.rank = RANK_SCHAR
 	},
 	*uchartype = &(Type) {
 		.op = INT,
@@ -36,7 +36,7 @@
 		.letter = L_UCHAR,
 		.sign = 1,
 		.defined = 1,
-		.u.rank = RANK_UCHAR
+		.n.rank = RANK_UCHAR
 	},
 	*chartype = &(Type) {
 		.op = INT,
@@ -43,19 +43,19 @@
 		.letter = L_CHAR,
 		.sign = 1,
 		.defined = 1,
-		.u.rank = RANK_CHAR
+		.n.rank = RANK_CHAR
 	},
 	*ushortype = &(Type) {
 		.op = INT,
 		.letter = L_USHORT,
 		.defined = 1,
-		.u.rank = RANK_USHORT
+		.n.rank = RANK_USHORT
 	},
 	*shortype = &(Type) {
 		.op = INT,
 		.letter = L_SHORT,
 		.defined = 1,
-		.u.rank = RANK_SHORT
+		.n.rank = RANK_SHORT
 	},
 	*uinttype = &(Type) {
 		.op = INT,
@@ -62,19 +62,19 @@
 		.letter = L_UINT,
 		.sign = 1,
 		.defined = 1,
-		.u.rank = RANK_UINT
+		.n.rank = RANK_UINT
 	},
 	*inttype = &(Type) {
 		.op = INT,
 		.letter = L_INT,
 		.defined = 1,
-		.u.rank = RANK_INT
+		.n.rank = RANK_INT
 	},
 	*longtype = &(Type) {
 		.op = INT,
 		.letter = L_LONG,
 		.defined = 1,
-		.u.rank = RANK_LONG
+		.n.rank = RANK_LONG
 	},
 	*ulongtype = &(Type) {
 		.op = INT,
@@ -81,7 +81,7 @@
 		.letter = L_ULONG,
 		.sign = 1,
 		.defined = 1,
-		.u.rank = RANK_ULONG
+		.n.rank = RANK_ULONG
 	},
 	*ullongtype = &(Type) {
 		.op = INT,
@@ -88,31 +88,31 @@
 		.letter = L_ULLONG,
 		.sign = 1,
 		.defined = 1,
-		.u.rank = RANK_ULLONG
+		.n.rank = RANK_ULLONG
 	},
 	*llongtype = &(Type) {
 		.op = INT,
 		.letter = L_LLONG,
 		.defined = 1,
-		.u.rank = RANK_LLONG
+		.n.rank = RANK_LLONG
 	},
 	*floattype = &(Type) {
 		.op = FLOAT,
 		.letter = L_FLOAT,
 		.defined = 1,
-		.u.rank = RANK_FLOAT
+		.n.rank = RANK_FLOAT
 	},
 	*doubletype = &(Type) {
 		.op = FLOAT,
 		.letter = L_DOUBLE,
 		.defined = 1,
-		.u.rank = RANK_DOUBLE
+		.n.rank = RANK_DOUBLE
 	},
 	*ldoubletype = &(Type) {
 		.op = FLOAT,
 		.letter = L_LDOUBLE,
 		.defined = 1,
-		.u.rank = RANK_LDOUBLE
+		.n.rank = RANK_LDOUBLE
 	};
 
 Type *
@@ -204,8 +204,8 @@
 	type.op = op;
 	type.sign = 0;
 	type.letter = letters[op];
-	type.nelem = nelem;
-	type.u.pars = data;
+	type.pars = data;
+	type.n.elem = nelem;
 
 	if (op == ARY && nelem == 0 || op == STRUCT || op == UNION)
 		type.defined = 0;
@@ -233,7 +233,7 @@
 
 	if (tp1 == tp2)
 		return 1;
-	if (tp1->op != tp2->op || tp1->nelem != tp2->nelem)
+	if (tp1->op != tp2->op || tp1->n.elem != tp2->n.elem)
 		return 0;
 	switch (tp1->op) {
 	case PTR:
@@ -240,8 +240,8 @@
 		return eqtype(tp1->type, tp2->type);
 	/* TODO: use the same struct for function parameters and fields */
 	case FTN:
-		p1 = tp1->u.pars, p2 = tp2->u.pars;
-		for (n = tp1->nelem; n != 0; --n) {
+		p1 = tp1->pars, p2 = tp2->pars;
+		for (n = tp1->n.elem; n != 0; --n) {
 			if (!eqtype(*p1++, *p2++))
 				return 0;
 		}
@@ -248,7 +248,7 @@
 		return 1;
 	case UNION:
 	case STRUCT: {
-		Field *fp1 = tp1->u.pars, *fp2 = tp2->u.pars;
+		Field *fp1 = tp1->pars, *fp2 = tp2->pars;
 
 		while (fp1 && fp2) {
 			if (!eqtype(fp1->type, fp2->type))