aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-decl.c
diff options
context:
space:
mode:
authorJoseph Myers <jsm28@cam.ac.uk>2001-12-04 22:55:40 +0000
committerJoseph Myers <jsm28@gcc.gnu.org>2001-12-04 22:55:40 +0000
commitdb3acfa5476e5af834fee38ad8e1b193a8f018dc (patch)
tree47e4fa29a6462438cb5aea8ae3e2c690243e3f52 /gcc/c-decl.c
parentd062a680ab959bc09d0f2572ac16e444b5396aa4 (diff)
downloadgcc-db3acfa5476e5af834fee38ad8e1b193a8f018dc.zip
gcc-db3acfa5476e5af834fee38ad8e1b193a8f018dc.tar.gz
gcc-db3acfa5476e5af834fee38ad8e1b193a8f018dc.tar.bz2
re PR c/4787 (Different anonymous variables declared but only one allocated when the initialization is the same)
* c-common.def (COMPOUND_LITERAL_EXPR): New. * c-common.c (c_expand_expr): Handle COMPOUND_LITERAL_EXPR. (c_staticp): New function. * c-common.h (COMPOUND_LITERAL_EXPR_DECL): New. (c_staticp): Declare. * c-typeck.c (default_function_array_conversion, build_unary_op): Don't handle CONSTRUCTOR specially. (lvalue_p, mark_addressable): Handle COMPOUND_LITERAL_EXPR. * c-decl.c (build_compound_literal): New function. * c-tree.h (build_compound_literal): Declare. * c-parse.in (primary): Use build_compound_literal. * c-lang.c (LANG_HOOKS_STATICP): Define. * objc/objc-lang.c (LANG_HOOKS_STATICP): Likewise. * doc/c-tree.texi: Document COMPOUND_LITERAL_EXPR. * doc/extend.texi: Update documentation of compound literals. Fixes PR c/4787. testsuite: * gcc.c-torture/execute/20000722-1.x, gcc.c-torture/execute/20010123-1.x: Remove. * gcc.c-torture/compile/init-3.c: Don't use a compound literal. * gcc.dg/c90-complit-1.c, gcc.dg/c99-complit-1.c, gcc.dg/c99-complit-2.c: New tests. From-SVN: r47629
Diffstat (limited to 'gcc/c-decl.c')
-rw-r--r--gcc/c-decl.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/gcc/c-decl.c b/gcc/c-decl.c
index 3427854..b5597f9 100644
--- a/gcc/c-decl.c
+++ b/gcc/c-decl.c
@@ -3793,6 +3793,59 @@ clear_parm_order ()
current_binding_level->parm_order = NULL_TREE;
}
+/* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
+ literal, which may be an incomplete array type completed by the
+ initializer; INIT is a CONSTRUCTOR that initializes the compound
+ literal. */
+
+tree
+build_compound_literal (type, init)
+ tree type;
+ tree init;
+{
+ /* We do not use start_decl here because we have a type, not a declarator;
+ and do not use finish_decl because the decl should be stored inside
+ the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
+ tree decl = build_decl (VAR_DECL, NULL_TREE, type);
+ tree complit;
+ DECL_EXTERNAL (decl) = 0;
+ TREE_PUBLIC (decl) = 0;
+ TREE_STATIC (decl) = (current_binding_level == global_binding_level);
+ DECL_CONTEXT (decl) = current_function_decl;
+ TREE_USED (decl) = 1;
+ TREE_TYPE (decl) = type;
+ store_init_value (decl, init);
+
+ if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
+ {
+ int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
+ if (failure)
+ abort ();
+ }
+
+ type = TREE_TYPE (decl);
+ if (type == error_mark_node || !COMPLETE_TYPE_P (type))
+ return error_mark_node;
+
+ complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), decl);
+ TREE_SIDE_EFFECTS (complit) = 1;
+
+ layout_decl (decl, 0);
+
+ if (TREE_STATIC (decl))
+ {
+ /* This decl needs a name for the assembler output. We also need
+ a unique suffix to be added to the name, for which DECL_CONTEXT
+ must be set. */
+ DECL_NAME (decl) = get_identifier ("__compound_literal");
+ DECL_CONTEXT (decl) = complit;
+ rest_of_decl_compilation (decl, NULL, 1, 0);
+ DECL_CONTEXT (decl) = NULL_TREE;
+ }
+
+ return complit;
+}
+
/* Make TYPE a complete type based on INITIAL_VALUE.
Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
2 if there was no information (in which case assume 1 if DO_DEFAULT). */