2013-05-17 14:25:48 -06:00
|
|
|
package ssa
|
|
|
|
|
2013-06-14 13:50:37 -06:00
|
|
|
// This file defines utilities for method-set computation, synthesis
|
|
|
|
// of wrapper methods, and desugaring of implicit field selections.
|
|
|
|
//
|
|
|
|
// Wrappers include:
|
|
|
|
// - promotion wrappers for methods of embedded fields.
|
|
|
|
// - interface method wrappers for closures of I.f.
|
|
|
|
// - bound method wrappers, for uncalled obj.Method closures.
|
|
|
|
// - indirection wrappers, for calls to T-methods on a *T receiver.
|
|
|
|
|
2013-07-15 16:09:18 -06:00
|
|
|
// TODO(adonovan): rename to wrappers.go when promotion logic has evaporated.
|
2013-05-17 14:25:48 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"code.google.com/p/go.tools/go/types"
|
2013-05-30 07:59:17 -06:00
|
|
|
"fmt"
|
|
|
|
"go/token"
|
2013-05-17 14:25:48 -06:00
|
|
|
)
|
|
|
|
|
2013-06-24 12:15:13 -06:00
|
|
|
// anonFieldPath is a linked list of anonymous fields that
|
2013-05-17 14:25:48 -06:00
|
|
|
// breadth-first traversal has entered, rightmost (outermost) first.
|
|
|
|
// e.g. "e.f" denoting "e.A.B.C.f" would have a path [C, B, A].
|
|
|
|
// Common tails may be shared.
|
|
|
|
//
|
|
|
|
// It is used by various "promotion"-related algorithms.
|
|
|
|
//
|
|
|
|
type anonFieldPath struct {
|
|
|
|
tail *anonFieldPath
|
|
|
|
index int // index of field within enclosing types.Struct.Fields
|
|
|
|
field *types.Field
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *anonFieldPath) contains(f *types.Field) bool {
|
|
|
|
for ; p != nil; p = p.tail {
|
|
|
|
if p.field == f {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// reverse returns the linked list reversed, as a slice.
|
|
|
|
func (p *anonFieldPath) reverse() []*anonFieldPath {
|
|
|
|
n := 0
|
|
|
|
for q := p; q != nil; q = q.tail {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
s := make([]*anonFieldPath, n)
|
|
|
|
n = 0
|
|
|
|
for ; p != nil; p = p.tail {
|
|
|
|
s[len(s)-1-n] = p
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// isIndirect returns true if the path indirects a pointer.
|
|
|
|
func (p *anonFieldPath) isIndirect() bool {
|
|
|
|
for ; p != nil; p = p.tail {
|
2013-06-04 13:15:41 -06:00
|
|
|
if isPointer(p.field.Type()) {
|
2013-05-17 14:25:48 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method Set construction ----------------------------------------
|
|
|
|
|
|
|
|
// A candidate is a method eligible for promotion: a method of an
|
|
|
|
// abstract (interface) or concrete (anonymous struct or named) type,
|
|
|
|
// along with the anonymous field path via which it is implicitly
|
|
|
|
// reached. If there is exactly one candidate for a given id, it will
|
|
|
|
// be promoted to membership of the original type's method-set.
|
|
|
|
//
|
|
|
|
// Candidates with path=nil are trivially members of the original
|
|
|
|
// type's method-set.
|
|
|
|
//
|
|
|
|
type candidate struct {
|
2013-07-10 16:08:42 -06:00
|
|
|
method *types.Func // method object of abstract or concrete type
|
|
|
|
path *anonFieldPath // desugared selector path
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c candidate) String() string {
|
|
|
|
s := ""
|
|
|
|
// Inefficient!
|
|
|
|
for p := c.path; p != nil; p = p.tail {
|
2013-06-04 13:15:41 -06:00
|
|
|
s = "." + p.field.Name() + s
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
2013-07-10 16:01:11 -06:00
|
|
|
return s + "." + c.method.Name()
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-07-10 16:08:42 -06:00
|
|
|
func (c candidate) isConcrete() bool {
|
|
|
|
return c.method.Type().(*types.Signature).Recv() != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ptrRecv returns true if this candidate is a concrete method with a
|
|
|
|
// pointer receiver.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
func (c candidate) ptrRecv() bool {
|
2013-07-10 16:08:42 -06:00
|
|
|
recv := c.method.Type().(*types.Signature).Recv()
|
|
|
|
return recv != nil && isPointer(recv.Type())
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-06-14 13:50:37 -06:00
|
|
|
// MethodSet returns the method set for type typ, building wrapper
|
|
|
|
// methods as needed for embedded field promotion, and indirection for
|
|
|
|
// *T receiver types, etc.
|
2013-05-17 14:25:48 -06:00
|
|
|
// A nil result indicates an empty set.
|
|
|
|
//
|
|
|
|
// Thread-safe.
|
2013-07-10 16:08:42 -06:00
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
func (p *Program) MethodSet(typ types.Type) MethodSet {
|
|
|
|
if !canHaveConcreteMethods(typ, true) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-14 13:50:37 -06:00
|
|
|
p.methodsMu.Lock()
|
|
|
|
defer p.methodsMu.Unlock()
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-07-10 16:01:11 -06:00
|
|
|
mset := p.methodSets.At(typ)
|
2013-05-17 14:25:48 -06:00
|
|
|
if mset == nil {
|
|
|
|
mset = buildMethodSet(p, typ)
|
2013-07-10 16:01:11 -06:00
|
|
|
p.methodSets.Set(typ, mset)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
2013-07-10 16:01:11 -06:00
|
|
|
return mset.(MethodSet)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// buildMethodSet computes the concrete method set for type typ.
|
|
|
|
// It is the implementation of Program.MethodSet.
|
|
|
|
//
|
2013-07-15 16:09:18 -06:00
|
|
|
// TODO(adonovan): use go/types.MethodSet(typ) when it's ready.
|
|
|
|
//
|
2013-07-10 16:08:42 -06:00
|
|
|
// EXCLUSIVE_LOCKS_REQUIRED(meth.Prog.methodsMu)
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
func buildMethodSet(prog *Program, typ types.Type) MethodSet {
|
|
|
|
if prog.mode&LogSource != 0 {
|
2013-07-10 16:08:42 -06:00
|
|
|
defer logStack("buildMethodSet %s", typ)()
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// cands maps ids (field and method names) encountered at any
|
|
|
|
// level of of the breadth-first traversal to a unique
|
|
|
|
// promotion candidate. A nil value indicates a "blocked" id
|
|
|
|
// (i.e. a field or ambiguous method).
|
|
|
|
//
|
|
|
|
// nextcands is the same but carries just the level in progress.
|
|
|
|
cands, nextcands := make(map[Id]*candidate), make(map[Id]*candidate)
|
|
|
|
|
|
|
|
var next, list []*anonFieldPath
|
|
|
|
list = append(list, nil) // hack: nil means "use typ"
|
|
|
|
|
|
|
|
// For each level of the type graph...
|
|
|
|
for len(list) > 0 {
|
|
|
|
// Invariant: next=[], nextcands={}.
|
|
|
|
|
|
|
|
// Collect selectors from one level into 'nextcands'.
|
|
|
|
// Record the next levels into 'next'.
|
|
|
|
for _, node := range list {
|
|
|
|
t := typ // first time only
|
|
|
|
if node != nil {
|
2013-06-04 13:15:41 -06:00
|
|
|
t = node.field.Type()
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
2013-07-12 22:09:33 -06:00
|
|
|
t = deref(t)
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-05-17 15:02:47 -06:00
|
|
|
if nt, ok := t.(*types.Named); ok {
|
2013-05-30 22:58:14 -06:00
|
|
|
for i, n := 0, nt.NumMethods(); i < n; i++ {
|
2013-07-10 16:08:42 -06:00
|
|
|
addCandidate(nextcands, nt.Method(i), node)
|
2013-05-30 22:58:14 -06:00
|
|
|
}
|
2013-05-17 15:02:47 -06:00
|
|
|
t = nt.Underlying()
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
switch t := t.(type) {
|
|
|
|
case *types.Interface:
|
2013-05-30 22:58:14 -06:00
|
|
|
for i, n := 0, t.NumMethods(); i < n; i++ {
|
2013-07-10 16:08:42 -06:00
|
|
|
addCandidate(nextcands, t.Method(i), node)
|
2013-05-30 22:58:14 -06:00
|
|
|
}
|
2013-05-17 14:25:48 -06:00
|
|
|
|
|
|
|
case *types.Struct:
|
2013-05-17 15:02:47 -06:00
|
|
|
for i, n := 0, t.NumFields(); i < n; i++ {
|
|
|
|
f := t.Field(i)
|
2013-07-16 10:23:55 -06:00
|
|
|
nextcands[makeId(f.Name(), f.Pkg())] = nil // a field: block id
|
2013-05-17 14:25:48 -06:00
|
|
|
// Queue up anonymous fields for next iteration.
|
|
|
|
// Break cycles to ensure termination.
|
2013-06-04 13:15:41 -06:00
|
|
|
if f.Anonymous() && !node.contains(f) {
|
2013-05-17 14:25:48 -06:00
|
|
|
next = append(next, &anonFieldPath{node, i, f})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Examine collected selectors.
|
|
|
|
// Promote unique, non-blocked ones to cands.
|
|
|
|
for id, cand := range nextcands {
|
|
|
|
delete(nextcands, id)
|
|
|
|
if cand == nil {
|
|
|
|
// Update cands so we ignore it at all deeper levels.
|
|
|
|
// Don't clobber existing (shallower) binding!
|
|
|
|
if _, ok := cands[id]; !ok {
|
|
|
|
cands[id] = nil // block id
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := cands[id]; ok {
|
|
|
|
// Ignore candidate: a shallower binding exists.
|
|
|
|
} else {
|
|
|
|
cands[id] = cand
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list, next = next, list[:0] // reuse array
|
|
|
|
}
|
|
|
|
|
2013-06-14 13:50:37 -06:00
|
|
|
// Build method sets and wrapper methods.
|
2013-05-17 14:25:48 -06:00
|
|
|
mset := make(MethodSet)
|
|
|
|
for id, cand := range cands {
|
|
|
|
if cand == nil {
|
|
|
|
continue // blocked; ignore
|
|
|
|
}
|
2013-06-13 12:43:35 -06:00
|
|
|
if cand.ptrRecv() && !isPointer(typ) && !cand.path.isIndirect() {
|
2013-05-17 14:25:48 -06:00
|
|
|
// A candidate concrete method f with receiver
|
|
|
|
// *C is promoted into the method set of
|
|
|
|
// (non-pointer) E iff the implicit path selection
|
|
|
|
// is indirect, e.g. e.A->B.C.f
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var method *Function
|
|
|
|
if cand.path == nil {
|
2013-06-13 12:43:35 -06:00
|
|
|
// Trivial member of method-set; no promotion needed.
|
2013-07-10 16:08:42 -06:00
|
|
|
method = prog.concreteMethods[cand.method]
|
2013-06-13 12:43:35 -06:00
|
|
|
|
|
|
|
if !cand.ptrRecv() && isPointer(typ) {
|
|
|
|
// Call to method on T from receiver of type *T.
|
|
|
|
method = indirectionWrapper(method)
|
|
|
|
}
|
2013-05-17 14:25:48 -06:00
|
|
|
} else {
|
2013-06-14 13:50:37 -06:00
|
|
|
method = promotionWrapper(prog, typ, cand)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
if method == nil {
|
|
|
|
panic("unexpected nil method in method set")
|
|
|
|
}
|
|
|
|
mset[id] = method
|
|
|
|
}
|
|
|
|
return mset
|
|
|
|
}
|
|
|
|
|
2013-07-10 16:08:42 -06:00
|
|
|
// addCandidate adds the promotion candidate (method, node) to m[(name, package)].
|
|
|
|
// If a map entry already exists (whether nil or not), its value is set to nil.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-07-10 16:08:42 -06:00
|
|
|
func addCandidate(m map[Id]*candidate, method *types.Func, node *anonFieldPath) {
|
2013-07-16 10:23:55 -06:00
|
|
|
id := makeId(method.Name(), method.Pkg())
|
2013-05-17 14:25:48 -06:00
|
|
|
prev, found := m[id]
|
|
|
|
switch {
|
|
|
|
case prev != nil:
|
|
|
|
// Two candidates for same selector: ambiguous; block it.
|
|
|
|
m[id] = nil
|
|
|
|
case found:
|
|
|
|
// Already blocked.
|
|
|
|
default:
|
|
|
|
// A viable candidate.
|
2013-07-10 16:08:42 -06:00
|
|
|
m[id] = &candidate{method, node}
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 13:50:37 -06:00
|
|
|
// promotionWrapper returns a synthetic Function that delegates to a
|
2013-05-17 14:25:48 -06:00
|
|
|
// "promoted" method. For example, given these decls:
|
|
|
|
//
|
|
|
|
// type A struct {B}
|
|
|
|
// type B struct {*C}
|
|
|
|
// type C ...
|
|
|
|
// func (*C) f()
|
|
|
|
//
|
2013-06-14 13:50:37 -06:00
|
|
|
// then promotionWrapper(typ=A, cand={method:(*C).f, path:[B,*C]}) will
|
|
|
|
// synthesize this wrapper method:
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// func (a A) f() { return a.B.C->f() }
|
|
|
|
//
|
|
|
|
// prog is the program to which the synthesized method will belong.
|
2013-06-14 13:50:37 -06:00
|
|
|
// typ is the receiver type of the wrapper method. cand is the
|
2013-05-17 14:25:48 -06:00
|
|
|
// candidate method to be promoted; it may be concrete or an interface
|
|
|
|
// method.
|
|
|
|
//
|
2013-07-10 16:08:42 -06:00
|
|
|
// EXCLUSIVE_LOCKS_REQUIRED(meth.Prog.methodsMu)
|
|
|
|
//
|
2013-06-14 13:50:37 -06:00
|
|
|
func promotionWrapper(prog *Program, typ types.Type, cand *candidate) *Function {
|
2013-05-17 15:02:47 -06:00
|
|
|
old := cand.method.Type().(*types.Signature)
|
2013-06-04 13:15:41 -06:00
|
|
|
sig := types.NewSignature(types.NewVar(token.NoPos, nil, "recv", typ), old.Params(), old.Results(), old.IsVariadic())
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-06-14 13:50:37 -06:00
|
|
|
// TODO(adonovan): consult memoization cache keyed by (typ, cand).
|
|
|
|
// Needs typemap. Also needs hash/eq functions for 'candidate'.
|
2013-05-17 14:25:48 -06:00
|
|
|
if prog.mode&LogSource != 0 {
|
2013-07-10 16:01:11 -06:00
|
|
|
defer logStack("promotionWrapper (%s)%s, type %s", typ, cand, sig)()
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
2013-07-11 12:12:30 -06:00
|
|
|
// TODO(adonovan): is there a *types.Func for this function?
|
2013-05-17 14:25:48 -06:00
|
|
|
fn := &Function{
|
2013-05-30 07:59:17 -06:00
|
|
|
name: cand.method.Name(),
|
2013-05-17 15:02:47 -06:00
|
|
|
Signature: sig,
|
2013-07-10 16:01:11 -06:00
|
|
|
Synthetic: fmt.Sprintf("promotion wrapper for (%s)%s", typ, cand),
|
2013-05-17 14:25:48 -06:00
|
|
|
Prog: prog,
|
2013-07-03 15:57:20 -06:00
|
|
|
pos: cand.method.Pos(),
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
fn.startBody()
|
2013-05-17 15:02:47 -06:00
|
|
|
fn.addSpilledParam(sig.Recv())
|
2013-05-17 14:25:48 -06:00
|
|
|
createParams(fn)
|
|
|
|
|
2013-06-14 13:50:37 -06:00
|
|
|
// Each promotion wrapper performs a sequence of selections,
|
2013-05-17 14:25:48 -06:00
|
|
|
// then tailcalls the promoted method.
|
|
|
|
// We use pointer arithmetic (FieldAddr possibly followed by
|
|
|
|
// Load) in preference to value extraction (Field possibly
|
|
|
|
// preceded by Load).
|
|
|
|
var v Value = fn.Locals[0] // spilled receiver
|
|
|
|
if isPointer(typ) {
|
|
|
|
v = emitLoad(fn, v)
|
|
|
|
}
|
|
|
|
// Iterate over selections e.A.B.C.f in the natural order [A,B,C].
|
|
|
|
for _, p := range cand.path.reverse() {
|
|
|
|
// Loop invariant: v holds a pointer to a struct.
|
2013-07-12 22:09:33 -06:00
|
|
|
if _, ok := deref(v.Type()).Underlying().(*types.Struct); !ok {
|
2013-05-17 14:25:48 -06:00
|
|
|
panic(fmt.Sprint("not a *struct: ", v.Type(), p.field.Type))
|
|
|
|
}
|
|
|
|
sel := &FieldAddr{
|
|
|
|
X: v,
|
|
|
|
Field: p.index,
|
|
|
|
}
|
2013-07-16 10:23:55 -06:00
|
|
|
sel.setType(types.NewPointer(p.field.Type()))
|
2013-05-17 14:25:48 -06:00
|
|
|
v = fn.emit(sel)
|
2013-06-04 13:15:41 -06:00
|
|
|
if isPointer(p.field.Type()) {
|
2013-05-17 14:25:48 -06:00
|
|
|
v = emitLoad(fn, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !cand.ptrRecv() {
|
|
|
|
v = emitLoad(fn, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
var c Call
|
2013-07-10 16:08:42 -06:00
|
|
|
if cand.isConcrete() {
|
|
|
|
c.Call.Func = prog.concreteMethods[cand.method]
|
2013-05-17 14:25:48 -06:00
|
|
|
c.Call.Args = append(c.Call.Args, v)
|
|
|
|
} else {
|
2013-05-31 14:36:03 -06:00
|
|
|
iface := v.Type().Underlying().(*types.Interface)
|
2013-07-16 10:23:55 -06:00
|
|
|
id := makeId(cand.method.Name(), cand.method.Pkg())
|
2013-06-14 13:50:37 -06:00
|
|
|
c.Call.Method, _ = interfaceMethodIndex(iface, id)
|
2013-05-17 14:25:48 -06:00
|
|
|
c.Call.Recv = v
|
|
|
|
}
|
2013-05-22 15:56:18 -06:00
|
|
|
for _, arg := range fn.Params[1:] {
|
|
|
|
c.Call.Args = append(c.Call.Args, arg)
|
|
|
|
}
|
2013-05-17 14:25:48 -06:00
|
|
|
emitTailCall(fn, &c)
|
|
|
|
fn.finishBody()
|
|
|
|
return fn
|
|
|
|
}
|
|
|
|
|
2013-06-14 13:50:37 -06:00
|
|
|
// createParams creates parameters for wrapper method fn based on its
|
2013-06-13 12:43:35 -06:00
|
|
|
// Signature.Params, which do not include the receiver.
|
|
|
|
//
|
2013-05-17 14:25:48 -06:00
|
|
|
func createParams(fn *Function) {
|
|
|
|
var last *Parameter
|
2013-05-17 15:02:47 -06:00
|
|
|
tparams := fn.Signature.Params()
|
|
|
|
for i, n := 0, tparams.Len(); i < n; i++ {
|
2013-05-30 07:59:17 -06:00
|
|
|
last = fn.addParamObj(tparams.At(i))
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
2013-05-17 15:02:47 -06:00
|
|
|
if fn.Signature.IsVariadic() {
|
2013-05-30 07:59:17 -06:00
|
|
|
last.typ = types.NewSlice(last.typ)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 13:50:37 -06:00
|
|
|
// Wrappers for standalone interface methods ----------------------------------
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-06-14 13:50:37 -06:00
|
|
|
// interfaceMethodWrapper returns a synthetic wrapper function permitting a
|
2013-05-17 14:25:48 -06:00
|
|
|
// method id of interface typ to be called like a standalone function,
|
|
|
|
// e.g.:
|
|
|
|
//
|
|
|
|
// type I interface { f(x int) R }
|
2013-06-14 13:50:37 -06:00
|
|
|
// m := I.f // wrapper
|
2013-05-17 14:25:48 -06:00
|
|
|
// var i I
|
|
|
|
// m(i, 0)
|
|
|
|
//
|
2013-06-14 13:50:37 -06:00
|
|
|
// The wrapper is defined as if by:
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// func I.f(i I, x int, ...) R {
|
|
|
|
// return i.f(x, ...)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// TODO(adonovan): opt: currently the stub is created even when used
|
|
|
|
// in call position: I.f(i, 0). Clearly this is suboptimal.
|
|
|
|
//
|
2013-06-14 13:50:37 -06:00
|
|
|
// EXCLUSIVE_LOCKS_ACQUIRED(meth.Prog.methodsMu)
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-06-14 13:50:37 -06:00
|
|
|
func interfaceMethodWrapper(prog *Program, typ types.Type, id Id) *Function {
|
|
|
|
index, meth := interfaceMethodIndex(typ.Underlying().(*types.Interface), id)
|
|
|
|
prog.methodsMu.Lock()
|
|
|
|
defer prog.methodsMu.Unlock()
|
|
|
|
// If one interface embeds another they'll share the same
|
|
|
|
// wrappers for common methods. This is safe, but it might
|
|
|
|
// confuse some tools because of the implicit interface
|
|
|
|
// conversion applied to the first argument. If this becomes
|
|
|
|
// a problem, we should include 'typ' in the memoization key.
|
|
|
|
fn, ok := prog.ifaceMethodWrappers[meth]
|
|
|
|
if !ok {
|
|
|
|
if prog.mode&LogSource != 0 {
|
|
|
|
defer logStack("interfaceMethodWrapper %s.%s", typ, id)()
|
|
|
|
}
|
|
|
|
fn = &Function{
|
|
|
|
name: meth.Name(),
|
2013-07-11 12:12:30 -06:00
|
|
|
object: meth,
|
2013-06-14 13:50:37 -06:00
|
|
|
Signature: meth.Type().(*types.Signature),
|
2013-07-03 15:57:20 -06:00
|
|
|
Synthetic: fmt.Sprintf("interface method wrapper for %s.%s", typ, id),
|
|
|
|
pos: meth.Pos(),
|
2013-06-14 13:50:37 -06:00
|
|
|
Prog: prog,
|
|
|
|
}
|
|
|
|
fn.startBody()
|
|
|
|
fn.addParam("recv", typ, token.NoPos)
|
|
|
|
createParams(fn)
|
|
|
|
var c Call
|
|
|
|
c.Call.Method = index
|
|
|
|
c.Call.Recv = fn.Params[0]
|
|
|
|
for _, arg := range fn.Params[1:] {
|
|
|
|
c.Call.Args = append(c.Call.Args, arg)
|
|
|
|
}
|
|
|
|
emitTailCall(fn, &c)
|
|
|
|
fn.finishBody()
|
|
|
|
|
|
|
|
prog.ifaceMethodWrappers[meth] = fn
|
2013-05-22 15:56:18 -06:00
|
|
|
}
|
|
|
|
return fn
|
|
|
|
}
|
|
|
|
|
2013-06-14 13:50:37 -06:00
|
|
|
// Wrappers for bound methods -------------------------------------------------
|
2013-05-22 15:56:18 -06:00
|
|
|
|
2013-06-14 13:50:37 -06:00
|
|
|
// boundMethodWrapper returns a synthetic wrapper function that
|
|
|
|
// delegates to a concrete method. The wrapper has one free variable,
|
|
|
|
// the method's receiver. Use MakeClosure with such a wrapper to
|
2013-05-22 15:56:18 -06:00
|
|
|
// construct a bound-method closure.
|
|
|
|
// e.g.:
|
|
|
|
//
|
|
|
|
// type T int
|
|
|
|
// func (t T) meth()
|
|
|
|
// var t T
|
|
|
|
// f := t.meth
|
|
|
|
// f() // calls t.meth()
|
|
|
|
//
|
2013-06-14 13:50:37 -06:00
|
|
|
// f is a closure of a synthetic wrapper defined as if by:
|
2013-05-22 15:56:18 -06:00
|
|
|
//
|
|
|
|
// f := func() { return t.meth() }
|
|
|
|
//
|
2013-06-14 13:50:37 -06:00
|
|
|
// EXCLUSIVE_LOCKS_ACQUIRED(meth.Prog.methodsMu)
|
2013-05-22 15:56:18 -06:00
|
|
|
//
|
2013-06-14 13:50:37 -06:00
|
|
|
func boundMethodWrapper(meth *Function) *Function {
|
|
|
|
prog := meth.Prog
|
|
|
|
prog.methodsMu.Lock()
|
|
|
|
defer prog.methodsMu.Unlock()
|
|
|
|
fn, ok := prog.boundMethodWrappers[meth]
|
|
|
|
if !ok {
|
|
|
|
if prog.mode&LogSource != 0 {
|
|
|
|
defer logStack("boundMethodWrapper %s", meth)()
|
|
|
|
}
|
|
|
|
s := meth.Signature
|
|
|
|
fn = &Function{
|
2013-06-26 10:38:08 -06:00
|
|
|
name: "bound$" + meth.String(),
|
2013-06-14 13:50:37 -06:00
|
|
|
Signature: types.NewSignature(nil, s.Params(), s.Results(), s.IsVariadic()), // drop recv
|
2013-07-03 15:57:20 -06:00
|
|
|
Synthetic: "bound method wrapper for " + meth.String(),
|
2013-06-14 13:50:37 -06:00
|
|
|
Prog: prog,
|
2013-07-03 15:57:20 -06:00
|
|
|
pos: meth.Pos(),
|
2013-06-14 13:50:37 -06:00
|
|
|
}
|
2013-05-22 15:56:18 -06:00
|
|
|
|
2013-06-14 13:50:37 -06:00
|
|
|
cap := &Capture{name: "recv", typ: s.Recv().Type(), parent: fn}
|
|
|
|
fn.FreeVars = []*Capture{cap}
|
|
|
|
fn.startBody()
|
|
|
|
createParams(fn)
|
|
|
|
var c Call
|
|
|
|
c.Call.Func = meth
|
|
|
|
c.Call.Args = []Value{cap}
|
|
|
|
for _, arg := range fn.Params {
|
|
|
|
c.Call.Args = append(c.Call.Args, arg)
|
|
|
|
}
|
|
|
|
emitTailCall(fn, &c)
|
|
|
|
fn.finishBody()
|
|
|
|
|
|
|
|
prog.boundMethodWrappers[meth] = fn
|
2013-05-22 15:56:18 -06:00
|
|
|
}
|
2013-05-17 14:25:48 -06:00
|
|
|
return fn
|
|
|
|
}
|
|
|
|
|
2013-06-13 12:43:35 -06:00
|
|
|
// Receiver indirection wrapper ------------------------------------
|
|
|
|
|
|
|
|
// indirectionWrapper returns a synthetic method with *T receiver
|
|
|
|
// that delegates to meth, which has a T receiver.
|
|
|
|
//
|
|
|
|
// func (recv *T) f(...) ... {
|
|
|
|
// return (*recv).f(...)
|
|
|
|
// }
|
|
|
|
//
|
2013-06-14 13:50:37 -06:00
|
|
|
// EXCLUSIVE_LOCKS_REQUIRED(meth.Prog.methodsMu)
|
2013-06-13 12:43:35 -06:00
|
|
|
//
|
|
|
|
func indirectionWrapper(meth *Function) *Function {
|
|
|
|
prog := meth.Prog
|
|
|
|
fn, ok := prog.indirectionWrappers[meth]
|
|
|
|
if !ok {
|
|
|
|
if prog.mode&LogSource != 0 {
|
|
|
|
defer logStack("makeIndirectionWrapper %s", meth)()
|
|
|
|
}
|
|
|
|
|
|
|
|
s := meth.Signature
|
2013-07-01 13:24:50 -06:00
|
|
|
recv := types.NewVar(token.NoPos, meth.Pkg.Object, "recv",
|
2013-06-13 12:43:35 -06:00
|
|
|
types.NewPointer(s.Recv().Type()))
|
2013-07-11 12:12:30 -06:00
|
|
|
// TODO(adonovan): is there a *types.Func for this method?
|
2013-06-13 12:43:35 -06:00
|
|
|
fn = &Function{
|
|
|
|
name: meth.Name(),
|
|
|
|
Signature: types.NewSignature(recv, s.Params(), s.Results(), s.IsVariadic()),
|
|
|
|
Prog: prog,
|
2013-07-03 15:57:20 -06:00
|
|
|
Synthetic: "receiver indirection wrapper for " + meth.String(),
|
|
|
|
pos: meth.Pos(),
|
2013-06-13 12:43:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn.startBody()
|
|
|
|
fn.addParamObj(recv)
|
|
|
|
createParams(fn)
|
|
|
|
// TODO(adonovan): consider emitting a nil-pointer check here
|
|
|
|
// with a nice error message, like gc does.
|
|
|
|
var c Call
|
|
|
|
c.Call.Func = meth
|
|
|
|
c.Call.Args = append(c.Call.Args, emitLoad(fn, fn.Params[0]))
|
|
|
|
for _, arg := range fn.Params[1:] {
|
|
|
|
c.Call.Args = append(c.Call.Args, arg)
|
|
|
|
}
|
|
|
|
emitTailCall(fn, &c)
|
|
|
|
fn.finishBody()
|
|
|
|
|
|
|
|
prog.indirectionWrappers[meth] = fn
|
|
|
|
}
|
|
|
|
return fn
|
|
|
|
}
|