improve parsing of function pointers
[cparser] / write_fluffy.c
1 #include <config.h>
2
3 #include <errno.h>
4 #include <string.h>
5
6 #include "ast_t.h"
7 #include "type_t.h"
8 #include "adt/error.h"
9
10 static const context_t *global_context;
11 static FILE            *out;
12
13 static void write_type(const type_t *type);
14
15 static const char *get_atomic_type_string(const atomic_type_type_t type)
16 {
17         switch(type) {
18         case ATOMIC_TYPE_VOID:       return "void";
19         case ATOMIC_TYPE_CHAR:       return "byte";
20         case ATOMIC_TYPE_SCHAR:      return "byte";
21         case ATOMIC_TYPE_UCHAR:      return "unsigned byte";
22         case ATOMIC_TYPE_SHORT:      return "short";
23         case ATOMIC_TYPE_USHORT:     return "unsigned short";
24         case ATOMIC_TYPE_INT:        return "int";
25         case ATOMIC_TYPE_UINT:       return "unsigned int";
26         case ATOMIC_TYPE_LONG:       return "int";
27         case ATOMIC_TYPE_ULONG:      return "unsigned int";
28         case ATOMIC_TYPE_LONGLONG:   return "long";
29         case ATOMIC_TYPE_ULONGLONG:  return "unsigned long";
30         case ATOMIC_TYPE_FLOAT:      return "float";
31         case ATOMIC_TYPE_DOUBLE:     return "double";
32         case ATOMIC_TYPE_LONG_DOUBLE: return "double";
33         case ATOMIC_TYPE_BOOL:       return "bool";
34         default:                     panic("unsupported atomic type");
35         }
36 }
37
38 static void write_atomic_type(const atomic_type_t *type)
39 {
40         fprintf(out, "%s", get_atomic_type_string(type->atype));
41 }
42
43 static void write_pointer_type(const pointer_type_t *type)
44 {
45         write_type(type->points_to);
46         fputc('*', out);
47 }
48
49 static void write_compound_type(const compound_type_t *type)
50 {
51         /* first: search for a matching typedef in the global type... */
52         declaration_t *declaration = global_context->declarations;
53         while(declaration != NULL) {
54                 if(! (declaration->storage_class & STORAGE_CLASS_TYPEDEF)) {
55                         declaration = declaration->next;
56                         continue;
57                 }
58                 if(declaration->type == (type_t*) type)
59                         break;
60                 declaration = declaration->next;
61         }
62
63         if(declaration != NULL) {
64                 fprintf(out, "%s", declaration->symbol->string);
65                 return;
66         }
67
68         /* does the struct have a name? */
69         if(type->symbol != NULL) {
70                 /* TODO: make sure we create a struct for it... */
71                 fprintf(out, "%s", type->symbol->string);
72                 return;
73         }
74         /* TODO: create a struct and use its name here... */
75         fprintf(out, "/* TODO anonymous struct */byte");
76 }
77
78 static void write_method_type(const method_type_t *type)
79 {
80         fprintf(out, "(func(");
81
82         method_parameter_type_t *parameter_type = type->parameter_types;
83         int                      first          = 1;
84         while(parameter_type != NULL) {
85                 if(!first) {
86                         fprintf(out, ", ");
87                 } else {
88                         first = 0;
89                 }
90
91                 if(parameter_type->symbol != NULL) {
92                         fprintf(out, "%s : ", parameter_type->symbol->string);
93                 } else {
94                         /* TODO make up some unused names (or allow _ in fluffy?) */
95                         fprintf(out, "_ : ");
96                 }
97                 write_type(parameter_type->type);
98
99                 parameter_type = parameter_type->next;
100         }
101
102         fprintf(out, ") : ");
103         write_type(type->result_type);
104         fprintf(out, ")");
105 }
106
107 static void write_type(const type_t *type)
108 {
109         switch(type->type) {
110         case TYPE_ATOMIC:
111                 write_atomic_type((const atomic_type_t*) type);
112                 return;
113         case TYPE_POINTER:
114                 write_pointer_type((const pointer_type_t*) type);
115                 return;
116         case TYPE_COMPOUND_UNION:
117         case TYPE_COMPOUND_STRUCT:
118                 write_compound_type((const compound_type_t*) type);
119                 return;
120         case TYPE_METHOD:
121                 write_method_type((const method_type_t*) type);
122                 return;
123         case TYPE_INVALID:
124                 panic("invalid type found");
125                 break;
126         default:
127                 fprintf(out, "/* TODO type */");
128                 break;
129         }
130 }
131
132 static void write_struct_entry(const declaration_t *declaration)
133 {
134         fprintf(out, "\t%s : ", declaration->symbol->string);
135         write_type(declaration->type);
136         fprintf(out, "\n");
137 }
138
139 static void write_struct(const symbol_t *symbol, const compound_type_t *type)
140 {
141         (void) type;
142         fprintf(out, "struct %s:\n", symbol->string);
143
144         const declaration_t *declaration = type->context.declarations;
145         while(declaration != NULL) {
146                 write_struct_entry(declaration);
147                 declaration = declaration->next;
148         }
149
150         fprintf(out, "\n");
151 }
152
153 static void write_variable(const declaration_t *declaration)
154 {
155         fprintf(out, "var %s : ", declaration->symbol->string);
156         write_type(declaration->type);
157         /* TODO: initializers */
158         fprintf(out, "\n");
159 }
160
161 static void write_function(const declaration_t *declaration)
162 {
163         if(declaration->statement != NULL) {
164                 fprintf(stderr, "Warning: can't convert function bodies (at %s)\n",
165                         declaration->symbol->string);
166         }
167
168         fprintf(out, "func extern %s(",
169                 declaration->symbol->string);
170
171         declaration_t *parameter = declaration->context.declarations;
172         int            first     = 1;
173         for( ; parameter != NULL; parameter = parameter->next) {
174                 if(!first) {
175                         fprintf(out, ", ");
176                 } else {
177                         first = 0;
178                 }
179                 if(parameter->symbol != NULL) {
180                         fprintf(out, "%s : ", parameter->symbol->string);
181                 } else {
182                         fputs("_ : ", out);
183                 }
184                 write_type(parameter->type);
185         }
186         fprintf(out, ")");
187
188         const method_type_t *method_type = (const method_type_t*) declaration->type;
189         const type_t        *result_type = method_type->result_type;
190         if(result_type->type != TYPE_ATOMIC ||
191                         ((const atomic_type_t*) result_type)->atype != ATOMIC_TYPE_VOID) {
192                 fprintf(out, " : ");
193                 write_type(result_type);
194         }
195         fputc('\n', out);
196 }
197
198 void write_fluffy_decls(const translation_unit_t *unit)
199 {
200 #if 0
201         out = fopen("out.fluffy", "w");
202         if(out == NULL) {
203                 fprintf(stderr, "Couldn't open out.fluffy: %s\n", strerror(errno));
204                 exit(1);
205         }
206 #endif
207         out            = stdout;
208         global_context = &unit->context;
209
210         fprintf(out, "/* WARNING: Automatically generated file */\n");
211
212         /* write structs + enums */
213         declaration_t *declaration = unit->context.declarations;
214         while(declaration != NULL) {
215                 //fprintf(out, "// Decl: %s\n", declaration->symbol->string);
216                 if(! (declaration->storage_class & STORAGE_CLASS_TYPEDEF)) {
217                         declaration = declaration->next;
218                         continue;
219                 }
220                 type_t *type = declaration->type;
221                 if(type->type == TYPE_COMPOUND_STRUCT) {
222                         write_struct(declaration->symbol, (compound_type_t*) type);
223                 } else if(type->type == TYPE_COMPOUND_UNION) {
224                         /* TODO */
225                 }
226
227                 declaration = declaration->next;
228         }
229
230         /* write global variables */
231         declaration = unit->context.declarations;
232         for( ; declaration != NULL; declaration = declaration->next) {
233                 if(declaration->storage_class & STORAGE_CLASS_TYPEDEF)
234                         continue;
235
236                 type_t *type = declaration->type;
237                 if(type->type == TYPE_METHOD)
238                         continue;
239
240                 write_variable(declaration);
241         }
242
243         /* write functions */
244         declaration = unit->context.declarations;
245         for( ; declaration != NULL; declaration = declaration->next) {
246                 if(declaration->storage_class & STORAGE_CLASS_TYPEDEF)
247                         continue;
248
249                 type_t *type = declaration->type;
250                 if(type->type != TYPE_METHOD)
251                         continue;
252
253                 write_function(declaration);
254         }
255
256         //fclose(out);
257 }