OSDN Git Service

* system.h (malloc, realloc, calloc, strdup, bzero, bcmp, rindex):
[pf3gnuchains/gcc-fork.git] / gcc / 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 /* We must include stdarg.h/varargs.h before stdio.h. */
32 #ifdef ANSI_PROTOTYPES
33 #include <stdarg.h>
34 #else
35 #include <varargs.h>
36 #endif
37
38 #ifndef va_copy
39 # ifdef __va_copy
40 #   define va_copy(d,s)  __va_copy((d),(s))
41 # else
42 #   define va_copy(d,s)  ((d) = (s))
43 # endif
44 #endif
45
46 #ifdef HAVE_STDDEF_H
47 # include <stddef.h>
48 #endif
49
50 #include <stdio.h>
51
52 /* Define a generic NULL if one hasn't already been defined.  */
53 #ifndef NULL
54 #define NULL 0
55 #endif
56
57 /* The compiler is not a multi-threaded application and therefore we
58    do not have to use the locking functions.
59
60    HAVE_DECL_PUTC_UNLOCKED actually indicates whether or not the IO
61    code is multi-thread safe by default.  If it is set to 0, then do
62    not worry about using the _unlocked functions.
63    
64    fputs_unlocked is an extension and needs to be prototyped specially.  */
65
66 #if defined HAVE_PUTC_UNLOCKED && (defined (HAVE_DECL_PUTC_UNLOCKED) && HAVE_DECL_PUTC_UNLOCKED)
67 # undef putc
68 # define putc(C, Stream) putc_unlocked (C, Stream)
69 #endif
70 #if defined HAVE_FPUTC_UNLOCKED && (defined (HAVE_DECL_PUTC_UNLOCKED) && HAVE_DECL_PUTC_UNLOCKED)
71 # undef fputc
72 # define fputc(C, Stream) fputc_unlocked (C, Stream)
73 #endif
74 #if defined HAVE_FPUTS_UNLOCKED && (defined (HAVE_DECL_PUTC_UNLOCKED) && HAVE_DECL_PUTC_UNLOCKED)
75 # undef fputs
76 # define fputs(String, Stream) fputs_unlocked (String, Stream)
77 # if defined (HAVE_DECL_FPUTS_UNLOCKED) && !HAVE_DECL_FPUTS_UNLOCKED
78 extern int fputs_unlocked PARAMS ((const char *, FILE *));
79 # endif
80 #endif
81
82 /* There are an extraordinary number of issues with <ctype.h>.
83    The last straw is that it varies with the locale.  Use libiberty's
84    replacement instead.  */
85 #include <safe-ctype.h>
86
87 /* Define a default escape character; it's different for EBCDIC.  */
88 #ifndef TARGET_ESC
89 #define TARGET_ESC 033
90 #endif
91
92 #include <sys/types.h>
93
94 #include <errno.h>
95
96 #ifndef errno
97 extern int errno;
98 #endif
99
100 #ifdef STRING_WITH_STRINGS
101 # include <string.h>
102 # include <strings.h>
103 #else
104 # ifdef HAVE_STRING_H
105 #  include <string.h>
106 # else
107 #  ifdef HAVE_STRINGS_H
108 #   include <strings.h>
109 #  endif
110 # endif
111 #endif
112
113 #ifdef HAVE_STDLIB_H
114 # include <stdlib.h>
115 #endif
116
117 /* If we don't have an overriding definition, set SUCCESS_EXIT_CODE and
118    FATAL_EXIT_CODE to EXIT_SUCCESS and EXIT_FAILURE respectively,
119    or 0 and 1 if those macros are not defined.  */
120 #ifndef SUCCESS_EXIT_CODE
121 # ifdef EXIT_SUCCESS
122 #  define SUCCESS_EXIT_CODE EXIT_SUCCESS
123 # else
124 #  define SUCCESS_EXIT_CODE 0
125 # endif
126 #endif
127
128 #ifndef FATAL_EXIT_CODE
129 # ifdef EXIT_FAILURE
130 #  define FATAL_EXIT_CODE EXIT_FAILURE
131 # else
132 #  define FATAL_EXIT_CODE 1
133 # endif
134 #endif
135
136 #ifdef HAVE_UNISTD_H
137 # include <unistd.h>
138 #endif
139
140 #ifdef HAVE_SYS_PARAM_H
141 # include <sys/param.h>
142 #endif
143
144 #if HAVE_LIMITS_H
145 # include <limits.h>
146 #endif
147
148 /* Find HOST_WIDEST_INT and set its bit size, type and print macros.
149    It will be the largest integer mode supported by the host which may
150    (or may not) be larger than HOST_WIDE_INT.  This must appear after
151    <limits.h> since we only use `long long' if its bigger than a
152    `long' and also if it is supported by macros in limits.h.  For old
153    hosts which don't have a limits.h (and thus won't include it in
154    stage2 cause we don't rerun configure) we assume gcc supports long
155    long.)  Note, you won't get these defined if you don't include
156    {ht}config.h before this file to set the HOST_BITS_PER_* macros. */
157
158 #ifndef HOST_WIDEST_INT
159 # if defined (HOST_BITS_PER_LONG) && defined (HOST_BITS_PER_LONGLONG)
160 #  if (HOST_BITS_PER_LONGLONG > HOST_BITS_PER_LONG) && (defined (LONG_LONG_MAX) || defined (LONGLONG_MAX) || defined (LLONG_MAX) || defined (__GNUC__))
161 #   define HOST_BITS_PER_WIDEST_INT HOST_BITS_PER_LONGLONG
162 #   define HOST_WIDEST_INT long long
163 #   define HOST_WIDEST_INT_PRINT_DEC "%lld"
164 #   define HOST_WIDEST_INT_PRINT_UNSIGNED "%llu"
165 #   define HOST_WIDEST_INT_PRINT_HEX "0x%llx"
166 #  else
167 #   define HOST_BITS_PER_WIDEST_INT HOST_BITS_PER_LONG
168 #   define HOST_WIDEST_INT long
169 #   define HOST_WIDEST_INT_PRINT_DEC "%ld"
170 #   define HOST_WIDEST_INT_PRINT_UNSIGNED "%lu"
171 #   define HOST_WIDEST_INT_PRINT_HEX "0x%lx"
172 #  endif /*(long long>long) && (LONG_LONG_MAX||LONGLONG_MAX||LLONG_MAX||GNUC)*/
173 # endif /* defined(HOST_BITS_PER_LONG) && defined(HOST_BITS_PER_LONGLONG) */
174 #endif /* ! HOST_WIDEST_INT */
175
176 /* Infrastructure for defining missing _MAX and _MIN macros.  Note that
177    macros defined with these cannot be used in #if.  */
178
179 /* The extra casts work around common compiler bugs.  */
180 #define INTTYPE_SIGNED(t) (! ((t) 0 < (t) -1))
181 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
182    It is necessary at least when t == time_t.  */
183 #define INTTYPE_MINIMUM(t) ((t) (INTTYPE_SIGNED (t) \
184                              ? ~ (t) 0 << (sizeof(t) * CHAR_BIT - 1) : (t) 0))
185 #define INTTYPE_MAXIMUM(t) ((t) (~ (t) 0 - INTTYPE_MINIMUM (t)))
186
187 /* Use that infrastructure to provide a few constants.  */
188 #ifndef UCHAR_MAX
189 # define UCHAR_MAX INTTYPE_MAXIMUM (unsigned char)
190 #endif
191
192 #ifdef TIME_WITH_SYS_TIME
193 # include <sys/time.h>
194 # include <time.h>
195 #else
196 # if HAVE_SYS_TIME_H
197 #  include <sys/time.h>
198 # else
199 #  ifdef HAVE_TIME_H
200 #   include <time.h>
201 #  endif
202 # endif
203 #endif
204
205 #ifdef HAVE_FCNTL_H
206 # include <fcntl.h>
207 #else
208 # ifdef HAVE_SYS_FILE_H
209 #  include <sys/file.h>
210 # endif
211 #endif
212
213 #ifndef SEEK_SET
214 # define SEEK_SET 0
215 # define SEEK_CUR 1
216 # define SEEK_END 2
217 #endif
218 #ifndef F_OK
219 # define F_OK 0
220 # define X_OK 1
221 # define W_OK 2
222 # define R_OK 4
223 #endif
224 #ifndef O_RDONLY
225 # define O_RDONLY 0
226 #endif
227 #ifndef O_WRONLY
228 # define O_WRONLY 1
229 #endif
230
231 /* Some systems define these in, e.g., param.h.  We undefine these names
232    here to avoid the warnings.  We prefer to use our definitions since we
233    know they are correct.  */
234
235 #undef MIN
236 #undef MAX
237 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
238 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
239
240 /* Returns the least number N such that N * Y >= X.  */
241 #define CEIL(x,y) (((x) + (y) - 1) / (y))
242
243 #ifdef HAVE_SYS_WAIT_H
244 #include <sys/wait.h>
245 #endif
246
247 #ifndef WIFSIGNALED
248 #define WIFSIGNALED(S) (((S) & 0xff) != 0 && ((S) & 0xff) != 0x7f)
249 #endif
250 #ifndef WTERMSIG
251 #define WTERMSIG(S) ((S) & 0x7f)
252 #endif
253 #ifndef WIFEXITED
254 #define WIFEXITED(S) (((S) & 0xff) == 0)
255 #endif
256 #ifndef WEXITSTATUS
257 #define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
258 #endif
259 #ifndef WSTOPSIG
260 #define WSTOPSIG WEXITSTATUS
261 #endif
262
263 /* The HAVE_DECL_* macros are three-state, undefined, 0 or 1.  If they
264    are defined to 0 then we must provide the relevant declaration
265    here.  These checks will be in the undefined state while configure
266    is running so be careful to test "defined (HAVE_DECL_*)".  */
267
268 #ifndef bcopy
269 # ifdef HAVE_BCOPY
270 #  if defined (HAVE_DECL_BCOPY) && !HAVE_DECL_BCOPY
271 extern void bcopy PARAMS ((const PTR, PTR, size_t));
272 #  endif
273 # else /* ! HAVE_BCOPY */
274 #  define bcopy(src,dst,len) memmove((dst),(src),(len))
275 # endif
276 #endif
277
278 #if defined (HAVE_DECL_ATOF) && !HAVE_DECL_ATOF
279 extern double atof PARAMS ((const char *));
280 #endif
281
282 #if defined (HAVE_DECL_ATOL) && !HAVE_DECL_ATOL
283 extern long atol PARAMS ((const char *));
284 #endif
285
286 #if defined (HAVE_DECL_FREE) && !HAVE_DECL_FREE
287 extern void free PARAMS ((PTR));
288 #endif
289
290 #if defined (HAVE_DECL_GETCWD) && !HAVE_DECL_GETCWD
291 extern char *getcwd PARAMS ((char *, size_t));
292 #endif
293
294 #if defined (HAVE_DECL_GETENV) && !HAVE_DECL_GETENV
295 extern char *getenv PARAMS ((const char *));
296 #endif
297
298 #if defined (HAVE_DECL_GETOPT) && !HAVE_DECL_GETOPT
299 extern int getopt PARAMS ((int, char * const *, const char *));
300 #endif
301
302 #if defined (HAVE_DECL_GETWD) && !HAVE_DECL_GETWD
303 extern char *getwd PARAMS ((char *));
304 #endif
305
306 #if defined (HAVE_DECL_SBRK) && !HAVE_DECL_SBRK
307 extern PTR sbrk PARAMS ((int));
308 #endif
309
310 #if defined (HAVE_DECL_STRSTR) && !HAVE_DECL_STRSTR
311 extern char *strstr PARAMS ((const char *, const char *));
312 #endif
313
314 #ifdef HAVE_MALLOC_H
315 #include <malloc.h>
316 #endif
317
318 #if defined (HAVE_DECL_MALLOC) && !HAVE_DECL_MALLOC
319 extern PTR malloc PARAMS ((size_t));
320 #endif
321
322 #if defined (HAVE_DECL_CALLOC) && !HAVE_DECL_CALLOC
323 extern PTR calloc PARAMS ((size_t, size_t));
324 #endif
325
326 #if defined (HAVE_DECL_REALLOC) && !HAVE_DECL_REALLOC
327 extern PTR realloc PARAMS ((PTR, size_t));
328 #endif
329
330 /* If the system doesn't provide strsignal, we get it defined in
331    libiberty but no declaration is supplied. */
332 #ifndef HAVE_STRSIGNAL
333 # ifndef strsignal
334 extern const char *strsignal PARAMS ((int));
335 # endif
336 #endif
337
338 #ifdef HAVE_GETRLIMIT
339 # if defined (HAVE_DECL_GETRLIMIT) && !HAVE_DECL_GETRLIMIT
340 #  ifndef getrlimit
341 #   ifdef ANSI_PROTOTYPES
342 struct rlimit;
343 #   endif
344 extern int getrlimit PARAMS ((int, struct rlimit *));
345 #  endif
346 # endif
347 #endif
348
349 #ifdef HAVE_SETRLIMIT
350 # if defined (HAVE_DECL_SETRLIMIT) && !HAVE_DECL_SETRLIMIT
351 #  ifndef setrlimit
352 #   ifdef ANSI_PROTOTYPES
353 struct rlimit;
354 #   endif
355 extern int setrlimit PARAMS ((int, const struct rlimit *));
356 #  endif
357 # endif
358 #endif
359
360 /* HAVE_VOLATILE only refers to the stage1 compiler.  We also check
361    __STDC__ and assume gcc sets it and has volatile in stage >=2. */
362 #if !defined(HAVE_VOLATILE) && !defined(__STDC__) && !defined(volatile)
363 #define volatile
364 #endif
365
366 #if defined (HAVE_DECL_ABORT) && !HAVE_DECL_ABORT
367 extern void abort PARAMS ((void));
368 #endif
369
370 /* 1 if we have C99 designated initializers.  */
371 #if !defined(HAVE_DESIGNATED_INITIALIZERS)
372 #define HAVE_DESIGNATED_INITIALIZERS \
373   ((GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L))
374 #endif
375
376 /* 1 if we have _Bool.  */
377 #ifndef HAVE__BOOL
378 # define HAVE__BOOL \
379    ((GCC_VERSION >= 3000) || (__STDC_VERSION__ >= 199901L))
380 #endif
381
382
383
384 /* Define a STRINGIFY macro that's right for ANSI or traditional C.
385    Note: if the argument passed to STRINGIFY is itself a macro, eg
386    #define foo bar, STRINGIFY(foo) will produce "foo", not "bar".
387    Although the __STDC__ case could be made to expand this via a layer
388    of indirection, the traditional C case can not do so.  Therefore
389    this behavior is not supported. */
390 #ifndef STRINGIFY
391 # ifdef HAVE_STRINGIZE
392 #  define STRINGIFY(STRING) #STRING
393 # else
394 #  define STRINGIFY(STRING) "STRING"
395 # endif
396 #endif /* ! STRINGIFY */
397
398 #if HAVE_SYS_STAT_H
399 # include <sys/stat.h>
400 #endif
401
402 /* Test if something is a normal file.  */
403 #ifndef S_ISREG
404 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
405 #endif
406
407 /* Test if something is a directory.  */
408 #ifndef S_ISDIR
409 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
410 #endif
411
412 /* Test if something is a character special file.  */
413 #ifndef S_ISCHR
414 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
415 #endif
416
417 /* Test if something is a block special file.  */
418 #ifndef S_ISBLK
419 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
420 #endif
421
422 /* Test if something is a socket.  */
423 #ifndef S_ISSOCK
424 # ifdef S_IFSOCK
425 #   define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
426 # else
427 #   define S_ISSOCK(m) 0
428 # endif
429 #endif
430
431 /* Test if something is a FIFO.  */
432 #ifndef S_ISFIFO
433 # ifdef S_IFIFO
434 #  define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
435 # else
436 #  define S_ISFIFO(m) 0
437 # endif
438 #endif
439
440 /* Approximate O_NONBLOCK.  */
441 #ifndef O_NONBLOCK
442 #define O_NONBLOCK O_NDELAY
443 #endif
444
445 /* Approximate O_NOCTTY.  */
446 #ifndef O_NOCTTY
447 #define O_NOCTTY 0
448 #endif
449
450 /* Define well known filenos if the system does not define them.  */
451 #ifndef STDIN_FILENO
452 # define STDIN_FILENO   0
453 #endif
454 #ifndef STDOUT_FILENO
455 # define STDOUT_FILENO  1
456 #endif
457 #ifndef STDERR_FILENO
458 # define STDERR_FILENO  2
459 #endif
460
461 /* Some systems have mkdir that takes a single argument. */
462 #ifdef MKDIR_TAKES_ONE_ARG
463 # define mkdir(a,b) mkdir(a)
464 #endif
465
466 /* Provide a way to print an address via printf.  */
467 #ifndef HOST_PTR_PRINTF
468 # ifdef HAVE_PRINTF_PTR
469 #  define HOST_PTR_PRINTF "%p"
470 # else
471 #  define HOST_PTR_PRINTF \
472     (sizeof (int) == sizeof (char *) ? "%x" \
473      : sizeof (long) == sizeof (char *) ? "%lx" : "%llx")
474 # endif
475 #endif /* ! HOST_PTR_PRINTF */
476
477 /* By default, colon separates directories in a path.  */
478 #ifndef PATH_SEPARATOR
479 #define PATH_SEPARATOR ':'
480 #endif
481
482 #ifndef DIR_SEPARATOR
483 #define DIR_SEPARATOR '/'
484 #endif
485
486 /* Define IS_DIR_SEPARATOR.  */
487 #ifndef DIR_SEPARATOR_2
488 # define IS_DIR_SEPARATOR(CH) ((CH) == DIR_SEPARATOR)
489 #else /* DIR_SEPARATOR_2 */
490 # define IS_DIR_SEPARATOR(CH) \
491         (((CH) == DIR_SEPARATOR) || ((CH) == DIR_SEPARATOR_2))
492 #endif /* DIR_SEPARATOR_2 */
493
494 /* Say how to test for an absolute pathname.  On Unix systems, this is if
495    it starts with a leading slash or a '$', the latter meaning the value of
496    an environment variable is to be used.  On machien with DOS-based
497    file systems, it is also absolute if it starts with a drive identifier.  */
498 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
499 #define IS_ABSOLUTE_PATHNAME(STR) \
500   (IS_DIR_SEPARATOR ((STR)[0]) || (STR)[0] == '$' \
501    || ((STR)[0] != '\0' && (STR)[1] == ':' && IS_DIR_SEPARATOR ((STR)[2])))
502 #else
503 #define IS_ABSOLUTE_PATHNAME(STR) \
504   (IS_DIR_SEPARATOR ((STR)[0]) || (STR)[0] == '$')
505 #endif
506
507 /* Get libiberty declarations. */
508 #include "libiberty.h"
509
510 /* Make sure that ONLY_INT_FIELDS has an integral value.  */
511 #ifdef ONLY_INT_FIELDS
512 #undef ONLY_INT_FIELDS
513 #define ONLY_INT_FIELDS 1
514 #else
515 #define ONLY_INT_FIELDS 0
516 #endif 
517
518 /* Provide a default for the HOST_BIT_BUCKET.
519    This suffices for POSIX-like hosts.  */
520
521 #ifndef HOST_BIT_BUCKET
522 #define HOST_BIT_BUCKET "/dev/null"
523 #endif
524
525 /* Enumerated bitfields are safe to use unless we've been explictly told
526    otherwise or if they are signed. */
527  
528 #define USE_ENUM_BITFIELDS (__GNUC__ || (!ONLY_INT_FIELDS && ENUM_BITFIELDS_ARE_UNSIGNED))
529
530 #if USE_ENUM_BITFIELDS
531 #define ENUM_BITFIELD(TYPE) enum TYPE
532 #else
533 #define ENUM_BITFIELD(TYPE) unsigned int
534 #endif
535
536 #ifndef offsetof
537 #define offsetof(TYPE, MEMBER)  ((size_t) &((TYPE *)0)->MEMBER)
538 #endif
539
540 /* Traditional C cannot initialize union members of structs.  Provide
541    a macro which expands appropriately to handle it.  This only works
542    if you intend to initalize the union member to zero since it relies
543    on default initialization to zero in the traditional C case.  */
544 #ifdef __STDC__
545 #define UNION_INIT_ZERO , {0}
546 #else
547 #define UNION_INIT_ZERO
548 #endif
549
550 /* Various error reporting routines want to use __FUNCTION__.  */
551 #if (GCC_VERSION < 2007)
552 #ifndef __FUNCTION__
553 #define __FUNCTION__ "?"
554 #endif /* ! __FUNCTION__ */
555 #endif
556
557 /* Provide some sort of boolean type.  We use stdbool.h if it's
558   available.  This is dead last because various system headers might
559   mess us up.  */
560 #undef bool
561 #undef true
562 #undef false
563 #undef TRUE
564 #undef FALSE
565
566 #ifdef HAVE_STDBOOL_H
567 # include <stdbool.h>
568 #else
569 # if !HAVE__BOOL
570 typedef char _Bool;
571 # endif
572 # define bool _Bool
573 # define true 1
574 # define false 0
575 #endif
576
577 #define TRUE true
578 #define FALSE false
579
580 /* As the last action in this file, we poison the identifiers that
581    shouldn't be used.  Note, luckily gcc-3.0's token-based integrated
582    preprocessor won't trip on poisoned identifiers that arrive from
583    the expansion of macros.  E.g. #define strrchr rindex, won't error
584    if rindex is poisoned after this directive is issued and later on
585    strrchr is called.
586
587    Note: We define bypass macros for the few cases where we really
588    want to use the libc memory allocation routines.  Otherwise we
589    insist you use the "x" versions from libiberty.  */
590
591 #define really_call_malloc malloc
592 #define really_call_calloc calloc
593 #define really_call_realloc realloc
594
595 #if (GCC_VERSION >= 3000)
596
597 #undef malloc
598 #undef realloc
599 #undef calloc
600 #undef strdup
601  #pragma GCC poison malloc realloc calloc strdup
602
603 /* Note: not all uses of `bcopy' and `index' (esp. variable names)
604    have been eliminated.  */
605 #undef bzero
606 #undef bcmp
607 #undef rindex
608  #pragma GCC poison bzero bcmp rindex
609
610 #endif /* GCC >= 3.0 */
611
612 #endif /* __GCC_SYSTEM_H__ */