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