flush
[m6w6/ext-psi] / src / validator.c
index 19956b73f475079183c1697632062b39933abf47..7bc2f701989c698c63a0e491f4031ab38b30dd19 100644 (file)
@@ -6,6 +6,8 @@
 
 #include <jit/jit.h>
 
+#include "php.h"
+#include "php_psi.h"
 #include "validator.h"
 
 PSI_Validator *PSI_ValidatorInit(PSI_Validator *V, PSI_Parser *P)
@@ -90,21 +92,15 @@ static inline int locate_decl_type_struct(decl_structs *structs, decl_type *type
        }
        return 0;
 }
-static inline int validate_decl_type(PSI_Validator *V, decl_arg *arg, decl_type *type) {
+static inline int validate_decl_type(PSI_Validator *V, decl_type *type) {
        switch (type->type) {
        case PSI_T_NAME:
                if (!V->defs || !locate_decl_type_alias(V->defs, type)) {
-                       V->error(PSI_WARNING, "Cannot use '%s' as type for '%s';"
-                               " Use 'typedef <type> <alias>;' statement",
-                               type->name, arg->var->name);
                        return 0;
                }
-               return validate_decl_type(V, arg, type->real);
+               return validate_decl_type(V, type->real);
        case PSI_T_STRUCT:
                if (!V->structs || !locate_decl_type_struct(V->structs, type)) {
-                       V->error(PSI_WARNING, "Cannot use '%s' as type for '%s';"
-                               " Use 'typedef struct <name> <alias>;' statement",
-                               type->name, arg->var->name);
                        return 0;
                }
                break;
@@ -131,24 +127,6 @@ static inline int validate_typedefs(PSI_Validator *V) {
 
        return 1;
 }
-static inline int validate_decl_func(PSI_Validator *V, decl *decl, decl_arg *func)
-{
-       if (!strcmp(func->var->name, "dlsym")) {
-               V->error(PSI_WARNING, "Cannot dlsym dlsym (sic!)");
-               return 0;
-       }
-
-       if (!validate_decl_type(V, func, func->type)) {
-               return 0;
-       }
-
-       decl->dlptr = dlsym(V->dlopened ?: RTLD_NEXT, func->var->name);
-       if (!decl->dlptr) {
-               V->error(PSI_WARNING, "Failed to located symbol '%s': %s",
-                       func->var->name, dlerror());
-       }
-       return 1;
-}
 static const char * const abi_ccs[] = {
                "default", /* \                 */
                "extern",  /*  > - all the same */
@@ -159,7 +137,7 @@ static const char * const abi_ccs[] = {
 static inline int validate_decl_abi(PSI_Validator *V, decl_abi *abi) {
        size_t i;
 
-       for (i = 0; i < sizeof(abi_ccs)/sizeof(char*); ++ i) {
+       for (i = 0; i < sizeof(abi_ccs)/sizeof(char*); ++i) {
                if (strcasecmp(abi->convention, abi_ccs[i])) {
                        return 1;
                }
@@ -168,7 +146,9 @@ static inline int validate_decl_abi(PSI_Validator *V, decl_abi *abi) {
        return 0;
 }
 static inline int validate_decl_arg(PSI_Validator *V, decl_arg *arg) {
-       if (!validate_decl_type(V, arg, arg->type)) {
+       if (!validate_decl_type(V, arg->type)) {
+               V->error(PSI_WARNING, "Cannot use '%s' as type for '%s'",
+                       arg->type->name, arg->var->name);
                return 0;
        }
        return 1;
@@ -183,6 +163,26 @@ static inline int validate_decl_args(PSI_Validator *V, decl_args *args) {
        }
        return 1;
 }
+static inline int validate_decl_func(PSI_Validator *V, decl *decl, decl_arg *func)
+{
+       if (!strcmp(func->var->name, "dlsym")) {
+               V->error(PSI_WARNING, "Cannot dlsym dlsym (sic!)");
+               return 0;
+       }
+
+       if (!validate_decl_arg(V, func)) {
+               return 0;
+       }
+#ifndef RTLD_NEXT
+# define RTLD_NEXT ((void *) -1l)
+#endif
+       decl->dlptr = dlsym(V->dlopened ?: RTLD_NEXT, func->var->name);
+       if (!decl->dlptr) {
+               V->error(PSI_WARNING, "Failed to located symbol '%s': %s",
+                       func->var->name, dlerror());
+       }
+       return 1;
+}
 static inline int validate_decl(PSI_Validator *V, decl *decl) {
        if (!validate_decl_abi(V, decl->abi)) {
                return 0;
@@ -214,15 +214,17 @@ static inline int validate_struct(PSI_Validator *V, decl_struct *s) {
 
        s->layout = calloc(s->args->count, sizeof(*s->layout));
        for (i = 0; i < s->args->count; ++i) {
-               decl_type *t = real_decl_type(s->args->args[i]->type);
+               decl_arg *darg = s->args->args[i];
+               decl_type *type = real_decl_type(darg->type);
+               token_t t = darg->var->pointer_level ? PSI_T_POINTER : type->type;
 
                if (i) {
                        decl_struct_layout *l = &s->layout[i-1];
-                       s->layout[i].pos = psi_t_align(t->type, l->pos + l->size);
+                       s->layout[i].pos = psi_t_align(t, l->pos + l->len);
                } else {
                        s->layout[i].pos = 0;
                }
-               s->layout[i].len = psi_t_size(t->type);
+               s->layout[i].len = psi_t_size(t);
        }
        return 1;
 }
@@ -230,7 +232,7 @@ static inline int validate_structs(PSI_Validator *V) {
        size_t i;
 
        for (i = 0; i < V->structs->count; ++i) {
-               if (!validate_struct(V->structs->list[i])) {
+               if (!validate_struct(V, V->structs->list[i])) {
                        return 0;
                }
        }
@@ -294,7 +296,7 @@ static inline int validate_impl_stmts(PSI_Validator *V, impl *impl, impl_stmts *
        if (stmts->ret.count != 1) {
                if (stmts->ret.count > 1) {
                        V->error(PSI_WARNING, "Too many `ret` statements for implmentation %s;"
-                                       "found %zu, exactly one is needed",
+                                       " found %zu, exactly one is needed",
                                        impl->func->name, stmts->ret.count);
                } else {
                        V->error(PSI_WARNING, "Missing `ret` statement for implementation %s",
@@ -312,7 +314,7 @@ static inline int validate_impl_stmts(PSI_Validator *V, impl *impl, impl_stmts *
        }
 
        /* check that we have a let stmt for every decl arg */
-       for (i = 0; i < decl->args->count; ++i) {
+       if (decl->args) for (i = 0; i < decl->args->count; ++i) {
                decl_arg *darg = decl->args->args[i];
                int check = 0;
 
@@ -327,7 +329,7 @@ static inline int validate_impl_stmts(PSI_Validator *V, impl *impl, impl_stmts *
                }
                if (!check) {
                        V->error(PSI_WARNING, "Missing `let` statement for arg '%s %.*s%s'"
-                                       "of declaration '%s' for implementation '%s'",
+                                       " of declaration '%s' for implementation '%s'",
                                        darg->type->name, (int) darg->var->pointer_level, "*****",
                                        darg->var->name, decl->func->var->name, impl->func->name);
                        return 0;
@@ -338,8 +340,15 @@ static inline int validate_impl_stmts(PSI_Validator *V, impl *impl, impl_stmts *
                let_stmt *let = stmts->let.list[i];
                int check = 0;
 
+               if (let->val && let->val->func && let->val->func->alloc) {
+                       if (!validate_decl_type(V, let->val->func->alloc->type)) {
+                               V->error(PSI_WARNING, "Cannot use '%s' as type for calloc in `let` statement",
+                                       let->val->func->alloc->type->name);
+                               return 0;
+                       }
+               }
                if (let->val && let->val->var) {
-                       for (j = 0; j < impl->func->args->count; ++j) {
+                       if (impl->func->args) for (j = 0; j < impl->func->args->count; ++j) {
                                impl_arg *iarg = impl->func->args->args[j];
 
                                if (!strcmp(let->val->var->name, iarg->var->name)) {
@@ -361,7 +370,7 @@ static inline int validate_impl_stmts(PSI_Validator *V, impl *impl, impl_stmts *
                set_stmt *set = stmts->set.list[i];
                int check = 0;
 
-               for (j = 0; j < impl->func->args->count; ++j) {
+               if (impl->func->args) for (j = 0; j < impl->func->args->count; ++j) {
                        impl_arg *iarg = impl->func->args->args[j];
 
                        if (!strcmp(set->var->name, iarg->var->name)) {
@@ -381,7 +390,7 @@ static inline int validate_impl_stmts(PSI_Validator *V, impl *impl, impl_stmts *
                        decl_var *set_var = set->val->vars->vars[j];
 
                        check = 0;
-                       for (k = 0; k < decl->args->count; ++k) {
+                       if (decl->args) for (k = 0; k < decl->args->count; ++k) {
                                decl_arg *set_arg = decl->args->args[k];
 
                                if (!strcmp(set_var->name, set_arg->var->name)) {
@@ -410,7 +419,7 @@ static inline int validate_impl_stmts(PSI_Validator *V, impl *impl, impl_stmts *
                        if (!strcmp(free_var->name, decl->func->var->name)) {
                                continue;
                        }
-                       for (k = 0; k < decl->args->count; ++k) {
+                       if (decl->args) for (k = 0; k < decl->args->count; ++k) {
                                decl_arg *free_arg = decl->args->args[k];
 
                                if (!strcmp(free_var->name, free_arg->var->name)) {