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
, const char *name
,
30 struct psi_decl_var
*var
)
32 struct psi_set_func
*func
= calloc(1, sizeof(*func
));
35 func
->name
= strdup(name
);
41 void psi_set_func_free(struct psi_set_func
**func_ptr
)
44 struct psi_set_func
*func
= *func_ptr
;
50 psi_decl_var_free(&func
->var
);
56 void psi_set_func_dump(int fd
, struct psi_set_func
*func
, unsigned level
)
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
;
67 while (psi_plist_get(func
->inner
, i
++, &inner
)) {
69 psi_set_exp_dump(fd
, inner
, level
, i
== count
);
73 if (func
->recursive
) {
76 if (func
->inner
&& !func
->recursive
) {
77 dprintf(fd
, "\n%s", psi_t_indent(level
));
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
)
86 struct psi_set_exp
*sub_exp
;
88 psi_plist_get(func
->inner
, 0, &sub_exp
);
90 switch (psi_plist_count(func
->inner
)) {
92 data
->error(data
, **(struct psi_token
***) &sub_exp
->data
, PSI_WARNING
,
93 "Unexpected sub-expressions");
97 if (sub_exp
->kind
!= PSI_SET_NUMEXP
) {
98 data
->error(data
, **(struct psi_token
***) &sub_exp
->data
,
99 PSI_WARNING
, "Expected numeric expression");
102 func
->handler
= psi_set_to_stringl
;
106 func
->handler
= psi_set_to_string
;
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
)
115 struct psi_set_exp
*sub_exp
;
116 struct psi_decl_var
*set_var
= psi_set_exp_get_decl_var(set
);
118 switch (psi_plist_count(set
->inner
)) {
120 if (func
->recursive
) {
121 func
->handler
= psi_set_to_recursive
;
122 set
->inner
= func
->inner
= set
->outer
->inner
;
125 data
->error(data
, func
->token
, PSI_WARNING
,
126 "Expected sub-expressions in to_array() cast expression");
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");
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 */
140 func
->handler
= psi_set_to_array_simple
;
144 psi_plist_get(set
->inner
, 0, &sub_exp
);
145 if (sub_exp
->kind
!= PSI_SET_NUMEXP
) {
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");
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
,
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
);
163 func
->handler
= psi_set_to_array_counted
;
168 switch (psi_decl_type_get_real(set_var
->arg
->type
)->type
) {
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
);
179 func
->handler
= psi_set_to_array
;
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
)
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'",
198 func
->handler
= psi_set_to_recursive
;
199 set
->inner
= set
->outer
->inner
;
200 // FIXME validate recursive type
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
)
207 if (!psi_decl_var_validate(data
, func
->var
, impl
->decl
, NULL
, set
)
208 && !psi_decl_var_validate(data
, func
->var
, 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
);
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
)) {
227 struct psi_set_exp
*inner
;
229 while (psi_plist_get(func
->inner
, i
++, &inner
)) {
230 struct psi_decl_var
*sub_var
;
231 struct psi_plist
*sub_args
;
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
);
248 switch (func
->type
) {
250 func
->handler
= psi_set_void
;
253 func
->handler
= psi_set_zval
;
256 func
->handler
= psi_set_to_bool
;
259 func
->handler
= psi_set_to_int
;
262 func
->handler
= psi_set_to_float
;
264 case PSI_T_TO_OBJECT
:
265 func
->handler
= psi_set_to_object
;
267 case PSI_T_TO_STRING
:
268 if (!psi_set_func_validate_to_string(data
, func
, set
, impl
)) {
273 if (!psi_set_func_validate_to_array(data
, func
, set
, impl
)) {
278 data
->error(data
, func
->token
, PSI_WARNING
,
279 "Unknown cast '%s' in `set` statement of implementation '%s'",
280 func
->name
, impl
->func
->name
);