patch: reflect (type)

This commit is contained in:
xushiwei
2024-06-20 14:17:37 +08:00
parent f7d7f81c49
commit f8b0a7105b
9 changed files with 1204 additions and 26 deletions

8
_demo/rtype/rtype.go Normal file
View File

@@ -0,0 +1,8 @@
package main
import "reflect"
func main() {
tyIntSlice := reflect.SliceOf(reflect.TypeOf(0))
println(tyIntSlice.String())
}

View File

@@ -519,10 +519,9 @@ func ignoreName(name string) bool {
}
*/
return (strings.HasPrefix(name, "internal/") && !supportedInternal(name)) ||
strings.HasPrefix(name, "crypto/") || strings.HasPrefix(name, "reflect.") ||
strings.HasPrefix(name, "crypto/") || strings.HasPrefix(name, "runtime/") ||
strings.HasPrefix(name, "arena.") || strings.HasPrefix(name, "maps.") ||
strings.HasPrefix(name, "time.") || strings.HasPrefix(name, "runtime/") ||
strings.HasPrefix(name, "plugin.")
strings.HasPrefix(name, "time.") || strings.HasPrefix(name, "plugin.")
}
func supportedInternal(name string) bool {

View File

@@ -337,8 +337,8 @@ func (t *Type) Align() int { return int(t.Align_) }
func (t *Type) FieldAlign() int { return int(t.FieldAlign_) }
// Name returns the name of type t.
func (t *Type) Name() string {
// String returns string form of type t.
func (t *Type) String() string {
if t.TFlag&TFlagExtraStar != 0 {
return "*" + t.Str_
}

View File

@@ -727,6 +727,7 @@ var hasAltPkg = map[string]none{
"io/fs": {},
"math": {},
"math/cmplx": {},
"reflect": {},
"sync": {},
"sync/atomic": {},
"syscall": {},

View File

@@ -0,0 +1,22 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reflect
// llgo:skipall
import (
_ "unsafe"
)

1011
internal/lib/reflect/type.go Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,120 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reflect
import (
"unsafe"
"github.com/goplus/llgo/internal/abi"
)
// Value is the reflection interface to a Go value.
//
// Not all methods apply to all kinds of values. Restrictions,
// if any, are noted in the documentation for each method.
// Use the Kind method to find out the kind of value before
// calling kind-specific methods. Calling a method
// inappropriate to the kind of type causes a run time panic.
//
// The zero Value represents no value.
// Its IsValid method returns false, its Kind method returns Invalid,
// its String method returns "<invalid Value>", and all other methods panic.
// Most functions and methods never return an invalid value.
// If one does, its documentation states the conditions explicitly.
//
// A Value can be used concurrently by multiple goroutines provided that
// the underlying Go value can be used concurrently for the equivalent
// direct operations.
//
// To compare two Values, compare the results of the Interface method.
// Using == on two Values does not compare the underlying values
// they represent.
type Value struct {
// typ_ holds the type of the value represented by a Value.
// Access using the typ method to avoid escape of v.
typ_ *abi.Type
// Pointer-valued data or, if flagIndir is set, pointer to data.
// Valid when either flagIndir is set or typ.pointers() is true.
ptr unsafe.Pointer
// flag holds metadata about the value.
//
// The lowest five bits give the Kind of the value, mirroring typ.Kind().
//
// The next set of bits are flag bits:
// - flagStickyRO: obtained via unexported not embedded field, so read-only
// - flagEmbedRO: obtained via unexported embedded field, so read-only
// - flagIndir: val holds a pointer to the data
// - flagAddr: v.CanAddr is true (implies flagIndir and ptr is non-nil)
// - flagMethod: v is a method value.
// If ifaceIndir(typ), code can assume that flagIndir is set.
//
// The remaining 22+ bits give a method number for method values.
// If flag.kind() != Func, code can assume that flagMethod is unset.
flag
// A method value represents a curried method invocation
// like r.Read for some receiver r. The typ+val+flag bits describe
// the receiver r, but the flag's Kind bits say Func (methods are
// functions), and the top bits of the flag give the method number
// in r's type's method table.
}
type flag uintptr
const (
flagKindWidth = 5 // there are 27 kinds
flagKindMask flag = 1<<flagKindWidth - 1
flagStickyRO flag = 1 << 5
flagEmbedRO flag = 1 << 6
flagIndir flag = 1 << 7
flagAddr flag = 1 << 8
flagMethod flag = 1 << 9
flagMethodShift = 10
flagRO flag = flagStickyRO | flagEmbedRO
)
func (f flag) kind() Kind {
return Kind(f & flagKindMask)
}
func (f flag) ro() flag {
if f&flagRO != 0 {
return flagStickyRO
}
return 0
}
// emptyInterface is the header for an interface{} value.
type emptyInterface struct {
typ *abi.Type
word unsafe.Pointer
}
// nonEmptyInterface is the header for an interface value with methods.
type nonEmptyInterface struct {
// see ../runtime/iface.go:/Itab
itab *struct {
ityp *abi.Type // static interface type
typ *abi.Type // dynamic concrete type
hash uint32 // copy of typ.hash
_ [4]byte
fun [100000]unsafe.Pointer // method table
}
word unsafe.Pointer
}

View File

@@ -116,6 +116,9 @@ func NewNamed(kind abi.Kind, methods, ptrMethods int) *Type {
// InitNamed initializes an uninitialized named type.
func InitNamed(ret *Type, pkgPath, name string, underlying *Type, methods, ptrMethods []Method) {
ptr := ret.PtrToThis_
if pkgPath != "" {
name = pkgPath + "." + name
}
doInitNamed(ret, pkgPath, name, underlying, methods)
doInitNamed(ptr, pkgPath, name, newPointer(ret), ptrMethods)
ret.PtrToThis_ = ptr
@@ -130,7 +133,7 @@ func newUninitedNamed(kind abi.Kind, methods int) *Type {
return ret
}
func doInitNamed(ret *Type, pkgPath, name string, underlying *Type, methods []Method) {
func doInitNamed(ret *Type, pkgPath, fullName string, underlying *Type, methods []Method) {
tflag := underlying.TFlag
if tflag&abi.TFlagUncommon != 0 {
panic("runtime: underlying type is already named")
@@ -146,7 +149,7 @@ func doInitNamed(ret *Type, pkgPath, name string, underlying *Type, methods []Me
c.Memcpy(ptr, unsafe.Pointer(underlying), baseSize)
ret.TFlag = tflag | abi.TFlagNamed | abi.TFlagUncommon
ret.Str_ = name
ret.Str_ = fullName
n := len(methods)
xcount := uint16(0)
@@ -174,6 +177,7 @@ func Func(in, out []*Type, variadic bool) *FuncType {
Size_: unsafe.Sizeof(uintptr(0)),
Hash: uint32(abi.Func), // TODO(xsw): hash
Kind_: uint8(abi.Func),
Str_: "func(...)",
},
In: in,
Out: out,

View File

@@ -28,49 +28,53 @@ type Type = abi.Type
// -----------------------------------------------------------------------------
func Basic(kind Kind) *Type {
name, size := basicTypeInfo(kind)
return &Type{
Size_: basicTypeSize(kind),
Size_: size,
Hash: uint32(kind), // TODO(xsw): hash
Kind_: uint8(kind),
Str_: name,
}
}
func basicTypeSize(kind abi.Kind) uintptr {
func basicTypeInfo(kind abi.Kind) (string, uintptr) {
switch kind {
case abi.Bool:
return unsafe.Sizeof(false)
return "bool", unsafe.Sizeof(false)
case abi.Int:
return unsafe.Sizeof(0)
return "int", unsafe.Sizeof(0)
case abi.Int8:
return 1
return "int8", 1
case abi.Int16:
return 2
return "int16", 2
case abi.Int32:
return 4
return "int32", 4
case abi.Int64:
return 8
return "int64", 8
case abi.Uint:
return unsafe.Sizeof(uint(0))
return "uint", unsafe.Sizeof(uint(0))
case abi.Uint8:
return 1
return "uint8", 1
case abi.Uint16:
return 2
return "uint16", 2
case abi.Uint32:
return 4
return "uint32", 4
case abi.Uint64:
return 8
return "uint64", 8
case abi.Uintptr:
return unsafe.Sizeof(uintptr(0))
return "uintptr", unsafe.Sizeof(uintptr(0))
case abi.Float32:
return 4
return "float32", 4
case abi.Float64:
return 8
return "float64", 8
case abi.Complex64:
return 8
return "complex64", 8
case abi.Complex128:
return 16
return "complex128", 16
case abi.String:
return unsafe.Sizeof(String{})
return "string", unsafe.Sizeof(String{})
case abi.UnsafePointer:
return "unsafe.Pointer", unsafe.Sizeof(unsafe.Pointer(nil))
}
panic("unreachable")
}
@@ -95,6 +99,7 @@ func Struct(pkgPath string, size uintptr, fields ...abi.StructField) *Type {
Size_: size,
Hash: uint32(abi.Struct), // TODO(xsw): hash
Kind_: uint8(abi.Struct),
Str_: "struct {...}",
},
PkgPath_: pkgPath,
Fields: fields,
@@ -123,6 +128,12 @@ func newPointer(elem *Type) *Type {
},
Elem: elem,
}
if (elem.TFlag & abi.TFlagExtraStar) != 0 {
ptr.Str_ = "**" + elem.Str_
} else {
ptr.TFlag = abi.TFlagExtraStar
ptr.Str_ = elem.Str_
}
return &ptr.Type
}
@@ -133,6 +144,7 @@ func SliceOf(elem *Type) *Type {
Size_: unsafe.Sizeof([]int{}),
Hash: uint32(abi.Slice),
Kind_: uint8(abi.Slice),
Str_: "[]" + elem.String(),
},
Elem: elem,
}
@@ -146,6 +158,7 @@ func ArrayOf(length uintptr, elem *Type) *Type {
Size_: length * elem.Size_,
Hash: uint32(abi.Array),
Kind_: uint8(abi.Array),
Str_: "[...]" + elem.String(), // TODO(xsw): itoa
},
Elem: elem,
Slice: SliceOf(elem),