To: J3 J3/23-115 From: Malcolm Cohen Subject: Interp F23/001 on C descriptor attribute member Date: 2023-February-07 ---------------------------------------------------------------------- NUMBER: F23/001 TITLE: Ambiguity in attribute passed in C descriptor KEYWORDS: C descriptor, CFI_attribute_t DEFECT TYPE: Erratum STATUS: J3 consideration in progress QUESTION: In 18.5.3 The CFI_cdesc_t structure type, the description of the attribute member states "CFI_attribute_t attribute; The value is equal to the value of an attribute code that indicates whether the object described is allocatable, a data pointer, or a nonallocatable nonpointer data object." However, this does not explicitly state what object is being described in a procedure reference. In a reference from Fortran to C, there are three possibilities for the object: 1) the actual argument, 2) the effective argument, 3) the dummy argument. Which of these is intended? Here is an example C function: #include int attrtest(CFI_cdesc_t *y) { if (y->attribute==CFI_attribute_allocatable) return 1; else if (y->attribute==CFI_attribute_pointer) return 2; else return 3; /* other */ } This is called from Fortran as follows: Program test Use Iso_C_Binding Interface Function attrtest(z) Bind(C) Import Real(C_float),Intent(In) :: z(..) Integer(C_int) attrtest End Function End Interface Real(C_float),Allocatable,Target :: a(:) Real(C_float),Pointer :: p(:) Allocate(a(100),Source=0.0_C_float) p => a Print *,attrtest(a),attrtest(p),attrtest(p+1) End Program This has been tested on four compilers. Two appear to pass a C descriptor describing the actual argument, as they print 1 2 3 Two other compilers appear to describe the dummy argument, as they print 3 3 3 It could also be argued that the second group are describing the effective argument. Certainly for the pointer case, describing the target would produce CFI_attribute_other, but for the allocatable case, the standard appears to say that the effective argument is the allocatable variable, resulting in "1 3 3" not "3 3 3". Or, it would perhaps be more natural to think that when the actual argument is allocatable but the dummy argument is not, the effective argument is the actual argument "without" the ALLOCATABLE attribute; that would require a change to 15.5.2.4 Argument association. Evidence that it should be not be the dummy argument that should be described comes from the "dim" member, where the standard states that for an assumed-size array, the extent member of the last dim member has the value -1; that implies that at least this part of the descriptor is describing either the actual argument or the effective argument. Q1. Which output is correct? That is, should the actual argument be described, the dummy argument, or something else? In a reference to a Fortran procedure from C, using a C descriptor argument, it seems clear from 15.5.2.7p2 that if the dummy argument is allocatable, the attribute member in the passed C descriptor needs to be CFI_attribute_allocatable. Similarly, if the dummy argument is a pointer that is not INTENT(IN), it needs to be CFI_attribute_pointer. Q2. Is it correct to deduce that the attribute member passed to an ALLOCATABLE dummy needs to be CFI_attribute_allocatable, and for a non-INTENT(IN) POINTER dummy it needs to be CFI_attribute_pointer? But what if the dummy argument is an "ordinary" dummy argument? Is it required to be CFI_attribute_other? (This would be consistent with the answer to Q1 being that the dummy argument is described.) Or may it be CFI_attribute_allocatable or CFI_attribute_pointer? Q3. In a reference from C to Fortran, is the attribute member of a C descriptor corresponding to an ordinary dummy argument required to be CFI_attribute_other? Finally, what if the dummy argument is an INTENT(IN) pointer? Clearly CFI_attribute_pointer is valid. Perhaps CFI_attribute_other is also valid, indicating use of the "auto-target" feature? But what about CFI_attribute_allocatable? Is that permitted? Q4. Which attribute values may be provided for an INTENT(IN) pointer dummy argument? POSSIBLE ANSWER: A1. The second output is correct, that is, it is the dummy argument that should be described by the C descriptor in this case. A2. Yes. A3. Yes, the attribute member is required to be CFI_attribute_other, describing the dummy argument. A4. The attribute member shall be CFI_attribute_pointer. The C function is already creating a pointer object (the C descriptor) to pass to Fortran, so there is no need for "auto-target". VARIATION ANSWER: Same results as above, but say it is the effective argument that is described. This would be more consistent with the semantics of the dim member, but will need an edit to modify the effective argument definition for allocatable. ALTERNATIVE ANSWER: A1. The first output is correct, that is, it is the actual argument that should be described by the C descriptor in this case. A2. Yes. A3. No, there are no requirements on the attribute member when the Fortran dummy argument does not have the ALLOCATABLE or POINTER attribute. A4. All attribute values are valid when the Fortran dummy argument is an INTENT(IN) pointer. EDITS to N2209: To be provided. SUBMITTED BY: Malcolm Cohen HISTORY: 23-nnn m229 Submitted ----------------------------------------------------------------------