flush
[m6w6/ext-psi] / src / context_validate.c
index da760c64afc19446994fa5ee395cbc78c538c20c..7c7a245abee60d27e54581163a583ecbe297971b 100644 (file)
@@ -1,4 +1,4 @@
-#ifdef HAVE_CONFIG_H
+       #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 
@@ -48,10 +48,10 @@ static inline int locate_decl_type_alias(decl_typedefs *defs, decl_type *type) {
        if (type->real) {
                return 1;
        }
-       for (i = 0; i < defs->count; ++i) {
-               decl_typedef *def = defs->list[i];
+       if (defs) for (i = 0; i < defs->count; ++i) {
+               decl_arg *def = defs->list[i];
 
-               if (def->type->type != type->type && !strcmp(def->alias, type->name)) {
+               if (def->type->type != type->type && !strcmp(def->var->name, type->name)) {
                        type->real = def->type;
                        return 1;
                }
@@ -65,13 +65,14 @@ static inline int locate_decl_type_alias(decl_typedefs *defs, decl_type *type) {
 
        return 0;
 }
+
 static inline int locate_decl_type_struct(decl_structs *structs, decl_type *type) {
        size_t i;
 
        if (type->strct) {
                return 1;
        }
-       for (i = 0; i < structs->count; ++i) {
+       if (structs) for (i = 0; i < structs->count; ++i) {
                if (!strcmp(structs->list[i]->name, type->name)) {
                        type->strct = structs->list[i];
                        return 1;
@@ -80,6 +81,21 @@ static inline int locate_decl_type_struct(decl_structs *structs, decl_type *type
        return 0;
 }
 
+static inline int locate_decl_type_enum(decl_enums *enums, decl_type *type) {
+       size_t i;
+
+       if (type->enm) {
+               return 1;
+       }
+       if (enums) for (i = 0; i < enums->count; ++i) {
+               if (!strcmp(enums->list[i]->name, type->name)) {
+                       type->enm = enums->list[i];
+                       return 1;
+               }
+       }
+       return 0;
+}
+
 static inline int validate_decl_type(PSI_Data *data, decl_type *type) {
        switch (type->type) {
        case PSI_T_CHAR:
@@ -87,7 +103,7 @@ static inline int validate_decl_type(PSI_Data *data, decl_type *type) {
        case PSI_T_INT:
        case PSI_T_LONG:
        case PSI_T_NAME:
-               if (!data->defs || !locate_decl_type_alias(data->defs, type)) {
+               if (!locate_decl_type_alias(data->defs, type)) {
                        return 0;
                }
                if (type->real) {
@@ -95,21 +111,25 @@ static inline int validate_decl_type(PSI_Data *data, decl_type *type) {
                }
                return 1;
        case PSI_T_STRUCT:
-               if (!data->structs || !locate_decl_type_struct(data->structs, type)) {
+               if (!locate_decl_type_struct(data->structs, type)) {
                        return 0;
                }
                break;
+       case PSI_T_ENUM:
+               if (!locate_decl_type_enum(data->enums, type)) {
+                       return 0;
+               }
        }
        return 1;
 }
-static inline int validate_decl_typedef(PSI_Data *data, decl_typedef *def) {
+static inline int validate_decl_typedef(PSI_Data *data, decl_arg *def) {
        if (!validate_decl_type(data, def->type)) {
                data->error(def->token, PSI_WARNING,
                        "Type '%s' cannot be aliased to %s'%s'",
-                       def->type->name, def->type->type == PSI_T_STRUCT?"struct ":"",def->alias);
+                       def->type->name, def->type->type == PSI_T_STRUCT?"struct ":"",
+                       def->var->name);
                return 0;
        }
-       /* FIXME: check def->alias */
        return 1;
 }
 
@@ -202,6 +222,7 @@ static inline int validate_decl_struct(PSI_Data *data, decl_struct *s) {
                        }
                } else {
                        token_t t;
+                       size_t size, align;
 
                        if (darg->var->pointer_level && (!darg->var->array_size || darg->var->pointer_level == 1)) {
                                t = PSI_T_POINTER;
@@ -209,17 +230,31 @@ static inline int validate_decl_struct(PSI_Data *data, decl_struct *s) {
                                t = real_decl_type(darg->type)->type;
                        }
 
+                       switch (t) {
+                       case PSI_T_STRUCT:
+                               if (!validate_decl_struct(data, real_decl_type(darg->type)->strct)) {
+                                       return 0;
+                               }
+                               size = real_decl_type(darg->type)->strct->size;
+                               break;
+                       default:
+                               size = psi_t_size(t) * (darg->var->array_size ?: 1);
+                               break;
+                       }
+
                        if (i) {
                                decl_arg *last = s->args->args[i-1];
-                               darg->layout = init_decl_struct_layout(
-                                               psi_t_align(t, last->layout->pos + last->layout->len),
-                                               psi_t_size(t) * darg->var->array_size);
+
+                               align = psi_t_align(t, last->layout->pos + last->layout->len);
                        } else {
-                               darg->layout = init_decl_struct_layout(0, psi_t_size(t));
+                               align = 0;
                        }
+
+                       darg->layout = init_decl_struct_layout(align, size);
                }
                if (s->size < darg->layout->pos + darg->layout->len) {
                        s->size = darg->layout->pos + darg->layout->len;
+                       /* FIXME: align struct */
                }
        }
 
@@ -300,7 +335,7 @@ static inline int validate_decl(PSI_Data *data, void *dl, decl *decl) {
 static inline decl_arg *locate_decl_var_arg(decl_var *var, decl_args *args, decl_arg *func) {
        size_t i;
 
-       for (i = 0; i < args->count; ++i) {
+       if (args) for (i = 0; i < args->count; ++i) {
                decl_arg *arg = args->args[i];
 
                if (!strcmp(var->name, arg->var->name)) {
@@ -335,7 +370,35 @@ static inline constant *locate_num_exp_constant(num_exp *exp, constants *consts)
 
        return NULL;
 }
-static inline int validate_num_exp(PSI_Data *data, decl_args *dargs, decl_arg *func, num_exp *exp) {
+static inline decl_enum_item *locate_num_exp_enum_item_ex(num_exp *exp, decl_enum *e) {
+       size_t k;
+
+       if (e) for (k = 0; k < e->items->count; ++k) {
+               decl_enum_item *i = e->items->list[k];
+
+               if (!strcmp(i->name, exp->u.dvar->name)) {
+                       free_decl_var(exp->u.dvar);
+                       exp->t = PSI_T_ENUM;
+                       exp->u.enm = i;
+                       return i;
+               }
+       }
+       return NULL;
+}
+static inline decl_enum_item *locate_num_exp_enum_item(num_exp *exp, decl_enums *enums) {
+       size_t j;
+
+       if (enums) for (j = 0; j < enums->count; ++j) {
+               decl_enum *e = enums->list[j];
+               decl_enum_item *i = locate_num_exp_enum_item_ex(exp, e);
+
+               if (i) {
+                       return i;
+               }
+       }
+       return NULL;
+}
+static inline int validate_num_exp(PSI_Data *data, num_exp *exp, decl_args *dargs, decl_arg *func, decl_enum *enm) {
        if (exp->operand) {
                switch (exp->operator) {
                case PSI_T_PLUS:
@@ -352,16 +415,18 @@ static inline int validate_num_exp(PSI_Data *data, decl_args *dargs, decl_arg *f
                        break;
                EMPTY_SWITCH_DEFAULT_CASE();
                }
-               if (!validate_num_exp(data, dargs, func, exp->operand)) {
+               if (!validate_num_exp(data, exp->operand, dargs, func, enm)) {
                        return 0;
                }
        }
        switch (exp->t) {
        case PSI_T_NAME:
                if (!locate_decl_var_arg(exp->u.dvar, dargs, func)) {
-                       data->error(exp->token, PSI_WARNING, "Unknown variable '%s' in numeric expression",
-                                       exp->u.dvar->name);
-                       return 0;
+                       if (!locate_num_exp_enum_item(exp, data->enums) && !locate_num_exp_enum_item_ex(exp, enm)) {
+                               data->error(exp->token, PSI_WARNING, "Unknown variable '%s' in numeric expression",
+                                               exp->u.dvar->name);
+                               return 0;
+                       }
                }
                return 1;
        case PSI_T_NSNAME:
@@ -372,11 +437,45 @@ static inline int validate_num_exp(PSI_Data *data, decl_args *dargs, decl_arg *f
                }
                return 1;
        case PSI_T_NUMBER:
+       case PSI_T_ENUM:
                return 1;
        default:
                return 0;
        }
 }
+
+static inline int validate_decl_enum(PSI_Data *data, decl_enum *e) {
+       size_t j;
+
+       if (!e->items || !e->items->count) {
+               data->error(e->token, PSI_WARNING, "Empty enum '%s'", e->name);
+               return 0;
+       }
+
+       for (j = 0; j < e->items->count; ++j) {
+               decl_enum_item *i = e->items->list[j];
+
+               if (!i->num) {
+                       if (j) {
+                               i->inc.t = PSI_T_NUMBER;
+                               i->inc.u.numb = "1";
+                               i->inc.operator = PSI_T_PLUS;
+                               i->inc.operand = i->prev->num ?: &i->prev->inc;
+                               i->num = &i->inc;
+                       } else {
+                               i->inc.t = PSI_T_NUMBER;
+                               i->inc.u.numb = "0";
+                               i->num = &i->inc;
+                       }
+               }
+               if (!validate_num_exp(data, i->num, NULL, NULL, e)) {
+                       return 0;
+               }
+       }
+
+       return 1;
+}
+
 static inline int validate_set_value_handler(set_value *set) {
        switch (set->func->type) {
        case PSI_T_TO_BOOL:
@@ -460,7 +559,7 @@ static inline int validate_set_value_ex(PSI_Data *data, set_value *set, decl_arg
                }
        }
        if (set->num) {
-               if (!validate_num_exp(data, ref_list, ref, set->num)) {
+               if (!validate_num_exp(data, set->num, ref_list, ref, NULL)) {
                        return 0;
                }
        }
@@ -605,15 +704,15 @@ static inline int validate_impl_let_stmts(PSI_Data *data, impl *impl) {
                                                        let->var->array_size));
                        break;
                case PSI_LET_NUMEXP:
-                       if (!validate_num_exp(data, impl->decl->args, impl->decl->func, let->val->data.num)) {
+                       if (!validate_num_exp(data, let->val->data.num, impl->decl->args, impl->decl->func, NULL)) {
                                return 0;
                        }
                        break;
                case PSI_LET_CALLOC:
-                       if (!validate_num_exp(data, impl->decl->args, impl->decl->func, let->val->data.alloc->nmemb)) {
+                       if (!validate_num_exp(data, let->val->data.alloc->nmemb, impl->decl->args, impl->decl->func, NULL)) {
                                return 0;
                        }
-                       if (!validate_num_exp(data, impl->decl->args, impl->decl->func, let->val->data.alloc->size)) {
+                       if (!validate_num_exp(data, let->val->data.alloc->size, impl->decl->args, impl->decl->func, NULL)) {
                                return 0;
                        }
                        break;
@@ -846,6 +945,7 @@ static inline int validate_impl_args(PSI_Data *data, impl *impl) {
 
        return 1;
 }
+
 static inline int validate_impl(PSI_Data *data, impl *impl) {
        if (!validate_impl_args(data, impl)) {
                return 0;
@@ -858,32 +958,89 @@ int PSI_ContextValidate(PSI_Context *C, PSI_Parser *P)
 {
        PSI_Data *D;
        void *dlopened = NULL;
-       size_t count = C->count++;
+       size_t i, count = C->count++, check_round, check_count;
+       decl_typedefs *check_defs = P->defs;
+       decl_structs *check_structs = P->structs;
+       decl_enums *check_enums = P->enums;
 
        C->data = realloc(C->data, C->count * sizeof(*C->data));
        D = PSI_DataExchange(&C->data[count], PSI_DATA(P));
-
+/*
        if (D->defs) {
-               size_t i;
-
                for (i = 0; i < D->defs->count; ++i) {
                        if (validate_decl_typedef(PSI_DATA(C), D->defs->list[i])) {
                                C->defs = add_decl_typedef(C->defs, D->defs->list[i]);
+                       } else {
+                               check_defs = add_decl_typedef(check_defs, D->defs->list[i]);
                        }
                }
        }
        if (D->structs) {
-               size_t i;
-
                for (i = 0; i < D->structs->count; ++i) {
                        if (validate_decl_struct(PSI_DATA(C), D->structs->list[i])) {
                                C->structs = add_decl_struct(C->structs, D->structs->list[i]);
+                       } else {
+                               check_structs = add_decl_struct(check_structs, D->structs->list[i]);
                        }
                }
        }
-       if (D->consts) {
-               size_t i;
+       if (D->enums) {
+               for (i = 0; i < D->enums->count; ++i) {
+                       if (validate_decl_enum(PSI_DATA(C), D->enums->list[i])) {
+                               C->enums = add_decl_enum(C->enums, D->enums->list[i]);
+                       } else {
+                               check_enums = add_decl_enum(check_enums, D->enums->list[i]);
+                       }
+               }
+       }
+*/
+#define REVALIDATE(what) do { \
+               if (check_round && check_ ##what) { \
+                       free(check_ ##what->list); \
+                       free(check_ ##what); \
+               } \
+               check_ ##what = recheck_ ##what; \
+} while (0)
+#define CHECK_TOTAL (CHECK_COUNT(defs) + CHECK_COUNT(structs) + CHECK_COUNT(enums))
+#define CHECK_COUNT(of) (check_ ##of ? check_ ##of->count : 0)
+
+       for (check_round = 0, check_count = 0; CHECK_TOTAL && check_count != CHECK_TOTAL; ++check_round) {
+               decl_typedefs *recheck_defs = NULL;
+               decl_structs *recheck_structs = NULL;
+               decl_enums *recheck_enums = NULL;
+
+               check_count = CHECK_TOTAL;
+               fprintf(stderr, "### Validation round %zu with %zu checks\n", check_round, check_count);
+
+               for (i = 0; i < CHECK_COUNT(defs); ++i) {
+                       if (validate_decl_typedef(PSI_DATA(C), check_defs->list[i])) {
+                               C->defs = add_decl_typedef(C->defs, check_defs->list[i]);
+                       } else {
+                               recheck_defs = add_decl_typedef(recheck_defs, check_defs->list[i]);
+                       }
+               }
+               for (i = 0; i < CHECK_COUNT(structs); ++i) {
+                       if (validate_decl_struct(PSI_DATA(C), check_structs->list[i])) {
+                               C->structs = add_decl_struct(C->structs, check_structs->list[i]);
+                       } else {
+                               recheck_structs = add_decl_struct(recheck_structs, check_structs->list[i]);
+                       }
+               }
+               for (i = 0; i < CHECK_COUNT(enums); ++i) {
+                       if (validate_decl_enum(PSI_DATA(C), check_enums->list[i])) {
+                               C->enums = add_decl_enum(C->enums, check_enums->list[i]);
+                       } else {
+                               recheck_enums = add_decl_enum(recheck_enums, check_enums->list[i]);
+                       }
+               }
+
+               REVALIDATE(defs);
+               REVALIDATE(structs);
+               REVALIDATE(enums);
+       }
+
 
+       if (D->consts) {
                for (i = 0; i < D->consts->count; ++i) {
                        if (validate_constant(PSI_DATA(C), D->consts->list[i])) {
                                C->consts = add_constant(C->consts, D->consts->list[i]);
@@ -898,8 +1055,6 @@ int PSI_ContextValidate(PSI_Context *C, PSI_Parser *P)
        add_decl_lib(&C->psi.libs, dlopened);
 
        if (D->decls) {
-               size_t i;
-
                for (i = 0; i < D->decls->count; ++i) {
                        if (validate_decl(PSI_DATA(C), dlopened, D->decls->list[i])) {
                                C->decls = add_decl(C->decls, D->decls->list[i]);
@@ -907,8 +1062,6 @@ int PSI_ContextValidate(PSI_Context *C, PSI_Parser *P)
                }
        }
        if (D->impls) {
-               size_t i;
-
                for (i = 0; i < D->impls->count; ++i) {
                        if (validate_impl(PSI_DATA(C), D->impls->list[i])) {
                                C->impls = add_impl(C->impls, D->impls->list[i]);
@@ -919,39 +1072,82 @@ int PSI_ContextValidate(PSI_Context *C, PSI_Parser *P)
        return 1;
 }
 
-void PSI_ContextValidatePredef(PSI_Context *C, PSI_Data *D)
+int PSI_ContextValidateData(PSI_Data *dest, PSI_Data *source)
 {
        size_t i;
+       int errors = 0;
+
+       if (source->defs) for (i = 0; i < source->defs->count; ++i) {
+               decl_arg *def = source->defs->list[i];
+
+               if (validate_decl_typedef(source, def)) {
+                       if (dest) {
+                               dest->defs = add_decl_typedef(dest->defs, def);
+                       }
+               } else {
+                       ++errors;
+               }
+       }
+
+       if (source->consts) for (i = 0; i < source->consts->count; ++i) {
+               constant *constant = source->consts->list[i];
+
+               if (validate_constant(source, constant)) {
+                       if (dest) {
+                               dest->consts = add_constant(dest->consts, constant);
+                       }
+               } else {
+                       ++errors;
+               }
+       }
 
-       for (i = 0; i < D->defs->count; ++i) {
-               decl_typedef *def = D->defs->list[i];
+       if (source->structs) for (i = 0; i < source->structs->count; ++i) {
+               decl_struct *dstruct = source->structs->list[i];
 
-               if (validate_decl_typedef(D, def)) {
-                       C->defs = add_decl_typedef(C->defs, def);
+               if (validate_decl_struct(source, dstruct)) {
+                       if (dest) {
+                               dest->structs = add_decl_struct(dest->structs, dstruct);
+                       }
+               } else {
+                       ++errors;
                }
        }
 
-       for (i = 0; i < D->consts->count; ++i) {
-               constant *constant = D->consts->list[i];
+       if (source->enums) for (i = 0; i < source->enums->count; ++i) {
+               decl_enum *denum = source->enums->list[i];
 
-               if (validate_constant(D, constant)) {
-                       C->consts = add_constant(C->consts, constant);
+               if (validate_decl_enum(source, denum)) {
+                       if (dest) {
+                               dest->enums = add_decl_enum(dest->enums, denum);
+                       }
+               } else {
+                       ++errors;
                }
        }
 
-       for (i = 0; i < D->structs->count; ++i) {
-               decl_struct *dstruct = D->structs->list[i];
+       if (source->decls) for (i = 0; i < source->decls->count; ++i) {
+               decl *decl = source->decls->list[i];
 
-               if (validate_decl_struct(D, dstruct)) {
-                       C->structs = add_decl_struct(C->structs, dstruct);
+               if (validate_decl(source, NULL, decl)) {
+                       if (dest) {
+                               dest->decls = add_decl(dest->decls, decl);
+                       }
+               } else {
+                       ++errors;
                }
        }
 
-       for (i = 0; i < D->decls->count; ++i) {
-               decl *decl = D->decls->list[i];
+       if (source->impls) for (i = 0; i < source->impls->count; ++i) {
+               impl *impl = source->impls->list[i];
 
-               if (validate_decl(D, NULL, decl)) {
-                       C->decls = add_decl(C->decls, decl);
+               if (validate_impl(source, impl)) {
+                       if (dest) {
+                               dest->impls = add_impl(dest->impls, impl);
+                       }
+               } else {
+                       ++errors;
                }
        }
+
+       return errors;
 }