This patch implements the derived-type finalization for
monomorphic and polymorphic derived-type.
The finalization is done through a call to the Destroy
runtime function so the allocatable component object are also
finalized correctly when needed. It would be possible to finalize
monomorphic derived-type with non finalizable component with a
direct call to their finalize subroutine.
7.5.6.3 point 1: LHS nonallocatable object and LHS allocatable
object finalization. Done with call to Destroy for monomorphic
derived-type and through Assign for polymorphic entities.
7.5.6.3 point 2: Done within the deallocation calls.
7.5.6.3 point 3: A function context is added to the bridge to
attach finalization that need to happen on function/subroutine
exit.
7.5.6.3 point 4: BLOCK construct not yet implemented.
7.5.6.3 point 5/6: Finalization attach to the stmtCtx in a
similar way than 9.7.3.2 point 4.
7.5.6.3 point 7: INTENT(OUT) finalization done with a
call to Destroy runtime function call.
This patch passes 9/10 tests in the proposed test-suite
https://github.com/llvm/llvm-test-suite/pull/13
- The case with BLOCK construct will be implemented later when BLOCK are implemented upstream.
- Automatic deallocation is not yet implemented. Finalization triggered by automatic deallocation is then not triggered.
I won't push you to deal with this now because I think you would have a better time doing this with HLFIR, but I do not think it is OK to destroy the lhs at that point in case the rhs overlaps/is the same as the lhs.
I guess this overlap scenario is even more relevant in the array assignment case (which I do not think this patch is covering since I think the code for array assignment is doing an early exit with genArrayAssignment).
In HLFIR, I think the solution will be to add a finalize optional unit attribute to hlfir::assign operation and to insert the lhs destroy after the rhs temporization if there is any possible aliasing. I am OK with leaving this a TODO in the current lowering for now.
By the way, do you know if it is possible to use assignments that require LHS finalization inside a FORALL ?