diff --git a/flang/lib/Lower/ConvertType.cpp b/flang/lib/Lower/ConvertType.cpp --- a/flang/lib/Lower/ConvertType.cpp +++ b/flang/lib/Lower/ConvertType.cpp @@ -143,6 +143,9 @@ 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 @@ 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 @@ } 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 @@ 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 @@ return fir::PointerType::get(ty); if (isAlloc) return fir::HeapType::get(ty); + if (isPolymorphic) + return fir::ClassType::get(ty); return ty; } diff --git a/flang/lib/Lower/HostAssociations.cpp b/flang/lib/Lower/HostAssociations.cpp --- a/flang/lib/Lower/HostAssociations.cpp +++ b/flang/lib/Lower/HostAssociations.cpp @@ -253,7 +253,7 @@ 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,