Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reflect: Add Function Reflect Support #4578

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions compiler/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,14 @@ func (c *compilerContext) getTypeCode(typ types.Type) llvm.Value {
typeFieldTypes = append(typeFieldTypes,
types.NewVar(token.NoPos, nil, "ptrTo", types.Typ[types.UnsafePointer]),
)
// TODO: methods
case *types.Signature:
typeFieldTypes = append(typeFieldTypes,
types.NewVar(token.NoPos, nil, "ptrTo", types.Typ[types.UnsafePointer]),
types.NewVar(token.NoPos, nil, "inCount", types.Typ[types.Uint16]),
types.NewVar(token.NoPos, nil, "outCount", types.Typ[types.Uint16]),
types.NewVar(token.NoPos, nil, "variadic", types.Typ[types.Bool]),
types.NewVar(token.NoPos, nil, "fields", types.NewArray(types.Typ[types.UnsafePointer], int64(typ.Params().Len()+typ.Results().Len()))),
)
// TODO: signature params and return values
}
if hasMethodSet {
// This method set is appended at the start of the struct. It is
Expand Down Expand Up @@ -408,8 +410,30 @@ func (c *compilerContext) getTypeCode(typ types.Type) llvm.Value {
typeFields = []llvm.Value{c.getTypeCode(types.NewPointer(typ))}
// TODO: methods
case *types.Signature:
typeFields = []llvm.Value{c.getTypeCode(types.NewPointer(typ))}
// TODO: params, return values, etc
v := 0
if typ.Variadic() {
v = 1
}

var vars []*types.Var
for i := 0; i < typ.Params().Len(); i++ {
vars = append(vars, typ.Params().At(i))
}

for i := 0; i < typ.Results().Len(); i++ {
vars = append(vars, typ.Results().At(i))
}

var fields []llvm.Value
for i := 0; i < len(vars); i++ {
fields = append(fields, c.getTypeCode(vars[i].Type()))
}
typeFields = []llvm.Value{c.getTypeCode(types.NewPointer(typ)),
llvm.ConstInt(c.ctx.Int16Type(), uint64(typ.Params().Len()), false),
llvm.ConstInt(c.ctx.Int16Type(), uint64(typ.Results().Len()), false),
llvm.ConstInt(c.ctx.Int1Type(), uint64(v), false),
}
typeFields = append(typeFields, llvm.ConstArray(c.dataPtrType, fields))
}
// Prepend metadata byte.
typeFields = append([]llvm.Value{
Expand Down
4 changes: 4 additions & 0 deletions src/reflect/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3312,6 +3312,8 @@ func TestMethodPkgPath(t *testing.T) {
}
}

*/

func TestVariadicType(t *testing.T) {
// Test example from Type documentation.
var f func(x int, y ...float64)
Expand All @@ -3335,6 +3337,8 @@ func TestVariadicType(t *testing.T) {
t.Error(s)
}

/*

type inner struct {
x int
}
Expand Down
121 changes: 114 additions & 7 deletions src/reflect/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,15 @@ type structField struct {
data unsafe.Pointer // various bits of information, packed in a byte array
}

type funcType struct {
rawType
ptrTo *rawType
inCount uint16
outCount uint16
variadic bool
fields [1]*rawType // the remaining fields are all of type funcField
}

// Equivalent to (go/types.Type).Underlying(): if this is a named type return
// the underlying type, else just return the type itself.
func (t *rawType) underlying() *rawType {
Expand Down Expand Up @@ -598,6 +607,36 @@ func (t *rawType) String() string {
}
s += " }"
return s
case Func:
isVariadic := t.IsVariadic()

f := "func("
for i := 0; i < t.NumIn(); i++ {
if i > 0 {
f += ", "
}

input := t.In(i).String()
if isVariadic && i == t.NumIn()-1 {
f += "..."
input = input[2:]
}
f += input
}

f += ") "

var rets string
for i := 0; i < t.NumOut(); i++ {
if i > 0 {
rets += ", "
}
rets += t.Out(i).String()
}
if t.NumOut() > 1 {
rets = "(" + rets + ")"
}
return f + rets
case Interface:
// TODO(dgryski): Needs actual method set info
return "interface {}"
Expand Down Expand Up @@ -1038,15 +1077,40 @@ func (t *rawType) ConvertibleTo(u Type) bool {
}

func (t *rawType) IsVariadic() bool {
panic("unimplemented: (reflect.Type).IsVariadic()")
if t.isNamed() {
named := (*namedType)(unsafe.Pointer(t))
t = named.elem
}

if t.Kind() != Func {
panic("reflect: IsVariadic of non-func type")
}

return (*funcType)(unsafe.Pointer(t)).variadic
}

func (t *rawType) NumIn() int {
panic("unimplemented: (reflect.Type).NumIn()")
if t.isNamed() {
named := (*namedType)(unsafe.Pointer(t))
return int((*funcType)(unsafe.Pointer(named.elem)).inCount)
}

if t.Kind() != Func {
panic("reflect: NumIn of non-func type")
}
return int((*funcType)(unsafe.Pointer(t)).inCount)
}

func (t *rawType) NumOut() int {
panic("unimplemented: (reflect.Type).NumOut()")
if t.isNamed() {
named := (*namedType)(unsafe.Pointer(t))
return int((*funcType)(unsafe.Pointer(named.elem)).outCount)
}

if t.Kind() != Func {
panic("reflect: NumOut of non-func type")
}
return int((*funcType)(unsafe.Pointer(t)).outCount)
}

func (t *rawType) NumMethod() int {
Expand Down Expand Up @@ -1110,12 +1174,55 @@ func (t *rawType) Key() Type {
return t.key()
}

func (t rawType) In(i int) Type {
panic("unimplemented: (reflect.Type).In()")
// addChecked returns p+x.
//
// The whySafe string is ignored, so that the function still inlines
// as efficiently as p+x, but all call sites should use the string to
// record why the addition is safe, which is to say why the addition
// does not cause x to advance to the very end of p's allocation
// and therefore point incorrectly at the next block in memory.
func addChecked(p unsafe.Pointer, x uintptr, whySafe string) unsafe.Pointer {
return unsafe.Pointer(uintptr(p) + x)
}

func (t *rawType) In(i int) Type {
if t.isNamed() {
named := (*namedType)(unsafe.Pointer(t))
t = named.elem
}

if t.Kind() != Func {
panic(errTypeField)
}
fType := (*funcType)(unsafe.Pointer(t))
if uint(i) >= uint(fType.inCount) {
panic("reflect: field index out of range")
}

pointer := (unsafe.Add(unsafe.Pointer(&fType.fields[0]), uintptr(i)*unsafe.Sizeof(unsafe.Pointer(nil))))
return (*(**rawType)(pointer))
}

func (t rawType) Out(i int) Type {
panic("unimplemented: (reflect.Type).Out()")
func (t *rawType) Out(i int) Type {
if t.isNamed() {
named := (*namedType)(unsafe.Pointer(t))
t = named.elem
}

if t.Kind() != Func {
panic(errTypeField)
}

fType := (*funcType)(unsafe.Pointer(t))

if uint(i) >= uint(fType.outCount) {
panic("reflect: field index out of range")
}

// Shift the index by the number of input parameters.
i = i + int(fType.inCount)
pointer := (unsafe.Add(unsafe.Pointer(&fType.fields[0]), uintptr(i)*unsafe.Sizeof(unsafe.Pointer(nil))))
return (*(**rawType)(pointer))
}

// OverflowComplex reports whether the complex128 x cannot be represented by type t.
Expand Down
2 changes: 2 additions & 0 deletions src/reflect/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func TestTypeFor(t *testing.T) {
type (
mystring string
myiface interface{}
myfunc func()
)

testcases := []struct {
Expand All @@ -25,6 +26,7 @@ func TestTypeFor(t *testing.T) {
{new(mystring), reflect.TypeFor[mystring]()},
{new(any), reflect.TypeFor[any]()},
{new(myiface), reflect.TypeFor[myiface]()},
{new(myfunc), reflect.TypeFor[myfunc]()},
}
for _, tc := range testcases {
want := reflect.ValueOf(tc.wantFrom).Elem().Type()
Expand Down
53 changes: 53 additions & 0 deletions src/reflect/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,59 @@ func TestTinyStruct(t *testing.T) {
}
}

func TestTinyFunc(t *testing.T) {
type barStruct struct {
QuxString string
BazInt int
}

type foobar func(bar barStruct, x int, v ...string) string

var fb foobar

reffb := TypeOf(fb)

numIn := reffb.NumIn()
if want := 3; numIn != want {
t.Errorf("NumIn=%v, want %v", numIn, want)
}

numOut := reffb.NumOut()
if want := 1; numOut != want {
t.Errorf("NumOut=%v, want %v", numOut, want)
}

in0 := reffb.In(0)
if want := TypeOf(barStruct{}); in0 != want {
t.Errorf("In(0)=%v, want %v", in0, want)
}

in1 := reffb.In(1)
if want := TypeOf(0); in1 != want {
t.Errorf("In(1)=%v, want %v", in1, want)
}

in2 := reffb.In(2)
if want := TypeOf([]string{}); in2 != want {
t.Errorf("In(2)=%v, want %v", in2, want)
}

out0 := reffb.Out(0)
if want := TypeOf(""); out0 != want {
t.Errorf("Out(0)=%v, want %v", out0, want)
}

isVariadic := reffb.IsVariadic()
if want := true; isVariadic != want {
t.Errorf("IsVariadic=%v, want %v", isVariadic, want)
}

if got, want := reffb.String(), "reflect_test.foobar"; got != want {
t.Errorf("Value.String()=%v, want %v", got, want)
}

}

func TestTinyZero(t *testing.T) {
s := "hello, world"
sptr := &s
Expand Down