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 struct {
732 struct set_value *set;
733 impl_val *val;
734 } outer;
735 struct set_value **inner;
736 size_t count;
737 } set_value;
738
739 static inline set_value *init_set_value(set_func *func, decl_vars *vars) {
740 set_value *val = calloc(1, sizeof(*val));
741 val->func = func;
742 val->vars = vars;
743 return val;
744 }
745 static inline set_value *add_inner_set_value(set_value *val, set_value *inner) {
746 val->inner = realloc(val->inner, ++val->count * sizeof(*val->inner));
747 val->inner[val->count-1] = inner;
748 return val;
749 }
750
751 static inline void free_set_value(set_value *val) {
752 free_set_func(val->func);
753 free_decl_vars(val->vars);
754 if (val->inner) {
755 size_t i;
756 for (i = 0; i < val->count; ++i) {
757 free_set_value(val->inner[i]);
758 }
759 free(val->inner);
760 }
761 free(val);
762 }
763
764 typedef struct set_stmt {
765 impl_var *var;
766 set_value *val;
767 impl_arg *arg;
768 } set_stmt;
769
770 static inline set_stmt *init_set_stmt(impl_var *var, set_value *val) {
771 set_stmt *set = calloc(1, sizeof(*set));
772 set->var = var;
773 set->val = val;
774 return set;
775 }
776
777 static inline void free_set_stmt(set_stmt *set) {
778 free_impl_var(set->var);
779 free_set_value(set->val);
780 free(set);
781 }
782
783 typedef struct return_stmt {
784 set_value *set;
785 decl_arg *decl;
786 } return_stmt;
787
788 static inline return_stmt *init_return_stmt(set_value *val) {
789 return_stmt *ret = calloc(1, sizeof(*ret));
790 ret->set = val;
791 return ret;
792 }
793
794 static inline void free_return_stmt(return_stmt *ret) {
795 //free_set_func(ret->func);
796 //free_decl_var(ret->decl);
797 free_set_value(ret->set);
798 free(ret);
799 }
800
801 typedef struct free_call {
802 char *func;
803 decl_vars *vars;
804 decl *decl;
805 } free_call;
806
807 static inline free_call *init_free_call(const char *func, decl_vars *vars) {
808 free_call *f = calloc(1, sizeof(*f));
809 f->func = strdup(func);
810 f->vars = vars;
811 return f;
812 }
813
814 static inline void free_free_call(free_call *f) {
815 free(f->func);
816 free(f);
817 }
818
819 typedef struct free_calls {
820 free_call **list;
821 size_t count;
822 } free_calls;
823
824 static inline free_calls *init_free_calls(free_call *f) {
825 free_calls *fcs = calloc(1, sizeof(*fcs));
826 if (f) {
827 fcs->count = 1;
828 fcs->list = calloc(1, sizeof(*fcs->list));
829 fcs->list[0] = f;
830 }
831 return fcs;
832 }
833
834 static inline void free_free_calls(free_calls *fcs) {
835 size_t i;
836
837 for (i = 0; i < fcs->count; ++i) {
838 free_free_call(fcs->list[i]);
839 }
840 free(fcs->list);
841 free(fcs);
842 }
843
844 static inline free_calls *add_free_call(free_calls *fcs, free_call *f) {
845 fcs->list = realloc(fcs->list, ++fcs->count * sizeof(*fcs->list));
846 fcs->list[fcs->count-1] = f;
847 return fcs;
848 }
849
850 typedef struct free_stmt {
851 free_calls *calls;
852 } free_stmt;
853
854 static inline free_stmt *init_free_stmt(free_calls *calls) {
855 free_stmt *f = calloc(1, sizeof(*f));
856 f->calls = calls;
857 return f;
858 }
859
860 static inline void free_free_stmt(free_stmt *f) {
861 free_free_calls(f->calls);
862 free(f);
863 }
864
865 typedef struct impl_stmt {
866 token_t type;
867 union {
868 let_stmt *let;
869 set_stmt *set;
870 return_stmt *ret;
871 free_stmt *fre;
872 void *ptr;
873 } s;
874 } impl_stmt;
875
876 static inline impl_stmt *init_impl_stmt(token_t type, void *ptr) {
877 impl_stmt *stmt = calloc(1, sizeof(*stmt));
878 stmt->type = type;
879 stmt->s.ptr = ptr;
880 return stmt;
881 }
882
883 static inline void free_impl_stmt(impl_stmt *stmt) {
884 switch (stmt->type) {
885 case PSI_T_LET:
886 free_let_stmt(stmt->s.let);
887 break;
888 case PSI_T_SET:
889 free_set_stmt(stmt->s.set);
890 break;
891 case PSI_T_RETURN:
892 free_return_stmt(stmt->s.ret);
893 break;
894 case PSI_T_FREE:
895 free_free_stmt(stmt->s.fre);
896 break;
897 }
898 free(stmt);
899 }
900
901 typedef struct impl_stmts {
902 struct {
903 return_stmt **list;
904 size_t count;
905 } ret;
906 struct {
907 let_stmt **list;
908 size_t count;
909 } let;
910 struct {
911 set_stmt **list;
912 size_t count;
913 } set;
914 struct {
915 free_stmt **list;
916 size_t count;
917 } fre;
918 } impl_stmts;
919
920 static inline void *add_impl_stmt_ex(void *list, size_t count, void *stmt) {
921 list = realloc(list, count * sizeof(list));
922 ((void **)list)[count-1] = stmt;
923 return list;
924 }
925
926 static inline impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt) {
927 switch (stmt->type) {
928 case PSI_T_RETURN:
929 stmts->ret.list = add_impl_stmt_ex(stmts->ret.list, ++stmts->ret.count, stmt->s.ret);
930 break;
931 case PSI_T_LET:
932 stmts->let.list = add_impl_stmt_ex(stmts->let.list, ++stmts->let.count, stmt->s.let);
933 break;
934 case PSI_T_SET:
935 stmts->set.list = add_impl_stmt_ex(stmts->set.list, ++stmts->set.count, stmt->s.set);
936 break;
937 case PSI_T_FREE:
938 stmts->fre.list = add_impl_stmt_ex(stmts->fre.list, ++stmts->fre.count, stmt->s.fre);
939 break;
940 }
941 free(stmt);
942 return stmts;
943 }
944
945 static inline impl_stmts *init_impl_stmts(impl_stmt *stmt) {
946 impl_stmts *stmts = calloc(1, sizeof(*stmts));
947 return add_impl_stmt(stmts, stmt);
948 }
949
950 static inline void free_impl_stmts(impl_stmts *stmts) {
951 size_t i;
952
953 for (i = 0; i < stmts->let.count; ++i) {
954 free_let_stmt(stmts->let.list[i]);
955 }
956 free(stmts->let.list);
957 for (i = 0; i < stmts->ret.count; ++i) {
958 free_return_stmt(stmts->ret.list[i]);
959 }
960 free(stmts->ret.list);
961 for (i = 0; i < stmts->set.count; ++i) {
962 free_set_stmt(stmts->set.list[i]);
963 }
964 free(stmts->set.list);
965 for (i = 0; i < stmts->fre.count; ++i) {
966 free_free_stmt(stmts->fre.list[i]);
967 }
968 free(stmts->fre.list);
969 free(stmts);
970 }
971
972 typedef struct impl {
973 impl_func *func;
974 impl_stmts *stmts;
975 decl *decl;
976 } impl;
977
978 static inline impl *init_impl(impl_func *func, impl_stmts *stmts) {
979 impl *i = calloc(1, sizeof(*i));
980 i->func = func;
981 i->stmts = stmts;
982 return i;
983 }
984
985 static inline void free_impl(impl *impl) {
986 free_impl_func(impl->func);
987 free_impl_stmts(impl->stmts);
988 free(impl);
989 }
990
991 typedef struct impls {
992 size_t count;
993 impl **list;
994 } impls;
995
996 static inline impls *add_impl(impls *impls, impl *impl) {
997 if (!impls) {
998 impls = calloc(1, sizeof(*impls));
999 }
1000 impls->list = realloc(impls->list, ++impls->count * sizeof(*impls->list));
1001 impls->list[impls->count-1] = impl;
1002 return impls;
1003 }
1004
1005 static void free_impls(impls *impls) {
1006 size_t i;
1007
1008 for (i = 0; i < impls->count; ++i) {
1009 free_impl(impls->list[i]);
1010 }
1011 free(impls->list);
1012 free(impls);
1013 }
1014
1015
1016 #define PSI_ERROR 16
1017 #define PSI_WARNING 32
1018 typedef void (*psi_error_cb)(int type, const char *msg, ...);
1019
1020 typedef struct decl_file {
1021 char *ln;
1022 char *fn;
1023 } decl_file;
1024
1025 static inline void free_decl_file(decl_file *file) {
1026 if (file->ln) {
1027 free(file->ln);
1028 }
1029 if (file->fn) {
1030 free(file->fn);
1031 }
1032 memset(file, 0, sizeof(*file));
1033 }
1034
1035 typedef struct decl_libs {
1036 void **dl;
1037 size_t count;
1038 } decl_libs;
1039
1040 static inline void free_decl_libs(decl_libs *libs) {
1041 if (libs->dl) {
1042 size_t i;
1043 for (i = 0; i < libs->count; ++i) {
1044 if (libs->dl[i]) {
1045 dlclose(libs->dl[i]);
1046 }
1047 }
1048 free(libs->dl);
1049 }
1050 memset(libs, 0, sizeof(*libs));
1051 }
1052
1053 static inline void add_decl_lib(decl_libs *libs, void *dlopened) {
1054 libs->dl = realloc(libs->dl, ++libs->count * sizeof(*libs->dl));
1055 libs->dl[libs->count-1] = dlopened;
1056 }
1057
1058 #define PSI_DATA(D) ((PSI_Data *) (D))
1059 #define PSI_DATA_MEMBERS \
1060 constants *consts; \
1061 decl_typedefs *defs; \
1062 decl_structs *structs; \
1063 decls *decls; \
1064 impls *impls; \
1065 union { \
1066 decl_file file; \
1067 decl_libs libs; \
1068 } psi; \
1069 psi_error_cb error
1070 typedef struct PSI_Data {
1071 PSI_DATA_MEMBERS;
1072 } PSI_Data;
1073
1074 static inline PSI_Data *PSI_DataExchange(PSI_Data *dest, PSI_Data *src) {
1075 if (!dest) {
1076 dest = malloc(sizeof(*dest));
1077 }
1078 memcpy(dest, src, sizeof(*dest));
1079 memset(src, 0, sizeof(*src));
1080 return dest;
1081 }
1082
1083 static inline void PSI_DataDtor(PSI_Data *data) {
1084 if (data->consts) {
1085 free_constants(data->consts);
1086 }
1087 if (data->defs) {
1088 free_decl_typedefs(data->defs);
1089 }
1090 if (data->structs) {
1091 free_decl_structs(data->structs);
1092 }
1093 if (data->decls) {
1094 free_decls(data->decls);
1095 }
1096 if (data->impls) {
1097 free_impls(data->impls);
1098 }
1099 free_decl_file(&data->psi.file);
1100 }
1101
1102 typedef struct PSI_Parser {
1103 PSI_DATA_MEMBERS;
1104 FILE *fp;
1105 unsigned flags;
1106 unsigned errors;
1107 void *proc;
1108 size_t line;
1109 token_t num;
1110 char *cur, *tok, *lim, *eof, *ctx, *mrk, buf[BSIZE];
1111 } PSI_Parser;
1112
1113 static inline PSI_Token *PSI_TokenAlloc(PSI_Parser *P) {
1114 PSI_Token *T;
1115 size_t token_len;
1116
1117 if (P->cur < P->tok) {
1118 return NULL;
1119 }
1120
1121 token_len = P->cur - P->tok;
1122
1123 T = calloc(1, sizeof(*T) + token_len);
1124 T->type = P->num;
1125 T->line = P->line;
1126 T->size = token_len;
1127 T->text[token_len] = 0;
1128 memcpy(T->text, P->tok, token_len);
1129
1130 return T;
1131 }
1132
1133 #define PSI_PARSER_DEBUG 0x1
1134
1135 PSI_Parser *PSI_ParserInit(PSI_Parser *P, const char *filename, psi_error_cb error, unsigned flags);
1136 void PSI_ParserSyntaxError(PSI_Parser *P, const char *fn, size_t ln, const char *msg, ...);
1137 size_t PSI_ParserFill(PSI_Parser *P, size_t n);
1138 token_t PSI_ParserScan(PSI_Parser *P);
1139 void PSI_ParserParse(PSI_Parser *P, PSI_Token *T);
1140 void PSI_ParserDtor(PSI_Parser *P);
1141 void PSI_ParserFree(PSI_Parser **P);
1142
1143 #endif