From 3e05939b65aa8285bd180172668784539bda721d Mon Sep 17 00:00:00 2001 From: Joe Conigliaro Date: Sun, 15 Mar 2020 12:51:31 +1100 Subject: [PATCH] cgen: enum fixes --- vlib/v/ast/ast.v | 3 ++- vlib/v/checker/checker.v | 20 +++++++++++--------- vlib/v/gen/cgen.v | 4 ++-- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 55ce58f91e..15c8522aba 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -438,8 +438,9 @@ pub: enum_name string val string mod string // for full path `mod_Enum_val` - // typ table.Type pos token.Position +mut: + typ table.Type } pub struct EnumDecl { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 8e44e33efd..95c75b47e3 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -612,7 +612,7 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type { return table.byte_type } ast.EnumVal { - return c.enum_val(it) + return c.enum_val(mut it) } ast.FloatLiteral { return table.f64_type @@ -922,26 +922,28 @@ pub fn (c mut Checker) index_expr(node mut ast.IndexExpr) table.Type { // `.green` or `Color.green` // If a short form is used, `expected_type` needs to be an enum // with this value. -pub fn (c mut Checker) enum_val(node ast.EnumVal) table.Type { +pub fn (c mut Checker) enum_val(node mut ast.EnumVal) table.Type { typ_idx := if node.enum_name == '' { c.expected_type } else { // c.table.find_type_idx(node.enum_name) } // println('checker: enum_val: $node.enum_name typeidx=$typ_idx') if typ_idx == 0 { c.error('not an enum (name=$node.enum_name) (type_idx=0)', node.pos) } - typ := c.table.get_type_symbol(table.Type(typ_idx)) + typ := table.Type(typ_idx) + typ_sym := c.table.get_type_symbol(typ) // println('tname=$typ.name') - if typ.kind != .enum_ { + if typ_sym.kind != .enum_ { c.error('not an enum', node.pos) } - // info := typ.info as table.Enum - info := typ.enum_info() - // rintln('checker: x = $info.x enum val $c.expected_type $typ.name') + // info := typ_sym.info as table.Enum + info := typ_sym.enum_info() + // rintln('checker: x = $info.x enum val $c.expected_type $typ_sym.name') // println(info.vals) if !(node.val in info.vals) { - c.error('enum `$typ.name` does not have a value `$node.val`', node.pos) + c.error('enum `$typ_sym.name` does not have a value `$node.val`', node.pos) } - return typ_idx + node.typ = typ + return typ } pub fn (c mut Checker) map_init(node mut ast.MapInit) table.Type { diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index da483468ad..f7aaa92a1f 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -628,8 +628,8 @@ fn (g mut Gen) expr(node ast.Expr) { } ast.EnumVal { // g.write('/*EnumVal*/${it.mod}${it.enum_name}_$it.val') - enum_name := it.enum_name.replace('.', '__') - g.write('${enum_name}_$it.val') + g.write(g.typ(it.typ)) + g.write('_$it.val') } ast.FloatLiteral { g.write(it.val)