2013-05-17 14:25:48 -06:00
|
|
|
package ssa
|
|
|
|
|
2013-07-19 15:35:29 -06:00
|
|
|
// This file defines utilities for method-set computation including
|
|
|
|
// synthesis of wrapper methods.
|
2013-06-14 13:50:37 -06:00
|
|
|
//
|
|
|
|
// 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-19 15:35:29 -06:00
|
|
|
// TODO(adonovan): rename to wrappers.go.
|
2013-05-17 14:25:48 -06:00
|
|
|
|
|
|
|
import (
|
2013-05-30 07:59:17 -06:00
|
|
|
"fmt"
|
|
|
|
"go/token"
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-07-19 15:35:29 -06:00
|
|
|
"code.google.com/p/go.tools/go/types"
|
|
|
|
)
|
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-07-19 15:35:29 -06:00
|
|
|
func (prog *Program) MethodSet(typ types.Type) MethodSet {
|
2013-07-23 11:38:52 -06:00
|
|
|
return prog.populateMethodSet(typ, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// populateMethodSet returns the method set for typ, ensuring that it
|
|
|
|
// contains at least key id. If id is empty, the entire method set is
|
|
|
|
// populated.
|
|
|
|
//
|
|
|
|
func (prog *Program) populateMethodSet(typ types.Type, id string) MethodSet {
|
|
|
|
tmset := typ.MethodSet()
|
|
|
|
n := tmset.Len()
|
|
|
|
if n == 0 {
|
2013-05-17 14:25:48 -06:00
|
|
|
return nil
|
|
|
|
}
|
2013-07-19 15:35:29 -06:00
|
|
|
if _, ok := deref(typ).Underlying().(*types.Interface); ok {
|
|
|
|
// TODO(gri): fix: go/types bug: pointer-to-interface
|
|
|
|
// has no methods---yet go/types says it has!
|
|
|
|
return nil
|
|
|
|
}
|
2013-05-17 14:25:48 -06:00
|
|
|
|
|
|
|
if prog.mode&LogSource != 0 {
|
2013-07-23 11:38:52 -06:00
|
|
|
defer logStack("MethodSet %s id=%s", typ, id)()
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-07-19 15:35:29 -06:00
|
|
|
prog.methodsMu.Lock()
|
|
|
|
defer prog.methodsMu.Unlock()
|
2013-05-17 14:25:48 -06:00
|
|
|
|
2013-07-23 11:38:52 -06:00
|
|
|
mset, _ := prog.methodSets.At(typ).(MethodSet)
|
|
|
|
if mset == nil {
|
|
|
|
mset = make(MethodSet)
|
|
|
|
prog.methodSets.Set(typ, mset)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-07-23 11:38:52 -06:00
|
|
|
if len(mset) < n {
|
|
|
|
if id != "" { // single method
|
|
|
|
// tmset.Lookup() is no use to us with only an Id string.
|
|
|
|
if mset[id] == nil {
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
obj := tmset.At(i)
|
|
|
|
if obj.Id() == id {
|
|
|
|
mset[id] = makeMethod(prog, typ, obj)
|
|
|
|
return mset
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// complete set
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
obj := tmset.At(i)
|
|
|
|
if id := obj.Id(); mset[id] == nil {
|
|
|
|
mset[id] = makeMethod(prog, typ, obj)
|
|
|
|
}
|
|
|
|
}
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
2013-07-23 11:38:52 -06:00
|
|
|
|
2013-05-17 14:25:48 -06:00
|
|
|
return mset
|
|
|
|
}
|
|
|
|
|
2013-07-23 11:38:52 -06:00
|
|
|
// LookupMethod returns the method id of type typ, building wrapper
|
|
|
|
// methods on demand. It returns nil if the typ has no such method.
|
|
|
|
//
|
|
|
|
// Thread-safe.
|
|
|
|
//
|
|
|
|
func (prog *Program) LookupMethod(typ types.Type, id string) *Function {
|
|
|
|
return prog.populateMethodSet(typ, id)[id]
|
|
|
|
}
|
|
|
|
|
2013-07-19 15:35:29 -06:00
|
|
|
// makeMethod returns the concrete Function for the method obj,
|
|
|
|
// adapted if necessary so that its receiver type is typ.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-07-19 15:35:29 -06:00
|
|
|
// EXCLUSIVE_LOCKS_REQUIRED(prog.methodsMu)
|
|
|
|
//
|
|
|
|
func makeMethod(prog *Program, typ types.Type, obj *types.Method) *Function {
|
|
|
|
// Promoted method accessed via implicit field selections?
|
|
|
|
if len(obj.Index()) > 1 {
|
|
|
|
return promotionWrapper(prog, typ, obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
method := prog.concreteMethods[obj.Func]
|
|
|
|
if method == nil {
|
|
|
|
panic("no concrete method for " + obj.Func.String())
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
2013-07-19 15:35:29 -06:00
|
|
|
|
|
|
|
// Call to method on T from receiver of type *T?
|
|
|
|
if !isPointer(method.Signature.Recv().Type()) && isPointer(typ) {
|
|
|
|
method = indirectionWrapper(method)
|
|
|
|
}
|
|
|
|
|
|
|
|
return method
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
|
2013-07-19 15:35:29 -06:00
|
|
|
// promotionWrapper returns a synthetic wrapper Function that performs
|
|
|
|
// a sequence of implicit field selections then tailcalls a "promoted"
|
|
|
|
// method. For example, given these decls:
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
|
|
|
// type A struct {B}
|
|
|
|
// type B struct {*C}
|
|
|
|
// type C ...
|
|
|
|
// func (*C) f()
|
|
|
|
//
|
2013-07-19 15:35:29 -06:00
|
|
|
// then promotionWrapper(typ=A, obj={Func:(*C).f, Indices=[B,C,f]})
|
2013-06-14 13:50:37 -06:00
|
|
|
// 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-07-19 15:35:29 -06:00
|
|
|
// typ is the receiver type of the wrapper method. obj is the
|
|
|
|
// type-checker's object for the promoted method; its Func may be a
|
|
|
|
// concrete or an interface method.
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-07-19 15:35:29 -06:00
|
|
|
// EXCLUSIVE_LOCKS_REQUIRED(prog.methodsMu)
|
2013-07-10 16:08:42 -06:00
|
|
|
//
|
2013-07-19 15:35:29 -06:00
|
|
|
func promotionWrapper(prog *Program, typ types.Type, obj *types.Method) *Function {
|
|
|
|
old := obj.Func.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-07-19 15:35:29 -06:00
|
|
|
// TODO(adonovan): include implicit field path in description.
|
2013-07-19 17:38:16 -06:00
|
|
|
description := fmt.Sprintf("promotion wrapper for (%s).%s", old.Recv(), obj.Func.Name())
|
2013-07-19 15:35:29 -06:00
|
|
|
|
2013-05-17 14:25:48 -06:00
|
|
|
if prog.mode&LogSource != 0 {
|
2013-07-19 15:35:29 -06:00
|
|
|
defer logStack("make %s to (%s)", description, typ)()
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
|
|
|
fn := &Function{
|
2013-07-19 15:35:29 -06:00
|
|
|
name: obj.Name(),
|
|
|
|
object: obj,
|
2013-05-17 15:02:47 -06:00
|
|
|
Signature: sig,
|
2013-07-19 15:35:29 -06:00
|
|
|
Synthetic: description,
|
2013-05-17 14:25:48 -06:00
|
|
|
Prog: prog,
|
2013-07-19 15:35:29 -06:00
|
|
|
pos: obj.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)
|
|
|
|
|
|
|
|
var v Value = fn.Locals[0] // spilled receiver
|
|
|
|
if isPointer(typ) {
|
|
|
|
v = emitLoad(fn, v)
|
|
|
|
}
|
2013-07-19 15:35:29 -06:00
|
|
|
|
|
|
|
// Invariant: v is a pointer, either
|
|
|
|
// value of *A receiver param, or
|
|
|
|
// address of A spilled receiver.
|
|
|
|
|
|
|
|
// We use pointer arithmetic (FieldAddr possibly followed by
|
|
|
|
// Load) in preference to value extraction (Field possibly
|
|
|
|
// preceded by Load).
|
|
|
|
|
|
|
|
indices := obj.Index()
|
|
|
|
v = emitImplicitSelections(fn, v, indices[:len(indices)-1])
|
|
|
|
|
|
|
|
// Invariant: v is a pointer, either
|
|
|
|
// value of implicit *C field, or
|
|
|
|
// address of implicit C field.
|
2013-05-17 14:25:48 -06:00
|
|
|
|
|
|
|
var c Call
|
2013-07-19 17:38:16 -06:00
|
|
|
if _, ok := old.Recv().Type().Underlying().(*types.Interface); !ok { // concrete method
|
2013-07-19 15:35:29 -06:00
|
|
|
if !isPointer(old.Recv().Type()) {
|
|
|
|
v = emitLoad(fn, v)
|
|
|
|
}
|
|
|
|
m := prog.concreteMethods[obj.Func]
|
|
|
|
if m == nil {
|
|
|
|
panic("oops: " + fn.Synthetic)
|
|
|
|
}
|
|
|
|
c.Call.Func = m
|
2013-05-17 14:25:48 -06:00
|
|
|
c.Call.Args = append(c.Call.Args, v)
|
|
|
|
} else {
|
2013-07-19 15:35:29 -06:00
|
|
|
c.Call.Method = indices[len(indices)-1]
|
|
|
|
c.Call.Recv = emitLoad(fn, v)
|
2013-05-17 14:25:48 -06:00
|
|
|
}
|
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-07-19 15:35:29 -06:00
|
|
|
// EXCLUSIVE_LOCKS_ACQUIRED(prog.methodsMu)
|
2013-05-17 14:25:48 -06:00
|
|
|
//
|
2013-07-19 17:38:16 -06:00
|
|
|
func interfaceMethodWrapper(prog *Program, typ types.Type, id string) *Function {
|
2013-06-14 13:50:37 -06:00
|
|
|
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
|
|
|
|
}
|