C++ PATCH for array-to-pointer conversion

Mark Mitchell mark@markmitchell.com
Fri Feb 26 12:15:00 GMT 1999


Here's a case where we mishandled the array-to-pointer conversion,
trying to be a little bit too clever.

-- 
Mark Mitchell 			mark@markmitchell.com
Mark Mitchell Consulting	http://www.markmitchell.com

Fri Feb 26 12:15:45 1999  Mark Mitchell  <mark@markmitchell.com>

	* typeck.c (decay_conversion): Don't confuse constant array
	variables with their intiailizers.

Index: testsuite/g++.old-deja/g++.other/string1.C
===================================================================
RCS file: string1.C
diff -N string1.C
*** /dev/null	Sat Dec  5 20:30:03 1998
--- string1.C	Fri Feb 26 12:12:45 1999
***************
*** 0 ****
--- 1,20 ----
+ // Build don't link:
+ // Origin: mrs@wrs.com (Mike Stump)
+ 
+ class Wrapper {
+ public:
+   static const char msgPtr[];
+   static const char *JunkFunc() {
+     return &msgPtr[0];
+   }
+ };
+  
+ const char Wrapper::msgPtr[] = "Hello world.";
+  
+ int main() {
+   const char *p1 = &Wrapper::msgPtr[0];
+   const char *p2 = Wrapper::JunkFunc();
+  
+   if (p1 != p2)
+     return 1;
+ }
Index: cp/typeck.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/typeck.c,v
retrieving revision 1.139
diff -c -p -r1.139 typeck.c
*** typeck.c	1999/02/21 16:38:23	1.139
--- typeck.c	1999/02/26 20:12:50
*************** c_alignof (type)
*** 1589,1600 ****
    return t;
  }
  
! /* Perform default promotions for C data used in expressions.
!    Arrays and functions are converted to pointers;
!    enumeral types or short or char, to int.
!    In addition, manifest constants symbols are replaced by their values.
  
!    C++: this will automatically bash references to their target type.  */
  
  tree
  decay_conversion (exp)
--- 1589,1599 ----
    return t;
  }
  
! /* Perform the array-to-pointer and function-to-pointer conversions
!    for EXP.  
  
!    In addition, references are converted to rvalues and manifest
!    constants are replaced by their values.  */
  
  tree
  decay_conversion (exp)
*************** decay_conversion (exp)
*** 1628,1635 ****
    /* Constants can be used directly unless they're not loadable.  */
    if (TREE_CODE (exp) == CONST_DECL)
      exp = DECL_INITIAL (exp);
!   /* Replace a nonvolatile const static variable with its value.  */
!   else if (TREE_READONLY_DECL_P (exp))
      {
        exp = decl_constant_value (exp);
        type = TREE_TYPE (exp);
--- 1627,1641 ----
    /* Constants can be used directly unless they're not loadable.  */
    if (TREE_CODE (exp) == CONST_DECL)
      exp = DECL_INITIAL (exp);
!   /* Replace a nonvolatile const static variable with its value.  We
!      don't do this for arrays, though; we want the address of the
!      first element of the array, not the address of the first element
!      of its initializing constant.  We *do* replace variables that the
!      user isn't really supposed to know about; this is a hack to deal
!      with __PRETTY_FUNCTION__ and the like.  */
!   else if (TREE_READONLY_DECL_P (exp)
! 	   && (code != ARRAY_TYPE 
! 	       || (TREE_CODE (exp) == VAR_DECL && DECL_IGNORED_P (exp))))
      {
        exp = decl_constant_value (exp);
        type = TREE_TYPE (exp);
*************** decay_conversion (exp)
*** 1649,1657 ****
        return build_unary_op (ADDR_EXPR, exp, 0);
      }
    if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
!     {
!       return build_unary_op (ADDR_EXPR, exp, 0);
!     }
    if (code == ARRAY_TYPE)
      {
        register tree adr;
--- 1655,1661 ----
        return build_unary_op (ADDR_EXPR, exp, 0);
      }
    if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
!     return build_unary_op (ADDR_EXPR, exp, 0);
    if (code == ARRAY_TYPE)
      {
        register tree adr;


More information about the Gcc-patches mailing list