Merge from branches/gcc-4_8-branch up to rev 201477
[official-gcc.git] / gcc-4_8-branch / libgo / go / reflect / makefunc.go
blobf03beb344837925c145683028b80ea1bd44f0795
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 "unsafe"
13 // makeFuncImpl is the closure value implementing the function
14 // returned by MakeFunc.
15 type makeFuncImpl struct {
16 code uintptr
17 typ *funcType
18 fn func([]Value) []Value
21 // MakeFunc returns a new function of the given Type
22 // that wraps the function fn. When called, that new function
23 // does the following:
25 // - converts its arguments to a list of Values args.
26 // - runs results := fn(args).
27 // - returns the results as a slice of Values, one per formal result.
29 // The implementation fn can assume that the argument Value slice
30 // has the number and type of arguments given by typ.
31 // If typ describes a variadic function, the final Value is itself
32 // a slice representing the variadic arguments, as in the
33 // body of a variadic function. The result Value slice returned by fn
34 // must have the number and type of results given by typ.
36 // The Value.Call method allows the caller to invoke a typed function
37 // in terms of Values; in contrast, MakeFunc allows the caller to implement
38 // a typed function in terms of Values.
40 // The Examples section of the documentation includes an illustration
41 // of how to use MakeFunc to build a swap function for different types.
43 func MakeFunc(typ Type, fn func(args []Value) (results []Value)) Value {
44 if typ.Kind() != Func {
45 panic("reflect: call of MakeFunc with non-Func type")
48 t := typ.common()
49 ftyp := (*funcType)(unsafe.Pointer(t))
51 _, _ = t, ftyp
53 panic("reflect MakeFunc not implemented")
56 // makeMethodValue converts v from the rcvr+method index representation
57 // of a method value to an actual method func value, which is
58 // basically the receiver value with a special bit set, into a true
59 // func value - a value holding an actual func. The output is
60 // semantically equivalent to the input as far as the user of package
61 // reflect can tell, but the true func representation can be handled
62 // by code like Convert and Interface and Assign.
63 func makeMethodValue(op string, v Value) Value {
64 if v.flag&flagMethod == 0 {
65 panic("reflect: internal error: invalid use of makePartialFunc")
68 // Ignoring the flagMethod bit, v describes the receiver, not the method type.
69 fl := v.flag & (flagRO | flagAddr | flagIndir)
70 fl |= flag(v.typ.Kind()) << flagKindShift
71 rcvr := Value{v.typ, v.val, fl}
73 // Cause panic if method is not appropriate.
74 // The panic would still happen during the call if we omit this,
75 // but we want Interface() and other operations to fail early.
76 methodReceiver(op, rcvr, int(v.flag)>>flagMethodShift)
78 panic("reflect makeMethodValue not implemented")