@@ -711,8 +711,8 @@ class FirConverter : public Fortran::lower::AbstractConverter {
711711 return bool (shallowLookupSymbol (sym));
712712 }
713713
714- bool createHostAssociateVarClone (
715- const Fortran::semantics::Symbol &sym ) override final {
714+ bool createHostAssociateVarClone (const Fortran::semantics::Symbol &sym,
715+ bool skipDefaultInit ) override final {
716716 mlir::Location loc = genLocation (sym.name ());
717717 mlir::Type symType = genType (sym);
718718 const auto *details = sym.detailsIf <Fortran::semantics::HostAssocDetails>();
@@ -769,13 +769,21 @@ class FirConverter : public Fortran::lower::AbstractConverter {
769769 // Initialise cloned allocatable
770770 hexv.match (
771771 [&](const fir::MutableBoxValue &box) -> void {
772- // Do not process pointers
772+ const auto new_box = exv. getBoxOf <fir::MutableBoxValue>();
773773 if (Fortran::semantics::IsPointer (sym.GetUltimate ())) {
774+ // Establish the pointer descriptors. The rank and type code/size
775+ // at least must be set properly for later inquiry of the pointer
776+ // to work, and new pointers are always given disassociated status
777+ // by flang for safety, even if this is not required by the
778+ // language.
779+ auto empty = fir::factory::createUnallocatedBox (
780+ *builder, loc, new_box->getBoxTy (), box.nonDeferredLenParams (),
781+ {});
782+ builder->create <fir::StoreOp>(loc, empty, new_box->getAddr ());
774783 return ;
775784 }
776- // Allocate storage for a pointer/allocatble descriptor.
777- // No shape/lengths to be passed to the alloca.
778- const auto new_box = exv.getBoxOf <fir::MutableBoxValue>();
785+ // Copy allocation status of Allocatables, creating new storage if
786+ // needed.
779787
780788 // allocate if allocated
781789 mlir::Value isAllocated =
@@ -823,7 +831,22 @@ class FirConverter : public Fortran::lower::AbstractConverter {
823831 if_builder.end ();
824832 },
825833 [&](const auto &) -> void {
826- // Do nothing
834+ if (skipDefaultInit)
835+ return ;
836+ // Initialize local/private derived types with default
837+ // initialization (Fortran 2023 section 11.1.7.5 and OpenMP 5.2
838+ // section 5.3). Pointer and allocatable components, when allowed,
839+ // also need to be established so that flang runtime can later work
840+ // with them.
841+ if (const Fortran::semantics::DeclTypeSpec *declTypeSpec =
842+ sym.GetType ())
843+ if (const Fortran::semantics::DerivedTypeSpec *derivedTypeSpec =
844+ declTypeSpec->AsDerived ())
845+ if (derivedTypeSpec->HasDefaultInitialization (
846+ /* ignoreAllocatable=*/ false , /* ignorePointer=*/ false )) {
847+ mlir::Value box = builder->createBox (loc, exv);
848+ fir::runtime::genDerivedTypeInitialize (*builder, loc, box);
849+ }
827850 });
828851
829852 return bindIfNewSymbol (sym, exv);
@@ -1966,9 +1989,9 @@ class FirConverter : public Fortran::lower::AbstractConverter {
19661989 Fortran::semantics::SemanticsContext &semanticsContext =
19671990 bridge.getSemanticsContext ();
19681991 for (const Fortran::semantics::Symbol *sym : info.localSymList )
1969- createHostAssociateVarClone (*sym);
1992+ createHostAssociateVarClone (*sym, /* skipDefaultInit= */ false );
19701993 for (const Fortran::semantics::Symbol *sym : info.localInitSymList ) {
1971- createHostAssociateVarClone (*sym);
1994+ createHostAssociateVarClone (*sym, /* skipDefaultInit= */ true );
19721995 const auto *hostDetails =
19731996 sym->detailsIf <Fortran::semantics::HostAssocDetails>();
19741997 assert (hostDetails && " missing locality spec host symbol" );
@@ -1986,6 +2009,9 @@ class FirConverter : public Fortran::lower::AbstractConverter {
19862009 sym->detailsIf <Fortran::semantics::HostAssocDetails>();
19872010 copySymbolBinding (hostDetails->symbol (), *sym);
19882011 }
2012+ // Note that allocatable, types with ultimate components, and type
2013+ // requiring finalization are forbidden in LOCAL/LOCAL_INIT (F2023 C1130),
2014+ // so no clean-up needs to be generated for these entities.
19892015 }
19902016
19912017 // / Generate FIR for a DO construct. There are six variants:
0 commit comments