github.com/cockroachdb/tools@v0.0.0-20230222021103-a6d27438930d/cmd/signature-fuzzer/internal/fuzz-generator/mapparm.go (about)

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package generator
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  )
    11  
    12  // mapparm describes a parameter of map type; it implements the
    13  // "parm" interface.
    14  type mapparm struct {
    15  	aname   string
    16  	qname   string
    17  	keytype parm
    18  	valtype parm
    19  	keytmp  string
    20  	isBlank
    21  	addrTakenHow
    22  	isGenValFunc
    23  	skipCompare
    24  }
    25  
    26  func (p mapparm) IsControl() bool {
    27  	return false
    28  }
    29  
    30  func (p mapparm) TypeName() string {
    31  	return p.aname
    32  }
    33  
    34  func (p mapparm) QualName() string {
    35  	return p.qname
    36  }
    37  
    38  func (p mapparm) Declare(b *bytes.Buffer, prefix string, suffix string, caller bool) {
    39  	n := p.aname
    40  	if caller {
    41  		n = p.qname
    42  	}
    43  	b.WriteString(fmt.Sprintf("%s %s%s", prefix, n, suffix))
    44  }
    45  
    46  func (p mapparm) String() string {
    47  	return fmt.Sprintf("%s map[%s]%s", p.aname,
    48  		p.keytype.String(), p.valtype.String())
    49  }
    50  
    51  func (p mapparm) GenValue(s *genstate, f *funcdef, value int, caller bool) (string, int) {
    52  	var buf bytes.Buffer
    53  
    54  	verb(5, "mapparm.GenValue(%d)", value)
    55  
    56  	n := p.aname
    57  	if caller {
    58  		n = p.qname
    59  	}
    60  	buf.WriteString(fmt.Sprintf("%s{", n))
    61  	buf.WriteString(p.keytmp + ": ")
    62  
    63  	var valstr string
    64  	valstr, value = s.GenValue(f, p.valtype, value, caller)
    65  	buf.WriteString(valstr + "}")
    66  	return buf.String(), value
    67  }
    68  
    69  func (p mapparm) GenElemRef(elidx int, path string) (string, parm) {
    70  	vne := p.valtype.NumElements()
    71  	verb(4, "begin GenElemRef(%d,%s) on %s %d", elidx, path, p.String(), vne)
    72  
    73  	ppath := fmt.Sprintf("%s[mkt.%s]", path, p.keytmp)
    74  
    75  	// otherwise dig into the value
    76  	verb(4, "recur GenElemRef(%d,...)", elidx)
    77  
    78  	// Otherwise our victim is somewhere inside the value
    79  	if p.IsBlank() {
    80  		ppath = "_"
    81  	}
    82  	return p.valtype.GenElemRef(elidx, ppath)
    83  }
    84  
    85  func (p mapparm) NumElements() int {
    86  	return p.valtype.NumElements()
    87  }
    88  
    89  func (p mapparm) HasPointer() bool {
    90  	return true
    91  }