made the real decl_type a union
[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 #define PSI_LET_REFERENCE 0x1;
961 typedef struct let_val {
962 enum let_val_kind {
963 PSI_LET_NULL,
964 PSI_LET_NUMEXP,
965 PSI_LET_CALLOC,
966 PSI_LET_CALLBACK,
967 PSI_LET_FUNC,
968 PSI_LET_TMP,
969 } kind;
970 union {
971 num_exp *num;
972 let_calloc *alloc;
973 let_callback *callback;
974 let_func *func;
975 decl_var *var;
976 } data;
977 union {
978 struct {
979 unsigned is_reference:1;
980 } one;
981 unsigned all;
982 } flags;
983 } let_val;
984
985 static inline let_val *init_let_val(enum let_val_kind kind, void *data) {
986 let_val *let = calloc(1, sizeof(*let));
987 switch (let->kind = kind) {
988 case PSI_LET_NULL:
989 break;
990 case PSI_LET_NUMEXP:
991 let->data.num = data;
992 break;
993 case PSI_LET_CALLOC:
994 let->data.alloc = data;
995 break;
996 case PSI_LET_CALLBACK:
997 let->data.callback = data;
998 break;
999 case PSI_LET_FUNC:
1000 let->data.func = data;
1001 break;
1002 case PSI_LET_TMP:
1003 let->data.var = data;
1004 break;
1005 EMPTY_SWITCH_DEFAULT_CASE();
1006 }
1007 return let;
1008 }
1009
1010 static inline void free_let_val(let_val *let) {
1011 switch (let->kind) {
1012 case PSI_LET_NULL:
1013 break;
1014 case PSI_LET_NUMEXP:
1015 free_num_exp(let->data.num);
1016 break;
1017 case PSI_LET_CALLOC:
1018 free_let_calloc(let->data.alloc);
1019 break;
1020 case PSI_LET_CALLBACK:
1021 free_let_callback(let->data.callback);
1022 break;
1023 case PSI_LET_FUNC:
1024 free_let_func(let->data.func);
1025 break;
1026 case PSI_LET_TMP:
1027 free_decl_var(let->data.var);
1028 break;
1029 EMPTY_SWITCH_DEFAULT_CASE();
1030 }
1031 free(let);
1032 }
1033
1034 typedef struct let_stmt {
1035 decl_var *var;
1036 let_val *val;
1037 } let_stmt;
1038
1039 static inline let_stmt *init_let_stmt(decl_var *var, let_val *val) {
1040 let_stmt *let = calloc(1, sizeof(*let));
1041 let->var = var;
1042 let->val = val;
1043 return let;
1044 }
1045
1046 static inline void free_let_stmt(let_stmt *stmt) {
1047 if (stmt->val) {
1048 if (stmt->val->kind == PSI_LET_TMP && stmt->var->arg) {
1049 free_decl_arg(stmt->var->arg);
1050 }
1051 free_let_val(stmt->val);
1052 }
1053 free_decl_var(stmt->var);
1054 free(stmt);
1055 }
1056
1057 struct set_value;
1058
1059 typedef struct set_func {
1060 PSI_Token *token;
1061 token_t type;
1062 char *name;
1063 void (*handler)(zval *, struct set_value *set, impl_val *ret_val);
1064 } set_func;
1065
1066 static inline set_func *init_set_func(token_t type, const char *name) {
1067 set_func *func = calloc(1, sizeof(*func));
1068 func->type = type;
1069 func->name = strdup(name);
1070 return func;
1071 }
1072
1073 static inline void free_set_func(set_func *func) {
1074 if (func->token) {
1075 free(func->token);
1076 }
1077 free(func->name);
1078 free(func);
1079 }
1080
1081 typedef struct set_value {
1082 set_func *func;
1083 decl_vars *vars;
1084 num_exp *num;
1085 struct {
1086 struct set_value *set;
1087 impl_val *val;
1088 } outer;
1089 struct set_values *inner;
1090 } set_value;
1091
1092 typedef struct set_values {
1093 set_value **vals;
1094 size_t count;
1095 } set_values;
1096
1097
1098 static inline set_value *init_set_value(set_func *func, decl_vars *vars) {
1099 set_value *val = calloc(1, sizeof(*val));
1100 val->func = func;
1101 val->vars = vars;
1102 return val;
1103 }
1104
1105 static inline set_values *add_set_value(set_values *vals, set_value *val);
1106 static inline set_value *add_inner_set_value(set_value *val, set_value *inner) {
1107 val->inner = add_set_value(val->inner, inner);
1108 inner->outer.set = val;
1109 return val;
1110 }
1111
1112 static inline void free_set_value(set_value *val) {
1113 if (val->func) {
1114 free_set_func(val->func);
1115 }
1116 if (val->vars) {
1117 free_decl_vars(val->vars);
1118 }
1119 if (val->inner && (!val->outer.set || val->outer.set->inner != val->inner)) {
1120 free_set_values(val->inner);
1121 }
1122 if (val->num) {
1123 free_num_exp(val->num);
1124 }
1125 free(val);
1126 }
1127
1128 static inline set_values *init_set_values(set_value *val) {
1129 set_values *vals = calloc(1, sizeof(*vals));
1130 if (val) {
1131 vals->count = 1;
1132 vals->vals = calloc(1, sizeof(val));
1133 vals->vals[0] = val;
1134 }
1135 return vals;
1136 }
1137
1138 static inline set_values *add_set_value(set_values *vals, set_value *val) {
1139 if (!vals) {
1140 vals = calloc(1, sizeof(*vals));
1141 }
1142 vals->vals = realloc(vals->vals, ++vals->count * sizeof(val));
1143 vals->vals[vals->count-1] = val;
1144 return vals;
1145 }
1146
1147 static inline void free_set_values(set_values *vals) {
1148 if (vals->vals) {
1149 size_t i;
1150
1151 for (i = 0; i < vals->count; ++i) {
1152 free_set_value(vals->vals[i]);
1153 }
1154 free(vals->vals);
1155 }
1156 free(vals);
1157 }
1158
1159 typedef struct set_stmt {
1160 impl_var *var;
1161 set_value *val;
1162 impl_arg *arg;
1163 } set_stmt;
1164
1165 static inline set_stmt *init_set_stmt(impl_var *var, set_value *val) {
1166 set_stmt *set = calloc(1, sizeof(*set));
1167 set->var = var;
1168 set->val = val;
1169 return set;
1170 }
1171
1172 static inline void free_set_stmt(set_stmt *set) {
1173 free_impl_var(set->var);
1174 free_set_value(set->val);
1175 free(set);
1176 }
1177
1178 typedef struct return_stmt {
1179 PSI_Token *token;
1180 set_value *set;
1181 decl_arg *decl;
1182 } return_stmt;
1183
1184 static inline return_stmt *init_return_stmt(set_value *val) {
1185 return_stmt *ret = calloc(1, sizeof(*ret));
1186 ret->set = val;
1187 return ret;
1188 }
1189
1190 static inline void free_return_stmt(return_stmt *ret) {
1191 if (ret->token) {
1192 free(ret->token);
1193 }
1194 free_set_value(ret->set);
1195 free(ret);
1196 }
1197
1198 typedef struct free_call {
1199 PSI_Token *token;
1200 char *func;
1201 decl_vars *vars;
1202 decl *decl;
1203 } free_call;
1204
1205 static inline free_call *init_free_call(const char *func, decl_vars *vars) {
1206 free_call *f = calloc(1, sizeof(*f));
1207 f->func = strdup(func);
1208 f->vars = vars;
1209 return f;
1210 }
1211
1212 static inline void free_free_call(free_call *f) {
1213 if (f->token) {
1214 free(f->token);
1215 }
1216 free(f->func);
1217 free_decl_vars(f->vars);
1218 free(f);
1219 }
1220
1221 typedef struct free_calls {
1222 free_call **list;
1223 size_t count;
1224 } free_calls;
1225
1226 static inline free_calls *init_free_calls(free_call *f) {
1227 free_calls *fcs = calloc(1, sizeof(*fcs));
1228 if (f) {
1229 fcs->count = 1;
1230 fcs->list = calloc(1, sizeof(*fcs->list));
1231 fcs->list[0] = f;
1232 }
1233 return fcs;
1234 }
1235
1236 static inline void free_free_calls(free_calls *fcs) {
1237 size_t i;
1238
1239 for (i = 0; i < fcs->count; ++i) {
1240 free_free_call(fcs->list[i]);
1241 }
1242 free(fcs->list);
1243 free(fcs);
1244 }
1245
1246 static inline free_calls *add_free_call(free_calls *fcs, free_call *f) {
1247 fcs->list = realloc(fcs->list, ++fcs->count * sizeof(*fcs->list));
1248 fcs->list[fcs->count-1] = f;
1249 return fcs;
1250 }
1251
1252 typedef struct free_stmt {
1253 free_calls *calls;
1254 } free_stmt;
1255
1256 static inline free_stmt *init_free_stmt(free_calls *calls) {
1257 free_stmt *f = calloc(1, sizeof(*f));
1258 f->calls = calls;
1259 return f;
1260 }
1261
1262 static inline void free_free_stmt(free_stmt *f) {
1263 free_free_calls(f->calls);
1264 free(f);
1265 }
1266
1267 typedef struct impl_stmt {
1268 token_t type;
1269 union {
1270 let_stmt *let;
1271 set_stmt *set;
1272 return_stmt *ret;
1273 free_stmt *fre;
1274 void *ptr;
1275 } s;
1276 } impl_stmt;
1277
1278 static inline impl_stmt *init_impl_stmt(token_t type, void *ptr) {
1279 impl_stmt *stmt = calloc(1, sizeof(*stmt));
1280 stmt->type = type;
1281 stmt->s.ptr = ptr;
1282 return stmt;
1283 }
1284
1285 static inline void free_impl_stmt(impl_stmt *stmt) {
1286 switch (stmt->type) {
1287 case PSI_T_LET:
1288 free_let_stmt(stmt->s.let);
1289 break;
1290 case PSI_T_SET:
1291 free_set_stmt(stmt->s.set);
1292 break;
1293 case PSI_T_RETURN:
1294 free_return_stmt(stmt->s.ret);
1295 break;
1296 case PSI_T_FREE:
1297 free_free_stmt(stmt->s.fre);
1298 break;
1299 }
1300 free(stmt);
1301 }
1302
1303 typedef struct impl_stmts {
1304 struct {
1305 return_stmt **list;
1306 size_t count;
1307 } ret;
1308 struct {
1309 let_stmt **list;
1310 size_t count;
1311 } let;
1312 struct {
1313 set_stmt **list;
1314 size_t count;
1315 } set;
1316 struct {
1317 free_stmt **list;
1318 size_t count;
1319 } fre;
1320 } impl_stmts;
1321
1322 static inline void *add_impl_stmt_ex(void *list, size_t count, void *stmt) {
1323 list = realloc(list, count * sizeof(list));
1324 ((void **)list)[count-1] = stmt;
1325 return list;
1326 }
1327
1328 static inline impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt) {
1329 switch (stmt->type) {
1330 case PSI_T_RETURN:
1331 stmts->ret.list = add_impl_stmt_ex(stmts->ret.list, ++stmts->ret.count, stmt->s.ret);
1332 break;
1333 case PSI_T_LET:
1334 stmts->let.list = add_impl_stmt_ex(stmts->let.list, ++stmts->let.count, stmt->s.let);
1335 break;
1336 case PSI_T_SET:
1337 stmts->set.list = add_impl_stmt_ex(stmts->set.list, ++stmts->set.count, stmt->s.set);
1338 break;
1339 case PSI_T_FREE:
1340 stmts->fre.list = add_impl_stmt_ex(stmts->fre.list, ++stmts->fre.count, stmt->s.fre);
1341 break;
1342 }
1343 free(stmt);
1344 return stmts;
1345 }
1346
1347 static inline impl_stmts *init_impl_stmts(impl_stmt *stmt) {
1348 impl_stmts *stmts = calloc(1, sizeof(*stmts));
1349 return add_impl_stmt(stmts, stmt);
1350 }
1351
1352 static inline void free_impl_stmts(impl_stmts *stmts) {
1353 size_t i;
1354
1355 for (i = 0; i < stmts->let.count; ++i) {
1356 free_let_stmt(stmts->let.list[i]);
1357 }
1358 free(stmts->let.list);
1359 for (i = 0; i < stmts->ret.count; ++i) {
1360 free_return_stmt(stmts->ret.list[i]);
1361 }
1362 free(stmts->ret.list);
1363 for (i = 0; i < stmts->set.count; ++i) {
1364 free_set_stmt(stmts->set.list[i]);
1365 }
1366 free(stmts->set.list);
1367 for (i = 0; i < stmts->fre.count; ++i) {
1368 free_free_stmt(stmts->fre.list[i]);
1369 }
1370 free(stmts->fre.list);
1371 free(stmts);
1372 }
1373
1374 typedef struct impl {
1375 impl_func *func;
1376 impl_stmts *stmts;
1377 decl *decl;
1378 } impl;
1379
1380 static inline impl *init_impl(impl_func *func, impl_stmts *stmts) {
1381 impl *i = calloc(1, sizeof(*i));
1382 i->func = func;
1383 i->stmts = stmts;
1384 return i;
1385 }
1386
1387 static inline void free_impl(impl *impl) {
1388 free_impl_func(impl->func);
1389 free_impl_stmts(impl->stmts);
1390 free(impl);
1391 }
1392
1393 typedef struct impls {
1394 size_t count;
1395 impl **list;
1396 } impls;
1397
1398 static inline impls *add_impl(impls *impls, impl *impl) {
1399 if (!impls) {
1400 impls = calloc(1, sizeof(*impls));
1401 }
1402 impls->list = realloc(impls->list, ++impls->count * sizeof(*impls->list));
1403 impls->list[impls->count-1] = impl;
1404 return impls;
1405 }
1406
1407 static void free_impls(impls *impls) {
1408 size_t i;
1409
1410 for (i = 0; i < impls->count; ++i) {
1411 free_impl(impls->list[i]);
1412 }
1413 free(impls->list);
1414 free(impls);
1415 }
1416
1417 typedef struct decl_file {
1418 char *ln;
1419 char *fn;
1420 } decl_file;
1421
1422 static inline void free_decl_file(decl_file *file) {
1423 if (file->ln) {
1424 free(file->ln);
1425 }
1426 if (file->fn) {
1427 free(file->fn);
1428 }
1429 memset(file, 0, sizeof(*file));
1430 }
1431
1432 typedef struct decl_libs {
1433 void **dl;
1434 size_t count;
1435 } decl_libs;
1436
1437 static inline void free_decl_libs(decl_libs *libs) {
1438 if (libs->dl) {
1439 size_t i;
1440 for (i = 0; i < libs->count; ++i) {
1441 if (libs->dl[i]) {
1442 dlclose(libs->dl[i]);
1443 }
1444 }
1445 free(libs->dl);
1446 }
1447 memset(libs, 0, sizeof(*libs));
1448 }
1449
1450 static inline void add_decl_lib(decl_libs *libs, void *dlopened) {
1451 libs->dl = realloc(libs->dl, ++libs->count * sizeof(*libs->dl));
1452 libs->dl[libs->count-1] = dlopened;
1453 }
1454
1455 static inline impl_val *deref_impl_val(impl_val *ret_val, decl_var *var) {
1456 unsigned i;
1457
1458 ZEND_ASSERT(var->arg->var != var);
1459 #if 0
1460 fprintf(stderr, "deref: %s pl=%u:%u as=%u:%u %p\n",
1461 var->name, var->pointer_level, var->arg->var->pointer_level,
1462 var->array_size, var->arg->var->array_size, ret_val);
1463 #endif
1464 for (i = 0; i < var->pointer_level; ++i) {
1465 #if 0
1466 fprintf(stderr, "-- %p %p %p\n", ret_val, *(void**)ret_val, ret_val->ptr);
1467 #endif
1468 ret_val = *(void **) ret_val;
1469 }
1470 return ret_val;
1471 }
1472
1473 static inline impl_val *enref_impl_val(void *ptr, decl_var *var) {
1474 impl_val *val, *val_ptr;
1475 unsigned i;
1476
1477 ZEND_ASSERT(var->arg->var == var);
1478 #if 0
1479 fprintf(stderr, "enref: %s pl=%u:%u as=%u:%u\n",
1480 var->name, var->pointer_level, var->arg->var->pointer_level,
1481 var->array_size, var->arg->var->array_size);
1482 #endif
1483 if (!var->pointer_level ){//&& real_decl_type(var->arg->type)->type != PSI_T_STRUCT) {
1484 return ptr;
1485 }
1486
1487 val = val_ptr = calloc(var->pointer_level + 1, sizeof(void *));
1488 for (i = !var->arg->var->array_size; i < var->pointer_level; ++i) {
1489 #if 0
1490 fprintf(stderr, "++\n");
1491 #endif
1492 val_ptr->ptr = (void **) val_ptr + 1;
1493 val_ptr = val_ptr->ptr;
1494 }
1495 val_ptr->ptr = ptr;
1496 return val;
1497 }
1498
1499 static inline impl_val *struct_member_ref(decl_arg *set_arg, impl_val *struct_ptr, impl_val **to_free) {
1500 void *ptr = (char *) struct_ptr + set_arg->layout->pos;
1501 #if 0
1502 fprintf(stderr, "struct member %s: %p\n", set_arg->var->name, ptr);
1503 #endif
1504 return ptr;
1505 }
1506
1507
1508 #define PSI_ERROR 16
1509 #define PSI_WARNING 32
1510 typedef void (*psi_error_cb)(void *context, PSI_Token *token, int type, const char *msg, ...);
1511
1512 #define PSI_DATA(D) ((PSI_Data *) (D))
1513 #define PSI_DATA_MEMBERS \
1514 constants *consts; \
1515 decl_typedefs *defs; \
1516 decl_structs *structs; \
1517 decl_unions *unions; \
1518 decl_enums *enums; \
1519 decls *decls; \
1520 impls *impls; \
1521 union { \
1522 decl_file file; \
1523 decl_libs libs; \
1524 } psi; \
1525 psi_error_cb error; \
1526 unsigned errors; \
1527 unsigned flags
1528 typedef struct PSI_Data {
1529 PSI_DATA_MEMBERS;
1530 } PSI_Data;
1531
1532 static inline PSI_Data *PSI_DataExchange(PSI_Data *dest, PSI_Data *src) {
1533 if (!dest) {
1534 dest = malloc(sizeof(*dest));
1535 }
1536 memcpy(dest, src, sizeof(*dest));
1537 memset(src, 0, sizeof(*src));
1538 return dest;
1539 }
1540
1541 static inline void PSI_DataDtor(PSI_Data *data) {
1542 if (data->consts) {
1543 free_constants(data->consts);
1544 }
1545 if (data->defs) {
1546 free_decl_typedefs(data->defs);
1547 }
1548 if (data->structs) {
1549 free_decl_structs(data->structs);
1550 }
1551 if (data->unions) {
1552 free_decl_unions(data->unions);
1553 }
1554 if (data->enums) {
1555 free_decl_enums(data->enums);
1556 }
1557 if (data->decls) {
1558 free_decls(data->decls);
1559 }
1560 if (data->impls) {
1561 free_impls(data->impls);
1562 }
1563 free_decl_file(&data->psi.file);
1564 }
1565
1566 typedef struct PSI_Parser {
1567 PSI_DATA_MEMBERS;
1568 FILE *fp;
1569 token_t num;
1570 void *proc;
1571 unsigned line, col;
1572 char *cur, *tok, *lim, *eof, *ctx, *mrk, buf[BSIZE];
1573 } PSI_Parser;
1574
1575 static inline size_t PSI_TokenAllocSize(size_t token_len, size_t fname_len) {
1576 return sizeof(PSI_Token) + token_len + fname_len + 2;
1577 }
1578
1579 static inline PSI_Token *PSI_TokenAlloc(PSI_Parser *P) {
1580 PSI_Token *T;
1581 size_t token_len, fname_len;
1582 token_t token_typ;
1583
1584 if (P->cur < P->tok) {
1585 return NULL;
1586 }
1587
1588 token_typ = P->num;
1589 token_len = P->cur - P->tok;
1590 fname_len = strlen(P->psi.file.fn);
1591
1592 T = calloc(1, PSI_TokenAllocSize(token_len, fname_len));
1593 T->type = token_typ;
1594 T->size = token_len;
1595 T->text = &T->buf[0];
1596 T->file = &T->buf[token_len + 1];
1597 T->line = P->line;
1598 T->col = P->col;
1599
1600 memcpy(T->text, P->tok, token_len);
1601 memcpy(T->file, P->psi.file.fn, fname_len);
1602
1603 return T;
1604 }
1605
1606 static inline PSI_Token *PSI_TokenCopy(PSI_Token *src) {
1607 size_t strct_len = PSI_TokenAllocSize(src->size, strlen(src->file));
1608 PSI_Token *ptr = malloc(strct_len);
1609
1610 memcpy(ptr, src, strct_len);
1611
1612 ptr->text = &ptr->buf[0];
1613 ptr->file = &ptr->buf[ptr->size + 1];
1614
1615 return ptr;
1616 }
1617
1618 static inline PSI_Token *PSI_TokenCat(unsigned argc, ...) {
1619 va_list argv;
1620 unsigned i;
1621 PSI_Token *T = NULL;
1622
1623 va_start(argv, argc);
1624 for (i = 0; i < argc; ++i) {
1625 PSI_Token *arg = va_arg(argv, PSI_Token *);
1626
1627 if (T) {
1628 size_t token_len = T->size, fname_len = strlen(T->file);
1629
1630 T = realloc(T, PSI_TokenAllocSize(T->size += arg->size + 1, fname_len));
1631 T->text = &T->buf[0];
1632 T->file = &T->buf[T->size + 1];
1633 T->buf[token_len] = ' ';
1634 memmove(&T->buf[T->size + 1], &T->buf[token_len + 1], fname_len + 1);
1635 memcpy(&T->buf[token_len + 1], arg->text, arg->size + 1);
1636 } else {
1637 T = PSI_TokenCopy(arg);
1638 T->type = PSI_T_NAME;
1639 }
1640 }
1641 va_end(argv);
1642
1643 return T;
1644 }
1645
1646 static inline PSI_Token *PSI_TokenAppend(PSI_Token *T, unsigned argc, ...) {
1647 va_list argv;
1648 unsigned i;
1649
1650 va_start(argv, argc);
1651 for (i = 0; i < argc; ++i) {
1652 char *str = va_arg(argv, char *);
1653 size_t str_len = strlen(str), token_len = T->size, fname_len = strlen(T->file);
1654
1655 T = realloc(T, PSI_TokenAllocSize(T->size += str_len + 1, fname_len));
1656 T->text = &T->buf[0];
1657 T->file = &T->buf[T->size + 1];
1658 T->buf[token_len] = ' ';
1659 memmove(&T->buf[T->size + 1], &T->buf[token_len + 1], fname_len + 1);
1660 memcpy(&T->buf[token_len + 1], str, str_len + 1);
1661 }
1662 va_end(argv);
1663
1664 return T;
1665 }
1666
1667 char *php_strtr(char *str, size_t len, char *str_from, char *str_to, size_t trlen);
1668 static inline PSI_Token *PSI_TokenTranslit(PSI_Token *T, char *from, char *to) {
1669 php_strtr(T->text, T->size, from, to, MIN(strlen(from), strlen(to)));
1670 return T;
1671 }
1672
1673 static inline uint64_t psi_hash(char *digest_buf, ...)
1674 {
1675 uint64_t hash = 5381;
1676 uint8_t c;
1677 const uint8_t *ptr;
1678 va_list argv;
1679
1680 va_start(argv, digest_buf);
1681 while ((ptr = va_arg(argv, const uint8_t *))) {
1682 while ((c = *ptr++)) {
1683 hash = ((hash << 5) + hash) + c;
1684 }
1685 }
1686 va_end(argv);
1687
1688 if (digest_buf) {
1689 sprintf(digest_buf, "%" PRIx64, hash);
1690 }
1691
1692 return hash;
1693 }
1694
1695 static inline uint64_t PSI_TokenHash(PSI_Token *t, char *digest_buf) {
1696 char loc_buf[48];
1697
1698 sprintf(loc_buf, "%u%u", t->line, t->col);
1699 return psi_hash(digest_buf, t->file, loc_buf, NULL);
1700 }
1701
1702 #define PSI_PARSER_DEBUG 0x1
1703 #define PSI_PARSER_SILENT 0x2
1704
1705 PSI_Parser *PSI_ParserInit(PSI_Parser *P, const char *filename, psi_error_cb error, unsigned flags);
1706 void PSI_ParserSyntaxError(PSI_Parser *P, const char *fn, size_t ln, const char *msg, ...);
1707 size_t PSI_ParserFill(PSI_Parser *P, size_t n);
1708 token_t PSI_ParserScan(PSI_Parser *P);
1709 void PSI_ParserParse(PSI_Parser *P, PSI_Token *src);
1710 void PSI_ParserDtor(PSI_Parser *P);
1711 void PSI_ParserFree(PSI_Parser **P);
1712
1713 #endif