From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 30509 invoked by alias); 6 Aug 2014 17:38:03 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 30445 invoked by uid 89); 6 Aug 2014 17:38:02 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.2 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.3.2 X-HELO: eggs.gnu.org Received: from eggs.gnu.org (HELO eggs.gnu.org) (208.118.235.92) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Wed, 06 Aug 2014 17:38:01 +0000 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XF4rj-0000Bd-8y for gcc-patches@gcc.gnu.org; Wed, 06 Aug 2014 13:19:22 -0400 Received: from mx1.redhat.com ([209.132.183.28]:3678) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XF4rj-0000BS-09 for gcc-patches@gcc.gnu.org; Wed, 06 Aug 2014 13:19:07 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s76HJ6lB007929 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 6 Aug 2014 13:19:06 -0400 Received: from c64.redhat.com (vpn-239-139.phx2.redhat.com [10.3.239.139]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s76HJ2nf030913; Wed, 6 Aug 2014 13:19:05 -0400 From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 005/236] Introduce as_a_nullable Date: Wed, 06 Aug 2014 17:38:00 -0000 Message-Id: <1407345815-14551-6-git-send-email-dmalcolm@redhat.com> In-Reply-To: <1407345815-14551-1-git-send-email-dmalcolm@redhat.com> References: <1407345815-14551-1-git-send-email-dmalcolm@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 X-IsSubscribed: yes X-SW-Source: 2014-08/txt/msg00596.txt.bz2 In many circumstances, is_a_helper ::test assumes that the pointer is non-NULL, but sometimes you have a pointer of type T that can be NULL. Earlier versions of this patch kit made numerous uses of the ternary operator to handle nullable pointers e.g.: return insn ? as_a (insn) : NULL; but this was ugly. Instead, introduce an as_a_nullable variant that adds a check for NULL, so the above can be written simply as: return as_a_nullable (insn); gcc/ * is-a.h (template as_a_nullable ) New function. --- gcc/is-a.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/gcc/is-a.h b/gcc/is-a.h index a14e344..f50e62c 100644 --- a/gcc/is-a.h +++ b/gcc/is-a.h @@ -46,6 +46,14 @@ TYPE as_a (pointer) do_something_with (as_a *ptr); +TYPE as_a_nullable (pointer) + + Like as_a (pointer), but where pointer could be NULL. This + adds a check against NULL where the regular is_a_helper hook for TYPE + assumes non-NULL. + + do_something_with (as_a_nullable *ptr); + TYPE dyn_cast (pointer) Converts pointer to TYPE if and only if "is_a pointer". Otherwise, @@ -185,6 +193,22 @@ as_a (U *p) return is_a_helper ::cast (p); } +/* Similar to as_a<>, but where the pointer can be NULL, even if + is_a_helper doesn't check for NULL. */ + +template +inline T +as_a_nullable (U *p) +{ + if (p) + { + gcc_checking_assert (is_a (p)); + return is_a_helper ::cast (p); + } + else + return NULL; +} + /* A generic checked conversion from a base type U to a derived type T. See the discussion above for when to use this function. */ -- 1.8.5.3