update lemon
[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: switch (psi_decl_type_get_real(set_var->arg->type)->type) {
168 case PSI_T_UNION:
169 case PSI_T_STRUCT:
170 break;
171 default:
172 data->error(data, func->token, PSI_WARNING,
173 "Expected struct or union type for complex to_array()"
174 " cast expression, got '%s'",
175 set_var->arg->type->name);
176 return false;
177 }
178 func->handler = psi_set_to_array;
179 return true;
180 }
181 }
182
183 static inline bool psi_set_func_validate_to_recursive(struct psi_data *data,
184 struct psi_set_func *func, struct psi_set_exp *set,
185 struct psi_impl *impl)
186 {
187 if (!set->outer
188 || set->outer->kind
189 != PSI_SET_FUNC|| set->outer->data.func->type != PSI_T_TO_ARRAY) {
190 data->error(data, func->token, PSI_WARNING,
191 "Expected to_array() as parent to recursion in `set` statement"
192 " of implementation '%s'",
193 impl->func->name);
194 return false;
195 }
196
197 func->handler = psi_set_to_recursive;
198 set->inner = set->outer->inner;
199 // FIXME validate recursive type
200 return true;
201 }
202
203 bool psi_set_func_validate(struct psi_data *data, struct psi_set_func *func,
204 struct psi_set_exp *set, struct psi_impl *impl, struct psi_decl *cb_decl)
205 {
206 if (!psi_decl_var_validate(data, func->var, impl->decl, NULL, set)
207 && !psi_decl_var_validate(data, func->var, cb_decl, NULL, NULL)
208 && !psi_impl_get_temp_let_arg(impl, func->var)) {
209 data->error(data, func->var->token, PSI_WARNING,
210 "Unknown variable '%s' in implementation %s",
211 func->var->name, impl->func->name);
212 return false;
213 }
214
215 if (set->outer) {
216 if (set->outer->kind != PSI_SET_FUNC) {
217 data->error(data, func->token, PSI_WARNING,
218 "Unexpected sub-expression, expected to_type() cast");
219 return false;
220 }
221 }
222
223 if (func->inner && (!set->outer || set->outer->inner != func->inner)) {
224
225 size_t i = 0;
226 struct psi_set_exp *inner;
227
228 while (psi_plist_get(func->inner, i++, &inner)) {
229 struct psi_decl_var *sub_var;
230 struct psi_plist *sub_args;
231
232 inner->outer = set;
233
234 sub_var = psi_set_exp_get_decl_var(inner);
235 sub_args = psi_decl_type_get_args(func->var->arg->type, NULL);
236 if (sub_var && sub_args && !psi_decl_arg_get_by_var(sub_var, sub_args, NULL)) {
237 /* remove expr for portability with different struct members */
238 psi_plist_del(func->inner, --i, NULL);
239 psi_set_exp_free(&inner);
240 } else if (!psi_set_exp_validate(data, inner, impl, cb_decl)) {
241 /* remove exp for portability */
242 psi_plist_del(func->inner, --i, NULL);
243 }
244 }
245 }
246
247 switch (func->type) {
248 case PSI_T_VOID:
249 func->handler = psi_set_void;
250 break;
251 case PSI_T_ZVAL:
252 func->handler = psi_set_zval;
253 break;
254 case PSI_T_TO_BOOL:
255 func->handler = psi_set_to_bool;
256 break;
257 case PSI_T_TO_INT:
258 func->handler = psi_set_to_int;
259 break;
260 case PSI_T_TO_FLOAT:
261 func->handler = psi_set_to_float;
262 break;
263 case PSI_T_TO_OBJECT:
264 func->handler = psi_set_to_object;
265 break;
266 case PSI_T_TO_STRING:
267 if (!psi_set_func_validate_to_string(data, func, set, impl)) {
268 return false;
269 }
270 break;
271 case PSI_T_TO_ARRAY:
272 if (!psi_set_func_validate_to_array(data, func, set, impl)) {
273 return false;
274 }
275 break;
276 case PSI_T_ELLIPSIS:
277 abort();
278 if (!psi_set_func_validate_to_recursive(data, func, set, impl)) {
279 return false;
280 }
281 break;
282 default:
283 data->error(data, func->token, PSI_WARNING,
284 "Unknown cast '%s' in `set` statement of implementation '%s'",
285 func->name, impl->func->name);
286 return false;
287 }
288
289 return true;
290 }