Skip to content
Open
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
25 changes: 13 additions & 12 deletions src/base/CommonEnv.zig
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub fn setNodeIndexById(self: *CommonEnv, gpa: std.mem.Allocator, ident_idx: Ide
pub fn getRegionInfo(self: *const CommonEnv, region: Region) !RegionInfo {
return RegionInfo.position(
self.source,
self.line_starts.items.items,
self.line_starts.items(),
region.start.offset,
region.end.offset,
);
Expand All @@ -213,7 +213,7 @@ pub fn calcRegionInfo(self: *const CommonEnv, region: Region) RegionInfo {

const info = RegionInfo.position(
source,
self.line_starts.items.items,
self.line_starts.items(),
region.start.offset,
region.end.offset,
) catch {
Expand Down Expand Up @@ -256,7 +256,7 @@ pub fn calcLineStarts(self: *CommonEnv, gpa: std.mem.Allocator) !void {

/// Returns all line start positions for source code position mapping.
pub fn getLineStartsAll(self: *const CommonEnv) []const u32 {
return self.line_starts.items.items;
return self.line_starts.items();
}

/// Get the source text for a given region
Expand All @@ -267,9 +267,10 @@ pub fn getSource(self: *const CommonEnv, region: Region) []const u8 {
/// Get the source line for a given region
pub fn getSourceLine(self: *const CommonEnv, region: Region) ![]const u8 {
const region_info = try self.getRegionInfo(region);
const line_start = self.line_starts.items.items[region_info.start_line_idx];
const line_end = if (region_info.start_line_idx + 1 < self.line_starts.items.items.len)
self.line_starts.items.items[region_info.start_line_idx + 1]
const line_starts_slice = self.line_starts.items();
const line_start = line_starts_slice[region_info.start_line_idx];
const line_end = if (region_info.start_line_idx + 1 < line_starts_slice.len)
line_starts_slice[region_info.start_line_idx + 1]
else
self.source.len;

Expand Down Expand Up @@ -329,10 +330,10 @@ test "CommonEnv.Serialized roundtrip" {
try testing.expectEqualStrings("world", env.getIdent(world_idx));

try testing.expectEqual(@as(usize, 1), env.exposed_items.count());
try testing.expectEqual(@as(usize, 3), env.line_starts.len());
try testing.expectEqual(@as(u32, 0), env.line_starts.items.items[0]);
try testing.expectEqual(@as(u32, 10), env.line_starts.items.items[1]);
try testing.expectEqual(@as(u32, 20), env.line_starts.items.items[2]);
try testing.expectEqual(@as(u32, 3), env.line_starts.len());
try testing.expectEqual(@as(u32, 0), env.line_starts.items()[0]);
try testing.expectEqual(@as(u32, 10), env.line_starts.items()[1]);
try testing.expectEqual(@as(u32, 20), env.line_starts.items()[2]);

try testing.expectEqualStrings(source, env.source);
}
Expand Down Expand Up @@ -474,11 +475,11 @@ test "CommonEnv.Serialized roundtrip with large data" {
try testing.expectEqualStrings("string_literal_24", env.getString(string_indices.items[24]));

// Verify line starts
try testing.expectEqual(@as(u32, 0), env.line_starts.items.items[0]);
try testing.expectEqual(@as(u32, 0), env.line_starts.items()[0]);
// Calculate the actual expected value for the second line start
const first_line = "Line 0: This is a test line with some content\n";
const expected_second_line_start = first_line.len;
try testing.expectEqual(@as(u32, expected_second_line_start), env.line_starts.items.items[1]);
try testing.expectEqual(@as(u32, expected_second_line_start), env.line_starts.items()[1]);
}

test "CommonEnv.Serialized roundtrip with special characters" {
Expand Down
48 changes: 24 additions & 24 deletions src/base/RegionInfo.zig
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,17 @@ test "lineIdx" {
_ = try line_starts.append(gpa, 20);
_ = try line_starts.append(gpa, 30);

try std.testing.expectEqual(0, RegionInfo.lineIdx(line_starts.items.items, 0));
try std.testing.expectEqual(0, RegionInfo.lineIdx(line_starts.items.items, 5));
try std.testing.expectEqual(0, RegionInfo.lineIdx(line_starts.items.items, 9));
try std.testing.expectEqual(1, RegionInfo.lineIdx(line_starts.items.items, 10));
try std.testing.expectEqual(1, RegionInfo.lineIdx(line_starts.items.items, 15));
try std.testing.expectEqual(1, RegionInfo.lineIdx(line_starts.items.items, 19));
try std.testing.expectEqual(2, RegionInfo.lineIdx(line_starts.items.items, 20));
try std.testing.expectEqual(2, RegionInfo.lineIdx(line_starts.items.items, 25));
try std.testing.expectEqual(2, RegionInfo.lineIdx(line_starts.items.items, 29));
try std.testing.expectEqual(3, RegionInfo.lineIdx(line_starts.items.items, 30));
try std.testing.expectEqual(3, RegionInfo.lineIdx(line_starts.items.items, 35));
try std.testing.expectEqual(0, RegionInfo.lineIdx(line_starts.items(), 0));
try std.testing.expectEqual(0, RegionInfo.lineIdx(line_starts.items(), 5));
try std.testing.expectEqual(0, RegionInfo.lineIdx(line_starts.items(), 9));
try std.testing.expectEqual(1, RegionInfo.lineIdx(line_starts.items(), 10));
try std.testing.expectEqual(1, RegionInfo.lineIdx(line_starts.items(), 15));
try std.testing.expectEqual(1, RegionInfo.lineIdx(line_starts.items(), 19));
try std.testing.expectEqual(2, RegionInfo.lineIdx(line_starts.items(), 20));
try std.testing.expectEqual(2, RegionInfo.lineIdx(line_starts.items(), 25));
try std.testing.expectEqual(2, RegionInfo.lineIdx(line_starts.items(), 29));
try std.testing.expectEqual(3, RegionInfo.lineIdx(line_starts.items(), 30));
try std.testing.expectEqual(3, RegionInfo.lineIdx(line_starts.items(), 35));
}

test "columnIdx" {
Expand All @@ -146,12 +146,12 @@ test "columnIdx" {
_ = try line_starts.append(gpa, 10);
_ = try line_starts.append(gpa, 20);

try std.testing.expectEqual(0, RegionInfo.columnIdx(line_starts.items.items, 0, 0));
try std.testing.expectEqual(5, RegionInfo.columnIdx(line_starts.items.items, 0, 5));
try std.testing.expectEqual(9, RegionInfo.columnIdx(line_starts.items.items, 0, 9));
try std.testing.expectEqual(0, RegionInfo.columnIdx(line_starts.items(), 0, 0));
try std.testing.expectEqual(5, RegionInfo.columnIdx(line_starts.items(), 0, 5));
try std.testing.expectEqual(9, RegionInfo.columnIdx(line_starts.items(), 0, 9));

try std.testing.expectEqual(0, RegionInfo.columnIdx(line_starts.items.items, 1, 10));
try std.testing.expectEqual(5, RegionInfo.columnIdx(line_starts.items.items, 1, 15));
try std.testing.expectEqual(0, RegionInfo.columnIdx(line_starts.items(), 1, 10));
try std.testing.expectEqual(5, RegionInfo.columnIdx(line_starts.items(), 1, 15));
}

test "getLineText" {
Expand All @@ -165,10 +165,10 @@ test "getLineText" {
_ = try line_starts.append(gpa, 6);
_ = try line_starts.append(gpa, 12);

try std.testing.expectEqualStrings("line0", RegionInfo.getLineText(source, line_starts.items.items, 0, 0));
try std.testing.expectEqualStrings("line1", RegionInfo.getLineText(source, line_starts.items.items, 1, 1));
try std.testing.expectEqualStrings("line0\nline1", RegionInfo.getLineText(source, line_starts.items.items, 0, 1));
try std.testing.expectEqualStrings("line2", RegionInfo.getLineText(source, line_starts.items.items, 2, 2));
try std.testing.expectEqualStrings("line0", RegionInfo.getLineText(source, line_starts.items(), 0, 0));
try std.testing.expectEqualStrings("line1", RegionInfo.getLineText(source, line_starts.items(), 1, 1));
try std.testing.expectEqualStrings("line0\nline1", RegionInfo.getLineText(source, line_starts.items(), 0, 1));
try std.testing.expectEqualStrings("line2", RegionInfo.getLineText(source, line_starts.items(), 2, 2));
}

test "get" {
Expand All @@ -182,17 +182,17 @@ test "get" {
_ = try line_starts.append(gpa, 6);
_ = try line_starts.append(gpa, 12);

const info1 = try RegionInfo.position(source, line_starts.items.items, 2, 4);
const info1 = try RegionInfo.position(source, line_starts.items(), 2, 4);
try std.testing.expectEqual(0, info1.start_line_idx);
try std.testing.expectEqual(2, info1.start_col_idx);
try std.testing.expectEqual(0, info1.end_line_idx);
try std.testing.expectEqual(4, info1.end_col_idx);
try std.testing.expectEqualStrings("line0", info1.calculateLineText(source, line_starts.items.items));
try std.testing.expectEqualStrings("line0", info1.calculateLineText(source, line_starts.items()));

const info2 = try RegionInfo.position(source, line_starts.items.items, 8, 10);
const info2 = try RegionInfo.position(source, line_starts.items(), 8, 10);
try std.testing.expectEqual(1, info2.start_line_idx);
try std.testing.expectEqual(2, info2.start_col_idx);
try std.testing.expectEqual(1, info2.end_line_idx);
try std.testing.expectEqual(4, info2.end_col_idx);
try std.testing.expectEqualStrings("line1", info2.calculateLineText(source, line_starts.items.items));
try std.testing.expectEqualStrings("line1", info2.calculateLineText(source, line_starts.items()));
}
29 changes: 16 additions & 13 deletions src/base/SmallStringInterner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ pub fn initCapacity(gpa: std.mem.Allocator, capacity: usize) std.mem.Allocator.E

// Initialize hash table with all zeros (Idx.unused)
self.hash_table = try collections.SafeList(Idx).initCapacity(gpa, hash_table_capacity);
try self.hash_table.items.ensureTotalCapacityPrecise(gpa, hash_table_capacity);
self.hash_table.items.items.len = hash_table_capacity;
@memset(self.hash_table.items.items, .unused);
try self.hash_table.ensureTotalCapacityPrecise(gpa, hash_table_capacity);
self.hash_table.setLen(@intCast(hash_table_capacity));
@memset(self.hash_table.rawCapacitySlice(), .unused);

return self;
}
Expand All @@ -77,8 +77,11 @@ pub fn findStringOrSlot(self: *const SmallStringInterner, string: []const u8) st
const table_size = self.hash_table.len();
var slot: usize = @intCast(hash % table_size);

const hash_table_slice = self.hash_table.items();
const bytes_slice = self.bytes.items();

while (true) {
const idx_at_slot = self.hash_table.items.items[slot];
const idx_at_slot = hash_table_slice[slot];

if (idx_at_slot == .unused) {
// Empty slot - string not found
Expand All @@ -92,9 +95,9 @@ pub fn findStringOrSlot(self: *const SmallStringInterner, string: []const u8) st
// If the stored string would have had to go past the end of bytes,
// they must not be equal. Also if there isn't a null terminator
// right where we expect, they must not be equal.
if (stored_end < self.bytes.len() and self.bytes.items.items[stored_end] == 0) {
if (stored_end < self.bytes.len() and bytes_slice[stored_end] == 0) {
// With that out of the way, we can safely compare the string contents.
if (std.mem.eql(u8, string, self.bytes.items.items[stored_idx..stored_end])) {
if (std.mem.eql(u8, string, bytes_slice[stored_idx..stored_end])) {
// Found the string!
return .{ .idx = idx_at_slot, .slot = slot };
}
Expand All @@ -112,17 +115,17 @@ fn resizeHashTable(self: *SmallStringInterner, gpa: std.mem.Allocator) std.mem.A

// Create new hash table initialized to zeros
self.hash_table = try collections.SafeList(Idx).initCapacity(gpa, new_size);
try self.hash_table.items.ensureTotalCapacityPrecise(gpa, new_size);
self.hash_table.items.items.len = new_size;
@memset(self.hash_table.items.items, .unused);
try self.hash_table.ensureTotalCapacityPrecise(gpa, new_size);
self.hash_table.setLen(@intCast(new_size));
@memset(self.hash_table.rawCapacitySlice(), .unused);

// Rehash all existing entries
for (old_table.items.items) |idx| {
for (old_table.items()) |idx| {
if (idx != .unused) {
// Get the string for this index
const string = self.getText(idx);
const result = self.findStringOrSlot(string);
self.hash_table.items.items[@intCast(result.slot)] = idx;
self.hash_table.items()[@intCast(result.slot)] = idx;
}
}

Expand Down Expand Up @@ -150,7 +153,7 @@ pub fn insert(self: *SmallStringInterner, gpa: std.mem.Allocator, string: []cons
_ = try self.bytes.append(gpa, 0);

// Add to hash table
self.hash_table.items.items[@intCast(result.slot)] = new_offset;
self.hash_table.items()[@intCast(result.slot)] = new_offset;
self.entry_count += 1;

return new_offset;
Expand All @@ -165,7 +168,7 @@ pub fn contains(self: *const SmallStringInterner, string: []const u8) bool {

/// Get a reference to the text for an interned string.
pub fn getText(self: *const SmallStringInterner, idx: Idx) []u8 {
const bytes_slice = self.bytes.items.items;
const bytes_slice = self.bytes.items();
const start = @intFromEnum(idx);

return std.mem.sliceTo(bytes_slice[start..], 0);
Expand Down
5 changes: 3 additions & 2 deletions src/base/StringLiteral.zig
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ pub const Store = struct {
/// Get a string literal's text from this `Store`.
pub fn get(self: *const Store, idx: Idx) []u8 {
const idx_u32: u32 = @intCast(@intFromEnum(idx));
const str_len = std.mem.bytesAsValue(u32, self.buffer.items.items[idx_u32 - 4 .. idx_u32]).*;
return self.buffer.items.items[idx_u32 .. idx_u32 + str_len];
const buffer_slice = self.buffer.items();
const str_len = std.mem.bytesAsValue(u32, buffer_slice[idx_u32 - 4 .. idx_u32]).*;
return buffer_slice[idx_u32 .. idx_u32 + str_len];
}

/// Serialize this Store to the given CompactWriter. The resulting Store
Expand Down
5 changes: 2 additions & 3 deletions src/build/builtin_compiler/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ fn replaceStrIsEmptyWithLowLevel(env: *ModuleEnv) !std.ArrayList(CIR.Def.Idx) {
const extra_start = def_node.data_1;

// Update the expr field (at extra_start + 1)
env.store.extra_data.items.items[extra_start + 1] = @intFromEnum(expr_idx);
env.store.extra_data.items()[extra_start + 1] = @intFromEnum(expr_idx);

// Track this replaced def index
try new_def_indices.append(gpa, def_idx);
Expand Down Expand Up @@ -839,8 +839,7 @@ fn compileModule(
// Copy old def indices
var i: u32 = 0;
while (i < old_def_count) : (i += 1) {
const idx = @as(collections.SafeList(u32).Idx, @enumFromInt(old_span.start + i));
const old_def_idx = module_env.store.extra_data.get(idx).*;
const old_def_idx = module_env.store.extra_data.items()[old_span.start + i];
_ = try module_env.store.extra_data.append(gpa, old_def_idx);
}

Expand Down
Loading
Loading