Instead of cascading the lengths back via function return values, since
we now have a state structure we can just track the total length in one
place. This is quite a lot conceptually simpler and cuts out a good
chunk of code.
struct output_state {
char *dst;
size_t dstlen;
struct output_state {
char *dst;
size_t dstlen;
};
size_t cdecl__advance(struct output_state *dst, size_t amount);
};
size_t cdecl__advance(struct output_state *dst, size_t amount);
#include "cdecl.h"
#include "cdecl-internal.h"
#include "cdecl.h"
#include "cdecl-internal.h"
declare_declarator(struct output_state *dst, struct cdecl_declarator *d);
declare_declarator(struct output_state *dst, struct cdecl_declarator *d);
-static size_t declare_decl(struct output_state *dst, struct cdecl *decl)
+static void declare_decl(struct output_state *dst, struct cdecl *decl)
- size_t ret;
-
- ret = cdecl__emit_specs(dst, decl->specifiers, -1);
+ cdecl__emit_specs(dst, decl->specifiers, -1);
if (decl->declarators->type != CDECL_DECL_NULL)
if (decl->declarators->type != CDECL_DECL_NULL)
- ret += cdecl__emit(dst, " ");
- return ret + declare_declarator(dst, decl->declarators);
+ declare_declarator(dst, decl->declarators);
declare_postfix_child(struct output_state *dst, struct cdecl_declarator *d)
{
declare_postfix_child(struct output_state *dst, struct cdecl_declarator *d)
{
if (d->type == CDECL_DECL_POINTER)
if (d->type == CDECL_DECL_POINTER)
- ret += cdecl__emit(dst, "(");
- ret += declare_declarator(dst, d);
+ declare_declarator(dst, d);
if (d->type == CDECL_DECL_POINTER)
if (d->type == CDECL_DECL_POINTER)
- ret += cdecl__emit(dst, ")");
-
- return ret;
-static size_t
-declare_pointer(struct output_state *dst, struct cdecl_pointer *p)
+static void declare_pointer(struct output_state *dst, struct cdecl_pointer *p)
{
struct cdecl_declspec *q = p->qualifiers;
if (q) {
{
struct cdecl_declspec *q = p->qualifiers;
if (q) {
- size_t ret;
-
- ret = cdecl__emit(dst, "* ");
- ret += cdecl__emit_specs(dst, q, -1);
- return ret + cdecl__emit(dst, " ");
+ cdecl__emit(dst, "* ");
+ cdecl__emit_specs(dst, q, -1);
+ cdecl__emit(dst, " ");
+ } else {
+ cdecl__emit(dst, "*");
-
- return cdecl__emit(dst, "*");
-static size_t declare_array(struct output_state *dst, struct cdecl_array *a)
+static void declare_array(struct output_state *dst, struct cdecl_array *a)
- size_t ret;
-
- ret = cdecl__emit(dst, "[");
if (a->vla) {
const char *s = a->vla[0] ? a->vla : "*";
if (a->vla) {
const char *s = a->vla[0] ? a->vla : "*";
- ret += cdecl__emit(dst, s);
} else {
size_t rc = snprintf(dst->dst, dst->dstlen, "%.0" PRIuMAX, a->length);
} else {
size_t rc = snprintf(dst->dst, dst->dstlen, "%.0" PRIuMAX, a->length);
- ret += cdecl__advance(dst, rc);
+ cdecl__advance(dst, rc);
- return ret + cdecl__emit(dst, "]");
declare_function(struct output_state *dst, struct cdecl_function *f)
{
struct cdecl *p;
declare_function(struct output_state *dst, struct cdecl_function *f)
{
struct cdecl *p;
- ret = cdecl__emit(dst, "(");
for (p = f->parameters; p; p = p->next) {
for (p = f->parameters; p; p = p->next) {
- ret += declare_decl(dst, p);
- ret += cdecl__emit(dst, ", ");
+ cdecl__emit(dst, ", ");
- ret += cdecl__emit(dst, ", ...");
+ cdecl__emit(dst, ", ...");
- return ret + cdecl__emit(dst, ")");
declare_declarator(struct output_state *dst, struct cdecl_declarator *d)
{
declare_declarator(struct output_state *dst, struct cdecl_declarator *d)
{
for (; d; d = d->child) {
switch (d->type) {
case CDECL_DECL_NULL:
break;
case CDECL_DECL_IDENT:
for (; d; d = d->child) {
switch (d->type) {
case CDECL_DECL_NULL:
break;
case CDECL_DECL_IDENT:
- ret += cdecl__emit(dst, d->u.ident);
+ cdecl__emit(dst, d->u.ident);
break;
case CDECL_DECL_POINTER:
break;
case CDECL_DECL_POINTER:
- ret += declare_pointer(dst, &d->u.pointer);
+ declare_pointer(dst, &d->u.pointer);
break;
/*
* Arrays and functions are special: since they are postfix,
break;
/*
* Arrays and functions are special: since they are postfix,
* "bodies".
*/
case CDECL_DECL_ARRAY:
* "bodies".
*/
case CDECL_DECL_ARRAY:
- ret += declare_postfix_child(dst, d->child);
- return ret + declare_array(dst, &d->u.array);
+ declare_postfix_child(dst, d->child);
+ declare_array(dst, &d->u.array);
+ return;
case CDECL_DECL_FUNCTION:
case CDECL_DECL_FUNCTION:
- ret += declare_postfix_child(dst, d->child);
- return ret + declare_function(dst, &d->u.function);
+ declare_postfix_child(dst, d->child);
+ declare_function(dst, &d->u.function);
+ return;
}
size_t cdecl_declare(char *buf, size_t n, struct cdecl *decl)
{
struct output_state dst = { buf, n };
}
size_t cdecl_declare(char *buf, size_t n, struct cdecl *decl)
{
struct output_state dst = { buf, n };
- return declare_decl(&dst, decl);
+ declare_decl(&dst, decl);
+
+ return dst.accum;
#define PRE_SPECS (CDECL_SPEC_FUNC|CDECL_SPEC_STOR)
#define POST_SPECS (CDECL_SPEC_QUAL|CDECL_SPEC_TYPE)
#define PRE_SPECS (CDECL_SPEC_FUNC|CDECL_SPEC_STOR)
#define POST_SPECS (CDECL_SPEC_QUAL|CDECL_SPEC_TYPE)
-static size_t explain_specs(struct output_state *dst, struct cdecl_declspec *s, unsigned mask)
+static void explain_specs(struct output_state *dst, struct cdecl_declspec *s, unsigned mask)
- ret = cdecl__emit_specs(dst, s, mask);
- return ret + cdecl__emit(dst, " " + !ret);
-}
-
-static size_t
-explain_specs_post(struct output_state *dst, struct cdecl_declspec *s)
-{
- return cdecl__emit_specs(dst, s, POST_SPECS);
+ rc = cdecl__emit_specs(dst, s, mask);
+ cdecl__emit(dst, " " + !rc);
}
/*
* If declarator declares an identifier foo, then emit "foo as "; otherwise
* emit nothing.
*/
}
/*
* If declarator declares an identifier foo, then emit "foo as "; otherwise
* emit nothing.
*/
explain_name(struct output_state *dst, struct cdecl_declarator *d)
{
explain_name(struct output_state *dst, struct cdecl_declarator *d)
{
while (d->child)
d = d->child;
while (d->child)
d = d->child;
- if (d->type != CDECL_DECL_IDENT)
- return 0;
-
- ret = cdecl__emit(dst, d->u.ident);
- return ret + cdecl__emit(dst, " as ");
+ if (d->type == CDECL_DECL_IDENT) {
+ cdecl__emit(dst, d->u.ident);
+ cdecl__emit(dst, " as ");
+ }
}
/*
* For a pointer declarator, emit "[QUAL ]pointer to ", where
* QUAL is the (possibly empty) list of qualifiers.
*/
}
/*
* For a pointer declarator, emit "[QUAL ]pointer to ", where
* QUAL is the (possibly empty) list of qualifiers.
*/
explain_pointer(struct output_state *dst, struct cdecl_pointer *p)
{
explain_pointer(struct output_state *dst, struct cdecl_pointer *p)
{
- size_t ret;
-
- ret = explain_specs(dst, p->qualifiers, -1);
- return ret + cdecl__emit(dst, "pointer to ");
+ explain_specs(dst, p->qualifiers, -1);
+ cdecl__emit(dst, "pointer to ");
* for a variable-length array X is the (possibly omitted) identifier name,
* and for normal arrays X is the (possibly omitted) length.
*/
* for a variable-length array X is the (possibly omitted) identifier name,
* and for normal arrays X is the (possibly omitted) length.
*/
explain_array(struct output_state *dst, struct cdecl_array *a)
{
explain_array(struct output_state *dst, struct cdecl_array *a)
{
- size_t ret = 0, rc = 0;
- ret += cdecl__emit(dst, "variable-length ");
- ret += cdecl__emit(dst, "array ");
+ cdecl__emit(dst, "variable-length ");
+ cdecl__emit(dst, "array ");
if (a->vla) {
rc = cdecl__emit(dst, a->vla);
if (a->vla) {
rc = cdecl__emit(dst, a->vla);
} else {
rc = snprintf(dst->dst, dst->dstlen, "%.0" PRIuMAX, a->length);
} else {
rc = snprintf(dst->dst, dst->dstlen, "%.0" PRIuMAX, a->length);
- ret += cdecl__advance(dst, rc);
+ cdecl__advance(dst, rc);
- return ret + cdecl__emit(dst, " of " + !rc);
+ cdecl__emit(dst, " of " + !rc);
explain_declarators(struct output_state *dst, struct cdecl_declarator *decl);
explain_declarators(struct output_state *dst, struct cdecl_declarator *decl);
-static size_t explain_decl(struct output_state *dst, struct cdecl *decl)
+static void explain_decl(struct output_state *dst, struct cdecl *decl)
- size_t ret;
-
- 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);
-
- return ret;
+ explain_name(dst, decl->declarators);
+ explain_specs(dst, decl->specifiers, PRE_SPECS);
+ explain_declarators(dst, decl->declarators);
+ cdecl__emit_specs(dst, decl->specifiers, POST_SPECS);
* PARAM is the (omitted in the case of an empty non-prototype identifier
* list) parameter or identifier lists enclosed in parentheses.
*/
* PARAM is the (omitted in the case of an empty non-prototype identifier
* list) parameter or identifier lists enclosed in parentheses.
*/
explain_function(struct output_state *dst, struct cdecl_function *f)
{
explain_function(struct output_state *dst, struct cdecl_function *f)
{
- size_t ret = 0;
-
- ret += cdecl__emit(dst, "function ");
+ cdecl__emit(dst, "function ");
if (f->parameters) {
struct cdecl *p;
if (f->parameters) {
struct cdecl *p;
- ret += cdecl__emit(dst, "(");
for (p = f->parameters; p; p = p->next) {
for (p = f->parameters; p; p = p->next) {
- ret += explain_decl(dst, p);
- ret += cdecl__emit(dst, ", ");
+ cdecl__emit(dst, ", ");
- ret += cdecl__emit(dst, ", ...) ");
+ cdecl__emit(dst, ", ...) ");
- ret += cdecl__emit(dst, ") ");
+ cdecl__emit(dst, ") ");
- return ret + cdecl__emit(dst, "returning ");
+ cdecl__emit(dst, "returning ");
explain_declarators(struct output_state *dst, struct cdecl_declarator *d)
{
explain_declarators(struct output_state *dst, struct cdecl_declarator *d)
{
if (d->type == CDECL_DECL_IDENT || d->type == CDECL_DECL_NULL)
if (d->type == CDECL_DECL_IDENT || d->type == CDECL_DECL_NULL)
- ret = explain_declarators(dst, d->child);
+ explain_declarators(dst, d->child);
switch (d->type) {
case CDECL_DECL_POINTER:
switch (d->type) {
case CDECL_DECL_POINTER:
- return ret + explain_pointer(dst, &d->u.pointer);
+ explain_pointer(dst, &d->u.pointer);
+ return;
- return ret + explain_array(dst, &d->u.array);
+ explain_array(dst, &d->u.array);
+ return;
case CDECL_DECL_FUNCTION:
case CDECL_DECL_FUNCTION:
- return ret + explain_function(dst, &d->u.function);
+ explain_function(dst, &d->u.function);
+ return;
size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl)
{
struct output_state dst = { buf, n };
size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl)
{
struct output_state dst = { buf, n };
if (cdecl_is_abstract(decl->declarators))
if (cdecl_is_abstract(decl->declarators))
- ret = cdecl__emit(&dst, "type ");
+ cdecl__emit(&dst, "type ");
- ret = cdecl__emit(&dst, "declare ");
+ cdecl__emit(&dst, "declare ");
- return ret + explain_decl(&dst, decl);
+ explain_decl(&dst, decl);
+ return dst.accum;
dst->dst += x;
dst->dstlen -= x;
dst->dst += x;
dst->dstlen -= x;