X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/6a0655d9cddec57a13a7fd813aa1dc81bdbcb38f..22b081deb7ab1d0ec7a279e5213fa7fdc2c85207:/src/symtab.c diff --git a/src/symtab.c b/src/symtab.c index 14d107c9..124da372 100644 --- a/src/symtab.c +++ b/src/symtab.c @@ -33,6 +33,7 @@ `-------------------------------------------------------------------*/ static symbol **symbols_sorted = NULL; +static symbol **semantic_types_sorted = NULL; /*------------------------. | Distinguished symbols. | @@ -118,12 +119,14 @@ code_props_type_string (code_props_type kind) `----------------------------------------*/ static semantic_type * -semantic_type_new (uniqstr tag) +semantic_type_new (uniqstr tag, const location *loc) { semantic_type *res = xmalloc (sizeof *res); uniqstr_assert (tag); res->tag = tag; + if (loc) + res->location = *loc; for (int i = 0; i < CODE_PROPS_SIZE; ++i) code_props_none_init (&res->props[i]); @@ -283,7 +286,7 @@ symbol_code_props_get (symbol const *sym, if (sym->type_name) { code_props const *code = - &semantic_type_get (sym->type_name)->props[kind]; + &semantic_type_get (sym->type_name, NULL)->props[kind]; if (code->code) return code; } @@ -416,6 +419,44 @@ symbol_check_defined (symbol *sym) sym->number = nvars++; } + for (int i = 0; i < 2; ++i) + if (sym->props[i].kind == CODE_PROPS_NONE && sym->type_name) + { + semantic_type *sem_type = semantic_type_get (sym->type_name, NULL); + if (sem_type + && sem_type->props[i].kind != CODE_PROPS_NONE) + sem_type->props[i].is_used = true; + } + + /* Set the semantic type status associated to the current symbol to + 'declared' so that we could check semantic types unnecessary uses. */ + if (sym->type_name) + { + semantic_type *sem_type = semantic_type_get (sym->type_name, NULL); + if (sem_type) + sem_type->status = declared; + } + + return true; +} + +static inline bool +semantic_type_check_defined (semantic_type *sem_type) +{ + if (sem_type->status == declared) + { + for (int i = 0; i < 2; ++i) + if (sem_type->props[i].kind != CODE_PROPS_NONE + && ! sem_type->props[i].is_used) + warn_at (sem_type->location, + _("useless %s for type <%s>"), + code_props_type_string (i), sem_type->tag); + } + else + warn_at (sem_type->location, + _("type <%s> is used, but is not associated to any symbol"), + sem_type->tag); + return true; } @@ -425,6 +466,13 @@ symbol_check_defined_processor (void *sym, void *null ATTRIBUTE_UNUSED) return symbol_check_defined (sym); } +static bool +semantic_type_check_defined_processor (void *sem_type, + void *null ATTRIBUTE_UNUSED) +{ + return semantic_type_check_defined (sem_type); +} + void symbol_make_alias (symbol *sym, symbol *str, location loc) @@ -690,7 +738,7 @@ symbol_from_uniqstr (const uniqstr key, location loc) `-----------------------------------------------------------------------*/ semantic_type * -semantic_type_from_uniqstr (const uniqstr key) +semantic_type_from_uniqstr (const uniqstr key, const location *loc) { semantic_type probe; semantic_type *entry; @@ -701,7 +749,7 @@ semantic_type_from_uniqstr (const uniqstr key) if (!entry) { /* First insertion in the hash. */ - entry = semantic_type_new (key); + entry = semantic_type_new (key, loc); if (!hash_insert (semantic_type_table, entry)) xalloc_die (); } @@ -727,9 +775,9 @@ symbol_get (const char *key, location loc) `-----------------------------------------------------------------------*/ semantic_type * -semantic_type_get (const char *key) +semantic_type_get (const char *key, const location *loc) { - return semantic_type_from_uniqstr (uniqstr_new (key)); + return semantic_type_from_uniqstr (uniqstr_new (key), loc); } @@ -819,6 +867,8 @@ symbols_check_defined (void) { symbols_do (symbol_check_defined_processor, NULL, symbol_table, symbols_sorted); + symbols_do (semantic_type_check_defined_processor, NULL, + semantic_type_table, semantic_types_sorted); } /*------------------------------------------------------------------. @@ -959,4 +1009,3 @@ default_tagless_code_props_set (code_props_type kind, code_props const *code) } default_tagless_code_props[kind] = *code; } -