From b941cfc45894745b4b99d06c762cd78d3c6beca5 Mon Sep 17 00:00:00 2001 From: Paul Chaignon Date: Thu, 9 Nov 2023 16:54:42 +0100 Subject: prog: support AUTO for structs The AUTO token can currently be used in place of ConstType, LenType, and CsumType. This commit extends this support to StructType on the condition that they are made of the above types. It will allow us to simplify all the {AUTO, AUTO, AUTO, AUTO} in test programs. The following struct can therefore be written as AUTO. auto_struct1 { f0 len[parent, int32] f1 const[0x43, int32] } In addition, with this commit, AUTO can also stand for nested structs as long as all the leaf fields are ConstType, LenType, or CsumType. For example, the following struct can be written as AUTO. auto_struct2 { f0 len[parent, int32] f1 auto_struct1 } Suggested-by: Aleksandr Nogikh Signed-off-by: Paul Chaignon --- prog/encoding.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'prog/encoding.go') diff --git a/prog/encoding.go b/prog/encoding.go index be883645b..6d7daeacb 100644 --- a/prog/encoding.go +++ b/prog/encoding.go @@ -489,9 +489,20 @@ func (p *parser) parseArgInt(typ Type, dir Dir) (Arg, error) { } func (p *parser) parseAuto(typ Type, dir Dir) (Arg, error) { - switch typ.(type) { + switch t1 := typ.(type) { case *ConstType, *LenType, *CsumType: return p.auto(MakeConstArg(typ, dir, 0)), nil + case *StructType: + var inner []Arg + for len(inner) < len(t1.Fields) { + field := t1.Fields[len(inner)] + innerArg, err := p.parseAuto(field.Type, dir) + if err != nil { + return nil, err + } + inner = append(inner, innerArg) + } + return MakeGroupArg(typ, dir, inner), nil default: return nil, fmt.Errorf("wrong type %T for AUTO", typ) } @@ -562,7 +573,7 @@ func (p *parser) parseArgAddr(typ Type, dir Dir) (Arg, error) { var inner Arg if p.Char() == '=' { p.Parse('=') - if p.Char() == 'A' { + if p.HasNext("ANY") { p.Parse('A') p.Parse('N') p.Parse('Y') -- cgit mrf-deployment