133965Sjdp/* ANSI and traditional C compatability macros
289857Sobrien   Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001
378828Sobrien   Free Software Foundation, Inc.
433965Sjdp   This file is part of the GNU C Library.
533965Sjdp
633965SjdpThis program is free software; you can redistribute it and/or modify
733965Sjdpit under the terms of the GNU General Public License as published by
833965Sjdpthe Free Software Foundation; either version 2 of the License, or
933965Sjdp(at your option) any later version.
1033965Sjdp
1133965SjdpThis program is distributed in the hope that it will be useful,
1233965Sjdpbut WITHOUT ANY WARRANTY; without even the implied warranty of
1333965SjdpMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1433965SjdpGNU General Public License for more details.
1533965Sjdp
1633965SjdpYou should have received a copy of the GNU General Public License
1733965Sjdpalong with this program; if not, write to the Free Software
18218822SdimFoundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
1933965Sjdp
2033965Sjdp/* ANSI and traditional C compatibility macros
2133965Sjdp
2233965Sjdp   ANSI C is assumed if __STDC__ is #defined.
2333965Sjdp
2489857Sobrien   Macro		ANSI C definition	Traditional C definition
2589857Sobrien   -----		---- - ----------	----------- - ----------
2689857Sobrien   ANSI_PROTOTYPES	1			not defined
2789857Sobrien   PTR			`void *'		`char *'
2889857Sobrien   PTRCONST		`void *const'		`char *'
2989857Sobrien   LONG_DOUBLE		`long double'		`double'
3089857Sobrien   const		not defined		`'
3189857Sobrien   volatile		not defined		`'
3289857Sobrien   signed		not defined		`'
3389857Sobrien   VA_START(ap, var)	va_start(ap, var)	va_start(ap)
3433965Sjdp
3589857Sobrien   Note that it is safe to write "void foo();" indicating a function
3689857Sobrien   with no return value, in all K+R compilers we have been able to test.
3733965Sjdp
3889857Sobrien   For declaring functions with prototypes, we also provide these:
3933965Sjdp
4089857Sobrien   PARAMS ((prototype))
4189857Sobrien   -- for functions which take a fixed number of arguments.  Use this
4289857Sobrien   when declaring the function.  When defining the function, write a
4389857Sobrien   K+R style argument list.  For example:
4433965Sjdp
4589857Sobrien	char *strcpy PARAMS ((char *dest, char *source));
4689857Sobrien	...
4789857Sobrien	char *
4889857Sobrien	strcpy (dest, source)
4989857Sobrien	     char *dest;
5089857Sobrien	     char *source;
5189857Sobrien	{ ... }
5233965Sjdp
5333965Sjdp
5489857Sobrien   VPARAMS ((prototype, ...))
5589857Sobrien   -- for functions which take a variable number of arguments.  Use
5689857Sobrien   PARAMS to declare the function, VPARAMS to define it.  For example:
5733965Sjdp
5889857Sobrien	int printf PARAMS ((const char *format, ...));
5989857Sobrien	...
6089857Sobrien	int
6189857Sobrien	printf VPARAMS ((const char *format, ...))
6289857Sobrien	{
6389857Sobrien	   ...
6489857Sobrien	}
6533965Sjdp
6689857Sobrien   For writing functions which take variable numbers of arguments, we
6789857Sobrien   also provide the VA_OPEN, VA_CLOSE, and VA_FIXEDARG macros.  These
6889857Sobrien   hide the differences between K+R <varargs.h> and C89 <stdarg.h> more
6989857Sobrien   thoroughly than the simple VA_START() macro mentioned above.
7033965Sjdp
7189857Sobrien   VA_OPEN and VA_CLOSE are used *instead of* va_start and va_end.
7289857Sobrien   Immediately after VA_OPEN, put a sequence of VA_FIXEDARG calls
7389857Sobrien   corresponding to the list of fixed arguments.  Then use va_arg
7489857Sobrien   normally to get the variable arguments, or pass your va_list object
7589857Sobrien   around.  You do not declare the va_list yourself; VA_OPEN does it
7689857Sobrien   for you.
7733965Sjdp
7889857Sobrien   Here is a complete example:
7933965Sjdp
8089857Sobrien	int
8189857Sobrien	printf VPARAMS ((const char *format, ...))
8289857Sobrien	{
8389857Sobrien	   int result;
8433965Sjdp
8589857Sobrien	   VA_OPEN (ap, format);
8689857Sobrien	   VA_FIXEDARG (ap, const char *, format);
8733965Sjdp
8889857Sobrien	   result = vfprintf (stdout, format, ap);
8989857Sobrien	   VA_CLOSE (ap);
9033965Sjdp
9189857Sobrien	   return result;
9289857Sobrien	}
9333965Sjdp
9433965Sjdp
9589857Sobrien   You can declare variables either before or after the VA_OPEN,
9689857Sobrien   VA_FIXEDARG sequence.  Also, VA_OPEN and VA_CLOSE are the beginning
9789857Sobrien   and end of a block.  They must appear at the same nesting level,
9889857Sobrien   and any variables declared after VA_OPEN go out of scope at
9989857Sobrien   VA_CLOSE.  Unfortunately, with a K+R compiler, that includes the
10089857Sobrien   argument list.  You can have multiple instances of VA_OPEN/VA_CLOSE
10189857Sobrien   pairs in a single function in case you need to traverse the
10289857Sobrien   argument list more than once.
10333965Sjdp
10489857Sobrien   For ease of writing code which uses GCC extensions but needs to be
10589857Sobrien   portable to other compilers, we provide the GCC_VERSION macro that
10689857Sobrien   simplifies testing __GNUC__ and __GNUC_MINOR__ together, and various
10789857Sobrien   wrappers around __attribute__.  Also, __extension__ will be #defined
10889857Sobrien   to nothing if it doesn't work.  See below.
10933965Sjdp
11089857Sobrien   This header also defines a lot of obsolete macros:
11189857Sobrien   CONST, VOLATILE, SIGNED, PROTO, EXFUN, DEFUN, DEFUN_VOID,
11289857Sobrien   AND, DOTS, NOARGS.  Don't use them.  */
11333965Sjdp
11489857Sobrien#ifndef	_ANSIDECL_H
11589857Sobrien#define _ANSIDECL_H	1
11633965Sjdp
11789857Sobrien/* Every source file includes this file,
11889857Sobrien   so they will all get the switch for lint.  */
11989857Sobrien/* LINTLIBRARY */
12033965Sjdp
12189857Sobrien/* Using MACRO(x,y) in cpp #if conditionals does not work with some
12289857Sobrien   older preprocessors.  Thus we can't define something like this:
12333965Sjdp
12489857Sobrien#define HAVE_GCC_VERSION(MAJOR, MINOR) \
12589857Sobrien  (__GNUC__ > (MAJOR) || (__GNUC__ == (MAJOR) && __GNUC_MINOR__ >= (MINOR)))
12633965Sjdp
12789857Sobrienand then test "#if HAVE_GCC_VERSION(2,7)".
12833965Sjdp
12989857SobrienSo instead we use the macro below and test it against specific values.  */
13033965Sjdp
13189857Sobrien/* This macro simplifies testing whether we are using gcc, and if it
13289857Sobrien   is of a particular minimum version. (Both major & minor numbers are
13389857Sobrien   significant.)  This macro will evaluate to 0 if we are not using
13489857Sobrien   gcc at all.  */
13589857Sobrien#ifndef GCC_VERSION
13689857Sobrien#define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
13789857Sobrien#endif /* GCC_VERSION */
13833965Sjdp
139107492Sobrien#if defined (__STDC__) || defined (_AIX) || (defined (__mips) && defined (_SYSTYPE_SVR4)) || defined(_WIN32) || (defined(__alpha) && defined(__cplusplus))
14033965Sjdp/* All known AIX compilers implement these things (but don't always
14133965Sjdp   define __STDC__).  The RISC/OS MIPS compiler defines these things
14233965Sjdp   in SVR4 mode, but does not define __STDC__.  */
143107492Sobrien/* eraxxon@alumni.rice.edu: The Compaq C++ compiler, unlike many other
144107492Sobrien   C++ compilers, does not define __STDC__, though it acts as if this
145107492Sobrien   was so. (Verified versions: 5.7, 6.2, 6.3, 6.5) */
14633965Sjdp
14789857Sobrien#define ANSI_PROTOTYPES	1
14889857Sobrien#define PTR		void *
14989857Sobrien#define PTRCONST	void *const
15089857Sobrien#define LONG_DOUBLE	long double
15133965Sjdp
152218822Sdim/* PARAMS is often defined elsewhere (e.g. by libintl.h), so wrap it in
153218822Sdim   a #ifndef.  */
154218822Sdim#ifndef PARAMS
15589857Sobrien#define PARAMS(ARGS)		ARGS
156218822Sdim#endif
157218822Sdim
15889857Sobrien#define VPARAMS(ARGS)		ARGS
15989857Sobrien#define VA_START(VA_LIST, VAR)	va_start(VA_LIST, VAR)
16033965Sjdp
16189857Sobrien/* variadic function helper macros */
16289857Sobrien/* "struct Qdmy" swallows the semicolon after VA_OPEN/VA_FIXEDARG's
16389857Sobrien   use without inhibiting further decls and without declaring an
16489857Sobrien   actual variable.  */
16589857Sobrien#define VA_OPEN(AP, VAR)	{ va_list AP; va_start(AP, VAR); { struct Qdmy
16689857Sobrien#define VA_CLOSE(AP)		} va_end(AP); }
16789857Sobrien#define VA_FIXEDARG(AP, T, N)	struct Qdmy
16889857Sobrien
16989857Sobrien#undef const
17089857Sobrien#undef volatile
17189857Sobrien#undef signed
17233965Sjdp
17389857Sobrien/* inline requires special treatment; it's in C99, and GCC >=2.7 supports
17489857Sobrien   it too, but it's not in C89.  */
17589857Sobrien#undef inline
17689857Sobrien#if __STDC_VERSION__ > 199901L
17789857Sobrien/* it's a keyword */
17889857Sobrien#else
17989857Sobrien# if GCC_VERSION >= 2007
18089857Sobrien#  define inline __inline__   /* __inline__ prevents -pedantic warnings */
18189857Sobrien# else
18289857Sobrien#  define inline  /* nothing */
18389857Sobrien# endif
18489857Sobrien#endif
18533965Sjdp
18633965Sjdp/* These are obsolete.  Do not use.  */
18760484Sobrien#ifndef IN_GCC
18889857Sobrien#define CONST		const
18989857Sobrien#define VOLATILE	volatile
19089857Sobrien#define SIGNED		signed
19189857Sobrien
19233965Sjdp#define PROTO(type, name, arglist)	type name arglist
19333965Sjdp#define EXFUN(name, proto)		name proto
19433965Sjdp#define DEFUN(name, arglist, args)	name(args)
19533965Sjdp#define DEFUN_VOID(name)		name(void)
19689857Sobrien#define AND		,
19789857Sobrien#define DOTS		, ...
19889857Sobrien#define NOARGS		void
19960484Sobrien#endif /* ! IN_GCC */
20033965Sjdp
20133965Sjdp#else	/* Not ANSI C.  */
20233965Sjdp
20389857Sobrien#undef  ANSI_PROTOTYPES
20489857Sobrien#define PTR		char *
20589857Sobrien#define PTRCONST	PTR
20689857Sobrien#define LONG_DOUBLE	double
20733965Sjdp
20889857Sobrien#define PARAMS(args)		()
20989857Sobrien#define VPARAMS(args)		(va_alist) va_dcl
21089857Sobrien#define VA_START(va_list, var)	va_start(va_list)
21160484Sobrien
21289857Sobrien#define VA_OPEN(AP, VAR)		{ va_list AP; va_start(AP); { struct Qdmy
21389857Sobrien#define VA_CLOSE(AP)			} va_end(AP); }
21489857Sobrien#define VA_FIXEDARG(AP, TYPE, NAME)	TYPE NAME = va_arg(AP, TYPE)
21533965Sjdp
21689857Sobrien/* some systems define these in header files for non-ansi mode */
21789857Sobrien#undef const
21889857Sobrien#undef volatile
21989857Sobrien#undef signed
22089857Sobrien#undef inline
22189857Sobrien#define const
22289857Sobrien#define volatile
22389857Sobrien#define signed
22489857Sobrien#define inline
22533965Sjdp
22660484Sobrien#ifndef IN_GCC
22733965Sjdp#define CONST
22889857Sobrien#define VOLATILE
22989857Sobrien#define SIGNED
23089857Sobrien
23133965Sjdp#define PROTO(type, name, arglist)	type name ()
23233965Sjdp#define EXFUN(name, proto)		name()
23333965Sjdp#define DEFUN(name, arglist, args)	name arglist args;
23433965Sjdp#define DEFUN_VOID(name)		name()
23589857Sobrien#define AND		;
23689857Sobrien#define DOTS
23789857Sobrien#define NOARGS
23860484Sobrien#endif /* ! IN_GCC */
23933965Sjdp
24033965Sjdp#endif	/* ANSI C.  */
24133965Sjdp
24260484Sobrien/* Define macros for some gcc attributes.  This permits us to use the
24360484Sobrien   macros freely, and know that they will come into play for the
24460484Sobrien   version of gcc in which they are supported.  */
24560484Sobrien
24660484Sobrien#if (GCC_VERSION < 2007)
24760484Sobrien# define __attribute__(x)
24860484Sobrien#endif
24960484Sobrien
25060484Sobrien/* Attribute __malloc__ on functions was valid as of gcc 2.96. */
25160484Sobrien#ifndef ATTRIBUTE_MALLOC
25260484Sobrien# if (GCC_VERSION >= 2096)
25360484Sobrien#  define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
25460484Sobrien# else
25560484Sobrien#  define ATTRIBUTE_MALLOC
25660484Sobrien# endif /* GNUC >= 2.96 */
25760484Sobrien#endif /* ATTRIBUTE_MALLOC */
25860484Sobrien
25960484Sobrien/* Attributes on labels were valid as of gcc 2.93. */
26060484Sobrien#ifndef ATTRIBUTE_UNUSED_LABEL
261218822Sdim# if (!defined (__cplusplus) && GCC_VERSION >= 2093)
26260484Sobrien#  define ATTRIBUTE_UNUSED_LABEL ATTRIBUTE_UNUSED
26360484Sobrien# else
26460484Sobrien#  define ATTRIBUTE_UNUSED_LABEL
265218822Sdim# endif /* !__cplusplus && GNUC >= 2.93 */
26660484Sobrien#endif /* ATTRIBUTE_UNUSED_LABEL */
26760484Sobrien
26860484Sobrien#ifndef ATTRIBUTE_UNUSED
26960484Sobrien#define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
27060484Sobrien#endif /* ATTRIBUTE_UNUSED */
27160484Sobrien
272218822Sdim/* Before GCC 3.4, the C++ frontend couldn't parse attributes placed after the
273218822Sdim   identifier name.  */
274218822Sdim#if ! defined(__cplusplus) || (GCC_VERSION >= 3004)
275218822Sdim# define ARG_UNUSED(NAME) NAME ATTRIBUTE_UNUSED
276218822Sdim#else /* !__cplusplus || GNUC >= 3.4 */
277218822Sdim# define ARG_UNUSED(NAME) NAME
278218822Sdim#endif /* !__cplusplus || GNUC >= 3.4 */
279218822Sdim
28060484Sobrien#ifndef ATTRIBUTE_NORETURN
28160484Sobrien#define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
28260484Sobrien#endif /* ATTRIBUTE_NORETURN */
28360484Sobrien
284130561Sobrien/* Attribute `nonnull' was valid as of gcc 3.3.  */
285130561Sobrien#ifndef ATTRIBUTE_NONNULL
286130561Sobrien# if (GCC_VERSION >= 3003)
287130561Sobrien#  define ATTRIBUTE_NONNULL(m) __attribute__ ((__nonnull__ (m)))
288130561Sobrien# else
289130561Sobrien#  define ATTRIBUTE_NONNULL(m)
290130561Sobrien# endif /* GNUC >= 3.3 */
291130561Sobrien#endif /* ATTRIBUTE_NONNULL */
292130561Sobrien
293218822Sdim/* Attribute `pure' was valid as of gcc 3.0.  */
294218822Sdim#ifndef ATTRIBUTE_PURE
295218822Sdim# if (GCC_VERSION >= 3000)
296218822Sdim#  define ATTRIBUTE_PURE __attribute__ ((__pure__))
297218822Sdim# else
298218822Sdim#  define ATTRIBUTE_PURE
299218822Sdim# endif /* GNUC >= 3.0 */
300218822Sdim#endif /* ATTRIBUTE_PURE */
301218822Sdim
302130561Sobrien/* Use ATTRIBUTE_PRINTF when the format specifier must not be NULL.
303130561Sobrien   This was the case for the `printf' format attribute by itself
304130561Sobrien   before GCC 3.3, but as of 3.3 we need to add the `nonnull'
305130561Sobrien   attribute to retain this behavior.  */
30660484Sobrien#ifndef ATTRIBUTE_PRINTF
307130561Sobrien#define ATTRIBUTE_PRINTF(m, n) __attribute__ ((__format__ (__printf__, m, n))) ATTRIBUTE_NONNULL(m)
30860484Sobrien#define ATTRIBUTE_PRINTF_1 ATTRIBUTE_PRINTF(1, 2)
30960484Sobrien#define ATTRIBUTE_PRINTF_2 ATTRIBUTE_PRINTF(2, 3)
31060484Sobrien#define ATTRIBUTE_PRINTF_3 ATTRIBUTE_PRINTF(3, 4)
31160484Sobrien#define ATTRIBUTE_PRINTF_4 ATTRIBUTE_PRINTF(4, 5)
31260484Sobrien#define ATTRIBUTE_PRINTF_5 ATTRIBUTE_PRINTF(5, 6)
31360484Sobrien#endif /* ATTRIBUTE_PRINTF */
31460484Sobrien
315218822Sdim/* Use ATTRIBUTE_FPTR_PRINTF when the format attribute is to be set on
316218822Sdim   a function pointer.  Format attributes were allowed on function
317218822Sdim   pointers as of gcc 3.1.  */
318218822Sdim#ifndef ATTRIBUTE_FPTR_PRINTF
319218822Sdim# if (GCC_VERSION >= 3001)
320218822Sdim#  define ATTRIBUTE_FPTR_PRINTF(m, n) ATTRIBUTE_PRINTF(m, n)
321218822Sdim# else
322218822Sdim#  define ATTRIBUTE_FPTR_PRINTF(m, n)
323218822Sdim# endif /* GNUC >= 3.1 */
324218822Sdim# define ATTRIBUTE_FPTR_PRINTF_1 ATTRIBUTE_FPTR_PRINTF(1, 2)
325218822Sdim# define ATTRIBUTE_FPTR_PRINTF_2 ATTRIBUTE_FPTR_PRINTF(2, 3)
326218822Sdim# define ATTRIBUTE_FPTR_PRINTF_3 ATTRIBUTE_FPTR_PRINTF(3, 4)
327218822Sdim# define ATTRIBUTE_FPTR_PRINTF_4 ATTRIBUTE_FPTR_PRINTF(4, 5)
328218822Sdim# define ATTRIBUTE_FPTR_PRINTF_5 ATTRIBUTE_FPTR_PRINTF(5, 6)
329218822Sdim#endif /* ATTRIBUTE_FPTR_PRINTF */
330218822Sdim
331130561Sobrien/* Use ATTRIBUTE_NULL_PRINTF when the format specifier may be NULL.  A
332130561Sobrien   NULL format specifier was allowed as of gcc 3.3.  */
333130561Sobrien#ifndef ATTRIBUTE_NULL_PRINTF
334130561Sobrien# if (GCC_VERSION >= 3003)
335130561Sobrien#  define ATTRIBUTE_NULL_PRINTF(m, n) __attribute__ ((__format__ (__printf__, m, n)))
336130561Sobrien# else
337130561Sobrien#  define ATTRIBUTE_NULL_PRINTF(m, n)
338130561Sobrien# endif /* GNUC >= 3.3 */
339130561Sobrien# define ATTRIBUTE_NULL_PRINTF_1 ATTRIBUTE_NULL_PRINTF(1, 2)
340130561Sobrien# define ATTRIBUTE_NULL_PRINTF_2 ATTRIBUTE_NULL_PRINTF(2, 3)
341130561Sobrien# define ATTRIBUTE_NULL_PRINTF_3 ATTRIBUTE_NULL_PRINTF(3, 4)
342130561Sobrien# define ATTRIBUTE_NULL_PRINTF_4 ATTRIBUTE_NULL_PRINTF(4, 5)
343130561Sobrien# define ATTRIBUTE_NULL_PRINTF_5 ATTRIBUTE_NULL_PRINTF(5, 6)
344130561Sobrien#endif /* ATTRIBUTE_NULL_PRINTF */
345130561Sobrien
346218822Sdim/* Attribute `sentinel' was valid as of gcc 3.5.  */
347218822Sdim#ifndef ATTRIBUTE_SENTINEL
348218822Sdim# if (GCC_VERSION >= 3005)
349218822Sdim#  define ATTRIBUTE_SENTINEL __attribute__ ((__sentinel__))
350218822Sdim# else
351218822Sdim#  define ATTRIBUTE_SENTINEL
352218822Sdim# endif /* GNUC >= 3.5 */
353218822Sdim#endif /* ATTRIBUTE_SENTINEL */
354218822Sdim
355218822Sdim
356218822Sdim#ifndef ATTRIBUTE_ALIGNED_ALIGNOF
357218822Sdim# if (GCC_VERSION >= 3000)
358218822Sdim#  define ATTRIBUTE_ALIGNED_ALIGNOF(m) __attribute__ ((__aligned__ (__alignof__ (m))))
359218822Sdim# else
360218822Sdim#  define ATTRIBUTE_ALIGNED_ALIGNOF(m)
361218822Sdim# endif /* GNUC >= 3.0 */
362218822Sdim#endif /* ATTRIBUTE_ALIGNED_ALIGNOF */
363218822Sdim
364218822Sdim/* Useful for structures whose layout must much some binary specification
365218822Sdim   regardless of the alignment and padding qualities of the compiler.  */
366218822Sdim#ifndef ATTRIBUTE_PACKED
367218822Sdim# define ATTRIBUTE_PACKED __attribute__ ((packed))
368218822Sdim#endif
369218822Sdim
370218822Sdim/* Attribute `hot' and `cold' was valid as of gcc 4.3.  */
371218822Sdim#ifndef ATTRIBUTE_COLD
372218822Sdim# if (GCC_VERSION >= 4003)
373218822Sdim#  define ATTRIBUTE_COLD __attribute__ ((__cold__))
374218822Sdim# else
375218822Sdim#  define ATTRIBUTE_COLD
376218822Sdim# endif /* GNUC >= 4.3 */
377218822Sdim#endif /* ATTRIBUTE_COLD */
378218822Sdim#ifndef ATTRIBUTE_HOT
379218822Sdim# if (GCC_VERSION >= 4003)
380218822Sdim#  define ATTRIBUTE_HOT __attribute__ ((__hot__))
381218822Sdim# else
382218822Sdim#  define ATTRIBUTE_HOT
383218822Sdim# endif /* GNUC >= 4.3 */
384218822Sdim#endif /* ATTRIBUTE_HOT */
385218822Sdim
38677298Sobrien/* We use __extension__ in some places to suppress -pedantic warnings
38777298Sobrien   about GCC extensions.  This feature didn't work properly before
38877298Sobrien   gcc 2.8.  */
38977298Sobrien#if GCC_VERSION < 2008
39077298Sobrien#define __extension__
39177298Sobrien#endif
39277298Sobrien
39333965Sjdp#endif	/* ansidecl.h	*/
394