Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support designated initialization style. #35

Merged
merged 1 commit into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions ctoxml/test.t/c99-struct-initializers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
struct point {
int x;
int y;
};

struct point3d {
int x;
int y;
int z;
};

int main() {
struct point p = { .y = 2, .x = 1 };
struct point3d p2 = { .z = 2, .x = 1, 5 }; // mixing both styles
}
54 changes: 54 additions & 0 deletions ctoxml/test.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -5331,3 +5331,57 @@
<struct ref="struct:s2"/>
</var>
</file>
$ ctoxml c99-struct-initializers.c
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<file>
<struct id="struct:point">
<field name="y">
<long/>
</field>
<field name="x">
<long/>
</field>
</struct>
<struct id="struct:point3d">
<field name="z">
<long/>
</field>
<field name="y">
<long/>
</field>
<field name="x">
<long/>
</field>
</struct>
<fundef id="main" store="auto">
<type>
<long/>
</type>
<body>
<var id="p" store="auto">
<struct ref="struct:point"/>
<compound>
<designated name="y">
<int>2</int>
</designated>
<designated name="x">
<int>1</int>
</designated>
</compound>
</var>
<var id="p2" store="auto">
<struct ref="struct:point3d"/>
<compound>
<designated name="z">
<int>2</int>
</designated>
<designated name="x">
<int>1</int>
</designated>
<int>5</int>
</compound>
</var>
<nop/>
</body>
</fundef>
</file>
2 changes: 2 additions & 0 deletions frontc/cabs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ and expression =
(** Pointer indirection through "->". *)
| GNU_BODY of body
(** GNU braces inside an expression. *)
| DESIGNATED of string * expression
(** Designated initialization, in compound constants only. *)
| EXPR_LINE of expression * string * int
(** Record the file and line of the expression. *)

Expand Down
18 changes: 17 additions & 1 deletion frontc/cparser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ enum_name: IDENT {($1, NOTHING)}

/*** Expressions ****/
init_expression:
LBRACE init_comma_expression RBRACE
LBRACE compound_comma_expression RBRACE
{CONSTANT (CONST_COMPOUND (List.rev $2))}
| expression
{$1}
Expand All @@ -793,6 +793,22 @@ init_expression
| init_comma_expression COMMA
{$1}
;
compound_expression:
LBRACE compound_comma_expression RBRACE
{CONSTANT (CONST_COMPOUND (List.rev $2))}
| expression
{$1}
| DOT type_name EQ expression
{ DESIGNATED ($2, $4) }
;
compound_comma_expression:
compound_expression
{[$1]}
| compound_comma_expression COMMA compound_expression
{$3::$1}
| compound_comma_expression COMMA
{$1}
;
opt_expression:
/* empty */
{NOTHING}
Expand Down
8 changes: 7 additions & 1 deletion frontc/cprint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ and print_old_params pars ell =
(*
** Expression printing
** Priorities
** 16 varaibles
** 16 variables
** 15 . -> [] call()
** 14 ++, -- (post)
** 13 ++ -- (pre) ~ ! - + & *(cast)
Expand Down Expand Up @@ -413,6 +413,7 @@ and get_operator exp =
| MEMBEROF _ -> ("", 15)
| MEMBEROFPTR _ -> ("", 15)
| GNU_BODY _ -> ("", 17)
| DESIGNATED _ -> ("", 15)
| EXPR_LINE (expr, _, _) -> get_operator expr

and print_comma_exps exps =
Expand Down Expand Up @@ -487,6 +488,11 @@ and print_expression (exp : expression) (lvl : int) =
print "(";
print_statement (BLOCK (decs, stat));
print ")"
| DESIGNATED (member, exp) ->
print ".";
print member;
print "=";
print_expression exp 16;
| EXPR_LINE (expr, _, _) ->
print_expression expr lvl in
if lvl > lvl' then print ")" else ()
Expand Down
2 changes: 2 additions & 0 deletions frontc/ctoxml.ml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ and convert_exp exp =
Cxml.new_elt "memberofptr" [("field", n)] [convert_exp b]
| GNU_BODY (d, s) ->
Cxml.new_elt "body" [] (convert_block (d, s))
| DESIGNATED (n, e) ->
Cxml.new_elt "designated" [("name", n)] [convert_exp e]
| EXPR_LINE (expr, file, line) ->
Cxml.new_elt "expr_line"
[("file", file); ("line", string_of_int line)]
Expand Down