fix enum dump; fix float formats; fix dumps
[m6w6/ext-psi] / src / types / decl_type.c
index f3b500a935f5c5127142f81b2137b0955ca5c80c..1445c353ebe2ec8fa719498d9616114473bc836f 100644 (file)
 #define PSI_STD_TYPES
 #include "php_psi_posix.h"
 
-struct psi_decl_type *psi_decl_type_init(token_t type, const char *name)
+struct psi_decl_type *psi_decl_type_init(token_t type, zend_string *name)
 {
-       struct psi_decl_type *t = calloc(1, sizeof(*t));
+       struct psi_decl_type *t = pecalloc(1, sizeof(*t), 1);
        t->type = type;
-       t->name = strdup(name);
+       t->name = zend_string_copy(name);
        return t;
 }
 
@@ -44,17 +44,29 @@ void psi_decl_type_free(struct psi_decl_type **type_ptr)
                struct psi_decl_type *type = *type_ptr;
 
                *type_ptr = NULL;
-               if (type->token) {
-                       free(type->token);
-               }
+               psi_token_free(&type->token);
                if (type->type == PSI_T_FUNCTION) {
                        psi_decl_free(&type->real.func);
                }
-               free(type->name);
+               zend_string_release(type->name);
                free(type);
        }
 }
 
