Skip to content
Closed
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
60 changes: 60 additions & 0 deletions lib/compiler/aro_translate_c/ast.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,6 +63,7 @@ pub const Node = extern union {
warning,
@"struct",
@"union",
@"enum",
@"comptime",
@"defer",
array_init,
Expand DownExpand Up@@ -227,6 +228,8 @@ pub const Node = extern union {
/// [1]type{val} ** count
array_filler,

labeled_break,

pub const last_no_payload_tag = Tag.@"break";
pub const no_payload_count = @intFromEnum(last_no_payload_tag) + 1;

Expand DownExpand Up@@ -349,6 +352,7 @@ pub const Node = extern union {
.type,
.helpers_macro,
.import_c_builtin,
.labeled_break,
=> Payload.Value,
.discard => Payload.Discard,
.@"if" => Payload.If,
Expand All@@ -359,6 +363,7 @@ pub const Node = extern union {
.var_decl => Payload.VarDecl,
.func => Payload.Func,
.@"struct", .@"union" => Payload.Record,
.@"enum" => Payload.Enum,
.tuple => Payload.TupleInit,
.container_init => Payload.ContainerInit,
.container_init_dot => Payload.ContainerInitDot,
Expand DownExpand Up@@ -581,6 +586,11 @@ pub const Payload = struct {
};
};

pub const Enum = struct {
base: Payload,
data: []const []const u8,
};

pub const TupleInit = struct {
base: Payload,
data: []Node,
Expand DownExpand Up@@ -1965,6 +1975,40 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
return renderFieldAccess(c, lhs, payload.field_name);
},
.@"struct", .@"union" => return renderRecord(c, node),
.@"enum" => {
const payload = node.castTag(.@"enum").?.data;

const enum_tok = try c.addToken(.keyword_enum, "enum");
_ = try c.addToken(.l_brace, "{");
const members = try c.gpa.alloc(NodeIndex, payload.len);
defer c.gpa.free(members);

for (payload, members) |field, *member| {
const name_tok = try c.addIdentifier(field);

member.* = try c.addNode(.{
.tag = .container_field_init,
.main_token = name_tok,
.data = .{
.lhs = 0,
.rhs = 0,
},
});
_ = try c.addToken(.comma, ",");
}

_ = try c.addToken(.r_brace, "}");

const span = try c.listToSpan(members);
return c.addNode(.{
.tag = .container_decl_trailing,
.main_token = enum_tok,
.data = .{
.lhs = span.start,
.rhs = span.end,
},
});
},
.enum_constant => {
const payload = node.castTag(.enum_constant).?.data;

Expand DownExpand Up@@ -2113,6 +2157,20 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
},
};
},
.labeled_break => {
const payload = node.castTag(.labeled_break).?.data;
const tok = try c.addToken(.keyword_break, "break");
_ = try c.addToken(.colon, ":");
const break_label = try c.addIdentifier(payload);
return c.addNode(.{
.tag = .@"break",
.main_token = tok,
.data = .{
.lhs = break_label,
.rhs = 0,
},
});
},
.@"anytype" => unreachable, // Handled in renderParams
}
}
Expand DownExpand Up@@ -2458,6 +2516,7 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
.@"if",
.@"struct",
.@"union",
.@"enum",
.array_init,
.vector_zero_init,
.tuple,
Expand DownExpand Up@@ -2513,6 +2572,7 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
.assign,
.helpers_macro,
.import_c_builtin,
.labeled_break,
=> {
// these should never appear in places where grouping might be needed.
unreachable;
Expand Down
13 changes: 13 additions & 0 deletions src/clang.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -612,6 +612,11 @@ pub const GenericSelectionExpr = opaque {
extern fn ZigClangGenericSelectionExpr_getResultExpr(*const GenericSelectionExpr) *const Expr;
};

pub const GotoStmt = opaque {
pub const getLabel = ZigClangGotoStmt_getLabel;
extern fn ZigClangGotoStmt_getLabel(*const GotoStmt) *NamedDecl;
};

pub const IfStmt = opaque {
pub const getThen = ZigClangIfStmt_getThen;
extern fn ZigClangIfStmt_getThen(*const IfStmt) *const Stmt;
Expand DownExpand Up@@ -650,6 +655,14 @@ pub const IntegerLiteral = opaque {
extern fn ZigClangIntegerLiteral_getSignum(*const IntegerLiteral, *c_int, *const ASTContext) bool;
};

pub const LabelStmt = opaque {
pub const getName = ZigClangLabelStmt_getName;
extern fn ZigClangLabelStmt_getName(*const LabelStmt) [*:0]const u8;

pub const getSubStmt = ZigClangLabelStmt_getSubStmt;
extern fn ZigClangLabelStmt_getSubStmt(*const LabelStmt) *const Stmt;
};

/// This is just used as a namespace for a static method on clang's Lexer class; we don't directly
/// deal with Lexer objects
pub const Lexer = struct {
Expand Down
Loading