EOF
[m6w6/ext-psi] / src / parser.h
index d4909dd9fa496456c5c790eb18e95e91b66c4cbd..5ec0b95e6354875c2f4fa7aa881df78c9b11c67b 100644 (file)
 
 typedef int token_t;
 
+/* in php_psi.h */
+size_t psi_t_alignment(token_t);
+size_t psi_t_size(token_t);
+
 typedef struct PSI_Token {
        token_t type;
        unsigned line;
@@ -25,15 +29,23 @@ typedef struct decl_type {
        char *name;
        token_t type;
        struct decl_type *real;
+       struct decl_struct *strct;
 } decl_type;
 
-static inline decl_type *init_decl_type(token_t type, char *name) {
-       decl_type *t = malloc(sizeof(*t));
+static inline decl_type *init_decl_type(token_t type, const char *name) {
+       decl_type *t = calloc(1, sizeof(*t));
        t->type = type;
        t->name = strdup(name);
        return t;
 }
 
+static inline decl_type *real_decl_type(decl_type *type) {
+       while (type->real) {
+               type = type->real;
+       }
+       return type;
+}
+
 static inline void free_decl_type(decl_type *type) {
        free(type->name);
        free(type);
@@ -44,8 +56,8 @@ typedef struct decl_typedef {
        decl_type *type;
 } decl_typedef;
 
-static inline decl_typedef *init_decl_typedef(char *name, decl_type *type) {
-       decl_typedef *t = malloc(sizeof(*t));
+static inline decl_typedef *init_decl_typedef(const char *name, decl_type *type) {
+       decl_typedef *t = calloc(1, sizeof(*t));
        t->alias = strdup(name);
        t->type = type;
        return t;
@@ -62,7 +74,7 @@ typedef struct decl_typedefs {
        decl_typedef **list;
 } decl_typedefs;
 
-static decl_typedefs *add_decl_typedef(decl_typedefs *defs, decl_typedef *def) {
+static inline decl_typedefs *add_decl_typedef(decl_typedefs *defs, decl_typedef *def) {
        if (!defs) {
                defs = calloc(1, sizeof(*defs));
        }
@@ -84,12 +96,15 @@ static void free_decl_typedefs(decl_typedefs *defs) {
 typedef struct decl_var {
        char *name;
        unsigned pointer_level;
+       unsigned array_size;
+       struct decl_arg *arg;
 } decl_var;
 
-static inline decl_var *init_decl_var(char *name, unsigned pl) {
-       decl_var *v = malloc(sizeof(*v));
+static inline decl_var *init_decl_var(const char *name, unsigned pl, unsigned as) {
+       decl_var *v = calloc(1, sizeof(*v));
        v->name = (char *) strdup((const char *) name);
        v->pointer_level = pl;
+       v->array_size = as;
        return v;
 }
 
@@ -98,21 +113,44 @@ static inline void free_decl_var(decl_var *var) {
        free(var);
 }
 
+typedef struct decl_struct_layout {
+       size_t pos;
+       size_t len;
+} decl_struct_layout;
+
+static inline decl_struct_layout *init_decl_struct_layout(size_t pos, size_t len) {
+       decl_struct_layout *l = calloc(1, sizeof(*l));
+
+       l->pos = pos;
+       l->len = len;
+       return l;
+}
+
+static inline void free_decl_struct_layout(decl_struct_layout *l) {
+       free(l);
+}
+
 typedef struct decl_arg {
        decl_type *type;
        decl_var *var;
+       decl_struct_layout *layout;
+       struct let_stmt *let;
 } decl_arg;
 
 static inline decl_arg *init_decl_arg(decl_type *type, decl_var *var) {
-       decl_arg *arg = malloc(sizeof(*arg));
+       decl_arg *arg = calloc(1, sizeof(*arg));
        arg->type = type;
        arg->var = var;
+       var->arg = arg;
        return arg;
 }
 
 static inline void free_decl_arg(decl_arg *arg) {
        free_decl_type(arg->type);
        free_decl_var(arg->var);
+       if (arg->layout) {
+               free_decl_struct_layout(arg->layout);
+       }
        free(arg);
 }
 
@@ -122,10 +160,12 @@ typedef struct decl_vars {
 } decl_vars;
 
 static inline decl_vars *init_decl_vars(decl_var *var) {
-       decl_vars *vars = malloc(sizeof(*vars));
-       vars->count = 1;
-       vars->vars = malloc(sizeof(*vars->vars));
-       vars->vars[0] = var;
+       decl_vars *vars = calloc(1, sizeof(*vars));
+       if (var) {
+               vars->count = 1;
+               vars->vars = calloc(1, sizeof(*vars->vars));
+               vars->vars[0] = var;
+       }
        return vars;
 }
 
@@ -151,10 +191,12 @@ typedef struct decl_args {
 } decl_args;
 
 static inline decl_args *init_decl_args(decl_arg *arg) {
-       decl_args *args = malloc(sizeof(*args));
-       args->count = 1;
-       args->args = malloc(sizeof(*args->args));
-       args->args[0] = arg;
+       decl_args *args = calloc(1, sizeof(*args));
+       if (arg) {
+               args->count = 1;
+               args->args = calloc(1, sizeof(*args->args));
+               args->args[0] = arg;
+       }
        return args;
 }
 
@@ -178,8 +220,8 @@ typedef struct decl_abi {
        char *convention;
 } decl_abi;
 
-static inline decl_abi *init_decl_abi(char *convention) {
-       decl_abi *abi = malloc(sizeof(*abi));
+static inline decl_abi *init_decl_abi(const char *convention) {
+       decl_abi *abi = calloc(1, sizeof(*abi));
        abi->convention = strdup(convention);
        return abi;
 }
@@ -193,11 +235,16 @@ typedef struct decl {
        decl_abi *abi;
        decl_arg *func;
        decl_args *args;
-       void *dlptr;
+       struct impl *impl;
+       struct {
+               void *sym;
+               void *info;
+               void **args;
+       } call;
 } decl;
 
 static inline decl* init_decl(decl_abi *abi, decl_arg *func, decl_args *args) {
-       decl *d = malloc(sizeof(*d));
+       decl *d = calloc(1, sizeof(*d));
        d->abi = abi;
        d->func = func;
        d->args = args;
@@ -207,7 +254,9 @@ static inline decl* init_decl(decl_abi *abi, decl_arg *func, decl_args *args) {
 static inline void free_decl(decl *d) {
        free_decl_abi(d->abi);
        free_decl_arg(d->func);
-       free_decl_args(d->args);
+       if (d->args) {
+               free_decl_args(d->args);
+       }
        free(d);
 }
 
@@ -235,16 +284,109 @@ static inline void free_decls(decls *decls) {
        free(decls);
 }
 
+typedef struct decl_struct {
+       char *name;
+       decl_args *args;
+       size_t size;
+} decl_struct;
+
+static inline decl_struct *init_decl_struct(const char *name, decl_args *args) {
+       decl_struct *s = calloc(1, sizeof(*s));
+       s->name = strdup(name);
+       s->args = args;
+       return s;
+}
+
+static inline void free_decl_struct(decl_struct *s) {
+       if (s->args) {
+               free_decl_args(s->args);
+       }
+       free(s->name);
+       free(s);
+}
+
+typedef struct decl_structs {
+       size_t count;
+       decl_struct **list;
+} decl_structs;
+
+static inline decl_structs *add_decl_struct(decl_structs *ss, decl_struct *s) {
+       if (!ss) {
+               ss = calloc(1, sizeof(*ss));
+       }
+       ss->list = realloc(ss->list, ++ss->count * sizeof(*ss->list));
+       ss->list[ss->count-1] = s;
+       return ss;
+}
+
+static inline void free_decl_structs(decl_structs *ss) {
+       size_t i;
+
+       for (i = 0; i < ss->count; ++i) {
+               free_decl_struct(ss->list[i]);
+       }
+       free(ss->list);
+       free(ss);
+}
+
+typedef union impl_val {
+       char cval;
+       int8_t i8;
+       uint8_t u8;
+       short sval;
+       int16_t i16;
+       uint16_t u16;
+       int ival;
+       int32_t i32;
+       uint32_t u32;
+       long lval;
+       int64_t i64;
+       uint64_t u64;
+       float fval;
+       double dval;
+       union {
+               zend_bool bval;
+               zend_long lval;
+               zend_string *str;
+       } zend;
+       void *ptr;
+       uint8_t _dbg[sizeof(void *)];
+} impl_val;
+
+static inline impl_val *deref_impl_val(impl_val *ret_val, decl_var *var) {
+       unsigned i;
+
+       if (var->arg->var != var) for (i = 1; i < var->pointer_level; ++i) {
+               ret_val = *(void **) ret_val;
+       }
+       return ret_val;
+}
+
+static inline impl_val *enref_impl_val(void *ptr, decl_var *var) {
+       impl_val *val, *val_ptr;
+       unsigned i;
+
+       if (!var->pointer_level && real_decl_type(var->arg->type)->type != PSI_T_STRUCT) {
+               return ptr;
+       }
+       val = val_ptr = calloc(var->pointer_level + 1, sizeof(void *));
+       for (i = 1; i < var->pointer_level; ++i) {
+               val_ptr->ptr = (void **) val_ptr + 1;
+               val_ptr = val_ptr->ptr;
+       }
+       val_ptr->ptr = ptr;
+       return val;
+}
 typedef struct impl_type {
        char *name;
        token_t type;
 } impl_type;
 
-static inline impl_type *init_impl_type(token_t type, char *name) {
-       impl_type *t = malloc(sizeof(*t));
+static inline impl_type *init_impl_type(token_t type, const char *name) {
+       impl_type *t = calloc(1, sizeof(*t));
 
        t->type = type;
-       t->name = (char *) strdup((const char *) name);
+       t->name = strdup(name);
        return t;
 }
 
@@ -258,9 +400,9 @@ typedef struct impl_var {
        unsigned reference:1;
 } impl_var;
 
-static inline impl_var *init_impl_var(char *name, int is_reference) {
-       impl_var *var = malloc(sizeof(*var));
-       var->name = (char *) strdup((const char *) name);
+static inline impl_var *init_impl_var(const char *name, int is_reference) {
+       impl_var *var = calloc(1, sizeof(*var));
+       var->name = strdup(name);
        var->reference = is_reference;
        return var;
 }
@@ -275,10 +417,10 @@ typedef struct impl_def_val {
        char *text;
 } impl_def_val;
 
-static inline impl_def_val *init_impl_def_val(PSI_Token *T) {
-       impl_def_val *def = malloc(sizeof(*def));
-       def->type = T->type;
-       def->text = strdup(T->text);
+static inline impl_def_val *init_impl_def_val(token_t t, const char *text) {
+       impl_def_val *def = calloc(1, sizeof(*def));
+       def->type = t;
+       def->text = strdup(text);
        return def;
 }
 
@@ -291,19 +433,12 @@ typedef struct impl_arg {
        impl_type *type;
        impl_var *var;
        impl_def_val *def;
-       union {
-               unsigned char bval;
-               zend_long lval;
-               double dval;
-               struct {
-                       char *val;
-                       size_t len;
-               } str;
-       } val;
+       impl_val val;
+       zval *_zv;
 } impl_arg;
 
 static inline impl_arg *init_impl_arg(impl_type *type, impl_var *var, impl_def_val *def) {
-       impl_arg *arg = malloc(sizeof(*arg));
+       impl_arg *arg = calloc(1, sizeof(*arg));
        arg->type = type;
        arg->var = var;
        arg->def = def;
@@ -325,14 +460,11 @@ typedef struct impl_args {
 } impl_args;
 
 static inline impl_args *init_impl_args(impl_arg *arg) {
-       impl_args *args = malloc(sizeof(*args));
-       args->args = malloc(sizeof(*args->args));
+       impl_args *args = calloc(1, sizeof(*args));
        if (arg) {
                args->count = 1;
+               args->args = calloc(1, sizeof(*args->args));
                args->args[0] = arg;
-       } else {
-               args->count = 0;
-               args->args = NULL;
        }
        return args;
 }
@@ -357,13 +489,15 @@ typedef struct impl_func {
        char *name;
        impl_args *args;
        impl_type *return_type;
+       unsigned return_reference:1;
 } impl_func;
 
-static inline impl_func *init_impl_func(char *name, impl_args *args, impl_type *type) {
-       impl_func *func = malloc(sizeof(*func));
+static inline impl_func *init_impl_func(char *name, impl_args *args, impl_type *type, int ret_reference) {
+       impl_func *func = calloc(1, sizeof(*func));
        func->name = strdup(name);
        func->args = args ? args : init_impl_args(NULL);
        func->return_type = type;
+       func->return_reference = ret_reference;
        return func;
 }
 
@@ -374,19 +508,41 @@ static inline void free_impl_func(impl_func *f) {
        free(f);
 }
 
+typedef struct let_calloc {
+       size_t n;
+       decl_type *type;
+} let_calloc;
+
+static inline let_calloc *init_let_calloc(long n, decl_type *type) {
+       let_calloc *alloc = calloc(1, sizeof(*alloc));
+       alloc->n = n;
+       alloc->type = type;
+       return alloc;
+}
+
+static inline void free_let_calloc(let_calloc *alloc) {
+       free_decl_type(alloc->type);
+       free(alloc);
+}
+
 typedef struct let_func {
        token_t type;
        char *name;
+       let_calloc *alloc;
 } let_func;
 
-static inline let_func *init_let_func(token_t type, char *name) {
-       let_func *func = malloc(sizeof(*func));
+static inline let_func *init_let_func(token_t type, const char *name, let_calloc *alloc) {
+       let_func *func = calloc(1, sizeof(*func));
        func->type = type;
-       func->name = (char *) strdup((const char *) name);
+       func->name = strdup(name);
+       func->alloc = alloc;
        return func;
 }
 
 static inline void free_let_func(let_func *func) {
+       if (func->alloc) {
+               free_let_calloc(func->alloc);
+       }
        free(func->name);
        free(func);
 }
@@ -394,12 +550,12 @@ static inline void free_let_func(let_func *func) {
 typedef struct let_value {
        let_func *func;
        impl_var *var;
-       unsigned null_pointer_ref:1;
+       unsigned is_reference:1;
 } let_value;
 
-static inline let_value *init_let_value(let_func *func, impl_var *var, int null_pointer_ref) {
-       let_value *val = malloc(sizeof(*val));
-       val->null_pointer_ref = null_pointer_ref;
+static inline let_value *init_let_value(let_func *func, impl_var *var, int is_reference) {
+       let_value *val = calloc(1, sizeof(*val));
+       val->is_reference = is_reference;
        val->func = func;
        val->var = var;
        return val;
@@ -418,10 +574,14 @@ static inline void free_let_value(let_value *val) {
 typedef struct let_stmt {
        decl_var *var;
        let_value *val;
+       impl_arg *arg;
+       impl_val out;
+       void *ptr;
+       void *mem;
 } let_stmt;
 
 static inline let_stmt *init_let_stmt(decl_var *var, let_value *val) {
-       let_stmt *let = malloc(sizeof(*let));
+       let_stmt *let = calloc(1, sizeof(*let));
        let->var = var;
        let->val = val;
        return let;
@@ -429,19 +589,24 @@ static inline let_stmt *init_let_stmt(decl_var *var, let_value *val) {
 
 static inline void free_let_stmt(let_stmt *stmt) {
        free_decl_var(stmt->var);
-       free_let_value(stmt->val);
+       if (stmt->val) {
+               free_let_value(stmt->val);
+       }
        free(stmt);
 }
 
+struct set_value;
+
 typedef struct set_func {
        token_t type;
        char *name;
+       void (*handler)(zval *, struct set_value *set, impl_val *ret_val);
 } set_func;
 
-static inline set_func *init_set_func(token_t type, char *name) {
-       set_func *func = malloc(sizeof(*func));
+static inline set_func *init_set_func(token_t type, const char *name) {
+       set_func *func = calloc(1, sizeof(*func));
        func->type = type;
-       func->name = (char *) strdup((const char *) name);
+       func->name = strdup(name);
        return func;
 }
 
@@ -453,28 +618,47 @@ static inline void free_set_func(set_func *func) {
 typedef struct set_value {
        set_func *func;
        decl_vars *vars;
+       struct {
+               struct set_value *set;
+               impl_val *val;
+       } outer;
+       struct set_value **inner;
+       size_t count;
 } set_value;
 
 static inline set_value *init_set_value(set_func *func, decl_vars *vars) {
-       set_value *val = malloc(sizeof(*val));
+       set_value *val = calloc(1, sizeof(*val));
        val->func = func;
        val->vars = vars;
        return val;
 }
+static inline set_value *add_inner_set_value(set_value *val, set_value *inner) {
+       val->inner = realloc(val->inner, ++val->count * sizeof(*val->inner));
+       val->inner[val->count-1] = inner;
+       return val;
+}
 
 static inline void free_set_value(set_value *val) {
        free_set_func(val->func);
        free_decl_vars(val->vars);
+       if (val->inner) {
+               size_t i;
+               for (i = 0; i < val->count; ++i) {
+                       free_set_value(val->inner[i]);
+               }
+               free(val->inner);
+       }
        free(val);
 }
 
 typedef struct set_stmt {
        impl_var *var;
        set_value *val;
+       impl_arg *arg;
 } set_stmt;
 
 static inline set_stmt *init_set_stmt(impl_var *var, set_value *val) {
-       set_stmt *set = malloc(sizeof(*set));
+       set_stmt *set = calloc(1, sizeof(*set));
        set->var = var;
        set->val = val;
        return set;
@@ -486,36 +670,101 @@ static inline void free_set_stmt(set_stmt *set) {
        free(set);
 }
 
-typedef struct ret_stmt {
-       set_func *func;
-       decl_var *decl;
-} ret_stmt;
+typedef struct return_stmt {
+       set_value *set;
+       decl_arg *decl;
+} return_stmt;
 
-static inline ret_stmt *init_ret_stmt(set_func *func, decl_var *decl) {
-       ret_stmt *ret = malloc(sizeof(*ret));
-       ret->func = func;
-       ret->decl = decl;
+static inline return_stmt *init_return_stmt(set_value *val) {
+       return_stmt *ret = calloc(1, sizeof(*ret));
+       ret->set = val;
        return ret;
 }
 
-static inline void free_ret_stmt(ret_stmt *ret) {
-       free_set_func(ret->func);
-       free_decl_var(ret->decl);
+static inline void free_return_stmt(return_stmt *ret) {
+       //free_set_func(ret->func);
+       //free_decl_var(ret->decl);
+       free_set_value(ret->set);
        free(ret);
 }
 
+typedef struct free_call {
+       char *func;
+       decl_vars *vars;
+       decl *decl;
+} free_call;
+
+static inline free_call *init_free_call(const char *func, decl_vars *vars) {
+       free_call *f = calloc(1, sizeof(*f));
+       f->func = strdup(func);
+       f->vars = vars;
+       return f;
+}
+
+static inline void free_free_call(free_call *f) {
+       free(f->func);
+       free(f);
+}
+
+typedef struct free_calls {
+       free_call **list;
+       size_t count;
+} free_calls;
+
+static inline free_calls *init_free_calls(free_call *f) {
+       free_calls *fcs = calloc(1, sizeof(*fcs));
+       if (f) {
+               fcs->count = 1;
+               fcs->list = calloc(1, sizeof(*fcs->list));
+               fcs->list[0] = f;
+       }
+       return fcs;
+}
+
+static inline void free_free_calls(free_calls *fcs) {
+       size_t i;
+
+       for (i = 0; i < fcs->count; ++i) {
+               free_free_call(fcs->list[i]);
+       }
+       free(fcs->list);
+       free(fcs);
+}
+
+static inline free_calls *add_free_call(free_calls *fcs, free_call *f) {
+       fcs->list = realloc(fcs->list, ++fcs->count * sizeof(*fcs->list));
+       fcs->list[fcs->count-1] = f;
+       return fcs;
+}
+
+typedef struct free_stmt {
+       free_calls *calls;
+} free_stmt;
+
+static inline free_stmt *init_free_stmt(free_calls *calls) {
+       free_stmt *f = calloc(1, sizeof(*f));
+       f->calls = calls;
+       return f;
+}
+
+static inline void free_free_stmt(free_stmt *f) {
+       free_free_calls(f->calls);
+       free(f);
+}
+
 typedef struct impl_stmt {
        token_t type;
        union {
                let_stmt *let;
                set_stmt *set;
-               ret_stmt *ret;
+               return_stmt *ret;
+               free_stmt *fre;
                void *ptr;
        } s;
 } impl_stmt;
 
 static inline impl_stmt *init_impl_stmt(token_t type, void *ptr) {
-       impl_stmt *stmt = malloc(sizeof(*stmt));
+       impl_stmt *stmt = calloc(1, sizeof(*stmt));
        stmt->type = type;
        stmt->s.ptr = ptr;
        return stmt;
@@ -529,8 +778,11 @@ static inline void free_impl_stmt(impl_stmt *stmt) {
        case PSI_T_SET:
                free_set_stmt(stmt->s.set);
                break;
-       case PSI_T_RET:
-               free_ret_stmt(stmt->s.ret);
+       case PSI_T_RETURN:
+               free_return_stmt(stmt->s.ret);
+               break;
+       case PSI_T_FREE:
+               free_free_stmt(stmt->s.fre);
                break;
        }
        free(stmt);
@@ -538,7 +790,7 @@ static inline void free_impl_stmt(impl_stmt *stmt) {
 
 typedef struct impl_stmts {
        struct {
-               ret_stmt **list;
+               return_stmt **list;
                size_t count;
        } ret;
        struct {
@@ -549,6 +801,10 @@ typedef struct impl_stmts {
                set_stmt **list;
                size_t count;
        } set;
+       struct {
+               free_stmt **list;
+               size_t count;
+       } fre;
 } impl_stmts;
 
 static inline void *add_impl_stmt_ex(void *list, size_t count, void *stmt) {
@@ -559,7 +815,7 @@ static inline void *add_impl_stmt_ex(void *list, size_t count, void *stmt) {
 
 static inline impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt) {
        switch (stmt->type) {
-       case PSI_T_RET:
+       case PSI_T_RETURN:
                stmts->ret.list = add_impl_stmt_ex(stmts->ret.list, ++stmts->ret.count, stmt->s.ret);
                break;
        case PSI_T_LET:
@@ -568,7 +824,11 @@ static inline impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt) {
        case PSI_T_SET:
                stmts->set.list = add_impl_stmt_ex(stmts->set.list, ++stmts->set.count, stmt->s.set);
                break;
+       case PSI_T_FREE:
+               stmts->fre.list = add_impl_stmt_ex(stmts->fre.list, ++stmts->fre.count, stmt->s.fre);
+               break;
        }
+       free(stmt);
        return stmts;
 }
 
@@ -585,13 +845,17 @@ static inline void free_impl_stmts(impl_stmts *stmts) {
        }
        free(stmts->let.list);
        for (i = 0; i < stmts->ret.count; ++i) {
-               free_ret_stmt(stmts->ret.list[i]);
+               free_return_stmt(stmts->ret.list[i]);
        }
        free(stmts->ret.list);
        for (i = 0; i < stmts->set.count; ++i) {
                free_set_stmt(stmts->set.list[i]);
        }
        free(stmts->set.list);
+       for (i = 0; i < stmts->fre.count; ++i) {
+               free_free_stmt(stmts->fre.list[i]);
+       }
+       free(stmts->fre.list);
        free(stmts);
 }
 
@@ -602,7 +866,7 @@ typedef struct impl {
 } impl;
 
 static inline impl *init_impl(impl_func *func, impl_stmts *stmts) {
-       impl *i = malloc(sizeof(*i));
+       impl *i = calloc(1, sizeof(*i));
        i->func = func;
        i->stmts = stmts;
        return i;
@@ -619,7 +883,7 @@ typedef struct impls {
        impl **list;
 } impls;
 
-static impls *add_impl(impls *impls, impl *impl) {
+static inline impls *add_impl(impls *impls, impl *impl) {
        if (!impls) {
                impls = calloc(1, sizeof(*impls));
        }
@@ -638,43 +902,156 @@ static void free_impls(impls *impls) {
        free(impls);
 }
 
-typedef struct PSI_Data {
-       decl_typedefs *defs;
-       decls *decls;
-       impls *impls;
-       char *lib;
+typedef struct const_type {
+       token_t type;
+       char *name;
+} const_type;
+
+static inline const_type *init_const_type(token_t type, const char *name) {
+       const_type *ct = calloc(1, sizeof(*ct));
+       ct->type = type;
+       ct->name = strdup(name);
+       return ct;
+}
+
+static inline void free_const_type(const_type *type) {
+       free(type->name);
+       free(type);
+}
+
+typedef struct constant {
+       const_type *type;
+       char *name;
+       impl_def_val *val;
+} constant;
+
+static inline constant *init_constant(const_type *type, const char *name, impl_def_val *val) {
+       constant *c = calloc(1, sizeof(*c));
+       c->type = type;
+       c->name = strdup(name);
+       c->val = val;
+       return c;
+}
+
+static inline void free_constant(constant *constant) {
+       free_const_type(constant->type);
+       free(constant->name);
+       free_impl_def_val(constant->val);
+       free(constant);
+}
+
+typedef struct constants {
+       size_t count;
+       constant **list;
+} constants;
+
+static inline constants *add_constant(constants *constants, constant *constant) {
+       if (!constants) {
+               constants = calloc(1, sizeof(*constants));
+       }
+       constants->list = realloc(constants->list, ++constants->count * sizeof(*constants->list));
+       constants->list[constants->count-1] = constant;
+       return constants;
+}
+
+static inline void free_constants(constants *c) {
+       size_t i;
+
+       for (i = 0; i < c->count; ++i) {
+               free_constant(c->list[i]);
+       }
+       free(c->list);
+       free(c);
+}
+
+#define PSI_ERROR 16
+#define PSI_WARNING 32
+typedef void (*psi_error_cb)(int type, const char *msg, ...);
+
+typedef struct decl_file {
+       char *ln;
        char *fn;
+} decl_file;
+
+static inline void free_decl_file(decl_file *file) {
+       if (file->ln) {
+               free(file->ln);
+       }
+       if (file->fn) {
+               free(file->fn);
+       }
+       memset(file, 0, sizeof(*file));
+}
+
+typedef struct decl_libs {
+       void **dl;
+       size_t count;
+} decl_libs;
+
+static inline void free_decl_libs(decl_libs *libs) {
+       if (libs->dl) {
+               size_t i;
+               for (i = 0; i < libs->count; ++i) {
+                       if (libs->dl[i]) {
+                               dlclose(libs->dl[i]);
+                       }
+               }
+               free(libs->dl);
+       }
+       memset(libs, 0, sizeof(*libs));
+}
+
+static inline void add_decl_lib(decl_libs *libs, void *dlopened) {
+       libs->dl = realloc(libs->dl, ++libs->count * sizeof(*libs->dl));
+       libs->dl[libs->count-1] = dlopened;
+}
+
+#define PSI_DATA(D) ((PSI_Data *) (D))
+#define PSI_DATA_MEMBERS \
+       constants *consts; \
+       decl_typedefs *defs; \
+       decl_structs *structs; \
+       decls *decls; \
+       impls *impls; \
+       union { \
+               decl_file file; \
+               decl_libs libs; \
+       } psi; \
+       psi_error_cb error
+typedef struct PSI_Data {
+       PSI_DATA_MEMBERS;
 } PSI_Data;
 
-static inline void PSI_DataExchange(PSI_Data *dest, PSI_Data *src) {
+static inline PSI_Data *PSI_DataExchange(PSI_Data *dest, PSI_Data *src) {
+       if (!dest) {
+               dest = malloc(sizeof(*dest));
+       }
        memcpy(dest, src, sizeof(*dest));
        memset(src, 0, sizeof(*src));
+       return dest;
 }
 
 static inline void PSI_DataDtor(PSI_Data *data) {
+       if (data->consts) {
+               free_constants(data->consts);
+       }
        if (data->defs) {
                free_decl_typedefs(data->defs);
        }
+       if (data->structs) {
+               free_decl_structs(data->structs);
+       }
        if (data->decls) {
                free_decls(data->decls);
        }
        if (data->impls) {
                free_impls(data->impls);
        }
-       if (data->lib) {
-               free(data->lib);
-       }
-       if (data->fn) {
-               free(data->fn);
-       }
+       free_decl_file(&data->psi.file);
 }
 
 typedef struct PSI_Parser {
-       decl_typedefs *defs;
-       decls *decls;
-       impls *impls;
-       char *lib;
-       char *fn;
+       PSI_DATA_MEMBERS;
        FILE *fp;
        unsigned flags;
        unsigned errors;
@@ -688,13 +1065,13 @@ static inline PSI_Token *PSI_TokenAlloc(PSI_Parser *P) {
        PSI_Token *T;
        size_t token_len;
 
-       if (P->cur <= P->tok) {
+       if (P->cur < P->tok) {
                return NULL;
        }
 
        token_len = P->cur - P->tok;
 
-       T = malloc(sizeof(*T) + token_len);
+       T = calloc(1, sizeof(*T) + token_len);
        T->type = P->num;
        T->line = P->line;
        T->size = token_len;
@@ -706,7 +1083,7 @@ static inline PSI_Token *PSI_TokenAlloc(PSI_Parser *P) {
 
 #define PSI_PARSER_DEBUG 0x1
 
-PSI_Parser *PSI_ParserInit(PSI_Parser *P, const char *filename, unsigned flags);
+PSI_Parser *PSI_ParserInit(PSI_Parser *P, const char *filename, psi_error_cb error, unsigned flags);
 void PSI_ParserSyntaxError(PSI_Parser *P, const char *fn, size_t ln, const char *msg, ...);
 size_t PSI_ParserFill(PSI_Parser *P, size_t n);
 token_t PSI_ParserScan(PSI_Parser *P);