- 2009 patch
[cparser] / wrappergen / write_caml.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_caml.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
33 static const scope_t *global_scope;
34 static FILE          *out;
35
36 static void write_type(const type_t *type);
37
38 static const char *get_atomic_type_string(const atomic_type_kind_t type)
39 {
40         switch(type) {
41         case ATOMIC_TYPE_VOID:        return "unit";
42         case ATOMIC_TYPE_CHAR:        return "byte";
43         case ATOMIC_TYPE_SCHAR:       return "byte";
44         case ATOMIC_TYPE_UCHAR:       return "unsigned byte";
45         case ATOMIC_TYPE_SHORT:       return "short";
46         case ATOMIC_TYPE_USHORT:      return "unsigned short";
47         case ATOMIC_TYPE_INT:         return "int";
48         case ATOMIC_TYPE_UINT:        return "unsigned int";
49         case ATOMIC_TYPE_LONG:        return "int";
50         case ATOMIC_TYPE_ULONG:       return "unsigned int";
51         case ATOMIC_TYPE_LONGLONG:    return "long";
52         case ATOMIC_TYPE_ULONGLONG:   return "unsigned long";
53         case ATOMIC_TYPE_FLOAT:       return "float";
54         case ATOMIC_TYPE_DOUBLE:      return "double";
55         case ATOMIC_TYPE_LONG_DOUBLE: return "double";
56         case ATOMIC_TYPE_BOOL:        return "bool";
57         default:                      panic("unsupported atomic type");
58         }
59 }
60
61 static void write_atomic_type(const atomic_type_t *type)
62 {
63         fprintf(out, "%s", get_atomic_type_string(type->akind));
64 }
65
66 static void write_pointer_type(const pointer_type_t *type)
67 {
68         type_t *points_to = type->points_to;
69         if (points_to->kind == TYPE_ATOMIC &&
70                         is_type_atomic(points_to, ATOMIC_TYPE_CHAR)) {
71                 fputs("string", out);
72                 return;
73         }
74
75         write_type(type->points_to);
76         fputc('*', out);
77 }
78
79 static entity_t *find_typedef(const type_t *type)
80 {
81         /* first: search for a matching typedef in the global type... */
82         entity_t *entity = global_scope->entities;
83         for ( ; entity != NULL; entity = entity->base.next) {
84                 if (entity->kind != ENTITY_TYPEDEF)
85                         continue;
86                 if (entity->typedefe.type == type)
87                         break;
88         }
89
90         return entity;
91 }
92
93 static void write_compound_type(const compound_type_t *type)
94 {
95         const entity_t *entity = find_typedef((const type_t*) type);
96         if (entity != NULL) {
97                 fprintf(out, "%s", entity->base.symbol->string);
98                 return;
99         }
100
101         /* does the struct have a name? */
102         symbol_t *symbol = type->compound->base.symbol;
103         if (symbol != NULL) {
104                 /* TODO: make sure we create a struct for it... */
105                 fprintf(out, "%s", symbol->string);
106                 return;
107         }
108         /* TODO: create a struct and use its name here... */
109         fprintf(out, "/* TODO anonymous struct */byte");
110 }
111
112 static void write_enum_type(const enum_type_t *type)
113 {
114         const entity_t *entity = find_typedef((const type_t*) type);
115         if (entity != NULL) {
116                 fprintf(out, "%s", entity->base.symbol->string);
117                 return;
118         }
119
120         /* does the enum have a name? */
121         symbol_t *symbol = type->enume->base.symbol;
122         if (symbol != NULL) {
123                 /* TODO: make sure we create an enum for it... */
124                 fprintf(out, "%s", symbol->string);
125                 return;
126         }
127         /* TODO: create a struct and use its name here... */
128         fprintf(out, "/* TODO anonymous enum */byte");
129 }
130
131 static void write_function_type(const function_type_t *type)
132 {
133         fprintf(out, "(func(");
134
135         function_parameter_t *parameter = type->parameters;
136         int                   first     = 1;
137         while(parameter != NULL) {
138                 if (!first) {
139                         fprintf(out, ", ");
140                 } else {
141                         first = 0;
142                 }
143
144 #if 0
145                 if (parameter->symbol != NULL) {
146                         fprintf(out, "%s : ", parameter->symbol->string);
147                 } else {
148                         /* TODO make up some unused names (or allow _ in fluffy?) */
149                         fprintf(out, "_ : ");
150                 }
151 #endif
152                 fputs("_ : ", out);
153                 write_type(parameter->type);
154
155                 parameter = parameter->next;
156         }
157
158         fprintf(out, ") : ");
159         write_type(type->return_type);
160         fprintf(out, ")");
161 }
162
163 static void write_type(const type_t *type)
164 {
165         switch(type->kind) {
166         case TYPE_ATOMIC:
167                 write_atomic_type(&type->atomic);
168                 return;
169         case TYPE_POINTER:
170                 write_pointer_type(&type->pointer);
171                 return;
172         case TYPE_COMPOUND_UNION:
173         case TYPE_COMPOUND_STRUCT:
174                 write_compound_type(&type->compound);
175                 return;
176         case TYPE_ENUM:
177                 write_enum_type(&type->enumt);
178                 return;
179         case TYPE_FUNCTION:
180                 write_function_type(&type->function);
181                 return;
182         case TYPE_INVALID:
183                 panic("invalid type found");
184         case TYPE_COMPLEX:
185         case TYPE_IMAGINARY:
186         default:
187                 fprintf(out, "/* TODO type */");
188                 break;
189         }
190 }
191
192 static void write_function(const entity_t *entity)
193 {
194         if (entity->function.statement != NULL) {
195                 fprintf(stderr, "Warning: can't convert function bodies (at %s)\n",
196                         entity->base.symbol->string);
197         }
198
199         fprintf(out, "external %s: ", entity->base.symbol->string);
200
201         const function_type_t *function_type
202                 = (const function_type_t*) entity->declaration.type;
203
204         entity_t *parameter = entity->function.parameters.entities;
205         for( ; parameter != NULL; parameter = parameter->base.next) {
206                 assert(parameter->kind == ENTITY_PARAMETER);
207                 write_type(parameter->declaration.type);
208                 fputs(" -> ", out);
209         }
210         if (function_type->variadic) {
211                 fprintf(stderr, "WARNING: Variadic function not supported yet\n");
212         }
213         if (function_type->unspecified_parameters) {
214                 fprintf(stderr, "WARNING: unspecified params not supported\n");
215         }
216         const type_t *return_type = function_type->return_type;
217         write_type(return_type);
218
219         fputs(" = \"", out);
220         fputs(entity->base.symbol->string, out);
221         fputs("\"", out);
222
223         fputc('\n', out);
224 }
225
226 void write_caml_decls(FILE *output, const translation_unit_t *unit)
227 {
228         out          = output;
229         global_scope = &unit->scope;
230
231         ast_set_output(out);
232         fprintf(out, "(* WARNING: Automatically generated file - chaning is useless *)\n");
233
234         /* write functions */
235         entity_t *entity = unit->scope.entities;
236         for( ; entity != NULL; entity = entity->base.next) {
237                 if (entity->kind != ENTITY_FUNCTION)
238                         continue;
239
240                 write_function(entity);
241         }
242 }