Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

Host Association

Host association allows the entities in a host scoping unit to be accessible to an internal subprogram, a module subprogram, or submodule program. This association remains in effect throughout the execution of the program.

The following also have access to entities from its host:

  • A module procedure interface body

  • A derived-type definition

  • An interface body that is not a separate interface body has access to the named entities from its host that are made accessible by IMPORT statements in the interface body.

The accessed entities are known by the same name and have the same attributes as in the host, except that a local entity can have the ASYNCHRONOUS attribute even if the host entity does not, and a non-coarray local entity can have the VOLATILE attribute even if the host entity does not. The accessed entities can be named data objects, derived types, abstract interfaces, procedures, generic identifiers, and NAMELIST groups.

Entities that are local to a procedure are not accessible to their hosts.

The following are considered local identifiers within a scoping unit so they are not accessible to their hosts:

  • A function name in a statement function, an entity declaration in a type declaration statement, or the name of an entity declared by an interface body, unless any of these are global identifiers.

  • An object name in an entity declaration in a type declaration statement or a POINTER, SAVE, ALLOCATABLE, or TARGET statement.

  • A type name in a derived type statement.

  • A procedure pointer that has the EXTERNAL attribute.

  • The name of a variable that is wholly or partially initialized in a DATA statement.

  • The name of an object that is wholly or partially initialized in an EQUIVALENCE statement.

  • A NAMELIST group name in a NAMELIST statement.

  • A generic name in a generic specification in an INTERFACE statement.

  • The name of a named construct.

  • The name of a dummy argument in a FUNCTION, SUBROUTINE, or ENTRY statement, or a statement function.

  • A named constant defined in a PARAMETER statement.

  • An array name in a DIMENSION statement.

  • A variable name in a common block object in a COMMON statement.

  • A result name in a FUNCTION or ENTRY statement.

  • An intrinsic procedure name in an INTRINSIC statement.

A name that appears in an ASYNCHRONOUS or VOLATILE statement is not necessarily the name of a local variable. In an internal or module procedure, if a variable that is accessible via host association is specified in an ASYNCHRONOUS or VOLATILE statement, that host variable is given the ASYNCHRONOUS or VOLATILE attribute in the local scope.

If an intrinsic procedure is accessed by means of host association, it must be established to be intrinsic in the host scoping unit by one of the following methods:

  • It must be explicitly given the INTRINSIC attribute.

  • It must be invoked as an intrinsic procedure.

  • It must be accessed from a module or from its host where it is established to be intrinsic.

If a procedure gains access to a pointer by host association, the association of the pointer with a target that is current at the time the procedure is invoked remains current within the procedure. This pointer association can be changed within the procedure. After execution of the procedure, the pointer association remains current, unless the execution caused the target to become undefined. If this occurs, the host associated pointer becomes undefined.

NOTE:

Implicit declarations can cause problems for host association. It is recommended that you use IMPLICIT NONE in both the host and the contained procedure, and that you explicitly declare all entities.

When all entities are explicitly declared, local declarations override host declarations, and host declarations that are not overridden are available in the contained procedure.

Examples

The following example shows how a host and an internal procedure can use host-associated entities:

 program INTERNAL
 ! shows use of internal subroutine and CONTAINS statement
    real a,b,c
    call find
    print *, c
 contains
    subroutine find
      read *, a,b
      c = sqrt(a**2 + b**2)
    end subroutine find
 end

In this case, the variables a, b, and c are available to the internal subroutine find through host association. They do not have to be passed as arguments to the internal procedure. In fact, if they are, they become local variables to the subroutine and hide the variables declared in the host program.

Conversely, the host program knows the value of c, when it returns from the internal subroutine that has defined c.

See Also