// makeThunk returns a thunk, a synthetic function that delegates to a // concrete or interface method denoted by sel.Obj(). The resulting // function has no receiver, but has an additional (first) regular // parameter. // // Precondition: sel.Kind() == types.MethodExpr. // // type T int or: type T interface { meth() } // func (t T) meth() // f := T.meth // var t T // f(t) // calls t.meth() // // f is a synthetic wrapper defined as if by: // // f := func(t T) { return t.meth() } // // TODO(adonovan): opt: currently the stub is created even when used // directly in a function call: C.f(i, 0). This is less efficient // than inlining the stub. // // EXCLUSIVE_LOCKS_ACQUIRED(meth.Prog.methodsMu) // func makeThunk(prog *Program, sel *types.Selection) *Function { if sel.Kind() != types.MethodExpr { panic(sel) } key := selectionKey{ kind: sel.Kind(), recv: sel.Recv(), obj: sel.Obj(), index: fmt.Sprint(sel.Index()), indirect: sel.Indirect(), } prog.methodsMu.Lock() defer prog.methodsMu.Unlock() // Canonicalize key.recv to avoid constructing duplicate thunks. canonRecv, ok := prog.canon.At(key.recv).(types.Type) if !ok { canonRecv = key.recv prog.canon.Set(key.recv, canonRecv) } key.recv = canonRecv fn, ok := prog.thunks[key] if !ok { fn = makeWrapper(prog, sel) if fn.Signature.Recv() != nil { panic(fn) // unexpected receiver } prog.thunks[key] = fn } return fn }
// makeThunk returns a thunk, a synthetic function that delegates to a // concrete or interface method denoted by sel.Obj(). The resulting // function has no receiver, but has an additional (first) regular // parameter. // // Precondition: sel.Kind() == types.MethodExpr. // // type T int or: type T interface { meth() } // func (t T) meth() // f := T.meth // var t T // f(t) // calls t.meth() // // f is a synthetic wrapper defined as if by: // // f := func(t T) { return t.meth() } // // TODO(adonovan): opt: currently the stub is created even when used // directly in a function call: C.f(i, 0). This is less efficient // than inlining the stub. // // EXCLUSIVE_LOCKS_ACQUIRED(meth.Prog.methodsMu) // func makeThunk(prog *Program, sel *types.Selection) *Function { if sel.Kind() != types.MethodExpr { panic(sel) } // TODO(adonovan): opt: canonicalize the recv Type to avoid // construct unnecessary duplicate thunks. key := selectionKey{ kind: sel.Kind(), recv: sel.Recv(), obj: sel.Obj(), index: fmt.Sprint(sel.Index()), indirect: sel.Indirect(), } prog.methodsMu.Lock() defer prog.methodsMu.Unlock() fn, ok := prog.thunks[key] if !ok { fn = makeWrapper(prog, sel) if fn.Signature.Recv() != nil { panic(fn) // unexpected receiver } prog.thunks[key] = fn } return fn }