[PATCH]: Change CONST_CAST macro to accept a TYPE argument

Richard Guenther richard.guenther@gmail.com
Sun Sep 2 12:58:00 GMT 2007


On 9/2/07, Kaveh R. GHAZI <ghazi@caip.rutgers.edu> wrote:
> This patch changes the CONST_CAST macro to accept a TYPE argument to align
> it better with C++'s const_cast<> operator as discussed here:
> http://gcc.gnu.org/ml/gcc-patches/2007-09/msg00032.html
>
> There are two minor cases that arose needing explanation:
>
> I wrote the general CONST_CAST(TYPE,X) macro to go from "const TYPE" to
> plain "TYPE".  This works for most cases.  However it doesn't work for
> typedef'ed pointers like tree or rtx.  I.e. "const tree" (with a space) is
> not the same as "const_tree", or I wouldn't have needed the new const_tree
> typedef in the first place.
>
> So I had to pass in the underlying type "union tree_node *" in order for
> prefixing it with "const" would work.  I wrote three helper macros for
> tree, rtx and basic_block to make it less ugly.
>
> Second issue, there was another case in gfortranspec.c that went outside
> the "const TYPE" to "TYPE" paradigm.  Instead it went from "const TYPE
> *const*" to "const TYPE **".  I created a generic macro accepting two type
> arguments to use in this case and defined everything in terms of that for
> all the other macros.  See the hunk in system.h and it should be clear.
>
> Bootstrapped on sparc-sun-solaris2.10, regtest in progress.
>
> Okay for mainline if it passes?

I like this a lot better than the old CONST_CAST and I don't mind
CONST_CAST_TREE and friends.

> +++ egcc-SVN20070901/gcc/tree.c 2007-09-01 19:20:12.803446055 -0400
> @@ -1194,8 +1194,8 @@ build_string (int len, const char *str)
>    TREE_CONSTANT (s) = 1;
>    TREE_INVARIANT (s) = 1;
>    TREE_STRING_LENGTH (s) = len;
> -  memcpy (CONST_CAST (TREE_STRING_POINTER (s)), str, len);
> -  ((char *) CONST_CAST (TREE_STRING_POINTER (s)))[len] = '\0';
> +  memcpy (CONST_CAST (char *, TREE_STRING_POINTER (s)), str, len);
> +  ((char *) CONST_CAST (char *, TREE_STRING_POINTER (s)))[len] = '\0';

I'm sure the cast to (char *) in the last line is not needed.

The patch is ok if it passes testing, but please wait 24h for others to
object.

Thanks,
Richard.



More information about the Gcc-patches mailing list