From b196ec55a89455648d9c051aaa5cb896adf961ab Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Fri, 9 Aug 2019 02:37:18 +0200 Subject: [PATCH] :art: Introduce TupleForm --- include/type.h | 13 ++++++++++--- src/lib/tuple.c | 42 +++++++++++++++++++++++++++++------------- src/oo/type.c | 20 +++++++------------- src/parse/check.c | 6 +++--- 4 files changed, 49 insertions(+), 32 deletions(-) diff --git a/include/type.h b/include/type.h index ddb647eb..6e3ae9ec 100644 --- a/include/type.h +++ b/include/type.h @@ -1,6 +1,13 @@ #ifndef __TYPE #define __TYPE +typedef struct TupleForm_* TupleForm; +struct TupleForm_ { + struct Vector_ types; + struct Vector_ offset; + Type_List list; +}; + struct TypeInfo_ { Type parent; Nspc owner; @@ -10,9 +17,7 @@ struct TypeInfo_ { Type base_type; } d; struct Vector_ contains; - struct Vector_ tuple_form; - struct Vector_ tuple_offset; - Type_List tuple_tl; + struct TupleForm_* tuple; }; struct Type_ { @@ -58,5 +63,7 @@ ANN m_uint get_depth(const Type type); ANN Type tuple_type(const Env, const Vector, const loc_t); ANN void tuple_info(const Env, Type_Decl*, const Var_Decl); +ANN TupleForm new_tupleform(MemPool p); +ANN void free_tupleform(MemPool p, const TupleForm tuple); #endif diff --git a/src/lib/tuple.c b/src/lib/tuple.c index 93ec09a1..c82e7a2c 100644 --- a/src/lib/tuple.c +++ b/src/lib/tuple.c @@ -45,8 +45,8 @@ INSTR(TupleMember) { const M_Object o = *(M_Object*)(shred->reg - SZ_INT); const Type base = o->type_ref; const m_bit* byte = shred->code->bytecode + shred->pc * BYTECODE_SZ; - const Type t = (Type)vector_at(&base->e->tuple_form, instr->m_val); - const m_uint offset = vector_at(&base->e->tuple_offset, instr->m_val); + const Type t = (Type)vector_at(&base->e->tuple->types, instr->m_val); + const m_uint offset = vector_at(&base->e->tuple->offset, instr->m_val); *(m_uint*)(byte + SZ_INT) = offset; if(!instr->m_val2) { if(t->size == SZ_INT) @@ -110,8 +110,8 @@ ANN void emit_unpack_instr(const Emitter emit, struct TupleEmit *te) { } static m_bool tuple_match(const Env env, const Type type[2]) { - const Vector lv = &type[0]->e->tuple_form; - const Vector rv = &type[1]->e->tuple_form; + const Vector lv = &type[0]->e->tuple->types; + const Vector rv = &type[1]->e->tuple->types; for(m_uint i = 0; i < vector_size(rv); i++) { DECL_OB(const Type, l, = (Type)vector_at(lv, i)) const Type r = (Type)vector_at(rv, i); @@ -130,7 +130,7 @@ static OP_CHECK(opck_at_tuple) { do { if(e->exp_type == ae_exp_decl) { e->d.exp_decl.td->xid->xid = insert_symbol(//could be better - ((Type)VPTR(&bin->lhs->type->e->tuple_form, i))->name); + ((Type)VPTR(&bin->lhs->type->e->tuple->types, i))->name); CHECK_BO(traverse_decl(env, &e->d.exp_decl)) } ++i; @@ -173,7 +173,7 @@ static OP_EMIT(opem_at_tuple) { return GW_OK; } const Exp e = bin->rhs->d.exp_primary.d.tuple.exp; - const Vector v = &bin->lhs->type->e->tuple_form; + const Vector v = &bin->lhs->type->e->tuple->types; struct TupleEmit te = { .e=e, .v=v }; emit_unpack_instr(emit, &te); return GW_OK; @@ -181,9 +181,9 @@ static OP_EMIT(opem_at_tuple) { ANN void tuple_info(const Env env, Type_Decl *base, const Var_Decl var) { const Value v = var->value; - const m_uint offset = vector_back(&env->class_def->e->tuple_offset); - vector_add(&env->class_def->e->tuple_form, (vtype)v->type); - vector_add(&env->class_def->e->tuple_offset, offset + v->type->size); + const m_uint offset = vector_back(&env->class_def->e->tuple->offset); + vector_add(&env->class_def->e->tuple->types, (vtype)v->type); + vector_add(&env->class_def->e->tuple->offset, offset + v->type->size); Type_Decl *td = cpy_type_decl(env->gwion->mp, base); if(var->array) { if(td->array) @@ -191,13 +191,13 @@ ANN void tuple_info(const Env env, Type_Decl *base, const Var_Decl var) { else td->array = cpy_array_sub(env->gwion->mp, var->array); } - if(env->class_def->e->tuple_tl) { - Type_List tl = env->class_def->e->tuple_tl; + if(env->class_def->e->tuple->list) { + Type_List tl = env->class_def->e->tuple->list; while(tl->next) tl = tl->next; tl->next = new_type_list(env->gwion->mp, td, NULL); } else - env->class_def->e->tuple_tl = new_type_list(env->gwion->mp, td, NULL); + env->class_def->e->tuple->list = new_type_list(env->gwion->mp, td, NULL); } INSTR(TupleCtor) { @@ -288,10 +288,26 @@ ANN Type tuple_type(const Env env, const Vector v, const loc_t pos) { cdef->base.tmpl = tmpl; CHECK_BO(scan0_class_def(env, cdef)) SET_FLAG(cdef->base.type, abstract); - cdef->base.type->e->tuple_tl = tlbase; + cdef->base.type->e->tuple->list = tlbase; // CHECK_BO(scan1_cdef(env, cdef)) CHECK_BO(traverse_cdef(env, cdef)) nspc_add_type(env->curr, sym, cdef->base.type); // map_set((Map)vector_front(&env->curr->info->type), sym, cdef->base.type); return cdef->base.type; } + +ANN TupleForm new_tupleform(MemPool p) { + TupleForm tuple = mp_calloc(p, TupleForm); + vector_init(&tuple->types); + vector_init(&tuple->offset); + vector_add(&tuple->offset, 0); + tuple->list = NULL; + return tuple; +} + +ANN void free_tupleform(MemPool p, const TupleForm tuple) { + vector_release(&tuple->types); + vector_release(&tuple->offset); + if(tuple->list) + free_type_list(p, tuple->list); +} diff --git a/src/oo/type.c b/src/oo/type.c index d62b8397..f8707e7a 100644 --- a/src/oo/type.c +++ b/src/oo/type.c @@ -23,12 +23,8 @@ ANN static void free_type(Type a, Gwion gwion) { } if(a->nspc) REM_REF(a->nspc, gwion); - if(a->e->tuple_form.ptr) - vector_release(&a->e->tuple_form); - if(a->e->tuple_offset.ptr) - vector_release(&a->e->tuple_offset); - if(a->e->tuple_tl) - free_type_list(gwion->mp, a->e->tuple_tl); + if(a->e->tuple) + free_tupleform(gwion->mp, a->e->tuple); if(a->e->contains.ptr) { for(m_uint i = 0; i < vector_size(&a->e->contains); ++i) REM_REF((Type)vector_at(&a->e->contains, i), gwion); @@ -46,9 +42,7 @@ Type new_type(MemPool p, const m_uint xid, const m_str name, const Type parent) type->e->parent = parent; if(type->e->parent) { type->size = parent->size; - vector_init(&type->e->tuple_form); - vector_init(&type->e->tuple_offset); - vector_add(&type->e->tuple_offset, 0); + type->e->tuple = new_tupleform(p); } type->ref = new_refcount(p, free_type); return type; @@ -63,10 +57,10 @@ ANN Type type_copy(MemPool p, const Type type) { a->array_depth = type->array_depth; a->e->def = type->e->def; if(t_function && isa(type, t_function) > 0) { - vector_release(&a->e->tuple_form); - a->e->tuple_form.ptr = NULL; - vector_release(&a->e->tuple_offset); - a->e->tuple_offset.ptr = NULL; + if(a->e->tuple) { + free_tupleform(p, a->e->tuple); + a->e->tuple = NULL; + } } return a; } diff --git a/src/parse/check.c b/src/parse/check.c index a40180e4..9f7b5d44 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -370,7 +370,7 @@ static m_bool array_access_valid(const Env env, const Exp_Array* array) { if(e->exp_type != ae_exp_primary || e->d.exp_primary.primary_type != ae_primary_num) ERR_B(exp_self(array)->pos, _("tuple subscripts must be litteral")) - if((Type)vector_at(&type->e->tuple_form, e->d.exp_primary.d.num) == t_undefined) + if((Type)vector_at(&type->e->tuple->types, e->d.exp_primary.d.num) == t_undefined) ERR_B(exp_self(array)->pos, _("tuple subscripts is undefined")) return 0; } @@ -391,9 +391,9 @@ static ANN Type check_exp_array(const Env env, const Exp_Array* array) { e = e->next; // if we implement tuple with no type, err_msg const Type type = array_base(array->base->type) ?: array->base->type; - if(e->d.exp_primary.d.num >= vector_size(&type->e->tuple_form)) + if(e->d.exp_primary.d.num >= vector_size(&type->e->tuple->types)) ERR_O(exp_self(array)->pos, "Invalid tuple subscript") - return (Type)vector_at(&type->e->tuple_form, e->d.exp_primary.d.num); + return (Type)vector_at(&type->e->tuple->types, e->d.exp_primary.d.num); } return at_depth(env, array->base->type, array->array->depth); } -- 2.43.0