zval passthru
[m6w6/ext-psi] / src / context_validate.c
index da760c64afc19446994fa5ee395cbc78c538c20c..180b34d94c3e3b652f7e20e1184a4bbe13019020 100644 (file)
@@ -13,6 +13,7 @@
 #include "php_psi_macros.h"
 #include "php_psi_redirs.h"
 
 #include "php_psi_macros.h"
 #include "php_psi_redirs.h"
 
+#include "parser.h"
 #include "calc.h"
 #include "marshal.h"
 #include "engine.h"
 #include "calc.h"
 #include "marshal.h"
 #include "engine.h"
@@ -28,13 +29,13 @@ static int validate_lib(PSI_Data *data, void **dlopened) {
        } else if (!strchr(ptr, '/')) {
                len = snprintf(lib, MAXPATHLEN, "lib%s.%s", ptr, PHP_PSI_SHLIB_SUFFIX);
                if (MAXPATHLEN == len) {
        } else if (!strchr(ptr, '/')) {
                len = snprintf(lib, MAXPATHLEN, "lib%s.%s", ptr, PHP_PSI_SHLIB_SUFFIX);
                if (MAXPATHLEN == len) {
-                       data->error(NULL, PSI_WARNING, "Library name too long: '%s'", ptr);
+                       data->error(data, NULL, PSI_WARNING, "Library name too long: '%s'", ptr);
                }
                lib[len] = 0;
                ptr = lib;
        }
        if (!(*dlopened = dlopen(ptr, RTLD_LAZY|RTLD_LOCAL))) {
                }
                lib[len] = 0;
                ptr = lib;
        }
        if (!(*dlopened = dlopen(ptr, RTLD_LAZY|RTLD_LOCAL))) {
-               data->error(NULL, PSI_WARNING, "Could not open library '%s': %s.",
+               data->error(data, NULL, PSI_WARNING, "Could not open library '%s': %s.",
                                data->psi.file.ln, dlerror());
                return 0;
        }
                                data->psi.file.ln, dlerror());
                return 0;
        }
