Rebase.
[official-gcc.git] / libgo / go / reflect / makefunc.go
blob736ac36ade74d50c7caf9bae6d58886bf51d337b
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 var code uintptr
121 var ffi *ffiData
122 switch runtime.GOARCH {
123 case "amd64", "386":
124 // Indirect Go func value (dummy) to obtain actual
125 // code address. (A Go func value is a pointer to a C
126 // function pointer. http://golang.org/s/go11func.)
127 dummy := makeFuncStub
128 code = **(**uintptr)(unsafe.Pointer(&dummy))
129 default:
130 code, ffi = makeFuncFFI(ftyp,
131 func(in []Value) []Value {
132 m := rcvr.Method(method)
133 return m.Call(in)
137 fv := &makeFuncImpl{
138 code: code,
139 typ: ftyp,
140 method: method,
141 rcvr: rcvr,
142 ffi: ffi,
145 return Value{ft, unsafe.Pointer(&fv), v.flag&flagRO | flag(Func)<<flagKindShift | flagIndir}
148 // makeValueMethod takes a method function and returns a function that
149 // takes a value receiver and calls the real method with a pointer to
150 // it.
151 func makeValueMethod(v Value) Value {
152 typ := v.typ
153 if typ.Kind() != Func {
154 panic("reflect: call of makeValueMethod with non-Func type")
156 if v.flag&flagMethodFn == 0 {
157 panic("reflect: call of makeValueMethod with non-MethodFn")
160 t := typ.common()
161 ftyp := (*funcType)(unsafe.Pointer(t))
163 var code uintptr
164 var ffi *ffiData
165 switch runtime.GOARCH {
166 case "amd64", "386":
167 // Indirect Go func value (dummy) to obtain actual
168 // code address. (A Go func value is a pointer to a C
169 // function pointer. http://golang.org/s/go11func.)
170 dummy := makeFuncStub
171 code = **(**uintptr)(unsafe.Pointer(&dummy))
172 default:
173 code, ffi = makeFuncFFI(ftyp,
174 func(in []Value) []Value {
175 return v.Call(in)
179 impl := &makeFuncImpl{
180 code: code,
181 typ: ftyp,
182 method: -2,
183 rcvr: v,
184 ffi: ffi,
187 return Value{t, unsafe.Pointer(&impl), flag(Func<<flagKindShift) | flagIndir}
190 // Call the function represented by a makeFuncImpl.
191 func (c *makeFuncImpl) call(in []Value) []Value {
192 if c.method == -1 {
193 return c.fn(in)
194 } else if c.method == -2 {
195 return c.rcvr.Call(in)
196 } else {
197 m := c.rcvr.Method(c.method)
198 return m.Call(in)