RFC: folding of literal array refs

Dirk Mueller dmueller@suse.de
Thu Apr 27 14:04:00 GMT 2006


Hi, 

recently I noticed that code like 

 if ("foo"[0] == 'g')
   return 1;
 return 0;

is properly const folded, while 

 if (((const char*)"foo")[0] == 'g')
   return 1;
 return 0;

is not. This is because the types don't match. the first variant has a 
"readonly" in the type node, the 2nd one doesn't. 


the patch below fixes this, but I'm unsure if its the most correct approach 
(no regtest failures): 

--- fold-const.c        (revision 113296)
+++ fold-const.c        (working copy)
@@ -12220,7 +12220,8 @@
        }
 
       if (string
-         && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (string))
+         && lang_hooks.types_compatible_p (TREE_TYPE (exp), 
+               TREE_TYPE (TREE_TYPE (string)))
          && TREE_CODE (string) == STRING_CST
          && TREE_CODE (index) == INTEGER_CST
          && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0


Comments?

Thanks,
Dirk



More information about the Gcc-patches mailing list