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