c-typeck.c (output_init_element): Allow initializing static storage duration objects...
authorJakub Jelinek <jakub@redhat.com>
Wed, 2 Jan 2002 23:43:24 +0000 (00:43 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Wed, 2 Jan 2002 23:43:24 +0000 (00:43 +0100)
* c-typeck.c (output_init_element): Allow initializing static storage
duration objects with compound literals.

* gcc.dg/gnu89-init-1.c: Added new tests.

From-SVN: r48487

gcc/ChangeLog
gcc/c-typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/gnu89-init-1.c

index cf89033c5585fafa074516e48be5e8ea32b863d7..645b2fe4472898ac62773577166e8f9b6fd5712b 100644 (file)
@@ -1,3 +1,8 @@
+2002-01-02  Jakub Jelinek  <jakub@redhat.com>
+
+       * c-typeck.c (output_init_element): Allow initializing static storage
+       duration objects with compound literals.
+
 2002-01-02  Richard Henderson  <rth@redhat.com>
 
        * objc/objc-act.c (hack_method_prototype): Clear current_function_decl
index 23d8a127e99671d9f6544d406211b87352b5ffd1..667dbe2dba14d5c7287bdc723752a0e8773560f8 100644 (file)
@@ -1,6 +1,6 @@
 /* Build expressions with type checking for C compiler.
    Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
-   1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+   1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -6275,6 +6275,16 @@ output_init_element (value, type, field, pending)
                         TYPE_MAIN_VARIANT (type))))
     value = default_conversion (value);
 
+  if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
+      && require_constant_value && !flag_isoc99 && pending)
+    {
+      /* As an extension, allow initializing objects with static storage
+        duration with compound literals (which are then treated just as
+        the brace enclosed list they contain).  */
+      tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
+      value = DECL_INITIAL (decl);
+    }
+
   if (value == error_mark_node)
     constructor_erroneous = 1;
   else if (!TREE_CONSTANT (value))
index d225074a5b7b627002e35556c5fac1cf59f85cbb..a4afe65ef3d1007aa5c3cba4fc0b937e06cabec0 100644 (file)
@@ -1,3 +1,7 @@
+2002-01-02  Jakub Jelinek  <jakub@redhat.com>
+
+       * gcc.dg/gnu89-init-1.c: Added new tests.
+
 2002-01-02  Nathan Sidwell  <nathan@codesourcery.com>
 
        * g++.dg/template/friend2.C: Remove as patch is reverted.
index e11a0a48c2acd764704a9f0b37a77a7f8b234f8d..3cabeb443c402bdb53b7fe588517d904c17e60c6 100644 (file)
@@ -8,6 +8,8 @@ extern void exit (int);
 
 struct A { int i; int j; int k[4]; };
 struct B { };
+struct C { int i; };
+struct D { int i; struct C j; };
 
 /* As a GNU extension, we allow initialization of objects with static storage
    duration by compound literals.  It is handled as if the object
@@ -22,6 +24,10 @@ int c[] = (int []) { [2] = 6, 7, 8 };
 int d[] = (int [3]) { 1 };
 int e[2] = (int []) { 1, 2 };
 int f[2] = (int [2]) { 1 };
+struct C g[3] = { [2] = (struct C) { 13 }, [1] = (const struct C) { 12 } };
+struct D h = { .j = (struct C) { 15 }, .i = 14 };
+struct D i[2] = { [1].j = (const struct C) { 17 },
+                 [0] = { 0, (struct C) { 16 } } };
 
 int main (void)
 {
@@ -43,5 +49,11 @@ int main (void)
     abort ();
   if (sizeof (f) != 2 * sizeof (int))
     abort ();
+  if (g[0].i || g[1].i != 12 || g[2].i != 13)
+    abort ();
+  if (h.i != 14 || h.j.i != 15)
+    abort ();
+  if (i[0].i || i[0].j.i != 16 || i[1].i || i[1].j.i != 17)
+    abort ();
   exit (0);
 }