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