aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-08-04 20:53:47 +0000
committerGitHub <noreply@github.com>2020-08-04 20:53:47 +0000
commit952a397b0e006444e770e51d32cce93186959bdb (patch)
treee7a8cf6fc9883bdb071ac35b59743af532aff2e2 /lib/std
parent4ab2f947f9fcd2c6a4181c509d7c1ab27c6e4d58 (diff)
parent331f6a07a98206c3b5c096e73860ef1b7a3dfe85 (diff)
downloadzig-952a397b0e006444e770e51d32cce93186959bdb.tar.gz
zig-952a397b0e006444e770e51d32cce93186959bdb.zip
Merge pull request #5978 from ziglang/stage2-dwarf-incr
self-hosted: line number debug information
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/build.zig50
-rw-r--r--lib/std/hash_map.zig18
-rw-r--r--lib/std/zig.zig16
-rw-r--r--lib/std/zig/ast.zig8
4 files changed, 66 insertions, 26 deletions
diff --git a/lib/std/build.zig b/lib/std/build.zig
index 103d3a3193..c85534ba7d 100644
--- a/lib/std/build.zig
+++ b/lib/std/build.zig
@@ -430,9 +430,9 @@ pub const Builder = struct {
const entry = self.user_input_options.getEntry(name) orelse return null;
entry.value.used = true;
switch (type_id) {
- TypeId.Bool => switch (entry.value.value) {
- UserValue.Flag => return true,
- UserValue.Scalar => |s| {
+ .Bool => switch (entry.value.value) {
+ .Flag => return true,
+ .Scalar => |s| {
if (mem.eql(u8, s, "true")) {
return true;
} else if (mem.eql(u8, s, "false")) {
@@ -443,21 +443,21 @@ pub const Builder = struct {
return null;
}
},
- UserValue.List => {
+ .List => {
warn("Expected -D{} to be a boolean, but received a list.\n", .{name});
self.markInvalidUserInput();
return null;
},
},
- TypeId.Int => panic("TODO integer options to build script", .{}),
- TypeId.Float => panic("TODO float options to build script", .{}),
- TypeId.Enum => switch (entry.value.value) {
- UserValue.Flag => {
+ .Int => panic("TODO integer options to build script", .{}),
+ .Float => panic("TODO float options to build script", .{}),
+ .Enum => switch (entry.value.value) {
+ .Flag => {
warn("Expected -D{} to be a string, but received a boolean.\n", .{name});
self.markInvalidUserInput();
return null;
},
- UserValue.Scalar => |s| {
+ .Scalar => |s| {
if (std.meta.stringToEnum(T, s)) |enum_lit| {
return enum_lit;
} else {
@@ -466,33 +466,35 @@ pub const Builder = struct {
return null;
}
},
- UserValue.List => {
+ .List => {
warn("Expected -D{} to be a string, but received a list.\n", .{name});
self.markInvalidUserInput();
return null;
},
},
- TypeId.String => switch (entry.value.value) {
- UserValue.Flag => {
+ .String => switch (entry.value.value) {
+ .Flag => {
warn("Expected -D{} to be a string, but received a boolean.\n", .{name});
self.markInvalidUserInput();
return null;
},
- UserValue.List => {
+ .List => {
warn("Expected -D{} to be a string, but received a list.\n", .{name});
self.markInvalidUserInput();
return null;
},
- UserValue.Scalar => |s| return s,
+ .Scalar => |s| return s,
},
- TypeId.List => switch (entry.value.value) {
- UserValue.Flag => {
+ .List => switch (entry.value.value) {
+ .Flag => {
warn("Expected -D{} to be a list, but received a boolean.\n", .{name});
self.markInvalidUserInput();
return null;
},
- UserValue.Scalar => |s| return &[_][]const u8{s},
- UserValue.List => |lst| return lst.span(),
+ .Scalar => |s| {
+ return self.allocator.dupe([]const u8, &[_][]const u8{s}) catch unreachable;
+ },
+ .List => |lst| return lst.span(),
},
}
}
@@ -1706,9 +1708,19 @@ pub const LibExeObjStep = struct {
pub fn addBuildOption(self: *LibExeObjStep, comptime T: type, name: []const u8, value: T) void {
const out = self.build_options_contents.outStream();
+ if (T == []const []const u8) {
+ out.print("pub const {}: []const []const u8 = &[_][]const u8{{\n", .{name}) catch unreachable;
+ for (value) |slice| {
+ out.writeAll(" ") catch unreachable;
+ std.zig.renderStringLiteral(slice, out) catch unreachable;
+ out.writeAll(",\n") catch unreachable;
+ }
+ out.writeAll("};\n") catch unreachable;
+ return;
+ }
switch (@typeInfo(T)) {
.Enum => |enum_info| {
- out.print("const {} = enum {{\n", .{@typeName(T)}) catch unreachable;
+ out.print("pub const {} = enum {{\n", .{@typeName(T)}) catch unreachable;
inline for (enum_info.fields) |field| {
out.print(" {},\n", .{field.name}) catch unreachable;
}
diff --git a/lib/std/hash_map.zig b/lib/std/hash_map.zig
index c81def3a00..d52a337422 100644
--- a/lib/std/hash_map.zig
+++ b/lib/std/hash_map.zig
@@ -196,6 +196,10 @@ pub fn HashMap(
return self.unmanaged.getEntry(key);
}
+ pub fn getIndex(self: Self, key: K) ?usize {
+ return self.unmanaged.getIndex(key);
+ }
+
pub fn get(self: Self, key: K) ?V {
return self.unmanaged.get(key);
}
@@ -479,17 +483,21 @@ pub fn HashMapUnmanaged(
}
pub fn getEntry(self: Self, key: K) ?*Entry {
+ const index = self.getIndex(key) orelse return null;
+ return &self.entries.items[index];
+ }
+
+ pub fn getIndex(self: Self, key: K) ?usize {
const header = self.index_header orelse {
// Linear scan.
const h = if (store_hash) hash(key) else {};
- for (self.entries.items) |*item| {
+ for (self.entries.items) |*item, i| {
if (item.hash == h and eql(key, item.key)) {
- return item;
+ return i;
}
}
return null;
};
-
switch (header.capacityIndexType()) {
.u8 => return self.getInternal(key, header, u8),
.u16 => return self.getInternal(key, header, u16),
@@ -711,7 +719,7 @@ pub fn HashMapUnmanaged(
unreachable;
}
- fn getInternal(self: Self, key: K, header: *IndexHeader, comptime I: type) ?*Entry {
+ fn getInternal(self: Self, key: K, header: *IndexHeader, comptime I: type) ?usize {
const indexes = header.indexes(I);
const h = hash(key);
const start_index = header.constrainIndex(h);
@@ -725,7 +733,7 @@ pub fn HashMapUnmanaged(
const entry = &self.entries.items[index.entry_index];
const hash_match = if (store_hash) h == entry.hash else true;
if (hash_match and eql(key, entry.key))
- return entry;
+ return index.entry_index;
}
return null;
}
diff --git a/lib/std/zig.zig b/lib/std/zig.zig
index 841827cc19..b070fbdcd5 100644
--- a/lib/std/zig.zig
+++ b/lib/std/zig.zig
@@ -43,6 +43,22 @@ pub fn findLineColumn(source: []const u8, byte_offset: usize) struct { line: usi
return .{ .line = line, .column = column };
}
+pub fn lineDelta(source: []const u8, start: usize, end: usize) isize {
+ var line: isize = 0;
+ if (end >= start) {
+ for (source[start..end]) |byte| switch (byte) {
+ '\n' => line += 1,
+ else => continue,
+ };
+ } else {
+ for (source[end..start]) |byte| switch (byte) {
+ '\n' => line -= 1,
+ else => continue,
+ };
+ }
+ return line;
+}
+
/// Returns the standard file system basename of a binary generated by the Zig compiler.
pub fn binNameAlloc(
allocator: *std.mem.Allocator,
diff --git a/lib/std/zig/ast.zig b/lib/std/zig/ast.zig
index 02d1fd6ba2..6b2b8b4cf2 100644
--- a/lib/std/zig/ast.zig
+++ b/lib/std/zig/ast.zig
@@ -1299,6 +1299,10 @@ pub const Node = struct {
});
}
+ pub fn body(self: *const FnProto) ?*Node {
+ return self.getTrailer("body_node");
+ }
+
pub fn getTrailer(self: *const FnProto, comptime name: []const u8) ?TrailerFlags.Field(name) {
const trailers_start = @alignCast(
@alignOf(ParamDecl),
@@ -1381,7 +1385,7 @@ pub const Node = struct {
.Invalid => {},
}
- if (self.getTrailer("body_node")) |body_node| {
+ if (self.body()) |body_node| {
if (i < 1) return body_node;
i -= 1;
}
@@ -1397,7 +1401,7 @@ pub const Node = struct {
}
pub fn lastToken(self: *const FnProto) TokenIndex {
- if (self.getTrailer("body_node")) |body_node| return body_node.lastToken();
+ if (self.body()) |body_node| return body_node.lastToken();
switch (self.return_type) {
.Explicit, .InferErrorSet => |node| return node.lastToken(),
.Invalid => |tok| return tok,