a9060bd5a67d5973edeae5b486935ce751fa52f2
[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 typedef struct impl_type {
356 char *name;
357 token_t type;
358 } impl_type;
359
360 static inline impl_type *init_impl_type(token_t type, const char *name) {
361 impl_type *t = calloc(1, sizeof(*t));
362
363 t->type = type;
364 t->name = strdup(name);
365 return t;
366 }
367
368 static inline void free_impl_type(impl_type *type) {
369 free(type->name);
370 free(type);
371 }
372
373 typedef struct impl_var {
374 char *name;
375 unsigned reference:1;
376 } impl_var;
377
378 static inline impl_var *init_impl_var(const char *name, int is_reference) {
379 impl_var *var = calloc(1, sizeof(*var));
380 var->name = strdup(name);
381 var->reference = is_reference;
382 return var;
383 }
384
385 static inline void free_impl_var(impl_var *var) {
386 free(var->name);
387 free(var);
388 }
389
390 typedef struct impl_def_val {
391 token_t type;
392 char *text;
393 } impl_def_val;
394
395 static inline impl_def_val *init_impl_def_val(token_t t, const char *text) {
396 impl_def_val *def = calloc(1, sizeof(*def));
397 def->type = t;
398 def->text = strdup(text);
399 return def;
400 }
401
402 static inline void free_impl_def_val(impl_def_val *def) {
403 free(def->text);
404 free(def);
405 }
406
407 typedef struct impl_arg {
408 impl_type *type;
409 impl_var *var;
410 impl_def_val *def;
411 impl_val val;
412 zval *_zv;
413 } impl_arg;
414
415 static inline impl_arg *init_impl_arg(impl_type *type, impl_var *var, impl_def_val *def) {
416 impl_arg *arg = calloc(1, sizeof(*arg));
417 arg->type = type;
418 arg->var = var;
419 arg->def = def;
420 return arg;
421 }
422
423 static inline void free_impl_arg(impl_arg *arg) {
424 free_impl_type(arg->type);
425 free_impl_var(arg->var);
426 if (arg->def) {
427 free_impl_def_val(arg->def);
428 }
429 free(arg);
430 }
431
432 typedef struct impl_args {
433 impl_arg **args;
434 size_t count;
435 } impl_args;
436
437 static inline impl_args *init_impl_args(impl_arg *arg) {
438 impl_args *args = calloc(1, sizeof(*args));
439 if (arg) {
440 args->count = 1;
441 args->args = calloc(1, sizeof(*args->args));
442 args->args[0] = arg;
443 }
444 return args;
445 }
446
447 static inline impl_args *add_impl_arg(impl_args *args, impl_arg *arg) {
448 args->args = realloc(args->args, ++args->count * sizeof(*args->args));
449 args->args[args->count-1] = arg;
450 return args;
451 }
452
453 static inline void free_impl_args(impl_args *args) {
454 size_t i;
455
456 for (i = 0; i < args->count; ++i) {
457 free_impl_arg(args->args[i]);
458 }
459 free(args->args);
460 free(args);
461 }
462
463 typedef struct impl_func {
464 char *name;
465 impl_args *args;
466 impl_type *return_type;
467 unsigned return_reference:1;
468 } impl_func;
469
470 static inline impl_func *init_impl_func(char *name, impl_args *args, impl_type *type, int ret_reference) {
471 impl_func *func = calloc(1, sizeof(*func));
472 func->name = strdup(name);
473 func->args = args ? args : init_impl_args(NULL);
474 func->return_type = type;
475 func->return_reference = ret_reference;
476 return func;
477 }
478
479 static inline void free_impl_func(impl_func *f) {
480 free_impl_type(f->return_type);
481 free_impl_args(f->args);
482 free(f->name);
483 free(f);
484 }
485
486 typedef struct let_calloc {
487 size_t n;
488 decl_type *type;
489 } let_calloc;
490
491 static inline let_calloc *init_let_calloc(long n, decl_type *type) {
492 let_calloc *alloc = calloc(1, sizeof(*alloc));
493 alloc->n = n;
494 alloc->type = type;
495 return alloc;
496 }
497
498 static inline void free_let_calloc(let_calloc *alloc) {
499 free_decl_type(alloc->type);
500 free(alloc);
501 }
502
503 typedef struct let_func {
504 token_t type;
505 char *name;
506 let_calloc *alloc;
507 } let_func;
508
509 static inline let_func *init_let_func(token_t type, const char *name, let_calloc *alloc) {
510 let_func *func = calloc(1, sizeof(*func));
511 func->type = type;
512 func->name = strdup(name);
513 func->alloc = alloc;
514 return func;
515 }
516
517 static inline void free_let_func(let_func *func) {
518 if (func->alloc) {
519 free_let_calloc(func->alloc);
520 }
521 free(func->name);
522 free(func);
523 }
524
525 typedef struct let_value {
526 let_func *func;
527 impl_var *var;
528 unsigned is_reference:1;
529 } let_value;
530
531 static inline let_value *init_let_value(let_func *func, impl_var *var, int is_reference) {
532 let_value *val = calloc(1, sizeof(*val));
533 val->is_reference = is_reference;
534 val->func = func;
535 val->var = var;
536 return val;
537 }
538
539 static inline void free_let_value(let_value *val) {
540 if (val->func) {
541 free_let_func(val->func);
542 }
543 if (val->var) {
544 free_impl_var(val->var);
545 }
546 free(val);
547 }
548
549 typedef struct let_stmt {
550 decl_var *var;
551 let_value *val;
552 impl_arg *arg;
553 impl_val out;
554 void *ptr;
555 void *mem;
556 } let_stmt;
557
558 static inline let_stmt *init_let_stmt(decl_var *var, let_value *val) {
559 let_stmt *let = calloc(1, sizeof(*let));
560 let->var = var;
561 let->val = val;
562 return let;
563 }
564
565 static inline void free_let_stmt(let_stmt *stmt) {
566 free_decl_var(stmt->var);
567 if (stmt->val) {
568 free_let_value(stmt->val);
569 }
570 free(stmt);
571 }
572
573 typedef struct set_func {
574 token_t type;
575 char *name;
576 void (*handler)(zval *, token_t, impl_val *, decl_var *);
577 } set_func;
578
579 static inline set_func *init_set_func(token_t type, const char *name) {
580 set_func *func = calloc(1, sizeof(*func));
581 func->type = type;
582 func->name = strdup(name);
583 return func;
584 }
585
586 static inline void free_set_func(set_func *func) {
587 free(func->name);
588 free(func);
589 }
590
591 typedef struct set_value {
592 set_func *func;
593 decl_vars *vars;
594 struct set_value **inner;
595 size_t count;
596 } set_value;
597
598 static inline set_value *init_set_value(set_func *func, decl_vars *vars) {
599 set_value *val = calloc(1, sizeof(*val));
600 val->func = func;
601 val->vars = vars;
602 return val;
603 }
604 static inline set_value *add_inner_set_value(set_value *val, set_value *inner) {
605 val->inner = realloc(val->inner, ++val->count * sizeof(*val->inner));
606 val->inner[val->count-1] = inner;
607 return val;
608 }
609
610 static inline void free_set_value(set_value *val) {
611 free_set_func(val->func);
612 free_decl_vars(val->vars);
613 if (val->inner) {
614 size_t i;
615 for (i = 0; i < val->count; ++i) {
616 free_set_value(val->inner[i]);
617 }
618 }
619 free(val);
620 }
621
622 typedef struct set_stmt {
623 impl_var *var;
624 set_value *val;
625 impl_arg *arg;
626 } set_stmt;
627
628 static inline set_stmt *init_set_stmt(impl_var *var, set_value *val) {
629 set_stmt *set = calloc(1, sizeof(*set));
630 set->var = var;
631 set->val = val;
632 return set;
633 }
634
635 static inline void free_set_stmt(set_stmt *set) {
636 free_impl_var(set->var);
637 free_set_value(set->val);
638 free(set);
639 }
640
641 typedef struct return_stmt {
642 set_value *set;
643 decl_arg *decl;
644 } return_stmt;
645
646 static inline return_stmt *init_return_stmt(set_value *val) {
647 return_stmt *ret = calloc(1, sizeof(*ret));
648 ret->set = val;
649 return ret;
650 }
651
652 static inline void free_return_stmt(return_stmt *ret) {
653 //free_set_func(ret->func);
654 //free_decl_var(ret->decl);
655 free_set_value(ret->set);
656 free(ret);
657 }
658
659 typedef struct free_stmt {
660 decl_vars *vars;
661 } free_stmt;
662
663 static inline free_stmt *init_free_stmt(decl_vars *vars) {
664 free_stmt *free_ = calloc(1, sizeof(*free_));
665 free_->vars = vars;
666 return free_;
667 }
668
669 static inline void free_free_stmt(free_stmt *free_) {
670 free_decl_vars(free_->vars);
671 free(free_);
672 }
673
674 typedef struct impl_stmt {
675 token_t type;
676 union {
677 let_stmt *let;
678 set_stmt *set;
679 return_stmt *ret;
680 free_stmt *fre;
681 void *ptr;
682 } s;
683 } impl_stmt;
684
685 static inline impl_stmt *init_impl_stmt(token_t type, void *ptr) {
686 impl_stmt *stmt = calloc(1, sizeof(*stmt));
687 stmt->type = type;
688 stmt->s.ptr = ptr;
689 return stmt;
690 }
691
692 static inline void free_impl_stmt(impl_stmt *stmt) {
693 switch (stmt->type) {
694 case PSI_T_LET:
695 free_let_stmt(stmt->s.let);
696 break;
697 case PSI_T_SET:
698 free_set_stmt(stmt->s.set);
699 break;
700 case PSI_T_RETURN:
701 free_return_stmt(stmt->s.ret);
702 break;
703 case PSI_T_FREE:
704 free_free_stmt(stmt->s.fre);
705 break;
706 }
707 free(stmt);
708 }
709
710 typedef struct impl_stmts {
711 struct {
712 return_stmt **list;
713 size_t count;
714 } ret;
715 struct {
716 let_stmt **list;
717 size_t count;
718 } let;
719 struct {
720 set_stmt **list;
721 size_t count;
722 } set;
723 struct {
724 free_stmt **list;
725 size_t count;
726 } fre;
727 } impl_stmts;
728
729 static inline void *add_impl_stmt_ex(void *list, size_t count, void *stmt) {
730 list = realloc(list, count * sizeof(list));
731 ((void **)list)[count-1] = stmt;
732 return list;
733 }
734
735 static inline impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt) {
736 switch (stmt->type) {
737 case PSI_T_RETURN:
738 stmts->ret.list = add_impl_stmt_ex(stmts->ret.list, ++stmts->ret.count, stmt->s.ret);
739 break;
740 case PSI_T_LET:
741 stmts->let.list = add_impl_stmt_ex(stmts->let.list, ++stmts->let.count, stmt->s.let);
742 break;
743 case PSI_T_SET:
744 stmts->set.list = add_impl_stmt_ex(stmts->set.list, ++stmts->set.count, stmt->s.set);
745 break;
746 case PSI_T_FREE:
747 stmts->fre.list = add_impl_stmt_ex(stmts->fre.list, ++stmts->fre.count, stmt->s.fre);
748 break;
749 }
750 free(stmt);
751 return stmts;
752 }
753
754 static inline impl_stmts *init_impl_stmts(impl_stmt *stmt) {
755 impl_stmts *stmts = calloc(1, sizeof(*stmts));
756 return add_impl_stmt(stmts, stmt);
757 }
758
759 static inline void free_impl_stmts(impl_stmts *stmts) {
760 size_t i;
761
762 for (i = 0; i < stmts->let.count; ++i) {
763 free_let_stmt(stmts->let.list[i]);
764 }
765 free(stmts->let.list);
766 for (i = 0; i < stmts->ret.count; ++i) {
767 free_return_stmt(stmts->ret.list[i]);
768 }
769 free(stmts->ret.list);
770 for (i = 0; i < stmts->set.count; ++i) {
771 free_set_stmt(stmts->set.list[i]);
772 }
773 free(stmts->set.list);
774 for (i = 0; i < stmts->fre.count; ++i) {
775 free_free_stmt(stmts->fre.list[i]);
776 }
777 free(stmts->fre.list);
778 free(stmts);
779 }
780
781 typedef struct impl {
782 impl_func *func;
783 impl_stmts *stmts;
784 decl *decl;
785 } impl;
786
787 static inline impl *init_impl(impl_func *func, impl_stmts *stmts) {
788 impl *i = calloc(1, sizeof(*i));
789 i->func = func;
790 i->stmts = stmts;
791 return i;
792 }
793
794 static inline void free_impl(impl *impl) {
795 free_impl_func(impl->func);
796 free_impl_stmts(impl->stmts);
797 free(impl);
798 }
799
800 typedef struct impls {
801 size_t count;
802 impl **list;
803 } impls;
804
805 static inline impls *add_impl(impls *impls, impl *impl) {
806 if (!impls) {
807 impls = calloc(1, sizeof(*impls));
808 }
809 impls->list = realloc(impls->list, ++impls->count * sizeof(*impls->list));
810 impls->list[impls->count-1] = impl;
811 return impls;
812 }
813
814 static void free_impls(impls *impls) {
815 size_t i;
816
817 for (i = 0; i < impls->count; ++i) {
818 free_impl(impls->list[i]);
819 }
820 free(impls->list);
821 free(impls);
822 }
823
824 typedef struct const_type {
825 token_t type;
826 char *name;
827 } const_type;
828
829 static inline const_type *init_const_type(token_t type, const char *name) {
830 const_type *ct = calloc(1, sizeof(*ct));
831 ct->type = type;
832 ct->name = strdup(name);
833 return ct;
834 }
835
836 static inline void free_const_type(const_type *type) {
837 free(type->name);
838 free(type);
839 }
840
841 typedef struct constant {
842 const_type *type;
843 char *name;
844 impl_def_val *val;
845 } constant;
846
847 static inline constant *init_constant(const_type *type, const char *name, impl_def_val *val) {
848 constant *c = calloc(1, sizeof(*c));
849 c->type = type;
850 c->name = strdup(name);
851 c->val = val;
852 return c;
853 }
854
855 static inline void free_constant(constant *constant) {
856 free_const_type(constant->type);
857 free(constant->name);
858 free_impl_def_val(constant->val);
859 free(constant);
860 }
861
862 typedef struct constants {
863 size_t count;
864 constant **list;
865 } constants;
866
867 static inline constants *add_constant(constants *constants, constant *constant) {
868 if (!constants) {
869 constants = calloc(1, sizeof(*constants));
870 }
871 constants->list = realloc(constants->list, ++constants->count * sizeof(*constants->list));
872 constants->list[constants->count-1] = constant;
873 return constants;
874 }
875
876 static inline void free_constants(constants *c) {
877 size_t i;
878
879 for (i = 0; i < c->count; ++i) {
880 free_constant(c->list[i]);
881 }
882 free(c->list);
883 free(c);
884 }
885
886 #define PSI_ERROR 16
887 #define PSI_WARNING 32
888 typedef void (*psi_error_cb)(int type, const char *msg, ...);
889
890 typedef struct decl_file {
891 char *ln;
892 char *fn;
893 } decl_file;
894
895 static inline void free_decl_file(decl_file *file) {
896 if (file->ln) {
897 free(file->ln);
898 }
899 if (file->fn) {
900 free(file->fn);
901 }
902 memset(file, 0, sizeof(*file));
903 }
904
905 typedef struct decl_libs {
906 void **dl;
907 size_t count;
908 } decl_libs;
909
910 static inline void free_decl_libs(decl_libs *libs) {
911 if (libs->dl) {
912 size_t i;
913 for (i = 0; i < libs->count; ++i) {
914 if (libs->dl[i]) {
915 dlclose(libs->dl[i]);
916 }
917 }
918 free(libs->dl);
919 }
920 memset(libs, 0, sizeof(*libs));
921 }
922
923 static inline void add_decl_lib(decl_libs *libs, void *dlopened) {
924 libs->dl = realloc(libs->dl, ++libs->count * sizeof(*libs->dl));
925 libs->dl[libs->count-1] = dlopened;
926 }
927
928 #define PSI_DATA(D) ((PSI_Data *) (D))
929 #define PSI_DATA_MEMBERS \
930 constants *consts; \
931 decl_typedefs *defs; \
932 decl_structs *structs; \
933 decls *decls; \
934 impls *impls; \
935 union { \
936 decl_file file; \
937 decl_libs libs; \
938 } psi; \
939 psi_error_cb error
940 typedef struct PSI_Data {
941 PSI_DATA_MEMBERS;
942 } PSI_Data;
943
944 static inline PSI_Data *PSI_DataExchange(PSI_Data *dest, PSI_Data *src) {
945 if (!dest) {
946 dest = malloc(sizeof(*dest));
947 }
948 memcpy(dest, src, sizeof(*dest));
949 memset(src, 0, sizeof(*src));
950 return dest;
951 }
952
953 static inline void PSI_DataDtor(PSI_Data *data) {
954 if (data->consts) {
955 free_constants(data->consts);
956 }
957 if (data->defs) {
958 free_decl_typedefs(data->defs);
959 }
960 if (data->structs) {
961 free_decl_structs(data->structs);
962 }
963 if (data->decls) {
964 free_decls(data->decls);
965 }
966 if (data->impls) {
967 free_impls(data->impls);
968 }
969 free_decl_file(&data->psi.file);
970 }
971
972 typedef struct PSI_Parser {
973 PSI_DATA_MEMBERS;
974 FILE *fp;
975 unsigned flags;
976 unsigned errors;
977 void *proc;
978 size_t line;
979 token_t num;
980 char *cur, *tok, *lim, *eof, *ctx, *mrk, buf[BSIZE];
981 } PSI_Parser;
982
983 static inline PSI_Token *PSI_TokenAlloc(PSI_Parser *P) {
984 PSI_Token *T;
985 size_t token_len;
986
987 if (P->cur <= P->tok) {
988 return NULL;
989 }
990
991 token_len = P->cur - P->tok;
992
993 T = calloc(1, sizeof(*T) + token_len);
994 T->type = P->num;
995 T->line = P->line;
996 T->size = token_len;
997 T->text[token_len] = 0;
998 memcpy(T->text, P->tok, token_len);
999
1000 return T;
1001 }
1002
1003 #define PSI_PARSER_DEBUG 0x1
1004
1005 PSI_Parser *PSI_ParserInit(PSI_Parser *P, const char *filename, psi_error_cb error, unsigned flags);
1006 void PSI_ParserSyntaxError(PSI_Parser *P, const char *fn, size_t ln, const char *msg, ...);
1007 size_t PSI_ParserFill(PSI_Parser *P, size_t n);
1008 token_t PSI_ParserScan(PSI_Parser *P);
1009 void PSI_ParserParse(PSI_Parser *P, PSI_Token *T);
1010 void PSI_ParserDtor(PSI_Parser *P);
1011 void PSI_ParserFree(PSI_Parser **P);
1012
1013 #endif