2 * This file is part of cparser.
3 * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
9 #include "adt/strutil.h"
10 #include "write_jna.h"
17 #include "adt/error.h"
18 #include "adt/xmalloc.h"
19 #include "adt/pset_new.h"
20 #include "separator_t.h"
21 #include "symbol_table.h"
23 typedef struct output_limit {
25 struct output_limit *next;
28 static const scope_t *global_scope;
30 static pset_new_t avoid_symbols;
31 static output_limit *output_limits;
32 static const char *libname;
34 static const char *fix_builtin_names(const char *name)
36 #define FIX(x) if (streq(name, x)) return "_" x
43 /* TODO put all reserved names here */
48 static const char *get_atomic_type_string(const atomic_type_kind_t type)
51 case ATOMIC_TYPE_VOID: return "void";
52 case ATOMIC_TYPE_CHAR: return "byte";
53 case ATOMIC_TYPE_SCHAR: return "byte";
54 case ATOMIC_TYPE_UCHAR: return "byte";
55 case ATOMIC_TYPE_SHORT: return "short";
56 case ATOMIC_TYPE_USHORT: return "short";
57 case ATOMIC_TYPE_INT: return "int";
58 case ATOMIC_TYPE_UINT: return "int";
59 case ATOMIC_TYPE_LONG: return "com.sun.jna.NativeLong";
60 case ATOMIC_TYPE_ULONG: return "com.sun.jna.NativeLong";
61 case ATOMIC_TYPE_LONGLONG: return "long";
62 case ATOMIC_TYPE_ULONGLONG: return "long";
63 case ATOMIC_TYPE_FLOAT: return "float";
64 case ATOMIC_TYPE_DOUBLE: return "double";
65 case ATOMIC_TYPE_BOOL: return "boolean";
66 default: panic("unsupported atomic type");
70 static void write_atomic_type(const atomic_type_t *type)
72 fputs(get_atomic_type_string(type->akind), out);
75 static void write_pointer_type(const pointer_type_t *type)
77 type_t *points_to = skip_typeref(type->points_to);
78 if (is_type_atomic(points_to, ATOMIC_TYPE_CHAR)) {
82 if (is_type_pointer(points_to)) {
84 fputs("java.nio.Buffer", out);
87 fputs("Pointer", out);
90 static entity_t *find_typedef(const type_t *type)
92 /* first: search for a matching typedef in the global type... */
93 entity_t *entity = global_scope->entities;
94 for ( ; entity != NULL; entity = entity->base.next) {
95 if (entity->kind != ENTITY_TYPEDEF)
97 if (entity->typedefe.type == type)
104 static entity_t *find_enum_typedef(const enum_t *enume)
106 /* first: search for a matching typedef in the global type... */
107 entity_t *entity = global_scope->entities;
108 for ( ; entity != NULL; entity = entity->base.next) {
109 if (entity->kind != ENTITY_TYPEDEF)
111 type_t *type = entity->typedefe.type;
112 if (type->kind != TYPE_ENUM)
115 enum_t *e_entity = type->enumt.enume;
116 if (e_entity == enume)
123 static void write_compound_type(const compound_type_t *type)
125 entity_t *entity = find_typedef((const type_t*) type);
127 fputs(entity->base.symbol->string, out);
131 /* does the struct have a name? */
132 symbol_t *symbol = type->compound->base.symbol;
134 /* TODO: make sure we create a struct for it... */
135 fputs(symbol->string, out);
138 /* TODO: create a struct and use its name here... */
139 fputs("/* TODO anonymous struct */byte", out);
142 static void write_enum_name(const enum_type_t *type)
144 entity_t *entity = find_typedef((const type_t*) type);
145 if (entity != NULL) {
146 fputs(entity->base.symbol->string, out);
150 /* does the enum have a name? */
151 symbol_t *symbol = type->enume->base.symbol;
152 if (symbol != NULL) {
153 /* TODO: make sure we create an enum for it... */
154 fputs(symbol->string, out);
158 /* now we have a problem as we don't know how we'll call the anonymous
160 panic("can't reference entries from anonymous enums yet");
163 static void write_enum_type(const enum_type_t *type)
165 entity_t *entity = find_typedef((const type_t*) type);
166 if (entity != NULL) {
167 fprintf(out, "/* %s */int", entity->base.symbol->string);
171 /* does the enum have a name? */
172 symbol_t *symbol = type->enume->base.symbol;
173 if (symbol != NULL) {
174 /* TODO: make sure we create an enum for it... */
175 fprintf(out, "/* %s */int", symbol->string);
178 fprintf(out, "/* anonymous enum */int");
181 static void write_type(type_t *type)
183 type = skip_typeref(type);
186 write_atomic_type(&type->atomic);
189 write_pointer_type(&type->pointer);
191 case TYPE_COMPOUND_UNION:
192 case TYPE_COMPOUND_STRUCT:
193 write_compound_type(&type->compound);
196 write_enum_type(&type->enumt);
201 panic("invalid type");
207 fprintf(out, "/* TODO type */Pointer");
213 static void write_compound_entry(const entity_t *entity)
215 fprintf(out, "\t%s : ", entity->base.symbol->string);
216 write_type(entity->declaration.type);
220 static void write_compound(const symbol_t *symbol, const compound_type_t *type)
222 fprintf(out, "%s %s:\n",
223 type->base.kind == TYPE_COMPOUND_STRUCT ? "struct" : "union",
226 const entity_t *entity = type->compound->members.entities;
227 for ( ; entity != NULL; entity = entity->base.next) {
228 write_compound_entry(entity);
235 static void write_expression(const expression_t *expression);
237 static void write_unary_expression(const unary_expression_t *expression)
239 switch(expression->base.kind) {
240 case EXPR_UNARY_NEGATE:
246 case EXPR_UNARY_CAST:
247 write_expression(expression->value);
250 panic("unimplemented unary expression");
252 write_expression(expression->value);
255 static void write_binary_expression(const binary_expression_t *expression)
258 write_expression(expression->left);
260 switch(expression->base.kind) {
261 case EXPR_BINARY_BITWISE_OR: fputs("|", out); break;
262 case EXPR_BINARY_BITWISE_AND: fputs("&", out); break;
263 case EXPR_BINARY_BITWISE_XOR: fputs("^", out); break;
264 case EXPR_BINARY_SHIFTLEFT: fputs("<<", out); break;
265 case EXPR_BINARY_SHIFTRIGHT: fputs(">>", out); break;
266 case EXPR_BINARY_ADD: fputs("+", out); break;
267 case EXPR_BINARY_SUB: fputs("-", out); break;
268 case EXPR_BINARY_MUL: fputs("*", out); break;
269 case EXPR_BINARY_DIV: fputs("/", out); break;
271 panic("unimplemented binexpr");
274 write_expression(expression->right);
278 static void write_integer(const literal_expression_t *literal)
280 for (const char *c = literal->value.begin; c != literal->suffix; ++c) {
285 static void write_expression(const expression_t *expression)
288 switch(expression->kind) {
289 case EXPR_LITERAL_INTEGER:
290 write_integer(&expression->literal);
293 case EXPR_ENUM_CONSTANT: {
294 /* UHOH... hacking */
295 entity_t *entity = expression->reference.entity;
296 write_enum_name(& entity->enum_value.enum_type->enumt);
297 fprintf(out, ".%s.val", entity->base.symbol->string);
300 case EXPR_UNARY_CASES:
301 write_unary_expression(&expression->unary);
303 case EXPR_BINARY_CASES:
304 write_binary_expression(&expression->binary);
307 panic("not implemented expression");
311 static void write_enum(const symbol_t *symbol, const enum_t *entity)
316 if (symbol == NULL) {
317 static int lastenum = 0;
318 snprintf(buf, sizeof(buf), "AnonEnum%d", lastenum++);
321 name = symbol->string;
324 fprintf(out, "\tpublic static enum %s {\n", name);
326 entity_t *entry = entity->base.next;
327 for ( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
328 entry = entry->base.next) {
329 fprintf(out, "\t\t%s", entry->base.symbol->string);
331 if(entry->enum_value.value != NULL) {
332 write_expression(entry->enum_value.value);
335 if (entry->base.next != NULL
336 && entry->base.next->kind == ENTITY_ENUM_VALUE) {
342 fprintf(out, "\t\tpublic final int val;\n");
344 fprintf(out, "\t\tprivate static class C {\n");
345 fprintf(out, "\t\t\tstatic int next_val;\n");
346 fprintf(out, "\t\t}\n");
348 fprintf(out, "\t\t%s(int val) {\n", name);
349 fprintf(out, "\t\t\tthis.val = val;\n");
350 fprintf(out, "\t\t\tC.next_val = val + 1;\n");
351 fprintf(out, "\t\t}\n");
353 fprintf(out, "\t\t%s() {\n", name);
354 fprintf(out, "\t\t\tthis.val = C.next_val++;\n");
355 fprintf(out, "\t\t}\n");
357 fprintf(out, "\t\tpublic static %s getEnum(int val) {\n", name);
358 fprintf(out, "\t\t\tfor (%s entry : values()) {\n", name);
359 fprintf(out, "\t\t\t\tif (val == entry.val)\n");
360 fprintf(out, "\t\t\t\t\treturn entry;\n");
361 fprintf(out, "\t\t\t}\n");
362 fprintf(out, "\t\t\treturn null;\n");
363 fprintf(out, "\t\t}\n");
364 fprintf(out, "\t}\n");
369 static void write_variable(const entity_t *entity)
371 fprintf(out, "var %s : ", entity->base.symbol->string);
372 write_type(entity->declaration.type);
377 static void write_function(const entity_t *entity)
379 if (entity->function.body != NULL) {
380 fprintf(stderr, "Warning: can't convert function bodies (at %s)\n",
381 entity->base.symbol->string);
386 const function_type_t *function_type
387 = (const function_type_t*) entity->declaration.type;
390 fprintf(out, "\tpublic static native ");
391 type_t *return_type = skip_typeref(function_type->return_type);
392 write_type(return_type);
393 fprintf(out, " %s(", entity->base.symbol->string);
395 entity_t *parameter = entity->function.parameters.entities;
396 separator_t sep = { "", ", " };
398 for ( ; parameter != NULL; parameter = parameter->base.next) {
399 assert(parameter->kind == ENTITY_PARAMETER);
400 fputs(sep_next(&sep), out);
401 write_type(parameter->declaration.type);
402 if(parameter->base.symbol != NULL) {
403 fprintf(out, " %s", fix_builtin_names(parameter->base.symbol->string));
405 fprintf(out, " _%d", n++);
408 if(function_type->variadic) {
409 fputs(sep_next(&sep), out);
410 fputs("Object ... args", out);
412 fprintf(out, ");\n");
415 void jna_limit_output(const char *filename)
417 output_limit *limit = xmalloc(sizeof(limit[0]));
418 limit->filename = filename;
420 limit->next = output_limits;
421 output_limits = limit;
424 void jna_set_libname(const char *new_libname)
426 libname = new_libname;
429 void write_jna_decls(FILE *output, const translation_unit_t *unit)
432 global_scope = &unit->scope;
434 pset_new_init(&avoid_symbols);
437 fprintf(out, "/* WARNING: Automatically generated file */\n");
438 fputs("import com.sun.jna.Native;\n", out);
439 fputs("import com.sun.jna.Pointer;\n", out);
442 const char *register_libname = libname;
443 if (register_libname == NULL)
444 register_libname = "library";
446 /* TODO: where to get the name from? */
447 fputs("public class binding {\n", out);
448 fputs("\tstatic {\n", out);
449 fprintf(out, "\t\tNative.register(\"%s\");\n", register_libname);
453 /* read the avoid list */
454 FILE *avoid = fopen("avoid.config", "r");
456 while (!feof(avoid)) {
458 char *res = fgets(buf, sizeof(buf), avoid);
464 size_t len = strlen(buf);
465 if (buf[len-1] == '\n')
468 char *str = malloc(len+1);
469 memcpy(str, buf, len+1);
470 symbol_t *symbol = symbol_table_insert(str);
471 pset_new_insert(&avoid_symbols, symbol);
476 /* write structs,unions + enums */
477 entity_t *entity = unit->scope.entities;
478 for ( ; entity != NULL; entity = entity->base.next) {
479 if (entity->kind == ENTITY_ENUM) {
480 if (find_enum_typedef(&entity->enume) != NULL)
482 write_enum(entity->base.symbol, &entity->enume);
483 } else if (entity->kind == ENTITY_TYPEDEF) {
484 type_t *type = entity->typedefe.type;
485 if (type->kind == TYPE_ENUM) {
486 write_enum(entity->base.symbol, type->enumt.enume);
491 if (is_type_compound(type)) {
492 write_compound(entity->base.symbol, &type->compound);
497 /* write functions */
498 entity = unit->scope.entities;
499 for ( ; entity != NULL; entity = entity->base.next) {
500 if (entity->kind != ENTITY_FUNCTION)
502 if (entity->base.pos.is_system_header)
504 if (entity->function.elf_visibility != ELF_VISIBILITY_DEFAULT)
506 if (output_limits != NULL) {
507 bool in_limits = false;
508 char const *const input_name = entity->base.pos.input_name;
509 for (output_limit *limit = output_limits; limit != NULL;
510 limit = limit->next) {
511 if (streq(limit->filename, input_name)) {
520 if (pset_new_contains(&avoid_symbols, entity->base.symbol))
522 write_function(entity);
527 pset_new_destroy(&avoid_symbols);