Fix bootstrap/PR63632
[official-gcc.git] / libgo / go / reflect / makefunc.go
blob977aacfd4382af704f64b10c82bcb003f9385905
1 // Copyright 2012 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.
5 // MakeFunc implementation.
7 package reflect
9 import (
10 "runtime"
11 "unsafe"
14 // makeFuncImpl is the closure value implementing the function
15 // returned by MakeFunc.
16 type makeFuncImpl struct {
17 code uintptr
18 typ *funcType
19 fn func([]Value) []Value
21 // For gccgo we use the same entry point for functions and for
22 // method values.
23 method int
24 rcvr Value
26 // When using FFI, hold onto the FFI closure for the garbage
27 // collector.
28 ffi *ffiData
31 // MakeFunc returns a new function of the given Type
32 // that wraps the function fn. When called, that new function
33 // does the following:
35 // - converts its arguments to a slice of Values.
36 // - runs results := fn(args).
37 // - returns the results as a slice of Values, one per formal result.
39 // The implementation fn can assume that the argument Value slice
40 // has the number and type of arguments given by typ.
41 // If typ describes a variadic function, the final Value is itself
42 // a slice representing the variadic arguments, as in the
43 // body of a variadic function. The result Value slice returned by fn
44 // must have the number and type of results given by typ.
46 // The Value.Call method allows the caller to invoke a typed function
47 // in terms of Values; in contrast, MakeFunc allows the caller to implement
48 // a typed function in terms of Values.
50 // The Examples section of the documentation includes an illustration
51 // of how to use MakeFunc to build a swap function for different types.
53 func MakeFunc(typ Type, fn func(args []Value) (results []Value)) Value {
54 if typ.Kind() != Func {
55 panic("reflect: call of MakeFunc with non-Func type")
58 t := typ.common()
59 ftyp := (*funcType)(unsafe.Pointer(t))
61 var code uintptr
62 var ffi *ffiData
63 switch runtime.GOARCH {
64 case "amd64", "386":
65 // Indirect Go func value (dummy) to obtain actual
66 // code address. (A Go func value is a pointer to a C
67 // function pointer. http://golang.org/s/go11func.)
68 dummy := makeFuncStub
69 code = **(**uintptr)(unsafe.Pointer(&dummy))
70 default:
71 code, ffi = makeFuncFFI(ftyp, fn)
74 impl := &makeFuncImpl{
75 code: code,
76 typ: ftyp,
77 fn: fn,
78 method: -1,
79 ffi: ffi,
82 return Value{t, unsafe.Pointer(&impl), flag(Func<<flagKindShift) | flagIndir}
85 // makeFuncStub is an assembly function that is the code half of
86 // the function returned from MakeFunc. It expects a *callReflectFunc
87 // as its context register, and its job is to invoke callReflect(ctxt, frame)
88 // where ctxt is the context register and frame is a pointer to the first
89 // word in the passed-in argument frame.
90 func makeFuncStub()
92 // makeMethodValue converts v from the rcvr+method index representation
93 // of a method value to an actual method func value, which is
94 // basically the receiver value with a special bit set, into a true
95 // func value - a value holding an actual func. The output is
96 // semantically equivalent to the input as far as the user of package
97 // reflect can tell, but the true func representation can be handled
98 // by code like Convert and Interface and Assign.
99 func makeMethodValue(op string, v Value) Value {
100 if v.flag&flagMethod == 0 {
101 panic("reflect: internal error: invalid use of makeMethodValue")
104 // Ignoring the flagMethod bit, v describes the receiver, not the method type.
105 fl := v.flag & (flagRO | flagAddr | flagIndir)
106 fl |= flag(v.typ.Kind()) << flagKindShift
107 rcvr := Value{v.typ, v.ptr /* v.scalar, */, fl}
109 // v.Type returns the actual type of the method value.
110 ft := v.Type().(*rtype)
112 // Cause panic if method is not appropriate.
113 // The panic would still happen during the call if we omit this,
114 // but we want Interface() and other operations to fail early.
115 _, t, _ := methodReceiver(op, rcvr, int(v.flag)>>flagMethodShift)
117 ftyp := (*funcType)(unsafe.Pointer(t))
118 method := int(v.flag) >> flagMethodShift
120 fv := &makeFuncImpl{
121 typ: ftyp,
122 method: method,
123 rcvr: rcvr,
126 switch runtime.GOARCH {
127 case "amd64", "386":
128 // Indirect Go func value (dummy) to obtain actual
129 // code address. (A Go func value is a pointer to a C
130 // function pointer. http://golang.org/s/go11func.)
131 dummy := makeFuncStub
132 fv.code = **(**uintptr)(unsafe.Pointer(&dummy))
133 default:
134 fv.code, fv.ffi = makeFuncFFI(ftyp, fv.call)
137 return Value{ft, unsafe.Pointer(&fv), v.flag&flagRO | flag(Func)<<flagKindShift | flagIndir}
140 // makeValueMethod takes a method function and returns a function that
141 // takes a value receiver and calls the real method with a pointer to
142 // it.
143 func makeValueMethod(v Value) Value {
144 typ := v.typ
145 if typ.Kind() != Func {
146 panic("reflect: call of makeValueMethod with non-Func type")
148 if v.flag&flagMethodFn == 0 {
149 panic("reflect: call of makeValueMethod with non-MethodFn")
152 t := typ.common()
153 ftyp := (*funcType)(unsafe.Pointer(t))
155 impl := &makeFuncImpl{
156 typ: ftyp,
157 method: -2,
158 rcvr: v,
161 switch runtime.GOARCH {
162 case "amd64", "386":
163 // Indirect Go func value (dummy) to obtain actual
164 // code address. (A Go func value is a pointer to a C
165 // function pointer. http://golang.org/s/go11func.)
166 dummy := makeFuncStub
167 impl.code = **(**uintptr)(unsafe.Pointer(&dummy))
168 default:
169 impl.code, impl.ffi = makeFuncFFI(ftyp, impl.call)
172 return Value{t, unsafe.Pointer(&impl), flag(Func<<flagKindShift) | flagIndir}
175 // Call the function represented by a makeFuncImpl.
176 func (c *makeFuncImpl) call(in []Value) []Value {
177 if c.method == -1 {
178 return c.fn(in)
179 } else if c.method == -2 {
180 if c.typ.IsVariadic() {
181 return c.rcvr.CallSlice(in)
182 } else {
183 return c.rcvr.Call(in)
185 } else {
186 m := c.rcvr.Method(c.method)
187 if c.typ.IsVariadic() {
188 return m.CallSlice(in)
189 } else {
190 return m.Call(in)