From: fennecdjay Date: Sat, 31 Aug 2019 19:08:34 +0000 (+0200) Subject: :art: use get_type X-Git-Tag: nightly~2256 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=b1ba94ac69aa1672ad6be6bad50f842d68cc0bc0;p=gwion.git :art: use get_type --- diff --git a/include/cpy_ast.h b/include/cpy_ast.h index 61ae846f..948a3a4d 100644 --- a/include/cpy_ast.h +++ b/include/cpy_ast.h @@ -8,4 +8,5 @@ ANN Func_Def cpy_func_def(MemPool, Func_Def); ANN struct Func_Base_* cpy_func_base(MemPool, struct Func_Base_*); ANN Class_Def cpy_class_def(MemPool, Class_Def); ANN Type_List cpy_type_list(MemPool p, const Type_List src); +ANN Decl_List cpy_decl_list(MemPool p, const Decl_List src); #endif diff --git a/include/parse.h b/include/parse.h index 6ad1e96f..86a6bdeb 100644 --- a/include/parse.h +++ b/include/parse.h @@ -94,4 +94,7 @@ xxx_cdef(scan1) xxx_cdef(scan2) xxx_cdef(check) xxx_cdef(traverse) + +__attribute__((returns_nonnull)) +ANN Type get_type(const Type t); #endif diff --git a/src/emit/emit.c b/src/emit/emit.c index 7c5bfd6a..2ccbb32c 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -157,7 +157,7 @@ ANN m_uint emit_local(const Emitter emit, const m_uint size, const m_bool is_obj ANN static void emit_pre_ctor(const Emitter emit, const Type type) { if(type->e->parent) emit_pre_ctor(emit, type->e->parent); - if(type->nspc->pre_ctor) + if(type->nspc->pre_ctor && !GET_FLAG(type, nonnull)) emit_ext_ctor(emit, type->nspc->pre_ctor); if(GET_FLAG(type, template) && GET_FLAG(type, builtin)) { const Type t = template_parent(emit->env, type); @@ -707,7 +707,7 @@ ANN static m_bool emit_class_def(const Emitter, const Class_Def); ANN static m_bool emit_cdef(const Emitter, const Class_Def); ANN static inline m_bool emit_exp_decl_template(const Emitter emit, const Exp_Decl* decl) { - const Type t = decl->type; + const Type t = get_type(decl->type); return !GET_FLAG(t, emit) ? emit_cdef(emit, t->e->def) : GW_OK; } diff --git a/src/lib/func.c b/src/lib/func.c index bb431858..ebedcd47 100644 --- a/src/lib/func.c +++ b/src/lib/func.c @@ -260,6 +260,8 @@ ANN Type check_exp_unary_spork(const Env env, const Stmt code); static OP_CHECK(opck_spork) { const Exp_Unary* unary = (Exp_Unary*)data; + if(exp_self(unary)->next) + ERR_O(exp_self(unary)->pos, _("spork/fork must not have next expression")) if(unary->op == insert_symbol("fork") && !unary->fork_ok) ERR_O(exp_self(unary)->pos, _("forks must be stored in a value:\n" "fork xxx @=> Fork f")) diff --git a/src/lib/object.c b/src/lib/object.c index ded0d4d3..e0f103e2 100644 --- a/src/lib/object.c +++ b/src/lib/object.c @@ -59,6 +59,8 @@ ANN void __release(const M_Object o, const VM_Shred shred) { MemPool p = shred->info->mp; Type t = o->type_ref; do { + if(GET_FLAG(t, nonnull)) + t = t->e->parent; struct scope_iter iter = { t->nspc->info->value, 0, 0 };\ Value v; while(scope_iter(&iter, &v) > 0) { diff --git a/src/oo/type.c b/src/oo/type.c index 605c961a..960530dc 100644 --- a/src/oo/type.c +++ b/src/oo/type.c @@ -14,9 +14,12 @@ ANN static void free_type(Type a, Gwion gwion) { if(GET_FLAG(a, template)) { if(GET_FLAG(a, union)) { - if(a->e->def->union_def && !GET_FLAG(a, pure)) { // <=> decl_list + if(a->e->def->union_def) { + if(!GET_FLAG(a, pure)) { // <=> decl_list UNSET_FLAG(a->e->def->union_def, global); free_union_def(gwion->mp, a->e->def->union_def); + } else + free_decl_list(gwion->mp, a->e->def->list); } a->e->def->union_def = NULL; } else if(a->e->def) diff --git a/src/parse/check.c b/src/parse/check.c index a13cd50f..35b17ccd 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -118,8 +118,11 @@ ANN Type check_exp_decl(const Env env, const Exp_Decl* decl) { } if(!decl->type) ERR_O(td_pos(decl->td), _("can't infer type.")); - if(GET_FLAG(decl->type , template) && !GET_FLAG(decl->type, check)) - CHECK_BO(check_cdef(env, decl->type->e->def)) +{ + const Type t = get_type(decl->type); + if(GET_FLAG(t, template) && !GET_FLAG(t, check)) + CHECK_BO(check_cdef(env, t->e->def)) +} const m_bool global = GET_FLAG(decl->td, global); const m_uint scope = !global ? env->scope->depth : env_push_global(env); do { diff --git a/src/parse/cpy_ast.c b/src/parse/cpy_ast.c index 84e3e492..3b16a26e 100644 --- a/src/parse/cpy_ast.c +++ b/src/parse/cpy_ast.c @@ -129,7 +129,8 @@ ANN static void cpy_exp_primary(MemPool p, Exp_Primary *a, const Exp_Primary *sr cpy_vec(p, &a->d.vec, &src->d.vec); break; default: - a->d.exp = cpy_exp(p, src->d.exp); + if(src->d.exp) + a->d.exp = cpy_exp(p, src->d.exp); break; } a->primary_type = src->primary_type; @@ -240,7 +241,7 @@ ANN static Exp cpy_exp(MemPool p, const Exp src) { return a; } -ANN static Decl_List cpy_decl_list(MemPool p, const Decl_List src) { +ANN /*static */Decl_List cpy_decl_list(MemPool p, const Decl_List src) { Decl_List a = mp_calloc(p, Decl_List); a->self = cpy_exp(p, src->self); if(src->next) diff --git a/src/parse/scan0.c b/src/parse/scan0.c index 220b37e2..70de8393 100644 --- a/src/parse/scan0.c +++ b/src/parse/scan0.c @@ -11,6 +11,7 @@ #include "traverse.h" #include "template.h" #include "parse.h" +#include "cpy_ast.h" static inline void add_type(const Env env, const Nspc nspc, const Type t) { map_set(&nspc->info->type->map, (m_uint)insert_symbol(t->name), (m_uint)t); @@ -193,7 +194,7 @@ ANN m_bool scan0_union_def(const Env env, const Union_Def udef) { udef->value->owner = nspc; nspc_add_value(nspc, udef->xid, udef->value); add_type(env, nspc, t); - SET_FLAG(t, scan1); + SET_FLAG(t, scan1 | ae_flag_union); SET_FLAG(udef->value, checked | udef->flag); if(env->class_def && !GET_FLAG(udef, static)) { SET_FLAG(udef->value, member); @@ -205,7 +206,7 @@ ANN m_bool scan0_union_def(const Env env, const Union_Def udef) { env->curr : env->global_nspc; udef->type = union_type(env, nspc, udef->type_xid, 1); SET_FLAG(udef->type, checked); - SET_FLAG(udef->type, scan1); + SET_FLAG(udef->type, scan1 | ae_flag_union); } else { const Nspc nspc = !GET_FLAG(udef, global) ? env->curr : env->global_nspc; @@ -219,7 +220,7 @@ ANN m_bool scan0_union_def(const Env env, const Union_Def udef) { nspc_add_value(nspc, udef->xid, udef->value); add_type(env, nspc, t); SET_FLAG(udef->value, checked | udef->flag); - SET_FLAG(t, scan1); + SET_FLAG(t, scan1 | ae_flag_union); } if(udef->tmpl) { if(tmpl_base(udef->tmpl)) { @@ -228,7 +229,7 @@ ANN m_bool scan0_union_def(const Env env, const Union_Def udef) { udef->type->e->def = cdef; cdef->base.tmpl = udef->tmpl; cdef->base.type = udef->type; - cdef->list = udef->l; + cdef->list = cpy_decl_list(env->gwion->mp, udef->l); SET_FLAG(cdef, union); SET_FLAG(udef->type, pure); SET_FLAG(udef, template); @@ -286,8 +287,7 @@ ANN static m_bool scan0_section(const Env env, const Section* section) { return GW_OK; } -ANN m_bool scan0_class_def(const Env env, const Class_Def cdef) { - CHECK_BB(scan0_class_def_pre(env, cdef)) +ANN static m_bool scan0_class_def_inner(const Env env, const Class_Def cdef) { CHECK_OB((cdef->base.type = scan0_class_def_init(env, cdef))) SET_FLAG(cdef->base.type, scan0); if(cdef->body) { @@ -297,9 +297,15 @@ ANN m_bool scan0_class_def(const Env env, const Class_Def cdef) { if(call)cdef->base.tmpl->call = NULL; } (void)mk_class(env, cdef->base.type); + return GW_OK; +} + +ANN m_bool scan0_class_def(const Env env, const Class_Def cdef) { + CHECK_BB(scan0_class_def_pre(env, cdef)) + const m_bool ret = scan0_class_def_inner(env, cdef); if(GET_FLAG(cdef, global)) env->curr = (Nspc)vector_pop(&env->scope->nspc_stack); - return GW_OK; + return ret; } ANN m_bool scan0_ast(const Env env, Ast ast) { diff --git a/src/parse/scan1.c b/src/parse/scan1.c index a58dddd5..5b7334e9 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -15,11 +15,15 @@ ANN static m_bool scan1_stmt_list(const Env env, Stmt_List list); ANN static m_bool scan1_stmt(const Env env, Stmt stmt); ANN static Type void_type(const Env env, const Type_Decl* td) { - DECL_OO(const Type, t, = known_type_noref(env, td)) - if(t->e->def && !GET_FLAG(t, scan1)) + DECL_OO(const Type, type, = known_type_noref(env, td)) +// if(t->e->def && !GET_FLAG(t, scan1)) +{ + const Type t = get_type(type); + if(GET_FLAG(t, template) && !GET_FLAG(t, scan1)) CHECK_BO(scan1_cdef(env, t->e->def)) - if(t->size) - return t; +} + if(type->size) + return type; ERR_O(td_pos(td), _("cannot declare variables of size '0' (i.e. 'void')...")) } @@ -261,32 +265,42 @@ ANN m_bool scan1_type_def(const Env env, const Type_Def tdef) { return isa(tdef->type, t_fptr) < 0 ? scan1_cdef(env, tdef->type->e->def) : GW_OK; } +ANN m_bool scan1_union_def_action(const Env env, const Union_Def udef, + const Decl_List l) { + const Exp_Decl decl = l->self->d.exp_decl; + SET_FLAG(decl.td, checked | udef->flag); + const m_bool global = GET_FLAG(udef, global); + if(global) + UNSET_FLAG(decl.td, global); + if(GET_FLAG(udef, member)) + SET_FLAG(decl.td, member); + else if(GET_FLAG(udef, static)) + SET_FLAG(decl.td, static); + CHECK_BB(scan1_exp(env, l->self)) + if(global) + SET_FLAG(decl.td, global); + return GW_OK; +} + +ANN m_bool scan1_union_def_inner(const Env env, const Union_Def udef) { + Decl_List l = udef->l; + do CHECK_BB(scan1_union_def_action(env, udef, l)) + while((l = l->next)); + return GW_OK; +} + ANN m_bool scan1_union_def(const Env env, const Union_Def udef) { if(tmpl_base(udef->tmpl)) return GW_OK; - Decl_List l = udef->l; const m_uint scope = union_push(env, udef); if(udef->xid || udef->type_xid) { UNSET_FLAG(udef, private); UNSET_FLAG(udef, protect); } - do { - const Exp_Decl decl = l->self->d.exp_decl; - SET_FLAG(decl.td, checked | udef->flag); - const m_bool global = GET_FLAG(udef, global); - if(global) - UNSET_FLAG(decl.td, global); - if(GET_FLAG(udef, member)) - SET_FLAG(decl.td, member); - else if(GET_FLAG(udef, static)) - SET_FLAG(decl.td, static); - CHECK_BB(scan1_exp(env, l->self)) - if(global) - SET_FLAG(decl.td, global); - } while((l = l->next)); + const m_bool ret = scan1_union_def_inner(env, udef); union_pop(env, udef, scope); SET_FLAG(udef, scan1); - return GW_OK; + return ret; } static const _exp_func stmt_func[] = { diff --git a/src/parse/scan2.c b/src/parse/scan2.c index 1a233f7c..a7cad391 100644 --- a/src/parse/scan2.c +++ b/src/parse/scan2.c @@ -21,8 +21,11 @@ ANN m_bool scan2_exp_decl(const Env env, const Exp_Decl* decl) { const m_bool global = GET_FLAG(decl->td, global); const m_uint scope = !global ? env->scope->depth : env_push_global(env); const Type type = decl->type; - if(type->e->def && /*GET_FLAG(type, template) &&*/ !GET_FLAG(type, scan2)) - CHECK_BB(scan2_cdef(env, decl->type->e->def)) +{ + const Type t = get_type(decl->type); + if(GET_FLAG(t, template) && !GET_FLAG(t, scan2)) + CHECK_BB(scan2_cdef(env, t->e->def)) +} Var_Decl_List list = decl->list; do { const Var_Decl var = list->self; diff --git a/src/parse/scanx.c b/src/parse/scanx.c index 98e4486d..f3bfd295 100644 --- a/src/parse/scanx.c +++ b/src/parse/scanx.c @@ -54,16 +54,17 @@ scanx_ext(const Env e, const Class_Def c, const _exp_func f, void* d) { return ret; } #undef scanx_parent + __attribute__((returns_nonnull)) -static inline Type get_type(const Type t) { -// return !t->array_depth ? t : array_base(t); +ANN Type get_type(const Type t) { const Type type = !t->array_depth ? t : array_base(t); return !GET_FLAG(type, nonnull) ? type : type->e->parent; } __attribute__((returns_nonnull)) static inline Class_Def get_type_def(const Type t) { - return get_type(t)->e->def; + const Type type = get_type(t); + return type->e->def; } ANN m_bool diff --git a/src/parse/template.c b/src/parse/template.c index ace93238..2293c366 100644 --- a/src/parse/template.c +++ b/src/parse/template.c @@ -203,7 +203,8 @@ ANN Type scan_type(const Env env, const Type t, const Type_Decl* type) { CHECK_BO(scan0_class_def(env, a)) map_set(&env->curr->info->type->map, (vtype)a->base.xid, (vtype)a->base.type); } else { - a->union_def = new_union_def(env->gwion->mp, a->list, t->e->def->pos); + a->union_def = new_union_def(env->gwion->mp, a->list, + loc_cpy(env->gwion->mp, t->e->def->pos)); a->union_def->type_xid = a->base.xid; CHECK_BO(scan0_union_def(env, a->union_def)) a->base.type = a->union_def->type;