shithub: scc

Download patch

ref: c9a7bedda831d58121897c43f3e92badb6852f3a
parent: b08f92985bfbf55d321c4942b3742823515c2ba0
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Apr 18 06:03:47 EDT 2014

Convert expressions of ternary operator

In a ternary operator the two result expressions must be of the
same type, or in other case they must be converted to the same type.

--- a/expr.c
+++ b/expr.c
@@ -692,6 +692,7 @@
 ternary(void)
 {
 	Node *cond, *ifyes, *ifno;
+	Type *tp1, *tp2;
 
 	cond = or();
 	while (accept('?')) {
@@ -698,7 +699,10 @@
 		ifyes = promote(expr());
 		expect(':');
 		ifno = promote(ternary());
-		typeconv(&ifyes, &ifno);
+		tp1 = UNQUAL(ifyes->type);
+		tp2 = UNQUAL(ifno->type);
+		if (tp1 != tp2)
+			typeconv(&ifyes, &ifno);
 		cond = ternarycode(compare(ONE, cond, constcode(zero)),
 	                           ifyes, ifno);
 	}
--