c99c26606e8fe17942a054a41d89e2e3ddc9e207
[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 #include <Zend/zend_API.h> /* fcall */
11
12 #include "parser_proc.h"
13
14 #define BSIZE 256
15
16 #define PSI_T_POINTER PSI_T_ASTERISK
17 #define PSI_T_LONG_DOUBLE (PSI_T_DOUBLE << 16)
18
19 typedef int token_t;
20
21 size_t psi_t_alignment(token_t);
22 size_t psi_t_size(token_t);
23
24 typedef struct PSI_Token {
25 token_t type;
26 unsigned size, line, col;
27 char *text, *file;
28 char buf[1];
29 } PSI_Token;
30
31 static inline PSI_Token *PSI_TokenCopy(PSI_Token *src);
32
33 typedef struct zend_fcall {
34 zend_fcall_info fci;
35 zend_fcall_info_cache fcc;
36 } zend_fcall;
37
38 typedef union impl_val {
39 char cval;
40 int8_t i8;
41 uint8_t u8;
42 short sval;
43 int16_t i16;
44 uint16_t u16;
45 int ival;
46 int32_t i32;
47 uint32_t u32;
48 long lval;
49 int64_t i64;
50 uint64_t u64;
51 float fval;
52 double dval;
53 #ifdef HAVE_LONG_DOUBLE
54 long double ldval;
55 #endif
56 union {
57 zend_bool bval;
58 zend_long lval;
59 zend_string *str;
60 zend_fcall *cb;
61 } zend;
62 void *ptr;
63 } impl_val;
64
65 typedef struct decl_type {
66 PSI_Token *token;
67 char *name;
68 token_t type;
69 union {
70 struct decl_arg *def;
71 struct decl_struct *strct;
72 struct decl_union *unn;
73 struct decl_enum *enm;
74 struct decl *func;
75 } real;
76 } decl_type;
77
78 static inline decl_type *init_decl_type(token_t type, const char *name) {
79 decl_type *t = calloc(1, sizeof(*t));
80 t->type = type;
81 t->name = strdup(name);
82 return t;
83 }
84
85 static inline int weak_decl_type(decl_type *type) {
86 switch (type->type) {
87 case PSI_T_CHAR:
88 case PSI_T_SHORT:
89 case PSI_T_INT:
90 case PSI_T_LONG:
91 case PSI_T_NAME:
92 return type->type;
93 default:
94 return 0;
95 }
96 }
97
98 static inline void free_decl(struct decl *decl);
99 static inline void free_decl_type(decl_type *type) {
100 if (type->token) {
101 free(type->token);
102 }
103 if (type->type == PSI_T_FUNCTION) {
104 free_decl(type->real.func);
105 }
106 free(type->name);
107 free(type);
108 }
109
110 typedef struct decl_var {
111 PSI_Token *token;
112 char *name;
113 unsigned pointer_level;
114 unsigned array_size;
115 struct decl_arg *arg;
116 } decl_var;
117
118 static inline decl_var *init_decl_var(const char *name, unsigned pl, unsigned as) {
119 decl_var *v = calloc(1, sizeof(*v));
120 v->name = (char *) strdup((const char *) name);
121 v->pointer_level = pl;
122 v->array_size = as;
123 return v;
124 }
125
126 static inline decl_var *copy_decl_var(decl_var *src) {
127 decl_var *dest = calloc(1, sizeof(*dest));
128
129 memcpy(dest, src, sizeof(*dest));
130 dest->name = strdup(dest->name);
131 if (dest->token) {
132 dest->token = PSI_TokenCopy(dest->token);
133 }
134 return dest;
135 }
136
137 static inline void free_decl_var(decl_var *var) {
138 if (var->token) {
139 free(var->token);
140 }
141 free(var->name);
142 free(var);
143 }
144
145 typedef struct decl_struct_layout {
146 size_t pos;
147 size_t len;
148 } decl_struct_layout;
149
150 static inline decl_struct_layout *init_decl_struct_layout(size_t pos, size_t len) {
151 decl_struct_layout *l = calloc(1, sizeof(*l));
152 ZEND_ASSERT(pos+len);
153 l->pos = pos;
154 l->len = len;
155 return l;
156 }
157
158 static inline void free_decl_struct_layout(decl_struct_layout *l) {
159 free(l);
160 }
161
162 typedef struct decl_arg {
163 PSI_Token *token;
164 decl_type *type;
165 decl_var *var;
166 decl_struct_layout *layout;
167 impl_val val;
168 void *ptr;
169 void *let;
170 void *mem;
171 } decl_arg;
172
173 static inline decl_arg *init_decl_arg(decl_type *type, decl_var *var) {
174 decl_arg *arg = calloc(1, sizeof(*arg));
175 arg->token = var->token;
176 arg->type = type;
177 arg->var = var;
178 var->arg = arg;
179 arg->ptr = &arg->val;
180 arg->let = arg->ptr;
181 return arg;
182 }
183
184 static inline void free_decl_arg(decl_arg *arg) {
185 if (arg->token && arg->token != arg->var->token) {
186 free(arg->token);
187 }
188 free_decl_type(arg->type);
189 free_decl_var(arg->var);
190 if (arg->layout) {
191 free_decl_struct_layout(arg->layout);
192 }
193 free(arg);
194 }
195
196 static inline decl_type *real_decl_type(decl_type *type) {
197 while (weak_decl_type(type)) {
198 type = type->real.def->type;
199 }
200 return type;
201 }
202
203 typedef struct decl_typedefs {
204 size_t count;
205 decl_arg **list;
206 } decl_typedefs;
207
208 static inline decl_typedefs *add_decl_typedef(decl_typedefs *defs, decl_arg *def) {
209 if (!defs) {
210 defs = calloc(1, sizeof(*defs));
211 }
212 defs->list = realloc(defs->list, ++defs->count * sizeof(*defs->list));
213 defs->list[defs->count-1] = def;
214 return defs;
215 }
216
217 static void free_decl_typedefs(decl_typedefs *defs) {
218 size_t i;
219
220 for (i = 0; i < defs->count; ++i) {
221 free_decl_arg(defs->list[i]);
222 }
223 free(defs->list);
224 free(defs);
225 }
226
227 typedef struct decl_vars {
228 decl_var **vars;
229 size_t count;
230 } decl_vars;
231
232 static inline decl_vars *init_decl_vars(decl_var *var) {
233 decl_vars *vars = calloc(1, sizeof(*vars));
234 if (var) {
235 vars->count = 1;
236 vars->vars = calloc(1, sizeof(*vars->vars));
237 vars->vars[0] = var;
238 }
239 return vars;
240 }
241
242 static inline decl_vars *add_decl_var(decl_vars *vars, decl_var *var) {
243 vars->vars = realloc(vars->vars, ++vars->count * sizeof(*vars->vars));
244 vars->vars[vars->count-1] = var;
245 return vars;
246 }
247
248 static inline void free_decl_vars(decl_vars *vars) {
249 size_t i;
250
251 for (i = 0; i < vars->count; ++i) {
252 free_decl_var(vars->vars[i]);
253 }
254 free(vars->vars);
255 free(vars);
256 }
257
258 typedef struct decl_args {
259 decl_arg **args;
260 size_t count;
261 unsigned varargs:1;
262 } decl_args;
263
264 static inline decl_args *init_decl_args(decl_arg *arg) {
265 decl_args *args = calloc(1, sizeof(*args));
266 if (arg) {
267 args->count = 1;
268 args->args = calloc(1, sizeof(*args->args));
269 args->args[0] = arg;
270 }
271 return args;
272 }
273
274 static inline decl_args *add_decl_arg(decl_args *args, decl_arg *arg) {
275 args->args = realloc(args->args, ++args->count * sizeof(*args->args));
276 args->args[args->count-1] = arg;
277 return args;
278 }
279
280 static inline void free_decl_args(decl_args *args) {
281 size_t i;
282
283 for (i = 0; i < args->count; ++i) {
284 free_decl_arg(args->args[i]);
285 }
286 free(args->args);
287 free(args);
288 }
289
290 typedef struct decl_abi {
291 PSI_Token *token;
292 char *convention;
293 } decl_abi;
294
295 static inline decl_abi *init_decl_abi(const char *convention) {
296 decl_abi *abi = calloc(1, sizeof(*abi));
297 abi->convention = strdup(convention);
298 return abi;
299 }
300
301 static inline void free_decl_abi(decl_abi *abi) {
302 if (abi->token) {
303 free(abi->token);
304 }
305 free(abi->convention);
306 free(abi);
307 }
308
309 typedef struct decl_callinfo {
310 void *sym;
311 void *info;
312 size_t argc;
313 void **args;
314 void **rval;
315 } decl_callinfo;
316
317 typedef struct decl {
318 decl_abi *abi;
319 decl_arg *func;
320 decl_args *args;
321 struct impl *impl;
322 decl_callinfo call;
323 } decl;
324
325 static inline decl* init_decl(decl_abi *abi, decl_arg *func, decl_args *args) {
326 decl *d = calloc(1, sizeof(*d));
327 d->abi = abi;
328 d->func = func;
329 d->args = args;
330 return d;
331 }
332
333 static inline void free_decl(decl *d) {
334 free_decl_abi(d->abi);
335 free_decl_arg(d->func);
336 if (d->args) {
337 free_decl_args(d->args);
338 }
339 free(d);
340 }
341
342 typedef struct decls {
343 size_t count;
344 decl **list;
345 } decls;
346
347 static inline decls *add_decl(decls *decls, decl *decl) {
348 if (!decls) {
349 decls = calloc(1, sizeof(*decls));
350 }
351 decls->list = realloc(decls->list, ++decls->count * sizeof(*decls->list));
352 decls->list[decls->count-1] = decl;
353 return decls;
354 }
355
356 static inline void free_decls(decls *decls) {
357 size_t i;
358
359 for (i = 0; i < decls->count; ++i) {
360 free_decl(decls->list[i]);
361 }
362 free(decls->list);
363 free(decls);
364 }
365
366
367 typedef struct decl_struct {
368 PSI_Token *token;
369 char *name;
370 decl_args *args;
371 size_t size;
372 size_t align;
373 struct {
374 void *type;
375 void (*dtor)(void *type);
376 } engine;
377 } decl_struct;
378
379 static inline decl_struct *init_decl_struct(const char *name, decl_args *args) {
380 decl_struct *s = calloc(1, sizeof(*s));
381 s->name = strdup(name);
382 s->args = args;
383 return s;
384 }
385
386 static inline void free_decl_struct(decl_struct *s) {
387 if (s->token) {
388 free(s->token);
389 }
390 if (s->args) {
391 free_decl_args(s->args);
392 }
393 if (s->engine.type && s->engine.dtor) {
394 s->engine.dtor(s->engine.type);
395 }
396 free(s->name);
397 free(s);
398 }
399
400 typedef struct decl_structs {
401 size_t count;
402 decl_struct **list;
403 } decl_structs;
404
405 static inline decl_structs *add_decl_struct(decl_structs *ss, decl_struct *s) {
406 if (!ss) {
407 ss = calloc(1, sizeof(*ss));
408 }
409 ss->list = realloc(ss->list, ++ss->count * sizeof(*ss->list));
410 ss->list[ss->count-1] = s;
411 return ss;
412 }
413
414 static inline void free_decl_structs(decl_structs *ss) {
415 size_t i;
416
417 for (i = 0; i < ss->count; ++i) {
418 free_decl_struct(ss->list[i]);
419 }
420 free(ss->list);
421 free(ss);
422 }
423
424 typedef struct decl_union {
425 PSI_Token *token;
426 char *name;
427 decl_args *args;
428 size_t size;
429 size_t align;
430 } decl_union;
431
432 static inline decl_union *init_decl_union(const char *name, decl_args *args) {
433 decl_union *u = calloc(1, sizeof(*u));
434 u->name = strdup(name);
435 u->args = args;
436 return u;
437 }
438
439 static inline void free_decl_union(decl_union *u) {
440 if (u->token) {
441 free(u->token);
442 }
443 if (u->args) {
444 free_decl_args(u->args);
445 }
446 free(u->name);
447 free(u);
448 }
449
450 typedef struct decl_unions {
451 decl_union **list;
452 size_t count;
453 } decl_unions;
454
455 static inline decl_unions *add_decl_union(decl_unions *uu, decl_union *u) {
456 if (!uu) {
457 uu = calloc(1, sizeof(*uu));
458 }
459 uu->list = realloc(uu->list, ++uu->count * sizeof(*uu->list));
460 uu->list[uu->count-1] = u;
461 return uu;
462 }
463
464 static inline void free_decl_unions(decl_unions *uu) {
465 size_t i;
466
467 for (i = 0; i < uu->count; ++i) {
468 free_decl_union(uu->list[i]);
469 }
470 free(uu->list);
471 free(uu);
472 }
473
474 typedef struct impl_type {
475 char *name;
476 token_t type;
477 } impl_type;
478
479 static inline impl_type *init_impl_type(token_t type, const char *name) {
480 impl_type *t = calloc(1, sizeof(*t));
481
482 t->type = type;
483 t->name = strdup(name);
484 return t;
485 }
486
487 static inline void free_impl_type(impl_type *type) {
488 free(type->name);
489 free(type);
490 }
491
492 typedef struct impl_var {
493 PSI_Token *token;
494 char *name;
495 struct impl_arg *arg;
496 unsigned reference:1;
497 } impl_var;
498
499 static inline impl_var *init_impl_var(const char *name, int is_reference) {
500 impl_var *var = calloc(1, sizeof(*var));
501 var->name = strdup(name);
502 var->reference = is_reference;
503 return var;
504 }
505
506 static inline impl_var *copy_impl_var(impl_var *var) {
507 impl_var *cpy = malloc(sizeof(*cpy));
508
509 memcpy(cpy, var, sizeof(*cpy));
510 cpy->name = strdup(cpy->name);
511 if (cpy->token) {
512 cpy->token = PSI_TokenCopy(cpy->token);
513 }
514 return cpy;
515 }
516
517 static inline void free_impl_var(impl_var *var) {
518 if (var->token) {
519 free(var->token);
520 }
521 free(var->name);
522 free(var);
523 }
524
525 typedef struct impl_def_val {
526 token_t type;
527 char *text;
528 } impl_def_val;
529
530 static inline impl_def_val *init_impl_def_val(token_t t, const char *text) {
531 impl_def_val *def = calloc(1, sizeof(*def));
532 def->type = t;
533 def->text = strdup(text);
534 return def;
535 }
536
537 static inline void free_impl_def_val(impl_def_val *def) {
538 free(def->text);
539 free(def);
540 }
541
542 typedef struct const_type {
543 token_t type;
544 char *name;
545 } const_type;
546
547 static inline const_type *init_const_type(token_t type, const char *name) {
548 const_type *ct = calloc(1, sizeof(*ct));
549 ct->type = type;
550 ct->name = strdup(name);
551 return ct;
552 }
553
554 static inline void free_const_type(const_type *type) {
555 free(type->name);
556 free(type);
557 }
558
559 typedef struct constant {
560 const_type *type;
561 char *name;
562 impl_def_val *val;
563 } constant;
564
565 static inline constant *init_constant(const_type *type, const char *name, impl_def_val *val) {
566 constant *c = calloc(1, sizeof(*c));
567 c->type = type;
568 c->name = strdup(name);
569 c->val = val;
570 return c;
571 }
572
573 static inline void free_constant(constant *constant) {
574 free_const_type(constant->type);
575 free(constant->name);
576 free_impl_def_val(constant->val);
577 free(constant);
578 }
579
580 typedef struct constants {
581 size_t count;
582 constant **list;
583 } constants;
584
585 static inline constants *add_constant(constants *constants, constant *constant) {
586 if (!constants) {
587 constants = calloc(1, sizeof(*constants));
588 }
589 constants->list = realloc(constants->list, ++constants->count * sizeof(*constants->list));
590 constants->list[constants->count-1] = constant;
591 return constants;
592 }
593
594 static inline void free_constants(constants *c) {
595 size_t i;
596
597 for (i = 0; i < c->count; ++i) {
598 free_constant(c->list[i]);
599 }
600 free(c->list);
601 free(c);
602 }
603
604 typedef struct impl_arg {
605 impl_type *type;
606 impl_var *var;
607 impl_def_val *def;
608 impl_val val;
609 zval *_zv;
610 } impl_arg;
611
612 static inline impl_arg *init_impl_arg(impl_type *type, impl_var *var, impl_def_val *def) {
613 impl_arg *arg = calloc(1, sizeof(*arg));
614 arg->type = type;
615 arg->var = var;
616 arg->var->arg = arg;
617 arg->def = def;
618 return arg;
619 }
620
621 static inline void free_impl_arg(impl_arg *arg) {
622 free_impl_type(arg->type);
623 free_impl_var(arg->var);
624 if (arg->def) {
625 free_impl_def_val(arg->def);
626 }
627 free(arg);
628 }
629
630 typedef struct impl_vararg {
631 impl_arg *name;
632 struct impl_args *args;
633 token_t *types;
634 impl_val *values;
635 void **free_list;
636 } impl_vararg;
637
638 typedef struct impl_args {
639 impl_arg **args;
640 size_t count;
641 impl_vararg vararg;
642 } impl_args;
643
644 static inline impl_args *init_impl_args(impl_arg *arg) {
645 impl_args *args = calloc(1, sizeof(*args));
646 if (arg) {
647 args->count = 1;
648 args->args = calloc(1, sizeof(*args->args));
649 args->args[0] = arg;
650 }
651 return args;
652 }
653
654 static inline impl_args *add_impl_arg(impl_args *args, impl_arg *arg) {
655 args->args = realloc(args->args, ++args->count * sizeof(*args->args));
656 args->args[args->count-1] = arg;
657 return args;
658 }
659
660 static inline void free_impl_args(impl_args *args) {
661 size_t i;
662
663 for (i = 0; i < args->count; ++i) {
664 free_impl_arg(args->args[i]);
665 }
666 if (args->vararg.name) {
667 free_impl_arg(args->vararg.name);
668 }
669 free(args->args);
670 free(args);
671 }
672
673 typedef struct impl_func {
674 PSI_Token *token;
675 char *name;
676 impl_args *args;
677 impl_type *return_type;
678 unsigned return_reference:1;
679 } impl_func;
680
681 static inline impl_func *init_impl_func(char *name, impl_args *args, impl_type *type, int ret_reference) {
682 impl_func *func = calloc(1, sizeof(*func));
683 func->name = strdup(name);
684 func->args = args ? args : init_impl_args(NULL);
685 func->return_type = type;
686 func->return_reference = ret_reference;
687 return func;
688 }
689
690 static inline void free_impl_func(impl_func *f) {
691 if (f->token) {
692 free(f->token);
693 }
694 free_impl_type(f->return_type);
695 free_impl_args(f->args);
696 free(f->name);
697 free(f);
698 }
699
700 typedef struct num_exp {
701 PSI_Token *token;
702 token_t t;
703 union {
704 char *numb;
705 constant *cnst;
706 decl_var *dvar;
707 struct decl_enum_item *enm;
708 } u;
709 token_t operator;
710 int (*calculator)(int t1, impl_val *v1, int t2, impl_val *v2, impl_val *res);
711 struct num_exp *operand;
712 } num_exp;
713
714 static inline num_exp *init_num_exp(token_t t, void *num) {
715 num_exp *exp = calloc(1, sizeof(*exp));
716 switch (exp->t = t) {
717 case PSI_T_NUMBER:
718 case PSI_T_NSNAME:
719 exp->u.numb = strdup(num);
720 break;
721 case PSI_T_NAME:
722 exp->u.dvar = num;
723 break;
724 EMPTY_SWITCH_DEFAULT_CASE();
725 }
726 return exp;
727 }
728
729 static inline num_exp *copy_num_exp(num_exp *exp) {
730 decl_var *dvar;
731 num_exp *num = calloc(1, sizeof(*num));
732
733 memcpy(num, exp, sizeof(*num));
734
735 if (num->token) {
736 num->token = PSI_TokenCopy(num->token);
737 }
738 if (num->operand) {
739 num->operand = copy_num_exp(num->operand);
740 }
741 switch (num->t) {
742 case PSI_T_NUMBER:
743 case PSI_T_NSNAME:
744 num->u.numb = strdup(num->u.numb);
745 break;
746 case PSI_T_NAME:
747 dvar = init_decl_var(num->u.dvar->name, num->u.dvar->pointer_level, num->u.dvar->array_size);
748 dvar->arg = num->u.dvar->arg;
749 if (num->u.dvar->token) {
750 dvar->token = PSI_TokenCopy(num->u.dvar->token);
751 }
752 num->u.dvar = dvar;
753 break;
754 }
755 return num;
756 }
757
758 static inline void free_num_exp(num_exp *exp) {
759 if (exp->token) {
760 free(exp->token);
761 }
762 switch (exp->t) {
763 case PSI_T_NUMBER:
764 free(exp->u.numb);
765 break;
766 case PSI_T_NSNAME:
767 break;
768 case PSI_T_NAME:
769 free_decl_var(exp->u.dvar);
770 break;
771 case PSI_T_ENUM:
772 break;
773 EMPTY_SWITCH_DEFAULT_CASE();
774 }
775 if (exp->operand) {
776 free_num_exp(exp->operand);
777 }
778 free(exp);
779 }
780
781 typedef struct decl_enum_item {
782 PSI_Token *token;
783 char *name;
784 num_exp *num;
785 num_exp inc;
786 struct decl_enum_item *prev;
787 } decl_enum_item;
788
789 static inline decl_enum_item *init_decl_enum_item(const char *name, num_exp *num) {
790 decl_enum_item *i = calloc(1, sizeof(*i));
791
792 i->name = strdup(name);
793 i->num = num;
794 return i;
795 }
796
797 static inline void free_decl_enum_item(decl_enum_item *i) {
798 if (i->token) {
799 free(i->token);
800 }
801 if (i->num && i->num != &i->inc) {
802 free_num_exp(i->num);
803 }
804 free(i->name);
805 free(i);
806 }
807
808 typedef struct decl_enum_items {
809 decl_enum_item **list;
810 size_t count;
811 } decl_enum_items;
812
813 static inline decl_enum_items *init_decl_enum_items(decl_enum_item *i) {
814 decl_enum_items *l = calloc(1, sizeof(*l));
815
816 if (i) {
817 l->count = 1;
818 l->list = calloc(1, sizeof(*l->list));
819 l->list[0] = i;
820 }
821 return l;
822 }
823
824 static inline decl_enum_items *add_decl_enum_item(decl_enum_items *l, decl_enum_item *i) {
825 l->list = realloc(l->list, sizeof(*l->list) * (l->count + 1));
826 l->list[l->count] = i;
827 if (l->count) {
828 i->prev = l->list[l->count - 1];
829 }
830 ++l->count;
831 return l;
832 }
833
834 static inline void free_decl_enum_items(decl_enum_items *l) {
835 if (l->list) {
836 size_t j;
837
838 for (j = 0; j < l->count; ++j) {
839 free_decl_enum_item(l->list[j]);
840 }
841 free(l->list);
842 }
843 free(l);
844 }
845
846 typedef struct decl_enum {
847 PSI_Token *token;
848 char *name;
849 decl_enum_items *items;
850 } decl_enum;
851
852 static inline decl_enum *init_decl_enum(const char *name, decl_enum_items *l) {
853 decl_enum *e = calloc(1, sizeof(*e));
854
855 e->name = strdup(name);
856 e->items = l;
857 return e;
858 }
859
860 static inline void free_decl_enum(decl_enum *e) {
861 if (e->token) {
862 free(e->token);
863 }
864 if (e->items) {
865 free_decl_enum_items(e->items);
866 }
867 free(e->name);
868 free(e);
869 }
870
871 typedef struct decl_enums {
872 decl_enum **list;
873 size_t count;
874 } decl_enums;
875
876 static inline decl_enums* add_decl_enum(decl_enums *es, decl_enum *e) {
877 if (!es) {
878 es = calloc(1, sizeof(*es));
879 }
880 es->list = realloc(es->list, ++es->count * sizeof(*es->list));
881 es->list[es->count-1] = e;
882 return es;
883 }
884
885 static inline void free_decl_enums(decl_enums *es) {
886 if (es->list) {
887 size_t j;
888
889 for (j = 0; j < es->count; ++j) {
890 free_decl_enum(es->list[j]);
891 }
892 }
893 free(es->list);
894 free(es);
895 }
896
897 typedef struct let_calloc {
898 num_exp *nmemb;
899 num_exp *size;
900 } let_calloc;
901
902 static inline let_calloc *init_let_calloc(num_exp *nmemb, num_exp *size) {
903 let_calloc *alloc = calloc(1, sizeof(*alloc));
904 alloc->nmemb = nmemb;
905 alloc->size = size;
906 return alloc;
907 }
908
909 static inline void free_let_calloc(let_calloc *alloc) {
910 free_num_exp(alloc->nmemb);
911 free_num_exp(alloc->size);
912 free(alloc);
913 }
914
915 typedef struct let_callback {
916 struct let_func *func;
917 struct set_values *args;
918 decl *decl;
919 } let_callback;
920
921 static inline void free_let_func(struct let_func *func);
922 static inline void free_set_values(struct set_values *vals);
923 static inline let_callback *init_let_callback(struct let_func *func, struct set_values *args) {
924 let_callback *cb = calloc(1, sizeof(*cb));
925
926 cb->func = func;
927 cb->args = args;
928 return cb;
929 }
930
931 static inline void free_let_callback(let_callback *cb) {
932 free_let_func(cb->func);
933 free_set_values(cb->args);
934 free(cb);
935 }
936
937 typedef impl_val *(*let_func_handler)(impl_val *tmp, decl_type *type, impl_arg *iarg, void **to_free);
938
939 typedef struct let_func {
940 token_t type;
941 char *name;
942 impl_var *var;
943 let_func_handler handler;
944 } let_func;
945
946 static inline let_func *init_let_func(token_t type, const char *name, impl_var *var) {
947 let_func *func = calloc(1, sizeof(*func));
948 func->type = type;
949 func->name = strdup(name);
950 func->var = var;
951 return func;
952 }
953
954 static inline void free_let_func(let_func *func) {
955 free_impl_var(func->var);
956 free(func->name);
957 free(func);
958 }
959
960 enum let_val_kind {
961 PSI_LET_NULL,
962 PSI_LET_NUMEXP,
963 PSI_LET_CALLOC,
964 PSI_LET_CALLBACK,
965 PSI_LET_FUNC,
966 PSI_LET_TMP,
967 };
968 #define PSI_LET_REFERENCE 0x1;
969 typedef struct let_val {
970 enum let_val_kind kind;
971 union {
972 num_exp *num;
973 let_calloc *alloc;
974 let_callback *callback;
975 let_func *func;
976 decl_var *var;
977 } data;
978 union {
979 struct {
980 unsigned is_reference:1;
981 } one;
982 unsigned all;
983 } flags;
984 } let_val;
985
986 static inline let_val *init_let_val(enum let_val_kind kind, void *data) {
987 let_val *let = calloc(1, sizeof(*let));
988 switch (let->kind = kind) {
989 case PSI_LET_NULL:
990 break;
991 case PSI_LET_NUMEXP:
992 let->data.num = data;
993 break;
994 case PSI_LET_CALLOC:
995 let->data.alloc = data;
996 break;
997 case PSI_LET_CALLBACK:
998 let->data.callback = data;
999 break;
1000 case PSI_LET_FUNC:
1001 let->data.func = data;
1002 break;
1003 case PSI_LET_TMP:
1004 let->data.var = data;
1005 break;
1006 EMPTY_SWITCH_DEFAULT_CASE();
1007 }
1008 return let;
1009 }
1010
1011 static inline void free_let_val(let_val *let) {
1012 switch (let->kind) {
1013 case PSI_LET_NULL:
1014 break;
1015 case PSI_LET_NUMEXP:
1016 free_num_exp(let->data.num);
1017 break;
1018 case PSI_LET_CALLOC:
1019 free_let_calloc(let->data.alloc);
1020 break;
1021 case PSI_LET_CALLBACK:
1022 free_let_callback(let->data.callback);
1023 break;
1024 case PSI_LET_FUNC:
1025 free_let_func(let->data.func);
1026 break;
1027 case PSI_LET_TMP:
1028 free_decl_var(let->data.var);
1029 break;
1030 EMPTY_SWITCH_DEFAULT_CASE();
1031 }
1032 free(let);
1033 }
1034
1035 typedef struct let_stmt {
1036 decl_var *var;
1037 let_val *val;
1038 } let_stmt;
1039
1040 static inline let_stmt *init_let_stmt(decl_var *var, let_val *val) {
1041 let_stmt *let = calloc(1, sizeof(*let));
1042 let->var = var;
1043 let->val = val;
1044 return let;
1045 }
1046
1047 static inline void free_let_stmt(let_stmt *stmt) {
1048 if (stmt->val) {
1049 if (stmt->val->kind == PSI_LET_TMP && stmt->var->arg) {
1050 free_decl_arg(stmt->var->arg);
1051 }
1052 free_let_val(stmt->val);
1053 }
1054 free_decl_var(stmt->var);
1055 free(stmt);
1056 }
1057
1058 struct set_value;
1059
1060 typedef struct set_func {
1061 PSI_Token *token;
1062 token_t type;
1063 char *name;
1064 void (*handler)(zval *, struct set_value *set, impl_val *ret_val);
1065 } set_func;
1066
1067 static inline set_func *init_set_func(token_t type, const char *name) {
1068 set_func *func = calloc(1, sizeof(*func));
1069 func->type = type;
1070 func->name = strdup(name);
1071 return func;
1072 }
1073
1074 static inline void free_set_func(set_func *func) {
1075 if (func->token) {
1076 free(func->token);
1077 }
1078 free(func->name);
1079 free(func);
1080 }
1081
1082 typedef struct set_value {
1083 set_func *func;
1084 decl_vars *vars;
1085 num_exp *num;
1086 struct {
1087 struct set_value *set;
1088 impl_val *val;
1089 } outer;
1090 struct set_values *inner;
1091 } set_value;
1092
1093 typedef struct set_values {
1094 set_value **vals;
1095 size_t count;
1096 } set_values;
1097
1098
1099 static inline set_value *init_set_value(set_func *func, decl_vars *vars) {
1100 set_value *val = calloc(1, sizeof(*val));
1101 val->func = func;
1102 val->vars = vars;
1103 return val;
1104 }
1105
1106 static inline set_values *add_set_value(set_values *vals, set_value *val);
1107 static inline set_value *add_inner_set_value(set_value *val, set_value *inner) {
1108 val->inner = add_set_value(val->inner, inner);
1109 inner->outer.set = val;
1110 return val;
1111 }
1112
1113 static inline void free_set_value(set_value *val) {
1114 if (val->func) {
1115 free_set_func(val->func);
1116 }
1117 if (val->vars) {
1118 free_decl_vars(val->vars);
1119 }
1120 if (val->inner && (!val->outer.set || val->outer.set->inner != val->inner)) {
1121 free_set_values(val->inner);
1122 }
1123 if (val->num) {
1124 free_num_exp(val->num);
1125 }
1126 free(val);
1127 }
1128
1129 static inline set_values *init_set_values(set_value *val) {
1130 set_values *vals = calloc(1, sizeof(*vals));
1131 if (val) {
1132 vals->count = 1;
1133 vals->vals = calloc(1, sizeof(val));
1134 vals->vals[0] = val;
1135 }
1136 return vals;
1137 }
1138
1139 static inline set_values *add_set_value(set_values *vals, set_value *val) {
1140 if (!vals) {
1141 vals = calloc(1, sizeof(*vals));
1142 }
1143 vals->vals = realloc(vals->vals, ++vals->count * sizeof(val));
1144 vals->vals[vals->count-1] = val;
1145 return vals;
1146 }
1147
1148 static inline void free_set_values(set_values *vals) {
1149 if (vals->vals) {
1150 size_t i;
1151
1152 for (i = 0; i < vals->count; ++i) {
1153 free_set_value(vals->vals[i]);
1154 }
1155 free(vals->vals);
1156 }
1157 free(vals);
1158 }
1159
1160 typedef struct set_stmt {
1161 impl_var *var;
1162 set_value *val;
1163 impl_arg *arg;
1164 } set_stmt;
1165
1166 static inline set_stmt *init_set_stmt(impl_var *var, set_value *val) {
1167 set_stmt *set = calloc(1, sizeof(*set));
1168 set->var = var;
1169 set->val = val;
1170 return set;
1171 }
1172
1173 static inline void free_set_stmt(set_stmt *set) {
1174 free_impl_var(set->var);
1175 free_set_value(set->val);
1176 free(set);
1177 }
1178
1179 typedef struct return_stmt {
1180 PSI_Token *token;
1181 set_value *set;
1182 decl_arg *decl;
1183 } return_stmt;
1184
1185 static inline return_stmt *init_return_stmt(set_value *val) {
1186 return_stmt *ret = calloc(1, sizeof(*ret));
1187 ret->set = val;
1188 return ret;
1189 }
1190
1191 static inline void free_return_stmt(return_stmt *ret) {
1192 if (ret->token) {
1193 free(ret->token);
1194 }
1195 free_set_value(ret->set);
1196 free(ret);
1197 }
1198
1199 typedef struct free_call {
1200 PSI_Token *token;
1201 char *func;
1202 decl_vars *vars;
1203 decl *decl;
1204 } free_call;
1205
1206 static inline free_call *init_free_call(const char *func, decl_vars *vars) {
1207 free_call *f = calloc(1, sizeof(*f));
1208 f->func = strdup(func);
1209 f->vars = vars;
1210 return f;
1211 }
1212
1213 static inline void free_free_call(free_call *f) {
1214 if (f->token) {
1215 free(f->token);
1216 }
1217 free(f->func);
1218 free_decl_vars(f->vars);
1219 free(f);
1220 }
1221
1222 typedef struct free_calls {
1223 free_call **list;
1224 size_t count;
1225 } free_calls;
1226
1227 static inline free_calls *init_free_calls(free_call *f) {
1228 free_calls *fcs = calloc(1, sizeof(*fcs));
1229 if (f) {
1230 fcs->count = 1;
1231 fcs->list = calloc(1, sizeof(*fcs->list));
1232 fcs->list[0] = f;
1233 }
1234 return fcs;
1235 }
1236
1237 static inline void free_free_calls(free_calls *fcs) {
1238 size_t i;
1239
1240 for (i = 0; i < fcs->count; ++i) {
1241 free_free_call(fcs->list[i]);
1242 }
1243 free(fcs->list);
1244 free(fcs);
1245 }
1246
1247 static inline free_calls *add_free_call(free_calls *fcs, free_call *f) {
1248 fcs->list = realloc(fcs->list, ++fcs->count * sizeof(*fcs->list));
1249 fcs->list[fcs->count-1] = f;
1250 return fcs;
1251 }
1252
1253 typedef struct free_stmt {
1254 free_calls *calls;
1255 } free_stmt;
1256
1257 static inline free_stmt *init_free_stmt(free_calls *calls) {
1258 free_stmt *f = calloc(1, sizeof(*f));
1259 f->calls = calls;
1260 return f;
1261 }
1262
1263 static inline void free_free_stmt(free_stmt *f) {
1264 free_free_calls(f->calls);
1265 free(f);
1266 }
1267
1268 typedef struct impl_stmt {
1269 token_t type;
1270 union {
1271 let_stmt *let;
1272 set_stmt *set;
1273 return_stmt *ret;
1274 free_stmt *fre;
1275 void *ptr;
1276 } s;
1277 } impl_stmt;
1278
1279 static inline impl_stmt *init_impl_stmt(token_t type, void *ptr) {
1280 impl_stmt *stmt = calloc(1, sizeof(*stmt));
1281 stmt->type = type;
1282 stmt->s.ptr = ptr;
1283 return stmt;
1284 }
1285
1286 static inline void free_impl_stmt(impl_stmt *stmt) {
1287 switch (stmt->type) {
1288 case PSI_T_LET:
1289 free_let_stmt(stmt->s.let);
1290 break;
1291 case PSI_T_SET:
1292 free_set_stmt(stmt->s.set);
1293 break;
1294 case PSI_T_RETURN:
1295 free_return_stmt(stmt->s.ret);
1296 break;
1297 case PSI_T_FREE:
1298 free_free_stmt(stmt->s.fre);
1299 break;
1300 }
1301 free(stmt);
1302 }
1303
1304 typedef struct impl_stmts {
1305 struct {
1306 return_stmt **list;
1307 size_t count;
1308 } ret;
1309 struct {
1310 let_stmt **list;
1311 size_t count;
1312 } let;
1313 struct {
1314 set_stmt **list;
1315 size_t count;
1316 } set;
1317 struct {
1318 free_stmt **list;
1319 size_t count;
1320 } fre;
1321 } impl_stmts;
1322
1323 static inline void *add_impl_stmt_ex(void *list, size_t count, void *stmt) {
1324 list = realloc(list, count * sizeof(list));
1325 ((void **)list)[count-1] = stmt;
1326 return list;
1327 }
1328
1329 static inline impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt) {
1330 switch (stmt->type) {
1331 case PSI_T_RETURN:
1332 stmts->ret.list = add_impl_stmt_ex(stmts->ret.list, ++stmts->ret.count, stmt->s.ret);
1333 break;
1334 case PSI_T_LET:
1335 stmts->let.list = add_impl_stmt_ex(stmts->let.list, ++stmts->let.count, stmt->s.let);
1336 break;
1337 case PSI_T_SET:
1338 stmts->set.list = add_impl_stmt_ex(stmts->set.list, ++stmts->set.count, stmt->s.set);
1339 break;
1340 case PSI_T_FREE:
1341 stmts->fre.list = add_impl_stmt_ex(stmts->fre.list, ++stmts->fre.count, stmt->s.fre);
1342 break;
1343 }
1344 free(stmt);
1345 return stmts;
1346 }
1347
1348 static inline impl_stmts *init_impl_stmts(impl_stmt *stmt) {
1349 impl_stmts *stmts = calloc(1, sizeof(*stmts));
1350 return add_impl_stmt(stmts, stmt);
1351 }
1352
1353 static inline void free_impl_stmts(impl_stmts *stmts) {
1354 size_t i;
1355
1356 for (i = 0; i < stmts->let.count; ++i) {
1357 free_let_stmt(stmts->let.list[i]);
1358 }
1359 free(stmts->let.list);
1360 for (i = 0; i < stmts->ret.count; ++i) {
1361 free_return_stmt(stmts->ret.list[i]);
1362 }
1363 free(stmts->ret.list);
1364 for (i = 0; i < stmts->set.count; ++i) {
1365 free_set_stmt(stmts->set.list[i]);
1366 }
1367 free(stmts->set.list);
1368 for (i = 0; i < stmts->fre.count; ++i) {
1369 free_free_stmt(stmts->fre.list[i]);
1370 }
1371 free(stmts->fre.list);
1372 free(stmts);
1373 }
1374
1375 typedef struct impl {
1376 impl_func *func;
1377 impl_stmts *stmts;
1378 decl *decl;
1379 } impl;
1380
1381 static inline impl *init_impl(impl_func *func, impl_stmts *stmts) {
1382 impl *i = calloc(1, sizeof(*i));
1383 i->func = func;
1384 i->stmts = stmts;
1385 return i;
1386 }
1387
1388 static inline void free_impl(impl *impl) {
1389 free_impl_func(impl->func);
1390 free_impl_stmts(impl->stmts);
1391 free(impl);
1392 }
1393
1394 typedef struct impls {
1395 size_t count;
1396 impl **list;
1397 } impls;
1398
1399 static inline impls *add_impl(impls *impls, impl *impl) {
1400 if (!impls) {
1401 impls = calloc(1, sizeof(*impls));
1402 }
1403 impls->list = realloc(impls->list, ++impls->count * sizeof(*impls->list));
1404 impls->list[impls->count-1] = impl;
1405 return impls;
1406 }
1407
1408 static void free_impls(impls *impls) {
1409 size_t i;
1410
1411 for (i = 0; i < impls->count; ++i) {
1412 free_impl(impls->list[i]);
1413 }
1414 free(impls->list);
1415 free(impls);
1416 }
1417
1418 typedef struct decl_file {
1419 char *ln;
1420 char *fn;
1421 } decl_file;
1422
1423 static inline void free_decl_file(decl_file *file) {
1424 if (file->ln) {
1425 free(file->ln);
1426 }
1427 if (file->fn) {
1428 free(file->fn);
1429 }
1430 memset(file, 0, sizeof(*file));
1431 }
1432
1433 typedef struct decl_libs {
1434 void **dl;
1435 size_t count;
1436 } decl_libs;
1437
1438 static inline void free_decl_libs(decl_libs *libs) {
1439 if (libs->dl) {
1440 size_t i;
1441 for (i = 0; i < libs->count; ++i) {
1442 if (libs->dl[i]) {
1443 dlclose(libs->dl[i]);
1444 }
1445 }
1446 free(libs->dl);
1447 }
1448 memset(libs, 0, sizeof(*libs));
1449 }
1450
1451 static inline void add_decl_lib(decl_libs *libs, void *dlopened) {
1452 libs->dl = realloc(libs->dl, ++libs->count * sizeof(*libs->dl));
1453 libs->dl[libs->count-1] = dlopened;
1454 }
1455
1456 static inline impl_val *deref_impl_val(impl_val *ret_val, decl_var *var) {
1457 unsigned i;
1458
1459 ZEND_ASSERT(var->arg->var != var);
1460 #if 0
1461 fprintf(stderr, "deref: %s pl=%u:%u as=%u:%u %p\n",
1462 var->name, var->pointer_level, var->arg->var->pointer_level,
1463 var->array_size, var->arg->var->array_size, ret_val);
1464 #endif
1465 for (i = 0; i < var->pointer_level; ++i) {
1466 #if 0
1467 fprintf(stderr, "-- %p %p %p\n", ret_val, *(void**)ret_val, ret_val->ptr);
1468 #endif
1469 ret_val = *(void **) ret_val;
1470 }
1471 return ret_val;
1472 }
1473
1474 static inline impl_val *enref_impl_val(void *ptr, decl_var *var) {
1475 impl_val *val, *val_ptr;
1476 unsigned i;
1477
1478 ZEND_ASSERT(var->arg->var == var);
1479 #if 0
1480 fprintf(stderr, "enref: %s pl=%u:%u as=%u:%u\n",
1481 var->name, var->pointer_level, var->arg->var->pointer_level,
1482 var->array_size, var->arg->var->array_size);
1483 #endif
1484 if (!var->pointer_level ){//&& real_decl_type(var->arg->type)->type != PSI_T_STRUCT) {
1485 return ptr;
1486 }
1487
1488 val = val_ptr = calloc(var->pointer_level + 1, sizeof(void *));
1489 for (i = !var->arg->var->array_size; i < var->pointer_level; ++i) {
1490 #if 0
1491 fprintf(stderr, "++\n");
1492 #endif
1493 val_ptr->ptr = (void **) val_ptr + 1;
1494 val_ptr = val_ptr->ptr;
1495 }
1496 val_ptr->ptr = ptr;
1497 return val;
1498 }
1499
1500 static inline impl_val *struct_member_ref(decl_arg *set_arg, impl_val *struct_ptr, impl_val **to_free) {
1501 void *ptr = (char *) struct_ptr + set_arg->layout->pos;
1502 #if 0
1503 fprintf(stderr, "struct member %s: %p\n", set_arg->var->name, ptr);
1504 #endif
1505 return ptr;
1506 }
1507
1508
1509 #define PSI_ERROR 16
1510 #define PSI_WARNING 32
1511 typedef void (*psi_error_cb)(void *context, PSI_Token *token, int type, const char *msg, ...);
1512
1513 #define PSI_DATA(D) ((PSI_Data *) (D))
1514 #define PSI_DATA_MEMBERS \
1515 constants *consts; \
1516 decl_typedefs *defs; \
1517 decl_structs *structs; \
1518 decl_unions *unions; \
1519 decl_enums *enums; \
1520 decls *decls; \
1521 impls *impls; \
1522 union { \
1523 decl_file file; \
1524 decl_libs libs; \
1525 } psi; \
1526 psi_error_cb error; \
1527 unsigned errors; \
1528 unsigned flags
1529 typedef struct PSI_Data {
1530 PSI_DATA_MEMBERS;
1531 } PSI_Data;
1532
1533 static inline PSI_Data *PSI_DataExchange(PSI_Data *dest, PSI_Data *src) {
1534 if (!dest) {
1535 dest = malloc(sizeof(*dest));
1536 }
1537 memcpy(dest, src, sizeof(*dest));
1538 memset(src, 0, sizeof(*src));
1539 return dest;
1540 }
1541
1542 static inline void PSI_DataDtor(PSI_Data *data) {
1543 if (data->consts) {
1544 free_constants(data->consts);
1545 }
1546 if (data->defs) {
1547 free_decl_typedefs(data->defs);
1548 }
1549 if (data->structs) {
1550 free_decl_structs(data->structs);
1551 }
1552 if (data->unions) {
1553 free_decl_unions(data->unions);
1554 }
1555 if (data->enums) {
1556 free_decl_enums(data->enums);
1557 }
1558 if (data->decls) {
1559 free_decls(data->decls);
1560 }
1561 if (data->impls) {
1562 free_impls(data->impls);
1563 }
1564 free_decl_file(&data->psi.file);
1565 }
1566
1567 typedef struct PSI_Parser {
1568 PSI_DATA_MEMBERS;
1569 FILE *fp;
1570 token_t num;
1571 void *proc;
1572 unsigned line, col;
1573 char *cur, *tok, *lim, *eof, *ctx, *mrk, buf[BSIZE];
1574 } PSI_Parser;
1575
1576 static inline size_t PSI_TokenAllocSize(size_t token_len, size_t fname_len) {
1577 return sizeof(PSI_Token) + token_len + fname_len + 2;
1578 }
1579
1580 static inline PSI_Token *PSI_TokenAlloc(PSI_Parser *P) {
1581 PSI_Token *T;
1582 size_t token_len, fname_len;
1583 token_t token_typ;
1584
1585 if (P->cur < P->tok) {
1586 return NULL;
1587 }
1588
1589 token_typ = P->num;
1590 token_len = P->cur - P->tok;
1591 fname_len = strlen(P->psi.file.fn);
1592
1593 T = calloc(1, PSI_TokenAllocSize(token_len, fname_len));
1594 T->type = token_typ;
1595 T->size = token_len;
1596 T->text = &T->buf[0];
1597 T->file = &T->buf[token_len + 1];
1598 T->line = P->line;
1599 T->col = P->col;
1600
1601 memcpy(T->text, P->tok, token_len);
1602 memcpy(T->file, P->psi.file.fn, fname_len);
1603
1604 return T;
1605 }
1606
1607 static inline PSI_Token *PSI_TokenCopy(PSI_Token *src) {
1608 size_t strct_len = PSI_TokenAllocSize(src->size, strlen(src->file));
1609 PSI_Token *ptr = malloc(strct_len);
1610
1611 memcpy(ptr, src, strct_len);
1612
1613 ptr->text = &ptr->buf[0];
1614 ptr->file = &ptr->buf[ptr->size + 1];
1615
1616 return ptr;
1617 }
1618
1619 static inline PSI_Token *PSI_TokenCat(unsigned argc, ...) {
1620 va_list argv;
1621 unsigned i;
1622 PSI_Token *T = NULL;
1623
1624 va_start(argv, argc);
1625 for (i = 0; i < argc; ++i) {
1626 PSI_Token *arg = va_arg(argv, PSI_Token *);
1627
1628 if (T) {
1629 size_t token_len = T->size, fname_len = strlen(T->file);
1630
1631 T = realloc(T, PSI_TokenAllocSize(T->size += arg->size + 1, fname_len));
1632 T->text = &T->buf[0];
1633 T->file = &T->buf[T->size + 1];
1634 T->buf[token_len] = ' ';
1635 memmove(&T->buf[T->size + 1], &T->buf[token_len + 1], fname_len + 1);
1636 memcpy(&T->buf[token_len + 1], arg->text, arg->size + 1);
1637 } else {
1638 T = PSI_TokenCopy(arg);
1639 T->type = PSI_T_NAME;
1640 }
1641 }
1642 va_end(argv);
1643
1644 return T;
1645 }
1646
1647 static inline PSI_Token *PSI_TokenAppend(PSI_Token *T, unsigned argc, ...) {
1648 va_list argv;
1649 unsigned i;
1650
1651 va_start(argv, argc);
1652 for (i = 0; i < argc; ++i) {
1653 char *str = va_arg(argv, char *);
1654 size_t str_len = strlen(str), token_len = T->size, fname_len = strlen(T->file);
1655
1656 T = realloc(T, PSI_TokenAllocSize(T->size += str_len + 1, fname_len));
1657 T->text = &T->buf[0];
1658 T->file = &T->buf[T->size + 1];
1659 T->buf[token_len] = ' ';
1660 memmove(&T->buf[T->size + 1], &T->buf[token_len + 1], fname_len + 1);
1661 memcpy(&T->buf[token_len + 1], str, str_len + 1);
1662 }
1663 va_end(argv);
1664
1665 return T;
1666 }
1667
1668 char *php_strtr(char *str, size_t len, char *str_from, char *str_to, size_t trlen);
1669 static inline PSI_Token *PSI_TokenTranslit(PSI_Token *T, char *from, char *to) {
1670 php_strtr(T->text, T->size, from, to, MIN(strlen(from), strlen(to)));
1671 return T;
1672 }
1673
1674 static inline uint64_t psi_hash(char *digest_buf, ...)
1675 {
1676 uint64_t hash = 5381;
1677 uint8_t c;
1678 const uint8_t *ptr;
1679 va_list argv;
1680
1681 va_start(argv, digest_buf);
1682 while ((ptr = va_arg(argv, const uint8_t *))) {
1683 while ((c = *ptr++)) {
1684 hash = ((hash << 5) + hash) + c;
1685 }
1686 }
1687 va_end(argv);
1688
1689 if (digest_buf) {
1690 sprintf(digest_buf, "%" PRIx64, hash);
1691 }
1692
1693 return hash;
1694 }
1695
1696 static inline uint64_t PSI_TokenHash(PSI_Token *t, char *digest_buf) {
1697 char loc_buf[48];
1698
1699 sprintf(loc_buf, "%u%u", t->line, t->col);
1700 return psi_hash(digest_buf, t->file, loc_buf, NULL);
1701 }
1702
1703 #define PSI_PARSER_DEBUG 0x1
1704 #define PSI_PARSER_SILENT 0x2
1705
1706 PSI_Parser *PSI_ParserInit(PSI_Parser *P, const char *filename, psi_error_cb error, unsigned flags);
1707 void PSI_ParserSyntaxError(PSI_Parser *P, const char *fn, size_t ln, const char *msg, ...);
1708 size_t PSI_ParserFill(PSI_Parser *P, size_t n);
1709 token_t PSI_ParserScan(PSI_Parser *P);
1710 void PSI_ParserParse(PSI_Parser *P, PSI_Token *src);
1711 void PSI_ParserDtor(PSI_Parser *P);
1712 void PSI_ParserFree(PSI_Parser **P);
1713
1714 #endif