Add cast_intrusive() and make use of it in two spots

This commit is contained in:
Jon Siwek 2020-05-06 17:37:23 -07:00
parent c6f2e35af0
commit 6a1e4d61d1
3 changed files with 18 additions and 6 deletions

View file

@ -2393,8 +2393,7 @@ IntrusivePtr<Val> AssignExpr::InitVal(const BroType* t, IntrusivePtr<Val> aggr)
if ( aggr->Type()->Tag() != TYPE_TABLE ) if ( aggr->Type()->Tag() != TYPE_TABLE )
Internal("bad aggregate in AssignExpr::InitVal"); Internal("bad aggregate in AssignExpr::InitVal");
// TODO: implement safer IntrusivePtr casts auto tv = cast_intrusive<TableVal>(std::move(aggr));
IntrusivePtr<TableVal> tv{NewRef{}, aggr->AsTableVal()};
const TableType* tt = tv->Type()->AsTableType(); const TableType* tt = tv->Type()->AsTableType();
const BroType* yt = tv->Type()->YieldType(); const BroType* yt = tv->Type()->YieldType();

View file

@ -184,6 +184,18 @@ IntrusivePtr<T> make_intrusive(Ts&&... args)
return {AdoptRef{}, new T(std::forward<Ts>(args)...)}; return {AdoptRef{}, new T(std::forward<Ts>(args)...)};
} }
/**
* Casts an @c IntrusivePtr object to another by way of static_cast on
* the underlying pointer.
* @param p The pointer of type @c U to cast to another type, @c T.
* @return The pointer, as cast to type @c T.
*/
template <class T, class U>
IntrusivePtr<T> cast_intrusive(IntrusivePtr<U> p) noexcept
{
return {AdoptRef{}, static_cast<T*>(p.release())};
}
// -- comparison to nullptr ---------------------------------------------------- // -- comparison to nullptr ----------------------------------------------------
/** /**

View file

@ -1892,12 +1892,12 @@ IntrusivePtr<BroType> merge_types(const BroType* t1, const BroType* t2)
const FuncType* ft1 = (const FuncType*) t1; const FuncType* ft1 = (const FuncType*) t1;
const FuncType* ft2 = (const FuncType*) t1; const FuncType* ft2 = (const FuncType*) t1;
auto args = merge_types(ft1->Args(), ft2->Args()); auto args = cast_intrusive<RecordType>(merge_types(ft1->Args(), ft2->Args()));
auto yield = t1->YieldType() ? auto yield = t1->YieldType() ?
merge_types(t1->YieldType(), t2->YieldType()) : nullptr; merge_types(t1->YieldType(), t2->YieldType()) : nullptr;
return make_intrusive<FuncType>(IntrusivePtr{AdoptRef{}, args.release()->AsRecordType()}, return make_intrusive<FuncType>(std::move(args), std::move(yield),
std::move(yield), ft1->Flavor()); ft1->Flavor());
} }
case TYPE_RECORD: case TYPE_RECORD:
@ -2125,7 +2125,8 @@ IntrusivePtr<BroType> init_type(Expr* init)
t = std::move(tl); t = std::move(tl);
} }
return make_intrusive<SetType>(IntrusivePtr{AdoptRef{}, t.release()->AsTypeList()}, nullptr); return make_intrusive<SetType>(cast_intrusive<TypeList>(std::move(t)),
nullptr);
} }
bool is_atomic_type(const BroType* t) bool is_atomic_type(const BroType* t)