raising the head after a three-weeks refactoring
[m6w6/ext-psi] / src / types / let_callback.c
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #include "php_psi_stdinc.h"
27 #include "data.h"
28
29 struct psi_let_callback *psi_let_callback_init(struct psi_let_func *func,
30 struct psi_plist *args)
31 {
32 struct psi_let_callback *cb = calloc(1, sizeof(*cb));
33 cb->func = func;
34 cb->args = args;
35 return cb;
36 }
37
38 void psi_let_callback_free(struct psi_let_callback **cb_ptr)
39 {
40 if (*cb_ptr) {
41 struct psi_let_callback *cb = *cb_ptr;
42
43 *cb_ptr = NULL;
44 psi_let_func_free(&cb->func);
45 psi_plist_free(cb->args);
46 if (cb->token) {
47 free(cb->token);
48 }
49 free(cb);
50 }
51 }
52
53 bool psi_let_callback_validate(struct psi_data *data, struct psi_let_exp *exp,
54 struct psi_let_callback *cb, struct psi_impl *impl)
55 {
56 size_t i = 0;
57 struct psi_decl *cb_func;
58 struct psi_decl_type *cb_type;
59 struct psi_decl_var *cb_var = exp->var;
60 struct psi_set_exp *set_exp;
61
62 cb_type = psi_decl_type_get_real(cb_var->arg->type);
63 if (cb_type->type != PSI_T_FUNCTION) {
64 data->error(data, cb_var->token, PSI_WARNING,
65 "Expected a function: %s",
66 cb_var->name);
67 return false;
68 }
69 cb_func = cb_type->real.func;
70
71 while (psi_plist_get(cb->args, i++, &set_exp)) {
72 if (!psi_set_exp_validate(data, set_exp, impl, cb_func)) {
73 return false;
74 }
75 }
76
77 if (!psi_decl_validate_nodl(data, cb_func)) {
78 return false;
79 }
80
81 cb->decl = cb_func;
82
83 return true;
84 }
85
86 void psi_let_callback_dump(int fd, struct psi_let_callback *callback,
87 unsigned level)
88 {
89 dprintf(fd, "callback %s(%s(",
90 callback->func->name,
91 callback->func->var->name);
92
93 if (callback->args) {
94 size_t i = 0, last = psi_plist_count(callback->args);
95 struct psi_set_exp *set;
96
97 dprintf(fd, "\n");
98 ++level;
99 while (psi_plist_get(callback->args, i++, &set)) {
100 psi_set_exp_dump(fd, set, level, i == last);
101 dprintf(fd, "\n");
102 }
103 --level;
104 dprintf(fd, "%s", psi_t_indent(level));
105 }
106 dprintf(fd, "))");
107 }