From 96fc9138d4e238512896a00d59d3f1dca76e5df1 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 29 Jul 2018 11:29:20 +0100 Subject: [PATCH] Do `using Foo :: enum` at the `type_decl` stage --- core/runtime/core.odin | 1 - src/check_decl.cpp | 27 +++++++++++++++++++++++++++ src/check_type.cpp | 24 +----------------------- src/checker.cpp | 2 +- src/checker.hpp | 2 +- src/ir.cpp | 3 --- src/parser.hpp | 6 +++--- src/types.cpp | 1 - 8 files changed, 33 insertions(+), 33 deletions(-) diff --git a/core/runtime/core.odin b/core/runtime/core.odin index fee2780a7..106318533 100644 --- a/core/runtime/core.odin +++ b/core/runtime/core.odin @@ -88,7 +88,6 @@ Type_Info_Enum :: struct { base: ^Type_Info, names: []string, values: []Type_Info_Enum_Value, - is_using: bool, // TODO(bill): Should this be in the `Type_Info`? }; Type_Info_Map :: struct { key: ^Type_Info, diff --git a/src/check_decl.cpp b/src/check_decl.cpp index 0ce662570..847296eef 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -248,6 +248,33 @@ void check_type_decl(CheckerContext *ctx, Entity *e, Ast *type_expr, Type *def) named->Named.base = bt; e->TypeName.is_type_alias = true; } + + // using decl + if (decl->is_using) { + // NOTE(bill): Must be an enum declaration + if (te->kind == Ast_EnumType) { + Scope *parent = ctx->scope->parent; + if (parent->flags&ScopeFlag_File) { + // NOTE(bill): Use package scope + parent = parent->parent; + } + + Type *t = base_type(e->type); + GB_ASSERT(t->kind == Type_Enum); + + for_array(i, t->Enum.fields) { + Entity *f = t->Enum.fields[i]; + if (f->kind != Entity_Constant) { + continue; + } + String name = f->token.string; + if (is_blank_ident(name)) { + continue; + } + add_entity(ctx->checker, parent, nullptr, f); + } + } + } } diff --git a/src/check_type.cpp b/src/check_type.cpp index 4f44e0c4d..77b370619 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -600,29 +600,7 @@ void check_enum_type(CheckerContext *ctx, Type *enum_type, Type *named_type, Ast GB_ASSERT(fields.count <= et->fields.count); - enum_type->Enum.fields = fields; - enum_type->Enum.is_using = et->is_using; - // TODO(bill): Should this be done elsewhere? e.g. delayed - if (et->is_using) { - Scope *parent = ctx->scope->parent; - if (parent->flags&ScopeFlag_File) { - // NOTE(bill): Use package scope - parent = parent->parent; - } - for_array(i, fields) { - Entity *f = fields[i]; - if (f->kind != Entity_Constant) { - continue; - } - String name = f->token.string; - if (is_blank_ident(name)) { - continue; - } - add_entity(ctx->checker, parent, nullptr, f); - } - } - - Scope *s = ctx->scope; + enum_type->Enum.fields = fields; enum_type->Enum.names = make_names_field_for_struct(ctx, ctx->scope); } diff --git a/src/checker.cpp b/src/checker.cpp index 9f59788b8..6bd10fb3b 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -2123,7 +2123,7 @@ void check_collect_value_decl(CheckerContext *c, Ast *decl) { if (vd->is_using) { if (e->kind == Entity_TypeName && init->kind == Ast_EnumType) { - init->EnumType.is_using = true; + d->is_using = true; } else { error(name, "'using' is not allowed on this constant value declaration"); } diff --git a/src/checker.hpp b/src/checker.hpp index a257e7809..310a99542 100644 --- a/src/checker.hpp +++ b/src/checker.hpp @@ -187,10 +187,10 @@ struct DeclInfo { Ast * type_expr; Ast * init_expr; - // Array init_expr_list; Array attributes; Ast * proc_lit; // Ast_ProcLit Type * gen_proc_type; // Precalculated + bool is_using; PtrSet deps; PtrSet type_info_deps; diff --git a/src/ir.cpp b/src/ir.cpp index 3a7691326..036727326 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -8163,9 +8163,6 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info irValue *base = ir_type_info(proc, t->Enum.base_type); ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 0), base); - // is_using - ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 3), ir_const_bool(t->Enum.is_using)); - if (t->Enum.fields.count > 0) { auto fields = t->Enum.fields; irValue *name_array = ir_generate_array(m, t_string, fields.count, diff --git a/src/parser.hpp b/src/parser.hpp index 30ce225e5..d542a8b7a 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -435,9 +435,9 @@ AST_KIND(_TypeBegin, "", bool) \ Ast *type; \ }) \ AST_KIND(PolyType, "polymorphic type", struct { \ - Token token; \ - Ast *type; \ - Ast *specialization; \ + Token token; \ + Ast * type; \ + Ast * specialization; \ }) \ AST_KIND(ProcType, "procedure type", struct { \ Token token; \ diff --git a/src/types.cpp b/src/types.cpp index e0ab91e90..240224059 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -135,7 +135,6 @@ struct TypeStruct { Scope * scope; \ Entity * names; \ Type * base_type; \ - bool is_using; \ }) \ TYPE_KIND(Union, struct { \ Array variants; \