From bf9f684c5977ac7f5e37fb09e196a8ba510d6a8d Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 30 Dec 2021 13:16:56 +0200 Subject: [PATCH] ast: improve support for type aliases, in TypeSymbol.xyz_info methods --- vlib/v/ast/types.v | 113 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 86 insertions(+), 27 deletions(-) diff --git a/vlib/v/ast/types.v b/vlib/v/ast/types.v index 2a9fcbfc69..99a2dd9a39 100644 --- a/vlib/v/ast/types.v +++ b/vlib/v/ast/types.v @@ -564,76 +564,135 @@ pub fn (t TypeSymbol) str() string { return t.name } +[noreturn] +fn (t &TypeSymbol) no_info_panic(fname string) { + panic('$fname: no info for type: $t.name') +} + [inline] pub fn (t &TypeSymbol) enum_info() Enum { - match mut t.info { - Enum { return t.info } - else { panic('TypeSymbol.enum_info(): no enum info for type: $t.name') } + if t.info is Enum { + return t.info } + if t.info is Alias { + fsym := global_table.final_sym(t.info.parent_type) + if fsym.info is Enum { + return fsym.info + } + } + t.no_info_panic(@METHOD) } [inline] pub fn (t &TypeSymbol) mr_info() MultiReturn { - match mut t.info { - MultiReturn { return t.info } - else { panic('TypeSymbol.mr_info(): no multi return info for type: $t.name') } + if t.info is MultiReturn { + return t.info } + if t.info is Alias { + fsym := global_table.final_sym(t.info.parent_type) + if fsym.info is MultiReturn { + return fsym.info + } + } + t.no_info_panic(@METHOD) } [inline] pub fn (t &TypeSymbol) array_info() Array { - match mut t.info { - Array { return t.info } - else { panic('TypeSymbol.array_info(): no array info for type: $t.name') } + if t.info is Array { + return t.info } + if t.info is Alias { + fsym := global_table.final_sym(t.info.parent_type) + if fsym.info is Array { + return fsym.info + } + } + t.no_info_panic(@METHOD) } [inline] pub fn (t &TypeSymbol) array_fixed_info() ArrayFixed { - match mut t.info { - ArrayFixed { return t.info } - else { panic('TypeSymbol.array_fixed(): no array fixed info for type: $t.name') } + if t.info is ArrayFixed { + return t.info } + if t.info is Alias { + fsym := global_table.final_sym(t.info.parent_type) + if fsym.info is ArrayFixed { + return fsym.info + } + } + t.no_info_panic(@METHOD) } [inline] pub fn (t &TypeSymbol) chan_info() Chan { - match mut t.info { - Chan { return t.info } - else { panic('TypeSymbol.chan_info(): no chan info for type: $t.name') } + if t.info is Chan { + return t.info } + if t.info is Alias { + fsym := global_table.final_sym(t.info.parent_type) + if fsym.info is Chan { + return fsym.info + } + } + t.no_info_panic(@METHOD) } [inline] pub fn (t &TypeSymbol) thread_info() Thread { - match mut t.info { - Thread { return t.info } - else { panic('TypeSymbol.thread_info(): no thread info for type: $t.name') } + if t.info is Thread { + return t.info } + if t.info is Alias { + fsym := global_table.final_sym(t.info.parent_type) + if fsym.info is Thread { + return fsym.info + } + } + t.no_info_panic(@METHOD) } [inline] pub fn (t &TypeSymbol) map_info() Map { - match mut t.info { - Map { return t.info } - else { panic('TypeSymbol.map_info(): no map info for type: $t.name') } + if t.info is Map { + return t.info } + if t.info is Alias { + fsym := global_table.final_sym(t.info.parent_type) + if fsym.info is Map { + return fsym.info + } + } + t.no_info_panic(@METHOD) } [inline] pub fn (t &TypeSymbol) struct_info() Struct { - match mut t.info { - Struct { return t.info } - else { panic('TypeSymbol.struct_info(): no struct info for type: $t.name') } + if t.info is Struct { + return t.info } + if t.info is Alias { + fsym := global_table.final_sym(t.info.parent_type) + if fsym.info is Struct { + return fsym.info + } + } + t.no_info_panic(@METHOD) } [inline] pub fn (t &TypeSymbol) sumtype_info() SumType { - match mut t.info { - SumType { return t.info } - else { panic('TypeSymbol.sumtype_info(): no sumtype info for type: $t.name') } + if t.info is SumType { + return t.info } + if t.info is SumType { + fsym := global_table.final_sym(t.info.parent_type) + if fsym.info is SumType { + return fsym.info + } + } + t.no_info_panic(@METHOD) } pub fn (t &TypeSymbol) is_heap() bool {