flush
[m6w6/ext-psi] / src / parser.h
1 #ifndef _PSI_PARSER_H
2 #define _PSI_PARSER_H
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdarg.h>
7 #include <string.h>
8
9 #include <Zend/zend_types.h>
10
11 #include "parser_proc.h"
12
13 #define BSIZE 256
14
15 #define PSI_T_POINTER PSI_T_ASTERISK
16 typedef int token_t;
17
18 /* in php_psi.h */
19 size_t psi_t_alignment(token_t);
20 size_t psi_t_size(token_t);
21
22 typedef struct PSI_Token {
23 token_t type;
24 unsigned line;
25 size_t size;
26 char text[1];
27 } PSI_Token;
28
29 typedef struct decl_type {
30 char *name;
31 token_t type;
32 struct decl_type *real;
33 struct decl_struct *strct;
34 } decl_type;
35
36 static inline decl_type *init_decl_type(token_t type, const char *name) {
37 decl_type *t = calloc(1, sizeof(*t));
38 t->type = type;
39 t->name = strdup(name);
40 return t;
41 }
42
43 static inline decl_type *real_decl_type(decl_type *type) {
44 while (type->real) {
45 type = type->real;
46 }
47 return type;
48 }
49
50 static inline void free_decl_type(decl_type *type) {
51 free(type->name);
52 free(type);
53 }
54
55 typedef struct decl_typedef {
56 char *alias;
57 decl_type *type;
58 } decl_typedef;
59
60 static inline decl_typedef *init_decl_typedef(const char *name, decl_type *type) {
61 decl_typedef *t = calloc(1, sizeof(*t));
62 t->alias = strdup(name);
63 t->type = type;
64 return t;
65 }
66
67 static inline void free_decl_typedef(decl_typedef *t) {
68 free(t->alias);
69 free_decl_type(t->type);
70 free(t);
71 }
72
73 typedef struct decl_typedefs {
74 size_t count;
75 decl_typedef **list;
76 } decl_typedefs;
77
78 static inline decl_typedefs *add_decl_typedef(decl_typedefs *defs, decl_typedef *def) {
79 if (!defs) {
80 defs = calloc(1, sizeof(*defs));
81 }
82 defs->list = realloc(defs->list, ++defs->count * sizeof(*defs->list));
83 defs->list[defs->count-1] = def;
84 return defs;
85 }
86
87 static void free_decl_typedefs(decl_typedefs *defs) {
88 size_t i;
89
90 for (i = 0; i < defs->count; ++i) {
91 free_decl_typedef(defs->list[i]);
92 }
93 free(defs->list);
94 free(defs);
95 }
96
97 typedef struct decl_var {
98 char *name;
99 unsigned pointer_level;
100 unsigned array_size;
101 struct decl_arg *arg;
102 } decl_var;
103
104 static inline decl_var *init_decl_var(const char *name, unsigned pl, unsigned as) {
105 decl_var *v = calloc(1, sizeof(*v));
106 v->name = (char *) strdup((const char *) name);
107 v->pointer_level = pl;
108 v->array_size = as;
109 return v;
110 }
111
112 static inline void free_decl_var(decl_var *var) {
113 free(var->name);
114 free(var);
115 }
116
117 typedef struct decl_struct_layout {
118 size_t pos;
119 size_t len;
120 } decl_struct_layout;
121
122 static inline decl_struct_layout *init_decl_struct_layout(size_t pos, size_t len) {
123 decl_struct_layout *l = calloc(1, sizeof(*l));
124
125 l->pos = pos;
126 l->len = len;
127 return l;
128 }
129
130 static inline void free_decl_struct_layout(decl_struct_layout *l) {
131 free(l);
132 }
133
134 typedef struct decl_arg {
135 decl_type *type;
136 decl_var *var;
137 decl_struct_layout *layout;
138 struct let_stmt *let;
139 } decl_arg;
140
141 static inline decl_arg *init_decl_arg(decl_type *type, decl_var *var) {
142 decl_arg *arg = calloc(1, sizeof(*arg));
143 arg->type = type;
144 arg->var = var;
145 var->arg = arg;
146 return arg;
147 }
148
149 static inline void free_decl_arg(decl_arg *arg) {
150 free_decl_type(arg->type);
151 free_decl_var(arg->var);
152 if (arg->layout) {
153 free_decl_struct_layout(arg->layout);
154 }
155 free(arg);
156 }
157
158 typedef struct decl_vars {
159 decl_var **vars;
160 size_t count;
161 } decl_vars;
162
163 static inline decl_vars *init_decl_vars(decl_var *var) {
164 decl_vars *vars = calloc(1, sizeof(*vars));
165 if (var) {
166 vars->count = 1;
167 vars->vars = calloc(1, sizeof(*vars->vars));
168 vars->vars[0] = var;
169 }
170 return vars;
171 }
172
173 static inline decl_vars *add_decl_var(decl_vars *vars, decl_var *var) {
174 vars->vars = realloc(vars->vars, ++vars->count * sizeof(*vars->vars));
175 vars->vars[vars->count-1] = var;
176 return vars;
177 }
178
179 static inline void free_decl_vars(decl_vars *vars) {
180 size_t i;
181
182 for (i = 0; i < vars->count; ++i) {
183 free_decl_var(vars->vars[i]);
184 }
185 free(vars->vars);
186 free(vars);
187 }
188
189 typedef struct decl_args {
190 decl_arg **args;
191 size_t count;
192 } decl_args;
193
194 static inline decl_args *init_decl_args(decl_arg *arg) {
195 decl_args *args = calloc(1, sizeof(*args));
196 if (arg) {
197 args->count = 1;
198 args->args = calloc(1, sizeof(*args->args));
199 args->args[0] = arg;
200 }
201 return args;
202 }
203
204 static inline decl_args *add_decl_arg(decl_args *args, decl_arg *arg) {
205 args->args = realloc(args->args, ++args->count * sizeof(*args->args));
206 args->args[args->count-1] = arg;
207 return args;
208 }
209
210 static inline void free_decl_args(decl_args *args) {
211 size_t i;
212
213 for (i = 0; i < args->count; ++i) {
214 free_decl_arg(args->args[i]);
215 }
216 free(args->args);
217 free(args);
218 }
219
220 typedef struct decl_abi {
221 char *convention;
222 } decl_abi;
223
224 static inline decl_abi *init_decl_abi(const char *convention) {
225 decl_abi *abi = calloc(1, sizeof(*abi));
226 abi->convention = strdup(convention);
227 return abi;
228 }
229
230 static inline void free_decl_abi(decl_abi *abi) {
231 free(abi->convention);
232 free(abi);
233 }
234
235 typedef struct decl {
236 decl_abi *abi;
237 decl_arg *func;
238 decl_args *args;
239 struct impl *impl;
240 struct {
241 void *sym;
242 void *info;
243 void **args;
244 } call;
245 } decl;
246
247 static inline decl* init_decl(decl_abi *abi, decl_arg *func, decl_args *args) {
248 decl *d = calloc(1, sizeof(*d));
249 d->abi = abi;
250 d->func = func;
251 d->args = args;
252 return d;
253 }
254
255 static inline void free_decl(decl *d) {
256 free_decl_abi(d->abi);
257 free_decl_arg(d->func);
258 if (d->args) {
259 free_decl_args(d->args);
260 }
261 free(d);
262 }
263
264 typedef struct decls {
265 size_t count;
266 decl **list;
267 } decls;
268
269 static inline decls *add_decl(decls *decls, decl *decl) {
270 if (!decls) {
271 decls = calloc(1, sizeof(*decls));
272 }
273 decls->list = realloc(decls->list, ++decls->count * sizeof(*decls->list));
274 decls->list[decls->count-1] = decl;
275 return decls;
276 }
277
278 static inline void free_decls(decls *decls) {
279 size_t i;
280
281 for (i = 0; i < decls->count; ++i) {
282 free_decl(decls->list[i]);
283 }
284 free(decls->list);
285 free(decls);
286 }
287
288 typedef struct decl_struct {
289 char *name;
290 decl_args *args;
291 size_t size;
292 } decl_struct;
293
294 static inline decl_struct *init_decl_struct(const char *name, decl_args *args) {
295 decl_struct *s = calloc(1, sizeof(*s));
296 s->name = strdup(name);
297 s->args = args;
298 return s;
299 }
300
301 static inline void free_decl_struct(decl_struct *s) {
302 if (s->args) {
303 free_decl_args(s->args);
304 }
305 free(s->name);
306 free(s);
307 }
308
309 typedef struct decl_structs {
310 size_t count;
311 decl_struct **list;
312 } decl_structs;
313
314 static inline decl_structs *add_decl_struct(decl_structs *ss, decl_struct *s) {
315 if (!ss) {
316 ss = calloc(1, sizeof(*ss));
317 }
318 ss->list = realloc(ss->list, ++ss->count * sizeof(*ss->list));
319 ss->list[ss->count-1] = s;
320 return ss;
321 }
322
323 static inline void free_decl_structs(decl_structs *ss) {
324 size_t i;
325
326 for (i = 0; i < ss->count; ++i) {
327 free_decl_struct(ss->list[i]);
328 }
329 free(ss->list);
330 free(ss);
331 }
332
333 typedef union impl_val {
334 char cval;
335 int8_t i8;
336 uint8_t u8;
337 short sval;
338 int16_t i16;
339 uint16_t u16;
340 int ival;
341 int32_t i32;
342 uint32_t u32;
343 long lval;
344 int64_t i64;
345 uint64_t u64;
346 float fval;
347 double dval;
348 union {
349 zend_bool bval;
350 zend_long lval;
351 zend_string *str;
352 } zend;
353 void *ptr;
354 uint8_t _dbg[sizeof(void *)];
355 } impl_val;
356
357 static inline impl_val *deref_impl_val(impl_val *ret_val, decl_var *var) {
358 unsigned i;
359
360 if (var->arg->var != var) for (i = 1; i < var->pointer_level; ++i) {
361 ret_val = *(void **) ret_val;
362 }
363 return ret_val;
364 }
365
366 static inline impl_val *enref_impl_val(void *ptr, decl_var *var) {
367 impl_val *val, *val_ptr;
368 unsigned i;
369
370 if (!var->pointer_level && real_decl_type(var->arg->type)->type != PSI_T_STRUCT) {
371 return ptr;
372 }
373 val = val_ptr = calloc(var->pointer_level + 1, sizeof(void *));
374 for (i = 1; i < var->pointer_level; ++i) {
375 val_ptr->ptr = (void **) val_ptr + 1;
376 val_ptr = val_ptr->ptr;
377 }
378 val_ptr->ptr = ptr;
379 return val;
380 }
381
382 typedef struct impl_type {
383 char *name;
384 token_t type;
385 } impl_type;
386
387 static inline impl_type *init_impl_type(token_t type, const char *name) {
388 impl_type *t = calloc(1, sizeof(*t));
389
390 t->type = type;
391 t->name = strdup(name);
392 return t;
393 }
394
395 static inline void free_impl_type(impl_type *type) {
396 free(type->name);
397 free(type);
398 }
399
400 typedef struct impl_var {
401 char *name;
402 unsigned reference:1;
403 } impl_var;
404
405 static inline impl_var *init_impl_var(const char *name, int is_reference) {
406 impl_var *var = calloc(1, sizeof(*var));
407 var->name = strdup(name);
408 var->reference = is_reference;
409 return var;
410 }
411
412 static inline void free_impl_var(impl_var *var) {
413 free(var->name);
414 free(var);
415 }
416
417 typedef struct impl_def_val {
418 token_t type;
419 char *text;
420 } impl_def_val;
421
422 static inline impl_def_val *init_impl_def_val(token_t t, const char *text) {
423 impl_def_val *def = calloc(1, sizeof(*def));
424 def->type = t;
425 def->text = strdup(text);
426 return def;
427 }
428
429 static inline void free_impl_def_val(impl_def_val *def) {
430 free(def->text);
431 free(def);
432 }
433
434 typedef struct const_type {
435 token_t type;
436 char *name;
437 } const_type;
438
439 static inline const_type *init_const_type(token_t type, const char *name) {
440 const_type *ct = calloc(1, sizeof(*ct));
441 ct->type = type;
442 ct->name = strdup(name);
443 return ct;
444 }
445
446 static inline void free_const_type(const_type *type) {
447 free(type->name);
448 free(type);
449 }
450
451 typedef struct constant {
452 const_type *type;
453 char *name;
454 impl_def_val *val;
455 } constant;
456
457 static inline constant *init_constant(const_type *type, const char *name, impl_def_val *val) {
458 constant *c = calloc(1, sizeof(*c));
459 c->type = type;
460 c->name = strdup(name);
461 c->val = val;
462 return c;
463 }
464
465 static inline void free_constant(constant *constant) {
466 free_const_type(constant->type);
467 free(constant->name);
468 free_impl_def_val(constant->val);
469 free(constant);
470 }
471
472 typedef struct constants {
473 size_t count;
474 constant **list;
475 } constants;
476
477 static inline constants *add_constant(constants *constants, constant *constant) {
478 if (!constants) {
479 constants = calloc(1, sizeof(*constants));
480 }
481 constants->list = realloc(constants->list, ++constants->count * sizeof(*constants->list));
482 constants->list[constants->count-1] = constant;
483 return constants;
484 }
485
486 static inline void free_constants(constants *c) {
487 size_t i;
488
489 for (i = 0; i < c->count; ++i) {
490 free_constant(c->list[i]);
491 }
492 free(c->list);
493 free(c);
494 }
495
496 typedef struct impl_arg {
497 impl_type *type;
498 impl_var *var;
499 impl_def_val *def;
500 impl_val val;
501 zval *_zv;
502 } impl_arg;
503
504 static inline impl_arg *init_impl_arg(impl_type *type, impl_var *var, impl_def_val *def) {
505 impl_arg *arg = calloc(1, sizeof(*arg));
506 arg->type = type;
507 arg->var = var;
508 arg->def = def;
509 return arg;
510 }
511
512 static inline void free_impl_arg(impl_arg *arg) {
513 free_impl_type(arg->type);
514 free_impl_var(arg->var);
515 if (arg->def) {
516 free_impl_def_val(arg->def);
517 }
518 free(arg);
519 }
520
521 typedef struct impl_args {
522 impl_arg **args;
523 size_t count;
524 } impl_args;
525
526 static inline impl_args *init_impl_args(impl_arg *arg) {
527 impl_args *args = calloc(1, sizeof(*args));
528 if (arg) {
529 args->count = 1;
530 args->args = calloc(1, sizeof(*args->args));
531 args->args[0] = arg;
532 }
533 return args;
534 }
535
536 static inline impl_args *add_impl_arg(impl_args *args, impl_arg *arg) {
537 args->args = realloc(args->args, ++args->count * sizeof(*args->args));
538 args->args[args->count-1] = arg;
539 return args;
540 }
541
542 static inline void free_impl_args(impl_args *args) {
543 size_t i;
544
545 for (i = 0; i < args->count; ++i) {
546 free_impl_arg(args->args[i]);
547 }
548 free(args->args);
549 free(args);
550 }
551
552 typedef struct impl_func {
553 char *name;
554 impl_args *args;
555 impl_type *return_type;
556 unsigned return_reference:1;
557 } impl_func;
558
559 static inline impl_func *init_impl_func(char *name, impl_args *args, impl_type *type, int ret_reference) {
560 impl_func *func = calloc(1, sizeof(*func));
561 func->name = strdup(name);
562 func->args = args ? args : init_impl_args(NULL);
563 func->return_type = type;
564 func->return_reference = ret_reference;
565 return func;
566 }
567
568 static inline void free_impl_func(impl_func *f) {
569 free_impl_type(f->return_type);
570 free_impl_args(f->args);
571 free(f->name);
572 free(f);
573 }
574
575 typedef struct num_exp {
576 token_t t;
577 union {
578 char *numb;
579 constant *cnst;
580 decl_var *dvar;
581 } u;
582 token_t operator;
583 int (*calculator)(int t1, impl_val *v1, int t2, impl_val *v2, impl_val *res);
584 struct num_exp *operand;
585 } num_exp;
586
587 static inline num_exp *init_num_exp(token_t t, void *num) {
588 num_exp *exp = calloc(1, sizeof(*exp));
589 switch (exp->t = t) {
590 case PSI_T_NUMBER:
591 case PSI_T_NSNAME:
592 exp->u.numb = strdup(num);
593 break;
594 case PSI_T_NAME:
595 exp->u.dvar = num;
596 break;
597 EMPTY_SWITCH_DEFAULT_CASE();
598 }
599 return exp;
600 }
601
602 static inline void free_num_exp(num_exp *exp) {
603 switch (exp->t) {
604 case PSI_T_NUMBER:
605 free(exp->u.numb);
606 break;
607 case PSI_T_NSNAME:
608 break;
609 case PSI_T_NAME:
610 free_decl_var(exp->u.dvar);
611 break;
612 EMPTY_SWITCH_DEFAULT_CASE();
613 }
614 if (exp->operand) {
615 free_num_exp(exp->operand);
616 }
617 free(exp);
618 }
619
620 typedef struct let_calloc {
621 num_exp *nmemb;
622 num_exp *size;
623 } let_calloc;
624
625 static inline let_calloc *init_let_calloc(num_exp *nmemb, num_exp *size) {
626 let_calloc *alloc = calloc(1, sizeof(*alloc));
627 alloc->nmemb = nmemb;
628 alloc->size = size;
629 return alloc;
630 }
631
632 static inline void free_let_calloc(let_calloc *alloc) {
633 free_num_exp(alloc->nmemb);
634 free_num_exp(alloc->size);
635 free(alloc);
636 }
637
638 typedef struct let_func {
639 token_t type;
640 char *name;
641 let_calloc *alloc;
642 } let_func;
643
644 static inline let_func *init_let_func(token_t type, const char *name, let_calloc *alloc) {
645 let_func *func = calloc(1, sizeof(*func));
646 func->type = type;
647 func->name = strdup(name);
648 func->alloc = alloc;
649 return func;
650 }
651
652 static inline void free_let_func(let_func *func) {
653 if (func->alloc) {
654 free_let_calloc(func->alloc);
655 }
656 free(func->name);
657 free(func);
658 }
659
660 typedef struct let_value {
661 let_func *func;
662 impl_var *var;
663 unsigned is_reference:1;
664 } let_value;
665
666 static inline let_value *init_let_value(let_func *func, impl_var *var, int is_reference) {
667 let_value *val = calloc(1, sizeof(*val));
668 val->is_reference = is_reference;
669 val->func = func;
670 val->var = var;
671 return val;
672 }
673
674 static inline void free_let_value(let_value *val) {
675 if (val->func) {
676 free_let_func(val->func);
677 }
678 if (val->var) {
679 free_impl_var(val->var);
680 }
681 free(val);
682 }
683
684 typedef struct let_stmt {
685 decl_var *var;
686 let_value *val;
687 impl_arg *arg;
688 impl_val out;
689 void *ptr;
690 void *mem;
691 } let_stmt;
692
693 static inline let_stmt *init_let_stmt(decl_var *var, let_value *val) {
694 let_stmt *let = calloc(1, sizeof(*let));
695 let->var = var;
696 let->val = val;
697 return let;
698 }
699
700 static inline void free_let_stmt(let_stmt *stmt) {
701 free_decl_var(stmt->var);
702 if (stmt->val) {
703 free_let_value(stmt->val);
704 }
705 free(stmt);
706 }
707
708 struct set_value;
709
710 typedef struct set_func {
711 token_t type;
712 char *name;
713 void (*handler)(zval *, struct set_value *set, impl_val *ret_val);
714 } set_func;
715
716 static inline set_func *init_set_func(token_t type, const char *name) {
717 set_func *func = calloc(1, sizeof(*func));
718 func->type = type;
719 func->name = strdup(name);
720 return func;
721 }
722
723 static inline void free_set_func(set_func *func) {
724 free(func->name);
725 free(func);
726 }
727
728 typedef struct set_value {
729 set_func *func;
730 decl_vars *vars;
731 num_exp *num;
732 struct {
733 struct set_value *set;
734 impl_val *val;
735 } outer;
736 struct set_value **inner;
737 size_t count;
738 } set_value;
739
740 static inline set_value *init_set_value(set_func *func, decl_vars *vars) {
741 set_value *val = calloc(1, sizeof(*val));
742 val->func = func;
743 val->vars = vars;
744 return val;
745 }
746 static inline set_value *add_inner_set_value(set_value *val, set_value *inner) {
747 val->inner = realloc(val->inner, ++val->count * sizeof(*val->inner));
748 val->inner[val->count-1] = inner;
749 return val;
750 }
751
752 static inline void free_set_value(set_value *val) {
753 free_set_func(val->func);
754 free_decl_vars(val->vars);
755 if (val->inner) {
756 size_t i;
757 for (i = 0; i < val->count; ++i) {
758 free_set_value(val->inner[i]);
759 }
760 free(val->inner);
761 }
762 free(val);
763 }
764
765 typedef struct set_stmt {
766 impl_var *var;
767 set_value *val;
768 impl_arg *arg;
769 } set_stmt;
770
771 static inline set_stmt *init_set_stmt(impl_var *var, set_value *val) {
772 set_stmt *set = calloc(1, sizeof(*set));
773 set->var = var;
774 set->val = val;
775 return set;
776 }
777
778 static inline void free_set_stmt(set_stmt *set) {
779 free_impl_var(set->var);
780 free_set_value(set->val);
781 free(set);
782 }
783
784 typedef struct return_stmt {
785 set_value *set;
786 decl_arg *decl;
787 } return_stmt;
788
789 static inline return_stmt *init_return_stmt(set_value *val) {
790 return_stmt *ret = calloc(1, sizeof(*ret));
791 ret->set = val;
792 return ret;
793 }
794
795 static inline void free_return_stmt(return_stmt *ret) {
796 //free_set_func(ret->func);
797 //free_decl_var(ret->decl);
798 free_set_value(ret->set);
799 free(ret);
800 }
801
802 typedef struct free_call {
803 char *func;
804 decl_vars *vars;
805 decl *decl;
806 } free_call;
807
808 static inline free_call *init_free_call(const char *func, decl_vars *vars) {
809 free_call *f = calloc(1, sizeof(*f));
810 f->func = strdup(func);
811 f->vars = vars;
812 return f;
813 }
814
815 static inline void free_free_call(free_call *f) {
816 free(f->func);
817 free(f);
818 }
819
820 typedef struct free_calls {
821 free_call **list;
822 size_t count;
823 } free_calls;
824
825 static inline free_calls *init_free_calls(free_call *f) {
826 free_calls *fcs = calloc(1, sizeof(*fcs));
827 if (f) {
828 fcs->count = 1;
829 fcs->list = calloc(1, sizeof(*fcs->list));
830 fcs->list[0] = f;
831 }
832 return fcs;
833 }
834
835 static inline void free_free_calls(free_calls *fcs) {
836 size_t i;
837
838 for (i = 0; i < fcs->count; ++i) {
839 free_free_call(fcs->list[i]);
840 }
841 free(fcs->list);
842 free(fcs);
843 }
844
845 static inline free_calls *add_free_call(free_calls *fcs, free_call *f) {
846 fcs->list = realloc(fcs->list, ++fcs->count * sizeof(*fcs->list));
847 fcs->list[fcs->count-1] = f;
848 return fcs;
849 }
850
851 typedef struct free_stmt {
852 free_calls *calls;
853 } free_stmt;
854
855 static inline free_stmt *init_free_stmt(free_calls *calls) {
856 free_stmt *f = calloc(1, sizeof(*f));
857 f->calls = calls;
858 return f;
859 }
860
861 static inline void free_free_stmt(free_stmt *f) {
862 free_free_calls(f->calls);
863 free(f);
864 }
865
866 typedef struct impl_stmt {
867 token_t type;
868 union {
869 let_stmt *let;
870 set_stmt *set;
871 return_stmt *ret;
872 free_stmt *fre;
873 void *ptr;
874 } s;
875 } impl_stmt;
876
877 static inline impl_stmt *init_impl_stmt(token_t type, void *ptr) {
878 impl_stmt *stmt = calloc(1, sizeof(*stmt));
879 stmt->type = type;
880 stmt->s.ptr = ptr;
881 return stmt;
882 }
883
884 static inline void free_impl_stmt(impl_stmt *stmt) {
885 switch (stmt->type) {
886 case PSI_T_LET:
887 free_let_stmt(stmt->s.let);
888 break;
889 case PSI_T_SET:
890 free_set_stmt(stmt->s.set);
891 break;
892 case PSI_T_RETURN:
893 free_return_stmt(stmt->s.ret);
894 break;
895 case PSI_T_FREE:
896 free_free_stmt(stmt->s.fre);
897 break;
898 }
899 free(stmt);
900 }
901
902 typedef struct impl_stmts {
903 struct {
904 return_stmt **list;
905 size_t count;
906 } ret;
907 struct {
908 let_stmt **list;
909 size_t count;
910 } let;
911 struct {
912 set_stmt **list;
913 size_t count;
914 } set;
915 struct {
916 free_stmt **list;
917 size_t count;
918 } fre;
919 } impl_stmts;
920
921 static inline void *add_impl_stmt_ex(void *list, size_t count, void *stmt) {
922 list = realloc(list, count * sizeof(list));
923 ((void **)list)[count-1] = stmt;
924 return list;
925 }
926
927 static inline impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt) {
928 switch (stmt->type) {
929 case PSI_T_RETURN:
930 stmts->ret.list = add_impl_stmt_ex(stmts->ret.list, ++stmts->ret.count, stmt->s.ret);
931 break;
932 case PSI_T_LET:
933 stmts->let.list = add_impl_stmt_ex(stmts->let.list, ++stmts->let.count, stmt->s.let);
934 break;
935 case PSI_T_SET:
936 stmts->set.list = add_impl_stmt_ex(stmts->set.list, ++stmts->set.count, stmt->s.set);
937 break;
938 case PSI_T_FREE:
939 stmts->fre.list = add_impl_stmt_ex(stmts->fre.list, ++stmts->fre.count, stmt->s.fre);
940 break;
941 }
942 free(stmt);
943 return stmts;
944 }
945
946 static inline impl_stmts *init_impl_stmts(impl_stmt *stmt) {
947 impl_stmts *stmts = calloc(1, sizeof(*stmts));
948 return add_impl_stmt(stmts, stmt);
949 }
950
951 static inline void free_impl_stmts(impl_stmts *stmts) {
952 size_t i;
953
954 for (i = 0; i < stmts->let.count; ++i) {
955 free_let_stmt(stmts->let.list[i]);
956 }
957 free(stmts->let.list);
958 for (i = 0; i < stmts->ret.count; ++i) {
959 free_return_stmt(stmts->ret.list[i]);
960 }
961 free(stmts->ret.list);
962 for (i = 0; i < stmts->set.count; ++i) {
963 free_set_stmt(stmts->set.list[i]);
964 }
965 free(stmts->set.list);
966 for (i = 0; i < stmts->fre.count; ++i) {
967 free_free_stmt(stmts->fre.list[i]);
968 }
969 free(stmts->fre.list);
970 free(stmts);
971 }
972
973 typedef struct impl {
974 impl_func *func;
975 impl_stmts *stmts;
976 decl *decl;
977 } impl;
978
979 static inline impl *init_impl(impl_func *func, impl_stmts *stmts) {
980 impl *i = calloc(1, sizeof(*i));
981 i->func = func;
982 i->stmts = stmts;
983 return i;
984 }
985
986 static inline void free_impl(impl *impl) {
987 free_impl_func(impl->func);
988 free_impl_stmts(impl->stmts);
989 free(impl);
990 }
991
992 typedef struct impls {
993 size_t count;
994 impl **list;
995 } impls;
996
997 static inline impls *add_impl(impls *impls, impl *impl) {
998 if (!impls) {
999 impls = calloc(1, sizeof(*impls));
1000 }
1001 impls->list = realloc(impls->list, ++impls->count * sizeof(*impls->list));
1002 impls->list[impls->count-1] = impl;
1003 return impls;
1004 }
1005
1006 static void free_impls(impls *impls) {
1007 size_t i;
1008
1009 for (i = 0; i < impls->count; ++i) {
1010 free_impl(impls->list[i]);
1011 }
1012 free(impls->list);
1013 free(impls);
1014 }
1015
1016 static inline impl_val *struct_member_ref(decl_arg *set_arg, impl_val *struct_ptr, impl_val **to_free) {
1017 void *ptr = (char *) struct_ptr->ptr + set_arg->layout->pos;
1018 impl_val *val = enref_impl_val(ptr, set_arg->var);
1019
1020 if (val != ptr) {
1021 *to_free = val;
1022 }
1023
1024 return val;
1025 }
1026
1027 #define PSI_ERROR 16
1028 #define PSI_WARNING 32
1029 typedef void (*psi_error_cb)(int type, const char *msg, ...);
1030
1031 typedef struct decl_file {
1032 char *ln;
1033 char *fn;
1034 } decl_file;
1035
1036 static inline void free_decl_file(decl_file *file) {
1037 if (file->ln) {
1038 free(file->ln);
1039 }
1040 if (file->fn) {
1041 free(file->fn);
1042 }
1043 memset(file, 0, sizeof(*file));
1044 }
1045
1046 typedef struct decl_libs {
1047 void **dl;
1048 size_t count;
1049 } decl_libs;
1050
1051 static inline void free_decl_libs(decl_libs *libs) {
1052 if (libs->dl) {
1053 size_t i;
1054 for (i = 0; i < libs->count; ++i) {
1055 if (libs->dl[i]) {
1056 dlclose(libs->dl[i]);
1057 }
1058 }
1059 free(libs->dl);
1060 }
1061 memset(libs, 0, sizeof(*libs));
1062 }
1063
1064 static inline void add_decl_lib(decl_libs *libs, void *dlopened) {
1065 libs->dl = realloc(libs->dl, ++libs->count * sizeof(*libs->dl));
1066 libs->dl[libs->count-1] = dlopened;
1067 }
1068
1069 #define PSI_DATA(D) ((PSI_Data *) (D))
1070 #define PSI_DATA_MEMBERS \
1071 constants *consts; \
1072 decl_typedefs *defs; \
1073 decl_structs *structs; \
1074 decls *decls; \
1075 impls *impls; \
1076 union { \
1077 decl_file file; \
1078 decl_libs libs; \
1079 } psi; \
1080 psi_error_cb error
1081 typedef struct PSI_Data {
1082 PSI_DATA_MEMBERS;
1083 } PSI_Data;
1084
1085 static inline PSI_Data *PSI_DataExchange(PSI_Data *dest, PSI_Data *src) {
1086 if (!dest) {
1087 dest = malloc(sizeof(*dest));
1088 }
1089 memcpy(dest, src, sizeof(*dest));
1090 memset(src, 0, sizeof(*src));
1091 return dest;
1092 }
1093
1094 static inline void PSI_DataDtor(PSI_Data *data) {
1095 if (data->consts) {
1096 free_constants(data->consts);
1097 }
1098 if (data->defs) {
1099 free_decl_typedefs(data->defs);
1100 }
1101 if (data->structs) {
1102 free_decl_structs(data->structs);
1103 }
1104 if (data->decls) {
1105 free_decls(data->decls);
1106 }
1107 if (data->impls) {
1108 free_impls(data->impls);
1109 }
1110 free_decl_file(&data->psi.file);
1111 }
1112
1113 typedef struct PSI_Parser {
1114 PSI_DATA_MEMBERS;
1115 FILE *fp;
1116 unsigned flags;
1117 unsigned errors;
1118 void *proc;
1119 size_t line;
1120 token_t num;
1121 char *cur, *tok, *lim, *eof, *ctx, *mrk, buf[BSIZE];
1122 } PSI_Parser;
1123
1124 static inline PSI_Token *PSI_TokenAlloc(PSI_Parser *P) {
1125 PSI_Token *T;
1126 size_t token_len;
1127
1128 if (P->cur < P->tok) {
1129 return NULL;
1130 }
1131
1132 token_len = P->cur - P->tok;
1133
1134 T = calloc(1, sizeof(*T) + token_len);
1135 T->type = P->num;
1136 T->line = P->line;
1137 T->size = token_len;
1138 T->text[token_len] = 0;
1139 memcpy(T->text, P->tok, token_len);
1140
1141 return T;
1142 }
1143
1144 #define PSI_PARSER_DEBUG 0x1
1145
1146 PSI_Parser *PSI_ParserInit(PSI_Parser *P, const char *filename, psi_error_cb error, unsigned flags);
1147 void PSI_ParserSyntaxError(PSI_Parser *P, const char *fn, size_t ln, const char *msg, ...);
1148 size_t PSI_ParserFill(PSI_Parser *P, size_t n);
1149 token_t PSI_ParserScan(PSI_Parser *P);
1150 void PSI_ParserParse(PSI_Parser *P, PSI_Token *T);
1151 void PSI_ParserDtor(PSI_Parser *P);
1152 void PSI_ParserFree(PSI_Parser **P);
1153
1154 #endif