Skip to content

Commit

Permalink
Fix precedence of compound literals
Browse files Browse the repository at this point in the history
  • Loading branch information
dspinellis committed Jun 27, 2024
1 parent bd1d669 commit a3ab4ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
22 changes: 11 additions & 11 deletions src/parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,17 @@ primary_expression:
| '(' compound_statement ')'
{ $$ = $2; }

/* Compound literal; C99 feature */
| '(' type_name ')' { saved_initializer_stacks.push(initializer_stack); initializer_stack = InitializerStack(); initializer_expect($2); } braced_initializer
{
if (DP()) {
cout << Fchar::get_path() << ':' << Fchar::get_line_num() << ": ";
cout << "Type of compound literal " << $2 << "\n";
}
$$ = $2;
initializer_stack = saved_initializer_stacks.top();
saved_initializer_stacks.pop();
}
| generic_selection
;

Expand Down Expand Up @@ -667,17 +678,6 @@ cast_expression:
if (DP())
cout << "cast to " << $2 << "\n";
}
/* Compound literal; C99 feature */
| '(' type_name ')' { saved_initializer_stacks.push(initializer_stack); initializer_stack = InitializerStack(); initializer_expect($2); } braced_initializer
{
if (DP()) {
cout << Fchar::get_path() << ':' << Fchar::get_line_num() << ": ";
cout << "Type of compound literal " << $2 << "\n";
}
$$ = $2;
initializer_stack = saved_initializer_stacks.top();
saved_initializer_stacks.pop();
}
;

multiplicative_expression:
Expand Down
3 changes: 2 additions & 1 deletion src/test/c/c19-initializers.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ main()
struct empty2 k = {}; /* Empty initializer */
k = (struct empty2){}; /* Empty compound literal */

int i;
// Compound literal used as a primary expression
int i = ((const int[]){ 64, 64, 64, 63 })[2];

typedef struct empty {} ES;
ES k2 = {}; /* Empty initializer */
Expand Down

0 comments on commit a3ab4ba

Please sign in to comment.