zval passthru
[m6w6/ext-psi] / src / context_validate.c
index 12befca821bf12da20baf7319a618622206961de..180b34d94c3e3b652f7e20e1184a4bbe13019020 100644 (file)
@@ -1,4 +1,4 @@
-       #ifdef HAVE_CONFIG_H
+#ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 
 # include "config.h"
 #endif
 
@@ -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"
@@ -111,6 +112,22 @@ static inline int locate_decl_type_enum(decl_enums *enums, decl_type *type) {
        return 0;
 }
 
        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_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);
@@ -143,6 +160,12 @@ static inline int validate_decl_type(PSI_Data *data, decl_type *type) {
                if (!locate_decl_type_enum(data->enums, type)) {
                        return 0;
                }
                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 1;
 }
@@ -154,6 +177,9 @@ static inline int validate_decl_typedef(PSI_Data *data, decl_arg *def) {
                        def->var->name);
                return 0;
        }
                        def->var->name);
                return 0;
        }
+       if (def->type->type == PSI_T_VOID && def->var->pointer_level) {
+               def->type->type = PSI_T_POINTER;
+       }
        return 1;
 }
 
        return 1;
 }
 
@@ -201,22 +227,31 @@ static inline void psi_sort_struct_args(void **args, size_t count) {
                        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_darg(PSI_Data *data, decl_arg *darg) {
+static inline int validate_decl_struct_darg(PSI_Data *data, decl_arg *darg, void *current) {
        decl_type *real = real_decl_type(darg->type);
 
        /* pre-validate any structs/unions/enums */
        switch (real->type) {
        case PSI_T_STRUCT:
        decl_type *real = real_decl_type(darg->type);
 
        /* 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 (!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 (!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;
                }
                if (!validate_decl_enum(data, real->enm)) {
                        return 0;
                }
@@ -253,30 +288,81 @@ static inline size_t sizeof_decl_arg(decl_arg *darg) {
                }
        }
 
                }
        }
 
+       ZEND_ASSERT(size);
+
        return size;
 }
 
        return size;
 }
 
-static inline size_t align_decl_arg(decl_arg *darg, size_t *pos, size_t *len) {
+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 {
        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 {
-               decl_type *real = real_decl_type(darg->type);
-
-               switch (real->type) {
-               case PSI_T_STRUCT:
-                       align = real->strct->align;
-                       break;
-               case PSI_T_UNION:
-                       align = real->unn->align;
-                       break;
-               default:
-                       align = psi_t_alignment(real->type);
-                       break;
-               }
+               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);
 
        *len = sizeof_decl_arg(darg);
        *pos = psi_align(align, *pos);
 
@@ -303,7 +389,9 @@ 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) {
+               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);
                        pos = darg->layout->pos;
 
                        align = align_decl_arg(darg, &pos, &len);
@@ -312,20 +400,16 @@ static inline int validate_decl_struct(PSI_Data *data, decl_struct *s) {
                                data->error(data, darg->token, PSI_WARNING,
                                                "Computed size %zu of %s.%s does not match"
                                                " pre-defined size %zu of type '%s'",
                                data->error(data, darg->token, PSI_WARNING,
                                                "Computed size %zu of %s.%s does not match"
                                                " pre-defined size %zu of type '%s'",
-                                               darg->layout->len, s->name, darg->var->name, len,
+                                               len, s->name, darg->var->name, darg->layout->len,
                                                darg->type->name);
                                                darg->type->name);
-                               return 0;
                        }
                        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",
                        }
                        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",
-                                               darg->layout->len, s->name, darg->var->name, len);
-                               return 0;
+                                               pos, s->name, darg->var->name, darg->layout->pos);
                        }
                        }
-               } else if (!validate_decl_struct_darg(data, darg)) {
-                       return 0;
-               } else {
+               } else  {
                        if (i) {
                                pos = s->args->args[i-1]->layout->pos +
                                                s->args->args[i-1]->layout->len;
                        if (i) {
                                pos = s->args->args[i-1]->layout->pos +
                                                s->args->args[i-1]->layout->len;
@@ -357,7 +441,7 @@ 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_union(PSI_Data *data, decl_union *u) {
-       size_t i, pos, len, size, align;
+       size_t i, pos, len, size = 0, align;
 
        if (!u->size && !u->args->count) {
                data->error(data, u->token, PSI_WARNING,
 
        if (!u->size && !u->args->count) {
                data->error(data, u->token, PSI_WARNING,
@@ -376,27 +460,26 @@ static inline int validate_decl_union(PSI_Data *data, decl_union *u) {
                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) {
+               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,
                        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 must be 0",
+                                               "Offset of %s.%s should be 0",
                                                u->name, darg->var->name);
                                                u->name, darg->var->name);
-                               return 0;
+                               darg->layout->pos = 0;
                        }
                        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'",
                        }
                        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'",
-                                               darg->layout->len, u->name, darg->var->name, size,
+                                               len, u->name, darg->var->name, darg->layout->len,
                                                darg->type->name);
                                                darg->type->name);
-                               return 0;
                        }
                        }
-               } else if (!validate_decl_struct_darg(data, darg)) {
-                       return 0;
                } else {
                        pos = 0;
 
                } else {
                        pos = 0;
 
@@ -448,9 +531,6 @@ static inline int validate_decl_func(PSI_Data *data, void *dl, decl *decl, decl_
                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;
@@ -469,14 +549,13 @@ static inline int validate_decl_func(PSI_Data *data, void *dl, decl *decl, decl_
        }
        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)) {
                data->error(data, decl->abi->token, PSI_WARNING,
                                "Invalid calling convention: '%s'", decl->abi->token->text);
                return 0;
        }
        if (!validate_decl_abi(data, decl->abi)) {
                data->error(data, decl->abi->token, PSI_WARNING,
                                "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) {
@@ -490,6 +569,15 @@ 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;
 
@@ -636,32 +724,18 @@ static inline int validate_decl_enum(PSI_Data *data, decl_enum *e) {
 
 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 */
@@ -706,7 +780,7 @@ 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);
 
                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);
 
@@ -722,23 +796,23 @@ static inline int validate_set_value_ex(PSI_Data *data, set_value *set, decl_arg
                }
        }
 
                }
        }
 
