1 /* Declarations for ISO Fortran binding.
2 Copyright (C) 2018-2021 Free Software Foundation, Inc.
3 Contributed by Daniel Celis Garza <celisdanieljr@gmail.com>
5 This file is part of the GNU Fortran runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #ifndef ISO_FORTRAN_BINDING_H
27 #define ISO_FORTRAN_BINDING_H
33 #include <stddef.h> /* Standard ptrdiff_t tand size_t. */
34 #include <stdint.h> /* Integer types. */
36 /* Constants, defined as macros. */
38 #define CFI_MAX_RANK 15
41 #define CFI_attribute_pointer 0
42 #define CFI_attribute_allocatable 1
43 #define CFI_attribute_other 2
46 CFI_INVALID_STRIDE should be defined in the standard because they are useful to the implementation of the functions.
50 #define CFI_ERROR_BASE_ADDR_NULL 2
51 #define CFI_ERROR_BASE_ADDR_NOT_NULL 3
52 #define CFI_INVALID_ELEM_LEN 4
53 #define CFI_INVALID_RANK 5
54 #define CFI_INVALID_TYPE 6
55 #define CFI_INVALID_ATTRIBUTE 7
56 #define CFI_INVALID_EXTENT 8
57 #define CFI_INVALID_STRIDE 9
58 #define CFI_INVALID_DESCRIPTOR 10
59 #define CFI_ERROR_MEM_ALLOCATION 11
60 #define CFI_ERROR_OUT_OF_BOUNDS 12
62 /* CFI type definitions. */
63 typedef ptrdiff_t CFI_index_t
;
64 typedef int8_t CFI_rank_t
;
65 typedef int8_t CFI_attribute_t
;
66 typedef int16_t CFI_type_t
;
69 typedef struct CFI_dim_t
71 CFI_index_t lower_bound
;
77 /* CFI_cdesc_t, C descriptors are cast to this structure as follows:
78 CFI_CDESC_T(CFI_MAX_RANK) foo;
79 CFI_cdesc_t * bar = (CFI_cdesc_t *) &foo;
81 typedef struct CFI_cdesc_t
87 CFI_attribute_t attribute
;
93 /* CFI_CDESC_T with an explicit type. */
94 #define CFI_CDESC_TYPE_T(r, base_type) \
96 base_type *base_addr; \
100 CFI_attribute_t attribute; \
104 #define CFI_CDESC_T(r) CFI_CDESC_TYPE_T (r, void)
106 /* CFI function declarations. */
107 extern void *CFI_address (const CFI_cdesc_t
*, const CFI_index_t
[]);
108 extern int CFI_allocate (CFI_cdesc_t
*, const CFI_index_t
[], const CFI_index_t
[],
110 extern int CFI_deallocate (CFI_cdesc_t
*);
111 extern int CFI_establish (CFI_cdesc_t
*, void *, CFI_attribute_t
, CFI_type_t
, size_t,
112 CFI_rank_t
, const CFI_index_t
[]);
113 extern int CFI_is_contiguous (const CFI_cdesc_t
*);
114 extern int CFI_section (CFI_cdesc_t
*, const CFI_cdesc_t
*, const CFI_index_t
[],
115 const CFI_index_t
[], const CFI_index_t
[]);
116 extern int CFI_select_part (CFI_cdesc_t
*, const CFI_cdesc_t
*, size_t, size_t);
117 extern int CFI_setpointer (CFI_cdesc_t
*, CFI_cdesc_t
*, const CFI_index_t
[]);
119 /* Types and kind numbers. Allows bitwise and to reveal the intrinsic type of a kind type. It also allows us to find the kind parameter by inverting the bit-shift equation.
120 CFI_type_kind_shift = 8
121 CFI_intrinsic_type = 0 0 0 0 0 0 0 0 0 0 1 0
122 CFI_type_kind = 0 0 0 0 0 0 0 0 1 0 0 0
123 CFI_type_example = CFI_intrinsic_type + (CFI_type_kind << CFI_type_kind_shift)
124 Defining the CFI_type_example.
125 CFI_type_kind = 0 0 0 0 0 0 0 0 1 0 0 0 << CFI_type_kind_shift
126 -------------------------
127 1 0 0 0 0 0 0 0 0 0 0 0 +
128 CFI_intrinsic_type = 0 0 0 0 0 0 0 0 0 0 1 0
129 -------------------------
130 CFI_type_example = 1 0 0 0 0 0 0 0 0 0 1 0
131 Finding the intrinsic type with the logical mask.
132 CFI_type_example = 1 0 0 0 0 0 0 0 0 0 1 0 &
133 CFI_type_mask = 0 0 0 0 1 1 1 1 1 1 1 1
134 -------------------------
135 CFI_intrinsic_type = 0 0 0 0 0 0 0 0 0 0 1 0
136 Using the intrinsic type and kind shift to find the kind value of the type.
137 CFI_type_kind = (CFI_type_example - CFI_intrinsic_type) >> CFI_type_kind_shift
138 CFI_type_example = 1 0 0 0 0 0 0 0 0 0 1 0 -
139 CFI_intrinsic_type = 0 0 0 0 0 0 0 0 0 0 1 0
140 -------------------------
141 1 0 0 0 0 0 0 0 0 0 0 0 >> CFI_type_kind_shift
142 -------------------------
143 CFI_type_kind = 0 0 0 0 0 0 0 0 1 0 0 0
145 #define CFI_type_mask 0xFF
146 #define CFI_type_kind_shift 8
148 /* Intrinsic types. Their kind number defines their storage size. */
149 #define CFI_type_Integer 1
150 #define CFI_type_Logical 2
151 #define CFI_type_Real 3
152 #define CFI_type_Complex 4
153 #define CFI_type_Character 5
155 /* Types with no kind. */
156 #define CFI_type_struct 6
157 #define CFI_type_cptr 7
158 #define CFI_type_cfunptr 8
159 #define CFI_type_other -1
161 /* Types with kind parameter.
162 The kind parameter represents the type's byte size. The exception is kind = 10, which has byte size of 64 but 80 bit precision. Complex variables are double the byte size of their real counterparts. The ucs4_char matches wchar_t if sizeof (wchar_t) == 4.
164 #define CFI_type_char (CFI_type_Character + (1 << CFI_type_kind_shift))
165 #define CFI_type_ucs4_char (CFI_type_Character + (4 << CFI_type_kind_shift))
167 /* C-Fortran Interoperability types. */
168 #define CFI_type_signed_char (CFI_type_Integer + (1 << CFI_type_kind_shift))
169 #define CFI_type_short (CFI_type_Integer + (2 << CFI_type_kind_shift))
170 #define CFI_type_int (CFI_type_Integer + (4 << CFI_type_kind_shift))
171 #define CFI_type_long (CFI_type_Integer + (8 << CFI_type_kind_shift))
172 #define CFI_type_long_long (CFI_type_Integer + (8 << CFI_type_kind_shift))
173 #define CFI_type_size_t (CFI_type_Integer + (8 << CFI_type_kind_shift))
174 #define CFI_type_int8_t (CFI_type_Integer + (1 << CFI_type_kind_shift))
175 #define CFI_type_int16_t (CFI_type_Integer + (2 << CFI_type_kind_shift))
176 #define CFI_type_int32_t (CFI_type_Integer + (4 << CFI_type_kind_shift))
177 #define CFI_type_int64_t (CFI_type_Integer + (8 << CFI_type_kind_shift))
178 #define CFI_type_int_least8_t (CFI_type_Integer + (1 << CFI_type_kind_shift))
179 #define CFI_type_int_least16_t (CFI_type_Integer + (2 << CFI_type_kind_shift))
180 #define CFI_type_int_least32_t (CFI_type_Integer + (4 << CFI_type_kind_shift))
181 #define CFI_type_int_least64_t (CFI_type_Integer + (8 << CFI_type_kind_shift))
182 #define CFI_type_int_fast8_t (CFI_type_Integer + (1 << CFI_type_kind_shift))
183 #define CFI_type_int_fast16_t (CFI_type_Integer + (2 << CFI_type_kind_shift))
184 #define CFI_type_int_fast32_t (CFI_type_Integer + (4 << CFI_type_kind_shift))
185 #define CFI_type_int_fast64_t (CFI_type_Integer + (8 << CFI_type_kind_shift))
186 #define CFI_type_intmax_t (CFI_type_Integer + (8 << CFI_type_kind_shift))
187 #define CFI_type_intptr_t (CFI_type_Integer + (8 << CFI_type_kind_shift))
188 #define CFI_type_ptrdiff_t (CFI_type_Integer + (8 << CFI_type_kind_shift))
189 #define CFI_type_int128_t (CFI_type_Integer + (16 << CFI_type_kind_shift))
190 #define CFI_type_int_least128_t (CFI_type_Integer + (16 << CFI_type_kind_shift))
191 #define CFI_type_int_fast128_t (CFI_type_Integer + (16 << CFI_type_kind_shift))
192 #define CFI_type_Bool (CFI_type_Logical + (1 << CFI_type_kind_shift))
193 #define CFI_type_float (CFI_type_Real + (4 << CFI_type_kind_shift))
194 #define CFI_type_double (CFI_type_Real + (8 << CFI_type_kind_shift))
195 #define CFI_type_long_double (CFI_type_Real + (10 << CFI_type_kind_shift))
196 #define CFI_type_float128 (CFI_type_Real + (16 << CFI_type_kind_shift))
197 #define CFI_type_float_Complex (CFI_type_Complex + (4 << CFI_type_kind_shift))
198 #define CFI_type_double_Complex (CFI_type_Complex + (8 << CFI_type_kind_shift))
199 #define CFI_type_long_double_Complex (CFI_type_Complex + (10 << CFI_type_kind_shift))
200 #define CFI_type_float128_Complex (CFI_type_Complex + (16 << CFI_type_kind_shift))
206 #endif /* ISO_FORTRAN_BINDING_H */