From fc08e91e04cff34f5e3c6510223d79fbf0826b19 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Tue, 5 Jul 2011 21:12:59 -0400 Subject: [PATCH] Make an explicit null declarator type. Now that we have a child pointers in every declarator, this is way cleaner than abusing the identifier type. --- src/cdecl.h | 1 + src/explain.c | 10 ++++++---- src/parse.y | 7 ++++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/cdecl.h b/src/cdecl.h index 807ad6a..883a4e4 100644 --- a/src/cdecl.h +++ b/src/cdecl.h @@ -41,6 +41,7 @@ enum { /* Declarator types. */ enum { + CDECL_DECL_NULL, CDECL_DECL_IDENT, CDECL_DECL_POINTER, CDECL_DECL_ARRAY, diff --git a/src/explain.c b/src/explain.c index 60e880b..b0e9a84 100644 --- a/src/explain.c +++ b/src/explain.c @@ -164,11 +164,13 @@ static size_t explain_prologue(char *buf, size_t n, struct cdecl_declarator *d) { while (d) { - if (d->type == CDECL_DECL_IDENT) { - if (!d->u.ident) - return snprintf(buf, n, "type"); + switch (d->type) { + case CDECL_DECL_NULL: + return snprintf(buf, n, "type"); + case CDECL_DECL_IDENT: return snprintf(buf, n, "declare %s as", d->u.ident); } + d = d->child; } } @@ -211,7 +213,7 @@ explain_declarators(char *buf, size_t n, struct cdecl_declarator *d) { size_t ret = 0, rc; - if (d->type == CDECL_DECL_IDENT) + if (d->type == CDECL_DECL_IDENT || d->type == CDECL_DECL_NULL) return 0; rc = explain_declarators(buf, n, d->child); diff --git a/src/parse.y b/src/parse.y index b21a1d4..7d4c3dc 100644 --- a/src/parse.y +++ b/src/parse.y @@ -79,6 +79,8 @@ static void free_declarator(struct cdecl_declarator *x) while (x) { p = x->next; switch (x->type) { + case CDECL_DECL_NULL: + break; case CDECL_DECL_IDENT: free(x->u.ident); break; @@ -289,12 +291,11 @@ pointer: T_ASTERISK qualifiers direct_declarator { .child = $3); } -declarator: direct_declarator | pointer; +declarator: direct_declarator | pointer direct_declarator: { ALLOC_STRUCT($$, struct cdecl_declarator, - .type = CDECL_DECL_IDENT, - .u.ident = NULL); + .type = CDECL_DECL_NULL); } | T_IDENT { ALLOC_STRUCT($$, struct cdecl_declarator, .type = CDECL_DECL_IDENT, -- 2.43.2