Skip to content

Commit 1e413b9

Browse files
committed
[flang][NFC] Centralize fir.class addition in ConvertType
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
1 parent 50ea17b commit 1e413b9

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

flang/lib/Lower/ConvertType.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ struct TypeBuilderImpl {
143143
Fortran::common::TypeCategory category = dynamicType->category();
144144

145145
mlir::Type baseType;
146+
bool isPolymorphic = (dynamicType->IsPolymorphic() ||
147+
dynamicType->IsUnlimitedPolymorphic()) &&
148+
!dynamicType->IsAssumedType();
146149
if (dynamicType->IsUnlimitedPolymorphic()) {
147150
baseType = mlir::NoneType::get(context);
148151
} else if (category == Fortran::common::TypeCategory::Derived) {
@@ -167,8 +170,14 @@ struct TypeBuilderImpl {
167170
for (int dim = 0; dim < rank; ++dim)
168171
shape.emplace_back(fir::SequenceType::getUnknownExtent());
169172
}
170-
if (!shape.empty())
173+
174+
if (!shape.empty()) {
175+
if (isPolymorphic)
176+
return fir::ClassType::get(fir::SequenceType::get(shape, baseType));
171177
return fir::SequenceType::get(shape, baseType);
178+
}
179+
if (isPolymorphic)
180+
return fir::ClassType::get(baseType);
172181
return baseType;
173182
}
174183

@@ -256,6 +265,9 @@ struct TypeBuilderImpl {
256265
} else {
257266
fir::emitFatalError(loc, "symbol must have a type");
258267
}
268+
bool isPolymorphic = (Fortran::semantics::IsPolymorphic(symbol) ||
269+
Fortran::semantics::IsUnlimitedPolymorphic(symbol)) &&
270+
!Fortran::semantics::IsAssumedType(symbol);
259271
if (ultimate.IsObjectArray()) {
260272
auto shapeExpr = Fortran::evaluate::GetShapeHelper{
261273
converter.getFoldingContext()}(ultimate);
@@ -266,18 +278,19 @@ struct TypeBuilderImpl {
266278
ty = fir::SequenceType::get(shape, ty);
267279
}
268280
if (Fortran::semantics::IsPointer(symbol))
269-
return fir::wrapInClassOrBoxType(
270-
fir::PointerType::get(ty), Fortran::semantics::IsPolymorphic(symbol));
281+
return fir::wrapInClassOrBoxType(fir::PointerType::get(ty),
282+
isPolymorphic);
271283
if (Fortran::semantics::IsAllocatable(symbol))
272-
return fir::wrapInClassOrBoxType(
273-
fir::HeapType::get(ty), Fortran::semantics::IsPolymorphic(symbol));
284+
return fir::wrapInClassOrBoxType(fir::HeapType::get(ty), isPolymorphic);
274285
// isPtr and isAlloc are variable that were promoted to be on the
275286
// heap or to be pointers, but they do not have Fortran allocatable
276287
// or pointer semantics, so do not use box for them.
277288
if (isPtr)
278289
return fir::PointerType::get(ty);
279290
if (isAlloc)
280291
return fir::HeapType::get(ty);
292+
if (isPolymorphic)
293+
return fir::ClassType::get(ty);
281294
return ty;
282295
}
283296

flang/lib/Lower/HostAssociations.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ class CapturedPolymorphic : public CapturedSymbols<CapturedPolymorphic> {
253253
public:
254254
static mlir::Type getType(Fortran::lower::AbstractConverter &converter,
255255
const Fortran::semantics::Symbol &sym) {
256-
return fir::ClassType::get(converter.genType(sym));
256+
return converter.genType(sym);
257257
}
258258
static void instantiateHostTuple(const InstantiateHostTuple &args,
259259
Fortran::lower::AbstractConverter &converter,

0 commit comments

Comments
 (0)