shithub: scc

Download patch

ref: 839b28355e9bc831c2a05947925111af79a5bc0f
parent: 28e44b2545e3c82482006f86c48f36e0786ddc7b
author: Roberto E. Vargas Caballero <[email protected]>
date: Tue Apr 22 02:58:33 EDT 2014

Fix pointer conversions in convert()

Float cannot be converted to pointers, and integer only
in the case of being casted. This new code has some problems
with integer 0, that must be allowed mixed with pointer
expressions, but it will be fixed later.

--- a/expr.c
+++ b/expr.c
@@ -82,26 +82,38 @@
 Node *
 convert(Node *np, Type *tp, char iscast)
 {
-	uint8_t t1, t2;
+	Type *utp;
+	uint8_t t;
 
 	if (np->type == tp)
 		return np;
-	t1 = np->typeop, t2 = BTYPE(tp);
 
-	switch (t1) {
+	utp = UNQUAL(tp);
+	t = utp->op;
+
+	switch (np->typeop) {
 	case ENUM: case INT: case FLOAT:
-		switch (t2) {
+		switch (t) {
+		case PTR:
+			if (!iscast || np->typeop == FLOAT)
+				return NULL;
 		case INT: case FLOAT: case ENUM:
-			return castcode(np, tp);
+			break;
+		default:
+			return NULL;
 		}
 		break;
 	case PTR:
-		switch (t2) {
+		switch (t) {
+		case ENUM: case INT: /* TODO: allow p = 0 */
+			if (!iscast)
+				return NULL;;
+			break;
 		case ARY: case FTN:
 			np = addr2ptr(np);
 		case PTR:
-			if (iscast && t2 != FLOAT ||
-			    tp == pvoidtype ||
+			if (iscast ||
+			    utp == pvoidtype ||
 			    np->utype == pvoidtype) {
 				/* TODO:
 				 * we assume conversion between pointers
@@ -109,12 +121,16 @@
 				 * alignment problems that may be false
 				 */
 				np->type = tp;
-				np->utype = UNQUAL(tp);
+				np->utype = utp;
 				return np;
 			}
+		default:
+			return NULL;
 		}
+	default:
+			return NULL;
 	}
-	return NULL;
+	return castcode(np, tp);
 }
 
 static Node *