сgen: print generic structs (#6967)

pull/6975/head
Daniel Däschle 2020-11-26 23:57:03 +01:00 committed by GitHub
parent 6563535a3d
commit d71d9ad7c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 4 deletions

View File

@ -367,6 +367,12 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp string, str_fn_name st
g.type_definitions.writeln('string indent_${str_fn_name}($styp x, int indent_count); // auto')
g.auto_str_funcs.writeln('string indent_${str_fn_name}($styp x, int indent_count) {')
mut clean_struct_v_type_name := styp.replace('__', '.')
if clean_struct_v_type_name.contains('_T_') {
// TODO: this is a bit hacky. styp shouldn't be even parsed with _T_
// use something different than g.typ for styp
clean_struct_v_type_name = clean_struct_v_type_name.replace('_T_', '<').replace('_', ', ') +
'>'
}
if styp.ends_with('*') {
deref_typ := styp.replace('*', '')
g.auto_str_funcs.writeln('\t$deref_typ *it = x;')

View File

@ -500,11 +500,10 @@ static inline $opt_el_type __Option_${styp}_popval($styp ch) {
fn (g &Gen) cc_type2(t table.Type) string {
sym := g.table.get_type_symbol(g.unwrap_generic(t))
mut styp := util.no_dots(sym.name)
if sym.kind == .struct_ {
info := sym.info as table.Struct
if info.generic_types.len > 0 {
if mut sym.info is table.Struct {
if sym.info.generic_types.len > 0 {
mut sgtyps := '_T'
for gt in info.generic_types {
for gt in sym.info.generic_types {
gts := g.table.get_type_symbol(if gt.has_flag(.generic) { g.unwrap_generic(gt) } else { gt })
sgtyps += '_$gts.name'
}

View File

@ -269,3 +269,24 @@ fn test_alias_struct() {
assert t.str() == 'TestAlias($ts)'
assert '$t' == 'TestAlias(TestStruct{\n x: 0\n})'
}
struct GenericStruct<T> {
x T
}
fn test_generic_struct() {
x := GenericStruct<TestStruct>{}
assert '$x' == 'GenericStruct<TestStruct>{\n x: TestStruct{\n x: 0\n }\n}'
assert x.str() == 'GenericStruct<TestStruct>{\n x: TestStruct{\n x: 0\n }\n}'
}
struct MultiGenericStruct<T, X> {
t T
x X
}
fn test_multi_generic_struct() {
x := MultiGenericStruct<TestStruct, TestStruct>{}
assert '$x' == 'MultiGenericStruct<TestStruct, TestStruct>{\n t: TestStruct{\n x: 0\n }\n x: TestStruct{\n x: 0\n }\n}'
assert x.str() == 'MultiGenericStruct<TestStruct, TestStruct>{\n t: TestStruct{\n x: 0\n }\n x: TestStruct{\n x: 0\n }\n}'
}