Skip to content

Commit

Permalink
Support C99 array declarator syntax involving static and * (#539)
Browse files Browse the repository at this point in the history
E.g. `int x[static 10]` or `int x [*]`.
Treated like `int x[10]` and `int x[]` respectively.

Fixes: #531
  • Loading branch information
xavierleroy authored Dec 2, 2024
1 parent 5e445cd commit 8408602
Show file tree
Hide file tree
Showing 3 changed files with 1,036 additions and 781 deletions.
43 changes: 39 additions & 4 deletions cparser/Parser.vy
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,23 @@ direct_declarator:
| decl = direct_declarator LBRACK RBRACK
{ let 'Cabs.Name name typ attr loc := decl in
Cabs.Name name (Cabs.ARRAY typ [] None) attr loc }
(*| direct_declarator LBRACK ... STATIC ... RBRACK
| direct_declarator LBRACK STAR RBRACK*)
| decl = direct_declarator LBRACK STATIC quallst = type_qualifier_list
expr = assignment_expression RBRACK
{ let 'Cabs.Name name typ attr loc := decl in
Cabs.Name name (Cabs.ARRAY typ (rev' quallst) (Some (fst expr))) attr loc }
| decl = direct_declarator LBRACK STATIC expr = assignment_expression RBRACK
{ let 'Cabs.Name name typ attr loc := decl in
Cabs.Name name (Cabs.ARRAY typ [] (Some (fst expr))) attr loc }
| decl = direct_declarator LBRACK quallst = type_qualifier_list STATIC
expr = assignment_expression RBRACK
{ let 'Cabs.Name name typ attr loc := decl in
Cabs.Name name (Cabs.ARRAY typ (rev' quallst) (Some (fst expr))) attr loc }
| decl = direct_declarator LBRACK quallst = type_qualifier_list STAR RBRACK
{ let 'Cabs.Name name typ attr loc := decl in
Cabs.Name name (Cabs.ARRAY typ (rev' quallst) None) attr loc }
| decl = direct_declarator LBRACK STAR RBRACK
{ let 'Cabs.Name name typ attr loc := decl in
Cabs.Name name (Cabs.ARRAY typ [] None) attr loc }
| decl = direct_declarator LPAREN params = parameter_type_list RPAREN
{ let 'Cabs.Name name typ attr loc := decl in
Cabs.Name name (Cabs.PROTO typ params) attr loc }
Expand Down Expand Up @@ -739,8 +754,28 @@ direct_abstract_declarator:
{ Cabs.ARRAY typ [] None }
| LBRACK RBRACK
{ Cabs.ARRAY Cabs.JUSTBASE [] None }
(*| direct_abstract_declarator? LBRACK STAR RBRACK*)
(*| direct_abstract_declarator? LBRACK ... STATIC ... RBRACK*)
| typ = direct_abstract_declarator LBRACK STATIC cvspec = type_qualifier_list
expr = assignment_expression RBRACK
{ Cabs.ARRAY typ cvspec (Some (fst expr)) }
| LBRACK STATIC cvspec = type_qualifier_list expr = assignment_expression RBRACK
{ Cabs.ARRAY Cabs.JUSTBASE cvspec (Some (fst expr)) }
| typ = direct_abstract_declarator LBRACK STATIC expr = assignment_expression RBRACK
{ Cabs.ARRAY typ [] (Some (fst expr)) }
| LBRACK STATIC expr = assignment_expression RBRACK
{ Cabs.ARRAY Cabs.JUSTBASE [] (Some (fst expr)) }
| typ = direct_abstract_declarator LBRACK cvspec = type_qualifier_list STATIC
expr = assignment_expression RBRACK
{ Cabs.ARRAY typ cvspec (Some (fst expr)) }
| LBRACK cvspec = type_qualifier_list STATIC expr = assignment_expression RBRACK
{ Cabs.ARRAY Cabs.JUSTBASE cvspec (Some (fst expr)) }
| typ = direct_abstract_declarator LBRACK cvspec = type_qualifier_list STAR RBRACK
{ Cabs.ARRAY typ cvspec None }
| typ = direct_abstract_declarator LBRACK STAR RBRACK
{ Cabs.ARRAY typ [] None }
| LBRACK cvspec = type_qualifier_list STAR RBRACK
{ Cabs.ARRAY Cabs.JUSTBASE cvspec None }
| LBRACK STAR RBRACK
{ Cabs.ARRAY Cabs.JUSTBASE [] None }
| typ = direct_abstract_declarator LPAREN params = parameter_type_list RPAREN
{ Cabs.PROTO typ params }
| LPAREN params = parameter_type_list RPAREN
Expand Down
Loading

0 comments on commit 8408602

Please sign in to comment.