| 1 |
// Copyright 2013 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package ir |
| 6 |
|
| 7 |
// This file defines synthesis of Functions that delegate to declared |
| 8 |
// methods; they come in three kinds: |
| 9 |
// |
| 10 |
// (1) wrappers: methods that wrap declared methods, performing |
| 11 |
// implicit pointer indirections and embedded field selections. |
| 12 |
// |
| 13 |
// (2) thunks: funcs that wrap declared methods. Like wrappers, |
| 14 |
// thunks perform indirections and field selections. The thunk's |
| 15 |
// first parameter is used as the receiver for the method call. |
| 16 |
// |
| 17 |
// (3) bounds: funcs that wrap declared methods. The bound's sole |
| 18 |
// free variable, supplied by a closure, is used as the receiver |
| 19 |
// for the method call. No indirections or field selections are |
| 20 |
// performed since they can be done before the call. |
| 21 |
|
| 22 |
import ( |
| 23 |
"fmt" |
| 24 |
"go/types" |
| 25 |
) |
| 26 |
|
| 27 |
// -- wrappers ----------------------------------------------------------- |
| 28 |
|
| 29 |
// makeWrapper returns a synthetic method that delegates to the |
| 30 |
// declared method denoted by meth.Obj(), first performing any |
| 31 |
// necessary pointer indirections or field selections implied by meth. |
| 32 |
// |
| 33 |
// The resulting method's receiver type is meth.Recv(). |
| 34 |
// |
| 35 |
// This function is versatile but quite subtle! Consider the |
| 36 |
// following axes of variation when making changes: |
| 37 |
// - optional receiver indirection |
| 38 |
// - optional implicit field selections |
| 39 |
// - meth.Obj() may denote a concrete or an interface method |
| 40 |
// - the result may be a thunk or a wrapper. |
| 41 |
// |
| 42 |
// EXCLUSIVE_LOCKS_REQUIRED(prog.methodsMu) |
| 43 |
func makeWrapper(prog *Program, sel *types.Selection) *Function { |
| 44 |
obj := sel.Obj().(*types.Func) // the declared function |
| 45 |
sig := sel.Type().(*types.Signature) // type of this wrapper |
| 46 |
|
| 47 |
var recv *types.Var // wrapper's receiver or thunk's params[0] |
| 48 |
name := obj.Name() |
| 49 |
var description Synthetic |
| 50 |
var start int // first regular param |
| 51 |
if sel.Kind() == types.MethodExpr { |
| 52 |
name += "$thunk" |
| 53 |
description = SyntheticThunk |
| 54 |
recv = sig.Params().At(0) |
| 55 |
start = 1 |
| 56 |
} else { |
| 57 |
description = SyntheticWrapper |
| 58 |
recv = sig.Recv() |
| 59 |
} |
| 60 |
|
| 61 |
if prog.mode&LogSource != 0 { |
| 62 |
defer logStack("make %s to (%s)", description, recv.Type())() |
| 63 |
} |
| 64 |
fn := &Function{ |
| 65 |
name: name, |
| 66 |
method: sel, |
| 67 |
object: obj, |
| 68 |
Signature: sig, |
| 69 |
Synthetic: description, |
| 70 |
Prog: prog, |
| 71 |
functionBody: new(functionBody), |
| 72 |
} |
| 73 |
fn.initHTML(prog.PrintFunc) |
| 74 |
fn.startBody() |
| 75 |
fn.addSpilledParam(recv, nil) |
| 76 |
createParams(fn, start) |
| 77 |
|
| 78 |
indices := sel.Index() |
| 79 |
|
| 80 |
var v Value = fn.Locals[0] // spilled receiver |
| 81 |
if isPointer(sel.Recv()) { |
| 82 |
v = emitLoad(fn, v, nil) |
| 83 |
|
| 84 |
// For simple indirection wrappers, perform an informative nil-check: |
| 85 |
// "value method (T).f called using nil *T pointer" |
| 86 |
if len(indices) == 1 && !isPointer(recvType(obj)) { |
| 87 |
var c Call |
| 88 |
c.Call.Value = &Builtin{ |
| 89 |
name: "ir:wrapnilchk", |
| 90 |
sig: types.NewSignatureType(nil, nil, nil, |
| 91 |
types.NewTuple(anonVar(sel.Recv()), anonVar(tString), anonVar(tString)), |
| 92 |
types.NewTuple(anonVar(sel.Recv())), false), |
| 93 |
} |
| 94 |
c.Call.Args = []Value{ |
| 95 |
v, |
| 96 |
emitConst(fn, stringConst(deref(sel.Recv()).String(), nil)), |
| 97 |
emitConst(fn, stringConst(sel.Obj().Name(), nil)), |
| 98 |
} |
| 99 |
c.setType(v.Type()) |
| 100 |
v = fn.emit(&c, nil) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// Invariant: v is a pointer, either |
| 105 |
// value of *A receiver param, or |
| 106 |
// address of A spilled receiver. |
| 107 |
|
| 108 |
// We use pointer arithmetic (FieldAddr possibly followed by |
| 109 |
// Load) in preference to value extraction (Field possibly |
| 110 |
// preceded by Load). |
| 111 |
|
| 112 |
v = emitImplicitSelections(fn, v, indices[:len(indices)-1], nil) |
| 113 |
|
| 114 |
// Invariant: v is a pointer, either |
| 115 |
// value of implicit *C field, or |
| 116 |
// address of implicit C field. |
| 117 |
|
| 118 |
var c Call |
| 119 |
if r := recvType(obj); !types.IsInterface(r) { // concrete method |
| 120 |
if !isPointer(r) { |
| 121 |
v = emitLoad(fn, v, nil) |
| 122 |
} |
| 123 |
c.Call.Value = prog.declaredFunc(obj) |
| 124 |
c.Call.Args = append(c.Call.Args, v) |
| 125 |
} else { |
| 126 |
c.Call.Method = obj |
| 127 |
c.Call.Value = emitLoad(fn, v, nil) |
| 128 |
} |
| 129 |
for _, arg := range fn.Params[1:] { |
| 130 |
c.Call.Args = append(c.Call.Args, arg) |
| 131 |
} |
| 132 |
emitTailCall(fn, &c, nil) |
| 133 |
fn.finishBody() |
| 134 |
return fn |
| 135 |
} |
| 136 |
|
| 137 |
// createParams creates parameters for wrapper method fn based on its |
| 138 |
// Signature.Params, which do not include the receiver. |
| 139 |
// start is the index of the first regular parameter to use. |
| 140 |
func createParams(fn *Function, start int) { |
| 141 |
tparams := fn.Signature.Params() |
| 142 |
for i, n := start, tparams.Len(); i < n; i++ { |
| 143 |
fn.addParamVar(tparams.At(i), nil) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
// -- bounds ----------------------------------------------------------- |
| 148 |
|
| 149 |
// makeBound returns a bound method wrapper (or "bound"), a synthetic |
| 150 |
// function that delegates to a concrete or interface method denoted |
| 151 |
// by obj. The resulting function has no receiver, but has one free |
| 152 |
// variable which will be used as the method's receiver in the |
| 153 |
// tail-call. |
| 154 |
// |
| 155 |
// Use MakeClosure with such a wrapper to construct a bound method |
| 156 |
// closure. e.g.: |
| 157 |
// |
| 158 |
// type T int or: type T interface { meth() } |
| 159 |
// func (t T) meth() |
| 160 |
// var t T |
| 161 |
// f := t.meth |
| 162 |
// f() // calls t.meth() |
| 163 |
// |
| 164 |
// f is a closure of a synthetic wrapper defined as if by: |
| 165 |
// |
| 166 |
// f := func() { return t.meth() } |
| 167 |
// |
| 168 |
// Unlike makeWrapper, makeBound need perform no indirection or field |
| 169 |
// selections because that can be done before the closure is |
| 170 |
// constructed. |
| 171 |
// |
| 172 |
// EXCLUSIVE_LOCKS_ACQUIRED(meth.Prog.methodsMu) |
| 173 |
func makeBound(prog *Program, obj *types.Func) *Function { |
| 174 |
prog.methodsMu.Lock() |
| 175 |
defer prog.methodsMu.Unlock() |
| 176 |
if prog.mode&LogSource != 0 { |
| 177 |
defer logStack("%s", SyntheticBound)() |
| 178 |
} |
| 179 |
fn := &Function{ |
| 180 |
name: obj.Name() + "$bound", |
| 181 |
object: obj, |
| 182 |
Signature: changeRecv(obj.Type().(*types.Signature), nil), // drop receiver |
| 183 |
Synthetic: SyntheticBound, |
| 184 |
Prog: prog, |
| 185 |
functionBody: new(functionBody), |
| 186 |
} |
| 187 |
fn.initHTML(prog.PrintFunc) |
| 188 |
|
| 189 |
fv := &FreeVar{name: "recv", typ: recvType(obj), parent: fn} |
| 190 |
fn.FreeVars = []*FreeVar{fv} |
| 191 |
fn.startBody() |
| 192 |
createParams(fn, 0) |
| 193 |
var c Call |
| 194 |
|
| 195 |
if !types.IsInterface(recvType(obj)) { // concrete |
| 196 |
c.Call.Value = prog.declaredFunc(obj) |
| 197 |
c.Call.Args = []Value{fv} |
| 198 |
} else { |
| 199 |
c.Call.Value = fv |
| 200 |
c.Call.Method = obj |
| 201 |
} |
| 202 |
for _, arg := range fn.Params { |
| 203 |
c.Call.Args = append(c.Call.Args, arg) |
| 204 |
} |
| 205 |
emitTailCall(fn, &c, nil) |
| 206 |
fn.finishBody() |
| 207 |
return fn |
| 208 |
} |
| 209 |
|
| 210 |
// -- thunks ----------------------------------------------------------- |
| 211 |
|
| 212 |
// makeThunk returns a thunk, a synthetic function that delegates to a |
| 213 |
// concrete or interface method denoted by sel.Obj(). The resulting |
| 214 |
// function has no receiver, but has an additional (first) regular |
| 215 |
// parameter. |
| 216 |
// |
| 217 |
// Precondition: sel.Kind() == types.MethodExpr. |
| 218 |
// |
| 219 |
// type T int or: type T interface { meth() } |
| 220 |
// func (t T) meth() |
| 221 |
// f := T.meth |
| 222 |
// var t T |
| 223 |
// f(t) // calls t.meth() |
| 224 |
// |
| 225 |
// f is a synthetic wrapper defined as if by: |
| 226 |
// |
| 227 |
// f := func(t T) { return t.meth() } |
| 228 |
// |
| 229 |
// EXCLUSIVE_LOCKS_ACQUIRED(meth.Prog.methodsMu) |
| 230 |
func makeThunk(prog *Program, sel *types.Selection) *Function { |
| 231 |
if sel.Kind() != types.MethodExpr { |
| 232 |
panic(sel) |
| 233 |
} |
| 234 |
|
| 235 |
prog.methodsMu.Lock() |
| 236 |
defer prog.methodsMu.Unlock() |
| 237 |
|
| 238 |
fn := makeWrapper(prog, sel) |
| 239 |
if fn.Signature.Recv() != nil { |
| 240 |
panic(fn) // unexpected receiver |
| 241 |
} |
| 242 |
return fn |
| 243 |
} |
| 244 |
|
| 245 |
func changeRecv(s *types.Signature, recv *types.Var) *types.Signature { |
| 246 |
return types.NewSignatureType(recv, nil, nil, s.Params(), s.Results(), s.Variadic()) |
| 247 |
} |
| 248 |
|
| 249 |
// makeInstance creates a wrapper function with signature sig that calls the generic function fn. |
| 250 |
// If targs is not nil, fn is a function and targs describes the concrete type arguments. |
| 251 |
// If targs is nil, fn is a method and the type arguments are derived from the receiver. |
| 252 |
func makeInstance(prog *Program, fn *Function, sig *types.Signature, targs *types.TypeList) *Function { |
| 253 |
if sig.Recv() != nil { |
| 254 |
assert(targs == nil) |
| 255 |
// Methods don't have their own type parameters, but the receiver does |
| 256 |
targs = types.Unalias(deref(sig.Recv().Type())).(*types.Named).TypeArgs() |
| 257 |
} else { |
| 258 |
assert(targs != nil) |
| 259 |
} |
| 260 |
|
| 261 |
wrapper := fn.generics.At(targs) |
| 262 |
if wrapper != nil { |
| 263 |
return wrapper |
| 264 |
} |
| 265 |
|
| 266 |
var name string |
| 267 |
if sig.Recv() != nil { |
| 268 |
name = fn.name |
| 269 |
} else { |
| 270 |
name = fmt.Sprintf("%s$generic#%d", fn.name, fn.generics.Len()) |
| 271 |
} |
| 272 |
w := &Function{ |
| 273 |
name: name, |
| 274 |
object: fn.object, |
| 275 |
Signature: sig, |
| 276 |
Synthetic: SyntheticGeneric, |
| 277 |
Prog: prog, |
| 278 |
functionBody: new(functionBody), |
| 279 |
} |
| 280 |
w.initHTML(prog.PrintFunc) |
| 281 |
w.startBody() |
| 282 |
if sig.Recv() != nil { |
| 283 |
w.addParamVar(sig.Recv(), nil) |
| 284 |
} |
| 285 |
createParams(w, 0) |
| 286 |
var c Call |
| 287 |
c.Call.Value = fn |
| 288 |
tresults := fn.Signature.Results() |
| 289 |
if tresults.Len() == 1 { |
| 290 |
c.typ = tresults.At(0).Type() |
| 291 |
} else { |
| 292 |
c.typ = tresults |
| 293 |
} |
| 294 |
|
| 295 |
changeType := func(v Value, typ types.Type) Value { |
| 296 |
if types.Identical(v.Type(), typ) { |
| 297 |
return v |
| 298 |
} |
| 299 |
var c ChangeType |
| 300 |
c.X = v |
| 301 |
c.typ = typ |
| 302 |
return w.emit(&c, nil) |
| 303 |
} |
| 304 |
|
| 305 |
for i, arg := range w.Params { |
| 306 |
if sig.Recv() != nil { |
| 307 |
if i == 0 { |
| 308 |
c.Call.Args = append(c.Call.Args, changeType(w.Params[0], fn.Signature.Recv().Type())) |
| 309 |
} else { |
| 310 |
c.Call.Args = append(c.Call.Args, changeType(arg, fn.Signature.Params().At(i-1).Type())) |
| 311 |
} |
| 312 |
} else { |
| 313 |
c.Call.Args = append(c.Call.Args, changeType(arg, fn.Signature.Params().At(i).Type())) |
| 314 |
} |
| 315 |
} |
| 316 |
for arg := range targs.Types() { |
| 317 |
c.Call.TypeArgs = append(c.Call.TypeArgs, arg) |
| 318 |
} |
| 319 |
results := w.emit(&c, nil) |
| 320 |
var ret Return |
| 321 |
switch tresults.Len() { |
| 322 |
case 0: |
| 323 |
case 1: |
| 324 |
ret.Results = []Value{changeType(results, sig.Results().At(0).Type())} |
| 325 |
default: |
| 326 |
for i := 0; i < tresults.Len(); i++ { |
| 327 |
v := emitExtract(w, results, i, nil) |
| 328 |
ret.Results = append(ret.Results, changeType(v, sig.Results().At(i).Type())) |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
w.Exit = w.newBasicBlock("exit") |
| 333 |
emitJump(w, w.Exit, nil) |
| 334 |
w.currentBlock = w.Exit |
| 335 |
w.emit(&ret, nil) |
| 336 |
w.currentBlock = nil |
| 337 |
|
| 338 |
w.finishBody() |
| 339 |
|
| 340 |
fn.generics.Set(targs, w) |
| 341 |
return w |
| 342 |
} |