2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 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"
31 #include "adt/error.h"
34 static const scope_t *global_scope;
37 static void write_type(const type_t *type);
39 static const char *get_atomic_type_string(const atomic_type_kind_t type)
42 case ATOMIC_TYPE_VOID: return "void";
43 case ATOMIC_TYPE_CHAR: return "byte";
44 case ATOMIC_TYPE_SCHAR: return "byte";
45 case ATOMIC_TYPE_UCHAR: return "unsigned byte";
46 case ATOMIC_TYPE_SHORT: return "short";
47 case ATOMIC_TYPE_USHORT: return "unsigned short";
48 case ATOMIC_TYPE_INT: return "int";
49 case ATOMIC_TYPE_UINT: return "unsigned int";
50 case ATOMIC_TYPE_LONG: return "int";
51 case ATOMIC_TYPE_ULONG: return "unsigned int";
52 case ATOMIC_TYPE_LONGLONG: return "long";
53 case ATOMIC_TYPE_ULONGLONG: return "unsigned long";
54 case ATOMIC_TYPE_FLOAT: return "float";
55 case ATOMIC_TYPE_DOUBLE: return "double";
56 case ATOMIC_TYPE_LONG_DOUBLE: return "double";
57 case ATOMIC_TYPE_BOOL: return "bool";
58 default: panic("unsupported atomic type");
62 static void write_atomic_type(const atomic_type_t *type)
64 fprintf(out, "%s", get_atomic_type_string(type->akind));
67 static void write_pointer_type(const pointer_type_t *type)
69 write_type(type->points_to);
73 static entity_t *find_typedef(const type_t *type)
75 /* first: search for a matching typedef in the global type... */
76 entity_t *entity = global_scope->entities;
77 for ( ; entity != NULL; entity = entity->base.next) {
78 if (entity->kind != ENTITY_TYPEDEF)
80 if (entity->typedefe.type == type)
87 static void write_compound_type(const compound_type_t *type)
89 entity_t *entity = find_typedef((const type_t*) type);
91 fprintf(out, "%s", entity->base.symbol->string);
95 /* does the struct have a name? */
96 symbol_t *symbol = type->compound->base.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 entity_t *entity = find_typedef((const type_t*) type);
109 if (entity != NULL) {
110 fprintf(out, "%s", entity->base.symbol->string);
114 /* does the enum have a name? */
115 symbol_t *symbol = type->enume->base.symbol;
116 if (symbol != NULL) {
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);
179 fprintf(out, "/* TODO type */");
184 static void write_compound_entry(const entity_t *entity)
186 fprintf(out, "\t%s : ", entity->base.symbol->string);
187 write_type(entity->declaration.type);
191 static void write_compound(const symbol_t *symbol, const compound_type_t *type)
193 fprintf(out, "%s %s:\n",
194 type->base.kind == TYPE_COMPOUND_STRUCT ? "struct" : "union",
197 const entity_t *entity = type->compound->members.entities;
198 for ( ; entity != NULL; entity = entity->base.next) {
199 write_compound_entry(entity);
205 static void write_expression(const expression_t *expression);
207 static void write_unary_expression(const unary_expression_t *expression)
209 switch(expression->base.kind) {
210 case EXPR_UNARY_NEGATE:
217 panic("unimeplemented unary expression found");
219 write_expression(expression->value);
222 static void write_expression(const expression_t *expression)
224 switch(expression->kind) {
225 case EXPR_LITERAL_INTEGER:
226 fprintf(out, "%s", expression->literal.value.begin);
228 case EXPR_UNARY_CASES:
229 write_unary_expression((const unary_expression_t*) expression);
232 panic("not implemented expression");
236 static void write_enum(const symbol_t *symbol, const enum_type_t *type)
238 fprintf(out, "enum %s:\n", symbol->string);
240 entity_t *entry = type->enume->base.next;
241 for ( ; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
242 entry = entry->base.next) {
243 fprintf(out, "\t%s", entry->base.symbol->string);
244 if(entry->enum_value.value != NULL) {
245 fprintf(out, " <- ");
246 write_expression(entry->enum_value.value);
250 fprintf(out, "typealias %s <- int\n", symbol->string);
254 static void write_variable(const entity_t *entity)
256 fprintf(out, "var %s : ", entity->base.symbol->string);
257 write_type(entity->declaration.type);
261 static void write_function(const entity_t *entity)
263 if (entity->function.body != NULL) {
264 fprintf(stderr, "Warning: can't convert function bodies (at %s)\n",
265 entity->base.symbol->string);
268 fprintf(out, "func extern %s(", entity->base.symbol->string);
270 const function_type_t *function_type
271 = (const function_type_t*) entity->declaration.type;
273 entity_t *parameter = entity->function.parameters.entities;
275 for( ; parameter != NULL; parameter = parameter->base.next) {
276 assert(parameter->kind == ENTITY_PARAMETER);
282 if(parameter->base.symbol != NULL) {
283 fprintf(out, "%s : ", parameter->base.symbol->string);
287 write_type(parameter->declaration.type);
289 if(function_type->variadic) {
299 const type_t *return_type = skip_typeref(function_type->return_type);
300 if (!is_type_void(return_type)) {
302 write_type(return_type);
307 void write_fluffy_decls(FILE *output, const translation_unit_t *unit)
310 global_scope = &unit->scope;
313 fprintf(out, "/* WARNING: Automatically generated file */\n");
315 /* write structs,unions + enums */
316 entity_t *entity = unit->scope.entities;
317 for( ; entity != NULL; entity = entity->base.next) {
318 if (entity->kind != ENTITY_TYPEDEF)
321 type_t *type = entity->typedefe.type;
322 if (is_type_compound(type)) {
323 write_compound(entity->base.symbol, &type->compound);
324 } else if(type->kind == TYPE_ENUM) {
325 write_enum(entity->base.symbol, &type->enumt);
329 /* write global variables */
330 entity = unit->scope.entities;
331 for( ; entity != NULL; entity = entity->base.next) {
332 if (entity->kind != ENTITY_VARIABLE)
335 write_variable(entity);
338 /* write functions */
339 entity = unit->scope.entities;
340 for( ; entity != NULL; entity = entity->base.next) {
341 if (entity->kind != ENTITY_FUNCTION)
344 write_function(entity);