flush
[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 struct impl *impl;
238 struct {
239 void *sym;
240 void *info;
241 void **args;
242 } call;
243 } decl;
244
245 static inline decl* init_decl(decl_abi *abi, decl_arg *func, decl_args *args) {
246 decl *d = calloc(1, sizeof(*d));
247 d->abi = abi;
248 d->func = func;
249 d->args = args;
250 return d;
251 }
252
253 static inline void free_decl(decl *d) {
254 free_decl_abi(d->abi);
255 free_decl_arg(d->func);
256 if (d->args) {
257 free_decl_args(d->args);
258 }
259 free(d);
260 }
261
262 typedef struct decls {
263 size_t count;
264 decl **list;
265 } decls;
266
267 static inline decls *add_decl(decls *decls, decl *decl) {
268 if (!decls) {
269 decls = calloc(1, sizeof(*decls));
270 }
271 decls->list = realloc(decls->list, ++decls->count * sizeof(*decls->list));
272 decls->list[decls->count-1] = decl;
273 return decls;
274 }
275
276 static inline void free_decls(decls *decls) {
277 size_t i;
278
279 for (i = 0; i < decls->count; ++i) {
280 free_decl(decls->list[i]);
281 }
282 free(decls->list);
283 free(decls);
284 }
285
286 typedef struct decl_struct {
287 char *name;
288 decl_args *args;
289 size_t size;
290 } decl_struct;
291
292 static inline decl_struct *init_decl_struct(const char *name, decl_args *args) {
293 decl_struct *s = calloc(1, sizeof(*s));
294 s->name = strdup(name);
295 s->args = args;
296 return s;
297 }
298
299 static inline void free_decl_struct(decl_struct *s) {
300 if (s->args) {
301 free_decl_args(s->args);
302 }
303 free(s->name);
304 free(s);
305 }
306
307 typedef struct decl_structs {
308 size_t count;
309 decl_struct **list;
310 } decl_structs;
311
312 static inline decl_structs *add_decl_struct(decl_structs *ss, decl_struct *s) {
313 if (!ss) {
314 ss = calloc(1, sizeof(*ss));
315 }
316 ss->list = realloc(ss->list, ++ss->count * sizeof(*ss->list));
317 ss->list[ss->count-1] = s;
318 return ss;
319 }
320
321 static inline void free_decl_structs(decl_structs *ss) {
322 size_t i;
323
324 for (i = 0; i < ss->count; ++i) {
325 free_decl_struct(ss->list[i]);
326 }
327 free(ss->list);
328 free(ss);
329 }
330
331 typedef union impl_val {
332 char cval;
333 int8_t i8;
334 short sval;
335 int16_t i16;
336 int ival;
337 int32_t i32;
338 long lval;
339 int64_t i64;
340 float fval;
341 double dval;
342 union {
343 zend_bool bval;
344 zend_long lval;
345 zend_string *str;
346 } zend;
347 void *ptr;
348 uint8_t _dbg[sizeof(void *)];
349 } impl_val;
350
351 static inline impl_val *deref_impl_val(impl_val *ret_val, decl_var *var) {
352 unsigned i;
353
354 if (var->arg->var != var) for (i = 1; i < var->pointer_level; ++i) {
355 ret_val = *(void **) ret_val;
356 }
357 return ret_val;
358 }
359
360 static inline impl_val *enref_impl_val(void *ptr, decl_var *var) {
361 impl_val *val, *val_ptr;
362 unsigned i;
363
364 if (!var->pointer_level && real_decl_type(var->arg->type)->type != PSI_T_STRUCT) {
365 return ptr;
366 }
367 val = val_ptr = calloc(var->pointer_level + 1, sizeof(void *));
368 for (i = 1; i < var->pointer_level; ++i) {
369 val_ptr->ptr = (void **) val_ptr + 1;
370 val_ptr = val_ptr->ptr;
371 }
372 val_ptr->ptr = ptr;
373 return val;
374 }
375 typedef struct impl_type {
376 char *name;
377 token_t type;
378 } impl_type;
379
380 static inline impl_type *init_impl_type(token_t type, const char *name) {
381 impl_type *t = calloc(1, sizeof(*t));
382
383 t->type = type;
384 t->name = strdup(name);
385 return t;
386 }
387
388 static inline void free_impl_type(impl_type *type) {
389 free(type->name);
390 free(type);
391 }
392
393 typedef struct impl_var {
394 char *name;
395 unsigned reference:1;
396 } impl_var;
397
398 static inline impl_var *init_impl_var(const char *name, int is_reference) {
399 impl_var *var = calloc(1, sizeof(*var));
400 var->name = strdup(name);
401 var->reference = is_reference;
402 return var;
403 }
404
405 static inline void free_impl_var(impl_var *var) {
406 free(var->name);
407 free(var);
408 }
409
410 typedef struct impl_def_val {
411 token_t type;
412 char *text;
413 } impl_def_val;
414
415 static inline impl_def_val *init_impl_def_val(token_t t, const char *text) {
416 impl_def_val *def = calloc(1, sizeof(*def));
417 def->type = t;
418 def->text = strdup(text);
419 return def;
420 }
421
422 static inline void free_impl_def_val(impl_def_val *def) {
423 free(def->text);
424 free(def);
425 }
426
427 typedef struct impl_arg {
428 impl_type *type;
429 impl_var *var;
430 impl_def_val *def;
431 impl_val val;
432 zval *_zv;
433 } impl_arg;
434
435 static inline impl_arg *init_impl_arg(impl_type *type, impl_var *var, impl_def_val *def) {
436 impl_arg *arg = calloc(1, sizeof(*arg));
437 arg->type = type;
438 arg->var = var;
439 arg->def = def;
440 return arg;
441 }
442
443 static inline void free_impl_arg(impl_arg *arg) {
444 free_impl_type(arg->type);
445 free_impl_var(arg->var);
446 if (arg->def) {
447 free_impl_def_val(arg->def);
448 }
449 free(arg);
450 }
451
452 typedef struct impl_args {
453 impl_arg **args;
454 size_t count;
455 } impl_args;
456
457 static inline impl_args *init_impl_args(impl_arg *arg) {
458 impl_args *args = calloc(1, sizeof(*args));
459 if (arg) {
460 args->count = 1;
461 args->args = calloc(1, sizeof(*args->args));
462 args->args[0] = arg;
463 }
464 return args;
465 }
466
467 static inline impl_args *add_impl_arg(impl_args *args, impl_arg *arg) {
468 args->args = realloc(args->args, ++args->count * sizeof(*args->args));
469 args->args[args->count-1] = arg;
470 return args;
471 }
472
473 static inline void free_impl_args(impl_args *args) {
474 size_t i;
475
476 for (i = 0; i < args->count; ++i) {
477 free_impl_arg(args->args[i]);
478 }
479 free(args->args);
480 free(args);
481 }
482
483 typedef struct impl_func {
484 char *name;
485 impl_args *args;
486 impl_type *return_type;
487 unsigned return_reference:1;
488 } impl_func;
489
490 static inline impl_func *init_impl_func(char *name, impl_args *args, impl_type *type, int ret_reference) {
491 impl_func *func = calloc(1, sizeof(*func));
492 func->name = strdup(name);
493 func->args = args ? args : init_impl_args(NULL);
494 func->return_type = type;
495 func->return_reference = ret_reference;
496 return func;
497 }
498
499 static inline void free_impl_func(impl_func *f) {
500 free_impl_type(f->return_type);
501 free_impl_args(f->args);
502 free(f->name);
503 free(f);
504 }
505
506 typedef struct let_calloc {
507 size_t n;
508 decl_type *type;
509 } let_calloc;
510
511 static inline let_calloc *init_let_calloc(long n, decl_type *type) {
512 let_calloc *alloc = calloc(1, sizeof(*alloc));
513 alloc->n = n;
514 alloc->type = type;
515 return alloc;
516 }
517
518 static inline void free_let_calloc(let_calloc *alloc) {
519 free_decl_type(alloc->type);
520 free(alloc);
521 }
522
523 typedef struct let_func {
524 token_t type;
525 char *name;
526 let_calloc *alloc;
527 } let_func;
528
529 static inline let_func *init_let_func(token_t type, const char *name, let_calloc *alloc) {
530 let_func *func = calloc(1, sizeof(*func));
531 func->type = type;
532 func->name = strdup(name);
533 func->alloc = alloc;
534 return func;
535 }
536
537 static inline void free_let_func(let_func *func) {
538 if (func->alloc) {
539 free_let_calloc(func->alloc);
540 }
541 free(func->name);
542 free(func);
543 }
544
545 typedef struct let_value {
546 let_func *func;
547 impl_var *var;
548 unsigned is_reference:1;
549 } let_value;
550
551 static inline let_value *init_let_value(let_func *func, impl_var *var, int is_reference) {
552 let_value *val = calloc(1, sizeof(*val));
553 val->is_reference = is_reference;
554 val->func = func;
555 val->var = var;
556 return val;
557 }
558
559 static inline void free_let_value(let_value *val) {
560 if (val->func) {
561 free_let_func(val->func);
562 }
563 if (val->var) {
564 free_impl_var(val->var);
565 }
566 free(val);
567 }
568
569 typedef struct let_stmt {
570 decl_var *var;
571 let_value *val;
572 impl_arg *arg;
573 impl_val out;
574 void *ptr;
575 void *mem;
576 } let_stmt;
577
578 static inline let_stmt *init_let_stmt(decl_var *var, let_value *val) {
579 let_stmt *let = calloc(1, sizeof(*let));
580 let->var = var;
581 let->val = val;
582 return let;
583 }
584
585 static inline void free_let_stmt(let_stmt *stmt) {
586 free_decl_var(stmt->var);
587 if (stmt->val) {
588 free_let_value(stmt->val);
589 }
590 free(stmt);
591 }
592
593 struct set_value;
594
595 typedef struct set_func {
596 token_t type;
597 char *name;
598 void (*handler)(zval *, struct set_value *set, impl_val *ret_val);
599 } set_func;
600
601 static inline set_func *init_set_func(token_t type, const char *name) {
602 set_func *func = calloc(1, sizeof(*func));
603 func->type = type;
604 func->name = strdup(name);
605 return func;
606 }
607
608 static inline void free_set_func(set_func *func) {
609 free(func->name);
610 free(func);
611 }
612
613 typedef struct set_value {
614 set_func *func;
615 decl_vars *vars;
616 struct set_value **inner;
617 size_t count;
618 } set_value;
619
620 static inline set_value *init_set_value(set_func *func, decl_vars *vars) {
621 set_value *val = calloc(1, sizeof(*val));
622 val->func = func;
623 val->vars = vars;
624 return val;
625 }
626 static inline set_value *add_inner_set_value(set_value *val, set_value *inner) {
627 val->inner = realloc(val->inner, ++val->count * sizeof(*val->inner));
628 val->inner[val->count-1] = inner;
629 return val;
630 }
631
632 static inline void free_set_value(set_value *val) {
633 free_set_func(val->func);
634 free_decl_vars(val->vars);
635 if (val->inner) {
636 size_t i;
637 for (i = 0; i < val->count; ++i) {
638 free_set_value(val->inner[i]);
639 }
640 free(val->inner);
641 }
642 free(val);
643 }
644
645 typedef struct set_stmt {
646 impl_var *var;
647 set_value *val;
648 impl_arg *arg;
649 } set_stmt;
650
651 static inline set_stmt *init_set_stmt(impl_var *var, set_value *val) {
652 set_stmt *set = calloc(1, sizeof(*set));
653 set->var = var;
654 set->val = val;
655 return set;
656 }
657
658 static inline void free_set_stmt(set_stmt *set) {
659 free_impl_var(set->var);
660 free_set_value(set->val);
661 free(set);
662 }
663
664 typedef struct return_stmt {
665 set_value *set;
666 decl_arg *decl;
667 } return_stmt;
668
669 static inline return_stmt *init_return_stmt(set_value *val) {
670 return_stmt *ret = calloc(1, sizeof(*ret));
671 ret->set = val;
672 return ret;
673 }
674
675 static inline void free_return_stmt(return_stmt *ret) {
676 //free_set_func(ret->func);
677 //free_decl_var(ret->decl);
678 free_set_value(ret->set);
679 free(ret);
680 }
681
682 typedef struct free_call {
683 char *func;
684 decl_vars *vars;
685 decl *decl;
686 } free_call;
687
688 static inline free_call *init_free_call(const char *func, decl_vars *vars) {
689 free_call *f = calloc(1, sizeof(*f));
690 f->func = strdup(func);
691 f->vars = vars;
692 return f;
693 }
694
695 static inline void free_free_call(free_call *f) {
696 free(f->func);
697 free(f);
698 }
699
700 typedef struct free_calls {
701 free_call **list;
702 size_t count;
703 } free_calls;
704
705 static inline free_calls *init_free_calls(free_call *f) {
706 free_calls *fcs = calloc(1, sizeof(*fcs));
707 if (f) {
708 fcs->count = 1;
709 fcs->list = calloc(1, sizeof(*fcs->list));
710 fcs->list[0] = f;
711 }
712 return fcs;
713 }
714
715 static inline void free_free_calls(free_calls *fcs) {
716 size_t i;
717
718 for (i = 0; i < fcs->count; ++i) {
719 free_free_call(fcs->list[i]);
720 }
721 free(fcs->list);
722 free(fcs);
723 }
724
725 static inline free_calls *add_free_call(free_calls *fcs, free_call *f) {
726 fcs->list = realloc(fcs->list, ++fcs->count * sizeof(*fcs->list));
727 fcs->list[fcs->count-1] = f;
728 return fcs;
729 }
730
731 typedef struct free_stmt {
732 free_calls *calls;
733 } free_stmt;
734
735 static inline free_stmt *init_free_stmt(free_calls *calls) {
736 free_stmt *f = calloc(1, sizeof(*f));
737 f->calls = calls;
738 return f;
739 }
740
741 static inline void free_free_stmt(free_stmt *f) {
742 free_free_calls(f->calls);
743 free(f);
744 }
745
746 typedef struct impl_stmt {
747 token_t type;
748 union {
749 let_stmt *let;
750 set_stmt *set;
751 return_stmt *ret;
752 free_stmt *fre;
753 void *ptr;
754 } s;
755 } impl_stmt;
756
757 static inline impl_stmt *init_impl_stmt(token_t type, void *ptr) {
758 impl_stmt *stmt = calloc(1, sizeof(*stmt));
759 stmt->type = type;
760 stmt->s.ptr = ptr;
761 return stmt;
762 }
763
764 static inline void free_impl_stmt(impl_stmt *stmt) {
765 switch (stmt->type) {
766 case PSI_T_LET:
767 free_let_stmt(stmt->s.let);
768 break;
769 case PSI_T_SET:
770 free_set_stmt(stmt->s.set);
771 break;
772 case PSI_T_RETURN:
773 free_return_stmt(stmt->s.ret);
774 break;
775 case PSI_T_FREE:
776 free_free_stmt(stmt->s.fre);
777 break;
778 }
779 free(stmt);
780 }
781
782 typedef struct impl_stmts {
783 struct {
784 return_stmt **list;
785 size_t count;
786 } ret;
787 struct {
788 let_stmt **list;
789 size_t count;
790 } let;
791 struct {
792 set_stmt **list;
793 size_t count;
794 } set;
795 struct {
796 free_stmt **list;
797 size_t count;
798 } fre;
799 } impl_stmts;
800
801 static inline void *add_impl_stmt_ex(void *list, size_t count, void *stmt) {
802 list = realloc(list, count * sizeof(list));
803 ((void **)list)[count-1] = stmt;
804 return list;
805 }
806
807 static inline impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt) {
808 switch (stmt->type) {
809 case PSI_T_RETURN:
810 stmts->ret.list = add_impl_stmt_ex(stmts->ret.list, ++stmts->ret.count, stmt->s.ret);
811 break;
812 case PSI_T_LET:
813 stmts->let.list = add_impl_stmt_ex(stmts->let.list, ++stmts->let.count, stmt->s.let);
814 break;
815 case PSI_T_SET:
816 stmts->set.list = add_impl_stmt_ex(stmts->set.list, ++stmts->set.count, stmt->s.set);
817 break;
818 case PSI_T_FREE:
819 stmts->fre.list = add_impl_stmt_ex(stmts->fre.list, ++stmts->fre.count, stmt->s.fre);
820 break;
821 }
822 free(stmt);
823 return stmts;
824 }
825
826 static inline impl_stmts *init_impl_stmts(impl_stmt *stmt) {
827 impl_stmts *stmts = calloc(1, sizeof(*stmts));
828 return add_impl_stmt(stmts, stmt);
829 }
830
831 static inline void free_impl_stmts(impl_stmts *stmts) {
832 size_t i;
833
834 for (i = 0; i < stmts->let.count; ++i) {
835 free_let_stmt(stmts->let.list[i]);
836 }
837 free(stmts->let.list);
838 for (i = 0; i < stmts->ret.count; ++i) {
839 free_return_stmt(stmts->ret.list[i]);
840 }
841 free(stmts->ret.list);
842 for (i = 0; i < stmts->set.count; ++i) {
843 free_set_stmt(stmts->set.list[i]);
844 }
845 free(stmts->set.list);
846 for (i = 0; i < stmts->fre.count; ++i) {
847 free_free_stmt(stmts->fre.list[i]);
848 }
849 free(stmts->fre.list);
850 free(stmts);
851 }
852
853 typedef struct impl {
854 impl_func *func;
855 impl_stmts *stmts;
856 decl *decl;
857 } impl;
858
859 static inline impl *init_impl(impl_func *func, impl_stmts *stmts) {
860 impl *i = calloc(1, sizeof(*i));
861 i->func = func;
862 i->stmts = stmts;
863 return i;
864 }
865
866 static inline void free_impl(impl *impl) {
867 free_impl_func(impl->func);
868 free_impl_stmts(impl->stmts);
869 free(impl);
870 }
871
872 typedef struct impls {
873 size_t count;
874 impl **list;
875 } impls;
876
877 static inline impls *add_impl(impls *impls, impl *impl) {
878 if (!impls) {
879 impls = calloc(1, sizeof(*impls));
880 }
881 impls->list = realloc(impls->list, ++impls->count * sizeof(*impls->list));
882 impls->list[impls->count-1] = impl;
883 return impls;
884 }
885
886 static void free_impls(impls *impls) {
887 size_t i;
888
889 for (i = 0; i < impls->count; ++i) {
890 free_impl(impls->list[i]);
891 }
892 free(impls->list);
893 free(impls);
894 }
895
896 typedef struct const_type {
897 token_t type;
898 char *name;
899 } const_type;
900
901 static inline const_type *init_const_type(token_t type, const char *name) {
902 const_type *ct = calloc(1, sizeof(*ct));
903 ct->type = type;
904 ct->name = strdup(name);
905 return ct;
906 }
907
908 static inline void free_const_type(const_type *type) {
909 free(type->name);
910 free(type);
911 }
912
913 typedef struct constant {
914 const_type *type;
915 char *name;
916 impl_def_val *val;
917 } constant;
918
919 static inline constant *init_constant(const_type *type, const char *name, impl_def_val *val) {
920 constant *c = calloc(1, sizeof(*c));
921 c->type = type;
922 c->name = strdup(name);
923 c->val = val;
924 return c;
925 }
926
927 static inline void free_constant(constant *constant) {
928 free_const_type(constant->type);
929 free(constant->name);
930 free_impl_def_val(constant->val);
931 free(constant);
932 }
933
934 typedef struct constants {
935 size_t count;
936 constant **list;
937 } constants;
938
939 static inline constants *add_constant(constants *constants, constant *constant) {
940 if (!constants) {
941 constants = calloc(1, sizeof(*constants));
942 }
943 constants->list = realloc(constants->list, ++constants->count * sizeof(*constants->list));
944 constants->list[constants->count-1] = constant;
945 return constants;
946 }
947
948 static inline void free_constants(constants *c) {
949 size_t i;
950
951 for (i = 0; i < c->count; ++i) {
952 free_constant(c->list[i]);
953 }
954 free(c->list);
955 free(c);
956 }
957
958 #define PSI_ERROR 16
959 #define PSI_WARNING 32
960 typedef void (*psi_error_cb)(int type, const char *msg, ...);
961
962 typedef struct decl_file {
963 char *ln;
964 char *fn;
965 } decl_file;
966
967 static inline void free_decl_file(decl_file *file) {
968 if (file->ln) {
969 free(file->ln);
970 }
971 if (file->fn) {
972 free(file->fn);
973 }
974 memset(file, 0, sizeof(*file));
975 }
976
977 typedef struct decl_libs {
978 void **dl;
979 size_t count;
980 } decl_libs;
981
982 static inline void free_decl_libs(decl_libs *libs) {
983 if (libs->dl) {
984 size_t i;
985 for (i = 0; i < libs->count; ++i) {
986 if (libs->dl[i]) {
987 dlclose(libs->dl[i]);
988 }
989 }
990 free(libs->dl);
991 }
992 memset(libs, 0, sizeof(*libs));
993 }
994
995 static inline void add_decl_lib(decl_libs *libs, void *dlopened) {
996 libs->dl = realloc(libs->dl, ++libs->count * sizeof(*libs->dl));
997 libs->dl[libs->count-1] = dlopened;
998 }
999
1000 #define PSI_DATA(D) ((PSI_Data *) (D))
1001 #define PSI_DATA_MEMBERS \
1002 constants *consts; \
1003 decl_typedefs *defs; \
1004 decl_structs *structs; \
1005 decls *decls; \
1006 impls *impls; \
1007 union { \
1008 decl_file file; \
1009 decl_libs libs; \
1010 } psi; \
1011 psi_error_cb error
1012 typedef struct PSI_Data {
1013 PSI_DATA_MEMBERS;
1014 } PSI_Data;
1015
1016 static inline PSI_Data *PSI_DataExchange(PSI_Data *dest, PSI_Data *src) {
1017 if (!dest) {
1018 dest = malloc(sizeof(*dest));
1019 }
1020 memcpy(dest, src, sizeof(*dest));
1021 memset(src, 0, sizeof(*src));
1022 return dest;
1023 }
1024
1025 static inline void PSI_DataDtor(PSI_Data *data) {
1026 if (data->consts) {
1027 free_constants(data->consts);
1028 }
1029 if (data->defs) {
1030 free_decl_typedefs(data->defs);
1031 }
1032 if (data->structs) {
1033 free_decl_structs(data->structs);
1034 }
1035 if (data->decls) {
1036 free_decls(data->decls);
1037 }
1038 if (data->impls) {
1039 free_impls(data->impls);
1040 }
1041 free_decl_file(&data->psi.file);
1042 }
1043
1044 typedef struct PSI_Parser {
1045 PSI_DATA_MEMBERS;
1046 FILE *fp;
1047 unsigned flags;
1048 unsigned errors;
1049 void *proc;
1050 size_t line;
1051 token_t num;
1052 char *cur, *tok, *lim, *eof, *ctx, *mrk, buf[BSIZE];
1053 } PSI_Parser;
1054
1055 static inline PSI_Token *PSI_TokenAlloc(PSI_Parser *P) {
1056 PSI_Token *T;
1057 size_t token_len;
1058
1059 if (P->cur <= P->tok) {
1060 return NULL;
1061 }
1062
1063 token_len = P->cur - P->tok;
1064
1065 T = calloc(1, sizeof(*T) + token_len);
1066 T->type = P->num;
1067 T->line = P->line;
1068 T->size = token_len;
1069 T->text[token_len] = 0;
1070 memcpy(T->text, P->tok, token_len);
1071
1072 return T;
1073 }
1074
1075 #define PSI_PARSER_DEBUG 0x1
1076
1077 PSI_Parser *PSI_ParserInit(PSI_Parser *P, const char *filename, psi_error_cb error, unsigned flags);
1078 void PSI_ParserSyntaxError(PSI_Parser *P, const char *fn, size_t ln, const char *msg, ...);
1079 size_t PSI_ParserFill(PSI_Parser *P, size_t n);
1080 token_t PSI_ParserScan(PSI_Parser *P);
1081 void PSI_ParserParse(PSI_Parser *P, PSI_Token *T);
1082 void PSI_ParserDtor(PSI_Parser *P);
1083 void PSI_ParserFree(PSI_Parser **P);
1084
1085 #endif