Warning: This file is not a C or C++ file. It does not have highlighting.

1/*===-- include/flang/ISO_Fortran_binding.h -----------------------*- C++ -*-===
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
10#ifndef CFI_ISO_FORTRAN_BINDING_H_
11#define CFI_ISO_FORTRAN_BINDING_H_
12
13/* When this header is included into the compiler and runtime implementations,
14 * it does so by means of a wrapper header that establishes namespaces and
15 * a macro for extra function attributes (RT_API_ATTRS).
16 */
17#ifndef FORTRAN_ISO_FORTRAN_BINDING_WRAPPER_H_
18#include <stddef.h>
19#define FORTRAN_ISO_NAMESPACE_
20#endif
21
22/* Standard interface to Fortran from C and C++.
23 * These interfaces are named in subclause 18.5 of the Fortran 2018
24 * standard, with most of the actual details being left to the
25 * implementation.
26 */
27
28#ifndef RT_API_ATTRS
29#define RT_API_ATTRS
30#endif
31
32/* 18.5.4 */
33#define CFI_VERSION 20180515
34
35#define CFI_MAX_RANK 15
36typedef unsigned char CFI_rank_t;
37
38/* This type is probably larger than a default Fortran INTEGER
39 * and should be used for all array indexing and loop bound calculations.
40 */
41typedef ptrdiff_t CFI_index_t;
42
43typedef unsigned char CFI_attribute_t;
44#define CFI_attribute_pointer 1
45#define CFI_attribute_allocatable 2
46#define CFI_attribute_other 0 /* neither pointer nor allocatable */
47
48typedef signed char CFI_type_t;
49/* These codes are required to be macros (i.e., #ifdef will work).
50 * They are not required to be distinct, but neither are they required
51 * to have had their synonyms combined.
52 */
53#define CFI_type_signed_char 1
54#define CFI_type_short 2
55#define CFI_type_int 3
56#define CFI_type_long 4
57#define CFI_type_long_long 5
58#define CFI_type_size_t 6
59#define CFI_type_int8_t 7
60#define CFI_type_int16_t 8
61#define CFI_type_int32_t 9
62#define CFI_type_int64_t 10
63#define CFI_type_int128_t 11 /* extension kind=16 */
64#define CFI_type_int_least8_t 12
65#define CFI_type_int_least16_t 13
66#define CFI_type_int_least32_t 14
67#define CFI_type_int_least64_t 15
68#define CFI_type_int_least128_t 16 /* extension */
69#define CFI_type_int_fast8_t 17
70#define CFI_type_int_fast16_t 18
71#define CFI_type_int_fast32_t 19
72#define CFI_type_int_fast64_t 20
73#define CFI_type_int_fast128_t 21 /* extension */
74#define CFI_type_intmax_t 22
75#define CFI_type_intptr_t 23
76#define CFI_type_ptrdiff_t 24
77#define CFI_type_half_float 25 /* extension: kind=2 */
78#define CFI_type_bfloat 26 /* extension: kind=3 */
79#define CFI_type_float 27
80#define CFI_type_double 28
81#define CFI_type_extended_double 29 /* extension: kind=10 */
82#define CFI_type_long_double 30
83#define CFI_type_float128 31 /* extension: kind=16 */
84#define CFI_type_half_float_Complex 32 /* extension: kind=2 */
85#define CFI_type_bfloat_Complex 33 /* extension: kind=3 */
86#define CFI_type_float_Complex 34
87#define CFI_type_double_Complex 35
88#define CFI_type_extended_double_Complex 36 /* extension: kind=10 */
89#define CFI_type_long_double_Complex 37
90#define CFI_type_float128_Complex 38 /* extension: kind=16 */
91#define CFI_type_Bool 39
92#define CFI_type_char 40
93#define CFI_type_cptr 41
94#define CFI_type_struct 42
95#define CFI_type_char16_t 43 /* extension kind=2 */
96#define CFI_type_char32_t 44 /* extension kind=4 */
97#define CFI_TYPE_LAST CFI_type_char32_t
98#define CFI_type_other (-1) // must be negative
99
100/* Error code macros - skip some of the small values to avoid conflicts with
101 * other status codes mandated by the standard, e.g. those returned by
102 * GET_ENVIRONMENT_VARIABLE (16.9.84) */
103#define CFI_SUCCESS 0 /* must be zero */
104#define CFI_ERROR_BASE_ADDR_NULL 11
105#define CFI_ERROR_BASE_ADDR_NOT_NULL 12
106#define CFI_INVALID_ELEM_LEN 13
107#define CFI_INVALID_RANK 14
108#define CFI_INVALID_TYPE 15
109#define CFI_INVALID_ATTRIBUTE 16
110#define CFI_INVALID_EXTENT 17
111#define CFI_INVALID_DESCRIPTOR 18
112#define CFI_ERROR_MEM_ALLOCATION 19
113#define CFI_ERROR_OUT_OF_BOUNDS 20
114
115/* 18.5.2 per-dimension information */
116typedef struct CFI_dim_t {
117 CFI_index_t lower_bound;
118 CFI_index_t extent; /* == -1 for assumed size */
119 CFI_index_t sm; /* memory stride in bytes */
120} CFI_dim_t;
121
122#ifdef __cplusplus
123namespace cfi_internal {
124// C++ does not support flexible array.
125// The below structure emulates a flexible array. This structure does not take
126// care of getting the memory storage. Note that it already contains one element
127// because a struct cannot be empty.
128extern "C++" template <typename T> struct FlexibleArray : T {
129 RT_API_ATTRS T &operator[](int index) { return *(this + index); }
130 RT_API_ATTRS const T &operator[](int index) const { return *(this + index); }
131 RT_API_ATTRS operator T *() { return this; }
132 RT_API_ATTRS operator const T *() const { return this; }
133};
134} // namespace cfi_internal
135#endif
136
137/* 18.5.3 generic data descriptor */
138
139/* Descriptor header members */
140#define _CFI_CDESC_T_HEADER_MEMBERS \
141 /* These three members must appear first, \
142 * in exactly this order. */ \
143 void *base_addr; \
144 size_t elem_len; /* element size in bytes */ \
145 int version; /* == CFI_VERSION */ \
146 CFI_rank_t rank; /* [0 .. CFI_MAX_RANK] */ \
147 CFI_type_t type; \
148 CFI_attribute_t attribute; \
149 unsigned char f18Addendum;
150
151typedef struct CFI_cdesc_t {
152 _CFI_CDESC_T_HEADER_MEMBERS
153#ifdef __cplusplus
154 cfi_internal::FlexibleArray<CFI_dim_t> dim;
155#else
156 CFI_dim_t dim[]; /* must appear last */
157#endif
158} CFI_cdesc_t;
159
160/* 18.5.4 */
161#ifdef __cplusplus
162// This struct acquires the additional storage, if any is
163// needed, for C++'s CFI_cdesc_t's emulated flexible
164// dim[] array.
165namespace cfi_internal {
166extern "C++" template <int r> struct CdescStorage : public CFI_cdesc_t {
167 static_assert((r > 1 && r <= CFI_MAX_RANK), "CFI_INVALID_RANK");
168 CFI_dim_t dim[r - 1];
169};
170extern "C++" template <> struct CdescStorage<1> : public CFI_cdesc_t {};
171extern "C++" template <> struct CdescStorage<0> : public CFI_cdesc_t {};
172} // namespace cfi_internal
173#define CFI_CDESC_T(rank) \
174 FORTRAN_ISO_NAMESPACE_::cfi_internal::CdescStorage<rank>
175#else
176#define CFI_CDESC_T(_RANK) \
177 struct { \
178 _CFI_CDESC_T_HEADER_MEMBERS \
179 CFI_dim_t dim[_RANK]; \
180 }
181#endif
182
183/* 18.5.5 procedural interfaces*/
184#ifdef __cplusplus
185extern "C" {
186#endif
187RT_API_ATTRS void *CFI_address(
188 const CFI_cdesc_t *, const CFI_index_t subscripts[]);
189RT_API_ATTRS int CFI_allocate(CFI_cdesc_t *, const CFI_index_t lower_bounds[],
190 const CFI_index_t upper_bounds[], size_t elem_len);
191RT_API_ATTRS int CFI_deallocate(CFI_cdesc_t *);
192RT_API_ATTRS int CFI_establish(CFI_cdesc_t *, void *base_addr, CFI_attribute_t,
193 CFI_type_t, size_t elem_len, CFI_rank_t, const CFI_index_t extents[]);
194RT_API_ATTRS int CFI_is_contiguous(const CFI_cdesc_t *);
195RT_API_ATTRS int CFI_section(CFI_cdesc_t *, const CFI_cdesc_t *source,
196 const CFI_index_t lower_bounds[], const CFI_index_t upper_bounds[],
197 const CFI_index_t strides[]);
198RT_API_ATTRS int CFI_select_part(CFI_cdesc_t *, const CFI_cdesc_t *source,
199 size_t displacement, size_t elem_len);
200RT_API_ATTRS int CFI_setpointer(
201 CFI_cdesc_t *, const CFI_cdesc_t *source, const CFI_index_t lower_bounds[]);
202#ifdef __cplusplus
203} // extern "C"
204#endif
205
206#endif /* CFI_ISO_FORTRAN_BINDING_H_ */
207

Warning: This file is not a C or C++ file. It does not have highlighting.

source code of flang/include/flang/ISO_Fortran_binding.h