add more ffi types; add typedefs
[m6w6/ext-psi] / idl / types.h
1
2 typedef struct decl_type {
3 text *name;
4 token_t type;
5 } decl_type;
6
7 static inline decl_type *init_decl_type(token_t type, text *name) {
8 decl_type *t = malloc(sizeof(*t));
9 t->type = type;
10 t->name = strdup(name);
11 return t;
12 }
13
14 static inline void free_decl_type(decl_type *type) {
15 free(type->name);
16 free(type);
17 }
18
19 typedef struct decl_typedef {
20 char *alias;
21 decl_type *type;
22 } decl_typedef;
23
24 static inline decl_typedef *init_decl_typedef(text *name, decl_type *type) {
25 decl_typedef *t = malloc(sizeof(*t));
26 t->alias = strdup(name);
27 t->type = type;
28 return t;
29 }
30
31 static inline void free_decl_typedef(decl_typedef *t) {
32 free(t->alias);
33 free_decl_type(t->type);
34 free(t);
35 }
36
37 typedef struct decl_typedefs {
38 size_t count;
39 decl_typedef **list;
40 } decl_typedefs;
41
42 static decl_typedefs *add_decl_typedef(decl_typedefs *defs, decl_typedef *def) {
43 if (!defs) {
44 defs = calloc(1, sizeof(*defs));
45 }
46 defs->list = realloc(defs->list, ++defs->count * sizeof(*defs->list));
47 defs->list[defs->count-1] = def;
48 return defs;
49 }
50
51 static void free_decl_typedefs(decl_typedefs *defs) {
52 size_t i;
53
54 for (i = 0; i < defs->count; ++i) {
55 free_decl_typedef(defs->list[i]);
56 }
57 free(defs->list);
58 free(defs);
59 }
60
61 typedef struct decl_var {
62 text *name;
63 unsigned pointer_level;
64 } decl_var;
65
66 static inline decl_var *init_decl_var(text *name, unsigned pl) {
67 decl_var *v = malloc(sizeof(*v));
68 v->name = (text *) strdup((const char *) name);
69 v->pointer_level = pl;
70 return v;
71 }
72
73 static inline void free_decl_var(decl_var *var) {
74 free(var->name);
75 free(var);
76 }
77
78 typedef struct decl_arg {
79 decl_type *type;
80 decl_var *var;
81 } decl_arg;
82
83 static inline decl_arg *init_decl_arg(decl_type *type, decl_var *var) {
84 decl_arg *arg = malloc(sizeof(*arg));
85 arg->type = type;
86 arg->var = var;
87 return arg;
88 }
89
90 static inline void free_decl_arg(decl_arg *arg) {
91 free_decl_type(arg->type);
92 free_decl_var(arg->var);
93 free(arg);
94 }
95
96 typedef struct decl_vars {
97 decl_var **vars;
98 size_t count;
99 } decl_vars;
100
101 static inline decl_vars *init_decl_vars(decl_var *var) {
102 decl_vars *vars = malloc(sizeof(*vars));
103 vars->count = 1;
104 vars->vars = malloc(sizeof(*vars->vars));
105 vars->vars[0] = var;
106 return vars;
107 }
108
109 static inline decl_vars *add_decl_var(decl_vars *vars, decl_var *var) {
110 vars->vars = realloc(vars->vars, ++vars->count * sizeof(*vars->vars));
111 vars->vars[vars->count-1] = var;
112 return vars;
113 }
114
115 static inline void free_decl_vars(decl_vars *vars) {
116 size_t i;
117
118 for (i = 0; i < vars->count; ++i) {
119 free_decl_var(vars->vars[i]);
120 }
121 free(vars->vars);
122 free(vars);
123 }
124
125 typedef struct decl_args {
126 decl_arg **args;
127 size_t count;
128 } decl_args;
129
130 static inline decl_args *init_decl_args(decl_arg *arg) {
131 decl_args *args = malloc(sizeof(*args));
132 args->count = 1;
133 args->args = malloc(sizeof(*args->args));
134 args->args[0] = arg;
135 return args;
136 }
137
138 static inline decl_args *add_decl_arg(decl_args *args, decl_arg *arg) {
139 args->args = realloc(args->args, ++args->count * sizeof(*args->args));
140 args->args[args->count-1] = arg;
141 return args;
142 }
143
144 static inline void free_decl_args(decl_args *args) {
145 size_t i;
146
147 for (i = 0; i < args->count; ++i) {
148 free_decl_arg(args->args[i]);
149 }
150 free(args->args);
151 free(args);
152 }
153
154 typedef struct decl {
155 decl_arg *func;
156 decl_args *args;
157 } decl;
158
159 static inline decl* init_decl(decl_arg *func, decl_args *args) {
160 decl *d = malloc(sizeof(*d));
161 d->func = func;
162 d->args = args;
163 return d;
164 }
165
166 static inline void free_decl(decl *d) {
167 free_decl_arg(d->func);
168 free_decl_args(d->args);
169 free(d);
170 }
171
172 typedef struct decls {
173 size_t count;
174 decl **list;
175 } decls;
176
177 static inline decls *add_decl(decls *decls, decl *decl) {
178 if (!decls) {
179 decls = calloc(1, sizeof(*decls));
180 }
181 decls->list = realloc(decls->list, ++decls->count * sizeof(*decls->list));
182 decls->list[decls->count-1] = decl;
183 return decls;
184 }
185
186 static inline void free_decls(decls *decls) {
187 size_t i;
188
189 for (i = 0; i < decls->count; ++i) {
190 free_decl(decls->list[i]);
191 }
192 free(decls->list);
193 free(decls);
194 }
195
196 typedef struct impl_type {
197 text *name;
198 token_t type;
199 } impl_type;
200
201 static inline impl_type *init_impl_type(token_t type, text *name) {
202 impl_type *t = malloc(sizeof(*t));
203
204 t->type = type;
205 t->name = (text *) strdup((const char *) name);
206 return t;
207 }
208
209 static inline void free_impl_type(impl_type *type) {
210 free(type->name);
211 free(type);
212 }
213
214 typedef struct impl_var {
215 text *name;
216 unsigned reference:1;
217 } impl_var;
218
219 static inline impl_var *init_impl_var(text *name, int is_reference) {
220 impl_var *var = malloc(sizeof(*var));
221 var->name = (text *) strdup((const char *) name);
222 var->reference = is_reference;
223 return var;
224 }
225
226 static inline void free_impl_var(impl_var *var) {
227 free(var->name);
228 free(var);
229 }
230
231 typedef struct impl_def_val {
232 token_t type;
233 union {
234 int64_t digits;
235 double decimals;
236 } v;
237 unsigned is_null:1;
238 } impl_def_val;
239
240 static inline impl_def_val *init_impl_def_val() {
241 impl_def_val *def = malloc(sizeof(*def));
242 def->type = 0;
243 def->is_null = 1;
244 return def;
245 }
246
247 static inline void free_impl_def_val(impl_def_val *def) {
248 free(def);
249 }
250
251 typedef struct impl_arg {
252 impl_type *type;
253 impl_var *var;
254 impl_def_val *def;
255 } impl_arg;
256
257 static inline impl_arg *init_impl_arg(impl_type *type, impl_var *var, impl_def_val *def) {
258 impl_arg *arg = malloc(sizeof(*arg));
259 arg->type = type;
260 arg->var = var;
261 arg->def = def;
262 return arg;
263 }
264
265 static inline void free_impl_arg(impl_arg *arg) {
266 free_impl_type(arg->type);
267 free_impl_var(arg->var);
268 if (arg->def) {
269 free_impl_def_val(arg->def);
270 }
271 free(arg);
272 }
273
274 typedef struct impl_args {
275 impl_arg **args;
276 size_t count;
277 } impl_args;
278
279 static inline impl_args *init_impl_args(impl_arg *arg) {
280 impl_args *args = malloc(sizeof(*args));
281 args->args = malloc(sizeof(*args->args));
282 if (arg) {
283 args->count = 1;
284 args->args[0] = arg;
285 } else {
286 args->count = 0;
287 args->args = NULL;
288 }
289 return args;
290 }
291
292 static inline impl_args *add_impl_arg(impl_args *args, impl_arg *arg) {
293 args->args = realloc(args->args, ++args->count * sizeof(*args->args));
294 args->args[args->count-1] = arg;
295 return args;
296 }
297
298 static inline void free_impl_args(impl_args *args) {
299 size_t i;
300
301 for (i = 0; i < args->count; ++i) {
302 free_impl_arg(args->args[i]);
303 }
304 free(args->args);
305 free(args);
306 }
307
308 typedef struct impl_func {
309 text *name;
310 impl_args *args;
311 impl_type *return_type;
312 } impl_func;
313
314 static inline impl_func *init_impl_func(text *name, impl_args *args, impl_type *type) {
315 impl_func *func = malloc(sizeof(*func));
316 func->name = strdup(name);
317 func->args = args ? args : init_impl_args(NULL);
318 func->return_type = type;
319 return func;
320 }
321
322 static inline void free_impl_func(impl_func *f) {
323 free_impl_type(f->return_type);
324 free_impl_args(f->args);
325 free(f->name);
326 free(f);
327 }
328
329 typedef struct let_func {
330 token_t type;
331 text *name;
332 } let_func;
333
334 static inline let_func *init_let_func(token_t type, text *name) {
335 let_func *func = malloc(sizeof(*func));
336 func->type = type;
337 func->name = (text *) strdup((const char *) name);
338 return func;
339 }
340
341 static inline void free_let_func(let_func *func) {
342 free(func->name);
343 free(func);
344 }
345
346 typedef struct let_value {
347 let_func *func;
348 impl_var *var;
349 unsigned null_pointer_ref:1;
350 } let_value;
351
352 static inline let_value *init_let_value(let_func *func, impl_var *var, int null_pointer_ref) {
353 let_value *val = malloc(sizeof(*val));
354 val->null_pointer_ref = null_pointer_ref;
355 val->func = func;
356 val->var = var;
357 return val;
358 }
359
360 static inline void free_let_value(let_value *val) {
361 if (val->func) {
362 free_let_func(val->func);
363 }
364 if (val->var) {
365 free_impl_var(val->var);
366 }
367 free(val);
368 }
369
370 typedef struct let_stmt {
371 decl_var *var;
372 let_value *val;
373 } let_stmt;
374
375 static inline let_stmt *init_let_stmt(decl_var *var, let_value *val) {
376 let_stmt *let = malloc(sizeof(*let));
377 let->var = var;
378 let->val = val;
379 return let;
380 }
381
382 static inline void free_let_stmt(let_stmt *stmt) {
383 free_decl_var(stmt->var);
384 free_let_value(stmt->val);
385 free(stmt);
386 }
387
388 typedef struct set_func {
389 token_t type;
390 text *name;
391 } set_func;
392
393 static inline set_func *init_set_func(token_t type, text *name) {
394 set_func *func = malloc(sizeof(*func));
395 func->type = type;
396 func->name = (text *) strdup((const char *) name);
397 return func;
398 }
399
400 static inline void free_set_func(set_func *func) {
401 free(func->name);
402 free(func);
403 }
404
405 typedef struct set_value {
406 set_func *func;
407 decl_vars *vars;
408 } set_value;
409
410 static inline set_value *init_set_value(set_func *func, decl_vars *vars) {
411 set_value *val = malloc(sizeof(*val));
412 val->func = func;
413 val->vars = vars;
414 return val;
415 }
416
417 static inline void free_set_value(set_value *val) {
418 free_set_func(val->func);
419 free_decl_vars(val->vars);
420 free(val);
421 }
422
423 typedef struct set_stmt {
424 impl_var *var;
425 set_value *val;
426 } set_stmt;
427
428 static inline set_stmt *init_set_stmt(impl_var *var, set_value *val) {
429 set_stmt *set = malloc(sizeof(*set));
430 set->var = var;
431 set->val = val;
432 return set;
433 }
434
435 static inline void free_set_stmt(set_stmt *set) {
436 free_impl_var(set->var);
437 free_set_value(set->val);
438 free(set);
439 }
440
441 typedef struct ret_stmt {
442 set_func *func;
443 decl_var *decl;
444 } ret_stmt;
445
446 static inline ret_stmt *init_ret_stmt(set_func *func, decl_var *decl) {
447 ret_stmt *ret = malloc(sizeof(*ret));
448 ret->func = func;
449 ret->decl = decl;
450 return ret;
451 }
452
453 static inline void free_ret_stmt(ret_stmt *ret) {
454 free_set_func(ret->func);
455 free_decl_var(ret->decl);
456 free(ret);
457 }
458
459 typedef struct impl_stmt {
460 token_t type;
461 union {
462 let_stmt *let;
463 set_stmt *set;
464 ret_stmt *ret;
465 void *ptr;
466 } s;
467 } impl_stmt;
468
469 static inline impl_stmt *init_impl_stmt(token_t type, void *ptr) {
470 impl_stmt *stmt = malloc(sizeof(*stmt));
471 stmt->type = type;
472 stmt->s.ptr = ptr;
473 return stmt;
474 }
475
476 static inline void free_impl_stmt(impl_stmt *stmt) {
477 switch (stmt->type) {
478 case PSI_T_LET:
479 free_let_stmt(stmt->s.let);
480 break;
481 case PSI_T_SET:
482 free_set_stmt(stmt->s.set);
483 break;
484 case PSI_T_RET:
485 free_ret_stmt(stmt->s.ret);
486 break;
487 }
488 free(stmt);
489 }
490
491 typedef struct impl_stmts {
492 impl_stmt **stmts;
493 size_t count;
494 } impl_stmts;
495
496 static inline impl_stmts *init_impl_stmts(impl_stmt *stmt) {
497 impl_stmts *stmts = malloc(sizeof(*stmts));
498 stmts->count = 1;
499 stmts->stmts = malloc(sizeof(*stmts->stmts));
500 stmts->stmts[0] = stmt;
501 return stmts;
502 }
503
504 static inline impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt) {
505 stmts->stmts = realloc(stmts->stmts, ++stmts->count * sizeof(*stmts->stmts));
506 stmts->stmts[stmts->count-1] = stmt;
507 return stmts;
508 }
509
510 static inline void free_impl_stmts(impl_stmts *stmts) {
511 size_t i;
512
513 for (i = 0; i < stmts->count; ++i) {
514 free_impl_stmt(stmts->stmts[i]);
515 }
516 free(stmts->stmts);
517 free(stmts);
518 }
519
520 typedef struct impl {
521 impl_func *func;
522 impl_stmts *stmts;
523 } impl;
524
525 static inline impl *init_impl(impl_func *func, impl_stmts *stmts) {
526 impl *i = malloc(sizeof(*i));
527 i->func = func;
528 i->stmts = stmts;
529 return i;
530 }
531
532 static inline void free_impl(impl *impl) {
533 free_impl_func(impl->func);
534 free_impl_stmts(impl->stmts);
535 free(impl);
536 }
537
538 typedef struct impls {
539 size_t count;
540 impl **list;
541 } impls;
542
543 static impls *add_impl(impls *impls, impl *impl) {
544 if (!impls) {
545 impls = calloc(1, sizeof(*impls));
546 }
547 impls->list = realloc(impls->list, ++impls->count * sizeof(*impls->list));
548 impls->list[impls->count-1] = impl;
549 return impls;
550 }
551
552 static void free_impls(impls *impls) {
553 size_t i;
554
555 for (i = 0; i < impls->count; ++i) {
556 free_impl(impls->list[i]);
557 }
558 free(impls->list);
559 free(impls);
560 }