abitype: support Patch

This commit is contained in:
xushiwei
2024-06-30 11:53:12 +08:00
parent c20bea50e3
commit 8c45eb7524
4 changed files with 18 additions and 9 deletions

View File

@@ -542,7 +542,7 @@ func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue
prog := p.prog prog := p.prog
t := prog.Type(v.Type(), llssa.InGo) t := prog.Type(v.Type(), llssa.InGo)
x := p.compileValue(b, v.X) x := p.compileValue(b, v.X)
ret = b.MakeInterface(t, patchValue(p, x)) ret = b.MakeInterface(t, x)
case *ssa.MakeSlice: case *ssa.MakeSlice:
var nCap llssa.Expr var nCap llssa.Expr
t := p.prog.Type(v.Type(), llssa.InGo) t := p.prog.Type(v.Type(), llssa.InGo)
@@ -790,6 +790,7 @@ func NewPackageEx(prog llssa.Program, patches Patches, pkg *ssa.Package, files [
if hasPatch { if hasPatch {
skips := ctx.skips skips := ctx.skips
typepatch.Merge(pkgTypes, oldTypes, skips, ctx.skipall) typepatch.Merge(pkgTypes, oldTypes, skips, ctx.skipall)
ret.SetPatch(ctx.patchType)
ctx.skips = nil ctx.skips = nil
ctx.state = pkgInPatch ctx.state = pkgInPatch
if _, ok := skips["init"]; ok || ctx.skipall { if _, ok := skips["init"]; ok || ctx.skipall {
@@ -864,21 +865,18 @@ func globalType(gbl *ssa.Global) types.Type {
return t return t
} }
func patchValue(ctx *context, v llssa.Expr) llssa.Expr { func (p *context) patchType(typ types.Type) types.Type {
/* TODO(xsw): if t, ok := typ.(*types.Named); ok {
t := v.RawType()
if t, ok := t.(*types.Named); ok {
o := t.Obj() o := t.Obj()
if pkg := o.Pkg(); typepatch.IsPatched(pkg) { if pkg := o.Pkg(); typepatch.IsPatched(pkg) {
if patch, ok := ctx.patches[pkg.Path()]; ok { if patch, ok := p.patches[pkg.Path()]; ok {
if obj := patch.Types.Scope().Lookup(o.Name()); obj != nil { if obj := patch.Types.Scope().Lookup(o.Name()); obj != nil {
v.Type = ctx.prog.Type(obj.Type(), llssa.InGo) return p.prog.Type(obj.Type(), llssa.InGo).RawType()
} }
} }
} }
} }
*/ return typ
return v
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------

View File

@@ -89,9 +89,11 @@ func Merge(alt, pkg *types.Package, skips map[string]struct{}, skipall bool) {
scope := *alt.Scope() scope := *alt.Scope()
old := getElems(&scope) old := getElems(&scope)
elems := make(map[string]types.Object, len(old)) elems := make(map[string]types.Object, len(old))
for name, o := range old { for name, o := range old {
elems[name] = o elems[name] = o
} }
setElems(&scope, elems) setElems(&scope, elems)
setScope(alt, &scope) setScope(alt, &scope)

View File

@@ -315,6 +315,9 @@ func lastParamType(prog Program, fn Expr) Type {
func (p Package) abiTypeInit(g Global, t types.Type, pub bool) { func (p Package) abiTypeInit(g Global, t types.Type, pub bool) {
b := p.afterBuilder() b := p.afterBuilder()
if p.patch != nil {
t = p.patch(t)
}
tabi := b.abiTypeOf(t) tabi := b.abiTypeOf(t)
expr := g.Expr expr := g.Expr
var eq Expr var eq Expr

View File

@@ -568,6 +568,7 @@ type aPackage struct {
strs map[string]llvm.Value strs map[string]llvm.Value
named map[types.Type]Expr named map[types.Type]Expr
afterb unsafe.Pointer afterb unsafe.Pointer
patch func(types.Type) types.Type
iRoutine int iRoutine int
} }
@@ -635,6 +636,11 @@ func (p Package) String() string {
return p.mod.String() return p.mod.String()
} }
// SetPatch sets a patch function.
func (p Package) SetPatch(fn func(types.Type) types.Type) {
p.patch = fn
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
func (p Package) afterBuilder() Builder { func (p Package) afterBuilder() Builder {