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"
31 struct psi_impl
*psi_impl_init(struct psi_impl_func
*func
,
32 struct psi_plist
*stmts
)
34 struct psi_impl
*impl
= calloc(1, sizeof(*impl
));
36 struct psi_token
**abstract_stmt
;
38 impl
->stmts
.ret
= psi_plist_init((psi_plist_dtor
) psi_return_stmt_free
);
39 impl
->stmts
.let
= psi_plist_init((psi_plist_dtor
) psi_let_stmt_free
);
40 impl
->stmts
.set
= psi_plist_init((psi_plist_dtor
) psi_set_stmt_free
);
41 impl
->stmts
.fre
= psi_plist_init((psi_plist_dtor
) psi_free_stmt_free
);
42 impl
->stmts
.ass
= psi_plist_init((psi_plist_dtor
) psi_assert_stmt_free
);
44 while (psi_plist_get(stmts
, i
++, &abstract_stmt
)) {
45 switch ((*abstract_stmt
)->type
) {
47 impl
->stmts
.ret
= psi_plist_add(impl
->stmts
.ret
, &abstract_stmt
);
51 impl
->stmts
.let
= psi_plist_add(impl
->stmts
.let
, &abstract_stmt
);
54 impl
->stmts
.set
= psi_plist_add(impl
->stmts
.set
, &abstract_stmt
);
57 impl
->stmts
.fre
= psi_plist_add(impl
->stmts
.fre
, &abstract_stmt
);
59 case PSI_T_PRE_ASSERT
:
60 case PSI_T_POST_ASSERT
:
61 impl
->stmts
.ass
= psi_plist_add(impl
->stmts
.ass
, &abstract_stmt
);
74 void psi_impl_free(struct psi_impl
**impl_ptr
)
77 struct psi_impl
*impl
= *impl_ptr
;
80 psi_impl_func_free(&impl
->func
);
81 psi_plist_free(impl
->stmts
.ret
);
82 psi_plist_free(impl
->stmts
.let
);
83 psi_plist_free(impl
->stmts
.set
);
84 psi_plist_free(impl
->stmts
.fre
);
85 psi_plist_free(impl
->stmts
.ass
);
90 void psi_impl_dump(int fd
, struct psi_impl
*impl
)
93 struct psi_return_stmt
*ret
;
94 struct psi_let_stmt
*let
;
95 struct psi_set_stmt
*set
;
96 struct psi_free_stmt
*fre
;
97 struct psi_assert_stmt
*ass
;
99 psi_impl_func_dump(fd
, impl
->func
);
101 for (i
= 0; psi_plist_get(impl
->stmts
.let
, i
, &let
); ++i
) {
102 psi_let_stmt_dump(fd
, let
);
104 for (i
= 0; psi_plist_get(impl
->stmts
.ass
, i
, &ass
); ++i
) {
105 psi_assert_stmt_dump(fd
, ass
);
107 for (i
= 0; psi_plist_get(impl
->stmts
.ret
, i
, &ret
); ++i
) {
108 psi_return_stmt_dump(fd
, ret
);
110 for (i
= 0; psi_plist_get(impl
->stmts
.set
, i
, &set
); ++i
) {
111 psi_set_stmt_dump(fd
, set
);
113 for (i
= 0; psi_plist_get(impl
->stmts
.fre
, i
, &fre
); ++i
) {
114 psi_free_stmt_dump(fd
, fre
);
119 bool psi_impl_validate(struct psi_data
*data
, struct psi_impl
*impl
,
120 struct psi_validate_scope
*scope
)
124 if (!psi_impl_func_validate(data
, impl
->func
, scope
)) {
127 if (!psi_return_stmt_validate(data
, scope
)) {
130 if (!psi_let_stmts_validate(data
, scope
)) {
133 if (!psi_set_stmts_validate(data
, scope
)) {
136 if (!psi_assert_stmts_validate(data
, scope
)) {
139 if (!psi_free_stmts_validate(data
, scope
)) {
145 size_t psi_impl_num_min_args(struct psi_impl
*impl
)
148 struct psi_impl_arg
*arg
;
150 for (i
= 0; psi_plist_get(impl
->func
->args
, i
, &arg
); ++i
) {
158 void psi_impl_stmt_free(struct psi_token
***abstract_stmt
)
160 switch ((**abstract_stmt
)->type
) {
162 psi_let_stmt_free((void *) abstract_stmt
);
165 psi_set_stmt_free((void *) abstract_stmt
);
168 psi_return_stmt_free((void *) abstract_stmt
);
171 psi_free_stmt_free((void *) abstract_stmt
);
173 case PSI_T_PRE_ASSERT
:
174 case PSI_T_POST_ASSERT
:
175 psi_assert_stmt_free((void *) abstract_stmt
);
182 struct psi_decl_arg
*psi_impl_get_decl_arg(struct psi_impl
*impl
,
183 struct psi_decl_var
*var
)
185 struct psi_return_stmt
*ret
;
187 if (psi_plist_get(impl
->stmts
.ret
, 0, &ret
)) {
188 if (ret
->exp
->args
) {
190 struct psi_decl_var
*arg
;
192 while (psi_plist_get(ret
->exp
->args
, i
++, &arg
)) {
193 if (!strcmp(var
->name
, arg
->name
)) {
194 return var
->arg
= arg
->arg
;
200 return psi_decl_get_arg(impl
->decl
, var
);
203 struct psi_let_stmt
*psi_impl_get_let(struct psi_impl
*impl
,
204 struct psi_decl_var
* var
)
207 struct psi_let_stmt
*let
;
209 while (psi_plist_get(impl
->stmts
.let
, i
++, &let
)) {
210 if (let
->exp
->var
->arg
== var
->arg
) {
217 struct psi_impl_arg
*psi_impl_get_arg(struct psi_impl
*impl
,
218 struct psi_impl_var
* var
)
221 struct psi_impl_arg
*iarg
;
223 while (psi_plist_get(impl
->func
->args
, i
++, &iarg
)) {
224 if (!strcmp(var
->name
, iarg
->var
->name
)) {
225 return var
->arg
= iarg
;
231 struct psi_decl_arg
*psi_impl_get_temp_let_arg(struct psi_impl
*impl
,
232 struct psi_decl_var
* var
)
235 struct psi_let_stmt
*let
= NULL
;
237 while (psi_plist_get(impl
->stmts
.let
, j
++, &let
)) {
238 if (let
->exp
->kind
!= PSI_LET_TMP
) {
241 if (strcmp(let
->exp
->var
->name
, var
->name
)) {
244 return var
->arg
= let
->exp
->var
->arg
;