1<!--===- docs/Calls.md
2
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
6
7-->
8
9# Representation of Fortran function calls
10
11```eval_rst
12.. contents::
13   :local:
14```
15
16## Procedure reference implementation protocol
17
18Fortran function and subroutine references are complicated.
19This document attempts to collect the requirements imposed by the 2018
20standard (and legacy extensions) on programs and implementations, work
21through the implications of the various features, and propose both a
22runtime model and a compiler design.
23
24All section, requirement, and constraint numbers herein pertain to
25the Fortran 2018 standard.
26
27This note does not consider calls to intrinsic procedures, statement
28functions, or calls to internal runtime support library routines.
29
30## Quick review of terminology
31
32* A *dummy argument* is a function or subroutine parameter.
33  It is *associated* with an *effective argument* at each call
34  to the procedure.
35* The *shape* of an array is a vector containing its extent (size)
36  on each dimension; the *rank* of an array is the number of its
37  dimensions (i.e., the shape of its shape).
38  The absolute values of the lower and upper bounds of the dimensions
39  of an array are not part of its shape, just their difference (plus 1).
40* An *explicit-shape* array has all of its bounds specified; lower
41  bounds default to 1.  These can be passed by with a single address
42  and their contents are contiguous.
43* An *assumed-size* array is an explicit-shape array with `*` as its
44  final dimension, which is the most-significant one in Fortran
45  and whose value does not affect indexed address calculations.
46* A *deferred-shape* array (`DIMENSION::A(:)`) is a `POINTER` or `ALLOCATABLE`.
47  `POINTER` target data might not be contiguous.
48* An *assumed-shape* (not size!) array (`DIMENSION::A(:)`) is a dummy argument
49  that is neither `POINTER` nor `ALLOCATABLE`; its lower bounds can be set
50  by the procedure that receives them (defaulting to 1), and its
51  upper bounds are functions of the lower bounds and the extents of
52  dimensions in the *shape* of the effective argument.
53* An *assumed-length* `CHARACTER(*)` dummy argument
54  takes its length from the effective argument.
55* An *assumed-length* `CHARACTER(*)` *result* of an external function (C721)
56  has its length determined by its eventual declaration in a calling scope.
57* An *assumed-rank* `DIMENSION::A(..)` dummy argument array has an unknown
58  number of dimensions.
59* A *polymorphic* `CLASS(t)` dummy argument, `ALLOCATABLE`, or `POINTER`
60  has a specific derived type or some extension of that type.
61  An *unlimited polymorphic* `CLASS(*)` object can have any
62  intrinsic or derived type.
63* *Interoperable* `BIND(C)` procedures are written in C or callable from C.
64
65## Interfaces
66
67Referenced procedures may or may not have declared interfaces
68available to their call sites.
69
70Procedures with some post-Fortran '77 features *require* an
71explicit interface to be called (15.4.2.2) or even passed (4.3.4(5)):
72
73* use of argument keywords in a call
74* procedures that are `ELEMENTAL` or `BIND(C)`
75* procedures that are required to be `PURE` due to the context of the call
76  (specification expression, `DO CONCURRENT`, `FORALL`)
77* dummy arguments with these attributes: `ALLOCATABLE`, `POINTER`,
78  `VALUE`, `TARGET`, `OPTIONAL`, `ASYNCHRONOUS`, `VOLATILE`,
79  and, as a consequence of limitations on its use, `CONTIGUOUS`;
80  `INTENT()`, however, does *not* require an explicit interface
81* dummy arguments that are coarrays
82* dummy arguments that are assumed-shape or assumed-rank arrays
83* dummy arguments with parameterized derived types
84* dummy arguments that are polymorphic
85* function result that is an array
86* function result that is `ALLOCATABLE` or `POINTER`
87* `CHARACTER` function result whose length is neither constant
88  nor assumed
89* derived type function result with `LEN` type parameter value that is
90  not constant
91  (note that result derived type parameters cannot be assumed (C795))
92
93Module procedures, internal procedures, procedure pointers,
94type-bound procedures, and recursive references by a procedure to itself
95always have explicit interfaces.
96(Consequently, they cannot be assumed-length `CHARACTER(*)` functions;
97conveniently, assumed-length `CHARACTER(*)` functions are prohibited from
98recursion (15.6.2.1(3))).
99
100Other uses of procedures besides calls may also require explicit interfaces,
101such as procedure pointer assignment, type-bound procedure bindings, &c.
102
103Note that non-parameterized monomorphic derived type arguments do
104*not* by themselves require the use of an explicit interface.
105However, dummy arguments with any derived type parameters *do*
106require an explicit interface, even if they are all `KIND` type
107parameters.
108
10915.5.2.9(2) explicitly allows an assumed-length `CHARACTER(*)` function
110to be passed as an actual argument to an explicit-length dummy;
111this has implications for calls to character-valued dummy functions
112and function pointers.
113(In the scopes that reference `CHARACTER` functions, they must have
114visible definitions with explicit result lengths.)
115
116### Implicit interfaces
117
118In the absence of any characteristic or context that *requires* an
119explicit interface (see above), an external function or subroutine (R503)
120or `ENTRY` (R1541) can be called directly or indirectly via its implicit interface.
121Each of the arguments can be passed as a simple address, including
122dummy procedures.
123Procedures that *can* be called via an implicit interface can
124undergo more thorough checking
125by semantics when an explicit interface for them exists, but they must be
126compiled as if all calls to them were through the implicit interface.
127This note will mention special handling for procedures that are exposed
128to the possibility of being called with an implicit interface as *F77ish* procedures
129below; this is of course not standard terminology.
130
131Internal and module subprograms that are ever passed as arguments &/or
132assigned as targets of procedure pointers may be F77ish.
133
134Every F77ish procedure can and must be distinguished at compilation time.
135Such procedures should respect the external naming conventions (when external)
136and any legacy ABI used for Fortran '77 programs on the target architecture,
137so that portable libraries can be compiled
138and used by distinct implementations (and their versions)
139of Fortran.
140
141Note that F77ish functions still have known result types, possibly by means
142of implicit typing of their names.
143They can also be `CHARACTER(*)` assumed-length character functions.
144
145In other words: these F77sh procedures that do not require the use of an explicit
146interface and that can possibly be referenced, directly or indirectly,
147with implicit interfaces are limited to argument lists that comprise
148only the addresses of effective arguments and the length of a `CHARACTER` function result
149(when there is one), and they can return only scalar values with constant
150type parameter values.
151None of their arguments or results need be (or can be) implemented
152with descriptors,
153and any internal procedures passed to them as arguments must be
154simple addresses of non-internal subprograms or trampolines for
155internal procedures.
156
157Note that the `INTENT` attribute does not, by itself,
158require the use of explicit interface; neither does the use of a dummy
159procedure (implicit or explicit in their interfaces).
160So the analysis of calls to F77ish procedures must allow for the
161invisible use of `INTENT(OUT)`.
162
163## Protocol overview
164
165Here is a summary script of all of the actions that may need to be taken
166by the calling procedure and its referenced procedure to effect
167the call, entry, exit, and return steps of the procedure reference
168protocol.
169The order of these steps is not particularly strict, and we have
170some design alternatives that are explored further below.
171
172### Before the call:
173
1741. Compute &/or copy into temporary storage the values of
175   some effective argument expressions and designators (see below).
1761. Create and populate descriptors for arguments that use them
177   (see below).
1781. Possibly allocate function result storage,
179   when its size can be known by all callers; function results that are
180   neither `POINTER` nor `ALLOCATABLE` must have explicit shapes (C816).
1811. Create and populate a descriptor for the function result, if it
182   needs one (deferred-shape/-length `POINTER`, any `ALLOCATABLE`,
183   derived type with non-constant length parameters, &c.).
1841. Capture the values of host-escaping local objects in memory;
185   package them into single address (for calls to internal procedures &
186   for calls that pass internal procedures as arguments).
1871. Resolve the target procedure's polymorphic binding, if any.
1881. Marshal effective argument addresses (or values for `%VAL()` and some
189   discretionary `VALUE` arguments) into registers.
1901. Marshal `CHARACTER` argument lengths in additional value arguments for
191   `CHARACTER` effective arguments not passed via descriptors.
192   These lengths must be 64-bit integers.
1931. Marshal an extra argument for the length of a `CHARACTER` function
194   result if the function is F77ish.
1951. Marshal an extra argument for the function result's descriptor,
196   if it needs one.
1971. Set the "host instance" (static link) register when calling an internal
198   procedure from its host or another internal procedure, a procedure pointer,
199   or dummy procedure (when it has a descriptor).
2001. Jump.
201
202### On entry:
2031. For subprograms with alternate `ENTRY` points: shuffle `ENTRY` dummy arguments
204   set a compiler-generated variable to identify the alternate entry point,
205   and jump to the common entry point for common processing and a `switch()`
206   to the statement after the `ENTRY`.
2071. Capture `CHARACTER` argument &/or assumed-length result length values.
2081. Complete `VALUE` copying if this step will not always be done
209   by the caller (as I think it should be).
2101. Finalize &/or re-initialize `INTENT(OUT)` non-pointer
211   effective arguments (see below).
2121. For interoperable procedures called from C: compact discontiguous
213   dummy argument values when necessary (`CONTIGUOUS` &/or
214   explicit-shape/assumed-size arrays of assumed-length `CHARACTER(*)`).
2151. Optionally compact assumed-shape arguments for contiguity on one
216   or more leading dimensions to improve SIMD vectorization, if not
217   `TARGET` and not already sufficiently contiguous.
218   (PGI does this in the caller, whether the callee needs it or not.)
2191. Complete allocation of function result storage, if that has
220   not been done by the caller.
2211. Initialize components of derived type local variables,
222   including the function result.
223
224Execute the callee, populating the function result or selecting
225the subroutine's alternate return.
226
227### On exit:
2281. Clean up local scope (finalization, deallocation)
2291. Deallocate `VALUE` argument temporaries.
230   (But don't finalize them; see 7.5.6.3(3)).
2311. Replace any assumed-shape argument data that were compacted on
232   entry for contiguity when the data were possibly
233   modified across the call (never when `INTENT(IN)` or `VALUE`).
2341. Identify alternate `RETURN` to caller.
2351. Marshal results.
2361. Jump
237
238### On return to the caller:
2391. Save the result registers, if any.
2401. Copy effective argument array designator data that was copied into
241   a temporary back into its original storage (see below).
2421. Complete deallocation of effective argument temporaries (not `VALUE`).
2431. Reload definable host-escaping local objects from memory, if they
244   were saved to memory by the host before the call.
2451. `GO TO` alternate return, if any.
2461. Use the function result in an expression.
2471. Eventually, finalize &/or deallocate the function result.
248
249(I've omitted some obvious steps, like preserving/restoring callee-saved
250registers on entry/exit, dealing with caller-saved registers before/after
251calls, and architecture-dependent ABI requirements.)
252
253## The messy details
254
255### Copying effective argument values into temporary storage
256
257There are several conditions that require the compiler to generate
258code that allocates and populates temporary storage for an actual
259argument.
260
261First, effective arguments that are expressions, not designators, obviously
262need to be computed and captured into memory in order to be passed
263by reference.
264This includes parenthesized designators like `(X)`, which are
265expressions in Fortran, as an important special case.
266(This case also technically includes unparenthesized constants,
267but those are better implemented by passing addresses in read-only
268memory.)
269The dummy argument cannot be known to have `INTENT(OUT)` or
270`INTENT(IN OUT)`.
271
272Small scalar or elemental `VALUE` arguments may be passed in registers,
273as should arguments wrapped in the legacy VMS `%VAL()` notation.
274Multiple elemental `VALUE` arguments might be packed into SIMD registers.
275
276Effective arguments that are designators, not expressions, must also
277be copied into temporaries in the following situations.
278
2791. Coindexed objects need to be copied into the local image.
280   This can get very involved if they contain `ALLOCATABLE`
281   components, which also need to be copied, along with their
282   `ALLOCATABLE` components, and may be best implemented with a runtime
283   library routine working off a description of the type.
2841. Effective arguments associated with dummies with the `VALUE`
285   attribute need to be copied; this can be done on either
286   side of the call, but there are optimization opportunities
287   available when the caller's side bears the responsibility.
2881. In non-elemental calls, the values of array sections with
289   vector-valued subscripts need to be gathered into temporaries.
290   These effective arguments are not definable, and they are not allowed to
291   be associated with non-`VALUE` dummy arguments with the attributes
292   `INTENT(OUT)`, `INTENT(IN OUT)`, `ASYNCHRONOUS`, or `VOLATILE`
293   (15.5.2.4(21)); `INTENT()` can't always be checked.
2941. Non-simply-contiguous (9.5.4) arrays being passed to non-`POINTER`
295   dummy arguments that must be contiguous (due to a `CONTIGUOUS`
296   attribute, or not being assumed-shape or assumed-rank; this
297   is always the case for F77ish procedures).
298   This should be a runtime decision, so that effective arguments
299   that turn out to be contiguous can be passed cheaply.
300   This rule does not apply to coarray dummies, whose effective arguments
301   are required to be simply contiguous when this rule would otherwise
302   force the use of a temporary (15.5.2.8); neither does it apply
303   to `ASYNCHRONOUS` and `VOLATILE` effective arguments, which are
304   disallowed when copies would be necessary (C1538 - C1540).
305   *Only temporaries created by this contiguity requirement are
306   candidates for being copied back to the original variable after
307   the call* (see below).
308
309Fortran requires (18.3.6(5)) that calls to interoperable procedures
310with dummy argument arrays with contiguity requirements
311handle the compaction of discontiguous data *in the Fortran callee*,
312at least when called from C.
313And discontiguous data must be compacted on the *caller's* side
314when passed from Fortran to C (18.3.6(6)).
315
316We could perform all argument compaction (discretionary or
317required) in the callee, but there are many cases where the
318compiler knows that the effective argument data are contiguous
319when compiling the caller (a temporary is needed for other reasons,
320or the effective argument is simply contiguous) and a run-time test for
321discontiguity in the callee can be avoided by using a caller-compaction
322convention when we have the freedom to choose.
323
324While we are unlikely to want to _needlessly_ use a temporary for
325an effective argument that does not require one for any of these
326reasons above, we are specifically disallowed from doing so
327by the standard in cases where pointers to the original target
328data are required to be valid across the call (15.5.2.4(9-10)).
329In particular, compaction of assumed-shape arrays for discretionary
330contiguity on the leading dimension to ease SIMD vectorization
331cannot be done safely for `TARGET` dummies without `VALUE`.
332
333Effective arguments associated with known `INTENT(OUT)` dummies that
334require allocation of a temporary -- and this can only be for reasons of
335contiguity -- don't have to populate it, but they do have to perform
336minimal initialization of any `ALLOCATABLE` components so that
337the runtime doesn't crash when the callee finalizes and deallocates
338them.
339`ALLOCATABLE` coarrays are prohibited from being affected by `INTENT(OUT)`
340(see C846).
341Note that calls to implicit interfaces must conservatively allow
342for the use of `INTENT(OUT)` by the callee.
343
344Except for `VALUE` and known `INTENT(IN)` dummy arguments, the original
345contents of local designators that have been compacted into temporaries
346could optionally have their `ALLOCATABLE` components invalidated
347across the call as an aid to debugging.
348
349Except for `VALUE` and known `INTENT(IN)` dummy arguments, the contents of
350the temporary storage will be copied back into the effective argument
351designator after control returns from the procedure, and it may be necessary
352to preserve addresses (or the values of subscripts and cosubscripts
353needed to recalculate them) of the effective argument designator, or its
354elements, in additional temporary storage if they can't be safely or
355quickly recomputed after the call.
356
357### `INTENT(OUT)` preparation
358
359Effective arguments that are associated with `INTENT(OUT)`
360dummy arguments are required to be definable.
361This cannot always be checked, as the use of `INTENT(OUT)`
362does not by itself mandate the use of an explicit interface.
363
364`INTENT(OUT)` arguments are finalized (as if) on entry to the called
365procedure.  In particular, in calls to elemental procedures,
366the elements of an array are finalized by a scalar or elemental
367`FINAL` procedure (7.5.6.3(7)).
368
369Derived type components that are `ALLOCATABLE` are finalized
370and deallocated; they are prohibited from being coarrays.
371Components with initializers are (re)initialized.
372
373The preparation of effective arguments for `INTENT(OUT)` could be
374done on either side of the call.  If the preparation is
375done by the caller, there is an optimization opportunity
376in situations where unmodified incoming `INTENT(OUT)` dummy
377arguments whose types lack `FINAL` procedures are being passed
378onward as outgoing `INTENT(OUT)` arguments.
379
380### Arguments and function results requiring descriptors
381
382Dummy arguments are represented with the addresses of new descriptors
383when they have any of the following characteristics:
384
3851. assumed-shape array (`DIMENSION::A(:)`)
3861. assumed-rank array (`DIMENSION::A(..)`)
3871. parameterized derived type with assumed `LEN` parameters
3881. polymorphic (`CLASS(T)`, `CLASS(*)`)
3891. assumed-type (`TYPE(*)`)
3901. coarray dummy argument
3911. `INTENT(IN) POINTER` argument (15.5.2.7, C.10.4)
392
393`ALLOCATABLE` and other `POINTER` arguments can be passed by simple
394address.
395
396Non-F77ish procedures use descriptors to represent two further
397kinds of dummy arguments:
398
3991. assumed-length `CHARACTER(*)`
4001. dummy procedures
401
402F77ish procedures use other means to convey character length and host instance
403links (respectively) for these arguments.
404
405Function results are described by the caller & callee in
406a caller-supplied descriptor when they have any of the following
407characteristics, some which necessitate an explicit interface:
408
4091. deferred-shape array (so `ALLOCATABLE` or `POINTER`)
4101. derived type with any non-constant `LEN` parameter
411   (C795 prohibit assumed lengths)
4121. procedure pointer result (when the interface must be explicit)
413
414Storage for a function call's result is allocated by the caller when
415possible: the result is neither `ALLOCATABLE` nor `POINTER`,
416the shape is scalar or explicit, and the type has `LEN` parameters
417that are constant expressions.
418In other words, the result doesn't require the use of a descriptor
419but can't be returned in registers.
420This allows a function result to be written directly into a local
421variable or temporary when it is safe to treat the variable as if
422it were an additional `INTENT(OUT)` argument.
423(Storage for `CHARACTER` results, assumed or explicit, is always
424allocated by the caller, and the length is always passed so that
425an assumed-length external function will work when eventually
426called from a scope that declares the length that it will use
427(15.5.2.9 (2)).)
428
429Note that the lower bounds of the dimensions of non-`POINTER`
430non-`ALLOCATABLE` dummy argument arrays are determined by the
431callee, not the caller.
432(A Fortran pitfall: declaring `A(0:9)`, passing it to a dummy
433array `D(:)`, and assuming that `LBOUND(D,1)` will be zero
434in the callee.)
435If the declaration of an assumed-shape dummy argument array
436contains an explicit lower bound expression (R819), its value
437needs to be computed by the callee;
438it may be captured and saved in the incoming descriptor
439as long as we assume that argument descriptors can be modified
440by callees.
441Callers should fill in all of the fields of outgoing
442non-`POINTER` non-`ALLOCATABLE` argument
443descriptors with the assumption that the callee will use 1 for
444lower bound values, and callees can rely on them being 1 if
445not modified.
446
447### Copying temporary storage back into argument designators
448
449Except for `VALUE` and known `INTENT(IN)` dummy arguments and array sections
450with vector-valued subscripts (15.5.2.4(21)), temporary storage into
451which effective argument data were compacted for contiguity before the call
452must be redistributed back to its original storage by the caller after
453the return.
454
455In conjunction with saved cosubscript values, a standard descriptor
456would suffice to represent a pointer to the original storage into which the
457temporary data should be redistributed;
458the descriptor need not be fully populated with type information.
459
460Note that coindexed objects with `ALLOCATABLE` ultimate components
461are required to be associated only with dummy arguments with the
462`VALUE` &/or `INTENT(IN)` attributes (15.6.2.4(6)), so there is no
463requirement that the local image somehow reallocate remote storage
464when copying the data back.
465
466### Polymorphic bindings
467
468Calls to the type-bound procedures of monomorphic types are
469resolved at compilation time, as are calls to `NON_OVERRIDABLE`
470type-bound procedures.
471The resolution of calls to overridable type-bound procedures of
472polymorphic types must be completed at execution (generic resolution
473of type-bound procedure bindings from effective argument types, kinds,
474and ranks is always a compilation-time task (15.5.6, C.10.6)).
475
476Each derived type that declares or inherits any overridable
477type-bound procedure bindings must correspond to a static constant
478table of code addresses (or, more likely, a static constant type
479description containing or pointing to such a table, along with
480information used by the runtime support library for initialization,
481copying, finalization, and I/O of type instances).  Each overridable
482type-bound procedure in the type corresponds to an index into this table.
483
484### Host instance linkage
485
486Calls to dummy procedures and procedure pointers that resolve to
487internal procedures need to pass an additional "host instance" argument that
488addresses a block of storage in the stack frame of their
489host subprogram that was active at the time they were passed as an
490effective argument or associated with a procedure pointer.
491This is similar to a static link in implementations of programming
492languages with nested subprograms, although Fortran only allows
493one level of nesting.
494The 64-bit x86 and little-endian OpenPower ABIs reserve registers
495for this purpose (`%r10` & `R11`); 64-bit ARM has a reserved register
496that can be used (`x18`).
497
498The host subprogram objects that are visible to any of their internal
499subprograms need to be resident in memory across any calls to them
500(direct or not).  Any host subprogram object that might be defined
501during a call to an internal subprogram needs to be reloaded after
502a call or reside permanently in memory.
503A simple conservative analysis of the internal subprograms can
504identify all of these escaping objects and their definable subset.
505
506The address of the host subprogram storage used to hold the escaping
507objects needs to be saved alongside the code address(es) that
508represent a procedure pointer.
509It also needs to be conveyed alongside the text address for a
510dummy procedure.
511
512For F77ish procedures, we cannot use a "procedure pointer descriptor"
513to pass a procedure argument -- they expect to receive a single
514address argument.
515We will need to package the host instance link in a trampoline
516that loads its address into the designated register.
517
518GNU Fortran and Intel Fortran construct trampolines by writing
519a sequence of machine instructions to a block of storage in the
520host's stack frame, which requires the stack to be executable,
521which seems inadvisable for security reasons;
522XLF manages trampolines in its runtime support library, which adds some overhead
523to their construction and a reclamation obligation;
524NAG Fortran manages a static fixed-sized stack of trampolines
525per call site, imposing a hidden limit on recursion and foregoing
526reentrancy;
527PGI passes host instance links in descriptors in additional arguments
528that are not always successfully forwarded across implicit interfaces,
529sometimes leading to crashes when they turn out to be needed.
530
531F18 will manage a pool of trampolines in its runtime support library
532that can be used to pass internal procedures as effective arguments
533to F77ish procedures, so that
534a bare code address can serve to represent the effective argument.
535But targets that can only be called with an explicit interface
536have the option of using a "fat pointer" (or additional argument)
537to represent a dummy procedure closure so as
538to avoid the overhead of constructing and reclaiming a trampoline.
539Procedure descriptors can also support multiple code addresses.
540
541### Naming
542
543External subroutines and functions (R503) and `ENTRY` points (R1541)
544with `BIND(C)` (R808) have linker-visible names that are either explicitly
545specified in the program or determined by straightforward rules.
546The names of other F77ish external procedures should respect the conventions
547of the target architecture for legacy Fortran '77 programs; this is typically
548something like `foo_`.
549
550In other cases, however, we have fewer constraints on external naming,
551as well as some additional requirements and goals.
552
553Module procedures need to be distinguished by the name of their module
554and (when they have one) the submodule where their interface was
555defined.
556Note that submodule names are distinct in their modules, not hierarchical,
557so at most two levels of qualification are needed.
558
559Pure `ELEMENTAL` functions (15.8) must use distinct names for any alternate
560entry points used for packed SIMD arguments of various widths if we support
561calls to these functions in SIMD parallel contexts.
562There are already conventions for these names in `libpgmath`.
563
564The names of non-F77ish external procedures
565should be distinguished as such so that incorrect attempts to call or pass
566them with an implicit interface will fail to resolve at link time.
567Fortran 2018 explicitly enables us to do this with a correction to Fortran
5682003 in 4.3.4(5).
569
570Last, there must be reasonably permanent naming conventions used
571by the F18 runtime library for those unrestricted specific intrinsic
572functions (table 16.2 in 16.8) and extensions that can be passed as
573arguments.
574
575In these cases where external naming is at the discretion
576of the implementation, we should use names that are not in the C language
577user namespace, begin with something that identifies
578the current incompatible version of F18, the module, the submodule, and
579elemental SIMD width, and are followed by the external name.
580The parts of the external name can be separated by some character that
581is acceptable for use in LLVM IR and assembly language but not in user
582Fortran or C code, or by switching case
583(so long as there's a way to cope with extension names that don't begin
584with letters).
585
586In particular, the period (`.`) seems safe to use as a separator character,
587so a `Fa.` prefix can serve to isolate these discretionary names from
588other uses and to identify the earliest link-compatible version.
589For examples: `Fa.mod.foo`, `Fa.mod.submod.foo`, and (for an external
590subprogram that requires an explicit interface) `Fa.foo`.
591When the ABI changes in the future in an incompatible way, the
592initial prefix becomes `Fb.`, `Fc.`, &c.
593
594## Summary of checks to be enforced in semantics analysis
595
5968.5.10 `INTENT` attributes
597* (C846) An `INTENT(OUT)` argument shall not be associated with an
598  object that is or has an allocatable coarray.
599* (C847) An `INTENT(OUT)` argument shall not have `LOCK_TYPE` or `EVENT_TYPE`.
600
6018.5.18 `VALUE` attribute
602* (C863) The argument cannot be assumed-size, a coarray, or have a coarray
603  ultimate component.
604* (C864) The argument cannot be `ALLOCATABLE`, `POINTER`, `INTENT(OUT)`,
605  `INTENT(IN OUT)`, or `VOLATILE`.
606* (C865) If the procedure is `BIND(C)`, the argument cannot be `OPTIONAL`.
607
60815.5.1 procedure references:
609* (C1533) can't pass non-intrinsic `ELEMENTAL` as argument
610* (C1536) alternate return labels must be in the inclusive scope
611* (C1537) coindexed argument cannot have a `POINTER` ultimate component
612
61315.5.2.4 requirements for non-`POINTER` non-`ALLOCATABLE` dummies:
614* (2) dummy must be monomorphic for coindexed polymorphic actual
615* (2) dummy must be polymorphic for assumed-size polymorphic actual
616* (2) dummy cannot be `TYPE(*)` if effective is PDT or has TBPs or `FINAL`
617* (4) character length of effective cannot be less than dummy
618* (6) coindexed effective with `ALLOCATABLE` ultimate component requires
619      `INTENT(IN)` &/or `VALUE` dummy
620* (13) a coindexed scalar effective requires a scalar dummy
621* (14) a non-conindexed scalar effective usually requires a scalar dummy,
622  but there are some exceptions that allow elements of storage sequences
623  to be passed and treated like explicit-shape or assumed-size arrays
624  (see 15.5.2.11)
625* (16) array rank agreement
626* (20) `INTENT(OUT)` & `INTENT(IN OUT)` dummies require definable actuals
627* (21) array sections with vector subscripts can't be passed to definable dummies
628       (`INTENT(OUT)`, `INTENT(IN OUT)`, `ASYNCHRONOUS`, `VOLATILE`)
629* (22) `VOLATILE` attributes must match when dummy has a coarray ultimate component
630* (C1538 - C1540) checks for `ASYNCHRONOUS` and `VOLATILE`
631
63215.5.2.5 requirements for `ALLOCATABLE` & `POINTER` arguments when both
633the dummy and effective arguments have the same attributes:
634* (2) both or neither can be polymorphic
635* (2) both are unlimited polymorphic or both have the same declared type
636* (3) rank compatibility
637* (4) effective argument must have deferred the same type parameters as the dummy
638
63915.5.2.6 `ALLOCATABLE` dummy arguments:
640* (2) effective must be `ALLOCATABLE`
641* (3) corank must match
642* (4) coindexed effective requires `INTENT(IN)` dummy
643* (7) `INTENT(OUT)` & `INTENT(IN OUT)` dummies require definable actuals
644
64515.5.2.7 `POINTER` dummy arguments:
646* (C1541) `CONTIGUOUS` dummy requires simply contiguous actual
647* (C1542) effective argument cannot be coindexed unless procedure is intrinsic
648* (2) effective argument must be `POINTER` unless dummy is `INTENT(IN)` and
649  effective could be the right-hand side of a pointer assignment statement
650
65115.5.2.8 corray dummy arguments:
652* (1) effective argument must be coarray
653* (1) `VOLATILE` attributes must match
654* (2) explicitly or implicitly contiguous dummy array requires a simply contiguous actual
655
65615.5.2.9 dummy procedures:
657* (1) explicit dummy procedure interface must have same characteristics as actual
658* (5) dummy procedure `POINTER` requirements on effective arguments
659
66015.6.2.1 procedure definitions:
661* `NON_RECURSIVE` procedures cannot recurse.
662* Assumed-length `CHARACTER(*)` functions cannot be declared as `RECURSIVE`, array-valued,
663  `POINTER`, `ELEMENTAL`, or `PURE' (C723), and cannot be called recursively (15.6.2.1(3)).
664* (C823) A function result cannot be a coarray or contain a coarray ultimate component.
665
666`PURE` requirements (15.7): C1583 - C1599.
667These also apply to `ELEMENTAL` procedures that are not `IMPURE`.
668
669`ELEMENTAL` requirements (15.8.1): C15100-C15103,
670and C1533 (can't pass as effective argument unless intrinsic)
671
672For interoperable procedures and interfaces (18.3.6):
673* C1552 - C1559
674* function result is scalar and of interoperable type (C1553, 18.3.1-3)
675* `VALUE` arguments are scalar and of interoperable type
676* `POINTER` dummies cannot be `CONTIGUOUS` (18.3.6 paragraph 2(5))
677* assumed-type dummies cannot be `ALLOCATABLE`, `POINTER`, assumed-shape, or assumed-rank (18.3.6 paragraph 2 (5))
678* `CHARACTER` dummies that are `ALLOCATABLE` or `POINTER` must be deferred-length
679
680## Further topics to document
681
682* Alternate return specifiers
683* `%VAL()`, `%REF()`, and `%DESCR()` legacy VMS interoperability extensions
684* Unrestricted specific intrinsic functions as effective arguments
685* SIMD variants of `ELEMENTAL` procedures (& unrestricted specific intrinsics)
686* Elemental subroutine calls with array arguments
687