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 bool is_system_header(const char *fname)
49 if (strncmp(fname, "/usr/include", 12) == 0)
51 if (fname == builtin_source_position.input_name)
56 static const char *fix_builtin_names(const char *name)
58 if (strcmp(name, "class") == 0) {
60 } else if(strcmp(name, "this") == 0) {
62 } else if(strcmp(name, "public") == 0) {
64 } else if(strcmp(name, "protected") == 0) {
66 } else if(strcmp(name, "private") == 0) {
68 } else if(strcmp(name, "final") == 0) {
71 /* TODO put all reserved names here */
75 static const char *get_atomic_type_string(const atomic_type_kind_t type)
78 case ATOMIC_TYPE_VOID: return "void";
79 case ATOMIC_TYPE_CHAR: return "byte";
80 case ATOMIC_TYPE_SCHAR: return "byte";
81 case ATOMIC_TYPE_UCHAR: return "byte";
82 case ATOMIC_TYPE_SHORT: return "short";
83 case ATOMIC_TYPE_USHORT: return "short";
84 case ATOMIC_TYPE_INT: return "int";
85 case ATOMIC_TYPE_UINT: return "int";
86 case ATOMIC_TYPE_LONG: return "com.sun.jna.NativeLong";
87 case ATOMIC_TYPE_ULONG: return "com.sun.jna.NativeLong";
88 case ATOMIC_TYPE_LONGLONG: return "long";
89 case ATOMIC_TYPE_ULONGLONG: return "long";
90 case ATOMIC_TYPE_FLOAT: return "float";
91 case ATOMIC_TYPE_DOUBLE: return "double";
92 case ATOMIC_TYPE_BOOL: return "boolean";
93 default: panic("unsupported atomic type");
97 static void write_atomic_type(const atomic_type_t *type)
99 fputs(get_atomic_type_string(type->akind), out);
102 static void write_pointer_type(const pointer_type_t *type)
104 type_t *points_to = skip_typeref(type->points_to);
105 if (is_type_atomic(points_to, ATOMIC_TYPE_CHAR)) {
106 fputs("String", out);
109 if (is_type_pointer(points_to)) {
111 fputs("java.nio.Buffer", out);
114 fputs("Pointer", out);
117 static entity_t *find_typedef(const type_t *type)
119 /* first: search for a matching typedef in the global type... */
120 entity_t *entity = global_scope->entities;
121 for ( ; entity != NULL; entity = entity->base.next) {
122 if (entity->kind != ENTITY_TYPEDEF)
124 if (entity->typedefe.type == type)
131 static entity_t *find_enum_typedef(const enum_t *enume)
133 /* first: search for a matching typedef in the global type... */
134 entity_t *entity = global_scope->entities;
135 for ( ; entity != NULL; entity = entity->base.next) {
136 if (entity->kind != ENTITY_TYPEDEF)
138 type_t *type = entity->typedefe.type;
139 if (type->kind != TYPE_ENUM)
142 enum_t *e_entity = type->enumt.enume;
143 if (e_entity == enume)
150 static void write_compound_type(const compound_type_t *type)
152 entity_t *entity = find_typedef((const type_t*) type);
154 fputs(entity->base.symbol->string, out);
158 /* does the struct have a name? */
159 symbol_t *symbol = type->compound->base.symbol;
161 /* TODO: make sure we create a struct for it... */
162 fputs(symbol->string, out);
165 /* TODO: create a struct and use its name here... */
166 fputs("/* TODO anonymous struct */byte", out);
169 static void write_enum_name(const enum_type_t *type)
171 entity_t *entity = find_typedef((const type_t*) type);
172 if (entity != NULL) {
173 fputs(entity->base.symbol->string, out);
177 /* does the enum have a name? */
178 symbol_t *symbol = type->enume->base.symbol;
179 if (symbol != NULL) {
180 /* TODO: make sure we create an enum for it... */
181 fputs(symbol->string, out);
185 /* now we have a problem as we don't know how we'll call the anonymous
187 panic("can't reference entries from anonymous enums yet");
190 static void write_enum_type(const enum_type_t *type)
192 entity_t *entity = find_typedef((const type_t*) type);
193 if (entity != NULL) {
194 fprintf(out, "/* %s */int", entity->base.symbol->string);
198 /* does the enum have a name? */
199 symbol_t *symbol = type->enume->base.symbol;
200 if (symbol != NULL) {
201 /* TODO: make sure we create an enum for it... */
202 fprintf(out, "/* %s */int", symbol->string);
205 fprintf(out, "/* anonymous enum */int");
208 static void write_type(type_t *type)
210 type = skip_typeref(type);
213 write_atomic_type(&type->atomic);
216 write_pointer_type(&type->pointer);
218 case TYPE_COMPOUND_UNION:
219 case TYPE_COMPOUND_STRUCT:
220 write_compound_type(&type->compound);
223 write_enum_type(&type->enumt);
228 panic("invalid type found");
234 fprintf(out, "/* TODO type */Pointer");
240 static void write_compound_entry(const entity_t *entity)
242 fprintf(out, "\t%s : ", entity->base.symbol->string);
243 write_type(entity->declaration.type);
247 static void write_compound(const symbol_t *symbol, const compound_type_t *type)
249 fprintf(out, "%s %s:\n",
250 type->base.kind == TYPE_COMPOUND_STRUCT ? "struct" : "union",
253 const entity_t *entity = type->compound->members.entities;
254 for ( ; entity != NULL; entity = entity->base.next) {
255 write_compound_entry(entity);
262 static void write_expression(const expression_t *expression);
264 static void write_unary_expression(const unary_expression_t *expression)
266 switch(expression->base.kind) {
267 case EXPR_UNARY_NEGATE:
273 case EXPR_UNARY_CAST:
274 write_expression(expression->value);
277 panic("unimeplemented unary expression found");
279 write_expression(expression->value);
282 static void write_binary_expression(const binary_expression_t *expression)
285 write_expression(expression->left);
287 switch(expression->base.kind) {
288 case EXPR_BINARY_BITWISE_OR: fputs("|", out); break;
289 case EXPR_BINARY_BITWISE_AND: fputs("&", out); break;
290 case EXPR_BINARY_BITWISE_XOR: fputs("^", out); break;
291 case EXPR_BINARY_SHIFTLEFT: fputs("<<", out); break;
292 case EXPR_BINARY_SHIFTRIGHT: fputs(">>", out); break;
293 case EXPR_BINARY_ADD: fputs("+", out); break;
294 case EXPR_BINARY_SUB: fputs("-", out); break;
295 case EXPR_BINARY_MUL: fputs("*", out); break;
296 case EXPR_BINARY_DIV: fputs("/", out); break;
298 panic("unimplemented binexpr");
301 write_expression(expression->right);
305 static void write_expression(const expression_t *expression)
308 switch(expression->kind) {
309 case EXPR_LITERAL_INTEGER:
310 case EXPR_LITERAL_INTEGER_OCTAL:
311 fprintf(out, "%s", expression->literal.value.begin);
313 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
314 fprintf(out, "0x%s", expression->literal.value.begin);
316 case EXPR_REFERENCE_ENUM_VALUE: {
317 /* UHOH... hacking */
318 entity_t *entity = expression->reference.entity;
319 write_enum_name(& entity->enum_value.enum_type->enumt);
320 fprintf(out, ".%s.val", entity->base.symbol->string);
324 write_unary_expression(&expression->unary);
327 write_binary_expression(&expression->binary);
330 panic("not implemented expression");
334 static void write_enum(const symbol_t *symbol, const enum_t *entity)
339 if (symbol == NULL) {
340 static int lastenum = 0;
341 snprintf(buf, sizeof(buf), "AnonEnum%d", lastenum++);
344 name = symbol->string;
347 fprintf(out, "\tpublic static enum %s {\n", name);
349 entity_t *entry = entity->base.next;
350 for ( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
351 entry = entry->base.next) {
352 fprintf(out, "\t\t%s", entry->base.symbol->string);
354 if(entry->enum_value.value != NULL) {
355 write_expression(entry->enum_value.value);
358 if (entry->base.next != NULL
359 && entry->base.next->kind == ENTITY_ENUM_VALUE) {
365 fprintf(out, "\t\tpublic final int val;\n");
367 fprintf(out, "\t\tprivate static class C {\n");
368 fprintf(out, "\t\t\tstatic int next_val;\n");
369 fprintf(out, "\t\t}\n");
371 fprintf(out, "\t\t%s(int val) {\n", name);
372 fprintf(out, "\t\t\tthis.val = val;\n");
373 fprintf(out, "\t\t\tC.next_val = val + 1;\n");
374 fprintf(out, "\t\t}\n");
376 fprintf(out, "\t\t%s() {\n", name);
377 fprintf(out, "\t\t\tthis.val = C.next_val++;\n");
378 fprintf(out, "\t\t}\n");
380 fprintf(out, "\t\tpublic static %s getEnum(int val) {\n", name);
381 fprintf(out, "\t\t\tfor (%s entry : values()) {\n", name);
382 fprintf(out, "\t\t\t\tif (val == entry.val)\n");
383 fprintf(out, "\t\t\t\t\treturn entry;\n");
384 fprintf(out, "\t\t\t}\n");
385 fprintf(out, "\t\t\treturn null;\n");
386 fprintf(out, "\t\t}\n");
387 fprintf(out, "\t}\n");
392 static void write_variable(const entity_t *entity)
394 fprintf(out, "var %s : ", entity->base.symbol->string);
395 write_type(entity->declaration.type);
400 static void write_function(const entity_t *entity)
402 if (entity->function.statement != NULL) {
403 fprintf(stderr, "Warning: can't convert function bodies (at %s)\n",
404 entity->base.symbol->string);
409 const function_type_t *function_type
410 = (const function_type_t*) entity->declaration.type;
413 fprintf(out, "\tpublic static native ");
414 type_t *return_type = skip_typeref(function_type->return_type);
415 write_type(return_type);
416 fprintf(out, " %s(", entity->base.symbol->string);
418 entity_t *parameter = entity->function.parameters.entities;
421 for ( ; parameter != NULL; parameter = parameter->base.next) {
422 assert(parameter->kind == ENTITY_PARAMETER);
428 write_type(parameter->declaration.type);
429 if(parameter->base.symbol != NULL) {
430 fprintf(out, " %s", fix_builtin_names(parameter->base.symbol->string));
432 fprintf(out, " _%d", n++);
435 if(function_type->variadic) {
441 fputs("Object ... args", out);
443 fprintf(out, ");\n");
446 void jna_limit_output(const char *filename)
448 output_limit *limit = xmalloc(sizeof(limit[0]));
449 limit->filename = filename;
451 limit->next = output_limits;
452 output_limits = limit;
455 void jna_set_libname(const char *new_libname)
457 libname = new_libname;
460 void write_jna_decls(FILE *output, const translation_unit_t *unit)
463 global_scope = &unit->scope;
465 pset_new_init(&avoid_symbols);
468 fprintf(out, "/* WARNING: Automatically generated file */\n");
469 fputs("import com.sun.jna.Native;\n", out);
470 fputs("import com.sun.jna.Pointer;\n", out);
473 const char *register_libname = libname;
474 if (register_libname == NULL)
475 register_libname = "library";
477 /* TODO: where to get the name from? */
478 fputs("public class binding {\n", out);
479 fputs("\tstatic {\n", out);
480 fprintf(out, "\t\tNative.register(\"%s\");\n", register_libname);
484 /* read the avoid list */
485 FILE *avoid = fopen("avoid.config", "r");
487 while (!feof(avoid)) {
489 char *res = fgets(buf, sizeof(buf), avoid);
495 size_t len = strlen(buf);
496 if (buf[len-1] == '\n')
499 char *str = malloc(len+1);
500 memcpy(str, buf, len+1);
501 symbol_t *symbol = symbol_table_insert(str);
502 pset_new_insert(&avoid_symbols, symbol);
507 /* write structs,unions + enums */
508 entity_t *entity = unit->scope.entities;
509 for ( ; entity != NULL; entity = entity->base.next) {
510 if (entity->kind == ENTITY_ENUM) {
511 if (find_enum_typedef(&entity->enume) != NULL)
513 write_enum(entity->base.symbol, &entity->enume);
514 } else if (entity->kind == ENTITY_TYPEDEF) {
515 type_t *type = entity->typedefe.type;
516 if (type->kind == TYPE_ENUM) {
517 write_enum(entity->base.symbol, type->enumt.enume);
522 if(type->kind == TYPE_COMPOUND_STRUCT
523 || type->kind == TYPE_COMPOUND_UNION) {
524 write_compound(entity->base.symbol, &type->compound);
529 /* write functions */
530 entity = unit->scope.entities;
531 for ( ; entity != NULL; entity = entity->base.next) {
532 if (entity->kind != ENTITY_FUNCTION)
534 const char *input_name = entity->base.source_position.input_name;
535 if (is_system_header(input_name))
537 if (entity->function.elf_visibility != ELF_VISIBILITY_DEFAULT)
539 if (output_limits != NULL) {
540 bool in_limits = false;
541 for (output_limit *limit = output_limits; limit != NULL;
542 limit = limit->next) {
543 if (strcmp(limit->filename, input_name) == 0) {
552 if (pset_new_contains(&avoid_symbols, entity->base.symbol))
554 write_function(entity);
559 pset_new_destroy(&avoid_symbols);