Merged revisions 196716,196830,198094,198116,198502,198877,199007,199262,199319,19946...
[official-gcc.git] / main / libgo / go / reflect / makefunc.go
blob3e0a79258e6b7698d6c741532397740127599e37
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
22 // MakeFunc returns a new function of the given Type
23 // that wraps the function fn. When called, that new function
24 // does the following:
26 // - converts its arguments to a list of Values args.
27 // - runs results := fn(args).
28 // - returns the results as a slice of Values, one per formal result.
30 // The implementation fn can assume that the argument Value slice
31 // has the number and type of arguments given by typ.
32 // If typ describes a variadic function, the final Value is itself
33 // a slice representing the variadic arguments, as in the
34 // body of a variadic function. The result Value slice returned by fn
35 // must have the number and type of results given by typ.
37 // The Value.Call method allows the caller to invoke a typed function
38 // in terms of Values; in contrast, MakeFunc allows the caller to implement
39 // a typed function in terms of Values.
41 // The Examples section of the documentation includes an illustration
42 // of how to use MakeFunc to build a swap function for different types.
44 func MakeFunc(typ Type, fn func(args []Value) (results []Value)) Value {
45 if typ.Kind() != Func {
46 panic("reflect: call of MakeFunc with non-Func type")
49 switch runtime.GOARCH {
50 case "amd64", "386":
51 default:
52 panic("reflect.MakeFunc not implemented for " + runtime.GOARCH)
55 t := typ.common()
56 ftyp := (*funcType)(unsafe.Pointer(t))
58 // Indirect Go func value (dummy) to obtain
59 // actual code address. (A Go func value is a pointer
60 // to a C function pointer. http://golang.org/s/go11func.)
61 dummy := makeFuncStub
62 code := **(**uintptr)(unsafe.Pointer(&dummy))
64 impl := &makeFuncImpl{code: code, typ: ftyp, fn: fn}
66 return Value{t, unsafe.Pointer(impl), flag(Func) << flagKindShift}
69 // makeFuncStub is an assembly function that is the code half of
70 // the function returned from MakeFunc. It expects a *callReflectFunc
71 // as its context register, and its job is to invoke callReflect(ctxt, frame)
72 // where ctxt is the context register and frame is a pointer to the first
73 // word in the passed-in argument frame.
74 func makeFuncStub()
76 // makeMethodValue converts v from the rcvr+method index representation
77 // of a method value to an actual method func value, which is
78 // basically the receiver value with a special bit set, into a true
79 // func value - a value holding an actual func. The output is
80 // semantically equivalent to the input as far as the user of package
81 // reflect can tell, but the true func representation can be handled
82 // by code like Convert and Interface and Assign.
83 func makeMethodValue(op string, v Value) Value {
84 if v.flag&flagMethod == 0 {
85 panic("reflect: internal error: invalid use of makePartialFunc")
88 // Ignoring the flagMethod bit, v describes the receiver, not the method type.
89 fl := v.flag & (flagRO | flagAddr | flagIndir)
90 fl |= flag(v.typ.Kind()) << flagKindShift
91 rcvr := Value{v.typ, v.val, fl}
93 // Cause panic if method is not appropriate.
94 // The panic would still happen during the call if we omit this,
95 // but we want Interface() and other operations to fail early.
96 methodReceiver(op, rcvr, int(v.flag)>>flagMethodShift)
98 panic("reflect makeMethodValue not implemented")