Skip to content

Commit

Permalink
Parser: set type correctly for _Generic selections
Browse files Browse the repository at this point in the history
  • Loading branch information
ehaas committed Aug 13, 2024
1 parent 063b977 commit fe293db
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/aro/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8745,6 +8745,7 @@ fn genericSelection(p: *Parser) Error!Result {
try p.list_buf.insert(list_buf_top + 1, try p.addNode(.{
.tag = .generic_default_expr,
.data = .{ .un = default.node },
.ty = default.ty,
}));
chosen = default;
} else {
Expand All @@ -8755,11 +8756,13 @@ fn genericSelection(p: *Parser) Error!Result {
try p.list_buf.insert(list_buf_top + 1, try p.addNode(.{
.tag = .generic_association_expr,
.data = .{ .un = chosen.node },
.ty = chosen.ty,
}));
if (default_tok != null) {
try p.list_buf.append(try p.addNode(.{
.tag = .generic_default_expr,
.data = .{ .un = chosen.node },
.data = .{ .un = default.node },
.ty = default.ty,
}));
}
}
Expand Down
38 changes: 38 additions & 0 deletions test/cases/ast/generic ast.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var: 'int'
name: x
init:
generic_expr: 'int'
controlling:
int_literal: 'int' (value: 5)
chosen:
generic_association_expr: 'int'
int_literal: 'int' (value: 42)
rest:
generic_association_expr: 'double'
float_literal: 'double' (value: 32.5)

var: 'int'
name: y
init:
generic_expr: 'int'
controlling:
int_literal: 'int' (value: 5)
chosen:
generic_association_expr: 'int'
int_literal: 'int' (value: 42)
rest:
generic_association_expr: 'double'
float_literal: 'double' (value: 32.5)
generic_default_expr: '[7]char'
string_literal_expr: '[7]char' lvalue (value: "string")

var: 'double'
name: z
init:
implicit_cast: (int_to_float) 'double'
generic_expr_one: 'int'
controlling:
int_literal: 'int' (value: 5)
chosen:
int_literal: 'int' (value: 32)

14 changes: 14 additions & 0 deletions test/cases/generic ast.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
int x = _Generic(5,
int: 42,
double: 32.5
);

int y = _Generic(5,
int: 42,
double: 32.5,
default: "string"
);

double z = _Generic(5,
default: 32
);

0 comments on commit fe293db

Please sign in to comment.