- Rework the way literals are handled, these are now kept as strings until
[cparser] / wrappergen / write_jna.c
index 8f438ff..26bfd4e 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of cparser.
- * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
+ * 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
@@ -28,6 +28,7 @@
 #include "type_t.h"
 #include "entity_t.h"
 #include "type.h"
+#include "printer.h"
 #include "adt/error.h"
 #include <libfirm/adt/pset_new.h>
 
@@ -72,8 +73,8 @@ static const char *get_atomic_type_string(const atomic_type_kind_t type)
        case ATOMIC_TYPE_USHORT:      return "short";
        case ATOMIC_TYPE_INT:         return "int";
        case ATOMIC_TYPE_UINT:        return "int";
-       case ATOMIC_TYPE_LONG:        return "NativeLong";
-       case ATOMIC_TYPE_ULONG:       return "NativeLong";
+       case ATOMIC_TYPE_LONG:        return "com.sun.jna.NativeLong";
+       case ATOMIC_TYPE_ULONG:       return "com.sun.jna.NativeLong";
        case ATOMIC_TYPE_LONGLONG:    return "long";
        case ATOMIC_TYPE_ULONGLONG:   return "long";
        case ATOMIC_TYPE_FLOAT:       return "float";
@@ -96,6 +97,11 @@ static void write_pointer_type(const pointer_type_t *type)
                fputs("String", out);
                return;
        }
+       if (is_type_pointer(points_to)) {
+               /* hack... */
+               fputs("Pointer[]", out);
+               return;
+       }
        fputs("Pointer", out);
 }
 
@@ -292,16 +298,10 @@ static void write_binary_expression(const binary_expression_t *expression)
 
 static void write_expression(const expression_t *expression)
 {
-       const const_expression_t *constant;
        /* TODO */
        switch(expression->kind) {
-       case EXPR_CONST:
-               constant = &expression->conste;
-               if(is_type_integer(expression->base.type)) {
-                       fprintf(out, "%lld", constant->v.int_value);
-               } else {
-                       fprintf(out, "%Lf", constant->v.float_value);
-               }
+       case EXPR_LITERAL_INTEGER:
+               fprintf(out, "%s", expression->literal.value.begin);
                break;
        case EXPR_REFERENCE_ENUM_VALUE: {
                /* UHOH... hacking */
@@ -434,14 +434,10 @@ void write_jna_decls(FILE *output, const translation_unit_t *unit)
 
        pset_new_init(&avoid_symbols);
 
-       ast_set_output(out);
-       type_set_output(out);
+       print_to_file(out);
        fprintf(out, "/* WARNING: Automatically generated file */\n");
        fputs("import com.sun.jna.Library;\n", out);
-       fputs("import com.sun.jna.Native;\n", out);
-       fputs("import com.sun.jna.Platform;\n", out);
        fputs("import com.sun.jna.Pointer;\n", out);
-       fputs("import com.sun.jna.NativeLong;\n", out);
        fputs("\n\n", out);
 
        /* TODO: where to get the name from? */
@@ -459,7 +455,9 @@ void write_jna_decls(FILE *output, const translation_unit_t *unit)
                        if (buf[0] == 0)
                                continue;
 
-                       symbol_t *symbol = symbol_table_insert(buf);
+                       char *str = malloc(len+1);
+                       memcpy(str, buf, len+1);
+                       symbol_t *symbol = symbol_table_insert(str);
                        pset_new_insert(&avoid_symbols, symbol);
                }
                fclose(avoid);