Files
ladybird/AK/TypeCasts.h
Jelle Raaijmakers 7eafdcd454 AK: Simplify as_if<T> when fast_is<T> is unavailable
If `fast_is<T>` was not available but `static_cast<T>` was, we would
call into `is<T>` causing a `dynamic_cast` there, throwing away the
result and return a `static_cast`ed pointer.

We can encourage better codegen by checking for the presence of
`fast_is<T>` ourselves. This does not change anything for when
`fast_is<T>` is present, but if it's unavailable we now reduce
`as_if<T>` to a simple tailcall of `dynamic_cast<T>`.

Old:

```
stp  x20, x19, [sp, #-32]!
stp  x29, x30, [sp, #16]
add  x29, sp, #16
mov  x19, x0
adrp x1, typeinfo for Base@PAGE
add  x1, x1, typeinfo for Base@PAGEOFF
adrp x2, typeinfo for WithoutFastIs@PAGE
add  x2, x2, typeinfo for WithoutFastIs@PAGEOFF
mov  x3, #0
bl   ___dynamic_cast
cmp  x0, #0
csel x0, xzr, x19, eq
ldp  x29, x30, [sp, #16]
ldp  x20, x19, [sp], #32
ret
```

New:

```
adrp x1, typeinfo for Base@PAGE
add  x1, x1, typeinfo for Base@PAGEOFF
adrp x2, typeinfo for WithoutFastIs@PAGE
add  x2, x2, typeinfo for WithoutFastIs@PAGEOFF
mov  x3, #0
b    ___dynamic_cast
```
2026-02-27 11:42:38 -05:00

93 lines
2.4 KiB
C++

/*
* Copyright (c) 2020-2021, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Assertions.h>
#include <AK/Concepts.h>
#include <AK/Forward.h>
#include <AK/Platform.h>
namespace AK {
template<typename OutputType, typename InputType>
ALWAYS_INLINE bool is(InputType& input)
{
static_assert(!SameAs<RemoveCVReference<OutputType>, RemoveCVReference<InputType>>);
if constexpr (requires { input.template fast_is<RemoveCVReference<OutputType>>(); }) {
return input.template fast_is<RemoveCVReference<OutputType>>();
}
return dynamic_cast<CopyConst<InputType, OutputType>*>(&input);
}
template<typename OutputType, typename InputType>
ALWAYS_INLINE bool is(InputType* input)
{
return input && is<OutputType>(*input);
}
template<typename OutputType, typename InputType>
ALWAYS_INLINE bool is(NonnullRefPtr<InputType> const& input)
{
return is<OutputType>(*input);
}
template<typename OutputType, typename InputType>
ALWAYS_INLINE CopyConst<InputType, OutputType>* as_if(InputType& input)
{
if constexpr (requires { input.template fast_is<RemoveCVReference<OutputType>>(); static_cast<CopyConst<InputType, OutputType>*>(&input); }) {
if (!is<OutputType>(input))
return nullptr;
return static_cast<CopyConst<InputType, OutputType>*>(&input);
}
return dynamic_cast<CopyConst<InputType, OutputType>*>(&input);
}
template<typename OutputType, typename InputType>
ALWAYS_INLINE CopyConst<InputType, OutputType>* as_if(InputType* input)
{
if (!input)
return nullptr;
return as_if<OutputType>(*input);
}
template<typename OutputType, typename InputType>
ALWAYS_INLINE CopyConst<InputType, OutputType>& as(InputType& input)
{
auto* result = as_if<OutputType>(input);
VERIFY(result);
return *result;
}
template<typename OutputType, typename InputType>
ALWAYS_INLINE CopyConst<InputType, OutputType>* as(InputType* input)
{
if (!input)
return nullptr;
auto* result = as_if<OutputType>(input);
VERIFY(result);
return result;
}
template<typename OutputType, typename InputType>
ALWAYS_INLINE CopyConst<InputType, OutputType>* bridge_cast(InputType input)
{
#ifdef AK_HAS_OBJC_ARC
return (__bridge CopyConst<InputType, OutputType>*)(input);
#else
return static_cast<CopyConst<InputType, OutputType>*>(input);
#endif
}
}
#if USING_AK_GLOBALLY
using AK::as;
using AK::as_if;
using AK::bridge_cast;
using AK::is;
#endif