scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 839b28355e9bc831c2a05947925111af79a5bc0f
parent 28e44b2545e3c82482006f86c48f36e0786ddc7b
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 22 Apr 2014 06:58:33 +0200

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.

Diffstat:
Mexpr.c | 36++++++++++++++++++++++++++----------
1 file changed, 26 insertions(+), 10 deletions(-)

diff --git a/expr.c b/expr.c @@ -82,26 +82,38 @@ addr2ptr(Node *np) 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 @@ convert(Node *np, Type *tp, char iscast) * 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 *