2018-12-08 20:14:51 -08:00
|
|
|
package gotypes
|
|
|
|
|
|
|
|
|
|
import (
|
2018-12-08 21:16:03 -08:00
|
|
|
"bytes"
|
2018-12-08 20:14:51 -08:00
|
|
|
"errors"
|
2018-12-08 21:16:03 -08:00
|
|
|
"fmt"
|
2018-12-08 20:14:51 -08:00
|
|
|
"go/token"
|
|
|
|
|
"go/types"
|
|
|
|
|
"strconv"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Signature struct {
|
2018-12-17 20:52:26 -08:00
|
|
|
pkg *types.Package
|
2018-12-08 20:14:51 -08:00
|
|
|
sig *types.Signature
|
|
|
|
|
params *Tuple
|
|
|
|
|
results *Tuple
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-17 20:52:26 -08:00
|
|
|
func NewSignature(pkg *types.Package, sig *types.Signature) *Signature {
|
2018-12-08 20:14:51 -08:00
|
|
|
s := &Signature{
|
2018-12-17 20:52:26 -08:00
|
|
|
pkg: pkg,
|
2018-12-08 20:14:51 -08:00
|
|
|
sig: sig,
|
|
|
|
|
}
|
|
|
|
|
s.init()
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-08 21:16:03 -08:00
|
|
|
func NewSignatureVoid() *Signature {
|
2018-12-17 20:52:26 -08:00
|
|
|
return NewSignature(nil, types.NewSignature(nil, nil, nil, false))
|
2018-12-08 21:16:03 -08:00
|
|
|
}
|
|
|
|
|
|
2018-12-08 20:14:51 -08:00
|
|
|
func ParseSignature(expr string) (*Signature, error) {
|
2018-12-17 20:52:26 -08:00
|
|
|
return ParseSignatureInPackage(nil, expr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ParseSignatureInPackage(pkg *types.Package, expr string) (*Signature, error) {
|
|
|
|
|
tv, err := types.Eval(token.NewFileSet(), pkg, token.NoPos, expr)
|
2018-12-08 20:14:51 -08:00
|
|
|
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")
|
|
|
|
|
}
|
2018-12-17 20:52:26 -08:00
|
|
|
return NewSignature(pkg, s), nil
|
2018-12-08 20:14:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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() }
|
|
|
|
|
|
2018-12-08 21:16:03 -08:00
|
|
|
func (s *Signature) String() string {
|
|
|
|
|
var buf bytes.Buffer
|
2018-12-17 20:52:26 -08:00
|
|
|
types.WriteSignature(&buf, s.sig, types.RelativeTo(s.pkg))
|
2018-12-08 21:16:03 -08:00
|
|
|
return buf.String()
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-08 20:14:51 -08:00
|
|
|
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{},
|
2018-12-08 21:16:03 -08:00
|
|
|
size: int(offsets[t.Len()] - offsets[0]),
|
2018-12-08 20:14:51 -08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-08 21:16:03 -08:00
|
|
|
func (t *Tuple) Lookup(name string) Component {
|
|
|
|
|
e := t.byname[name]
|
|
|
|
|
if e == nil {
|
|
|
|
|
return componenterr(fmt.Sprintf("unknown variable \"%s\"", name))
|
|
|
|
|
}
|
|
|
|
|
return e
|
|
|
|
|
}
|
2018-12-08 20:14:51 -08:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|