From 853bb4c41e84ec67551d3d0b51f75ca4e4d1f3ee Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Thu, 12 Mar 2020 17:56:44 +1100 Subject: [PATCH] cgen: alias & map types & add map_string/map_int aliases --- vlib/v/gen/cgen.v | 42 ++++++++++++++++++++++++------------ vlib/v/table/atype_symbols.v | 13 ++++++++--- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 3284d83d07..171e577b8c 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -44,7 +44,7 @@ pub fn (g mut Gen) init() { g.definitions.writeln(c_builtin_types) g.definitions.writeln(c_headers) g.write_builtin_types() - g.write_array_types() + g.write_typedef_types() g.write_sorted_types() g.write_multi_return_types() g.definitions.writeln('// end of definitions #endif') @@ -67,20 +67,34 @@ pub fn (g &Gen) styp(t string) string { } */ - -pub fn (g mut Gen) write_array_types() { +pub fn (g mut Gen) write_typedef_types() { for typ in g.table.types { - if typ.kind == .array { - styp := typ.name.replace('.', '__') - g.definitions.writeln('typedef array $styp;') - } - else if typ.kind == .array_fixed { - styp := typ.name.replace('.', '__') - // array_fixed_char_300 => char x[300] - mut fixed := styp[12..] - len := styp.after('_') - fixed = fixed[..fixed.len - len.len - 1] - g.definitions.writeln('typedef $fixed $styp [$len];') + match typ.kind { + .alias { + parent := &g.table.types[typ.parent_idx] + styp := typ.name.replace('.', '__') + parent_styp := parent.name.replace('.', '__') + g.definitions.writeln('typedef $parent_styp $styp;') + } + .array { + styp := typ.name.replace('.', '__') + g.definitions.writeln('typedef array $styp;') + } + .array_fixed { + styp := typ.name.replace('.', '__') + // array_fixed_char_300 => char x[300] + mut fixed := styp[12..] + len := styp.after('_') + fixed = fixed[..fixed.len - len.len - 1] + g.definitions.writeln('typedef $fixed $styp [$len];') + } + .map { + styp := typ.name.replace('.', '__') + g.definitions.writeln('typedef map $styp;') + } + else { + continue + } } } } diff --git a/vlib/v/table/atype_symbols.v b/vlib/v/table/atype_symbols.v index 8901646f8b..35f3c5d82a 100644 --- a/vlib/v/table/atype_symbols.v +++ b/vlib/v/table/atype_symbols.v @@ -248,11 +248,18 @@ pub fn (t mut Table) register_builtin_type_symbols() { kind: .map name: 'map' }) - // TODO: remove + // TODO: remove. for v1 map compatibility + map_string_string_idx := t.find_or_register_map(string_type, string_type) + map_string_int_idx := t.find_or_register_map(string_type, int_type) t.register_type_symbol(TypeSymbol{ - parent_idx: map_type_idx - kind: .struct_ + kind: .alias name: 'map_string' + parent_idx: map_string_string_idx + }) + t.register_type_symbol(TypeSymbol{ + kind: .alias + name: 'map_int' + parent_idx: map_string_int_idx }) }