Merge branch 'slimconfigure'
[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, struct psi_plist *cb_args)
31 {
32 struct psi_let_callback *cb = calloc(1, sizeof(*cb));
33 cb->func = func;
34 cb->args = args;
35 cb->cb_args = cb_args;
36 return cb;
37 }
38
39 void psi_let_callback_free(struct psi_let_callback **cb_ptr)
40 {
41 if (*cb_ptr) {
42 struct psi_let_callback *cb = *cb_ptr;
43
44 *cb_ptr = NULL;
45 psi_let_func_free(&cb->func);
46 psi_plist_free(cb->args);
47 if (cb->cb_args) {
48 psi_plist_free(cb->cb_args);
49 }
50 if (cb->token) {
51 free(cb->token);
52 }
53 free(cb);
54 }
55 }
56
57
58 static inline bool psi_let_callback_validate_decl_args(struct psi_data *data,
59 struct psi_let_callback *cb, struct psi_decl *cb_decl,
60 struct psi_impl *impl)
61 {
62 size_t i;
63 struct psi_decl_var *call_arg;
64
65 if (cb->cb_args) {
66 if (psi_plist_count(cb->cb_args) != psi_plist_count(cb_decl->args)) {
67 data->error(data, cb->token, PSI_WARNING,
68 "Argument count of callback statement of implementation '%s'"
69 "does not match argument count of callback declaration '%s'",
70 impl->func->name, cb_decl->func->var->name);
71 return false;
72 }
73
74 for (i = 0; psi_plist_get(cb->cb_args, i, &call_arg); ++i) {
75 psi_plist_get(cb_decl->args, i, &call_arg->arg);
76 }
77 }
78 return true;
79 }
80
81 bool psi_let_callback_validate(struct psi_data *data, struct psi_let_callback *cb,
82 struct psi_validate_scope *scope)
83 {
84 size_t i = 0;
85 struct psi_decl_type *cb_type;
86 struct psi_let_exp *exp = scope->current_let;
87 struct psi_decl_var *cb_var = exp->var;
88 struct psi_set_exp *set_exp, *parent_set = scope->current_set;
89
90 cb_type = psi_decl_type_get_real(cb_var->arg->type);
91 if (cb_type->type != PSI_T_FUNCTION) {
92 data->error(data, cb_var->token, PSI_WARNING,
93 "Expected a function: %s",
94 cb_var->name);
95 return false;
96 }
97 cb->decl = cb_type->real.func;
98
99 if (!psi_decl_validate_nodl(data, cb->decl, scope)) {
100 return false;
101 }
102 if (!psi_let_callback_validate_decl_args(data, cb, cb->decl, scope->impl)) {
103 return false;
104 }
105 while (psi_plist_get(cb->args, i++, &set_exp)) {
106 if (cb->cb_args) {
107 struct psi_decl_var *cb_var;
108
109 if (psi_plist_get(cb->cb_args, i - 1, &cb_var)) {
110 struct psi_decl_var *dvar = psi_set_exp_get_decl_var(set_exp);
111 dvar->arg = cb_var->arg;
112 }
113 }
114
115 scope->current_set = set_exp;
116 scope->cb_decl = cb->decl;
117
118 if (!psi_set_exp_validate(data, set_exp, scope)) {
119 scope->cb_decl = NULL;
120 scope->current_set = parent_set;
121 return false;
122 }
123 scope->cb_decl = NULL;
124 scope->current_set = parent_set;
125 }
126
127 return true;
128 }
129
130 void psi_let_callback_dump(int fd, struct psi_let_callback *callback,
131 unsigned level)
132 {
133 dprintf(fd, "callback(");
134 if (callback->cb_args) {
135 size_t i = 0;
136 struct psi_decl_var *cb_arg;
137
138 while (psi_plist_get(callback->cb_args, i++, &cb_arg)) {
139 if (i > 1) {
140 dprintf(fd, ", ");
141 }
142 psi_decl_var_dump(fd, cb_arg);
143 }
144 }
145 dprintf(fd, ") as %s(%s(",
146 callback->func->name,
147 callback->func->var->name);
148
149 if (callback->args) {
150 size_t i = 0, last = psi_plist_count(callback->args);
151 struct psi_set_exp *set;
152
153 dprintf(fd, "\n");
154 ++level;
155 while (psi_plist_get(callback->args, i++, &set)) {
156 psi_set_exp_dump(fd, set, level, i == last);
157 dprintf(fd, "\n");
158 }
159 --level;
160 dprintf(fd, "%s", psi_t_indent(level));
161 }
162 dprintf(fd, "))");
163 }