From ae306f13681aff021c2c161fcebe5b1e4cca4ac6 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Mon, 1 Apr 2019 13:53:05 +0200 Subject: [PATCH] :art: No more static symbol table --- ast | 2 +- include/func.h | 2 +- include/gwion.h | 1 + include/parse.h | 5 ++++ include/type.h | 6 ++-- src/compile.c | 2 +- src/emit/emit.c | 18 ++++++------ src/gwion.c | 3 +- src/lib/func.c | 6 ++-- src/lib/import.c | 62 +++++++++++++++++++++--------------------- src/lib/instr.c | 10 +++---- src/lib/object.c | 3 +- src/lib/ptr.c | 7 +++-- src/lib/string.c | 3 +- src/oo/env.c | 2 ++ src/oo/env_utils.c | 2 ++ src/oo/type.c | 10 ++++--- src/parse/check.c | 26 +++++++++--------- src/parse/func.c | 8 ++++-- src/parse/operator.c | 3 +- src/parse/scan0.c | 18 ++++++------ src/parse/scan1.c | 3 +- src/parse/scan2.c | 21 +++++++------- src/parse/template.c | 16 ++++++----- src/parse/type_decl.c | 12 +++++--- src/parse/type_utils.c | 2 ++ util | 2 +- 27 files changed, 143 insertions(+), 112 deletions(-) diff --git a/ast b/ast index e04d8382..0112a154 160000 --- a/ast +++ b/ast @@ -1 +1 @@ -Subproject commit e04d8382825ddf63f52b834dee13d8b85902fb21 +Subproject commit 0112a154df71cafe08e69427e0d8487cb9bdcafb diff --git a/include/func.h b/include/func.h index ef7992a3..382aa47d 100644 --- a/include/func.h +++ b/include/func.h @@ -13,6 +13,6 @@ struct Func_ { }; ANEW ANN Func new_func(const m_str, const Func_Def); -ANN2(1,2) Symbol func_symbol(const m_str, const m_str, const m_str, const m_uint); +ANN2(1,2) Symbol func_symbol(const Env, const m_str, const m_str, const m_str, const m_uint); ANN2(1,3,4) m_bool check_lambda(const Env, const Type, Exp_Lambda*, const Func_Def); #endif diff --git a/include/gwion.h b/include/gwion.h index 431af256..622c18dc 100644 --- a/include/gwion.h +++ b/include/gwion.h @@ -13,6 +13,7 @@ struct Gwion_ { Emitter emit; VM* vm; struct Map_ freearg; + SymTable *st; }; ANN m_bool gwion_ini(const Gwion, struct Arg_*); diff --git a/include/parse.h b/include/parse.h index 28a2d1ae..f1c0fa5a 100644 --- a/include/parse.h +++ b/include/parse.h @@ -1,5 +1,10 @@ #ifndef __PARSE #define __PARSE + +#include "gwion.h" +#define insert_symbol(a) insert_symbol(env->gwion->st, (a)) + + #define RET_NSPC(exp) \ ++env->scope->depth; \ nspc_push_value(env->curr); \ diff --git a/include/type.h b/include/type.h index 37dd9c15..c59d8cb4 100644 --- a/include/type.h +++ b/include/type.h @@ -25,12 +25,12 @@ Type t_void, t_int, t_float, t_dur, t_time, t_now, t_complex, t_polar, t_vec3, t ANN2(2) ANEW Type new_type(const m_uint xid, const m_str name, const Type); ANEW ANN Type type_copy(const Type type); -ANN m_str get_type_name(const m_str, const m_uint); +ANN m_str get_type_name(const Env, const m_str, const m_uint); ANN Value find_value(const Type, const Symbol); ANN Func find_func(const Type, const Symbol); ANN m_bool isa(const Type, const Type) __attribute__((pure)); ANN m_bool isres(const Symbol); -ANN Type array_type(const Type, const m_uint); +ANN Type array_type(const Env, const Type, const m_uint); ANN Type find_common_anc(const Type, const Type) __attribute__((pure)); ANN m_uint id_list_len(ID_List); ANN void type_path(const m_str, const ID_List); @@ -38,7 +38,7 @@ ANN Type typedef_base(Type) __attribute__((pure)); ANN Type array_base(Type) __attribute__((pure)); ANN m_bool type_ref(Type) __attribute__((pure)); __attribute__((returns_nonnull)) -ANN Type template_parent(const Type type); +ANN Type template_parent(const Env, const Type type); static inline Type actual_type(const Type t) { return isa(t, t_class) > 0 ? t->d.base_type : t; } diff --git a/src/compile.c b/src/compile.c index 5e3b1fba..0d1c481d 100644 --- a/src/compile.c +++ b/src/compile.c @@ -60,7 +60,7 @@ static m_bool compiler_open(struct Compiler* c) { static m_bool check(struct Gwion_* gwion, struct Compiler* c) { CHECK_BB(compiler_open(c)) - CHECK_OB((c->ast = parse(c->name, c->file))) + CHECK_OB((c->ast = parse(gwion->st, c->name, c->file))) gwion->env->name = c->name; return type_engine_check_prog(gwion->env, c->ast); } diff --git a/src/emit/emit.c b/src/emit/emit.c index 9c6286ec..75bfa88f 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -22,6 +22,9 @@ #include "import.h" #include "switch.h" +#undef insert_symbol +#define insert_symbol(a) insert_symbol(emit->env->gwion->st, (a)) + typedef struct Local_ { m_uint size; m_uint offset; @@ -148,7 +151,7 @@ ANN static void emit_pre_ctor(const Emitter emit, const Type type) { GWDEBUG_EXE if(type->nspc->pre_ctor) emit_ext_ctor(emit, type->nspc->pre_ctor); if(GET_FLAG(type, template) && GET_FLAG(type, builtin)) { - const Type t = template_parent(type); + const Type t = template_parent(emit->env, type); if(t->nspc->pre_ctor) emit_ext_ctor(emit, t->nspc->pre_ctor); } @@ -177,7 +180,7 @@ ANN ArrayInfo* emit_array_extend_inner(const Emitter emit, const Type t, const E ArrayInfo* info = mp_alloc(ArrayInfo); vector_init(&info->type); for(m_uint i = 1; i < t->array_depth; ++i) - vector_add(&info->type, (vtype)array_type(base, i)); + vector_add(&info->type, (vtype)array_type(emit->env, base, i)); vector_add(&info->type, (vtype)t); info->depth = (m_int)t->array_depth; info->base = base; @@ -248,20 +251,20 @@ static const f_instr dotmember[] = { DotMember, DotMember2, DotMember3, DotMemb static const f_instr allocmember[] = { RegPushImm, RegPushImm2, RegPushImm3, AllocMember4 }; static const f_instr allocword[] = { AllocWord, AllocWord2, AllocWord3, AllocWord4 }; -ANN static inline Exp this_exp(const Type t, const uint pos) { +ANN static inline Exp this_exp(const Emitter emit, const Type t, const uint pos) { const Exp exp = new_exp_prim_id(insert_symbol("this"), pos); exp->type = t; return exp; } -ANN static inline Exp dot_this_exp(const Exp_Primary* prim, const Type t) { - const Exp exp = this_exp(t, prim->self->pos); +ANN static inline Exp dot_this_exp(const Emitter emit, const Exp_Primary* prim, const Type t) { + const Exp exp = this_exp(emit, t, prim->self->pos); const Exp dot = new_exp_dot(exp, prim->d.var); dot->d.exp_dot.t_base = t; return dot; } -ANN static inline Exp dot_static_exp(const Exp_Primary* prim, const Type t) { +ANN static inline Exp dot_static_exp(const Emitter emit, const Exp_Primary* prim, const Type t) { const Symbol s = insert_symbol(t->name); const Exp e = new_exp_prim_id(s, prim->self->pos); const Value val = nspc_lookup_value1(t->nspc->parent, s); @@ -272,8 +275,7 @@ ANN static inline Exp dot_static_exp(const Exp_Primary* prim, const Type t) { ANN static m_bool emit_symbol_owned(const Emitter emit, const Exp_Primary* prim) { const Value v = prim->value; - const Exp dot = !GET_FLAG(v, static) ? - dot_this_exp(prim, v->owner_class) : dot_static_exp(prim, v->owner_class); + const Exp dot = (!GET_FLAG(v, static) ? dot_this_exp : dot_static_exp)(emit, prim, v->owner_class); dot->type = v->type; dot->emit_var = prim->self->emit_var; const m_bool ret = emit_exp_dot(emit, &dot->d.exp_dot); diff --git a/src/gwion.c b/src/gwion.c index 72834153..fe5ad9c4 100644 --- a/src/gwion.c +++ b/src/gwion.c @@ -64,6 +64,7 @@ ANN VM* gwion_cpy(const VM* src) { return gwion->vm; } ANN m_bool gwion_ini(const Gwion gwion, Arg* arg) { + gwion->st = new_symbol_table(65347); gwion->vm = new_vm(); gwion->emit = new_emitter(); gwion->env = new_env(); @@ -103,5 +104,5 @@ ANN void gwion_end(const Gwion gwion) { free_vm(gwion->vm); free_plug(gwion); map_release(&gwion->freearg); - free_symbols(); + free_symbols(gwion->st); } diff --git a/src/lib/func.c b/src/lib/func.c index 5333c438..fa17d921 100644 --- a/src/lib/func.c +++ b/src/lib/func.c @@ -42,14 +42,14 @@ static OP_EMIT(opem_func_assign) { } return GW_OK; } -ANN static Type fptr_type(Exp_Binary* bin) { +ANN static Type fptr_type(const Env env, Exp_Binary* bin) { const Func l_func = bin->lhs->type->d.func; const Func r_func = bin->rhs->type->d.func; const Nspc nspc = l_func->value_ref->owner; const m_str c = s_name(l_func->def->name); const Value v = l_func->value_ref; for(m_uint i = 0; i <= v->offset; ++i) { - const Symbol sym = func_symbol(nspc->name, c, NULL, i); + const Symbol sym = func_symbol(env, nspc->name, c, NULL, i); const Func f = nspc_lookup_func1(nspc, sym); // was lookup2 CHECK_OO(f) if(compat_func(r_func->def, f->def) > 0) @@ -119,7 +119,7 @@ static OP_CHECK(opck_fptr_at) { r_fdef->ret_type->name, l_fdef->ret_type->name) if(isa(bin->lhs->type, t_fptr) > 0 && isa(bin->lhs->type, bin->rhs->type) > 0) return bin->rhs->type; - return fptr_type(bin); + return fptr_type(env, bin); } static OP_CHECK(opck_fptr_cast) { diff --git a/src/lib/import.c b/src/lib/import.c index 662c263d..1a20bc44 100644 --- a/src/lib/import.c +++ b/src/lib/import.c @@ -24,11 +24,11 @@ struct Path { m_uint len; }; -ANN static ID_List templater_def(const Templater* templater) { +ANN static ID_List templater_def(SymTable *st, const Templater* templater) { ID_List list[templater->n]; - list[0] = new_id_list(insert_symbol(templater->list[0]), 0); + list[0] = new_id_list(insert_symbol(st, templater->list[0]), 0); for(m_uint i = 1; i < templater->n; i++) { - list[i] = new_id_list(insert_symbol(templater->list[i]), 0); + list[i] = new_id_list(insert_symbol(st, templater->list[i]), 0); list[i - 1]->next = list[i]; } return list[0]; @@ -120,7 +120,7 @@ ANN static void path_valid_inner(const m_str curr) { } } -ANN static m_bool path_valid(ID_List* list, const struct Path* p) { +ANN static m_bool path_valid(SymTable *st,ID_List* list, const struct Path* p) { char last = '\0'; for(m_uint i = p->len + 1; --i;) { const char c = p->path[i - 1]; @@ -130,7 +130,7 @@ ANN static m_bool path_valid(ID_List* list, const struct Path* p) { if((i != 1 && last != '.' && last != '\0') || (i == 1 && c != '.')) { path_valid_inner(p->curr); - *list = prepend_id_list(insert_symbol(p->curr), *list, 0); + *list = prepend_id_list(insert_symbol(st, p->curr), *list, 0); memset(p->curr, 0, p->len + 1); } else ERR_B(0, "path '%s' must not ini or end with '.'.", p->path) @@ -140,7 +140,7 @@ ANN static m_bool path_valid(ID_List* list, const struct Path* p) { return GW_OK; } -ANN static ID_List str2list(const m_str path, m_uint* array_depth) { +ANN static ID_List str2list(SymTable *st, const m_str path, m_uint* array_depth) { const m_uint len = strlen(path); ID_List list = NULL; m_uint depth = 0; @@ -153,14 +153,14 @@ ANN static ID_List str2list(const m_str path, m_uint* array_depth) { p.len -= 2; } *array_depth = depth; - if(path_valid(&list, &p) < 0) { + if(path_valid(st, &list, &p) < 0) { if(list) free_id_list(list); return NULL; } CHECK_OO(list) strncpy(curr, path, p.len); - list->xid = insert_symbol(curr); + list->xid = insert_symbol(st, curr); return list; } @@ -208,8 +208,8 @@ ANN2(1,2) m_int gwi_class_ini(const Gwi gwi, const Type type, const f_xtor pre_c if(type->nspc) ERR_B(0, "during import: class '%s' already imported.", type->name) if(gwi->templater.n) { - const ID_List types = templater_def(&gwi->templater); - type->def = new_class_def(0, insert_symbol(type->name), NULL, NULL); + const ID_List types = templater_def(gwi->gwion->st,&gwi->templater); + type->def = new_class_def(0, insert_symbol(gwi->gwion->st, type->name), NULL, NULL); type->def->tmpl = new_tmpl_class(types, -1); type->def->type = type; SET_FLAG(type, template); @@ -301,10 +301,10 @@ ANN static void dl_var_release(const DL_Var* v) { ANN m_int gwi_item_ini(const Gwi gwi, const restrict m_str type, const restrict m_str name) { DL_Var* v = &gwi->var; memset(v, 0, sizeof(DL_Var)); - if(!(v->t.xid = str2list(type, &v->array_depth))) + if(!(v->t.xid = str2list(gwi->gwion->st, type, &v->array_depth))) ERR_B(0, "\t...\tduring var import '%s.%s'.", gwi->gwion->env->class_def->name, name) - v->var.xid = insert_symbol(name); + v->var.xid = insert_symbol(gwi->gwion->st, name); return GW_OK; } @@ -361,9 +361,9 @@ 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(s, i++); + m_str type_name = get_type_name(env, s, i++); CHECK_OO(type_name) - ID_List id = str2list(type_name, depth); + ID_List id = str2list(env->gwion->st, type_name, depth); CHECK_OO(id) Type_Decl* td = new_type_decl(id, 0); Type_List tmp = NULL; @@ -371,7 +371,7 @@ ANN Type_Decl* str2decl(const Env env, const m_str s, m_uint *depth) { free_id_list(id); return NULL; } - while((type_name = get_type_name(s, i++))) { + while((type_name = get_type_name(env, s, i++))) { m_uint d = 0; if(!tmp) td->types = tmp = str2tl(env, type_name, &d); @@ -399,7 +399,7 @@ ANN static Arg_List make_dll_arg_list(const Env env, DL_Func * dl_fun) { free_arg_list(arg_list); ERR_O(0, "\t...\tat argument '%i'", i + 1) } - if((type_path2 = str2list(arg->name, &array_depth2))) + if((type_path2 = str2list(env->gwion->st, arg->name, &array_depth2))) free_id_list(type_path2); if(array_depth && array_depth2) { free_type_decl(type_decl); @@ -408,7 +408,7 @@ ANN static Arg_List make_dll_arg_list(const Env env, DL_Func * dl_fun) { ERR_O(0, "array subscript specified incorrectly for built-in module") } array_sub = make_dll_arg_list_array(array_sub, &array_depth, array_depth2); - var_decl = new_var_decl(insert_symbol(arg->name), array_sub, 0); + var_decl = new_var_decl(insert_symbol(env->gwion->st, arg->name), array_sub, 0); arg_list = new_arg_list(type_decl, var_decl, arg_list); } return arg_list; @@ -423,7 +423,7 @@ ANN static Func_Def make_dll_as_fun(const Env env, DL_Func * dl_fun, ae_flag fla m_uint i, array_depth = 0; flag |= ae_flag_builtin; - if(!(type_path = str2list(dl_fun->type, &array_depth)) || + if(!(type_path = str2list(env->gwion->st, dl_fun->type, &array_depth)) || !(type_decl = new_type_decl(type_path, 0))) ERR_O(0, "\t...\tduring @ function import '%s' (type).", dl_fun->name) if(array_depth) { @@ -434,7 +434,7 @@ ANN static Func_Def make_dll_as_fun(const Env env, DL_Func * dl_fun, ae_flag fla } name = dl_fun->name; arg_list = make_dll_arg_list(env, dl_fun); - func_def = new_func_def(type_decl, insert_symbol(name), arg_list, NULL, flag); + func_def = new_func_def(type_decl, insert_symbol(env->gwion->st, name), arg_list, NULL, flag); func_def->d.dl_func_ptr = (void*)(m_uint)dl_fun->addr; return func_def; } @@ -449,7 +449,7 @@ ANN m_int gwi_func_end(const Gwi gwi, const ae_flag flag) { CHECK_OB(def) if(gwi->templater.n) { def = new_func_def(NULL, NULL, NULL, NULL, 0); - const ID_List list = templater_def(&gwi->templater); + const ID_List list = templater_def(gwi->gwion->st, &gwi->templater); def->tmpl = new_tmpl_list(list, -1); SET_FLAG(def, template); } @@ -468,11 +468,11 @@ ANN m_int gwi_func_end(const Gwi gwi, const ae_flag flag) { static Type get_type(const Env env, const m_str str) { m_uint depth = 0; - const ID_List list = (str && str != (m_str)OP_ANY_TYPE) ? str2list(str, &depth) : NULL; + const ID_List list = (str && str != (m_str)OP_ANY_TYPE) ? str2list(env->gwion->st, str, &depth) : NULL; const Type t = (str == (m_str) OP_ANY_TYPE) ? OP_ANY_TYPE : list ? find_type(env, list) : NULL; if(list) free_id_list(list); - return t ? (depth ? array_type(t, depth) : t) : NULL; + return t ? (depth ? array_type(env, t, depth) : t) : NULL; } ANN2(1,2) static int import_op(const Env env, const DL_Oper* op, @@ -528,11 +528,11 @@ ANN static Stmt import_fptr(const Env env, DL_Func* dl_fun, ae_flag flag) { Type_Decl* type_decl = NULL; const Arg_List args = make_dll_arg_list(env, dl_fun); flag |= ae_flag_builtin; - if(!(type_path = str2list(dl_fun->type, &array_depth)) || + if(!(type_path = str2list(env->gwion->st, dl_fun->type, &array_depth)) || !(type_decl = new_type_decl(type_path, 0))) ERR_O(0, "\t...\tduring fptr import '%s' (type).", dl_fun->name) - return new_stmt_fptr(insert_symbol(dl_fun->name), type_decl, args, flag); + return new_stmt_fptr(insert_symbol(env->gwion->st, dl_fun->name), type_decl, args, flag); } ANN m_int gwi_fptr_end(const Gwi gwi, const ae_flag flag) { @@ -548,30 +548,30 @@ ANN m_int gwi_fptr_end(const Gwi gwi, const ae_flag flag) { return GW_OK; } -ANN static Exp make_exp(const m_str type, const m_str name) { +ANN static Exp make_exp(SymTable *st, const m_str type, const m_str name) { Type_Decl *type_decl; ID_List id_list; m_uint array_depth; Array_Sub array = NULL; - CHECK_OO((id_list = str2list(type, &array_depth))) + CHECK_OO((id_list = str2list(st, type, &array_depth))) if(array_depth) { array = new_array_sub(NULL); array->depth = array_depth; } type_decl = new_type_decl(id_list, 0); - const Var_Decl var_decl = new_var_decl(insert_symbol(name), array, 0); + const Var_Decl var_decl = new_var_decl(insert_symbol(st, name), array, 0); const Var_Decl_List var_decl_list = new_var_decl_list(var_decl, NULL); return new_exp_decl(type_decl, var_decl_list); } ANN2(1) m_int gwi_union_ini(const Gwi gwi, const m_str name) { if(name) - gwi->union_data.xid = insert_symbol(name); + gwi->union_data.xid = insert_symbol(gwi->gwion->st, name); return GW_OK; } ANN m_int gwi_union_add(const Gwi gwi, const restrict m_str type, const restrict m_str name) { - const Exp exp = make_exp(type, name); + const Exp exp = make_exp(gwi->gwion->st, type, name); CHECK_OB(exp); const Type t = type_decl_resolve(gwi->gwion->env, exp->d.exp_decl.td); if(!t) @@ -605,7 +605,7 @@ ANN2(1) m_int gwi_enum_ini(const Gwi gwi, const m_str type) { } ANN m_int gwi_enum_add(const Gwi gwi, const m_str name, const m_uint i) { - const ID_List list = new_id_list(insert_symbol(name), 0); + const ID_List list = new_id_list(insert_symbol(gwi->gwion->st, name), 0); DL_Enum* d = &gwi->enum_data; ALLOC_PTR(addr, m_int, i); vector_add(&gwi->enum_data.addr, (vtype)addr); @@ -635,7 +635,7 @@ ANN static void import_enum_end(const Gwi gwi, const Vector v) { ANN m_int gwi_enum_end(const Gwi gwi) { DL_Enum* d = &gwi->enum_data; - const Stmt stmt = new_stmt_enum(d->base, d->t ? insert_symbol(d->t) : NULL); + const Stmt stmt = new_stmt_enum(d->base, d->t ? insert_symbol(gwi->gwion->st, d->t) : NULL); if(traverse_stmt_enum(gwi->gwion->env, &stmt->d.stmt_enum) < 0) { free_id_list(d->base); return GW_ERROR; diff --git a/src/lib/instr.c b/src/lib/instr.c index b070d141..e7f57a54 100644 --- a/src/lib/instr.c +++ b/src/lib/instr.c @@ -61,13 +61,13 @@ INSTR(PopArrayClass) { GWDEBUG_EXE #include "value.h" #include "template.h" -ANN static Func_Def from_base(const struct dottmpl_ *dt, const Type t) { - const Symbol sym = func_symbol(t->name, s_name(dt->base->name), +ANN static Func_Def from_base(const Env env, const struct dottmpl_ *dt, const Type t) { + const Symbol sym = func_symbol(env, t->name, s_name(dt->base->name), "template", dt->overload); const Value v = nspc_lookup_value1(t->nspc, sym); CHECK_OO(v) const Func_Def base = v->d.func_ref->def; - const Func_Def def = new_func_def(base->td, insert_symbol(v->name), + const Func_Def def = new_func_def(base->td, insert_symbol(env->gwion->st, v->name), base->arg_list, base->d.code, base->flag); def->tmpl = new_tmpl_list(base->tmpl->list, dt->overload); SET_FLAG(def, template); @@ -85,7 +85,7 @@ INSTR(DotTmpl) { char str[instr->m_val2 + strlen(t->name) + 1]; strcpy(str, name); strcpy(str + instr->m_val2, t->name); - const Func f = nspc_lookup_func1(t->nspc, insert_symbol(str)); + const Func f = nspc_lookup_func1(t->nspc, insert_symbol(emit->env->gwion->st, str)); if(f) { if(!f->code) { dt->def = f->def;// @@ -97,7 +97,7 @@ INSTR(DotTmpl) { shred->reg += SZ_INT; return; } else { - const Func_Def def = from_base(dt, t); + const Func_Def def = from_base(emit->env, dt, t); if(!def) continue; dt->def = def; // diff --git a/src/lib/object.c b/src/lib/object.c index 9303d0e5..2960f520 100644 --- a/src/lib/object.c +++ b/src/lib/object.c @@ -12,6 +12,7 @@ #include "object.h" #include "import.h" #include "operator.h" +#include "gwion.h" ANN void exception(const VM_Shred shred, const m_str c) { err_msg(0, "%s: shred[id=%" UINT_F ":%s], PC=[%" UINT_F "]", @@ -38,7 +39,7 @@ M_Object new_object(const VM_Shred shred, const Type t) { M_Object new_string(const VM_Shred shred, const m_str str) { const M_Object o = new_object(shred, t_string); - STRING(o) = s_name(insert_symbol(str)); + STRING(o) = s_name(insert_symbol(shred->info->vm->gwion->st, str)); return o; } diff --git a/src/lib/ptr.c b/src/lib/ptr.c index 1776e6f0..f9601b97 100644 --- a/src/lib/ptr.c +++ b/src/lib/ptr.c @@ -12,12 +12,13 @@ #include "import.h" #include "emit.h" #include "operator.h" +#include "gwion.h" static OP_CHECK(opck_ptr_assign) { const Exp_Binary* bin = (Exp_Binary*)data; Type t = bin->lhs->type; do { - if(!strcmp(t->name, get_type_name(bin->rhs->type->name, 1))) { + if(!strcmp(t->name, get_type_name(env, bin->rhs->type->name, 1))) { if(bin->lhs->meta != ae_meta_var) ERR_N(0, "left side operand is constant") bin->lhs->emit_var = 1; @@ -35,14 +36,14 @@ static INSTR(instr_ptr_assign) { GWDEBUG_EXE static OP_CHECK(opck_ptr_deref) { const Exp_Unary* unary = (Exp_Unary*)data; - unary->self->type = nspc_lookup_type1(unary->exp->type->owner, insert_symbol(get_type_name(unary->exp->type->name, 1))); + unary->self->type = nspc_lookup_type1(unary->exp->type->owner, insert_symbol(env->gwion->st, get_type_name(env, unary->exp->type->name, 1))); return unary->self->type; } static OP_CHECK(opck_implicit_ptr) { const struct Implicit* imp = (struct Implicit*)data; const Exp e = (Exp)imp->e; - if(!strcmp(get_type_name(imp->t->name, 1), e->type->name)) { + if(!strcmp(get_type_name(env, imp->t->name, 1), e->type->name)) { if(e->meta == ae_meta_value) ERR_N(0, "can't cast constant to Ptr"); e->cast_to = imp->t; diff --git a/src/lib/string.c b/src/lib/string.c index 87f24563..1e2361fb 100644 --- a/src/lib/string.c +++ b/src/lib/string.c @@ -11,9 +11,10 @@ #include "instr.h" #include "object.h" #include "import.h" +#include "gwion.h" ANN static void push_string(const VM_Shred shred, const M_Object obj, const m_str c) { - STRING(obj) = s_name(insert_symbol(c)); + STRING(obj) = s_name(insert_symbol(shred->info->vm->gwion->st, c)); *(M_Object*)REG(-SZ_INT) = (M_Object)obj; _release(obj, shred); } diff --git a/src/oo/env.c b/src/oo/env.c index 80b41051..1cd1bf22 100644 --- a/src/oo/env.c +++ b/src/oo/env.c @@ -12,6 +12,8 @@ #include "nspc.h" #include "mpool.h" #include "switch.h" +#include "vm.h" +#include "parse.h" ANN static struct Env_Scope_ *new_scope(void) { struct Env_Scope_ *a = mp_alloc(Env_Scope); diff --git a/src/oo/env_utils.c b/src/oo/env_utils.c index 14c9149c..f218fdce 100644 --- a/src/oo/env_utils.c +++ b/src/oo/env_utils.c @@ -8,6 +8,8 @@ #include "type.h" #include "context.h" #include "nspc.h" +#include "vm.h" +#include "parse.h" ANN Map env_label(const Env env) { return &env->context->lbls; diff --git a/src/oo/type.c b/src/oo/type.c index 257e2305..0d6c9253 100644 --- a/src/oo/type.c +++ b/src/oo/type.c @@ -5,6 +5,8 @@ #include "env.h" #include "type.h" #include "nspc.h" +#include "vm.h" +#include "parse.h" ANN static void free_type(Type a, void *gwion) { if(GET_FLAG(a, template)) @@ -65,7 +67,7 @@ ANN Type array_base(Type type) { return t->d.base_type; } -ANN Type array_type(const Type base, const m_uint depth) { +ANN Type array_type(const Env env, const Type base, const m_uint depth) { m_uint i = depth + 1; size_t len = strlen(base->name); char name[len + 2* depth + 1]; @@ -92,8 +94,8 @@ ANN Type array_type(const Type base, const m_uint depth) { } __attribute__((returns_nonnull)) -ANN Type template_parent(const Type type) { - const m_str name = get_type_name(type->name, 0); +ANN Type template_parent(const Env env, const Type type) { + const m_str name = get_type_name(env, type->name, 0); return nspc_lookup_type1(type->nspc->parent, insert_symbol(name)); } @@ -108,7 +110,7 @@ ANN m_bool type_ref(Type t) { return 0; } -ANN m_str get_type_name(const m_str s, const m_uint index) { +ANN m_str get_type_name(const Env env, const m_str s, const m_uint index) { m_str name = strstr(s, "<"); m_uint i = 0; m_uint lvl = 0; diff --git a/src/parse/check.c b/src/parse/check.c index 30a89a5a..928de22d 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -124,11 +124,11 @@ ANN static m_bool prim_array_inner(const Type t, Type type, const Exp e) { return err_msg(e->pos, "array init [...] contains incompatible types ..."); } -ANN static inline Type prim_array_match(Exp e) { +ANN static inline Type prim_array_match(const Env env, Exp e) { const Type type = e->type; do CHECK_BO(prim_array_inner(e->type, type, e)) while((e = e->next)); - return array_type(type->array_depth ? array_base(type) : type, type->array_depth + 1); + return array_type(env, type->array_depth ? array_base(type) : type, type->array_depth + 1); } ANN static Type prim_array(const Env env, const Exp_Primary* primary) { @@ -137,7 +137,7 @@ ANN static Type prim_array(const Env env, const Exp_Primary* primary) { if(!e) ERR_O(primary->self->pos, "must provide values/expressions for array [...]") CHECK_OO(check_exp(env, e)) - return (array->type = prim_array_match(e)); + return (array->type = prim_array_match(env, e)); } ANN static Value check_non_res_value(const Env env, const Exp_Primary* primary) { @@ -161,7 +161,7 @@ ANN static Type prim_id_non_res(const Env env, const Exp_Primary* primary) { if(!v || !GET_FLAG(v, checked)) { err_msg(primary->self->pos, "variable %s not legit at this point.", s_name(primary->d.var)); - did_you_mean(s_name(primary->d.var)); + did_you_mean(env->gwion->st, s_name(primary->d.var)); return NULL; } if(env->func && !GET_FLAG(v, const) && v->owner) @@ -291,7 +291,7 @@ ANN Type check_exp_array(const Env env, const Exp_Array* array) { GWDEBUG_EXE array->array->depth, t_base->array_depth) } return depth == t_base->array_depth ? array_base(t_base) : - array_type(array_base(t_base), t_base->array_depth - depth); + array_type(env, array_base(t_base), t_base->array_depth - depth); } ANN static Type_List mk_type_list(const Env env, const Type type) { @@ -375,8 +375,8 @@ ANN static m_bool check_call(const Env env, const Exp_Call* exp) { return exp->args ? !!check_exp(env, exp->args) : -1; } -ANN static inline Value template_get_ready(const Value v, const m_str tmpl, const m_uint i) { - const Symbol sym = func_symbol(v->owner->name, v->name, tmpl, i); +ANN static inline Value template_get_ready(const Env env, const Value v, const m_str tmpl, const m_uint i) { + const Symbol sym = func_symbol(env, v->owner->name, v->name, tmpl, i); return v->owner_class ? find_value(v->owner_class, sym) : nspc_lookup_value1(v->owner, sym); } @@ -390,7 +390,7 @@ ANN static Func _find_template_match(const Env env, const Value v, const Exp_Cal for(m_uint i = 0; i < v->offset + 1; ++i) { Func_Def def = NULL; Func_Def base = NULL; - Value value = template_get_ready(v, tmpl_name, i); + Value value = template_get_ready(env, v, tmpl_name, i); if(value) { if(env->func == value->d.func_ref) { free(tmpl_name); @@ -399,13 +399,13 @@ ANN static Func _find_template_match(const Env env, const Value v, const Exp_Cal } base = def = value->d.func_ref->def; if(!def->tmpl) { - if(!(value = template_get_ready(v, "template", i))) + if(!(value = template_get_ready(env, v, "template", i))) continue; base = value->d.func_ref->def; def->tmpl = new_tmpl_list(base->tmpl->list, (m_int)i); } } else { - if(!(value = template_get_ready(v, "template", i))) + if(!(value = template_get_ready(env, v, "template", i))) continue; base = value->d.func_ref->def; def = new_func_def(base->td, insert_symbol(v->name), @@ -784,7 +784,7 @@ ANN static m_bool do_stmt_auto(const Env env, const Stmt_Auto stmt) { GWDEBUG_EX if(!GET_FLAG(ptr, checked)) check_class_def(env, ptr->def); } - t = depth ? array_type(ptr, depth) : ptr; + t = depth ? array_type(env, ptr, depth) : ptr; stmt->v = new_value(t, s_name(stmt->sym)); SET_FLAG(stmt->v, checked); nspc_add_value(env->curr, stmt->sym, stmt->v); @@ -1008,7 +1008,7 @@ ANN static m_bool check_func_args(const Env env, Arg_List arg_list) { GWDEBUG_EX } ANN static inline Func get_overload(const Env env, const Func_Def def, const m_uint i) { - const Symbol sym = func_symbol(env->curr->name, s_name(def->name), NULL, i); + const Symbol sym = func_symbol(env, env->curr->name, s_name(def->name), NULL, i); return nspc_lookup_func1(env->curr, sym); } @@ -1048,7 +1048,7 @@ ANN static Value set_variadic(const Env env) { return variadic; } -ANN static void operator_func(Func f) { +ANN static void operator_func(const Func f) { const Arg_List a = f->def->arg_list; const m_bool is_unary = GET_FLAG(f->def, unary); const Type l = is_unary ? NULL : a->type; diff --git a/src/parse/func.c b/src/parse/func.c index 5451c651..27eb6f02 100644 --- a/src/parse/func.c +++ b/src/parse/func.c @@ -29,6 +29,8 @@ ANN Func new_func(const m_str name, const Func_Def def) { #include #include "env.h" #include "type.h" +#include "vm.h" +#include "gwion.h" ANN Func get_func(const Env env, const Func_Def def) { Func f = def->func; CHECK_OO(f) @@ -40,18 +42,18 @@ ANN Func get_func(const Env env, const Func_Def def) { char c[len + elen + 1]; memcpy(c, f->name, len); strcpy(c + len, env->class_def->name); - return nspc_lookup_func1(env->class_def->nspc, insert_symbol(c)); + return nspc_lookup_func1(env->class_def->nspc, insert_symbol(env->gwion->st, c)); } return f; } -ANN2(1,2) Symbol func_symbol(const m_str nspc, const m_str base, +ANN2(1,2) Symbol func_symbol(const Env env, const m_str nspc, const m_str base, const m_str tmpl, const m_uint i) { char* name; CHECK_BO(asprintf(&name, "%s%s%s%s@%" UINT_F "@%s", base, !tmpl ? "" : "<", !tmpl ? "" : tmpl, !tmpl ? "" : ">", i, nspc)) - const Symbol sym = insert_symbol(name); + const Symbol sym = insert_symbol(env->gwion->st, name); free(name); return sym; } diff --git a/src/parse/operator.c b/src/parse/operator.c index 0ddd7d0f..500da607 100644 --- a/src/parse/operator.c +++ b/src/parse/operator.c @@ -12,6 +12,7 @@ #include "func.h" #include "nspc.h" #include "operator.h" +#include "gwion.h" typedef Type (*f_type)(const Env env, const Exp exp); @@ -55,7 +56,7 @@ ANN static Type op_parent(const Env env, const Type t) { for(size_t i = 0; i < len; i++) c[i] = type->name[i]; c[len] = 0; - return nspc_lookup_type1(env->curr, insert_symbol(c)); + return nspc_lookup_type1(env->curr, insert_symbol(env->gwion->st, c)); } return t->parent; } diff --git a/src/parse/scan0.c b/src/parse/scan0.c index a90f5460..b88e54fa 100644 --- a/src/parse/scan0.c +++ b/src/parse/scan0.c @@ -7,10 +7,12 @@ #include "value.h" #include "func.h" #include "nspc.h" +#include "vm.h" +#include "parse.h" ANN m_bool scan0_class_def(const Env env, const Class_Def class_def); -ANN static Value mk_class(const Type base) { +ANN static Value mk_class(const Env env, const Type base) { const Type t = type_copy(t_class); const Value v = new_value(t, base->name); t->d.base_type = base; @@ -37,7 +39,7 @@ ANN m_bool scan0_stmt_fptr(const Env env, const Stmt_Fptr stmt) { GWDEBUG_EXE t->flag = stmt->td->flag; stmt->type = t; nspc_add_type(t->owner, stmt->xid, t); - stmt->value = mk_class(t); + stmt->value = mk_class(env, t); return GW_OK; } @@ -85,12 +87,12 @@ ANN m_bool scan0_stmt_enum(const Env env, const Stmt_Enum stmt) { GWDEBUG_EXE stmt->t = t; if(stmt->xid) { nspc_add_type(nspc, stmt->xid, t); - mk_class(t); + mk_class(env, t); } return GW_OK; } -ANN static Type union_type(const Nspc nspc, const Symbol s, const m_bool add) { +ANN static Type union_type(const Env env, const Nspc nspc, const Symbol s, const m_bool add) { const m_str name = s_name(s); const Type t = type_copy(t_union); t->name = name; @@ -99,7 +101,7 @@ ANN static Type union_type(const Nspc nspc, const Symbol s, const m_bool add) { t->owner = nspc; if(add) { nspc_add_type(nspc, s, t); - mk_class(t); + mk_class(env, t); } return t; } @@ -111,7 +113,7 @@ ANN static m_bool scan0_stmt_union(const Env env, const Stmt_Union stmt) { GWDEB CHECK_BB(scan0_defined(env, stmt->xid, stmt->self->pos)) const Nspc nspc = !GET_FLAG(stmt, global) ? env->curr : env->global_nspc; - const Type t = union_type(nspc, stmt->type_xid ?: stmt->xid, + const Type t = union_type(env, nspc, stmt->type_xid ?: stmt->xid, !!stmt->type_xid); stmt->value = new_value(t, s_name(stmt->xid)); stmt->value->owner_class = env->class_def; @@ -125,7 +127,7 @@ ANN static m_bool scan0_stmt_union(const Env env, const Stmt_Union stmt) { GWDEB } else if(stmt->type_xid) { const Nspc nspc = !GET_FLAG(stmt, global) ? env->curr : env->global_nspc; - stmt->type = union_type(nspc, stmt->type_xid, 1); + stmt->type = union_type(env, nspc, stmt->type_xid, 1); } return GW_OK; } @@ -197,7 +199,7 @@ ANN m_bool scan0_class_def(const Env env, const Class_Def class_def) { GWDEBUG_E while((body = body->next)); env_pop(env, scope); } - (void)mk_class(class_def->type); + (void)mk_class(env, class_def->type); if(GET_FLAG(class_def, global)) env->curr = (Nspc)vector_pop(&env->scope->nspc_stack); return GW_OK; diff --git a/src/parse/scan1.c b/src/parse/scan1.c index 3a0436ac..5d4c8457 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -6,6 +6,7 @@ #include "nspc.h" #include "value.h" #include "optim.h" +#include "vm.h" #include "parse.h" #define FAKE_FUNC ((Func)1) @@ -71,7 +72,7 @@ ANN m_bool scan1_exp_decl(const Env env, Exp_Decl* decl) { GWDEBUG_EXE if(var->array) { if(var->array->exp) CHECK_BB(scan1_exp(env, var->array->exp)) - t = array_type(decl->type, var->array->depth); + t = array_type(env, decl->type, var->array->depth); } const Value v = var->value = former ? former : new_value(t, s_name(var->xid)); nspc_add_value(nspc, var->xid, v); diff --git a/src/parse/scan2.c b/src/parse/scan2.c index cc01dd29..29516a1d 100644 --- a/src/parse/scan2.c +++ b/src/parse/scan2.c @@ -60,12 +60,12 @@ ANN static Value arg_value(const Arg_List list) { return var->value; } -ANN static m_bool scan2_args(const Func_Def f) { GWDEBUG_EXE +ANN static m_bool scan2_args(const Env env, const Func_Def f) { GWDEBUG_EXE Arg_List list = f->arg_list; do { const Var_Decl var = list->var_decl; if(var->array) - list->type = array_type(list->type, var->array->depth); + list->type = array_type(env, list->type, var->array->depth); var->value = arg_value(list); var->value->offset = f->stack_depth; f->stack_depth += list->type->size; @@ -93,7 +93,7 @@ ANN m_bool scan2_stmt_fptr(const Env env, const Stmt_Fptr ptr) { GWDEBUG_EXE struct Func_Def_ d = { .stack_depth=0 }; d.arg_list = ptr->args; if(d.arg_list) - CHECK_BB(scan2_args(&d)) + CHECK_BB(scan2_args(env, &d)) const Func_Def def = new_func_def(ptr->td, ptr->xid, ptr->args, NULL, ptr->td->flag); def->ret_type = ptr->ret_type; def->stack_depth = d.stack_depth; @@ -305,8 +305,7 @@ ANN static m_bool scan2_func_def_overload(const Func_Def f, const Value overload const m_bool base = tmpl_list_base(f->tmpl); const m_bool tmpl = GET_FLAG(overload, template); if(isa(overload->type, t_function) < 0) - ERR_B(f->td->xid->pos, "function name '%s' is already used by another value", - s_name(f->name)) + ERR_B(f->td->xid->pos, "function name '%s' is already used by another value", overload->name) if((!tmpl && base) || (tmpl && !base && !GET_FLAG(f, template))) ERR_B(f->td->xid->pos, "must overload template function with template") return GW_OK; @@ -368,7 +367,7 @@ ANN2(1, 2) static m_bool scan2_func_def_template(const Env env, const Func_Def f } m_bool ret = compat_func(ff->def, f); if(ret > 0) { - const Symbol sym = func_symbol(env->curr->name, func_name, + const Symbol sym = func_symbol(env, env->curr->name, func_name, "template", ff->vt_index); nspc_add_value(env->curr, sym, value); if(!overload) { @@ -382,7 +381,7 @@ ANN2(1, 2) static m_bool scan2_func_def_template(const Env env, const Func_Def f } } while(type && (type = type->parent) && (nspc = type->nspc)); --i; - const Symbol sym = func_symbol(env->curr->name, func_name, "template", i); + const Symbol sym = func_symbol(env, env->curr->name, func_name, "template", i); nspc_add_value(env->curr, sym, value); if(!overload) { func->vt_index = i; @@ -459,7 +458,7 @@ ANN static m_str func_tmpl_name(const Env env, const Func_Def f) { } tmpl_name[tlen+1] = '\0'; vector_release(&v); - const Symbol sym = func_symbol(env->curr->name, func_name, tmpl_name, (m_uint)f->tmpl->base); + const Symbol sym = func_symbol(env, env->curr->name, func_name, tmpl_name, (m_uint)f->tmpl->base); return s_name(sym); } @@ -493,7 +492,7 @@ ANN m_bool scan2_func_def(const Env env, const Func_Def f) { GWDEBUG_EXE if(tmpl_list_base(f->tmpl)) return scan2_func_def_template(env, f, overload); if(!f->tmpl) { - const Symbol sym = func_symbol(env->curr->name, func_name, NULL, overload ? ++overload->offset : 0); + const Symbol sym = func_symbol(env, env->curr->name, func_name, NULL, overload ? ++overload->offset : 0); func_name = s_name(sym); } else { if(f->func) @@ -507,7 +506,7 @@ ANN m_bool scan2_func_def(const Env env, const Func_Def f) { GWDEBUG_EXE if(GET_FLAG(func->def, variadic)) f->stack_depth += SZ_INT; f->ret_type = type_decl_resolve(env, f->td); - return (f->arg_list && f->arg_list->type) ? scan2_args(f) : GW_OK; + return (f->arg_list && f->arg_list->type) ? scan2_args(env, f) : GW_OK; } } const Func base = get_func(env, f); @@ -517,7 +516,7 @@ ANN m_bool scan2_func_def(const Env env, const Func_Def f) { GWDEBUG_EXE f->func = base; } if(f->arg_list) - CHECK_BB(scan2_args(f)) + CHECK_BB(scan2_args(env, f)) if(!GET_FLAG(f, builtin) && f->d.code->d.stmt_code.stmt_list) CHECK_BB(scan2_func_def_code(env, f)) if(!base) { diff --git a/src/parse/template.c b/src/parse/template.c index 44908a69..5dbb7d64 100644 --- a/src/parse/template.c +++ b/src/parse/template.c @@ -7,17 +7,19 @@ #include "type.h" #include "nspc.h" #include "template.h" +#include "vm.h" +#include "parse.h" -ANN static inline Type owner_type(const Type t) { +ANN static inline Type owner_type(const Env env, const Type t) { const Nspc nspc = t->nspc ? t->nspc->parent : NULL; return (nspc && nspc->parent) ? nspc_lookup_type1(nspc->parent, insert_symbol(nspc->name)) : NULL; } -ANEW ANN static Vector get_types(Type t) { +ANEW ANN static Vector get_types(const Env env, Type t) { const Vector v = new_vector(); do if(GET_FLAG(t, template)) vector_add(v, (vtype)t->def->tmpl->list.list); - while((t = owner_type(t))); + while((t = owner_type(env, t))); return v; } @@ -29,11 +31,11 @@ ANEW ANN static ID_List id_list_copy(ID_List src) { return list; } -ANN static ID_List get_total_type_list(const Type t) { - const Type parent = owner_type(t); +ANN static ID_List get_total_type_list(const Env env, const Type t) { + const Type parent = owner_type(env, t); if(!parent) return t->def->tmpl ? t->def->tmpl->list.list : NULL; - const Vector v = get_types(parent); + const Vector v = get_types(env, parent); const ID_List base = (ID_List)vector_pop(v); if(!base) { free_vector(v); @@ -160,7 +162,7 @@ ANN Type scan_type(const Env env, const Type t, const Type_Decl* type) { SET_FLAG(a->type, dtor); ADD_REF(t->nspc->dtor) } - a->tmpl = new_tmpl_class(get_total_type_list(t), 0); + a->tmpl = new_tmpl_class(get_total_type_list(env, t), 0); a->tmpl->base = type->types; nspc_add_type(t->owner, insert_symbol(a->type->name), a->type); return a->type; diff --git a/src/parse/type_decl.c b/src/parse/type_decl.c index ac6c9177..b410083d 100644 --- a/src/parse/type_decl.c +++ b/src/parse/type_decl.c @@ -5,12 +5,14 @@ #include "oo.h" #include "env.h" #include "type.h" +#include "vm.h" +#include "parse.h" ANN Type type_decl_resolve(const Env env, const Type_Decl* td) { Type t = find_type(env, td->xid); CHECK_OO(t) CHECK_OO((t = scan_type(env, t, td))) - return !td->array ? t : array_type(t, td->array->depth); + return !td->array ? t : array_type(env, t, td->array->depth); } struct td_info { @@ -68,11 +70,13 @@ ANEW ANN m_str tl2str(const Env env, Type_List tl) { return info.str; } -ANN static inline void* type_unknown(const ID_List id) { +#include "vm.h" +#include "gwion.h" +ANN static inline void* type_unknown(const Env env, const ID_List id) { char path[id_list_len(id)]; type_path(path, id); err_msg(id->pos, "unknown type '%s'", path); - did_you_mean(s_name(id->xid)); + did_you_mean(env->gwion->st, s_name(id->xid)); return NULL; } @@ -84,5 +88,5 @@ ANN static Type prim_ref(const Type t, const Type_Decl* td) { ANN Type known_type(const Env env, const Type_Decl* td) { const Type t = type_decl_resolve(env, td); - return t ? prim_ref(t, td) : type_unknown(td->xid); + return t ? prim_ref(t, td) : type_unknown(env, td->xid); } diff --git a/src/parse/type_utils.c b/src/parse/type_utils.c index c4699e27..e9f832e1 100644 --- a/src/parse/type_utils.c +++ b/src/parse/type_utils.c @@ -6,6 +6,8 @@ #include "env.h" #include "value.h" #include "type.h" +#include "vm.h" +#include "parse.h" ANN m_bool isres(const Symbol xid) { const m_str s = s_name(xid); diff --git a/util b/util index 1e9a605a..f2f9ab4b 160000 --- a/util +++ b/util @@ -1 +1 @@ -Subproject commit 1e9a605aeb73cef2853972984b90e6dc43324d84 +Subproject commit f2f9ab4bec6cffc26da671aa5d1907bb05b2a5ad -- 2.43.0