d32b00b25aafc8bcc86196b2d127caca29ca70a4
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"
40 let_func
*init_let_func(token_t type
, const char *name
, impl_var
*var
) {
41 let_func
*func
= calloc(1, sizeof(*func
));
43 func
->name
= strdup(name
);
48 void free_let_func(let_func
*func
) {
49 free_impl_var(func
->var
);
52 free_let_vals(func
->inner
);
57 void dump_let_func(int fd
, let_func
*func
, unsigned level
) {
58 dprintf(fd
, "%s(%s", func
->name
, func
->var
->name
);
64 for (i
= 0; i
< func
->inner
->count
; ++i
) {
66 dump_let_val(fd
, func
->inner
->vals
[i
], level
+1, i
+1 == func
->inner
->count
);
69 dprintf(fd
, "%s", psi_t_indent(level
));
74 static inline int validate_let_func_type(struct psi_data
*data
, let_func
*func
, impl
*impl
) {
88 data
->error(data
, func
->var
->token
, PSI_WARNING
,
89 "Unknown `let` cast function '%s' of implementation '%s'",
90 func
->name
, impl
->func
->name
);
95 static inline int validate_let_func_inner(struct psi_data
*data
, let_func
*func
, decl_var
*let_var
, impl
*impl
) {
99 decl_args
*sub_args
= extract_decl_type_args(let_var
->arg
->type
, &var_typ
);
102 data
->error(data
, let_var
->token
, PSI_WARNING
,
103 "Inner let statement's values must refer to a structure type,"
104 " got '%s' for '%s'",
105 var_typ
->name
, let_var
->name
);
109 for (i
= 0; i
< func
->inner
->count
; ++i
) {
110 let_val
*inner
= func
->inner
->vals
[i
];
111 let_val
*outer
= func
->outer
;
112 const char *name
= locate_let_val_varname(inner
);
116 sub_arg
= locate_decl_arg(sub_args
, name
);
118 if (!name
|| !sub_arg
) {
119 data
->error(data
, let_var
->token
, PSI_WARNING
,
120 "Unknown variable '%s' of '%s'",
121 name
, var_typ
->real
.strct
->name
);
124 if (!validate_let_val(data
, inner
, sub_arg
->var
, impl
)) {
132 int validate_let_func(struct psi_data
*data
, let_func
*func
, decl_var
*let_var
, impl
*impl
) {
136 if (impl
->func
->args
) {
137 locate_impl_var_arg(func
->var
, impl
->func
->args
);
139 if (!func
->var
->arg
&& !func
->ref
) {
140 data
->error(data
, func
->var
->token
, PSI_WARNING
,
141 "Unknown variable '%s%s' of `let` statement"
142 " for cast '%s' of implementation '%s'",
143 *func
->var
->name
== '$' ? "" : "$",
144 func
->var
->name
, func
->name
, impl
->func
->name
);
147 if (!validate_let_func_type(data
, func
, impl
)) {
150 if (!validate_let_func_inner(data
, func
, let_var
, impl
)) {