Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 6/24/2024
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

FINAL Statement

Statement: Denotes a finalization procedure that defines one or more final subroutines that are bound to a derived type.

The FINAL statement takes the following form:

FINAL [::] sub1 [, sub2 ]...

sub1, sub2, ...

Is a final subroutine, which is a module procedure with exactly one dummy argument of the derived type. The dummy argument cannot be INTENT(OUT), it cannot have the VALUE attribute, it cannot be optional, and it cannot be a pointer, allocatable, or a coarray. All array shape and length type parameters are assumed.

Description

A final subroutine must not have a dummy argument with the same kind type parameters and rank as the dummy argument of another final subroutine of the type.

You cannot specify a subroutine that was previously specified as a final subroutine for the derived type.

If a final subroutine has an assumed-rank dummy argument, no other final subroutine of that type can have a dummy argument with the same kind type parameters.

This statement is used in derived-type type-bound procedures.

A derived type is finalizable only if it has a final subroutine or a nonpointer, nonallocatable component of finalizable type. A nonpointer data entity is finalizable only if it is of finalizable type.

When an entity is finalized, the following actions occur in this order:

  1. If the dynamic type of the entity has a final subroutine whose dummy argument has the same kind type parameters and rank as the entity being finalized, it is called with the entity as an actual argument.

    Otherwise, if there is an elemental final subroutine whose dummy argument has the same kind type parameters as the entity being finalized, or a final subroutine whose dummy argument is assumed-rank with the same kind type parameters as the entity being finalized, it is called with the entity as an actual argument. Otherwise, no subroutine is called.

  2. Finalizable components in the type are finalized. If the entity is an array, each finalizable component of each element is finalized. The order of finalization is processor dependent.

  3. If the entity is of an extended type with a finalizable parent type, the parent component is finalized.

Effects of Finalization

If the variable of an intrinsic assignment is not an unallocated allocatable array when the statement is executed, the variable is finalized after the expression on the right-hand side (RHS) is evaluated and before the variable is defined. If the variable is an allocated allocatable variable or has an allocated allocatable subobject that would be deallocated by intrinsic assignment, finalization occurs before deallocation.

When a pointer is deallocated, its target is finalized. When an allocatable entity is deallocated, it is finalized unless it is the variable on the left-hand side (LHS) of the intrinsic assignment or a subobject of the variable.

A nonpointer, nonallocatable object that is not a dummy argument or function result is finalized immediately before it would become undefined due to execution of a RETURN or END statement.

If the entity is of extended type and the parent type is finalizable, the parent component is finalized.

A nonpointer, nonallocatable local variable of a BLOCK construct is finalized immediately before it would become undefined due to termination of the BLOCK construct.

The following rules also apply:

  • If an executable construct references a nonpointer function, the result is finalized after execution of the innermost executable construct containing the reference.

  • If a specification expression in a scoping unit references a function, the result is finalized before execution of the executable constructs in the scoping unit.

  • A nonpointer, nonallocatable, INTENT(OUT) dummy argument of a procedure is finalized when the procedure is invoked before the dummy argument becomes undefined. Finalization of INTENT(OUT) dummy arguments occurs in the invoked procedure; for elemental procedures, an INTENT(OUT) argument will be finalized only if a scalar or elemental final subroutine is available, regardless of the rank of the argument.

  • If image execution is terminated, either by an error or by execution of a ERROR STOP, STOP, or END PROGRAM statement, any entities that exist immediately before termination are not finalized.

Example

The following example declares two module subroutines to be final:


TYPE MY_TYPE
...   ! Component declarations
CONTAINS
  FINAL :: CLEAN1, CLEAN2
END TYPE MY_TYPE