From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id 955543A1B40C for ; Fri, 13 Nov 2020 08:16:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 955543A1B40C Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 53F06142F for ; Fri, 13 Nov 2020 00:16:27 -0800 (PST) Received: from localhost (e121540-lin.manchester.arm.com [10.32.98.126]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id F0A863F718 for ; Fri, 13 Nov 2020 00:16:26 -0800 (PST) From: Richard Sandiford To: gcc-patches@gcc.gnu.org Mail-Followup-To: gcc-patches@gcc.gnu.org, richard.sandiford@arm.com Subject: [10/23] Tweak the way that is_a is implemented References: Date: Fri, 13 Nov 2020 08:16:25 +0000 In-Reply-To: (Richard Sandiford's message of "Fri, 13 Nov 2020 08:10:54 +0000") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 13 Nov 2020 08:16:29 -0000 At the moment, class hierarchies that use is_a are expected to define specialisations like: template <> template <> inline bool is_a_helper ::test (symtab_node *p) { return p->type =3D=3D SYMTAB_FUNCTION; } But this doesn't scale well to larger hierarchies, because it only defines ::test for an argument that is exactly =E2=80=9Csymtab_node *=E2=80= =9D (and not for example =E2=80=9Cconst symtab_node *=E2=80=9D or something that comes between cgraph_node and symtab_node in the hierarchy). For example: struct A { int x; }; struct B : A {}; struct C : B {}; template <> template <> inline bool is_a_helper ::test (A *a) { return a->x =3D=3D 1; } bool f(B *b) { return is_a (b); } gives: warning: inline function =E2=80=98static bool is_a_helper::test(U*) [w= ith U =3D B; T =3D C*]=E2=80=99 used but never defined and: bool f(const A *a) { return is_a (a); } gives: warning: inline function =E2=80=98static bool is_a_helper::test(U*) [w= ith U =3D const A; T =3D const C*]=E2=80=99 used but never defined This patch instead allows is_a to be implemented by specialising is_a_helper as a whole, for example: template<> struct is_a_helper : static_is_a_helper { static inline bool test (const A *a) { return a->x =3D=3D 1; } }; It also adds a general specialisation of is_a_helper for const pointers. Together, this makes both of the above examples work. gcc/ * is-a.h (reinterpret_is_a_helper): New class. (static_is_a_helper): Likewise. (is_a_helper): Inherit from reinterpret_is_a_helper. (is_a_helper): New specialization. --- gcc/is-a.h | 81 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 63 insertions(+), 18 deletions(-) diff --git a/gcc/is-a.h b/gcc/is-a.h index e84c3e4880c..26f53a5ba4a 100644 --- a/gcc/is-a.h +++ b/gcc/is-a.h @@ -116,9 +116,30 @@ the connection between the types has not been made. S= ee below. =20 EXTENDING THE GENERIC TYPE FACILITY =20 -Each connection between types must be made by defining a specialization of= the -template member function 'test' of the template class 'is_a_helper'. For -example, +Method 1 +-------- + +If DERIVED is derived from BASE, and if BASE contains enough information +to determine whether an object is actually an instance of DERIVED, +then you can make the above routines work for DERIVED by defining +a specialization of is_a_helper such as: + + template<> + struct is_a_helper : static_is_a_helper + { + static inline bool test (const BASE *p) { return ...; } + }; + +This test function should return true if P is an instanced of DERIVED. +This on its own is enough; the comments below for method 2 do not apply. + +Method 2 +-------- + +Alternatively, if two types are connected in ways other than C++ +inheritance, each connection between them must be made by defining a +specialization of the template member function 'test' of the template +class 'is_a_helper'. For example, =20 template <> template <> @@ -145,15 +166,52 @@ when needed may result in a crash. For example, #ifndef GCC_IS_A_H #define GCC_IS_A_H =20 +/* A base class that specializations of is_a_helper can use if casting + U * to T is simply a reinterpret_cast. */ + +template +struct reinterpret_is_a_helper +{ + template + static inline T cast (U *p) { return reinterpret_cast (p); } +}; + +/* A base class that specializations of is_a_helper can use if casting + U * to T is simply a static_cast. This is more type-safe than + reinterpret_is_a_helper. */ + +template +struct static_is_a_helper +{ + template + static inline T cast (U *p) { return static_cast (p); } +}; + /* A generic type conversion internal helper class. */ =20 template -struct is_a_helper +struct is_a_helper : reinterpret_is_a_helper { template static inline bool test (U *p); +}; + +/* Reuse the definition of is_a_helper to implement + is_a_helper. */ + +template +struct is_a_helper +{ template - static inline T cast (U *p); + static inline const T *cast (const U *p) + { + return is_a_helper::cast (const_cast (p)); + } + template + static inline bool test (const U *p) + { + return is_a_helper::test (p); + } }; =20 /* Note that we deliberately do not define the 'test' member template. Not @@ -161,19 +219,6 @@ struct is_a_helper not been defined, rather than a run-time error. See the discussion abo= ve for when to define this member. */ =20 -/* This is the generic implementation for casting from one type to another. - Do not use this routine directly; it is an internal function. See the - discussion above for when to define this member. */ - -template -template -inline T -is_a_helper ::cast (U *p) -{ - return reinterpret_cast (p); -} - - /* The public interface. */ =20 /* A generic test for a type relationship. See the discussion above for w= hen --=20 2.17.1