Merge branch 'slimconfigure'
[m6w6/ext-psi] / src / types / decl_enum_item.c
index 26e2d263b6e8db94db7d289949cc08dce7090e00..7410e01d7621b3b319b15a6582d59bb895db9bef 100644 (file)
@@ -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
  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 <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-
-#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));
+struct psi_decl_enum_item *psi_decl_enum_item_init(const char *name,
+               struct psi_num_exp *num)
+{
+       struct psi_decl_enum_item *i = calloc(1, sizeof(*i));
        i->name = strdup(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;
+               if (i->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);
+                       }
+               }
+               free(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);
+       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;
 }