diff --git a/include/verilated_types.h b/include/verilated_types.h index 0a92725a9..be2914d70 100644 --- a/include/verilated_types.h +++ b/include/verilated_types.h @@ -1685,6 +1685,10 @@ public: template static inline bool VL_CAST_DYNAMIC(VlClassRef in, VlClassRef& outr) { + if (!in) { + outr = VlNull{}; + return true; + } VlClassRef casted = in.template dynamicCast(); if (VL_LIKELY(casted)) { outr = casted; @@ -1694,6 +1698,12 @@ static inline bool VL_CAST_DYNAMIC(VlClassRef in, VlClassRef& outr) { } } +template +static inline bool VL_CAST_DYNAMIC(VlNull in, VlClassRef& outr) { + outr = VlNull{}; + return true; +} + //============================================================================= // VlSampleQueue stores samples for input clockvars in clocking blocks. At a clocking event, // samples from this queue should be written to the correct input clockvar. diff --git a/test_regress/t/t_castdyn.v b/test_regress/t/t_castdyn.v index 9ccb19dc8..faa7d7542 100644 --- a/test_regress/t/t_castdyn.v +++ b/test_regress/t/t_castdyn.v @@ -9,6 +9,10 @@ endclass class BasedA extends Base; endclass class BasedB extends Base; + static function BasedB getBasedB(bit getNull); + BasedB b = new; + return getNull ? null : b; + endfunction endclass module t (/*AUTOARG*/); @@ -55,6 +59,18 @@ module t (/*AUTOARG*/); if (i != 1) $stop; if (b != bb) $stop; + bb = null; + b = bb; + i = $cast(bbo, b); + if (i != 1) $stop; + if (b != bb) $stop; + + bb = BasedB::getBasedB(1); + b = bb; + i = $cast(bbo, b); + if (i != 1) $stop; + if (b != bb) $stop; + bb = new; b = bb; bao = ba;