raising the head after a three-weeks refactoring
[m6w6/ext-psi] / src / types / set_exp.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 #include "call.h"
29 #include "marshal.h"
30
31 struct psi_set_exp *psi_set_exp_init(enum psi_set_exp_kind kind, void *data)
32 {
33 struct psi_set_exp *val = calloc(1, sizeof(*val));
34
35 switch (val->kind = kind) {
36 case PSI_SET_FUNC:
37 val->data.func = data;
38 val->inner = val->data.func->inner;
39 break;
40 case PSI_SET_NUMEXP:
41 val->data.num = data;
42 break;
43 default:
44 assert(0);
45 }
46
47 return val;
48 }
49
50 void psi_set_exp_exec(struct psi_set_exp *val, struct psi_call_frame *frame)
51 {
52 struct psi_call_frame_symbol *frame_sym = NULL;
53 struct psi_call_frame_argument *frame_arg;
54 struct psi_decl_var *set_dvar = psi_set_exp_get_decl_var(val);
55 struct psi_impl_var *set_ivar = psi_set_exp_get_impl_var(val);
56
57 assert(set_dvar);
58 assert(set_ivar);
59
60 frame_sym = psi_call_frame_fetch_symbol(frame, set_dvar);
61 frame_arg = psi_call_frame_get_argument(frame, set_ivar->fqn);
62
63 if (frame_arg && frame_arg->zval_ptr) {
64 zval_dtor(frame_arg->zval_ptr);
65 psi_set_exp_exec_ex(val, frame_arg->zval_ptr, frame_sym->ptr, frame);
66 }
67 }
68
69 void psi_set_exp_exec_ex(struct psi_set_exp *val, zval *zv, impl_val *iv,
70 struct psi_call_frame *frame)
71 {
72 switch (val->kind) {
73 case PSI_SET_FUNC:
74 val->data.func->handler(zv, val, iv, frame);
75 break;
76 case PSI_SET_NUMEXP:
77 psi_num_exp_exec(val->data.num, iv, frame);
78 psi_set_to_int(zv, val, iv, frame);
79 break;
80 default:
81 assert(0);
82 break;
83 }
84 }
85
86 void psi_set_exp_free(struct psi_set_exp **exp_ptr)
87 {
88 if (*exp_ptr) {
89 struct psi_set_exp *exp = *exp_ptr;
90
91 *exp_ptr = NULL;
92
93 if (exp->inner && (!exp->outer || exp->outer->inner != exp->inner)) {
94 psi_plist_free(exp->inner);
95 }
96
97 switch (exp->kind) {
98 case PSI_SET_FUNC:
99 psi_set_func_free(&exp->data.func);
100 break;
101 case PSI_SET_NUMEXP:
102 psi_num_exp_free(&exp->data.num);
103 break;
104 default:
105 assert(0);
106 break;
107 }
108 if (exp->var) {
109 psi_impl_var_free(&exp->var);
110 }
111 free(exp);
112 }
113 }
114
115 void psi_set_exp_dump(int fd, struct psi_set_exp *set, unsigned level, int last)
116 {
117 if (level > 1) {
118 /* only if not directly after `set ...` */
119 dprintf(fd, "%s", psi_t_indent(level));
120 }
121
122 if (set->var) {
123 /* parsed, or generated */
124 if (set->var->token) {
125 dprintf(fd, "%s = ", set->var->name);
126 }
127 }
128
129 switch (set->kind) {
130 case PSI_SET_FUNC:
131 psi_set_func_dump(fd, set->data.func, level);
132 break;
133 case PSI_SET_NUMEXP:
134 psi_num_exp_dump(fd, set->data.num);
135 break;
136 default:
137 assert(0);
138 }
139
140 if (!last) {
141 dprintf(fd, ",");
142 }
143
144 if (set->var) {
145 dprintf(fd, "\t/* fqn=%s */", set->var->fqn);
146 }
147 }
148
149 struct psi_decl_var *psi_set_exp_get_decl_var(struct psi_set_exp *exp)
150 {
151 switch (exp->kind) {
152 case PSI_SET_FUNC:
153 return exp->data.func->var;
154 default:
155 return NULL;
156 }
157 }
158
159 struct psi_impl_var *psi_set_exp_get_impl_var(struct psi_set_exp *exp)
160 {
161 if (!exp->var) {
162 struct psi_decl_var *dvar = psi_set_exp_get_decl_var(exp);
163 char *dollar_name;
164
165 if (!dvar) {
166 return NULL;
167 }
168
169 dollar_name = psi_impl_var_name_prepend(strdup("$"), dvar->name);
170 exp->var = psi_impl_var_init(dollar_name, 0);
171 free(dollar_name);
172 }
173
174 return exp->var;
175 }
176
177 bool psi_set_exp_validate(struct psi_data *data, struct psi_set_exp *set,
178 struct psi_impl *impl, struct psi_decl *cb_decl)
179 {
180 struct psi_impl_var *ivar = psi_set_exp_get_impl_var(set);
181
182 if (ivar && !psi_impl_var_validate(data, ivar, impl, NULL, set)) {
183 data->error(data, ivar->token ? : **(struct psi_token ***) &set->data,
184 PSI_WARNING, "Unknown variable '%s'", ivar->name);
185 return false;
186 }
187
188 switch (set->kind) {
189 case PSI_SET_NUMEXP:
190 if (!psi_num_exp_validate(data, set->data.num, impl, cb_decl, NULL, set, NULL)) {
191 return false;
192 }
193 break;
194 case PSI_SET_FUNC:
195 if (!psi_set_func_validate(data, set->data.func, set, impl, cb_decl)) {
196 return false;
197 }
198 break;
199 default:
200 assert(0);
201 }
202 return true;
203 }
204