5f664b01b2e388615872fba03e538efde924c76a
[m6w6/ext-psi] / src / types / decl_enum_item.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 struct psi_decl_enum_item *psi_decl_enum_item_init(const char *name,
30 struct psi_num_exp *num)
31 {
32 struct psi_decl_enum_item *i = calloc(1, sizeof(*i));
33 i->name = strdup(name);
34 i->num = num;
35 return i;
36 }
37
38 void psi_decl_enum_item_free(struct psi_decl_enum_item **i_ptr)
39 {
40 if (*i_ptr) {
41 struct psi_decl_enum_item *i = *i_ptr;
42
43 *i_ptr = NULL;
44 if (i->token) {
45 free(i->token);
46 }
47 if (i->num && i->num != &i->inc) {
48 psi_num_exp_free(&i->num);
49 }
50 free(i->name);
51 free(i);
52 }
53 }
54
55 void psi_decl_enum_item_dump(int fd, struct psi_decl_enum_item *item)
56 {
57 dprintf(fd, "%s", item->name);
58 if (item->num && item->num != &item->inc) {
59 dprintf(fd, " = ");
60 psi_num_exp_dump(fd, item->num);
61 }
62 }
63
64 bool psi_decl_enum_item_validate(struct psi_data *data,
65 struct psi_decl_enum *enm, struct psi_decl_enum_item *item, size_t seq)
66 {
67 if (!item->num) {
68 if (seq) {
69 item->inc.type = PSI_T_INT64;
70 item->inc.data.ival.i64 = 1;
71 item->inc.op = PSI_T_PLUS;
72 item->inc.operand = item->prev->num ? : &item->prev->inc;
73 item->num = &item->inc;
74 } else {
75 item->inc.type = PSI_T_INT64;
76 item->inc.data.ival.i64 = 0;
77 item->num = &item->inc;
78 }
79 }
80
81 if (!psi_num_exp_validate(data, item->num, NULL, NULL, NULL, NULL, enm)) {
82 return false;
83 }
84
85 item->val = psi_long_num_exp(item->num, NULL);
86
87 return true;
88 }