Skip to content

Commit

Permalink
Tree: add char kind to char and string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexu committed Feb 8, 2025
1 parent 50650a9 commit c1ac1a3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
23 changes: 20 additions & 3 deletions src/aro/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8878,7 +8878,7 @@ fn makePredefinedIdentifier(p: *Parser, strings_top: usize) !Result {
const slice = p.strings.items[strings_top..];
const val = try Value.intern(p.comp, .{ .bytes = slice });

const str_lit = try p.addNode(.{ .string_literal_expr = .{ .qt = array_qt, .literal_tok = p.tok_i } });
const str_lit = try p.addNode(.{ .string_literal_expr = .{ .qt = array_qt, .literal_tok = p.tok_i, .kind = .ascii } });
if (!p.in_macro) try p.tree.value_map.put(p.gpa, str_lit, val);

return .{ .qt = array_qt, .node = try p.addNode(.{
Expand Down Expand Up @@ -9013,6 +9013,13 @@ fn stringLiteral(p: *Parser) Error!Result {
.node = try p.addNode(.{ .string_literal_expr = .{
.literal_tok = string_start,
.qt = array_qt,
.kind = switch (string_kind) {
.char, .unterminated => .ascii,
.wide => .wide,
.utf_8 => .utf8,
.utf_16 => .utf16,
.utf_32 => .utf32,
},
} }),
};
try res.putValue(p);
Expand All @@ -9031,7 +9038,7 @@ fn charLiteral(p: *Parser) Error!?Result {
return .{
.qt = .int,
.val = .zero,
.node = try p.addNode(.{ .char_literal = .{ .qt = .int, .literal_tok = p.tok_i } }),
.node = try p.addNode(.{ .char_literal = .{ .qt = .int, .literal_tok = p.tok_i, .kind = .ascii } }),
};
};
if (char_kind == .utf_8) try p.err(.u8_char_lit);
Expand Down Expand Up @@ -9130,7 +9137,17 @@ fn charLiteral(p: *Parser) Error!?Result {
const res = Result{
.qt = if (p.in_macro) macro_qt else char_literal_qt,
.val = value,
.node = try p.addNode(.{ .char_literal = .{ .qt = char_literal_qt, .literal_tok = p.tok_i } }),
.node = try p.addNode(.{ .char_literal = .{
.qt = char_literal_qt,
.literal_tok = p.tok_i,
.kind = switch (char_kind) {
.char, .unterminated => .ascii,
.wide => .wide,
.utf_8 => .utf8,
.utf_16 => .utf16,
.utf_32 => .utf32,
},
} }),
};
if (!p.in_macro) try p.tree.value_map.put(p.gpa, res.node, res.val);
return res;
Expand Down
20 changes: 18 additions & 2 deletions src/aro/Tree.zig
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ pub const Node = union(enum) {
/// integer literal, always unsigned
int_literal: Literal,
/// Same as int_literal, but originates from a char literal
char_literal: Literal,
char_literal: CharLiteral,
/// a floating point literal
float_literal: Literal,
string_literal_expr: Literal,
string_literal_expr: CharLiteral,
/// wraps a float or double literal
imaginary_literal: Unary,
/// A compound literal (type){ init }
Expand Down Expand Up @@ -605,6 +605,18 @@ pub const Node = union(enum) {
qt: QualType,
};

pub const CharLiteral = struct {
literal_tok: TokenIndex,
qt: QualType,
kind: enum {
ascii,
wide,
utf8,
utf16,
utf32,
},
};

pub const CompoundLiteral = struct {
l_paren_tok: TokenIndex,
qt: QualType,
Expand Down Expand Up @@ -1447,6 +1459,7 @@ pub const Node = union(enum) {
.char_literal = .{
.literal_tok = node_tok,
.qt = @bitCast(node_data[0]),
.kind = @enumFromInt(node_data[1]),
},
},
.float_literal => .{
Expand All @@ -1459,6 +1472,7 @@ pub const Node = union(enum) {
.string_literal_expr = .{
.literal_tok = node_tok,
.qt = @bitCast(node_data[0]),
.kind = @enumFromInt(node_data[1]),
},
},
.imaginary_literal => .{
Expand Down Expand Up @@ -2498,6 +2512,7 @@ pub fn setNode(tree: *Tree, node: Node, index: usize) !void {
.char_literal => |literal| {
repr.tag = .char_literal;
repr.data[0] = @bitCast(literal.qt);
repr.data[1] = @intFromEnum(literal.kind);
repr.tok = literal.literal_tok;
},
.float_literal => |literal| {
Expand All @@ -2508,6 +2523,7 @@ pub fn setNode(tree: *Tree, node: Node, index: usize) !void {
.string_literal_expr => |literal| {
repr.tag = .string_literal_expr;
repr.data[0] = @bitCast(literal.qt);
repr.data[1] = @intFromEnum(literal.kind);
repr.tok = literal.literal_tok;
},
.imaginary_literal => |un| {
Expand Down

0 comments on commit c1ac1a3

Please sign in to comment.