cl: makeInterface check instance named

This commit is contained in:
visualfc
2024-08-09 22:23:48 +08:00
parent d4af6af594
commit 3435b6c4a4
12 changed files with 618 additions and 58 deletions

View File

@@ -167,7 +167,7 @@ func (b *Builder) TypeName(t types.Type) (ret string, pub bool) {
case *types.Named:
o := t.Obj()
pkg := o.Pkg()
return "_llgo_" + FullName(pkg, o.Name()), (pkg == nil || o.Exported())
return "_llgo_" + FullName(pkg, NamedName(t)), (pkg == nil || o.Exported())
case *types.Interface:
if t.Empty() {
return "_llgo_any", true
@@ -194,6 +194,26 @@ func (b *Builder) TypeName(t types.Type) (ret string, pub bool) {
return
}
func NamedName(t *types.Named) string {
if targs := t.TypeArgs(); targs != nil {
n := targs.Len()
infos := make([]string, n)
for i := 0; i < n; i++ {
infos[i] = types.TypeString(targs.At(i), PathOf)
}
return t.Obj().Name() + "[" + strings.Join(infos, ",") + "]"
}
return t.Obj().Name()
}
func TypeArgs(typeArgs []types.Type) string {
targs := make([]string, len(typeArgs))
for i, t := range typeArgs {
targs[i] = types.TypeString(t, PathOf)
}
return "[" + strings.Join(targs, ",") + "]"
}
const (
PatchPathPrefix = "github.com/goplus/llgo/internal/lib/"
)

View File

@@ -153,7 +153,7 @@ func (b Builder) abiMethodOf(mPkg *types.Package, mName string, mSig *types.Sign
}
func (b Builder) abiMthd(mPkg *types.Package, mName string, mSig *types.Signature, name, abiTyp, ifn llvm.Value) (ret Expr, tfn llvm.Value) {
fullName := FuncName(mPkg, mName, mSig.Recv())
fullName := FuncName(mPkg, mName, mSig.Recv(), false)
if b.Pkg.fnlink != nil {
fullName = b.Pkg.fnlink(fullName)
}
@@ -230,7 +230,8 @@ func (b Builder) abiInitNamed(ret Expr, t *types.Named) func() Expr {
pkg := b.Pkg
prog := b.Prog
path := abi.PathOf(t.Obj().Pkg())
name := t.Obj().Name()
name := abi.NamedName(t)
//targs := abi.NamedTypeArgs(t)
var initNamed = pkg.rtFunc("InitNamed")
var tSlice = lastParamType(prog, initNamed)
mset := typeutil.IntuitiveMethodSet(t, nil)

View File

@@ -180,11 +180,11 @@ type Function = *aFunction
// NewFunc creates a new function.
func (p Package) NewFunc(name string, sig *types.Signature, bg Background) Function {
return p.NewFuncEx(name, sig, bg, false)
return p.NewFuncEx(name, sig, bg, false, false)
}
// NewFuncEx creates a new function.
func (p Package) NewFuncEx(name string, sig *types.Signature, bg Background, hasFreeVars bool) Function {
func (p Package) NewFuncEx(name string, sig *types.Signature, bg Background, hasFreeVars bool, instantiated bool) Function {
if v, ok := p.fns[name]; ok {
return v
}
@@ -193,6 +193,9 @@ func (p Package) NewFuncEx(name string, sig *types.Signature, bg Background, has
log.Println("NewFunc", name, t.raw.Type, "hasFreeVars:", hasFreeVars)
}
fn := llvm.AddFunction(p.mod, name, t.ll)
if instantiated {
fn.SetLinkage(llvm.LinkOnceAnyLinkage)
}
ret := newFunction(fn, t, p, p.Prog, hasFreeVars)
p.fns[name] = ret
return ret

View File

@@ -19,7 +19,6 @@ package ssa
import (
"fmt"
"go/types"
"strings"
"github.com/goplus/llgo/ssa/abi"
"github.com/goplus/llvm"
@@ -515,16 +514,7 @@ func (p Program) toNamed(raw *types.Named) Type {
// NameOf returns the full name of a named type.
func NameOf(typ *types.Named) string {
name := abi.TypeName(typ.Obj())
if targs := typ.TypeArgs(); targs != nil {
n := targs.Len()
args := make([]string, n)
for i := 0; i < n; i++ {
args[i] = types.TypeString(targs.At(i), PathOf)
}
name += "[" + strings.Join(args, ", ") + "]"
}
return name
return abi.FullName(typ.Obj().Pkg(), abi.NamedName(typ))
}
// FullName returns the full name of a package member.
@@ -540,14 +530,22 @@ func PathOf(pkg *types.Package) string {
// FuncName:
// - func: pkg.name
// - method: pkg.T.name, pkg.(*T).name
func FuncName(pkg *types.Package, name string, recv *types.Var) string {
func FuncName(pkg *types.Package, name string, recv *types.Var, org bool) string {
if recv != nil {
var tName string
t := recv.Type()
if tp, ok := t.(*types.Pointer); ok {
tName = "(*" + tp.Elem().(*types.Named).Obj().Name() + ")"
if org {
if tp, ok := t.(*types.Pointer); ok {
tName = "(*" + tp.Elem().(*types.Named).Obj().Name() + ")"
} else {
tName = t.(*types.Named).Obj().Name()
}
} else {
tName = t.(*types.Named).Obj().Name()
if tp, ok := t.(*types.Pointer); ok {
tName = "(*" + abi.NamedName(tp.Elem().(*types.Named)) + ")"
} else {
tName = abi.NamedName(t.(*types.Named))
}
}
return PathOf(pkg) + "." + tName + "." + name
}
@@ -558,4 +556,8 @@ func FuncName(pkg *types.Package, name string, recv *types.Var) string {
return ret
}
func TypeArgs(typeArgs []types.Type) string {
return abi.TypeArgs(typeArgs)
}
// -----------------------------------------------------------------------------