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