826fc6a0ca090a7140bdfaf28ad810c4ee1f53fc
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 *******************************************************************************/
29 # include "php_config.h"
33 struct psi_set_func
*psi_set_func_init(token_t type
, zend_string
*name
,
34 struct psi_decl_var
*var
)
36 struct psi_set_func
*func
= pecalloc(1, sizeof(*func
), 1);
39 func
->name
= zend_string_copy(name
);
45 void psi_set_func_free(struct psi_set_func
**func_ptr
)
48 struct psi_set_func
*func
= *func_ptr
;
51 psi_token_free(&func
->token
);
52 psi_decl_var_free(&func
->var
);
53 zend_string_release(func
->name
);
58 void psi_set_func_dump(struct psi_dump
*dump
, struct psi_set_func
*func
, unsigned level
)
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
;
69 while (psi_plist_get(func
->inner
, i
++, &inner
)) {
71 psi_set_exp_dump(dump
, inner
, level
, i
== count
);
75 if (func
->recursive
) {
76 PSI_DUMP(dump
, ", ...");
78 if (func
->inner
&& !func
->recursive
) {
79 PSI_DUMP(dump
, "\n%s", psi_t_indent(level
));
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
)
88 struct psi_set_exp
*sub_exp
;
90 psi_plist_get(func
->inner
, 0, &sub_exp
);
92 switch (psi_plist_count(func
->inner
)) {
94 data
->error(data
, **(struct psi_token
***) &sub_exp
->data
, PSI_WARNING
,
95 "Unexpected sub-expressions");
99 if (sub_exp
->kind
!= PSI_SET_NUMEXP
) {
100 data
->error(data
, **(struct psi_token
***) &sub_exp
->data
,
101 PSI_WARNING
, "Expected numeric expression");
104 func
->handler
= psi_set_to_stringl
;
108 func
->handler
= psi_set_to_string
;
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
)
117 struct psi_set_exp
*sub_exp
;
118 struct psi_decl_var
*set_var
= psi_set_exp_get_decl_var(set
);
120 switch (psi_plist_count(set
->inner
)) {
122 if (func
->recursive
) {
123 func
->handler
= psi_set_to_recursive
;
124 set
->inner
= func
->inner
= set
->outer
->inner
;
127 data
->error(data
, func
->token
, PSI_WARNING
,
128 "Expected sub-expressions in to_array() cast expression");
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");
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 */
142 func
->handler
= psi_set_to_array_simple
;
146 psi_plist_get(set
->inner
, 0, &sub_exp
);
147 if (sub_exp
->kind
!= PSI_SET_NUMEXP
) {
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");
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
,
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
,
166 func
->handler
= psi_set_to_array_counted
;
171 switch (psi_decl_type_get_real(set_var
->arg
->type
)->type
) {
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
);
182 func
->handler
= psi_set_to_array
;
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
)
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
);
201 func
->handler
= psi_set_to_recursive
;
202 set
->inner
= set
->outer
->inner
;
203 // FIXME validate recursive type
207 bool psi_set_func_validate(struct psi_data
*data
, struct psi_set_func
*func
,
208 struct psi_validate_scope
*scope
)
210 struct psi_set_exp
*set
= scope
->current_set
;
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
);
221 if (set
->outer
->kind
!= PSI_SET_FUNC
) {
222 data
->error(data
, func
->token
, PSI_WARNING
,
223 "Unexpected sub-expression, expected to_type() cast");
228 if (func
->inner
&& (!set
->outer
|| set
->outer
->inner
!= func
->inner
)) {
230 struct psi_set_exp
*inner
;
232 while (psi_plist_get(func
->inner
, i
++, &inner
)) {
233 struct psi_decl_var
*sub_var
;
234 struct psi_plist
*sub_args
;
237 scope
->current_set
= inner
;
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
)) {
248 if (psi_set_exp_validate(data
, inner
, scope
)) {
249 scope
->current_set
= set
;
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
);
260 switch (func
->type
) {
262 func
->handler
= psi_set_void
;
265 func
->handler
= psi_set_zval
;
268 func
->handler
= psi_set_to_bool
;
271 func
->handler
= psi_set_to_int
;
274 func
->handler
= psi_set_to_float
;
276 case PSI_T_TO_OBJECT
:
277 func
->handler
= psi_set_to_object
;
279 case PSI_T_TO_STRING
:
280 if (!psi_set_func_validate_to_string(data
, func
, set
, scope
->impl
)) {
285 if (!psi_set_func_validate_to_array(data
, func
, set
, scope
->impl
)) {
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
);