diff --git a/flang/include/flang/Optimizer/Dialect/FIRType.h b/flang/include/flang/Optimizer/Dialect/FIRType.h --- a/flang/include/flang/Optimizer/Dialect/FIRType.h +++ b/flang/include/flang/Optimizer/Dialect/FIRType.h @@ -24,6 +24,26 @@ using KindTy = unsigned; } // namespace fir +//===----------------------------------------------------------------------===// +// BaseBoxType +//===----------------------------------------------------------------------===// + +namespace fir { + +/// This class provides a shared interface for box and class types. +class BaseBoxType : public mlir::Type { +public: + using mlir::Type::Type; + + /// Returns the element type of this box type. + mlir::Type getEleTy() const; + + /// Methods for support type inquiry through isa, cast, and dyn_cast. + static bool classof(mlir::Type type); +}; + +} // namespace fir + #define GET_TYPEDEF_CLASSES #include "flang/Optimizer/Dialect/FIROpsTypes.h.inc" @@ -283,6 +303,10 @@ mlir::Type fromRealTypeID(mlir::MLIRContext *context, llvm::Type::TypeID typeID, fir::KindTy kind); +inline bool BaseBoxType::classof(mlir::Type type) { + return type.isa(); +} + } // namespace fir #endif // FORTRAN_OPTIMIZER_DIALECT_FIRTYPE_H diff --git a/flang/include/flang/Optimizer/Dialect/FIRTypes.td b/flang/include/flang/Optimizer/Dialect/FIRTypes.td --- a/flang/include/flang/Optimizer/Dialect/FIRTypes.td +++ b/flang/include/flang/Optimizer/Dialect/FIRTypes.td @@ -20,7 +20,9 @@ // FIR Types //===----------------------------------------------------------------------===// -class FIR_Type : TypeDef { +class FIR_Type traits = [], + string baseCppClass = "::mlir::Type"> + : TypeDef { let mnemonic = typeMnemonic; } @@ -66,7 +68,7 @@ let hasCustomAssemblyFormat = 1; } -def fir_BoxType : FIR_Type<"Box", "box"> { +def fir_BoxType : FIR_Type<"Box", "box", [], "BaseBoxType"> { let summary = "The type of a Fortran descriptor"; let description = [{ diff --git a/flang/lib/Optimizer/Dialect/FIRType.cpp b/flang/lib/Optimizer/Dialect/FIRType.cpp --- a/flang/lib/Optimizer/Dialect/FIRType.cpp +++ b/flang/lib/Optimizer/Dialect/FIRType.cpp @@ -924,6 +924,15 @@ } } +//===----------------------------------------------------------------------===// +// BaseBoxType +//===----------------------------------------------------------------------===// + +mlir::Type BaseBoxType::getEleTy() const { + return llvm::TypeSwitch(*this) + .Case([](auto type) { return type.getEleTy(); }); +} + //===----------------------------------------------------------------------===// // FIROpsDialect //===----------------------------------------------------------------------===//