From 7dc1a0640a520710a5ecb0c2dc3b63523340423d Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Mon, 10 Jun 2019 07:44:23 +0900 Subject: [PATCH] Delay creating actual enum symbols In order to keep enumerator type and enum type the same, the values need to have their type set after the enum type is finalized, and then the appropriate symbols created in the parent scope. This fixes the infinite recursion when assigning an enum value to its own type. --- tools/qfcc/source/struct.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/tools/qfcc/source/struct.c b/tools/qfcc/source/struct.c index ddf30f8ea..d3f014111 100644 --- a/tools/qfcc/source/struct.c +++ b/tools/qfcc/source/struct.c @@ -159,25 +159,38 @@ start_enum (symbol_t *sym) symbol_t * finish_enum (symbol_t *sym) { - sym->type = find_type (sym->type); + symbol_t *enum_sym; + symbol_t *name; + type_t *enum_type; + symtab_t *enum_tab; + + enum_type = sym->type = find_type (sym->type); + enum_tab = enum_type->t.symtab; + + for (name = enum_tab->symbols; name; name = name->next) { + name->type = sym->type; + + enum_sym = new_symbol_type (name->name, enum_type); + enum_sym->sy_type = sy_const; + enum_sym->s.value = name->s.value; + symtab_addsymbol (enum_tab->parent, enum_sym); + } return sym; } void add_enum (symbol_t *enm, symbol_t *name, expr_t *val) { - symbol_t *sym; type_t *enum_type = enm->type; - symtab_t *enum_tab; + symtab_t *enum_tab = enum_type->t.symtab; int value; - if (name->table == current_symtab) + if (name->table == current_symtab || name->table == enum_tab) error (0, "%s redefined", name->name); if (name->table) name = new_symbol (name->name); name->sy_type = sy_const; name->type = enum_type; - enum_tab = enum_type->t.symtab; value = 0; if (enum_tab->symbols) value = ((symbol_t *)(enum_tab->symtail))->s.value->v.integer_val + 1; @@ -191,10 +204,6 @@ add_enum (symbol_t *enm, symbol_t *name, expr_t *val) } name->s.value = new_integer_val (value); symtab_addsymbol (enum_tab, name); - sym = new_symbol_type (name->name, name->type); - sym->sy_type = sy_const; - sym->s.value = name->s.value; - symtab_addsymbol (enum_tab->parent, sym); } int