Re: [C PATCH v3] Fix ICEs related to VM types in C 2/2

2023-05-23 Thread Martin Uecker via Gcc-patches
Am Dienstag, dem 23.05.2023 um 10:18 +0200 schrieb Richard Biener:
> On Tue, May 23, 2023 at 8:24 AM Martin Uecker 
> wrote:
> > 
> > Am Dienstag, dem 23.05.2023 um 08:13 +0200 schrieb Richard Biener:
> > > On Mon, May 22, 2023 at 7:24 PM Martin Uecker via Gcc-patches
> > >  wrote:
> > > > 
> > > > 
> > > > 
> > > > This version contains the middle-end changes for PR109450
> > > > and test cases as before.  The main middle-end change is that
> > > > we use gimplify_type_sizes also for parameters and remove
> > > > the special code that also walked into pointers (which is
> > > > incorrect).
> > > > 
> > > > In addition, in the C FE this patch now also adds DECL_EXPR
> > > > for vm-types which are pointed-to by parameters declared
> > > > as arrays.  The new function created contains the exact
> > > > code previously used only for regular pointers, and is
> > > > now also called for parameters declared as arrays.
> > > > 
> > > > 
> > > > Martin
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > > 
> > > >     Fix ICEs related to VM types in C 2/2 [PR109450]
> > > > 
> > > >     Size expressions were sometimes lost and not gimplified
> > > > correctly,
> > > >     leading to ICEs and incorrect evaluation order.  Fix this
> > > > by 1) not
> > > >     recursing pointers when gimplifying parameters in the
> > > > middle-end
> > > >     (the code is merged with gimplify_type_sizes), which is
> > > > incorrect
> > > >     because it might access variables declared later for
> > > > incomplete
> > > >     structs, and 2) adding a decl expr for variably-modified
> > > > arrays
> > > >     that are pointed to by parameters declared as arrays.
> > > > 
> > > >     PR c/109450
> > > > 
> > > >     gcc/
> > > >     * c/c-decl.cc (add_decl_expr): New function.
> > > >     (grokdeclarator): Add decl expr for size expression
> > > > in
> > > >     types pointed to by parameters declared as arrays.
> > > >     * function.cc (gimplify_parm_type): Remove
> > > > function.
> > > >     (gimplify_parameters): Call gimplify_parm_sizes.
> > > >     * gimplify.cc (gimplify_type_sizes): Make function
> > > > static.
> > > >     (gimplify_parm_sizes): New function.
> > > > 
> > > >     gcc/testsuite/
> > > >     * gcc.dg/pr109450-1.c: New test.
> > > >     * gcc.dg/pr109450-2.c: New test.
> > > >     * gcc.dg/vla-26.c: New test.
> > > > 
> > > > diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
> > > > index 494d3cf1747..c35347734b2 100644
> > > > --- a/gcc/c/c-decl.cc
> > > > +++ b/gcc/c/c-decl.cc
> > > > @@ -6490,6 +6490,55 @@ smallest_type_quals_location (const
> > > > location_t *locations,
> > > >    return loc;
> > > >  }
> > > > 
> > > > +
> > > > +/* We attach an artificial TYPE_DECL to pointed-to type
> > > > +   and arrange for it to be included in a DECL_EXPR.  This
> > > > +   forces the sizes evaluation at a safe point and ensures it
> > > > +   is not deferred until e.g. within a deeper conditional
> > > > context.
> > > > +
> > > > +   PARM contexts have no enclosing statement list that
> > > > +   can hold the DECL_EXPR, so we need to use a BIND_EXPR
> > > > +   instead, and add it to the list of expressions that
> > > > +   need to be evaluated.
> > > > +
> > > > +   TYPENAME contexts do have an enclosing statement list,
> > > > +   but it would be incorrect to use it, as the size should
> > > > +   only be evaluated if the containing expression is
> > > > +   evaluated.  We might also be in the middle of an
> > > > +   expression with side effects on the pointed-to type size
> > > > +   "arguments" prior to the pointer declaration point and
> > > > +   the fake TYPE_DECL in the enclosing context would force
> > > > +   the size evaluation prior to the side effects.  We
> > > > therefore
> > > > +   use BIND_EXPRs in TYPENAME contexts too.  */
> > > > +static void
> > > > +add_decl_expr(location_t loc, enum decl_context decl_context,
> > > > tree type, tree *expr)
> > > > +{
> > > > +  tree bind = NULL_TREE;
> > > > +  if (decl_context == TYPENAME || decl_context == PARM ||
> > > > decl_context == FIELD)
> > > > +    {
> > > > +  bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
> > > > NULL_TREE, NULL_TREE);
> > > > +  TREE_SIDE_EFFECTS (bind) = 1;
> > > > +  BIND_EXPR_BODY (bind) = push_stmt_list ();
> > > > +  push_scope ();
> > > > +    }
> > > > +
> > > > +  tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
> > > > +  pushdecl (decl);
> > > > +  DECL_ARTIFICIAL (decl) = 1;
> > > > +  add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
> > > > DECL_EXPR, decl));
> > > > +  TYPE_NAME (type) = decl;
> > > > +
> > > > +  if (bind)
> > > > +    {
> > > > +  pop_scope ();
> > > > +  BIND_EXPR_BODY (bind) = pop_stmt_list (BIND_EXPR_BODY
> > > > (bind));
> > > > +  if (*expr)
> > > > +   *expr = build2 (COMPOUND_EXPR, void_type_node, *expr,
> > > > bind);
> > > > +   

Re: [C PATCH v3] Fix ICEs related to VM types in C 2/2

2023-05-23 Thread Richard Biener via Gcc-patches
On Tue, May 23, 2023 at 8:24 AM Martin Uecker  wrote:
>
> Am Dienstag, dem 23.05.2023 um 08:13 +0200 schrieb Richard Biener:
> > On Mon, May 22, 2023 at 7:24 PM Martin Uecker via Gcc-patches
> >  wrote:
> > >
> > >
> > >
> > > This version contains the middle-end changes for PR109450
> > > and test cases as before.  The main middle-end change is that
> > > we use gimplify_type_sizes also for parameters and remove
> > > the special code that also walked into pointers (which is
> > > incorrect).
> > >
> > > In addition, in the C FE this patch now also adds DECL_EXPR
> > > for vm-types which are pointed-to by parameters declared
> > > as arrays.  The new function created contains the exact
> > > code previously used only for regular pointers, and is
> > > now also called for parameters declared as arrays.
> > >
> > >
> > > Martin
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > Fix ICEs related to VM types in C 2/2 [PR109450]
> > >
> > > Size expressions were sometimes lost and not gimplified correctly,
> > > leading to ICEs and incorrect evaluation order.  Fix this by 1) not
> > > recursing pointers when gimplifying parameters in the middle-end
> > > (the code is merged with gimplify_type_sizes), which is incorrect
> > > because it might access variables declared later for incomplete
> > > structs, and 2) adding a decl expr for variably-modified arrays
> > > that are pointed to by parameters declared as arrays.
> > >
> > > PR c/109450
> > >
> > > gcc/
> > > * c/c-decl.cc (add_decl_expr): New function.
> > > (grokdeclarator): Add decl expr for size expression in
> > > types pointed to by parameters declared as arrays.
> > > * function.cc (gimplify_parm_type): Remove function.
> > > (gimplify_parameters): Call gimplify_parm_sizes.
> > > * gimplify.cc (gimplify_type_sizes): Make function static.
> > > (gimplify_parm_sizes): New function.
> > >
> > > gcc/testsuite/
> > > * gcc.dg/pr109450-1.c: New test.
> > > * gcc.dg/pr109450-2.c: New test.
> > > * gcc.dg/vla-26.c: New test.
> > >
> > > diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
> > > index 494d3cf1747..c35347734b2 100644
> > > --- a/gcc/c/c-decl.cc
> > > +++ b/gcc/c/c-decl.cc
> > > @@ -6490,6 +6490,55 @@ smallest_type_quals_location (const location_t 
> > > *locations,
> > >return loc;
> > >  }
> > >
> > > +
> > > +/* We attach an artificial TYPE_DECL to pointed-to type
> > > +   and arrange for it to be included in a DECL_EXPR.  This
> > > +   forces the sizes evaluation at a safe point and ensures it
> > > +   is not deferred until e.g. within a deeper conditional context.
> > > +
> > > +   PARM contexts have no enclosing statement list that
> > > +   can hold the DECL_EXPR, so we need to use a BIND_EXPR
> > > +   instead, and add it to the list of expressions that
> > > +   need to be evaluated.
> > > +
> > > +   TYPENAME contexts do have an enclosing statement list,
> > > +   but it would be incorrect to use it, as the size should
> > > +   only be evaluated if the containing expression is
> > > +   evaluated.  We might also be in the middle of an
> > > +   expression with side effects on the pointed-to type size
> > > +   "arguments" prior to the pointer declaration point and
> > > +   the fake TYPE_DECL in the enclosing context would force
> > > +   the size evaluation prior to the side effects.  We therefore
> > > +   use BIND_EXPRs in TYPENAME contexts too.  */
> > > +static void
> > > +add_decl_expr(location_t loc, enum decl_context decl_context, tree type, 
> > > tree *expr)
> > > +{
> > > +  tree bind = NULL_TREE;
> > > +  if (decl_context == TYPENAME || decl_context == PARM || decl_context 
> > > == FIELD)
> > > +{
> > > +  bind = build3 (BIND_EXPR, void_type_node, NULL_TREE, NULL_TREE, 
> > > NULL_TREE);
> > > +  TREE_SIDE_EFFECTS (bind) = 1;
> > > +  BIND_EXPR_BODY (bind) = push_stmt_list ();
> > > +  push_scope ();
> > > +}
> > > +
> > > +  tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
> > > +  pushdecl (decl);
> > > +  DECL_ARTIFICIAL (decl) = 1;
> > > +  add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
> > > +  TYPE_NAME (type) = decl;
> > > +
> > > +  if (bind)
> > > +{
> > > +  pop_scope ();
> > > +  BIND_EXPR_BODY (bind) = pop_stmt_list (BIND_EXPR_BODY (bind));
> > > +  if (*expr)
> > > +   *expr = build2 (COMPOUND_EXPR, void_type_node, *expr, bind);
> > > +  else
> > > +   *expr = bind;
> > > +}
> > > +}
> > > +
> > >  /* Given declspecs and a declarator,
> > > determine the name and type of the object declared
> > > and construct a ..._DECL node for it.
> > > @@ -7474,58 +7523,9 @@ grokdeclarator (const struct c_declarator 
> > > *declarator,
> > >
> > >This is expected to happen automatically when the 
> > > pointed-to
> > >   

Re: [C PATCH v3] Fix ICEs related to VM types in C 2/2

2023-05-23 Thread Martin Uecker via Gcc-patches
Am Dienstag, dem 23.05.2023 um 08:13 +0200 schrieb Richard Biener:
> On Mon, May 22, 2023 at 7:24 PM Martin Uecker via Gcc-patches
>  wrote:
> > 
> > 
> > 
> > This version contains the middle-end changes for PR109450
> > and test cases as before.  The main middle-end change is that
> > we use gimplify_type_sizes also for parameters and remove
> > the special code that also walked into pointers (which is
> > incorrect).
> > 
> > In addition, in the C FE this patch now also adds DECL_EXPR
> > for vm-types which are pointed-to by parameters declared
> > as arrays.  The new function created contains the exact
> > code previously used only for regular pointers, and is
> > now also called for parameters declared as arrays.
> > 
> > 
> > Martin
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > Fix ICEs related to VM types in C 2/2 [PR109450]
> > 
> > Size expressions were sometimes lost and not gimplified correctly,
> > leading to ICEs and incorrect evaluation order.  Fix this by 1) not
> > recursing pointers when gimplifying parameters in the middle-end
> > (the code is merged with gimplify_type_sizes), which is incorrect
> > because it might access variables declared later for incomplete
> > structs, and 2) adding a decl expr for variably-modified arrays
> > that are pointed to by parameters declared as arrays.
> > 
> > PR c/109450
> > 
> > gcc/
> > * c/c-decl.cc (add_decl_expr): New function.
> > (grokdeclarator): Add decl expr for size expression in
> > types pointed to by parameters declared as arrays.
> > * function.cc (gimplify_parm_type): Remove function.
> > (gimplify_parameters): Call gimplify_parm_sizes.
> > * gimplify.cc (gimplify_type_sizes): Make function static.
> > (gimplify_parm_sizes): New function.
> > 
> > gcc/testsuite/
> > * gcc.dg/pr109450-1.c: New test.
> > * gcc.dg/pr109450-2.c: New test.
> > * gcc.dg/vla-26.c: New test.
> > 
> > diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
> > index 494d3cf1747..c35347734b2 100644
> > --- a/gcc/c/c-decl.cc
> > +++ b/gcc/c/c-decl.cc
> > @@ -6490,6 +6490,55 @@ smallest_type_quals_location (const location_t 
> > *locations,
> >    return loc;
> >  }
> > 
> > +
> > +/* We attach an artificial TYPE_DECL to pointed-to type
> > +   and arrange for it to be included in a DECL_EXPR.  This
> > +   forces the sizes evaluation at a safe point and ensures it
> > +   is not deferred until e.g. within a deeper conditional context.
> > +
> > +   PARM contexts have no enclosing statement list that
> > +   can hold the DECL_EXPR, so we need to use a BIND_EXPR
> > +   instead, and add it to the list of expressions that
> > +   need to be evaluated.
> > +
> > +   TYPENAME contexts do have an enclosing statement list,
> > +   but it would be incorrect to use it, as the size should
> > +   only be evaluated if the containing expression is
> > +   evaluated.  We might also be in the middle of an
> > +   expression with side effects on the pointed-to type size
> > +   "arguments" prior to the pointer declaration point and
> > +   the fake TYPE_DECL in the enclosing context would force
> > +   the size evaluation prior to the side effects.  We therefore
> > +   use BIND_EXPRs in TYPENAME contexts too.  */
> > +static void
> > +add_decl_expr(location_t loc, enum decl_context decl_context, tree type, 
> > tree *expr)
> > +{
> > +  tree bind = NULL_TREE;
> > +  if (decl_context == TYPENAME || decl_context == PARM || decl_context == 
> > FIELD)
> > +{
> > +  bind = build3 (BIND_EXPR, void_type_node, NULL_TREE, NULL_TREE, 
> > NULL_TREE);
> > +  TREE_SIDE_EFFECTS (bind) = 1;
> > +  BIND_EXPR_BODY (bind) = push_stmt_list ();
> > +  push_scope ();
> > +}
> > +
> > +  tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
> > +  pushdecl (decl);
> > +  DECL_ARTIFICIAL (decl) = 1;
> > +  add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
> > +  TYPE_NAME (type) = decl;
> > +
> > +  if (bind)
> > +{
> > +  pop_scope ();
> > +  BIND_EXPR_BODY (bind) = pop_stmt_list (BIND_EXPR_BODY (bind));
> > +  if (*expr)
> > +   *expr = build2 (COMPOUND_EXPR, void_type_node, *expr, bind);
> > +  else
> > +   *expr = bind;
> > +}
> > +}
> > +
> >  /* Given declspecs and a declarator,
> > determine the name and type of the object declared
> > and construct a ..._DECL node for it.
> > @@ -7474,58 +7523,9 @@ grokdeclarator (const struct c_declarator 
> > *declarator,
> > 
> >    This is expected to happen automatically when the pointed-to
> >    type has a name/declaration of it's own, but special 
> > attention
> > -  is required if the type is anonymous.
> > -
> > -  We attach an artificial TYPE_DECL to such pointed-to type
> > -  and arrange for it to be included in a DECL_EXPR.  

Re: [C PATCH v3] Fix ICEs related to VM types in C 2/2

2023-05-23 Thread Richard Biener via Gcc-patches
On Mon, May 22, 2023 at 7:24 PM Martin Uecker via Gcc-patches
 wrote:
>
>
>
> This version contains the middle-end changes for PR109450
> and test cases as before.  The main middle-end change is that
> we use gimplify_type_sizes also for parameters and remove
> the special code that also walked into pointers (which is
> incorrect).
>
> In addition, in the C FE this patch now also adds DECL_EXPR
> for vm-types which are pointed-to by parameters declared
> as arrays.  The new function created contains the exact
> code previously used only for regular pointers, and is
> now also called for parameters declared as arrays.
>
>
> Martin
>
>
>
>
>
>
>
> Fix ICEs related to VM types in C 2/2 [PR109450]
>
> Size expressions were sometimes lost and not gimplified correctly,
> leading to ICEs and incorrect evaluation order.  Fix this by 1) not
> recursing pointers when gimplifying parameters in the middle-end
> (the code is merged with gimplify_type_sizes), which is incorrect
> because it might access variables declared later for incomplete
> structs, and 2) adding a decl expr for variably-modified arrays
> that are pointed to by parameters declared as arrays.
>
> PR c/109450
>
> gcc/
> * c/c-decl.cc (add_decl_expr): New function.
> (grokdeclarator): Add decl expr for size expression in
> types pointed to by parameters declared as arrays.
> * function.cc (gimplify_parm_type): Remove function.
> (gimplify_parameters): Call gimplify_parm_sizes.
> * gimplify.cc (gimplify_type_sizes): Make function static.
> (gimplify_parm_sizes): New function.
>
> gcc/testsuite/
> * gcc.dg/pr109450-1.c: New test.
> * gcc.dg/pr109450-2.c: New test.
> * gcc.dg/vla-26.c: New test.
>
> diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
> index 494d3cf1747..c35347734b2 100644
> --- a/gcc/c/c-decl.cc
> +++ b/gcc/c/c-decl.cc
> @@ -6490,6 +6490,55 @@ smallest_type_quals_location (const location_t 
> *locations,
>return loc;
>  }
>
> +
> +/* We attach an artificial TYPE_DECL to pointed-to type
> +   and arrange for it to be included in a DECL_EXPR.  This
> +   forces the sizes evaluation at a safe point and ensures it
> +   is not deferred until e.g. within a deeper conditional context.
> +
> +   PARM contexts have no enclosing statement list that
> +   can hold the DECL_EXPR, so we need to use a BIND_EXPR
> +   instead, and add it to the list of expressions that
> +   need to be evaluated.
> +
> +   TYPENAME contexts do have an enclosing statement list,
> +   but it would be incorrect to use it, as the size should
> +   only be evaluated if the containing expression is
> +   evaluated.  We might also be in the middle of an
> +   expression with side effects on the pointed-to type size
> +   "arguments" prior to the pointer declaration point and
> +   the fake TYPE_DECL in the enclosing context would force
> +   the size evaluation prior to the side effects.  We therefore
> +   use BIND_EXPRs in TYPENAME contexts too.  */
> +static void
> +add_decl_expr(location_t loc, enum decl_context decl_context, tree type, 
> tree *expr)
> +{
> +  tree bind = NULL_TREE;
> +  if (decl_context == TYPENAME || decl_context == PARM || decl_context == 
> FIELD)
> +{
> +  bind = build3 (BIND_EXPR, void_type_node, NULL_TREE, NULL_TREE, 
> NULL_TREE);
> +  TREE_SIDE_EFFECTS (bind) = 1;
> +  BIND_EXPR_BODY (bind) = push_stmt_list ();
> +  push_scope ();
> +}
> +
> +  tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
> +  pushdecl (decl);
> +  DECL_ARTIFICIAL (decl) = 1;
> +  add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
> +  TYPE_NAME (type) = decl;
> +
> +  if (bind)
> +{
> +  pop_scope ();
> +  BIND_EXPR_BODY (bind) = pop_stmt_list (BIND_EXPR_BODY (bind));
> +  if (*expr)
> +   *expr = build2 (COMPOUND_EXPR, void_type_node, *expr, bind);
> +  else
> +   *expr = bind;
> +}
> +}
> +
>  /* Given declspecs and a declarator,
> determine the name and type of the object declared
> and construct a ..._DECL node for it.
> @@ -7474,58 +7523,9 @@ grokdeclarator (const struct c_declarator *declarator,
>
>This is expected to happen automatically when the pointed-to
>type has a name/declaration of it's own, but special attention
> -  is required if the type is anonymous.
> -
> -  We attach an artificial TYPE_DECL to such pointed-to type
> -  and arrange for it to be included in a DECL_EXPR.  This
> -  forces the sizes evaluation at a safe point and ensures it
> -  is not deferred until e.g. within a deeper conditional context.
> -
> -  PARM contexts have no enclosing statement list that
> -  can hold the DECL_EXPR, so we need to use a BIND_EXPR
> -  instead, and add it to 

Re: [C PATCH v3] Fix ICEs related to VM types in C 2/2

2023-05-22 Thread Joseph Myers
On Mon, 22 May 2023, Martin Uecker via Gcc-patches wrote:

> +static void
> +add_decl_expr(location_t loc, enum decl_context decl_context, tree type, 
> tree *expr)

Missing space before '(', and the line should be wrapped to be no more 
than 80 columns.

The C front-end changes are OK with those fixes.  The testsuite changes 
are also OK.

-- 
Joseph S. Myers
jos...@codesourcery.com


[C PATCH v3] Fix ICEs related to VM types in C 2/2

2023-05-22 Thread Martin Uecker via Gcc-patches



This version contains the middle-end changes for PR109450
and test cases as before.  The main middle-end change is that
we use gimplify_type_sizes also for parameters and remove
the special code that also walked into pointers (which is
incorrect).  

In addition, in the C FE this patch now also adds DECL_EXPR
for vm-types which are pointed-to by parameters declared
as arrays.  The new function created contains the exact
code previously used only for regular pointers, and is
now also called for parameters declared as arrays.


Martin







Fix ICEs related to VM types in C 2/2 [PR109450]

Size expressions were sometimes lost and not gimplified correctly,
leading to ICEs and incorrect evaluation order.  Fix this by 1) not
recursing pointers when gimplifying parameters in the middle-end
(the code is merged with gimplify_type_sizes), which is incorrect
because it might access variables declared later for incomplete
structs, and 2) adding a decl expr for variably-modified arrays
that are pointed to by parameters declared as arrays.

PR c/109450

gcc/
* c/c-decl.cc (add_decl_expr): New function.
(grokdeclarator): Add decl expr for size expression in
types pointed to by parameters declared as arrays.
* function.cc (gimplify_parm_type): Remove function.
(gimplify_parameters): Call gimplify_parm_sizes.
* gimplify.cc (gimplify_type_sizes): Make function static.
(gimplify_parm_sizes): New function.

gcc/testsuite/
* gcc.dg/pr109450-1.c: New test.
* gcc.dg/pr109450-2.c: New test.
* gcc.dg/vla-26.c: New test.

diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
index 494d3cf1747..c35347734b2 100644
--- a/gcc/c/c-decl.cc
+++ b/gcc/c/c-decl.cc
@@ -6490,6 +6490,55 @@ smallest_type_quals_location (const location_t 
*locations,
   return loc;
 }
 
+
+/* We attach an artificial TYPE_DECL to pointed-to type
+   and arrange for it to be included in a DECL_EXPR.  This
+   forces the sizes evaluation at a safe point and ensures it
+   is not deferred until e.g. within a deeper conditional context.
+
+   PARM contexts have no enclosing statement list that
+   can hold the DECL_EXPR, so we need to use a BIND_EXPR
+   instead, and add it to the list of expressions that
+   need to be evaluated.
+
+   TYPENAME contexts do have an enclosing statement list,
+   but it would be incorrect to use it, as the size should
+   only be evaluated if the containing expression is
+   evaluated.  We might also be in the middle of an
+   expression with side effects on the pointed-to type size
+   "arguments" prior to the pointer declaration point and
+   the fake TYPE_DECL in the enclosing context would force
+   the size evaluation prior to the side effects.  We therefore
+   use BIND_EXPRs in TYPENAME contexts too.  */
+static void
+add_decl_expr(location_t loc, enum decl_context decl_context, tree type, tree 
*expr)
+{
+  tree bind = NULL_TREE;
+  if (decl_context == TYPENAME || decl_context == PARM || decl_context == 
FIELD)
+{
+  bind = build3 (BIND_EXPR, void_type_node, NULL_TREE, NULL_TREE, 
NULL_TREE);
+  TREE_SIDE_EFFECTS (bind) = 1;
+  BIND_EXPR_BODY (bind) = push_stmt_list ();
+  push_scope ();
+}
+
+  tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
+  pushdecl (decl);
+  DECL_ARTIFICIAL (decl) = 1;
+  add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
+  TYPE_NAME (type) = decl;
+
+  if (bind)
+{
+  pop_scope ();
+  BIND_EXPR_BODY (bind) = pop_stmt_list (BIND_EXPR_BODY (bind));
+  if (*expr)
+   *expr = build2 (COMPOUND_EXPR, void_type_node, *expr, bind);
+  else
+   *expr = bind;
+}
+}
+
 /* Given declspecs and a declarator,
determine the name and type of the object declared
and construct a ..._DECL node for it.
@@ -7474,58 +7523,9 @@ grokdeclarator (const struct c_declarator *declarator,
 
   This is expected to happen automatically when the pointed-to
   type has a name/declaration of it's own, but special attention
-  is required if the type is anonymous.
-
-  We attach an artificial TYPE_DECL to such pointed-to type
-  and arrange for it to be included in a DECL_EXPR.  This
-  forces the sizes evaluation at a safe point and ensures it
-  is not deferred until e.g. within a deeper conditional context.
-
-  PARM contexts have no enclosing statement list that
-  can hold the DECL_EXPR, so we need to use a BIND_EXPR
-  instead, and add it to the list of expressions that
-  need to be evaluated.
-
-  TYPENAME contexts do have an enclosing statement list,
-  but it would be incorrect to use it, as the size should
-  only be evaluated if the containing expression is
-