Skip to content

Remove calls to isa/dyn_cast/cast/... member functions. #90453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 2 additions & 22 deletions llvm/include/llvm/ADT/TypeSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,9 @@ template <typename DerivedT, typename T> class TypeSwitchBase {
}

protected:
/// Trait to check whether `ValueT` provides a 'dyn_cast' method with type
/// `CastT`.
template <typename ValueT, typename CastT>
using has_dyn_cast_t =
decltype(std::declval<ValueT &>().template dyn_cast<CastT>());

/// Attempt to dyn_cast the given `value` to `CastT`. This overload is
/// selected if `value` already has a suitable dyn_cast method.
/// Attempt to dyn_cast the given `value` to `CastT`.
template <typename CastT, typename ValueT>
static decltype(auto) castValue(
ValueT &&value,
std::enable_if_t<is_detected<has_dyn_cast_t, ValueT, CastT>::value> * =
nullptr) {
return value.template dyn_cast<CastT>();
}

/// Attempt to dyn_cast the given `value` to `CastT`. This overload is
/// selected if llvm::dyn_cast should be used.
template <typename CastT, typename ValueT>
static decltype(auto) castValue(
ValueT &&value,
std::enable_if_t<!is_detected<has_dyn_cast_t, ValueT, CastT>::value> * =
nullptr) {
static decltype(auto) castValue(ValueT &&value) {
return dyn_cast<CastT>(value);
}

Expand Down
23 changes: 23 additions & 0 deletions mlir/include/mlir/Tools/PDLL/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,29 @@ struct DenseMapInfo<mlir::pdll::ast::Type> {
return lhs == rhs;
}
};

/// Add support for llvm style casts.
/// We provide a cast between To and From if From is mlir::pdll::ast::Type or
/// derives from it
template <typename To, typename From>
struct CastInfo<
To, From,
std::enable_if_t<
std::is_same_v<mlir::pdll::ast::Type, std::remove_const_t<From>> ||
std::is_base_of_v<mlir::pdll::ast::Type, From>>>
: NullableValueCastFailed<To>,
DefaultDoCastIfPossible<To, From, CastInfo<To, From>> {
static inline bool isPossible(mlir::pdll::ast::Type ty) {
/// Return a constant true instead of a dynamic true when casting to self or
/// up the hierarchy.
if constexpr (std::is_base_of_v<To, From>) {
return true;
} else {
return To::classof(ty);
};
}
static inline To doCast(mlir::pdll::ast::Type ty) { return To(ty.getImpl()); }
};
} // namespace llvm

#endif // MLIR_TOOLS_PDLL_AST_TYPES_H_