| 1 | package gotypes |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "go/token" |
| 8 | "go/types" |
| 9 | "strconv" |
| 10 | |
| 11 | "github.com/mmcloughlin/avo/operand" |
| 12 | ) |
| 13 | |
| 14 | // Signature represents a Go function signature. |
| 15 | type Signature struct { |
| 16 | pkg *types.Package |
| 17 | sig *types.Signature |
| 18 | params *Tuple |
| 19 | results *Tuple |
| 20 | } |
| 21 | |
| 22 | // NewSignature constructs a Signature. |
| 23 | func NewSignature(pkg *types.Package, sig *types.Signature) *Signature { |
| 24 | s := &Signature{ |
| 25 | pkg: pkg, |
| 26 | sig: sig, |
| 27 | } |
| 28 | s.init() |
| 29 | return s |
| 30 | } |
| 31 | |
| 32 | // NewSignatureVoid builds the void signature "func()". |
| 33 | func NewSignatureVoid() *Signature { |
| 34 | return NewSignature(nil, types.NewSignatureType(nil, nil, nil, nil, nil, false)) |
| 35 | } |
| 36 | |
| 37 | // LookupSignature returns the signature of the named function in the provided package. |
| 38 | func LookupSignature(pkg *types.Package, name string) (*Signature, error) { |
| 39 | scope := pkg.Scope() |
| 40 | obj := scope.Lookup(name) |
| 41 | if obj == nil { |
| 42 | return nil, fmt.Errorf("could not find function \"%s\"", name) |
| 43 | } |
| 44 | s, ok := obj.Type().(*types.Signature) |
| 45 | if !ok { |
| 46 | return nil, fmt.Errorf("object \"%s\" does not have signature type", name) |
| 47 | } |
| 48 | return NewSignature(pkg, s), nil |
| 49 | } |
| 50 | |
| 51 | // ParseSignature builds a Signature by parsing a Go function type expression. |
| 52 | // The function type must reference builtin types only; see |
| 53 | // ParseSignatureInPackage if custom types are required. |
| 54 | func ParseSignature(expr string) (*Signature, error) { |
| 55 | return ParseSignatureInPackage(nil, expr) |
| 56 | } |
| 57 | |
| 58 | // ParseSignatureInPackage builds a Signature by parsing a Go function type |
| 59 | // expression. The expression may reference types in the provided package. |
| 60 | func ParseSignatureInPackage(pkg *types.Package, expr string) (*Signature, error) { |
| 61 | tv, err := types.Eval(token.NewFileSet(), pkg, token.NoPos, expr) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | if tv.Value != nil { |
| 66 | return nil, errors.New("signature expression should have nil value") |
| 67 | } |
| 68 | s, ok := tv.Type.(*types.Signature) |
| 69 | if !ok { |
| 70 | return nil, errors.New("provided type is not a function signature") |
| 71 | } |
| 72 | return NewSignature(pkg, s), nil |
| 73 | } |
| 74 | |
| 75 | // Params returns the function signature argument types. |
| 76 | func (s *Signature) Params() *Tuple { return s.params } |
| 77 | |
| 78 | // Results returns the function return types. |
| 79 | func (s *Signature) Results() *Tuple { return s.results } |
| 80 | |
| 81 | // Bytes returns the total size of the function arguments and return values. |
| 82 | func (s *Signature) Bytes() int { return s.Params().Bytes() + s.Results().Bytes() } |
| 83 | |
| 84 | // String writes Signature as a string. This does not include the "func" keyword. |
| 85 | func (s *Signature) String() string { |
| 86 | var buf bytes.Buffer |
| 87 | types.WriteSignature(&buf, s.sig, func(pkg *types.Package) string { |
| 88 | if pkg == s.pkg { |
| 89 | return "" |
| 90 | } |
| 91 | return pkg.Name() |
| 92 | }) |
| 93 | return buf.String() |
| 94 | } |
| 95 | |
| 96 | func (s *Signature) init() { |
| 97 | p := s.sig.Params() |
| 98 | r := s.sig.Results() |
| 99 | |
| 100 | // Compute parameter offsets. Note that if the function has results, |
| 101 | // additional padding up to max align is inserted between parameters and |
| 102 | // results. |
| 103 | vs := tuplevars(p) |
| 104 | vs = append(vs, types.NewParam(token.NoPos, nil, "sentinel", types.Typ[types.Uint64])) |
| 105 | paramsoffsets := Sizes.Offsetsof(vs) |
| 106 | paramssize := paramsoffsets[p.Len()] |
| 107 | if r.Len() == 0 { |
| 108 | paramssize = structsize(vs[:p.Len()]) |
| 109 | } |
| 110 | s.params = newTuple(p, paramsoffsets, paramssize, "arg") |
| 111 | |
| 112 | // Result offsets. |
| 113 | vs = tuplevars(r) |
| 114 | resultsoffsets := Sizes.Offsetsof(vs) |
| 115 | resultssize := structsize(vs) |
| 116 | for i := range resultsoffsets { |
| 117 | resultsoffsets[i] += paramssize |
| 118 | } |
| 119 | s.results = newTuple(r, resultsoffsets, resultssize, "ret") |
| 120 | } |
| 121 | |
| 122 | // Tuple represents a tuple of variables, such as function arguments or results. |
| 123 | type Tuple struct { |
| 124 | components []Component |
| 125 | byname map[string]Component |
| 126 | size int |
| 127 | } |
| 128 | |
| 129 | func newTuple(t *types.Tuple, offsets []int64, size int64, defaultprefix string) *Tuple { |
| 130 | tuple := &Tuple{ |
| 131 | byname: map[string]Component{}, |
| 132 | size: int(size), |
| 133 | } |
| 134 | for i := 0; i < t.Len(); i++ { |
| 135 | v := t.At(i) |
| 136 | name := v.Name() |
| 137 | if name == "" { |
| 138 | name = defaultprefix |
| 139 | if i > 0 { |
| 140 | name += strconv.Itoa(i) |
| 141 | } |
| 142 | } |
| 143 | addr := operand.NewParamAddr(name, int(offsets[i])) |
| 144 | c := NewComponent(v.Type(), addr) |
| 145 | tuple.components = append(tuple.components, c) |
| 146 | if v.Name() != "" { |
| 147 | tuple.byname[v.Name()] = c |
| 148 | } |
| 149 | } |
| 150 | return tuple |
| 151 | } |
| 152 | |
| 153 | // Lookup returns the variable with the given name. |
| 154 | func (t *Tuple) Lookup(name string) Component { |
| 155 | e := t.byname[name] |
| 156 | if e == nil { |
| 157 | return errorf("unknown variable \"%s\"", name) |
| 158 | } |
| 159 | return e |
| 160 | } |
| 161 | |
| 162 | // At returns the variable at index i. |
| 163 | func (t *Tuple) At(i int) Component { |
| 164 | if i >= len(t.components) { |
| 165 | return errorf("index out of range") |
| 166 | } |
| 167 | return t.components[i] |
| 168 | } |
| 169 | |
| 170 | // Bytes returns the size of the Tuple. This may include additional padding. |
| 171 | func (t *Tuple) Bytes() int { return t.size } |
| 172 | |
| 173 | func tuplevars(t *types.Tuple) []*types.Var { |
| 174 | vs := make([]*types.Var, t.Len()) |
| 175 | for i := 0; i < t.Len(); i++ { |
| 176 | vs[i] = t.At(i) |
| 177 | } |
| 178 | return vs |
| 179 | } |
| 180 | |
| 181 | // structsize computes the size of a struct containing the given variables as |
| 182 | // fields. It would be equivalent to calculating the size of types.NewStruct(vs, |
| 183 | // nil), apart from the fact that NewStruct panics if multiple fields have the |
| 184 | // same name, and this happens for example if the variables represent return |
| 185 | // types from a function. |
| 186 | func structsize(vs []*types.Var) int64 { |
| 187 | n := len(vs) |
| 188 | if n == 0 { |
| 189 | return 0 |
| 190 | } |
| 191 | offsets := Sizes.Offsetsof(vs) |
| 192 | return offsets[n-1] + Sizes.Sizeof(vs[n-1].Type()) |
| 193 | } |