diff options
author | jsm28 <jsm28@138bc75d-0d04-0410-961f-82ee72b054a4> | 2013-11-13 00:38:49 +0000 |
---|---|---|
committer | jsm28 <jsm28@138bc75d-0d04-0410-961f-82ee72b054a4> | 2013-11-13 00:38:49 +0000 |
commit | 4fba5eb95587cdfbbc183874bdc5b044f10b713c (patch) | |
tree | e843a9dc913b73d2b8924f2993e8aaeee0c8be4f /gcc/doc | |
parent | ded3d3f8abc23c9641c8a975da35e16b26ce432a (diff) | |
download | gcc-4fba5eb95587cdfbbc183874bdc5b044f10b713c.tar.gz |
* doc/extend.texi (Statement Exprs, Typeof): Discuss __auto_type.
* ginclude/stdatomic.h (kill_dependency, atomic_store_explicit)
(atomic_load_explicit, atomic_exchange_explicit)
(atomic_compare_exchange_strong_explicit)
(atomic_compare_exchange_weak_explicit): Use __auto_type to
declare variable initialized with PTR argument.
c-family:
* c-common.h (enum rid): Add RID_AUTO_TYPE.
* c-common.c (c_common_reswords): Add __auto_type.
(keyword_begins_type_specifier): Handle RID_AUTO_TYPE.
c:
* c-tree.h (c_typespec_keyword): Add cts_auto_type.
* c-decl.c (declspecs_add_type, finish_declspecs): Handle
__auto_type.
* c-parser.c (c_token_starts_typename, c_token_starts_declspecs)
(c_parser_attribute_any_word, c_parser_objc_selector): Handle
RID_AUTO_TYPE.
(c_parser_declspecs): Take argument AUTO_TYPE_OK.
(c_parser_declaration_or_fndef, c_parser_struct_declaration)
(c_parser_declarator, c_parser_direct_declarator_inner)
(c_parser_parameter_declaration, c_parser_type_name): All callers
changed.
(c_parser_declaration_or_fndef): Handle declarations with type
determined from the initializer.
testsuite:
* gcc.dg/atomic/stdatomic-vm.c, gcc.dg/auto-type-1.c,
gcc.dg/auto-type-2.c: New tests.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@204731 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/doc')
-rw-r--r-- | gcc/doc/extend.texi | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 2132b1e5602..599dee3c3bf 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -153,7 +153,7 @@ the value of an enumeration constant, the width of a bit-field, or the initial value of a static variable. If you don't know the type of the operand, you can still do this, but you -must use @code{typeof} (@pxref{Typeof}). +must use @code{typeof} or @code{__auto_type} (@pxref{Typeof}). In G++, the result value of a statement expression undergoes array and function pointer decay, and is returned by value to the enclosing @@ -755,6 +755,35 @@ Thus, @code{array (pointer (char), 4)} is the type of arrays of 4 pointers to @code{char}. @end itemize +In GNU C, but not GNU C++, you may also declare the type of a variable +as @code{__auto_type}. In that case, the declaration must declare +only one variable, whose declarator must just be an identifier, the +declaration must be initialized, and the type of the variable is +determined by the initializer; the name of the variable is not in +scope until after the initializer. (In C++, you should use C++11 +@code{auto} for this purpose.) Using @code{__auto_type}, the +``maximum'' macro above could be written as: + +@smallexample +#define max(a,b) \ + (@{ __auto_type _a = (a); \ + __auto_type _b = (b); \ + _a > _b ? _a : _b; @}) +@end smallexample + +Using @code{__auto_type} instead of @code{typeof} has two advantages: + +@itemize @bullet +@item Each argument to the macro appears only once in the expansion of +the macro. This prevents the size of the macro expansion growing +exponentially when calls to such macros are nested inside arguments of +such macros. + +@item If the argument to the macro has variably modified type, it is +evaluated only once when using @code{__auto_type}, but twice if +@code{typeof} is used. +@end itemize + @emph{Compatibility Note:} In addition to @code{typeof}, GCC 2 supported a more limited extension that permitted one to write |