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:
return b.Load(x)
case token.SUB:
switch t := x.Type.raw.Underlying().(type) {
switch t := x.raw.Type.Underlying().(type) {
case *types.Basic:
ret.Type = x.Type
if t.Info()&types.IsInteger != 0 {

View File

@@ -41,7 +41,7 @@ func (b Builder) abiStruct(t *types.Struct) Expr {
g := pkg.VarOf(name)
if g == nil {
prog := b.Prog
g := pkg.doNewVar(name, prog.AbiTypePtrPtr())
g = pkg.doNewVar(name, prog.AbiTypePtrPtr())
g.Init(prog.Null(g.Type))
}
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)
eq := b.BinOp(token.EQL, tx, tabi)
if commaOk {
/*
prog := b.Prog
t := prog.Tuple(assertedTyp, prog.Bool())
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)
valFalse := aggregateValue(b.impl, t.ll, zero.impl, prog.BoolVal(false).impl)
return Expr{llvm.CreateSelect(b.impl, eq.impl, valTrue, valFalse), t}
*/
panic("todo")
}
blks := b.Func.MakeBlocks(2)
b.If(eq, blks[0], blks[1])

View File

@@ -291,13 +291,13 @@ func (p Program) NewPackage(name, pkgPath string) Package {
ret := &aPackage{
mod: mod, vars: gbls, fns: fns, stubs: stubs,
pyobjs: pyobjs, pymods: pymods, Prog: p}
ret.abi.Init(pkgPath)
return ret
}
// Tuple returns a tuple type.
func (p Program) Tuple(typs ...Type) Type {
n := len(typs)
els := make([]*types.Var, n)
els := make([]*types.Var, len(typs))
for i, t := range typs {
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 {
types.Type
Type types.Type
}
type aType struct {
@@ -285,12 +285,14 @@ func (p Program) toType(raw types.Type) Type {
return p.toNamed(t)
case *types.Signature: // represents a C function pointer in raw type
return &aType{p.toLLVMFuncPtr(t), typ, vkFuncPtr}
case *types.Tuple:
return &aType{p.toLLVMTuple(t), typ, vkTuple}
case *types.Array:
elem := p.rawType(t.Elem())
return &aType{llvm.ArrayType(elem.ll, int(t.Len())), typ, vkArray}
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 {