This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[c++] RFC: partial C99 designated-initializers support
- From: Bernardo Innocenti <bernie at develer dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Fri, 22 Oct 2004 23:47:49 +0200
- Subject: [c++] RFC: partial C99 designated-initializers support
- Organization: Develer S.r.l.
Hello,
the C++ frontend supports the GNU syntax for designated
initializers, but not the more popular C99 one.
A possible improvement for a followup patch would be
fixing process_init_constructor() to reorder initializers.
DISCLAIMER: No, I'm not going to design a full blown C++ language
extension and submit it to the ISO comitee. This is just a
better-than-nothing, short-term solution which I consider at
least as acceptable as the GNU syntax we already support,
and much more useful in real-world code.
Bootstrapped and not tested as this will have to wait for
4.1 anyway. I'm willing to update the documentation to
mention both the GNU and C99 extensions if the proposal
is accepted.
gcc/cp/
2004-10-22 Bernardo Innocenti <bernie@develer.com>
* parser.c (cp_parser_initializer_clause): Parse a subset of C99's
designated initializers syntax.
gcc/testsuite
2004-10-22 Bernardo Innocenti <bernie@develer.com>
* g++.dg/parse/designated-initializers.C: New test.
diff -u -p -u -p -r1.269 parser.c
--- gcc/cp/parser.c 22 Oct 2004 11:42:23 -0000 1.269
+++ gcc/cp/parser.c 22 Oct 2004 21:07:09 -0000
@@ -12005,12 +12005,16 @@ cp_parser_initializer_clause (cp_parser*
initializer-clause
initializer-list , initializer-clause
- GNU Extension:
+ GNU Extensions:
initializer-list:
identifier : initializer-clause
initializer-list, identifier : initializer-clause
+ initializer-list:
+ . identifier = initializer-clause
+ initializer-list, . identifier = initializer-clause
+
Returns a TREE_LIST. The TREE_VALUE of each node is an expression
for the initializer. If the TREE_PURPOSE is non-NULL, it is the
IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
@@ -12044,6 +12048,20 @@ cp_parser_initializer_list (cp_parser* p
/* Consume the `:'. */
cp_lexer_consume_token (parser->lexer);
}
+ /* We also accept a simple subset of C99's designated-initializer
+ syntax: ".identifier = value". */
+ else if (cp_parser_allow_gnu_extensions_p (parser)
+ && cp_lexer_peek_nth_token (parser->lexer, 1)->type == CPP_DOT
+ && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
+ && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
+ {
+ /* Consume the `.'. */
+ cp_lexer_consume_token (parser->lexer);
+ /* Consume the identifier. */
+ identifier = cp_lexer_consume_token (parser->lexer)->value;
+ /* Consume the `='. */
+ cp_lexer_consume_token (parser->lexer);
+ }
else
identifier = NULL_TREE;
// { dg-do compile }
// Test C99 and GNU designated initializer extensions.
// Origin: Bernardo Innocenti <bernie@develer.com>
struct
{
int first, second, third, forth;
struct { int a; } nested;
int array[3];
}
foo =
{
.first = 1, // C99 designated-initializer syntax
second: 2, // GNU designated-initializer syntax
.forth = 4, // UNSUPPORTED: Out-of-order initialization
.nested.a = 5, // UNSUPPORTED: Non-trivial initializer { dg-error "" }
.array[2] = 6, // UNSUPPORTED: Array initializer { dg-error "" }
}; // { dg-error "" } This is where out-of-order initialization is detected
--
// Bernardo Innocenti - Develer S.r.l., R&D dept.
\X/ http://www.develer.com/