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