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