lift single lib statement restriction
[m6w6/ext-psi] / src / types / impl_def_val.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_def_val *psi_impl_def_val_init(token_t t, void *data)
32 {
33 struct psi_impl_def_val *def = calloc(1, sizeof(*def));
34
35 switch ((def->type = t)) {
36 case PSI_T_TRUE:
37 case PSI_T_FALSE:
38 case PSI_T_NULL:
39 break;
40 case PSI_T_QUOTED_STRING:
41 /* immediate upgrade */
42 def->type = PSI_T_STRING;
43 /* no break */
44 case PSI_T_STRING:
45 if (data) {
46 def->ival.zend.str = zend_string_init(data, strlen(data), 1);
47 }
48 break;
49
50 case PSI_T_NUMBER:
51 def->data.num = data;
52 break;
53
54 default:
55 assert(0);
56 }
57
58 return def;
59 }
60
61 void psi_impl_def_val_free(struct psi_impl_def_val **def_ptr)
62 {
63 if (*def_ptr) {
64 struct psi_impl_def_val *def = *def_ptr;
65
66 *def_ptr = NULL;
67 if (def->token) {
68 free(def->token);
69 }
70 switch (def->type) {
71 case PSI_T_NUMBER:
72 psi_num_exp_free(&def->data.num);
73 break;
74
75 case PSI_T_STRING:
76 if (def->ival.zend.str) {
77 zend_string_release(def->ival.zend.str);
78 }
79 break;
80 default:
81 break;
82 }
83 free(def);
84 }
85 }
86
87 bool psi_impl_def_val_validate(struct psi_data *data,
88 struct psi_impl_def_val *def, struct psi_impl_type *cmp,
89 struct psi_validate_scope *scope)
90 {
91 if (def->type == PSI_T_NULL) {
92 return true;
93 }
94
95 switch (cmp->type) {
96 case PSI_T_BOOL:
97 def->ival.zend.bval = def->type == PSI_T_TRUE ? 1 : 0;
98 break;
99
100 /* macros */
101 case PSI_T_NUMBER:
102 if (def->type == PSI_T_NUMBER) {
103 token_t typ = psi_num_exp_exec(def->data.num, &def->ival, NULL, scope->defs);
104
105 switch (typ) {
106 case PSI_T_FLOAT:
107 def->ival.dval = def->ival.fval;
108 /* no break */
109 case PSI_T_DOUBLE:
110 def->type = PSI_T_FLOAT;
111 cmp->type = PSI_T_FLOAT;
112 strcpy(cmp->name, "float");
113 break;
114 default:
115 def->type = PSI_T_INT;
116 cmp->type = PSI_T_INT;
117 strcpy(cmp->name, "int");
118 break;
119 }
120 psi_num_exp_free(&def->data.num);
121 return true;
122 }
123 break;
124
125 case PSI_T_INT:
126 if (def->type == PSI_T_NUMBER) {
127 def->type = PSI_T_INT;
128 def->ival.zend.lval = psi_num_exp_get_long(def->data.num, NULL, scope->defs);
129 psi_num_exp_free(&def->data.num);
130 }
131 if (def->type == PSI_T_INT) {
132 return true;
133 }
134 break;
135
136 case PSI_T_FLOAT:
137 case PSI_T_DOUBLE:
138 if (def->type == PSI_T_NUMBER) {
139 def->type = PSI_T_DOUBLE;
140 def->ival.dval = psi_num_exp_get_double(def->data.num, NULL, scope->defs);
141 psi_num_exp_free(&def->data.num);
142 }
143 if (def->type == PSI_T_DOUBLE) {
144 return true;
145 }
146 break;
147
148 case PSI_T_STRING:
149 if (def->type == PSI_T_STRING) {
150 return true;
151 }
152 break;
153
154 default:
155 data->error(data, def->token, PSI_WARNING,
156 "Invalid default value type '%s', "
157 "expected one of bool, int, float, string.",
158 cmp->name);
159 }
160
161 return false;
162 }
163
164 void psi_impl_def_val_dump(int fd, struct psi_impl_def_val *val) {
165 switch (val->type) {
166 case PSI_T_TRUE:
167 dprintf(fd, "true");
168 break;
169 case PSI_T_FALSE:
170 dprintf(fd, "false");
171 break;
172 case PSI_T_BOOL:
173 dprintf(fd, "%s", val->ival.zend.bval ? "true" : "false");
174 break;
175 case PSI_T_INT:
176 dprintf(fd, "%ld", val->ival.zend.lval);
177 break;
178 case PSI_T_FLOAT:
179 case PSI_T_DOUBLE:
180 dprintf(fd, "%" PRIdval, val->ival.dval);
181 break;
182 case PSI_T_STRING:
183 dprintf(fd, "\"%s\"", val->ival.zend.str->val);
184 break;
185 default:
186 assert(0);
187 }
188 }