cleanup: Add and use macro MAX().
[cparser] / wrappergen / write_fluffy.c
index f1a4021..8dbe880 100644 (file)
@@ -1,28 +1,11 @@
 /*
  * This file is part of cparser.
- * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
- *
- * 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 2
- * 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, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- * 02111-1307, USA.
+ * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
  */
 #include <config.h>
 
-#include <errno.h>
-#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 +110,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) {
@@ -173,8 +152,6 @@ static void write_type(const type_t *type)
        case TYPE_FUNCTION:
                write_function_type(&type->function);
                return;
-       case TYPE_INVALID:
-               panic("invalid type found");
        case TYPE_COMPLEX:
        case TYPE_IMAGINARY:
        default:
@@ -216,7 +193,7 @@ static void write_unary_expression(const unary_expression_t *expression)
                fputc('!', out);
                break;
        default:
-               panic("unimeplemented unary expression found");
+               panic("unimplemented unary expression");
        }
        write_expression(expression->value);
 }
@@ -227,7 +204,7 @@ static void write_expression(const expression_t *expression)
        case EXPR_LITERAL_INTEGER:
                fprintf(out, "%s", expression->literal.value.begin);
                break;
-       EXPR_UNARY_CASES
+       case EXPR_UNARY_CASES:
                write_unary_expression((const unary_expression_t*) expression);
                break;
        default:
@@ -262,7 +239,7 @@ static void write_variable(const entity_t *entity)
 
 static void write_function(const entity_t *entity)
 {
-       if (entity->function.statement != NULL) {
+       if (entity->function.body != NULL) {
                fprintf(stderr, "Warning: can't convert function bodies (at %s)\n",
                        entity->base.symbol->string);
        }
@@ -272,15 +249,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 {
@@ -289,17 +262,13 @@ static void write_function(const entity_t *entity)
                write_type(parameter->declaration.type);
        }
        if(function_type->variadic) {
-               if(!first) {
-                       fprintf(out, ", ");
-               } else {
-                       first = 0;
-               }
+               fputs(sep_next(&sep), out);
                fputs("...", out);
        }
        fprintf(out, ")");
 
        const type_t *return_type = skip_typeref(function_type->return_type);
-       if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
+       if (!is_type_void(return_type)) {
                fprintf(out, " : ");
                write_type(return_type);
        }
@@ -321,8 +290,7 @@ void write_fluffy_decls(FILE *output, const translation_unit_t *unit)
                        continue;
 
                type_t *type = entity->typedefe.type;
-               if(type->kind == TYPE_COMPOUND_STRUCT
-                               || type->kind == TYPE_COMPOUND_UNION) {
+               if (is_type_compound(type)) {
                        write_compound(entity->base.symbol, &type->compound);
                } else if(type->kind == TYPE_ENUM) {
                        write_enum(entity->base.symbol, &type->enumt);