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