Problems with __complex__ in C++

Gabriel Dos Reis gdr@codesourcery.com
Mon Apr 24 11:23:00 GMT 2000


Branko =?iso-8859-2?Q?=C8ibej?= <branko.cibej@hermes.si> writes:

| Hi,
| 
| I've been trying to track down a bug in g++ that shows up when I try
| to compile libstdc++-v3 on sparc-sun-solaris2.6. The root of the
| problem seems to be that implicit conversions between different
| complex types are not implemented in C++.

Your analysis is correct.  Currently, G++'s *effective* support for
__complex__ is near to inexistent :-(

| I managed to reproduce the problem with the testcase below. It compiles
| as C, but fails as C++.
| 
| Unfortunately I can't find my way around the sources too well, so
| my debugging efforts haven't been much use yet. If somewone could
| give me a few pointers, I could try to fix the problem myself.
| In the meantime, here's the testcase and compilation logs:
| 
| 
|     int main (void)
|     {
|         static __complex__ float x;
|         static __complex__ double y;
|         x = y;
|         y = x;
|         return 0;
|     }

The patch attached below makes G++ accept conversion between objects
of complex types.

What it does not is to accept complex object intialization with scalar
expression:

    __complex__ double z = 1.0; // still rejected.

OK to apply?

-- Gaby
CodeSourcery, LLC                             http://www.codesourcery.com


2000-04-24  Gabriel Dos Reis  <gdr@codesourcery.com>

	* call.c (standard_conversion): Accept conversion between
	COMPLEX_TYPEs 

	* cvt.c (ocp_convert): Handle conversion to COMPLEX_TYPE

Index: call.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/call.c,v
retrieving revision 1.211
diff -p -r1.211 call.c
*** call.c	2000/04/20 05:53:57	1.211
--- call.c	2000/04/24 18:03:18
*************** standard_conversion (to, from, expr)
*** 679,684 ****
--- 679,704 ----
    else if (fromref || (expr && real_lvalue_p (expr)))
      conv = build_conv (RVALUE_CONV, from, conv);
  
+   /* Allow conversion between `__complex__' data types  */
+   if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
+     {
+       /* The standard conversion sequence to convert FROM to TO is the
+          standard conversion sequence to perform componentwise
+          conversion.  */
+       tree part_conv = standard_conversion
+         (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE);
+       
+       if (part_conv)
+         {
+           conv = build_conv (TREE_CODE (part_conv), to, conv);
+           ICS_STD_RANK (conv) = ICS_STD_RANK (part_conv);
+         }
+       else
+         conv = NULL_TREE;
+       
+       return conv;
+     }
+ 
    if (same_type_p (from, to))
      return conv;
  
Index: cvt.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/cvt.c,v
retrieving revision 1.81
diff -p -r1.81 cvt.c
*** cvt.c	2000/04/11 00:38:37	1.81
--- cvt.c	2000/04/24 18:03:19
*************** ocp_convert (type, expr, convtype, flags
*** 692,697 ****
--- 692,701 ----
  	   that can result in infinite recursion; fold will call
  	   convert, which will call ocp_convert, etc.  */
  	return e;
+       /* For complex data types, we need to perform componentwise
+          conversion.  */
+       else if (TREE_CODE (type) == COMPLEX_TYPE)
+         return fold (convert_to_complex (type, e));
        else
  	return fold (build1 (NOP_EXPR, type, e));
      }


More information about the Gcc-patches mailing list