Skip to content

Commit bc39e6b

Browse files
authored
Merge pull request #430 from LLNL/update-capptr
Add YAML group 'destructors'
2 parents 4e1ad77 + b66f403 commit bc39e6b

34 files changed

Lines changed: 683 additions & 124 deletions

docs/input.rst

Lines changed: 70 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -294,49 +294,66 @@ a default argument may include a plus symbol:
294294
api
295295
^^^
296296

297-
Controls the API used by the C wrapper. The values are *capi*,
298-
*buf*, *capsule*, *capptr*, *cdesc* and *cfi*.
299-
Normally, this attribute is determined by Shroud
300-
internally. Scalar native types such as ``int`` and ``double`` will
301-
use *capi* since the argument can be passed directly to C using the
302-
*interoperability with C* feature of Fortran.
303-
304-
Otherwise a 'bufferify' wrapper will also be created. Pointers to native and
305-
``char`` use additional metadata extracted by the Fortran wrapper via
306-
intrinsics ``LEN`` and ``SIZE``. In addition, *intent(in)* strings
307-
will be copied and null-terminated. This uses *api(buf)*.
308-
309-
*cdesc* will pass down a pointer to a struct which contains metadata
310-
for the argument instead of passing additional fields. The advantage
311-
is the struct can also be used to return metadata from the C wrapper
312-
to the Fortran wrapper.
313-
The metadata includes shape information from a *dimension* attribute
314-
and is used on *intent(OUT)* arguments and function results to set
315-
Fortran ``POINTER`` shapes.
316-
The struct is named by the format fields
317-
*C_array_type* and *F_array_type*.
318-
319-
The option *F_CFI*, will use the *Further interoperability with C*
320-
features and pass ``CFI_cdesc_t`` arguments to the C where where the
321-
metadata is extracted. This uses *api(cfi)*.
322-
323-
The *capsule* and *capptr* APIs are used by the capsule created by
324-
shadow types created for C++ classes. In both cases the result is
325-
passed from Fortran to C as an extra argument for function which
326-
return a class. With *capptr*, the C wrapper will return a pointer to
327-
the capsule argument while *capsule* will not return a value for
328-
the function. This is controlled by the *C_shadow_result* option.
329-
330-
There is currently one useful case where the user would want to set
331-
this attribute. To avoid creating a wrapper which copies and null
332-
terminates a ``char *`` argument the user can set *api(capi)*. The
333-
address of the formal parameter will be passed to the user's library.
334-
This is useful when null termination does not make sense. For example,
335-
when the argument is a large buffer to be written to a file. The C
336-
library must have some other way of determining the length of the
337-
argument such as another argument with the explicit length.
297+
Controls the API used by the C wrapper.
298+
Normally, this attribute is determined by Shroud internally based on the
299+
argument type and attributes.
300+
But in some cases it's helpful if the user defines it.
301+
302+
buf
303+
304+
Pass a pointer to a string along with meta data about the length
305+
or size.
306+
Pointers to native and
307+
``char`` use additional metadata extracted by the Fortran wrapper via
308+
intrinsics ``LEN`` and ``SIZE``. In addition, *intent(in)* strings
309+
will be copied and null-terminated.
310+
311+
capi
312+
313+
There is currently one useful case where the user would want to set
314+
this attribute. To avoid creating a wrapper which copies and null
315+
terminates a ``char *`` argument the user can set *api(capi)*. The
316+
address of the formal parameter will be passed to the user's library.
317+
This is useful when null termination does not make sense. For example,
318+
when the argument is a large buffer to be written to a file. The C
319+
library must have some other way of determining the length of the
320+
argument such as another argument with the explicit length.
338321

