Merge pull request #371 from xushiwei/q

patch: reflect (reflect.Type)
This commit is contained in:
xushiwei
2024-06-20 17:51:31 +08:00
committed by GitHub
12 changed files with 1233 additions and 27 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())
}

6
c/c.go
View File

@@ -75,6 +75,12 @@ func Memmove(dst, src Pointer, n uintptr) Pointer
//go:linkname Memset C.memset //go:linkname Memset C.memset
func Memset(s Pointer, c Int, n uintptr) Pointer func Memset(s Pointer, c Int, n uintptr) Pointer
//go:linkname Memchr C.memchr
func Memchr(s Pointer, c Int, n uintptr) Pointer
//go:linkname Memcmp C.memcmp
func Memcmp(s1, s2 Pointer, n uintptr) Int
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
//go:linkname Strlen C.strlen //go:linkname Strlen C.strlen

View File

@@ -519,10 +519,9 @@ func ignoreName(name string) bool {
} }
*/ */
return (strings.HasPrefix(name, "internal/") && !supportedInternal(name)) || 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, "arena.") || strings.HasPrefix(name, "maps.") ||
strings.HasPrefix(name, "time.") || strings.HasPrefix(name, "runtime/") || strings.HasPrefix(name, "time.") || strings.HasPrefix(name, "plugin.")
strings.HasPrefix(name, "plugin.")
} }
func supportedInternal(name string) bool { 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_) } func (t *Type) FieldAlign() int { return int(t.FieldAlign_) }
// Name returns the name of type t. // String returns string form of type t.
func (t *Type) Name() string { func (t *Type) String() string {
if t.TFlag&TFlagExtraStar != 0 { if t.TFlag&TFlagExtraStar != 0 {
return "*" + t.Str_ return "*" + t.Str_
} }

View File

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

View File

@@ -18,5 +18,25 @@ package bytealg
// llgo:skip init // llgo:skip init
import ( import (
_ "unsafe" "unsafe"
"github.com/goplus/llgo/c"
) )
func IndexByte(b []byte, ch byte) int {
ptr := unsafe.Pointer(unsafe.SliceData(b))
ret := c.Memchr(ptr, c.Int(ch), uintptr(len(b)))
if ret != nil {
return int(uintptr(ret) - uintptr(ptr))
}
return -1
}
func IndexByteString(s string, ch byte) int {
ptr := unsafe.Pointer(unsafe.StringData(s))
ret := c.Memchr(ptr, c.Int(ch), uintptr(len(s)))
if ret != nil {
return int(uintptr(ret) - uintptr(ptr))
}
return -1
}

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

View File

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

View File

@@ -966,6 +966,8 @@ func (b Builder) BuiltinCall(fn string, args ...Expr) (ret Expr) {
case "Slice": // unsafe.Slice case "Slice": // unsafe.Slice
size := args[1].impl size := args[1].impl
return b.unsafeSlice(args[0], size, size) return b.unsafeSlice(args[0], size, size)
case "StringData":
return b.StringData(args[0]) // TODO(xsw): check return type
case "SliceData": case "SliceData":
return b.SliceData(args[0]) // TODO(xsw): check return type return b.SliceData(args[0]) // TODO(xsw): check return type
} }