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 <libfirm/adt/pset_new.h>
35 static const scope_t *global_scope;
37 static pset_new_t avoid_symbols;
39 static void write_type(type_t *type);
41 static bool is_system_header(const char *fname)
43 return strncmp(fname, "/usr/include", 12) == 0;
46 static const char *fix_builtin_names(const char *name)
48 if (strcmp(name, "class") == 0) {
50 } else if(strcmp(name, "this") == 0) {
52 } else if(strcmp(name, "public") == 0) {
54 } else if(strcmp(name, "protected") == 0) {
56 } else if(strcmp(name, "private") == 0) {
58 } else if(strcmp(name, "final") == 0) {
61 /* TODO put all reserved names here */
65 static const char *get_atomic_type_string(const atomic_type_kind_t type)
68 case ATOMIC_TYPE_VOID: return "void";
69 case ATOMIC_TYPE_CHAR: return "byte";
70 case ATOMIC_TYPE_SCHAR: return "byte";
71 case ATOMIC_TYPE_UCHAR: return "byte";
72 case ATOMIC_TYPE_SHORT: return "short";
73 case ATOMIC_TYPE_USHORT: return "short";
74 case ATOMIC_TYPE_INT: return "int";
75 case ATOMIC_TYPE_UINT: return "int";
76 case ATOMIC_TYPE_LONG: return "com.sun.jna.NativeLong";
77 case ATOMIC_TYPE_ULONG: return "com.sun.jna.NativeLong";
78 case ATOMIC_TYPE_LONGLONG: return "long";
79 case ATOMIC_TYPE_ULONGLONG: return "long";
80 case ATOMIC_TYPE_FLOAT: return "float";
81 case ATOMIC_TYPE_DOUBLE: return "double";
82 case ATOMIC_TYPE_LONG_DOUBLE: return "double";
83 case ATOMIC_TYPE_BOOL: return "boolean";
84 default: panic("unsupported atomic type");
88 static void write_atomic_type(const atomic_type_t *type)
90 fputs(get_atomic_type_string(type->akind), out);
93 static void write_pointer_type(const pointer_type_t *type)
95 type_t *points_to = skip_typeref(type->points_to);
96 if (is_type_atomic(points_to, ATOMIC_TYPE_CHAR)) {
100 if (is_type_pointer(points_to)) {
102 fputs("Pointer[]", out);
105 fputs("Pointer", out);
108 static entity_t *find_typedef(const type_t *type)
110 /* first: search for a matching typedef in the global type... */
111 entity_t *entity = global_scope->entities;
112 for ( ; entity != NULL; entity = entity->base.next) {
113 if (entity->kind != ENTITY_TYPEDEF)
115 if (entity->typedefe.type == type)
122 static entity_t *find_enum_typedef(const enum_t *enume)
124 /* first: search for a matching typedef in the global type... */
125 entity_t *entity = global_scope->entities;
126 for ( ; entity != NULL; entity = entity->base.next) {
127 if (entity->kind != ENTITY_TYPEDEF)
129 type_t *type = entity->typedefe.type;
130 if (type->kind != TYPE_ENUM)
133 enum_t *e_entity = type->enumt.enume;
134 if (e_entity == enume)
141 static void write_compound_type(const compound_type_t *type)
143 entity_t *entity = find_typedef((const type_t*) type);
145 fputs(entity->base.symbol->string, out);
149 /* does the struct have a name? */
150 symbol_t *symbol = type->compound->base.symbol;
152 /* TODO: make sure we create a struct for it... */
153 fputs(symbol->string, out);
156 /* TODO: create a struct and use its name here... */
157 fputs("/* TODO anonymous struct */byte", out);
160 static void write_enum_name(const enum_type_t *type)
162 entity_t *entity = find_typedef((const type_t*) type);
163 if (entity != NULL) {
164 fputs(entity->base.symbol->string, out);
168 /* does the enum have a name? */
169 symbol_t *symbol = type->enume->base.symbol;
170 if (symbol != NULL) {
171 /* TODO: make sure we create an enum for it... */
172 fputs(symbol->string, out);
176 /* now we have a problem as we don't know how we'll call the anonymous
178 panic("can't reference entries from anonymous enums yet");
181 static void write_enum_type(const enum_type_t *type)
183 entity_t *entity = find_typedef((const type_t*) type);
184 if (entity != NULL) {
185 fprintf(out, "/* %s */int", entity->base.symbol->string);
189 /* does the enum have a name? */
190 symbol_t *symbol = type->enume->base.symbol;
191 if (symbol != NULL) {
192 /* TODO: make sure we create an enum for it... */
193 fprintf(out, "/* %s */int", symbol->string);
196 fprintf(out, "/* anonymous enum */int");
199 static void write_type(type_t *type)
201 type = skip_typeref(type);
204 write_atomic_type(&type->atomic);
207 write_pointer_type(&type->pointer);
209 case TYPE_COMPOUND_UNION:
210 case TYPE_COMPOUND_STRUCT:
211 write_compound_type(&type->compound);
214 write_enum_type(&type->enumt);
217 write_type(type->builtin.real_type);
223 panic("invalid type found");
230 fprintf(out, "/* TODO type */Pointer");
236 static void write_compound_entry(const entity_t *entity)
238 fprintf(out, "\t%s : ", entity->base.symbol->string);
239 write_type(entity->declaration.type);
243 static void write_compound(const symbol_t *symbol, const compound_type_t *type)
245 fprintf(out, "%s %s:\n",
246 type->base.kind == TYPE_COMPOUND_STRUCT ? "struct" : "union",
249 const entity_t *entity = type->compound->members.entities;
250 for ( ; entity != NULL; entity = entity->base.next) {
251 write_compound_entry(entity);
258 static void write_expression(const expression_t *expression);
260 static void write_unary_expression(const unary_expression_t *expression)
262 switch(expression->base.kind) {
263 case EXPR_UNARY_NEGATE:
269 case EXPR_UNARY_CAST_IMPLICIT:
270 write_expression(expression->value);
273 panic("unimeplemented unary expression found");
275 write_expression(expression->value);
278 static void write_binary_expression(const binary_expression_t *expression)
281 write_expression(expression->left);
282 switch(expression->base.kind) {
283 case EXPR_BINARY_BITWISE_OR: fputs("|", out); break;
284 case EXPR_BINARY_BITWISE_AND: fputs("&", out); break;
285 case EXPR_BINARY_BITWISE_XOR: fputs("^", out); break;
286 case EXPR_BINARY_SHIFTLEFT: fputs("<<", out); break;
287 case EXPR_BINARY_SHIFTRIGHT: fputs(">>", out); break;
288 case EXPR_BINARY_ADD: fputs("+", out); break;
289 case EXPR_BINARY_SUB: fputs("-", out); break;
290 case EXPR_BINARY_MUL: fputs("*", out); break;
291 case EXPR_BINARY_DIV: fputs("/", out); break;
293 panic("unimplemented binexpr");
295 write_expression(expression->right);
299 static void write_expression(const expression_t *expression)
302 switch(expression->kind) {
303 case EXPR_LITERAL_INTEGER:
304 case EXPR_LITERAL_INTEGER_OCTAL:
305 fprintf(out, "%s", expression->literal.value.begin);
307 case EXPR_LITERAL_INTEGER_HEXADECIMAL:
308 fprintf(out, "0x%s", expression->literal.value.begin);
310 case EXPR_REFERENCE_ENUM_VALUE: {
311 /* UHOH... hacking */
312 entity_t *entity = expression->reference.entity;
313 write_enum_name(& entity->enum_value.enum_type->enumt);
314 fprintf(out, ".%s.val", entity->base.symbol->string);
318 write_unary_expression(&expression->unary);
321 write_binary_expression(&expression->binary);
324 panic("not implemented expression");
328 static void write_enum(const symbol_t *symbol, const enum_t *entity)
333 if (symbol == NULL) {
334 static int lastenum = 0;
335 snprintf(buf, sizeof(buf), "AnonEnum%d", lastenum++);
338 name = symbol->string;
341 fprintf(out, "\tpublic static enum %s {\n", name);
343 entity_t *entry = entity->base.next;
344 for ( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
345 entry = entry->base.next) {
346 fprintf(out, "\t\t%s", entry->base.symbol->string);
348 if(entry->enum_value.value != NULL) {
349 write_expression(entry->enum_value.value);
352 if (entry->base.next != NULL
353 && entry->base.next->kind == ENTITY_ENUM_VALUE) {
359 fprintf(out, "\t\tpublic final int val;\n");
360 fprintf(out, "\t\tprivate static class C { static int next_val; }\n\n");
361 fprintf(out, "\t\t%s(int val) {\n", name);
362 fprintf(out, "\t\t\tthis.val = val;\n");
363 fprintf(out, "\t\t\tC.next_val = val + 1;\n");
364 fprintf(out, "\t\t}\n");
365 fprintf(out, "\t\t%s() {\n", name);
366 fprintf(out, "\t\t\tthis.val = C.next_val++;\n");
367 fprintf(out, "\t\t}\n");
368 fprintf(out, "\t\t\n");
369 fprintf(out, "\t\tpublic static %s getEnum(int val) {\n", name);
370 fprintf(out, "\t\t\tfor(%s entry : values()) {\n", name);
371 fprintf(out, "\t\t\t\tif (val == entry.val)\n");
372 fprintf(out, "\t\t\t\t\treturn entry;\n");
373 fprintf(out, "\t\t\t}\n");
374 fprintf(out, "\t\t\treturn null;\n");
375 fprintf(out, "\t\t}\n");
376 fprintf(out, "\t}\n");
380 static void write_variable(const entity_t *entity)
382 fprintf(out, "var %s : ", entity->base.symbol->string);
383 write_type(entity->declaration.type);
388 static void write_function(const entity_t *entity)
390 if (entity->function.statement != NULL) {
391 fprintf(stderr, "Warning: can't convert function bodies (at %s)\n",
392 entity->base.symbol->string);
397 const function_type_t *function_type
398 = (const function_type_t*) entity->declaration.type;
401 type_t *return_type = skip_typeref(function_type->return_type);
402 write_type(return_type);
403 fprintf(out, " %s(", entity->base.symbol->string);
405 entity_t *parameter = entity->function.parameters.entities;
408 for( ; parameter != NULL; parameter = parameter->base.next) {
409 assert(parameter->kind == ENTITY_PARAMETER);
415 write_type(parameter->declaration.type);
416 if(parameter->base.symbol != NULL) {
417 fprintf(out, " %s", fix_builtin_names(parameter->base.symbol->string));
419 fprintf(out, " _%d", n++);
422 if(function_type->variadic) {
428 fputs("Object ... args", out);
430 fprintf(out, ");\n");
434 void write_jna_decls(FILE *output, const translation_unit_t *unit)
437 global_scope = &unit->scope;
439 pset_new_init(&avoid_symbols);
442 fprintf(out, "/* WARNING: Automatically generated file */\n");
443 fputs("import com.sun.jna.Library;\n", out);
444 fputs("import com.sun.jna.Pointer;\n", out);
447 /* TODO: where to get the name from? */
448 fputs("public interface binding extends Library {\n", out);
450 /* read the avoid list */
451 FILE *avoid = fopen("avoid.config", "r");
453 while (!feof(avoid)) {
455 char *res = fgets(buf, sizeof(buf), avoid);
461 size_t len = strlen(buf);
462 if (buf[len-1] == '\n')
465 char *str = malloc(len+1);
466 memcpy(str, buf, len+1);
467 symbol_t *symbol = symbol_table_insert(str);
468 pset_new_insert(&avoid_symbols, symbol);
473 /* write structs,unions + enums */
474 entity_t *entity = unit->scope.entities;
475 for( ; entity != NULL; entity = entity->base.next) {
476 if (entity->kind == ENTITY_ENUM) {
477 if (find_enum_typedef(&entity->enume) != NULL)
479 write_enum(entity->base.symbol, &entity->enume);
480 } else if (entity->kind == ENTITY_TYPEDEF) {
481 type_t *type = entity->typedefe.type;
482 if (type->kind == TYPE_ENUM) {
483 write_enum(entity->base.symbol, type->enumt.enume);
488 if(type->kind == TYPE_COMPOUND_STRUCT
489 || type->kind == TYPE_COMPOUND_UNION) {
490 write_compound(entity->base.symbol, &type->compound);
495 /* write functions */
496 entity = unit->scope.entities;
497 for( ; entity != NULL; entity = entity->base.next) {
498 if (entity->kind != ENTITY_FUNCTION)
500 if (is_system_header(entity->base.source_position.input_name))
503 if (pset_new_contains(&avoid_symbols, entity->base.symbol))
505 write_function(entity);
510 pset_new_destroy(&avoid_symbols);