TypeAssert refactor

This commit is contained in:
xushiwei
2024-05-23 01:34:48 +08:00
parent a4c4324ba3
commit 4986592dd7
4 changed files with 15 additions and 16 deletions

View File

@@ -425,7 +425,7 @@ func (b Builder) UnOp(op token.Token, x Expr) (ret Expr) {
case token.MUL: case token.MUL:
return b.Load(x) return b.Load(x)
case token.SUB: case token.SUB:
switch t := x.Type.raw.Underlying().(type) { switch t := x.raw.Type.Underlying().(type) {
case *types.Basic: case *types.Basic:
ret.Type = x.Type ret.Type = x.Type
if t.Info()&types.IsInteger != 0 { if t.Info()&types.IsInteger != 0 {

View File

@@ -41,7 +41,7 @@ func (b Builder) abiStruct(t *types.Struct) Expr {
g := pkg.VarOf(name) g := pkg.VarOf(name)
if g == nil { if g == nil {
prog := b.Prog prog := b.Prog
g := pkg.doNewVar(name, prog.AbiTypePtrPtr()) g = pkg.doNewVar(name, prog.AbiTypePtrPtr())
g.Init(prog.Null(g.Type)) g.Init(prog.Null(g.Type))
} }
pkg.abitys = append(pkg.abitys, func() { pkg.abitys = append(pkg.abitys, func() {
@@ -293,7 +293,6 @@ func (b Builder) TypeAssert(x Expr, assertedTyp Type, commaOk bool) Expr {
tabi := b.abiType(assertedTyp.raw.Type) tabi := b.abiType(assertedTyp.raw.Type)
eq := b.BinOp(token.EQL, tx, tabi) eq := b.BinOp(token.EQL, tx, tabi)
if commaOk { if commaOk {
/*
prog := b.Prog prog := b.Prog
t := prog.Tuple(assertedTyp, prog.Bool()) t := prog.Tuple(assertedTyp, prog.Bool())
val := b.valFromData(assertedTyp, b.InterfaceData(x)) val := b.valFromData(assertedTyp, b.InterfaceData(x))
@@ -301,8 +300,6 @@ func (b Builder) TypeAssert(x Expr, assertedTyp Type, commaOk bool) Expr {
valTrue := aggregateValue(b.impl, t.ll, val.impl, prog.BoolVal(true).impl) valTrue := aggregateValue(b.impl, t.ll, val.impl, prog.BoolVal(true).impl)
valFalse := aggregateValue(b.impl, t.ll, zero.impl, prog.BoolVal(false).impl) valFalse := aggregateValue(b.impl, t.ll, zero.impl, prog.BoolVal(false).impl)
return Expr{llvm.CreateSelect(b.impl, eq.impl, valTrue, valFalse), t} return Expr{llvm.CreateSelect(b.impl, eq.impl, valTrue, valFalse), t}
*/
panic("todo")
} }
blks := b.Func.MakeBlocks(2) blks := b.Func.MakeBlocks(2)
b.If(eq, blks[0], blks[1]) b.If(eq, blks[0], blks[1])

View File

@@ -291,13 +291,13 @@ func (p Program) NewPackage(name, pkgPath string) Package {
ret := &aPackage{ ret := &aPackage{
mod: mod, vars: gbls, fns: fns, stubs: stubs, mod: mod, vars: gbls, fns: fns, stubs: stubs,
pyobjs: pyobjs, pymods: pymods, Prog: p} pyobjs: pyobjs, pymods: pymods, Prog: p}
ret.abi.Init(pkgPath)
return ret return ret
} }
// Tuple returns a tuple type. // Tuple returns a tuple type.
func (p Program) Tuple(typs ...Type) Type { func (p Program) Tuple(typs ...Type) Type {
n := len(typs) els := make([]*types.Var, len(typs))
els := make([]*types.Var, n)
for i, t := range typs { for i, t := range typs {
els[i] = types.NewParam(token.NoPos, nil, "", t.raw.Type) els[i] = types.NewParam(token.NoPos, nil, "", t.raw.Type)
} }

View File

@@ -75,7 +75,7 @@ func indexType(t types.Type) types.Type {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
type rawType struct { type rawType struct {
types.Type Type types.Type
} }
type aType struct { type aType struct {
@@ -285,12 +285,14 @@ func (p Program) toType(raw types.Type) Type {
return p.toNamed(t) return p.toNamed(t)
case *types.Signature: // represents a C function pointer in raw type case *types.Signature: // represents a C function pointer in raw type
return &aType{p.toLLVMFuncPtr(t), typ, vkFuncPtr} return &aType{p.toLLVMFuncPtr(t), typ, vkFuncPtr}
case *types.Tuple:
return &aType{p.toLLVMTuple(t), typ, vkTuple}
case *types.Array: case *types.Array:
elem := p.rawType(t.Elem()) elem := p.rawType(t.Elem())
return &aType{llvm.ArrayType(elem.ll, int(t.Len())), typ, vkArray} return &aType{llvm.ArrayType(elem.ll, int(t.Len())), typ, vkArray}
case *types.Chan: case *types.Chan:
} }
panic(fmt.Sprintf("toLLVMType: todo - %T\n", typ)) panic(fmt.Sprintf("toLLVMType: todo - %T\n", raw))
} }
func (p Program) toLLVMNamedStruct(name string, raw *types.Struct) llvm.Type { func (p Program) toLLVMNamedStruct(name string, raw *types.Struct) llvm.Type {