Skip to content

Commit ee7dde4

Browse files
feat: update funcStruct to use variadic, impl String
Co-authored-by: Ethan Lewis <[email protected]>
1 parent 6a866d4 commit ee7dde4

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

compiler/interface.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ func (c *compilerContext) getTypeCode(typ types.Type) llvm.Value {
248248
types.NewVar(token.NoPos, nil, "ptrTo", types.Typ[types.UnsafePointer]),
249249
types.NewVar(token.NoPos, nil, "inCount", types.Typ[types.Uint16]),
250250
types.NewVar(token.NoPos, nil, "outCount", types.Typ[types.Uint16]),
251+
types.NewVar(token.NoPos, nil, "variadic", types.Typ[types.Bool]),
251252
types.NewVar(token.NoPos, nil, "fields", types.NewArray(types.Typ[types.UnsafePointer], int64(typ.Params().Len()+typ.Results().Len()))),
252253
)
253254
}
@@ -409,13 +410,9 @@ func (c *compilerContext) getTypeCode(typ types.Type) llvm.Value {
409410
typeFields = []llvm.Value{c.getTypeCode(types.NewPointer(typ))}
410411
// TODO: methods
411412
case *types.Signature:
412-
v := typ.Results().Len()
413+
v := 0
413414
if typ.Variadic() {
414-
// set variadic to 1
415-
v = v | (1 << 15)
416-
} else {
417-
// set variadic to 0
418-
v = v & ^(1 << 15)
415+
v = 1
419416
}
420417

421418
var vars []*types.Var
@@ -433,9 +430,9 @@ func (c *compilerContext) getTypeCode(typ types.Type) llvm.Value {
433430
}
434431
typeFields = []llvm.Value{c.getTypeCode(types.NewPointer(typ)),
435432
llvm.ConstInt(c.ctx.Int16Type(), uint64(typ.Params().Len()), false),
436-
llvm.ConstInt(c.ctx.Int16Type(), uint64(v), false),
433+
llvm.ConstInt(c.ctx.Int16Type(), uint64(typ.Results().Len()), false),
434+
llvm.ConstInt(c.ctx.Int1Type(), uint64(v), false),
437435
}
438-
439436
typeFields = append(typeFields, llvm.ConstArray(c.dataPtrType, fields))
440437
}
441438
// Prepend metadata byte.

src/reflect/type.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,10 @@ type structField struct {
489489

490490
type funcType struct {
491491
rawType
492-
ptrType *rawType
492+
ptrTo *rawType
493493
inCount uint16
494494
outCount uint16
495+
variadic bool
495496
fields [1]*rawType // the remaining fields are all of type funcField
496497
}
497498

@@ -606,6 +607,28 @@ func (t *rawType) String() string {
606607
}
607608
s += " }"
608609
return s
610+
case Func:
611+
612+
f := "func("
613+
for i := 0; i < t.NumIn(); i++ {
614+
if i > 0 {
615+
f += ", "
616+
}
617+
f += t.In(i).String()
618+
}
619+
f += ") "
620+
621+
var rets string
622+
for i := 0; i < t.NumOut(); i++ {
623+
if i > 0 {
624+
rets += ", "
625+
}
626+
rets += t.Out(i).String()
627+
}
628+
if t.NumOut() > 1 {
629+
rets = "(" + rets + ")"
630+
}
631+
return f + rets
609632
case Interface:
610633
// TODO(dgryski): Needs actual method set info
611634
return "interface {}"
@@ -1050,7 +1073,7 @@ func (t *rawType) IsVariadic() bool {
10501073
if t.Kind() != Func {
10511074
panic("reflect: IsVariadic of non-func type")
10521075
}
1053-
return (*funcType)(unsafe.Pointer(t)).outCount&(1<<15) != 0
1076+
return (*funcType)(unsafe.Pointer(t)).variadic
10541077
}
10551078

10561079
func (t *rawType) NumIn() int {
@@ -1156,12 +1179,15 @@ func (t *rawType) Out(i int) Type {
11561179
if t.Kind() != Func {
11571180
panic(errTypeField)
11581181
}
1159-
i = i + int((*funcType)(unsafe.Pointer(t)).inCount)
1182+
11601183
descriptor := (*funcType)(unsafe.Pointer(t.underlying()))
1161-
if uint(i) > uint(descriptor.inCount) && uint(i) <= uint(descriptor.outCount+descriptor.inCount) {
1184+
if uint(i) >= uint(descriptor.outCount) {
11621185
panic("reflect: field index out of range")
11631186
}
11641187

1188+
// Shift the index by the number of input parameters.
1189+
i = i + int((*funcType)(unsafe.Pointer(t)).inCount)
1190+
11651191
pointer := (unsafe.Add(unsafe.Pointer(&descriptor.fields[0]), uintptr(i)*unsafe.Sizeof(unsafe.Pointer(nil))))
11661192
return (*rawType)(*(**rawType)(pointer))
11671193
}

0 commit comments

Comments
 (0)