faa95e4931d9811788bf76e5decaa87a87fc08d6
[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 char *cnst;
580 decl_var *dvar;
581 } u;
582 token_t operator;
583 struct num_exp *operand;
584 } num_exp;
585
586 static inline num_exp *init_num_exp(token_t t, void *num) {
587 num_exp *exp = calloc(1, sizeof(*exp));
588 switch (exp->t = t) {
589 case PSI_T_NUMBER:
590 exp->u.numb = strdup(num);
591 break;
592 case PSI_T_NSNAME:
593 exp->u.cnst = strdup(num);
594 break;
595 case PSI_T_NAME:
596 exp->u.dvar = num;
597 break;
598 EMPTY_SWITCH_DEFAULT_CASE();
599 }
600 return exp;
601 }
602
603 static inline void free_num_exp(num_exp *exp) {
604 switch (exp->t) {
605 case PSI_T_NUMBER:
606 free(exp->u.numb);
607 break;
608 case PSI_T_NSNAME:
609 free(exp->u.cnst);
610 break;
611 case PSI_T_NAME:
612 free_decl_var(exp->u.dvar);
613 break;
614 EMPTY_SWITCH_DEFAULT_CASE();
615 }
616 if (exp->operand) {
617 free_num_exp(exp->operand);
618 }
619 free(exp);
620 }
621
622 typedef struct let_calloc {
623 num_exp *nmemb;
624 num_exp *size;
625 } let_calloc;
626
627 static inline let_calloc *init_let_calloc(num_exp *nmemb, num_exp *size) {
628 let_calloc *alloc = calloc(1, sizeof(*alloc));
629 alloc->nmemb = nmemb;
630 alloc->size = size;
631 return alloc;
632 }
633
634 static inline void free_let_calloc(let_calloc *alloc) {
635 free_num_exp(alloc->nmemb);
636 free_num_exp(alloc->size);
637 free(alloc);
638 }
639
640 typedef struct let_func {
641 token_t type;
642 char *name;
643 let_calloc *alloc;
644 } let_func;
645
646 static inline let_func *init_let_func(token_t type, const char *name, let_calloc *alloc) {
647 let_func *func = calloc(1, sizeof(*func));
648 func->type = type;
649 func->name = strdup(name);
650 func->alloc = alloc;
651 return func;
652 }
653
654 static inline void free_let_func(let_func *func) {
655 if (func->alloc) {
656 free_let_calloc(func->alloc);
657 }
658 free(func->name);
659 free(func);
660 }
661
662 typedef struct let_value {
663 let_func *func;
664 impl_var *var;
665 unsigned is_reference:1;
666 } let_value;
667
668 static inline let_value *init_let_value(let_func *func, impl_var *var, int is_reference) {
669 let_value *val = calloc(1, sizeof(*val));
670 val->is_reference = is_reference;
671 val->func = func;
672 val->var = var;
673 return val;
674 }
675
676 static inline void free_let_value(let_value *val) {
677 if (val->func) {
678 free_let_func(val->func);
679 }
680 if (val->var) {
681 free_impl_var(val->var);
682 }
683 free(val);
684 }
685
686 typedef struct let_stmt {
687 decl_var *var;
688 let_value *val;
689 impl_arg *arg;
690 impl_val out;
691 void *ptr;
692 void *mem;
693 } let_stmt;
694
695 static inline let_stmt *init_let_stmt(decl_var *var, let_value *val) {
696 let_stmt *let = calloc(1, sizeof(*let));
697 let->var = var;
698 let->val = val;
699 return let;
700 }
701
702 static inline void free_let_stmt(let_stmt *stmt) {
703 free_decl_var(stmt->var);
704 if (stmt->val) {
705 free_let_value(stmt->val);
706 }
707 free(stmt);
708 }
709
710 struct set_value;
711
712 typedef struct set_func {
713 token_t type;
714 char *name;
715 void (*handler)(zval *, struct set_value *set, impl_val *ret_val);
716 } set_func;
717
718 static inline set_func *init_set_func(token_t type, const char *name) {
719 set_func *func = calloc(1, sizeof(*func));
720 func->type = type;
721 func->name = strdup(name);
722 return func;
723 }
724
725 static inline void free_set_func(set_func *func) {
726 free(func->name);
727 free(func);
728 }
729
730 typedef struct set_value {
731 set_func *func;
732 decl_vars *vars;
733 struct {
734 struct set_value *set;
735 impl_val *val;
736 } outer;
737 struct set_value **inner;
738 size_t count;
739 } set_value;
740
741 static inline set_value *init_set_value(set_func *func, decl_vars *vars) {
742 set_value *val = calloc(1, sizeof(*val));
743 val->func = func;
744 val->vars = vars;
745 return val;
746 }
747 static inline set_value *add_inner_set_value(set_value *val, set_value *inner) {
748 val->inner = realloc(val->inner, ++val->count * sizeof(*val->inner));
749 val->inner[val->count-1] = inner;
750 return val;
751 }
752
753 static inline void free_set_value(set_value *val) {
754 free_set_func(val->func);
755 free_decl_vars(val->vars);
756 if (val->inner) {
757 size_t i;
758 for (i = 0; i < val->count; ++i) {
759 free_set_value(val->inner[i]);
760 }
761 free(val->inner);
762 }
763 free(val);
764 }
765
766 typedef struct set_stmt {
767 impl_var *var;
768 set_value *val;
769 impl_arg *arg;
770 } set_stmt;
771
772 static inline set_stmt *init_set_stmt(impl_var *var, set_value *val) {
773 set_stmt *set = calloc(1, sizeof(*set));
774 set->var = var;
775 set->val = val;
776 return set;
777 }
778
779 static inline void free_set_stmt(set_stmt *set) {
780 free_impl_var(set->var);
781 free_set_value(set->val);
782 free(set);
783 }
784
785 typedef struct return_stmt {
786 set_value *set;
787 decl_arg *decl;
788 } return_stmt;
789
790 static inline return_stmt *init_return_stmt(set_value *val) {
791 return_stmt *ret = calloc(1, sizeof(*ret));
792 ret->set = val;
793 return ret;
794 }
795
796 static inline void free_return_stmt(return_stmt *ret) {
797 //free_set_func(ret->func);
798 //free_decl_var(ret->decl);
799 free_set_value(ret->set);
800 free(ret);
801 }
802
803 typedef struct free_call {
804 char *func;
805 decl_vars *vars;
806 decl *decl;
807 } free_call;
808
809 static inline free_call *init_free_call(const char *func, decl_vars *vars) {
810 free_call *f = calloc(1, sizeof(*f));
811 f->func = strdup(func);
812 f->vars = vars;
813 return f;
814 }
815
816 static inline void free_free_call(free_call *f) {
817 free(f->func);
818 free(f);
819 }
820
821 typedef struct free_calls {
822 free_call **list;
823 size_t count;
824 } free_calls;
825
826 static inline free_calls *init_free_calls(free_call *f) {
827 free_calls *fcs = calloc(1, sizeof(*fcs));
828 if (f) {
829 fcs->count = 1;
830 fcs->list = calloc(1, sizeof(*fcs->list));
831 fcs->list[0] = f;
832 }
833 return fcs;
834 }
835
836 static inline void free_free_calls(free_calls *fcs) {
837 size_t i;
838
839 for (i = 0; i < fcs->count; ++i) {
840 free_free_call(fcs->list[i]);
841 }
842 free(fcs->list);
843 free(fcs);
844 }
845
846 static inline free_calls *add_free_call(free_calls *fcs, free_call *f) {
847 fcs->list = realloc(fcs->list, ++fcs->count * sizeof(*fcs->list));
848 fcs->list[fcs->count-1] = f;
849 return fcs;
850 }
851
852 typedef struct free_stmt {
853 free_calls *calls;
854 } free_stmt;
855
856 static inline free_stmt *init_free_stmt(free_calls *calls) {
857 free_stmt *f = calloc(1, sizeof(*f));
858 f->calls = calls;
859 return f;
860 }
861
862 static inline void free_free_stmt(free_stmt *f) {
863 free_free_calls(f->calls);
864 free(f);
865 }
866
867 typedef struct impl_stmt {
868 token_t type;
869 union {
870 let_stmt *let;
871 set_stmt *set;
872 return_stmt *ret;
873 free_stmt *fre;
874 void *ptr;
875 } s;
876 } impl_stmt;
877
878 static inline impl_stmt *init_impl_stmt(token_t type, void *ptr) {
879 impl_stmt *stmt = calloc(1, sizeof(*stmt));
880 stmt->type = type;
881 stmt->s.ptr = ptr;
882 return stmt;
883 }
884
885 static inline void free_impl_stmt(impl_stmt *stmt) {
886 switch (stmt->type) {
887 case PSI_T_LET:
888 free_let_stmt(stmt->s.let);
889 break;
890 case PSI_T_SET:
891 free_set_stmt(stmt->s.set);
892 break;
893 case PSI_T_RETURN:
894 free_return_stmt(stmt->s.ret);
895 break;
896 case PSI_T_FREE:
897 free_free_stmt(stmt->s.fre);
898 break;
899 }
900 free(stmt);
901 }
902
903 typedef struct impl_stmts {
904 struct {
905 return_stmt **list;
906 size_t count;
907 } ret;
908 struct {
909 let_stmt **list;
910 size_t count;
911 } let;
912 struct {
913 set_stmt **list;
914 size_t count;
915 } set;
916 struct {
917 free_stmt **list;
918 size_t count;
919 } fre;
920 } impl_stmts;
921
922 static inline void *add_impl_stmt_ex(void *list, size_t count, void *stmt) {
923 list = realloc(list, count * sizeof(list));
924 ((void **)list)[count-1] = stmt;
925 return list;
926 }
927
928 static inline impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt) {
929 switch (stmt->type) {
930 case PSI_T_RETURN:
931 stmts->ret.list = add_impl_stmt_ex(stmts->ret.list, ++stmts->ret.count, stmt->s.ret);
932 break;
933 case PSI_T_LET:
934 stmts->let.list = add_impl_stmt_ex(stmts->let.list, ++stmts->let.count, stmt->s.let);
935 break;
936 case PSI_T_SET:
937 stmts->set.list = add_impl_stmt_ex(stmts->set.list, ++stmts->set.count, stmt->s.set);
938 break;
939 case PSI_T_FREE:
940 stmts->fre.list = add_impl_stmt_ex(stmts->fre.list, ++stmts->fre.count, stmt->s.fre);
941 break;
942 }
943 free(stmt);
944 return stmts;
945 }
946
947 static inline impl_stmts *init_impl_stmts(impl_stmt *stmt) {
948 impl_stmts *stmts = calloc(1, sizeof(*stmts));
949 return add_impl_stmt(stmts, stmt);
950 }
951
952 static inline void free_impl_stmts(impl_stmts *stmts) {
953 size_t i;
954
955 for (i = 0; i < stmts->let.count; ++i) {
956 free_let_stmt(stmts->let.list[i]);
957 }
958 free(stmts->let.list);
959 for (i = 0; i < stmts->ret.count; ++i) {
960 free_return_stmt(stmts->ret.list[i]);
961 }
962 free(stmts->ret.list);
963 for (i = 0; i < stmts->set.count; ++i) {
964 free_set_stmt(stmts->set.list[i]);
965 }
966 free(stmts->set.list);
967 for (i = 0; i < stmts->fre.count; ++i) {
968 free_free_stmt(stmts->fre.list[i]);
969 }
970 free(stmts->fre.list);
971 free(stmts);
972 }
973
974 typedef struct impl {
975 impl_func *func;
976 impl_stmts *stmts;
977 decl *decl;
978 } impl;
979
980 static inline impl *init_impl(impl_func *func, impl_stmts *stmts) {
981 impl *i = calloc(1, sizeof(*i));
982 i->func = func;
983 i->stmts = stmts;
984 return i;
985 }
986
987 static inline void free_impl(impl *impl) {
988 free_impl_func(impl->func);
989 free_impl_stmts(impl->stmts);
990 free(impl);
991 }
992
993 typedef struct impls {
994 size_t count;
995 impl **list;
996 } impls;
997
998 static inline impls *add_impl(impls *impls, impl *impl) {
999 if (!impls) {
1000 impls = calloc(1, sizeof(*impls));
1001 }
1002 impls->list = realloc(impls->list, ++impls->count * sizeof(*impls->list));
1003 impls->list[impls->count-1] = impl;
1004 return impls;
1005 }
1006
1007 static void free_impls(impls *impls) {
1008 size_t i;
1009
1010 for (i = 0; i < impls->count; ++i) {
1011 free_impl(impls->list[i]);
1012 }
1013 free(impls->list);
1014 free(impls);
1015 }
1016
1017
1018 #define PSI_ERROR 16
1019 #define PSI_WARNING 32
1020 typedef void (*psi_error_cb)(int type, const char *msg, ...);
1021
1022 typedef struct decl_file {
1023 char *ln;
1024 char *fn;
1025 } decl_file;
1026
1027 static inline void free_decl_file(decl_file *file) {
1028 if (file->ln) {
1029 free(file->ln);
1030 }
1031 if (file->fn) {
1032 free(file->fn);
1033 }
1034 memset(file, 0, sizeof(*file));
1035 }
1036
1037 typedef struct decl_libs {
1038 void **dl;
1039 size_t count;
1040 } decl_libs;
1041
1042 static inline void free_decl_libs(decl_libs *libs) {
1043 if (libs->dl) {
1044 size_t i;
1045 for (i = 0; i < libs->count; ++i) {
1046 if (libs->dl[i]) {
1047 dlclose(libs->dl[i]);
1048 }
1049 }
1050 free(libs->dl);
1051 }
1052 memset(libs, 0, sizeof(*libs));
1053 }
1054
1055 static inline void add_decl_lib(decl_libs *libs, void *dlopened) {
1056 libs->dl = realloc(libs->dl, ++libs->count * sizeof(*libs->dl));
1057 libs->dl[libs->count-1] = dlopened;
1058 }
1059
1060 #define PSI_DATA(D) ((PSI_Data *) (D))
1061 #define PSI_DATA_MEMBERS \
1062 constants *consts; \
1063 decl_typedefs *defs; \
1064 decl_structs *structs; \
1065 decls *decls; \
1066 impls *impls; \
1067 union { \
1068 decl_file file; \
1069 decl_libs libs; \
1070 } psi; \
1071 psi_error_cb error
1072 typedef struct PSI_Data {
1073 PSI_DATA_MEMBERS;
1074 } PSI_Data;
1075
1076 static inline PSI_Data *PSI_DataExchange(PSI_Data *dest, PSI_Data *src) {
1077 if (!dest) {
1078 dest = malloc(sizeof(*dest));
1079 }
1080 memcpy(dest, src, sizeof(*dest));
1081 memset(src, 0, sizeof(*src));
1082 return dest;
1083 }
1084
1085 static inline void PSI_DataDtor(PSI_Data *data) {
1086 if (data->consts) {
1087 free_constants(data->consts);
1088 }
1089 if (data->defs) {
1090 free_decl_typedefs(data->defs);
1091 }
1092 if (data->structs) {
1093 free_decl_structs(data->structs);
1094 }
1095 if (data->decls) {
1096 free_decls(data->decls);
1097 }
1098 if (data->impls) {
1099 free_impls(data->impls);
1100 }
1101 free_decl_file(&data->psi.file);
1102 }
1103
1104 typedef struct PSI_Parser {
1105 PSI_DATA_MEMBERS;
1106 FILE *fp;
1107 unsigned flags;
1108 unsigned errors;
1109 void *proc;
1110 size_t line;
1111 token_t num;
1112 char *cur, *tok, *lim, *eof, *ctx, *mrk, buf[BSIZE];
1113 } PSI_Parser;
1114
1115 static inline PSI_Token *PSI_TokenAlloc(PSI_Parser *P) {
1116 PSI_Token *T;
1117 size_t token_len;
1118
1119 if (P->cur < P->tok) {
1120 return NULL;
1121 }
1122
1123 token_len = P->cur - P->tok;
1124
1125 T = calloc(1, sizeof(*T) + token_len);
1126 T->type = P->num;
1127 T->line = P->line;
1128 T->size = token_len;
1129 T->text[token_len] = 0;
1130 memcpy(T->text, P->tok, token_len);
1131
1132 return T;
1133 }
1134
1135 #define PSI_PARSER_DEBUG 0x1
1136
1137 PSI_Parser *PSI_ParserInit(PSI_Parser *P, const char *filename, psi_error_cb error, unsigned flags);
1138 void PSI_ParserSyntaxError(PSI_Parser *P, const char *fn, size_t ln, const char *msg, ...);
1139 size_t PSI_ParserFill(PSI_Parser *P, size_t n);
1140 token_t PSI_ParserScan(PSI_Parser *P);
1141 void PSI_ParserParse(PSI_Parser *P, PSI_Token *T);
1142 void PSI_ParserDtor(PSI_Parser *P);
1143 void PSI_ParserFree(PSI_Parser **P);
1144
1145 #endif