@@ -48,10 +49,10 @@ static inline int locate_decl_type_alias(decl_typedefs *defs, decl_type *type) {
        if (type->real) {
                return 1;
        }
        if (type->real) {
                return 1;
        }
-       for (i = 0; i < defs->count; ++i) {
-               decl_typedef *def = defs->list[i];
+       if (defs) for (i = 0; i < defs->count; ++i) {
+               decl_arg *def = defs->list[i];
 
 
-               if (def->type->type != type->type && !strcmp(def->alias, type->name)) {
+               if (def->type->type != type->type && !strcmp(def->var->name, type->name)) {
                        type->real = def->type;
                        return 1;
                }
                        type->real = def->type;
                        return 1;
                }
@@ -65,13 +66,14 @@ static inline int locate_decl_type_alias(decl_typedefs *defs, decl_type *type) {
 
        return 0;
 }
 
        return 0;
 }
+
 static inline int locate_decl_type_struct(decl_structs *structs, decl_type *type) {
        size_t i;
 
        if (type->strct) {
                return 1;
        }
 static inline int locate_decl_type_struct(decl_structs *structs, decl_type *type) {
        size_t i;
 
        if (type->strct) {
                return 1;
        }
-       for (i = 0; i < structs->count; ++i) {
+       if (structs) for (i = 0; i < structs->count; ++i) {
                if (!strcmp(structs->list[i]->name, type->name)) {
                        type->strct = structs->list[i];
                        return 1;
                if (!strcmp(structs->list[i]->name, type->name)) {
                        type->strct = structs->list[i];
                        return 1;
@@ -80,6 +82,56 @@ static inline int locate_decl_type_struct(decl_structs *structs, decl_type *type
        return 0;
 }
 
        return 0;
 }
 
+static inline int locate_decl_type_union(decl_unions *unions, decl_type *type) {
+       size_t i;
+
+       if (type->unn) {
+               return 1;
+       }
+       if (unions) for (i = 0; i < unions->count; ++i) {
+               if (!strcmp(unions->list[i]->name, type->name)) {
+                       type->unn = unions->list[i];
+                       return 1;
+               }
+       }
+       return 0;
+}
+
+static inline int locate_decl_type_enum(decl_enums *enums, decl_type *type) {
+       size_t i;
+
+       if (type->enm) {
+               return 1;
+       }
+       if (enums) for (i = 0; i < enums->count; ++i) {
+               if (!strcmp(enums->list[i]->name, type->name)) {
+                       type->enm = enums->list[i];
+                       return 1;
+               }
+       }
+       return 0;
+}
+
+static inline int locate_decl_type_decl(decls *decls, decl_type *type) {
+       size_t i;
+
+       if (type->func) {
+               return 1;
+       }
+       if (decls) for (i = 0; i < decls->count; ++i) {
+               if (!strcmp(decls->list[i]->func->var->name, type->name)) {
+                       type->func = decls->list[i];
+                       return 1;
+               }
+       }
+
+       return 0;
+}
+
+static inline int validate_decl_struct(PSI_Data *data, decl_struct *s);
+static inline int validate_decl_union(PSI_Data *data, decl_union *u);
+static inline int validate_decl_enum(PSI_Data *data, decl_enum *e);
+
 static inline int validate_decl_type(PSI_Data *data, decl_type *type) {
        switch (type->type) {
        case PSI_T_CHAR:
 static inline int validate_decl_type(PSI_Data *data, decl_type *type) {
        switch (type->type) {
        case PSI_T_CHAR:
@@ -87,7 +139,7 @@ static inline int validate_decl_type(PSI_Data *data, decl_type *type) {
        case PSI_T_INT:
        case PSI_T_LONG:
        case PSI_T_NAME:
        case PSI_T_INT:
        case PSI_T_LONG:
        case PSI_T_NAME:
-               if (!data->defs || !locate_decl_type_alias(data->defs, type)) {
+               if (!locate_decl_type_alias(data->defs, type)) {
                        return 0;
                }
                if (type->real) {
                        return 0;
                }
                if (type->real) {
@@ -95,21 +147,39 @@ static inline int validate_decl_type(PSI_Data *data, decl_type *type) {
                }
                return 1;
        case PSI_T_STRUCT:
                }
                return 1;
        case PSI_T_STRUCT:
-               if (!data->structs || !locate_decl_type_struct(data->structs, type)) {
+               if (!locate_decl_type_struct(data->structs, type)) {
+                       return 0;
+               }
+               break;
+       case PSI_T_UNION:
+               if (!locate_decl_type_union(data->unions, type)) {
+                       return 0;
+               }
+               break;
+       case PSI_T_ENUM:
+               if (!locate_decl_type_enum(data->enums, type)) {
+                       return 0;
+               }
+               break;
+       case PSI_T_FUNCTION:
+               if (!locate_decl_type_decl(data->decls, type)) {
                        return 0;
                }
                break;
        }
        return 1;
 }
                        return 0;
                }
                break;
        }
        return 1;
 }
-static inline int validate_decl_typedef(PSI_Data *data, decl_typedef *def) {
+static inline int validate_decl_typedef(PSI_Data *data, decl_arg *def) {
        if (!validate_decl_type(data, def->type)) {
        if (!validate_decl_type(data, def->type)) {
-               data->error(def->token, PSI_WARNING,
+               data->error(data, def->token, PSI_WARNING,
                        "Type '%s' cannot be aliased to %s'%s'",
                        "Type '%s' cannot be aliased to %s'%s'",
-                       def->type->name, def->type->type == PSI_T_STRUCT?"struct ":"",def->alias);
+                       def->type->name, def->type->type == PSI_T_STRUCT?"struct ":"",
+                       def->var->name);
                return 0;
        }
                return 0;
        }
-       /* FIXME: check def->alias */
+       if (def->type->type == PSI_T_VOID && def->var->pointer_level) {
+               def->type->type = PSI_T_POINTER;
+       }
        return 1;
 }
 
        return 1;
 }
 
@@ -120,9 +190,9 @@ static inline int validate_constant(PSI_Data *data, constant *c) {
 
 static inline int validate_decl_arg(PSI_Data *data, decl_arg *arg) {
        if (!validate_decl_type(data, arg->type)) {
 
 static inline int validate_decl_arg(PSI_Data *data, decl_arg *arg) {
        if (!validate_decl_type(data, arg->type)) {
-               data->error(arg->type->token, PSI_WARNING,
-                       "Cannot use '%s'(%d) as type for decl var '%s'",
-                       arg->type->name, arg->type->type, arg->var->name);
+               data->error(data, arg->type->token, PSI_WARNING,
+                       "Cannot use '%s' as type for '%s'",
+                       arg->type->name, arg->var->name);
                return 0;
        }
        return 1;
                return 0;
        }
        return 1;
@@ -152,18 +222,161 @@ static void psi_sort_struct_arg_swp(void *a, void *b) {
        *_b = *_a;
        *_a = _c;
 }
        *_b = *_a;
        *_a = _c;
 }
-static inline void psi_sort_struct_args(decl_struct *s) {
-       zend_insert_sort(s->args->args, s->args->count, sizeof(*s->args->args),
+static inline void psi_sort_struct_args(void **args, size_t count) {
+       zend_insert_sort(args, count, sizeof(*args),
                        psi_sort_struct_arg_cmp, psi_sort_struct_arg_swp);
 }
 
                        psi_sort_struct_arg_cmp, psi_sort_struct_arg_swp);
 }
 
-static inline int validate_decl_struct(PSI_Data *data, decl_struct *s) {
-       size_t i;
+static inline int validate_decl_struct_darg(PSI_Data *data, decl_arg *darg, void *current) {
+       decl_type *real = real_decl_type(darg->type);
 
 
-       for (i = 0; i < s->args->count; ++i) {
-               if (!validate_decl_arg(data, s->args->args[i])) {
+       /* pre-validate any structs/unions/enums */
+       switch (real->type) {
+       case PSI_T_STRUCT:
+               if (current && current == real->strct) {
+                       return 1;
+               }
+               if (!validate_decl_struct(data, real->strct)) {
+                       return 0;
+               }
+               break;
+       case PSI_T_UNION:
+               if (current && current == real->unn) {
+                       return 1;
+               }
+               if (!validate_decl_union(data, real->unn)) {
+                       return 0;
+               }
+               break;
+       case PSI_T_ENUM:
+               if (current && current == real->enm) {
+                       return 1;
+               }
+               if (!validate_decl_enum(data, real->enm)) {
                        return 0;
                }
                        return 0;
                }
+               break;
+       }
+
+       return 1;
+}
+
+static inline size_t sizeof_decl_arg(decl_arg *darg) {
+       size_t size;
+       decl_type *real = real_decl_type(darg->type);
+
+       if (darg->var->array_size) {
+               if (darg->var->pointer_level > 2) {
+                       size = psi_t_size(PSI_T_POINTER) * darg->var->array_size;
+               } else {
+                       size = psi_t_size(real->type) * darg->var->array_size;
+               }
+       } else if (darg->var->pointer_level) {
+               size = psi_t_size(PSI_T_POINTER);
+       } else {
+               switch (real->type) {
+               case PSI_T_UNION:
+                       size = real->unn->size;
+                       break;
+               case PSI_T_STRUCT:
+                       size = real->strct->size;
+                       break;
+               case PSI_T_ENUM:
+               default:
+                       size = psi_t_size(real->type);
+                       break;
+               }
+       }
+
+       ZEND_ASSERT(size);
+
+       return size;
+}
+
+static inline size_t alignof_decl_type(decl_type *t);
+static inline size_t alignof_decl_arg(decl_arg *darg);
+static inline size_t alignof_decl_union(decl_union *u);
+static inline size_t alignof_decl_struct(decl_struct *s);
+
+static inline size_t alignof_decl_args(decl_args *args) {
+       size_t i, maxalign = 0;
+
+       for (i = 0; i < args->count; ++i) {
+               decl_arg *darg = args->args[i];
+               size_t align = alignof_decl_arg(darg);
+
+               if (align > maxalign) {
+                       maxalign = align;
+               }
+       }
+
+       return maxalign;
+}
+
+static inline size_t alignof_decl_struct(decl_struct *s) {
+       if (!s->align) {
+               s->align = alignof_decl_args(s->args);
+       }
+       return s->align;
+}
+
+static inline size_t alignof_decl_union(decl_union *u) {
+       if (!u->align) {
+               u->align = alignof_decl_args(u->args);
+       }
+       return u->align;
+}
+
+static inline size_t alignof_decl_type(decl_type *t) {
+       decl_type *real = real_decl_type(t);
+       size_t align;
+
+       switch (real->type) {
+       case PSI_T_STRUCT:
+               align = alignof_decl_struct(real->strct);
+               break;
+       case PSI_T_UNION:
+               align = alignof_decl_union(real->unn);
+               break;
+       case PSI_T_ENUM:
+       default:
+               align = psi_t_alignment(real->type);
+       }
+
+       return align;
+}
+
+static inline size_t alignof_decl_arg(decl_arg *darg) {
+       size_t align;
+
+       if (darg->var->pointer_level && (!darg->var->array_size || darg->var->pointer_level > 2)) {
+               align = psi_t_alignment(PSI_T_POINTER);
+       } else {
+               align = alignof_decl_type(darg->type);
+       }
+
+       return align;
+}
+
+static inline size_t align_decl_arg(decl_arg *darg, size_t *pos, size_t *len) {
+       size_t align = alignof_decl_arg(darg);
+
+       ZEND_ASSERT(align);
+
+       *len = sizeof_decl_arg(darg);
+       *pos = psi_align(align, *pos);
+
+       return align;
+}
+
+static inline int validate_decl_struct(PSI_Data *data, decl_struct *s) {
+       size_t i, pos, len, size, align;
+
+       if (!s->size && !s->args->count) {
+               data->error(data, s->token, PSI_WARNING,
+                               "Cannot compute size of empty struct %s",
+                               s->name);
+               return 0;
        }
 
        for (i = 0; i < s->args->count; ++i) {
        }
 
        for (i = 0; i < s->args->count; ++i) {
@@ -176,54 +389,117 @@ static inline int validate_decl_struct(PSI_Data *data, decl_struct *s) {
                ZEND_ASSERT(!darg->var->arg || darg->var->arg == darg);
                darg->var->arg = darg;
 
                ZEND_ASSERT(!darg->var->arg || darg->var->arg == darg);
                darg->var->arg = darg;
 
-               if (darg->layout) {
-                       size_t size;
+               if (!validate_decl_struct_darg(data, darg, s)) {
+                       return 0;
+               } else if (darg->layout) {
+                       pos = darg->layout->pos;
+
+                       align = align_decl_arg(darg, &pos, &len);
 
 
-                       if (darg->var->array_size) {
-                               size = psi_t_size(real_decl_type(darg->type)->type) * darg->var->array_size;
-                       } else if (darg->var->pointer_level) {
-                               size = psi_t_size(PSI_T_POINTER);
+                       if (darg->layout->len != len) {
+                               data->error(data, darg->token, PSI_WARNING,
+                                               "Computed size %zu of %s.%s does not match"
+                                               " pre-defined size %zu of type '%s'",
+                                               len, s->name, darg->var->name, darg->layout->len,
+                                               darg->type->name);
+                       }
+                       if (darg->layout->pos != pos) {
+                               data->error(data, darg->token, PSI_WARNING,
+                                               "Computed offset %zu of %s.%s does not match"
+                                               " pre-defined offset %zu",
+                                               pos, s->name, darg->var->name, darg->layout->pos);
+                       }
+               } else  {
+                       if (i) {
+                               pos = s->args->args[i-1]->layout->pos +
+                                               s->args->args[i-1]->layout->len;
                        } else {
                        } else {
-                               decl_type *real = real_decl_type(darg->type);
+                               pos = 0;
+                       }
 
 
-                               if (real->type == PSI_T_STRUCT) {
-                                       size = real->strct->size;
-                               } else {
-                                       size = psi_t_size(real->type);
-                               }
+                       align = align_decl_arg(darg, &pos, &len);
+                       darg->layout = init_decl_struct_layout(pos, len);
+               }
+
+               if (align > s->align) {
+                       s->align = align;
+               }
+       }
+
+       psi_sort_struct_args((void **) s->args->args, s->args->count);
+
+       if (s->args->count) {
+               decl_arg *darg = s->args->args[s->args->count-1];
+
+               size = darg->layout->pos + darg->layout->len;
+               if (s->size < size) {
+                       s->size = psi_align(size, s->align);
+               }
+       }
+
+       return 1;
+}
+
+static inline int validate_decl_union(PSI_Data *data, decl_union *u) {
+       size_t i, pos, len, size = 0, align;
+
+       if (!u->size && !u->args->count) {
+               data->error(data, u->token, PSI_WARNING,
+                               "Cannot compute size of empty union %s",
+                               u->name);
+               return 0;
+       }
+
+       for (i = 0; i < u->args->count; ++i) {
+               decl_arg *darg = u->args->args[i];
+
+               if (!validate_decl_arg(data, darg)) {
+                       return 0;
+               }
+
+               ZEND_ASSERT(!darg->var->arg || darg->var->arg == darg);
+               darg->var->arg = darg;
+
+               if (!validate_decl_struct_darg(data, darg, u)) {
+                       return 0;
+               } else if (darg->layout) {
+                       pos = darg->layout->pos;
+
+                       align = align_decl_arg(darg, &pos, &len);
+
+                       if (darg->layout->pos != 0) {
+                               data->error(data, darg->token, PSI_WARNING,
+                                               "Offset of %s.%s should be 0",
+                                               u->name, darg->var->name);
+                               darg->layout->pos = 0;
                        }
                        }
-                       if (darg->layout->len != size) {
-                               data->error(darg->token, PSI_WARNING,
-                                               "Computed length %zu of %s.%s does not match"
-                                               " pre-defined length %zu of type '%s'",
-                                               darg->layout->len, s->name, darg->var->name, size,
+                       if (darg->layout->len != len) {
+                               data->error(data, darg->token, PSI_WARNING,
+                                               "Computed size %zu of %s.%s does not match"
+                                               " pre-defined size %zu of type '%s'",
+                                               len, u->name, darg->var->name, darg->layout->len,
                                                darg->type->name);
                                                darg->type->name);
-                               return 0;
                        }
                } else {
                        }
                } else {
-                       token_t t;
+                       pos = 0;
 
 
-                       if (darg->var->pointer_level && (!darg->var->array_size || darg->var->pointer_level == 1)) {
-                               t = PSI_T_POINTER;
-                       } else {
-                               t = real_decl_type(darg->type)->type;
-                       }
+                       align = align_decl_arg(darg, &pos, &len);
+                       darg->layout = init_decl_struct_layout(pos, len);
 
 
-                       if (i) {
-                               decl_arg *last = s->args->args[i-1];
-                               darg->layout = init_decl_struct_layout(
-                                               psi_t_align(t, last->layout->pos + last->layout->len),
-                                               psi_t_size(t) * darg->var->array_size);
-                       } else {
-                               darg->layout = init_decl_struct_layout(0, psi_t_size(t));
-                       }
                }
                }
-               if (s->size < darg->layout->pos + darg->layout->len) {
-                       s->size = darg->layout->pos + darg->layout->len;
+               if (len > size) {
+                       size = len;
+               }
+               if (align > u->align) {
+                       u->align = align;
                }
        }
 
                }
        }
 
-       psi_sort_struct_args(s);
+       psi_sort_struct_args((void **) u->args->args, u->args->count);
+
+       if (u->size < size) {
+               u->size = psi_align(size, u->align);
+       }
 
        return 1;
 }
 
        return 1;
 }
@@ -251,13 +527,10 @@ static inline int validate_decl_func(PSI_Data *data, void *dl, decl *decl, decl_
        struct psi_func_redir *redir;
 
        if (!strcmp(func->var->name, "dlsym")) {
        struct psi_func_redir *redir;
 
        if (!strcmp(func->var->name, "dlsym")) {
-               data->error(func->token, PSI_WARNING, "Cannot dlsym dlsym (sic!)");
+               data->error(data, func->token, PSI_WARNING, "Cannot dlsym dlsym (sic!)");
                return 0;
        }
 
                return 0;
        }
 
-       if (!validate_decl_arg(data, func)) {
-               return 0;
-       }
        for (redir = &psi_func_redirs[0]; redir->name; ++redir) {
                if (!strcmp(func->var->name, redir->name)) {
                        decl->call.sym = redir->func;
        for (redir = &psi_func_redirs[0]; redir->name; ++redir) {
                if (!strcmp(func->var->name, redir->name)) {
                        decl->call.sym = redir->func;
@@ -269,21 +542,20 @@ static inline int validate_decl_func(PSI_Data *data, void *dl, decl *decl, decl_
 #endif
                decl->call.sym = dlsym(dl ?: RTLD_NEXT, func->var->name);
                if (!decl->call.sym) {
 #endif
                decl->call.sym = dlsym(dl ?: RTLD_NEXT, func->var->name);
                if (!decl->call.sym) {
-                       data->error(func->token, PSI_WARNING,
+                       data->error(data, func->token, PSI_WARNING,
                                "Failed to locate symbol '%s': %s",
                                "Failed to locate symbol '%s': %s",
-                               func->var->name, dlerror());
+                               func->var->name, dlerror() ?: "not found");
                }
        }
        return 1;
 }
                }
        }
        return 1;
 }
-
-static inline int validate_decl(PSI_Data *data, void *dl, decl *decl) {
+static inline int validate_decl_nodl(PSI_Data *data, decl *decl) {
        if (!validate_decl_abi(data, decl->abi)) {
        if (!validate_decl_abi(data, decl->abi)) {
-               data->error(decl->abi->token, PSI_WARNING,
+               data->error(data, decl->abi->token, PSI_WARNING,
                                "Invalid calling convention: '%s'", decl->abi->token->text);
                return 0;
        }
                                "Invalid calling convention: '%s'", decl->abi->token->text);
                return 0;
        }
-       if (!validate_decl_func(data, dl, decl, decl->func)) {
+       if (!validate_decl_arg(data, decl->func)) {
                return 0;
        }
        if (decl->args) {
                return 0;
        }
        if (decl->args) {
@@ -297,10 +569,19 @@ static inline int validate_decl(PSI_Data *data, void *dl, decl *decl) {
        }
        return 1;
 }
        }
        return 1;
 }
+static inline int validate_decl(PSI_Data *data, void *dl, decl *decl) {
+       if (!validate_decl_nodl(data, decl)) {
+               return 0;
+       }
+       if (!validate_decl_func(data, dl, decl, decl->func)) {
+               return 0;
+       }
+       return 1;
+}
 static inline decl_arg *locate_decl_var_arg(decl_var *var, decl_args *args, decl_arg *func) {
        size_t i;
 
 static inline decl_arg *locate_decl_var_arg(decl_var *var, decl_args *args, decl_arg *func) {
        size_t i;
 
-       for (i = 0; i < args->count; ++i) {
+       if (args) for (i = 0; i < args->count; ++i) {
                decl_arg *arg = args->args[i];
 
                if (!strcmp(var->name, arg->var->name)) {
                decl_arg *arg = args->args[i];
 
                if (!strcmp(var->name, arg->var->name)) {
@@ -335,7 +616,35 @@ static inline constant *locate_num_exp_constant(num_exp *exp, constants *consts)
 
        return NULL;
 }
 
        return NULL;
 }
-static inline int validate_num_exp(PSI_Data *data, decl_args *dargs, decl_arg *func, num_exp *exp) {
+static inline decl_enum_item *locate_num_exp_enum_item_ex(num_exp *exp, decl_enum *e) {
+       size_t k;
+
+       if (e) for (k = 0; k < e->items->count; ++k) {
+               decl_enum_item *i = e->items->list[k];
+
+               if (!strcmp(i->name, exp->u.dvar->name)) {
+                       free_decl_var(exp->u.dvar);
+                       exp->t = PSI_T_ENUM;
+                       exp->u.enm = i;
+                       return i;
+               }
+       }
+       return NULL;
+}
+static inline decl_enum_item *locate_num_exp_enum_item(num_exp *exp, decl_enums *enums) {
+       size_t j;
+
+       if (enums) for (j = 0; j < enums->count; ++j) {
+               decl_enum *e = enums->list[j];
+               decl_enum_item *i = locate_num_exp_enum_item_ex(exp, e);
+
+               if (i) {
+                       return i;
+               }
+       }
+       return NULL;
+}
+static inline int validate_num_exp(PSI_Data *data, num_exp *exp, decl_args *dargs, decl_arg *func, decl_enum *enm) {
        if (exp->operand) {
                switch (exp->operator) {
                case PSI_T_PLUS:
        if (exp->operand) {
                switch (exp->operator) {
                case PSI_T_PLUS:
@@ -352,59 +661,81 @@ static inline int validate_num_exp(PSI_Data *data, decl_args *dargs, decl_arg *f
                        break;
                EMPTY_SWITCH_DEFAULT_CASE();
                }
                        break;
                EMPTY_SWITCH_DEFAULT_CASE();
                }
-               if (!validate_num_exp(data, dargs, func, exp->operand)) {
+               if (!validate_num_exp(data, exp->operand, dargs, func, enm)) {
                        return 0;
                }
        }
        switch (exp->t) {
        case PSI_T_NAME:
                if (!locate_decl_var_arg(exp->u.dvar, dargs, func)) {
                        return 0;
                }
        }
        switch (exp->t) {
        case PSI_T_NAME:
                if (!locate_decl_var_arg(exp->u.dvar, dargs, func)) {
-                       data->error(exp->token, PSI_WARNING, "Unknown variable '%s' in numeric expression",
-                                       exp->u.dvar->name);
-                       return 0;
+                       if (!locate_num_exp_enum_item(exp, data->enums) && !locate_num_exp_enum_item_ex(exp, enm)) {
+                               data->error(data, exp->token, PSI_WARNING, "Unknown variable '%s' in numeric expression",
+                                               exp->u.dvar->name);
+                               return 0;
+                       }
                }
                return 1;
        case PSI_T_NSNAME:
                if (!locate_num_exp_constant(exp, data->consts)) {
                }
                return 1;
        case PSI_T_NSNAME:
                if (!locate_num_exp_constant(exp, data->consts)) {
-                       data->error(exp->token, PSI_WARNING, "Unknown constant '%s' in numeric expression",
+                       data->error(data, exp->token, PSI_WARNING, "Unknown constant '%s' in numeric expression",
                                        exp->u.numb);
                        return 0;
                }
                return 1;
        case PSI_T_NUMBER:
                                        exp->u.numb);
                        return 0;
                }
                return 1;
        case PSI_T_NUMBER:
+       case PSI_T_ENUM:
                return 1;
        default:
                return 0;
        }
 }
                return 1;
        default:
                return 0;
        }
 }
+
+static inline int validate_decl_enum(PSI_Data *data, decl_enum *e) {
+       size_t j;
+
+       if (!e->items || !e->items->count) {
+               data->error(data, e->token, PSI_WARNING, "Empty enum '%s'", e->name);
+               return 0;
+       }
+
+       for (j = 0; j < e->items->count; ++j) {
+               decl_enum_item *i = e->items->list[j];
+
+               if (!i->num) {
+                       if (j) {
+                               i->inc.t = PSI_T_NUMBER;
+                               i->inc.u.numb = "1";
+                               i->inc.operator = PSI_T_PLUS;
+                               i->inc.operand = i->prev->num ?: &i->prev->inc;
+                               i->num = &i->inc;
+                       } else {
+                               i->inc.t = PSI_T_NUMBER;
+                               i->inc.u.numb = "0";
+                               i->num = &i->inc;
+                       }
+               }
+               if (!validate_num_exp(data, i->num, NULL, NULL, e)) {
+                       return 0;
+               }
+       }
+
+       return 1;
+}
+
 static inline int validate_set_value_handler(set_value *set) {
        switch (set->func->type) {
 static inline int validate_set_value_handler(set_value *set) {
        switch (set->func->type) {
-       case PSI_T_TO_BOOL:
-               set->func->handler = psi_to_bool;
-               break;
-       case PSI_T_TO_INT:
-               set->func->handler = psi_to_int;
-               break;
-       case PSI_T_TO_FLOAT:
-               set->func->handler = psi_to_double;
-               break;
-       case PSI_T_TO_STRING:
-               set->func->handler = psi_to_string;
-               break;
-       case PSI_T_TO_ARRAY:
-               set->func->handler = psi_to_array;
-               break;
-       case PSI_T_TO_OBJECT:
-               set->func->handler = psi_to_object;
-               break;
-       case PSI_T_VOID:
-               set->func->handler = psi_to_void;
-               break;
+       case PSI_T_TO_BOOL:             set->func->handler = psi_to_bool;               break;
+       case PSI_T_TO_INT:              set->func->handler = psi_to_int;                break;
+       case PSI_T_TO_FLOAT:    set->func->handler = psi_to_double;             break;
+       case PSI_T_TO_STRING:   set->func->handler = psi_to_string;             break;
+       case PSI_T_TO_ARRAY:    set->func->handler = psi_to_array;              break;
+       case PSI_T_TO_OBJECT:   set->func->handler = psi_to_object;             break;
+       case PSI_T_VOID:                set->func->handler = psi_to_void;               break;
+       case PSI_T_ZVAL:                set->func->handler = psi_to_zval;               break;
        case PSI_T_ELLIPSIS:
                if (set->outer.set && set->outer.set->func->type == PSI_T_TO_ARRAY) {
                        set->func->handler = psi_to_recursive;
                        set->inner = set->outer.set->inner;
        case PSI_T_ELLIPSIS:
                if (set->outer.set && set->outer.set->func->type == PSI_T_TO_ARRAY) {
                        set->func->handler = psi_to_recursive;
                        set->inner = set->outer.set->inner;
-                       set->count = set->outer.set->count;
                        break;
                }
                /* no break */
                        break;
                }
                /* no break */
@@ -432,14 +763,14 @@ static inline int validate_set_value_ex(PSI_Data *data, set_value *set, decl_arg
        decl_var *set_var = set->vars->vars[0];
 
        if (!validate_set_value_handler(set)) {
        decl_var *set_var = set->vars->vars[0];
 
        if (!validate_set_value_handler(set)) {
-               data->error(set->func->token, PSI_WARNING, "Invalid cast '%s' in `set` statement", set->func->name);
+               data->error(data, set->func->token, PSI_WARNING, "Invalid cast '%s' in `set` statement", set->func->name);
                return 0;
        }
 
        for (i = 0; i < set->vars->count; ++i) {
                decl_var *svar = set->vars->vars[i];
                if (!svar->arg && !locate_decl_var_arg(svar, ref_list, NULL)) {
                return 0;
        }
 
        for (i = 0; i < set->vars->count; ++i) {
                decl_var *svar = set->vars->vars[i];
                if (!svar->arg && !locate_decl_var_arg(svar, ref_list, NULL)) {
-                       data->error(svar->token, PSI_WARNING, "Unknown variable '%s' in `set` statement", svar->name);
+                       data->error(data, svar->token, PSI_WARNING, "Unknown variable '%s' in `set` statement", svar->name);
                        return 0;
                }
        }
                        return 0;
                }
        }
@@ -449,52 +780,52 @@ static inline int validate_set_value_ex(PSI_Data *data, set_value *set, decl_arg
        }
        ref_type = real_decl_type(ref->type);
 
        }
        ref_type = real_decl_type(ref->type);
 
-       if (set->count) {
+       if (set->inner && set->inner->count) {
                int is_to_array = (set->func->type == PSI_T_TO_ARRAY);
                int is_pointer_to_struct = (ref_type->type == PSI_T_STRUCT && ref->var->pointer_level);
 
                if (!is_to_array && !is_pointer_to_struct) {
                int is_to_array = (set->func->type == PSI_T_TO_ARRAY);
                int is_pointer_to_struct = (ref_type->type == PSI_T_STRUCT && ref->var->pointer_level);
 
                if (!is_to_array && !is_pointer_to_struct) {
-                       data->error(set->func->token, E_WARNING, "Inner `set` statement casts only work with "
+                       data->error(data, set->func->token, E_WARNING, "Inner `set` statement casts only work with "
                                        "to_array() casts on structs or pointers: %s(%s...", set->func->name, set->vars->vars[0]->name);
                        return 0;
                }
        }
        if (set->num) {
                                        "to_array() casts on structs or pointers: %s(%s...", set->func->name, set->vars->vars[0]->name);
                        return 0;
                }
        }
        if (set->num) {
-               if (!validate_num_exp(data, ref_list, ref, set->num)) {
+               if (!validate_num_exp(data, set->num, ref_list, ref, NULL)) {
                        return 0;
                }
        }
 
                        return 0;
                }
        }
 
-       if (ref_type->type == PSI_T_STRUCT) {
+       if (set->inner && ref_type->type == PSI_T_STRUCT) {
                /* to_array(struct, to_...) */
                /* to_array(struct, to_...) */
-               if (!set->outer.set || set->outer.set->inner != set->inner) {
-                       for (i = 0; i < set->count; ++i) {
-                               decl_var *sub_var = set->inner[i]->vars->vars[0];
+               if (!set->outer.set || set->outer.set->inner->vals != set->inner->vals) {
+                       for (i = 0; i < set->inner->count; ++i) {
+                               decl_var *sub_var = set->inner->vals[i]->vars->vars[0];
                                decl_arg *sub_ref = locate_struct_member(ref_type->strct, sub_var);
 
                                if (sub_ref) {
                                decl_arg *sub_ref = locate_struct_member(ref_type->strct, sub_var);
 
                                if (sub_ref) {
-                                       if (!validate_set_value_ex(data, set->inner[i], sub_ref, ref_type->strct->args)) {
+                                       if (!validate_set_value_ex(data, set->inner->vals[i], sub_ref, ref_type->strct->args)) {
                                                return 0;
                                        }
                                }
                        }
                }
                                                return 0;
                                        }
                                }
                        }
                }
-       } else if (set->count == 1) {
+       } else if (set->inner && set->inner->count == 1) {
                /* to_array(ptr, to_string(*ptr)) */
                /* to_array(ptr, to_string(*ptr)) */
-               decl_var *sub_var = set->inner[0]->vars->vars[0];
+               decl_var *sub_var = set->inner->vals[0]->vars->vars[0];
                decl_arg *sub_ref = locate_decl_var_arg(sub_var, ref_list, ref);
 
                if (sub_ref) {
                        if (strcmp(sub_var->name, set_var->name)) {
                decl_arg *sub_ref = locate_decl_var_arg(sub_var, ref_list, ref);
 
                if (sub_ref) {
                        if (strcmp(sub_var->name, set_var->name)) {
-                               data->error(sub_var->token, E_WARNING, "Inner `set` statement casts on pointers must reference the same variable");
+                               data->error(data, sub_var->token, E_WARNING, "Inner `set` statement casts on pointers must reference the same variable");
                                return 0;
                        }
                                return 0;
                        }
-                       if (!validate_set_value_ex(data, set->inner[0], sub_ref, ref_list)) {
+                       if (!validate_set_value_ex(data, set->inner->vals[0], sub_ref, ref_list)) {
                                return 0;
                        }
                }
                                return 0;
                        }
                }
-       } else if (set->count > 1) {
-               data->error(set->func->token, E_WARNING, "Inner `set` statement casts on pointers may only occur once");
+       } else if (set->inner && set->inner->count > 1) {
+               data->error(data, set->func->token, E_WARNING, "Inner `set` statement casts on pointers may only occur once");
                return 0;
        }
 
                return 0;
        }
 
@@ -536,12 +867,12 @@ static inline int validate_impl_ret_stmt(PSI_Data *data, impl *impl) {
        /* and which type cast to apply */
        if (impl->stmts->ret.count != 1) {
                if (impl->stmts->ret.count > 1) {
        /* and which type cast to apply */
        if (impl->stmts->ret.count != 1) {
                if (impl->stmts->ret.count > 1) {
-                       data->error(impl->stmts->ret.list[1]->token, PSI_WARNING,
+                       data->error(data, impl->stmts->ret.list[1]->token, PSI_WARNING,
                                        "Too many `return` statements for implmentation %s;"
                                        " found %zu, exactly one is needed",
                                        impl->func->name, impl->stmts->ret.count);
                } else {
                                        "Too many `return` statements for implmentation %s;"
                                        " found %zu, exactly one is needed",
                                        impl->func->name, impl->stmts->ret.count);
                } else {
-                       data->error(impl->func->token, PSI_WARNING,
+                       data->error(data, impl->func->token, PSI_WARNING,
                                        "Missing `return` statement for implementation %s",
                                        impl->func->name);
                }
                                        "Missing `return` statement for implementation %s",
                                        impl->func->name);
                }
@@ -551,7 +882,7 @@ static inline int validate_impl_ret_stmt(PSI_Data *data, impl *impl) {
        ret = impl->stmts->ret.list[0];
 
        if (!(impl->decl = locate_impl_decl(data->decls, ret))) {
        ret = impl->stmts->ret.list[0];
 
        if (!(impl->decl = locate_impl_decl(data->decls, ret))) {
-               data->error(ret->token, PSI_WARNING,
+               data->error(data, ret->token, PSI_WARNING,
                                "Missing declaration '%s' for `return` statment for implementation %s",
                                ret->set->vars->vars[0]->name, impl->func->name);
                return 0;
                                "Missing declaration '%s' for `return` statment for implementation %s",
                                ret->set->vars->vars[0]->name, impl->func->name);
                return 0;
@@ -561,7 +892,75 @@ static inline int validate_impl_ret_stmt(PSI_Data *data, impl *impl) {
                return 0;
        }
 
                return 0;
        }
 
-       impl->decl->impl = impl;
+       //impl->decl->impl = impl;
+
+       return 1;
+}
+
+static inline impl_arg *locate_impl_var_arg(impl_var *var, impl_args *args) {
+       size_t i;
+
+       for (i = 0; i < args->count; ++i) {
+               impl_arg *iarg = args->args[i];
+
+               if (!strcmp(var->name, iarg->var->name)) {
+                       return var->arg = iarg;
+               }
+       }
+
+       return NULL;
+}
+
+static inline int validate_let_func(PSI_Data *data, let_func *func, impl *impl) {
+       if (impl->func->args) {
+               if (!locate_impl_var_arg(func->var, impl->func->args)) {
+                       data->error(data, func->var->token, PSI_WARNING,
+                                       "Unknown variable '$%s' of `let` statement"
+                                       " for cast '%s' of implementation '%s'",
+                                       func->var->name, func->name, impl->func->name);
+                       return 0;
+               }
+       }
+       switch (func->type) {
+       case PSI_T_BOOLVAL:             func->handler = psi_let_boolval;        break;
+       case PSI_T_INTVAL:              func->handler = psi_let_intval;         break;
+       case PSI_T_FLOATVAL:    func->handler = psi_let_floatval;       break;
+       case PSI_T_STRVAL:              func->handler = psi_let_strval;         break;
+       case PSI_T_STRLEN:              func->handler = psi_let_strlen;         break;
+       case PSI_T_PATHVAL:             func->handler = psi_let_pathval;        break;
+       case PSI_T_ARRVAL:              func->handler = psi_let_arrval;         break;
+       case PSI_T_OBJVAL:              func->handler = psi_let_objval;         break;
+       case PSI_T_ZVAL:                func->handler = psi_let_zval;           break;
+       EMPTY_SWITCH_DEFAULT_CASE();
+       }
+       return 1;
+}
+
+static inline int validate_let_callback(PSI_Data *data, decl_var *cb_var, let_callback *cb, impl *impl) {
+       size_t i;
+       decl *cb_func;
+       decl_type *cb_type = real_decl_type(cb_var->arg->type);
+
+       if (!validate_let_func(data, cb->func, impl)) {
+               return 0;
+       }
+
+       if (cb_type->type != PSI_T_FUNCTION) {
+               data->error(data, cb_var->token, PSI_WARNING, "Not a function: %s", cb_var->name);
+               return 0;
+       }
+       cb_func = cb_type->func;
+       for (i = 0; i < cb->args->count; ++i) {
+               if (!validate_set_value(data, cb->args->vals[i], cb_func->args->count, cb_func->args->args, 0)) {
+                       return 0;
+               }
+       }
+
+       if (!validate_decl_nodl(data, cb_func)) {
+               return 0;
+       }
+
+       cb->decl = cb_func;
 
        return 1;
 }
 
        return 1;
 }
@@ -574,7 +973,6 @@ static inline int validate_impl_let_stmts(PSI_Data *data, impl *impl) {
        for (i = 0; i < impl->stmts->let.count; ++i) {
                let_stmt *let = impl->stmts->let.list[i];
                decl_var *let_var;
        for (i = 0; i < impl->stmts->let.count; ++i) {
                let_stmt *let = impl->stmts->let.list[i];
                decl_var *let_var;
-               int check = 0;
 
                if (let->val && let->val->kind == PSI_LET_TMP) {
                        let_var = let->val->data.var;
 
                if (let->val && let->val->kind == PSI_LET_TMP) {
                        let_var = let->val->data.var;
@@ -583,7 +981,7 @@ static inline int validate_impl_let_stmts(PSI_Data *data, impl *impl) {
                }
 
                if (!locate_decl_var_arg(let_var, impl->decl->args, impl->decl->func)) {
                }
 
                if (!locate_decl_var_arg(let_var, impl->decl->args, impl->decl->func)) {
-                       data->error(let_var->token, PSI_WARNING, "Unknown variable '%s' in `let` statement"
+                       data->error(data, let_var->token, PSI_WARNING, "Unknown variable '%s' in `let` statement"
                                        " of implementation '%s'", let_var->name, impl->func->name);
                        return 0;
                }
                                        " of implementation '%s'", let_var->name, impl->func->name);
                        return 0;
                }
@@ -605,34 +1003,25 @@ static inline int validate_impl_let_stmts(PSI_Data *data, impl *impl) {
                                                        let->var->array_size));
                        break;
                case PSI_LET_NUMEXP:
                                                        let->var->array_size));
                        break;
                case PSI_LET_NUMEXP:
-                       if (!validate_num_exp(data, impl->decl->args, impl->decl->func, let->val->data.num)) {
+                       if (!validate_num_exp(data, let->val->data.num, impl->decl->args, impl->decl->func, NULL)) {
                                return 0;
                        }
                        break;
                case PSI_LET_CALLOC:
                                return 0;
                        }
                        break;
                case PSI_LET_CALLOC:
-                       if (!validate_num_exp(data, impl->decl->args, impl->decl->func, let->val->data.alloc->nmemb)) {
+                       if (!validate_num_exp(data, let->val->data.alloc->nmemb, impl->decl->args, impl->decl->func, NULL)) {
                                return 0;
                        }
                                return 0;
                        }
-                       if (!validate_num_exp(data, impl->decl->args, impl->decl->func, let->val->data.alloc->size)) {
+                       if (!validate_num_exp(data, let->val->data.alloc->size, impl->decl->args, impl->decl->func, NULL)) {
                                return 0;
                        }
                        break;
                                return 0;
                        }
                        break;
-               case PSI_LET_FUNC:
-                       if (impl->func->args) {
-                               for (j = 0; j < impl->func->args->count; ++j) {
-                                       impl_arg *iarg = impl->func->args->args[j];
-
-                                       if (!strcmp(let->val->data.func->var->name, iarg->var->name)) {
-                                               let->val->data.func->arg = iarg;
-                                               check = 1;
-                                               break;
-                                       }
-                               }
+               case PSI_LET_CALLBACK:
+                       if (!validate_let_callback(data, let->var, let->val->data.callback, impl)) {
+                               return 0;
                        }
                        }
-                       if (!check) {
-                               data->error(let->var->token, PSI_WARNING, "Unknown value '$%s' of `let` statement"
-                                               " for variable '%s' of implementation '%s'",
-                                               let->val->data.func->var->name, let->var->name, impl->func->name);
+                       break;
+               case PSI_LET_FUNC:
+                       if (!validate_let_func(data, let->val->data.func, impl)) {
                                return 0;
                        }
                        break;
                                return 0;
                        }
                        break;
@@ -654,7 +1043,7 @@ static inline int validate_impl_let_stmts(PSI_Data *data, impl *impl) {
                        }
                }
                if (!check) {
                        }
                }
                if (!check) {
-                       data->error(impl->func->token, PSI_WARNING,
+                       data->error(data, impl->func->token, PSI_WARNING,
                                        "Missing `let` statement for arg '%s %.*s%s'"
                                        " of declaration '%s' for implementation '%s'",
                                        darg->type->name, (int) darg->var->pointer_level, "*****",
                                        "Missing `let` statement for arg '%s %.*s%s'"
                                        " of declaration '%s' for implementation '%s'",
                                        darg->type->name, (int) darg->var->pointer_level, "*****",
@@ -683,7 +1072,7 @@ static inline int validate_impl_set_stmts(PSI_Data *data, impl *impl) {
                        }
                }
                if (!check) {
                        }
                }
                if (!check) {
-                       data->error(set->var->token, PSI_WARNING, "Unknown variable '$%s' of `set` statement"
+                       data->error(data, set->var->token, PSI_WARNING, "Unknown variable '$%s' of `set` statement"
                                        " of implementation '%s'",
                                        set->var->name, impl->func->name);
                        return 0;
                                        " of implementation '%s'",
                                        set->var->name, impl->func->name);
                        return 0;
@@ -726,7 +1115,7 @@ static inline int validate_impl_set_stmts(PSI_Data *data, impl *impl) {
                        }
 
                        if (!check) {
                        }
 
                        if (!check) {
-                               data->error(set_var->token, PSI_WARNING, "Unknown value '%s' of `set` statement"
+                               data->error(data, set_var->token, PSI_WARNING, "Unknown value '%s' of `set` statement"
                                                " for variable '$%s' of implementation '%s'",
                                                set_var->name, set->arg->var->name, impl->func->name);
                                return 0;
                                                " for variable '$%s' of implementation '%s'",
                                                set_var->name, set->arg->var->name, impl->func->name);
                                return 0;
@@ -760,7 +1149,7 @@ static inline int validate_impl_free_stmts(PSI_Data *data, impl *impl) {
 
                        /* first find the decl of the free func */
                        if (!locate_free_decl(data->decls, free_call)) {
 
                        /* first find the decl of the free func */
                        if (!locate_free_decl(data->decls, free_call)) {
-                               data->error(free_call->token, PSI_WARNING,
+                               data->error(data, free_call->token, PSI_WARNING,
                                                "Missing declaration '%s' in `free` statement"
                                                " of implementation '%s'",
                                                free_call->func, impl->func->name);
                                                "Missing declaration '%s' in `free` statement"
                                                " of implementation '%s'",
                                                free_call->func, impl->func->name);
@@ -790,7 +1179,7 @@ static inline int validate_impl_free_stmts(PSI_Data *data, impl *impl) {
                                }
 
                                if (!check) {
                                }
 
                                if (!check) {
-                                       data->error(free_var->token, PSI_WARNING,
+                                       data->error(data, free_var->token, PSI_WARNING,
                                                        "Unknown variable '%s' of `free` statement"
                                                        " of implementation '%s'",
                                                        free_var->name, impl->func->name);
                                                        "Unknown variable '%s' of `free` statement"
                                                        " of implementation '%s'",
                                                        free_var->name, impl->func->name);
@@ -803,7 +1192,7 @@ static inline int validate_impl_free_stmts(PSI_Data *data, impl *impl) {
 }
 static inline int validate_impl_stmts(PSI_Data *data, impl *impl) {
        if (!impl->stmts) {
 }
 static inline int validate_impl_stmts(PSI_Data *data, impl *impl) {
        if (!impl->stmts) {
-               data->error(impl->func->token, PSI_WARNING,
+               data->error(data, impl->func->token, PSI_WARNING,
                                "Missing body for implementation %s!",
                                impl->func->name);
                return 0;
                                "Missing body for implementation %s!",
                                impl->func->name);
                return 0;
@@ -836,7 +1225,7 @@ static inline int validate_impl_args(PSI_Data *data, impl *impl) {
                if (iarg->def) {
                        def = 1;
                } else if (def) {
                if (iarg->def) {
                        def = 1;
                } else if (def) {
-                       data->error(impl->func->token, PSI_WARNING,
+                       data->error(data, impl->func->token, PSI_WARNING,
                                        "Non-optional argument %zu '$%s' of implementation '%s'"
                                        " follows optional argument",
                                        i+1, iarg->var->name, impl->func->name);
                                        "Non-optional argument %zu '$%s' of implementation '%s'"
                                        " follows optional argument",
                                        i+1, iarg->var->name, impl->func->name);
@@ -846,6 +1235,7 @@ static inline int validate_impl_args(PSI_Data *data, impl *impl) {
 
        return 1;
 }
 
        return 1;
 }
+
 static inline int validate_impl(PSI_Data *data, impl *impl) {
        if (!validate_impl_args(data, impl)) {
                return 0;
 static inline int validate_impl(PSI_Data *data, impl *impl) {
        if (!validate_impl_args(data, impl)) {
                return 0;
@@ -858,32 +1248,79 @@ int PSI_ContextValidate(PSI_Context *C, PSI_Parser *P)
 {
        PSI_Data *D;
        void *dlopened = NULL;
 {
        PSI_Data *D;
        void *dlopened = NULL;
-       size_t count = C->count++;
+       size_t i, count = C->count++, check_round, check_count;
+       decl_typedefs *check_defs = P->defs;
+       decl_structs *check_structs = P->structs;
+       decl_unions *check_unions = P->unions;
+       decl_enums *check_enums = P->enums;
+       unsigned silent = C->flags & PSI_PARSER_SILENT;
 
        C->data = realloc(C->data, C->count * sizeof(*C->data));
        D = PSI_DataExchange(&C->data[count], PSI_DATA(P));
 
 
        C->data = realloc(C->data, C->count * sizeof(*C->data));
        D = PSI_DataExchange(&C->data[count], PSI_DATA(P));
 
-       if (D->defs) {
-               size_t i;
+#define REVALIDATE(what) do { \
+               if (check_round && check_ ##what) { \
+                       free(check_ ##what->list); \
+                       free(check_ ##what); \
+               } \
+               check_ ##what = recheck_ ##what; \
+} while (0)
+#define CHECK_TOTAL (CHECK_COUNT(defs) + CHECK_COUNT(structs) + CHECK_COUNT(enums))
+#define CHECK_COUNT(of) (check_ ##of ? check_ ##of->count : 0)
+
+       if (!silent) {
+               /* no warnings on first round */
+               C->flags |= PSI_PARSER_SILENT;
+       }
+       for (check_round = 0, check_count = 0; CHECK_TOTAL && check_count != CHECK_TOTAL; ++check_round) {
+               decl_typedefs *recheck_defs = NULL;
+               decl_structs *recheck_structs = NULL;
+               decl_unions *recheck_unions = NULL;
+               decl_enums *recheck_enums = NULL;
 
 
-               for (i = 0; i < D->defs->count; ++i) {
-                       if (validate_decl_typedef(PSI_DATA(C), D->defs->list[i])) {
-                               C->defs = add_decl_typedef(C->defs, D->defs->list[i]);
+               check_count = CHECK_TOTAL;
+
+               for (i = 0; i < CHECK_COUNT(defs); ++i) {
+                       if (validate_decl_typedef(PSI_DATA(C), check_defs->list[i])) {
+                               C->defs = add_decl_typedef(C->defs, check_defs->list[i]);
+                       } else {
+                               recheck_defs = add_decl_typedef(recheck_defs, check_defs->list[i]);
                        }
                }
                        }
                }
-       }
-       if (D->structs) {
-               size_t i;
-
-               for (i = 0; i < D->structs->count; ++i) {
-                       if (validate_decl_struct(PSI_DATA(C), D->structs->list[i])) {
-                               C->structs = add_decl_struct(C->structs, D->structs->list[i]);
+               for (i = 0; i < CHECK_COUNT(structs); ++i) {
+                       if (validate_decl_struct(PSI_DATA(C), check_structs->list[i])) {
+                               C->structs = add_decl_struct(C->structs, check_structs->list[i]);
+                       } else {
+                               recheck_structs = add_decl_struct(recheck_structs, check_structs->list[i]);
+                       }
+               }
+               for (i = 0; i < CHECK_COUNT(unions); ++i) {
+                       if (validate_decl_union(PSI_DATA(C), check_unions->list[i])) {
+                               C->unions = add_decl_union(C->unions, check_unions->list[i]);
+                       } else {
+                               recheck_unions = add_decl_union(recheck_unions, check_unions->list[i]);
+                       }
+               }
+               for (i = 0; i < CHECK_COUNT(enums); ++i) {
+                       if (validate_decl_enum(PSI_DATA(C), check_enums->list[i])) {
+                               C->enums = add_decl_enum(C->enums, check_enums->list[i]);
+                       } else {
+                               recheck_enums = add_decl_enum(recheck_enums, check_enums->list[i]);
                        }
                }
                        }
                }
+
+               REVALIDATE(defs);
+               REVALIDATE(structs);
+               REVALIDATE(unions);
+               REVALIDATE(enums);
+
+               if (check_round == 0 && !silent) {
+                       C->flags &= ~PSI_PARSER_SILENT;
+               }
        }
        }
-       if (D->consts) {
-               size_t i;
 
 
+
+       if (D->consts) {
                for (i = 0; i < D->consts->count; ++i) {
                        if (validate_constant(PSI_DATA(C), D->consts->list[i])) {
                                C->consts = add_constant(C->consts, D->consts->list[i]);
                for (i = 0; i < D->consts->count; ++i) {
                        if (validate_constant(PSI_DATA(C), D->consts->list[i])) {
                                C->consts = add_constant(C->consts, D->consts->list[i]);
@@ -898,8 +1335,6 @@ int PSI_ContextValidate(PSI_Context *C, PSI_Parser *P)
        add_decl_lib(&C->psi.libs, dlopened);
 
        if (D->decls) {
        add_decl_lib(&C->psi.libs, dlopened);
 
        if (D->decls) {
-               size_t i;
-
                for (i = 0; i < D->decls->count; ++i) {
                        if (validate_decl(PSI_DATA(C), dlopened, D->decls->list[i])) {
                                C->decls = add_decl(C->decls, D->decls->list[i]);
                for (i = 0; i < D->decls->count; ++i) {
                        if (validate_decl(PSI_DATA(C), dlopened, D->decls->list[i])) {
                                C->decls = add_decl(C->decls, D->decls->list[i]);
@@ -907,8 +1342,6 @@ int PSI_ContextValidate(PSI_Context *C, PSI_Parser *P)
                }
        }
        if (D->impls) {
                }
        }
        if (D->impls) {
-               size_t i;
-
                for (i = 0; i < D->impls->count; ++i) {
                        if (validate_impl(PSI_DATA(C), D->impls->list[i])) {
                                C->impls = add_impl(C->impls, D->impls->list[i]);
                for (i = 0; i < D->impls->count; ++i) {
                        if (validate_impl(PSI_DATA(C), D->impls->list[i])) {
                                C->impls = add_impl(C->impls, D->impls->list[i]);
@@ -919,39 +1352,94 @@ int PSI_ContextValidate(PSI_Context *C, PSI_Parser *P)
        return 1;
 }
 
        return 1;
 }
 
-void PSI_ContextValidatePredef(PSI_Context *C, PSI_Data *D)
+int PSI_ContextValidateData(PSI_Data *dest, PSI_Data *source)
 {
        size_t i;
 {
        size_t i;
+       int errors = 0;
 
 
-       for (i = 0; i < D->defs->count; ++i) {
-               decl_typedef *def = D->defs->list[i];
+       if (source->defs) for (i = 0; i < source->defs->count; ++i) {
+               decl_arg *def = source->defs->list[i];
 
 
-               if (validate_decl_typedef(D, def)) {
-                       C->defs = add_decl_typedef(C->defs, def);
+               if (validate_decl_typedef(source, def)) {
+                       if (dest) {
+                               dest->defs = add_decl_typedef(dest->defs, def);
+                       }
+               } else {
+                       ++errors;
                }
        }
 
                }
        }
 
-       for (i = 0; i < D->consts->count; ++i) {
-               constant *constant = D->consts->list[i];
+       if (source->consts) for (i = 0; i < source->consts->count; ++i) {
+               constant *constant = source->consts->list[i];
 
 
-               if (validate_constant(D, constant)) {
-                       C->consts = add_constant(C->consts, constant);
+               if (validate_constant(source, constant)) {
+                       if (dest) {
+                               dest->consts = add_constant(dest->consts, constant);
+                       }
+               } else {
+                       ++errors;
                }
        }
 
                }
        }
 
-       for (i = 0; i < D->structs->count; ++i) {
-               decl_struct *dstruct = D->structs->list[i];
+       if (source->structs) for (i = 0; i < source->structs->count; ++i) {
+               decl_struct *dstruct = source->structs->list[i];
 
 
-               if (validate_decl_struct(D, dstruct)) {
-                       C->structs = add_decl_struct(C->structs, dstruct);
+               if (validate_decl_struct(source, dstruct)) {
+                       if (dest) {
+                               dest->structs = add_decl_struct(dest->structs, dstruct);
+                       }
+               } else {
+                       ++errors;
                }
        }
 
                }
        }
 
-       for (i = 0; i < D->decls->count; ++i) {
-               decl *decl = D->decls->list[i];
+       if (source->unions) for (i = 0; i < source->unions->count; ++i) {
+               decl_union *dunion = source->unions->list[i];
 
 
-               if (validate_decl(D, NULL, decl)) {
-                       C->decls = add_decl(C->decls, decl);
+               if (validate_decl_union(source, dunion)) {
+                       if (dest) {
+                               dest->unions = add_decl_union(dest->unions, dunion);
+                       }
+               } else {
+                       ++errors;
                }
        }
                }
        }
+
+       if (source->enums) for (i = 0; i < source->enums->count; ++i) {
+               decl_enum *denum = source->enums->list[i];
+
+               if (validate_decl_enum(source, denum)) {
+                       if (dest) {
+                               dest->enums = add_decl_enum(dest->enums, denum);
+                       }
+               } else {
+                       ++errors;
+               }
+       }
+
+       if (source->decls) for (i = 0; i < source->decls->count; ++i) {
+               decl *decl = source->decls->list[i];
+
+               if (validate_decl(source, NULL, decl)) {
+                       if (dest) {
+                               dest->decls = add_decl(dest->decls, decl);
+                       }
+               } else {
+                       ++errors;
+               }
+       }
+
+       if (source->impls) for (i = 0; i < source->impls->count; ++i) {
+               impl *impl = source->impls->list[i];
+
+               if (validate_impl(source, impl)) {
+                       if (dest) {
+                               dest->impls = add_impl(dest->impls, impl);
+                       }
+               } else {
+                       ++errors;
+               }
+       }
+
+       return errors;
 }
 }