raising the head after a three-weeks refactoring
[m6w6/ext-psi] / src / types / decl_struct.c
index 522e405e3bda04ef226fc624cd12fad1543b3294..bdee3fec00ae58168477a0bb2b6347e041b2a208 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 <assert.h>
 
-#include "data.h"
-
-decl_struct* init_decl_struct(const char* name, decl_args* args) {
-       decl_struct* s = calloc(1, sizeof(*s));
+struct psi_decl_struct* psi_decl_struct_init(const char *name,
+               struct psi_plist *args)
+{
+       struct psi_decl_struct *s = calloc(1, sizeof(*s));
        s->name = strdup(name);
        s->args = args;
        return s;
 }
 
-void free_decl_struct(decl_struct* s) {
-       if (s->token) {
-               free(s->token);
-       }
-       if (s->args) {
-               free_decl_args(s->args);
-       }
-       if (s->engine.type && s->engine.dtor) {
-               s->engine.dtor(s->engine.type);
+void psi_decl_struct_free(struct psi_decl_struct **s_ptr)
+{
+       if (*s_ptr) {
+               struct psi_decl_struct *s = *s_ptr;
+
+               *s_ptr = NULL;
+               if (s->token) {
+                       free(s->token);
+               }
+               if (s->args) {
+                       psi_plist_free(s->args);
+               }
+               if (s->engine.type && s->engine.dtor) {
+                       s->engine.dtor(s->engine.type);
+               }
+               free(s->name);
+               free(s);
        }
-       free(s->name);
-       free(s);
 }
 
-void dump_decl_struct(int fd, decl_struct *strct) {
-       dprintf(fd, "struct %s::(%zu, %zu)", strct->name, strct->align, strct->size);
-       if (strct->args && strct->args->count) {
-               dump_decl_args_with_layout(fd, strct->args, 0);
+void psi_decl_struct_dump(int fd, struct psi_decl_struct *strct)
+{
+       dprintf(fd, "struct %s::(%zu, %zu)", strct->name, strct->align,
+                       strct->size);
+       if (psi_plist_count(strct->args)) {
+               psi_decl_type_dump_args_with_layout(fd, strct->args, 0);
        } else {
                dprintf(fd, ";");
        }
 }
 
-decl_arg *locate_decl_struct_member(decl_struct *s, decl_var *var) {
+struct psi_decl_arg *psi_decl_struct_get_arg(struct psi_decl_struct *s,
+               struct psi_decl_var *var)
+{
        if (s->args) {
-               return locate_decl_var_arg(var, s->args, NULL);
+               return psi_decl_arg_get_by_var(var, s->args, NULL);
        }
 
        return NULL;
 }
 
-int validate_decl_struct(struct psi_data *data, decl_struct *s) {
+bool psi_decl_struct_validate(struct psi_data *data, struct psi_decl_struct *s)
+{
        size_t i, pos, len, size, align;
+       struct psi_decl_arg *darg, *prev_arg;
 
-       if (!s->size && !s->args->count) {
+       if (!s->size && !psi_plist_count(s->args)) {
                data->error(data, s->token, PSI_WARNING,
-                               "Cannot compute size of empty struct '%s'",
-                               s->name);
-               return 0;
+                               "Cannot compute size of empty struct '%s'", s->name);
+               return false;
        }
 
-       for (i = 0; i < s->args->count; ++i) {
-               decl_arg *darg = s->args->args[i];
+       for (i = 0; psi_plist_get(s->args, i, &darg); ++i) {
+               darg->var->arg = darg;
 
-               if (!validate_decl_arg(data, darg)) {
-                       return 0;
+               if (!psi_decl_arg_validate(data, darg)) {
+                       return false;
                }
 
-               assert(!darg->var->arg || darg->var->arg == darg);
-
-               darg->var->arg = darg;
-
-               if (!validate_decl_arg_args(data, darg, s)) {
-                       return 0;
-               } else if (darg->layout) {
+               if (darg->layout) {
                        pos = darg->layout->pos;
-
-                       align = align_decl_arg(darg, &pos, &len);
+                       align = psi_decl_arg_align(darg, &pos, &len);
 
                        if (darg->layout->len != len) {
                                data->error(data, darg->token, PSI_WARNING,
@@ -115,27 +113,26 @@ int validate_decl_struct(struct psi_data *data, decl_struct *s) {
                                                " pre-defined offset %zu",
                                                pos, s->name, darg->var->name, darg->layout->pos);
                        }
-               } else  {
+               } else {
                        if (i) {
-                               pos = s->args->args[i-1]->layout->pos +
-                                               s->args->args[i-1]->layout->len;
+                               pos = prev_arg->layout->pos + prev_arg->layout->len;
                        } else {
                                pos = 0;
                        }
 
-                       align = align_decl_arg(darg, &pos, &len);
-                       darg->layout = init_decl_struct_layout(pos, len);
+                       align = psi_decl_arg_align(darg, &pos, &len);
+                       darg->layout = psi_layout_init(pos, len);
                }
 
                if (align > s->align) {
                        s->align = align;
                }
+               prev_arg = darg;
        }
 
-       sort_decl_args(s->args);
-
-       if (s->args->count) {
-               decl_arg *darg = s->args->args[s->args->count-1];
+       if (psi_plist_count(s->args)) {
+               psi_plist_sort(s->args, psi_layout_sort_cmp, NULL);
+               psi_plist_get(s->args, psi_plist_count(s->args) - 1, &darg);
 
                size = darg->layout->pos + darg->layout->len;
                if (s->size < size) {
@@ -143,12 +140,13 @@ int validate_decl_struct(struct psi_data *data, decl_struct *s) {
                }
        }
 
-       return 1;
+       return true;
 }
 
-size_t alignof_decl_struct(decl_struct *s) {
+size_t psi_decl_struct_get_align(struct psi_decl_struct *s)
+{
        if (!s->align) {
-               s->align = alignof_decl_args(s->args);
+               s->align = psi_decl_type_get_args_align(s->args);
        }
        return s->align;
 }