]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/is-a.h
PR translation/79183
[thirdparty/gcc.git] / gcc / is-a.h
index a14e344761af51b6bf491b9753cc301ae1987df9..f3533f6ad046229ba2bda0650987f1c79a8bb27f 100644 (file)
@@ -1,5 +1,5 @@
 /* Dynamic testing for abstract is-a relationships.
-   Copyright (C) 2012-2014 Free Software Foundation, Inc.
+   Copyright (C) 2012-2019 Free Software Foundation, Inc.
    Contributed by Lawrence Crowl.
 
 This file is part of GCC.
@@ -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,
@@ -95,6 +103,11 @@ TYPE dyn_cast <TYPE> (pointer)
     Note that we have converted two sets of assertions in the calls to varpool
     into safe and efficient use of a variable.
 
+TYPE safe_dyn_cast <TYPE> (pointer)
+
+    Like dyn_cast <TYPE> (pointer), except that it accepts null pointers
+    and returns null results for them.
+
 
 If you use these functions and get a 'inline function not defined' or a
 'missing symbol' error message for 'is_a_helper<....>::test', it means that
@@ -185,6 +198,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.  */
 
@@ -198,4 +227,13 @@ dyn_cast (U *p)
     return static_cast <T> (0);
 }
 
+/* Similar to dyn_cast, except that the pointer may be null.  */
+
+template <typename T, typename U>
+inline T
+safe_dyn_cast (U *p)
+{
+  return p ? dyn_cast <T> (p) : 0;
+}
+
 #endif  /* GCC_IS_A_H  */