ref: 1cdbbd148a18c528591f343bf5453c872911913e
parent: c5b90c26871eee25138cfbdb503a293ba47568f3
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Dec 14 08:41:37 EST 2016
[cc1] Create deftype() This function does all the steps needed when a new type is defined, instead of copying the same all the time.
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -349,6 +349,7 @@
extern void unexpected(void);
extern void errorp(char *fmt, ...);
extern void cpperror(char *fmt, ...);
+extern Type *deftype(Type *tp);
/* types.c */
extern int eqtype(Type *tp1, Type *tp2, int eqflag);
--- a/cc1/decl.c
+++ b/cc1/decl.c
@@ -521,7 +521,6 @@
if (tp->prop & TDEFINED && sym->ctx == curctx)
error("redefinition of struct/union '%s'", sym->name);
- tp->prop |= TDEFINED;
if (nested == NR_STRUCT_LEVEL)
error("too many levels of nested structure or union definitions");
@@ -532,8 +531,7 @@
}
--nested;
- typesize(tp);
- emit(OTYP, tp);
+ deftype(tp);
namespace = ns;
expect('}');
return tp;
@@ -555,9 +553,7 @@
goto restore_name;
if (tp->prop & TDEFINED)
errorp("redefinition of enumeration '%s'", tagsym->name);
- tp->prop |= TDEFINED;
- emit(OTYP, tp);
- typesize(tp);
+ deftype(tp);
namespace = NS_IDEN;
/* TODO: check incorrect values in val */
--- a/cc1/init.c
+++ b/cc1/init.c
@@ -120,9 +120,8 @@
}
len = sym->type->n.elem-1;
if (!(tp->prop & TDEFINED)) {
- tp->prop |= TDEFINED;
tp->n.elem = len+1;
- typesize(tp);
+ deftype(tp);
} else if (tp->n.elem < len) {
warn("initializer-string for array of chars is too long");
}
--- a/cc1/types.c
+++ b/cc1/types.c
@@ -242,6 +242,15 @@
}
}
+Type *
+deftype(Type *tp)
+{
+ tp->prop |= TDEFINED;
+ typesize(tp);
+ emit(OTYP, tp);
+ return tp;
+}
+
static Type *
newtype(Type *base)
{