gotypes,build: pointer dereference (#61)

Provides a method on `gotypes.Component` to allow pointer dereference. This will enable `gotypes` helpers to be used with struct pointer arguments, for example.

Updates #53 
Fixes #54
This commit is contained in:
Michael McLoughlin
2019-01-27 19:22:21 -08:00
committed by GitHub
parent eb225e9d2c
commit 9eb409b935
9 changed files with 137 additions and 29 deletions

View File

@@ -7,6 +7,8 @@ import (
"go/types"
"strconv"
"github.com/mmcloughlin/avo/reg"
"github.com/mmcloughlin/avo/operand"
)
@@ -27,13 +29,14 @@ type Component interface {
// resolution time.
Resolve() (*Basic, error)
Base() Component // base pointer of a string or slice
Len() Component // length of a string or slice
Cap() Component // capacity of a slice
Real() Component // real part of a complex value
Imag() Component // imaginary part of a complex value
Index(int) Component // index into an array
Field(string) Component // access a struct field
Dereference(r reg.Register) Component // dereference a pointer
Base() Component // base pointer of a string or slice
Len() Component // length of a string or slice
Cap() Component // capacity of a slice
Real() Component // real part of a complex value
Imag() Component // imaginary part of a complex value
Index(int) Component // index into an array
Field(string) Component // access a struct field
}
// componenterr is an error that also provides a null implementation of the
@@ -45,28 +48,27 @@ func errorf(format string, args ...interface{}) Component {
return componenterr(fmt.Sprintf(format, args...))
}
func (c componenterr) Error() string { return string(c) }
func (c componenterr) Resolve() (*Basic, error) { return nil, c }
func (c componenterr) Base() Component { return c }
func (c componenterr) Len() Component { return c }
func (c componenterr) Cap() Component { return c }
func (c componenterr) Real() Component { return c }
func (c componenterr) Imag() Component { return c }
func (c componenterr) Index(int) Component { return c }
func (c componenterr) Field(string) Component { return c }
func (c componenterr) Error() string { return string(c) }
func (c componenterr) Resolve() (*Basic, error) { return nil, c }
func (c componenterr) Dereference(r reg.Register) Component { return c }
func (c componenterr) Base() Component { return c }
func (c componenterr) Len() Component { return c }
func (c componenterr) Cap() Component { return c }
func (c componenterr) Real() Component { return c }
func (c componenterr) Imag() Component { return c }
func (c componenterr) Index(int) Component { return c }
func (c componenterr) Field(string) Component { return c }
type component struct {
name string
typ types.Type
offset int
typ types.Type
addr operand.Mem
}
// NewComponent builds a component for the named type at the given memory offset.
func NewComponent(name string, t types.Type, offset int) Component {
// NewComponent builds a component for the named type at the given address.
func NewComponent(t types.Type, addr operand.Mem) Component {
return &component{
name: name,
typ: t,
offset: offset,
typ: t,
addr: addr,
}
}
@@ -76,11 +78,19 @@ func (c *component) Resolve() (*Basic, error) {
return nil, errors.New("component is not primitive")
}
return &Basic{
Addr: operand.NewParamAddr(c.name, c.offset),
Addr: c.addr,
Type: b,
}, nil
}
func (c *component) Dereference(r reg.Register) Component {
p, ok := c.typ.Underlying().(*types.Pointer)
if !ok {
return errorf("not pointer type")
}
return NewComponent(p.Elem(), operand.Mem{Base: r})
}
// Reference: https://github.com/golang/go/blob/50bd1c4d4eb4fac8ddeb5f063c099daccfb71b26/src/reflect/value.go#L1800-L1804
//
// type SliceHeader struct {
@@ -194,8 +204,10 @@ func (c *component) Field(n string) Component {
func (c *component) sub(suffix string, offset int, t types.Type) *component {
s := *c
s.name += suffix
s.offset += offset
if s.addr.Symbol.Name != "" {
s.addr.Symbol.Name += suffix
}
s.addr = s.addr.Offset(offset)
s.typ = t
return &s
}

View File

@@ -2,7 +2,12 @@ package gotypes
import (
"go/types"
"strings"
"testing"
"github.com/mmcloughlin/avo/reg"
"github.com/mmcloughlin/avo/operand"
)
func TestBasicKindsArePrimitive(t *testing.T) {
@@ -33,8 +38,61 @@ func TestPointersArePrimitive(t *testing.T) {
}
func AssertPrimitive(t *testing.T, typ types.Type) {
c := NewComponent("primitive", typ, 0)
c := NewComponent(typ, operand.NewParamAddr("primitive", 0))
if _, err := c.Resolve(); err != nil {
t.Errorf("expected type %s to be primitive: got error '%s'", typ, err)
}
}
func TestComponentErrors(t *testing.T) {
comp := NewComponent(types.Typ[types.Uint32], operand.Mem{})
cases := []struct {
Component Component
ErrorSubstring string
}{
{comp.Base(), "only slices and strings"},
{comp.Len(), "only slices and strings"},
{comp.Cap(), "only slices have"},
{comp.Real(), "only complex"},
{comp.Imag(), "only complex"},
{comp.Index(42), "not array type"},
{comp.Field("a"), "not struct type"},
{comp.Dereference(reg.R12), "not pointer type"},
}
for _, c := range cases {
_, err := c.Component.Resolve()
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), c.ErrorSubstring) {
t.Fatalf("error message %q; expected substring %q", err.Error(), c.ErrorSubstring)
}
}
}
func TestComponentErrorChaining(t *testing.T) {
// Build a component with an error.
comp := NewComponent(types.Typ[types.Uint32], operand.Mem{}).Index(3)
_, expect := comp.Resolve()
if expect == nil {
t.Fatal("expected error")
}
// Confirm that the error is preserved through chaining operations.
cases := []Component{
comp.Dereference(reg.R13),
comp.Base(),
comp.Len(),
comp.Cap(),
comp.Real(),
comp.Imag(),
comp.Index(42),
comp.Field("field"),
}
for _, c := range cases {
_, err := c.Resolve()
if err != expect {
t.Fatal("chaining should preserve error")
}
}
}

View File

@@ -7,6 +7,8 @@ import (
"go/token"
"go/types"
"strconv"
"github.com/mmcloughlin/avo/operand"
)
// Signature represents a Go function signature.
@@ -136,7 +138,8 @@ func newTuple(t *types.Tuple, offsets []int64, size int64, defaultprefix string)
name += strconv.Itoa(i)
}
}
c := NewComponent(name, v.Type(), int(offsets[i]))
addr := operand.NewParamAddr(name, int(offsets[i]))
c := NewComponent(v.Type(), addr)
tuple.components = append(tuple.components, c)
if v.Name() != "" {
tuple.byname[v.Name()] = c