Revert "[flang][fir][NFC] Move BoxType to TableGen type definition"

This reverts commit d96bb48f7874d636ffd0ee233e2d66fff0614b8f.

GitOrigin-RevId: 661f9e2a92302b1c7140528423fdbfc133a68b41
diff --git a/include/flang/Optimizer/Dialect/FIROps.h b/include/flang/Optimizer/Dialect/FIROps.h
index c9541c8..6647784 100644
--- a/include/flang/Optimizer/Dialect/FIROps.h
+++ b/include/flang/Optimizer/Dialect/FIROps.h
@@ -9,7 +9,6 @@
 #ifndef OPTIMIZER_DIALECT_FIROPS_H
 #define OPTIMIZER_DIALECT_FIROPS_H
 
-#include "flang/Optimizer/Dialect/FIRType.h"
 #include "mlir/Dialect/StandardOps/IR/Ops.h"
 #include "mlir/Interfaces/LoopLikeInterface.h"
 #include "mlir/Interfaces/SideEffectInterfaces.h"
diff --git a/include/flang/Optimizer/Dialect/FIROps.td b/include/flang/Optimizer/Dialect/FIROps.td
index ebed9f5..918fdd4 100644
--- a/include/flang/Optimizer/Dialect/FIROps.td
+++ b/include/flang/Optimizer/Dialect/FIROps.td
@@ -83,6 +83,9 @@
 def AnyReferenceLike : TypeConstraint<Or<[fir_ReferenceType.predicate,
     fir_HeapType.predicate, fir_PointerType.predicate]>, "any reference">;
 
+// A descriptor tuple (captures a reference to an entity and other information)
+def fir_BoxType : Type<CPred<"$_self.isa<fir::BoxType>()">, "box type">;
+
 // CHARACTER type descriptor. A pair of a data reference and a LEN value.
 def fir_BoxCharType : Type<CPred<"$_self.isa<fir::BoxCharType>()">,
     "box character type">;
@@ -91,11 +94,11 @@
 def fir_BoxProcType : Type<CPred<"$_self.isa<fir::BoxProcType>()">,
     "box procedure type">;
 
-def AnyBoxLike : TypeConstraint<Or<[BoxType.predicate,
+def AnyBoxLike : TypeConstraint<Or<[fir_BoxType.predicate,
     fir_BoxCharType.predicate, fir_BoxProcType.predicate]>, "any box">;
 
 def AnyRefOrBox : TypeConstraint<Or<[fir_ReferenceType.predicate,
-    fir_HeapType.predicate, fir_PointerType.predicate, BoxType.predicate]>,
+    fir_HeapType.predicate, fir_PointerType.predicate, fir_BoxType.predicate]>,
     "any reference or box">;
 
 def AnyShapeLike : TypeConstraint<Or<[ShapeType.predicate,
@@ -1045,7 +1048,7 @@
 
   let arguments = (ins AnyReferenceLike:$memref, Variadic<AnyEmboxArg>:$args);
 
-  let results = (outs BoxType);
+  let results = (outs fir_BoxType);
 
   let parser = "return parseEmboxOp(parser, result);";
 
@@ -1254,7 +1257,7 @@
     ```
   }];
 
-  let arguments = (ins BoxType:$box);
+  let arguments = (ins fir_BoxType:$box);
 
   let results = (outs
     fir_ReferenceType,  // pointer to data
@@ -1325,7 +1328,7 @@
     ```
   }];
 
-  let arguments = (ins BoxType:$val);
+  let arguments = (ins fir_BoxType:$val);
 
   let results = (outs AnyReferenceLike);
 
@@ -1369,7 +1372,7 @@
     the box. The triple will be the lower bound, upper bound, and stride.
   }];
 
-  let arguments = (ins BoxType:$val, AnyIntegerLike:$dim);
+  let arguments = (ins fir_BoxType:$val, AnyIntegerLike:$dim);
 
   let results = (outs AnyIntegerLike, AnyIntegerLike, AnyIntegerLike);
 
@@ -1398,7 +1401,7 @@
     must box an array of REAL values (with dynamic rank and extent).
   }];
 
-  let arguments = (ins BoxType:$val);
+  let arguments = (ins fir_BoxType:$val);
 
   let results = (outs AnyIntegerLike);
 }
@@ -1421,7 +1424,7 @@
     variable is an `ALLOCATABLE`.
   }];
 
-  let arguments = (ins BoxType:$val);
+  let arguments = (ins fir_BoxType:$val);
 
   let results = (outs BoolLike);
 }
@@ -1442,7 +1445,7 @@
     ```
   }];
 
-  let arguments = (ins BoxType:$val);
+  let arguments = (ins fir_BoxType:$val);
 
   let results = (outs BoolLike);
 }
@@ -1460,7 +1463,7 @@
     ```
   }];
 
-  let arguments = (ins BoxType:$val);
+  let arguments = (ins fir_BoxType:$val);
 
   let results = (outs BoolLike);
 }
@@ -1504,7 +1507,7 @@
     descriptor may be either an array or a scalar, so the value is nonnegative.
   }];
 
