scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit e6e67bde1898d8478134a77583129e1f28e2ca12
parent 43fcafa0c1307d5b3f9fbf43ffbbfb256723f8e8
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed, 14 Dec 2016 13:41:37 +0100

[cc1] Create deftype()

This function does all the steps needed when a new type is defined,
instead of copying the same all the time.

Diffstat:
Mcc1/cc1.h | 1+
Mcc1/decl.c | 8++------
Mcc1/init.c | 3+--
Mcc1/types.c | 9+++++++++
4 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -349,6 +349,7 @@ extern void warn(char *fmt, ...); 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); diff --git a/cc1/decl.c b/cc1/decl.c @@ -521,7 +521,6 @@ structdcl(void) 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 @@ structdcl(void) } --nested; - typesize(tp); - emit(OTYP, tp); + deftype(tp); namespace = ns; expect('}'); return tp; @@ -555,9 +553,7 @@ enumdcl(void) 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 */ diff --git a/cc1/init.c b/cc1/init.c @@ -120,9 +120,8 @@ initialize(Type *tp) } 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"); } diff --git a/cc1/types.c b/cc1/types.c @@ -242,6 +242,15 @@ typesize(Type *tp) } } +Type * +deftype(Type *tp) +{ + tp->prop |= TDEFINED; + typesize(tp); + emit(OTYP, tp); + return tp; +} + static Type * newtype(Type *base) {