From 9216048cf974dedfa20ec80e8601da7bcbf7d230 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Wed, 2 Oct 2019 20:55:30 +0200 Subject: [PATCH] :bug: AFL :smile: --- src/emit/emit.c | 38 ++++++++++++++++++++++---------------- src/lib/prim.c | 1 + src/lib/ptr.c | 16 ++++++++-------- src/lib/vararg.c | 3 +++ src/parse/check.c | 8 ++++++-- src/parse/scan0.c | 1 - src/parse/scan1.c | 2 ++ src/parse/scan2.c | 2 ++ 8 files changed, 44 insertions(+), 27 deletions(-) diff --git a/src/emit/emit.c b/src/emit/emit.c index 7f8aabf7..744195ba 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -1014,11 +1014,12 @@ static m_bool scoped_stmt(const Emitter emit, const Stmt stmt, const m_bool pop) ++emit->env->scope->depth; emit_push_scope(emit); const Instr gc = emit_add_instr(emit, NoOp); - CHECK_BB(emit_stmt(emit, stmt, pop)) - const m_bool pure = !vector_back(&emit->info->pure); - if(!pure) { - gc->opcode = eGcIni; - emit_add_instr(emit, GcEnd); + if(emit_stmt(emit, stmt, pop) > 0) { + const m_bool pure = !vector_back(&emit->info->pure); + if(!pure) { + gc->opcode = eGcIni; + emit_add_instr(emit, GcEnd); + } } emit_pop_scope(emit); --emit->env->scope->depth; @@ -1699,7 +1700,9 @@ ANN static m_bool emit_exp_dot(const Emitter emit, const Exp_Dot* member) { (isa(exp_self(member)->type, emit->gwion->type[et_function]) > 0 && !is_fptr(emit->gwion, exp_self(member)->type))) ) { CHECK_BB(emit_exp(emit, member->base, 0)) - emit_except(emit, member->t_base); + emit_add_instr(emit, GWOP_EXCEPT); + +// emit_except(emit, member->t_base); } if(isa(exp_self(member)->type, emit->gwion->type[et_function]) > 0 && !is_fptr(emit->gwion, exp_self(member)->type)) return emit_member_func(emit, member); @@ -1811,18 +1814,21 @@ ANN static m_bool emit_func_def(const Emitter emit, const Func_Def fdef) { emit_push_scope(emit); if(fdef->base->tmpl) CHECK_BB(template_push_types(emit->env, fdef->base->tmpl)) - CHECK_BB(emit_func_def_body(emit, fdef)) - emit_func_def_return(emit); - emit_func_def_code(emit, func); + const m_bool ret = emit_func_def_body(emit, fdef); + if(ret > 0) + emit_func_def_return(emit); if(fdef->base->tmpl) emit_pop_type(emit); - emit->env->func = former; - if(!emit->env->class_def && !GET_FLAG(fdef, global) && !fdef->base->tmpl) - emit_func_def_global(emit, func->value_ref); - if(emit->info->memoize && GET_FLAG(func, pure)) - func->code->memoize = memoize_ini(emit, func, - kindof(func->def->base->ret_type->size, !func->def->base->ret_type->size)); - return GW_OK; + if(ret > 0) { + emit_func_def_code(emit, func); + emit->env->func = former; + if(!emit->env->class_def && !GET_FLAG(fdef, global) && !fdef->base->tmpl) + emit_func_def_global(emit, func->value_ref); + if(emit->info->memoize && GET_FLAG(func, pure)) + func->code->memoize = memoize_ini(emit, func, + kindof(func->def->base->ret_type->size, !func->def->base->ret_type->size)); + } + return ret; } #define emit_fptr_def dummy_func diff --git a/src/lib/prim.c b/src/lib/prim.c index af6ccf9b..0e0f42f9 100644 --- a/src/lib/prim.c +++ b/src/lib/prim.c @@ -85,6 +85,7 @@ static GWION_IMPORT(int_values) { GWI_BB(gwi_gack(gwi, t_bool, gack_bool)) gwi->gwion->type[et_bool] = t_bool; GWI_BB(gwi_oper_ini(gwi, NULL, "int", "bool")) + GWI_BB(gwi_oper_add(gwi, opck_unary_meta)) // should return bool GWI_BB(gwi_oper_end(gwi, "!", IntNot)) gwi_reserve(gwi, "maybe"); struct SpecialId_ spid = { .type=t_bool, .exec=RegPushMaybe, .is_const=1 }; diff --git a/src/lib/ptr.c b/src/lib/ptr.c index b3f84fde..2de8082a 100644 --- a/src/lib/ptr.c +++ b/src/lib/ptr.c @@ -14,6 +14,7 @@ #include "import.h" #include "emit.h" #include "traverse.h" +#include "parse.h" #include "gwi.h" static OP_CHECK(opck_ptr_assign) { @@ -43,21 +44,20 @@ static OP_EMIT(opem_ptr_assign) { static OP_CHECK(opck_ptr_deref) { const Exp_Unary* unary = (Exp_Unary*)data; - return exp_self(unary)->type = nspc_lookup_type1(unary->exp->type->e->owner, insert_symbol(env->gwion->st, get_type_name(env, unary->exp->type->name, 1))); + return exp_self(unary)->type = nspc_lookup_type1(unary->exp->type->e->owner, insert_symbol(get_type_name(env, unary->exp->type->name, 1))); } - +#include "tuple.h" static OP_CHECK(opck_ptr_cast) { const Exp_Cast* cast = (Exp_Cast*)data; - const Type t = type_decl_resolve(env, cast->td); + DECL_ON(const Type, t, = type_decl_resolve(env, cast->td)) if(!GET_FLAG(t, check)) { assert(t->e->def); - CHECK_BO(traverse_class_def(env, t->e->def)) + CHECK_BN(traverse_class_def(env, t->e->def)) } -// TODO check types. -// const Type ptr = nspc_ -// if(t && isa(cast->exp->type, get_type(env, t, 1)) > 0) + const Type to = (Type)vector_at(&t->e->tuple->types, 0); + if(isa(cast->exp->type, to) > 0) return t; -// ERR_N(exp_self(cast)->pos, "invalid pointer cast") + ERR_N(exp_self(cast)->pos, "invalid pointer cast") } static OP_CHECK(opck_implicit_ptr) { diff --git a/src/lib/vararg.c b/src/lib/vararg.c index 852f0ac1..eb7358fc 100644 --- a/src/lib/vararg.c +++ b/src/lib/vararg.c @@ -13,6 +13,7 @@ #include "operator.h" #include "import.h" #include "gwi.h" +#include "specialid.h" void free_vararg(MemPool p, struct Vararg_* arg) { xfree(arg->d); @@ -113,6 +114,8 @@ GWION_IMPORT(vararg) { GWI_BB(gwi_oper_add(gwi, at_varobj)) GWI_BB(gwi_oper_end(gwi, "@=>", VarargAssign)) register_freearg(gwi, VarargIni, freearg_vararg); +struct SpecialId_ spid = { .type=t_vararg, .exec=RegPushImm, .is_const=1 }; +gwi_specialid(gwi, "vararg", &spid); gwi_reserve(gwi, "vararg"); return GW_OK; } diff --git a/src/parse/check.c b/src/parse/check.c index a76ca190..28b52d30 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -410,7 +410,7 @@ ANN static Type at_depth(const Env env, const Array_Sub array) { static ANN Type check_exp_array(const Env env, const Exp_Array* array) { CHECK_OO((array->array->type = check_exp(env, array->base))) - CHECK_OO(check_subscripts(env, array->array)) + CHECK_BO(check_subscripts(env, array->array)) return at_depth(env, array->array); } @@ -1065,7 +1065,11 @@ ANN static m_bool check_stmt_return(const Env env, const Stmt_Exp stmt) { } if(isa(ret_type, env->func->def->base->ret_type) > 0) return GW_OK; - return check_implicit(env, stmt->val, env->func->def->base->ret_type); + if(stmt->val) + return check_implicit(env, stmt->val, env->func->def->base->ret_type); + if(isa(env->func->def->base->ret_type, env->gwion->type[et_void]) > 0) + return GW_OK; + ERR_B(stmt_self(stmt)->pos, _("missing value for return statement")) } #define describe_check_stmt_stack(stack, name) \ diff --git a/src/parse/scan0.c b/src/parse/scan0.c index 859be604..66afbc67 100644 --- a/src/parse/scan0.c +++ b/src/parse/scan0.c @@ -294,7 +294,6 @@ ANN static Type scan0_class_def_init(const Env env, const Class_Def cdef) { const Type t = scan0_type(env, ++env->scope->type_xid, s_name(cdef->base.xid), env->gwion->type[et_object]); t->e->owner = env->curr; t->nspc = new_nspc(env->gwion->mp, t->name); -// t->nspc->parent = GET_FLAG(cdef, global) ? env_nspc(env) : env->curr; t->nspc->parent = env->curr; t->e->def = cdef; t->flag = cdef->flag; diff --git a/src/parse/scan1.c b/src/parse/scan1.c index bd864bf4..42e78f61 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -72,6 +72,8 @@ ANN m_bool scan1_exp_decl(const Env env, const Exp_Decl* decl) { ((Exp_Decl*)decl)->type = scan1_exp_decl_type(env, (Exp_Decl*)decl); CHECK_OB(decl->type) const m_bool global = GET_FLAG(decl->td, global); + if(global && decl->type->e->owner != env->global_nspc) + ERR_B(exp_self(decl)->pos, _("type '%s' is not global\n"), decl->type->name) if(env->context) env->context->global = 1; const m_uint scope = !global ? env->scope->depth : env_push_global(env); diff --git a/src/parse/scan2.c b/src/parse/scan2.c index 8323f549..5f8e0430 100644 --- a/src/parse/scan2.c +++ b/src/parse/scan2.c @@ -365,6 +365,7 @@ ANN2(1, 2) static m_bool scan2_func_def_template(const Env env, const Func_Def f const Value v = nspc_lookup_value1(nspc, f->base->xid); if(v) { Func ff = v->d.func_ref; + if(!ff)continue; do { if(ff->def == f) { ++i; @@ -446,6 +447,7 @@ ANN static m_str func_tmpl_name(const Env env, const Func_Def f) { vector_init(&v); do { const Type t = nspc_lookup_type0(env->curr, id->xid); + if(!t)continue; vector_add(&v, (vtype)t); tlen += strlen(t->name); } while((id = id->next) && ++tlen); -- 2.43.0