9e89c0615e4c83ea97a3195fd54271f2bbdab831
[fw/sdcc] / support / cpp2 / system.h
1 /* Get common system includes and various definitions and declarations based
2    on autoconf macros.
3    Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC 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 2, or (at your option)
10 any later version.
11
12 GNU CC 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.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22
23 #ifndef GCC_SYSTEM_H
24 #define GCC_SYSTEM_H
25
26 /* This is the location of the online document giving information how
27    to report bugs. If you change this string, also check for strings
28    not under control of the preprocessor.  */
29 #define GCCBUGURL "<URL:http://www.gnu.org/software/gcc/bugs.html>"
30
31 #include "ansidecl.h"
32
33 /* We must include stdarg.h/varargs.h before stdio.h. */
34 #ifdef ANSI_PROTOTYPES
35 #include <stdarg.h>
36 #else
37 #include <varargs.h>
38 #endif
39
40 #ifndef va_copy
41 # ifdef __va_copy
42 #   define va_copy(d,s)  __va_copy((d),(s))
43 # else
44 #   define va_copy(d,s)  ((d) = (s))
45 # endif
46 #endif
47
48 #ifdef HAVE_STDDEF_H
49 # include <stddef.h>
50 #endif
51
52 #include <stdio.h>
53
54 #define CHAR_BIT 8
55
56 /* Define a generic NULL if one hasn't already been defined.  */
57 #ifndef NULL
58 #define NULL 0
59 #endif
60
61 /* There are an extraordinary number of issues with <ctype.h>.
62    The last straw is that it varies with the locale.  Use libiberty's
63    replacement instead.  */
64 #if defined(__APPLE__) && defined(__MACH__)
65 #include <libiberty/safe-ctype.h>
66 #else
67 #include <safe-ctype.h>
68 #endif
69
70 /* Define a default escape character; it's different for EBCDIC.  */
71 #ifndef TARGET_ESC
72 #define TARGET_ESC 033
73 #endif
74
75 #include <sys/types.h>
76
77 #include <errno.h>
78
79 #if !defined (errno) && defined (HAVE_DECL_ERRNO) && !HAVE_DECL_ERRNO
80 extern int errno;
81 #endif
82
83 #ifdef STRING_WITH_STRINGS
84 # include <string.h>
85 # include <strings.h>
86 #else
87 # ifdef HAVE_STRING_H
88 #  include <string.h>
89 # else
90 #  ifdef HAVE_STRINGS_H
91 #   include <strings.h>
92 #  endif
93 # endif
94 #endif
95
96 #ifdef HAVE_STDLIB_H
97 # include <stdlib.h>
98 #endif
99
100 /* If we don't have an overriding definition, set SUCCESS_EXIT_CODE and
101    FATAL_EXIT_CODE to EXIT_SUCCESS and EXIT_FAILURE respectively,
102    or 0 and 1 if those macros are not defined.  */
103 #ifndef SUCCESS_EXIT_CODE
104 # ifdef EXIT_SUCCESS
105 #  define SUCCESS_EXIT_CODE EXIT_SUCCESS
106 # else
107 #  define SUCCESS_EXIT_CODE 0
108 # endif
109 #endif
110
111 #ifndef FATAL_EXIT_CODE
112 # ifdef EXIT_FAILURE
113 #  define FATAL_EXIT_CODE EXIT_FAILURE
114 # else
115 #  define FATAL_EXIT_CODE 1
116 # endif
117 #endif
118
119 #ifdef HAVE_UNISTD_H
120 # include <unistd.h>
121 #endif
122
123 #ifdef HAVE_SYS_PARAM_H
124 # include <sys/param.h>
125 #endif
126
127 #if HAVE_LIMITS_H
128 # include <limits.h>
129 #endif
130
131 /* Get definitions of HOST_WIDE_INT and HOST_WIDEST_INT.  */
132 #include "hwint.h"
133
134 /* Infrastructure for defining missing _MAX and _MIN macros.  Note that
135    macros defined with these cannot be used in #if.  */
136
137 /* The extra casts work around common compiler bugs.  */
138 #define INTTYPE_SIGNED(t) (! ((t) 0 < (t) -1))
139 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
140    It is necessary at least when t == time_t.  */
141 #define INTTYPE_MINIMUM(t) ((t) (INTTYPE_SIGNED (t) \
142                              ? ~ (t) 0 << (sizeof(t) * CHAR_BIT - 1) : (t) 0))
143 #define INTTYPE_MAXIMUM(t) ((t) (~ (t) 0 - INTTYPE_MINIMUM (t)))
144
145 /* Use that infrastructure to provide a few constants.  */
146 #ifndef UCHAR_MAX
147 # define UCHAR_MAX INTTYPE_MAXIMUM (unsigned char)
148 #endif
149
150 #ifdef TIME_WITH_SYS_TIME
151 # include <sys/time.h>
152 # include <time.h>
153 #else
154 # if HAVE_SYS_TIME_H
155 #  include <sys/time.h>
156 # else
157 #  ifdef HAVE_TIME_H
158 #   include <time.h>
159 #  endif
160 # endif
161 #endif
162
163 #ifdef HAVE_FCNTL_H
164 # include <fcntl.h>
165 #else
166 # ifdef HAVE_SYS_FILE_H
167 #  include <sys/file.h>
168 # endif
169 #endif
170
171 #ifndef SEEK_SET
172 # define SEEK_SET 0
173 # define SEEK_CUR 1
174 # define SEEK_END 2
175 #endif
176 #ifndef F_OK
177 # define F_OK 0
178 # define X_OK 1
179 # define W_OK 2
180 # define R_OK 4
181 #endif
182 #ifndef O_RDONLY
183 # define O_RDONLY 0
184 #endif
185 #ifndef O_WRONLY
186 # define O_WRONLY 1
187 #endif
188
189 /* Some systems define these in, e.g., param.h.  We undefine these names
190    here to avoid the warnings.  We prefer to use our definitions since we
191    know they are correct.  */
192
193 #undef MIN
194 #undef MAX
195 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
196 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
197
198 /* Returns the least number N such that N * Y >= X.  */
199 #define CEIL(x,y) (((x) + (y) - 1) / (y))
200
201 #ifdef HAVE_SYS_WAIT_H
202 #include <sys/wait.h>
203 #endif
204
205 #ifndef WIFSIGNALED
206 #define WIFSIGNALED(S) (((S) & 0xff) != 0 && ((S) & 0xff) != 0x7f)
207 #endif
208 #ifndef WTERMSIG
209 #define WTERMSIG(S) ((S) & 0x7f)
210 #endif
211 #ifndef WIFEXITED
212 #define WIFEXITED(S) (((S) & 0xff) == 0)
213 #endif
214 #ifndef WEXITSTATUS
215 #define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
216 #endif
217 #ifndef WSTOPSIG
218 #define WSTOPSIG WEXITSTATUS
219 #endif
220
221 /* The HAVE_DECL_* macros are three-state, undefined, 0 or 1.  If they
222    are defined to 0 then we must provide the relevant declaration
223    here.  These checks will be in the undefined state while configure
224    is running so be careful to test "defined (HAVE_DECL_*)".  */
225
226 #if defined (HAVE_DECL_ATOF) && !HAVE_DECL_ATOF
227 extern double atof PARAMS ((const char *));
228 #endif
229
230 #if defined (HAVE_DECL_ATOL) && !HAVE_DECL_ATOL
231 extern long atol PARAMS ((const char *));
232 #endif
233
234 #if defined (HAVE_DECL_FREE) && !HAVE_DECL_FREE
235 extern void free PARAMS ((PTR));
236 #endif
237
238 #if defined (HAVE_DECL_GETCWD) && !HAVE_DECL_GETCWD
239 extern char *getcwd PARAMS ((char *, size_t));
240 #endif
241
242 #if defined (HAVE_DECL_GETENV) && !HAVE_DECL_GETENV
243 extern char *getenv PARAMS ((const char *));
244 #endif
245
246 #if defined (HAVE_DECL_GETOPT) && !HAVE_DECL_GETOPT
247 extern int getopt PARAMS ((int, char * const *, const char *));
248 #endif
249
250 #if defined (HAVE_DECL_GETWD) && !HAVE_DECL_GETWD
251 extern char *getwd PARAMS ((char *));
252 #endif
253
254 #if defined (HAVE_DECL_SBRK) && !HAVE_DECL_SBRK
255 extern PTR sbrk PARAMS ((int));
256 #endif
257
258 #if defined (HAVE_DECL_STRSTR) && !HAVE_DECL_STRSTR
259 extern char *strstr PARAMS ((const char *, const char *));
260 #endif
261
262 #ifdef HAVE_MALLOC_H
263 #include <malloc.h>
264 #endif
265
266 #if defined (HAVE_DECL_MALLOC) && !HAVE_DECL_MALLOC
267 extern PTR malloc PARAMS ((size_t));
268 #endif
269
270 #if defined (HAVE_DECL_CALLOC) && !HAVE_DECL_CALLOC
271 extern PTR calloc PARAMS ((size_t, size_t));
272 #endif
273
274 #if defined (HAVE_DECL_REALLOC) && !HAVE_DECL_REALLOC
275 extern PTR realloc PARAMS ((PTR, size_t));
276 #endif
277
278 #ifdef HAVE_GETRLIMIT
279 # if defined (HAVE_DECL_GETRLIMIT) && !HAVE_DECL_GETRLIMIT
280 #  ifndef getrlimit
281 #   ifdef ANSI_PROTOTYPES
282 struct rlimit;
283 #   endif
284 extern int getrlimit PARAMS ((int, struct rlimit *));
285 #  endif
286 # endif
287 #endif
288
289 #ifdef HAVE_SETRLIMIT
290 # if defined (HAVE_DECL_SETRLIMIT) && !HAVE_DECL_SETRLIMIT
291 #  ifndef setrlimit
292 #   ifdef ANSI_PROTOTYPES
293 struct rlimit;
294 #   endif
295 extern int setrlimit PARAMS ((int, const struct rlimit *));
296 #  endif
297 # endif
298 #endif
299
300 #if !defined(__STDC__) && !defined(volatile)
301 #define volatile
302 #endif
303
304 #if defined (HAVE_DECL_ABORT) && !HAVE_DECL_ABORT
305 extern void abort PARAMS ((void));
306 #endif
307
308 /* 1 if we have C99 designated initializers.  */
309 #if !defined(HAVE_DESIGNATED_INITIALIZERS)
310 #define HAVE_DESIGNATED_INITIALIZERS \
311   ((GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L))
312 #endif
313
314 #if HAVE_SYS_STAT_H
315 # include <sys/stat.h>
316 #endif
317
318 /* Test if something is a normal file.  */
319 #ifndef S_ISREG
320 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
321 #endif
322
323 /* Test if something is a directory.  */
324 #ifndef S_ISDIR
325 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
326 #endif
327
328 /* Test if something is a character special file.  */
329 #ifndef S_ISCHR
330 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
331 #endif
332
333 /* Test if something is a block special file.  */
334 #ifndef S_ISBLK
335 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
336 #endif
337
338 /* Test if something is a socket.  */
339 #ifndef S_ISSOCK
340 # ifdef S_IFSOCK
341 #   define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
342 # else
343 #   define S_ISSOCK(m) 0
344 # endif
345 #endif
346
347 /* Test if something is a FIFO.  */
348 #ifndef S_ISFIFO
349 # ifdef S_IFIFO
350 #  define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
351 # else
352 #  define S_ISFIFO(m) 0
353 # endif
354 #endif
355
356 /* Approximate O_NONBLOCK.  */
357 #ifndef O_NONBLOCK
358 #define O_NONBLOCK O_NDELAY
359 #endif
360
361 /* Approximate O_NOCTTY.  */
362 #ifndef O_NOCTTY
363 #define O_NOCTTY 0
364 #endif
365
366 /* Define well known filenos if the system does not define them.  */
367 #ifndef STDIN_FILENO
368 # define STDIN_FILENO   0
369 #endif
370 #ifndef STDOUT_FILENO
371 # define STDOUT_FILENO  1
372 #endif
373 #ifndef STDERR_FILENO
374 # define STDERR_FILENO  2
375 #endif
376
377 /* Some systems have mkdir that takes a single argument. */
378 #ifdef MKDIR_TAKES_ONE_ARG
379 # define mkdir(a,b) mkdir(a)
380 #endif
381
382 /* Provide a way to print an address via printf.  */
383 #ifndef HOST_PTR_PRINTF
384 # ifdef HAVE_PRINTF_PTR
385 #  define HOST_PTR_PRINTF "%p"
386 # else
387 #  define HOST_PTR_PRINTF \
388     (sizeof (int) == sizeof (char *) ? "%x" \
389      : sizeof (long) == sizeof (char *) ? "%lx" : "%llx")
390 # endif
391 #endif /* ! HOST_PTR_PRINTF */
392
393 /* By default, colon separates directories in a path.  */
394 #ifndef PATH_SEPARATOR
395 #define PATH_SEPARATOR ':'
396 #endif
397
398 #ifndef DIR_SEPARATOR
399 #define DIR_SEPARATOR '/'
400 #endif
401
402 /* Define IS_DIR_SEPARATOR.  */
403 #ifndef DIR_SEPARATOR_2
404 # define IS_DIR_SEPARATOR(CH) ((CH) == DIR_SEPARATOR)
405 #else /* DIR_SEPARATOR_2 */
406 # define IS_DIR_SEPARATOR(CH) \
407         (((CH) == DIR_SEPARATOR) || ((CH) == DIR_SEPARATOR_2))
408 #endif /* DIR_SEPARATOR_2 */
409
410 /* Say how to test for an absolute pathname.  On Unix systems, this is if
411    it starts with a leading slash or a '$', the latter meaning the value of
412    an environment variable is to be used.  On machien with DOS-based
413    file systems, it is also absolute if it starts with a drive identifier.  */
414 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
415 #define IS_ABSOLUTE_PATHNAME(STR) \
416   (IS_DIR_SEPARATOR ((STR)[0]) || (STR)[0] == '$' \
417    || ((STR)[0] != '\0' && (STR)[1] == ':' && IS_DIR_SEPARATOR ((STR)[2])))
418 #else
419 #define IS_ABSOLUTE_PATHNAME(STR) \
420   (IS_DIR_SEPARATOR ((STR)[0]) || (STR)[0] == '$')
421 #endif
422
423 /* Get libiberty declarations. */
424 /* #include "libiberty.h" */
425 #include "sdcc.h"
426 #include "symcat.h"
427
428 /* Provide a default for the HOST_BIT_BUCKET.
429    This suffices for POSIX-like hosts.  */
430
431 #ifndef HOST_BIT_BUCKET
432 #define HOST_BIT_BUCKET "/dev/null"
433 #endif
434
435 /* Be conservative and only use enum bitfields with GCC.
436    FIXME: provide a complete autoconf test for buggy enum bitfields.  */
437
438 #if (GCC_VERSION > 2000)
439 #define ENUM_BITFIELD(TYPE) enum TYPE
440 #else
441 #define ENUM_BITFIELD(TYPE) unsigned int
442 #endif
443
444 #ifndef offsetof
445 #define offsetof(TYPE, MEMBER)  ((size_t) &((TYPE *)0)->MEMBER)
446 #endif
447
448 /* Traditional C cannot initialize union members of structs.  Provide
449    a macro which expands appropriately to handle it.  This only works
450    if you intend to initalize the union member to zero since it relies
451    on default initialization to zero in the traditional C case.  */
452 #ifdef __STDC__
453 #define UNION_INIT_ZERO , {0}
454 #else
455 #define UNION_INIT_ZERO
456 #endif
457
458 /* Various error reporting routines want to use __FUNCTION__.  */
459 #if (GCC_VERSION < 2007)
460 #ifndef __FUNCTION__
461 #define __FUNCTION__ "?"
462 #endif /* ! __FUNCTION__ */
463 #endif
464
465 /* As the last action in this file, we poison the identifiers that
466    shouldn't be used.  Note, luckily gcc-3.0's token-based integrated
467    preprocessor won't trip on poisoned identifiers that arrive from
468    the expansion of macros.  E.g. #define strrchr rindex, won't error
469    if rindex is poisoned after this directive is issued and later on
470    strrchr is called.
471
472    Note: We define bypass macros for the few cases where we really
473    want to use the libc memory allocation routines.  Otherwise we
474    insist you use the "x" versions from libiberty.  */
475
476 #define really_call_malloc malloc
477 #define really_call_calloc calloc
478 #define really_call_realloc realloc
479
480 #if (GCC_VERSION >= 3000)
481
482 /* Note autoconf checks for prototype declarations and includes
483    system.h while doing so.  Only poison these tokens if actually
484    compiling gcc, so that the autoconf declaration tests for malloc
485    etc don't spuriously fail.  */
486 #ifdef IN_GCC
487 #undef malloc
488 #undef realloc
489 #undef calloc
490 #undef strdup
491  #pragma GCC poison malloc realloc calloc strdup
492 #endif /* IN_GCC */
493
494 /* Note: not all uses of the `index' token (e.g. variable names and
495    structure members) have been eliminated.  */
496 #undef bcopy
497 #undef bzero
498 #undef bcmp
499 #undef rindex
500  #pragma GCC poison bcopy bzero bcmp rindex
501
502 #endif /* GCC >= 3.0 */
503
504 #endif /* ! GCC_SYSTEM_H */