parameter loading

This commit is contained in:
Michael McLoughlin
2018-12-08 20:14:51 -08:00
parent bbbf6399a1
commit 69ee0e39cb
9 changed files with 355 additions and 61 deletions

View File

@@ -35,28 +35,30 @@ func (c componenterr) Index(int) Component { return c }
type component struct {
name string
offset int
typ types.Type
offset int
err error
}
func NewComponentFromVar(v *types.Var) Component {
func NewComponent(name string, t types.Type, offset int) Component {
return &component{
name: v.Name(),
name: name,
typ: t,
offset: offset,
}
}
func NewComponentFromVar(v *types.Var, offset int) Component {
return NewComponent(v.Name(), v.Type(), offset)
}
func (c *component) Resolve() (*Basic, error) {
b, ok := c.typ.(*types.Basic)
if !ok {
return nil, errors.New("not basic type")
}
if b.Kind() == types.String {
return nil, errors.New("string types are handled specially")
if !isprimitive(c.typ) {
return nil, errors.New("component is not primitive")
}
return &Basic{
Addr: operand.NewParamAddr(c.name, c.offset),
Type: b,
Type: c.typ.(*types.Basic),
}, nil
}
@@ -134,51 +136,7 @@ func (c *component) sub(suffix string, offset int, t types.Type) *component {
}
// TODO(mbm): gotypes.Component handling for structs
// TODO(mbm): gotypes.Component handling for complex
type Signature struct {
sig *types.Signature
}
func newSignature(sig *types.Signature) *Signature {
return &Signature{
sig: sig,
}
}
func ParseSignature(expr string) (*Signature, error) {
tv, err := types.Eval(token.NewFileSet(), nil, token.NoPos, expr)
if err != nil {
return nil, err
}
if tv.Value != nil {
return nil, errors.New("signature expression should have nil value")
}
s, ok := tv.Type.(*types.Signature)
if !ok {
return nil, errors.New("provided type is not a function signature")
}
return newSignature(s), nil
}
func (s *Signature) Parameter(name string) Component {
p := s.param(name)
if p == nil {
return componenterr("unknown parameter")
}
return NewComponentFromVar(p)
}
func (s *Signature) param(name string) *types.Var {
t := s.sig.Params()
for i := 0; i < t.Len(); i++ {
p := t.At(i)
if p.Name() == name {
return p
}
}
return nil
}
// TODO(mbm): gotypes.Component handling for complex64/128
func isslice(t types.Type) bool {
_, ok := t.(*types.Slice)
@@ -189,3 +147,9 @@ func isstring(t types.Type) bool {
b, ok := t.(*types.Basic)
return ok && b.Kind() == types.String
}
// isprimitive returns true if the type cannot be broken into components.
func isprimitive(t types.Type) bool {
b, ok := t.(*types.Basic)
return ok && (b.Info()&(types.IsString|types.IsComplex)) == 0
}

101
gotypes/signature.go Normal file
View File

@@ -0,0 +1,101 @@
package gotypes
import (
"errors"
"go/token"
"go/types"
"strconv"
)
type Signature struct {
sig *types.Signature
params *Tuple
results *Tuple
}
func NewSignature(sig *types.Signature) *Signature {
s := &Signature{
sig: sig,
}
s.init()
return s
}
func ParseSignature(expr string) (*Signature, error) {
tv, err := types.Eval(token.NewFileSet(), nil, token.NoPos, expr)
if err != nil {
return nil, err
}
if tv.Value != nil {
return nil, errors.New("signature expression should have nil value")
}
s, ok := tv.Type.(*types.Signature)
if !ok {
return nil, errors.New("provided type is not a function signature")
}
return NewSignature(s), nil
}
func (s *Signature) Params() *Tuple { return s.params }
func (s *Signature) Results() *Tuple { return s.results }
func (s *Signature) Bytes() int { return s.Params().Bytes() + s.Results().Bytes() }
func (s *Signature) init() {
p := s.sig.Params()
r := s.sig.Results()
// Compute offsets of parameters and return values.
vs := tuplevars(p)
vs = append(vs, tuplevars(r)...)
vs = append(vs, types.NewParam(token.NoPos, nil, "sentinel", types.Typ[types.Uint64]))
offsets := Sizes.Offsetsof(vs)
// Build components for them.
s.params = newTuple(p, offsets, "arg")
s.results = newTuple(r, offsets[p.Len():], "ret")
}
type Tuple struct {
components []Component
byname map[string]Component
size int
}
func newTuple(t *types.Tuple, offsets []int64, defaultprefix string) *Tuple {
tuple := &Tuple{
byname: map[string]Component{},
size: int(offsets[t.Len()]),
}
for i := 0; i < t.Len(); i++ {
v := t.At(i)
name := v.Name()
if name == "" {
name = defaultprefix
if i > 0 {
name += strconv.Itoa(i)
}
}
c := NewComponent(name, v.Type(), int(offsets[i]))
tuple.components = append(tuple.components, c)
if v.Name() != "" {
tuple.byname[v.Name()] = c
}
}
return tuple
}
func (t *Tuple) Lookup(name string) Component { return t.byname[name] }
func (t *Tuple) At(i int) Component { return t.components[i] }
func (t *Tuple) Bytes() int { return t.size }
func tuplevars(t *types.Tuple) []*types.Var {
vs := make([]*types.Var, t.Len())
for i := 0; i < t.Len(); i++ {
vs[i] = t.At(i)
}
return vs
}

View File

@@ -51,11 +51,11 @@ func TestParseSignature(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if !TypesTuplesEqual(s.Params(), c.ExpectParams) {
t.Errorf("parameter mismatch\ngot %#v\nexpect %#v\n", s.Params(), c.ExpectParams)
if !TypesTuplesEqual(s.sig.Params(), c.ExpectParams) {
t.Errorf("parameter mismatch\ngot %#v\nexpect %#v\n", s.sig.Params(), c.ExpectParams)
}
if !TypesTuplesEqual(s.Results(), c.ExpectReturn) {
t.Errorf("return value(s) mismatch\ngot %#v\nexpect %#v\n", s.Results(), c.ExpectReturn)
if !TypesTuplesEqual(s.sig.Results(), c.ExpectReturn) {
t.Errorf("return value(s) mismatch\ngot %#v\nexpect %#v\n", s.sig.Results(), c.ExpectReturn)
}
}
}