11-128 To: J3 From: Nick Maclaren Subject: Interop TR: CFI_desc_t scoping issues Date: 2011 January 27 Reference: N1838, 10-235 1. Summary ---------- This issue was raised in 10-235, especially in section 2.3. I shall not repeat that, but there are three critical points: 1) The scoping and lifetime of a descriptor is clearly unrelated to that of the data it refers to, because descriptors are closer to being the instantiation of an association than to one of an object. However, the TR says nothing about what that scoping and lifetime might be. 2) The rules on object scoping and lifetimes are wildly different between Fortran and C, not just in detail but in their basic concepts. There are important constraints that are obvious to Fortran experts, but will be positively counter-intuitive to C experts. 3) The C code can obtain the address of both a descriptor and an object, (in summary) such pointers remain valid while that object does, and caching them is normal programming practice in C. However, Fortran permits any object that does not have the TARGET, ASYNCHRONOUS or VOLATILE attributes to be moved at the whim of the implementor. Note that C has at least 5 very different classes of 'undefined', and an object can become undefined while still leaving some uses of it as defined behaviour. That being so, some wording making it clear to C programmers what Fortran's rules really are is essential. Unlike most of 10-235, this wording is easy to add to the current design. I have changed it slightly, in an attempt to clarify it. 2. Edits -------- [16:18+] Add some new paragraphs to 5.2.7 Restrictions on lifetimes: When a Fortran object or internal procedure is deallocated, execution of its host instance is completed, or its allocation or association status becomes undefined, all C descriptors and C pointers to any part of it become undefined[*]. A C descriptor received as a dummy argument becomes undefined on return from the procedure call. If the dummy argument does not have any of the TARGET, ASYNCHRONOUS or VOLATILE attributes, all C pointers to any part of the object it describes become undefined on return from the procedure call[*]. If a C descriptor is passed as an actual argument, its lifetime and that of the object it describes shall not end before the return from the procedure call. A Fortran pointer variable that is associated with a C descriptor shall not be accessed beyond the end of the lifetime of the C descriptor or the object it describes. NOTE [*] In C terms, the pointers and values become indeterminate (ISO/IEC 9899:1999 6.2.4 paragraph 2), because the lifetime of the descriptor or object pointed to has ended.