Skip to content

Commit

Permalink
[flang][NFC] Centralize fir.class addition in ConvertType
Browse files Browse the repository at this point in the history
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
  • Loading branch information
clementval committed Feb 8, 2023
1 parent 50ea17b commit 1e413b9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
23 changes: 18 additions & 5 deletions flang/lib/Lower/ConvertType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -266,18 +278,19 @@ 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.
if (isPtr)
return fir::PointerType::get(ty);
if (isAlloc)
return fir::HeapType::get(ty);
if (isPolymorphic)
return fir::ClassType::get(ty);
return ty;
}

Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Lower/HostAssociations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1e413b9

Please sign in to comment.