abiType fix

This commit is contained in:
xushiwei
2024-05-25 08:20:09 +08:00
parent 1c8f860b6e
commit 5eac8d860a
6 changed files with 228 additions and 79 deletions

View File

@@ -121,7 +121,8 @@ func (b *Builder) TypeName(t types.Type) (ret string, pub bool) {
case *types.Struct:
return b.StructName(t)
case *types.Named:
return NamedName(t), false // all named types are private
o := t.Obj()
return TypeName(o), o.Exported()
}
panic("todo")
}
@@ -139,9 +140,8 @@ func FullName(pkg *types.Package, name string) string {
return PathOf(pkg) + "." + name
}
// NamedName returns the ABI type name for the specified named type.
func NamedName(t *types.Named) string {
o := t.Obj()
// TypeName returns the ABI type name for the specified named type.
func TypeName(o *types.TypeName) string {
return FullName(o.Pkg(), o.Name())
}

View File

@@ -38,10 +38,12 @@ func (b Builder) abiBasic(t *types.Basic) Expr {
return b.InlineCall(b.Pkg.rtFunc("Basic"), b.Prog.Val(kind))
}
/*
func (b Builder) abiExtern(name string) Expr {
g := b.Pkg.NewVarFrom(name, b.Prog.AbiTypePtrPtr())
return b.Load(g.Expr)
}
*/
func (b Builder) abiTypeOf(t types.Type) Expr {
switch t := t.(type) {
@@ -59,7 +61,7 @@ func (b Builder) abiTypeOf(t types.Type) Expr {
func (b Builder) abiNamedOf(t *types.Named) Expr {
under := b.abiTypeOf(t.Underlying())
name := abi.NamedName(t)
name := NameOf(t)
return b.Call(b.Pkg.rtFunc("Named"), b.Str(name), under)
}
@@ -100,22 +102,12 @@ func (b Builder) structField(sfAbi Expr, prog Program, f *types.Var, offset uint
// abiType returns the abi type of the specified type.
func (b Builder) abiType(t types.Type) Expr {
var name string
var pub bool
var pkg = b.Pkg
switch tx := t.(type) {
case *types.Basic:
return b.abiBasic(tx)
case *types.Named:
o := tx.Obj()
oPkgPath := abi.PathOf(o.Pkg())
name = oPkgPath + "." + o.Name()
if oPkgPath != pkg.Path() {
return b.abiExtern(name)
}
default:
name, pub = pkg.abi.TypeName(t)
}
pkg := b.Pkg
name, pub := pkg.abi.TypeName(t)
g := pkg.VarOf(name)
if g == nil {
prog := b.Prog

View File

@@ -404,7 +404,7 @@ func (p Program) toNamed(raw *types.Named) Type {
// NameOf returns the full name of a named type.
func NameOf(typ *types.Named) string {
return abi.NamedName(typ)
return abi.TypeName(typ.Obj())
}
// FullName returns the full name of a package member.