2004-11-22 Daniel Berlin <dberlin@dberlin.org>
[official-gcc.git] / libgfortran / libgfortran.h
blobe73c00a480cb6fb50d9aa8c406ab0dafa9b5380c
1 /* Common declarations for all of libgfor.
2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>, and
4 Andy Vaught <andy@xena.eas.asu.edu>
6 This file is part of the GNU Fortran 95 runtime library (libgfor).
8 Libgfor is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 Libgfor is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with libgfor; see the file COPYING.LIB. If not,
20 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 #ifndef LIBGFOR_H
25 #define LIBGFOR_H
27 #include <math.h>
28 #include <stddef.h>
30 #ifndef M_PI
31 #define M_PI 3.14159265358979323846264338327
32 #endif
34 #include "config.h"
36 #if HAVE_COMPLEX_H
37 # include <complex.h>
38 #else
39 #define complex __complex__
40 #endif
42 #if HAVE_STDINT_H
43 #include <stdint.h>
44 #endif
46 #if HAVE_INTTYPES_H
47 #include <inttypes.h>
48 #endif
50 #if !defined(HAVE_STDINT_H) && !defined(HAVE_INTTYPES_H) && defined(TARGET_ILP32)
51 typedef char int8_t;
52 typedef short int16_t;
53 typedef int int32_t;
54 typedef long long int64_t;
55 typedef unsigned char uint8_t;
56 typedef unsigned short uint16_t;
57 typedef unsigned int uint32_t;
58 typedef unsigned long long uint64_t;
59 #endif
61 #if HAVE_SYS_TYPES_H
62 #include <sys/types.h>
63 #endif
64 typedef off_t gfc_offset;
66 #ifndef NULL
67 #define NULL (void *) 0
68 #endif
70 #ifndef __GNUC__
71 #define __attribute__(x)
72 #endif
74 /* For a library, a standard prefix is a requirement in order to
75 partition the namespace. It's ugly to look at and a pain to type,
76 so we hide it behind macros. */
77 #define prefix(x) _gfortran_ ## x
79 /* The only reliable way to get the offset of a field in a struct
80 in a system independent way is via this macro. */
81 #ifndef offsetof
82 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *) 0)->MEMBER)
83 #endif
85 /* TODO: find the C99 version of these an move into above ifdef. */
86 #define REALPART(z) (__real__(z))
87 #define IMAGPART(z) (__imag__(z))
88 #define COMPLEX_ASSIGN(z_, r_, i_) {__real__(z_) = (r_); __imag__(z_) = (i_);}
90 typedef int32_t GFC_INTEGER_4;
91 typedef int64_t GFC_INTEGER_8;
92 typedef uint32_t GFC_UINTEGER_4;
93 typedef uint64_t GFC_UINTEGER_8;
94 typedef GFC_INTEGER_4 GFC_LOGICAL_4;
95 typedef GFC_INTEGER_8 GFC_LOGICAL_8;
96 typedef float GFC_REAL_4;
97 typedef double GFC_REAL_8;
98 typedef complex float GFC_COMPLEX_4;
99 typedef complex double GFC_COMPLEX_8;
101 /* The following two definitions must be consistent with the types used
102 by the compiler. */
103 /* The type used of array indices, amongst other things. */
104 typedef size_t index_type;
105 /* The type used for the lengths of character variables. */
106 typedef GFC_INTEGER_4 gfc_charlen_type;
108 /* This will be 0 on little-endian machines and one on big-endian machines. */
109 #define l8_to_l4_offset prefix(l8_to_l4_offset)
110 extern int l8_to_l4_offset;
112 #define GFOR_POINTER_L8_TO_L4(p8) \
113 (l8_to_l4_offset + (GFC_LOGICAL_4 *)(p8))
115 #define GFC_INTEGER_4_HUGE \
116 (GFC_INTEGER_4)((((GFC_UINTEGER_4)1) << 31) - 1)
117 #define GFC_INTEGER_8_HUGE \
118 (GFC_INTEGER_8)((((GFC_UINTEGER_8)1) << 63) - 1)
119 #define GFC_REAL_4_HUGE FLT_MAX
120 #define GFC_REAL_8_HUGE DBL_MAX
122 #ifndef GFC_MAX_DIMENSIONS
123 #define GFC_MAX_DIMENSIONS 7
124 #endif
126 typedef struct descriptor_dimension
128 index_type stride;
129 index_type lbound;
130 index_type ubound;
132 descriptor_dimension;
134 #define GFC_ARRAY_DESCRIPTOR(r, type) \
135 struct {\
136 type *data;\
137 type *base;\
138 index_type dtype;\
139 descriptor_dimension dim[r];\
142 /* Commonly used array descriptor types. */
143 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, void) gfc_array_void;
144 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, char) gfc_array_char;
145 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_INTEGER_4) gfc_array_i4;
146 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_INTEGER_8) gfc_array_i8;
147 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_REAL_4) gfc_array_r4;
148 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_REAL_8) gfc_array_r8;
149 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_COMPLEX_4) gfc_array_c4;
150 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_COMPLEX_8) gfc_array_c8;
151 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_LOGICAL_4) gfc_array_l4;
152 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_LOGICAL_8) gfc_array_l8;
154 #define GFC_DTYPE_RANK_MASK 0x07
155 #define GFC_DTYPE_TYPE_SHIFT 3
156 #define GFC_DTYPE_TYPE_MASK 0x38
157 #define GFC_DTYPE_SIZE_SHIFT 6
159 enum
161 GFC_DTYPE_UNKNOWN = 0,
162 GFC_DTYPE_INTEGER,
163 /* TODO: recognize logical types. */
164 GFC_DTYPE_LOGICAL,
165 GFC_DTYPE_REAL,
166 GFC_DTYPE_COMPLEX,
167 GFC_DTYPE_DERIVED,
168 GFC_DTYPE_CHARACTER
171 #define GFC_DESCRIPTOR_RANK(desc) ((desc)->dtype & GFC_DTYPE_RANK_MASK)
172 #define GFC_DESCRIPTOR_TYPE(desc) (((desc)->dtype & GFC_DTYPE_TYPE_MASK) \
173 >> GFC_DTYPE_TYPE_SHIFT)
174 #define GFC_DESCRIPTOR_SIZE(desc) ((desc)->dtype >> GFC_DTYPE_SIZE_SHIFT)
175 #define GFC_DESCRIPTOR_DATA(desc) ((desc)->data)
176 #define GFC_DESCRIPTOR_DTYPE(desc) ((desc)->dtype)
178 /* Runtime library include. */
179 #define stringize(x) expand_macro(x)
180 #define expand_macro(x) # x
182 /* Runtime options structure. */
184 typedef struct
186 int stdin_unit, stdout_unit, optional_plus;
187 int allocate_init_flag, allocate_init_value;
188 int locus;
190 int separator_len;
191 const char *separator;
193 int mem_check;
194 int use_stderr, all_unbuffered, default_recl;
196 int fpu_round, fpu_precision, fpu_invalid, fpu_denormal, fpu_zerodiv,
197 fpu_overflow, fpu_underflow, fpu_precision_loss;
199 int sighup, sigint;
201 options_t;
204 #define options prefix(options)
205 extern options_t options;
208 /* Structure for statement options. */
210 typedef struct
212 const char *name;
213 int value;
215 st_option;
217 /* Runtime errors. The EOR and EOF errors are required to be negative. */
219 typedef enum
221 ERROR_FIRST = -3, /* Marker for the first error. */
222 ERROR_EOR = -2,
223 ERROR_END = -1,
224 ERROR_OK = 0, /* Indicates success, must be zero. */
225 ERROR_OS, /* Operating system error, more info in errno. */
226 ERROR_OPTION_CONFLICT,
227 ERROR_BAD_OPTION,
228 ERROR_MISSING_OPTION,
229 ERROR_ALREADY_OPEN,
230 ERROR_BAD_UNIT,
231 ERROR_FORMAT,
232 ERROR_BAD_ACTION,
233 ERROR_ENDFILE,
234 ERROR_BAD_US,
235 ERROR_READ_VALUE,
236 ERROR_READ_OVERFLOW,
237 ERROR_LAST /* Not a real error, the last error # + 1. */
239 error_codes;
242 /* The filename and line number don't go inside the globals structure.
243 They are set by the rest of the program and must be linked to. */
245 #define line prefix(line)
246 extern unsigned line; /* Location of the current libray call (optional). */
248 #define filename prefix(filename)
249 extern char *filename;
251 /* Avoid conflicting prototypes of alloca() in system headers by using
252 GCC's builtin alloca(). */
254 #define gfc_alloca(x) __builtin_alloca(x)
257 /* main.c */
259 #define library_start prefix(library_start)
260 void library_start (void);
262 #define library_end prefix(library_end)
263 void library_end (void);
265 #define set_args prefix(set_args)
266 void set_args (int, char **);
268 #define get_args prefix(get_args)
269 void get_args (int *, char ***);
272 /* error.c */
273 #define itoa prefix(itoa)
274 char *itoa (int64_t);
276 #define xtoa prefix(xtoa)
277 char *xtoa (uint64_t);
279 #define os_error prefix(os_error)
280 void os_error (const char *) __attribute__ ((noreturn));
282 #define show_locus prefix(show_locus)
283 void show_locus (void);
285 #define runtime_error prefix(runtime_error)
286 void runtime_error (const char *) __attribute__ ((noreturn));
288 #define internal_error prefix(internal_error)
289 void internal_error (const char *) __attribute__ ((noreturn));
291 #define get_oserror prefix(get_oserror)
292 const char *get_oserror (void);
294 #define write_error prefix(write_error)
295 void write_error (const char *);
297 #define sys_exit prefix(sys_exit)
298 void sys_exit (int) __attribute__ ((noreturn));
300 #define st_printf prefix(st_printf)
301 int st_printf (const char *, ...) __attribute__ ((format (printf, 1, 2)));
303 #define st_sprintf prefix(st_sprintf)
304 void st_sprintf (char *, const char *, ...) __attribute__ ((format (printf, 2, 3)));
306 #define translate_error prefix(translate_error)
307 const char *translate_error (int);
309 #define generate_error prefix(generate_error)
310 void generate_error (int, const char *);
313 /* memory.c */
315 #define memory_init prefix(memory_init)
316 void memory_init (void);
318 #define runtime_cleanup prefix(runtime_cleanup)
319 void runtime_cleanup (void);
321 #define get_mem prefix(get_mem)
322 void *get_mem (size_t) __attribute__ ((malloc));
324 #define free_mem prefix(free_mem)
325 void free_mem (void *);
327 #define internal_malloc_size prefix(internal_malloc_size)
328 void *internal_malloc_size (size_t);
330 #define internal_malloc prefix(internal_malloc)
331 void *internal_malloc (GFC_INTEGER_4);
333 #define internal_malloc64 prefix(internal_malloc64)
334 void *internal_malloc64 (GFC_INTEGER_8);
336 #define internal_free prefix(internal_free)
337 void internal_free (void *);
339 #define allocate prefix(allocate)
340 void allocate (void **, GFC_INTEGER_4, GFC_INTEGER_4 *);
342 #define allocate64 prefix(allocate64)
343 void allocate64 (void **, GFC_INTEGER_8, GFC_INTEGER_4 *);
345 #define deallocate prefix(deallocate)
346 void deallocate (void **, GFC_INTEGER_4 *);
349 /* environ.c */
351 #define check_buffered prefix(check_buffered)
352 int check_buffered (int);
354 #define init_variables prefix(init_variables)
355 void init_variables (void);
357 #define show_variables prefix(show_variables)
358 void show_variables (void);
361 /* string.c */
363 #define find_option prefix(find_option)
364 int find_option (const char *, int, st_option *, const char *);
366 #define fstrlen prefix(fstrlen)
367 int fstrlen (const char *, int);
369 #define fstrcpy prefix(fstrcpy)
370 void fstrcpy (char *, int, const char *, int);
372 #define cf_strcpy prefix(cf_strcpy)
373 void cf_strcpy (char *, int, const char *);
375 /* io.c */
377 #define init_units prefix(init_units)
378 void init_units (void);
380 #define close_units prefix(close_units)
381 void close_units (void);
383 /* stop.c */
384 #define stop_numeric prefix(stop_numeric)
385 void stop_numeric (GFC_INTEGER_4);
387 /* reshape_packed.c */
388 #define reshape_packed prefix(reshape_packed)
389 void reshape_packed (char *, index_type, const char *, index_type,
390 const char *, index_type);
392 /* Repacking functions. */
393 #define internal_pack prefix(internal_pack)
394 void *internal_pack (gfc_array_char *);
396 #define internal_unpack prefix(internal_unpack)
397 void internal_unpack (gfc_array_char *, const void *);
399 #define internal_pack_4 prefix(internal_pack_4)
400 GFC_INTEGER_4 *internal_pack_4 (gfc_array_i4 *);
402 #define internal_pack_8 prefix(internal_pack_8)
403 GFC_INTEGER_8 *internal_pack_8 (gfc_array_i8 *);
405 #define internal_unpack_4 prefix(internal_unpack_4)
406 void internal_unpack_4 (gfc_array_i4 *, const GFC_INTEGER_4 *);
408 #define internal_unpack_8 prefix(internal_unpack_8)
409 void internal_unpack_8 (gfc_array_i8 *, const GFC_INTEGER_8 *);
411 /* date_and_time.c */
413 #define date_and_time prefix(date_and_time)
414 void date_and_time (char *, char *, char *, gfc_array_i4 *,
415 GFC_INTEGER_4, GFC_INTEGER_4, GFC_INTEGER_4);
417 /* string_intrinsics.c */
419 #define compare_string prefix(compare_string)
420 GFC_INTEGER_4 compare_string (GFC_INTEGER_4, const char *,
421 GFC_INTEGER_4, const char *);
423 /* random.c */
425 #define random_seed prefix(random_seed)
426 void random_seed (GFC_INTEGER_4 * size, gfc_array_i4 * put,
427 gfc_array_i4 * get);
429 /* normalize.c */
431 #define normalize_r4_i4 prefix(normalize_r4_i4)
432 GFC_REAL_4 normalize_r4_i4 (GFC_UINTEGER_4, GFC_UINTEGER_4);
434 #define normalize_r8_i8 prefix(normalize_r8_i8)
435 GFC_REAL_8 normalize_r8_i8 (GFC_UINTEGER_8, GFC_UINTEGER_8);
437 /* size.c */
439 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, void) array_t;
441 #define size0 prefix(size0)
442 index_type size0 (const array_t * array);
444 #endif