remove abi.Name

This commit is contained in:
xushiwei
2024-05-26 17:53:29 +08:00
parent ddabfdca3d
commit eae94c5f23
14 changed files with 333 additions and 562 deletions

Binary file not shown.

View File

@@ -20,6 +20,15 @@ import (
"unsafe"
)
// IsExported reports whether name starts with an upper-case letter.
func IsExported(name string) bool {
if len(name) > 0 {
c := name[0]
return 'A' <= c && c <= 'Z'
}
return false
}
// -----------------------------------------------------------------------------
// Type is the runtime representation of a Go type.
@@ -46,8 +55,8 @@ type Type struct {
// If the KindGCProg bit is set in kind, GCData is a GC program.
// Otherwise it is a ptrmask bitmap. See mbitmap.go for details.
GCData *byte
Str_ Name // string form
PtrToThis_ *Type // type for pointer to this type, may be nil
Str_ string // string form
PtrToThis_ *Type // type for pointer to this type, may be nil
}
// A Kind represents the specific kind of type that a Type represents.
@@ -209,32 +218,41 @@ func (p *FuncType) Variadic() bool {
}
type StructField struct {
Name Name // name is always non-empty
Name_ string // name is always non-empty
Typ *Type // type of field
Offset uintptr // byte offset of field
Tag_ string
Embedded_ bool
}
// Embedded reports whether the field is embedded.
func (f *StructField) Embedded() bool {
return f.Name.IsEmbedded()
return f.Embedded_
}
// Exported reports whether the field is exported.
func (f *StructField) Exported() bool {
return IsExported(f.Name_)
}
type StructType struct {
Type
PkgPath Name
Fields []StructField
PkgPath_ string
Fields []StructField
}
type InterfaceType struct {
Type
PkgPath Name // import path
Methods []Imethod // sorted by hash
PkgPath_ string // import path
Methods []Imethod // sorted by hash
}
type Text = unsafe.Pointer // TODO(xsw): to be confirmed
// Method on non-interface type
type Method struct {
Name_ Name // name of method
Name_ string // name of method
Mtyp_ *FuncType // method type (without receiver)
Ifn_ Text // fn used in interface call (one-word receiver)
Tfn_ Text // fn used for normal method call
@@ -242,7 +260,7 @@ type Method struct {
// Exported reports whether the method is exported.
func (p *Method) Exported() bool {
return p.Name_.IsExported()
return IsExported(p.Name_)
}
// UncommonType is present only for defined types or types with methods
@@ -250,7 +268,7 @@ func (p *Method) Exported() bool {
// Using a pointer to this struct reduces the overall size required
// to describe a non-defined type with no methods.
type UncommonType struct {
PkgPath_ Name // import path; empty for built-in types like int, string
PkgPath_ string // import path; empty for built-in types like int, string
Mcount uint16 // number of methods
Xcount uint16 // number of exported methods
Moff uint32 // offset from this uncommontype to [mcount]Method
@@ -272,7 +290,7 @@ func (t *UncommonType) ExportedMethods() []Method {
// Imethod represents a method on an interface type
type Imethod struct {
Name_ Name // name of method
Name_ string // name of method
Typ_ *FuncType // .(*FuncType) underneath
}
@@ -438,6 +456,7 @@ func addChecked(p unsafe.Pointer, x uintptr, whySafe string) unsafe.Pointer {
return unsafe.Pointer(uintptr(p) + x)
}
/*
// Name is an encoded type Name with optional extra data.
//
// The first byte is a bit field containing:
@@ -588,5 +607,6 @@ func NewName(n, tag string, exported, embedded bool) Name {
return Name{Bytes: &b[0]}
}
*/
// -----------------------------------------------------------------------------

Binary file not shown.

View File

@@ -86,7 +86,7 @@ func Func(in, out []*Type, variadic bool) *FuncType {
}
// Imethod returns an interface method.
func Imethod(name Name, typ *FuncType) abi.Imethod {
func Imethod(name string, typ *FuncType) abi.Imethod {
return abi.Imethod{
Name_: name,
Typ_: typ,
@@ -94,7 +94,7 @@ func Imethod(name Name, typ *FuncType) abi.Imethod {
}
// Method returns a method.
func Method(name Name, typ *FuncType, ifn, tfn abi.Text) abi.Method {
func Method(name string, typ *FuncType, ifn, tfn abi.Text) abi.Method {
return abi.Method{
Name_: name,
Mtyp_: typ,
@@ -104,11 +104,11 @@ func Method(name Name, typ *FuncType, ifn, tfn abi.Text) abi.Method {
}
// Named returns a named type.
func Named(pkgPath, name Name, underlying *Type, methods []abi.Method) *Type {
func Named(pkgPath, name string, underlying *Type, methods []abi.Method) *Type {
tflag := underlying.TFlag
size := typeHdrSize
n := len(methods)
if n > 0 || pkgPath.Bytes != nil {
if n > 0 || pkgPath != "" {
size += uncommonTypeHdrSize + uintptr(n)*methodSize
tflag |= abi.TFlagUncommon
}
@@ -139,15 +139,15 @@ func Named(pkgPath, name Name, underlying *Type, methods []abi.Method) *Type {
}
// Interface returns an interface type.
func Interface(pkgPath Name, methods []abi.Imethod) *Type {
func Interface(pkgPath string, methods []abi.Imethod) *Type {
ret := &abi.InterfaceType{
Type: Type{
Size_: unsafe.Sizeof(eface{}),
Hash: uint32(abi.Interface), // TODO(xsw): hash
Kind_: uint8(abi.Interface),
},
PkgPath: pkgPath,
Methods: methods,
PkgPath_: pkgPath,
Methods: methods,
}
return &ret.Type
}
@@ -168,7 +168,7 @@ func NewItab(inter *InterfaceType, typ *Type) *Itab {
ret.fun[0] = 0
} else {
data := (*uintptr)(c.Advance(ptr, int(itabHdrSize)))
mthds := methods(u, inter.PkgPath)
mthds := methods(u, inter.PkgPath_)
for i, m := range inter.Methods {
fn := findMethod(mthds, m)
if fn == nil {
@@ -182,9 +182,9 @@ func NewItab(inter *InterfaceType, typ *Type) *Itab {
}
func findMethod(mthds []abi.Method, im abi.Imethod) abi.Text {
imName := im.Name_.Name()
imName := im.Name_
for _, m := range mthds {
mName := m.Name_.Name()
mName := m.Name_
if mName >= imName {
if mName == imName && m.Mtyp_ == im.Typ_ {
return m.Ifn_
@@ -195,8 +195,8 @@ func findMethod(mthds []abi.Method, im abi.Imethod) abi.Text {
return nil
}
func methods(u *abi.UncommonType, from abi.Name) []abi.Method {
if u.PkgPath_.Name() == from.Name() {
func methods(u *abi.UncommonType, from string) []abi.Method {
if u.PkgPath_ == from {
return u.Methods()
}
return u.ExportedMethods()

View File

@@ -23,11 +23,13 @@ import (
)
type Kind = abi.Kind
type Name = abi.Name
type Type = abi.Type
// -----------------------------------------------------------------------------
/*
type Name = abi.Name
// NewName creates a new name.
func NewName(name string, exported bool) Name {
return abi.NewName(name, "", exported, false)
@@ -40,6 +42,7 @@ func NewPkgName(pkgPath string) (ret Name) {
}
return
}
*/
// -----------------------------------------------------------------------------
@@ -102,25 +105,26 @@ func basicType(kind abi.Kind) *Type {
// -----------------------------------------------------------------------------
// StructField returns a struct field.
func StructField(name string, typ *Type, off uintptr, tag string, exported, embedded bool) abi.StructField {
n := abi.NewName(name, tag, exported, embedded)
func StructField(name string, typ *Type, off uintptr, tag string, embedded bool) abi.StructField {
return abi.StructField{
Name: n,
Typ: typ,
Offset: off,
Name_: name,
Typ: typ,
Offset: off,
Tag_: tag,
Embedded_: embedded,
}
}
// Struct returns a struct type.
func Struct(pkgPath Name, size uintptr, fields ...abi.StructField) *Type {
func Struct(pkgPath string, size uintptr, fields ...abi.StructField) *Type {
ret := &abi.StructType{
Type: Type{
Size_: size,
Hash: uint32(abi.Struct), // TODO(xsw): hash
Kind_: uint8(abi.Struct),
},
PkgPath: pkgPath,
Fields: fields,
PkgPath_: pkgPath,
Fields: fields,
}
return &ret.Type
}