mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-04 11:07:46 +00:00
compiler: implement comparing interfaces to nil
Comparing an interface to nil is easy, as the dynamic type is also nil. Comparing the dynamic values (when the dynamic types match) is much harder and depends on reflection capabilities, so is not yet implemented.
This commit is contained in:
+11
@@ -2219,6 +2219,17 @@ func (c *Compiler) parseBinOp(frame *Frame, binop *ssa.BinOp) (llvm.Value, error
|
||||
} else {
|
||||
return llvm.Value{}, errors.New("todo: unknown basic type in binop: " + typ.String())
|
||||
}
|
||||
case *types.Interface:
|
||||
switch binop.Op {
|
||||
case token.EQL, token.NEQ: // ==, !=
|
||||
result := c.builder.CreateCall(c.mod.NamedFunction("runtime.interfaceEqual"), []llvm.Value{x, y}, "")
|
||||
if binop.Op == token.NEQ {
|
||||
result = c.builder.CreateNot(result, "")
|
||||
}
|
||||
return result, nil
|
||||
default:
|
||||
return llvm.Value{}, errors.New("binop on interface: " + binop.Op.String())
|
||||
}
|
||||
case *types.Pointer:
|
||||
switch binop.Op {
|
||||
case token.EQL: // ==
|
||||
|
||||
@@ -58,3 +58,17 @@ func interfaceMethod(itf _interface, method uint16) *uint8 {
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
// Return true iff both interfaces are equal.
|
||||
func interfaceEqual(x, y _interface) bool {
|
||||
if x.typecode != y.typecode {
|
||||
// Different dynamic type so always unequal.
|
||||
return false
|
||||
}
|
||||
if x.typecode == 0 {
|
||||
// Both interfaces are nil, so they are equal.
|
||||
return true
|
||||
}
|
||||
// TODO: depends on reflection.
|
||||
panic("unimplemented: interface equality")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user