libgo: Merge to master revision 19184.
[official-gcc.git] / libgo / go / reflect / makefunc.go
blobb248743de70e247a43d8385ad8c17d99ad7a13af
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
27 // MakeFunc returns a new function of the given Type
28 // that wraps the function fn. When called, that new function
29 // does the following:
31 // - converts its arguments to a slice of Values.
32 // - runs results := fn(args).
33 // - returns the results as a slice of Values, one per formal result.
35 // The implementation fn can assume that the argument Value slice
36 // has the number and type of arguments given by typ.
37 // If typ describes a variadic function, the final Value is itself
38 // a slice representing the variadic arguments, as in the
39 // body of a variadic function. The result Value slice returned by fn
40 // must have the number and type of results given by typ.
42 // The Value.Call method allows the caller to invoke a typed function
43 // in terms of Values; in contrast, MakeFunc allows the caller to implement
44 // a typed function in terms of Values.
46 // The Examples section of the documentation includes an illustration
47 // of how to use MakeFunc to build a swap function for different types.
49 func MakeFunc(typ Type, fn func(args []Value) (results []Value)) Value {
50 if typ.Kind() != Func {
51 panic("reflect: call of MakeFunc with non-Func type")
54 switch runtime.GOARCH {
55 case "amd64", "386":
56 default:
57 panic("reflect.MakeFunc not implemented for " + runtime.GOARCH)
60 t := typ.common()
61 ftyp := (*funcType)(unsafe.Pointer(t))
63 // Indirect Go func value (dummy) to obtain
64 // actual code address. (A Go func value is a pointer
65 // to a C function pointer. http://golang.org/s/go11func.)
66 dummy := makeFuncStub
67 code := **(**uintptr)(unsafe.Pointer(&dummy))
69 impl := &makeFuncImpl{code: code, typ: ftyp, fn: fn, method: -1}
71 return Value{t, unsafe.Pointer(&impl), flag(Func<<flagKindShift) | flagIndir}
74 // makeFuncStub is an assembly function that is the code half of
75 // the function returned from MakeFunc. It expects a *callReflectFunc
76 // as its context register, and its job is to invoke callReflect(ctxt, frame)
77 // where ctxt is the context register and frame is a pointer to the first
78 // word in the passed-in argument frame.
79 func makeFuncStub()
81 // makeMethodValue converts v from the rcvr+method index representation
82 // of a method value to an actual method func value, which is
83 // basically the receiver value with a special bit set, into a true
84 // func value - a value holding an actual func. The output is
85 // semantically equivalent to the input as far as the user of package
86 // reflect can tell, but the true func representation can be handled
87 // by code like Convert and Interface and Assign.
88 func makeMethodValue(op string, v Value) Value {
89 if v.flag&flagMethod == 0 {
90 panic("reflect: internal error: invalid use of makeMethodValue")
93 switch runtime.GOARCH {
94 case "amd64", "386":
95 default:
96 panic("reflect.makeMethodValue not implemented for " + runtime.GOARCH)
99 // Ignoring the flagMethod bit, v describes the receiver, not the method type.
100 fl := v.flag & (flagRO | flagAddr | flagIndir)
101 fl |= flag(v.typ.Kind()) << flagKindShift
102 rcvr := Value{v.typ, v.ptr /* v.scalar, */, fl}
104 // v.Type returns the actual type of the method value.
105 ft := v.Type().(*rtype)
107 // Indirect Go func value (dummy) to obtain
108 // actual code address. (A Go func value is a pointer
109 // to a C function pointer. http://golang.org/s/go11func.)
110 dummy := makeFuncStub
111 code := **(**uintptr)(unsafe.Pointer(&dummy))
113 // Cause panic if method is not appropriate.
114 // The panic would still happen during the call if we omit this,
115 // but we want Interface() and other operations to fail early.
116 t, _ := methodReceiver(op, rcvr, int(v.flag)>>flagMethodShift)
118 fv := &makeFuncImpl{
119 code: code,
120 typ: (*funcType)(unsafe.Pointer(t)),
121 method: int(v.flag) >> flagMethodShift,
122 rcvr: rcvr,
125 return Value{ft, unsafe.Pointer(&fv), v.flag&flagRO | flag(Func)<<flagKindShift | flagIndir}
128 // makeValueMethod takes a method function and returns a function that
129 // takes a value receiver and calls the real method with a pointer to
130 // it.
131 func makeValueMethod(v Value) Value {
132 typ := v.typ
133 if typ.Kind() != Func {
134 panic("reflect: call of makeValueMethod with non-Func type")
136 if v.flag&flagMethodFn == 0 {
137 panic("reflect: call of makeValueMethod with non-MethodFn")
140 switch runtime.GOARCH {
141 case "amd64", "386":
142 default:
143 panic("reflect.makeValueMethod not implemented for " + runtime.GOARCH)
146 t := typ.common()
147 ftyp := (*funcType)(unsafe.Pointer(t))
149 // Indirect Go func value (dummy) to obtain
150 // actual code address. (A Go func value is a pointer
151 // to a C function pointer. http://golang.org/s/go11func.)
152 dummy := makeFuncStub
153 code := **(**uintptr)(unsafe.Pointer(&dummy))
155 impl := &makeFuncImpl{
156 code: code,
157 typ: ftyp,
158 method: -2,
159 rcvr: v,
162 return Value{t, unsafe.Pointer(&impl), flag(Func<<flagKindShift) | flagIndir}
165 // Call the function represented by a makeFuncImpl.
166 func (c *makeFuncImpl) call(in []Value) []Value {
167 if c.method == -1 {
168 return c.fn(in)
169 } else if c.method == -2 {
170 if c.typ.IsVariadic() {
171 return c.rcvr.CallSlice(in)
172 } else {
173 return c.rcvr.Call(in)
175 } else {
176 m := c.rcvr.Method(c.method)
177 if c.typ.IsVariadic() {
178 return m.CallSlice(in)
179 } else {
180 return m.Call(in)