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"
|
2019-01-22 22:35:01 -08:00
|
|
|
"fmt"
|
2018-12-08 20:14:51 -08:00
|
|
|
"go/token"
|
|
|
|
|
"go/types"
|
|
|
|
|
"strconv"
|
2019-01-27 19:22:21 -08:00
|
|
|
|
|
|
|
|
"github.com/mmcloughlin/avo/operand"
|
2018-12-08 20:14:51 -08:00
|
|
|
)
|
|
|
|
|
|
2019-01-04 20:38:21 -08:00
|
|
|
// Signature represents a Go function signature.
|
2018-12-08 20:14:51 -08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-04 20:38:21 -08:00
|
|
|
// NewSignature constructs a Signature.
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-04 20:38:21 -08:00
|
|
|
// NewSignatureVoid builds the void signature "func()".
|
2018-12-08 21:16:03 -08:00
|
|
|
func NewSignatureVoid() *Signature {
|
2023-03-05 20:04:45 -08:00
|
|
|
return NewSignature(nil, types.NewSignatureType(nil, nil, nil, nil, nil, false))
|
2018-12-08 21:16:03 -08:00
|
|
|
}
|
|
|
|
|
|
2019-01-22 22:35:01 -08:00
|
|
|
// LookupSignature returns the signature of the named function in the provided package.
|
|
|
|
|
func LookupSignature(pkg *types.Package, name string) (*Signature, error) {
|
|
|
|
|
scope := pkg.Scope()
|
|
|
|
|
obj := scope.Lookup(name)
|
|
|
|
|
if obj == nil {
|
|
|
|
|
return nil, fmt.Errorf("could not find function \"%s\"", name)
|
|
|
|
|
}
|
|
|
|
|
s, ok := obj.Type().(*types.Signature)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, fmt.Errorf("object \"%s\" does not have signature type", name)
|
|
|
|
|
}
|
|
|
|
|
return NewSignature(pkg, s), nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-04 20:38:21 -08:00
|
|
|
// ParseSignature builds a Signature by parsing a Go function type expression.
|
|
|
|
|
// The function type must reference builtin types only; see
|
|
|
|
|
// ParseSignatureInPackage if custom types are required.
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-04 20:38:21 -08:00
|
|
|
// ParseSignatureInPackage builds a Signature by parsing a Go function type
|
|
|
|
|
// expression. The expression may reference types in the provided package.
|
2018-12-17 20:52:26 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2019-01-04 20:38:21 -08:00
|
|
|
// Params returns the function signature argument types.
|
2018-12-08 20:14:51 -08:00
|
|
|
func (s *Signature) Params() *Tuple { return s.params }
|
|
|
|
|
|
2019-01-04 20:38:21 -08:00
|
|
|
// Results returns the function return types.
|
2018-12-08 20:14:51 -08:00
|
|
|
func (s *Signature) Results() *Tuple { return s.results }
|
|
|
|
|
|
2019-01-04 20:38:21 -08:00
|
|
|
// Bytes returns the total size of the function arguments and return values.
|
2018-12-08 20:14:51 -08:00
|
|
|
func (s *Signature) Bytes() int { return s.Params().Bytes() + s.Results().Bytes() }
|
|
|
|
|
|
2019-01-04 20:38:21 -08:00
|
|
|
// String writes Signature as a string. This does not include the "func" keyword.
|
2018-12-08 21:16:03 -08:00
|
|
|
func (s *Signature) String() string {
|
|
|
|
|
var buf bytes.Buffer
|
2019-01-22 22:35:01 -08:00
|
|
|
types.WriteSignature(&buf, s.sig, func(pkg *types.Package) string {
|
|
|
|
|
if pkg == s.pkg {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return pkg.Name()
|
|
|
|
|
})
|
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()
|
|
|
|
|
|
2021-10-30 13:32:25 -07:00
|
|
|
// Compute parameter offsets. Note that if the function has results,
|
|
|
|
|
// additional padding up to max align is inserted between parameters and
|
|
|
|
|
// results.
|
2018-12-08 20:14:51 -08:00
|
|
|
vs := tuplevars(p)
|
|
|
|
|
vs = append(vs, types.NewParam(token.NoPos, nil, "sentinel", types.Typ[types.Uint64]))
|
2018-12-19 13:08:56 -08:00
|
|
|
paramsoffsets := Sizes.Offsetsof(vs)
|
|
|
|
|
paramssize := paramsoffsets[p.Len()]
|
2021-10-30 13:32:25 -07:00
|
|
|
if r.Len() == 0 {
|
|
|
|
|
paramssize = structsize(vs[:p.Len()])
|
|
|
|
|
}
|
2018-12-19 13:08:56 -08:00
|
|
|
s.params = newTuple(p, paramsoffsets, paramssize, "arg")
|
|
|
|
|
|
|
|
|
|
// Result offsets.
|
|
|
|
|
vs = tuplevars(r)
|
|
|
|
|
resultsoffsets := Sizes.Offsetsof(vs)
|
2021-10-30 13:32:25 -07:00
|
|
|
resultssize := structsize(vs)
|
2018-12-19 13:08:56 -08:00
|
|
|
for i := range resultsoffsets {
|
|
|
|
|
resultsoffsets[i] += paramssize
|
|
|
|
|
}
|
|
|
|
|
s.results = newTuple(r, resultsoffsets, resultssize, "ret")
|
2018-12-08 20:14:51 -08:00
|
|
|
}
|
|
|
|
|
|
2019-01-04 20:38:21 -08:00
|
|
|
// Tuple represents a tuple of variables, such as function arguments or results.
|
2018-12-08 20:14:51 -08:00
|
|
|
type Tuple struct {
|
|
|
|
|
components []Component
|
|
|
|
|
byname map[string]Component
|
|
|
|
|
size int
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-19 13:08:56 -08:00
|
|
|
func newTuple(t *types.Tuple, offsets []int64, size int64, defaultprefix string) *Tuple {
|
2018-12-08 20:14:51 -08:00
|
|
|
tuple := &Tuple{
|
|
|
|
|
byname: map[string]Component{},
|
2018-12-19 13:08:56 -08:00
|
|
|
size: int(size),
|
2018-12-08 20:14:51 -08:00
|
|
|
}
|
2024-12-22 19:01:48 -05:00
|
|
|
for i := range t.Len() {
|
2018-12-08 20:14:51 -08:00
|
|
|
v := t.At(i)
|
|
|
|
|
name := v.Name()
|
|
|
|
|
if name == "" {
|
|
|
|
|
name = defaultprefix
|
|
|
|
|
if i > 0 {
|
|
|
|
|
name += strconv.Itoa(i)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-27 19:22:21 -08:00
|
|
|
addr := operand.NewParamAddr(name, int(offsets[i]))
|
|
|
|
|
c := NewComponent(v.Type(), addr)
|
2018-12-08 20:14:51 -08:00
|
|
|
tuple.components = append(tuple.components, c)
|
|
|
|
|
if v.Name() != "" {
|
|
|
|
|
tuple.byname[v.Name()] = c
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return tuple
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-04 20:38:21 -08:00
|
|
|
// Lookup returns the variable with the given name.
|
2018-12-08 21:16:03 -08:00
|
|
|
func (t *Tuple) Lookup(name string) Component {
|
|
|
|
|
e := t.byname[name]
|
|
|
|
|
if e == nil {
|
2018-12-19 22:09:55 -08:00
|
|
|
return errorf("unknown variable \"%s\"", name)
|
2018-12-08 21:16:03 -08:00
|
|
|
}
|
|
|
|
|
return e
|
|
|
|
|
}
|
2018-12-08 20:14:51 -08:00
|
|
|
|
2019-01-04 20:38:21 -08:00
|
|
|
// At returns the variable at index i.
|
2018-12-19 22:09:55 -08:00
|
|
|
func (t *Tuple) At(i int) Component {
|
|
|
|
|
if i >= len(t.components) {
|
|
|
|
|
return errorf("index out of range")
|
|
|
|
|
}
|
|
|
|
|
return t.components[i]
|
|
|
|
|
}
|
2018-12-08 20:14:51 -08:00
|
|
|
|
2019-01-04 20:38:21 -08:00
|
|
|
// Bytes returns the size of the Tuple. This may include additional padding.
|
2018-12-08 20:14:51 -08:00
|
|
|
func (t *Tuple) Bytes() int { return t.size }
|
|
|
|
|
|
|
|
|
|
func tuplevars(t *types.Tuple) []*types.Var {
|
|
|
|
|
vs := make([]*types.Var, t.Len())
|
2024-12-22 19:01:48 -05:00
|
|
|
for i := range t.Len() {
|
2018-12-08 20:14:51 -08:00
|
|
|
vs[i] = t.At(i)
|
|
|
|
|
}
|
|
|
|
|
return vs
|
|
|
|
|
}
|
2021-10-30 13:32:25 -07:00
|
|
|
|
|
|
|
|
// structsize computes the size of a struct containing the given variables as
|
|
|
|
|
// fields. It would be equivalent to calculating the size of types.NewStruct(vs,
|
|
|
|
|
// nil), apart from the fact that NewStruct panics if multiple fields have the
|
|
|
|
|
// same name, and this happens for example if the variables represent return
|
|
|
|
|
// types from a function.
|
|
|
|
|
func structsize(vs []*types.Var) int64 {
|
|
|
|
|
n := len(vs)
|
|
|
|
|
if n == 0 {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
offsets := Sizes.Offsetsof(vs)
|
|
|
|
|
return offsets[n-1] + Sizes.Sizeof(vs[n-1].Type())
|
|
|
|
|
}
|