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