[RFC] is-a.h supporting casting from non const to const

Andrew Pinski pinskia@gmail.com
Sun Nov 23 23:21:00 GMT 2014


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
>



More information about the Gcc mailing list