1 <!--===- docs/RuntimeTypeInfo.md
3 Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 See https://llvm.org/LICENSE.txt for license information.
5 SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9 # The derived type runtime information table
19 Many operations on derived types must be implemented, or can be
20 implemented, with calls to the runtime support library rather than
21 directly with generated code.
22 Some operations might be initially implemented in the runtime library
23 and then reimplemented later in generated code for compelling
24 performance gains in optimized compilations.
26 The runtime library uses *derived type description* tables to represent
27 the relevant characteristics of derived types.
28 This note summarizes the requirements for these descriptions.
30 The semantics phase of the F18 frontend constructs derived type
31 descriptions from its scoped symbol table after name resolution
32 and semantic constraint checking have succeeded.
33 The lowering phase then transfers the tables to the static
34 read-only data section of the generated program by translating them into
36 During execution, references to the tables occur by passing their addresses
37 as arguments to relevant runtime library APIs and as pointers in
38 the addenda of descriptors.
42 The following Fortran language features require, or may require, the use of
43 derived type descriptions in the runtime library.
47 The components of a derived type need to be described in component
48 order (7.4.7), but when there is a parent component, its components
49 can be described by reference to the description of the type of the
52 The ordered component descriptions are needed to implement
53 * default initialization
54 * `ALLOCATE`, with and without `SOURCE=`
55 * intrinsic assignment of derived types with `ALLOCATABLE` and
57 * intrinsic I/O of derived type instances
58 * `NAMELIST` I/O of derived type instances
61 The characteristics of data components include their names, types,
62 offsets, bounds, cobounds, derived type descriptions when appropriate,
63 default component initializers, and flags for `ALLOCATABLE`, `POINTER`,
64 `PRIVATE`, and automatic components (implicit allocatables).
65 Procedure pointer components require only their offsets and address(es).
67 ### Calls to type-bound procedures
69 Only extensible derived types -- those without `SEQUENCE` or `BIND(C)`
70 -- are allowed to have type-bound procedures.
71 Calls to these bindings will be resolved at compilation time when
72 the binding is `NON_OVERRIDABLE` or when an object is not polymorphic.
73 Calls to overridable bindings of polymorphic objects requires the
74 use of a runtime table of procedure addresses.
76 Each derived type (or instantiation of a parameterized derived type)
77 will have a complete type-bound procedure table in which all of the
78 bindings of its ancestor types appear first.
79 (Specifically, the table offsets of any inherited bindings must be
80 the same as they are in the table of the ancestral type's table.)
81 These ancestral bindings reflect their overrides, if any.
83 The non-inherited bindings of a type then follow the inherited
84 bindings, and they do so in alphabetical order of binding name.
85 (This is an arbitrary choice -- we could also define them to
86 appear in binding declaration order, I suppose -- but a consistent
87 ordering should be used so that relocatables generated by distinct
88 versions of the F18 compiler will have a better chance to interoperate.)
90 ### Type parameter values and "same type" testing
92 The values of the `KIND` and `LEN` parameters of a particular derived type
93 instance can be obtained to implement type parameter inquiries without
94 requiring derived type information tables.
95 In the case of a `KIND` type parameter, it's a constant value known at
96 compilation time, and in the case of a `LEN` type parameter, it's a
97 member of the addendum to the object's descriptor.
99 The runtime library will have an API (TBD) to be called as
100 part of the implementation of `TYPE IS` and `CLASS IS` guards
101 of the `SELECT TYPE` construct.
102 This language support predicate returns a true result when
103 an object's type matches a particular type specification and
104 `KIND` (but not `LEN`) type parameter values.
106 Note that this "is same type as" predicate is *not* the same as
107 the one to be called to implement the `SAME_TYPE_AS()` intrinsic function,
108 which is specified so as to *ignore* the values of `KIND` type
111 Subclause 7.5.2 defines what being the "same" derived type means
113 In short, each definition of a derived type defines a distinct type,
114 so type equality testing can usually compare addresses of derived
115 type descriptions at runtime.
116 The exceptions are `SEQUENCE` types and interoperable (`BIND(C)`)
118 Independent definitions of each of these are considered to be the "same type"
119 when these definitions match in terms of names, types, and attributes,
120 both being either `SEQUENCE` or `BIND(C)`, and containing
121 no `PRIVATE` components.
122 These "sequence" derived types cannot have type parameters, type-bound
123 procedures, an absence of components, or components that are not themselves
124 of a sequence type, so we can use a static hash code to implement
125 their "same type" tests.
127 ### FINAL subroutines
129 When an instance of a derived type is deallocated or goes out of scope,
130 one of its `FINAL` subroutines may be called.
131 Subclause 7.5.6.3 defines when finalization occurs -- it doesn't happen
134 The subroutines named in a derived type's `FINAL` statements are not
135 bindings, so their arguments are not passed object dummy arguments and
136 do not have to satisfy the constraints of a passed object.
137 Specifically, they can be arrays, and cannot be polymorphic.
138 If a `FINAL` subroutine's dummy argument is an array, it may be
139 assumed-shape or assumed-rank, but it could also be an explicit-shape
140 or assumed-size argument.
141 This means that it may or may not be passed by means of a descriptor.
143 Note that a `FINAL` subroutine with a scalar argument does not define
144 a finalizer for array objects unless the subroutine is elemental
145 (and probably `IMPURE`).
146 This seems to be a language pitfall and F18 will emit a
147 warning when an array of a finalizable derived type is declared
148 with a rank lacking a `FINAL` subroutine when other ranks do have one.
150 So the necessary information in the derived type table for a `FINAL`
151 subroutine comprises:
152 * address(es) of the subroutine
153 * rank of the argument, or whether it is assumed-rank
154 * for rank 0, whether the subroutine is elemental
155 * for rank > 0, whether the argument requires a descriptor
157 This descriptor flag is needed to handle a difficult case with
158 `FINAL` subroutines that most other implementations of Fortran
159 fail to get right: a `FINAL` subroutine
160 whose argument is a an explicit shape or assumed size array may
161 have to be called upon the parent component of an array of
162 an extended derived type.
171 type, extends(parent) :: extended
181 type(extended) :: arr(1)
185 If the `FINAL` subroutine doesn't use a descriptor -- and it
186 will not if there are no `LEN` type parameters -- the runtime
187 will have to allocate and populate a temporary array of copies
188 elements of the parent component of the array so that it can
189 be passed by reference to the `FINAL` subroutine.
191 ### Defined assignment
193 A defined assignment subroutine for a derived type can be declared
194 by means of a generic `INTERFACE ASSIGNMENT(=)` and by means of
195 a generic type-bound procedure.
196 Defined assignments with non-type-bound generic interfaces are
197 resolved to specific subroutines at compilation time.
198 Most cases of type-bound defined assignment are resolved to their
199 bindings at compilation time as well (with possible runtime
200 resolution of overridable bindings).
202 Intrinsic assignment of derived types with components that have
203 derived types with type-bound generic assignments is specified
204 by subclause 10.2.1.3 paragraph 13 as invoking defined assignment
205 subroutines, however.
207 This seems to be the only case of defined assignment that may be of
208 interest to the runtime library.
209 If this is correct, then the requirements are somewhat constrained;
210 we know that the rank of the target of the assignment must match
211 the rank of the source, and that one of the dummy arguments of the
212 bound subroutine is a passed object dummy argument and satisfies
213 all of the constraints of one -- in particular, it's scalar and
216 So the derived type information for a defined assignment needs to
218 * address(es) of the subroutine
219 * whether the first, second, or both arguments are descriptors
220 * whether the subroutine is elemental (necessarily also impure)
222 ### User defined derived type I/O
224 Fortran programs can specify subroutines that implement formatted and
225 unformatted `READ` and `WRITE` operations for derived types.
226 These defined I/O subroutines may be specified with an explicit `INTERFACE`
227 or with a type-bound generic.
228 When specified with an `INTERFACE`, the first argument must not be
229 polymorphic, but when specified with a type-bound generic, the first
230 argument is a passed-object dummy argument and required to be so.
231 In any case, the argument is scalar.
233 Nearly all invocations of user defined derived type I/O subroutines
234 are resolved at compilation time to specific procedures or to
235 overridable bindings.
236 (The I/O library APIs for acquiring their arguments remain to be
238 The case that is of interest to the runtime library is that of
239 NAMELIST I/O, which is specified to invoke user defined derived
240 type I/O subroutines if they have been defined.
242 The derived type information for a user defined derived type I/O
243 subroutine comprises:
244 * address(es) of the subroutine
245 * whether it is for a read or a write
246 * whether it is formatted or unformatted
247 * whether the first argument is a descriptor (true if it is a
248 binding of the derived type, or has a `LEN` type parameter)
250 ## Exporting derived type descriptions from module relocatables
252 Subclause 7.5.2 requires that two objects be considered as having the
253 same derived type if they are declared "with reference to the same
254 derived type definition".
255 For derived types that are defined in modules and accessed by means
256 of use association, we need to be able to describe the type in the
257 read-only static data section of the module and access the description
258 as a link-time external.
260 This is not always possible to achieve in the case of instantiations
261 of parameterized derived types, however.
262 Two identical instantiations in distinct compilation units of the same
263 use associated parameterized derived type seem impractical to implement
264 using the same address.
265 (Perhaps some linkers would support unification of global objects
266 with "mangled" names and identical contents, but this seems unportable.)
268 Derived type descriptions therefore will contain pointers to
269 their "uninstantiated" original derived types.
270 For derived types with no `KIND` type parameters, these pointers
271 will be null; for uninstantiated derived types, these pointers
272 will point at themselves.