eec90e6783ea123dac39de26f5eb058a25b92909
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
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.
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 *******************************************************************************/
26 #include "php_psi_stdinc.h"
32 #define PSI_IMPL_DEF_VAL_DEBUG 0
34 struct psi_impl_def_val
*psi_impl_def_val_init(token_t t
, void *data
)
36 struct psi_impl_def_val
*def
= pecalloc(1, sizeof(*def
), 1);
38 switch ((def
->type
= t
)) {
43 case PSI_T_QUOTED_STRING
:
44 /* immediate upgrade */
45 def
->type
= PSI_T_STRING
;
49 def
->ival
.zend
.str
= zend_string_copy(data
);
64 void psi_impl_def_val_free(struct psi_impl_def_val
**def_ptr
)
67 struct psi_impl_def_val
*def
= *def_ptr
;
70 psi_token_free(&def
->token
);
73 psi_num_exp_free(&def
->data
.num
);
77 if (def
->ival
.zend
.str
) {
78 zend_string_release(def
->ival
.zend
.str
);
88 bool psi_impl_def_val_validate(struct psi_data
*data
,
89 struct psi_impl_def_val
*val
, struct psi_impl_type
*type
,
90 struct psi_validate_scope
*scope
)
92 if (val
->type
== PSI_T_NULL
) {
94 } else if (val
->type
== PSI_T_NUMBER
) {
95 if (!psi_num_exp_validate(data
, val
->data
.num
, scope
)) {
100 switch (type
? type
->type
: PSI_T_MIXED
) {
102 val
->ival
.zend
.bval
= val
->type
== PSI_T_TRUE
? 1 : 0;
108 if (val
->type
== PSI_T_NUMBER
) {
109 token_t typ
= psi_num_exp_exec(val
->data
.num
, &val
->ival
, NULL
, scope
->cpp
);
113 val
->ival
.dval
= val
->ival
.fval
;
116 val
->type
= PSI_T_FLOAT
;
117 type
->type
= PSI_T_FLOAT
;
118 zend_string_release(type
->name
);
119 type
->name
= zend_string_init_interned(ZEND_STRL("float"), 1);
122 if (val
->ival
.u64
> ZEND_LONG_MAX
) {
123 data
->error(data
, val
->token
, PSI_WARNING
,
124 "Integer too big for signed representation: '%" PRIu64
"'",
128 /* FIXME big integers */
129 val
->type
= PSI_T_INT
;
130 type
->type
= PSI_T_INT
;
131 zend_string_release(type
->name
);
132 type
->name
= zend_string_init_interned(ZEND_STRL("int"), 1);
135 psi_num_exp_free(&val
->data
.num
);
141 if (val
->type
== PSI_T_NUMBER
) {
142 val
->type
= PSI_T_INT
;
143 val
->ival
.zend
.lval
= psi_num_exp_get_long(val
->data
.num
, NULL
, scope
->cpp
);
144 #if PSI_IMPL_DEF_VAL_DEBUG
145 PSI_DEBUG_PRINT(data
, "PSI: NUMBER (long) %" PRIi64
" from ", val
->ival
.zend
.lval
);
146 PSI_DEBUG_DUMP(data
, psi_num_exp_dump
, val
->data
.num
);
147 PSI_DEBUG_PRINT(data
, "\n");
149 psi_num_exp_free(&val
->data
.num
);
151 if (val
->type
== PSI_T_INT
) {
158 if (val
->type
== PSI_T_NUMBER
) {
159 val
->type
= PSI_T_DOUBLE
;
160 val
->ival
.dval
= psi_num_exp_get_double(val
->data
.num
, NULL
, scope
->cpp
);
161 #if PSI_IMPL_DEF_VAL_DEBUG
162 PSI_DEBUG_PRINT(data
, "PSI: NUMBER (double) %" PRIdval
" from ", val
->ival
.dval
);
163 PSI_DEBUG_DUMP(data
, psi_num_exp_dump
, val
->data
.num
);
164 PSI_DEBUG_PRINT(data
, "\n");
166 psi_num_exp_free(&val
->data
.num
);
168 if (val
->type
== PSI_T_DOUBLE
) {
174 if (val
->type
== PSI_T_STRING
) {
192 if (val
->type
== PSI_T_NUMBER
) {
193 token_t typ
= psi_num_exp_exec(val
->data
.num
, &val
->ival
, NULL
, scope
->cpp
);
198 case PSI_T_LONG_DOUBLE
:
201 psi_calc_cast(typ
, &val
->ival
, PSI_T_DOUBLE
, &val
->ival
);
204 val
->type
= PSI_T_DOUBLE
;
207 if (val
->ival
.u64
> (uint64_t) ZEND_LONG_MAX
) {
208 if (val
->ival
.u64
> (1LU<<53)) {
211 /* convert to string */
212 char *res
= zend_print_ulong_to_buf(buf
+ sizeof(buf
) - 1, val
->ival
.u64
);
213 val
->type
= PSI_T_STRING
;
214 val
->ival
.zend
.str
= zend_string_init(res
, buf
+ sizeof(buf
) - 1 - res
, 1);
222 psi_calc_cast(typ
, &val
->ival
, PSI_T_INT64
, &val
->ival
);
225 val
->type
= PSI_T_INT
;
231 data
->error(data
, val
->token
, PSI_WARNING
,
232 "Invalid default value type '%s', "
233 "expected one of bool, int, float, string.",
234 type
? type
->name
->val
: "mixed");
240 void psi_impl_def_val_dump(int fd
, struct psi_impl_def_val
*val
) {
249 dprintf(fd
, "false");
252 dprintf(fd
, "%s", val
->ival
.zend
.bval
? "true" : "false");
255 dprintf(fd
, ZEND_LONG_FMT
, val
->ival
.zend
.lval
);
259 if (isinf(val
->ival
.dval
)) {
260 dprintf(fd
, "\\INF");
261 } else if (isnan(val
->ival
.dval
)) {
262 dprintf(fd
, "\\NAN");
264 dprintf(fd
, "%" PRIdval
, val
->ival
.dval
);
268 dprintf(fd
, "\"%s\"", val
->ival
.zend
.str
->val
);
271 psi_num_exp_dump(fd
, val
->data
.num
);