1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING.  If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_f77_fcn_h)
27 #define octave_f77_fcn_h 1
28 
29 #include "octave-config.h"
30 
31 #include "lo-error.h"
32 #include "quit.h"
33 
34 #if defined (__cplusplus)
35 #  include <cstddef>
36 #  include <limits>
37 using std::size_t;
38 extern "C" {
39 #else
40 #  include <stddef.h>
41 #endif
42 
43 /* This macro is obsolete.  */
44 
45 #define F77_XFCN(f, F, args)                    \
46   F77_FUNC (f, F) args
47 
48 #if ! defined (F77_FCN)
49 #  define F77_FCN(f, F) F77_FUNC (f, F)
50 #endif
51 
52 /*
53 
54 The following macros are used for handling Fortran <-> C calling
55 conventions.  They are defined below for three different types of
56 systems, Cray (possibly now obsolete), Visual Fortran, and gfortran.
57 Note that we don't attempt to handle Fortran functions, we always use
58 subroutine wrappers for them and pass the return value as an extra
59 argument.
60 
61 Use these macros to pass character strings from C to Fortran:
62 
63   F77_CHAR_ARG(x)
64   F77_CONST_CHAR_ARG(x)
65   F77_CXX_STRING_ARG(x)
66   F77_CHAR_ARG_LEN(l)
67   F77_CHAR_ARG_DECL
68   F77_CONST_CHAR_ARG_DECL
69   F77_CHAR_ARG_LEN_DECL
70 
71 Use these macros to write C-language functions that accept
72 Fortran-style character strings:
73 
74   F77_CHAR_ARG_DEF(s, len)
75   F77_CONST_CHAR_ARG_DEF(s, len)
76   F77_CHAR_ARG_LEN_DEF(len)
77   F77_CHAR_ARG_USE(s)
78   F77_CHAR_ARG_LEN_USE(s, len)
79 
80 Use these macros for C++ code
81 
82   F77_INT         Equivalent to Fortran INTEGER type
83   F77_INT4        Equivalent to Fortran INTEGER*4 type
84   F77_DBLE        Equivalent to Fortran DOUBLE PRECISION type
85   F77_REAL        Equivalent to Fortran REAL type
86   F77_CMPLX       Equivalent to Fortran COMPLEX type
87   F77_DBLE_CMPLX  Equivalent to Fortran DOUBLE COMPLEX type
88   F77_LOGICAL     Equivalent to Fortran LOGICAL type
89   F77_RET_T       Return type of a C++ function that acts like a
90                   Fortran subroutine.
91 
92 Use these macros to return from C-language functions that are supposed
93 to act like Fortran subroutines.  F77_NORETURN is intended to be used
94 as the last statement of such a function that has been tagged with a
95 "noreturn" attribute.  If the compiler supports the "noreturn"
96 attribute or if F77_RET_T is void, then it should expand to nothing so
97 that we avoid warnings about functions tagged as "noreturn"
98 containing a return statement.  Otherwise, it should expand to a
99 statement that returns the given value so that we avoid warnings about
100 not returning a value from a function declared to return something.
101 
102   F77_RETURN(retval)
103   F77_NORETURN(retval)
104 
105 */
106 
107 #if defined (F77_USES_CRAY_CALLING_CONVENTION)
108 
109 #include <fortran.h>
110 
111 /* Use these macros to pass character strings from C to Fortran.  Cray
112    Fortran uses a descriptor structure to pass a pointer to the string
113    and the length in a single argument.  */
114 
115 #define F77_CHAR_ARG(x) octave_make_cray_ftn_ch_dsc (x, strlen (x))
116 #define F77_CONST_CHAR_ARG(x)                           \
117   octave_make_cray_const_ftn_ch_dsc (x, strlen (x))
118 #define F77_CHAR_ARG2(x, l) octave_make_cray_ftn_ch_dsc (x, l)
119 #define F77_CONST_CHAR_ARG2(x, l) octave_make_cray_const_ftn_ch_dsc (x, l)
120 #define F77_CXX_STRING_ARG(x)                                   \
121   octave_make_cray_const_ftn_ch_dsc (x.c_str (), x.length ())
122 #define F77_CHAR_ARG_LEN(l)
123 #define F77_CHAR_ARG_LEN_TYPE
124 #define F77_CHAR_ARG_LEN_DECL
125 #define F77_CHAR_ARG_DECL octave_cray_ftn_ch_dsc
126 #define F77_CONST_CHAR_ARG_DECL octave_cray_ftn_ch_dsc
127 
128 /* Use these macros to write C-language functions that accept
129    Fortran-style character strings.  */
130 #define F77_CHAR_ARG_DEF(s, len) octave_cray_ftn_ch_dsc s
131 #define F77_CONST_CHAR_ARG_DEF(s, len) octave_cray_ftn_ch_dsc s
132 #define F77_CHAR_ARG_LEN_DEF(len)
133 #define F77_CHAR_ARG_USE(s) s.ptr
134 #define F77_CHAR_ARG_LEN_USE(s, len) (s.mask.len >> 3)
135 
136 #define F77_RET_T int
137 
138 /* Use these macros to return from C-language functions that are
139    supposed to act like Fortran subroutines.  F77_NORETURN is intended
140    to be used as the last statement of such a function that has been
141    tagged with a "noreturn" attribute.  */
142 
143 #define F77_RETURN(retval) return retval;
144 #if defined (HAVE_OCTAVE_NORETURN_ATTR)
145 #  define F77_NORETURN(retval)
146 #else
147 #  define F77_NORETURN(retval) return retval;
148 #endif
149 
150 /* FIXME: These should work for SV1 or Y-MP systems but will
151           need to be changed for others.  */
152 
153 typedef union
154 {
155   const char *const_ptr;
156   char *ptr;
157   struct
158   {
159     unsigned off : 6;
160     unsigned len : 26;
161     unsigned add : 32;
162   } mask;
163 } octave_cray_descriptor;
164 
165 typedef void *octave_cray_ftn_ch_dsc;
166 
167 #if defined (__cplusplus)
168 #  define OCTAVE_F77_FCN_INLINE inline
169 #else
170 #  define OCTAVE_F77_FCN_INLINE
171 #endif
172 
173 static OCTAVE_F77_FCN_INLINE octave_cray_ftn_ch_dsc
octave_make_cray_ftn_ch_dsc(char * ptr_arg,unsigned long len_arg)174 octave_make_cray_ftn_ch_dsc (char *ptr_arg, unsigned long len_arg)
175 {
176   octave_cray_descriptor desc;
177   desc.ptr = ptr_arg;
178   desc.mask.len = len_arg << 3;
179   return *((octave_cray_ftn_ch_dsc *) &desc);
180 }
181 
182 static OCTAVE_F77_FCN_INLINE octave_cray_ftn_ch_dsc
octave_make_cray_const_ftn_ch_dsc(const char * ptr_arg,unsigned long len_arg)183 octave_make_cray_const_ftn_ch_dsc (const char *ptr_arg, unsigned long len_arg)
184 {
185   octave_cray_descriptor desc;
186   desc.const_ptr = ptr_arg;
187   desc.mask.len = len_arg << 3;
188   return *((octave_cray_ftn_ch_dsc *) &desc);
189 }
190 
191 #undef OCTAVE_F77_FCN_INLINE
192 
193 #elif defined (F77_USES_VISUAL_FORTRAN_CALLING_CONVENTION)
194 
195 /* Use these macros to pass character strings from C to Fortran.
196    Visual Fortran inserts the length after each character string
197    argument.  */
198 
199 #define F77_CHAR_ARG(x) x, strlen (x)
200 #define F77_CONST_CHAR_ARG(x) F77_CHAR_ARG (x)
201 #define F77_CHAR_ARG2(x, l) x, l
202 #define F77_CONST_CHAR_ARG2(x, l) F77_CHAR_ARG2 (x, l)
203 #define F77_CXX_STRING_ARG(x) F77_CONST_CHAR_ARG2 (x.c_str (), x.length ())
204 #define F77_CHAR_ARG_LEN(l)
205 #define F77_CHAR_ARG_LEN_TYPE int
206 #define F77_CHAR_ARG_LEN_DECL
207 #define F77_CHAR_ARG_DECL char *, F77_CHAR_ARG_LEN_TYPE
208 #define F77_CONST_CHAR_ARG_DECL const char *, F77_CHAR_ARG_LEN_TYPE
209 
210 #define F77_CHAR_ARG_DEF(s, len) char *s, F77_CHAR_ARG_LEN_TYPE len
211 #define F77_CONST_CHAR_ARG_DEF(s, len) const char *s, F77_CHAR_ARG_LEN_TYPE len
212 #define F77_CHAR_ARG_LEN_DEF(len)
213 #define F77_CHAR_ARG_USE(s) s
214 #define F77_CHAR_ARG_LEN_USE(s, len) len
215 
216 #define F77_RET_T void
217 
218 #define F77_RETURN(retval) return;
219 #define F77_NORETURN(retval)
220 
221 #elif defined (F77_USES_GFORTRAN_CALLING_CONVENTION)
222 
223 /* Use these macros to pass character strings from C to Fortran.
224    gfortran appends length arguments for assumed size character
225    strings to the and ignores others.
226 
227    FIXME: I don't think we correctly handle the case of mixing some
228    fixed-length and some assumed-length character string arguments as
229    we don't handle each case separately, so it seems there could be
230    mismatch?  However, I don't think we currently have to handle this
231    case in Octave.  */
232 
233 #define F77_CHAR_ARG(x) x
234 #define F77_CONST_CHAR_ARG(x) F77_CHAR_ARG (x)
235 #define F77_CHAR_ARG2(x, l) x
236 #define F77_CONST_CHAR_ARG2(x, l) F77_CHAR_ARG2 (x, l)
237 #define F77_CXX_STRING_ARG(x) F77_CONST_CHAR_ARG2 (x.c_str (), x.length ())
238 #define F77_CHAR_ARG_LEN(l) , l
239 #if defined (__GNUC__) && __GNUC__ >= 8
240 #  define F77_CHAR_ARG_LEN_TYPE size_t
241 #else
242 #  define F77_CHAR_ARG_LEN_TYPE int
243 #endif
244 #define F77_CHAR_ARG_LEN_DECL , F77_CHAR_ARG_LEN_TYPE
245 #define F77_CHAR_ARG_DECL char *
246 #define F77_CONST_CHAR_ARG_DECL const char *
247 
248 #define F77_CHAR_ARG_DEF(s, len) char *s
249 #define F77_CONST_CHAR_ARG_DEF(s, len) const char *s
250 #define F77_CHAR_ARG_LEN_DEF(len) , F77_CHAR_ARG_LEN_TYPE len
251 #define F77_CHAR_ARG_USE(s) s
252 #define F77_CHAR_ARG_LEN_USE(s, len) len
253 
254 #define F77_RET_T void
255 
256 #define F77_RETURN(retval) return;
257 #if defined (HAVE_OCTAVE_NORETURN_ATTR)
258 #  define F77_NORETURN(retval)
259 #else
260 #  define F77_NORETURN(retval) return retval;
261 #endif
262 
263 #elif defined (F77_USES_F2C_CALLING_CONVENTION)
264 
265 /* Assume f2c-compatible calling convention.  */
266 
267 /* Use these macros to pass character strings from C to Fortran.  f2c
268    appends all length arguments at the end of the parameter list.  */
269 
270 #define F77_CHAR_ARG(x) x
271 #define F77_CONST_CHAR_ARG(x) F77_CHAR_ARG (x)
272 #define F77_CHAR_ARG2(x, l) x
273 #define F77_CONST_CHAR_ARG2(x, l) F77_CHAR_ARG2 (x, l)
274 #define F77_CXX_STRING_ARG(x) F77_CONST_CHAR_ARG2 (x.c_str (), x.length ())
275 #define F77_CHAR_ARG_LEN(l) , l
276 #define F77_CHAR_ARG_LEN_TYPE long
277 #define F77_CHAR_ARG_LEN_DECL , F77_CHAR_ARG_LEN_TYPE
278 #define F77_CHAR_ARG_DECL char *
279 #define F77_CONST_CHAR_ARG_DECL const char *
280 
281 #define F77_CHAR_ARG_DEF(s, len) char *s
282 #define F77_CONST_CHAR_ARG_DEF(s, len) const char *s
283 #define F77_CHAR_ARG_LEN_DEF(len) , F77_CHAR_ARG_LEN_TYPE len
284 #define F77_CHAR_ARG_USE(s) s
285 #define F77_CHAR_ARG_LEN_USE(s, len) len
286 
287 #define F77_RET_T int
288 
289 #define F77_RETURN(retval) return retval;
290 #if defined (HAVE_OCTAVE_NORETURN_ATTR)
291 #  define F77_NORETURN(retval)
292 #else
293 #  define F77_NORETURN(retval) return retval;
294 #endif
295 
296 #else
297 
298 #error "unknown C++ to Fortran calling convention"
299 
300 #endif
301 
302 typedef double F77_DBLE;
303 typedef float F77_REAL;
304 typedef double _Complex F77_DBLE_CMPLX;
305 typedef float _Complex F77_CMPLX;
306 typedef octave_f77_int_type F77_INT;
307 typedef int32_t F77_INT4;
308 typedef octave_f77_int_type F77_LOGICAL;
309 
310 #define F77_CMPLX_ARG(x)                        \
311   reinterpret_cast<float _Complex *> (x)
312 
313 #define F77_CONST_CMPLX_ARG(x)                  \
314   reinterpret_cast<const float _Complex *> (x)
315 
316 #define F77_DBLE_CMPLX_ARG(x)                   \
317   reinterpret_cast<double _Complex *> (x)
318 
319 #define F77_CONST_DBLE_CMPLX_ARG(x)             \
320   reinterpret_cast<const double _Complex *> (x)
321 
322 /* Build a C string local variable CS from the Fortran string parameter S
323    declared as F77_CHAR_ARG_DEF(s, len) or F77_CONST_CHAR_ARG_DEF(s, len).
324    The string will be cleaned up at the end of the current block.
325    Needs to include <cstring> and <vector>.  */
326 
327 #define F77_CSTRING(s, len, cs)                                         \
328   OCTAVE_LOCAL_BUFFER (char, cs, F77_CHAR_ARG_LEN_USE (s, len) + 1);    \
329   memcpy (cs, F77_CHAR_ARG_USE (s), F77_CHAR_ARG_LEN_USE (s, len));     \
330   cs[F77_CHAR_ARG_LEN_USE(s, len)] = '\0'
331 
332 OCTAVE_NORETURN OCTAVE_API extern
333 F77_RET_T
334 F77_FUNC (xstopx, XSTOPX) (F77_CONST_CHAR_ARG_DECL
335                            F77_CHAR_ARG_LEN_DECL);
336 
337 #if defined (__cplusplus)
338 
339 namespace octave
340 {
341   inline F77_INT
to_f77_int(octave_idx_type x)342   to_f77_int (octave_idx_type x)
343   {
344     if (x < std::numeric_limits<F77_INT>::min ()
345         || x > std::numeric_limits<F77_INT>::max ())
346       (*current_liboctave_error_handler)
347         ("integer dimension or index out of range for Fortran INTEGER type");
348 
349     return static_cast<F77_INT> (x);
350   }
351 }
352 
353 #endif
354 
355 #if defined (__cplusplus)
356 }
357 #endif
358 
359 #endif
360