support signatures and param load/stores

This commit is contained in:
Michael McLoughlin
2018-12-08 21:16:03 -08:00
parent 69ee0e39cb
commit 5431f2edef
9 changed files with 115 additions and 18 deletions

View File

@@ -1,7 +1,9 @@
package gotypes
import (
"bytes"
"errors"
"fmt"
"go/token"
"go/types"
"strconv"
@@ -21,6 +23,10 @@ func NewSignature(sig *types.Signature) *Signature {
return s
}
func NewSignatureVoid() *Signature {
return NewSignature(types.NewSignature(nil, nil, nil, false))
}
func ParseSignature(expr string) (*Signature, error) {
tv, err := types.Eval(token.NewFileSet(), nil, token.NoPos, expr)
if err != nil {
@@ -42,6 +48,12 @@ func (s *Signature) Results() *Tuple { return s.results }
func (s *Signature) Bytes() int { return s.Params().Bytes() + s.Results().Bytes() }
func (s *Signature) String() string {
var buf bytes.Buffer
types.WriteSignature(&buf, s.sig, nil)
return buf.String()
}
func (s *Signature) init() {
p := s.sig.Params()
r := s.sig.Results()
@@ -66,7 +78,7 @@ type Tuple struct {
func newTuple(t *types.Tuple, offsets []int64, defaultprefix string) *Tuple {
tuple := &Tuple{
byname: map[string]Component{},
size: int(offsets[t.Len()]),
size: int(offsets[t.Len()] - offsets[0]),
}
for i := 0; i < t.Len(); i++ {
v := t.At(i)
@@ -86,7 +98,13 @@ func newTuple(t *types.Tuple, offsets []int64, defaultprefix string) *Tuple {
return tuple
}
func (t *Tuple) Lookup(name string) Component { return t.byname[name] }
func (t *Tuple) Lookup(name string) Component {
e := t.byname[name]
if e == nil {
return componenterr(fmt.Sprintf("unknown variable \"%s\"", name))
}
return e
}
func (t *Tuple) At(i int) Component { return t.components[i] }