67252e24855b9aa5cae8c0a68c01def1e2587d3b
[m6w6/ext-psi] / src / parser.h
1 #ifndef _PSI_PARSER_H
2 #define _PSI_PARSER_H
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdarg.h>
7 #include <string.h>
8
9 #include <Zend/zend_types.h>
10
11 #include "parser_proc.h"
12
13 #define BSIZE 256
14
15 typedef int token_t;
16
17 /* in php_psi.h */
18 size_t psi_t_alignment(token_t);
19 size_t psi_t_size(token_t);
20
21 typedef struct PSI_Token {
22 token_t type;
23 unsigned line;
24 size_t size;
25 char text[1];
26 } PSI_Token;
27
28 typedef struct decl_type {
29 char *name;
30 token_t type;
31 struct decl_type *real;
32 struct decl_struct *strct;
33 } decl_type;
34
35 static inline decl_type *init_decl_type(token_t type, const char *name) {
36 decl_type *t = calloc(1, sizeof(*t));
37 t->type = type;
38 t->name = strdup(name);
39 return t;
40 }
41
42 static inline decl_type *real_decl_type(decl_type *type) {
43 while (type->real) {
44 type = type->real;
45 }
46 return type;
47 }
48
49 static inline void free_decl_type(decl_type *type) {
50 free(type->name);
51 free(type);
52 }
53
54 typedef struct decl_typedef {
55 char *alias;
56 decl_type *type;
57 } decl_typedef;
58
59 static inline decl_typedef *init_decl_typedef(const char *name, decl_type *type) {
60 decl_typedef *t = calloc(1, sizeof(*t));
61 t->alias = strdup(name);
62 t->type = type;
63 return t;
64 }
65
66 static inline void free_decl_typedef(decl_typedef *t) {
67 free(t->alias);
68 free_decl_type(t->type);
69 free(t);
70 }
71
72 typedef struct decl_typedefs {
73 size_t count;
74 decl_typedef **list;
75 } decl_typedefs;
76
77 static inline decl_typedefs *add_decl_typedef(decl_typedefs *defs, decl_typedef *def) {
78 if (!defs) {
79 defs = calloc(1, sizeof(*defs));
80 }
81 defs->list = realloc(defs->list, ++defs->count * sizeof(*defs->list));
82 defs->list[defs->count-1] = def;
83 return defs;
84 }
85
86 static void free_decl_typedefs(decl_typedefs *defs) {
87 size_t i;
88
89 for (i = 0; i < defs->count; ++i) {
90 free_decl_typedef(defs->list[i]);
91 }
92 free(defs->list);
93 free(defs);
94 }
95
96 typedef struct decl_var {
97 char *name;
98 unsigned pointer_level;
99 unsigned array_size;
100 struct decl_arg *arg;
101 } decl_var;
102
103 static inline decl_var *init_decl_var(const char *name, unsigned pl, unsigned as) {
104 decl_var *v = calloc(1, sizeof(*v));
105 v->name = (char *) strdup((const char *) name);
106 v->pointer_level = pl;
107 v->array_size = as;
108 return v;
109 }
110
111 static inline void free_decl_var(decl_var *var) {
112 free(var->name);
113 free(var);
114 }
115
116 typedef struct decl_struct_layout {
117 size_t pos;
118 size_t len;
119 } decl_struct_layout;
120
121 static inline decl_struct_layout *init_decl_struct_layout(size_t pos, size_t len) {
122 decl_struct_layout *l = calloc(1, sizeof(*l));
123
124 l->pos = pos;
125 l->len = len;
126 return l;
127 }
128
129 static inline void free_decl_struct_layout(decl_struct_layout *l) {
130 free(l);
131 }
132
133 typedef struct decl_arg {
134 decl_type *type;
135 decl_var *var;
136 decl_struct_layout *layout;
137 struct let_stmt *let;
138 } decl_arg;
139
140 static inline decl_arg *init_decl_arg(decl_type *type, decl_var *var) {
141 decl_arg *arg = calloc(1, sizeof(*arg));
142 arg->type = type;
143 arg->var = var;
144 return arg;
145 }
146
147 static inline void free_decl_arg(decl_arg *arg) {
148 free_decl_type(arg->type);
149 free_decl_var(arg->var);
150 if (arg->layout) {
151 free_decl_struct_layout(arg->layout);
152 }
153 free(arg);
154 }
155
156 typedef struct decl_vars {
157 decl_var **vars;
158 size_t count;
159 } decl_vars;
160
161 static inline decl_vars *init_decl_vars(decl_var *var) {
162 decl_vars *vars = calloc(1, sizeof(*vars));
163 if (var) {
164 vars->count = 1;
165 vars->vars = calloc(1, sizeof(*vars->vars));
166 vars->vars[0] = var;
167 }
168 return vars;
169 }
170
171 static inline decl_vars *add_decl_var(decl_vars *vars, decl_var *var) {
172 vars->vars = realloc(vars->vars, ++vars->count * sizeof(*vars->vars));
173 vars->vars[vars->count-1] = var;
174 return vars;
175 }
176
177 static inline void free_decl_vars(decl_vars *vars) {
178 size_t i;
179
180 for (i = 0; i < vars->count; ++i) {
181 free_decl_var(vars->vars[i]);
182 }
183 free(vars->vars);
184 free(vars);
185 }
186
187 typedef struct decl_args {
188 decl_arg **args;
189 size_t count;
190 } decl_args;
191
192 static inline decl_args *init_decl_args(decl_arg *arg) {
193 decl_args *args = calloc(1, sizeof(*args));
194 if (arg) {
195 args->count = 1;
196 args->args = calloc(1, sizeof(*args->args));
197 args->args[0] = arg;
198 }
199 return args;
200 }
201
202 static inline decl_args *add_decl_arg(decl_args *args, decl_arg *arg) {
203 args->args = realloc(args->args, ++args->count * sizeof(*args->args));
204 args->args[args->count-1] = arg;
205 return args;
206 }
207
208 static inline void free_decl_args(decl_args *args) {
209 size_t i;
210
211 for (i = 0; i < args->count; ++i) {
212 free_decl_arg(args->args[i]);
213 }
214 free(args->args);
215 free(args);
216 }
217
218 typedef struct decl_abi {
219 char *convention;
220 } decl_abi;
221
222 static inline decl_abi *init_decl_abi(const char *convention) {
223 decl_abi *abi = calloc(1, sizeof(*abi));
224 abi->convention = strdup(convention);
225 return abi;
226 }
227
228 static inline void free_decl_abi(decl_abi *abi) {
229 free(abi->convention);
230 free(abi);
231 }
232
233 typedef struct decl {
234 decl_abi *abi;
235 decl_arg *func;
236 decl_args *args;
237 void *dlptr;
238 } decl;
239
240 static inline decl* init_decl(decl_abi *abi, decl_arg *func, decl_args *args) {
241 decl *d = calloc(1, sizeof(*d));
242 d->abi = abi;
243 d->func = func;
244 d->args = args;
245 return d;
246 }
247
248 static inline void free_decl(decl *d) {
249 free_decl_abi(d->abi);
250 free_decl_arg(d->func);
251 if (d->args) {
252 free_decl_args(d->args);
253 }
254 free(d);
255 }
256
257 typedef struct decls {
258 size_t count;
259 decl **list;
260 } decls;
261
262 static inline decls *add_decl(decls *decls, decl *decl) {
263 if (!decls) {
264 decls = calloc(1, sizeof(*decls));
265 }
266 decls->list = realloc(decls->list, ++decls->count * sizeof(*decls->list));
267 decls->list[decls->count-1] = decl;
268 return decls;
269 }
270
271 static inline void free_decls(decls *decls) {
272 size_t i;
273
274 for (i = 0; i < decls->count; ++i) {
275 free_decl(decls->list[i]);
276 }
277 free(decls->list);
278 free(decls);
279 }
280
281 typedef struct decl_struct {
282 char *name;
283 decl_args *args;
284 size_t size;
285 } decl_struct;
286
287 static inline decl_struct *init_decl_struct(const char *name, decl_args *args) {
288 decl_struct *s = calloc(1, sizeof(*s));
289 s->name = strdup(name);
290 s->args = args;
291 return s;
292 }
293
294 static inline void free_decl_struct(decl_struct *s) {
295 if (s->args) {
296 free_decl_args(s->args);
297 }
298 free(s->name);
299 free(s);
300 }
301
302 typedef struct decl_structs {
303 size_t count;
304 decl_struct **list;
305 } decl_structs;
306
307 static inline decl_structs *add_decl_struct(decl_structs *ss, decl_struct *s) {
308 if (!ss) {
309 ss = calloc(1, sizeof(*ss));
310 }
311 ss->list = realloc(ss->list, ++ss->count * sizeof(*ss->list));
312 ss->list[ss->count-1] = s;
313 return ss;
314 }
315
316 static inline void free_decl_structs(decl_structs *ss) {
317 size_t i;
318
319 for (i = 0; i < ss->count; ++i) {
320 free_decl_struct(ss->list[i]);
321 }
322 free(ss->list);
323 free(ss);
324 }
325
326 typedef union impl_val {
327 char cval;
328 int8_t i8;
329 short sval;
330 int16_t i16;
331 int ival;
332 int32_t i32;
333 long lval;
334 int64_t i64;
335 float fval;
336 double dval;
337 union {
338 zend_bool bval;
339 zend_long lval;
340 zend_string *str;
341 } zend;
342 void *ptr;
343 uint8_t _dbg[sizeof(void *)];
344 } impl_val;
345
346 static inline impl_val *deref_impl_val(impl_val *ret_val, decl_var *var) {
347 unsigned i;
348
349 if (var->arg->var != var) for (i = 0; i < var->pointer_level; ++i) {
350 ret_val = *(void **) ret_val;
351 }
352 return ret_val;
353 }
354
355 static inline impl_val *enref_impl_val(void *ptr, decl_var *var) {
356 impl_val *val, *val_ptr;
357 unsigned i;
358
359 if (!var->pointer_level && real_decl_type(var->arg->type)->type != PSI_T_STRUCT) {
360 return ptr;
361 }
362 val = val_ptr = calloc(var->pointer_level, sizeof(void *));
363 for (i = 1; i < var->pointer_level; ++i) {
364 val_ptr->ptr = (void **) val_ptr + 1;
365 val_ptr = val_ptr->ptr;
366 }
367 val_ptr->ptr = ptr;
368 return val;
369 }
370 typedef struct impl_type {
371 char *name;
372 token_t type;
373 } impl_type;
374
375 static inline impl_type *init_impl_type(token_t type, const char *name) {
376 impl_type *t = calloc(1, sizeof(*t));
377
378 t->type = type;
379 t->name = strdup(name);
380 return t;
381 }
382
383 static inline void free_impl_type(impl_type *type) {
384 free(type->name);
385 free(type);
386 }
387
388 typedef struct impl_var {
389 char *name;
390 unsigned reference:1;
391 } impl_var;
392
393 static inline impl_var *init_impl_var(const char *name, int is_reference) {
394 impl_var *var = calloc(1, sizeof(*var));
395 var->name = strdup(name);
396 var->reference = is_reference;
397 return var;
398 }
399
400 static inline void free_impl_var(impl_var *var) {
401 free(var->name);
402 free(var);
403 }
404
405 typedef struct impl_def_val {
406 token_t type;
407 char *text;
408 } impl_def_val;
409
410 static inline impl_def_val *init_impl_def_val(token_t t, const char *text) {
411 impl_def_val *def = calloc(1, sizeof(*def));
412 def->type = t;
413 def->text = strdup(text);
414 return def;
415 }
416
417 static inline void free_impl_def_val(impl_def_val *def) {
418 free(def->text);
419 free(def);
420 }
421
422 typedef struct impl_arg {
423 impl_type *type;
424 impl_var *var;
425 impl_def_val *def;
426 impl_val val;
427 zval *_zv;
428 } impl_arg;
429
430 static inline impl_arg *init_impl_arg(impl_type *type, impl_var *var, impl_def_val *def) {
431 impl_arg *arg = calloc(1, sizeof(*arg));
432 arg->type = type;
433 arg->var = var;
434 arg->def = def;
435 return arg;
436 }
437
438 static inline void free_impl_arg(impl_arg *arg) {
439 free_impl_type(arg->type);
440 free_impl_var(arg->var);
441 if (arg->def) {
442 free_impl_def_val(arg->def);
443 }
444 free(arg);
445 }
446
447 typedef struct impl_args {
448 impl_arg **args;
449 size_t count;
450 } impl_args;
451
452 static inline impl_args *init_impl_args(impl_arg *arg) {
453 impl_args *args = calloc(1, sizeof(*args));
454 if (arg) {
455 args->count = 1;
456 args->args = calloc(1, sizeof(*args->args));
457 args->args[0] = arg;
458 }
459 return args;
460 }
461
462 static inline impl_args *add_impl_arg(impl_args *args, impl_arg *arg) {
463 args->args = realloc(args->args, ++args->count * sizeof(*args->args));
464 args->args[args->count-1] = arg;
465 return args;
466 }
467
468 static inline void free_impl_args(impl_args *args) {
469 size_t i;
470
471 for (i = 0; i < args->count; ++i) {
472 free_impl_arg(args->args[i]);
473 }
474 free(args->args);
475 free(args);
476 }
477
478 typedef struct impl_func {
479 char *name;
480 impl_args *args;
481 impl_type *return_type;
482 unsigned return_reference:1;
483 } impl_func;
484
485 static inline impl_func *init_impl_func(char *name, impl_args *args, impl_type *type, int ret_reference) {
486 impl_func *func = calloc(1, sizeof(*func));
487 func->name = strdup(name);
488 func->args = args ? args : init_impl_args(NULL);
489 func->return_type = type;
490 func->return_reference = ret_reference;
491 return func;
492 }
493
494 static inline void free_impl_func(impl_func *f) {
495 free_impl_type(f->return_type);
496 free_impl_args(f->args);
497 free(f->name);
498 free(f);
499 }
500
501 typedef struct let_calloc {
502 size_t n;
503 decl_type *type;
504 } let_calloc;
505
506 static inline let_calloc *init_let_calloc(long n, decl_type *type) {
507 let_calloc *alloc = calloc(1, sizeof(*alloc));
508 alloc->n = n;
509 alloc->type = type;
510 return alloc;
511 }
512
513 static inline void free_let_calloc(let_calloc *alloc) {
514 free_decl_type(alloc->type);
515 free(alloc);
516 }
517
518 typedef struct let_func {
519 token_t type;
520 char *name;
521 let_calloc *alloc;
522 } let_func;
523
524 static inline let_func *init_let_func(token_t type, const char *name, let_calloc *alloc) {
525 let_func *func = calloc(1, sizeof(*func));
526 func->type = type;
527 func->name = strdup(name);
528 func->alloc = alloc;
529 return func;
530 }
531
532 static inline void free_let_func(let_func *func) {
533 if (func->alloc) {
534 free_let_calloc(func->alloc);
535 }
536 free(func->name);
537 free(func);
538 }
539
540 typedef struct let_value {
541 let_func *func;
542 impl_var *var;
543 unsigned is_reference:1;
544 } let_value;
545
546 static inline let_value *init_let_value(let_func *func, impl_var *var, int is_reference) {
547 let_value *val = calloc(1, sizeof(*val));
548 val->is_reference = is_reference;
549 val->func = func;
550 val->var = var;
551 return val;
552 }
553
554 static inline void free_let_value(let_value *val) {
555 if (val->func) {
556 free_let_func(val->func);
557 }
558 if (val->var) {
559 free_impl_var(val->var);
560 }
561 free(val);
562 }
563
564 typedef struct let_stmt {
565 decl_var *var;
566 let_value *val;
567 impl_arg *arg;
568 impl_val out;
569 void *ptr;
570 void *mem;
571 } let_stmt;
572
573 static inline let_stmt *init_let_stmt(decl_var *var, let_value *val) {
574 let_stmt *let = calloc(1, sizeof(*let));
575 let->var = var;
576 let->val = val;
577 return let;
578 }
579
580 static inline void free_let_stmt(let_stmt *stmt) {
581 free_decl_var(stmt->var);
582 if (stmt->val) {
583 free_let_value(stmt->val);
584 }
585 free(stmt);
586 }
587
588 struct set_value;
589
590 typedef struct set_func {
591 token_t type;
592 char *name;
593 void (*handler)(zval *, token_t, impl_val *, struct set_value *set, decl_var *);
594 } set_func;
595
596 static inline set_func *init_set_func(token_t type, const char *name) {
597 set_func *func = calloc(1, sizeof(*func));
598 func->type = type;
599 func->name = strdup(name);
600 return func;
601 }
602
603 static inline void free_set_func(set_func *func) {
604 free(func->name);
605 free(func);
606 }
607
608 typedef struct set_value {
609 set_func *func;
610 decl_vars *vars;
611 struct set_value **inner;
612 size_t count;
613 } set_value;
614
615 static inline set_value *init_set_value(set_func *func, decl_vars *vars) {
616 set_value *val = calloc(1, sizeof(*val));
617 val->func = func;
618 val->vars = vars;
619 return val;
620 }
621 static inline set_value *add_inner_set_value(set_value *val, set_value *inner) {
622 val->inner = realloc(val->inner, ++val->count * sizeof(*val->inner));
623 val->inner[val->count-1] = inner;
624 return val;
625 }
626
627 static inline void free_set_value(set_value *val) {
628 free_set_func(val->func);
629 free_decl_vars(val->vars);
630 if (val->inner) {
631 size_t i;
632 for (i = 0; i < val->count; ++i) {
633 free_set_value(val->inner[i]);
634 }
635 free(val->inner);
636 }
637 free(val);
638 }
639
640 typedef struct set_stmt {
641 impl_var *var;
642 set_value *val;
643 impl_arg *arg;
644 } set_stmt;
645
646 static inline set_stmt *init_set_stmt(impl_var *var, set_value *val) {
647 set_stmt *set = calloc(1, sizeof(*set));
648 set->var = var;
649 set->val = val;
650 return set;
651 }
652
653 static inline void free_set_stmt(set_stmt *set) {
654 free_impl_var(set->var);
655 free_set_value(set->val);
656 free(set);
657 }
658
659 typedef struct return_stmt {
660 set_value *set;
661 decl_arg *decl;
662 } return_stmt;
663
664 static inline return_stmt *init_return_stmt(set_value *val) {
665 return_stmt *ret = calloc(1, sizeof(*ret));
666 ret->set = val;
667 return ret;
668 }
669
670 static inline void free_return_stmt(return_stmt *ret) {
671 //free_set_func(ret->func);
672 //free_decl_var(ret->decl);
673 free_set_value(ret->set);
674 free(ret);
675 }
676
677 typedef struct free_call {
678 char *func;
679 decl_vars *vars;
680 decl *decl;
681 } free_call;
682
683 static inline free_call *init_free_call(const char *func, decl_vars *vars) {
684 free_call *f = calloc(1, sizeof(*f));
685 f->func = strdup(func);
686 f->vars = vars;
687 return f;
688 }
689
690 static inline void free_free_call(free_call *f) {
691 free(f->func);
692 free(f);
693 }
694
695 typedef struct free_calls {
696 free_call **list;
697 size_t count;
698 } free_calls;
699
700 static inline free_calls *init_free_calls(free_call *f) {
701 free_calls *fcs = calloc(1, sizeof(*fcs));
702 if (f) {
703 fcs->count = 1;
704 fcs->list = calloc(1, sizeof(*fcs->list));
705 fcs->list[0] = f;
706 }
707 return fcs;
708 }
709
710 static inline void free_free_calls(free_calls *fcs) {
711 size_t i;
712
713 for (i = 0; i < fcs->count; ++i) {
714 free_free_call(fcs->list[i]);
715 }
716 free(fcs->list);
717 free(fcs);
718 }
719
720 static inline free_calls *add_free_call(free_calls *fcs, free_call *f) {
721 fcs->list = realloc(fcs->list, ++fcs->count * sizeof(*fcs->list));
722 fcs->list[fcs->count-1] = f;
723 return fcs;
724 }
725
726 typedef struct free_stmt {
727 free_calls *calls;
728 } free_stmt;
729
730 static inline free_stmt *init_free_stmt(free_calls *calls) {
731 free_stmt *f = calloc(1, sizeof(*f));
732 f->calls = calls;
733 return f;
734 }
735
736 static inline void free_free_stmt(free_stmt *f) {
737 free_free_calls(f->calls);
738 free(f);
739 }
740
741 typedef struct impl_stmt {
742 token_t type;
743 union {
744 let_stmt *let;
745 set_stmt *set;
746 return_stmt *ret;
747 free_stmt *fre;
748 void *ptr;
749 } s;
750 } impl_stmt;
751
752 static inline impl_stmt *init_impl_stmt(token_t type, void *ptr) {
753 impl_stmt *stmt = calloc(1, sizeof(*stmt));
754 stmt->type = type;
755 stmt->s.ptr = ptr;
756 return stmt;
757 }
758
759 static inline void free_impl_stmt(impl_stmt *stmt) {
760 switch (stmt->type) {
761 case PSI_T_LET:
762 free_let_stmt(stmt->s.let);
763 break;
764 case PSI_T_SET:
765 free_set_stmt(stmt->s.set);
766 break;
767 case PSI_T_RETURN:
768 free_return_stmt(stmt->s.ret);
769 break;
770 case PSI_T_FREE:
771 free_free_stmt(stmt->s.fre);
772 break;
773 }
774 free(stmt);
775 }
776
777 typedef struct impl_stmts {
778 struct {
779 return_stmt **list;
780 size_t count;
781 } ret;
782 struct {
783 let_stmt **list;
784 size_t count;
785 } let;
786 struct {
787 set_stmt **list;
788 size_t count;
789 } set;
790 struct {
791 free_stmt **list;
792 size_t count;
793 } fre;
794 } impl_stmts;
795
796 static inline void *add_impl_stmt_ex(void *list, size_t count, void *stmt) {
797 list = realloc(list, count * sizeof(list));
798 ((void **)list)[count-1] = stmt;
799 return list;
800 }
801
802 static inline impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt) {
803 switch (stmt->type) {
804 case PSI_T_RETURN:
805 stmts->ret.list = add_impl_stmt_ex(stmts->ret.list, ++stmts->ret.count, stmt->s.ret);
806 break;
807 case PSI_T_LET:
808 stmts->let.list = add_impl_stmt_ex(stmts->let.list, ++stmts->let.count, stmt->s.let);
809 break;
810 case PSI_T_SET:
811 stmts->set.list = add_impl_stmt_ex(stmts->set.list, ++stmts->set.count, stmt->s.set);
812 break;
813 case PSI_T_FREE:
814 stmts->fre.list = add_impl_stmt_ex(stmts->fre.list, ++stmts->fre.count, stmt->s.fre);
815 break;
816 }
817 free(stmt);
818 return stmts;
819 }
820
821 static inline impl_stmts *init_impl_stmts(impl_stmt *stmt) {
822 impl_stmts *stmts = calloc(1, sizeof(*stmts));
823 return add_impl_stmt(stmts, stmt);
824 }
825
826 static inline void free_impl_stmts(impl_stmts *stmts) {
827 size_t i;
828
829 for (i = 0; i < stmts->let.count; ++i) {
830 free_let_stmt(stmts->let.list[i]);
831 }
832 free(stmts->let.list);
833 for (i = 0; i < stmts->ret.count; ++i) {
834 free_return_stmt(stmts->ret.list[i]);
835 }
836 free(stmts->ret.list);
837 for (i = 0; i < stmts->set.count; ++i) {
838 free_set_stmt(stmts->set.list[i]);
839 }
840 free(stmts->set.list);
841 for (i = 0; i < stmts->fre.count; ++i) {
842 free_free_stmt(stmts->fre.list[i]);
843 }
844 free(stmts->fre.list);
845 free(stmts);
846 }
847
848 typedef struct impl {
849 impl_func *func;
850 impl_stmts *stmts;
851 decl *decl;
852 } impl;
853
854 static inline impl *init_impl(impl_func *func, impl_stmts *stmts) {
855 impl *i = calloc(1, sizeof(*i));
856 i->func = func;
857 i->stmts = stmts;
858 return i;
859 }
860
861 static inline void free_impl(impl *impl) {
862 free_impl_func(impl->func);
863 free_impl_stmts(impl->stmts);
864 free(impl);
865 }
866
867 typedef struct impls {
868 size_t count;
869 impl **list;
870 } impls;
871
872 static inline impls *add_impl(impls *impls, impl *impl) {
873 if (!impls) {
874 impls = calloc(1, sizeof(*impls));
875 }
876 impls->list = realloc(impls->list, ++impls->count * sizeof(*impls->list));
877 impls->list[impls->count-1] = impl;
878 return impls;
879 }
880
881 static void free_impls(impls *impls) {
882 size_t i;
883
884 for (i = 0; i < impls->count; ++i) {
885 free_impl(impls->list[i]);
886 }
887 free(impls->list);
888 free(impls);
889 }
890
891 typedef struct const_type {
892 token_t type;
893 char *name;
894 } const_type;
895
896 static inline const_type *init_const_type(token_t type, const char *name) {
897 const_type *ct = calloc(1, sizeof(*ct));
898 ct->type = type;
899 ct->name = strdup(name);
900 return ct;
901 }
902
903 static inline void free_const_type(const_type *type) {
904 free(type->name);
905 free(type);
906 }
907
908 typedef struct constant {
909 const_type *type;
910 char *name;
911 impl_def_val *val;
912 } constant;
913
914 static inline constant *init_constant(const_type *type, const char *name, impl_def_val *val) {
915 constant *c = calloc(1, sizeof(*c));
916 c->type = type;
917 c->name = strdup(name);
918 c->val = val;
919 return c;
920 }
921
922 static inline void free_constant(constant *constant) {
923 free_const_type(constant->type);
924 free(constant->name);
925 free_impl_def_val(constant->val);
926 free(constant);
927 }
928
929 typedef struct constants {
930 size_t count;
931 constant **list;
932 } constants;
933
934 static inline constants *add_constant(constants *constants, constant *constant) {
935 if (!constants) {
936 constants = calloc(1, sizeof(*constants));
937 }
938 constants->list = realloc(constants->list, ++constants->count * sizeof(*constants->list));
939 constants->list[constants->count-1] = constant;
940 return constants;
941 }
942
943 static inline void free_constants(constants *c) {
944 size_t i;
945
946 for (i = 0; i < c->count; ++i) {
947 free_constant(c->list[i]);
948 }
949 free(c->list);
950 free(c);
951 }
952
953 #define PSI_ERROR 16
954 #define PSI_WARNING 32
955 typedef void (*psi_error_cb)(int type, const char *msg, ...);
956
957 typedef struct decl_file {
958 char *ln;
959 char *fn;
960 } decl_file;
961
962 static inline void free_decl_file(decl_file *file) {
963 if (file->ln) {
964 free(file->ln);
965 }
966 if (file->fn) {
967 free(file->fn);
968 }
969 memset(file, 0, sizeof(*file));
970 }
971
972 typedef struct decl_libs {
973 void **dl;
974 size_t count;
975 } decl_libs;
976
977 static inline void free_decl_libs(decl_libs *libs) {
978 if (libs->dl) {
979 size_t i;
980 for (i = 0; i < libs->count; ++i) {
981 if (libs->dl[i]) {
982 dlclose(libs->dl[i]);
983 }
984 }
985 free(libs->dl);
986 }
987 memset(libs, 0, sizeof(*libs));
988 }
989
990 static inline void add_decl_lib(decl_libs *libs, void *dlopened) {
991 libs->dl = realloc(libs->dl, ++libs->count * sizeof(*libs->dl));
992 libs->dl[libs->count-1] = dlopened;
993 }
994
995 #define PSI_DATA(D) ((PSI_Data *) (D))
996 #define PSI_DATA_MEMBERS \
997 constants *consts; \
998 decl_typedefs *defs; \
999 decl_structs *structs; \
1000 decls *decls; \
1001 impls *impls; \
1002 union { \
1003 decl_file file; \
1004 decl_libs libs; \
1005 } psi; \
1006 psi_error_cb error
1007 typedef struct PSI_Data {
1008 PSI_DATA_MEMBERS;
1009 } PSI_Data;
1010
1011 static inline PSI_Data *PSI_DataExchange(PSI_Data *dest, PSI_Data *src) {
1012 if (!dest) {
1013 dest = malloc(sizeof(*dest));
1014 }
1015 memcpy(dest, src, sizeof(*dest));
1016 memset(src, 0, sizeof(*src));
1017 return dest;
1018 }
1019
1020 static inline void PSI_DataDtor(PSI_Data *data) {
1021 if (data->consts) {
1022 free_constants(data->consts);
1023 }
1024 if (data->defs) {
1025 free_decl_typedefs(data->defs);
1026 }
1027 if (data->structs) {
1028 free_decl_structs(data->structs);
1029 }
1030 if (data->decls) {
1031 free_decls(data->decls);
1032 }
1033 if (data->impls) {
1034 free_impls(data->impls);
1035 }
1036 free_decl_file(&data->psi.file);
1037 }
1038
1039 typedef struct PSI_Parser {
1040 PSI_DATA_MEMBERS;
1041 FILE *fp;
1042 unsigned flags;
1043 unsigned errors;
1044 void *proc;
1045 size_t line;
1046 token_t num;
1047 char *cur, *tok, *lim, *eof, *ctx, *mrk, buf[BSIZE];
1048 } PSI_Parser;
1049
1050 static inline PSI_Token *PSI_TokenAlloc(PSI_Parser *P) {
1051 PSI_Token *T;
1052 size_t token_len;
1053
1054 if (P->cur <= P->tok) {
1055 return NULL;
1056 }
1057
1058 token_len = P->cur - P->tok;
1059
1060 T = calloc(1, sizeof(*T) + token_len);
1061 T->type = P->num;
1062 T->line = P->line;
1063 T->size = token_len;
1064 T->text[token_len] = 0;
1065 memcpy(T->text, P->tok, token_len);
1066
1067 return T;
1068 }
1069
1070 #define PSI_PARSER_DEBUG 0x1
1071
1072 PSI_Parser *PSI_ParserInit(PSI_Parser *P, const char *filename, psi_error_cb error, unsigned flags);
1073 void PSI_ParserSyntaxError(PSI_Parser *P, const char *fn, size_t ln, const char *msg, ...);
1074 size_t PSI_ParserFill(PSI_Parser *P, size_t n);
1075 token_t PSI_ParserScan(PSI_Parser *P);
1076 void PSI_ParserParse(PSI_Parser *P, PSI_Token *T);
1077 void PSI_ParserDtor(PSI_Parser *P);
1078 void PSI_ParserFree(PSI_Parser **P);
1079
1080 #endif