Add separator_t to aid printing lists with separators.
authorChristoph Mallon <christoph.mallon@gmx.de>
Wed, 28 Mar 2012 07:10:29 +0000 (09:10 +0200)
committerChristoph Mallon <christoph.mallon@gmx.de>
Thu, 8 Nov 2012 08:24:22 +0000 (09:24 +0100)
ast.c
diagnostic.c
separator_t.h [new file with mode: 0644]
type.c
wrappergen/write_fluffy.c
wrappergen/write_jna.c

diff --git a/ast.c b/ast.c
index 712add2..3f258cb 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -26,6 +26,7 @@
 #include "lang_features.h"
 #include "entity_t.h"
 #include "printer.h"
+#include "separator_t.h"
 #include "types.h"
 
 #include <assert.h>
@@ -288,10 +289,9 @@ static void print_call_expression(const call_expression_t *call)
 {
        print_expression_prec(call->function, PREC_POSTFIX);
        print_char('(');
-       char const *sep = "";
+       separator_t sep = { "", ", " };
        for (call_argument_t const *arg = call->arguments; arg; arg = arg->next) {
-               print_string(sep);
-               sep = ", ";
+               print_string(sep_next(&sep));
                print_assignment_expression(arg->expression);
        }
        print_char(')');
@@ -1007,11 +1007,10 @@ static void print_for_statement(const for_statement_t *statement)
  */
 static void print_asm_arguments(asm_argument_t *arguments)
 {
+       separator_t     sep      = { "", ", " };
        asm_argument_t *argument = arguments;
        for (; argument != NULL; argument = argument->next) {
-               if (argument != arguments)
-                       print_string(", ");
-
+               print_string(sep_next(&sep));
                if (argument->symbol) {
                        print_format("[%s] ", argument->symbol->string);
                }
@@ -1029,11 +1028,10 @@ static void print_asm_arguments(asm_argument_t *arguments)
  */
 static void print_asm_clobbers(asm_clobber_t *clobbers)
 {
+       separator_t    sep     = { "", ", " };
        asm_clobber_t *clobber = clobbers;
        for (; clobber != NULL; clobber = clobber->next) {
-               if (clobber != clobbers)
-                       print_string(", ");
-
+               print_string(sep_next(&sep));
                print_quoted_string(&clobber->clobber, '"');
        }
 }
@@ -1206,26 +1204,20 @@ static void print_ms_modifiers(const declaration_t *declaration)
 
        decl_modifiers_t modifiers = declaration->modifiers;
 
-       bool        ds_shown = false;
-       const char *next     = "(";
+       separator_t sep = { "__declspec(", ", " };
 
        if (declaration->base.kind == ENTITY_VARIABLE) {
                variable_t *variable = (variable_t*)declaration;
                if (variable->alignment != 0
                                || variable->get_property_sym != NULL
                                || variable->put_property_sym != NULL) {
-                       if (!ds_shown) {
-                               print_string("__declspec");
-                               ds_shown = true;
-                       }
-
                        if (variable->alignment != 0) {
-                               print_string(next); next = ", "; print_format("align(%u)", variable->alignment);
+                               print_format("%salign(%u)", sep_next(&sep), variable->alignment);
                        }
                        if (variable->get_property_sym != NULL
                                        || variable->put_property_sym != NULL) {
                                char *comma = "";
-                               print_string(next); next = ", "; print_string("property(");
+                               print_format("%sproperty(", sep_next(&sep));
                                if (variable->get_property_sym != NULL) {
                                        print_format("get=%s", variable->get_property_sym->string);
                                        comma = ", ";
@@ -1239,52 +1231,48 @@ static void print_ms_modifiers(const declaration_t *declaration)
 
        /* DM_FORCEINLINE handled outside. */
        if ((modifiers & ~DM_FORCEINLINE) != 0) {
-               if (!ds_shown) {
-                       print_string("__declspec");
-                       ds_shown = true;
-               }
                if (modifiers & DM_DLLIMPORT) {
-                       print_string(next); next = ", "; print_string("dllimport");
+                       print_format("%sdllimport", sep_next(&sep));
                }
                if (modifiers & DM_DLLEXPORT) {
-                       print_string(next); next = ", "; print_string("dllexport");
+                       print_format("%sdllexport", sep_next(&sep));
                }
                if (modifiers & DM_THREAD) {
-                       print_string(next); next = ", "; print_string("thread");
+                       print_format("%sthread", sep_next(&sep));
                }
                if (modifiers & DM_NAKED) {
-                       print_string(next); next = ", "; print_string("naked");
+                       print_format("%snaked", sep_next(&sep));
                }
                if (modifiers & DM_THREAD) {
-                       print_string(next); next = ", "; print_string("thread");
+                       print_format("%sthread", sep_next(&sep));
                }
                if (modifiers & DM_SELECTANY) {
-                       print_string(next); next = ", "; print_string("selectany");
+                       print_format("%sselectany", sep_next(&sep));
                }
                if (modifiers & DM_NOTHROW) {
-                       print_string(next); next = ", "; print_string("nothrow");
+                       print_format("%snothrow", sep_next(&sep));
                }
                if (modifiers & DM_NORETURN) {
-                       print_string(next); next = ", "; print_string("noreturn");
+                       print_format("%snoreturn", sep_next(&sep));
                }
                if (modifiers & DM_NOINLINE) {
-                       print_string(next); next = ", "; print_string("noinline");
+                       print_format("%snoinline", sep_next(&sep));
                }
                if (modifiers & DM_DEPRECATED) {
-                       print_string(next); next = ", "; print_string("deprecated");
+                       print_format("%sdeprecated", sep_next(&sep));
                        if (declaration->deprecated_string != NULL)
                                print_format("(\"%s\")",
                                        declaration->deprecated_string);
                }
                if (modifiers & DM_RESTRICT) {
-                       print_string(next); next = ", "; print_string("restrict");
+                       print_format("%srestrict", sep_next(&sep));
                }
                if (modifiers & DM_NOALIAS) {
-                       print_string(next); next = ", "; print_string("noalias");
+                       print_format("%snoalias", sep_next(&sep));
                }
        }
 
-       if (ds_shown)
+       if (!sep_at_first(&sep))
                print_string(") ");
 }
 #endif
index 27fdda9..0464538 100644 (file)
@@ -23,6 +23,7 @@
 #include "diagnostic.h"
 #include "adt/error.h"
 #include "entity_t.h"
+#include "separator_t.h"
 #include "symbol_t.h"
 #include "token_t.h"
 #include "ast.h"
@@ -185,18 +186,13 @@ done_flags:;
 
                case 'k': {
                        if (extended) {
-                               bool              first     = true;
-                               va_list*          toks      = va_arg(ap, va_list*);
-                               const char* const delimiter = va_arg(ap, const char*);
+                               va_list* const toks = va_arg(ap, va_list*);
+                               separator_t    sep  = { "", va_arg(ap, const char*) };
                                for (;;) {
                                        const token_kind_t tok = (token_kind_t)va_arg(*toks, int);
                                        if (tok == 0)
                                                break;
-                                       if (first) {
-                                               first = false;
-                                       } else {
-                                               fputs(delimiter, stderr);
-                                       }
+                                       fputs(sep_next(&sep), stderr);
                                        print_token_kind(stderr, tok);
                                }
                        } else {
diff --git a/separator_t.h b/separator_t.h
new file mode 100644 (file)
index 0000000..63ee1e6
--- /dev/null
@@ -0,0 +1,31 @@
+#ifndef SEPARATOR_H
+#define SEPARATOR_H
+
+#include <stdbool.h>
+
+typedef struct separator_t separator_t;
+struct separator_t
+{
+       char const *first; /**< Returned on the first call to sep_next(). */
+       char const *rest;  /**< Returned on all further calls to sep_next(). */
+};
+
+/**
+ * Returns first on the first call for s and rest on all further calls.
+ */
+static inline char const* sep_next(separator_t* const s)
+{
+       char const *const cur = s->first;
+       s->first = s->rest;
+       return cur;
+}
+
+/**
+ * Returns whether sep_next() has not been called for s.
+ */
+static inline bool sep_at_first(separator_t const* const s)
+{
+       return s->first != s->rest;
+}
+
+#endif
diff --git a/type.c b/type.c
index f009f36..c904c7c 100644 (file)
--- a/type.c
+++ b/type.c
@@ -33,6 +33,7 @@
 #include "warning.h"
 #include "diagnostic.h"
 #include "printer.h"
+#include "separator_t.h"
 
 /** The default calling convention. */
 cc_kind_t default_calling_convention = CC_CDECL;
@@ -373,15 +374,11 @@ static void print_function_type_post(const function_type_t *type,
                                      const scope_t *parameters)
 {
        print_char('(');
-       bool first = true;
+       separator_t sep = { "", ", " };
        if (parameters == NULL) {
                function_parameter_t *parameter = type->parameters;
                for( ; parameter != NULL; parameter = parameter->next) {
-                       if (first) {
-                               first = false;
-                       } else {
-                               print_string(", ");
-                       }
+                       print_string(sep_next(&sep));
                        print_type(parameter->type);
                }
        } else {
@@ -390,11 +387,7 @@ static void print_function_type_post(const function_type_t *type,
                        if (parameter->kind != ENTITY_PARAMETER)
                                continue;
 
-                       if (first) {
-                               first = false;
-                       } else {
-                               print_string(", ");
-                       }
+                       print_string(sep_next(&sep));
                        const type_t *const param_type = parameter->declaration.type;
                        if (param_type == NULL) {
                                print_string(parameter->base.symbol->string);
@@ -404,14 +397,10 @@ static void print_function_type_post(const function_type_t *type,
                }
        }
        if (type->variadic) {
-               if (first) {
-                       first = false;
-               } else {
-                       print_string(", ");
-               }
+               print_string(sep_next(&sep));
                print_string("...");
        }
-       if (first && !type->unspecified_parameters) {
+       if (sep_at_first(&sep) && !type->unspecified_parameters) {
                print_string("void");
        }
        print_char(')');
index 2ff84a3..2ea7714 100644 (file)
@@ -23,6 +23,7 @@
 #include <string.h>
 
 #include "write_fluffy.h"
+#include "separator_t.h"
 #include "symbol_t.h"
 #include "ast_t.h"
 #include "type_t.h"
@@ -127,13 +128,9 @@ static void write_function_type(const function_type_t *type)
        fprintf(out, "(func(");
 
        function_parameter_t *parameter = type->parameters;
-       int                   first     = 1;
+       separator_t           sep       = { "", ", " };
        while(parameter != NULL) {
-               if(!first) {
-                       fprintf(out, ", ");
-               } else {
-                       first = 0;
-               }
+               fputs(sep_next(&sep), out);
 
 #if 0
                if(parameter->symbol != NULL) {
@@ -270,15 +267,11 @@ static void write_function(const entity_t *entity)
        const function_type_t *function_type
                = (const function_type_t*) entity->declaration.type;
 
-       entity_t *parameter = entity->function.parameters.entities;
-       int       first     = 1;
+       entity_t   *parameter = entity->function.parameters.entities;
+       separator_t sep       = { "", ", " };
        for( ; parameter != NULL; parameter = parameter->base.next) {
                assert(parameter->kind == ENTITY_PARAMETER);
-               if(!first) {
-                       fprintf(out, ", ");
-               } else {
-                       first = 0;
-               }
+               fputs(sep_next(&sep), out);
                if(parameter->base.symbol != NULL) {
                        fprintf(out, "%s : ", parameter->base.symbol->string);
                } else {
@@ -287,9 +280,7 @@ static void write_function(const entity_t *entity)
                write_type(parameter->declaration.type);
        }
        if(function_type->variadic) {
-               if(!first) {
-                       fprintf(out, ", ");
-               }
+               fputs(sep_next(&sep), out);
                fputs("...", out);
        }
        fprintf(out, ")");
index 3e17794..af9e8bb 100644 (file)
@@ -33,6 +33,7 @@
 #include "adt/error.h"
 #include "adt/xmalloc.h"
 #include "adt/pset_new.h"
+#include "separator_t.h"
 
 typedef struct output_limit {
        const char          *filename;
@@ -420,16 +421,12 @@ static void write_function(const entity_t *entity)
        write_type(return_type);
        fprintf(out, " %s(", entity->base.symbol->string);
 
-       entity_t *parameter = entity->function.parameters.entities;
-       int       first     = 1;
-       int       n         = 0;
+       entity_t   *parameter = entity->function.parameters.entities;
+       separator_t sep       = { "", ", " };
+       int         n         = 0;
        for ( ; parameter != NULL; parameter = parameter->base.next) {
                assert(parameter->kind == ENTITY_PARAMETER);
-               if(!first) {
-                       fprintf(out, ", ");
-               } else {
-                       first = 0;
-               }
+               fputs(sep_next(&sep), out);
                write_type(parameter->declaration.type);
                if(parameter->base.symbol != NULL) {
                        fprintf(out, " %s", fix_builtin_names(parameter->base.symbol->string));
@@ -438,9 +435,7 @@ static void write_function(const entity_t *entity)
                }
        }
        if(function_type->variadic) {
-               if(!first) {
-                       fprintf(out, ", ");
-               }
+               fputs(sep_next(&sep), out);
                fputs("Object ... args", out);
        }
        fprintf(out, ");\n");