]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/extend.texi
Makefile.in (bootstrap): avoid recursion if subdir missing
[thirdparty/gcc.git] / gcc / extend.texi
CommitLineData
069cfbff 1@c Copyright (C) 1988,89,92,93,94,96,98,99,2000 Free Software Foundation, Inc.
c1f7febf
RK
2@c This is part of the GCC manual.
3@c For copying conditions, see the file gcc.texi.
4
5@node C Extensions
6@chapter Extensions to the C Language Family
7@cindex extensions, C language
8@cindex C language extensions
9
10GNU C provides several language features not found in ANSI standard C.
11(The @samp{-pedantic} option directs GNU CC to print a warning message if
12any of these features is used.) To test for the availability of these
13features in conditional compilation, check for a predefined macro
14@code{__GNUC__}, which is always defined under GNU CC.
15
16These extensions are available in C and Objective C. Most of them are
17also available in C++. @xref{C++ Extensions,,Extensions to the
18C++ Language}, for extensions that apply @emph{only} to C++.
19
20@c The only difference between the two versions of this menu is that the
21@c version for clear INTERNALS has an extra node, "Constraints" (which
22@c appears in a separate chapter in the other version of the manual).
23@ifset INTERNALS
24@menu
25* Statement Exprs:: Putting statements and declarations inside expressions.
26* Local Labels:: Labels local to a statement-expression.
27* Labels as Values:: Getting pointers to labels, and computed gotos.
28* Nested Functions:: As in Algol and Pascal, lexical scoping of functions.
29* Constructing Calls:: Dispatching a call to another function.
30* Naming Types:: Giving a name to the type of some expression.
31* Typeof:: @code{typeof}: referring to the type of an expression.
32* Lvalues:: Using @samp{?:}, @samp{,} and casts in lvalues.
33* Conditionals:: Omitting the middle operand of a @samp{?:} expression.
34* Long Long:: Double-word integers---@code{long long int}.
35* Complex:: Data types for complex numbers.
6f4d7222 36* Hex Floats:: Hexadecimal floating-point constants.
c1f7febf
RK
37* Zero Length:: Zero-length arrays.
38* Variable Length:: Arrays whose length is computed at run time.
39* Macro Varargs:: Macros with variable number of arguments.
40* Subscripting:: Any array can be subscripted, even if not an lvalue.
41* Pointer Arith:: Arithmetic on @code{void}-pointers and function pointers.
42* Initializers:: Non-constant initializers.
43* Constructors:: Constructor expressions give structures, unions
44 or arrays as values.
45* Labeled Elements:: Labeling elements of initializers.
46* Cast to Union:: Casting to union type from any member of the union.
47* Case Ranges:: `case 1 ... 9' and such.
48* Function Attributes:: Declaring that functions have no side effects,
49 or that they can never return.
50* Function Prototypes:: Prototype declarations and old-style definitions.
51* C++ Comments:: C++ comments are recognized.
52* Dollar Signs:: Dollar sign is allowed in identifiers.
53* Character Escapes:: @samp{\e} stands for the character @key{ESC}.
54* Variable Attributes:: Specifying attributes of variables.
55* Type Attributes:: Specifying attributes of types.
56* Alignment:: Inquiring about the alignment of a type or variable.
57* Inline:: Defining inline functions (as fast as macros).
58* Extended Asm:: Assembler instructions with C expressions as operands.
59 (With them you can define ``built-in'' functions.)
60* Asm Labels:: Specifying the assembler name to use for a C symbol.
61* Explicit Reg Vars:: Defining variables residing in specified registers.
62* Alternate Keywords:: @code{__const__}, @code{__asm__}, etc., for header files.
63* Incomplete Enums:: @code{enum foo;}, with details to follow.
64* Function Names:: Printable strings which are the name of the current
65 function.
66* Return Address:: Getting the return or frame address of a function.
185ebd6c 67* Other Builtins:: Other built-in functions.
2de45c06 68* Deprecated Features:: Things might disappear from g++.
7b300d13 69* Backwards Compatibility:: Compatibilities with earlier definitions of C++.
c1f7febf
RK
70@end menu
71@end ifset
72@ifclear INTERNALS
73@menu
74* Statement Exprs:: Putting statements and declarations inside expressions.
75* Local Labels:: Labels local to a statement-expression.
76* Labels as Values:: Getting pointers to labels, and computed gotos.
77* Nested Functions:: As in Algol and Pascal, lexical scoping of functions.
78* Constructing Calls:: Dispatching a call to another function.
79* Naming Types:: Giving a name to the type of some expression.
80* Typeof:: @code{typeof}: referring to the type of an expression.
81* Lvalues:: Using @samp{?:}, @samp{,} and casts in lvalues.
82* Conditionals:: Omitting the middle operand of a @samp{?:} expression.
83* Long Long:: Double-word integers---@code{long long int}.
84* Complex:: Data types for complex numbers.
6f4d7222 85* Hex Floats:: Hexadecimal floating-point constants.
c1f7febf
RK
86* Zero Length:: Zero-length arrays.
87* Variable Length:: Arrays whose length is computed at run time.
88* Macro Varargs:: Macros with variable number of arguments.
89* Subscripting:: Any array can be subscripted, even if not an lvalue.
90* Pointer Arith:: Arithmetic on @code{void}-pointers and function pointers.
91* Initializers:: Non-constant initializers.
92* Constructors:: Constructor expressions give structures, unions
93 or arrays as values.
94* Labeled Elements:: Labeling elements of initializers.
95* Cast to Union:: Casting to union type from any member of the union.
96* Case Ranges:: `case 1 ... 9' and such.
97* Function Attributes:: Declaring that functions have no side effects,
98 or that they can never return.
99* Function Prototypes:: Prototype declarations and old-style definitions.
100* C++ Comments:: C++ comments are recognized.
101* Dollar Signs:: Dollar sign is allowed in identifiers.
102* Character Escapes:: @samp{\e} stands for the character @key{ESC}.
103* Variable Attributes:: Specifying attributes of variables.
104* Type Attributes:: Specifying attributes of types.
105* Alignment:: Inquiring about the alignment of a type or variable.
106* Inline:: Defining inline functions (as fast as macros).
107* Extended Asm:: Assembler instructions with C expressions as operands.
108 (With them you can define ``built-in'' functions.)
109* Constraints:: Constraints for asm operands
110* Asm Labels:: Specifying the assembler name to use for a C symbol.
111* Explicit Reg Vars:: Defining variables residing in specified registers.
112* Alternate Keywords:: @code{__const__}, @code{__asm__}, etc., for header files.
113* Incomplete Enums:: @code{enum foo;}, with details to follow.
114* Function Names:: Printable strings which are the name of the current
115 function.
116* Return Address:: Getting the return or frame address of a function.
2de45c06 117* Deprecated Features:: Things might disappear from g++.
7b300d13 118* Backwards Compatibility:: Compatibilities with earlier definitions of C++.
c5c76735 119* Other Builtins:: Other built-in functions.
c1f7febf
RK
120@end menu
121@end ifclear
122
123@node Statement Exprs
124@section Statements and Declarations in Expressions
125@cindex statements inside expressions
126@cindex declarations inside expressions
127@cindex expressions containing statements
128@cindex macros, statements in expressions
129
130@c the above section title wrapped and causes an underfull hbox.. i
131@c changed it from "within" to "in". --mew 4feb93
132
133A compound statement enclosed in parentheses may appear as an expression
134in GNU C. This allows you to use loops, switches, and local variables
135within an expression.
136
137Recall that a compound statement is a sequence of statements surrounded
138by braces; in this construct, parentheses go around the braces. For
139example:
140
141@example
142(@{ int y = foo (); int z;
143 if (y > 0) z = y;
144 else z = - y;
145 z; @})
146@end example
147
148@noindent
149is a valid (though slightly more complex than necessary) expression
150for the absolute value of @code{foo ()}.
151
152The last thing in the compound statement should be an expression
153followed by a semicolon; the value of this subexpression serves as the
154value of the entire construct. (If you use some other kind of statement
155last within the braces, the construct has type @code{void}, and thus
156effectively no value.)
157
158This feature is especially useful in making macro definitions ``safe'' (so
159that they evaluate each operand exactly once). For example, the
160``maximum'' function is commonly defined as a macro in standard C as
161follows:
162
163@example
164#define max(a,b) ((a) > (b) ? (a) : (b))
165@end example
166
167@noindent
168@cindex side effects, macro argument
169But this definition computes either @var{a} or @var{b} twice, with bad
170results if the operand has side effects. In GNU C, if you know the
171type of the operands (here let's assume @code{int}), you can define
172the macro safely as follows:
173
174@example
175#define maxint(a,b) \
176 (@{int _a = (a), _b = (b); _a > _b ? _a : _b; @})
177@end example
178
179Embedded statements are not allowed in constant expressions, such as
180the value of an enumeration constant, the width of a bit field, or
181the initial value of a static variable.
182
183If you don't know the type of the operand, you can still do this, but you
184must use @code{typeof} (@pxref{Typeof}) or type naming (@pxref{Naming
185Types}).
186
b98e139b
MM
187Statement expressions are not supported fully in G++, and their fate
188there is unclear. (It is possible that they will become fully supported
189at some point, or that they will be deprecated, or that the bugs that
190are present will continue to exist indefinitely.) Presently, statement
191expressions do not work well as default arguments.
192
193In addition, there are semantic issues with statement-expressions in
194C++. If you try to use statement-expressions instead of inline
195functions in C++, you may be surprised at the way object destruction is
196handled. For example:
197
198@example
199#define foo(a) (@{int b = (a); b + 3; @})
200@end example
201
202@noindent
203does not work the same way as:
204
205@example
54e1d3a6 206inline int foo(int a) @{ int b = a; return b + 3; @}
b98e139b
MM
207@end example
208
209@noindent
210In particular, if the expression passed into @code{foo} involves the
211creation of temporaries, the destructors for those temporaries will be
212run earlier in the case of the macro than in the case of the function.
213
214These considerations mean that it is probably a bad idea to use
215statement-expressions of this form in header files that are designed to
54e1d3a6
MM
216work with C++. (Note that some versions of the GNU C Library contained
217header files using statement-expression that lead to precisely this
218bug.)
b98e139b 219
c1f7febf
RK
220@node Local Labels
221@section Locally Declared Labels
222@cindex local labels
223@cindex macros, local labels
224
225Each statement expression is a scope in which @dfn{local labels} can be
226declared. A local label is simply an identifier; you can jump to it
227with an ordinary @code{goto} statement, but only from within the
228statement expression it belongs to.
229
230A local label declaration looks like this:
231
232@example
233__label__ @var{label};
234@end example
235
236@noindent
237or
238
239@example
240__label__ @var{label1}, @var{label2}, @dots{};
241@end example
242
243Local label declarations must come at the beginning of the statement
244expression, right after the @samp{(@{}, before any ordinary
245declarations.
246
247The label declaration defines the label @emph{name}, but does not define
248the label itself. You must do this in the usual way, with
249@code{@var{label}:}, within the statements of the statement expression.
250
251The local label feature is useful because statement expressions are
252often used in macros. If the macro contains nested loops, a @code{goto}
253can be useful for breaking out of them. However, an ordinary label
254whose scope is the whole function cannot be used: if the macro can be
255expanded several times in one function, the label will be multiply
256defined in that function. A local label avoids this problem. For
257example:
258
259@example
260#define SEARCH(array, target) \
261(@{ \
262 __label__ found; \
263 typeof (target) _SEARCH_target = (target); \
264 typeof (*(array)) *_SEARCH_array = (array); \
265 int i, j; \
266 int value; \
267 for (i = 0; i < max; i++) \
268 for (j = 0; j < max; j++) \
269 if (_SEARCH_array[i][j] == _SEARCH_target) \
270 @{ value = i; goto found; @} \
271 value = -1; \
272 found: \
273 value; \
274@})
275@end example
276
277@node Labels as Values
278@section Labels as Values
279@cindex labels as values
280@cindex computed gotos
281@cindex goto with computed label
282@cindex address of a label
283
284You can get the address of a label defined in the current function
285(or a containing function) with the unary operator @samp{&&}. The
286value has type @code{void *}. This value is a constant and can be used
287wherever a constant of that type is valid. For example:
288
289@example
290void *ptr;
291@dots{}
292ptr = &&foo;
293@end example
294
295To use these values, you need to be able to jump to one. This is done
296with the computed goto statement@footnote{The analogous feature in
297Fortran is called an assigned goto, but that name seems inappropriate in
298C, where one can do more than simply store label addresses in label
299variables.}, @code{goto *@var{exp};}. For example,
300
301@example
302goto *ptr;
303@end example
304
305@noindent
306Any expression of type @code{void *} is allowed.
307
308One way of using these constants is in initializing a static array that
309will serve as a jump table:
310
311@example
312static void *array[] = @{ &&foo, &&bar, &&hack @};
313@end example
314
315Then you can select a label with indexing, like this:
316
317@example
318goto *array[i];
319@end example
320
321@noindent
322Note that this does not check whether the subscript is in bounds---array
323indexing in C never does that.
324
325Such an array of label values serves a purpose much like that of the
326@code{switch} statement. The @code{switch} statement is cleaner, so
327use that rather than an array unless the problem does not fit a
328@code{switch} statement very well.
329
330Another use of label values is in an interpreter for threaded code.
331The labels within the interpreter function can be stored in the
332threaded code for super-fast dispatching.
333
47620e09
RH
334You may not use this mechanism to jump to code in a different function.
335If you do that, totally unpredictable things will happen. The best way to
c1f7febf
RK
336avoid this is to store the label address only in automatic variables and
337never pass it as an argument.
338
47620e09
RH
339An alternate way to write the above example is
340
341@example
342static const int array[] = @{ &&foo - &&foo, &&bar - &&foo, &&hack - &&foo @};
343goto *(&&foo + array[i]);
344@end example
345
346@noindent
347This is more friendly to code living in shared libraries, as it reduces
348the number of dynamic relocations that are needed, and by consequence,
349allows the data to be read-only.
350
c1f7febf
RK
351@node Nested Functions
352@section Nested Functions
353@cindex nested functions
354@cindex downward funargs
355@cindex thunks
356
357A @dfn{nested function} is a function defined inside another function.
358(Nested functions are not supported for GNU C++.) The nested function's
359name is local to the block where it is defined. For example, here we
360define a nested function named @code{square}, and call it twice:
361
362@example
363@group
364foo (double a, double b)
365@{
366 double square (double z) @{ return z * z; @}
367
368 return square (a) + square (b);
369@}
370@end group
371@end example
372
373The nested function can access all the variables of the containing
374function that are visible at the point of its definition. This is
375called @dfn{lexical scoping}. For example, here we show a nested
376function which uses an inherited variable named @code{offset}:
377
378@example
379bar (int *array, int offset, int size)
380@{
381 int access (int *array, int index)
382 @{ return array[index + offset]; @}
383 int i;
384 @dots{}
385 for (i = 0; i < size; i++)
386 @dots{} access (array, i) @dots{}
387@}
388@end example
389
390Nested function definitions are permitted within functions in the places
391where variable definitions are allowed; that is, in any block, before
392the first statement in the block.
393
394It is possible to call the nested function from outside the scope of its
395name by storing its address or passing the address to another function:
396
397@example
398hack (int *array, int size)
399@{
400 void store (int index, int value)
401 @{ array[index] = value; @}
402
403 intermediate (store, size);
404@}
405@end example
406
407Here, the function @code{intermediate} receives the address of
408@code{store} as an argument. If @code{intermediate} calls @code{store},
409the arguments given to @code{store} are used to store into @code{array}.
410But this technique works only so long as the containing function
411(@code{hack}, in this example) does not exit.
412
413If you try to call the nested function through its address after the
414containing function has exited, all hell will break loose. If you try
415to call it after a containing scope level has exited, and if it refers
416to some of the variables that are no longer in scope, you may be lucky,
417but it's not wise to take the risk. If, however, the nested function
418does not refer to anything that has gone out of scope, you should be
419safe.
420
421GNU CC implements taking the address of a nested function using a
674032e2 422technique called @dfn{trampolines}. A paper describing them is
59c5d27a 423available as @uref{http://master.debian.org/~karlheg/Usenix88-lexic.pdf}.
c1f7febf
RK
424
425A nested function can jump to a label inherited from a containing
426function, provided the label was explicitly declared in the containing
427function (@pxref{Local Labels}). Such a jump returns instantly to the
428containing function, exiting the nested function which did the
429@code{goto} and any intermediate functions as well. Here is an example:
430
431@example
432@group
433bar (int *array, int offset, int size)
434@{
435 __label__ failure;
436 int access (int *array, int index)
437 @{
438 if (index > size)
439 goto failure;
440 return array[index + offset];
441 @}
442 int i;
443 @dots{}
444 for (i = 0; i < size; i++)
445 @dots{} access (array, i) @dots{}
446 @dots{}
447 return 0;
448
449 /* @r{Control comes here from @code{access}
450 if it detects an error.} */
451 failure:
452 return -1;
453@}
454@end group
455@end example
456
457A nested function always has internal linkage. Declaring one with
458@code{extern} is erroneous. If you need to declare the nested function
459before its definition, use @code{auto} (which is otherwise meaningless
460for function declarations).
461
462@example
463bar (int *array, int offset, int size)
464@{
465 __label__ failure;
466 auto int access (int *, int);
467 @dots{}
468 int access (int *array, int index)
469 @{
470 if (index > size)
471 goto failure;
472 return array[index + offset];
473 @}
474 @dots{}
475@}
476@end example
477
478@node Constructing Calls
479@section Constructing Function Calls
480@cindex constructing calls
481@cindex forwarding calls
482
483Using the built-in functions described below, you can record
484the arguments a function received, and call another function
485with the same arguments, without knowing the number or types
486of the arguments.
487
488You can also record the return value of that function call,
489and later return that value, without knowing what data type
490the function tried to return (as long as your caller expects
491that data type).
492
493@table @code
494@findex __builtin_apply_args
495@item __builtin_apply_args ()
496This built-in function returns a pointer of type @code{void *} to data
497describing how to perform a call with the same arguments as were passed
498to the current function.
499
500The function saves the arg pointer register, structure value address,
501and all registers that might be used to pass arguments to a function
502into a block of memory allocated on the stack. Then it returns the
503address of that block.
504
505@findex __builtin_apply
506@item __builtin_apply (@var{function}, @var{arguments}, @var{size})
507This built-in function invokes @var{function} (type @code{void (*)()})
508with a copy of the parameters described by @var{arguments} (type
509@code{void *}) and @var{size} (type @code{int}).
510
511The value of @var{arguments} should be the value returned by
512@code{__builtin_apply_args}. The argument @var{size} specifies the size
513of the stack argument data, in bytes.
514
515This function returns a pointer of type @code{void *} to data describing
516how to return whatever value was returned by @var{function}. The data
517is saved in a block of memory allocated on the stack.
518
519It is not always simple to compute the proper value for @var{size}. The
520value is used by @code{__builtin_apply} to compute the amount of data
521that should be pushed on the stack and copied from the incoming argument
522area.
523
524@findex __builtin_return
525@item __builtin_return (@var{result})
526This built-in function returns the value described by @var{result} from
527the containing function. You should specify, for @var{result}, a value
528returned by @code{__builtin_apply}.
529@end table
530
531@node Naming Types
532@section Naming an Expression's Type
533@cindex naming types
534
535You can give a name to the type of an expression using a @code{typedef}
536declaration with an initializer. Here is how to define @var{name} as a
537type name for the type of @var{exp}:
538
539@example
540typedef @var{name} = @var{exp};
541@end example
542
543This is useful in conjunction with the statements-within-expressions
544feature. Here is how the two together can be used to define a safe
545``maximum'' macro that operates on any arithmetic type:
546
547@example
548#define max(a,b) \
549 (@{typedef _ta = (a), _tb = (b); \
550 _ta _a = (a); _tb _b = (b); \
551 _a > _b ? _a : _b; @})
552@end example
553
554@cindex underscores in variables in macros
555@cindex @samp{_} in variables in macros
556@cindex local variables in macros
557@cindex variables, local, in macros
558@cindex macros, local variables in
559
560The reason for using names that start with underscores for the local
561variables is to avoid conflicts with variable names that occur within the
562expressions that are substituted for @code{a} and @code{b}. Eventually we
563hope to design a new form of declaration syntax that allows you to declare
564variables whose scopes start only after their initializers; this will be a
565more reliable way to prevent such conflicts.
566
567@node Typeof
568@section Referring to a Type with @code{typeof}
569@findex typeof
570@findex sizeof
571@cindex macros, types of arguments
572
573Another way to refer to the type of an expression is with @code{typeof}.
574The syntax of using of this keyword looks like @code{sizeof}, but the
575construct acts semantically like a type name defined with @code{typedef}.
576
577There are two ways of writing the argument to @code{typeof}: with an
578expression or with a type. Here is an example with an expression:
579
580@example
581typeof (x[0](1))
582@end example
583
584@noindent
585This assumes that @code{x} is an array of functions; the type described
586is that of the values of the functions.
587
588Here is an example with a typename as the argument:
589
590@example
591typeof (int *)
592@end example
593
594@noindent
595Here the type described is that of pointers to @code{int}.
596
597If you are writing a header file that must work when included in ANSI C
598programs, write @code{__typeof__} instead of @code{typeof}.
599@xref{Alternate Keywords}.
600
601A @code{typeof}-construct can be used anywhere a typedef name could be
602used. For example, you can use it in a declaration, in a cast, or inside
603of @code{sizeof} or @code{typeof}.
604
605@itemize @bullet
606@item
607This declares @code{y} with the type of what @code{x} points to.
608
609@example
610typeof (*x) y;
611@end example
612
613@item
614This declares @code{y} as an array of such values.
615
616@example
617typeof (*x) y[4];
618@end example
619
620@item
621This declares @code{y} as an array of pointers to characters:
622
623@example
624typeof (typeof (char *)[4]) y;
625@end example
626
627@noindent
628It is equivalent to the following traditional C declaration:
629
630@example
631char *y[4];
632@end example
633
634To see the meaning of the declaration using @code{typeof}, and why it
635might be a useful way to write, let's rewrite it with these macros:
636
637@example
638#define pointer(T) typeof(T *)
639#define array(T, N) typeof(T [N])
640@end example
641
642@noindent
643Now the declaration can be rewritten this way:
644
645@example
646array (pointer (char), 4) y;
647@end example
648
649@noindent
650Thus, @code{array (pointer (char), 4)} is the type of arrays of 4
651pointers to @code{char}.
652@end itemize
653
654@node Lvalues
655@section Generalized Lvalues
656@cindex compound expressions as lvalues
657@cindex expressions, compound, as lvalues
658@cindex conditional expressions as lvalues
659@cindex expressions, conditional, as lvalues
660@cindex casts as lvalues
661@cindex generalized lvalues
662@cindex lvalues, generalized
663@cindex extensions, @code{?:}
664@cindex @code{?:} extensions
665Compound expressions, conditional expressions and casts are allowed as
666lvalues provided their operands are lvalues. This means that you can take
667their addresses or store values into them.
668
669Standard C++ allows compound expressions and conditional expressions as
670lvalues, and permits casts to reference type, so use of this extension
671is deprecated for C++ code.
672
673For example, a compound expression can be assigned, provided the last
674expression in the sequence is an lvalue. These two expressions are
675equivalent:
676
677@example
678(a, b) += 5
679a, (b += 5)
680@end example
681
682Similarly, the address of the compound expression can be taken. These two
683expressions are equivalent:
684
685@example
686&(a, b)
687a, &b
688@end example
689
690A conditional expression is a valid lvalue if its type is not void and the
691true and false branches are both valid lvalues. For example, these two
692expressions are equivalent:
693
694@example
695(a ? b : c) = 5
696(a ? b = 5 : (c = 5))
697@end example
698
699A cast is a valid lvalue if its operand is an lvalue. A simple
700assignment whose left-hand side is a cast works by converting the
701right-hand side first to the specified type, then to the type of the
702inner left-hand side expression. After this is stored, the value is
703converted back to the specified type to become the value of the
704assignment. Thus, if @code{a} has type @code{char *}, the following two
705expressions are equivalent:
706
707@example
708(int)a = 5
709(int)(a = (char *)(int)5)
710@end example
711
712An assignment-with-arithmetic operation such as @samp{+=} applied to a cast
713performs the arithmetic using the type resulting from the cast, and then
714continues as in the previous case. Therefore, these two expressions are
715equivalent:
716
717@example
718(int)a += 5
719(int)(a = (char *)(int) ((int)a + 5))
720@end example
721
722You cannot take the address of an lvalue cast, because the use of its
723address would not work out coherently. Suppose that @code{&(int)f} were
724permitted, where @code{f} has type @code{float}. Then the following
725statement would try to store an integer bit-pattern where a floating
726point number belongs:
727
728@example
729*&(int)f = 1;
730@end example
731
732This is quite different from what @code{(int)f = 1} would do---that
733would convert 1 to floating point and store it. Rather than cause this
734inconsistency, we think it is better to prohibit use of @samp{&} on a cast.
735
736If you really do want an @code{int *} pointer with the address of
737@code{f}, you can simply write @code{(int *)&f}.
738
739@node Conditionals
740@section Conditionals with Omitted Operands
741@cindex conditional expressions, extensions
742@cindex omitted middle-operands
743@cindex middle-operands, omitted
744@cindex extensions, @code{?:}
745@cindex @code{?:} extensions
746
747The middle operand in a conditional expression may be omitted. Then
748if the first operand is nonzero, its value is the value of the conditional
749expression.
750
751Therefore, the expression
752
753@example
754x ? : y
755@end example
756
757@noindent
758has the value of @code{x} if that is nonzero; otherwise, the value of
759@code{y}.
760
761This example is perfectly equivalent to
762
763@example
764x ? x : y
765@end example
766
767@cindex side effect in ?:
768@cindex ?: side effect
769@noindent
770In this simple case, the ability to omit the middle operand is not
771especially useful. When it becomes useful is when the first operand does,
772or may (if it is a macro argument), contain a side effect. Then repeating
773the operand in the middle would perform the side effect twice. Omitting
774the middle operand uses the value already computed without the undesirable
775effects of recomputing it.
776
777@node Long Long
778@section Double-Word Integers
779@cindex @code{long long} data types
780@cindex double-word arithmetic
781@cindex multiprecision arithmetic
782
783GNU C supports data types for integers that are twice as long as
784@code{int}. Simply write @code{long long int} for a signed integer, or
785@code{unsigned long long int} for an unsigned integer. To make an
786integer constant of type @code{long long int}, add the suffix @code{LL}
787to the integer. To make an integer constant of type @code{unsigned long
788long int}, add the suffix @code{ULL} to the integer.
789
790You can use these types in arithmetic like any other integer types.
791Addition, subtraction, and bitwise boolean operations on these types
792are open-coded on all types of machines. Multiplication is open-coded
793if the machine supports fullword-to-doubleword a widening multiply
794instruction. Division and shifts are open-coded only on machines that
795provide special support. The operations that are not open-coded use
796special library routines that come with GNU CC.
797
798There may be pitfalls when you use @code{long long} types for function
799arguments, unless you declare function prototypes. If a function
800expects type @code{int} for its argument, and you pass a value of type
801@code{long long int}, confusion will result because the caller and the
802subroutine will disagree about the number of bytes for the argument.
803Likewise, if the function expects @code{long long int} and you pass
804@code{int}. The best way to avoid such problems is to use prototypes.
805
806@node Complex
807@section Complex Numbers
808@cindex complex numbers
809
810GNU C supports complex data types. You can declare both complex integer
811types and complex floating types, using the keyword @code{__complex__}.
812
813For example, @samp{__complex__ double x;} declares @code{x} as a
814variable whose real part and imaginary part are both of type
815@code{double}. @samp{__complex__ short int y;} declares @code{y} to
816have real and imaginary parts of type @code{short int}; this is not
817likely to be useful, but it shows that the set of complex types is
818complete.
819
820To write a constant with a complex data type, use the suffix @samp{i} or
821@samp{j} (either one; they are equivalent). For example, @code{2.5fi}
822has type @code{__complex__ float} and @code{3i} has type
823@code{__complex__ int}. Such a constant always has a pure imaginary
824value, but you can form any complex value you like by adding one to a
825real constant.
826
827To extract the real part of a complex-valued expression @var{exp}, write
828@code{__real__ @var{exp}}. Likewise, use @code{__imag__} to
829extract the imaginary part.
830
831The operator @samp{~} performs complex conjugation when used on a value
832with a complex type.
833
834GNU CC can allocate complex automatic variables in a noncontiguous
835fashion; it's even possible for the real part to be in a register while
836the imaginary part is on the stack (or vice-versa). None of the
837supported debugging info formats has a way to represent noncontiguous
838allocation like this, so GNU CC describes a noncontiguous complex
839variable as if it were two separate variables of noncomplex type.
840If the variable's actual name is @code{foo}, the two fictitious
841variables are named @code{foo$real} and @code{foo$imag}. You can
842examine and set these two fictitious variables with your debugger.
843
844A future version of GDB will know how to recognize such pairs and treat
845them as a single variable with a complex type.
846
6f4d7222 847@node Hex Floats
6b42b9ea
UD
848@section Hex Floats
849@cindex hex floats
c5c76735 850
2a59078d 851GNU CC recognizes floating-point numbers written not only in the usual
6f4d7222
UD
852decimal notation, such as @code{1.55e1}, but also numbers such as
853@code{0x1.fp3} written in hexadecimal format. In that format the
854@code{0x} hex introducer and the @code{p} or @code{P} exponent field are
855mandatory. The exponent is a decimal number that indicates the power of
8562 by which the significand part will be multiplied. Thus @code{0x1.f} is
8571 15/16, @code{p3} multiplies it by 8, and the value of @code{0x1.fp3}
858is the same as @code{1.55e1}.
859
860Unlike for floating-point numbers in the decimal notation the exponent
861is always required in the hexadecimal notation. Otherwise the compiler
862would not be able to resolve the ambiguity of, e.g., @code{0x1.f}. This
863could mean @code{1.0f} or @code{1.9375} since @code{f} is also the
864extension for floating-point constants of type @code{float}.
865
c1f7febf
RK
866@node Zero Length
867@section Arrays of Length Zero
868@cindex arrays of length zero
869@cindex zero-length arrays
870@cindex length-zero arrays
871
872Zero-length arrays are allowed in GNU C. They are very useful as the last
873element of a structure which is really a header for a variable-length
874object:
875
876@example
877struct line @{
878 int length;
879 char contents[0];
880@};
881
882@{
883 struct line *thisline = (struct line *)
884 malloc (sizeof (struct line) + this_length);
885 thisline->length = this_length;
886@}
887@end example
888
889In standard C, you would have to give @code{contents} a length of 1, which
890means either you waste space or complicate the argument to @code{malloc}.
891
892@node Variable Length
893@section Arrays of Variable Length
894@cindex variable-length arrays
895@cindex arrays of variable length
896
897Variable-length automatic arrays are allowed in GNU C. These arrays are
898declared like any other automatic arrays, but with a length that is not
899a constant expression. The storage is allocated at the point of
900declaration and deallocated when the brace-level is exited. For
901example:
902
903@example
904FILE *
905concat_fopen (char *s1, char *s2, char *mode)
906@{
907 char str[strlen (s1) + strlen (s2) + 1];
908 strcpy (str, s1);
909 strcat (str, s2);
910 return fopen (str, mode);
911@}
912@end example
913
914@cindex scope of a variable length array
915@cindex variable-length array scope
916@cindex deallocating variable length arrays
917Jumping or breaking out of the scope of the array name deallocates the
918storage. Jumping into the scope is not allowed; you get an error
919message for it.
920
921@cindex @code{alloca} vs variable-length arrays
922You can use the function @code{alloca} to get an effect much like
923variable-length arrays. The function @code{alloca} is available in
924many other C implementations (but not in all). On the other hand,
925variable-length arrays are more elegant.
926
927There are other differences between these two methods. Space allocated
928with @code{alloca} exists until the containing @emph{function} returns.
929The space for a variable-length array is deallocated as soon as the array
930name's scope ends. (If you use both variable-length arrays and
931@code{alloca} in the same function, deallocation of a variable-length array
932will also deallocate anything more recently allocated with @code{alloca}.)
933
934You can also use variable-length arrays as arguments to functions:
935
936@example
937struct entry
938tester (int len, char data[len][len])
939@{
940 @dots{}
941@}
942@end example
943
944The length of an array is computed once when the storage is allocated
945and is remembered for the scope of the array in case you access it with
946@code{sizeof}.
947
948If you want to pass the array first and the length afterward, you can
949use a forward declaration in the parameter list---another GNU extension.
950
951@example
952struct entry
953tester (int len; char data[len][len], int len)
954@{
955 @dots{}
956@}
957@end example
958
959@cindex parameter forward declaration
960The @samp{int len} before the semicolon is a @dfn{parameter forward
961declaration}, and it serves the purpose of making the name @code{len}
962known when the declaration of @code{data} is parsed.
963
964You can write any number of such parameter forward declarations in the
965parameter list. They can be separated by commas or semicolons, but the
966last one must end with a semicolon, which is followed by the ``real''
967parameter declarations. Each forward declaration must match a ``real''
968declaration in parameter name and data type.
969
970@node Macro Varargs
971@section Macros with Variable Numbers of Arguments
972@cindex variable number of arguments
973@cindex macro with variable arguments
974@cindex rest argument (in macro)
975
976In GNU C, a macro can accept a variable number of arguments, much as a
977function can. The syntax for defining the macro looks much like that
978used for a function. Here is an example:
979
980@example
981#define eprintf(format, args...) \
982 fprintf (stderr, format , ## args)
983@end example
984
985Here @code{args} is a @dfn{rest argument}: it takes in zero or more
986arguments, as many as the call contains. All of them plus the commas
987between them form the value of @code{args}, which is substituted into
988the macro body where @code{args} is used. Thus, we have this expansion:
989
990@example
991eprintf ("%s:%d: ", input_file_name, line_number)
992@expansion{}
993fprintf (stderr, "%s:%d: " , input_file_name, line_number)
994@end example
995
996@noindent
997Note that the comma after the string constant comes from the definition
998of @code{eprintf}, whereas the last comma comes from the value of
999@code{args}.
1000
1001The reason for using @samp{##} is to handle the case when @code{args}
1002matches no arguments at all. In this case, @code{args} has an empty
1003value. In this case, the second comma in the definition becomes an
1004embarrassment: if it got through to the expansion of the macro, we would
1005get something like this:
1006
1007@example
1008fprintf (stderr, "success!\n" , )
1009@end example
1010
1011@noindent
1012which is invalid C syntax. @samp{##} gets rid of the comma, so we get
1013the following instead:
1014
1015@example
1016fprintf (stderr, "success!\n")
1017@end example
1018
1019This is a special feature of the GNU C preprocessor: @samp{##} before a
1020rest argument that is empty discards the preceding sequence of
1021non-whitespace characters from the macro definition. (If another macro
1022argument precedes, none of it is discarded.)
1023
1024It might be better to discard the last preprocessor token instead of the
1025last preceding sequence of non-whitespace characters; in fact, we may
1026someday change this feature to do so. We advise you to write the macro
1027definition so that the preceding sequence of non-whitespace characters
1028is just a single token, so that the meaning will not change if we change
1029the definition of this feature.
1030
1031@node Subscripting
1032@section Non-Lvalue Arrays May Have Subscripts
1033@cindex subscripting
1034@cindex arrays, non-lvalue
1035
1036@cindex subscripting and function values
1037Subscripting is allowed on arrays that are not lvalues, even though the
1038unary @samp{&} operator is not. For example, this is valid in GNU C though
1039not valid in other C dialects:
1040
1041@example
1042@group
1043struct foo @{int a[4];@};
1044
1045struct foo f();
1046
1047bar (int index)
1048@{
1049 return f().a[index];
1050@}
1051@end group
1052@end example
1053
1054@node Pointer Arith
1055@section Arithmetic on @code{void}- and Function-Pointers
1056@cindex void pointers, arithmetic
1057@cindex void, size of pointer to
1058@cindex function pointers, arithmetic
1059@cindex function, size of pointer to
1060
1061In GNU C, addition and subtraction operations are supported on pointers to
1062@code{void} and on pointers to functions. This is done by treating the
1063size of a @code{void} or of a function as 1.
1064
1065A consequence of this is that @code{sizeof} is also allowed on @code{void}
1066and on function types, and returns 1.
1067
1068The option @samp{-Wpointer-arith} requests a warning if these extensions
1069are used.
1070
1071@node Initializers
1072@section Non-Constant Initializers
1073@cindex initializers, non-constant
1074@cindex non-constant initializers
1075
1076As in standard C++, the elements of an aggregate initializer for an
1077automatic variable are not required to be constant expressions in GNU C.
1078Here is an example of an initializer with run-time varying elements:
1079
1080@example
1081foo (float f, float g)
1082@{
1083 float beat_freqs[2] = @{ f-g, f+g @};
1084 @dots{}
1085@}
1086@end example
1087
1088@node Constructors
1089@section Constructor Expressions
1090@cindex constructor expressions
1091@cindex initializations in expressions
1092@cindex structures, constructor expression
1093@cindex expressions, constructor
1094
1095GNU C supports constructor expressions. A constructor looks like
1096a cast containing an initializer. Its value is an object of the
1097type specified in the cast, containing the elements specified in
1098the initializer.
1099
1100Usually, the specified type is a structure. Assume that
1101@code{struct foo} and @code{structure} are declared as shown:
1102
1103@example
1104struct foo @{int a; char b[2];@} structure;
1105@end example
1106
1107@noindent
1108Here is an example of constructing a @code{struct foo} with a constructor:
1109
1110@example
1111structure = ((struct foo) @{x + y, 'a', 0@});
1112@end example
1113
1114@noindent
1115This is equivalent to writing the following:
1116
1117@example
1118@{
1119 struct foo temp = @{x + y, 'a', 0@};
1120 structure = temp;
1121@}
1122@end example
1123
1124You can also construct an array. If all the elements of the constructor
1125are (made up of) simple constant expressions, suitable for use in
1126initializers, then the constructor is an lvalue and can be coerced to a
1127pointer to its first element, as shown here:
1128
1129@example
1130char **foo = (char *[]) @{ "x", "y", "z" @};
1131@end example
1132
1133Array constructors whose elements are not simple constants are
1134not very useful, because the constructor is not an lvalue. There
1135are only two valid ways to use it: to subscript it, or initialize
1136an array variable with it. The former is probably slower than a
1137@code{switch} statement, while the latter does the same thing an
1138ordinary C initializer would do. Here is an example of
1139subscripting an array constructor:
1140
1141@example
1142output = ((int[]) @{ 2, x, 28 @}) [input];
1143@end example
1144
1145Constructor expressions for scalar types and union types are is
1146also allowed, but then the constructor expression is equivalent
1147to a cast.
1148
1149@node Labeled Elements
1150@section Labeled Elements in Initializers
1151@cindex initializers with labeled elements
1152@cindex labeled elements in initializers
1153@cindex case labels in initializers
1154
1155Standard C requires the elements of an initializer to appear in a fixed
1156order, the same as the order of the elements in the array or structure
1157being initialized.
1158
1159In GNU C you can give the elements in any order, specifying the array
1160indices or structure field names they apply to. This extension is not
1161implemented in GNU C++.
1162
1163To specify an array index, write @samp{[@var{index}]} or
1164@samp{[@var{index}] =} before the element value. For example,
1165
1166@example
1167int a[6] = @{ [4] 29, [2] = 15 @};
1168@end example
1169
1170@noindent
1171is equivalent to
1172
1173@example
1174int a[6] = @{ 0, 0, 15, 0, 29, 0 @};
1175@end example
1176
1177@noindent
1178The index values must be constant expressions, even if the array being
1179initialized is automatic.
1180
1181To initialize a range of elements to the same value, write
1182@samp{[@var{first} ... @var{last}] = @var{value}}. For example,
1183
1184@example
1185int widths[] = @{ [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 @};
1186@end example
1187
1188@noindent
1189Note that the length of the array is the highest value specified
1190plus one.
1191
1192In a structure initializer, specify the name of a field to initialize
1193with @samp{@var{fieldname}:} before the element value. For example,
1194given the following structure,
1195
1196@example
1197struct point @{ int x, y; @};
1198@end example
1199
1200@noindent
1201the following initialization
1202
1203@example
1204struct point p = @{ y: yvalue, x: xvalue @};
1205@end example
1206
1207@noindent
1208is equivalent to
1209
1210@example
1211struct point p = @{ xvalue, yvalue @};
1212@end example
1213
1214Another syntax which has the same meaning is @samp{.@var{fieldname} =}.,
1215as shown here:
1216
1217@example
1218struct point p = @{ .y = yvalue, .x = xvalue @};
1219@end example
1220
1221You can also use an element label (with either the colon syntax or the
1222period-equal syntax) when initializing a union, to specify which element
1223of the union should be used. For example,
1224
1225@example
1226union foo @{ int i; double d; @};
1227
1228union foo f = @{ d: 4 @};
1229@end example
1230
1231@noindent
1232will convert 4 to a @code{double} to store it in the union using
1233the second element. By contrast, casting 4 to type @code{union foo}
1234would store it into the union as the integer @code{i}, since it is
1235an integer. (@xref{Cast to Union}.)
1236
1237You can combine this technique of naming elements with ordinary C
1238initialization of successive elements. Each initializer element that
1239does not have a label applies to the next consecutive element of the
1240array or structure. For example,
1241
1242@example
1243int a[6] = @{ [1] = v1, v2, [4] = v4 @};
1244@end example
1245
1246@noindent
1247is equivalent to
1248
1249@example
1250int a[6] = @{ 0, v1, v2, 0, v4, 0 @};
1251@end example
1252
1253Labeling the elements of an array initializer is especially useful
1254when the indices are characters or belong to an @code{enum} type.
1255For example:
1256
1257@example
1258int whitespace[256]
1259 = @{ [' '] = 1, ['\t'] = 1, ['\h'] = 1,
1260 ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 @};
1261@end example
1262
1263@node Case Ranges
1264@section Case Ranges
1265@cindex case ranges
1266@cindex ranges in case statements
1267
1268You can specify a range of consecutive values in a single @code{case} label,
1269like this:
1270
1271@example
1272case @var{low} ... @var{high}:
1273@end example
1274
1275@noindent
1276This has the same effect as the proper number of individual @code{case}
1277labels, one for each integer value from @var{low} to @var{high}, inclusive.
1278
1279This feature is especially useful for ranges of ASCII character codes:
1280
1281@example
1282case 'A' ... 'Z':
1283@end example
1284
1285@strong{Be careful:} Write spaces around the @code{...}, for otherwise
1286it may be parsed wrong when you use it with integer values. For example,
1287write this:
1288
1289@example
1290case 1 ... 5:
1291@end example
1292
1293@noindent
1294rather than this:
1295
1296@example
1297case 1...5:
1298@end example
1299
1300@node Cast to Union
1301@section Cast to a Union Type
1302@cindex cast to a union
1303@cindex union, casting to a
1304
1305A cast to union type is similar to other casts, except that the type
1306specified is a union type. You can specify the type either with
1307@code{union @var{tag}} or with a typedef name. A cast to union is actually
1308a constructor though, not a cast, and hence does not yield an lvalue like
1309normal casts. (@xref{Constructors}.)
1310
1311The types that may be cast to the union type are those of the members
1312of the union. Thus, given the following union and variables:
1313
1314@example
1315union foo @{ int i; double d; @};
1316int x;
1317double y;
1318@end example
1319
1320@noindent
1321both @code{x} and @code{y} can be cast to type @code{union} foo.
1322
1323Using the cast as the right-hand side of an assignment to a variable of
1324union type is equivalent to storing in a member of the union:
1325
1326@example
1327union foo u;
1328@dots{}
1329u = (union foo) x @equiv{} u.i = x
1330u = (union foo) y @equiv{} u.d = y
1331@end example
1332
1333You can also use the union cast as a function argument:
1334
1335@example
1336void hack (union foo);
1337@dots{}
1338hack ((union foo) x);
1339@end example
1340
1341@node Function Attributes
1342@section Declaring Attributes of Functions
1343@cindex function attributes
1344@cindex declaring attributes of functions
1345@cindex functions that never return
1346@cindex functions that have no side effects
1347@cindex functions in arbitrary sections
2a59078d 1348@cindex functions that behave like malloc
c1f7febf
RK
1349@cindex @code{volatile} applied to function
1350@cindex @code{const} applied to function
bb72a084 1351@cindex functions with @code{printf}, @code{scanf} or @code{strftime} style arguments
c1f7febf
RK
1352@cindex functions that are passed arguments in registers on the 386
1353@cindex functions that pop the argument stack on the 386
1354@cindex functions that do not pop the argument stack on the 386
1355
1356In GNU C, you declare certain things about functions called in your program
1357which help the compiler optimize function calls and check your code more
1358carefully.
1359
1360The keyword @code{__attribute__} allows you to specify special
1361attributes when making a declaration. This keyword is followed by an
140592a0 1362attribute specification inside double parentheses. Ten attributes,
07417085 1363@code{noreturn}, @code{const}, @code{format},
140592a0
AG
1364@code{no_instrument_function}, @code{section}, @code{constructor},
1365@code{destructor}, @code{unused}, @code{weak} and @code{malloc} are
c1f7febf
RK
1366currently defined for functions. Other attributes, including
1367@code{section} are supported for variables declarations (@pxref{Variable
1368Attributes}) and for types (@pxref{Type Attributes}).
1369
1370You may also specify attributes with @samp{__} preceding and following
1371each keyword. This allows you to use them in header files without
1372being concerned about a possible macro of the same name. For example,
1373you may use @code{__noreturn__} instead of @code{noreturn}.
1374
1375@table @code
1376@cindex @code{noreturn} function attribute
1377@item noreturn
1378A few standard library functions, such as @code{abort} and @code{exit},
1379cannot return. GNU CC knows this automatically. Some programs define
1380their own functions that never return. You can declare them
1381@code{noreturn} to tell the compiler this fact. For example,
1382
1383@smallexample
1384void fatal () __attribute__ ((noreturn));
1385
1386void
1387fatal (@dots{})
1388@{
1389 @dots{} /* @r{Print error message.} */ @dots{}
1390 exit (1);
1391@}
1392@end smallexample
1393
1394The @code{noreturn} keyword tells the compiler to assume that
1395@code{fatal} cannot return. It can then optimize without regard to what
1396would happen if @code{fatal} ever did return. This makes slightly
1397better code. More importantly, it helps avoid spurious warnings of
1398uninitialized variables.
1399
1400Do not assume that registers saved by the calling function are
1401restored before calling the @code{noreturn} function.
1402
1403It does not make sense for a @code{noreturn} function to have a return
1404type other than @code{void}.
1405
1406The attribute @code{noreturn} is not implemented in GNU C versions
1407earlier than 2.5. An alternative way to declare that a function does
1408not return, which works in the current version and in some older
1409versions, is as follows:
1410
1411@smallexample
1412typedef void voidfn ();
1413
1414volatile voidfn fatal;
1415@end smallexample
1416
2a8f6b90
JH
1417@cindex @code{pure} function attribute
1418@item pure
1419Many functions have no effects except the return value and their
d4047e24 1420return value depends only on the parameters and/or global variables.
2a8f6b90 1421Such a function can be subject
c1f7febf
RK
1422to common subexpression elimination and loop optimization just as an
1423arithmetic operator would be. These functions should be declared
2a8f6b90 1424with the attribute @code{pure}. For example,
c1f7febf
RK
1425
1426@smallexample
2a8f6b90 1427int square (int) __attribute__ ((pure));
c1f7febf
RK
1428@end smallexample
1429
1430@noindent
1431says that the hypothetical function @code{square} is safe to call
1432fewer times than the program says.
1433
2a8f6b90
JH
1434Some of common examples of pure functions are @code{strlen} or @code{memcmp}.
1435Interesting non-pure functions are functions with infinite loops or those
1436depending on volatile memory or other system resource, that may change between
2a59078d 1437two consecutive calls (such as @code{feof} in a multithreading environment).
2a8f6b90
JH
1438
1439The attribute @code{pure} is not implemented in GNU C versions earlier
1440than 2.96.
1441@cindex @code{const} function attribute
1442@item const
1443Many functions do not examine any values except their arguments, and
1444have no effects except the return value. Basically this is just slightly
1445more strict class than the "pure" attribute above, since function is not
2a59078d 1446allowed to read global memory.
2a8f6b90
JH
1447
1448@cindex pointer arguments
1449Note that a function that has pointer arguments and examines the data
1450pointed to must @emph{not} be declared @code{const}. Likewise, a
1451function that calls a non-@code{const} function usually must not be
1452@code{const}. It does not make sense for a @code{const} function to
1453return @code{void}.
1454
c1f7febf
RK
1455The attribute @code{const} is not implemented in GNU C versions earlier
1456than 2.5. An alternative way to declare that a function has no side
1457effects, which works in the current version and in some older versions,
1458is as follows:
1459
1460@smallexample
1461typedef int intfn ();
1462
1463extern const intfn square;
1464@end smallexample
1465
1466This approach does not work in GNU C++ from 2.6.0 on, since the language
1467specifies that the @samp{const} must be attached to the return value.
1468
c1f7febf
RK
1469
1470@item format (@var{archetype}, @var{string-index}, @var{first-to-check})
1471@cindex @code{format} function attribute
bb72a084
PE
1472The @code{format} attribute specifies that a function takes @code{printf},
1473@code{scanf}, or @code{strftime} style arguments which should be type-checked
1474against a format string. For example, the declaration:
c1f7febf
RK
1475
1476@smallexample
1477extern int
1478my_printf (void *my_object, const char *my_format, ...)
1479 __attribute__ ((format (printf, 2, 3)));
1480@end smallexample
1481
1482@noindent
1483causes the compiler to check the arguments in calls to @code{my_printf}
1484for consistency with the @code{printf} style format string argument
1485@code{my_format}.
1486
1487The parameter @var{archetype} determines how the format string is
bb72a084
PE
1488interpreted, and should be either @code{printf}, @code{scanf}, or
1489@code{strftime}. The
c1f7febf
RK
1490parameter @var{string-index} specifies which argument is the format
1491string argument (starting from 1), while @var{first-to-check} is the
1492number of the first argument to check against the format string. For
1493functions where the arguments are not available to be checked (such as
1494@code{vprintf}), specify the third parameter as zero. In this case the
1495compiler only checks the format string for consistency.
1496
1497In the example above, the format string (@code{my_format}) is the second
1498argument of the function @code{my_print}, and the arguments to check
1499start with the third argument, so the correct parameters for the format
1500attribute are 2 and 3.
1501
1502The @code{format} attribute allows you to identify your own functions
1503which take format strings as arguments, so that GNU CC can check the
1504calls to these functions for errors. The compiler always checks formats
1505for the ANSI library functions @code{printf}, @code{fprintf},
bb72a084 1506@code{sprintf}, @code{scanf}, @code{fscanf}, @code{sscanf}, @code{strftime},
c1f7febf
RK
1507@code{vprintf}, @code{vfprintf} and @code{vsprintf} whenever such
1508warnings are requested (using @samp{-Wformat}), so there is no need to
1509modify the header file @file{stdio.h}.
1510
1511@item format_arg (@var{string-index})
1512@cindex @code{format_arg} function attribute
1513The @code{format_arg} attribute specifies that a function takes
1514@code{printf} or @code{scanf} style arguments, modifies it (for example,
1515to translate it into another language), and passes it to a @code{printf}
1516or @code{scanf} style function. For example, the declaration:
1517
1518@smallexample
1519extern char *
1520my_dgettext (char *my_domain, const char *my_format)
1521 __attribute__ ((format_arg (2)));
1522@end smallexample
1523
1524@noindent
1525causes the compiler to check the arguments in calls to
bb72a084
PE
1526@code{my_dgettext} whose result is passed to a @code{printf},
1527@code{scanf}, or @code{strftime} type function for consistency with the
1528@code{printf} style format string argument @code{my_format}.
c1f7febf
RK
1529
1530The parameter @var{string-index} specifies which argument is the format
1531string argument (starting from 1).
1532
1533The @code{format-arg} attribute allows you to identify your own
1534functions which modify format strings, so that GNU CC can check the
bb72a084
PE
1535calls to @code{printf}, @code{scanf}, or @code{strftime} function whose
1536operands are a call to one of your own function. The compiler always
1537treats @code{gettext}, @code{dgettext}, and @code{dcgettext} in this
1538manner.
c1f7febf 1539
07417085
KR
1540@item no_instrument_function
1541@cindex @code{no_instrument_function} function attribute
1542If @samp{-finstrument-functions} is given, profiling function calls will
1543be generated at entry and exit of most user-compiled functions.
1544Functions with this attribute will not be so instrumented.
1545
c1f7febf
RK
1546@item section ("section-name")
1547@cindex @code{section} function attribute
1548Normally, the compiler places the code it generates in the @code{text} section.
1549Sometimes, however, you need additional sections, or you need certain
1550particular functions to appear in special sections. The @code{section}
1551attribute specifies that a function lives in a particular section.
1552For example, the declaration:
1553
1554@smallexample
1555extern void foobar (void) __attribute__ ((section ("bar")));
1556@end smallexample
1557
1558@noindent
1559puts the function @code{foobar} in the @code{bar} section.
1560
1561Some file formats do not support arbitrary sections so the @code{section}
1562attribute is not available on all platforms.
1563If you need to map the entire contents of a module to a particular
1564section, consider using the facilities of the linker instead.
1565
1566@item constructor
1567@itemx destructor
1568@cindex @code{constructor} function attribute
1569@cindex @code{destructor} function attribute
1570The @code{constructor} attribute causes the function to be called
1571automatically before execution enters @code{main ()}. Similarly, the
1572@code{destructor} attribute causes the function to be called
1573automatically after @code{main ()} has completed or @code{exit ()} has
1574been called. Functions with these attributes are useful for
1575initializing data that will be used implicitly during the execution of
1576the program.
1577
1578These attributes are not currently implemented for Objective C.
1579
1580@item unused
1581This attribute, attached to a function, means that the function is meant
1582to be possibly unused. GNU CC will not produce a warning for this
1583function. GNU C++ does not currently support this attribute as
1584definitions without parameters are valid in C++.
1585
1586@item weak
1587@cindex @code{weak} attribute
1588The @code{weak} attribute causes the declaration to be emitted as a weak
1589symbol rather than a global. This is primarily useful in defining
1590library functions which can be overridden in user code, though it can
1591also be used with non-function declarations. Weak symbols are supported
1592for ELF targets, and also for a.out targets when using the GNU assembler
1593and linker.
1594
140592a0
AG
1595@item malloc
1596@cindex @code{malloc} attribute
1597The @code{malloc} attribute is used to tell the compiler that a function
1598may be treated as if it were the malloc function. The compiler assumes
1599that calls to malloc result in a pointers that cannot alias anything.
1600This will often improve optimization.
1601
c1f7febf
RK
1602@item alias ("target")
1603@cindex @code{alias} attribute
1604The @code{alias} attribute causes the declaration to be emitted as an
1605alias for another symbol, which must be specified. For instance,
1606
1607@smallexample
1608void __f () @{ /* do something */; @}
1609void f () __attribute__ ((weak, alias ("__f")));
1610@end smallexample
1611
1612declares @samp{f} to be a weak alias for @samp{__f}. In C++, the
1613mangled name for the target must be used.
1614
af3e86c2
RK
1615Not all target machines support this attribute.
1616
7d384cc0
KR
1617@item no_check_memory_usage
1618@cindex @code{no_check_memory_usage} function attribute
c5c76735
JL
1619The @code{no_check_memory_usage} attribute causes GNU CC to omit checks
1620of memory references when it generates code for that function. Normally
1621if you specify @samp{-fcheck-memory-usage} (see @pxref{Code Gen
1622Options}), GNU CC generates calls to support routines before most memory
1623accesses to permit support code to record usage and detect uses of
1624uninitialized or unallocated storage. Since GNU CC cannot handle
1625@code{asm} statements properly they are not allowed in such functions.
1626If you declare a function with this attribute, GNU CC will not generate
7d384cc0 1627memory checking code for that function, permitting the use of @code{asm}
c5c76735
JL
1628statements without having to compile that function with different
1629options. This also allows you to write support routines of your own if
1630you wish, without getting infinite recursion if they get compiled with
1631@code{-fcheck-memory-usage}.
7d384cc0 1632
c1f7febf
RK
1633@item regparm (@var{number})
1634@cindex functions that are passed arguments in registers on the 386
1635On the Intel 386, the @code{regparm} attribute causes the compiler to
1636pass up to @var{number} integer arguments in registers @var{EAX},
1637@var{EDX}, and @var{ECX} instead of on the stack. Functions that take a
1638variable number of arguments will continue to be passed all of their
1639arguments on the stack.
1640
1641@item stdcall
1642@cindex functions that pop the argument stack on the 386
1643On the Intel 386, the @code{stdcall} attribute causes the compiler to
1644assume that the called function will pop off the stack space used to
1645pass arguments, unless it takes a variable number of arguments.
1646
1647The PowerPC compiler for Windows NT currently ignores the @code{stdcall}
1648attribute.
1649
1650@item cdecl
1651@cindex functions that do pop the argument stack on the 386
1652On the Intel 386, the @code{cdecl} attribute causes the compiler to
1653assume that the calling function will pop off the stack space used to
1654pass arguments. This is
1655useful to override the effects of the @samp{-mrtd} switch.
1656
1657The PowerPC compiler for Windows NT currently ignores the @code{cdecl}
1658attribute.
1659
1660@item longcall
1661@cindex functions called via pointer on the RS/6000 and PowerPC
1662On the RS/6000 and PowerPC, the @code{longcall} attribute causes the
1663compiler to always call the function via a pointer, so that functions
1664which reside further than 64 megabytes (67,108,864 bytes) from the
1665current location can be called.
1666
c27ba912
DM
1667@item long_call/short_call
1668@cindex indirect calls on ARM
1669This attribute allows to specify how to call a particular function on
1670ARM. Both attributes override the @code{-mlong-calls} (@pxref{ARM Options})
1671command line switch and @code{#pragma long_calls} settings. The
1672@code{long_call} attribute causes the compiler to always call the
1673function by first loading its address into a register and then using the
1674contents of that register. The @code{short_call} attribute always places
1675the offset to the function from the call site into the @samp{BL}
1676instruction directly.
1677
c1f7febf
RK
1678@item dllimport
1679@cindex functions which are imported from a dll on PowerPC Windows NT
1680On the PowerPC running Windows NT, the @code{dllimport} attribute causes
1681the compiler to call the function via a global pointer to the function
1682pointer that is set up by the Windows NT dll library. The pointer name
1683is formed by combining @code{__imp_} and the function name.
1684
1685@item dllexport
1686@cindex functions which are exported from a dll on PowerPC Windows NT
1687On the PowerPC running Windows NT, the @code{dllexport} attribute causes
1688the compiler to provide a global pointer to the function pointer, so
1689that it can be called with the @code{dllimport} attribute. The pointer
1690name is formed by combining @code{__imp_} and the function name.
1691
1692@item exception (@var{except-func} [, @var{except-arg}])
1693@cindex functions which specify exception handling on PowerPC Windows NT
1694On the PowerPC running Windows NT, the @code{exception} attribute causes
1695the compiler to modify the structured exception table entry it emits for
1696the declared function. The string or identifier @var{except-func} is
1697placed in the third entry of the structured exception table. It
1698represents a function, which is called by the exception handling
1699mechanism if an exception occurs. If it was specified, the string or
1700identifier @var{except-arg} is placed in the fourth entry of the
1701structured exception table.
1702
1703@item function_vector
1704@cindex calling functions through the function vector on the H8/300 processors
1705Use this option on the H8/300 and H8/300H to indicate that the specified
1706function should be called through the function vector. Calling a
1707function through the function vector will reduce code size, however;
1708the function vector has a limited size (maximum 128 entries on the H8/300
1709and 64 entries on the H8/300H) and shares space with the interrupt vector.
1710
1711You must use GAS and GLD from GNU binutils version 2.7 or later for
1712this option to work correctly.
1713
1714@item interrupt_handler
1715@cindex interrupt handler functions on the H8/300 processors
1716Use this option on the H8/300 and H8/300H to indicate that the specified
1717function is an interrupt handler. The compiler will generate function
1718entry and exit sequences suitable for use in an interrupt handler when this
1719attribute is present.
1720
1721@item eightbit_data
1722@cindex eight bit data on the H8/300 and H8/300H
1723Use this option on the H8/300 and H8/300H to indicate that the specified
1724variable should be placed into the eight bit data section.
1725The compiler will generate more efficient code for certain operations
1726on data in the eight bit data area. Note the eight bit data area is limited to
1727256 bytes of data.
1728
1729You must use GAS and GLD from GNU binutils version 2.7 or later for
1730this option to work correctly.
1731
1732@item tiny_data
1733@cindex tiny data section on the H8/300H
1734Use this option on the H8/300H to indicate that the specified
1735variable should be placed into the tiny data section.
1736The compiler will generate more efficient code for loads and stores
1737on data in the tiny data section. Note the tiny data area is limited to
1738slightly under 32kbytes of data.
845da534
DE
1739
1740@item interrupt
1741@cindex interrupt handlers on the M32R/D
1742Use this option on the M32R/D to indicate that the specified
1743function is an interrupt handler. The compiler will generate function
1744entry and exit sequences suitable for use in an interrupt handler when this
1745attribute is present.
1746
052a4b28
DC
1747Interrupt handler functions on the AVR processors
1748Use this option on the AVR to indicate that the specified
1749function is an interrupt handler. The compiler will generate function
1750entry and exit sequences suitable for use in an interrupt handler when this
1751attribute is present. Interrupts will be enabled inside function.
1752
1753@item signal
1754@cindex signal handler functions on the AVR processors
1755Use this option on the AVR to indicate that the specified
1756function is an signal handler. The compiler will generate function
1757entry and exit sequences suitable for use in an signal handler when this
1758attribute is present. Interrupts will be disabled inside function.
1759
1760@item naked
1761@cindex function without a prologue/epilogue code on the AVR processors
1762Use this option on the AVR to indicate that the specified
1763function don't have a prologue/epilogue. The compiler don't generate
1764function entry and exit sequences.
1765
845da534
DE
1766@item model (@var{model-name})
1767@cindex function addressability on the M32R/D
1768Use this attribute on the M32R/D to set the addressability of an object,
1769and the code generated for a function.
1770The identifier @var{model-name} is one of @code{small}, @code{medium},
1771or @code{large}, representing each of the code models.
1772
1773Small model objects live in the lower 16MB of memory (so that their
1774addresses can be loaded with the @code{ld24} instruction), and are
1775callable with the @code{bl} instruction.
1776
1777Medium model objects may live anywhere in the 32 bit address space (the
1778compiler will generate @code{seth/add3} instructions to load their addresses),
1779and are callable with the @code{bl} instruction.
1780
1781Large model objects may live anywhere in the 32 bit address space (the
1782compiler will generate @code{seth/add3} instructions to load their addresses),
1783and may not be reachable with the @code{bl} instruction (the compiler will
1784generate the much slower @code{seth/add3/jl} instruction sequence).
1785
c1f7febf
RK
1786@end table
1787
1788You can specify multiple attributes in a declaration by separating them
1789by commas within the double parentheses or by immediately following an
1790attribute declaration with another attribute declaration.
1791
1792@cindex @code{#pragma}, reason for not using
1793@cindex pragma, reason for not using
1794Some people object to the @code{__attribute__} feature, suggesting that ANSI C's
1795@code{#pragma} should be used instead. There are two reasons for not
1796doing this.
1797
1798@enumerate
1799@item
1800It is impossible to generate @code{#pragma} commands from a macro.
1801
1802@item
1803There is no telling what the same @code{#pragma} might mean in another
1804compiler.
1805@end enumerate
1806
1807These two reasons apply to almost any application that might be proposed
1808for @code{#pragma}. It is basically a mistake to use @code{#pragma} for
1809@emph{anything}.
1810
1811@node Function Prototypes
1812@section Prototypes and Old-Style Function Definitions
1813@cindex function prototype declarations
1814@cindex old-style function definitions
1815@cindex promotion of formal parameters
1816
1817GNU C extends ANSI C to allow a function prototype to override a later
1818old-style non-prototype definition. Consider the following example:
1819
1820@example
1821/* @r{Use prototypes unless the compiler is old-fashioned.} */
d863830b 1822#ifdef __STDC__
c1f7febf
RK
1823#define P(x) x
1824#else
1825#define P(x) ()
1826#endif
1827
1828/* @r{Prototype function declaration.} */
1829int isroot P((uid_t));
1830
1831/* @r{Old-style function definition.} */
1832int
1833isroot (x) /* ??? lossage here ??? */
1834 uid_t x;
1835@{
1836 return x == 0;
1837@}
1838@end example
1839
1840Suppose the type @code{uid_t} happens to be @code{short}. ANSI C does
1841not allow this example, because subword arguments in old-style
1842non-prototype definitions are promoted. Therefore in this example the
1843function definition's argument is really an @code{int}, which does not
1844match the prototype argument type of @code{short}.
1845
1846This restriction of ANSI C makes it hard to write code that is portable
1847to traditional C compilers, because the programmer does not know
1848whether the @code{uid_t} type is @code{short}, @code{int}, or
1849@code{long}. Therefore, in cases like these GNU C allows a prototype
1850to override a later old-style definition. More precisely, in GNU C, a
1851function prototype argument type overrides the argument type specified
1852by a later old-style definition if the former type is the same as the
1853latter type before promotion. Thus in GNU C the above example is
1854equivalent to the following:
1855
1856@example
1857int isroot (uid_t);
1858
1859int
1860isroot (uid_t x)
1861@{
1862 return x == 0;
1863@}
1864@end example
1865
1866GNU C++ does not support old-style function definitions, so this
1867extension is irrelevant.
1868
1869@node C++ Comments
1870@section C++ Style Comments
1871@cindex //
1872@cindex C++ comments
1873@cindex comments, C++ style
1874
1875In GNU C, you may use C++ style comments, which start with @samp{//} and
1876continue until the end of the line. Many other C implementations allow
1877such comments, and they are likely to be in a future C standard.
1878However, C++ style comments are not recognized if you specify
1879@w{@samp{-ansi}} or @w{@samp{-traditional}}, since they are incompatible
1880with traditional constructs like @code{dividend//*comment*/divisor}.
1881
1882@node Dollar Signs
1883@section Dollar Signs in Identifier Names
1884@cindex $
1885@cindex dollar signs in identifier names
1886@cindex identifier names, dollar signs in
1887
79188db9
RK
1888In GNU C, you may normally use dollar signs in identifier names.
1889This is because many traditional C implementations allow such identifiers.
1890However, dollar signs in identifiers are not supported on a few target
1891machines, typically because the target assembler does not allow them.
c1f7febf
RK
1892
1893@node Character Escapes
1894@section The Character @key{ESC} in Constants
1895
1896You can use the sequence @samp{\e} in a string or character constant to
1897stand for the ASCII character @key{ESC}.
1898
1899@node Alignment
1900@section Inquiring on Alignment of Types or Variables
1901@cindex alignment
1902@cindex type alignment
1903@cindex variable alignment
1904
1905The keyword @code{__alignof__} allows you to inquire about how an object
1906is aligned, or the minimum alignment usually required by a type. Its
1907syntax is just like @code{sizeof}.
1908
1909For example, if the target machine requires a @code{double} value to be
1910aligned on an 8-byte boundary, then @code{__alignof__ (double)} is 8.
1911This is true on many RISC machines. On more traditional machine
1912designs, @code{__alignof__ (double)} is 4 or even 2.
1913
1914Some machines never actually require alignment; they allow reference to any
1915data type even at an odd addresses. For these machines, @code{__alignof__}
1916reports the @emph{recommended} alignment of a type.
1917
1918When the operand of @code{__alignof__} is an lvalue rather than a type, the
1919value is the largest alignment that the lvalue is known to have. It may
1920have this alignment as a result of its data type, or because it is part of
1921a structure and inherits alignment from that structure. For example, after
1922this declaration:
1923
1924@example
1925struct foo @{ int x; char y; @} foo1;
1926@end example
1927
1928@noindent
1929the value of @code{__alignof__ (foo1.y)} is probably 2 or 4, the same as
1930@code{__alignof__ (int)}, even though the data type of @code{foo1.y}
1931does not itself demand any alignment.@refill
1932
9d27bffe
SS
1933It is an error to ask for the alignment of an incomplete type.
1934
c1f7febf
RK
1935A related feature which lets you specify the alignment of an object is
1936@code{__attribute__ ((aligned (@var{alignment})))}; see the following
1937section.
1938
1939@node Variable Attributes
1940@section Specifying Attributes of Variables
1941@cindex attribute of variables
1942@cindex variable attributes
1943
1944The keyword @code{__attribute__} allows you to specify special
1945attributes of variables or structure fields. This keyword is followed
1946by an attribute specification inside double parentheses. Eight
1947attributes are currently defined for variables: @code{aligned},
1948@code{mode}, @code{nocommon}, @code{packed}, @code{section},
1949@code{transparent_union}, @code{unused}, and @code{weak}. Other
1950attributes are available for functions (@pxref{Function Attributes}) and
1951for types (@pxref{Type Attributes}).
1952
1953You may also specify attributes with @samp{__} preceding and following
1954each keyword. This allows you to use them in header files without
1955being concerned about a possible macro of the same name. For example,
1956you may use @code{__aligned__} instead of @code{aligned}.
1957
1958@table @code
1959@cindex @code{aligned} attribute
1960@item aligned (@var{alignment})
1961This attribute specifies a minimum alignment for the variable or
1962structure field, measured in bytes. For example, the declaration:
1963
1964@smallexample
1965int x __attribute__ ((aligned (16))) = 0;
1966@end smallexample
1967
1968@noindent
1969causes the compiler to allocate the global variable @code{x} on a
197016-byte boundary. On a 68040, this could be used in conjunction with
1971an @code{asm} expression to access the @code{move16} instruction which
1972requires 16-byte aligned operands.
1973
1974You can also specify the alignment of structure fields. For example, to
1975create a double-word aligned @code{int} pair, you could write:
1976
1977@smallexample
1978struct foo @{ int x[2] __attribute__ ((aligned (8))); @};
1979@end smallexample
1980
1981@noindent
1982This is an alternative to creating a union with a @code{double} member
1983that forces the union to be double-word aligned.
1984
1985It is not possible to specify the alignment of functions; the alignment
1986of functions is determined by the machine's requirements and cannot be
1987changed. You cannot specify alignment for a typedef name because such a
1988name is just an alias, not a distinct type.
1989
1990As in the preceding examples, you can explicitly specify the alignment
1991(in bytes) that you wish the compiler to use for a given variable or
1992structure field. Alternatively, you can leave out the alignment factor
1993and just ask the compiler to align a variable or field to the maximum
1994useful alignment for the target machine you are compiling for. For
1995example, you could write:
1996
1997@smallexample
1998short array[3] __attribute__ ((aligned));
1999@end smallexample
2000
2001Whenever you leave out the alignment factor in an @code{aligned} attribute
2002specification, the compiler automatically sets the alignment for the declared
2003variable or field to the largest alignment which is ever used for any data
2004type on the target machine you are compiling for. Doing this can often make
2005copy operations more efficient, because the compiler can use whatever
2006instructions copy the biggest chunks of memory when performing copies to
2007or from the variables or fields that you have aligned this way.
2008
2009The @code{aligned} attribute can only increase the alignment; but you
2010can decrease it by specifying @code{packed} as well. See below.
2011
2012Note that the effectiveness of @code{aligned} attributes may be limited
2013by inherent limitations in your linker. On many systems, the linker is
2014only able to arrange for variables to be aligned up to a certain maximum
2015alignment. (For some linkers, the maximum supported alignment may
2016be very very small.) If your linker is only able to align variables
2017up to a maximum of 8 byte alignment, then specifying @code{aligned(16)}
2018in an @code{__attribute__} will still only provide you with 8 byte
2019alignment. See your linker documentation for further information.
2020
2021@item mode (@var{mode})
2022@cindex @code{mode} attribute
2023This attribute specifies the data type for the declaration---whichever
2024type corresponds to the mode @var{mode}. This in effect lets you
2025request an integer or floating point type according to its width.
2026
2027You may also specify a mode of @samp{byte} or @samp{__byte__} to
2028indicate the mode corresponding to a one-byte integer, @samp{word} or
2029@samp{__word__} for the mode of a one-word integer, and @samp{pointer}
2030or @samp{__pointer__} for the mode used to represent pointers.
2031
2032@item nocommon
2033@cindex @code{nocommon} attribute
2034This attribute specifies requests GNU CC not to place a variable
2035``common'' but instead to allocate space for it directly. If you
2036specify the @samp{-fno-common} flag, GNU CC will do this for all
2037variables.
2038
2039Specifying the @code{nocommon} attribute for a variable provides an
2040initialization of zeros. A variable may only be initialized in one
2041source file.
2042
2043@item packed
2044@cindex @code{packed} attribute
2045The @code{packed} attribute specifies that a variable or structure field
2046should have the smallest possible alignment---one byte for a variable,
2047and one bit for a field, unless you specify a larger value with the
2048@code{aligned} attribute.
2049
2050Here is a structure in which the field @code{x} is packed, so that it
2051immediately follows @code{a}:
2052
2053@example
2054struct foo
2055@{
2056 char a;
2057 int x[2] __attribute__ ((packed));
2058@};
2059@end example
2060
2061@item section ("section-name")
2062@cindex @code{section} variable attribute
2063Normally, the compiler places the objects it generates in sections like
2064@code{data} and @code{bss}. Sometimes, however, you need additional sections,
2065or you need certain particular variables to appear in special sections,
2066for example to map to special hardware. The @code{section}
2067attribute specifies that a variable (or function) lives in a particular
2068section. For example, this small program uses several specific section names:
2069
2070@smallexample
2071struct duart a __attribute__ ((section ("DUART_A"))) = @{ 0 @};
2072struct duart b __attribute__ ((section ("DUART_B"))) = @{ 0 @};
2073char stack[10000] __attribute__ ((section ("STACK"))) = @{ 0 @};
2074int init_data __attribute__ ((section ("INITDATA"))) = 0;
2075
2076main()
2077@{
2078 /* Initialize stack pointer */
2079 init_sp (stack + sizeof (stack));
2080
2081 /* Initialize initialized data */
2082 memcpy (&init_data, &data, &edata - &data);
2083
2084 /* Turn on the serial ports */
2085 init_duart (&a);
2086 init_duart (&b);
2087@}
2088@end smallexample
2089
2090@noindent
2091Use the @code{section} attribute with an @emph{initialized} definition
2092of a @emph{global} variable, as shown in the example. GNU CC issues
2093a warning and otherwise ignores the @code{section} attribute in
2094uninitialized variable declarations.
2095
2096You may only use the @code{section} attribute with a fully initialized
2097global definition because of the way linkers work. The linker requires
2098each object be defined once, with the exception that uninitialized
2099variables tentatively go in the @code{common} (or @code{bss}) section
2100and can be multiply "defined". You can force a variable to be
2101initialized with the @samp{-fno-common} flag or the @code{nocommon}
2102attribute.
2103
2104Some file formats do not support arbitrary sections so the @code{section}
2105attribute is not available on all platforms.
2106If you need to map the entire contents of a module to a particular
2107section, consider using the facilities of the linker instead.
2108
593d3a34
MK
2109@item shared
2110@cindex @code{shared} variable attribute
2a59078d 2111On Windows NT, in addition to putting variable definitions in a named
593d3a34
MK
2112section, the section can also be shared among all running copies of an
2113executable or DLL. For example, this small program defines shared data
2114by putting it in a named section "shared" and marking the section
2115shareable:
2116
2117@smallexample
2118int foo __attribute__((section ("shared"), shared)) = 0;
2119
2120int
2121main()
2122@{
2123 /* Read and write foo. All running copies see the same value. */
2124 return 0;
2125@}
2126@end smallexample
2127
2128@noindent
2129You may only use the @code{shared} attribute along with @code{section}
2130attribute with a fully initialized global definition because of the way
2131linkers work. See @code{section} attribute for more information.
2132
2133The @code{shared} attribute is only available on Windows NT.
2134
c1f7febf
RK
2135@item transparent_union
2136This attribute, attached to a function parameter which is a union, means
2137that the corresponding argument may have the type of any union member,
2138but the argument is passed as if its type were that of the first union
2139member. For more details see @xref{Type Attributes}. You can also use
2140this attribute on a @code{typedef} for a union data type; then it
2141applies to all function parameters with that type.
2142
2143@item unused
2144This attribute, attached to a variable, means that the variable is meant
2145to be possibly unused. GNU CC will not produce a warning for this
2146variable.
2147
2148@item weak
2149The @code{weak} attribute is described in @xref{Function Attributes}.
845da534
DE
2150
2151@item model (@var{model-name})
2152@cindex variable addressability on the M32R/D
2153Use this attribute on the M32R/D to set the addressability of an object.
2154The identifier @var{model-name} is one of @code{small}, @code{medium},
2155or @code{large}, representing each of the code models.
2156
2157Small model objects live in the lower 16MB of memory (so that their
2158addresses can be loaded with the @code{ld24} instruction).
2159
2160Medium and large model objects may live anywhere in the 32 bit address space
2161(the compiler will generate @code{seth/add3} instructions to load their
2162addresses).
2163
c1f7febf
RK
2164@end table
2165
2166To specify multiple attributes, separate them by commas within the
2167double parentheses: for example, @samp{__attribute__ ((aligned (16),
2168packed))}.
2169
2170@node Type Attributes
2171@section Specifying Attributes of Types
2172@cindex attribute of types
2173@cindex type attributes
2174
2175The keyword @code{__attribute__} allows you to specify special
2176attributes of @code{struct} and @code{union} types when you define such
2177types. This keyword is followed by an attribute specification inside
2178double parentheses. Three attributes are currently defined for types:
2179@code{aligned}, @code{packed}, and @code{transparent_union}. Other
2180attributes are defined for functions (@pxref{Function Attributes}) and
2181for variables (@pxref{Variable Attributes}).
2182
2183You may also specify any one of these attributes with @samp{__}
2184preceding and following its keyword. This allows you to use these
2185attributes in header files without being concerned about a possible
2186macro of the same name. For example, you may use @code{__aligned__}
2187instead of @code{aligned}.
2188
2189You may specify the @code{aligned} and @code{transparent_union}
2190attributes either in a @code{typedef} declaration or just past the
2191closing curly brace of a complete enum, struct or union type
2192@emph{definition} and the @code{packed} attribute only past the closing
2193brace of a definition.
2194
4051959b
JM
2195You may also specify attributes between the enum, struct or union
2196tag and the name of the type rather than after the closing brace.
2197
c1f7febf
RK
2198@table @code
2199@cindex @code{aligned} attribute
2200@item aligned (@var{alignment})
2201This attribute specifies a minimum alignment (in bytes) for variables
2202of the specified type. For example, the declarations:
2203
2204@smallexample
f69eecfb
JL
2205struct S @{ short f[3]; @} __attribute__ ((aligned (8)));
2206typedef int more_aligned_int __attribute__ ((aligned (8)));
c1f7febf
RK
2207@end smallexample
2208
2209@noindent
d863830b 2210force the compiler to insure (as far as it can) that each variable whose
c1f7febf
RK
2211type is @code{struct S} or @code{more_aligned_int} will be allocated and
2212aligned @emph{at least} on a 8-byte boundary. On a Sparc, having all
2213variables of type @code{struct S} aligned to 8-byte boundaries allows
2214the compiler to use the @code{ldd} and @code{std} (doubleword load and
2215store) instructions when copying one variable of type @code{struct S} to
2216another, thus improving run-time efficiency.
2217
2218Note that the alignment of any given @code{struct} or @code{union} type
2219is required by the ANSI C standard to be at least a perfect multiple of
2220the lowest common multiple of the alignments of all of the members of
2221the @code{struct} or @code{union} in question. This means that you @emph{can}
2222effectively adjust the alignment of a @code{struct} or @code{union}
2223type by attaching an @code{aligned} attribute to any one of the members
2224of such a type, but the notation illustrated in the example above is a
2225more obvious, intuitive, and readable way to request the compiler to
2226adjust the alignment of an entire @code{struct} or @code{union} type.
2227
2228As in the preceding example, you can explicitly specify the alignment
2229(in bytes) that you wish the compiler to use for a given @code{struct}
2230or @code{union} type. Alternatively, you can leave out the alignment factor
2231and just ask the compiler to align a type to the maximum
2232useful alignment for the target machine you are compiling for. For
2233example, you could write:
2234
2235@smallexample
2236struct S @{ short f[3]; @} __attribute__ ((aligned));
2237@end smallexample
2238
2239Whenever you leave out the alignment factor in an @code{aligned}
2240attribute specification, the compiler automatically sets the alignment
2241for the type to the largest alignment which is ever used for any data
2242type on the target machine you are compiling for. Doing this can often
2243make copy operations more efficient, because the compiler can use
2244whatever instructions copy the biggest chunks of memory when performing
2245copies to or from the variables which have types that you have aligned
2246this way.
2247
2248In the example above, if the size of each @code{short} is 2 bytes, then
2249the size of the entire @code{struct S} type is 6 bytes. The smallest
2250power of two which is greater than or equal to that is 8, so the
2251compiler sets the alignment for the entire @code{struct S} type to 8
2252bytes.
2253
2254Note that although you can ask the compiler to select a time-efficient
2255alignment for a given type and then declare only individual stand-alone
2256objects of that type, the compiler's ability to select a time-efficient
2257alignment is primarily useful only when you plan to create arrays of
2258variables having the relevant (efficiently aligned) type. If you
2259declare or use arrays of variables of an efficiently-aligned type, then
2260it is likely that your program will also be doing pointer arithmetic (or
2261subscripting, which amounts to the same thing) on pointers to the
2262relevant type, and the code that the compiler generates for these
2263pointer arithmetic operations will often be more efficient for
2264efficiently-aligned types than for other types.
2265
2266The @code{aligned} attribute can only increase the alignment; but you
2267can decrease it by specifying @code{packed} as well. See below.
2268
2269Note that the effectiveness of @code{aligned} attributes may be limited
2270by inherent limitations in your linker. On many systems, the linker is
2271only able to arrange for variables to be aligned up to a certain maximum
2272alignment. (For some linkers, the maximum supported alignment may
2273be very very small.) If your linker is only able to align variables
2274up to a maximum of 8 byte alignment, then specifying @code{aligned(16)}
2275in an @code{__attribute__} will still only provide you with 8 byte
2276alignment. See your linker documentation for further information.
2277
2278@item packed
2279This attribute, attached to an @code{enum}, @code{struct}, or
2280@code{union} type definition, specified that the minimum required memory
2281be used to represent the type.
2282
2283Specifying this attribute for @code{struct} and @code{union} types is
2284equivalent to specifying the @code{packed} attribute on each of the
2285structure or union members. Specifying the @samp{-fshort-enums}
2286flag on the line is equivalent to specifying the @code{packed}
2287attribute on all @code{enum} definitions.
2288
2289You may only specify this attribute after a closing curly brace on an
1cd4bca9
BK
2290@code{enum} definition, not in a @code{typedef} declaration, unless that
2291declaration also contains the definition of the @code{enum}.
c1f7febf
RK
2292
2293@item transparent_union
2294This attribute, attached to a @code{union} type definition, indicates
2295that any function parameter having that union type causes calls to that
2296function to be treated in a special way.
2297
2298First, the argument corresponding to a transparent union type can be of
2299any type in the union; no cast is required. Also, if the union contains
2300a pointer type, the corresponding argument can be a null pointer
2301constant or a void pointer expression; and if the union contains a void
2302pointer type, the corresponding argument can be any pointer expression.
2303If the union member type is a pointer, qualifiers like @code{const} on
2304the referenced type must be respected, just as with normal pointer
2305conversions.
2306
2307Second, the argument is passed to the function using the calling
2308conventions of first member of the transparent union, not the calling
2309conventions of the union itself. All members of the union must have the
2310same machine representation; this is necessary for this argument passing
2311to work properly.
2312
2313Transparent unions are designed for library functions that have multiple
2314interfaces for compatibility reasons. For example, suppose the
2315@code{wait} function must accept either a value of type @code{int *} to
2316comply with Posix, or a value of type @code{union wait *} to comply with
2317the 4.1BSD interface. If @code{wait}'s parameter were @code{void *},
2318@code{wait} would accept both kinds of arguments, but it would also
2319accept any other pointer type and this would make argument type checking
2320less useful. Instead, @code{<sys/wait.h>} might define the interface
2321as follows:
2322
2323@smallexample
2324typedef union
2325 @{
2326 int *__ip;
2327 union wait *__up;
2328 @} wait_status_ptr_t __attribute__ ((__transparent_union__));
2329
2330pid_t wait (wait_status_ptr_t);
2331@end smallexample
2332
2333This interface allows either @code{int *} or @code{union wait *}
2334arguments to be passed, using the @code{int *} calling convention.
2335The program can call @code{wait} with arguments of either type:
2336
2337@example
2338int w1 () @{ int w; return wait (&w); @}
2339int w2 () @{ union wait w; return wait (&w); @}
2340@end example
2341
2342With this interface, @code{wait}'s implementation might look like this:
2343
2344@example
2345pid_t wait (wait_status_ptr_t p)
2346@{
2347 return waitpid (-1, p.__ip, 0);
2348@}
2349@end example
d863830b
JL
2350
2351@item unused
2352When attached to a type (including a @code{union} or a @code{struct}),
2353this attribute means that variables of that type are meant to appear
2354possibly unused. GNU CC will not produce a warning for any variables of
2355that type, even if the variable appears to do nothing. This is often
2356the case with lock or thread classes, which are usually defined and then
2357not referenced, but contain constructors and destructors that have
956d6950 2358nontrivial bookkeeping functions.
d863830b 2359
c1f7febf
RK
2360@end table
2361
2362To specify multiple attributes, separate them by commas within the
2363double parentheses: for example, @samp{__attribute__ ((aligned (16),
2364packed))}.
2365
2366@node Inline
2367@section An Inline Function is As Fast As a Macro
2368@cindex inline functions
2369@cindex integrating function code
2370@cindex open coding
2371@cindex macros, inline alternative
2372
2373By declaring a function @code{inline}, you can direct GNU CC to
2374integrate that function's code into the code for its callers. This
2375makes execution faster by eliminating the function-call overhead; in
2376addition, if any of the actual argument values are constant, their known
2377values may permit simplifications at compile time so that not all of the
2378inline function's code needs to be included. The effect on code size is
2379less predictable; object code may be larger or smaller with function
2380inlining, depending on the particular case. Inlining of functions is an
2381optimization and it really ``works'' only in optimizing compilation. If
2382you don't use @samp{-O}, no function is really inline.
2383
2384To declare a function inline, use the @code{inline} keyword in its
2385declaration, like this:
2386
2387@example
2388inline int
2389inc (int *a)
2390@{
2391 (*a)++;
2392@}
2393@end example
2394
2395(If you are writing a header file to be included in ANSI C programs, write
2396@code{__inline__} instead of @code{inline}. @xref{Alternate Keywords}.)
c1f7febf 2397You can also make all ``simple enough'' functions inline with the option
247b14bd
RH
2398@samp{-finline-functions}.
2399
2400Note that certain usages in a function definition can make it unsuitable
2401for inline substitution. Among these usages are: use of varargs, use of
2402alloca, use of variable sized data types (@pxref{Variable Length}),
2403use of computed goto (@pxref{Labels as Values}), use of nonlocal goto,
2404and nested functions (@pxref{Nested Functions}). Using @samp{-Winline}
2405will warn when a function marked @code{inline} could not be substituted,
2406and will give the reason for the failure.
c1f7febf
RK
2407
2408Note that in C and Objective C, unlike C++, the @code{inline} keyword
2409does not affect the linkage of the function.
2410
2411@cindex automatic @code{inline} for C++ member fns
2412@cindex @code{inline} automatic for C++ member fns
2413@cindex member fns, automatically @code{inline}
2414@cindex C++ member fns, automatically @code{inline}
2415GNU CC automatically inlines member functions defined within the class
2416body of C++ programs even if they are not explicitly declared
2417@code{inline}. (You can override this with @samp{-fno-default-inline};
2418@pxref{C++ Dialect Options,,Options Controlling C++ Dialect}.)
2419
2420@cindex inline functions, omission of
2421When a function is both inline and @code{static}, if all calls to the
2422function are integrated into the caller, and the function's address is
2423never used, then the function's own assembler code is never referenced.
2424In this case, GNU CC does not actually output assembler code for the
2425function, unless you specify the option @samp{-fkeep-inline-functions}.
2426Some calls cannot be integrated for various reasons (in particular,
2427calls that precede the function's definition cannot be integrated, and
2428neither can recursive calls within the definition). If there is a
2429nonintegrated call, then the function is compiled to assembler code as
2430usual. The function must also be compiled as usual if the program
2431refers to its address, because that can't be inlined.
2432
2433@cindex non-static inline function
2434When an inline function is not @code{static}, then the compiler must assume
2435that there may be calls from other source files; since a global symbol can
2436be defined only once in any program, the function must not be defined in
2437the other source files, so the calls therein cannot be integrated.
2438Therefore, a non-@code{static} inline function is always compiled on its
2439own in the usual fashion.
2440
2441If you specify both @code{inline} and @code{extern} in the function
2442definition, then the definition is used only for inlining. In no case
2443is the function compiled on its own, not even if you refer to its
2444address explicitly. Such an address becomes an external reference, as
2445if you had only declared the function, and had not defined it.
2446
2447This combination of @code{inline} and @code{extern} has almost the
2448effect of a macro. The way to use it is to put a function definition in
2449a header file with these keywords, and put another copy of the
2450definition (lacking @code{inline} and @code{extern}) in a library file.
2451The definition in the header file will cause most calls to the function
2452to be inlined. If any uses of the function remain, they will refer to
2453the single copy in the library.
2454
2455GNU C does not inline any functions when not optimizing. It is not
2456clear whether it is better to inline or not, in this case, but we found
2457that a correct implementation when not optimizing was difficult. So we
2458did the easy thing, and turned it off.
2459
2460@node Extended Asm
2461@section Assembler Instructions with C Expression Operands
2462@cindex extended @code{asm}
2463@cindex @code{asm} expressions
2464@cindex assembler instructions
2465@cindex registers
2466
c85f7c16
JL
2467In an assembler instruction using @code{asm}, you can specify the
2468operands of the instruction using C expressions. This means you need not
2469guess which registers or memory locations will contain the data you want
c1f7febf
RK
2470to use.
2471
c85f7c16
JL
2472You must specify an assembler instruction template much like what
2473appears in a machine description, plus an operand constraint string for
2474each operand.
c1f7febf
RK
2475
2476For example, here is how to use the 68881's @code{fsinx} instruction:
2477
2478@example
2479asm ("fsinx %1,%0" : "=f" (result) : "f" (angle));
2480@end example
2481
2482@noindent
2483Here @code{angle} is the C expression for the input operand while
2484@code{result} is that of the output operand. Each has @samp{"f"} as its
c85f7c16
JL
2485operand constraint, saying that a floating point register is required.
2486The @samp{=} in @samp{=f} indicates that the operand is an output; all
2487output operands' constraints must use @samp{=}. The constraints use the
2488same language used in the machine description (@pxref{Constraints}).
2489
2490Each operand is described by an operand-constraint string followed by
2491the C expression in parentheses. A colon separates the assembler
2492template from the first output operand and another separates the last
2493output operand from the first input, if any. Commas separate the
2494operands within each group. The total number of operands is limited to
2495ten or to the maximum number of operands in any instruction pattern in
2496the machine description, whichever is greater.
2497
2498If there are no output operands but there are input operands, you must
2499place two consecutive colons surrounding the place where the output
c1f7febf
RK
2500operands would go.
2501
2502Output operand expressions must be lvalues; the compiler can check this.
c85f7c16
JL
2503The input operands need not be lvalues. The compiler cannot check
2504whether the operands have data types that are reasonable for the
2505instruction being executed. It does not parse the assembler instruction
2506template and does not know what it means or even whether it is valid
2507assembler input. The extended @code{asm} feature is most often used for
2508machine instructions the compiler itself does not know exist. If
2509the output expression cannot be directly addressed (for example, it is a
2510bit field), your constraint must allow a register. In that case, GNU CC
2511will use the register as the output of the @code{asm}, and then store
2512that register into the output.
2513
2514The ordinary output operands must be write-only; GNU CC will assume that
2515the values in these operands before the instruction are dead and need
2516not be generated. Extended asm supports input-output or read-write
2517operands. Use the constraint character @samp{+} to indicate such an
2518operand and list it with the output operands.
2519
2520When the constraints for the read-write operand (or the operand in which
2521only some of the bits are to be changed) allows a register, you may, as
2522an alternative, logically split its function into two separate operands,
2523one input operand and one write-only output operand. The connection
2524between them is expressed by constraints which say they need to be in
2525the same location when the instruction executes. You can use the same C
2526expression for both operands, or different expressions. For example,
2527here we write the (fictitious) @samp{combine} instruction with
2528@code{bar} as its read-only source operand and @code{foo} as its
2529read-write destination:
c1f7febf
RK
2530
2531@example
2532asm ("combine %2,%0" : "=r" (foo) : "0" (foo), "g" (bar));
2533@end example
2534
2535@noindent
c85f7c16
JL
2536The constraint @samp{"0"} for operand 1 says that it must occupy the
2537same location as operand 0. A digit in constraint is allowed only in an
2538input operand and it must refer to an output operand.
c1f7febf
RK
2539
2540Only a digit in the constraint can guarantee that one operand will be in
c85f7c16
JL
2541the same place as another. The mere fact that @code{foo} is the value
2542of both operands is not enough to guarantee that they will be in the
2543same place in the generated assembler code. The following would not
2544work reliably:
c1f7febf
RK
2545
2546@example
2547asm ("combine %2,%0" : "=r" (foo) : "r" (foo), "g" (bar));
2548@end example
2549
2550Various optimizations or reloading could cause operands 0 and 1 to be in
2551different registers; GNU CC knows no reason not to do so. For example, the
2552compiler might find a copy of the value of @code{foo} in one register and
2553use it for operand 1, but generate the output operand 0 in a different
2554register (copying it afterward to @code{foo}'s own address). Of course,
2555since the register for operand 1 is not even mentioned in the assembler
2556code, the result will not work, but GNU CC can't tell that.
2557
c85f7c16
JL
2558Some instructions clobber specific hard registers. To describe this,
2559write a third colon after the input operands, followed by the names of
2560the clobbered hard registers (given as strings). Here is a realistic
2561example for the VAX:
c1f7febf
RK
2562
2563@example
2564asm volatile ("movc3 %0,%1,%2"
2565 : /* no outputs */
2566 : "g" (from), "g" (to), "g" (count)
2567 : "r0", "r1", "r2", "r3", "r4", "r5");
2568@end example
2569
c5c76735
JL
2570You may not write a clobber description in a way that overlaps with an
2571input or output operand. For example, you may not have an operand
2572describing a register class with one member if you mention that register
2573in the clobber list. There is no way for you to specify that an input
2574operand is modified without also specifying it as an output
2575operand. Note that if all the output operands you specify are for this
2576purpose (and hence unused), you will then also need to specify
2577@code{volatile} for the @code{asm} construct, as described below, to
2578prevent GNU CC from deleting the @code{asm} statement as unused.
8fe1938e 2579
c1f7febf 2580If you refer to a particular hardware register from the assembler code,
c85f7c16
JL
2581you will probably have to list the register after the third colon to
2582tell the compiler the register's value is modified. In some assemblers,
2583the register names begin with @samp{%}; to produce one @samp{%} in the
2584assembler code, you must write @samp{%%} in the input.
2585
2586If your assembler instruction can alter the condition code register, add
2587@samp{cc} to the list of clobbered registers. GNU CC on some machines
2588represents the condition codes as a specific hardware register;
2589@samp{cc} serves to name this register. On other machines, the
2590condition code is handled differently, and specifying @samp{cc} has no
2591effect. But it is valid no matter what the machine.
c1f7febf
RK
2592
2593If your assembler instruction modifies memory in an unpredictable
c85f7c16
JL
2594fashion, add @samp{memory} to the list of clobbered registers. This
2595will cause GNU CC to not keep memory values cached in registers across
dd40655a
GK
2596the assembler instruction. You will also want to add the
2597@code{volatile} keyword if the memory affected is not listed in the
2598inputs or outputs of the @code{asm}, as the @samp{memory} clobber does
2599not count as a side-effect of the @code{asm}.
c1f7febf 2600
c85f7c16
JL
2601You can put multiple assembler instructions together in a single
2602@code{asm} template, separated either with newlines (written as
2603@samp{\n}) or with semicolons if the assembler allows such semicolons.
2604The GNU assembler allows semicolons and most Unix assemblers seem to do
2605so. The input operands are guaranteed not to use any of the clobbered
2606registers, and neither will the output operands' addresses, so you can
2607read and write the clobbered registers as many times as you like. Here
2608is an example of multiple instructions in a template; it assumes the
2609subroutine @code{_foo} accepts arguments in registers 9 and 10:
c1f7febf
RK
2610
2611@example
2612asm ("movl %0,r9;movl %1,r10;call _foo"
2613 : /* no outputs */
2614 : "g" (from), "g" (to)
2615 : "r9", "r10");
2616@end example
2617
c85f7c16
JL
2618Unless an output operand has the @samp{&} constraint modifier, GNU CC
2619may allocate it in the same register as an unrelated input operand, on
2620the assumption the inputs are consumed before the outputs are produced.
c1f7febf
RK
2621This assumption may be false if the assembler code actually consists of
2622more than one instruction. In such a case, use @samp{&} for each output
c85f7c16 2623operand that may not overlap an input. @xref{Modifiers}.
c1f7febf 2624
c85f7c16
JL
2625If you want to test the condition code produced by an assembler
2626instruction, you must include a branch and a label in the @code{asm}
2627construct, as follows:
c1f7febf
RK
2628
2629@example
2630asm ("clr %0;frob %1;beq 0f;mov #1,%0;0:"
2631 : "g" (result)
2632 : "g" (input));
2633@end example
2634
2635@noindent
2636This assumes your assembler supports local labels, as the GNU assembler
2637and most Unix assemblers do.
2638
2639Speaking of labels, jumps from one @code{asm} to another are not
c85f7c16
JL
2640supported. The compiler's optimizers do not know about these jumps, and
2641therefore they cannot take account of them when deciding how to
c1f7febf
RK
2642optimize.
2643
2644@cindex macros containing @code{asm}
2645Usually the most convenient way to use these @code{asm} instructions is to
2646encapsulate them in macros that look like functions. For example,
2647
2648@example
2649#define sin(x) \
2650(@{ double __value, __arg = (x); \
2651 asm ("fsinx %1,%0": "=f" (__value): "f" (__arg)); \
2652 __value; @})
2653@end example
2654
2655@noindent
2656Here the variable @code{__arg} is used to make sure that the instruction
2657operates on a proper @code{double} value, and to accept only those
2658arguments @code{x} which can convert automatically to a @code{double}.
2659
c85f7c16
JL
2660Another way to make sure the instruction operates on the correct data
2661type is to use a cast in the @code{asm}. This is different from using a
c1f7febf
RK
2662variable @code{__arg} in that it converts more different types. For
2663example, if the desired type were @code{int}, casting the argument to
2664@code{int} would accept a pointer with no complaint, while assigning the
2665argument to an @code{int} variable named @code{__arg} would warn about
2666using a pointer unless the caller explicitly casts it.
2667
2668If an @code{asm} has output operands, GNU CC assumes for optimization
c85f7c16
JL
2669purposes the instruction has no side effects except to change the output
2670operands. This does not mean instructions with a side effect cannot be
2671used, but you must be careful, because the compiler may eliminate them
2672if the output operands aren't used, or move them out of loops, or
2673replace two with one if they constitute a common subexpression. Also,
2674if your instruction does have a side effect on a variable that otherwise
2675appears not to change, the old value of the variable may be reused later
2676if it happens to be found in a register.
c1f7febf
RK
2677
2678You can prevent an @code{asm} instruction from being deleted, moved
2679significantly, or combined, by writing the keyword @code{volatile} after
2680the @code{asm}. For example:
2681
2682@example
c85f7c16
JL
2683#define get_and_set_priority(new) \
2684(@{ int __old; \
2685 asm volatile ("get_and_set_priority %0, %1": "=g" (__old) : "g" (new)); \
2686 __old; @})
24f98470 2687@end example
c1f7febf
RK
2688
2689@noindent
c85f7c16
JL
2690If you write an @code{asm} instruction with no outputs, GNU CC will know
2691the instruction has side-effects and will not delete the instruction or
2692move it outside of loops. If the side-effects of your instruction are
2693not purely external, but will affect variables in your program in ways
2694other than reading the inputs and clobbering the specified registers or
2695memory, you should write the @code{volatile} keyword to prevent future
2696versions of GNU CC from moving the instruction around within a core
2697region.
2698
2699An @code{asm} instruction without any operands or clobbers (and ``old
2700style'' @code{asm}) will not be deleted or moved significantly,
2701regardless, unless it is unreachable, the same wasy as if you had
2702written a @code{volatile} keyword.
c1f7febf
RK
2703
2704Note that even a volatile @code{asm} instruction can be moved in ways
2705that appear insignificant to the compiler, such as across jump
2706instructions. You can't expect a sequence of volatile @code{asm}
2707instructions to remain perfectly consecutive. If you want consecutive
2708output, use a single @code{asm}.
2709
2710It is a natural idea to look for a way to give access to the condition
2711code left by the assembler instruction. However, when we attempted to
2712implement this, we found no way to make it work reliably. The problem
2713is that output operands might need reloading, which would result in
2714additional following ``store'' instructions. On most machines, these
2715instructions would alter the condition code before there was time to
2716test it. This problem doesn't arise for ordinary ``test'' and
2717``compare'' instructions because they don't have any output operands.
2718
eda3fbbe
GB
2719For reasons similar to those described above, it is not possible to give
2720an assembler instruction access to the condition code left by previous
2721instructions.
2722
c1f7febf
RK
2723If you are writing a header file that should be includable in ANSI C
2724programs, write @code{__asm__} instead of @code{asm}. @xref{Alternate
2725Keywords}.
2726
fe0ce426
JH
2727@subsection i386 floating point asm operands
2728
2729There are several rules on the usage of stack-like regs in
2730asm_operands insns. These rules apply only to the operands that are
2731stack-like regs:
2732
2733@enumerate
2734@item
2735Given a set of input regs that die in an asm_operands, it is
2736necessary to know which are implicitly popped by the asm, and
2737which must be explicitly popped by gcc.
2738
2739An input reg that is implicitly popped by the asm must be
2740explicitly clobbered, unless it is constrained to match an
2741output operand.
2742
2743@item
2744For any input reg that is implicitly popped by an asm, it is
2745necessary to know how to adjust the stack to compensate for the pop.
2746If any non-popped input is closer to the top of the reg-stack than
2747the implicitly popped reg, it would not be possible to know what the
2748stack looked like --- it's not clear how the rest of the stack ``slides
2749up''.
2750
2751All implicitly popped input regs must be closer to the top of
2752the reg-stack than any input that is not implicitly popped.
2753
2754It is possible that if an input dies in an insn, reload might
2755use the input reg for an output reload. Consider this example:
2756
2757@example
2758asm ("foo" : "=t" (a) : "f" (b));
2759@end example
2760
2761This asm says that input B is not popped by the asm, and that
2762the asm pushes a result onto the reg-stack, ie, the stack is one
2763deeper after the asm than it was before. But, it is possible that
2764reload will think that it can use the same reg for both the input and
2765the output, if input B dies in this insn.
2766
2767If any input operand uses the @code{f} constraint, all output reg
2768constraints must use the @code{&} earlyclobber.
2769
2770The asm above would be written as
2771
2772@example
2773asm ("foo" : "=&t" (a) : "f" (b));
2774@end example
2775
2776@item
2777Some operands need to be in particular places on the stack. All
2778output operands fall in this category --- there is no other way to
2779know which regs the outputs appear in unless the user indicates
2780this in the constraints.
2781
2782Output operands must specifically indicate which reg an output
2783appears in after an asm. @code{=f} is not allowed: the operand
2784constraints must select a class with a single reg.
2785
2786@item
2787Output operands may not be ``inserted'' between existing stack regs.
2788Since no 387 opcode uses a read/write operand, all output operands
2789are dead before the asm_operands, and are pushed by the asm_operands.
2790It makes no sense to push anywhere but the top of the reg-stack.
2791
2792Output operands must start at the top of the reg-stack: output
2793operands may not ``skip'' a reg.
2794
2795@item
2796Some asm statements may need extra stack space for internal
2797calculations. This can be guaranteed by clobbering stack registers
2798unrelated to the inputs and outputs.
2799
2800@end enumerate
2801
2802Here are a couple of reasonable asms to want to write. This asm
2803takes one input, which is internally popped, and produces two outputs.
2804
2805@example
2806asm ("fsincos" : "=t" (cos), "=u" (sin) : "0" (inp));
2807@end example
2808
2809This asm takes two inputs, which are popped by the @code{fyl2xp1} opcode,
2810and replaces them with one output. The user must code the @code{st(1)}
2811clobber for reg-stack.c to know that @code{fyl2xp1} pops both inputs.
2812
2813@example
2814asm ("fyl2xp1" : "=t" (result) : "0" (x), "u" (y) : "st(1)");
2815@end example
2816
c1f7febf
RK
2817@ifclear INTERNALS
2818@c Show the details on constraints if they do not appear elsewhere in
2819@c the manual
2820@include md.texi
2821@end ifclear
2822
2823@node Asm Labels
2824@section Controlling Names Used in Assembler Code
2825@cindex assembler names for identifiers
2826@cindex names used in assembler code
2827@cindex identifiers, names in assembler code
2828
2829You can specify the name to be used in the assembler code for a C
2830function or variable by writing the @code{asm} (or @code{__asm__})
2831keyword after the declarator as follows:
2832
2833@example
2834int foo asm ("myfoo") = 2;
2835@end example
2836
2837@noindent
2838This specifies that the name to be used for the variable @code{foo} in
2839the assembler code should be @samp{myfoo} rather than the usual
2840@samp{_foo}.
2841
2842On systems where an underscore is normally prepended to the name of a C
2843function or variable, this feature allows you to define names for the
2844linker that do not start with an underscore.
2845
2846You cannot use @code{asm} in this way in a function @emph{definition}; but
2847you can get the same effect by writing a declaration for the function
2848before its definition and putting @code{asm} there, like this:
2849
2850@example
2851extern func () asm ("FUNC");
2852
2853func (x, y)
2854 int x, y;
2855@dots{}
2856@end example
2857
2858It is up to you to make sure that the assembler names you choose do not
2859conflict with any other assembler symbols. Also, you must not use a
2860register name; that would produce completely invalid assembler code. GNU
2861CC does not as yet have the ability to store static variables in registers.
2862Perhaps that will be added.
2863
2864@node Explicit Reg Vars
2865@section Variables in Specified Registers
2866@cindex explicit register variables
2867@cindex variables in specified registers
2868@cindex specified registers
2869@cindex registers, global allocation
2870
2871GNU C allows you to put a few global variables into specified hardware
2872registers. You can also specify the register in which an ordinary
2873register variable should be allocated.
2874
2875@itemize @bullet
2876@item
2877Global register variables reserve registers throughout the program.
2878This may be useful in programs such as programming language
2879interpreters which have a couple of global variables that are accessed
2880very often.
2881
2882@item
2883Local register variables in specific registers do not reserve the
2884registers. The compiler's data flow analysis is capable of determining
2885where the specified registers contain live values, and where they are
8d344fbc 2886available for other uses. Stores into local register variables may be deleted
0deaf590
JL
2887when they appear to be dead according to dataflow analysis. References
2888to local register variables may be deleted or moved or simplified.
c1f7febf
RK
2889
2890These local variables are sometimes convenient for use with the extended
2891@code{asm} feature (@pxref{Extended Asm}), if you want to write one
2892output of the assembler instruction directly into a particular register.
2893(This will work provided the register you specify fits the constraints
2894specified for that operand in the @code{asm}.)
2895@end itemize
2896
2897@menu
2898* Global Reg Vars::
2899* Local Reg Vars::
2900@end menu
2901
2902@node Global Reg Vars
2903@subsection Defining Global Register Variables
2904@cindex global register variables
2905@cindex registers, global variables in
2906
2907You can define a global register variable in GNU C like this:
2908
2909@example
2910register int *foo asm ("a5");
2911@end example
2912
2913@noindent
2914Here @code{a5} is the name of the register which should be used. Choose a
2915register which is normally saved and restored by function calls on your
2916machine, so that library routines will not clobber it.
2917
2918Naturally the register name is cpu-dependent, so you would need to
2919conditionalize your program according to cpu type. The register
2920@code{a5} would be a good choice on a 68000 for a variable of pointer
2921type. On machines with register windows, be sure to choose a ``global''
2922register that is not affected magically by the function call mechanism.
2923
2924In addition, operating systems on one type of cpu may differ in how they
2925name the registers; then you would need additional conditionals. For
2926example, some 68000 operating systems call this register @code{%a5}.
2927
2928Eventually there may be a way of asking the compiler to choose a register
2929automatically, but first we need to figure out how it should choose and
2930how to enable you to guide the choice. No solution is evident.
2931
2932Defining a global register variable in a certain register reserves that
2933register entirely for this use, at least within the current compilation.
2934The register will not be allocated for any other purpose in the functions
2935in the current compilation. The register will not be saved and restored by
2936these functions. Stores into this register are never deleted even if they
2937would appear to be dead, but references may be deleted or moved or
2938simplified.
2939
2940It is not safe to access the global register variables from signal
2941handlers, or from more than one thread of control, because the system
2942library routines may temporarily use the register for other things (unless
2943you recompile them specially for the task at hand).
2944
2945@cindex @code{qsort}, and global register variables
2946It is not safe for one function that uses a global register variable to
2947call another such function @code{foo} by way of a third function
2948@code{lose} that was compiled without knowledge of this variable (i.e. in a
2949different source file in which the variable wasn't declared). This is
2950because @code{lose} might save the register and put some other value there.
2951For example, you can't expect a global register variable to be available in
2952the comparison-function that you pass to @code{qsort}, since @code{qsort}
2953might have put something else in that register. (If you are prepared to
2954recompile @code{qsort} with the same global register variable, you can
2955solve this problem.)
2956
2957If you want to recompile @code{qsort} or other source files which do not
2958actually use your global register variable, so that they will not use that
2959register for any other purpose, then it suffices to specify the compiler
2960option @samp{-ffixed-@var{reg}}. You need not actually add a global
2961register declaration to their source code.
2962
2963A function which can alter the value of a global register variable cannot
2964safely be called from a function compiled without this variable, because it
2965could clobber the value the caller expects to find there on return.
2966Therefore, the function which is the entry point into the part of the
2967program that uses the global register variable must explicitly save and
2968restore the value which belongs to its caller.
2969
2970@cindex register variable after @code{longjmp}
2971@cindex global register after @code{longjmp}
2972@cindex value after @code{longjmp}
2973@findex longjmp
2974@findex setjmp
2975On most machines, @code{longjmp} will restore to each global register
2976variable the value it had at the time of the @code{setjmp}. On some
2977machines, however, @code{longjmp} will not change the value of global
2978register variables. To be portable, the function that called @code{setjmp}
2979should make other arrangements to save the values of the global register
2980variables, and to restore them in a @code{longjmp}. This way, the same
2981thing will happen regardless of what @code{longjmp} does.
2982
2983All global register variable declarations must precede all function
2984definitions. If such a declaration could appear after function
2985definitions, the declaration would be too late to prevent the register from
2986being used for other purposes in the preceding functions.
2987
2988Global register variables may not have initial values, because an
2989executable file has no means to supply initial contents for a register.
2990
2991On the Sparc, there are reports that g3 @dots{} g7 are suitable
2992registers, but certain library functions, such as @code{getwd}, as well
2993as the subroutines for division and remainder, modify g3 and g4. g1 and
2994g2 are local temporaries.
2995
2996On the 68000, a2 @dots{} a5 should be suitable, as should d2 @dots{} d7.
2997Of course, it will not do to use more than a few of those.
2998
2999@node Local Reg Vars
3000@subsection Specifying Registers for Local Variables
3001@cindex local variables, specifying registers
3002@cindex specifying registers for local variables
3003@cindex registers for local variables
3004
3005You can define a local register variable with a specified register
3006like this:
3007
3008@example
3009register int *foo asm ("a5");
3010@end example
3011
3012@noindent
3013Here @code{a5} is the name of the register which should be used. Note
3014that this is the same syntax used for defining global register
3015variables, but for a local variable it would appear within a function.
3016
3017Naturally the register name is cpu-dependent, but this is not a
3018problem, since specific registers are most often useful with explicit
3019assembler instructions (@pxref{Extended Asm}). Both of these things
3020generally require that you conditionalize your program according to
3021cpu type.
3022
3023In addition, operating systems on one type of cpu may differ in how they
3024name the registers; then you would need additional conditionals. For
3025example, some 68000 operating systems call this register @code{%a5}.
3026
c1f7febf
RK
3027Defining such a register variable does not reserve the register; it
3028remains available for other uses in places where flow control determines
3029the variable's value is not live. However, these registers are made
e5e809f4
JL
3030unavailable for use in the reload pass; excessive use of this feature
3031leaves the compiler too few available registers to compile certain
3032functions.
3033
3034This option does not guarantee that GNU CC will generate code that has
3035this variable in the register you specify at all times. You may not
3036code an explicit reference to this register in an @code{asm} statement
3037and assume it will always refer to this variable.
c1f7febf 3038
8d344fbc 3039Stores into local register variables may be deleted when they appear to be dead
0deaf590
JL
3040according to dataflow analysis. References to local register variables may
3041be deleted or moved or simplified.
3042
c1f7febf
RK
3043@node Alternate Keywords
3044@section Alternate Keywords
3045@cindex alternate keywords
3046@cindex keywords, alternate
3047
3048The option @samp{-traditional} disables certain keywords; @samp{-ansi}
3049disables certain others. This causes trouble when you want to use GNU C
3050extensions, or ANSI C features, in a general-purpose header file that
3051should be usable by all programs, including ANSI C programs and traditional
3052ones. The keywords @code{asm}, @code{typeof} and @code{inline} cannot be
3053used since they won't work in a program compiled with @samp{-ansi}, while
3054the keywords @code{const}, @code{volatile}, @code{signed}, @code{typeof}
3055and @code{inline} won't work in a program compiled with
3056@samp{-traditional}.@refill
3057
3058The way to solve these problems is to put @samp{__} at the beginning and
3059end of each problematical keyword. For example, use @code{__asm__}
3060instead of @code{asm}, @code{__const__} instead of @code{const}, and
3061@code{__inline__} instead of @code{inline}.
3062
3063Other C compilers won't accept these alternative keywords; if you want to
3064compile with another compiler, you can define the alternate keywords as
3065macros to replace them with the customary keywords. It looks like this:
3066
3067@example
3068#ifndef __GNUC__
3069#define __asm__ asm
3070#endif
3071@end example
3072
6e6b0525 3073@findex __extension__
f1b5ff21 3074@samp{-pedantic} and other options cause warnings for many GNU C extensions.
dbe519e0 3075You can
c1f7febf
RK
3076prevent such warnings within one expression by writing
3077@code{__extension__} before the expression. @code{__extension__} has no
3078effect aside from this.
3079
3080@node Incomplete Enums
3081@section Incomplete @code{enum} Types
3082
3083You can define an @code{enum} tag without specifying its possible values.
3084This results in an incomplete type, much like what you get if you write
3085@code{struct foo} without describing the elements. A later declaration
3086which does specify the possible values completes the type.
3087
3088You can't allocate variables or storage using the type while it is
3089incomplete. However, you can work with pointers to that type.
3090
3091This extension may not be very useful, but it makes the handling of
3092@code{enum} more consistent with the way @code{struct} and @code{union}
3093are handled.
3094
3095This extension is not supported by GNU C++.
3096
3097@node Function Names
3098@section Function Names as Strings
3099
22acfb79
NM
3100GNU CC predefines two magic identifiers to hold the name of the current
3101function. The identifier @code{__FUNCTION__} holds the name of the function
3102as it appears in the source. The identifier @code{__PRETTY_FUNCTION__}
3103holds the name of the function pretty printed in a language specific
3104fashion.
c1f7febf
RK
3105
3106These names are always the same in a C function, but in a C++ function
3107they may be different. For example, this program:
3108
3109@smallexample
3110extern "C" @{
3111extern int printf (char *, ...);
3112@}
3113
3114class a @{
3115 public:
3116 sub (int i)
3117 @{
3118 printf ("__FUNCTION__ = %s\n", __FUNCTION__);
3119 printf ("__PRETTY_FUNCTION__ = %s\n", __PRETTY_FUNCTION__);
3120 @}
3121@};
3122
3123int
3124main (void)
3125@{
3126 a ax;
3127 ax.sub (0);
3128 return 0;
3129@}
3130@end smallexample
3131
3132@noindent
3133gives this output:
3134
3135@smallexample
3136__FUNCTION__ = sub
3137__PRETTY_FUNCTION__ = int a::sub (int)
3138@end smallexample
3139
22acfb79
NM
3140The compiler automagically replaces the identifiers with a string
3141literal containing the appropriate name. Thus, they are neither
3142preprocessor macros, like @code{__FILE__} and @code{__LINE__}, nor
3143variables. This means that they catenate with other string literals, and
3144that they can be used to initialize char arrays. For example
3145
3146@smallexample
3147char here[] = "Function " __FUNCTION__ " in " __FILE__;
3148@end smallexample
3149
3150On the other hand, @samp{#ifdef __FUNCTION__} does not have any special
c1f7febf
RK
3151meaning inside a function, since the preprocessor does not do anything
3152special with the identifier @code{__FUNCTION__}.
3153
22acfb79 3154GNU CC also supports the magic word @code{__func__}, defined by the
34527c47 3155ISO standard C-99:
22acfb79
NM
3156
3157@display
3158The identifier @code{__func__} is implicitly declared by the translator
3159as if, immediately following the opening brace of each function
3160definition, the declaration
3161
3162@smallexample
3163static const char __func__[] = "function-name";
3164@end smallexample
3165
3166appeared, where function-name is the name of the lexically-enclosing
3167function. This name is the unadorned name of the function.
3168@end display
3169
3170By this definition, @code{__func__} is a variable, not a string literal.
3171In particular, @code{__func__} does not catenate with other string
3172literals.
3173
3174In @code{C++}, @code{__FUNCTION__} and @code{__PRETTY_FUNCTION__} are
3175variables, declared in the same way as @code{__func__}.
3176
c1f7febf
RK
3177@node Return Address
3178@section Getting the Return or Frame Address of a Function
3179
3180These functions may be used to get information about the callers of a
3181function.
3182
3183@table @code
185ebd6c 3184@findex __builtin_return_address
c1f7febf
RK
3185@item __builtin_return_address (@var{level})
3186This function returns the return address of the current function, or of
3187one of its callers. The @var{level} argument is number of frames to
3188scan up the call stack. A value of @code{0} yields the return address
3189of the current function, a value of @code{1} yields the return address
3190of the caller of the current function, and so forth.
3191
3192The @var{level} argument must be a constant integer.
3193
3194On some machines it may be impossible to determine the return address of
3195any function other than the current one; in such cases, or when the top
3196of the stack has been reached, this function will return @code{0}.
3197
3198This function should only be used with a non-zero argument for debugging
3199purposes.
3200
185ebd6c 3201@findex __builtin_frame_address
c1f7febf
RK
3202@item __builtin_frame_address (@var{level})
3203This function is similar to @code{__builtin_return_address}, but it
3204returns the address of the function frame rather than the return address
3205of the function. Calling @code{__builtin_frame_address} with a value of
3206@code{0} yields the frame address of the current function, a value of
3207@code{1} yields the frame address of the caller of the current function,
3208and so forth.
3209
3210The frame is the area on the stack which holds local variables and saved
3211registers. The frame address is normally the address of the first word
3212pushed on to the stack by the function. However, the exact definition
3213depends upon the processor and the calling convention. If the processor
3214has a dedicated frame pointer register, and the function has a frame,
3215then @code{__builtin_frame_address} will return the value of the frame
3216pointer register.
3217
3218The caveats that apply to @code{__builtin_return_address} apply to this
3219function as well.
3220@end table
3221
185ebd6c
RH
3222@node Other Builtins
3223@section Other built-in functions provided by GNU CC
3224
3225GNU CC provides a large number of built-in functions other than the ones
3226mentioned above. Some of these are for internal use in the processing
3227of exceptions or variable-length argument lists and will not be
3228documented here because they may change from time to time; we do not
3229recommend general use of these functions.
3230
3231The remaining functions are provided for optimization purposes.
3232
3233GNU CC includes builtin versions of many of the functions in the
3234standard C library. These will always be treated as having the same
3235meaning as the C library function even if you specify the
3236@samp{-fno-builtin} (@pxref{C Dialect Options}) option. These functions
349c29b5
HPN
3237correspond to the C library functions @code{abort}, @code{abs},
3238@code{alloca}, @code{cos}, @code{cosf}, @code{cosl}, @code{exit},
3239@code{_exit}, @code{fabs}, @code{fabsf}, @code{fabsl}, @code{ffs},
3240@code{labs}, @code{memcmp}, @code{memcpy}, @code{memset}, @code{sin},
3241@code{sinf}, @code{sinl}, @code{sqrt}, @code{sqrtf}, @code{sqrtl},
3242@code{strcmp}, @code{strcpy}, and @code{strlen}.
185ebd6c 3243
994a57cd 3244@table @code
185ebd6c 3245@findex __builtin_constant_p
994a57cd 3246@item __builtin_constant_p (@var{exp})
185ebd6c
RH
3247You can use the builtin function @code{__builtin_constant_p} to
3248determine if a value is known to be constant at compile-time and hence
3249that GNU CC can perform constant-folding on expressions involving that
3250value. The argument of the function is the value to test. The function
3251returns the integer 1 if the argument is known to be a compile-time
3252constant and 0 if it is not known to be a compile-time constant. A
3253return of 0 does not indicate that the value is @emph{not} a constant,
3254but merely that GNU CC cannot prove it is a constant with the specified
3255value of the @samp{-O} option.
3256
3257You would typically use this function in an embedded application where
3258memory was a critical resource. If you have some complex calculation,
3259you may want it to be folded if it involves constants, but need to call
3260a function if it does not. For example:
3261
4d390518 3262@smallexample
185ebd6c
RH
3263#define Scale_Value(X) \
3264 (__builtin_constant_p (X) ? ((X) * SCALE + OFFSET) : Scale (X))
3265@end smallexample
3266
3267You may use this builtin function in either a macro or an inline
3268function. However, if you use it in an inlined function and pass an
3269argument of the function as the argument to the builtin, GNU CC will
3270never return 1 when you call the inline function with a string constant
3271or constructor expression (@pxref{Constructors}) and will not return 1
3272when you pass a constant numeric value to the inline function unless you
3273specify the @samp{-O} option.
3274
994a57cd
RH
3275@findex __builtin_expect
3276@item __builtin_expect(@var{exp}, @var{c})
3277You may use @code{__builtin_expect} to provide the compiler with
3278branch prediction information. In general, you should prefer to
3279use actual profile feedback for this (@samp{-fprofile-arcs}), as
3280programmers are notoriously bad at predicting how their programs
60b6e1f5 3281actually perform. However, there are applications in which this
994a57cd
RH
3282data is hard to collect.
3283
3284The return value is the value of @var{exp}, which should be an
3285integral expression. The value of @var{c} must be a compile-time
3286constant. The semantics of the builtin are that it is expected
3287that @var{exp} == @var{c}. For example:
3288
3289@smallexample
3290if (__builtin_expect (x, 0))
3291 foo ();
3292@end smallexample
3293
3294@noindent
3295would indicate that we do not expect to call @code{foo}, since
3296we expect @code{x} to be zero. Since you are limited to integral
3297expressions for @var{exp}, you should use constructions such as
3298
3299@smallexample
3300if (__builtin_expect (ptr != NULL, 1))
3301 error ();
3302@end smallexample
3303
3304@noindent
3305when testing pointer or floating-point values.
3306@end table
3307
2de45c06
ML
3308@node Deprecated Features
3309@section Deprecated Features
3310
3311In the past, the GNU C++ compiler was extended to experiment with new
3312features, at a time when the C++ language was still evolving. Now that
4a21803f 3313the C++ standard is complete, some of those features are superseded by
2de45c06
ML
3314superior alternatives. Using the old features might cause a warning in
3315some cases that the feature will be dropped in the future. In other
3316cases, the feature might be gone already.
3317
3318While the list below is not exhaustive, it documents some of the options
3319that are now deprecated:
3320
69942c20 3321@table @code
2de45c06
ML
3322@item -fexternal-templates
3323@itemx -falt-external-templates
3324These are two of the many ways for g++ to implement template
3325instantiation. @xref{Template Instantiation}. The C++ standard clearly
3326defines how template definitions have to be organized across
3327implementation units. g++ has an implicit instantiation mechanism that
3328should work just fine for standard-conforming code.
3329
7b300d13
NS
3330@item -fstrict-prototype
3331@itemx -fno-strict-prototype
3332Previously it was possible to use an empty prototype parameter list to
3333indicate an unspecified number of parameters (like C), rather than no
3334parameters, as C++ demands. This feature has been removed, except where
69fe169e 3335it is required for backwards compatibility @xref{Backwards Compatibility}.
7b300d13
NS
3336@end table
3337
44835fdd
MM
3338The named return value extension has been deprecated, and will be
3339removed from g++ at some point.
3340
7b300d13
NS
3341@node Backwards Compatibility
3342@section Backwards Compatibility
3343@cindex Backwards Compatibility
3344@cindex ARM
3345
3346Now that there is a definitive ISO standard C++, g++ has a specification
3347to adhere to. The C++ language evolved over time, and features that used
3348to be acceptable in previous drafts of the standard, such as the ARM, are
3349no longer accepted. In order to allow compilation of C++ written to such
3350drafts, g++ contains some backwards compatibilities. @emph{All such
3351backwards compatibility features are liable to disappear in future
3352versions of g++.} They should be considered deprecated @xref{Deprecated
3353Features}.
3354
3355@table @code
3356@item For scope
3357If a variable is declared at for scope, it used to remain in scope until
3358the end of the scope which contained the for statement (rather than just
3359within the for scope). g++ retains this, but issues a warning, if such a
3360variable is accessed outside the for scope.
3361
3362@item implicit C language
3363Old C system header files did not contain an @code{extern "C" @{...@}}
3364scope to set the language. On such systems, all header files are
3365implicitly scoped inside a C language scope. Also, an empty prototype
3366@code{()} will be treated as an unspecified number of arguments, rather
3367than no arguments, as C++ demands.
2de45c06
ML
3368@end table
3369
c1f7febf
RK
3370@node C++ Extensions
3371@chapter Extensions to the C++ Language
3372@cindex extensions, C++ language
3373@cindex C++ language extensions
3374
3375The GNU compiler provides these extensions to the C++ language (and you
3376can also use most of the C language extensions in your C++ programs). If you
3377want to write code that checks whether these features are available, you can
3378test for the GNU compiler the same way as for C programs: check for a
3379predefined macro @code{__GNUC__}. You can also use @code{__GNUG__} to
3380test specifically for GNU C++ (@pxref{Standard Predefined,,Standard
3381Predefined Macros,cpp.info,The C Preprocessor}).
3382
3383@menu
c1f7febf 3384* Min and Max:: C++ Minimum and maximum operators.
02cac427 3385* Volatiles:: What constitutes an access to a volatile object.
49419c8f 3386* Restricted Pointers:: C99 restricted pointers and references.
c1f7febf
RK
3387* C++ Interface:: You can use a single C++ header file for both
3388 declarations and definitions.
3389* Template Instantiation:: Methods for ensuring that exactly one copy of
3390 each needed template instantiation is emitted.
0ded1f18
JM
3391* Bound member functions:: You can extract a function pointer to the
3392 method denoted by a @samp{->*} or @samp{.*} expression.
c1f7febf
RK
3393@end menu
3394
c1f7febf
RK
3395@node Min and Max
3396@section Minimum and Maximum Operators in C++
3397
3398It is very convenient to have operators which return the ``minimum'' or the
3399``maximum'' of two arguments. In GNU C++ (but not in GNU C),
3400
3401@table @code
3402@item @var{a} <? @var{b}
3403@findex <?
3404@cindex minimum operator
3405is the @dfn{minimum}, returning the smaller of the numeric values
3406@var{a} and @var{b};
3407
3408@item @var{a} >? @var{b}
3409@findex >?
3410@cindex maximum operator
3411is the @dfn{maximum}, returning the larger of the numeric values @var{a}
3412and @var{b}.
3413@end table
3414
3415These operations are not primitive in ordinary C++, since you can
3416use a macro to return the minimum of two things in C++, as in the
3417following example.
3418
3419@example
3420#define MIN(X,Y) ((X) < (Y) ? : (X) : (Y))
3421@end example
3422
3423@noindent
3424You might then use @w{@samp{int min = MIN (i, j);}} to set @var{min} to
3425the minimum value of variables @var{i} and @var{j}.
3426
3427However, side effects in @code{X} or @code{Y} may cause unintended
3428behavior. For example, @code{MIN (i++, j++)} will fail, incrementing
3429the smaller counter twice. A GNU C extension allows you to write safe
3430macros that avoid this kind of problem (@pxref{Naming Types,,Naming an
3431Expression's Type}). However, writing @code{MIN} and @code{MAX} as
3432macros also forces you to use function-call notation for a
3433fundamental arithmetic operation. Using GNU C++ extensions, you can
3434write @w{@samp{int min = i <? j;}} instead.
3435
3436Since @code{<?} and @code{>?} are built into the compiler, they properly
3437handle expressions with side-effects; @w{@samp{int min = i++ <? j++;}}
3438works correctly.
3439
02cac427
NS
3440@node Volatiles
3441@section When is a Volatile Object Accessed?
3442@cindex accessing volatiles
3443@cindex volatile read
3444@cindex volatile write
3445@cindex volatile access
3446
3447Both the C and C++ standard have the concept of volatile objects. These
3448are normally accessed by pointers and used for accessing hardware. The
8117da65 3449standards encourage compilers to refrain from optimizations
02cac427
NS
3450concerning accesses to volatile objects that it might perform on
3451non-volatile objects. The C standard leaves it implementation defined
3452as to what constitutes a volatile access. The C++ standard omits to
3453specify this, except to say that C++ should behave in a similar manner
3454to C with respect to volatiles, where possible. The minimum either
8117da65 3455standard specifies is that at a sequence point all previous accesses to
02cac427
NS
3456volatile objects have stabilized and no subsequent accesses have
3457occurred. Thus an implementation is free to reorder and combine
3458volatile accesses which occur between sequence points, but cannot do so
3459for accesses across a sequence point. The use of volatiles does not
3460allow you to violate the restriction on updating objects multiple times
3461within a sequence point.
3462
3463In most expressions, it is intuitively obvious what is a read and what is
3464a write. For instance
3465
3466@example
3467volatile int *dst = <somevalue>;
3468volatile int *src = <someothervalue>;
3469*dst = *src;
3470@end example
3471
3472@noindent
3473will cause a read of the volatile object pointed to by @var{src} and stores the
3474value into the volatile object pointed to by @var{dst}. There is no
3475guarantee that these reads and writes are atomic, especially for objects
3476larger than @code{int}.
3477
3478Less obvious expressions are where something which looks like an access
3479is used in a void context. An example would be,
3480
3481@example
3482volatile int *src = <somevalue>;
3483*src;
3484@end example
3485
3486With C, such expressions are rvalues, and as rvalues cause a read of
3487the object, gcc interprets this as a read of the volatile being pointed
3488to. The C++ standard specifies that such expressions do not undergo
3489lvalue to rvalue conversion, and that the type of the dereferenced
3490object may be incomplete. The C++ standard does not specify explicitly
3491that it is this lvalue to rvalue conversion which is responsible for
3492causing an access. However, there is reason to believe that it is,
3493because otherwise certain simple expressions become undefined. However,
3494because it would surprise most programmers, g++ treats dereferencing a
3495pointer to volatile object of complete type in a void context as a read
3496of the object. When the object has incomplete type, g++ issues a
3497warning.
3498
3499@example
3500struct S;
3501struct T @{int m;@};
3502volatile S *ptr1 = <somevalue>;
3503volatile T *ptr2 = <somevalue>;
3504*ptr1;
3505*ptr2;
3506@end example
3507
3508In this example, a warning is issued for @code{*ptr1}, and @code{*ptr2}
3509causes a read of the object pointed to. If you wish to force an error on
3510the first case, you must force a conversion to rvalue with, for instance
3511a static cast, @code{static_cast<S>(*ptr1)}.
3512
3513When using a reference to volatile, g++ does not treat equivalent
3514expressions as accesses to volatiles, but instead issues a warning that
3515no volatile is accessed. The rationale for this is that otherwise it
3516becomes difficult to determine where volatile access occur, and not
3517possible to ignore the return value from functions returning volatile
3518references. Again, if you wish to force a read, cast the reference to
3519an rvalue.
3520
535233a8
NS
3521@node Restricted Pointers
3522@section Restricting Pointer Aliasing
3523@cindex restricted pointers
3524@cindex restricted references
3525@cindex restricted this pointer
3526
49419c8f 3527As with gcc, g++ understands the C99 feature of restricted pointers,
535233a8 3528specified with the @code{__restrict__}, or @code{__restrict} type
49419c8f 3529qualifier. Because you cannot compile C++ by specifying the -std=c99
535233a8
NS
3530language flag, @code{restrict} is not a keyword in C++.
3531
3532In addition to allowing restricted pointers, you can specify restricted
3533references, which indicate that the reference is not aliased in the local
3534context.
3535
3536@example
3537void fn (int *__restrict__ rptr, int &__restrict__ rref)
3538@{
3539 @dots{}
3540@}
3541@end example
3542
3543@noindent
3544In the body of @code{fn}, @var{rptr} points to an unaliased integer and
3545@var{rref} refers to a (different) unaliased integer.
3546
3547You may also specify whether a member function's @var{this} pointer is
3548unaliased by using @code{__restrict__} as a member function qualifier.
3549
3550@example
3551void T::fn () __restrict__
3552@{
3553 @dots{}
3554@}
3555@end example
3556
3557@noindent
3558Within the body of @code{T::fn}, @var{this} will have the effective
3559definition @code{T *__restrict__ const this}. Notice that the
3560interpretation of a @code{__restrict__} member function qualifier is
3561different to that of @code{const} or @code{volatile} qualifier, in that it
3562is applied to the pointer rather than the object. This is consistent with
3563other compilers which implement restricted pointers.
3564
3565As with all outermost parameter qualifiers, @code{__restrict__} is
3566ignored in function definition matching. This means you only need to
3567specify @code{__restrict__} in a function definition, rather than
3568in a function prototype as well.
3569
c1f7febf
RK
3570@node C++ Interface
3571@section Declarations and Definitions in One Header
3572
3573@cindex interface and implementation headers, C++
3574@cindex C++ interface and implementation headers
3575C++ object definitions can be quite complex. In principle, your source
3576code will need two kinds of things for each object that you use across
3577more than one source file. First, you need an @dfn{interface}
3578specification, describing its structure with type declarations and
3579function prototypes. Second, you need the @dfn{implementation} itself.
3580It can be tedious to maintain a separate interface description in a
3581header file, in parallel to the actual implementation. It is also
3582dangerous, since separate interface and implementation definitions may
3583not remain parallel.
3584
3585@cindex pragmas, interface and implementation
3586With GNU C++, you can use a single header file for both purposes.
3587
3588@quotation
3589@emph{Warning:} The mechanism to specify this is in transition. For the
3590nonce, you must use one of two @code{#pragma} commands; in a future
3591release of GNU C++, an alternative mechanism will make these
3592@code{#pragma} commands unnecessary.
3593@end quotation
3594
3595The header file contains the full definitions, but is marked with
3596@samp{#pragma interface} in the source code. This allows the compiler
3597to use the header file only as an interface specification when ordinary
3598source files incorporate it with @code{#include}. In the single source
3599file where the full implementation belongs, you can use either a naming
3600convention or @samp{#pragma implementation} to indicate this alternate
3601use of the header file.
3602
3603@table @code
3604@item #pragma interface
3605@itemx #pragma interface "@var{subdir}/@var{objects}.h"
3606@kindex #pragma interface
3607Use this directive in @emph{header files} that define object classes, to save
3608space in most of the object files that use those classes. Normally,
3609local copies of certain information (backup copies of inline member
3610functions, debugging information, and the internal tables that implement
3611virtual functions) must be kept in each object file that includes class
3612definitions. You can use this pragma to avoid such duplication. When a
3613header file containing @samp{#pragma interface} is included in a
3614compilation, this auxiliary information will not be generated (unless
3615the main input source file itself uses @samp{#pragma implementation}).
3616Instead, the object files will contain references to be resolved at link
3617time.
3618
3619The second form of this directive is useful for the case where you have
3620multiple headers with the same name in different directories. If you
3621use this form, you must specify the same string to @samp{#pragma
3622implementation}.
3623
3624@item #pragma implementation
3625@itemx #pragma implementation "@var{objects}.h"
3626@kindex #pragma implementation
3627Use this pragma in a @emph{main input file}, when you want full output from
3628included header files to be generated (and made globally visible). The
3629included header file, in turn, should use @samp{#pragma interface}.
3630Backup copies of inline member functions, debugging information, and the
3631internal tables used to implement virtual functions are all generated in
3632implementation files.
3633
3634@cindex implied @code{#pragma implementation}
3635@cindex @code{#pragma implementation}, implied
3636@cindex naming convention, implementation headers
3637If you use @samp{#pragma implementation} with no argument, it applies to
3638an include file with the same basename@footnote{A file's @dfn{basename}
3639was the name stripped of all leading path information and of trailing
3640suffixes, such as @samp{.h} or @samp{.C} or @samp{.cc}.} as your source
3641file. For example, in @file{allclass.cc}, giving just
3642@samp{#pragma implementation}
3643by itself is equivalent to @samp{#pragma implementation "allclass.h"}.
3644
3645In versions of GNU C++ prior to 2.6.0 @file{allclass.h} was treated as
3646an implementation file whenever you would include it from
3647@file{allclass.cc} even if you never specified @samp{#pragma
3648implementation}. This was deemed to be more trouble than it was worth,
3649however, and disabled.
3650
3651If you use an explicit @samp{#pragma implementation}, it must appear in
3652your source file @emph{before} you include the affected header files.
3653
3654Use the string argument if you want a single implementation file to
3655include code from multiple header files. (You must also use
3656@samp{#include} to include the header file; @samp{#pragma
3657implementation} only specifies how to use the file---it doesn't actually
3658include it.)
3659
3660There is no way to split up the contents of a single header file into
3661multiple implementation files.
3662@end table
3663
3664@cindex inlining and C++ pragmas
3665@cindex C++ pragmas, effect on inlining
3666@cindex pragmas in C++, effect on inlining
3667@samp{#pragma implementation} and @samp{#pragma interface} also have an
3668effect on function inlining.
3669
3670If you define a class in a header file marked with @samp{#pragma
3671interface}, the effect on a function defined in that class is similar to
3672an explicit @code{extern} declaration---the compiler emits no code at
3673all to define an independent version of the function. Its definition
3674is used only for inlining with its callers.
3675
3676Conversely, when you include the same header file in a main source file
3677that declares it as @samp{#pragma implementation}, the compiler emits
3678code for the function itself; this defines a version of the function
3679that can be found via pointers (or by callers compiled without
3680inlining). If all calls to the function can be inlined, you can avoid
3681emitting the function by compiling with @samp{-fno-implement-inlines}.
3682If any calls were not inlined, you will get linker errors.
3683
3684@node Template Instantiation
3685@section Where's the Template?
3686
3687@cindex template instantiation
3688
3689C++ templates are the first language feature to require more
3690intelligence from the environment than one usually finds on a UNIX
3691system. Somehow the compiler and linker have to make sure that each
3692template instance occurs exactly once in the executable if it is needed,
3693and not at all otherwise. There are two basic approaches to this
3694problem, which I will refer to as the Borland model and the Cfront model.
3695
3696@table @asis
3697@item Borland model
3698Borland C++ solved the template instantiation problem by adding the code
469b759e
JM
3699equivalent of common blocks to their linker; the compiler emits template
3700instances in each translation unit that uses them, and the linker
3701collapses them together. The advantage of this model is that the linker
3702only has to consider the object files themselves; there is no external
3703complexity to worry about. This disadvantage is that compilation time
3704is increased because the template code is being compiled repeatedly.
3705Code written for this model tends to include definitions of all
3706templates in the header file, since they must be seen to be
3707instantiated.
c1f7febf
RK
3708
3709@item Cfront model
3710The AT&T C++ translator, Cfront, solved the template instantiation
3711problem by creating the notion of a template repository, an
469b759e
JM
3712automatically maintained place where template instances are stored. A
3713more modern version of the repository works as follows: As individual
3714object files are built, the compiler places any template definitions and
3715instantiations encountered in the repository. At link time, the link
3716wrapper adds in the objects in the repository and compiles any needed
3717instances that were not previously emitted. The advantages of this
3718model are more optimal compilation speed and the ability to use the
3719system linker; to implement the Borland model a compiler vendor also
c1f7febf 3720needs to replace the linker. The disadvantages are vastly increased
469b759e
JM
3721complexity, and thus potential for error; for some code this can be
3722just as transparent, but in practice it can been very difficult to build
c1f7febf 3723multiple programs in one directory and one program in multiple
469b759e
JM
3724directories. Code written for this model tends to separate definitions
3725of non-inline member templates into a separate file, which should be
3726compiled separately.
c1f7febf
RK
3727@end table
3728
469b759e 3729When used with GNU ld version 2.8 or later on an ELF system such as
a4b3b54a
JM
3730Linux/GNU or Solaris 2, or on Microsoft Windows, g++ supports the
3731Borland model. On other systems, g++ implements neither automatic
3732model.
469b759e
JM
3733
3734A future version of g++ will support a hybrid model whereby the compiler
3735will emit any instantiations for which the template definition is
3736included in the compile, and store template definitions and
3737instantiation context information into the object file for the rest.
3738The link wrapper will extract that information as necessary and invoke
3739the compiler to produce the remaining instantiations. The linker will
3740then combine duplicate instantiations.
3741
3742In the mean time, you have the following options for dealing with
3743template instantiations:
c1f7febf
RK
3744
3745@enumerate
d863830b
JL
3746@item
3747Compile your template-using code with @samp{-frepo}. The compiler will
3748generate files with the extension @samp{.rpo} listing all of the
3749template instantiations used in the corresponding object files which
3750could be instantiated there; the link wrapper, @samp{collect2}, will
3751then update the @samp{.rpo} files to tell the compiler where to place
3752those instantiations and rebuild any affected object files. The
3753link-time overhead is negligible after the first pass, as the compiler
3754will continue to place the instantiations in the same files.
3755
3756This is your best option for application code written for the Borland
3757model, as it will just work. Code written for the Cfront model will
3758need to be modified so that the template definitions are available at
3759one or more points of instantiation; usually this is as simple as adding
3760@code{#include <tmethods.cc>} to the end of each template header.
3761
3762For library code, if you want the library to provide all of the template
3763instantiations it needs, just try to link all of its object files
3764together; the link will fail, but cause the instantiations to be
3765generated as a side effect. Be warned, however, that this may cause
3766conflicts if multiple libraries try to provide the same instantiations.
3767For greater control, use explicit instantiation as described in the next
3768option.
3769
c1f7febf
RK
3770@item
3771Compile your code with @samp{-fno-implicit-templates} to disable the
3772implicit generation of template instances, and explicitly instantiate
3773all the ones you use. This approach requires more knowledge of exactly
3774which instances you need than do the others, but it's less
3775mysterious and allows greater control. You can scatter the explicit
3776instantiations throughout your program, perhaps putting them in the
3777translation units where the instances are used or the translation units
3778that define the templates themselves; you can put all of the explicit
3779instantiations you need into one big file; or you can create small files
3780like
3781
3782@example
3783#include "Foo.h"
3784#include "Foo.cc"
3785
3786template class Foo<int>;
3787template ostream& operator <<
3788 (ostream&, const Foo<int>&);
3789@end example
3790
3791for each of the instances you need, and create a template instantiation
3792library from those.
3793
3794If you are using Cfront-model code, you can probably get away with not
3795using @samp{-fno-implicit-templates} when compiling files that don't
3796@samp{#include} the member template definitions.
3797
3798If you use one big file to do the instantiations, you may want to
3799compile it without @samp{-fno-implicit-templates} so you get all of the
3800instances required by your explicit instantiations (but not by any
3801other files) without having to specify them as well.
3802
3803g++ has extended the template instantiation syntax outlined in the
03d0f4af 3804Working Paper to allow forward declaration of explicit instantiations
4003d7f9
JM
3805(with @code{extern}), instantiation of the compiler support data for a
3806template class (i.e. the vtable) without instantiating any of its
3807members (with @code{inline}), and instantiation of only the static data
3808members of a template class, without the support data or member
3809functions (with (@code{static}):
c1f7febf
RK
3810
3811@example
3812extern template int max (int, int);
c1f7febf 3813inline template class Foo<int>;
4003d7f9 3814static template class Foo<int>;
c1f7febf
RK
3815@end example
3816
3817@item
3818Do nothing. Pretend g++ does implement automatic instantiation
3819management. Code written for the Borland model will work fine, but
3820each translation unit will contain instances of each of the templates it
3821uses. In a large program, this can lead to an unacceptable amount of code
3822duplication.
3823
3824@item
3825Add @samp{#pragma interface} to all files containing template
3826definitions. For each of these files, add @samp{#pragma implementation
3827"@var{filename}"} to the top of some @samp{.C} file which
3828@samp{#include}s it. Then compile everything with
3829@samp{-fexternal-templates}. The templates will then only be expanded
3830in the translation unit which implements them (i.e. has a @samp{#pragma
3831implementation} line for the file where they live); all other files will
3832use external references. If you're lucky, everything should work
3833properly. If you get undefined symbol errors, you need to make sure
3834that each template instance which is used in the program is used in the
3835file which implements that template. If you don't have any use for a
3836particular instance in that file, you can just instantiate it
3837explicitly, using the syntax from the latest C++ working paper:
3838
3839@example
3840template class A<int>;
3841template ostream& operator << (ostream&, const A<int>&);
3842@end example
3843
3844This strategy will work with code written for either model. If you are
3845using code written for the Cfront model, the file containing a class
3846template and the file containing its member templates should be
3847implemented in the same translation unit.
3848
3849A slight variation on this approach is to instead use the flag
3850@samp{-falt-external-templates}; this flag causes template
3851instances to be emitted in the translation unit that implements the
3852header where they are first instantiated, rather than the one which
3853implements the file where the templates are defined. This header must
3854be the same in all translation units, or things are likely to break.
3855
3856@xref{C++ Interface,,Declarations and Definitions in One Header}, for
3857more discussion of these pragmas.
3858@end enumerate
3859
0ded1f18
JM
3860@node Bound member functions
3861@section Extracting the function pointer from a bound pointer to member function
3862
3863@cindex pmf
3864@cindex pointer to member function
3865@cindex bound pointer to member function
3866
3867In C++, pointer to member functions (PMFs) are implemented using a wide
3868pointer of sorts to handle all the possible call mechanisms; the PMF
3869needs to store information about how to adjust the @samp{this} pointer,
3870and if the function pointed to is virtual, where to find the vtable, and
3871where in the vtable to look for the member function. If you are using
3872PMFs in an inner loop, you should really reconsider that decision. If
3873that is not an option, you can extract the pointer to the function that
3874would be called for a given object/PMF pair and call it directly inside
3875the inner loop, to save a bit of time.
3876
3877Note that you will still be paying the penalty for the call through a
3878function pointer; on most modern architectures, such a call defeats the
3879branch prediction features of the CPU. This is also true of normal
3880virtual function calls.
3881
3882The syntax for this extension is
3883
3884@example
3885extern A a;
3886extern int (A::*fp)();
3887typedef int (*fptr)(A *);
3888
3889fptr p = (fptr)(a.*fp);
3890@end example
3891
0fb6bbf5
ML
3892For PMF constants (i.e. expressions of the form @samp{&Klasse::Member}),
3893no object is needed to obtain the address of the function. They can be
3894converted to function pointers directly:
3895
3896@example
3897fptr p1 = (fptr)(&A::foo);
3898@end example
3899
0ded1f18
JM
3900You must specify @samp{-Wno-pmf-conversions} to use this extension.
3901