]> git.draconx.ca Git - cdecl99.git/blobdiff - src/explain.c
libcdecl: Use a structure for dst/dstlen in output routines.
[cdecl99.git] / src / explain.c
index 60e880b0586b2e4c7b47ca5d9f48d6b3ee07b5eb..e6b4f16abd4410b39b9b8914f0d5fcc716896e0f 100644 (file)
+/*
+ *  Render C declarations as English.
+ *  Copyright © 2011, 2021, 2023 Nick Bowler
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
 #include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
+#include <inttypes.h>
 #include <assert.h>
 
 #include "cdecl.h"
-#include "typemap.h"
+#include "cdecl-internal.h"
 
-/* declare [ident] as [storage] [function specs] [other crap] [qualifiers] [type specs] */
+#define PRE_SPECS (CDECL_SPEC_FUNC|CDECL_SPEC_STOR)
+#define POST_SPECS (CDECL_SPEC_QUAL|CDECL_SPEC_TYPE)
 
-static size_t
-voutput(char *buf, size_t n, size_t off, const char *fmt, va_list ap)
-{
-       if (off >= n)
-               return vsnprintf(NULL, 0, fmt, ap);
-       return vsnprintf(buf+off, n-off, fmt, ap);
-}
-
-static size_t output(char *buf, size_t n, size_t off, const char *fmt, ...)
+static size_t explain_specs(struct output_state *dst, struct cdecl_declspec *s, unsigned mask)
 {
-       va_list ap;
        size_t ret;
 
-       va_start(ap, fmt);
-       ret = voutput(buf, n, off, fmt, ap);
-       va_end(ap);
-
-       return ret;
+       ret = cdecl__emit_specs(dst, s, mask);
+       return ret + cdecl__emit(dst, " " + !ret);
 }
 
-static size_t advance_(char **buf, size_t *n, size_t amount)
+static size_t
+explain_specs_post(struct output_state *dst, struct cdecl_declspec *s)
 {
-       if (amount >= *n) {
-               *n   = 0;
-               *buf = 0;
-       } else {
-               *buf += amount;
-               *n   -= amount;
-       }
-
-       return amount;
+       return cdecl__emit_specs(dst, s, POST_SPECS);
 }
 
-static size_t advance(char **buf, size_t *n, size_t amount)
+/*
+ * If declarator declares an identifier foo, then emit "foo as "; otherwise
+ * emit nothing.
+ */
+static size_t
+explain_name(struct output_state *dst, struct cdecl_declarator *d)
 {
-       size_t ret, rc;
+       size_t ret;
 
-       if (!amount)
+       while (d->child)
+               d = d->child;
+
+       if (d->type != CDECL_DECL_IDENT)
                return 0;
 
-       ret = advance_(buf, n, amount);
-       rc = snprintf(*buf, *n, " ");
-       return ret + advance_(buf, n, rc);
+       ret = cdecl__emit(dst, d->u.ident);
+       return ret + cdecl__emit(dst, " as ");
 }
 
+/*
+ * For a pointer declarator, emit "[QUAL ]pointer to ", where
+ * QUAL is the (possibly empty) list of qualifiers.
+ */
 static size_t
-explain_qualifiers(char *buf, size_t n, struct cdecl_declspec *s)
+explain_pointer(struct output_state *dst, struct cdecl_pointer *p)
 {
-       unsigned long qualmap = 0;
-       size_t ret = 0, rc = 0;
-
-       for (struct cdecl_declspec *c = s; c; c = c->next) {
-               if (cdecl_spec_kind(c) != CDECL_SPEC_QUAL)
-                       continue;
-               qualmap |= 1ul << (c->type & 0xff);
-       }
-
-       if (qualmap & (1ul << (CDECL_QUAL_RESTRICT & 0xff))) {
-               ret += advance(&buf, &n, rc);
-               rc = snprintf(buf, n, "restrict");
-       }
-       if (qualmap & (1ul << (CDECL_QUAL_VOLATILE & 0xff))) {
-               ret += advance(&buf, &n, rc);
-               rc = snprintf(buf, n, "volatile");
-       }
-       if (qualmap & (1ul << (CDECL_QUAL_CONST & 0xff))) {
-               ret += advance(&buf, &n, rc);
-               rc = snprintf(buf, n, "const");
-       }
+       size_t ret;
 
-       return ret + rc;
+       ret = explain_specs(dst, p->qualifiers, -1);
+       return ret + cdecl__emit(dst, "pointer to ");
 }
 
