This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: [RFC] is-a.h supporting casting from non const to const
- From: Andrew Pinski <pinskia at gmail dot com>
- To: Trevor Saunders <tsaunders at mozilla dot com>
- Cc: GCC Mailing List <gcc at gcc dot gnu dot org>
- Date: Sun, 23 Nov 2014 15:12:06 -0800
- Subject: Re: [RFC] is-a.h supporting casting from non const to const
- Authentication-results: sourceware.org; auth=none
- References: <CA+=Sn1mM11FUwSk7hiX7oM45TKjCp7_a2c1AL3Rhw1ECSnzg7Q at mail dot gmail dot com> <20141123205151 dot GB12829 at tsaunders-iceball dot corp dot tor1 dot mozilla dot com>
On Sun, Nov 23, 2014 at 12:51 PM, Trevor Saunders <tsaunders@mozilla.com> wrote:
> On Sun, Nov 23, 2014 at 12:05:52PM -0800, Andrew Pinski wrote:
>> Hi,
>> While working on a patch to change some error to inform, I changed
>> the as_a cast in RTL_LOCATION to be as_a <const rtx_insn *>. This
>> falls down when you start with a rtx rather than a const_rtx..
>
> so, don't do that? if you have a const_rtx use as_a<const rtx_insn *>,
> otherwise as_a<rtx_insn *>. I can't think of a case where it is
> necessary to cast to const rtx_insn * from rtx, instead of letting the
> constification be implicit.
In a function, I already have a const_rtx and I want to use
RTL_LOCATION to get a location for the rtx. How should I handle this
case? In 4.9, it would just work as there was no casting needed but
in GCC 5 I need to cast away the const first before I could use
RTL_LOCATION.
RTL_LOCATION does not modify at all the rtx so it should be using
as_a<const rtx_insn *>. Should I just add a cast to const_rtx inside
the RTL_LOCATION macro before doing the as_a<const rtx_insn*>?
Thanks,
Andrew
>
> Trev
>
>>
>> I had to modify is-a.h to this but I really don't like the
>> modification at all since it shows the bigger issues with templates.
>> Can anyone think of a better way of supporting this? Attached is my
>> patch currently which is able to bootstrap with the modification to
>> RTL_LOCATION being there.
>>
>> Thanks,
>> Andrew Pinski
>
>> Index: is-a.h
>> ===================================================================
>> --- is-a.h (revision 217985)
>> +++ is-a.h (working copy)
>> @@ -147,10 +147,26 @@ struct is_a_helper
>> {
>> template <typename U>
>> static inline bool test (U *p);
>> + static inline bool test (T *) { return true; }
>> template <typename U>
>> static inline T cast (U *p);
>> };
>>
>> +
>> +template <typename T>
>> +struct is_a_helper<const T*>
>> +{
>> + template <typename U>
>> + static inline bool test (const U *p);
>> + template <typename U>
>> + static inline bool test (U *p) {return test(const_cast<const U*>(p));}
>> + static inline bool test (const T *) { return true; }
>> + template <typename U>
>> + static inline const T *cast (const U *p);
>> + template <typename U>
>> + static inline const T *cast (U *p) { return cast(const_cast<const U*>(p)); }
>> +};
>> +
>> /* Note that we deliberately do not define the 'test' member template. Not
>> doing so will result in a build-time error for type relationships that have
>> not been defined, rather than a run-time error. See the discussion above
>> @@ -169,6 +185,15 @@ is_a_helper <T>::cast (U *p)
>> }
>>
>>
>> +template <typename T>
>> +template <typename U>
>> +inline const T*
>> +is_a_helper <const T*>::cast (const U *p)
>> +{
>> + return reinterpret_cast <const T*> (p);
>> +}
>> +
>> +
>> /* The public interface. */
>>
>> /* A generic test for a type relationship. See the discussion above for when
>