shithub: scc

Download patch

ref: 28af012e9746a4adacc4e2909d0cfb5e9d8e94e9
parent: 46eaaf7b32ddb0e7ce17c03db45d6c6ef98bc0f0
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Oct 30 04:13:18 EDT 2013

Convert mktype to static function

ctype is a more general interface to the creation of types,
so it is better have only one interface than two.

--- a/decl.c
+++ b/decl.c
@@ -83,8 +83,7 @@
 	register struct ctype *tp, *base;
 
 	if (!(base = specifier())) {
-		base = newctype();
-		base->type = INT;
+		base = ctype(NULL, INT);
 		warn(options.implicit,
 		     "data definition has no type or storage class");
 	}
--- a/symbol.h
+++ b/symbol.h
@@ -73,7 +73,6 @@
 extern struct symbol *lookup(const char *s, signed char ns);
 extern void insert(struct symbol *sym, unsigned char ctx);
 extern struct ctype *storage(struct ctype *tp, unsigned char mod);
-extern struct ctype *newctype(void);
 extern void delctype(struct ctype *tp);
 extern unsigned char hash(register const char *s);
 
--- a/types.c
+++ b/types.c
@@ -1,6 +1,7 @@
 
 #include <assert.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "sizes.h"
 #include "cc.h"
@@ -11,14 +12,11 @@
 static unsigned char stack[NR_DECLARATORS];
 static unsigned char *stackp = stack;
 
-
-struct ctype *
-newctype(void)
+void
+initctype(register struct ctype *tp)
 {
-	register struct ctype *tp = xcalloc(sizeof(*tp), 1);
-
+	memset(tp, 0, sizeof(*tp));
 	tp->forward = 1;
-	return tp;
 }
 
 void
@@ -43,7 +41,8 @@
 	case PTR: case FTN: {
 		register struct ctype *aux = tp;
 
-		tp = newctype();
+		tp = xmalloc(sizeof(*tp));
+		initctype(tp);
 		tp->type = op;
 		tp->base = aux;
 		tp->len = len;
@@ -88,8 +87,10 @@
 {
 	register unsigned char type;
 
-	if (!tp)
-		tp = newctype();
+	if (!tp) {
+		tp = xmalloc(sizeof(*tp));
+		initctype(tp);
+	}
 
 	type = tp->type;
 	switch (tok) {
@@ -191,8 +192,10 @@
 {
 	extern unsigned char curctx;
 
-	if (!tp)
-		tp = newctype();
+	if (!tp) {
+		tp = xmalloc(sizeof(*tp));
+		initctype(tp);
+	}
 	switch (mod) {
 	case TYPEDEF:
 		if (tp->c_typedef)
--