Merge branch 'slimconfigure'
[m6w6/ext-psi] / src / types / let_callback.c
index 4c5809ca1cc80709ed95343616b63a1818581ebd..45639f82449dbded4f8c76252f6a796915ea9e9d 100644 (file)
@@ -5,11 +5,11 @@
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
 
    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
 
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*******************************************************************************/
-
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#else
-# include "php_config.h"
-#endif
-
-#include <stdlib.h>
-#include <stdio.h>
+ *******************************************************************************/
 
+#include "php_psi_stdinc.h"
 #include "data.h"
 
-let_callback *init_let_callback(struct let_func *func, struct set_values *args) {
-       let_callback *cb = calloc(1, sizeof(*cb));
+struct psi_let_callback *psi_let_callback_init(struct psi_let_func *func,
+               struct psi_plist *args, struct psi_plist *cb_args)
+{
+       struct psi_let_callback *cb = calloc(1, sizeof(*cb));
        cb->func = func;
        cb->args = args;
+       cb->cb_args = cb_args;
        return cb;
 }
 
-void free_let_callback(let_callback *cb) {
-       free_let_func(cb->func);
-       free_set_values(cb->args);
-       free(cb);
+void psi_let_callback_free(struct psi_let_callback **cb_ptr)
+{
+       if (*cb_ptr) {
+               struct psi_let_callback *cb = *cb_ptr;
+
+               *cb_ptr = NULL;
+               psi_let_func_free(&cb->func);
+               psi_plist_free(cb->args);
+               if (cb->cb_args) {
+                       psi_plist_free(cb->cb_args);
+               }
+               if (cb->token) {
+                       free(cb->token);
+               }
+               free(cb);
+       }
 }
 
-int validate_let_callback(struct psi_data *data, decl_var *cb_var, let_callback *cb, impl *impl) {
+
+static inline bool psi_let_callback_validate_decl_args(struct psi_data *data,
+               struct psi_let_callback *cb, struct psi_decl *cb_decl,
+               struct psi_impl *impl)
+{
        size_t i;
-       decl *cb_func;
-       decl_type *cb_type = real_decl_type(cb_var->arg->type);
+       struct psi_decl_var *call_arg;
 
-       if (!validate_let_func(data, cb->func, cb_var, impl)) {
-               return 0;
+       if (cb->cb_args) {
+               if (psi_plist_count(cb->cb_args) != psi_plist_count(cb_decl->args)) {
+                       data->error(data, cb->token, PSI_WARNING,
+                                       "Argument count of callback statement of implementation '%s'"
+                                       "does not match argument count of callback declaration '%s'",
+                                       impl->func->name, cb_decl->func->var->name);
+                       return false;
+               }
+
+               for (i = 0; psi_plist_get(cb->cb_args, i, &call_arg); ++i) {
+                       psi_plist_get(cb_decl->args, i, &call_arg->arg);
+               }
        }
+       return true;
+}
+
+bool psi_let_callback_validate(struct psi_data *data, struct psi_let_callback *cb,
+               struct psi_validate_scope *scope)
+{
+       size_t i = 0;
+       struct psi_decl_type *cb_type;
+       struct psi_let_exp *exp = scope->current_let;
+       struct psi_decl_var *cb_var = exp->var;
+       struct psi_set_exp *set_exp, *parent_set = scope->current_set;
 
+       cb_type = psi_decl_type_get_real(cb_var->arg->type);
        if (cb_type->type != PSI_T_FUNCTION) {
-               data->error(data, cb_var->token, PSI_WARNING, "Not a function: %s", cb_var->name);
-               return 0;
+               data->error(data, cb_var->token, PSI_WARNING,
+                               "Expected a function: %s",
+                               cb_var->name);
+               return false;
        }
-       cb_func = cb_type->real.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;
+       cb->decl = cb_type->real.func;
+
+       if (!psi_decl_validate_nodl(data, cb->decl, scope)) {
+               return false;
+       }
+       if (!psi_let_callback_validate_decl_args(data, cb, cb->decl, scope->impl)) {
+               return false;
+       }
+       while (psi_plist_get(cb->args, i++, &set_exp)) {
+               if (cb->cb_args) {
+                       struct psi_decl_var *cb_var;
+
+                       if (psi_plist_get(cb->cb_args, i - 1, &cb_var)) {
+                               struct psi_decl_var *dvar = psi_set_exp_get_decl_var(set_exp);
+                               dvar->arg = cb_var->arg;
+                       }
+               }
+
+               scope->current_set = set_exp;
+               scope->cb_decl = cb->decl;
+
+               if (!psi_set_exp_validate(data, set_exp, scope)) {
+                       scope->cb_decl = NULL;
+                       scope->current_set = parent_set;
+                       return false;
                }
+               scope->cb_decl = NULL;
+               scope->current_set = parent_set;
        }
 
-       if (!validate_decl_nodl(data, cb_func)) {
-               return 0;
+       return true;
+}
+
+void psi_let_callback_dump(int fd, struct psi_let_callback *callback,
+               unsigned level)
+{
+       dprintf(fd, "callback(");
+       if (callback->cb_args) {
+               size_t i = 0;
+               struct psi_decl_var *cb_arg;
+
+               while (psi_plist_get(callback->cb_args, i++, &cb_arg)) {
+                       if (i > 1) {
+                               dprintf(fd, ", ");
+                       }
+                       psi_decl_var_dump(fd, cb_arg);
+               }
        }
+       dprintf(fd, ") as %s(%s(",
+                       callback->func->name,
+                       callback->func->var->name);
 
-       cb->decl = cb_func;
+       if (callback->args) {
+               size_t i = 0, last = psi_plist_count(callback->args);
+               struct psi_set_exp *set;
 
-       return 1;
+               dprintf(fd, "\n");
+               ++level;
+               while (psi_plist_get(callback->args, i++, &set)) {
+                       psi_set_exp_dump(fd, set, level, i == last);
+                       dprintf(fd, "\n");
+               }
+               --level;
+               dprintf(fd, "%s", psi_t_indent(level));
+       }
+       dprintf(fd, "))");
 }