1
0
mirror of https://github.com/golang/go synced 2024-11-20 06:04:52 -07:00

gc: simplify complex typecheck

do not convert to float prematurely.

R=ken2
CC=golang-dev
https://golang.org/cl/311041
This commit is contained in:
Russ Cox 2010-03-08 15:44:18 -08:00
parent 4f89dcdf99
commit 4ac011a011
3 changed files with 37 additions and 31 deletions

View File

@ -888,6 +888,9 @@ nodcplxlit(Val r, Val i)
Node *n; Node *n;
Mpcplx *c; Mpcplx *c;
r = toflt(r);
i = toflt(i);
c = mal(sizeof(*c)); c = mal(sizeof(*c));
n = nod(OLITERAL, N, N); n = nod(OLITERAL, N, N);
n->type = types[TIDEAL]; n->type = types[TIDEAL];

View File

@ -309,6 +309,26 @@ exprfmt(Fmt *f, Node *n, int prec)
fmtprint(f, ")"); fmtprint(f, ")");
break; break;
case OCMPLX:
fmtprint(f, "cmplx(");
exprfmt(f, n->left, 0);
fmtprint(f, ", ");
exprfmt(f, n->right, 0);
fmtprint(f, ")");
break;
case OREAL:
fmtprint(f, "real(");
exprfmt(f, n->left, 0);
fmtprint(f, ")");
break;
case OIMAG:
fmtprint(f, "imag(");
exprfmt(f, n->left, 0);
fmtprint(f, ")");
break;
case OCONV: case OCONV:
case OCONVNOP: case OCONVNOP:
case OCONVSLICE: case OCONVSLICE:

View File

@ -788,51 +788,34 @@ reswitch:
if(l->type == T || r->type == T) if(l->type == T || r->type == T)
goto error; goto error;
defaultlit2(&l, &r, 0); defaultlit2(&l, &r, 0);
if(l->op == OLITERAL && r->op == OLITERAL) {
// make it a complex literal
switch(l->type->etype) {
default:
yyerror("real and imag parts must be the floating");
goto error;
case TIDEAL:
convlit(&l, types[TFLOAT]);
convlit(&r, types[TFLOAT]);
t = types[TIDEAL];
// fallthrough
case TFLOAT:
t = types[TCOMPLEX];
break;
case TFLOAT32:
t = types[TCOMPLEX64];
break;
case TFLOAT64:
t = types[TCOMPLEX128];
break;
}
n = nodcplxlit(l->val, r->val);
n->type = t;
goto ret;
}
n->left = l; n->left = l;
n->right = r; n->right = r;
if(l->type->etype != l->type->etype) { if(l->type->etype != l->type->etype) {
yyerror("real and imag parts must be the same type"); badcmplx:
yyerror("invalid operation: %#N (cmplx of types %T, %T)", n, l->type, r->type);
goto error; goto error;
} }
switch(l->type->etype) { switch(l->type->etype) {
default: default:
yyerror("real and imag parts must be the floating"); goto badcmplx;
goto error; case TIDEAL:
t = types[TIDEAL];
break;
case TFLOAT: case TFLOAT:
n->type = types[TCOMPLEX]; t = types[TCOMPLEX];
break; break;
case TFLOAT32: case TFLOAT32:
n->type = types[TCOMPLEX64]; t = types[TCOMPLEX64];
break; break;
case TFLOAT64: case TFLOAT64:
n->type = types[TCOMPLEX128]; t = types[TCOMPLEX128];
break; break;
} }
if(l->op == OLITERAL && r->op == OLITERAL) {
// make it a complex literal
n = nodcplxlit(l->val, r->val);
}
n->type = t;
goto ret; goto ret;
case OCLOSED: case OCLOSED: