12159047fSniklas /* ANSI and traditional C compatability macros
2*c074d1c9Sdrahn    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001
3b55d4692Sfgsch    Free Software Foundation, Inc.
42159047fSniklas    This file is part of the GNU C Library.
52159047fSniklas 
62159047fSniklas This program is free software; you can redistribute it and/or modify
72159047fSniklas it under the terms of the GNU General Public License as published by
82159047fSniklas the Free Software Foundation; either version 2 of the License, or
92159047fSniklas (at your option) any later version.
102159047fSniklas 
112159047fSniklas This program is distributed in the hope that it will be useful,
122159047fSniklas but WITHOUT ANY WARRANTY; without even the implied warranty of
132159047fSniklas MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
142159047fSniklas GNU General Public License for more details.
152159047fSniklas 
162159047fSniklas You should have received a copy of the GNU General Public License
172159047fSniklas along with this program; if not, write to the Free Software
182159047fSniklas Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
192159047fSniklas 
202159047fSniklas /* ANSI and traditional C compatibility macros
212159047fSniklas 
222159047fSniklas    ANSI C is assumed if __STDC__ is #defined.
232159047fSniklas 
242159047fSniklas    Macro		ANSI C definition	Traditional C definition
252159047fSniklas    -----		---- - ----------	----------- - ----------
262159047fSniklas    ANSI_PROTOTYPES	1			not defined
27*c074d1c9Sdrahn    PTR			`void *'		`char *'
28*c074d1c9Sdrahn    PTRCONST		`void *const'		`char *'
29*c074d1c9Sdrahn    LONG_DOUBLE		`long double'		`double'
30*c074d1c9Sdrahn    const		not defined		`'
31*c074d1c9Sdrahn    volatile		not defined		`'
32*c074d1c9Sdrahn    signed		not defined		`'
33*c074d1c9Sdrahn    VA_START(ap, var)	va_start(ap, var)	va_start(ap)
342159047fSniklas 
35*c074d1c9Sdrahn    Note that it is safe to write "void foo();" indicating a function
36*c074d1c9Sdrahn    with no return value, in all K+R compilers we have been able to test.
372159047fSniklas 
38*c074d1c9Sdrahn    For declaring functions with prototypes, we also provide these:
392159047fSniklas 
40*c074d1c9Sdrahn    PARAMS ((prototype))
41*c074d1c9Sdrahn    -- for functions which take a fixed number of arguments.  Use this
42*c074d1c9Sdrahn    when declaring the function.  When defining the function, write a
43*c074d1c9Sdrahn    K+R style argument list.  For example:
442159047fSniklas 
45*c074d1c9Sdrahn 	char *strcpy PARAMS ((char *dest, char *source));
46*c074d1c9Sdrahn 	...
47*c074d1c9Sdrahn 	char *
48*c074d1c9Sdrahn 	strcpy (dest, source)
49*c074d1c9Sdrahn 	     char *dest;
50*c074d1c9Sdrahn 	     char *source;
51*c074d1c9Sdrahn 	{ ... }
522159047fSniklas 
532159047fSniklas 
54*c074d1c9Sdrahn    VPARAMS ((prototype, ...))
55*c074d1c9Sdrahn    -- for functions which take a variable number of arguments.  Use
56*c074d1c9Sdrahn    PARAMS to declare the function, VPARAMS to define it.  For example:
572159047fSniklas 
58*c074d1c9Sdrahn 	int printf PARAMS ((const char *format, ...));
59*c074d1c9Sdrahn 	...
60*c074d1c9Sdrahn 	int
61*c074d1c9Sdrahn 	printf VPARAMS ((const char *format, ...))
62*c074d1c9Sdrahn 	{
63*c074d1c9Sdrahn 	   ...
64*c074d1c9Sdrahn 	}
652159047fSniklas 
66*c074d1c9Sdrahn    For writing functions which take variable numbers of arguments, we
67*c074d1c9Sdrahn    also provide the VA_OPEN, VA_CLOSE, and VA_FIXEDARG macros.  These
68*c074d1c9Sdrahn    hide the differences between K+R <varargs.h> and C89 <stdarg.h> more
69*c074d1c9Sdrahn    thoroughly than the simple VA_START() macro mentioned above.
702159047fSniklas 
71*c074d1c9Sdrahn    VA_OPEN and VA_CLOSE are used *instead of* va_start and va_end.
72*c074d1c9Sdrahn    Immediately after VA_OPEN, put a sequence of VA_FIXEDARG calls
73*c074d1c9Sdrahn    corresponding to the list of fixed arguments.  Then use va_arg
74*c074d1c9Sdrahn    normally to get the variable arguments, or pass your va_list object
75*c074d1c9Sdrahn    around.  You do not declare the va_list yourself; VA_OPEN does it
76*c074d1c9Sdrahn    for you.
772159047fSniklas 
78*c074d1c9Sdrahn    Here is a complete example:
79e93f7393Sniklas 
80*c074d1c9Sdrahn 	int
81*c074d1c9Sdrahn 	printf VPARAMS ((const char *format, ...))
82*c074d1c9Sdrahn 	{
83*c074d1c9Sdrahn 	   int result;
84e93f7393Sniklas 
85*c074d1c9Sdrahn 	   VA_OPEN (ap, format);
86*c074d1c9Sdrahn 	   VA_FIXEDARG (ap, const char *, format);
872159047fSniklas 
88*c074d1c9Sdrahn 	   result = vfprintf (stdout, format, ap);
89*c074d1c9Sdrahn 	   VA_CLOSE (ap);
902159047fSniklas 
91*c074d1c9Sdrahn 	   return result;
92*c074d1c9Sdrahn 	}
932159047fSniklas 
942159047fSniklas 
95*c074d1c9Sdrahn    You can declare variables either before or after the VA_OPEN,
96*c074d1c9Sdrahn    VA_FIXEDARG sequence.  Also, VA_OPEN and VA_CLOSE are the beginning
97*c074d1c9Sdrahn    and end of a block.  They must appear at the same nesting level,
98*c074d1c9Sdrahn    and any variables declared after VA_OPEN go out of scope at
99*c074d1c9Sdrahn    VA_CLOSE.  Unfortunately, with a K+R compiler, that includes the
100*c074d1c9Sdrahn    argument list.  You can have multiple instances of VA_OPEN/VA_CLOSE
101*c074d1c9Sdrahn    pairs in a single function in case you need to traverse the
102*c074d1c9Sdrahn    argument list more than once.
1032159047fSniklas 
104*c074d1c9Sdrahn    For ease of writing code which uses GCC extensions but needs to be
105*c074d1c9Sdrahn    portable to other compilers, we provide the GCC_VERSION macro that
106*c074d1c9Sdrahn    simplifies testing __GNUC__ and __GNUC_MINOR__ together, and various
107*c074d1c9Sdrahn    wrappers around __attribute__.  Also, __extension__ will be #defined
108*c074d1c9Sdrahn    to nothing if it doesn't work.  See below.
1092159047fSniklas 
110*c074d1c9Sdrahn    This header also defines a lot of obsolete macros:
111*c074d1c9Sdrahn    CONST, VOLATILE, SIGNED, PROTO, EXFUN, DEFUN, DEFUN_VOID,
112*c074d1c9Sdrahn    AND, DOTS, NOARGS.  Don't use them.  */
1132159047fSniklas 
1142159047fSniklas #ifndef	_ANSIDECL_H
1152159047fSniklas #define _ANSIDECL_H	1
1162159047fSniklas 
1172159047fSniklas /* Every source file includes this file,
1182159047fSniklas    so they will all get the switch for lint.  */
1192159047fSniklas /* LINTLIBRARY */
1202159047fSniklas 
121b305b0f1Sespie /* Using MACRO(x,y) in cpp #if conditionals does not work with some
122b305b0f1Sespie    older preprocessors.  Thus we can't define something like this:
123b305b0f1Sespie 
124b305b0f1Sespie #define HAVE_GCC_VERSION(MAJOR, MINOR) \
125b305b0f1Sespie   (__GNUC__ > (MAJOR) || (__GNUC__ == (MAJOR) && __GNUC_MINOR__ >= (MINOR)))
126b305b0f1Sespie 
127b305b0f1Sespie and then test "#if HAVE_GCC_VERSION(2,7)".
128b305b0f1Sespie 
129b305b0f1Sespie So instead we use the macro below and test it against specific values.  */
130b305b0f1Sespie 
131b305b0f1Sespie /* This macro simplifies testing whether we are using gcc, and if it
132b305b0f1Sespie    is of a particular minimum version. (Both major & minor numbers are
133b305b0f1Sespie    significant.)  This macro will evaluate to 0 if we are not using
134b305b0f1Sespie    gcc at all.  */
135b305b0f1Sespie #ifndef GCC_VERSION
136b305b0f1Sespie #define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
137b305b0f1Sespie #endif /* GCC_VERSION */
138b305b0f1Sespie 
139*c074d1c9Sdrahn #if defined (__STDC__) || defined (_AIX) || (defined (__mips) && defined (_SYSTYPE_SVR4)) || defined(_WIN32) || (defined(__alpha) && defined(__cplusplus))
140*c074d1c9Sdrahn /* All known AIX compilers implement these things (but don't always
141*c074d1c9Sdrahn    define __STDC__).  The RISC/OS MIPS compiler defines these things
142*c074d1c9Sdrahn    in SVR4 mode, but does not define __STDC__.  */
143*c074d1c9Sdrahn /* eraxxon@alumni.rice.edu: The Compaq C++ compiler, unlike many other
144*c074d1c9Sdrahn    C++ compilers, does not define __STDC__, though it acts as if this
145*c074d1c9Sdrahn    was so. (Verified versions: 5.7, 6.2, 6.3, 6.5) */
146*c074d1c9Sdrahn 
147*c074d1c9Sdrahn #define ANSI_PROTOTYPES	1
148*c074d1c9Sdrahn #define PTR		void *
149*c074d1c9Sdrahn #define PTRCONST	void *const
150*c074d1c9Sdrahn #define LONG_DOUBLE	long double
151*c074d1c9Sdrahn 
152*c074d1c9Sdrahn #define PARAMS(ARGS)		ARGS
153*c074d1c9Sdrahn #define VPARAMS(ARGS)		ARGS
154*c074d1c9Sdrahn #define VA_START(VA_LIST, VAR)	va_start(VA_LIST, VAR)
155*c074d1c9Sdrahn 
156*c074d1c9Sdrahn /* variadic function helper macros */
157*c074d1c9Sdrahn /* "struct Qdmy" swallows the semicolon after VA_OPEN/VA_FIXEDARG's
158*c074d1c9Sdrahn    use without inhibiting further decls and without declaring an
159*c074d1c9Sdrahn    actual variable.  */
160*c074d1c9Sdrahn #define VA_OPEN(AP, VAR)	{ va_list AP; va_start(AP, VAR); { struct Qdmy
161*c074d1c9Sdrahn #define VA_CLOSE(AP)		} va_end(AP); }
162*c074d1c9Sdrahn #define VA_FIXEDARG(AP, T, N)	struct Qdmy
163*c074d1c9Sdrahn 
164*c074d1c9Sdrahn #undef const
165*c074d1c9Sdrahn #undef volatile
166*c074d1c9Sdrahn #undef signed
167*c074d1c9Sdrahn 
168*c074d1c9Sdrahn /* inline requires special treatment; it's in C99, and GCC >=2.7 supports
169*c074d1c9Sdrahn    it too, but it's not in C89.  */
170*c074d1c9Sdrahn #undef inline
171*c074d1c9Sdrahn #if __STDC_VERSION__ > 199901L
172*c074d1c9Sdrahn /* it's a keyword */
173*c074d1c9Sdrahn #else
174*c074d1c9Sdrahn # if GCC_VERSION >= 2007
175*c074d1c9Sdrahn #  define inline __inline__   /* __inline__ prevents -pedantic warnings */
176*c074d1c9Sdrahn # else
177*c074d1c9Sdrahn #  define inline  /* nothing */
178*c074d1c9Sdrahn # endif
179*c074d1c9Sdrahn #endif
180*c074d1c9Sdrahn 
181*c074d1c9Sdrahn /* These are obsolete.  Do not use.  */
182*c074d1c9Sdrahn #ifndef IN_GCC
183*c074d1c9Sdrahn #define CONST		const
184*c074d1c9Sdrahn #define VOLATILE	volatile
185*c074d1c9Sdrahn #define SIGNED		signed
186*c074d1c9Sdrahn 
187*c074d1c9Sdrahn #define PROTO(type, name, arglist)	type name arglist
188*c074d1c9Sdrahn #define EXFUN(name, proto)		name proto
189*c074d1c9Sdrahn #define DEFUN(name, arglist, args)	name(args)
190*c074d1c9Sdrahn #define DEFUN_VOID(name)		name(void)
191*c074d1c9Sdrahn #define AND		,
192*c074d1c9Sdrahn #define DOTS		, ...
193*c074d1c9Sdrahn #define NOARGS		void
194*c074d1c9Sdrahn #endif /* ! IN_GCC */
195*c074d1c9Sdrahn 
196*c074d1c9Sdrahn #else	/* Not ANSI C.  */
197*c074d1c9Sdrahn 
198*c074d1c9Sdrahn #undef  ANSI_PROTOTYPES
199*c074d1c9Sdrahn #define PTR		char *
200*c074d1c9Sdrahn #define PTRCONST	PTR
201*c074d1c9Sdrahn #define LONG_DOUBLE	double
202*c074d1c9Sdrahn 
203*c074d1c9Sdrahn #define PARAMS(args)		()
204*c074d1c9Sdrahn #define VPARAMS(args)		(va_alist) va_dcl
205*c074d1c9Sdrahn #define VA_START(va_list, var)	va_start(va_list)
206*c074d1c9Sdrahn 
207*c074d1c9Sdrahn #define VA_OPEN(AP, VAR)		{ va_list AP; va_start(AP); { struct Qdmy
208*c074d1c9Sdrahn #define VA_CLOSE(AP)			} va_end(AP); }
209*c074d1c9Sdrahn #define VA_FIXEDARG(AP, TYPE, NAME)	TYPE NAME = va_arg(AP, TYPE)
210*c074d1c9Sdrahn 
211*c074d1c9Sdrahn /* some systems define these in header files for non-ansi mode */
212*c074d1c9Sdrahn #undef const
213*c074d1c9Sdrahn #undef volatile
214*c074d1c9Sdrahn #undef signed
215*c074d1c9Sdrahn #undef inline
216*c074d1c9Sdrahn #define const
217*c074d1c9Sdrahn #define volatile
218*c074d1c9Sdrahn #define signed
219*c074d1c9Sdrahn #define inline
220*c074d1c9Sdrahn 
221*c074d1c9Sdrahn #ifndef IN_GCC
222*c074d1c9Sdrahn #define CONST
223*c074d1c9Sdrahn #define VOLATILE
224*c074d1c9Sdrahn #define SIGNED
225*c074d1c9Sdrahn 
226*c074d1c9Sdrahn #define PROTO(type, name, arglist)	type name ()
227*c074d1c9Sdrahn #define EXFUN(name, proto)		name()
228*c074d1c9Sdrahn #define DEFUN(name, arglist, args)	name arglist args;
229*c074d1c9Sdrahn #define DEFUN_VOID(name)		name()
230*c074d1c9Sdrahn #define AND		;
231*c074d1c9Sdrahn #define DOTS
232*c074d1c9Sdrahn #define NOARGS
233*c074d1c9Sdrahn #endif /* ! IN_GCC */
234*c074d1c9Sdrahn 
235*c074d1c9Sdrahn #endif	/* ANSI C.  */
236*c074d1c9Sdrahn 
237b305b0f1Sespie /* Define macros for some gcc attributes.  This permits us to use the
238b305b0f1Sespie    macros freely, and know that they will come into play for the
239b305b0f1Sespie    version of gcc in which they are supported.  */
240b305b0f1Sespie 
241b305b0f1Sespie #if (GCC_VERSION < 2007)
242b305b0f1Sespie # define __attribute__(x)
243b305b0f1Sespie #endif
244b305b0f1Sespie 
245b305b0f1Sespie /* Attribute __malloc__ on functions was valid as of gcc 2.96. */
246b305b0f1Sespie #ifndef ATTRIBUTE_MALLOC
247b305b0f1Sespie # if (GCC_VERSION >= 2096)
248b305b0f1Sespie #  define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
249b305b0f1Sespie # else
250b305b0f1Sespie #  define ATTRIBUTE_MALLOC
251b305b0f1Sespie # endif /* GNUC >= 2.96 */
252b305b0f1Sespie #endif /* ATTRIBUTE_MALLOC */
253b305b0f1Sespie 
254b305b0f1Sespie /* Attributes on labels were valid as of gcc 2.93. */
255b305b0f1Sespie #ifndef ATTRIBUTE_UNUSED_LABEL
256b305b0f1Sespie # if (GCC_VERSION >= 2093)
257b305b0f1Sespie #  define ATTRIBUTE_UNUSED_LABEL ATTRIBUTE_UNUSED
258b305b0f1Sespie # else
259b305b0f1Sespie #  define ATTRIBUTE_UNUSED_LABEL
260b305b0f1Sespie # endif /* GNUC >= 2.93 */
261b305b0f1Sespie #endif /* ATTRIBUTE_UNUSED_LABEL */
262b305b0f1Sespie 
263b305b0f1Sespie #ifndef ATTRIBUTE_UNUSED
264b305b0f1Sespie #define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
265b305b0f1Sespie #endif /* ATTRIBUTE_UNUSED */
266b305b0f1Sespie 
267b305b0f1Sespie #ifndef ATTRIBUTE_NORETURN
268b305b0f1Sespie #define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
269b305b0f1Sespie #endif /* ATTRIBUTE_NORETURN */
270b305b0f1Sespie 
271*c074d1c9Sdrahn /* Attribute `nonnull' was valid as of gcc 3.3.  */
272*c074d1c9Sdrahn #ifndef ATTRIBUTE_NONNULL
273*c074d1c9Sdrahn # if (GCC_VERSION >= 3003)
274*c074d1c9Sdrahn #  define ATTRIBUTE_NONNULL(m) __attribute__ ((__nonnull__ (m)))
275*c074d1c9Sdrahn # else
276*c074d1c9Sdrahn #  define ATTRIBUTE_NONNULL(m)
277*c074d1c9Sdrahn # endif /* GNUC >= 3.3 */
278*c074d1c9Sdrahn #endif /* ATTRIBUTE_NONNULL */
279*c074d1c9Sdrahn 
280*c074d1c9Sdrahn /* Use ATTRIBUTE_PRINTF when the format specifier must not be NULL.
281*c074d1c9Sdrahn    This was the case for the `printf' format attribute by itself
282*c074d1c9Sdrahn    before GCC 3.3, but as of 3.3 we need to add the `nonnull'
283*c074d1c9Sdrahn    attribute to retain this behavior.  */
284b305b0f1Sespie #ifndef ATTRIBUTE_PRINTF
285*c074d1c9Sdrahn #define ATTRIBUTE_PRINTF(m, n) __attribute__ ((__format__ (__printf__, m, n))) ATTRIBUTE_NONNULL(m)
286b305b0f1Sespie #define ATTRIBUTE_PRINTF_1 ATTRIBUTE_PRINTF(1, 2)
287b305b0f1Sespie #define ATTRIBUTE_PRINTF_2 ATTRIBUTE_PRINTF(2, 3)
288b305b0f1Sespie #define ATTRIBUTE_PRINTF_3 ATTRIBUTE_PRINTF(3, 4)
289b305b0f1Sespie #define ATTRIBUTE_PRINTF_4 ATTRIBUTE_PRINTF(4, 5)
290b305b0f1Sespie #define ATTRIBUTE_PRINTF_5 ATTRIBUTE_PRINTF(5, 6)
291b305b0f1Sespie #endif /* ATTRIBUTE_PRINTF */
292b305b0f1Sespie 
293*c074d1c9Sdrahn /* Use ATTRIBUTE_NULL_PRINTF when the format specifier may be NULL.  A
294*c074d1c9Sdrahn    NULL format specifier was allowed as of gcc 3.3.  */
295*c074d1c9Sdrahn #ifndef ATTRIBUTE_NULL_PRINTF
296*c074d1c9Sdrahn # if (GCC_VERSION >= 3003)
297*c074d1c9Sdrahn #  define ATTRIBUTE_NULL_PRINTF(m, n) __attribute__ ((__format__ (__printf__, m, n)))
298*c074d1c9Sdrahn # else
299*c074d1c9Sdrahn #  define ATTRIBUTE_NULL_PRINTF(m, n)
300*c074d1c9Sdrahn # endif /* GNUC >= 3.3 */
301*c074d1c9Sdrahn # define ATTRIBUTE_NULL_PRINTF_1 ATTRIBUTE_NULL_PRINTF(1, 2)
302*c074d1c9Sdrahn # define ATTRIBUTE_NULL_PRINTF_2 ATTRIBUTE_NULL_PRINTF(2, 3)
303*c074d1c9Sdrahn # define ATTRIBUTE_NULL_PRINTF_3 ATTRIBUTE_NULL_PRINTF(3, 4)
304*c074d1c9Sdrahn # define ATTRIBUTE_NULL_PRINTF_4 ATTRIBUTE_NULL_PRINTF(4, 5)
305*c074d1c9Sdrahn # define ATTRIBUTE_NULL_PRINTF_5 ATTRIBUTE_NULL_PRINTF(5, 6)
306*c074d1c9Sdrahn #endif /* ATTRIBUTE_NULL_PRINTF */
307*c074d1c9Sdrahn 
308b55d4692Sfgsch /* We use __extension__ in some places to suppress -pedantic warnings
309b55d4692Sfgsch    about GCC extensions.  This feature didn't work properly before
310b55d4692Sfgsch    gcc 2.8.  */
311b55d4692Sfgsch #if GCC_VERSION < 2008
312b55d4692Sfgsch #define __extension__
313b55d4692Sfgsch #endif
314b55d4692Sfgsch 
3152159047fSniklas #endif	/* ansidecl.h	*/
316