aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorriverbl <94326797+riverbl@users.noreply.github.com>2021-12-30 22:54:11 +0000
committerVeikka Tuominen <git@vexu.eu>2022-01-17 16:54:48 +0200
commitc71cf48cb5886cdac2172a602b6a6015f483ff93 (patch)
tree2cb4c2494f5b4d025c14b1cd6b8e84705cbddca5 /src
parent4addb26bba94e9e5abb919b39efcd5d1b99934af (diff)
downloadzig-c71cf48cb5886cdac2172a602b6a6015f483ff93.tar.gz
zig-c71cf48cb5886cdac2172a602b6a6015f483ff93.zip
stage2: do not interpret identifier containing underscores (eg: u3_2) as int primitive type
Diffstat (limited to 'src')
-rw-r--r--src/AstGen.zig27
1 files changed, 24 insertions, 3 deletions
diff --git a/src/AstGen.zig b/src/AstGen.zig
index 09d9b02a0e..32efc8305f 100644
--- a/src/AstGen.zig
+++ b/src/AstGen.zig
@@ -6065,6 +6065,27 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
}
}
+/// Parses the string `buf` as a base 10 integer of type `u16`.
+///
+/// Unlike std.fmt.parseInt, does not allow the '_' character in `buf`.
+fn parseBitCount(buf: []const u8) std.fmt.ParseIntError!u16 {
+ if (buf.len == 0) return error.InvalidCharacter;
+
+ var x: u16 = 0;
+
+ for (buf) |c| {
+ const digit = switch (c) {
+ '0'...'9' => c - '0',
+ else => return error.InvalidCharacter,
+ };
+
+ if (x != 0) x = try std.math.mul(u16, x, 10);
+ x = try std.math.add(u16, x, @as(u16, digit));
+ }
+
+ return x;
+}
+
fn identifier(
gz: *GenZir,
scope: *Scope,
@@ -6098,7 +6119,7 @@ fn identifier(
true => .signed,
false => .unsigned,
};
- const bit_count = std.fmt.parseInt(u16, ident_name_raw[1..], 10) catch |err| switch (err) {
+ const bit_count = parseBitCount(ident_name_raw[1..]) catch |err| switch (err) {
error.Overflow => return astgen.failNode(
ident,
"primitive integer type '{s}' exceeds maximum bit width of 65535",
@@ -8985,7 +9006,7 @@ const GenZir = struct {
parent: *Scope,
/// All `GenZir` scopes for the same ZIR share this.
astgen: *AstGen,
- /// Keeps track of the list of instructions in this scope. Possibly shared.
+ /// Keeps track of the list of instructions in this scope. Possibly shared.
/// Indexes to instructions in `astgen`.
instructions: *ArrayListUnmanaged(Zir.Inst.Index),
/// A sub-block may share its instructions ArrayList with containing GenZir,
@@ -10231,7 +10252,7 @@ pub fn isPrimitive(name: []const u8) bool {
if (name.len < 2) return false;
const first_c = name[0];
if (first_c != 'i' and first_c != 'u') return false;
- if (std.fmt.parseInt(u16, name[1..], 10)) |_| {
+ if (parseBitCount(name[1..])) |_| {
return true;
} else |err| switch (err) {
error.Overflow => return true,