ssa: func Instantiate

This commit is contained in:
visualfc
2024-10-30 21:02:08 +08:00
parent ce87f293aa
commit 38091b2021
3 changed files with 37 additions and 22 deletions

View File

@@ -148,15 +148,7 @@ func (p goTypes) cvtNamed(t *types.Named) (raw *types.Named, cvt bool) {
p.typs[unsafe.Pointer(t)] = unsafe.Pointer(t)
if tund, cvt := p.cvtType(t.Underlying()); cvt {
named.SetUnderlying(tund)
if tp := t.TypeArgs(); tp != nil {
targs := make([]types.Type, tp.Len())
for i := 0; i < tp.Len(); i++ {
targs[i] = tp.At(i)
}
typ, err := types.Instantiate(nil, named, targs, true)
if err != nil {
panic(fmt.Errorf("cvtNamed error: %v", err))
}
if typ, ok := Instantiate(named, t); ok {
named = typ.(*types.Named)
}
p.typs[unsafe.Pointer(t)] = unsafe.Pointer(named)
@@ -165,6 +157,19 @@ func (p goTypes) cvtNamed(t *types.Named) (raw *types.Named, cvt bool) {
return t, false
}
func Instantiate(orig types.Type, t *types.Named) (types.Type, bool) {
if tp := t.TypeArgs(); tp != nil {
targs := make([]types.Type, tp.Len())
for i := 0; i < tp.Len(); i++ {
targs[i] = tp.At(i)
}
if typ, err := types.Instantiate(nil, orig, targs, true); err == nil {
return typ, true
}
}
return orig, false
}
func (p goTypes) cvtClosure(sig *types.Signature) *types.Struct {
ctx := types.NewParam(token.NoPos, nil, closureCtx, types.Typ[types.UnsafePointer])
raw := p.cvtFunc(sig, ctx)