fix types named recursive

This commit is contained in:
visualfc
2024-05-11 12:03:02 +08:00
parent 2589c23998
commit 92827a1f04
5 changed files with 201 additions and 6 deletions

View File

@@ -106,6 +106,7 @@ type aProgram struct {
target *Target
td llvm.TargetData
// tm llvm.TargetMachine
named map[string]llvm.Type
intType llvm.Type
int1Type llvm.Type
@@ -154,7 +155,7 @@ func NewProgram(target *Target) Program {
// TODO(xsw): Finalize may cause panic, so comment it.
ctx.Finalize()
*/
return &aProgram{ctx: ctx, gocvt: newGoTypes(), target: target, td: td}
return &aProgram{ctx: ctx, gocvt: newGoTypes(), target: target, td: td, named: make(map[string]llvm.Type)}
}
// SetRuntime sets the runtime.

View File

@@ -255,7 +255,11 @@ func (p Program) toType(raw types.Type) Type {
}
func (p Program) toLLVMNamedStruct(name string, raw *types.Struct) llvm.Type {
if typ, ok := p.named[name]; ok {
return typ
}
t := p.ctx.StructCreateNamed(name)
p.named[name] = t
fields := p.toLLVMFields(raw)
t.StructSetBody(fields, false)
return t

View File

@@ -26,12 +26,14 @@ import (
// -----------------------------------------------------------------------------
type goTypes struct {
typs map[unsafe.Pointer]unsafe.Pointer
typs map[unsafe.Pointer]unsafe.Pointer
named map[string]*types.Named
}
func newGoTypes() goTypes {
typs := make(map[unsafe.Pointer]unsafe.Pointer)
return goTypes{typs}
named := make(map[string]*types.Named)
return goTypes{typs, named}
}
type Background int
@@ -115,10 +117,16 @@ func (p goTypes) cvtNamed(t *types.Named) (raw *types.Named, cvt bool) {
defer func() {
p.typs[unsafe.Pointer(t)] = unsafe.Pointer(raw)
}()
id := t.String()
if named, ok := p.named[id]; ok {
return named, false
}
named := types.NewNamed(t.Obj(), types.Typ[types.Int], nil)
p.named[id] = named
defer delete(p.named, id)
if tund, cvt := p.cvtType(t.Underlying()); cvt {
old := t.Obj()
obj := types.NewTypeName(old.Pos(), old.Pkg(), old.Name(), nil)
return types.NewNamed(obj, tund, nil), true
named.SetUnderlying(tund)
return named, true
}
return t, false
}