decl_var: fix fqn
[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 (!psi_decl_var_validate(data, func->var, NULL, impl->decl, NULL, set)
208 && !psi_decl_var_validate(data, func->var, NULL, cb_decl, NULL, NULL)
209 && !psi_impl_get_temp_let_arg(impl, func->var)) {
210 data->error(data, func->var->token, PSI_WARNING,
211 "Unknown variable '%s' in implementation %s",
212 func->var->name, impl->func->name);
213 return false;
214 }
215
216 if (set->outer) {
217 if (set->outer->kind != PSI_SET_FUNC) {
218 data->error(data, func->token, PSI_WARNING,
219 "Unexpected sub-expression, expected to_type() cast");
220 return false;
221 }
222 }
223
224 if (func->inner && (!set->outer || set->outer->inner != func->inner)) {
225
226 size_t i = 0;
227 struct psi_set_exp *inner;
228
229 while (psi_plist_get(func->inner, i++, &inner)) {
230 struct psi_decl_var *sub_var;
231 struct psi_plist *sub_args;
232
233 inner->outer = set;
234
235 sub_var = psi_set_exp_get_decl_var(inner);
236 sub_args = psi_decl_type_get_args(func->var->arg->type, NULL);
237 if (sub_var && sub_args && !psi_decl_arg_get_by_var(sub_var, sub_args, NULL)) {
238 /* remove expr for portability with different struct members */
239 psi_plist_del(func->inner, --i, NULL);
240 psi_set_exp_free(&inner);
241 } else if (!psi_set_exp_validate(data, inner, impl, cb_decl)) {
242 /* remove exp for portability */
243 psi_plist_del(func->inner, --i, NULL);
244 }
245 }
246 }
247
248 switch (func->type) {
249 case PSI_T_VOID:
250 func->handler = psi_set_void;
251 break;
252 case PSI_T_ZVAL:
253 func->handler = psi_set_zval;
254 break;
255 case PSI_T_TO_BOOL:
256 func->handler = psi_set_to_bool;
257 break;
258 case PSI_T_TO_INT:
259 func->handler = psi_set_to_int;
260 break;
261 case PSI_T_TO_FLOAT:
262 func->handler = psi_set_to_float;
263 break;
264 case PSI_T_TO_OBJECT:
265 func->handler = psi_set_to_object;
266 break;
267 case PSI_T_TO_STRING:
268 if (!psi_set_func_validate_to_string(data, func, set, impl)) {
269 return false;
270 }
271 break;
272 case PSI_T_TO_ARRAY:
273 if (!psi_set_func_validate_to_array(data, func, set, impl)) {
274 return false;
275 }
276 break;
277 default:
278 data->error(data, func->token, PSI_WARNING,
279 "Unknown cast '%s' in `set` statement of implementation '%s'",
280 func->name, impl->func->name);
281 return false;
282 }
283
284 return true;
285 }