9115c7d37905609f44a70278b9ea58fa7c7c6c2f
[cparser] / wrappergen / write_jna.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
4  *
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.
9  *
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.
14  *
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
18  * 02111-1307, USA.
19  */
20 #include <config.h>
21
22 #include <errno.h>
23 #include <string.h>
24
25 #include "write_jna.h"
26 #include "symbol_t.h"
27 #include "ast_t.h"
28 #include "type_t.h"
29 #include "entity_t.h"
30 #include "type.h"
31 #include "printer.h"
32 #include "adt/error.h"
33 #include <libfirm/adt/pset_new.h>
34
35 static const scope_t *global_scope;
36 static FILE          *out;
37 static pset_new_t     avoid_symbols;
38
39 static void write_type(type_t *type);
40
41 static bool is_system_header(const char *fname)
42 {
43         if (strncmp(fname, "/usr/include", 12) == 0)
44                 return true;
45         if (fname == builtin_source_position.input_name)
46                 return true;
47         return false;
48 }
49
50 static const char *fix_builtin_names(const char *name)
51 {
52         if (strcmp(name, "class") == 0) {
53                 return "_class";
54         } else if(strcmp(name, "this") == 0) {
55                 return "_this";
56         } else if(strcmp(name, "public") == 0) {
57                 return "_public";
58         } else if(strcmp(name, "protected") == 0) {
59                 return "_protected";
60         } else if(strcmp(name, "private") == 0) {
61                 return "_private";
62         } else if(strcmp(name, "final") == 0) {
63                 return "_final";
64         }
65         /* TODO put all reserved names here */
66         return name;
67 }
68
69 static const char *get_atomic_type_string(const atomic_type_kind_t type)
70 {
71         switch(type) {
72         case ATOMIC_TYPE_VOID:        return "void";
73         case ATOMIC_TYPE_CHAR:        return "byte";
74         case ATOMIC_TYPE_SCHAR:       return "byte";
75         case ATOMIC_TYPE_UCHAR:       return "byte";
76         case ATOMIC_TYPE_SHORT:       return "short";
77         case ATOMIC_TYPE_USHORT:      return "short";
78         case ATOMIC_TYPE_INT:         return "int";
79         case ATOMIC_TYPE_UINT:        return "int";
80         case ATOMIC_TYPE_LONG:        return "com.sun.jna.NativeLong";
81         case ATOMIC_TYPE_ULONG:       return "com.sun.jna.NativeLong";
82         case ATOMIC_TYPE_LONGLONG:    return "long";
83         case ATOMIC_TYPE_ULONGLONG:   return "long";
84         case ATOMIC_TYPE_FLOAT:       return "float";
85         case ATOMIC_TYPE_DOUBLE:      return "double";
86         case ATOMIC_TYPE_LONG_DOUBLE: return "double";
87         case ATOMIC_TYPE_BOOL:        return "boolean";
88         default:                      panic("unsupported atomic type");
89         }
90 }
91
92 static void write_atomic_type(const atomic_type_t *type)
93 {
94         fputs(get_atomic_type_string(type->akind), out);
95 }
96
97 static void write_pointer_type(const pointer_type_t *type)
98 {
99         type_t *points_to = skip_typeref(type->points_to);
100         if (is_type_atomic(points_to, ATOMIC_TYPE_CHAR)) {
101                 fputs("String", out);
102                 return;
103         }
104         if (is_type_pointer(points_to)) {
105                 /* hack... */
106                 fputs("Pointer[]", out);
107                 return;
108         }
109         fputs("Pointer", out);
110 }
111
112 static entity_t *find_typedef(const type_t *type)
113 {
114         /* first: search for a matching typedef in the global type... */
115         entity_t *entity = global_scope->entities;
116         for ( ; entity != NULL; entity = entity->base.next) {
117                 if (entity->kind != ENTITY_TYPEDEF)
118                         continue;
119                 if (entity->typedefe.type == type)
120                         break;
121         }
122
123         return entity;
124 }
125
126 static entity_t *find_enum_typedef(const enum_t *enume)
127 {
128         /* first: search for a matching typedef in the global type... */
129         entity_t *entity = global_scope->entities;
130         for ( ; entity != NULL; entity = entity->base.next) {
131                 if (entity->kind != ENTITY_TYPEDEF)
132                         continue;
133                 type_t *type = entity->typedefe.type;
134                 if (type->kind != TYPE_ENUM)
135                         continue;
136
137                 enum_t *e_entity = type->enumt.enume;
138                 if (e_entity == enume)
139                         break;
140         }
141
142         return entity;
143 }
144
145 static void write_compound_type(const compound_type_t *type)
146 {
147         entity_t *entity = find_typedef((const type_t*) type);
148         if(entity != NULL) {
149                 fputs(entity->base.symbol->string, out);
150                 return;
151         }
152
153         /* does the struct have a name? */
154         symbol_t *symbol = type->compound->base.symbol;
155         if(symbol != NULL) {
156                 /* TODO: make sure we create a struct for it... */
157                 fputs(symbol->string, out);
158                 return;
159         }
160         /* TODO: create a struct and use its name here... */
161         fputs("/* TODO anonymous struct */byte", out);
162 }
163
164 static void write_enum_name(const enum_type_t *type)
165 {
166         entity_t *entity = find_typedef((const type_t*) type);
167         if (entity != NULL) {
168                 fputs(entity->base.symbol->string, out);
169                 return;
170         }
171
172         /* does the enum have a name? */
173         symbol_t *symbol = type->enume->base.symbol;
174         if (symbol != NULL) {
175                 /* TODO: make sure we create an enum for it... */
176                 fputs(symbol->string, out);
177                 return;
178         }
179
180         /* now we have a problem as we don't know how we'll call the anonymous
181          * enum */
182         panic("can't reference entries from anonymous enums yet");
183 }
184
185 static void write_enum_type(const enum_type_t *type)
186 {
187         entity_t *entity = find_typedef((const type_t*) type);
188         if (entity != NULL) {
189                 fprintf(out, "/* %s */int", entity->base.symbol->string);
190                 return;
191         }
192
193         /* does the enum have a name? */
194         symbol_t *symbol = type->enume->base.symbol;
195         if (symbol != NULL) {
196                 /* TODO: make sure we create an enum for it... */
197                 fprintf(out, "/* %s */int", symbol->string);
198                 return;
199         }
200         fprintf(out, "/* anonymous enum */int");
201 }
202
203 static void write_type(type_t *type)
204 {
205         type = skip_typeref(type);
206         switch(type->kind) {
207         case TYPE_ATOMIC:
208                 write_atomic_type(&type->atomic);
209                 return;
210         case TYPE_POINTER:
211                 write_pointer_type(&type->pointer);
212                 return;
213         case TYPE_COMPOUND_UNION:
214         case TYPE_COMPOUND_STRUCT:
215                 write_compound_type(&type->compound);
216                 return;
217         case TYPE_ENUM:
218                 write_enum_type(&type->enumt);
219                 return;
220         case TYPE_BUILTIN:
221                 write_type(type->builtin.real_type);
222                 return;
223         case TYPE_ERROR:
224         case TYPE_INVALID:
225         case TYPE_TYPEOF:
226         case TYPE_TYPEDEF:
227                 panic("invalid type found");
228         case TYPE_ARRAY:
229         case TYPE_BITFIELD:
230         case TYPE_REFERENCE:
231         case TYPE_FUNCTION:
232         case TYPE_COMPLEX:
233         case TYPE_IMAGINARY:
234                 fprintf(out, "/* TODO type */Pointer");
235                 break;
236         }
237 }
238
239 #if 0
240 static void write_compound_entry(const entity_t *entity)
241 {
242         fprintf(out, "\t%s : ", entity->base.symbol->string);
243         write_type(entity->declaration.type);
244         fprintf(out, "\n");
245 }
246
247 static void write_compound(const symbol_t *symbol, const compound_type_t *type)
248 {
249         fprintf(out, "%s %s:\n",
250                 type->base.kind == TYPE_COMPOUND_STRUCT ? "struct" : "union",
251                         symbol->string);
252
253         const entity_t *entity = type->compound->members.entities;
254         for ( ; entity != NULL; entity = entity->base.next) {
255                 write_compound_entry(entity);
256         }
257
258         fprintf(out, "\n");
259 }
260 #endif
261
262 static void write_expression(const expression_t *expression);
263
264 static void write_unary_expression(const unary_expression_t *expression)
265 {
266         switch(expression->base.kind) {
267         case EXPR_UNARY_NEGATE:
268                 fputc('-', out);
269                 break;
270         case EXPR_UNARY_NOT:
271                 fputc('!', out);
272                 break;
273         case EXPR_UNARY_CAST_IMPLICIT:
274                 write_expression(expression->value);
275                 return;
276         default:
277                 panic("unimeplemented unary expression found");
278         }
279         write_expression(expression->value);
280 }
281
282 static void write_binary_expression(const binary_expression_t *expression)
283 {
284         fputs("(", out);
285         write_expression(expression->left);
286         switch(expression->base.kind) {
287         case EXPR_BINARY_BITWISE_OR:  fputs("|", out); break;
288         case EXPR_BINARY_BITWISE_AND: fputs("&", out); break;
289         case EXPR_BINARY_BITWISE_XOR: fputs("^", out); break;
290         case EXPR_BINARY_SHIFTLEFT:   fputs("<<", out); break;
291         case EXPR_BINARY_SHIFTRIGHT:  fputs(">>", out); break;
292         case EXPR_BINARY_ADD:         fputs("+", out); break;
293         case EXPR_BINARY_SUB:         fputs("-", out); break;
294         case EXPR_BINARY_MUL:         fputs("*", out); break;
295         case EXPR_BINARY_DIV:         fputs("/", out); break;
296         default:
297                 panic("unimplemented binexpr");
298         }
299         write_expression(expression->right);
300         fputs(")", out);
301 }
302
303 static void write_expression(const expression_t *expression)
304 {
305         /* TODO */
306         switch(expression->kind) {
307         case EXPR_LITERAL_INTEGER:
308         case EXPR_LITERAL_INTEGER_OCTAL:
309                 fprintf(out, "%s", expression->literal.value.begin);
310                 break;
311         case EXPR_LITERAL_INTEGER_HEXADECIMAL:
312                 fprintf(out, "0x%s", expression->literal.value.begin);
313                 break;
314         case EXPR_REFERENCE_ENUM_VALUE: {
315                 /* UHOH... hacking */
316                 entity_t *entity = expression->reference.entity;
317                 write_enum_name(& entity->enum_value.enum_type->enumt);
318                 fprintf(out, ".%s.val", entity->base.symbol->string);
319                 break;
320         }
321         EXPR_UNARY_CASES
322                 write_unary_expression(&expression->unary);
323                 break;
324         EXPR_BINARY_CASES
325                 write_binary_expression(&expression->binary);
326                 break;
327         default:
328                 panic("not implemented expression");
329         }
330 }
331
332 static void write_enum(const symbol_t *symbol, const enum_t *entity)
333 {
334         char buf[128];
335         const char *name;
336
337         if (symbol == NULL) {
338                 static int lastenum = 0;
339                 snprintf(buf, sizeof(buf), "AnonEnum%d", lastenum++);
340                 name = buf;
341         } else {
342                 name = symbol->string;
343         }
344
345         fprintf(out, "\tpublic static enum %s {\n", name);
346
347         entity_t *entry = entity->base.next;
348         for ( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
349                         entry = entry->base.next) {
350                 fprintf(out, "\t\t%s", entry->base.symbol->string);
351                 fprintf(out, "(");
352                 if(entry->enum_value.value != NULL) {
353                         write_expression(entry->enum_value.value);
354                 }
355                 fprintf(out, ")");
356                 if (entry->base.next != NULL
357                                 && entry->base.next->kind == ENTITY_ENUM_VALUE) {
358                         fputs(",\n", out);
359                 } else {
360                         fputs(";\n", out);
361                 }
362         }
363         fprintf(out, "\t\tpublic final int val;\n");
364         fprintf(out, "\t\tprivate static class C { static int next_val; }\n\n");
365         fprintf(out, "\t\t%s(int val) {\n", name);
366         fprintf(out, "\t\t\tthis.val = val;\n");
367         fprintf(out, "\t\t\tC.next_val = val + 1;\n");
368         fprintf(out, "\t\t}\n");
369         fprintf(out, "\t\t%s() {\n", name);
370         fprintf(out, "\t\t\tthis.val = C.next_val++;\n");
371         fprintf(out, "\t\t}\n");
372         fprintf(out, "\t\t\n");
373         fprintf(out, "\t\tpublic static %s getEnum(int val) {\n", name);
374         fprintf(out, "\t\t\tfor(%s entry : values()) {\n", name);
375         fprintf(out, "\t\t\t\tif (val == entry.val)\n");
376         fprintf(out, "\t\t\t\t\treturn entry;\n");
377         fprintf(out, "\t\t\t}\n");
378         fprintf(out, "\t\t\treturn null;\n");
379         fprintf(out, "\t\t}\n");
380         fprintf(out, "\t}\n");
381 }
382
383 #if 0
384 static void write_variable(const entity_t *entity)
385 {
386         fprintf(out, "var %s : ", entity->base.symbol->string);
387         write_type(entity->declaration.type);
388         fprintf(out, "\n");
389 }
390 #endif
391
392 static void write_function(const entity_t *entity)
393 {
394         if (entity->function.statement != NULL) {
395                 fprintf(stderr, "Warning: can't convert function bodies (at %s)\n",
396                         entity->base.symbol->string);
397                 return;
398         }
399
400
401         const function_type_t *function_type
402                 = (const function_type_t*) entity->declaration.type;
403
404         fprintf(out, "\tpublic static native ");
405         type_t *return_type = skip_typeref(function_type->return_type);
406         write_type(return_type);
407         fprintf(out, " %s(", entity->base.symbol->string);
408
409         entity_t *parameter = entity->function.parameters.entities;
410         int       first     = 1;
411         int       n         = 0;
412         for( ; parameter != NULL; parameter = parameter->base.next) {
413                 assert(parameter->kind == ENTITY_PARAMETER);
414                 if(!first) {
415                         fprintf(out, ", ");
416                 } else {
417                         first = 0;
418                 }
419                 write_type(parameter->declaration.type);
420                 if(parameter->base.symbol != NULL) {
421                         fprintf(out, " %s", fix_builtin_names(parameter->base.symbol->string));
422                 } else {
423                         fprintf(out, " _%d", n++);
424                 }
425         }
426         if(function_type->variadic) {
427                 if(!first) {
428                         fprintf(out, ", ");
429                 } else {
430                         first = 0;
431                 }
432                 fputs("Object ... args", out);
433         }
434         fprintf(out, ");\n");
435 }
436
437
438 void write_jna_decls(FILE *output, const translation_unit_t *unit)
439 {
440         out          = output;
441         global_scope = &unit->scope;
442
443         pset_new_init(&avoid_symbols);
444
445         print_to_file(out);
446         fprintf(out, "/* WARNING: Automatically generated file */\n");
447         fputs("import com.sun.jna.Native;\n", out);
448         fputs("import com.sun.jna.Pointer;\n", out);
449         fputs("\n\n", out);
450
451         /* TODO: where to get the name from? */
452         fputs("public class binding {\n", out);
453         fputs("\tstatic { Native.register(\"firm\"); }\n", out);
454
455         /* read the avoid list */
456         FILE *avoid = fopen("avoid.config", "r");
457         if (avoid != NULL) {
458                 while (!feof(avoid)) {
459                         char buf[1024];
460                         char *res = fgets(buf, sizeof(buf), avoid);
461                         if (res == NULL)
462                                 break;
463                         if (buf[0] == 0)
464                                 continue;
465
466                         size_t len = strlen(buf);
467                         if (buf[len-1] == '\n')
468                                 buf[len-1] = 0;
469
470                         char *str = malloc(len+1);
471                         memcpy(str, buf, len+1);
472                         symbol_t *symbol = symbol_table_insert(str);
473                         pset_new_insert(&avoid_symbols, symbol);
474                 }
475                 fclose(avoid);
476         }
477
478         /* write structs,unions + enums */
479         entity_t *entity = unit->scope.entities;
480         for( ; entity != NULL; entity = entity->base.next) {
481                 if (entity->kind == ENTITY_ENUM) {
482                         if (find_enum_typedef(&entity->enume) != NULL)
483                                 continue;
484                         write_enum(entity->base.symbol, &entity->enume);
485                 } else if (entity->kind == ENTITY_TYPEDEF) {
486                         type_t *type = entity->typedefe.type;
487                         if (type->kind == TYPE_ENUM) {
488                                 write_enum(entity->base.symbol, type->enumt.enume);
489                         }
490                 }
491
492 #if 0
493                 if(type->kind == TYPE_COMPOUND_STRUCT
494                                 || type->kind == TYPE_COMPOUND_UNION) {
495                         write_compound(entity->base.symbol, &type->compound);
496                 }
497 #endif
498         }
499
500         /* write functions */
501         entity = unit->scope.entities;
502         for( ; entity != NULL; entity = entity->base.next) {
503                 if (entity->kind != ENTITY_FUNCTION)
504                         continue;
505                 if (is_system_header(entity->base.source_position.input_name))
506                         continue;
507
508                 if (pset_new_contains(&avoid_symbols, entity->base.symbol))
509                         continue;
510                 write_function(entity);
511         }
512
513         fputs("}\n", out);
514
515         pset_new_destroy(&avoid_symbols);
516 }