fff7c88c30b0d955cbf87e412418eac357562bf1
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
;
167 complex: 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
);
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
)
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'",
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_set_exp
*set
, struct psi_impl
*impl
, struct psi_decl
*cb_decl
)
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
);
216 if (set
->outer
->kind
!= PSI_SET_FUNC
) {
217 data
->error(data
, func
->token
, PSI_WARNING
,
218 "Unexpected sub-expression, expected to_type() cast");
223 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
;
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
);
247 switch (func
->type
) {
249 func
->handler
= psi_set_void
;
252 func
->handler
= psi_set_zval
;
255 func
->handler
= psi_set_to_bool
;
258 func
->handler
= psi_set_to_int
;
261 func
->handler
= psi_set_to_float
;
263 case PSI_T_TO_OBJECT
:
264 func
->handler
= psi_set_to_object
;
266 case PSI_T_TO_STRING
:
267 if (!psi_set_func_validate_to_string(data
, func
, set
, impl
)) {
272 if (!psi_set_func_validate_to_array(data
, func
, set
, impl
)) {
278 if (!psi_set_func_validate_to_recursive(data
, func
, set
, impl
)) {
283 data
->error(data
, func
->token
, PSI_WARNING
,
284 "Unknown cast '%s' in `set` statement of implementation '%s'",
285 func
->name
, impl
->func
->name
);