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 {...@@ -253,17 +253,12 @@ fn transDecl(c: *Context, scope: *Scope, decl: NodeIndex) !void {
253 .struct_decl_two,253 .struct_decl_two,
254 .union_decl_two,254 .union_decl_two,
255 => {255 => {
256 var fields = [2]NodeIndex{ data.bin.lhs, data.bin.rhs };256 try transRecordDecl(c, scope, node_ty[@intFromEnum(decl)]);
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]);
261 },257 },
262 .struct_decl,258 .struct_decl,
263 .union_decl,259 .union_decl,
264 => {260 => {
265 const fields = c.tree.data[data.range.start..data.range.end];261 try transRecordDecl(c, scope, node_ty[@intFromEnum(decl)]);
266 try transRecordDecl(c, scope, decl, fields);
267 },262 },
268263
269 .enum_decl_two => {264 .enum_decl_two => {
...@@ -333,16 +328,14 @@ fn mangleWeakGlobalName(c: *Context, want_name: []const u8) ![]const u8 {...@@ -333,16 +328,14 @@ fn mangleWeakGlobalName(c: *Context, want_name: []const u8) ![]const u8 {
333 return cur_name;328 return cur_name;
334}329}
335330
336fn transRecordDecl(c: *Context, scope: *Scope, record_node: NodeIndex, field_nodes: []const NodeIndex) Error!void {331fn transRecordDecl(c: *Context, scope: *Scope, record_ty: Type) Error!void {
337 const node_types = c.tree.nodes.items(.ty);332 const record_decl = record_ty.getRecord().?;
338 const raw_record_ty = node_types[@intFromEnum(record_node)];
339 const record_decl = raw_record_ty.getRecord().?;
340 if (c.decl_table.get(@intFromPtr(record_decl))) |_|333 if (c.decl_table.get(@intFromPtr(record_decl))) |_|
341 return; // Avoid processing this decl twice334 return; // Avoid processing this decl twice
342 const toplevel = scope.id == .root;335 const toplevel = scope.id == .root;
343 const bs: *Scope.Block = if (!toplevel) try scope.findBlockScope(c) else undefined;336 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";
346 const container_kind_name: []const u8 = @tagName(container_kind);339 const container_kind_name: []const u8 = @tagName(container_kind);
347340
348 var is_unnamed = false;341 var is_unnamed = false;
...@@ -353,7 +346,7 @@ fn transRecordDecl(c: *Context, scope: *Scope, record_node: NodeIndex, field_nod...@@ -353,7 +346,7 @@ fn transRecordDecl(c: *Context, scope: *Scope, record_node: NodeIndex, field_nod
353 bare_name = typedef_name;346 bare_name = typedef_name;
354 name = typedef_name;347 name = typedef_name;
355 } else {348 } else {
356 if (raw_record_ty.isAnonymousRecord(c.comp)) {349 if (record_ty.isAnonymousRecord(c.comp)) {
357 bare_name = try std.fmt.allocPrint(c.arena, "unnamed_{d}", .{c.getMangle()});350 bare_name = try std.fmt.allocPrint(c.arena, "unnamed_{d}", .{c.getMangle()});
358 is_unnamed = true;351 is_unnamed = true;
359 }352 }
...@@ -380,17 +373,10 @@ fn transRecordDecl(c: *Context, scope: *Scope, record_node: NodeIndex, field_nod...@@ -380,17 +373,10 @@ fn transRecordDecl(c: *Context, scope: *Scope, record_node: NodeIndex, field_nod
380 // layout, then we can just use a simple `extern` type. If it does have attributes,373 // layout, then we can just use a simple `extern` type. If it does have attributes,
381 // then we need to inspect the layout and assign an `align` value for each field.374 // then we need to inspect the layout and assign an `align` value for each field.
382 const has_alignment_attributes = record_decl.field_attributes != null or375 const has_alignment_attributes = record_decl.field_attributes != null or
383 raw_record_ty.hasAttribute(.@"packed") or376 record_ty.hasAttribute(.@"packed") or
384 raw_record_ty.hasAttribute(.aligned);377 record_ty.hasAttribute(.aligned);
385 const head_field_alignment: ?c_uint = if (has_alignment_attributes) headFieldAlignment(record_decl) else null;378 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
394 for (record_decl.fields, 0..) |field, field_index| {380 for (record_decl.fields, 0..) |field, field_index| {
395 const field_loc = field.name_tok;381 const field_loc = field.name_tok;
396382
...@@ -742,6 +728,7 @@ fn transType(c: *Context, scope: *Scope, raw_ty: Type, qual_handling: Type.QualH...@@ -742,6 +728,7 @@ fn transType(c: *Context, scope: *Scope, raw_ty: Type, qual_handling: Type.QualH
742 const name_id = c.mapper.lookup(record_decl.name);728 const name_id = c.mapper.lookup(record_decl.name);
743 if (c.weak_global_names.contains(name_id)) trans_scope = &c.global_scope.base;729 if (c.weak_global_names.contains(name_id)) trans_scope = &c.global_scope.base;
744 }730 }
731 try transRecordDecl(c, trans_scope, ty);
745 const name = c.decl_table.get(@intFromPtr(ty.data.record)).?;732 const name = c.decl_table.get(@intFromPtr(ty.data.record)).?;
746 return ZigTag.identifier.create(c.arena, name);733 return ZigTag.identifier.create(c.arena, name);
747 },734 },