cvtClosure, llvmParamsEx

This commit is contained in:
xushiwei
2024-05-05 18:20:51 +08:00
parent d7df46d578
commit 87ca3a39dc
6 changed files with 32 additions and 31 deletions

View File

@@ -54,7 +54,7 @@ func (p Program) Type(typ types.Type, bg Background) Type {
// FuncDecl converts a Go/C function declaration into raw type.
func (p Program) FuncDecl(sig *types.Signature, bg Background) Type {
if bg == InGo {
sig = p.gocvt.cvtFunc(sig, true)
sig = p.gocvt.cvtFunc(sig, sig.Recv())
}
return &aType{p.toLLVMFunc(sig), rawType{sig}, vkFuncDecl}
}
@@ -123,7 +123,8 @@ func (p goTypes) cvtNamed(t *types.Named) (raw *types.Named, cvt bool) {
}
func (p goTypes) cvtClosure(sig *types.Signature) *types.Struct {
raw := p.cvtFunc(sig, false)
ctx := types.NewParam(token.NoPos, nil, ClosureCtx, types.Typ[types.UnsafePointer])
raw := p.cvtFunc(sig, ctx)
flds := []*types.Var{
types.NewField(token.NoPos, nil, "f", raw, false),
types.NewField(token.NoPos, nil, "data", types.Typ[types.UnsafePointer], false),
@@ -131,15 +132,9 @@ func (p goTypes) cvtClosure(sig *types.Signature) *types.Struct {
return types.NewStruct(flds, nil)
}
func (p goTypes) cvtFunc(sig *types.Signature, hasRecv bool) (raw *types.Signature) {
if v, ok := p.typs[unsafe.Pointer(sig)]; ok {
return (*types.Signature)(v)
}
defer func() {
p.typs[unsafe.Pointer(sig)] = unsafe.Pointer(raw)
}()
if hasRecv {
sig = methodToFunc(sig)
func (p goTypes) cvtFunc(sig *types.Signature, recv *types.Var) (raw *types.Signature) {
if recv != nil {
sig = FuncAddCtx(recv, sig)
}
params, cvt1 := p.cvtTuple(sig.Params())
results, cvt2 := p.cvtTuple(sig.Results())
@@ -174,7 +169,7 @@ func (p goTypes) cvtExplicitMethods(typ *types.Interface) ([]*types.Func, bool)
for i := 0; i < n; i++ {
m := typ.ExplicitMethod(i)
sig := m.Type().(*types.Signature)
if raw := p.cvtFunc(sig, false); sig != raw {
if raw := p.cvtFunc(sig, nil); sig != raw {
m = types.NewFunc(m.Pos(), m.Pkg(), m.Name(), raw)
needcvt = true
}
@@ -243,14 +238,6 @@ func (p goTypes) cvtStruct(typ *types.Struct) (raw *types.Struct, cvt bool) {
// -----------------------------------------------------------------------------
// convert method to func
func methodToFunc(sig *types.Signature) *types.Signature {
if recv := sig.Recv(); recv != nil {
return FuncAddCtx(recv, sig)
}
return sig
}
// FuncAddCtx adds a ctx to a function signature.
func FuncAddCtx(ctx *types.Var, sig *types.Signature) *types.Signature {
tParams := sig.Params()