339322
.. tested by strings.yaml CpassCharPtrCAPI
323+
324+
capptr
325+
capsule
326+
327+
The *capsule* and *capptr* APIs are used by the capsule created by
328+
shadow types created for C++ classes. In both cases the result is
329+
passed from Fortran to C as an extra argument for function which
330+
return a class. With *capptr*, the C wrapper will return a pointer to
331+
the capsule argument while *capsule* will not return a value for
332+
the function. This is controlled by the **C_shadow_result** option.
333+
334+
cdesc
335+
336+
Uses a struct to pass the metadata between Fortran and C.
337+
This struct contains additional metadata beyond
338+
the length and size used by *api(buf)*.
339+
It is also used to return metadata from the C wrapper to the Fortran wrapper.
340+
The metadata includes shape information from a *dimension* attribute
341+
and is used on *intent(OUT)* arguments and function results to set the
342+
shape of Fortran ``POINTER`` variables.
343+
The struct is named by the format fields *C_array_type* and *F_array_type*.
344+
It is similar to the ``CFI_cdesc_t`` struct provied by Fortran 2018.
345+
346+
cfi
347+
348+
Use *Further interoperability with C* features and pass
349+
``CFI_cdesc_t`` arguments to the C wrapper which will extract the
350+
metadata. Using option *F_CFI* will make this the default
351+
behavior.
352+
353+
this
354+
355+
Used when the *return_this* field is *True*.
356+
The C++ function returns a pointer to the **this** variable.
340357

341358
.. In the future a user settable api might be useful to do custom
342359
actions in the wrappers.
@@ -510,6 +527,18 @@ scalar
510527
511528
.. function pointers meta[deref] for both the +external and +funptr.
512529
530+
destructor_name
531+
^^^^^^^^^^^^^^^
532+
533+
Specifies a name in the **destructors** section which lists code to be used to
534+
release memory. Used with function results.
535+
It is used in the *C_memory_dtor_function* and will have the
536+
variable ``void *ptr`` available as the pointer to the memory
537+
to be released.
538+
See :ref:`MemoryManagementAnchor` for details.
539+
540+
.. and *intent(out)* arguments.
541+
513542
dimension
514543
^^^^^^^^^
515544

@@ -589,18 +618,6 @@ as the dummy argument for the function pointer.
589618
See also the *funptr* attribute.
590619
See :ref:`DeclAnchor_Function_Pointers`.
591620

592-
free_pattern
593-
^^^^^^^^^^^^
594-
595-
A name in the **patterns** section which lists code to be used to
596-
release memory. Used with function results.
597-
It is used in the *C_memory_dtor_function* and will have the
598-
variable ``void *ptr`` available as the pointer to the memory
599-
to be released.
600-
See :ref:`MemoryManagementAnchor` for details.
601-
602-
.. and *intent(out)* arguments.
603-
604621
funcarg
605622
^^^^^^^
606623

docs/pointers.rst

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,20 +265,24 @@ is used with ``std::vector`` and provides the lines:
265265
- std::vector<{cxx_T}> *cxx_ptr = reinterpret_cast<std::vector<{cxx_T}> *>(ptr);
266266
- delete cxx_ptr;
267267
268-
Patterns can be used to provide code to free memory for a wrapped
269-
function. The address of the memory to free will be in the variable
268+
Destructor code can be defined without creating a new statement group
269+
by defining it in the **destructors** section of the YAML file. Then
270+
use the *+destructor_name* attribute in the declaration.
271+
This allows custom destructor code to be used more easily.
272+
273+
The address of the memory to free will be in the variable
270274
``void *ptr``, which should be referenced in the pattern:
271275

272276
.. code-block:: yaml
273277
274278
declarations:
275-
- decl: char * getName() +free_pattern(free_getName)
279+
- decl: char *getName() +destructor_name(free_getName)
276280
277-
patterns:
281+
destructors:
278282
free_getName: |
279283
decref(ptr);
280284
281-
Without any explicit *destructor_name* or pattern, ``free`` will be
285+
Without any explicit *destructor_name*, ``free`` will be
282286
used to release POD pointers; otherwise, ``delete`` will be used.
283287

284288
.. When to use ``delete[] ptr``?
@@ -310,7 +314,8 @@ And :file:`wrapftutorial.f`:
310314

311315
*addr* is the address of the C or C++ variable, such as a ``char *``
312316
or ``std::string *``. *idtor* is a Shroud generated index of the
313-
destructor code defined by *destructor_name* or the *free_pattern* attribute.
317+
destructor code defined by *destructor_name* in the statement group
318+
or the *destructor_name* attribute.
314319
These code segments are collected and written to function
315320
*C_memory_dtor_function*. A value of 0 indicated the memory will not
316321
be released and is used with the **owner(library)** attribute.

