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