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