From 24d6bf3d96e4e4676449d10d7632e903fc9ae557 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Thu, 3 Jul 2014 10:42:45 -0400 Subject: [PATCH 005/236] Introduce as_a_nullable 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