-       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) {
                decl_arg *sub_ref = locate_decl_var_arg(sub_var, ref_list, ref);
 
                if (sub_ref) {
@@ -746,11 +820,11 @@ static inline int validate_set_value_ex(PSI_Data *data, set_value *set, decl_arg
                                data->error(data, sub_var->token, E_WARNING, "Inner `set` statement casts on pointers must reference the same variable");
                                return 0;
                        }
                                data->error(data, sub_var->token, E_WARNING, "Inner `set` statement casts on pointers must reference the same variable");
                                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) {
+       } 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;
        }
                data->error(data, set->func->token, E_WARNING, "Inner `set` statement casts on pointers may only occur once");
                return 0;
        }
@@ -818,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;
 }
@@ -831,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;
@@ -874,22 +1015,13 @@ static inline int validate_impl_let_stmts(PSI_Data *data, impl *impl) {
                                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(data, 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;
@@ -1119,6 +1251,7 @@ int PSI_ContextValidate(PSI_Context *C, PSI_Parser *P)
        size_t i, count = C->count++, check_round, check_count;
        decl_typedefs *check_defs = P->defs;
        decl_structs *check_structs = P->structs;
        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;
 
        decl_enums *check_enums = P->enums;
        unsigned silent = C->flags & PSI_PARSER_SILENT;
 
@@ -1142,6 +1275,7 @@ int PSI_ContextValidate(PSI_Context *C, PSI_Parser *P)
        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;
        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;
 
                check_count = CHECK_TOTAL;
                decl_enums *recheck_enums = NULL;
 
                check_count = CHECK_TOTAL;
@@ -1160,6 +1294,13 @@ int PSI_ContextValidate(PSI_Context *C, PSI_Parser *P)
                                recheck_structs = add_decl_struct(recheck_structs, check_structs->list[i]);
                        }
                }
                                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]);
                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]);
@@ -1170,6 +1311,7 @@ int PSI_ContextValidate(PSI_Context *C, PSI_Parser *P)
 
                REVALIDATE(defs);
                REVALIDATE(structs);
 
                REVALIDATE(defs);
                REVALIDATE(structs);
+               REVALIDATE(unions);
                REVALIDATE(enums);
 
                if (check_round == 0 && !silent) {
                REVALIDATE(enums);
 
                if (check_round == 0 && !silent) {