caf32ff6b7e24d9c4493f703b06cfbdf96875e29
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
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.
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 *******************************************************************************/
26 #include "php_psi_stdinc.h"
29 struct psi_set_func
*psi_set_func_init(token_t type
, zend_string
*name
,
30 struct psi_decl_var
*var
)
32 struct psi_set_func
*func
= pecalloc(1, sizeof(*func
), 1);
35 func
->name
= zend_string_copy(name
);
41 void psi_set_func_free(struct psi_set_func
**func_ptr
)
44 struct psi_set_func
*func
= *func_ptr
;
47 psi_token_free(&func
->token
);
48 psi_decl_var_free(&func
->var
);
49 zend_string_release(func
->name
);
54 void psi_set_func_dump(int fd
, struct psi_set_func
*func
, unsigned level
)
56 dprintf(fd
, "%s(", func
->name
->val
);
57 psi_decl_var_dump(fd
, func
->var
);
58 dprintf(fd
, "\t/* fqn=%s */", func
->var
->fqn
->val
);
59 if (func
->inner
&& !func
->recursive
) {
60 size_t i
= 0, count
= psi_plist_count(func
->inner
);
61 struct psi_set_exp
*inner
;
65 while (psi_plist_get(func
->inner
, i
++, &inner
)) {
67 psi_set_exp_dump(fd
, inner
, level
, i
== count
);
71 if (func
->recursive
) {
74 if (func
->inner
&& !func
->recursive
) {
75 dprintf(fd
, "\n%s", psi_t_indent(level
));
80 static inline bool psi_set_func_validate_to_string(struct psi_data
*data
,
81 struct psi_set_func
*func
, struct psi_set_exp
*set
,
82 struct psi_impl
*impl
)
84 struct psi_set_exp
*sub_exp
;
86 psi_plist_get(func
->inner
, 0, &sub_exp
);
88 switch (psi_plist_count(func
->inner
)) {
90 data
->error(data
, **(struct psi_token
***) &sub_exp
->data
, PSI_WARNING
,
91 "Unexpected sub-expressions");
95 if (sub_exp
->kind
!= PSI_SET_NUMEXP
) {
96 data
->error(data
, **(struct psi_token
***) &sub_exp
->data
,
97 PSI_WARNING
, "Expected numeric expression");
100 func
->handler
= psi_set_to_stringl
;
104 func
->handler
= psi_set_to_string
;
109 static inline bool psi_set_func_validate_to_array(struct psi_data
*data
,
110 struct psi_set_func
*func
, struct psi_set_exp
*set
,
111 struct psi_impl
*impl
)
113 struct psi_set_exp
*sub_exp
;
114 struct psi_decl_var
*set_var
= psi_set_exp_get_decl_var(set
);
116 switch (psi_plist_count(set
->inner
)) {
118 if (func
->recursive
) {
119 func
->handler
= psi_set_to_recursive
;
120 set
->inner
= func
->inner
= set
->outer
->inner
;
123 data
->error(data
, func
->token
, PSI_WARNING
,
124 "Expected sub-expressions in to_array() cast expression");
128 psi_plist_get(set
->inner
, 0, &sub_exp
);
129 if (sub_exp
->kind
!= PSI_SET_FUNC
) {
130 data
->error(data
, **(struct psi_token
***) &sub_exp
->data
,
131 PSI_WARNING
, "Expected to_type() cast expression");
134 if (!zend_string_equals(set_var
->name
, psi_set_exp_get_decl_var(sub_exp
)->name
)) {
135 /* no warning, because of ambiguity with a one-field-struct monstrosity */
138 func
->handler
= psi_set_to_array_simple
;
142 psi_plist_get(set
->inner
, 0, &sub_exp
);
143 if (sub_exp
->kind
!= PSI_SET_NUMEXP
) {
146 psi_plist_get(set
->inner
, 1, &sub_exp
);
147 if (sub_exp
->kind
!= PSI_SET_FUNC
) {
148 data
->error(data
, **(struct psi_token
***) &sub_exp
->data
,
149 PSI_WARNING
, "Expected to_type() cast expression");
152 if (!zend_string_equals(set_var
->name
, psi_set_exp_get_decl_var(sub_exp
)->name
)) {
153 data
->error(data
, **(struct psi_token
***) &sub_exp
->data
,
155 "Expected %s(%s) cast expression to reference the same"
156 " variable like the outer `set` statement, '%s'",
157 sub_exp
->data
.func
->name
->val
,
158 psi_set_exp_get_decl_var(sub_exp
)->name
->val
,
162 func
->handler
= psi_set_to_array_counted
;
167 switch (psi_decl_type_get_real(set_var
->arg
->type
)->type
) {
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
->val
);
178 func
->handler
= psi_set_to_array
;
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
)
188 || set
->outer
->kind
!= PSI_SET_FUNC
189 || 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
->val
);
197 func
->handler
= psi_set_to_recursive
;
198 set
->inner
= set
->outer
->inner
;
199 // FIXME validate recursive type
203 bool psi_set_func_validate(struct psi_data
*data
, struct psi_set_func
*func
,
204 struct psi_validate_scope
*scope
)
206 struct psi_set_exp
*set
= scope
->current_set
;
208 if (!psi_decl_var_validate(data
, func
->var
, scope
)
209 && !psi_impl_get_temp_let_arg(scope
->impl
, func
->var
)) {
210 data
->error(data
, func
->var
->token
, PSI_WARNING
,
211 "Unknown variable '%s' in implementation %s",
212 func
->var
->name
->val
, scope
->impl
->func
->name
->val
);
217 if (set
->outer
->kind
!= PSI_SET_FUNC
) {
218 data
->error(data
, func
->token
, PSI_WARNING
,
219 "Unexpected sub-expression, expected to_type() cast");
224 if (func
->inner
&& (!set
->outer
|| set
->outer
->inner
!= func
->inner
)) {
226 struct psi_set_exp
*inner
;
228 while (psi_plist_get(func
->inner
, i
++, &inner
)) {
229 struct psi_decl_var
*sub_var
;
230 struct psi_plist
*sub_args
;
233 scope
->current_set
= inner
;
235 /* skip to "fail:" if field does not exists */
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
) {
239 if (!psi_decl_arg_get_by_var(sub_var
, sub_args
, NULL
)) {
244 if (psi_set_exp_validate(data
, inner
, scope
)) {
245 scope
->current_set
= set
;
249 scope
->current_set
= set
;
250 /* remove expr for portability with different struct members */
251 psi_plist_del(func
->inner
, --i
, NULL
);
252 psi_set_exp_free(&inner
);
256 switch (func
->type
) {
258 func
->handler
= psi_set_void
;
261 func
->handler
= psi_set_zval
;
264 func
->handler
= psi_set_to_bool
;
267 func
->handler
= psi_set_to_int
;
270 func
->handler
= psi_set_to_float
;
272 case PSI_T_TO_OBJECT
:
273 func
->handler
= psi_set_to_object
;
275 case PSI_T_TO_STRING
:
276 if (!psi_set_func_validate_to_string(data
, func
, set
, scope
->impl
)) {
281 if (!psi_set_func_validate_to_array(data
, func
, set
, scope
->impl
)) {
286 data
->error(data
, func
->token
, PSI_WARNING
,
287 "Unknown cast '%s' in `set` statement of implementation '%s'",
288 func
->name
->val
, scope
->impl
->func
->name
->val
);