parser: LET foo = CALLBACK[(var_list spec) AS ] LET_CALLBACK
[m6w6/ext-psi] / src / types / set_func.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_set_func *psi_set_func_init(token_t type, const char *name,
30 struct psi_decl_var *var)
31 {
32 struct psi_set_func *func = calloc(1, sizeof(*func));
33
34 func->type = type;
35 func->name = strdup(name);
36 func->var = var;
37
38 return func;
39 }
40
41 void psi_set_func_free(struct psi_set_func **func_ptr)
42 {
43 if (*func_ptr) {
44 struct psi_set_func *func = *func_ptr;
45
46 *func_ptr = NULL;
47 if (func->token) {
48 free(func->token);
49 }
50 psi_decl_var_free(&func->var);
51 free(func->name);
52 free(func);
53 }
54 }
55
56 void psi_set_func_dump(int fd, struct psi_set_func *func, unsigned level)
57 {
58 dprintf(fd, "%s(", func->name);
59 psi_decl_var_dump(fd, func->var);
60 dprintf(fd, "\t/* fqn=%s */", func->var->fqn);
61 if (func->inner && !func->recursive) {
62 size_t i = 0, count = psi_plist_count(func->inner);
63 struct psi_set_exp *inner;
64
65 dprintf(fd, ",");
66 ++level;
67 while (psi_plist_get(func->inner, i++, &inner)) {
68 dprintf(fd, "\n");
69 psi_set_exp_dump(fd, inner, level, i == count);
70 }
71 --level;
72 }
73 if (func->recursive) {
74 dprintf(fd, ", ...");
75 }
76 if (func->inner && !func->recursive) {
77 dprintf(fd, "\n%s", psi_t_indent(level));
78 }
79 dprintf(fd, ")");
80 }
81
82 static inline bool psi_set_func_validate_to_string(struct psi_data *data,
83 struct psi_set_func *func, struct psi_set_exp *set,
84 struct psi_impl *impl)
85 {
86 struct psi_set_exp *sub_exp;
87
88 psi_plist_get(func->inner, 0, &sub_exp);
89
90 switch (psi_plist_count(func->inner)) {
91 default:
92 data->error(data, **(struct psi_token ***) &sub_exp->data, PSI_WARNING,
93 "Unexpected sub-expressions");
94 return false;
95
96 case 1:
97 if (sub_exp->kind != PSI_SET_NUMEXP) {
98 data->error(data, **(struct psi_token ***) &sub_exp->data,
99 PSI_WARNING, "Expected numeric expression");
100 return false;
101 }
102 func->handler = psi_set_to_stringl;
103 return true;
104
105 case 0:
106 func->handler = psi_set_to_string;
107 return true;
108 }
109 }
110
111 static inline bool psi_set_func_validate_to_array(struct psi_data *data,
112 struct psi_set_func *func, struct psi_set_exp *set,
113 struct psi_impl *impl)
114 {
115 struct psi_set_exp *sub_exp;
116 struct psi_decl_var *set_var = psi_set_exp_get_decl_var(set);
117
118 switch (psi_plist_count(set->inner)) {
119 case 0:
120 if (func->recursive) {
121 func->handler = psi_set_to_recursive;
122 set->inner = func->inner = set->outer->inner;
123 return true;
124 }
125 data->error(data, func->token, PSI_WARNING,
126 "Expected sub-expressions in to_array() cast expression");
127 return false;
128
129 case 1:
130 psi_plist_get(set->inner, 0, &sub_exp);
131 if (sub_exp->kind != PSI_SET_FUNC) {
132 data->error(data, **(struct psi_token ***) &sub_exp->data,
133 PSI_WARNING, "Expected to_type() cast expression");
134 return false;
135 }
136 if (strcmp(set_var->name, psi_set_exp_get_decl_var(sub_exp)->name)) {
137 /* no warning, because of ambiguity with a one-field-struct monstrosity */
138 goto complex;
139 }
140 func->handler = psi_set_to_array_simple;
141 return true;
142
143 case 2:
144 psi_plist_get(set->inner, 0, &sub_exp);
145 if (sub_exp->kind != PSI_SET_NUMEXP) {
146 goto complex;
147 }
148 psi_plist_get(set->inner, 1, &sub_exp);
149 if (sub_exp->kind != PSI_SET_FUNC) {
150 data->error(data, **(struct psi_token ***) &sub_exp->data,
151 PSI_WARNING, "Expected to_type() cast expression");
152 return false;
153 }
154 if (strcmp(set_var->name, psi_set_exp_get_decl_var(sub_exp)->name)) {
155 data->error(data, **(struct psi_token ***) &sub_exp->data,
156 PSI_WARNING,
157 "Expected %s(%s) cast expression to reference the same"
158 " variable like the outer `set` statement, '%s'",
159 sub_exp->data.func->name,
160 psi_set_exp_get_decl_var(sub_exp)->name, set_var);
161 return false;
162 }
163 func->handler = psi_set_to_array_counted;
164 return true;
165
166 default:
167 complex:
168 switch (psi_decl_type_get_real(set_var->arg->type)->type) {
169 case PSI_T_UNION:
170 case PSI_T_STRUCT:
171 break;
172 default:
173 data->error(data, func->token, PSI_WARNING,
174 "Expected struct or union type for complex to_array()"
175 " cast expression, got '%s'",
176 set_var->arg->type->name);
177 return false;
178 }
179 func->handler = psi_set_to_array;
180 return true;
181 }
182 }
183
184 static inline bool psi_set_func_validate_to_recursive(struct psi_data *data,
185 struct psi_set_func *func, struct psi_set_exp *set,
186 struct psi_impl *impl)
187 {
188 if (!set->outer
189 || set->outer->kind != PSI_SET_FUNC
190 || set->outer->data.func->type != PSI_T_TO_ARRAY) {
191 data->error(data, func->token, PSI_WARNING,
192 "Expected to_array() as parent to recursion in `set` statement"
193 " of implementation '%s'",
194 impl->func->name);
195 return false;
196 }
197
198 func->handler = psi_set_to_recursive;
199 set->inner = set->outer->inner;
200 // FIXME validate recursive type
201 return true;
202 }
203
204 bool psi_set_func_validate(struct psi_data *data, struct psi_set_func *func,
205 struct psi_set_exp *set, struct psi_impl *impl, struct psi_decl *cb_decl)
206 {
207 if (!func->var->arg
208 && !psi_decl_var_validate(data, func->var, impl, impl->decl, NULL, set)
209 && !psi_decl_var_validate(data, func->var, NULL, cb_decl, NULL, NULL)
210 && !psi_impl_get_temp_let_arg(impl, func->var)) {
211 data->error(data, func->var->token, PSI_WARNING,
212 "Unknown variable '%s' in implementation %s",
213 func->var->name, impl->func->name);
214 return false;
215 }
216
217 if (set->outer) {
218 if (set->outer->kind != PSI_SET_FUNC) {
219 data->error(data, func->token, PSI_WARNING,
220 "Unexpected sub-expression, expected to_type() cast");
221 return false;
222 }
223 }
224
225 if (func->inner && (!set->outer || set->outer->inner != func->inner)) {
226
227 size_t i = 0;
228 struct psi_set_exp *inner;
229
230 while (psi_plist_get(func->inner, i++, &inner)) {
231 struct psi_decl_var *sub_var;
232 struct psi_plist *sub_args;
233
234 inner->outer = set;
235
236 sub_var = psi_set_exp_get_decl_var(inner);
237 sub_args = psi_decl_type_get_args(func->var->arg->type, NULL);
238 if (sub_var && sub_args && !psi_decl_arg_get_by_var(sub_var, sub_args, NULL)) {
239 /* remove expr for portability with different struct members */
240 psi_plist_del(func->inner, --i, NULL);
241 psi_set_exp_free(&inner);
242 } else if (!psi_set_exp_validate(data, inner, impl, cb_decl)) {
243 /* remove exp for portability */
244 psi_plist_del(func->inner, --i, NULL);
245 }
246 }
247 }
248
249 switch (func->type) {
250 case PSI_T_VOID:
251 func->handler = psi_set_void;
252 break;
253 case PSI_T_ZVAL:
254 func->handler = psi_set_zval;
255 break;
256 case PSI_T_TO_BOOL:
257 func->handler = psi_set_to_bool;
258 break;
259 case PSI_T_TO_INT:
260 func->handler = psi_set_to_int;
261 break;
262 case PSI_T_TO_FLOAT:
263 func->handler = psi_set_to_float;
264 break;
265 case PSI_T_TO_OBJECT:
266 func->handler = psi_set_to_object;
267 break;
268 case PSI_T_TO_STRING:
269 if (!psi_set_func_validate_to_string(data, func, set, impl)) {
270 return false;
271 }
272 break;
273 case PSI_T_TO_ARRAY:
274 if (!psi_set_func_validate_to_array(data, func, set, impl)) {
275 return false;
276 }
277 break;
278 default:
279 data->error(data, func->token, PSI_WARNING,
280 "Unknown cast '%s' in `set` statement of implementation '%s'",
281 func->name, impl->func->name);
282 return false;
283 }
284
285 return true;
286 }