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