]> git.draconx.ca Git - cdecl99.git/blobdiff - src/declare.c
Port to use getline.h from dxcommon.
[cdecl99.git] / src / declare.c
index bae96f351d370bbf0a3442d4579949bc4440692d..ea6003f6c9a26c8cff0ca3d8206eabf74e132f8c 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Render C declarations.
- *  Copyright © 2011 Nick Bowler
+ *  Copyright © 2011, 2021, 2023-2024 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
  *  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 <assert.h>
+#include <stdbool.h>
 
 #include "cdecl.h"
-#include "output.h"
-
-static size_t declare_specs(char *buf, size_t n, struct cdecl_declspec *s)
-{
-       size_t ret = 0, rc;
-
-       if (!s)
-               return 0;
-
-       rc = cdecl__explain_pre_specs(buf, n, s);
-       ret += cdecl__advance(&buf, &n, rc);
+#include "cdecl-internal.h"
 
-       rc = cdecl__explain_post_specs(buf, n, s);
-       return ret + rc;
-}
-
-static size_t
-declare_declarator(char *buf, size_t n, struct cdecl_declarator *d);
+static void
+declare_declarator(struct output_state *dst, struct cdecl_declarator *d);
 
-static size_t declare_decl(char *buf, size_t n, struct cdecl *decl)
+static void declare_decl(struct output_state *dst, struct cdecl *decl)
 {
-       size_t ret = 0, rc;
+       struct cdecl_declarator *d = decl->declarators;
+       const char *sep;
 
-       rc = declare_specs(buf, n, decl->specifiers);
-       if (decl->declarators->type != CDECL_DECL_NULL)
-               ret += cdecl__advance(&buf, &n, rc);
-       else
-               ret += cdecl__advance_(&buf, &n, rc);
+       sep = cdecl__emit_specs(dst, decl->specifiers, -1);
+       if (d->type != CDECL_DECL_NULL)
+               cdecl__emit(dst, sep);
 
-       return ret + declare_declarator(buf, n, decl->declarators);
+       declare_declarator(dst, d);
 }
 
-static size_t
-declare_postfix_child(char *buf, size_t n, struct cdecl_declarator *d)
+static void
+declare_postfix_child(struct output_state *dst, struct cdecl_declarator *d)
 {
-       size_t ret = 0, rc;
-
-       if (d->type == CDECL_DECL_POINTER) {
-               rc = snprintf(buf, n, "(");
-               ret += cdecl__advance_(&buf, &n, rc);
-       }
-
-       rc = declare_declarator(buf, n, d);
-       ret += cdecl__advance_(&buf, &n, rc);
+       bool need_parens = (d->type == CDECL_DECL_POINTER);
 
-       if (d->type == CDECL_DECL_POINTER) {
-               rc = snprintf(buf, n, ")");
-               ret += cdecl__advance_(&buf, &n, rc);
-       }
-
-       return ret;
+       if (need_parens) cdecl__emit(dst, "(");
+       declare_declarator(dst, d);
+       if (need_parens) cdecl__emit(dst, ")");
 }
 
-static size_t declare_pointer(char *buf, size_t n, struct cdecl_pointer *p)
+static void declare_pointer(struct output_state *dst, struct cdecl_declarator *d)
 {
-       size_t ret = 0, rc;
+       struct cdecl_declspec *q = d->u.pointer.qualifiers;
 
-       rc = snprintf(buf, n, "*");
-       if (p->qualifiers)
-               ret += cdecl__advance(&buf, &n, rc);
-       else
-               ret += cdecl__advance_(&buf, &n, rc);
+       if (q) {
+               const char *sep;
 
-       rc = cdecl__explain_specs(buf, n, p->qualifiers, CDECL_SPEC_QUAL);
-       return ret + cdecl__advance(&buf, &n, rc);
+               cdecl__emit(dst, "* ");
+               sep = cdecl__emit_specs(dst, q, -1);
+               if (d->child->type != CDECL_DECL_NULL)
+                       cdecl__emit(dst, sep);
+       } else {
+               cdecl__emit(dst, "*");
+       }
 }
 
-static size_t declare_array(char *buf, size_t n, struct cdecl_array *a)
+static void declare_array(struct output_state *dst, struct cdecl_array *a)
 {
-       size_t ret = 0, rc;
+       cdecl__emit(dst, "[");
 
-       rc = snprintf(buf, n, "[");
-       ret += cdecl__advance_(&buf, &n, rc);
-
-       if (a->vla)
-               rc = snprintf(buf, n, "%s", a->vla[0] ? a->vla : "*");
-       else
-               rc = snprintf(buf, n, "%.0ju", a->length);
-       ret += cdecl__advance_(&buf, &n, rc);
+       if (a->vla) {
+               const char *s = a->vla[0] ? a->vla : "*";
+               cdecl__emit(dst, s);
+       } else {
+               cdecl__emit_uint(dst, a->length);
+       }
 
-       return ret + snprintf(buf, n, "]");
+       cdecl__emit(dst, "]");
 }
 
-static size_t declare_function(char *buf, size_t n, struct cdecl_function *f)
+static void
+declare_function(struct output_state *dst, struct cdecl_function *f)
 {
-       size_t ret = 0, rc;
+       struct cdecl *p;
 
-       rc = snprintf(buf, n, "(");
-       ret += cdecl__advance_(&buf, &n, rc);
+       cdecl__emit(dst, "(");
 
-       for (struct cdecl *p = f->parameters; p; p = p->next) {
-               rc = declare_decl(buf, n, p);
-               ret += cdecl__advance_(&buf, &n, rc);
+       for (p = f->parameters; p; p = p->next) {
+               declare_decl(dst, p);
 
-               rc = 0;
                if (p->next)
-                       rc = snprintf(buf, n, ", ");
+                       cdecl__emit(dst, ", ");
                else if (f->variadic)
-                       rc = snprintf(buf, n, ", ...");
-               ret += cdecl__advance_(&buf, &n, rc);
+                       cdecl__emit(dst, ", ...");
        }
 
-       return ret + snprintf(buf, n, ")");
+       cdecl__emit(dst, ")");
 }
 
-static size_t
-declare_declarator(char *buf, size_t n, struct cdecl_declarator *d)
+static void
+declare_declarator(struct output_state *dst, struct cdecl_declarator *d)
 {
-       size_t ret = 0, rc;
-
        for (; d; d = d->child) {
                switch (d->type) {
                case CDECL_DECL_NULL:
                        break;
                case CDECL_DECL_IDENT:
-                       rc = snprintf(buf, n, "%s", d->u.ident);
-                       ret += cdecl__advance_(&buf, &n, rc);
+                       cdecl__emit(dst, d->u.ident);
                        break;
                case CDECL_DECL_POINTER:
-                       rc = declare_pointer(buf, n, &d->u.pointer);
-                       ret += cdecl__advance_(&buf, &n, rc);
+                       declare_pointer(dst, d);
                        break;
                /*
                 * Arrays and functions are special: since they are postfix,
@@ -148,22 +115,22 @@ declare_declarator(char *buf, size_t n, struct cdecl_declarator *d)
                 * "bodies".
                 */
                case CDECL_DECL_ARRAY:
-                       rc = declare_postfix_child(buf, n, d->child);
-                       ret += cdecl__advance_(&buf, &n, rc);
-                       return ret + declare_array(buf, n, &d->u.array);
+                       declare_postfix_child(dst, d->child);
+                       declare_array(dst, &d->u.array);
+                       return;
                case CDECL_DECL_FUNCTION:
-                       rc = declare_postfix_child(buf, n, d->child);
-                       ret += cdecl__advance_(&buf, &n, rc);
-                       return ret + declare_function(buf, n, &d->u.function);
-               default:
-                       assert(0);
+                       declare_postfix_child(dst, d->child);
+                       declare_function(dst, &d->u.function);
+                       return;
                }
        }
-
-       return ret;
 }
 
 size_t cdecl_declare(char *buf, size_t n, struct cdecl *decl)
 {
-       return declare_decl(buf, n, decl);
+       struct output_state dst = { buf, n };
+
+       declare_decl(&dst, decl);
+
+       return dst.accum;
 }