X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Ftypes%2Fdecl_enum_item.c;h=7c4914261599380f658394ea3135c541718f0aa6;hp=26e2d263b6e8db94db7d289949cc08dce7090e00;hb=2fa436074ca9a5e87f39b696de832fa2188fcfc6;hpb=2f5af21b263403997e154658635d6b6e6eaab453 diff --git a/src/types/decl_enum_item.c b/src/types/decl_enum_item.c index 26e2d26..7c49142 100644 --- a/src/types/decl_enum_item.c +++ b/src/types/decl_enum_item.c @@ -5,11 +5,11 @@ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE @@ -21,34 +21,88 @@ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*******************************************************************************/ + *******************************************************************************/ -#ifdef HAVE_CONFIG_H -# include "config.h" -#else -# include "php_config.h" -#endif +#include "php_psi_stdinc.h" +#include "data.h" -#include -#include -#include - -#include "decl_enum_item.h" - -decl_enum_item *init_decl_enum_item(const char *name, num_exp *num) { - decl_enum_item *i = calloc(1, sizeof(*i)); - i->name = strdup(name); +struct psi_decl_enum_item *psi_decl_enum_item_init(zend_string *name, + struct psi_num_exp *num) +{ + struct psi_decl_enum_item *i = calloc(1, sizeof(*i)); + i->name = zend_string_copy(name); i->num = num; return i; } -void free_decl_enum_item(decl_enum_item *i) { - if (i->token) { - free(i->token); +void psi_decl_enum_item_free(struct psi_decl_enum_item **i_ptr) +{ + if (*i_ptr) { + struct psi_decl_enum_item *i = *i_ptr; + + *i_ptr = NULL; + psi_token_free(&i->token); + if (i->num) { + if (i->num == &i->inc) { + switch (i->inc.op) { + case PSI_T_NUMBER: + psi_number_free(&i->inc.data.n); + break; + case PSI_T_PLUS: + psi_num_exp_free(&i->inc.data.b.lhs); + psi_num_exp_free(&i->inc.data.b.rhs); + break; + default: + assert(0); + } + } else { + psi_num_exp_free(&i->num); + } + } + zend_string_release(i->name); + free(i); } - if (i->num && i->num != &i->inc) { - free_num_exp(i->num); +} + +void psi_decl_enum_item_dump(int fd, struct psi_decl_enum_item *item) +{ + dprintf(fd, "%s", item->name->val); + if (item->num && item->num != &item->inc) { + dprintf(fd, " = "); + psi_num_exp_dump(fd, item->num); } - free(i->name); - free(i); +} + +bool psi_decl_enum_item_validate(struct psi_data *data, + struct psi_decl_enum *enm, struct psi_decl_enum_item *item, size_t seq) +{ + struct psi_validate_scope scope = {0}; + + if (!item->num) { + if (seq) { + int64_t one = 1; + + item->inc.op = PSI_T_PLUS; + item->inc.data.b.lhs = psi_num_exp_init_unary(PSI_T_LPAREN, + psi_num_exp_copy(item->prev->num)); + item->inc.data.b.rhs = psi_num_exp_init_num( + psi_number_init(PSI_T_INT64, &one, 0)); + item->num = &item->inc; + } else { + int64_t nil = 0; + + item->inc.op = PSI_T_NUMBER; + item->inc.data.n = psi_number_init(PSI_T_INT64, &nil, 0); + item->num = &item->inc; + } + } + + scope.current_enum = enm; + if (!psi_num_exp_validate(data, item->num, &scope)) { + return false; + } + + item->val = psi_num_exp_get_long(item->num, NULL, NULL); + + return true; }