]> git.draconx.ca Git - cdecl99.git/blobdiff - src/declare.c
Port to use getline.h from dxcommon.
[cdecl99.git] / src / declare.c
index 119d2b24d157dfb33b2e7ab21cb6dd1936552b24..ea6003f6c9a26c8cff0ca3d8206eabf74e132f8c 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Render C declarations.
- *  Copyright © 2011, 2021, 2023 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
  */
 
 #include <config.h>
-#include <stdio.h>
 #include <stdbool.h>
-#include <inttypes.h>
-#include <assert.h>
 
 #include "cdecl.h"
 #include "cdecl-internal.h"
@@ -30,35 +27,37 @@ declare_declarator(struct output_state *dst, struct cdecl_declarator *d);
 
 static void declare_decl(struct output_state *dst, struct cdecl *decl)
 {
+       struct cdecl_declarator *d = decl->declarators;
        const char *sep;
 
        sep = cdecl__emit_specs(dst, decl->specifiers, -1);
-       if (decl->declarators->type != CDECL_DECL_NULL)
+       if (d->type != CDECL_DECL_NULL)
                cdecl__emit(dst, sep);
 
-       declare_declarator(dst, decl->declarators);
+       declare_declarator(dst, d);
 }
 
 static void
 declare_postfix_child(struct output_state *dst, struct cdecl_declarator *d)
 {
-       if (d->type == CDECL_DECL_POINTER)
-               cdecl__emit(dst, "(");
+       bool need_parens = (d->type == CDECL_DECL_POINTER);
 
+       if (need_parens) cdecl__emit(dst, "(");
        declare_declarator(dst, d);
-
-       if (d->type == CDECL_DECL_POINTER)
-               cdecl__emit(dst, ")");
+       if (need_parens) cdecl__emit(dst, ")");
 }
 
-static void declare_pointer(struct output_state *dst, struct cdecl_pointer *p)
+static void declare_pointer(struct output_state *dst, struct cdecl_declarator *d)
 {
-       struct cdecl_declspec *q = p->qualifiers;
+       struct cdecl_declspec *q = d->u.pointer.qualifiers;
 
        if (q) {
+               const char *sep;
+
                cdecl__emit(dst, "* ");
-               cdecl__emit_specs(dst, q, -1);
-               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, "*");
        }
@@ -72,8 +71,7 @@ static void declare_array(struct output_state *dst, struct cdecl_array *a)
                const char *s = a->vla[0] ? a->vla : "*";
                cdecl__emit(dst, s);
        } else {
-               size_t rc = snprintf(dst->dst, dst->dstlen, "%.0" PRIuMAX, a->length);
-               cdecl__advance(dst, rc);
+               cdecl__emit_uint(dst, a->length);
        }
 
        cdecl__emit(dst, "]");
@@ -109,7 +107,7 @@ declare_declarator(struct output_state *dst, struct cdecl_declarator *d)
                        cdecl__emit(dst, d->u.ident);
                        break;
                case CDECL_DECL_POINTER:
-                       declare_pointer(dst, &d->u.pointer);
+                       declare_pointer(dst, d);
                        break;
                /*
                 * Arrays and functions are special: since they are postfix,
@@ -124,8 +122,6 @@ declare_declarator(struct output_state *dst, struct cdecl_declarator *d)
                        declare_postfix_child(dst, d->child);
                        declare_function(dst, &d->u.function);
                        return;
-               default:
-                       assert(0);
                }
        }
 }