]> Cypherpunks repositories - gostls13.git/commitdiff
gc: simplify complex typecheck
authorRuss Cox <rsc@golang.org>
Mon, 8 Mar 2010 23:44:18 +0000 (15:44 -0800)
committerRuss Cox <rsc@golang.org>
Mon, 8 Mar 2010 23:44:18 +0000 (15:44 -0800)
do not convert to float prematurely.

R=ken2
CC=golang-dev
https://golang.org/cl/311041

src/cmd/gc/const.c
src/cmd/gc/print.c
src/cmd/gc/typecheck.c

index 5a5a05966859c51932238debcc36dc0a8f8ea7f7..7debb370839f3acc21e6d0307abe89454a45c462 100644 (file)
@@ -888,6 +888,9 @@ nodcplxlit(Val r, Val i)
        Node *n;
        Mpcplx *c;
 
+       r = toflt(r);
+       i = toflt(i);
+
        c = mal(sizeof(*c));
        n = nod(OLITERAL, N, N);
        n->type = types[TIDEAL];
index b460953a2af1e23636df993de8f6ee8c80d1a63b..af69202838c2f6327d55770fbeecef821a92add2 100644 (file)
@@ -309,6 +309,26 @@ exprfmt(Fmt *f, Node *n, int prec)
                fmtprint(f, ")");
                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 OCONVNOP:
        case OCONVSLICE:
index e7db038bf6afb2074ee53437a824dccf42dc857d..654e72b5f725a4628a83e3c867a7dc23c2b48501 100644 (file)
@@ -788,51 +788,34 @@ reswitch:
                if(l->type == T || r->type == T)
                        goto error;
                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->right = r;
                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;
                }
                switch(l->type->etype) {
                default:
-                       yyerror("real and imag parts must be the floating");
-                       goto error;
+                       goto badcmplx;
+               case TIDEAL:
+                       t = types[TIDEAL];
+                       break;
                case TFLOAT:
-                       n->type = types[TCOMPLEX];
+                       t = types[TCOMPLEX];
                        break;
                case TFLOAT32:
-                       n->type = types[TCOMPLEX64];
+                       t = types[TCOMPLEX64];
                        break;
                case TFLOAT64:
-                       n->type = types[TCOMPLEX128];
+                       t = types[TCOMPLEX128];
                        break;
                }
+               if(l->op == OLITERAL && r->op == OLITERAL) {
+                       // make it a complex literal
+                       n = nodcplxlit(l->val, r->val);
+               }
+               n->type = t;
                goto ret;
 
        case OCLOSED: