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"
29 #include "adt/error.h"
31 static const scope_t *global_scope;
34 static void write_type(const type_t *type);
36 static const char *get_atomic_type_string(const atomic_type_kind_t type)
39 case ATOMIC_TYPE_VOID: return "void";
40 case ATOMIC_TYPE_CHAR: return "byte";
41 case ATOMIC_TYPE_SCHAR: return "byte";
42 case ATOMIC_TYPE_UCHAR: return "unsigned byte";
43 case ATOMIC_TYPE_SHORT: return "short";
44 case ATOMIC_TYPE_USHORT: return "unsigned short";
45 case ATOMIC_TYPE_INT: return "int";
46 case ATOMIC_TYPE_UINT: return "unsigned int";
47 case ATOMIC_TYPE_LONG: return "int";
48 case ATOMIC_TYPE_ULONG: return "unsigned int";
49 case ATOMIC_TYPE_LONGLONG: return "long";
50 case ATOMIC_TYPE_ULONGLONG: return "unsigned long";
51 case ATOMIC_TYPE_FLOAT: return "float";
52 case ATOMIC_TYPE_DOUBLE: return "double";
53 case ATOMIC_TYPE_LONG_DOUBLE: return "double";
54 case ATOMIC_TYPE_BOOL: return "bool";
55 default: panic("unsupported atomic type");
59 static void write_atomic_type(const atomic_type_t *type)
61 fprintf(out, "%s", get_atomic_type_string(type->akind));
64 static void write_pointer_type(const pointer_type_t *type)
66 write_type(type->points_to);
70 static declaration_t *find_typedef(const type_t *type)
72 /* first: search for a matching typedef in the global type... */
73 declaration_t *declaration = global_scope->declarations;
74 while(declaration != NULL) {
75 if(! (declaration->storage_class == STORAGE_CLASS_TYPEDEF)) {
76 declaration = declaration->next;
79 if(declaration->type == type)
81 declaration = declaration->next;
87 static void write_compound_type(const compound_type_t *type)
89 declaration_t *declaration = find_typedef((const type_t*) type);
90 if(declaration != NULL) {
91 fprintf(out, "%s", declaration->symbol->string);
95 /* does the struct have a name? */
96 symbol_t *symbol = type->declaration->symbol;
98 /* TODO: make sure we create a struct for it... */
99 fprintf(out, "%s", symbol->string);
102 /* TODO: create a struct and use its name here... */
103 fprintf(out, "/* TODO anonymous struct */byte");
106 static void write_enum_type(const enum_type_t *type)
108 declaration_t *declaration = find_typedef((const type_t*) type);
109 if(declaration != NULL) {
110 fprintf(out, "%s", declaration->symbol->string);
114 /* does the enum have a name? */
115 symbol_t *symbol = type->declaration->symbol;
117 /* TODO: make sure we create an enum for it... */
118 fprintf(out, "%s", symbol->string);
121 /* TODO: create a struct and use its name here... */
122 fprintf(out, "/* TODO anonymous enum */byte");
125 static void write_function_type(const function_type_t *type)
127 fprintf(out, "(func(");
129 function_parameter_t *parameter = type->parameters;
131 while(parameter != NULL) {
139 if(parameter->symbol != NULL) {
140 fprintf(out, "%s : ", parameter->symbol->string);
142 /* TODO make up some unused names (or allow _ in fluffy?) */
143 fprintf(out, "_ : ");
147 write_type(parameter->type);
149 parameter = parameter->next;
152 fprintf(out, ") : ");
153 write_type(type->return_type);
157 static void write_type(const type_t *type)
161 write_atomic_type(&type->atomic);
164 write_pointer_type(&type->pointer);
166 case TYPE_COMPOUND_UNION:
167 case TYPE_COMPOUND_STRUCT:
168 write_compound_type(&type->compound);
171 write_enum_type(&type->enumt);
174 write_function_type(&type->function);
177 panic("invalid type found");
180 fprintf(out, "/* TODO type */");
185 static void write_struct_entry(const declaration_t *declaration)
187 fprintf(out, "\t%s : ", declaration->symbol->string);
188 write_type(declaration->type);
192 static void write_struct(const symbol_t *symbol, const compound_type_t *type)
194 fprintf(out, "struct %s:\n", symbol->string);
196 const declaration_t *declaration = type->declaration->scope.declarations;
197 while(declaration != NULL) {
198 write_struct_entry(declaration);
199 declaration = declaration->next;
205 static void write_union(const symbol_t *symbol, const compound_type_t *type)
207 fprintf(out, "union %s:\n", symbol->string);
209 const declaration_t *declaration = type->declaration->scope.declarations;
210 while(declaration != NULL) {
211 write_struct_entry(declaration);
212 declaration = declaration->next;
218 static void write_expression(const expression_t *expression);
220 static void write_unary_expression(const unary_expression_t *expression)
222 switch(expression->base.kind) {
223 case EXPR_UNARY_NEGATE:
230 panic("unimeplemented unary expression found");
232 write_expression(expression->value);
235 static void write_expression(const expression_t *expression)
237 const const_expression_t *constant;
239 switch(expression->kind) {
241 constant = &expression->conste;
242 if(is_type_integer(expression->base.type)) {
243 fprintf(out, "%lld", constant->v.int_value);
245 fprintf(out, "%Lf", constant->v.float_value);
249 write_unary_expression((const unary_expression_t*) expression);
252 panic("not implemented expression");
256 static void write_enum(const symbol_t *symbol, const enum_type_t *type)
258 fprintf(out, "enum %s:\n", symbol->string);
260 declaration_t *entry = type->declaration->next;
261 for ( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
262 entry = entry->next) {
263 fprintf(out, "\t%s", entry->symbol->string);
264 if(entry->init.initializer != NULL) {
265 fprintf(out, " <- ");
266 write_expression(entry->init.enum_value);
270 fprintf(out, "typealias %s <- int\n", symbol->string);
274 static void write_variable(const declaration_t *declaration)
276 fprintf(out, "var %s : ", declaration->symbol->string);
277 write_type(declaration->type);
278 /* TODO: initializers */
282 static void write_function(const declaration_t *declaration)
284 if(declaration->init.statement != NULL) {
285 fprintf(stderr, "Warning: can't convert function bodies (at %s)\n",
286 declaration->symbol->string);
289 fprintf(out, "func extern %s(",
290 declaration->symbol->string);
292 const function_type_t *function_type
293 = (const function_type_t*) declaration->type;
295 declaration_t *parameter = declaration->scope.declarations;
297 for( ; parameter != NULL; parameter = parameter->next) {
303 if(parameter->symbol != NULL) {
304 fprintf(out, "%s : ", parameter->symbol->string);
308 write_type(parameter->type);
310 if(function_type->variadic) {
320 const type_t *return_type = function_type->return_type;
321 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
323 write_type(return_type);
328 void write_fluffy_decls(FILE *output, const translation_unit_t *unit)
331 global_scope = &unit->scope;
334 fprintf(out, "/* WARNING: Automatically generated file */\n");
336 /* write structs,unions + enums */
337 declaration_t *declaration = unit->scope.declarations;
338 for( ; declaration != NULL; declaration = declaration->next) {
339 //fprintf(out, "// Decl: %s\n", declaration->symbol->string);
340 if(! (declaration->storage_class == STORAGE_CLASS_TYPEDEF)) {
343 type_t *type = declaration->type;
344 if(type->kind == TYPE_COMPOUND_STRUCT) {
345 write_struct(declaration->symbol, &type->compound);
346 } else if(type->kind == TYPE_COMPOUND_UNION) {
347 write_union(declaration->symbol, &type->compound);
348 } else if(type->kind == TYPE_ENUM) {
349 write_enum(declaration->symbol, &type->enumt);
353 /* write global variables */
354 declaration = unit->scope.declarations;
355 for( ; declaration != NULL; declaration = declaration->next) {
356 if(declaration->namespc != NAMESPACE_NORMAL)
358 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF
359 || declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
362 type_t *type = declaration->type;
363 if(type->kind == TYPE_FUNCTION)
366 write_variable(declaration);
369 /* write functions */
370 declaration = unit->scope.declarations;
371 for( ; declaration != NULL; declaration = declaration->next) {
372 if(declaration->namespc != NAMESPACE_NORMAL)
374 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF
375 || declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
378 type_t *type = declaration->type;
379 if(type->kind != TYPE_FUNCTION)
382 write_function(declaration);