+struct psi_decl_type *psi_decl_type_copy(struct psi_decl_type *src)
+{
+       struct psi_decl_type *dst = pecalloc(1, sizeof(*dst), 1);
+
+       dst->type = src->type;
+       if (src->name) {
+               dst->name = zend_string_copy(src->name);
+       }
+       if (src->token) {
+               dst->token = psi_token_copy(src->token);
+       }
+       return dst;
+}
+
 struct psi_plist *psi_decl_type_get_args(struct psi_decl_type *dtyp,
                struct psi_decl_type **real_typ_ptr)
 {
@@ -87,9 +99,9 @@ size_t psi_decl_type_get_size(struct psi_decl_type *dtyp,
 
        switch (var_typ->type) {
        case PSI_T_STRUCT:
-               return var_typ->real.strct->size;
+               return var_typ->real.strct ? var_typ->real.strct->size : 0;
        case PSI_T_UNION:
-               return var_typ->real.unn->size;
+               return var_typ->real.unn ? var_typ->real.unn->size : 0;
        default:
                return psi_t_size(var_typ->type);
        }
@@ -104,16 +116,16 @@ bool psi_decl_type_get_alias(struct psi_decl_type *type, struct psi_plist *defs)
        if (type->real.def) {
                return true;
        }
-       if (defs)
+       if (defs) {
                while (psi_plist_get(defs, i++, &def)) {
-                       if (def->type->type != type->type
-                                       && !strcmp(def->var->name, type->name)) {
+                       if (zend_string_equals(def->var->name, type->name)) {
                                type->real.def = def;
                                return true;
                        }
                }
+       }
        for (stdtyp = &psi_std_types[0]; stdtyp->type_tag; ++stdtyp) {
-               if (!strcmp(type->name, stdtyp->alias ?: stdtyp->type_name)) {
+               if (!strcmp(type->name->val, stdtyp->alias ?: stdtyp->type_name)) {
                        type->type = stdtyp->type_tag;
                        return true;
                }
@@ -132,7 +144,7 @@ bool psi_decl_type_get_struct(struct psi_decl_type *type, struct psi_plist *stru
        }
        if (structs) {
                while (psi_plist_get(structs, i++, &s)) {
-                       if (!strcmp(s->name, type->name)) {
+                       if (zend_string_equals(s->name, type->name)) {
                                type->real.strct = s;
                                return true;
                        }
@@ -151,7 +163,7 @@ bool psi_decl_type_get_union(struct psi_decl_type *type, struct psi_plist *union
        }
        if (unions) {
                while (psi_plist_get(unions, i++, &u)) {
-                       if (!strcmp(u->name, type->name)) {
+                       if (zend_string_equals(u->name, type->name)) {
                                type->real.unn = u;
                                return true;
                        }
@@ -170,7 +182,7 @@ bool psi_decl_type_get_enum(struct psi_decl_type *type, struct psi_plist *enums)
        }
        if (enums) {
                while (psi_plist_get(enums, i++, &e)) {
-                       if (!strcmp(e->name, type->name)) {
+                       if (zend_string_equals(e->name, type->name)) {
                                type->real.enm = e;
                                return true;
                        }
@@ -189,7 +201,7 @@ bool psi_decl_type_get_decl(struct psi_decl_type *type, struct psi_plist *decls)
        }
        if (decls) {
                while (psi_plist_get(decls, i++, &decl)) {
-                       if (!strcmp(decl->func->var->name, type->name)) {
+                       if (zend_string_equals(decl->func->var->name, type->name)) {
                                type->real.func = decl;
                                return true;
                        }
@@ -199,122 +211,153 @@ bool psi_decl_type_get_decl(struct psi_decl_type *type, struct psi_plist *decls)
 }
 
 bool psi_decl_type_validate(struct psi_data *data, struct psi_decl_type *type,
-               struct psi_decl_arg *def)
+               struct psi_decl_arg *def, struct psi_validate_scope *scope)
 {
        if (psi_decl_type_is_weak(type)) {
                if (!psi_decl_type_get_alias(type, data->types)) {
-                       return false;
+                       if (!psi_validate_scope_has_type(scope, type->name)) {
+                               return false;
+                       }
+                       type->real.def = psi_validate_scope_get_type(scope, type->name);
                }
                if (type->real.def) {
-                       return psi_decl_type_validate(data, type->real.def->type,
-                                       type->real.def);
+                       if (!psi_decl_type_validate(data, type->real.def->type,
+                                       type->real.def, scope)) {
+                               return false;
+                       }
+                       if (def) {
+                               def->var->pointer_level += type->real.def->var->pointer_level;
+                       }
                }
                return true;
        }
 
        switch (type->type) {
        case PSI_T_STRUCT:
-               if (!psi_decl_type_get_struct(type, data->structs) && !def) {
-                       data->error(data, type->token, PSI_WARNING,
-                                       "Unknown struct '%s'", type->name);
+               if (!psi_decl_type_get_struct(type, data->structs)) {
+                       if (psi_validate_scope_has_struct(scope, type->name)) {
+                               type->real.strct = psi_validate_scope_get_struct(scope, type->name);
+                       } else if (def && def->var->pointer_level) {
+                               return true;
+                       } else {
+                               data->error(data, type->token, PSI_WARNING,
+                                               "Unknown struct '%s'", type->name->val);
+                               return false;
+                       }
+               }
+               if (!psi_decl_struct_validate(data, type->real.strct, scope)) {
                        return false;
                }
                break;
        case PSI_T_UNION:
-               if (!psi_decl_type_get_union(type, data->unions) && !def) {
-                       data->error(data, type->token, PSI_WARNING,
-                                       "Unknown union '%s'", type->name);
+               if (!psi_decl_type_get_union(type, data->unions)) {
+                       if (psi_validate_scope_has_union(scope, type->name)) {
+                               type->real.unn = psi_validate_scope_get_union(scope, type->name);
+                       } else if (def && def->var->pointer_level) {
+                               return true;
+                       } else {
+                               data->error(data, type->token, PSI_WARNING,
+                                               "Unknown union '%s'", type->name->val);
+                               return false;
+                       }
+               }
+               if (!psi_decl_union_validate(data, type->real.unn, scope)) {
                        return false;
                }
                break;
        case PSI_T_ENUM:
-               if (!psi_decl_type_get_enum(type, data->enums) && !def) {
+               if (!psi_decl_type_get_enum(type, data->enums)) {
                        data->error(data, type->token, PSI_WARNING,
-                                       "Unknown enum '%s'", type->name);
+                                       "Unknown enum '%s'", type->name->val);
                        return false;
                }
                break;
        case PSI_T_FUNCTION:
                if (!psi_decl_type_get_decl(type, data->decls)) {
                        data->error(data, type->token, PSI_WARNING,
-                                       "Unknown decl '%s'", type->name);
+                                       "Unknown decl '%s'", type->name->val);
                        return false;
                }
-               if (!psi_decl_validate_nodl(data, type->real.func)) {
+               if (!psi_decl_validate_nodl(data, type->real.func, scope)) {
                        return false;
                }
                break;
+       default:
+               break;
        }
+
        return true;
 }
 
-void psi_decl_type_dump_args_with_layout(int fd, struct psi_plist *args,
+void psi_decl_type_dump_args_with_layout(struct psi_dump *dump, struct psi_plist *args,
                unsigned level)
 {
        size_t i = 0;
 
-       dprintf(fd, " {\n");
+       PSI_DUMP(dump, " {\n");
        if (args) {
                struct psi_decl_arg *sarg;
 
                ++level;
                while (psi_plist_get(args, i++, &sarg)) {
-                       dprintf(fd, "%s", psi_t_indent(level));
-                       psi_decl_arg_dump(fd, sarg, level);
-                       dprintf(fd, "::(%zu, %zu);\n", sarg->layout->pos,
-                                       sarg->layout->len);
+                       PSI_DUMP(dump, "%s", psi_t_indent(level));
+                       psi_decl_arg_dump(dump, sarg, level);
+                       if (sarg->layout) {
+                               if (sarg->layout->bfw) {
+                                       PSI_DUMP(dump, ":%zu", sarg->layout->bfw->len);
+                               }
+                               PSI_DUMP(dump, "::(%zu, %zu);\n", sarg->layout->pos,
+                                               sarg->layout->len);
+                       } else {
+                               PSI_DUMP(dump, ";\n");
+                       }
                }
                --level;
        }
-       dprintf(fd, "%s", psi_t_indent(level));
-       dprintf(fd, "}");
+       PSI_DUMP(dump, "%s", psi_t_indent(level));
+       PSI_DUMP(dump, "}");
 }
 
-void psi_decl_type_dump(int fd, struct psi_decl_type *t, unsigned level)
+void psi_decl_type_dump(struct psi_dump *dump, struct psi_decl_type *t, unsigned level)
 {
        switch (t->type) {
        case PSI_T_POINTER:
-               dprintf(fd, "%s *", t->name);
+               PSI_DUMP(dump, "%s *", t->name->val);
                return;
 
        case PSI_T_ENUM:
-               dprintf(fd, "enum ");
                if (psi_decl_type_is_anon(t->name, "enum")) {
-                       size_t i = 0, c = psi_plist_count(t->real.enm->items);
-                       struct psi_decl_enum_item *item;
-
-                       dprintf(fd, "{\n");
-                       ++level;
-                       while (psi_plist_get(t->real.enm->items, i++, &item)) {
-                               dprintf(fd, "%s", psi_t_indent(level));
-                               psi_decl_enum_item_dump(fd, item);
-                               if (i < c) {
-                                       dprintf(fd, "%s\n", i < c ? "," : "");
-                               }
-                       }
-                       --level;
-                       dprintf(fd, "%s} ", psi_t_indent(level));
+                       psi_decl_enum_dump(dump, t->real.enm, level);
                        return;
                }
+               PSI_DUMP(dump, "enum ");
                break;
 
        case PSI_T_STRUCT:
-               dprintf(fd, "struct ");
+               PSI_DUMP(dump, "struct ");
                if (psi_decl_type_is_anon(t->name, "struct")) {
-                       psi_decl_type_dump_args_with_layout(fd, t->real.strct->args, level);
+                       psi_decl_type_dump_args_with_layout(dump, t->real.strct->args, level);
                        return;
                }
                break;
 
        case PSI_T_UNION:
-               dprintf(fd, "union ");
+               PSI_DUMP(dump, "union ");
                if (psi_decl_type_is_anon(t->name, "union")) {
-                       psi_decl_type_dump_args_with_layout(fd, t->real.unn->args, level);
+                       psi_decl_type_dump_args_with_layout(dump, t->real.unn->args, level);
                        return;
                }
                break;
+
+       case PSI_T_FUNCTION:
+               psi_decl_type_dump(dump, t->real.func->func->type, level);
+               return;
+
+       default:
+               break;
        }
-       dprintf(fd, "%s", t->name);
+
+       PSI_DUMP(dump, "%s", t->name->val);
 }
 
 int psi_decl_type_is_weak(struct psi_decl_type *type)
@@ -324,6 +367,8 @@ int psi_decl_type_is_weak(struct psi_decl_type *type)
        case PSI_T_SHORT:
        case PSI_T_INT:
        case PSI_T_LONG:
+       case PSI_T_UNSIGNED:
+       case PSI_T_SIGNED:
        case PSI_T_NAME:
                return type->type;
        default:
@@ -354,6 +399,7 @@ size_t psi_decl_type_get_align(struct psi_decl_type *t)
        case PSI_T_ENUM:
        default:
                align = psi_t_alignment(real->type);
+               break;
        }
 
        return align;