From 28f37f3a2278aba1652638c7642377ad442e54d7 Mon Sep 17 00:00:00 2001 From: Michael Forney Date: Sat, 20 Apr 2019 11:18:46 -0700 Subject: expr: Add some type checking for unary operators --- expr.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/expr.c b/expr.c index 165c7f8..3e2c140 100644 --- a/expr.c +++ b/expr.c @@ -674,17 +674,23 @@ unaryexpr(struct scope *s) case TADD: next(); e = castexpr(s); + if (!(e->type->prop & PROPARITH)) + error(&tok.loc, "operand of unary '+' operator must have arithmetic type"); e = exprconvert(e, typeintpromote(e->type)); break; case TSUB: next(); e = castexpr(s); + if (!(e->type->prop & PROPARITH)) + error(&tok.loc, "operand of unary '-' operator must have arithmetic type"); e = exprconvert(e, typeintpromote(e->type)); e = mkbinaryexpr(&tok.loc, TSUB, mkconstexpr(&typeint, 0), e); break; case TBNOT: next(); e = castexpr(s); + if (!(e->type->prop & PROPINT)) + error(&tok.loc, "operand of '~' operator must have integer type"); e = exprconvert(e, typeintpromote(e->type)); e = mkbinaryexpr(&tok.loc, TXOR, e, mkconstexpr(e->type, -1)); break; -- cgit v1.2.3