-/* Renders the type qualifiers and type specifiers in canonical form. */
+/*
+ * For an array declarator, emit "[variable-length ]array [X ]of ", where
+ * for a variable-length array X is the (possibly omitted) identifier name,
+ * and for normal arrays X is the (possibly omitted) length.
+ */
 static size_t
-explain_post_specs(char *buf, size_t n, struct cdecl_declspec *s)
+explain_array(struct output_state *dst, struct cdecl_array *a)
 {
-       const char *tag = NULL;
-       unsigned long typemap;
-       size_t ret = 0, rc;
-
-       typemap = cdecl__build_typemap(s);
-       if (typemap == -1)
-               return 0;
-
-       for (struct cdecl_declspec *c = s; c; c = c->next) {
-               if (cdecl_spec_kind(c) != CDECL_SPEC_TYPE)
-                       continue;
-
-               /* Valid C types have at most one identifier. */
-               if (c->ident)
-                       tag = c->ident;
-       }
+       size_t ret = 0, rc = 0;
 
-       rc = explain_qualifiers(buf, n, s);
-       ret += advance(&buf, &n, rc);
+       if (a->vla)
+               ret += cdecl__emit(dst, "variable-length ");
+       ret += cdecl__emit(dst, "array ");
 
-       rc = snprintf(buf, n, "%s", cdecl__explain_typemap(typemap));
-       if (tag) {
-               ret += advance(&buf, &n, rc);
-               rc = snprintf(buf, n, "%s", tag);
+       if (a->vla) {
+               rc = cdecl__emit(dst, a->vla);
+               ret += rc;
+       } else {
+               rc = snprintf(dst->dst, dst->dstlen, "%.0" PRIuMAX, a->length);
+               ret += cdecl__advance(dst, rc);
        }
 
-       return ret + rc;
+       return ret + cdecl__emit(dst, " of " + !rc);
 }
 
-static const char *explain_storage(unsigned spec)
-{
-       switch (spec) {
-       case CDECL_STOR_TYPEDEF:
-               return "typedef";
-       case CDECL_STOR_EXTERN:
-               return "extern";
-       case CDECL_STOR_STATIC:
-               return "static";
-       case CDECL_STOR_AUTO:
-               return "auto";
-       case CDECL_STOR_REGISTER:
-               return "register";
-       default:
-               assert(0);
-       }
-}
+static size_t
+explain_declarators(struct output_state *dst, struct cdecl_declarator *decl);
 
-/* Renders the storage-class and function specifiers in canonical form. */
-static size_t explain_pre_specs(char *buf, size_t n, struct cdecl_declspec *s)
+static size_t explain_decl(struct output_state *dst, struct cdecl *decl)
 {
-       unsigned long funcmap = 0;
-       const char *storage = NULL;
-       size_t ret = 0;
+       size_t ret;
 
-       for (struct cdecl_declspec *c = s; c; c = c->next) {
-               switch (cdecl_spec_kind(c)) {
-               case CDECL_SPEC_FUNC:
-                       funcmap |= 1ul << (c->type & 0xff);
-                       break;
-               case CDECL_SPEC_STOR:
-                       /* Valid C declarations have at most one
-                        * storage-class specifier. */
-                       storage = explain_storage(c->type);
-                       break;
-               }
-       }
+       ret  = explain_name(dst, decl->declarators);
+       ret += explain_specs(dst, decl->specifiers, PRE_SPECS);
+       ret += explain_declarators(dst, decl->declarators);
+       ret += explain_specs_post(dst, decl->specifiers);
 
-       if (storage)
-               ret += output(buf, n, ret, "%s", storage);
-       if (funcmap & (1ul << (CDECL_FUNC_INLINE & 0xff)))
-               ret += output(buf, n, ret, "%.*s%s", !!ret, "", "inline");
        return ret;
 }
 
-/* Renders the name of the thing being declared. */
+/*
+ * For a function declarator, emit "function [PARAM ]returning ", where
+ * PARAM is the (omitted in the case of an empty non-prototype identifier
+ * list) parameter or identifier lists enclosed in parentheses.
+ */
 static size_t
-explain_prologue(char *buf, size_t n, struct cdecl_declarator *d)
+explain_function(struct output_state *dst, struct cdecl_function *f)
 {
-       while (d) {
-               if (d->type == CDECL_DECL_IDENT) {
-                       if (!d->u.ident)
-                               return snprintf(buf, n, "type");
-                       return snprintf(buf, n, "declare %s as", d->u.ident);
-               }
-               d = d->child;
-       }
-}
-
-static size_t
-explain_pointer(char *buf, size_t n, struct cdecl_pointer *p)
-{
-       size_t ret = 0, rc;
+       size_t ret = 0;
 
-       rc = explain_qualifiers(buf, n, p->qualifiers);
-       ret += advance(&buf, &n, rc);
+       ret += cdecl__emit(dst, "function ");
 
-       return ret + snprintf(buf, n, "pointer to");
-}
+       if (f->parameters) {
+               struct cdecl *p;
 
-static size_t
-explain_array(char *buf, size_t n, struct cdecl_array *a)
-{
-       size_t ret = 0, rc = 0;
+               ret += cdecl__emit(dst, "(");
 
-       if (a->vla)
-               rc = snprintf(buf, n, "variable-length array");
-       else
-               rc = snprintf(buf, n, "array");
-       ret += advance(&buf, &n, rc);
+               for (p = f->parameters; p; p = p->next) {
+                       ret += explain_decl(dst, p);
+                       if (p->next)
+                               ret += cdecl__emit(dst, ", ");
+               }
 
-       if (a->vla) {
-               rc = snprintf(buf, n, "%s", a->vla);
-               ret += advance(&buf, &n, rc);
-       } else if (a->length) {
-               rc = snprintf(buf, n, "%ju", a->length);
-               ret += advance(&buf, &n, rc);
+               if (f->variadic)
+                       ret += cdecl__emit(dst, ", ...) ");
+               else
+                       ret += cdecl__emit(dst, ") ");
        }
 
-       return ret + snprintf(buf, n, "of");
+       return ret + cdecl__emit(dst, "returning ");
 }
 
 static size_t
-explain_declarators(char *buf, size_t n, struct cdecl_declarator *d)
+explain_declarators(struct output_state *dst, struct cdecl_declarator *d)
 {
-       size_t ret = 0, rc;
+       size_t ret;
 
-       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);
-       ret += advance(&buf, &n, rc);
+       ret = explain_declarators(dst, d->child);
 
        switch (d->type) {
        case CDECL_DECL_POINTER:
-               return ret + explain_pointer(buf, n, &d->u.pointer);
+               return ret + explain_pointer(dst, &d->u.pointer);
        case CDECL_DECL_ARRAY:
-               return ret + explain_array(buf, n, &d->u.array);
+               return ret + explain_array(dst, &d->u.array);
+       case CDECL_DECL_FUNCTION:
+               return ret + explain_function(dst, &d->u.function);
        default:
                assert(0);
        }
@@ -229,16 +169,13 @@ explain_declarators(char *buf, size_t n, struct cdecl_declarator *d)
 
 size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl)
 {
-       size_t ret = 0, rc;
-
-       rc = explain_prologue(buf, n, decl->declarators);
-       ret += advance(&buf, &n, rc);
-
-       rc = explain_pre_specs(buf, n, decl->specifiers);
-       ret += advance(&buf, &n, rc);
+       struct output_state dst = { buf, n };
+       size_t ret;
 
-       rc = explain_declarators(buf, n, decl->declarators);
-       ret += advance(&buf, &n, rc);
+       if (cdecl_is_abstract(decl->declarators))
+               ret = cdecl__emit(&dst, "type ");
+       else
+               ret = cdecl__emit(&dst, "declare ");
 
-       return ret + explain_post_specs(buf, n, decl->specifiers);
+       return ret + explain_decl(&dst, decl);
 }