2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
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.
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.
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
25 #include "write_fluffy.h"
30 #include "adt/error.h"
32 static const scope_t *global_scope;
35 static void write_type(const type_t *type);
37 static const char *get_atomic_type_string(const atomic_type_kind_t type)
40 case ATOMIC_TYPE_VOID: return "void";
41 case ATOMIC_TYPE_CHAR: return "byte";
42 case ATOMIC_TYPE_SCHAR: return "byte";
43 case ATOMIC_TYPE_UCHAR: return "unsigned byte";
44 case ATOMIC_TYPE_SHORT: return "short";
45 case ATOMIC_TYPE_USHORT: return "unsigned short";
46 case ATOMIC_TYPE_INT: return "int";
47 case ATOMIC_TYPE_UINT: return "unsigned int";
48 case ATOMIC_TYPE_LONG: return "int";
49 case ATOMIC_TYPE_ULONG: return "unsigned int";
50 case ATOMIC_TYPE_LONGLONG: return "long";
51 case ATOMIC_TYPE_ULONGLONG: return "unsigned long";
52 case ATOMIC_TYPE_FLOAT: return "float";
53 case ATOMIC_TYPE_DOUBLE: return "double";
54 case ATOMIC_TYPE_LONG_DOUBLE: return "double";
55 case ATOMIC_TYPE_BOOL: return "bool";
56 default: panic("unsupported atomic type");
60 static void write_atomic_type(const atomic_type_t *type)
62 fprintf(out, "%s", get_atomic_type_string(type->akind));
65 static void write_pointer_type(const pointer_type_t *type)
67 write_type(type->points_to);
71 static declaration_t *find_typedef(const type_t *type)
73 /* first: search for a matching typedef in the global type... */
74 declaration_t *declaration = global_scope->declarations;
75 while(declaration != NULL) {
76 if(! (declaration->storage_class == STORAGE_CLASS_TYPEDEF)) {
77 declaration = declaration->next;
80 if(declaration->type == type)
82 declaration = declaration->next;
88 static void write_compound_type(const compound_type_t *type)
90 declaration_t *declaration = find_typedef((const type_t*) type);
91 if(declaration != NULL) {
92 fprintf(out, "%s", declaration->symbol->string);
96 /* does the struct have a name? */
97 symbol_t *symbol = type->declaration->symbol;
99 /* TODO: make sure we create a struct for it... */
100 fprintf(out, "%s", symbol->string);
103 /* TODO: create a struct and use its name here... */
104 fprintf(out, "/* TODO anonymous struct */byte");
107 static void write_enum_type(const enum_type_t *type)
109 declaration_t *declaration = find_typedef((const type_t*) type);
110 if(declaration != NULL) {
111 fprintf(out, "%s", declaration->symbol->string);
115 /* does the enum have a name? */
116 symbol_t *symbol = type->declaration->symbol;
118 /* TODO: make sure we create an enum for it... */
119 fprintf(out, "%s", symbol->string);
122 /* TODO: create a struct and use its name here... */
123 fprintf(out, "/* TODO anonymous enum */byte");
126 static void write_function_type(const function_type_t *type)
128 fprintf(out, "(func(");
130 function_parameter_t *parameter = type->parameters;
132 while(parameter != NULL) {
140 if(parameter->symbol != NULL) {
141 fprintf(out, "%s : ", parameter->symbol->string);
143 /* TODO make up some unused names (or allow _ in fluffy?) */
144 fprintf(out, "_ : ");
148 write_type(parameter->type);
150 parameter = parameter->next;
153 fprintf(out, ") : ");
154 write_type(type->return_type);
158 static void write_type(const type_t *type)
162 write_atomic_type(&type->atomic);
165 write_pointer_type(&type->pointer);
167 case TYPE_COMPOUND_UNION:
168 case TYPE_COMPOUND_STRUCT:
169 write_compound_type(&type->compound);
172 write_enum_type(&type->enumt);
175 write_function_type(&type->function);
178 panic("invalid type found");
183 fprintf(out, "/* TODO type */");
188 static void write_struct_entry(const declaration_t *declaration)
190 fprintf(out, "\t%s : ", declaration->symbol->string);
191 write_type(declaration->type);
195 static void write_struct(const symbol_t *symbol, const compound_type_t *type)
197 fprintf(out, "struct %s:\n", symbol->string);
199 const declaration_t *declaration = type->declaration->scope.declarations;
200 while(declaration != NULL) {
201 write_struct_entry(declaration);
202 declaration = declaration->next;
208 static void write_union(const symbol_t *symbol, const compound_type_t *type)
210 fprintf(out, "union %s:\n", symbol->string);
212 const declaration_t *declaration = type->declaration->scope.declarations;
213 while(declaration != NULL) {
214 write_struct_entry(declaration);
215 declaration = declaration->next;
221 static void write_expression(const expression_t *expression);
223 static void write_unary_expression(const unary_expression_t *expression)
225 switch(expression->base.kind) {
226 case EXPR_UNARY_NEGATE:
233 panic("unimeplemented unary expression found");
235 write_expression(expression->value);
238 static void write_expression(const expression_t *expression)
240 const const_expression_t *constant;
242 switch(expression->kind) {
244 constant = &expression->conste;
245 if(is_type_integer(expression->base.type)) {
246 fprintf(out, "%lld", constant->v.int_value);
248 fprintf(out, "%Lf", constant->v.float_value);
252 write_unary_expression((const unary_expression_t*) expression);
255 panic("not implemented expression");
259 static void write_enum(const symbol_t *symbol, const enum_type_t *type)
261 fprintf(out, "enum %s:\n", symbol->string);
263 declaration_t *entry = type->declaration->next;
264 for ( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
265 entry = entry->next) {
266 fprintf(out, "\t%s", entry->symbol->string);
267 if(entry->init.initializer != NULL) {
268 fprintf(out, " <- ");
269 write_expression(entry->init.enum_value);
273 fprintf(out, "typealias %s <- int\n", symbol->string);
277 static void write_variable(const declaration_t *declaration)
279 fprintf(out, "var %s : ", declaration->symbol->string);
280 write_type(declaration->type);
281 /* TODO: initializers */
285 static void write_function(const declaration_t *declaration)
287 if(declaration->init.statement != NULL) {
288 fprintf(stderr, "Warning: can't convert function bodies (at %s)\n",
289 declaration->symbol->string);
292 fprintf(out, "func extern %s(",
293 declaration->symbol->string);
295 const function_type_t *function_type
296 = (const function_type_t*) declaration->type;
298 declaration_t *parameter = declaration->scope.declarations;
300 for( ; parameter != NULL; parameter = parameter->next) {
306 if(parameter->symbol != NULL) {
307 fprintf(out, "%s : ", parameter->symbol->string);
311 write_type(parameter->type);
313 if(function_type->variadic) {
323 const type_t *return_type = function_type->return_type;
324 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
326 write_type(return_type);
331 void write_fluffy_decls(FILE *output, const translation_unit_t *unit)
334 global_scope = &unit->scope;
337 type_set_output(out);
338 fprintf(out, "/* WARNING: Automatically generated file */\n");
340 /* write structs,unions + enums */
341 declaration_t *declaration = unit->scope.declarations;
342 for( ; declaration != NULL; declaration = declaration->next) {
343 //fprintf(out, "// Decl: %s\n", declaration->symbol->string);
344 if(! (declaration->storage_class == STORAGE_CLASS_TYPEDEF)) {
347 type_t *type = declaration->type;
348 if(type->kind == TYPE_COMPOUND_STRUCT) {
349 write_struct(declaration->symbol, &type->compound);
350 } else if(type->kind == TYPE_COMPOUND_UNION) {
351 write_union(declaration->symbol, &type->compound);
352 } else if(type->kind == TYPE_ENUM) {
353 write_enum(declaration->symbol, &type->enumt);
357 /* write global variables */
358 declaration = unit->scope.declarations;
359 for( ; declaration != NULL; declaration = declaration->next) {
360 if(declaration->namespc != NAMESPACE_NORMAL)
362 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF
363 || declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
366 type_t *type = declaration->type;
367 if(type->kind == TYPE_FUNCTION)
370 write_variable(declaration);
373 /* write functions */
374 declaration = unit->scope.declarations;
375 for( ; declaration != NULL; declaration = declaration->next) {
376 if(declaration->namespc != NAMESPACE_NORMAL)
378 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF
379 || declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
382 type_t *type = declaration->type;
383 if(type->kind != TYPE_FUNCTION)
386 write_function(declaration);