-  let arguments = (ins BoxType:$val);
+  let arguments = (ins fir_BoxType:$val);
 
   let results = (outs AnyIntegerType);
 }
@@ -1522,7 +1525,7 @@
     ```
   }];
 
-  let arguments = (ins BoxType:$val);
+  let arguments = (ins fir_BoxType:$val);
 
   let results = (outs fir_TypeDescType);
 }
@@ -2097,7 +2100,7 @@
 
   let arguments = (ins
     StrAttr:$method,
-    BoxType:$object,
+    fir_BoxType:$object,
     Variadic<AnyType>:$args
   );
 
diff --git a/include/flang/Optimizer/Dialect/FIRType.h b/include/flang/Optimizer/Dialect/FIRType.h
index a531a51..44e676c 100644
--- a/include/flang/Optimizer/Dialect/FIRType.h
+++ b/include/flang/Optimizer/Dialect/FIRType.h
@@ -42,6 +42,7 @@
 using KindTy = unsigned;
 
 namespace detail {
+struct BoxTypeStorage;
 struct BoxCharTypeStorage;
 struct BoxProcTypeStorage;
 struct CharacterTypeStorage;
@@ -177,6 +178,23 @@
 
 // FIR support types
 
+/// The type of a Fortran descriptor. Descriptors are tuples of information that
+/// describe an entity being passed from a calling context. This information
+/// might include (but is not limited to) whether the entity is an array, its
+/// size, or what type it has.
+class BoxType
+    : public mlir::Type::TypeBase<BoxType, mlir::Type, detail::BoxTypeStorage> {
+public:
+  using Base::Base;
+  static BoxType get(mlir::Type eleTy, mlir::AffineMapAttr map = {});
+  mlir::Type getEleTy() const;
+  mlir::AffineMapAttr getLayoutMap() const;
+
+  static mlir::LogicalResult
+  verifyConstructionInvariants(mlir::Location, mlir::Type eleTy,
+                               mlir::AffineMapAttr map);
+};
+
 /// The type of a pair that describes a CHARACTER variable. Specifically, a
 /// CHARACTER consists of a reference to a buffer (the string value) and a LEN
 /// type parameter (the runtime length of the buffer).
diff --git a/include/flang/Optimizer/Dialect/FIRTypes.td b/include/flang/Optimizer/Dialect/FIRTypes.td
index 5a25d9e..99f1c17 100644
--- a/include/flang/Optimizer/Dialect/FIRTypes.td
+++ b/include/flang/Optimizer/Dialect/FIRTypes.td
@@ -21,29 +21,6 @@
   let mnemonic = typeMnemonic;
 }
 
-def BoxType : FIR_Type<"Box", "box"> {
-  let summary = "The type of a Fortran descriptor";
-
-  let description = [{
-    Descriptors are tuples of information that describe an entity being passed
-    from a calling context. This information might include (but is not limited
-    to) whether the entity is an array, its size, or what type it has.
-  }];
-
-  let parameters = (ins "mlir::Type":$eleTy, "mlir::AffineMapAttr":$map);
-
-  let extraClassDeclaration = [{
-    mlir::Type getElementType() const { return getEleTy(); }
-    mlir::AffineMapAttr getLayoutMap() const { return getMap(); }
-    static BoxType get(mlir::Type eleTy, mlir::AffineMapAttr map = {}) {
-      return get(eleTy, map);
-    }
-  }];
-
-  let genAccessors = 1;
-  let genVerifyInvariantsDecl = 1;
-}
-
 def ShapeType : FIR_Type<"Shape", "shape"> {
   let summary = "shape of a multidimensional array object";
 
diff --git a/lib/Optimizer/Dialect/FIRType.cpp b/lib/Optimizer/Dialect/FIRType.cpp
index 35e71a7..c0fd3d3 100644
--- a/lib/Optimizer/Dialect/FIRType.cpp
+++ b/lib/Optimizer/Dialect/FIRType.cpp
@@ -58,6 +58,27 @@
   return TYPE::get(ty);
 }
 
+// `box` `<` type (',' affine-map)? `>`
+BoxType parseBox(mlir::DialectAsmParser &parser, mlir::Location loc) {
+  mlir::Type ofTy;
+  if (parser.parseLess() || parser.parseType(ofTy)) {
+    parser.emitError(parser.getCurrentLocation(), "expected type parameter");
+    return {};
+  }
+
+  mlir::AffineMapAttr map;
+  if (!parser.parseOptionalComma())
+    if (parser.parseAttribute(map)) {
+      parser.emitError(parser.getCurrentLocation(), "expected affine map");
+      return {};
+    }
+  if (parser.parseGreater()) {
+    parser.emitError(parser.getCurrentLocation(), "expected '>'");
+    return {};
+  }
+  return BoxType::get(ofTy, map);
+}
+
 // `boxchar` `<` kind `>`
 BoxCharType parseBoxChar(mlir::DialectAsmParser &parser) {
   return parseKindSingleton<BoxCharType>(parser);
@@ -334,7 +355,7 @@
   if (typeNameLit == "array")
     return parseSequence(parser, loc);
   if (typeNameLit == "box")
-    return generatedTypeParser(dialect->getContext(), parser, typeNameLit);
+    return parseBox(parser, loc);
   if (typeNameLit == "boxchar")
     return parseBoxChar(parser);
   if (typeNameLit == "boxproc")
@@ -573,6 +594,41 @@
   explicit RealTypeStorage(KindTy kind) : kind{kind} {}
 };
 
+/// Boxed object (a Fortran descriptor)
+struct BoxTypeStorage : public mlir::TypeStorage {
+  using KeyTy = std::tuple<mlir::Type, mlir::AffineMapAttr>;
+
+  static unsigned hashKey(const KeyTy &key) {
+    auto hashVal{llvm::hash_combine(std::get<mlir::Type>(key))};
+    return llvm::hash_combine(
+        hashVal, llvm::hash_combine(std::get<mlir::AffineMapAttr>(key)));
+  }
+
+  bool operator==(const KeyTy &key) const {
+    return std::get<mlir::Type>(key) == getElementType() &&
+           std::get<mlir::AffineMapAttr>(key) == getLayoutMap();
+  }
+
+  static BoxTypeStorage *construct(mlir::TypeStorageAllocator &allocator,
+                                   const KeyTy &key) {
+    auto *storage = allocator.allocate<BoxTypeStorage>();
+    return new (storage) BoxTypeStorage{std::get<mlir::Type>(key),
+                                        std::get<mlir::AffineMapAttr>(key)};
+  }
+
+  mlir::Type getElementType() const { return eleTy; }
+  mlir::AffineMapAttr getLayoutMap() const { return map; }
+
+protected:
+  mlir::Type eleTy;
+  mlir::AffineMapAttr map;
+
+private:
+  BoxTypeStorage() = delete;
+  explicit BoxTypeStorage(mlir::Type eleTy, mlir::AffineMapAttr map)
+      : eleTy{eleTy}, map{map} {}
+};
+
 /// Boxed CHARACTER object type
 struct BoxCharTypeStorage : public mlir::TypeStorage {
   using KeyTy = KindTy;
@@ -957,6 +1013,20 @@
 
 KindTy fir::RealType::getFKind() const { return getImpl()->getFKind(); }
 
+// Box<T>
+
+BoxType fir::BoxType::get(mlir::Type elementType, mlir::AffineMapAttr map) {
+  return Base::get(elementType.getContext(), elementType, map);
+}
+
+mlir::Type fir::BoxType::getEleTy() const {
+  return getImpl()->getElementType();
+}
+
+mlir::AffineMapAttr fir::BoxType::getLayoutMap() const {
+  return getImpl()->getLayoutMap();
+}
+
 mlir::LogicalResult
 fir::BoxType::verifyConstructionInvariants(mlir::Location, mlir::Type eleTy,
                                            mlir::AffineMapAttr map) {
@@ -1270,6 +1340,16 @@
 void fir::printFirType(FIROpsDialect *, mlir::Type ty,
                        mlir::DialectAsmPrinter &p) {
   auto &os = p.getStream();
+  if (auto type = ty.dyn_cast<BoxType>()) {
+    os << "box<";
+    p.printType(type.getEleTy());
+    if (auto map = type.getLayoutMap()) {
+      os << ", ";
+      p.printAttribute(map);
+    }
+    os << '>';
+    return;
+  }
   if (auto type = ty.dyn_cast<BoxCharType>()) {
     os << "boxchar<" << type.getEleTy().cast<fir::CharacterType>().getFKind()
        << '>';
@@ -1420,44 +1500,3 @@
   }
   return false;
 }
-
-namespace fir {
-
-//===----------------------------------------------------------------------===//
-// BoxType
-//===----------------------------------------------------------------------===//
-
-// `box` `<` type (',' affine-map)? `>`
-mlir::Type BoxType::parse(mlir::MLIRContext *context,
-                          mlir::DialectAsmParser &parser) {
-  mlir::Type ofTy;
-  if (parser.parseLess() || parser.parseType(ofTy)) {
-    parser.emitError(parser.getCurrentLocation(), "expected type parameter");
-    return Type();
-  }
-
-  mlir::AffineMapAttr map;
-  if (!parser.parseOptionalComma()) {
-    if (parser.parseAttribute(map)) {
-      parser.emitError(parser.getCurrentLocation(), "expected affine map");
-      return Type();
-    }
-  }
-  if (parser.parseGreater()) {
-    parser.emitError(parser.getCurrentLocation(), "expected '>'");
-    return Type();
-  }
-  return get(ofTy, map);
-}
-
-void BoxType::print(::mlir::DialectAsmPrinter &printer) const {
-  printer << "box<";
-  printer.printType(getEleTy());
-  if (auto map = getLayoutMap()) {
-    printer << ", ";
-    printer.printAttribute(map);
-  }
-  printer << '>';
-}
-
-} // namespace fir