authorgravatar for evan@lagerdata.comEvan Haas <evan@lagerdata.com> 2024-07-30 18:24:16-07:00
committergravatar for evan@lagerdata.comEvan Haas <evan@lagerdata.com> 2024-07-31 09:35:07-07:00
log055077f9dd0091a0e071243e10e4d2500038d0be
treeea49d2bb081fd328baf005b8f5a69ab923302e24
parentc57fcd1db536591ecdee8c9ec497c8ea667c57f0
signaturebadge-check Signed by SSH key SHA256:cf2/TFgSxv2uRX26INvFSw25Prr1Dy9H8MiRXgLpok4

aro_translate_c: improve record translation

Move field record decl translation into `transType` instead of `transDecl`

1 files changed, 9 insertions(+), 22 deletions(-)

lib/compiler/aro_translate_c.zig+9-22
......@@ -253,17 +253,12 @@ fn transDecl(c: *Context, scope: *Scope, decl: NodeIndex) !void {
253253 .struct_decl_two,
254254 .union_decl_two,
255255 => {
256 var fields = [2]NodeIndex{ data.bin.lhs, data.bin.rhs };
257 var field_count: u2 = 0;
258 if (fields[0] != .none) field_count += 1;
259 if (fields[1] != .none) field_count += 1;
260 try transRecordDecl(c, scope, decl, fields[0..field_count]);
256 try transRecordDecl(c, scope, node_ty[@intFromEnum(decl)]);
261257 },
262258 .struct_decl,
263259 .union_decl,
264260 => {
265 const fields = c.tree.data[data.range.start..data.range.end];
266 try transRecordDecl(c, scope, decl, fields);
261 try transRecordDecl(c, scope, node_ty[@intFromEnum(decl)]);
267262 },
268263
269264 .enum_decl_two => {
......@@ -333,16 +328,14 @@ fn mangleWeakGlobalName(c: *Context, want_name: []const u8) ![]const u8 {
333328 return cur_name;
334329}
335330
336fn transRecordDecl(c: *Context, scope: *Scope, record_node: NodeIndex, field_nodes: []const NodeIndex) Error!void {
337 const node_types = c.tree.nodes.items(.ty);
338 const raw_record_ty = node_types[@intFromEnum(record_node)];
339 const record_decl = raw_record_ty.getRecord().?;
331fn transRecordDecl(c: *Context, scope: *Scope, record_ty: Type) Error!void {
332 const record_decl = record_ty.getRecord().?;
340333 if (c.decl_table.get(@intFromPtr(record_decl))) |_|
341334 return; // Avoid processing this decl twice
342335 const toplevel = scope.id == .root;
343336 const bs: *Scope.Block = if (!toplevel) try scope.findBlockScope(c) else undefined;
344337
345 const container_kind: ZigTag = if (raw_record_ty.is(.@"union")) .@"union" else .@"struct";
338 const container_kind: ZigTag = if (record_ty.is(.@"union")) .@"union" else .@"struct";
346339 const container_kind_name: []const u8 = @tagName(container_kind);
347340
348341 var is_unnamed = false;
......@@ -353,7 +346,7 @@ fn transRecordDecl(c: *Context, scope: *Scope, record_node: NodeIndex, field_nod
353346 bare_name = typedef_name;
354347 name = typedef_name;
355348 } else {
356 if (raw_record_ty.isAnonymousRecord(c.comp)) {
349 if (record_ty.isAnonymousRecord(c.comp)) {
357350 bare_name = try std.fmt.allocPrint(c.arena, "unnamed_{d}", .{c.getMangle()});
358351 is_unnamed = true;
359352 }
......@@ -380,17 +373,10 @@ fn transRecordDecl(c: *Context, scope: *Scope, record_node: NodeIndex, field_nod
380373 // layout, then we can just use a simple `extern` type. If it does have attributes,
381374 // then we need to inspect the layout and assign an `align` value for each field.
382375 const has_alignment_attributes = record_decl.field_attributes != null or
383 raw_record_ty.hasAttribute(.@"packed") or
384 raw_record_ty.hasAttribute(.aligned);
376 record_ty.hasAttribute(.@"packed") or
377 record_ty.hasAttribute(.aligned);
385378 const head_field_alignment: ?c_uint = if (has_alignment_attributes) headFieldAlignment(record_decl) else null;
386379
387 // Iterate over field nodes so that we translate any type decls included in this record decl.
388 // TODO: Move this logic into `fn transType()` instead of handling decl translation here.
389 for (field_nodes) |field_node| {
390 const field_raw_ty = node_types[@intFromEnum(field_node)];
391 if (field_raw_ty.isEnumOrRecord()) try transDecl(c, scope, field_node);
392 }
393
394380 for (record_decl.fields, 0..) |field, field_index| {
395381 const field_loc = field.name_tok;
396382
......@@ -742,6 +728,7 @@ fn transType(c: *Context, scope: *Scope, raw_ty: Type, qual_handling: Type.QualH
742728 const name_id = c.mapper.lookup(record_decl.name);
743729 if (c.weak_global_names.contains(name_id)) trans_scope = &c.global_scope.base;
744730 }
731 try transRecordDecl(c, trans_scope, ty);
745732 const name = c.decl_table.get(@intFromPtr(ty.data.record)).?;
746733 return ZigTag.identifier.create(c.arena, name);
747734 },