validation and marshaling of structs/unions
[m6w6/ext-psi] / src / context_dump.c
index efa99c6bbebf0ed08fdd7d11cddd546a17e9201b..b84f6c8f2c63371bec2cc680546f11a505f5eda4 100644 (file)
@@ -1,6 +1,12 @@
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include "php.h"
 #include "php_psi.h"
 
+#include "types.h"
+
 #include "libjit.h"
 #include "libffi.h"
 
@@ -16,34 +22,15 @@ static inline void dump_enum_items(int fd, decl_enum_items *items, unsigned leve
 #define is_anon_type(name, type) !strncmp(name, type "@", sizeof(type))
 
 static inline void dump_decl_type(int fd, decl_type *t, unsigned level) {
-       size_t j;
-
        switch (t->type) {
        case PSI_T_POINTER:
                dprintf(fd, "%s *", t->name);
                return;
 
-       case PSI_T_FUNCTION:
-               dump_decl_arg(fd, t->func->func, level);
-               dprintf(fd, "(");
-               if (t->func->args) {
-                       for (j = 0; j < t->func->args->count; ++j) {
-                               if (j) {
-                                       dprintf(fd, ", ");
-                               }
-                               dump_decl_arg(fd, t->func->args->args[j], level+1);
-                       }
-                       if (t->func->args->varargs) {
-                               dprintf(fd, ", ...");
-                       }
-               }
-               dprintf(fd, ")");
-               return;
-
        case PSI_T_STRUCT:
                dprintf(fd, "struct ");
                if (!strncmp(t->name, "struct@", sizeof("struct"))) {
-                       dump_struct_args(fd, t->strct->args, level);
+                       dump_struct_args(fd, t->real.strct->args, level);
                        return;
                }
                break;
@@ -51,7 +38,7 @@ static inline void dump_decl_type(int fd, decl_type *t, unsigned level) {
        case PSI_T_ENUM:
                dprintf(fd, "enum ");
                if (!strncmp(t->name, "enum@", sizeof("enum"))) {
-                       dump_enum_items(fd, t->enm->items, level);
+                       dump_enum_items(fd, t->real.enm->items, level);
                        return;
                }
                break;
@@ -59,7 +46,7 @@ static inline void dump_decl_type(int fd, decl_type *t, unsigned level) {
        case PSI_T_UNION:
                dprintf(fd, "union ");
                if (!strncmp(t->name, "union@", sizeof("union"))) {
-                       dump_struct_args(fd, t->unn->args, level);
+                       dump_struct_args(fd, t->real.unn->args, level);
                        return;
                }
                break;
@@ -75,9 +62,27 @@ static inline void dump_decl_var(int fd, decl_var *v) {
 }
 
 static inline void dump_decl_arg(int fd, decl_arg *a, unsigned level) {
-       dump_decl_type(fd, a->type, level);
+       if (a->type->type == PSI_T_FUNCTION) {
+               dump_decl_type(fd, a->type->real.func->func->type, level);
+               dprintf(fd, " (");
+               dump_decl_var(fd, a->var);
+               dprintf(fd, ")(");
+               if (a->type->real.func->args) {
+                       size_t j;
 
-       if (a->type->type != PSI_T_FUNCTION) {
+                       for (j = 0; j < a->type->real.func->args->count; ++j) {
+                               if (j) {
+                                       dprintf(fd, ", ");
+                               }
+                               dump_decl_arg(fd, a->type->real.func->args->args[j], level+1);
+                       }
+                       if (a->type->real.func->args->varargs) {
+                               dprintf(fd, ", ...");
+                       }
+               }
+               dprintf(fd, ")");
+       } else {
+               dump_decl_type(fd, a->type, level);
                dprintf(fd, " ");
                dump_decl_var(fd, a->var);
        }
@@ -367,53 +372,90 @@ static inline void dump_impl_func(int fd, impl_func *func) {
                        func->return_type->name);
 }
 
-static inline void dump_impl_let_stmt(int fd, let_stmt *let) {
-       dprintf(fd, "\tlet %s", let->var->name);
-       if (let->val) {
-               dprintf(fd, " = %s", let->val->flags.one.is_reference ? "&" : "");
-               switch (let->val->kind) {
-               case PSI_LET_NULL:
-                       dprintf(fd, "NULL");
-                       break;
-               case PSI_LET_TMP:
-                       dump_decl_var(fd, let->val->data.var);
-                       break;
-               case PSI_LET_CALLOC:
-                       dprintf(fd, "calloc(");
-                       dump_num_exp(fd, let->val->data.alloc->nmemb);
-                       dprintf(fd, ", ");
-                       dump_num_exp(fd, let->val->data.alloc->size);
-                       dprintf(fd, ")");
-                       break;
-               case PSI_LET_CALLBACK:
-                       dprintf(fd, "callback %s(%s(", let->val->data.callback->func->name,
-                                       let->val->data.callback->func->var->name);
-                       if (let->val->data.callback->args) {
-                               size_t i, c = let->val->data.callback->args->count;
-
-                               dprintf(fd, "\n");
-                               for (i = 0; i < c; ++i) {
-                                       set_value *set = let->val->data.callback->args->vals[i];
-                                       dump_impl_set_value(fd, set, 2, i + 1 == c);
-                               }
-                               dprintf(fd, "\t");
+static inline void dump_impl_let_func(int fd, let_func *func, unsigned level);
+
+static inline void dump_impl_let_val(int fd, let_val *val, unsigned level, int last) {
+       if (level > 1) {
+               /* only if not directly after `set ...` */
+               dump_level(fd, level);
+       }
+
+       dprintf(fd, "%s", val->flags.one.is_reference ? "&" : "");
+       switch (val->kind) {
+       case PSI_LET_NULL:
+               dprintf(fd, "NULL");
+               break;
+       case PSI_LET_TMP:
+               dump_decl_var(fd, val->data.var);
+               break;
+       case PSI_LET_CALLOC:
+               dprintf(fd, "calloc(");
+               dump_num_exp(fd, val->data.alloc->nmemb);
+               dprintf(fd, ", ");
+               dump_num_exp(fd, val->data.alloc->size);
+               dprintf(fd, ")");
+               break;
+       case PSI_LET_CALLBACK:
+               dprintf(fd, "callback %s(%s(", val->data.callback->func->name,
+                               val->data.callback->func->var->name);
+               if (val->data.callback->args) {
+                       size_t i, c = val->data.callback->args->count;
+
+                       dprintf(fd, "\n");
+                       for (i = 0; i < c; ++i) {
+                               set_value *set = val->data.callback->args->vals[i];
+                               ++level;
+                               dump_impl_set_value(fd, set, level, i + 1 == c);
+                               --level;
                        }
-                       dprintf(fd, "));");
-                       break;
-               case PSI_LET_FUNC:
-                       dprintf(fd, "%s(%s)", let->val->data.func->name,
-                                       let->val->data.func->var->name);
-                       break;
-               case PSI_LET_NUMEXP:
-                       dump_num_exp(fd, let->val->data.num);
-                       break;
+                       dump_level(fd, level);
+               }
+               dprintf(fd, "))");
+               break;
+       case PSI_LET_FUNC:
+               dump_impl_let_func(fd, val->data.func, level);
+               break;
+       case PSI_LET_NUMEXP:
+               dump_num_exp(fd, val->data.num);
+               break;
 
-               EMPTY_SWITCH_DEFAULT_CASE();
+       EMPTY_SWITCH_DEFAULT_CASE();
+       }
+       if (level > 1) {
+               if (!last) {
+                       dprintf(fd, ",");
                }
+       } else {
                dprintf(fd, ";");
        }
 }
 
+static inline void dump_impl_let_func(int fd, let_func *func, unsigned level) {
+       dprintf(fd, "%s(%s", func->name,
+                       func->var->name);
+       if (func->inner) {
+               size_t i;
+
+               dprintf(fd, ",");
+               ++level;
+               for (i = 0; i < func->inner->count; ++i) {
+                       dprintf(fd, "\n");
+                       dump_impl_let_val(fd, func->inner->vals[i], level, i+1 == func->inner->count);
+               }
+               --level;
+               dprintf(fd, "\n");
+               dump_level(fd, level);
+       }
+       dprintf(fd, ")");
+}
+static inline void dump_impl_let_stmt(int fd, let_stmt *let) {
+       dprintf(fd, "\tlet %s", let->var->name);
+       if (let->val) {
+               dprintf(fd, " = ");
+               dump_impl_let_val(fd, let->val, 1, 1);
+       }
+}
+
 static inline void dump_impl_return_stmt(int fd, return_stmt *ret) {
        dprintf(fd, "\treturn ");
        dump_impl_set_value(fd, ret->set, 1, 0);
@@ -498,15 +540,15 @@ static inline void dump_impls(int fd, impls *impls) {
        }
 }
 
-void PSI_ContextDump(PSI_Context *C, int fd)
+void psi_context_dump(struct psi_context *C, int fd)
 {
 #ifdef HAVE_LIBJIT
-       if (C->ops == PSI_Libjit()) {
+       if (C->ops == psi_libjit_ops()) {
                dprintf(fd, "// psi.engine=jit\n");
        }
 #endif
 #ifdef HAVE_LIBFFI
-       if (C->ops == PSI_Libffi()) {
+       if (C->ops == psi_libffi_ops()) {
                dprintf(fd, "// psi.engine=ffi\n");
        }
 #endif