From: Jérémie Astor Date: Sun, 20 Jun 2021 17:59:13 +0000 (+0200) Subject: :art: Use is_func X-Git-Tag: nightly~574 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=512a1f4cbc4490d42a196f380d3de18317db1c9d;p=gwion.git :art: Use is_func --- diff --git a/fmt b/fmt index 260f2565..11f86a1a 160000 --- a/fmt +++ b/fmt @@ -1 +1 @@ -Subproject commit 260f256586cbd65f0e5c74484dbe71d26b6db62e +Subproject commit 11f86a1a1aad81ff6dba21605398007898e2fb3f diff --git a/include/env/type.h b/include/env/type.h index df5ff724..e1ded1a2 100644 --- a/include/env/type.h +++ b/include/env/type.h @@ -68,8 +68,9 @@ ANN Type actual_type(const struct Gwion_ *gwion, const Type t); ANN static inline m_uint env_push_type(const Env env, const Type type) { return env_push(env, type, type->nspc); } -ANN m_bool is_fptr(const struct Gwion_ *, const Type t); -ANN m_bool is_class(const struct Gwion_ *, const Type t); +ANN bool is_func(const struct Gwion_ *, const Type t); +ANN bool is_fptr(const struct Gwion_ *, const Type t); +ANN bool is_class(const struct Gwion_ *, const Type t); ANN __attribute__((returns_nonnull)) static inline Type _class_base(Type t) { return t->info->base_type; } diff --git a/src/emit/emit.c b/src/emit/emit.c index 9059e70a..d6ed36d5 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -1009,7 +1009,7 @@ ANN static m_bool emit_func_args(const Emitter emit, const Exp_Call *exp_call) { // emit_exp_addref(emit, exp_call->args, -exp_totalsize(exp_call->args)); } const Type t = actual_type(emit->gwion, exp_call->func->type); - if (isa(t, emit->gwion->type[et_function]) > 0 && + if (is_func(emit->gwion, t) && fbflag(t->info->func->def->base, fbflag_variadic)) emit_func_arg_vararg(emit, exp_call); return GW_OK; @@ -1052,7 +1052,7 @@ ANN static inline bool member_inlinable(const Func f, const Exp e) { ANN static inline Func is_inlinable(const Emitter emit, const Exp_Call *exp_call) { const Type ftype = exp_call->func->type; - if (isa(ftype, emit->gwion->type[et_function]) < 0 || + if (!is_func(emit->gwion, ftype)|| is_fptr(emit->gwion, ftype) || !ftype->info->func->code || ftype->info->func->code->builtin) return false; @@ -1145,7 +1145,7 @@ ANN static m_bool _emit_exp_call(const Emitter emit, const Exp_Call *exp_call) { CHECK_BB(prepare_call(emit, exp_call)); else CHECK_BB(emit_func_args(emit, exp_call)); - if (isa(t, emit->gwion->type[et_function]) > 0) + if (is_func(emit->gwion, t)) CHECK_BB(emit_exp_call1(emit, t->info->func, is_static_call(emit, exp_call->func))); else { @@ -1166,7 +1166,7 @@ ANN static m_bool emit_exp_call(const Emitter emit, const Exp_Call *exp_call) { regpop(emit, exp_self(exp_call)->type->size); const Instr instr = emit_add_instr(emit, Reg2RegAddr); instr->m_val = -SZ_INT; - } else if (isa(exp_call->func->type, emit->gwion->type[et_function]) < 0 && + } else if (!is_func(emit->gwion, exp_call->func->type) && tflag(e->type, tflag_struct)) regpop(emit, SZ_INT); return GW_OK; diff --git a/src/env/type.c b/src/env/type.c index 4110a777..e964c15b 100644 --- a/src/env/type.c +++ b/src/env/type.c @@ -137,10 +137,13 @@ ANN m_uint get_depth(const Type type) { return depth; } -ANN m_bool is_fptr(const struct Gwion_ *gwion, const Type t) { +ANN bool is_func(const struct Gwion_ *gwion, const Type t) { + return isa(actual_type(gwion, t), gwion->type[et_function]) > 0; +} +ANN bool is_fptr(const struct Gwion_ *gwion, const Type t) { return isa(actual_type(gwion, t), gwion->type[et_fptr]) > 0; } -ANN inline m_bool is_class(const struct Gwion_ *gwion, const Type t) { +ANN inline bool is_class(const struct Gwion_ *gwion, const Type t) { return isa(t, gwion->type[et_class]) > 0; } diff --git a/src/lib/lib_func.c b/src/lib/lib_func.c index 2cdec9e3..dc6ffb7e 100644 --- a/src/lib/lib_func.c +++ b/src/lib/lib_func.c @@ -226,7 +226,7 @@ ANN static Type fptr_type(const Env env, struct FptrInfo *info) { if (!is_class(env->gwion, info->lhs->value_ref->type)) { if (!(info->lhs = nspc_lookup_func1(nspc, sym))) { const Value v = nspc_lookup_value1(nspc, insert_symbol(c)); - if (isa(v->type, env->gwion->type[et_function]) < 0) return NULL; + if (!is_func(env->gwion, v->type)) return NULL; info->lhs = v->type->info->func; } } else { diff --git a/src/lib/object_op.c b/src/lib/object_op.c index 906284e4..7ab2001e 100644 --- a/src/lib/object_op.c +++ b/src/lib/object_op.c @@ -170,7 +170,7 @@ OP_CHECK(opck_object_dot) { const Value value = get_value(env, member, the_base); if (!value) { const Value v = nspc_lookup_value1(env->curr, member->xid); - if (v && isa(v->type, env->gwion->type[et_function]) > 0) return v->type; + if (v && is_func(env->gwion, v->type)) return v->type; env_err(env, exp_self(member)->pos, _("class '%s' has no member '%s'"), the_base->name, str); if (member->base->type->nspc) did_you_mean_type(the_base, str); @@ -206,12 +206,12 @@ OP_EMIT(opem_object_dot) { } if (!is_class(emit->gwion, member->base->type) && (vflag(value, vflag_member) || - (isa(exp_self(member)->type, emit->gwion->type[et_function]) > 0 && + (is_func(emit->gwion, exp_self(member)->type) && !is_fptr(emit->gwion, exp_self(member)->type)))) { if (!tflag(t_base, tflag_struct) && vflag(value, vflag_member)) CHECK_BB(emit_exp(emit, member->base)); } - if (isa(exp_self(member)->type, emit->gwion->type[et_function]) > 0 && + if (is_func(emit->gwion, exp_self(member)->type) && !is_fptr(emit->gwion, exp_self(member)->type)) emit_member_func(emit, member); else if (vflag(value, vflag_member)) { diff --git a/src/lib/union.c b/src/lib/union.c index c48178d7..d01aff89 100644 --- a/src/lib/union.c +++ b/src/lib/union.c @@ -32,7 +32,7 @@ static OP_EMIT(opem_union_dot) { const Exp_Dot *member = (Exp_Dot *)data; const Map map = &member->base->type->nspc->info->value->map; CHECK_BB(emit_exp(emit, member->base)); - if (isa(exp_self(member)->type, emit->gwion->type[et_function]) > 0) { + if (is_func(emit->gwion, exp_self(member)->type)) { const Instr instr = emit_add_instr(emit, RegPushImm); const Func f = (Func)vector_front(&member->base->type->info->parent->nspc->vtable); diff --git a/src/parse/check.c b/src/parse/check.c index 53e19d2c..fc4dc3b1 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -537,8 +537,7 @@ ANN static Func call2ufcs(const Env env, Exp_Call *call, const Value v) { ANN Func ufcs(const Env env, const Func up, Exp_Call *const call) { const Value v = nspc_lookup_value1(env->curr, up->def->base->xid); - if (v && isa(v->type, env->gwion->type[et_function]) > 0 && - !vflag(v, vflag_member)) + if (v && is_func(env->gwion, v->type) && !vflag(v, vflag_member)) return call2ufcs(env, call, v); return NULL; } @@ -758,7 +757,7 @@ ANN m_bool func_check(const Env env, Exp_Call *const exp) { ERR_B(exp->func->pos, _("Can't call late function pointer at declaration " "site. did you meant to use `@=>`?")) const Type t = actual_type(env->gwion, exp->func->type); - if (isa(t, env->gwion->type[et_function]) > 0 && + if (is_func(env->gwion , t) && exp->func->exp_type == ae_exp_dot && !t->info->value->from->owner_class) { if (exp->args) CHECK_OB(check_exp(env, exp->args)); return call2ufcs(env, exp, t->info->func->value_ref) ? GW_OK : GW_ERROR; @@ -785,7 +784,7 @@ ANN Type check_exp_call1(const Env env, Exp_Call *const exp) { DECL_BO(const m_bool, ret, = func_check(env, exp)); if (!ret) return exp_self(exp)->type; const Type t = actual_type(env->gwion, exp->func->type); - if (isa(t, env->gwion->type[et_function]) < 0) { // use func flag? + if (!is_func(env->gwion, t)) { // use func flag? struct Op_Import opi = {.op = insert_symbol("@ctor"), .rhs = actual_type(env->gwion, exp->func->type), .data = (uintptr_t)exp, @@ -900,7 +899,7 @@ ANN static Type check_exp_call(const Env env, Exp_Call *exp) { DECL_BO(const m_bool, ret, = func_check(env, exp)); if (!ret) return exp_self(exp)->type; const Type t = actual_type(env->gwion, exp->func->type); - if (isa(t, env->gwion->type[et_function]) < 0) + if (!is_func(env->gwion, t)) return check_exp_call1(env, exp); if (exp->args) CHECK_OO(check_exp(env, exp->args)); if (!t->info->func->def->base->tmpl) @@ -1054,7 +1053,7 @@ ANN static Type check_exp_lambda(const Env env, const Exp_If *exp_if NUSED) { ANN static Type check_exp_td(const Env env, Type_Decl **td) { DECL_OO(const Type, t, = known_type(env, *td)); - if (isa(t, env->gwion->type[et_function]) > 0 && !is_fptr(env->gwion, t)) + if (is_func(env->gwion, t) && !is_fptr(env->gwion, t)) return type_class(env->gwion, t); return t; } @@ -1068,7 +1067,7 @@ ANN Type check_exp(const Env env, const Exp exp) { do { CHECK_OO((curr->type = check_exp_func[curr->exp_type](env, &curr->d))); if (env->func && isa(curr->type, env->gwion->type[et_lambda]) < 0 && - isa(curr->type, env->gwion->type[et_function]) > 0 && + is_func(env->gwion, curr->type) && !safe_fflag(curr->type->info->func, fflag_pure)) unset_fflag(env->func, fflag_pure); } while ((curr = curr->next)); @@ -1460,7 +1459,7 @@ ANN static m_bool check_parent_match(const Env env, const Func_Def fdef) { if (!env->curr->vtable.ptr) vector_init(&env->curr->vtable); if (parent) { const Value v = find_value(parent, fdef->base->xid); - if (v && isa(v->type, env->gwion->type[et_function]) > 0) { + if (v && is_func(env->gwion, v->type)) { const m_bool match = parent_match_actual(env, fdef, v->d.func_ref); if (match) return match; } @@ -1516,7 +1515,7 @@ ANN static m_bool check_func_def_override(const Env env, const Func_Def fdef, const Value override = find_value(env->class_def->info->parent, fdef->base->xid); if (override && override->from->owner_class && - isa(override->type, env->gwion->type[et_function]) < 0) + !is_func(env->gwion, override->type)) ERR_B(fdef->base->pos, _("function name '%s' conflicts with previously defined value...\n" " from super class '%s'..."), diff --git a/src/parse/check_traits.c b/src/parse/check_traits.c index b188fa5c..35d9a93a 100644 --- a/src/parse/check_traits.c +++ b/src/parse/check_traits.c @@ -61,7 +61,7 @@ ANN static bool request_fun(const Env env, const Type t, const Func_Def request) { const Value v = nspc_lookup_value0(t->nspc, request->base->xid); if (v) { - if (isa(v->type, env->gwion->type[et_function]) < 0 || + if (!is_func(env->gwion, v->type) || is_fptr(env->gwion, v->type)) { gwerr_basic("is not a function", NULL, NULL, v->from->filename, v->from->loc, 0); diff --git a/src/parse/scan0.c b/src/parse/scan0.c index 685bb306..54a8fb82 100644 --- a/src/parse/scan0.c +++ b/src/parse/scan0.c @@ -167,7 +167,7 @@ ANN m_bool scan0_type_def(const Env env, const Type_Def tdef) { context_global(env); env_push_global(env); } - if (isa(base, env->gwion->type[et_function]) < 0) { + if (!is_func(env->gwion, base)) { if (!tdef->ext->types && (!tdef->ext->array || !tdef->ext->array->exp)) typedef_simple(env, tdef, base); else diff --git a/src/parse/scan1.c b/src/parse/scan1.c index c7e66316..4b0cc294 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -628,7 +628,7 @@ ANN static inline m_bool scan1_fdef_defined(const Env env, const Func_Def fdef) { const Value v = nspc_lookup_value1(env->curr, fdef->base->xid); if (!v) return GW_OK; - if (isa(actual_type(env->gwion, v->type), env->gwion->type[et_function]) > 0) + if (is_func(env->gwion, actual_type(env->gwion, v->type))) return GW_OK; if ((!env->class_def || !GET_FLAG(env->class_def, final)) && !nspc_lookup_value0(env->curr, fdef->base->xid)) diff --git a/src/parse/scan2.c b/src/parse/scan2.c index 801514e4..1350c178 100644 --- a/src/parse/scan2.c +++ b/src/parse/scan2.c @@ -285,7 +285,7 @@ ANN static m_bool scan2_stmt_list(const Env env, Stmt_List list) { ANN static m_bool scan2_func_def_overload(const Env env, const Func_Def f, const Value overload) { const m_bool fptr = is_fptr(env->gwion, overload->type); - if (isa(overload->type, env->gwion->type[et_function]) < 0 || + if (!is_func(env->gwion, overload->type) || is_fptr(env->gwion, overload->type)) { if (!fbflag(f->base, fbflag_internal)) ERR_B(f->base->pos, diff --git a/src/parse/template.c b/src/parse/template.c index 03395c4c..7885afe7 100644 --- a/src/parse/template.c +++ b/src/parse/template.c @@ -100,7 +100,7 @@ static ANN Type scan_func(const Env env, const Type t, const Type_Decl *td) { } static ANN Type maybe_func(const Env env, const Type t, const Type_Decl *td) { - if (isa(t, env->gwion->type[et_function]) > 0 && + if (is_func(env->gwion, t) && t->info->func->def->base->tmpl) return scan_func(env, t, td); ERR_O(td->pos, @@ -109,7 +109,7 @@ static ANN Type maybe_func(const Env env, const Type t, const Type_Decl *td) { } ANN Type _scan_type(const Env env, const Type t, Type_Decl *td) { - if (tflag(t, tflag_tmpl) && isa(t, env->gwion->type[et_function]) < 0) { + if (tflag(t, tflag_tmpl) && !is_func(env->gwion, t)) { if (tflag(t, tflag_ntmpl) && !td->types) return t; struct TemplateScan ts = {.t = t, .td = td}; Type_List tl = td->types;