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