parser: RETURN [<native call> AS] SET_FUNC
[m6w6/ext-psi] / src / types / impl.c
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
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.
13
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 *******************************************************************************/
25
26 #include "php_psi_stdinc.h"
27 #include "data.h"
28
29 #include <assert.h>
30
31 struct psi_impl *psi_impl_init(struct psi_impl_func *func,
32 struct psi_plist *stmts)
33 {
34 struct psi_impl *impl = calloc(1, sizeof(*impl));
35 size_t i = 0;
36 struct psi_token **abstract_stmt;
37
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);
43
44 while (psi_plist_get(stmts, i++, &abstract_stmt)) {
45 switch ((*abstract_stmt)->type) {
46 case PSI_T_RETURN:
47 impl->stmts.ret = psi_plist_add(impl->stmts.ret, &abstract_stmt);
48 break;
49 case PSI_T_LET:
50 case PSI_T_TEMP:
51 impl->stmts.let = psi_plist_add(impl->stmts.let, &abstract_stmt);
52 break;
53 case PSI_T_SET:
54 impl->stmts.set = psi_plist_add(impl->stmts.set, &abstract_stmt);
55 break;
56 case PSI_T_FREE:
57 impl->stmts.fre = psi_plist_add(impl->stmts.fre, &abstract_stmt);
58 break;
59 case PSI_T_PRE_ASSERT:
60 case PSI_T_POST_ASSERT:
61 impl->stmts.ass = psi_plist_add(impl->stmts.ass, &abstract_stmt);
62 break;
63 default:
64 assert(0);
65 }
66 }
67 free(stmts);
68
69 impl->func = func;
70
71 return impl;
72 }
73
74 void psi_impl_free(struct psi_impl **impl_ptr)
75 {
76 if (*impl_ptr) {
77 struct psi_impl *impl = *impl_ptr;
78
79 *impl_ptr = NULL;
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);
86 free(impl);
87 }
88 }
89
90 void psi_impl_dump(int fd, struct psi_impl *impl)
91 {
92 size_t i;
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;
98
99 psi_impl_func_dump(fd, impl->func);
100 dprintf(fd, " {\n");
101 for (i = 0; psi_plist_get(impl->stmts.let, i, &let); ++i) {
102 psi_let_stmt_dump(fd, let);
103 }
104 for (i = 0; psi_plist_get(impl->stmts.ass, i, &ass); ++i) {
105 psi_assert_stmt_dump(fd, ass);
106 }
107 for (i = 0; psi_plist_get(impl->stmts.ret, i, &ret); ++i) {
108 psi_return_stmt_dump(fd, ret);
109 }
110 for (i = 0; psi_plist_get(impl->stmts.set, i, &set); ++i) {
111 psi_set_stmt_dump(fd, set);
112 }
113 for (i = 0; psi_plist_get(impl->stmts.fre, i, &fre); ++i) {
114 psi_free_stmt_dump(fd, fre);
115 }
116 dprintf(fd, "}\n");
117 }
118
119 bool psi_impl_validate(struct psi_data *data, struct psi_impl *impl)
120 {
121 if (!psi_impl_func_validate(data, impl->func)) {
122 return false;
123 }
124 if (!psi_return_stmt_validate(data, impl)) {
125 return false;
126 }
127 if (!psi_let_stmts_validate(data, impl)) {
128 return false;
129 }
130 if (!psi_set_stmts_validate(data, impl)) {
131 return false;
132 }
133 if (!psi_assert_stmts_validate(data, impl)) {
134 return false;
135 }
136 if (!psi_free_stmts_validate(data, impl)) {
137 return false;
138 }
139 return true;
140 }
141
142 size_t psi_impl_num_min_args(struct psi_impl *impl)
143 {
144 size_t i;
145 struct psi_impl_arg *arg;
146
147 for (i = 0; psi_plist_get(impl->func->args, i, &arg); ++i) {
148 if (arg->def) {
149 break;
150 }
151 }
152 return i;
153 }
154
155 void psi_impl_stmt_free(struct psi_token ***abstract_stmt)
156 {
157 switch ((**abstract_stmt)->type) {
158 case PSI_T_LET:
159 psi_let_stmt_free((void *) abstract_stmt);
160 break;
161 case PSI_T_SET:
162 psi_set_stmt_free((void *) abstract_stmt);
163 break;
164 case PSI_T_RETURN:
165 psi_return_stmt_free((void *) abstract_stmt);
166 break;
167 case PSI_T_FREE:
168 psi_free_stmt_free((void *) abstract_stmt);
169 break;
170 case PSI_T_PRE_ASSERT:
171 case PSI_T_POST_ASSERT:
172 psi_assert_stmt_free((void *) abstract_stmt);
173 break;
174 default:
175 assert(0);
176 }
177 }
178
179 struct psi_decl_arg *psi_impl_get_decl_arg(struct psi_impl *impl,
180 struct psi_decl_var *var)
181 {
182 struct psi_return_stmt *ret;
183
184 if (psi_plist_get(impl->stmts.ret, 0, &ret)) {
185 if (ret->exp->args) {
186 size_t i = 0;
187 struct psi_decl_var *arg;
188
189 while (psi_plist_get(ret->exp->args, i++, &arg)) {
190 if (!strcmp(var->name, arg->name)) {
191 return var->arg = arg->arg;
192 }
193 }
194 }
195 }
196
197 return psi_decl_get_arg(impl->decl, var);
198 }
199
200 struct psi_let_stmt *psi_impl_get_let(struct psi_impl *impl,
201 struct psi_decl_var* var)
202 {
203 size_t i = 0;
204 struct psi_let_stmt *let;
205
206 while (psi_plist_get(impl->stmts.let, i++, &let)) {
207 if (let->exp->var->arg == var->arg) {
208 return let;
209 }
210 }
211 return NULL;
212 }
213
214 struct psi_impl_arg *psi_impl_get_arg(struct psi_impl *impl,
215 struct psi_impl_var* var)
216 {
217 size_t i = 0;
218 struct psi_impl_arg *iarg;
219
220 while (psi_plist_get(impl->func->args, i++, &iarg)) {
221 if (!strcmp(var->name, iarg->var->name)) {
222 return var->arg = iarg;
223 }
224 }
225 return NULL;
226 }
227
228 struct psi_decl_arg *psi_impl_get_temp_let_arg(struct psi_impl *impl,
229 struct psi_decl_var* var)
230 {
231 size_t j = 0;
232 struct psi_let_stmt *let = NULL;
233
234 while (psi_plist_get(impl->stmts.let, j++, &let)) {
235 if (let->exp->kind != PSI_LET_TMP) {
236 continue;
237 }
238 if (strcmp(let->exp->var->name, var->name)) {
239 continue;
240 }
241 return var->arg = let->exp->var->arg;
242 }
243 return NULL;
244 }