BIND attribute
Standard: F77 F90 F95 F2003 F2008 Example program
<Syntax (variables and common blocks)>
type, BIND(C [ , NAME=expression ] ) [ , attr ] :: decl-list
BIND ( C [ , NAME=expression ] ) [ :: ] entity [ , entity ]...
entity ::= variable-name | / common-block-name /
The expression must be a scalar constant expression of type character with default kind.
For a variable, this declaration is only allowed in the specification part of a module.
For a common block, this attribute implies the SAVE attribute (which can be confirmed explicitly). A common block with this attribute must be declared with the attribute everywhere in the program.
If NAME=name appears, only one entity or decl is allowed.
<Syntax (types and procedures)>
The BIND attribute for a derived type appears on the TYPE statement. The BIND attribute for a procedure appears on the FUNCTION statement or SUBROUTINE statement in an external, internal, or module subprogram (when defining the procedure) or in an interface body (when declaring an external procedure or dummy procedure with that attribute). Prior to Fortran 2008, an internal procedure was not permitted to have the BIND attribute.
The NAME= specifier is not allowed for an internal procedure, or for the interface of a dummy procedure. It is also not allowed for an abstract interface (ABSTRACT keyword on the INTERFACE statement).
<Semantics>
The BIND attribute specifies that the entity interoperates with C.
If NAME= appears, the character constant expression specifies the C name of the entity; this is either a C global variable or a C “extern” procedure. If the procedure is an internal or dummy procedure, or an abstract interface, it has no C name; otherwise, if NAME= does not appear, the C name is the lowercase name of the Fortran entity. If NAME= appears but the character constant expression is blank, the Fortran entity does not have a C name.
Note that for a procedure or common block with a C name, whether specified by NAME= or by default, the global identifier of that entity is the C name; the Fortran name is merely a local identifier. (Prior to Fortran 2008, the Fortran name of an external procedure or common block was also considered to be a global identifier.)
<Semantics (variables and common blocks)>
A variable with the BIND attribute interoperates with a C global variable. The variable must be “interoperable”; this means that it must not have the ALLOCATABLE or POINTER attribute, if it is of intrinsic type it must have interoperable kind, and if it is of derived type the type must be interoperable. Named constants for interoperable intrinsic kinds are found in the ISO_C_BINDING module. User-defined types with the BIND attribute are interoperable, as are the special derived types C_PTR and C_FUNPTR which are found in the ISO_C_BINDING module.
A common block with the BIND attribute interoperates with a C global variable that is a “structure”; this is functionally equivalent to defining a type with the common block elements as its components, and having a variable of that type. This feature is error-prone and its use is not recommended.
<Semantics (procedures)>
In the case of a procedure, the use of the BIND attribute on a subprogram heading or ENTRY statement defines a Fortran procedure that is accessible from C. The use of the BIND attribute on a FUNCTION or SUBROUTINE statement that begins an interface body declares, in the absence of a matching Fortran procedure, a C procedure that can be accessed from Fortran.
A procedure with the BIND attribute must
- be a subroutine or a scalar function of interoperable type and kind, and
- have all of its dummy arguments interoperable.
A dummy argument is interoperable if it is of interoperable type, does not have the ALLOCATABLE or POINTER attributes, and is either scalar or an explicit-shape or assumed-size array.
<Semantics (derived types)>
In the case of a derived type, the components must all be of interoperable type and kind, and must not be pointers or allocatable. No type-bound procedure part is allowed in the type definition.
The SEQUENCE attribute is not allowed, but types with the BIND attribute act like sequence types in every other way (in particular, they follow the same rules for type matching).
<Related>
Attributes, Derived Type Definition, FUNCTION statement, SUBROUTINE statement, TYPE statement, Type Declaration statement