This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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.

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]