diff options
author | dmalcolm <dmalcolm@138bc75d-0d04-0410-961f-82ee72b054a4> | 2014-08-18 19:50:38 +0000 |
---|---|---|
committer | dmalcolm <dmalcolm@138bc75d-0d04-0410-961f-82ee72b054a4> | 2014-08-18 19:50:38 +0000 |
commit | 7f4326bd2ded676097021d6b634a70a790a3b9ae (patch) | |
tree | 9de6672999a52ba34bb22855eb614e2b5b7dc0b0 /gcc/is-a.h | |
parent | 57c8dfeed0793a7b3ba62ec4c32ebbf29abed044 (diff) | |
download | gcc-7f4326bd2ded676097021d6b634a70a790a3b9ae.tar.gz |
Introduce safe_as_a
gcc/
2014-08-18 David Malcolm <dmalcolm@redhat.com>
* is-a.h (template<T, U> safe_as_a <U *p>) New function.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@214117 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/is-a.h')
-rw-r--r-- | gcc/is-a.h | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/gcc/is-a.h b/gcc/is-a.h index a14e344761a..176066b42bc 100644 --- a/gcc/is-a.h +++ b/gcc/is-a.h @@ -46,6 +46,14 @@ TYPE as_a <TYPE> (pointer) do_something_with (as_a <cgraph_node *> *ptr); +TYPE safe_as_a <TYPE> (pointer) + + Like as_a <TYPE> (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 (safe_as_a <cgraph_node *> *ptr); + TYPE dyn_cast <TYPE> (pointer) Converts pointer to TYPE if and only if "is_a <TYPE> pointer". Otherwise, @@ -185,6 +193,22 @@ as_a (U *p) return is_a_helper <T>::cast (p); } +/* Similar to as_a<>, but where the pointer can be NULL, even if + is_a_helper<T> doesn't check for NULL. */ + +template <typename T, typename U> +inline T +safe_as_a (U *p) +{ + if (p) + { + gcc_checking_assert (is_a <T> (p)); + return is_a_helper <T>::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. */ |