raising the head after a three-weeks refactoring
[m6w6/ext-psi] / src / types / return_stmt.c
index 8d32fc82f1907a2a580f8dfcd8e3b1bbc72b0b40..5df84814355635e6e748f2c4ebf020fd54b51385 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"
+#include "call.h"
 
-return_stmt *init_return_stmt(set_value *val) {
-       return_stmt *ret = calloc(1, sizeof(*ret));
+struct psi_return_stmt *psi_return_stmt_init(struct psi_set_exp *val)
+{
+       struct psi_return_stmt *ret = calloc(1, sizeof(*ret));
        ret->set = val;
        return ret;
 }
 
-void free_return_stmt(return_stmt *ret) {
-       if (ret->token) {
-               free(ret->token);
-       }
-       free_set_value(ret->set);
-       free(ret);
+void psi_return_stmt_exec(struct psi_return_stmt *ret, zval *return_value,
+               struct psi_call_frame *frame)
+{
+       psi_set_exp_exec_ex(ret->set, return_value, frame->rpointer, frame);
 }
 
-void dump_return_stmt(int fd, return_stmt *ret) {
-       dprintf(fd, "\treturn ");
-       dump_set_value(fd, ret->set, 1, 0);
-}
-
-static inline decl *locate_impl_decl(decls *decls, return_stmt *ret) {
-       if (decls) {
-               size_t i;
+void psi_return_stmt_free(struct psi_return_stmt **ret_ptr)
+{
+       if (*ret_ptr) {
+               struct psi_return_stmt *ret = *ret_ptr;
 
-               for (i = 0; i < decls->count; ++i) {
-                       if (!strcmp(decls->list[i]->func->var->name, ret->set->vars->vars[0]->name)) {
-                               ret->decl = decls->list[i]->func;
-                               return decls->list[i];
-                       }
+               *ret_ptr = NULL;
+               if (ret->token) {
+                       free(ret->token);
                }
+               psi_set_exp_free(&ret->set);
+               free(ret);
        }
+}
 
-       return NULL;
+void psi_return_stmt_dump(int fd, struct psi_return_stmt *ret)
+{
+       dprintf(fd, "\treturn ");
+       psi_set_exp_dump(fd, ret->set, 1, 1);
+       dprintf(fd, ";\n");
 }
 
-int validate_return_stmt(struct psi_data *data, impl *impl) {
-       return_stmt *ret;
-
-       /* we must have exactly one ret stmt delcaring the native func to call */
-       /* and which type cast to apply */
-       if (impl->stmts->ret.count != 1) {
-               if (impl->stmts->ret.count > 1) {
-                       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 {
-                       data->error(data, impl->func->token, PSI_WARNING,
-                                       "Missing `return` statement for implementation %s",
-                                       impl->func->name);
-               }
-               return 0;
+bool psi_return_stmt_validate(struct psi_data *data, struct psi_impl *impl)
+{
+       size_t i = 0;
+       struct psi_decl *decl;
+       struct psi_return_stmt *ret;
+       size_t count = psi_plist_count(impl->stmts.ret);
+
+       psi_plist_get(impl->stmts.ret, 0, &ret);
+
+       /*
+        * we must have exactly one ret stmt declaring the native func to call
+        * and which type cast to apply
+        */
+       switch (count) {
+       default:
+               data->error(data, ret->token, PSI_WARNING,
+                               "Too many `return` statements for implementation %s;"
+                               " found %zu, exactly one is required",
+                               impl->func->name, count);
+               return false;
+       case 0:
+               data->error(data, impl->func->token, PSI_WARNING,
+                               "Missing `return` statement for implementation %s",
+                               impl->func->name);
+               return false;
+       case 1:
+               break;
        }
 
-       ret = impl->stmts->ret.list[0];
+       while (psi_plist_get(data->decls, i++, &decl)) {
+               if (!strcmp(decl->func->var->name, ret->set->data.func->var->name)) {
+                       impl->decl = decl;
+                       break;
+               }
+       }
 
-       if (!(impl->decl = locate_impl_decl(data->decls, ret))) {
+       if (!impl->decl) {
                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` statement of implementation %s",
+                               ret->set->data.func->var->name, impl->func->name);
+               return false;
        }
 
-       if (!validate_set_value(data, ret->set, 1, &ret->decl, impl->decl->args ? (int) impl->decl->args->count : 0, impl->decl->args ? impl->decl->args->args : NULL, 0)) {
-               return 0;
+       if (!psi_set_exp_validate(data, ret->set, impl, NULL)) {
+               return false;
        }
 
-       //impl->decl->impl = impl;
-
-       return 1;
+       return true;
 }