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");
181 fprintf(out, "/* TODO type */");
186 static void write_struct_entry(const declaration_t *declaration)
188 fprintf(out, "\t%s : ", declaration->symbol->string);
189 write_type(declaration->type);
193 static void write_struct(const symbol_t *symbol, const compound_type_t *type)
195 fprintf(out, "struct %s:\n", symbol->string);
197 const declaration_t *declaration = type->declaration->scope.declarations;
198 while(declaration != NULL) {
199 write_struct_entry(declaration);
200 declaration = declaration->next;
206 static void write_union(const symbol_t *symbol, const compound_type_t *type)
208 fprintf(out, "union %s:\n", symbol->string);
210 const declaration_t *declaration = type->declaration->scope.declarations;
211 while(declaration != NULL) {
212 write_struct_entry(declaration);
213 declaration = declaration->next;
219 static void write_expression(const expression_t *expression);
221 static void write_unary_expression(const unary_expression_t *expression)
223 switch(expression->base.kind) {
224 case EXPR_UNARY_NEGATE:
231 panic("unimeplemented unary expression found");
233 write_expression(expression->value);
236 static void write_expression(const expression_t *expression)
238 const const_expression_t *constant;
240 switch(expression->kind) {
242 constant = &expression->conste;
243 if(is_type_integer(expression->base.type)) {
244 fprintf(out, "%lld", constant->v.int_value);
246 fprintf(out, "%Lf", constant->v.float_value);
250 write_unary_expression((const unary_expression_t*) expression);
253 panic("not implemented expression");
257 static void write_enum(const symbol_t *symbol, const enum_type_t *type)
259 fprintf(out, "enum %s:\n", symbol->string);
261 declaration_t *entry = type->declaration->next;
262 for ( ; entry != NULL && entry->storage_class == STORAGE_CLASS_ENUM_ENTRY;
263 entry = entry->next) {
264 fprintf(out, "\t%s", entry->symbol->string);
265 if(entry->init.initializer != NULL) {
266 fprintf(out, " <- ");
267 write_expression(entry->init.enum_value);
271 fprintf(out, "typealias %s <- int\n", symbol->string);
275 static void write_variable(const declaration_t *declaration)
277 fprintf(out, "var %s : ", declaration->symbol->string);
278 write_type(declaration->type);
279 /* TODO: initializers */
283 static void write_function(const declaration_t *declaration)
285 if(declaration->init.statement != NULL) {
286 fprintf(stderr, "Warning: can't convert function bodies (at %s)\n",
287 declaration->symbol->string);
290 fprintf(out, "func extern %s(",
291 declaration->symbol->string);
293 const function_type_t *function_type
294 = (const function_type_t*) declaration->type;
296 declaration_t *parameter = declaration->scope.declarations;
298 for( ; parameter != NULL; parameter = parameter->next) {
304 if(parameter->symbol != NULL) {
305 fprintf(out, "%s : ", parameter->symbol->string);
309 write_type(parameter->type);
311 if(function_type->variadic) {
321 const type_t *return_type = function_type->return_type;
322 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
324 write_type(return_type);
329 void write_fluffy_decls(FILE *output, const translation_unit_t *unit)
332 global_scope = &unit->scope;
335 fprintf(out, "/* WARNING: Automatically generated file */\n");
337 /* write structs,unions + enums */
338 declaration_t *declaration = unit->scope.declarations;
339 for( ; declaration != NULL; declaration = declaration->next) {
340 //fprintf(out, "// Decl: %s\n", declaration->symbol->string);
341 if(! (declaration->storage_class == STORAGE_CLASS_TYPEDEF)) {
344 type_t *type = declaration->type;
345 if(type->kind == TYPE_COMPOUND_STRUCT) {
346 write_struct(declaration->symbol, &type->compound);
347 } else if(type->kind == TYPE_COMPOUND_UNION) {
348 write_union(declaration->symbol, &type->compound);
349 } else if(type->kind == TYPE_ENUM) {
350 write_enum(declaration->symbol, &type->enumt);
354 /* write global variables */
355 declaration = unit->scope.declarations;
356 for( ; declaration != NULL; declaration = declaration->next) {
357 if(declaration->namespc != NAMESPACE_NORMAL)
359 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF
360 || declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
363 type_t *type = declaration->type;
364 if(type->kind == TYPE_FUNCTION)
367 write_variable(declaration);
370 /* write functions */
371 declaration = unit->scope.declarations;
372 for( ; declaration != NULL; declaration = declaration->next) {
373 if(declaration->namespc != NAMESPACE_NORMAL)
375 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF
376 || declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY)
379 type_t *type = declaration->type;
380 if(type->kind != TYPE_FUNCTION)
383 write_function(declaration);