Submodules (Fortran 2008)
A submodule is a program unit that extends a module or another submodule. It might provide definitions for procedures whose interfaces are declared in the ancestor module or an ancestor submodule. It might also contain declarations and definitions of other entities. These definitions include data object definitions, namelist groups, derived-type definitions, procedure interface blocks, and procedure definitions.
A submodule cannot have the same name as other global entities, such as program units, common blocks, external procedures, critical sections, or binding labels of global entities. The name of a submodule can be the same as the name of another submodule if they do not have the same ancestor module.
Syntax
- SUBMODULE_statement
- For syntax details, see SUBMODULE.
- specification_part
- It is a sequence of statements from the statement groups numbered 2 , 4 , and 5 in Order of statements and execution sequence, excluding ENTRY and FORMAT statements.
- CONTAINS_statement
- For syntax details, see CONTAINS.
- END_SUBMODULE_statement
- For syntax details, see END.
Rules
A submodule has only one ancestor module and can have one or more ancestor submodules.
A submodule might contain module subprograms and is the host scoping unit of the module subprograms. Module subprograms must follow a CONTAINS statement and can contain internal procedures. A module procedure is defined by a module subprogram or an entry in a module subprogram.
In a submodule, you can define separate module procedures that are declared by corresponding module procedure interface bodies in the ancestor module or an ancestor submodule.
A submodule has access to the entities that are declared and defined in its ancestor module or submodules by host association. A submodule is a host to any module procedures, interface blocks, or derived-type definitions that it contains. Through host association, the module procedures, interface blocks, and derived-type definitions that a submodule contains can access entities in the scope of the submodule. The declarations and definitions of entities in a submodule are also accessible by host association in its descendant submodules.
A submodule cannot be accessed by use association.
A local variable of a submodule is accessible only in that submodule scoping unit, in any contained scoping units, or in its descendant submodules.
A variable, common block, or procedure pointer that is declared in the scoping unit of a submodule implicitly has the SAVE attribute, which might be confirmed by explicit specification.
- It cannot contain statement function statements, ENTRY statements, or FORMAT statements, although these statements can appear in the specification part of a module subprogram.
- If a specification expression or constant expression includes a reference to a generic entity, that generic entity cannot have specific procedures that are defined in the submodule subsequent to the specification expression or constant expression.
Example
In this example, two separate module procedures are defined in the submodule n
by a subroutine subprogram with the MODULE prefix specifier and by a
separate module subprogram. Their corresponding module procedure interface bodies are declared in
the ancestor module m
.
MODULE m ! The ancestor module m
INTEGER :: i
INTERFACE
MODULE SUBROUTINE sub1(arg1) ! Module procedure interface body for sub1
INTEGER :: arg1
END SUBROUTINE
MODULE SUBROUTINE sub2(arg2) ! Module procedure interface body for sub2
INTEGER :: arg2
END SUBROUTINE
END INTERFACE
END MODULE
SUBMODULE (m) n ! The descendant submodule n
INTEGER :: j ! Specification part
CONTAINS ! Module subprogram part
MODULE SUBROUTINE sub1(arg1) ! Definition of sub1 by subroutine subprogram
INTEGER :: arg 1
arg1 = 1
i = 2 ! Host association
j = 3 ! Host association
END SUBROUTINE
MODULE PROCEDURE sub2 ! Definition of sub2 by separate module subprogram
arg2 = 1
END PROCEDURE
END SUBMODULE