ref: cd3218bd460b20238d67eea3c4518c8b47a41285
parent: 720cc29f19477550800adf5539837fec15296bbc
author: Ori Bernstein <ori@eigenstate.org>
date: Sun Jan 21 11:47:19 EST 2018
Allow using the '==' operator on enum-style union.
--- a/mi/flatten.c
+++ b/mi/flatten.c
@@ -351,7 +351,22 @@
static Node *
comparecomplex(Flattenctx *s, Node *n, Op op)
{
- fatal(n, "Complex comparisons not yet supported\n");
+ Type *ty;
+ Node *l, *r, *e;
+
+ /* special case: unions with nullary constructors can be compared easily. */
+ ty = tybase(exprtype(n->expr.args[0]));
+ if (ty->type == Tyunion && isenum(ty)) {
+ l = mkexpr(n->loc, Outag, rval(s, n->expr.args[0]), NULL);
+ r = mkexpr(n->loc, Outag, rval(s, n->expr.args[1]), NULL);
+ l->expr.type = mktype(n->loc, Tyuint32);
+ r->expr.type = mktype(n->loc, Tyuint32);
+ e = mkexpr(n->loc, Oueq, l, r, NULL);
+ e->expr.type = mktype(n->loc, Tybool);
+ return e;
+ }
+ fatal(n, "cannot compare values of type %s for equality\n",
+ tystr(exprtype(n->expr.args[0]));
return NULL;
}