From 0b30240fb924e0dc29e7670b0277b7714d8ccfcd Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Tue, 3 Aug 2021 21:56:56 +0200 Subject: [PATCH] :art: Scoped enums by default --- ast | 2 +- examples/class_enum.gw | 2 +- src/emit/emit.c | 7 +------ src/lib/prim.c | 11 ++++++----- src/parse/check.c | 18 ++++++------------ src/parse/scan1.c | 18 ++++-------------- tests/enum/class_scoped.gw | 2 +- tests/enum/scoped.gw | 2 +- tests/error/class_enum.gw | 6 +++--- tests/tree/enum.gw | 2 +- 10 files changed, 25 insertions(+), 45 deletions(-) diff --git a/ast b/ast index d1d23609..a4e4f7f5 160000 --- a/ast +++ b/ast @@ -1 +1 @@ -Subproject commit d1d23609b24dc2f754c2808bd23f13100edcc001 +Subproject commit a4e4f7f5955fff6ebb05e90949a54c556dee964e diff --git a/examples/class_enum.gw b/examples/class_enum.gw index bd1eb29e..80d165af 100644 --- a/examples/class_enum.gw +++ b/examples/class_enum.gw @@ -5,7 +5,7 @@ class C { two, three } - <<< zero, " ", one, " ", two, " ", three >>>; + <<< E.zero, " ", E.one, " ", E.two, " ", E.three >>>; } var C c; <<< c >>>; diff --git a/src/emit/emit.c b/src/emit/emit.c index 2defb25d..3644477d 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -2214,12 +2214,7 @@ ANN static m_bool emit_enum_def(const Emitter emit, const Enum_Def edef) { LOOP_OPTIM for (m_uint i = 0; i < vector_size(&edef->values); ++i) { const Value v = (Value)vector_at(&edef->values, i); - if(edef->is_scoped || emit->env->class_def) - *(m_uint *)(v->from->owner->info->class_data + v->from->offset) = i; - else { - v->from->offset = emit_local(emit, emit->gwion->type[et_int]); - v->d.num = i; - } + *(m_uint *)(v->from->owner->info->class_data + v->from->offset) = i; } set_tflag(edef->t, tflag_emit); return GW_OK; diff --git a/src/lib/prim.c b/src/lib/prim.c index eff1c19e..7edefbb0 100644 --- a/src/lib/prim.c +++ b/src/lib/prim.c @@ -251,12 +251,13 @@ static GACK(gack_bool) { } static GWION_IMPORT(int_values) { - GWI_BB(gwi_enum_ini(gwi, "bool")) - GWI_BB(gwi_enum_add(gwi, "false", 0)) - GWI_BB(gwi_enum_add(gwi, "true", 1)) - const Type t_bool = gwi_enum_end(gwi); + DECL_OB(const Type, t_bool, = gwi_mk_type(gwi, "bool", SZ_INT, "int")); + GWI_BB(gwi_set_global_type(gwi, t_bool, et_bool)) GWI_BB(gwi_gack(gwi, t_bool, gack_bool)) - gwi->gwion->type[et_bool] = t_bool; + gwi_item_ini(gwi, "bool", "true"); + gwi_item_end(gwi, ae_flag_const, num, 0); + gwi_item_ini(gwi, "bool", "false"); + gwi_item_end(gwi, ae_flag_const, num, 1); GWI_BB(gwi_oper_ini(gwi, NULL, "int", "bool")) GWI_BB(gwi_oper_add(gwi, opck_unary_meta)) GWI_BB(gwi_oper_add(gwi, opck_int_not)) diff --git a/src/parse/check.c b/src/parse/check.c index 0e2fff4c..da633d90 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -1095,18 +1095,12 @@ ANN Type check_exp(const Env env, const Exp exp) { } ANN m_bool check_enum_def(const Env env, const Enum_Def edef) { - const bool is_scoped = edef->is_scoped; - const m_uint scope = is_scoped ? - env_push_type(env, edef->t) : 0; - if (is_scoped || env->class_def) { - ID_List list = edef->list; - do decl_static(env, nspc_lookup_value0(env->curr, list->xid)); - while ((list = list->next)); - } - if (is_scoped) { - env_pop(env, scope); - nspc_allocdata(env->gwion->mp, edef->t->nspc); - } + const m_uint scope = env_push_type(env, edef->t); + ID_List list = edef->list; + do decl_static(env, nspc_lookup_value0(env->curr, list->xid)); + while ((list = list->next)); + env_pop(env, scope); + nspc_allocdata(env->gwion->mp, edef->t->nspc); return GW_OK; } diff --git a/src/parse/scan1.c b/src/parse/scan1.c index c5224191..5860ece3 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -366,20 +366,11 @@ ANN static inline m_bool scan1_stmt_exp(const Env env, const Stmt_Exp stmt) { } ANN m_bool scan1_enum_def(const Env env, const Enum_Def edef) { - const m_bool is_scoped = edef->is_scoped; - if(is_scoped) - edef->t->nspc = new_nspc(env->gwion->mp, edef->t->name); - const Nspc nspc = is_scoped ? - edef->t->nspc : edef->t->info->value->from->owner; - const m_uint scope = is_scoped ? - env_push_type(env, edef->t) : 0; + edef->t->nspc = new_nspc(env->gwion->mp, edef->t->name); + const Nspc nspc = edef->t->nspc; + const m_uint scope = env_push_type(env, edef->t); ID_List list = edef->list; do { - if(!is_scoped) { - CHECK_BB(already_defined(env, list->xid, edef->pos)); - if (nspc_lookup_value1(nspc, list->xid)) - ERR_B(edef->pos, "'%s' already defined", s_name(list->xid)); - } const Value v = new_value(env->gwion->mp, edef->t, s_name(list->xid)); valuefrom(env, v->from, edef->pos); if (env->class_def) { @@ -393,8 +384,7 @@ ANN m_bool scan1_enum_def(const Env env, const Enum_Def edef) { nspc_add_value(nspc, list->xid, v); vector_add(&edef->values, (vtype)v); } while ((list = list->next)); - if(is_scoped) - env_pop(env, scope); + env_pop(env, scope); return GW_OK; } diff --git a/tests/enum/class_scoped.gw b/tests/enum/class_scoped.gw index d0b7d194..14a7986c 100644 --- a/tests/enum/class_scoped.gw +++ b/tests/enum/class_scoped.gw @@ -1,5 +1,5 @@ class C { - enum MyEnum@ { + enum MyEnum { zero, one } } diff --git a/tests/enum/scoped.gw b/tests/enum/scoped.gw index 4b4fcf76..27174e3f 100644 --- a/tests/enum/scoped.gw +++ b/tests/enum/scoped.gw @@ -1,5 +1,5 @@ #! [contains] 1 -enum MyEnum@ { +enum MyEnum { zero, one } diff --git a/tests/error/class_enum.gw b/tests/error/class_enum.gw index 29970bf0..c4feaf46 100644 --- a/tests/error/class_enum.gw +++ b/tests/error/class_enum.gw @@ -4,6 +4,6 @@ class C enum E { a, b }; } -C.a; -<<< C.a >>>; -<<< --C.a >>>; +C.E.a; +<<< C.E.a >>>; +<<< --C.E.a >>>; diff --git a/tests/tree/enum.gw b/tests/tree/enum.gw index 4c29fd9e..9a96920f 100644 --- a/tests/tree/enum.gw +++ b/tests/tree/enum.gw @@ -1,4 +1,4 @@ enum E { zero, one, two, three }; -<<< zero, " ", one, " ", two, " ", three >>>; +<<< E.zero, " ", E.one, " ", E.two, " ", E.three >>>; -- 2.43.0