From c155f01eec118cf59294d098069899cfda8b9425 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Sat, 8 Jun 2019 00:01:01 +0200 Subject: [PATCH] :art: Introduce DECl_XX macros --- src/emit/emit.c | 17 ++++++----------- src/lib/func.c | 6 ++---- src/lib/import.c | 22 +++++++--------------- src/lib/instr.c | 3 +-- src/lib/opfunc.c | 3 +-- src/parse/check.c | 36 ++++++++++++------------------------ src/parse/func.c | 3 +-- src/parse/operator.c | 3 +-- src/parse/scan0.c | 13 ++++++------- src/parse/scan1.c | 9 +++------ src/parse/scanx.c | 9 +++------ src/parse/template.c | 9 +++------ src/parse/type_decl.c | 5 ++--- util | 2 +- 14 files changed, 49 insertions(+), 91 deletions(-) diff --git a/src/emit/emit.c b/src/emit/emit.c index a451542e..db82326f 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -86,8 +86,7 @@ ANN static inline void frame_push(Frame* frame) { } ANN static m_int frame_pop(Frame* frame) { - const Local* l = (Local*)vector_pop(&frame->stack); - CHECK_OB(l) + DECL_OB(const Local*, l, = (Local*)vector_pop(&frame->stack)) frame->curr_offset -= l->size; return l->is_obj ? (m_int)l->offset : frame_pop(frame); } @@ -249,8 +248,7 @@ ANN2(1,2) m_bool emit_instantiate_object(const Emitter emit, const Type type, array = new_array_sub(emit->gwion->mp, base); } assert(array->exp); - ArrayInfo* info = emit_array_extend_inner(emit, type, array->exp); - CHECK_OB(info) + DECL_OB(ArrayInfo*, info, = emit_array_extend_inner(emit, type, array->exp)) info->is_ref = !!is_ref; if(array != arr) free_array_sub(emit->gwion->mp, array); @@ -473,8 +471,7 @@ ANN static m_bool prim_float(const Emitter emit, const Exp_Primary* primary) { } ANN static m_bool prim_char(const Emitter emit, const Exp_Primary* prim) { - const m_int c = str2char(emit, prim->d.chr, exp_self(prim)->pos); - CHECK_BB(c); + DECL_BB(const m_int, c, = str2char(emit, prim->d.chr, exp_self(prim)->pos)) regpushi(emit, c); return GW_OK; } @@ -703,8 +700,7 @@ ANN static m_bool emit_exp_call_template(const Emitter emit, const Exp_Call* exp if(emit->env->func && emit->env->func == exp_call->m_func) return prepare_call(emit, exp_call); exp_call->m_func->def->base->tmpl->call = exp_call->tmpl->call; - const m_int scope = push_tmpl_func(emit, exp_call->m_func); - CHECK_BB(scope); + DECL_BB(const m_int,scope, = push_tmpl_func(emit, exp_call->m_func)) CHECK_BB(prepare_call(emit, exp_call)) emit_pop_type(emit); emit_pop(emit, (m_uint)scope); @@ -1071,9 +1067,8 @@ ANN2(1) static m_bool emit_exp(const Emitter emit, Exp exp, const m_bool ref) { ANN static m_bool emit_stmt_if(const Emitter emit, const Stmt_If stmt) { emit_push_scope(emit); CHECK_BB(emit_exp(emit, stmt->cond, 0)) - const Instr op = emit_flow(emit, isa(stmt->cond->type, t_object) > 0 ? - t_int : stmt->cond->type, BranchEqInt, BranchEqFloat); - CHECK_OB(op) + DECL_OB(const Instr, op, = emit_flow(emit, isa(stmt->cond->type, t_object) > 0 ? + t_int : stmt->cond->type, BranchEqInt, BranchEqFloat)) CHECK_BB(scoped_stmt(emit, stmt->if_body, 1)) const Instr op2 = emit_add_instr(emit, Goto); op->m_val = emit_code_size(emit); diff --git a/src/lib/func.c b/src/lib/func.c index c1df7ae2..22a9d7a1 100644 --- a/src/lib/func.c +++ b/src/lib/func.c @@ -80,10 +80,8 @@ ANN static m_bool fptr_tmpl_push(const Env env, struct FptrInfo *info) { static m_bool td_match(const Env env, const Type_Decl *id[2]) { - const Type t0 = known_type(env, id[0]); - CHECK_OB(t0) - const Type t1 = known_type(env, id[1]); - CHECK_OB(t1) + DECL_OB(const Type, t0, = known_type(env, id[0])) + DECL_OB(const Type, t1, = known_type(env, id[1])) return isa(t0, t1); } diff --git a/src/lib/import.c b/src/lib/import.c index f8aea4e4..647982f2 100644 --- a/src/lib/import.c +++ b/src/lib/import.c @@ -241,8 +241,7 @@ ANN m_int gwi_class_ext(const Gwi gwi, Type_Decl* td) { if(td->array && !td->array->exp) GWI_ERR_B("class extend array can't be empty") if(!gwi->gwion->env->class_def->e->def) { - const Type t = known_type(gwi->gwion->env, td); - CHECK_OB(t) + DECL_OB(const Type, t, = known_type(gwi->gwion->env, td)) if(td->array) SET_FLAG(gwi->gwion->env->class_def, typedef); gwi->gwion->env->class_def->e->parent = t; @@ -360,10 +359,8 @@ ANN /*static */ Type_List str2tl(const Env env, const m_str s, m_uint *depth) { ANN Type_Decl* str2decl(const Env env, const m_str s, m_uint *depth) { m_uint i = 0; - m_str type_name = get_type_name(env, s, i++); - CHECK_OO(type_name) - ID_List id = str2list(env, type_name, depth); - CHECK_OO(id) + DECL_OO(m_str, type_name, = get_type_name(env, s, i++)) + DECL_OO(ID_List, id, = str2list(env, type_name, depth)) Type_Decl* td = new_type_decl(env->gwion->mp, id, 0); Type_List tmp = NULL; if(!td) { @@ -415,8 +412,7 @@ ANN static Arg_List make_dll_arg_list(const Gwi gwi, DL_Func * dl_fun) { ANN Type_Decl* import_td(const Gwi gwi, const m_str name) { const Env env = gwi->gwion->env; m_uint array_depth; - const ID_List type_path = str2list(env, name, &array_depth); - CHECK_OO(type_path) + DECL_OO(const ID_List, type_path, = str2list(env, name, &array_depth)) Type_Decl* type_decl = new_type_decl(env->gwion->mp, type_path, 0); if(!type_decl) { free_id_list(env->gwion->mp, type_path); @@ -447,8 +443,7 @@ ANN static Func_Def make_dll_as_fun(const Gwi gwi, DL_Func * dl_fun, ae_flag fla ANN m_int gwi_func_end(const Gwi gwi, const ae_flag flag) { CHECK_BB(name_valid(gwi, gwi->func.name)); - Func_Def def = make_dll_as_fun(gwi, &gwi->func, flag); - CHECK_OB(def) + DECL_OB(Func_Def, def, = make_dll_as_fun(gwi, &gwi->func, flag)) if(gwi->templater.n) { def = new_func_def(gwi->gwion->mp, new_func_base(gwi->gwion->mp, NULL, NULL, NULL), NULL, 0, loc_cpy(gwi->gwion->mp, gwi->loc)); const ID_List list = templater_def(gwi->gwion->st, gwi); @@ -595,11 +590,8 @@ ANN2(1) m_int gwi_union_ini(const Gwi gwi, const m_str name) { } ANN m_int gwi_union_add(const Gwi gwi, const restrict m_str type, const restrict m_str name) { - const Exp exp = make_exp(gwi, type, name); - CHECK_OB(exp); - const Type t = type_decl_resolve(gwi->gwion->env, exp->d.exp_decl.td); - if(!t) - GWI_ERR_B("type '%s' unknown in union declaration.", type) + DECL_OB(const Exp, exp, = make_exp(gwi, type, name)) + DECL_OB(const Type, t, = known_type(gwi->gwion->env, exp->d.exp_decl.td)) if(isa(t, t_object) > 0) SET_FLAG(exp->d.exp_decl.td, ref); gwi->union_data.list = new_decl_list(gwi->gwion->mp, exp, gwi->union_data.list); diff --git a/src/lib/instr.c b/src/lib/instr.c index cac9c040..1a183c5e 100644 --- a/src/lib/instr.c +++ b/src/lib/instr.c @@ -62,8 +62,7 @@ ANN static Func_Def from_base(const Env env, const struct dottmpl_ *dt, const Ns const Func_Def fdef = dt->def ?: dt->base; const Symbol sym = func_symbol(env, nspc->name, s_name(fdef->base->xid), "template", dt->vt_index); - const Value v = nspc_lookup_value1(nspc, sym); - CHECK_OO(v) + DECL_OO(const Value, v, = nspc_lookup_value1(nspc, sym)) const Func_Def base = v->d.func_ref->def; const Func_Def def = new_func_def(env->gwion->mp, new_func_base(env->gwion->mp, fdef->base->td, insert_symbol(env->gwion->st, v->name), fdef->base->args), fdef->d.code, fdef->flag, loc_cpy(env->gwion->mp, base->pos)); diff --git a/src/lib/opfunc.c b/src/lib/opfunc.c index b186ea72..5c27028e 100644 --- a/src/lib/opfunc.c +++ b/src/lib/opfunc.c @@ -95,9 +95,8 @@ ANN m_bool check_exp_array_subscripts(const Env env, const Exp exp); OP_CHECK(opck_new) { const Exp_Unary* unary = (Exp_Unary*)data; SET_FLAG(unary->td, ref); - const Type t = known_type(env, unary->td); + DECL_OO(const Type, t, = known_type(env, unary->td)) UNSET_FLAG(unary->td, ref); - CHECK_OO(t) if(unary->td->array) CHECK_BO(check_exp_array_subscripts(env, unary->td->array->exp)) return t; diff --git a/src/parse/check.c b/src/parse/check.c index 3aa5e50b..b4d55755 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -89,8 +89,7 @@ ANN Type check_exp_decl(const Env env, const Exp_Decl* decl) { Var_Decl_List list = decl->list; CHECK_BO(switch_decl(env, exp_self(decl)->pos)) if(!decl->td->xid) { - const Type t = check_td(env, decl->td); - CHECK_OO(t) + DECL_OO(const Type, t, = check_td(env, decl->td)) ((Exp_Decl*)decl)->type = NULL; CHECK_BO(scan1_exp(env, exp_self(decl))) CHECK_BO(scan2_exp(env, exp_self(decl))) @@ -290,8 +289,7 @@ ANN static Type check_exp_primary(const Env env, const Exp_Primary* primary) { } ANN Type check_exp_array(const Env env, const Exp_Array* array) { - Type t_base = check_exp(env, array->base); - CHECK_OO(t_base) + DECL_OO(Type, t_base, = check_exp(env, array->base)) Exp e = array->array->exp; CHECK_OO(check_exp(env, e)) m_uint depth = 0; @@ -581,8 +579,7 @@ ANN static Type check_exp_call_template(const Env env, const Exp_Call *exp) { const Exp call = exp->func; const Exp args = exp->args; m_uint args_number = 0; - const Value value = nspc_lookup_value1(call->type->e->owner, insert_symbol(call->type->name)); - CHECK_OO(value) + DECL_OO(const Value, value, = nspc_lookup_value1(call->type->e->owner, insert_symbol(call->type->name))) const m_uint type_number = get_type_number(value->d.func_ref->def->base->tmpl->list); Type_List tl[type_number]; ID_List list = value->d.func_ref->def->base->tmpl->list; @@ -673,8 +670,7 @@ ANN static Type check_exp_binary(const Env env, const Exp_Binary* bin) { } ANN static Type check_exp_cast(const Env env, const Exp_Cast* cast) { - const Type t = check_exp(env, cast->exp); - CHECK_OO(t) + DECL_OO(const Type, t, = check_exp(env, cast->exp)) CHECK_OO((exp_self(cast)->type = cast->td->xid ? known_type(env, cast->td) : check_td(env, cast->td))) struct Op_Import opi = { .op=op_cast, .lhs=t, .rhs=exp_self(cast)->type, .data=(uintptr_t)cast, .pos=exp_self(cast)->pos }; return op_check(env, &opi); @@ -719,12 +715,9 @@ ANN static Type check_exp_unary(const Env env, const Exp_Unary* unary) { } ANN static Type check_exp_if(const Env env, const Exp_If* exp_if) { - const Type cond = check_exp(env, exp_if->cond); - CHECK_OO(cond) - const Type if_exp = check_exp(env, exp_if->if_exp); - CHECK_OO(if_exp) - const Type else_exp = check_exp(env, exp_if->else_exp); - CHECK_OO(else_exp) + DECL_OO(const Type, cond, = check_exp(env, exp_if->cond)) + DECL_OO(const Type, if_exp, = check_exp(env, exp_if->if_exp)) + DECL_OO(const Type, else_exp, = check_exp(env, exp_if->else_exp)) if(isa(cond, t_int) < 0 && isa(cond, t_float) < 0 && isa(cond, t_object) < 0) ERR_O(exp_self(exp_if)->pos, "Invalid type '%s' in if expression condition.", cond->name) @@ -779,10 +772,8 @@ ANN static Type check_exp_lambda(const Env env NUSED, const Exp_If* exp_if NUSED) { return t_lambda; } ANN static Type check_exp_typeof(const Env env, const Exp_Typeof *exp) { - const Type t = check_exp(env, exp->exp); - CHECK_OO(t) - const Value v = nspc_lookup_value1(t->e->owner, insert_symbol(t->name)); - CHECK_OO(v) + DECL_OO(const Type, t, = check_exp(env, exp->exp)) + DECL_OO(Value, v, = nspc_lookup_value1(t->e->owner, insert_symbol(t->name))) return v->type; } @@ -848,8 +839,7 @@ ANN static inline m_bool for_empty(const Env env, const Stmt_For stmt) { } ANN static m_bool do_stmt_auto(const Env env, const Stmt_Auto stmt) { - Type t = check_exp(env, stmt->exp); - CHECK_OB(t) + DECL_OB(Type, t, = check_exp(env, stmt->exp)) Type ptr = array_base(t); const m_uint depth = t->array_depth - 1; if(GET_FLAG(t, typedef)) @@ -928,8 +918,7 @@ stmt_func_xxx(auto, Stmt_Auto,, do_stmt_auto(env, stmt)) ANN static m_bool check_stmt_return(const Env env, const Stmt_Exp stmt) { if(!env->func) ERR_B(stmt_self(stmt)->pos, "'return' statement found outside function definition") - const Type ret_type = stmt->val ? check_exp(env, stmt->val) : t_void; - CHECK_OB(ret_type) + DECL_OB(const Type, ret_type, = stmt->val ? check_exp(env, stmt->val) : t_void) if(env->func->value_ref->type == t_lambda) { if(env->func->def->base->ret_type && isa(ret_type, env->func->def->base->ret_type) < 0 && @@ -961,8 +950,7 @@ describe_check_stmt_stack(breaks, break) ANN Value case_value(const Exp exp); ANN static m_bool check_stmt_case(const Env env, const Stmt_Exp stmt) { CHECK_BB(switch_inside(env, stmt_self(stmt)->pos)); - const Type t = check_exp(env, stmt->val); - CHECK_OB(t); + DECL_OB(const Type, t, = check_exp(env, stmt->val)) if(isa(t, t_int) < 0) ERR_B(stmt_self(stmt)->pos, "invalid type '%s' case expression. should be 'int'", t->name) const Value v = case_value(stmt->val); diff --git a/src/parse/func.c b/src/parse/func.c index d34f381d..26a50eab 100644 --- a/src/parse/func.c +++ b/src/parse/func.c @@ -35,8 +35,7 @@ ANN Func new_func(MemPool p, const m_str name, const Func_Def def) { #include "vm.h" #include "gwion.h" ANN Func get_func(const Env env, const Func_Def def) { - Func f = def->base->func; - CHECK_OO(f) + DECL_OO(Func, f, = def->base->func) m_str end = strrchr(f->name, '@'); // test end cause some template func do not have @x@env->curr->name if(end && env->class_def && GET_FLAG(env->class_def, template)) { ++end; diff --git a/src/parse/operator.c b/src/parse/operator.c index f765fcf1..74d9d259 100644 --- a/src/parse/operator.c +++ b/src/parse/operator.c @@ -180,8 +180,7 @@ ANN Type op_check(const Env env, struct Op_Import* opi) { ANN m_bool operator_set_func(const struct Op_Import* opi) { const Nspc nspc = ((Func)opi->data)->value_ref->owner; const Vector v = (Vector)map_get(&nspc->info->op_map, opi->op); - M_Operator* mo = operator_find(v, opi->lhs, opi->rhs); - CHECK_OB(mo) + DECL_OB(M_Operator*, mo, = operator_find(v, opi->lhs, opi->rhs)) mo->func = (Func)opi->data; return GW_OK; } diff --git a/src/parse/scan0.c b/src/parse/scan0.c index fc56c533..eb89ff2d 100644 --- a/src/parse/scan0.c +++ b/src/parse/scan0.c @@ -45,8 +45,7 @@ ANN m_bool scan0_stmt_fptr(const Env env, const Stmt_Fptr stmt) { ANN m_bool scan0_stmt_type(const Env env, const Stmt_Type stmt) { CHECK_BB(env_access(env, stmt->ext->flag, stmt_self(stmt)->pos)) - const Type base = stmt->tmpl ? find_type(env, stmt->ext->xid) : known_type(env, stmt->ext); - CHECK_OB(base) + DECL_OB(const Type, base, = stmt->tmpl ? find_type(env, stmt->ext->xid) : known_type(env, stmt->ext)) CHECK_BB(scan0_defined(env, stmt->xid, td_pos(stmt->ext))) if(!stmt->ext->types && (!stmt->ext->array || !stmt->ext->array->exp)) { const Type t = new_type(env->gwion->mp, ++env->scope->type_xid, s_name(stmt->xid), base); @@ -246,11 +245,11 @@ ANN m_bool scan0_class_def(const Env env, const Class_Def cdef) { CHECK_BB(scan0_class_def_pre(env, cdef)) CHECK_OB((cdef->base.type = scan0_class_def_init(env, cdef))) if(cdef->body) { -int call = cdef->base.tmpl && !cdef->base.tmpl->call; -if(call)cdef->base.tmpl->call = (Type_List)1; - CHECK_BB(env_body(env, cdef, scan0_section)) -if(call)cdef->base.tmpl->call = NULL; -} + int call = cdef->base.tmpl && !cdef->base.tmpl->call; + if(call)cdef->base.tmpl->call = (Type_List)1; + CHECK_BB(env_body(env, cdef, scan0_section)) + if(call)cdef->base.tmpl->call = NULL; + } (void)mk_class(env, cdef->base.type); if(GET_FLAG(cdef, global)) env->curr = (Nspc)vector_pop(&env->scope->nspc_stack); diff --git a/src/parse/scan1.c b/src/parse/scan1.c index 106390d0..cbe60e52 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -15,8 +15,7 @@ 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) { - const Type t = known_type(env, td); - CHECK_OO(t) + DECL_OO(const Type, t, = known_type(env, td)) if(t->size) return t; ERR_O(td_pos(td), "cannot declare variables of size '0' (i.e. 'void')...") @@ -47,8 +46,7 @@ ANN static m_bool type_recursive(const Env env, Exp_Decl* decl, const Type t) { } ANN static Type scan1_exp_decl_type(const Env env, Exp_Decl* decl) { - const Type t = void_type(env, decl->td); - CHECK_OO(t); + DECL_OO(const Type ,t, = void_type(env, decl->td)) if(decl->td->xid && decl->td->xid->xid == insert_symbol("auto") && decl->type) return decl->type; if(!env->scope->depth && env->class_def) { @@ -335,8 +333,7 @@ ANN static m_bool scan1_parent(const Env env, const Class_Def cdef) { const loc_t pos = td_pos(cdef->base.ext); if(cdef->base.ext->array) CHECK_BB(scan1_exp(env, cdef->base.ext->array->exp)) - const Type parent = cdef->base.type->e->parent = known_type(env, cdef->base.ext); - CHECK_OB(parent) + DECL_OB(const Type , parent, = cdef->base.type->e->parent = known_type(env, cdef->base.ext)) Type t = parent; do { if(cdef->base.type == t) diff --git a/src/parse/scanx.c b/src/parse/scanx.c index 492478fa..568f30a5 100644 --- a/src/parse/scanx.c +++ b/src/parse/scanx.c @@ -25,8 +25,7 @@ ANN static inline m_bool tmpl_push(const Env env, const Tmpl* tmpl) { } ANN static inline m_int _push(const Env env, const Class_Def c) { - const m_int scope = env_push_type(env, c->base.type); - CHECK_BB(scope) + DECL_BB(const m_int, scope, = env_push_type(env, c->base.type)) return (!c->base.tmpl || tmpl_push(env, c->base.tmpl)) ? scope : GW_ERROR; } @@ -40,8 +39,7 @@ ANN static inline void _pop(const Env e, const Class_Def c, const m_uint s) { // TODO: 'v' should be 2° argument ANN m_bool scanx_body(const Env e, const Class_Def c, const _exp_func f, void* d) { - const m_int scope = _push(e, c); - CHECK_BB(scope) + DECL_BB(const m_int, scope, = _push(e, c)) const m_bool ret = _body(d, c->body, f); _pop(e, c, scope); return ret; @@ -50,8 +48,7 @@ scanx_body(const Env e, const Class_Def c, const _exp_func f, void* d) { #undef scanx_ext ANN m_bool scanx_ext(const Env e, const Class_Def c, const _exp_func f, void* d) { - const m_int scope = _push(e, c); - CHECK_BB(scope) + DECL_BB(const m_int, scope, = _push(e, c)) const m_bool ret = f(d, c); _pop(e, c, scope); return ret; diff --git a/src/parse/template.c b/src/parse/template.c index 41cdb0bb..50af46b3 100644 --- a/src/parse/template.c +++ b/src/parse/template.c @@ -74,8 +74,7 @@ ANN static ssize_t template_size(const Env env, struct tmpl_info* info) { Type_List call = info->call; size_t size = tmpl_set(info, info->cdef->base.type); do { - const Type t = known_type(env, call->td); - CHECK_OB(t) + DECL_OB(const Type, t, = known_type(env, call->td)) size += tmpl_set(info, t); } while((call = call->next) && (base = base->next) && ++size); return size + 16 + 3; @@ -118,8 +117,7 @@ ANN m_bool template_match(ID_List base, Type_List call) { } ANN static Class_Def template_class(const Env env, const Class_Def def, const Type_List call) { - const Symbol name = template_id(env, def, call); - CHECK_OO(name) + DECL_OO(const Symbol, name, = template_id(env, def, call)) if(env->class_def && name == insert_symbol(env->class_def->name)) return env->class_def->e->def; const Type t = nspc_lookup_type1(env->curr, name); @@ -159,8 +157,7 @@ ANN Type scan_type(const Env env, const Type t, const Type_Decl* type) { "you must provide template types for type '%s'", t->name) if(template_match(t->e->def->base.tmpl->list, type->types) < 0) ERR_O(type->xid->pos, "invalid template types number") - const Class_Def a = template_class(env, t->e->def, type->types); - CHECK_OO(a) + DECL_OO(const Class_Def, a, = template_class(env, t->e->def, type->types)) SET_FLAG(a, ref); if(a->base.type) return a->base.type; diff --git a/src/parse/type_decl.c b/src/parse/type_decl.c index bc1d4d9d..f648dd55 100644 --- a/src/parse/type_decl.c +++ b/src/parse/type_decl.c @@ -11,9 +11,8 @@ #include "parse.h" ANN Type type_decl_resolve(const Env env, const Type_Decl* td) { - const Type base = find_type(env, td->xid); - CHECK_OO(base) - const Type t = scan_type(env, base, td); + DECL_OO(const Type, base, = find_type(env, td->xid)) + DECL_OO(const Type, t, = scan_type(env, base, td)) return !td->array ? t : array_type(env, t, td->array->depth); } diff --git a/util b/util index f2e8f337..512600b2 160000 --- a/util +++ b/util @@ -1 +1 @@ -Subproject commit f2e8f337708be41cac47fb85862766b801d1bda6 +Subproject commit 512600b2403a7f31454245f3957866ab8cbde070 -- 2.43.0