To: J3 J3/25-110 From: generics Subject: Edits for TEMPLATES: Instantiation Date: 2025-February-03 References: 25-107, 25-108, 25-109, 24-125r5, 24-126r4, 24-127r4 Introduction: ============= This is the 4th of 6 papers that provide edits for the approved syntax for templates. Section 1: ========== * Append the following at the end of the clause from paper 25-109. tt.6 Instantiation tt.6.1 The INSTANTIATE statement An INSTANTIATE statement is a specification statement that identifies an instance of a template by specifying instantiation arguments that become associated with the deferred arguments of the named template. Rtt24 <> INSTANTIATE [::] ( [ ] ) [, ] <> INSTANTIATE [::] ( [ ] ), ONLY : [ ] Ctt39 (Rtt24) The shall be the name of a template that is not a templated procedure. Rtt25 <> INSTANTIATE :: => ( [ ] ) Ctt40 (Rtt25) The shall be the name of a templated procedure. Rtt26 <> <> Ctt41 (Rtt26) Within an , an shall not depend on any entity defined within the referenced template. Ctt42 (Rtt26) A shall not be the name of any construct in which it appears. The INSTANTIATE statement without the ONLY option provides access to all public entities of the referenced template. The INSTANTIATE statement with the ONLY option provides access only to those entities that appear as s, s, or s in the only list. An accessible entity of the referenced instantiation is associated with one or more accessed entities, each with its own identifier. These identifiers are - the identifier of the entity in the referenced template if that identifier appears as an or as the of a in any for that instantiation, - each of the s or s that the entity is given in any for that instantiation, and - the identifier of the entity in that referenced template if that identifier does not appear as a or in any for that instantiation. tt.6.2 Inline instantiation of templated procedures A templated procedure can be instantiated and referenced in an expression or the in a . Rtt27 <> ^ ( ) Ctt43 (Rtt27) The shall be the name of a templated procedure. Ctt44 (Rtt27) The shall not be the name of any construct in which it appears. The procedure designated by is the procedure produced from instantiating the templated procedure. NOTE Templated procedures cannot reference themselves. tt.6.3 Interpretation of template instantiation Multiple instantiations of a given template with the same actual instantiation arguments identify the same instance of the referenced template. NOTE 1 As a consequence, if a template defines a derived type, two identical instantiations of that template define the same type. This provides a mechanism for derived types from templates to be compatible across scoping units in a convenient manner. For example: TEMPLATE TMPL(T) DEFERRED TYPE :: T TYPE :: list_t TYPE(T), ALLOCATABLE :: elements(:) END TYPE END TEMPLATE ... SUBROUTINE SUB(list) INSTANTIATE TMPL(integer) TYPE(list_t) :: list END SUBROUTINE SUBROUTINE DRIVER() INSTANTIATE TMPL(integer) TYPE(list_t) :: list ! list_t is same type as in SUB CALL SUB(list) END SUBROUTINE Two corresponding constant instantiation arguments are the same if and only if both have the same shape, same type, same type parameters, and are equal. Two corresponding type-spec instantiation arguments are the same if and only if both have the same type and have the same kind and length type parameters. Two corresponding procedure instantiation arguments are the same if and only if both resolve to the same specific procedure. NOTE 2 Example showing how procedure instantiation arguments influence whether instantiations are the same: INTERFACE SUBROUTINE F1(x) TYPE(MY_T) :: x END SUBROUTINE SUBROUTINE F2(x) TYPE(MY_U) :: x END SUBROUTINE SUBROUTINE F3(x) TYPE(MY_U) :: x END SUBROUTINE END INTERFACE GENERIC :: A => F1, F2 GENERIC :: B => F1, F3 TEMPLATE TMPL(T, F) DEFERRED TYPE :: T DEFERRED INTERFACE SUBROUTINE F(x) TYPE(T), INTENT(INOUT) :: x END SUBROUTINE F END INTERFACE END TEMPLATE INSTANTIATE TMPL(MY_T, A) ! Resolves to F1 INSTANTIATE TMPL(MY_T, B) ! Resolves to F1 ==> same INSTANTIATE TMPL(MY_U, A) ! Resolves to F2 INSTANTIATE TMPL(MY_U, B) ! Resolves to F3 ==> different tt.6.4 Deferred argument association tt.6.4.1 Instantiation arguments Instantiation arguments are specified by an INSTANTIATE statement, a REQUIRE statement, or by inline instantiation. Rtt28 <> [ = ] Rtt29 <> <> <> <> Ctt45 (Rtt28) The = shall not be omitted from an unless it has been omitted from each preceding in the argument list. Ctt46 (Rtt28) Each shall be the name of a deferred argument in the referenced requirement or template. The instantiation argument list identifies the correspondence between the instantiation arguments and the deferred arguments of the referenced template or requirement. This correspondence can be established either by keyword or by position. If an argument keyword appears, the instantiation argument corresponds to the deferred argument whose name is the same as the argument keyword. In the absence of an argument keyword, an instantiation argument corresponds to the deferred argument occupying the corresponding position in the deferred argument list; that is, the first instantiation argument corresponds to the first deferred argument in the deferred argument list, the second instantiation instantiation argument corresponds to the second deferred argument, etc. Each instantiation argument shall correspond to a deferred argument, and exactly one instantiation argument shall correspond to each deferred argument. The entity that is associated with a deferred argument is called its effective instantiation argument. tt.6.4.2 Deferred type association Ctt47 (Rtt29) An that corresponds to a deferred type shall be a . Ctt48 (Rtt29) A shall specify constant type parameters. Ctt49 (Rtt29) A shall not specify an abstract type if its corresponding deferred type does not have the ABSTRACT attribute. { Judging by C7109 & C7110, just saying "its corresponding deferred type" should be sufficient and unambiguous in context. } Ctt50 (Rtt29) A shall specify an extensible derived type if its corresponding deferred type has the EXTENSIBLE attribute. Ctt51 (Rtt29) A shall not specify EVENT_TYPE, LOCK_TYPE, or NOTIFY_TYPE, or specify a type that has a potential subobject component of type EVENT_TYPE, LOCK_TYPE, or NOTIFY_TYPE. NOTE 1 Constraint Ctt51 ensures that intrinsic assignment is available for variables of deferred type. {A number of places in the standard have a similar exception for these three types. A separate edit paper could perhaps introduce a name for this category of types for which intrinsic assignment is not permitted so that all these constraints can reference a shared term.} Ctt52 (Rtt29) A shall not specify a type with a coarray potential subobject component. NOTE 2 Constraint Ctt52 avoids the possibility of assignment being invalid where the variable and expr do not agree on the allocation status of a coarray component. A deferred type becomes associated with the type and type parameters identified by its corresponding instantiation argument. NOTE 3 Non-abstract, extensible derived types can be associated with both abstract and non-extensible deferred type arguments. NOTE 4 Intrinsic types, SEQUENCE types, and types with the BIND attribute cannot be associated with deferred type arguments that have the EXTENSIBLE attribute. Simple example illustrating the above. TYPE :: MY_T1 END TYPE TYPE, ABSTRACT :: MY_T2 END TYPE TEMPLATE TMPL1(T) DEFERRED TYPE :: T END TEMPLATE TMPL TEMPLATE TMPL2(U) DEFERRED TYPE, ABSTRACT :: U END TEMPLATE TMPL INSTANTIATE TMPL1(INTEGER) ! ok INSTANTIATE TMPL1(MY_T1) ! ok INSTANTIATE TMPL1(MY_T2) ! invalid INSTANTIATE TMPL2(INTEGER) ! invalid INSTANTIATE TMPL2(MY_T1) ! ok INSTANTIATE TMPL2(MY_T2) ! ok tt.6.4.3 Deferred constant association Ctt53 (Rtt29) An that corresponds to a deferred constant shall be a . Ctt54 (Rtt29) The type and kind type parameters of a shall be the same as the type and kind type parameter of its corresponding deferred constant. Ctt55 (Rtt29) If the shape of the corresponding deferred constant is not implied, then the shall have the same shape. Ctt56 (Rtt29) If the rank of the corresponding deferred constant is not implied, then the shall have the same rank. The value of a deferred constant becomes associated with the value of the constant expression in the corresponding instantiation argument. tt.6.4.4 Deferred procedure association Ctt57 (Rtt29) An that corresponds to a deferred procedure shall be a or . Ctt58 (Rtt29) A shall denote a nonpointer, nonintrinsic procedure that has an explicit interface. Ctt59 (Rtt29) The procedure specified by a shall have the same characteristics as its corresponding deferred procedure, except that a pure procedure may correspond to a deferred procedure that is not pure, a simple procedure may correspond to a deferred procedure that is not simple, and an elemental procedure may correspond to a deferred procedure that is not elemental. Ctt60 (Rtt29) The generic identifier specified by a shall have exactly one specific procedure that has the same characteristics as the corresponding deferred procedure, except that a pure specific procedure may correspond to a deferred procedure that is not pure, a simple specific procedure may correspond to a deferred procedure that is not simple, and an elemental specific procedure may correspond to a deferred procedure that is not elemental. Ctt61 (Rtt29) A shall not be the name of a generic identifier. {Ctt61 is a disambiguating constraint.} If a deferred procedure corresponds to a specific procedure name, it becomes associated with that named procedure. If a deferred procedure corresponds to a nonintrinsic generic identifier, it becomes associated with the specific procedure that is a member of that generic identifier that has the same characteristics except whether it is PURE, SIMPLE, or ELEMENTAL. If a deferred procedure corresponds to an intrinsic generic identifier, it becomes associated with that intrinsic identifier. Ctt62 If a deferred procedure corresponds to an instantiation argument that is an intrinsic procedure or procedure from an intrinsic module, it shall be valid for that procedure to appear where the deferred procedure is referenced. NOTE If a generic identifier and a specific procedure have the same name, the generic identifier takes precedence. ===END===