diff --git a/llvm/include/llvm/ADT/TypeSwitch.h b/llvm/include/llvm/ADT/TypeSwitch.h index 10a2d48e918db99..5bbbdf23b257ed3 100644 --- a/llvm/include/llvm/ADT/TypeSwitch.h +++ b/llvm/include/llvm/ADT/TypeSwitch.h @@ -61,29 +61,9 @@ template class TypeSwitchBase { } protected: - /// Trait to check whether `ValueT` provides a 'dyn_cast' method with type - /// `CastT`. - template - using has_dyn_cast_t = - decltype(std::declval().template dyn_cast()); - - /// 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 - static decltype(auto) castValue( - ValueT &&value, - std::enable_if_t::value> * = - nullptr) { - return value.template dyn_cast(); - } - - /// Attempt to dyn_cast the given `value` to `CastT`. This overload is - /// selected if llvm::dyn_cast should be used. - template - static decltype(auto) castValue( - ValueT &&value, - std::enable_if_t::value> * = - nullptr) { + static decltype(auto) castValue(ValueT &&value) { return dyn_cast(value); } diff --git a/mlir/include/mlir/IR/Attributes.h b/mlir/include/mlir/IR/Attributes.h index cc0cee6a31183cc..8a077865b51b5f2 100644 --- a/mlir/include/mlir/IR/Attributes.h +++ b/mlir/include/mlir/IR/Attributes.h @@ -50,14 +50,19 @@ class Attribute { /// Casting utility functions. These are deprecated and will be removed, /// please prefer using the `llvm` namespace variants instead. template + [[deprecated("Use mlir::isa() instead")]] bool isa() const; template + [[deprecated("Use mlir::isa_and_nonnull() instead")]] bool isa_and_nonnull() const; template + [[deprecated("Use mlir::dyn_cast() instead")]] U dyn_cast() const; template + [[deprecated("Use mlir::dyn_cast_or_null() instead")]] U dyn_cast_or_null() const; template + [[deprecated("Use mlir::cast() instead")]] U cast() const; /// Return a unique identifier for the concrete attribute type. This is used diff --git a/mlir/include/mlir/IR/Location.h b/mlir/include/mlir/IR/Location.h index aa8314f38cdface..423b4d19b5b944a 100644 --- a/mlir/include/mlir/IR/Location.h +++ b/mlir/include/mlir/IR/Location.h @@ -78,14 +78,17 @@ class Location { /// Type casting utilities on the underlying location. template + [[deprecated("Use mlir::isa() instead")]] bool isa() const { return llvm::isa(*this); } template + [[deprecated("Use mlir::dyn_cast() instead")]] U dyn_cast() const { return llvm::dyn_cast(*this); } template + [[deprecated("Use mlir::cast() instead")]] U cast() const { return llvm::cast(*this); } diff --git a/mlir/include/mlir/IR/Types.h b/mlir/include/mlir/IR/Types.h index a89e13b625bf406..65824531fdc9088 100644 --- a/mlir/include/mlir/IR/Types.h +++ b/mlir/include/mlir/IR/Types.h @@ -97,14 +97,19 @@ class Type { bool operator!() const { return impl == nullptr; } template + [[deprecated("Use mlir::isa() instead")]] bool isa() const; template + [[deprecated("Use mlir::isa_and_nonnull() instead")]] bool isa_and_nonnull() const; template + [[deprecated("Use mlir::dyn_cast() instead")]] U dyn_cast() const; template + [[deprecated("Use mlir::dyn_cast_or_null() instead")]] U dyn_cast_or_null() const; template + [[deprecated("Use mlir::cast() instead")]] U cast() const; /// Return a unique identifier for the concrete type. This is used to support diff --git a/mlir/include/mlir/Tools/PDLL/AST/Types.h b/mlir/include/mlir/Tools/PDLL/AST/Types.h index 03252e9f6620c84..89c8e193ddc32b4 100644 --- a/mlir/include/mlir/Tools/PDLL/AST/Types.h +++ b/mlir/include/mlir/Tools/PDLL/AST/Types.h @@ -64,23 +64,28 @@ class Type { /// Provide type casting support. template + [[deprecated("Use mlir::isa() instead")]] bool isa() const { assert(impl && "isa<> used on a null type."); return U::classof(*this); } template + [[deprecated("Use mlir::isa() instead")]] bool isa() const { return isa() || isa(); } template + [[deprecated("Use mlir::dyn_cast() instead")]] U dyn_cast() const { return isa() ? U(impl) : U(nullptr); } template + [[deprecated("Use mlir::dyn_cast_or_null() instead")]] U dyn_cast_or_null() const { return (impl && isa()) ? U(impl) : U(nullptr); } template + [[deprecated("Use mlir::cast() instead")]] U cast() const { assert(isa()); return U(impl); @@ -323,6 +328,29 @@ struct DenseMapInfo { 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 +struct CastInfo< + To, From, + std::enable_if_t< + std::is_same_v> || + std::is_base_of_v>> + : NullableValueCastFailed, + DefaultDoCastIfPossible> { + 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) { + 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_