[flang][NFC] Centralize fir.class addition in ConvertType
authorValentin Clement <clementval@gmail.com>
Wed, 8 Feb 2023 09:28:31 +0000 (10:28 +0100)
committerValentin Clement <clementval@gmail.com>
Wed, 8 Feb 2023 09:28:58 +0000 (10:28 +0100)
fir.class type is always needed for polymorphic and unlimited
polymorphic entities. Wrapping the element type with a fir.class
type was done in ConvertType for some case and else where in the
code for other. Centralize this in ConvertType when converting
from expr or symbol.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D143490

flang/lib/Lower/ConvertType.cpp
flang/lib/Lower/HostAssociations.cpp

index e8d688a..4fab5a5 100644 (file)
@@ -143,6 +143,9 @@ struct TypeBuilderImpl {
     Fortran::common::TypeCategory category = dynamicType->category();
 
     mlir::Type baseType;
+    bool isPolymorphic = (dynamicType->IsPolymorphic() ||
+                          dynamicType->IsUnlimitedPolymorphic()) &&
+                         !dynamicType->IsAssumedType();
     if (dynamicType->IsUnlimitedPolymorphic()) {
       baseType = mlir::NoneType::get(context);
     } else if (category == Fortran::common::TypeCategory::Derived) {
@@ -167,8 +170,14 @@ struct TypeBuilderImpl {
       for (int dim = 0; dim < rank; ++dim)
         shape.emplace_back(fir::SequenceType::getUnknownExtent());
     }
-    if (!shape.empty())
+
+    if (!shape.empty()) {
+      if (isPolymorphic)
+        return fir::ClassType::get(fir::SequenceType::get(shape, baseType));
       return fir::SequenceType::get(shape, baseType);
+    }
+    if (isPolymorphic)
+      return fir::ClassType::get(baseType);
     return baseType;
   }
 
@@ -256,6 +265,9 @@ struct TypeBuilderImpl {
     } else {
       fir::emitFatalError(loc, "symbol must have a type");
     }
+    bool isPolymorphic = (Fortran::semantics::IsPolymorphic(symbol) ||
+                          Fortran::semantics::IsUnlimitedPolymorphic(symbol)) &&
+                         !Fortran::semantics::IsAssumedType(symbol);
     if (ultimate.IsObjectArray()) {
       auto shapeExpr = Fortran::evaluate::GetShapeHelper{
           converter.getFoldingContext()}(ultimate);
@@ -266,11 +278,10 @@ struct TypeBuilderImpl {
       ty = fir::SequenceType::get(shape, ty);
     }
     if (Fortran::semantics::IsPointer(symbol))
-      return fir::wrapInClassOrBoxType(
-          fir::PointerType::get(ty), Fortran::semantics::IsPolymorphic(symbol));
+      return fir::wrapInClassOrBoxType(fir::PointerType::get(ty),
+                                       isPolymorphic);
     if (Fortran::semantics::IsAllocatable(symbol))
-      return fir::wrapInClassOrBoxType(
-          fir::HeapType::get(ty), Fortran::semantics::IsPolymorphic(symbol));
+      return fir::wrapInClassOrBoxType(fir::HeapType::get(ty), isPolymorphic);
     // isPtr and isAlloc are variable that were promoted to be on the
     // heap or to be pointers, but they do not have Fortran allocatable
     // or pointer semantics, so do not use box for them.
@@ -278,6 +289,8 @@ struct TypeBuilderImpl {
       return fir::PointerType::get(ty);
     if (isAlloc)
       return fir::HeapType::get(ty);
+    if (isPolymorphic)
+      return fir::ClassType::get(ty);
     return ty;
   }
 
index b6c8ee0..4d8a120 100644 (file)
@@ -253,7 +253,7 @@ class CapturedPolymorphic : public CapturedSymbols<CapturedPolymorphic> {
 public:
   static mlir::Type getType(Fortran::lower::AbstractConverter &converter,
                             const Fortran::semantics::Symbol &sym) {
-    return fir::ClassType::get(converter.genType(sym));
+    return converter.genType(sym);
   }
   static void instantiateHostTuple(const InstantiateHostTuple &args,
                                    Fortran::lower::AbstractConverter &converter,