docs/releases.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ Changes to YAML input
1818
* Added attribute *+funptr*. Uses ``type(C_FUNPTR)`` for
1919
function pointer arguments.
2020

21+
* Renamed the *+free_pattern* attribute to *+destructor_name*.
22+
The values is looked up in the new **destructors** section of
23+
the YAML file instead of the **patterns** section.
24+
Current use of *+free_pattern* is still supported but will
25+
create a warning message in the logfile. The old usage will be
26+
removed in a future version.
27+
2128
* Create an abstract interface for typedef statements which
2229
are function pointers. Previously, only function pointers
2330
arguments were supported.

regression/input/error.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,16 @@ declarations:
5959
PY_array_arg: none
6060
PY_struct_arg: none
6161

62+
63+
#####
64+
# Report on old usage for free_pattern
65+
66+
- decl: const std::string * getConstStringPtrOwnsAllocPattern() +owner(caller)
67+
options:
68+
wrap_python: False
69+
fattrs:
70+
free_pattern: C_string_free
71+
72+
patterns:
73+
C_string_free: |
74+
// Used with +free_pattern(C_string_free)

regression/input/strings.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ declarations:
142142
description: |
143143
Similar to getConstStringPtrOwnsAlloc, but uses pattern to release memory.
144144
fattrs:
145-
free_pattern: C_string_free
145+
destructor_name: C_string_free
146146

147147
- decl: const std::string * getConstStringPtrPointer()
148148
+deref(pointer)+owner(library)
@@ -310,9 +310,11 @@ patterns:
310310
C_invalid_name_buf: |
311311
// Some error code for buf
312312
313+
destructors:
313314
# This pattern is added to C_memory_dtor_function
314315
# XXX - \t is not dealt with properly
315316
C_string_free: |
316-
// Used with +free_pattern(C_string_free)
317+
// Used with +destrucor_name(C_string_free)
317318
std::string *cxx_ptr = reinterpret_cast<std::string *>(ptr);
318319
delete cxx_ptr;
320+

regression/reference/char-cxx-cfi/char.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5466,6 +5466,11 @@
54665466
}
54675467
],
54685468
"language": "cxx",
5469+
"patterns": {
5470+
"C_invalid_name": "if ({cxx_var}.empty()) {{\n return NULL;\n}}\n",
5471+
"C_invalid_name_buf": "// Some error code for buf\n",
5472+
"C_string_free": "// Used with +free_pattern(C_string_free)\nstd::string *cxx_ptr = reinterpret_cast<std::string *>(ptr);\ndelete cxx_ptr;\n"
5473+
},
54695474
"scope_file": [
54705475
"char"
54715476
],

regression/reference/char-cxx/char.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6131,6 +6131,11 @@
61316131
}
61326132
],
61336133
"language": "cxx",
6134+
"patterns": {
6135+
"C_invalid_name": "if ({cxx_var}.empty()) {{\n return NULL;\n}}\n",
6136+
"C_invalid_name_buf": "// Some error code for buf\n",
6137+
"C_string_free": "// Used with +free_pattern(C_string_free)\nstd::string *cxx_ptr = reinterpret_cast<std::string *>(ptr);\ndelete cxx_ptr;\n"
6138+
},
61346139
"scope_file": [
61356140
"char"
61366141
],

regression/reference/error-ast/output

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,12 @@ Error in 'decl' field
108108
^
109109
Expected EOF, found ID
110110
--------------------
111-
Node: testattrs4
112-
line 117
111+
Node: IndexType
113112
Typemap IndexType: Unknown key 'no_such_keyword'
114113
--------------------
115-
Node: testattrs4
116-
line 117
117-
Typemap IndexType: Replacing deprecated field 'f_c_module' with 'i_module'
114+
Node: IndexType
115+
typedef int64_t IndexType
116+
Deprecated: Typemap IndexType: Replacing deprecated field 'f_c_module' with 'i_module'
118117
--------------------
119118
Node: process_twostruct
120119
line 139

0 commit comments

Comments
 (0)