abi: support named
This commit is contained in:
@@ -111,31 +111,53 @@ func (b *Builder) Init(pkg string) {
|
||||
}
|
||||
|
||||
// TypeName returns the ABI type name for the specified type.
|
||||
func (b *Builder) TypeName(t types.Type) (ret string, private bool) {
|
||||
func (b *Builder) TypeName(t types.Type) (ret string, pub bool) {
|
||||
switch t := t.(type) {
|
||||
case *types.Basic:
|
||||
return BasicName(t), false
|
||||
return BasicName(t), true
|
||||
case *types.Pointer:
|
||||
ret, private = b.TypeName(t.Elem())
|
||||
return "*" + ret, private
|
||||
ret, pub = b.TypeName(t.Elem())
|
||||
return "*" + ret, pub
|
||||
case *types.Struct:
|
||||
return b.StructName(t)
|
||||
case *types.Named:
|
||||
return NamedName(t), false // all named types are private
|
||||
}
|
||||
panic("todo")
|
||||
}
|
||||
|
||||
// PathOf returns the package path of the specified package.
|
||||
func PathOf(pkg *types.Package) string {
|
||||
if pkg.Name() == "main" {
|
||||
return "main"
|
||||
}
|
||||
return pkg.Path()
|
||||
}
|
||||
|
||||
// FullName returns the full name of a package member.
|
||||
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()
|
||||
return FullName(o.Pkg(), o.Name())
|
||||
}
|
||||
|
||||
// BasicName returns the ABI type name for the specified basic type.
|
||||
func BasicName(t *types.Basic) string {
|
||||
return "_llgo_" + t.Name()
|
||||
}
|
||||
|
||||
// StructName returns the ABI type name for the specified struct type.
|
||||
func (b *Builder) StructName(t *types.Struct) (ret string, private bool) {
|
||||
func (b *Builder) StructName(t *types.Struct) (ret string, pub bool) {
|
||||
hash, private := b.structHash(t)
|
||||
hashStr := base64.RawURLEncoding.EncodeToString(hash)
|
||||
if private {
|
||||
return b.Pkg + ".struct$" + hashStr, true
|
||||
return b.Pkg + ".struct$" + hashStr, false
|
||||
}
|
||||
return "_llgo_struct$" + hashStr, false
|
||||
return "_llgo_struct$" + hashStr, true
|
||||
}
|
||||
|
||||
func (b *Builder) structHash(t *types.Struct) (ret []byte, private bool) {
|
||||
@@ -152,8 +174,8 @@ func (b *Builder) structHash(t *types.Struct) (ret []byte, private bool) {
|
||||
if f.Embedded() {
|
||||
name = "-"
|
||||
}
|
||||
ft, fpriv := b.TypeName(f.Type())
|
||||
if fpriv {
|
||||
ft, pub := b.TypeName(f.Type())
|
||||
if !pub {
|
||||
private = true
|
||||
}
|
||||
fmt.Fprintln(h, name, ft)
|
||||
|
||||
@@ -26,7 +26,7 @@ import (
|
||||
)
|
||||
|
||||
func TestFromTestgo(t *testing.T) {
|
||||
cltest.FromDir(t, "", "../cl/_testgo", false)
|
||||
cltest.FromDir(t, "struczero", "../cl/_testgo", false)
|
||||
}
|
||||
|
||||
func TestFromTestpy(t *testing.T) {
|
||||
|
||||
@@ -32,14 +32,17 @@ import (
|
||||
func (b Builder) abiBasic(t *types.Basic) Expr {
|
||||
/*
|
||||
TODO(xsw):
|
||||
name := abi.BasicName(t)
|
||||
g := b.Pkg.NewVarFrom(name, b.Prog.AbiTypePtrPtr())
|
||||
return b.Load(g.Expr)
|
||||
return b.abiExtern(abi.BasicName(t))
|
||||
*/
|
||||
kind := int(abi.BasicKind(t))
|
||||
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) {
|
||||
case *types.Basic:
|
||||
@@ -48,10 +51,18 @@ func (b Builder) abiTypeOf(t types.Type) Expr {
|
||||
return b.abiPointerOf(t)
|
||||
case *types.Struct:
|
||||
return b.abiStructOf(t)
|
||||
case *types.Named:
|
||||
return b.abiNamedOf(t)
|
||||
}
|
||||
panic("todo")
|
||||
}
|
||||
|
||||
func (b Builder) abiNamedOf(t *types.Named) Expr {
|
||||
under := b.abiTypeOf(t.Underlying())
|
||||
name := abi.NamedName(t)
|
||||
return b.Call(b.Pkg.rtFunc("Named"), b.Str(name), under)
|
||||
}
|
||||
|
||||
func (b Builder) abiPointerOf(t *types.Pointer) Expr {
|
||||
elem := b.abiTypeOf(t.Elem())
|
||||
return b.Call(b.Pkg.rtFunc("Pointer"), elem)
|
||||
@@ -71,7 +82,7 @@ func (b Builder) abiStructOf(t *types.Struct) Expr {
|
||||
off := uintptr(prog.OffsetOf(typ, i))
|
||||
flds[i] = b.structField(sfAbi, prog, f, off, t.Tag(i))
|
||||
}
|
||||
pkgPath := b.Str(pkg.abi.Pkg)
|
||||
pkgPath := b.Str(pkg.Path())
|
||||
params := strucAbi.raw.Type.(*types.Signature).Params()
|
||||
tSlice := prog.rawType(params.At(params.Len() - 1).Type().(*types.Slice))
|
||||
fldSlice := b.SliceLit(tSlice, flds...)
|
||||
@@ -89,17 +100,27 @@ 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 {
|
||||
if tx, ok := t.(*types.Basic); ok {
|
||||
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, private := pkg.abi.TypeName(t)
|
||||
g := pkg.VarOf(name)
|
||||
if g == nil {
|
||||
prog := b.Prog
|
||||
g = pkg.doNewVar(name, prog.AbiTypePtrPtr())
|
||||
g.Init(prog.Null(g.Type))
|
||||
pub := !private
|
||||
if pub {
|
||||
g.impl.SetLinkage(llvm.LinkOnceAnyLinkage)
|
||||
}
|
||||
|
||||
@@ -551,6 +551,11 @@ func (p Package) closureStub(b Builder, t *types.Struct, v Expr) Expr {
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// Path returns the package path.
|
||||
func (p Package) Path() string {
|
||||
return p.abi.Pkg
|
||||
}
|
||||
|
||||
// String returns a string representation of the package.
|
||||
func (p Package) String() string {
|
||||
return p.mod.String()
|
||||
@@ -564,7 +569,7 @@ func (p Package) AfterInit(b Builder, ret BasicBlock) {
|
||||
b.SetBlockEx(ret, afterInit, false)
|
||||
if doAbiInit {
|
||||
sigAbiInit := types.NewSignatureType(nil, nil, nil, nil, nil, false)
|
||||
fn := p.NewFunc(p.abi.Pkg+".init$abi", sigAbiInit, InC)
|
||||
fn := p.NewFunc(p.Path()+".init$abi", sigAbiInit, InC)
|
||||
fnb := fn.MakeBody(1)
|
||||
for _, abiInit := range p.abiini {
|
||||
abiInit(unsafe.Pointer(fnb))
|
||||
|
||||
14
ssa/type.go
14
ssa/type.go
@@ -20,6 +20,7 @@ import (
|
||||
"fmt"
|
||||
"go/types"
|
||||
|
||||
"github.com/goplus/llgo/ssa/abi"
|
||||
"github.com/goplus/llvm"
|
||||
)
|
||||
|
||||
@@ -401,20 +402,19 @@ func (p Program) toNamed(raw *types.Named) Type {
|
||||
}
|
||||
}
|
||||
|
||||
// NameOf returns the full name of a named type.
|
||||
func NameOf(typ *types.Named) string {
|
||||
obj := typ.Obj()
|
||||
return FullName(obj.Pkg(), obj.Name())
|
||||
return abi.NamedName(typ)
|
||||
}
|
||||
|
||||
// FullName returns the full name of a package member.
|
||||
func FullName(pkg *types.Package, name string) string {
|
||||
return PathOf(pkg) + "." + name
|
||||
return abi.FullName(pkg, name)
|
||||
}
|
||||
|
||||
// PathOf returns the package path of the specified package.
|
||||
func PathOf(pkg *types.Package) string {
|
||||
if pkg.Name() == "main" {
|
||||
return "main"
|
||||
}
|
||||
return pkg.Path()
|
||||
return abi.PathOf(pkg)
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user