2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 #include "write_jna.h"
32 #include "adt/error.h"
33 #include "adt/xmalloc.h"
34 #include <libfirm/adt/pset_new.h>
36 typedef struct output_limit {
38 struct output_limit *next;
41 static const scope_t *global_scope;
43 static pset_new_t avoid_symbols;
44 static output_limit *output_limits;
45 static const char *libname;
47 static void write_type(type_t *type);
49 static bool is_system_header(const char *fname)
51 if (strncmp(fname, "/usr/include", 12) == 0)
53 if (fname == builtin_source_position.input_name)
58 static const char *fix_builtin_names(const char *name)
60 if (strcmp(name, "class") == 0) {
62 } else if(strcmp(name, "this") == 0) {
64 } else if(strcmp(name, "public") == 0) {
66 } else if(strcmp(name, "protected") == 0) {
68 } else if(strcmp(name, "private") == 0) {
70 } else if(strcmp(name, "final") == 0) {
73 /* TODO put all reserved names here */
77 static const char *get_atomic_type_string(const atomic_type_kind_t type)
80 case ATOMIC_TYPE_VOID: return "void";
81 case ATOMIC_TYPE_CHAR: return "byte";
82 case ATOMIC_TYPE_SCHAR: return "byte";
83 case ATOMIC_TYPE_UCHAR: return "byte";
84 case ATOMIC_TYPE_SHORT: return "short";
85 case ATOMIC_TYPE_USHORT: return "short";
86 case ATOMIC_TYPE_INT: return "int";
87 case ATOMIC_TYPE_UINT: return "int";
88 case ATOMIC_TYPE_LONG: return "com.sun.jna.NativeLong";
89 case ATOMIC_TYPE_ULONG: return "com.sun.jna.NativeLong";
90 case ATOMIC_TYPE_LONGLONG: return "long";
91 case ATOMIC_TYPE_ULONGLONG: return "long";
92 case ATOMIC_TYPE_FLOAT: return "float";
93 case ATOMIC_TYPE_DOUBLE: return "double";
94 case ATOMIC_TYPE_BOOL: return "boolean";
95 default: panic("unsupported atomic type");
99 static void write_atomic_type(const atomic_type_t *type)
101 fputs(get_atomic_type_string(type->akind), out);
104 static void write_pointer_type(const pointer_type_t *type)
106 type_t *points_to = skip_typeref(type->points_to);
107 if (is_type_atomic(points_to, ATOMIC_TYPE_CHAR)) {
108 fputs("String", out);
111 if (is_type_pointer(points_to)) {
113 fputs("java.nio.Buffer", out);
116 fputs("Pointer", out);
119 static entity_t *find_typedef(const type_t *type)
121 /* first: search for a matching typedef in the global type... */
122 entity_t *entity = global_scope->entities;
123 for ( ; entity != NULL; entity = entity->base.next) {
124 if (entity->kind != ENTITY_TYPEDEF)
126 if (entity->typedefe.type == type)
133 static entity_t *find_enum_typedef(const enum_t *enume)
135 /* first: search for a matching typedef in the global type... */
136 entity_t *entity = global_scope->entities;
137 for ( ; entity != NULL; entity = entity->base.next) {
138 if (entity->kind != ENTITY_TYPEDEF)
140 type_t *type = entity->typedefe.type;
141 if (type->kind != TYPE_ENUM)
144 enum_t *e_entity = type->enumt.enume;
145 if (e_entity == enume)
152 static void write_compound_type(const compound_type_t *type)
154 entity_t *entity = find_typedef((const type_t*) type);
156 fputs(entity->base.symbol->string, out);
160 /* does the struct have a name? */
161 symbol_t *symbol = type->compound->base.symbol;
163 /* TODO: make sure we create a struct for it... */
164 fputs(symbol->string, out);
167 /* TODO: create a struct and use its name here... */
168 fputs("/* TODO anonymous struct */byte", out);
171 static void write_enum_name(const enum_type_t *type)
173 entity_t *entity = find_typedef((const type_t*) type);
174 if (entity != NULL) {
175 fputs(entity->base.symbol->string, out);
179 /* does the enum have a name? */
180 symbol_t *symbol = type->enume->base.symbol;
181 if (symbol != NULL) {
182 /* TODO: make sure we create an enum for it... */
183 fputs(symbol->string, out);
187 /* now we have a problem as we don't know how we'll call the anonymous
189 panic("can't reference entries from anonymous enums yet");
192 static void write_enum_type(const enum_type_t *type)
194 entity_t *entity = find_typedef((const type_t*) type);
195 if (entity != NULL) {
196 fprintf(out, "/* %s */int", entity->base.symbol->string);
200 /* does the enum have a name? */
201 symbol_t *symbol = type->enume->base.symbol;
202 if (symbol != NULL) {
203 /* TODO: make sure we create an enum for it... */
204 fprintf(out, "/* %s */int", symbol->string);
207 fprintf(out, "/* anonymous enum */int");
210 static void write_type(type_t *type)
212 type = skip_typeref(type);
215 write_atomic_type(&type->atomic);
218 write_pointer_type(&type->pointer);
220 case TYPE_COMPOUND_UNION:
221 case TYPE_COMPOUND_STRUCT:
222 write_compound_type(&type->compound);
225 write_enum_type(&type->enumt);
231 panic("invalid type found");
238 fprintf(out, "/* TODO type */Pointer");
244 static void write_compound_entry(const entity_t *entity)
246 fprintf(out, "\t%s : ", entity->base.symbol->string);
247 write_type(entity->declaration.type);
251 static void write_compound(const symbol_t *symbol, const compound_type_t *type)
253 fprintf(out, "%s %s:\n",
254 type->base.kind == TYPE_COMPOUND_STRUCT ? "struct" : "union",
257 const entity_t *entity = type->compound->members.entities;
258 for ( ; entity != NULL; entity = entity->base.next) {
259 write_compound_entry(entity);
266 static void write_expression(const expression_t *expression);
268 static void write_unary_expression(const unary_expression_t *expression)
270 switch(expression->base.kind) {
271 case EXPR_UNARY_NEGATE:
277 case EXPR_UNARY_CAST_IMPLICIT:
278 write_expression(expression->value);
281 panic("unimeplemented unary expression found");
283 write_expression(expression->value);
286 static void write_binary_expression(const binary_expression_t *expression)
289 write_expression(expression->left);
291 switch(expression->base.kind) {
292 case EXPR_BINARY_BITWISE_OR: fputs("|", out); break;
293 case EXPR_BINARY_BITWISE_AND: fputs("&", out); break;
294 case EXPR_BINARY_BITWISE_XOR: fputs("^", out); break;
295 case EXPR_BINARY_SHIFTLEFT: fputs("<<", out); break;
296 case EXPR_BINARY_SHIFTRIGHT: fputs(">>", out); break;
297 case EXPR_BINARY_ADD: fputs("+", out); break;
298 case EXPR_BINARY_SUB: fputs("-", out); break;
299 case EXPR_BINARY_MUL: fputs("*", out); break;
300 case EXPR_BINARY_DIV: fputs("/", out); break;
302 panic("unimplemented binexpr");
305 write_expression(expression->right);
309 static void write_expression(const expression_t *expression)
312 switch(expression->kind) {
313 case EXPR_LITERAL_INTEGER:
314 case EXPR_LITERAL_INTEGER_OCTAL:
315 fprintf(out, "%s", expression->literal.value.begin);
317 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
318 fprintf(out, "0x%s", expression->literal.value.begin);
320 case EXPR_REFERENCE_ENUM_VALUE: {
321 /* UHOH... hacking */
322 entity_t *entity = expression->reference.entity;
323 write_enum_name(& entity->enum_value.enum_type->enumt);
324 fprintf(out, ".%s.val", entity->base.symbol->string);
328 write_unary_expression(&expression->unary);
331 write_binary_expression(&expression->binary);
334 panic("not implemented expression");
338 static void write_enum(const symbol_t *symbol, const enum_t *entity)
343 if (symbol == NULL) {
344 static int lastenum = 0;
345 snprintf(buf, sizeof(buf), "AnonEnum%d", lastenum++);
348 name = symbol->string;
351 fprintf(out, "\tpublic static enum %s {\n", name);
353 entity_t *entry = entity->base.next;
354 for ( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
355 entry = entry->base.next) {
356 fprintf(out, "\t\t%s", entry->base.symbol->string);
358 if(entry->enum_value.value != NULL) {
359 write_expression(entry->enum_value.value);
362 if (entry->base.next != NULL
363 && entry->base.next->kind == ENTITY_ENUM_VALUE) {
369 fprintf(out, "\t\tpublic final int val;\n");
371 fprintf(out, "\t\tprivate static class C {\n");
372 fprintf(out, "\t\t\tstatic int next_val;\n");
373 fprintf(out, "\t\t}\n");
375 fprintf(out, "\t\t%s(int val) {\n", name);
376 fprintf(out, "\t\t\tthis.val = val;\n");
377 fprintf(out, "\t\t\tC.next_val = val + 1;\n");
378 fprintf(out, "\t\t}\n");
380 fprintf(out, "\t\t%s() {\n", name);
381 fprintf(out, "\t\t\tthis.val = C.next_val++;\n");
382 fprintf(out, "\t\t}\n");
384 fprintf(out, "\t\tpublic static %s getEnum(int val) {\n", name);
385 fprintf(out, "\t\t\tfor (%s entry : values()) {\n", name);
386 fprintf(out, "\t\t\t\tif (val == entry.val)\n");
387 fprintf(out, "\t\t\t\t\treturn entry;\n");
388 fprintf(out, "\t\t\t}\n");
389 fprintf(out, "\t\t\treturn null;\n");
390 fprintf(out, "\t\t}\n");
391 fprintf(out, "\t}\n");
396 static void write_variable(const entity_t *entity)
398 fprintf(out, "var %s : ", entity->base.symbol->string);
399 write_type(entity->declaration.type);
404 static void write_function(const entity_t *entity)
406 if (entity->function.statement != NULL) {
407 fprintf(stderr, "Warning: can't convert function bodies (at %s)\n",
408 entity->base.symbol->string);
413 const function_type_t *function_type
414 = (const function_type_t*) entity->declaration.type;
417 fprintf(out, "\tpublic static native ");
418 type_t *return_type = skip_typeref(function_type->return_type);
419 write_type(return_type);
420 fprintf(out, " %s(", entity->base.symbol->string);
422 entity_t *parameter = entity->function.parameters.entities;
425 for ( ; parameter != NULL; parameter = parameter->base.next) {
426 assert(parameter->kind == ENTITY_PARAMETER);
432 write_type(parameter->declaration.type);
433 if(parameter->base.symbol != NULL) {
434 fprintf(out, " %s", fix_builtin_names(parameter->base.symbol->string));
436 fprintf(out, " _%d", n++);
439 if(function_type->variadic) {
445 fputs("Object ... args", out);
447 fprintf(out, ");\n");
450 void jna_limit_output(const char *filename)
452 output_limit *limit = xmalloc(sizeof(limit[0]));
453 limit->filename = filename;
455 limit->next = output_limits;
456 output_limits = limit;
459 void jna_set_libname(const char *new_libname)
461 libname = new_libname;
464 void write_jna_decls(FILE *output, const translation_unit_t *unit)
467 global_scope = &unit->scope;
469 pset_new_init(&avoid_symbols);
472 fprintf(out, "/* WARNING: Automatically generated file */\n");
473 fputs("import com.sun.jna.Native;\n", out);
474 fputs("import com.sun.jna.Pointer;\n", out);
477 const char *register_libname = libname;
478 if (register_libname == NULL)
479 register_libname = "library";
481 /* TODO: where to get the name from? */
482 fputs("public class binding {\n", out);
483 fputs("\tstatic {\n", out);
484 fprintf(out, "\t\tNative.register(\"%s\");\n", register_libname);
488 /* read the avoid list */
489 FILE *avoid = fopen("avoid.config", "r");
491 while (!feof(avoid)) {
493 char *res = fgets(buf, sizeof(buf), avoid);
499 size_t len = strlen(buf);
500 if (buf[len-1] == '\n')
503 char *str = malloc(len+1);
504 memcpy(str, buf, len+1);
505 symbol_t *symbol = symbol_table_insert(str);
506 pset_new_insert(&avoid_symbols, symbol);
511 /* write structs,unions + enums */
512 entity_t *entity = unit->scope.entities;
513 for ( ; entity != NULL; entity = entity->base.next) {
514 if (entity->kind == ENTITY_ENUM) {
515 if (find_enum_typedef(&entity->enume) != NULL)
517 write_enum(entity->base.symbol, &entity->enume);
518 } else if (entity->kind == ENTITY_TYPEDEF) {
519 type_t *type = entity->typedefe.type;
520 if (type->kind == TYPE_ENUM) {
521 write_enum(entity->base.symbol, type->enumt.enume);
526 if(type->kind == TYPE_COMPOUND_STRUCT
527 || type->kind == TYPE_COMPOUND_UNION) {
528 write_compound(entity->base.symbol, &type->compound);
533 /* write functions */
534 entity = unit->scope.entities;
535 for ( ; entity != NULL; entity = entity->base.next) {
536 if (entity->kind != ENTITY_FUNCTION)
538 const char *input_name = entity->base.source_position.input_name;
539 if (is_system_header(input_name))
541 if (entity->function.elf_visibility != ELF_VISIBILITY_DEFAULT)
543 if (output_limits != NULL) {
544 bool in_limits = false;
545 for (output_limit *limit = output_limits; limit != NULL;
546 limit = limit->next) {
547 if (strcmp(limit->filename, input_name) == 0) {
556 if (pset_new_contains(&avoid_symbols, entity->base.symbol))
558 write_function(entity);
563 pset_new_destroy(&avoid_symbols);