Do no include -Wswitch-default in -Wall.
[cparser] / write_fluffy.c
1 #include <config.h>
2
3 #include <errno.h>
4 #include <string.h>
5
6 #include "write_fluffy.h"
7 #include "ast_t.h"
8 #include "type_t.h"
9 #include "type.h"
10 #include "adt/error.h"
11
12 static const scope_t *global_scope;
13 static FILE          *out;
14
15 static void write_type(const type_t *type);
16
17 static const char *get_atomic_type_string(const atomic_type_kind_t type)
18 {
19         switch(type) {
20         case ATOMIC_TYPE_VOID:        return "void";
21         case ATOMIC_TYPE_CHAR:        return "byte";
22         case ATOMIC_TYPE_SCHAR:       return "byte";
23         case ATOMIC_TYPE_UCHAR:       return "unsigned byte";
24         case ATOMIC_TYPE_SHORT:       return "short";
25         case ATOMIC_TYPE_USHORT:      return "unsigned short";
26         case ATOMIC_TYPE_INT:         return "int";
27         case ATOMIC_TYPE_UINT:        return "unsigned int";
28         case ATOMIC_TYPE_LONG:        return "int";
29         case ATOMIC_TYPE_ULONG:       return "unsigned int";
30         case ATOMIC_TYPE_LONGLONG:    return "long";
31         case ATOMIC_TYPE_ULONGLONG:   return "unsigned long";
32         case ATOMIC_TYPE_FLOAT:       return "float";
33         case ATOMIC_TYPE_DOUBLE:      return "double";
34         case ATOMIC_TYPE_LONG_DOUBLE: return "double";
35         case ATOMIC_TYPE_BOOL:        return "bool";
36         default:                      panic("unsupported atomic type");
37         }
38 }
39
40 static void write_atomic_type(const atomic_type_t *type)
41 {
42         fprintf(out, "%s", get_atomic_type_string(type->akind));
43 }
44
45 static void write_pointer_type(const pointer_type_t *type)
46 {
47         write_type(type->points_to);
48         fputc('*', out);
49 }
50
51 static declaration_t *find_typedef(const type_t *type)
52 {
53         /* first: search for a matching typedef in the global type... */
54         declaration_t *declaration = global_scope->declarations;
55         while(declaration != NULL) {
56                 if(! (declaration->storage_class == STORAGE_CLASS_TYPEDEF)) {
57                         declaration = declaration->next;
58                         continue;
59                 }
60                 if(declaration->type == type)
61                         break;
62                 declaration = declaration->next;
63         }
64
65         return declaration;
66 }
67
68 static void write_compound_type(const compound_type_t *type)
69 {
70         declaration_t *declaration = find_typedef((const type_t*) type);
71         if(declaration != NULL) {
72                 fprintf(out, "%s", declaration->symbol->string);
73                 return;
74         }
75
76         /* does the struct have a name? */
77         symbol_t *symbol = type->declaration->symbol;
78         if(symbol != NULL) {
79                 /* TODO: make sure we create a struct for it... */
80                 fprintf(out, "%s", symbol->string);
81                 return;
82         }
83         /* TODO: create a struct and use its name here... */
84         fprintf(out, "/* TODO anonymous struct */byte");
85 }
86
87 static void write_enum_type(const enum_type_t *type)
88 {
89         declaration_t *declaration = find_typedef((const type_t*) type);
90         if(declaration != NULL) {
91                 fprintf(out, "%s", declaration->symbol->string);
92                 return;
93         }
94
95         /* does the enum have a name? */
96         symbol_t *symbol = type->declaration->symbol;
97         if(symbol != NULL) {
98                 /* TODO: make sure we create an enum for it... */
99                 fprintf(out, "%s", symbol->string);
100                 return;
101         }
102         /* TODO: create a struct and use its name here... */
103         fprintf(out, "/* TODO anonymous enum */byte");
104 }
105
106 static void write_function_type(const function_type_t *type)
107 {
108         fprintf(out, "(func(");
109
110         function_parameter_t *parameter = type->parameters;
111         int                   first     = 1;
112         while(parameter != NULL) {
113                 if(!first) {
114                         fprintf(out, ", ");
115                 } else {
116                         first = 0;
117                 }
118
119 #if 0
120                 if(parameter->symbol != NULL) {
121                         fprintf(out, "%s : ", parameter->symbol->string);
122                 } else {
123                         /* TODO make up some unused names (or allow _ in fluffy?) */
124                         fprintf(out, "_ : ");
125                 }
126 #endif
127                 fputs("_ : ", out);
128                 write_type(parameter->type);
129
130                 parameter = parameter->next;
131         }
132
133         fprintf(out, ") : ");
134         write_type(type->return_type);
135         fprintf(out, ")");
136 }
137
138 static void write_type(const type_t *type)
139 {
140         switch(type->kind) {
141         case TYPE_ATOMIC:
142                 write_atomic_type(&type->atomic);
143                 return;
144         case TYPE_POINTER:
145                 write_pointer_type(&type->pointer);
146                 return;
147         case TYPE_COMPOUND_UNION:
148         case TYPE_COMPOUND_STRUCT:
149                 write_compound_type(&type->compound);
150                 return;
151         case TYPE_ENUM:
152                 write_enum_type(&type->enumt);
153                 return;
154         case TYPE_FUNCTION:
155                 write_function_type(&type->function);
156                 return;
157         case TYPE_INVALID:
158                 panic("invalid type found");
159                 break;
160         default:
161                 fprintf(out, "/* TODO type */");
162                 break;
163         }
164 }
165
166 static void write_struct_entry(const declaration_t *declaration)
167 {
168         fprintf(out, "\t%s : ", declaration->symbol->string);
169         write_type(declaration->type);
170         fprintf(out, "\n");
171 }
172
173 static void write_struct(const symbol_t *symbol, const compound_type_t *type)
174 {
175         fprintf(out, "struct %s:\n", symbol->string);
176
177         const declaration_t *declaration = type->declaration->scope.declarations;
178         while(declaration != NULL) {
179                 write_struct_entry(declaration);
180                 declaration = declaration->next;
181         }
182
183         fprintf(out, "\n");
184 }
185
186 static void write_union(const symbol_t *symbol, const compound_type_t *type)
187 {
188         fprintf(out, "union %s:\n", symbol->string);
189
190         const declaration_t *declaration = type->declaration->scope.declarations;
191         while(declaration != NULL) {
192                 write_struct_entry(declaration);
193                 declaration = declaration->next;
194         }
195
196         fprintf(out, "\n");
197 }
198
199 static void write_expression(const expression_t *expression);
200
201 static void write_unary_expression(const unary_expression_t *expression)
202 {
203         switch(expression->expression.kind) {
204         case EXPR_UNARY_NEGATE:
205                 fputc('-', out);
206                 break;
207         case EXPR_UNARY_NOT:
208                 fputc('!', out);
209                 break;
210         default:
211                 panic("unimeplemented unary expression found");
212         }
213         write_expression(expression->value);
214 }
215
216 static void write_expression(const expression_t *expression)
217 {
218         const const_expression_t *constant;
219         /* TODO */
220         switch(expression->kind) {
221         case EXPR_CONST:
222                 constant = &expression->conste;
223                 if(is_type_integer(expression->base.datatype)) {
224                         fprintf(out, "%lld", constant->v.int_value);
225                 } else {
226                         fprintf(out, "%Lf", constant->v.float_value);
227                 }
228                 break;
229         EXPR_UNARY_CASES
230                 write_unary_expression((const unary_expression_t*) expression);
231                 break;
232         default:
233                 panic("not implemented expression");
234         }
235 }
236
237 static void write_enum(const symbol_t *symbol, const enum_type_t *type)
238 {
239         fprintf(out, "enum %s:\n", symbol->string);
240
241         declaration_t *entry = type->declaration->next;
242         for ( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
243                         entry = entry->next) {
244                 fprintf(out, "\t%s", entry->symbol->string);
245                 if(entry->init.initializer != NULL) {
246                         fprintf(out, " <- ");
247                         write_expression(entry->init.enum_value);
248                 }
249                 fputc('\n', out);
250         }
251         fprintf(out, "typealias %s <- int\n", symbol->string);
252         fprintf(out, "\n");
253 }
254
255 static void write_variable(const declaration_t *declaration)
256 {
257         fprintf(out, "var %s : ", declaration->symbol->string);
258         write_type(declaration->type);
259         /* TODO: initializers */
260         fprintf(out, "\n");
261 }
262
263 static void write_function(const declaration_t *declaration)
264 {
265         if(declaration->init.statement != NULL) {
266                 fprintf(stderr, "Warning: can't convert function bodies (at %s)\n",
267                         declaration->symbol->string);
268         }
269
270         fprintf(out, "func extern %s(",
271                 declaration->symbol->string);
272
273         const function_type_t *function_type
274                 = (const function_type_t*) declaration->type;
275
276         declaration_t *parameter = declaration->scope.declarations;
277         int            first     = 1;
278         for( ; parameter != NULL; parameter = parameter->next) {
279                 if(!first) {
280                         fprintf(out, ", ");
281                 } else {
282                         first = 0;
283                 }
284                 if(parameter->symbol != NULL) {
285                         fprintf(out, "%s : ", parameter->symbol->string);
286                 } else {
287                         fputs("_ : ", out);
288                 }
289                 write_type(parameter->type);
290         }
291         if(function_type->variadic) {
292                 if(!first) {
293                         fprintf(out, ", ");
294                 } else {
295                         first = 0;
296                 }
297                 fputs("...", out);
298         }
299         fprintf(out, ")");
300
301         const type_t *return_type = function_type->return_type;
302         if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
303                 fprintf(out, " : ");
304                 write_type(return_type);
305         }
306         fputc('\n', out);
307 }
308
309 void write_fluffy_decls(FILE *output, const translation_unit_t *unit)
310 {
311         out            = output;
312         global_scope = &unit->scope;
313
314         ast_set_output(out);
315         fprintf(out, "/* WARNING: Automatically generated file */\n");
316
317         /* write structs,unions + enums */
318         declaration_t *declaration = unit->scope.declarations;
319         for( ; declaration != NULL; declaration = declaration->next) {
320                 //fprintf(out, "// Decl: %s\n", declaration->symbol->string);
321                 if(! (declaration->storage_class == STORAGE_CLASS_TYPEDEF)) {
322                         continue;
323                 }
324                 type_t *type = declaration->type;
325                 if(type->kind == TYPE_COMPOUND_STRUCT) {
326                         write_struct(declaration->symbol, &type->compound);
327                 } else if(type->kind == TYPE_COMPOUND_UNION) {
328                         write_union(declaration->symbol, &type->compound);
329                 } else if(type->kind == TYPE_ENUM) {
330                         write_enum(declaration->symbol, &type->enumt);
331                 }
332         }
333
334         /* write global variables */
335         declaration = unit->scope.declarations;
336         for( ; declaration != NULL; declaration = declaration->next) {
337                 if(declaration->namespc != NAMESPACE_NORMAL)
338                         continue;
339                 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF
340                                 || declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
341                         continue;
342
343                 type_t *type = declaration->type;
344                 if(type->kind == TYPE_FUNCTION)
345                         continue;
346
347                 write_variable(declaration);
348         }
349
350         /* write functions */
351         declaration = unit->scope.declarations;
352         for( ; declaration != NULL; declaration = declaration->next) {
353                 if(declaration->namespc != NAMESPACE_NORMAL)
354                         continue;
355                 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF
356                                 || declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
357                         continue;
358
359                 type_t *type = declaration->type;
360                 if(type->kind != TYPE_FUNCTION)
361                         continue;
362
363                 write_function(declaration);
364         }
365 }