From 46549a1187f3c13779ebdc2d49ef81e47e4a0c93 Mon Sep 17 00:00:00 2001 From: Walter Bright Date: Mon, 28 Jan 2013 01:26:08 -0800 Subject: [PATCH] refactor type_enum --- src/backend/type.c | 25 +++++++++++++++++++++++++ src/backend/type.h | 1 + src/toctype.c | 14 +------------- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/backend/type.c b/src/backend/type.c index 6b8ca8ea759b..9d049b84a4ff 100644 --- a/src/backend/type.c +++ b/src/backend/type.c @@ -445,6 +445,31 @@ type *type_function(tym_t tyf, type **ptypes, size_t nparams, bool variadic, typ return t; } +/*************************************** + * Create an enum type. + * Input: + * name name of enum + * tbase "base" type of enum + * Returns: + * Tcount already incremented + */ +type *type_enum(const char *name, type *tbase) +{ + Symbol *s = symbol_calloc(name); + s->Sclass = SCenum; + s->Senum = (enum_t *) MEM_PH_CALLOC(sizeof(enum_t)); + s->Senum->SEflags |= SENforward; // forward reference + slist_add(s); + + type *t = type_allocn(TYenum, tbase); + t->Ttag = (Classsym *)s; // enum tag name + t->Tcount++; + s->Stype = t; + slist_add(s); + t->Tcount++; + return t; +} + /***************************** * Free up data type. */ diff --git a/src/backend/type.h b/src/backend/type.h index 0ba0010d2fc6..596665fa2b7d 100644 --- a/src/backend/type.h +++ b/src/backend/type.h @@ -210,6 +210,7 @@ type *type_static_array(unsigned long long dim, type *tnext); type *type_assoc_array(type *tkey, type *tvalue); type *type_delegate(type *tnext); type *type_function(tym_t tyf, type **ptypes, size_t nparams, bool variadic, type *tret); +type *type_enum(const char *name, type *tbase); #endif diff --git a/src/toctype.c b/src/toctype.c index 9842acc7e37a..d3cb6f5b06ee 100644 --- a/src/toctype.c +++ b/src/toctype.c @@ -265,19 +265,7 @@ type *TypeEnum::toCtype() } else if (sym->memtype->toBasetype()->ty == Tint32) { - Symbol *s = symbol_calloc(sym->toPrettyChars()); - s->Sclass = SCenum; - s->Senum = (enum_t *) MEM_PH_CALLOC(sizeof(enum_t)); - s->Senum->SEflags |= SENforward; // forward reference - slist_add(s); - - t = type_alloc(TYenum); - t->Ttag = (Classsym *)s; // enum tag name - t->Tcount++; - t->Tnext = sym->memtype->toCtype(); - t->Tnext->Tcount++; - s->Stype = t; - slist_add(s); + t = type_enum(sym->toPrettyChars(), sym->memtype->toCtype()); tm->ctype = t; ctype = t; }