diff --git a/build.zig b/build.zig index be46034..377d39b 100644 --- a/build.zig +++ b/build.zig @@ -47,6 +47,13 @@ pub fn build(b: *std.Build) void { oracle.has_side_effects = true; oracle_step.dependOn(&oracle.step); + const oracle_parse_step = b.step("oracle-parse", "Parse every dxc-valid corpus file with zdxc"); + const oracle_parse = b.addSystemCommand(&.{ "sh", "test/oracle/parse.sh" }); + oracle_parse.addArtifactArg(exe); + oracle_parse.setCwd(b.path(".")); + oracle_parse.has_side_effects = true; + oracle_parse_step.dependOn(&oracle_parse.step); + // Benchmarks always build optimized so numbers are comparable. const bench = b.addExecutable(.{ .name = "pp_bench", diff --git a/src/front/ast.zig b/src/front/ast.zig new file mode 100644 index 0000000..492acdd --- /dev/null +++ b/src/front/ast.zig @@ -0,0 +1,275 @@ +//! Syntax tree for HLSL 2021. Purely syntactic: names are not resolved and +//! types are recorded as written. Nodes live in the parser's arena. + +const std = @import("std"); +const source = @import("source.zig"); +pub const Loc = source.Loc; + +/// A possibly qualified name as written: `foo`, `N::foo`, `::foo`. +pub const Name = struct { + loc: Loc, + /// Components without separators; a leading empty component means + /// a global-scope `::` prefix. + parts: []const []const u8, + /// Template arguments per part (`S::value`); empty when no part has + /// any, otherwise the same length as `parts`. + args: []const []const TemplateArg = &.{}, + + pub fn last(n: Name) []const u8 { + return n.parts[n.parts.len - 1]; + } + + pub fn isSimple(n: Name, s: []const u8) bool { + return n.parts.len == 1 and std.mem.eql(u8, n.parts[0], s); + } +}; + +pub const TemplateArg = union(enum) { + type: *TypeRef, + expr: *Expr, +}; + +/// A type as written: `float4`, `Texture2D`, `vector`, +/// `N::S`. Resolution happens in Sema. +pub const TypeRef = struct { + loc: Loc, + name: Name, + args: []const TemplateArg = &.{}, + /// Array dimensions on type arguments: `StructuredBuffer`. + array_dims: []const ?*Expr = &.{}, + /// `[[...]]` attributes after the type (`__builtin_LinAlgMatrix [[...]]`). + attrs: []const Attribute = &.{}, + /// `unorm` / `snorm` inside resource element types. + norm: enum { none, unorm, snorm } = .none, +}; + +/// Storage classes, type qualifiers and parameter / interpolation +/// modifiers that may precede a declaration. +pub const Qualifiers = packed struct(u64) { + @"const": bool = false, + static: bool = false, + uniform: bool = false, + @"extern": bool = false, + @"volatile": bool = false, + precise: bool = false, + groupshared: bool = false, + shared: bool = false, + in: bool = false, + out: bool = false, + row_major: bool = false, + column_major: bool = false, + nointerpolation: bool = false, + linear: bool = false, + centroid: bool = false, + sample: bool = false, + noperspective: bool = false, + globallycoherent: bool = false, + reordercoherent: bool = false, + @"inline": bool = false, + @"export": bool = false, + // Geometry shader input primitive types. + point: bool = false, + line: bool = false, + triangle: bool = false, + lineadj: bool = false, + triangleadj: bool = false, + // Mesh shader output / payload modifiers. + vertices: bool = false, + indices: bool = false, + primitives: bool = false, + payload: bool = false, + _: u34 = 0, +}; + +/// `[name(args)]` or `[[ns::name(args)]]`. +pub const Attribute = struct { + loc: Loc, + /// e.g. `numthreads`, `vk::binding`. + name: []const u8, + args: []const *Expr, + double_bracket: bool, +}; + +/// Text after `:` on a declarator. +pub const Semantic = struct { + loc: Loc, + /// `access` is a ray payload qualifier: `read(caller)`, `write(miss)`. + kind: enum { semantic, register, packoffset, access }, + /// Semantic name (`SV_Target0`), or the comma-separated arguments of + /// register(...) / packoffset(...) with internal tokens joined + /// (`t0`, `space1`, `c0.x`). + values: []const []const u8, +}; + +pub const UnaryOp = enum { plus, minus, not, bit_not, pre_inc, pre_dec, post_inc, post_dec }; + +pub const BinaryOp = enum { + mul, + div, + rem, + add, + sub, + shl, + shr, + lt, + gt, + le, + ge, + eq, + ne, + bit_and, + bit_xor, + bit_or, + log_and, + log_or, + comma, +}; + +pub const AssignOp = enum { assign, mul, div, rem, add, sub, shl, shr, bit_and, bit_xor, bit_or }; + +pub const Expr = struct { + loc: Loc, + kind: Kind, + + pub const Kind = union(enum) { + int_lit: []const u8, + float_lit: []const u8, + bool_lit: bool, + string_lit: []const u8, + name: Name, + this, + unary: struct { op: UnaryOp, operand: *Expr }, + binary: struct { op: BinaryOp, lhs: *Expr, rhs: *Expr }, + assign: struct { op: AssignOp, lhs: *Expr, rhs: *Expr }, + conditional: struct { cond: *Expr, then: *Expr, @"else": *Expr }, + /// Function or method call; `template_args` for `f(...)`. + call: struct { callee: *Expr, args: []const *Expr, template_args: []const TemplateArg = &.{} }, + /// Type used as a function: `float4(...)`, `vector(...)`. + construct: struct { type: *TypeRef, args: []const *Expr }, + cast: struct { type: *TypeRef, array_dims: []const ?*Expr = &.{}, operand: *Expr }, + /// `.name` (field, swizzle or method); `arrow` is accepted for + /// error reporting. + member: struct { base: *Expr, name: []const u8, name_loc: Loc }, + index: struct { base: *Expr, index: *Expr }, + init_list: []const *Expr, + sizeof_type: *TypeRef, + sizeof_expr: *Expr, + }; +}; + +pub const Stmt = struct { + loc: Loc, + attrs: []const Attribute = &.{}, + kind: Kind, + + pub const Kind = union(enum) { + compound: []const *Stmt, + /// Local declarations: variables, typedefs, structs. + decl: []const *Decl, + expr: *Expr, + empty, + /// `cond_decl` is set for `if (T x = e)`; `cond` then names `x`. + @"if": struct { cond_decl: ?*Decl = null, cond: *Expr, then: *Stmt, @"else": ?*Stmt }, + @"for": struct { init: ?*Stmt, cond: ?*Expr, inc: ?*Expr, body: *Stmt }, + @"while": struct { cond_decl: ?*Decl = null, cond: *Expr, body: *Stmt }, + do_while: struct { body: *Stmt, cond: *Expr }, + @"switch": struct { cond_decl: ?*Decl = null, cond: *Expr, body: *Stmt }, + case: struct { value: *Expr, stmt: *Stmt }, + default: *Stmt, + @"break", + @"continue", + @"return": ?*Expr, + discard, + }; +}; + +pub const VarDecl = struct { + attrs: []const Attribute, + /// Non-empty for variable templates. + template_params: []const TemplateParam = &.{}, + /// Qualified out-of-line definitions (`int S::x = 1;`) keep the full + /// spelled name, e.g. `S::x`. + quals: Qualifiers, + type: *TypeRef, + name: []const u8, + name_loc: Loc, + array_dims: []const ?*Expr, + semantics: []const Semantic, + bitfield: ?*Expr = null, + init: ?*Expr = null, +}; + +pub const TemplateParam = struct { + loc: Loc, + name: []const u8, + /// null for `typename T` / `class T`; the type for value parameters. + value_type: ?*TypeRef, + default: ?TemplateArg, +}; + +pub const FuncDecl = struct { + attrs: []const Attribute, + quals: Qualifiers, + template_params: []const TemplateParam, + ret: *TypeRef, + /// Function name; for operators `operator+`, `operator[]`, `operator()`. + name: Name, + params: []const VarDecl, + ret_semantics: []const Semantic, + /// Array dimensions after the parameter list (`T f()[N]`). + ret_dims: []const ?*Expr = &.{}, + /// Trailing `const` on methods. + is_const: bool, + body: ?*Stmt, +}; + +pub const StructDecl = struct { + attrs: []const Attribute, + keyword: enum { @"struct", class, interface }, + template_params: []const TemplateParam, + name: ?[]const u8, + /// Arguments of a partial / explicit specialization: `S`. + specialization_args: []const TemplateArg = &.{}, + bases: []const *TypeRef, + /// Null for a forward declaration. + members: ?[]const *Decl, +}; + +pub const CBufferDecl = struct { + tbuffer: bool, + name: []const u8, + semantics: []const Semantic, + members: []const *Decl, +}; + +pub const EnumDecl = struct { + name: ?[]const u8, + scoped: bool, + underlying: ?*TypeRef, + values: []const struct { loc: Loc, name: []const u8, value: ?*Expr }, +}; + +pub const Decl = struct { + loc: Loc, + kind: Kind, + + pub const Kind = union(enum) { + @"var": VarDecl, + func: FuncDecl, + @"struct": StructDecl, + cbuffer: CBufferDecl, + typedef: struct { name: []const u8, type: *TypeRef, array_dims: []const ?*Expr, template_params: []const TemplateParam = &.{} }, + @"enum": EnumDecl, + namespace: struct { name: []const u8, decls: []const *Decl }, + /// `using namespace N;` + using_namespace: Name, + /// `using N::name;` + using_decl: Name, + pragma: []const u8, + static_assert: struct { cond: *Expr, message: ?[]const u8 }, + }; +}; + +pub const TranslationUnit = struct { + decls: []const *Decl, +}; diff --git a/src/front/ast_dump.zig b/src/front/ast_dump.zig new file mode 100644 index 0000000..bf04d6c --- /dev/null +++ b/src/front/ast_dump.zig @@ -0,0 +1,414 @@ +//! Compact S-expression dump of the AST, for tests and debugging. + +const std = @import("std"); +const ast = @import("ast.zig"); +const W = std.Io.Writer; + +pub fn dumpUnit(w: *W, tu: ast.TranslationUnit) W.Error!void { + for (tu.decls) |d| { + try decl(w, d, 0); + try w.writeByte('\n'); + } +} + +fn indent(w: *W, n: usize) W.Error!void { + try w.splatByteAll(' ', n * 2); +} + +fn name(w: *W, n: ast.Name) W.Error!void { + for (n.parts, 0..) |part, i| { + if (i > 0) try w.writeAll("::"); + try w.writeAll(part); + if (n.args.len > 0 and n.args[i].len > 0) try templateArgs(w, n.args[i]); + } +} + +fn templateArgs(w: *W, args: []const ast.TemplateArg) W.Error!void { + try w.writeByte('<'); + for (args, 0..) |a, i| { + if (i > 0) try w.writeAll(", "); + switch (a) { + .type => |ty| try typeRef(w, ty), + .expr => |e| try expr(w, e), + } + } + try w.writeByte('>'); +} + +pub fn typeRef(w: *W, t: *const ast.TypeRef) W.Error!void { + if (t.norm != .none) try w.print("{s} ", .{@tagName(t.norm)}); + try name(w, t.name); + if (t.args.len > 0) try templateArgs(w, t.args); + try dims(w, t.array_dims); +} + +fn quals(w: *W, q: ast.Qualifiers) W.Error!void { + const info = @typeInfo(ast.Qualifiers).@"struct"; + inline for (info.field_names, info.field_types) |fname, ftype| { + if (ftype == bool and @field(q, fname)) try w.print("{s} ", .{fname}); + } +} + +fn attrs(w: *W, list: []const ast.Attribute) W.Error!void { + for (list) |a| { + try w.print("[{s}", .{a.name}); + if (a.args.len > 0) { + try w.writeByte('('); + for (a.args, 0..) |e, i| { + if (i > 0) try w.writeAll(", "); + try expr(w, e); + } + try w.writeByte(')'); + } + try w.writeAll("] "); + } +} + +fn semantics(w: *W, list: []const ast.Semantic) W.Error!void { + for (list) |s| { + switch (s.kind) { + .semantic => try w.print(" : {s}", .{s.values[0]}), + .register, .packoffset, .access => { + try w.print(" : {s}(", .{@tagName(s.kind)}); + for (s.values, 0..) |v, i| { + if (i > 0) try w.writeAll(", "); + try w.writeAll(v); + } + try w.writeByte(')'); + }, + } + } +} + +fn dims(w: *W, list: []const ?*ast.Expr) W.Error!void { + for (list) |d| { + try w.writeByte('['); + if (d) |e| try expr(w, e); + try w.writeByte(']'); + } +} + +fn varDecl(w: *W, v: ast.VarDecl) W.Error!void { + try templateParams(w, v.template_params); + try attrs(w, v.attrs); + try quals(w, v.quals); + try typeRef(w, v.type); + try w.print(" {s}", .{v.name}); + try dims(w, v.array_dims); + if (v.bitfield) |b| { + try w.writeAll(" : "); + try expr(w, b); + } + try semantics(w, v.semantics); + if (v.init) |e| { + try w.writeAll(" = "); + try expr(w, e); + } +} + +fn templateParams(w: *W, params: []const ast.TemplateParam) W.Error!void { + if (params.len == 0) return; + try w.writeAll("template<"); + for (params, 0..) |tp, i| { + if (i > 0) try w.writeAll(", "); + if (tp.value_type) |vt| { + try typeRef(w, vt); + try w.print(" {s}", .{tp.name}); + } else try w.print("typename {s}", .{tp.name}); + } + try w.writeAll("> "); +} + +pub fn decl(w: *W, d: *const ast.Decl, depth: usize) W.Error!void { + try indent(w, depth); + switch (d.kind) { + .@"var" => |v| { + try w.writeAll("(var "); + try varDecl(w, v); + try w.writeByte(')'); + }, + .func => |f| { + try w.writeAll("(func "); + try templateParams(w, f.template_params); + try attrs(w, f.attrs); + try quals(w, f.quals); + try typeRef(w, f.ret); + try w.writeByte(' '); + try name(w, f.name); + try w.writeByte('('); + for (f.params, 0..) |prm, i| { + if (i > 0) try w.writeAll(", "); + try varDecl(w, prm); + } + try w.writeByte(')'); + try dims(w, f.ret_dims); + if (f.is_const) try w.writeAll(" const"); + try semantics(w, f.ret_semantics); + if (f.body) |b| { + try w.writeByte('\n'); + try stmt(w, b, depth + 1); + } + try w.writeByte(')'); + }, + .@"struct" => |s| { + try w.print("({s} ", .{@tagName(s.keyword)}); + try templateParams(w, s.template_params); + try attrs(w, s.attrs); + try w.writeAll(s.name orelse ""); + if (s.specialization_args.len > 0) try templateArgs(w, s.specialization_args); + for (s.bases) |b| { + try w.writeAll(" : "); + try typeRef(w, b); + } + if (s.members) |members| { + for (members) |m| { + try w.writeByte('\n'); + try decl(w, m, depth + 1); + } + } else try w.writeAll(" "); + try w.writeByte(')'); + }, + .cbuffer => |c| { + try w.print("({s} {s}", .{ if (c.tbuffer) "tbuffer" else "cbuffer", c.name }); + try semantics(w, c.semantics); + for (c.members) |m| { + try w.writeByte('\n'); + try decl(w, m, depth + 1); + } + try w.writeByte(')'); + }, + .typedef => |t| { + try w.writeAll("(typedef "); + try templateParams(w, t.template_params); + try typeRef(w, t.type); + try w.print(" {s}", .{t.name}); + try dims(w, t.array_dims); + try w.writeByte(')'); + }, + .@"enum" => |e| { + try w.print("(enum{s} {s}", .{ if (e.scoped) " class" else "", e.name orelse "" }); + for (e.values) |v| { + try w.print(" {s}", .{v.name}); + if (v.value) |x| { + try w.writeByte('='); + try expr(w, x); + } + } + try w.writeByte(')'); + }, + .namespace => |n| { + try w.print("(namespace {s}", .{n.name}); + for (n.decls) |m| { + try w.writeByte('\n'); + try decl(w, m, depth + 1); + } + try w.writeByte(')'); + }, + .using_namespace => |n| { + try w.writeAll("(using namespace "); + try name(w, n); + try w.writeByte(')'); + }, + .using_decl => |n| { + try w.writeAll("(using "); + try name(w, n); + try w.writeByte(')'); + }, + .pragma => |p| try w.print("(pragma {s})", .{p}), + .static_assert => |s| { + try w.writeAll("(static_assert "); + try expr(w, s.cond); + try w.writeByte(')'); + }, + } +} + +pub fn stmt(w: *W, s: *const ast.Stmt, depth: usize) W.Error!void { + try indent(w, depth); + try attrs(w, s.attrs); + switch (s.kind) { + .compound => |list| { + try w.writeAll("{"); + for (list) |c| { + try w.writeByte('\n'); + try stmt(w, c, depth + 1); + } + try w.writeAll("}"); + }, + .decl => |list| { + for (list, 0..) |d, i| { + if (i > 0) try w.writeByte('\n'); + try decl(w, d, if (i == 0) 0 else depth); + } + }, + .expr => |e| try expr(w, e), + .empty => try w.writeAll("(empty)"), + .@"if" => |x| { + try w.writeAll("(if "); + if (x.cond_decl) |d| { + try decl(w, d, 0); + try w.writeByte(' '); + } + try expr(w, x.cond); + try w.writeByte('\n'); + try stmt(w, x.then, depth + 1); + if (x.@"else") |e| { + try w.writeByte('\n'); + try stmt(w, e, depth + 1); + } + try w.writeByte(')'); + }, + .@"for" => |x| { + try w.writeAll("(for "); + if (x.init) |i| try stmt(w, i, 0) else try w.writeAll("-"); + try w.writeAll("; "); + if (x.cond) |c| try expr(w, c) else try w.writeAll("-"); + try w.writeAll("; "); + if (x.inc) |c| try expr(w, c) else try w.writeAll("-"); + try w.writeByte('\n'); + try stmt(w, x.body, depth + 1); + try w.writeByte(')'); + }, + .@"while" => |x| { + try w.writeAll("(while "); + try expr(w, x.cond); + try w.writeByte('\n'); + try stmt(w, x.body, depth + 1); + try w.writeByte(')'); + }, + .do_while => |x| { + try w.writeAll("(do\n"); + try stmt(w, x.body, depth + 1); + try w.writeAll(" while "); + try expr(w, x.cond); + try w.writeByte(')'); + }, + .@"switch" => |x| { + try w.writeAll("(switch "); + try expr(w, x.cond); + try w.writeByte('\n'); + try stmt(w, x.body, depth + 1); + try w.writeByte(')'); + }, + .case => |x| { + try w.writeAll("(case "); + try expr(w, x.value); + try w.writeByte('\n'); + try stmt(w, x.stmt, depth + 1); + try w.writeByte(')'); + }, + .default => |x| { + try w.writeAll("(default\n"); + try stmt(w, x, depth + 1); + try w.writeByte(')'); + }, + .@"break" => try w.writeAll("(break)"), + .@"continue" => try w.writeAll("(continue)"), + .discard => try w.writeAll("(discard)"), + .@"return" => |v| { + try w.writeAll("(return"); + if (v) |e| { + try w.writeByte(' '); + try expr(w, e); + } + try w.writeByte(')'); + }, + } +} + +pub fn expr(w: *W, e: *const ast.Expr) W.Error!void { + switch (e.kind) { + .int_lit, .float_lit, .string_lit => |t| try w.writeAll(t), + .bool_lit => |b| try w.writeAll(if (b) "true" else "false"), + .name => |n| try name(w, n), + .this => try w.writeAll("this"), + .unary => |u| { + try w.print("({s} ", .{@tagName(u.op)}); + try expr(w, u.operand); + try w.writeByte(')'); + }, + .binary => |b| { + try w.print("({s} ", .{@tagName(b.op)}); + try expr(w, b.lhs); + try w.writeByte(' '); + try expr(w, b.rhs); + try w.writeByte(')'); + }, + .assign => |a| { + try w.print("({s}= ", .{if (a.op == .assign) "" else @tagName(a.op)}); + try expr(w, a.lhs); + try w.writeByte(' '); + try expr(w, a.rhs); + try w.writeByte(')'); + }, + .conditional => |c| { + try w.writeAll("(? "); + try expr(w, c.cond); + try w.writeByte(' '); + try expr(w, c.then); + try w.writeByte(' '); + try expr(w, c.@"else"); + try w.writeByte(')'); + }, + .call => |c| { + try w.writeAll("(call "); + try expr(w, c.callee); + if (c.template_args.len > 0) { + const tmp: ast.TypeRef = .{ .loc = e.loc, .name = .{ .loc = e.loc, .parts = &.{""} }, .args = c.template_args }; + try typeRef(w, &tmp); + } + for (c.args) |a| { + try w.writeByte(' '); + try expr(w, a); + } + try w.writeByte(')'); + }, + .construct => |c| { + try w.writeAll("(construct "); + try typeRef(w, c.type); + for (c.args) |a| { + try w.writeByte(' '); + try expr(w, a); + } + try w.writeByte(')'); + }, + .cast => |c| { + try w.writeAll("(cast "); + try typeRef(w, c.type); + try dims(w, c.array_dims); + try w.writeByte(' '); + try expr(w, c.operand); + try w.writeByte(')'); + }, + .member => |m| { + try w.writeAll("(. "); + try expr(w, m.base); + try w.print(" {s})", .{m.name}); + }, + .index => |x| { + try w.writeAll("([] "); + try expr(w, x.base); + try w.writeByte(' '); + try expr(w, x.index); + try w.writeByte(')'); + }, + .init_list => |list| { + try w.writeByte('{'); + for (list, 0..) |a, i| { + if (i > 0) try w.writeAll(", "); + try expr(w, a); + } + try w.writeByte('}'); + }, + .sizeof_type => |t| { + try w.writeAll("(sizeof "); + try typeRef(w, t); + try w.writeByte(')'); + }, + .sizeof_expr => |x| { + try w.writeAll("(sizeof "); + try expr(w, x); + try w.writeByte(')'); + }, + } +} diff --git a/src/front/parser.zig b/src/front/parser.zig new file mode 100644 index 0000000..125307d --- /dev/null +++ b/src/front/parser.zig @@ -0,0 +1,1883 @@ +//! Recursive-descent parser for HLSL 2021, producing `ast` nodes. +//! +//! Like C/C++, HLSL needs to know which identifiers name types to tell +//! declarations from expressions (`T x;` vs `f(x);`, `(T)x` vs `(a)+b`) and +//! template argument lists from comparisons (`f(x)` vs `a < b`). The +//! parser keeps a scoped table of user type and template names for this; +//! builtin types are recognized by name. + +const std = @import("std"); +const Allocator = std.mem.Allocator; +const ast = @import("ast.zig"); +const lexer = @import("lexer.zig"); +const source = @import("source.zig"); +const Diagnostics = @import("../diag.zig").Diagnostics; +const Preprocessor = @import("preproc.zig").Preprocessor; +const Token = lexer.Token; +const Kind = lexer.Kind; +const Loc = source.Loc; + +pub const Error = Allocator.Error || error{Syntax}; + +const NameKind = enum { type, template_type, template_func, value }; + +pub const Parser = struct { + arena: Allocator, + diags: *Diagnostics, + toks: []Token, + pos: usize = 0, + /// name -> kind, innermost declaration wins. + names: std.StringHashMapUnmanaged(std.ArrayList(NameKind)) = .empty, + /// Names declared in each open scope, to undo on exit. + scopes: std.ArrayList(std.ArrayList([]const u8)) = .empty, + anon_count: u32 = 0, + /// Name of the struct most recently parsed by parseStructDeclInto + /// (synthesized for anonymous structs), for `typedef struct ...`. + last_struct_name: []const u8 = "", + /// Set while parsing template arguments: a top-level `>` ends the + /// argument instead of being a comparison. + no_greater: bool = false, + + /// Collect every token from `pp` and set up a parser over them. + pub fn fromPreprocessor(arena: Allocator, diags: *Diagnostics, pp: *Preprocessor) !Parser { + var toks: std.ArrayList(Token) = .empty; + while (true) { + const t = try pp.next(); + try toks.append(arena, t); + if (t.kind == .eof) break; + } + return .{ .arena = arena, .diags = diags, .toks = toks.items }; + } + + pub fn parseTranslationUnit(p: *Parser) Allocator.Error!ast.TranslationUnit { + try p.pushScope(); + var decls: std.ArrayList(*ast.Decl) = .empty; + while (p.tok().kind != .eof) { + const start = p.pos; + p.parseDeclInto(&decls, .global) catch |e| switch (e) { + error.OutOfMemory => return error.OutOfMemory, + error.Syntax => p.recover(start), + }; + } + return .{ .decls = decls.items }; + } + + // ------------------------------------------------------------------ + // Token helpers + + fn tok(p: *const Parser) Token { + return p.toks[p.pos]; + } + + fn peek(p: *const Parser, n: usize) Token { + const i = @min(p.pos + n, p.toks.len - 1); + return p.toks[i]; + } + + fn advance(p: *Parser) Token { + const t = p.toks[p.pos]; + if (t.kind != .eof) p.pos += 1; + return t; + } + + fn at(p: *const Parser, kind: Kind) bool { + return p.tok().kind == kind; + } + + fn atIdent(p: *const Parser, text: []const u8) bool { + return p.tok().isIdent(text); + } + + fn eat(p: *Parser, kind: Kind) bool { + if (!p.at(kind)) return false; + _ = p.advance(); + return true; + } + + fn eatIdent(p: *Parser, text: []const u8) bool { + if (!p.atIdent(text)) return false; + _ = p.advance(); + return true; + } + + fn fail(p: *Parser, comptime fmt: []const u8, args: anytype) error{Syntax} { + p.diags.err(p.tok().loc, fmt, args); + return error.Syntax; + } + + fn expect(p: *Parser, kind: Kind, what: []const u8) Error!Token { + if (p.at(kind)) return p.advance(); + return p.fail("expected {s}, found '{s}'", .{ what, describe(p.tok()) }); + } + + fn expectIdent(p: *Parser, what: []const u8) Error!Token { + const t = p.tok(); + if (t.kind == .identifier and !isReserved(t.text)) return p.advance(); + return p.fail("expected {s}, found '{s}'", .{ what, describe(t) }); + } + + /// Consume a `>` closing a template argument list, splitting `>>`, + /// `>=` and `>>=` as needed. + fn expectCloseAngle(p: *Parser) Error!void { + const t = p.tok(); + const rest: ?Kind = switch (t.kind) { + .greater => null, + .greater_greater => .greater, + .greater_equal => .equal, + .greater_greater_equal => .greater_equal, + else => return p.fail("expected '>', found '{s}'", .{describe(t)}), + }; + if (rest) |k| { + p.toks[p.pos] = .{ .kind = k, .text = t.text[1..], .loc = .{ .file = t.loc.file, .offset = t.loc.offset + 1 } }; + } else _ = p.advance(); + } + + fn mk(p: *Parser, comptime T: type, value: T) Allocator.Error!*T { + const ptr = try p.arena.create(T); + ptr.* = value; + return ptr; + } + + fn mkExpr(p: *Parser, loc: Loc, kind: ast.Expr.Kind) Allocator.Error!*ast.Expr { + return p.mk(ast.Expr, .{ .loc = loc, .kind = kind }); + } + + /// Error recovery: skip to the end of the current declaration or + /// statement, keeping braces balanced. Always makes progress. + fn recover(p: *Parser, start: usize) void { + if (p.pos == start) _ = p.advance(); + var depth: u32 = 0; + while (true) { + const t = p.tok(); + switch (t.kind) { + .eof => return, + .l_brace => depth += 1, + .r_brace => { + if (depth == 0) return; + depth -= 1; + if (depth == 0) { + _ = p.advance(); + _ = p.eat(.semi); + return; + } + }, + .semi => if (depth == 0) { + _ = p.advance(); + return; + }, + else => {}, + } + _ = p.advance(); + } + } + + // ------------------------------------------------------------------ + // Name table + + fn pushScope(p: *Parser) Allocator.Error!void { + try p.scopes.append(p.arena, .empty); + } + + fn popScope(p: *Parser) void { + var scope = p.scopes.pop().?; + for (scope.items) |name| { + if (p.names.getPtr(name)) |list| _ = list.pop(); + } + scope.deinit(p.arena); + } + + fn declareName(p: *Parser, name: []const u8, kind: NameKind) Allocator.Error!void { + const gop = try p.names.getOrPut(p.arena, name); + if (!gop.found_existing) gop.value_ptr.* = .empty; + try gop.value_ptr.append(p.arena, kind); + try p.scopes.items[p.scopes.items.len - 1].append(p.arena, name); + } + + fn lookupName(p: *const Parser, name: []const u8) ?NameKind { + const list = p.names.get(name) orelse return null; + if (list.items.len == 0) return null; + return list.items[list.items.len - 1]; + } + + fn isTypeName(p: *const Parser, name: []const u8) bool { + if (p.lookupName(name)) |k| return k == .type or k == .template_type; + return isBuiltinType(name); + } + + fn isTemplateName(p: *const Parser, name: []const u8) bool { + if (p.lookupName(name)) |k| { + if (k == .template_type or k == .template_func) return true; + if (k == .value) return false; + } + // Builtin templates keep working when a same-named user type exists + // in some namespace (the name table is not namespace-aware). + return isBuiltinTemplate(name); + } + + /// End index of a type starting at `i` (syntactic scan, no + /// allocation), or null if no type starts there. + fn scanType(p: *const Parser, start: usize) ?usize { + var i = start; + if (p.toks[i].isIdent("unorm") or p.toks[i].isIdent("snorm")) i += 1; + if (p.toks[i].isIdent("typename")) return p.scanDependentType(i + 1); + if (p.toks[i].isIdent("unsigned") or p.toks[i].isIdent("signed")) { + i += 1; + if (p.toks[i].kind == .identifier and isBuiltinType(p.toks[i].text)) i += 1; + return i; + } + if (p.toks[i].kind == .colon_colon) i += 1; + while (true) { + const t = p.toks[i]; + if (t.kind != .identifier or isReserved(t.text)) return null; + i += 1; + var after = i; + if (p.toks[i].kind == .less and p.isTemplateName(t.text)) { + after = p.skipAngles(i) orelse return null; + } + if (p.toks[after].kind == .colon_colon and p.toks[after + 1].kind == .identifier) { + i = after + 1; + continue; + } + if (!p.isTypeName(t.text)) return null; + return p.skipTypeAttrs(after); + } + } + + /// After `typename`: any qualified name is a type. + fn scanDependentType(p: *const Parser, start: usize) ?usize { + var i = start; + if (p.toks[i].kind == .colon_colon) i += 1; + while (true) { + const t = p.toks[i]; + if (t.kind != .identifier or isReserved(t.text)) return null; + i += 1; + if (p.toks[i].kind == .less and p.isTemplateName(t.text)) i = p.skipAngles(i) orelse return null; + if (p.toks[i].kind == .colon_colon and p.toks[i + 1].kind == .identifier) { + i += 1; + continue; + } + return p.skipTypeAttrs(i); + } + } + + fn skipTypeAttrs(p: *const Parser, start: usize) usize { + var i = start; + if (!(p.toks[i].kind == .l_square and p.toks[i + 1].kind == .l_square)) return i; + var depth: u32 = 0; + while (true) : (i += 1) { + switch (p.toks[i].kind) { + .l_square => depth += 1, + .r_square => { + depth -= 1; + if (depth == 0) return i + 1; + }, + .eof => return start, + else => {}, + } + } + } + + /// Index just past the `>` matching the `<` at `i`, or null. + fn skipAngles(p: *const Parser, start: usize) ?usize { + var i = start; + var depth: u32 = 0; + var parens: u32 = 0; + while (true) : (i += 1) { + switch (p.toks[i].kind) { + .less => if (parens == 0) { + depth += 1; + }, + .greater => if (parens == 0) { + depth -= 1; + if (depth == 0) return i + 1; + }, + .greater_greater => if (parens == 0) { + if (depth <= 2) return i + 1; + depth -= 2; + }, + .l_paren => parens += 1, + .r_paren => { + if (parens == 0) return null; + parens -= 1; + }, + .semi, .l_brace, .r_brace, .eof => return null, + else => {}, + } + } + } + + fn isTypeStartAt(p: *const Parser, i: usize) bool { + return p.scanType(i) != null; + } + + // ------------------------------------------------------------------ + // Attributes, qualifiers, types + + fn parseAttributes(p: *Parser) Error![]const ast.Attribute { + var attrs: std.ArrayList(ast.Attribute) = .empty; + while (p.at(.l_square)) { + const double = p.peek(1).kind == .l_square; + _ = p.advance(); + if (double) _ = p.advance(); + while (true) { + const start = p.tok(); + var name: std.ArrayList(u8) = .empty; + try name.appendSlice(p.arena, (try p.expectAttrName()).text); + while (p.eat(.colon_colon)) { + try name.appendSlice(p.arena, "::"); + try name.appendSlice(p.arena, (try p.expectAttrName()).text); + } + var args: std.ArrayList(*ast.Expr) = .empty; + if (p.eat(.l_paren)) { + if (!p.at(.r_paren)) { + while (true) { + try args.append(p.arena, try p.parseAssign()); + if (!p.eat(.comma)) break; + } + } + _ = try p.expect(.r_paren, "')'"); + } + try attrs.append(p.arena, .{ .loc = start.loc, .name = name.items, .args = args.items, .double_bracket = double }); + if (!p.eat(.comma)) break; + } + _ = try p.expect(.r_square, "']'"); + if (double) _ = try p.expect(.r_square, "']'"); + } + return attrs.items; + } + + fn expectAttrName(p: *Parser) Error!Token { + if (p.at(.identifier)) return p.advance(); + return p.fail("expected attribute name, found '{s}'", .{describe(p.tok())}); + } + + /// Parse leading qualifiers. Contextual keywords (`point`, `sample`, + /// `line`, ...) count only when a type or another qualifier follows. + fn parseQualifiers(p: *Parser) ast.Qualifiers { + var q: ast.Qualifiers = .{}; + while (p.at(.identifier)) { + const word = p.tok().text; + const field = qualifierField(word) orelse break; + if (isContextualQualifier(word)) { + const n = p.peek(1); + const next_is_qual = n.kind == .identifier and qualifierField(n.text) != null; + if (!next_is_qual and !p.isTypeStartAt(p.pos + 1)) break; + } + _ = p.advance(); + if (std.mem.eql(u8, word, "inout")) { + q.in = true; + q.out = true; + continue; + } + const info = @typeInfo(ast.Qualifiers).@"struct"; + inline for (info.field_names, info.field_types) |fname, ftype| { + if (ftype == bool and std.mem.eql(u8, fname, field)) @field(q, fname) = true; + } + } + return q; + } + + fn parseType(p: *Parser) Error!*ast.TypeRef { + var norm: @FieldType(ast.TypeRef, "norm") = .none; + if (p.eatIdent("unorm")) { + norm = .unorm; + } else if (p.eatIdent("snorm")) norm = .snorm; + const t = try p.parseTypeInner(); + if (norm != .none) t.norm = norm; + if (p.at(.l_square) and p.peek(1).kind == .l_square) t.attrs = try p.parseAttributes(); + return t; + } + + fn parseTypeInner(p: *Parser) Error!*ast.TypeRef { + const start = p.tok(); + if (p.eatIdent("typename")) { + // Dependent name: `typename X::type`. + const n = try p.parseNameWithFinalArgs(); + return p.mk(ast.TypeRef, .{ .loc = start.loc, .name = n.name, .args = n.args }); + } + if (p.atIdent("unsigned") or p.atIdent("signed")) { + const signed = p.advance().isIdent("signed"); + var base: []const u8 = "int"; + if (p.at(.identifier) and isBuiltinType(p.tok().text)) base = p.advance().text; + const name = if (signed) base else try unsignedName(p.arena, base); + return p.mk(ast.TypeRef, .{ .loc = start.loc, .name = try p.simpleName(start.loc, name) }); + } + const name = try p.parseName(); + if (!p.isTypeName(name.last())) { + p.diags.err(start.loc, "unknown type name '{s}'", .{name.last()}); + return error.Syntax; + } + var args: []const ast.TemplateArg = &.{}; + if (p.at(.less) and p.isTemplateName(name.last())) args = try p.parseTemplateArgs(); + return p.mk(ast.TypeRef, .{ .loc = start.loc, .name = name, .args = args }); + } + + /// A qualified name whose final part may carry template arguments. + fn parseNameWithFinalArgs(p: *Parser) Error!struct { name: ast.Name, args: []const ast.TemplateArg } { + const name = try p.parseName(); + var args: []const ast.TemplateArg = &.{}; + if (p.at(.less) and p.isTemplateName(name.last())) args = try p.parseTemplateArgs(); + return .{ .name = name, .args = args }; + } + + fn simpleName(p: *Parser, loc: Loc, s: []const u8) Allocator.Error!ast.Name { + const parts = try p.arena.alloc([]const u8, 1); + parts[0] = s; + return .{ .loc = loc, .parts = parts }; + } + + fn parseName(p: *Parser) Error!ast.Name { + const loc = p.tok().loc; + var parts: std.ArrayList([]const u8) = .empty; + var args: std.ArrayList([]const ast.TemplateArg) = .empty; + var any_args = false; + if (p.eat(.colon_colon)) { + try parts.append(p.arena, ""); + try args.append(p.arena, &.{}); + } + while (true) { + const id = try p.expectIdent("identifier"); + try parts.append(p.arena, id.text); + var part_args: []const ast.TemplateArg = &.{}; + // `S::member`: arguments followed by `::` belong to this part. + if (p.at(.less) and p.isTemplateName(id.text)) { + if (p.skipAngles(p.pos)) |end| { + if (p.toks[end].kind == .colon_colon and p.toks[end + 1].kind == .identifier) { + part_args = try p.parseTemplateArgs(); + any_args = true; + } + } + } + try args.append(p.arena, part_args); + if (!(p.at(.colon_colon) and p.peek(1).kind == .identifier and !p.peek(1).isIdent("operator"))) break; + _ = p.advance(); + } + return .{ .loc = loc, .parts = parts.items, .args = if (any_args) args.items else &.{} }; + } + + fn parseTemplateArgs(p: *Parser) Error![]const ast.TemplateArg { + _ = try p.expect(.less, "'<'"); + var args: std.ArrayList(ast.TemplateArg) = .empty; + const saved = p.no_greater; + p.no_greater = true; + defer p.no_greater = saved; + if (p.at(.greater)) { + _ = p.advance(); + return args.items; + } + while (true) { + var norm: @FieldType(ast.TypeRef, "norm") = .none; + if (p.atIdent("unorm")) norm = .unorm; + if (p.atIdent("snorm")) norm = .snorm; + if (norm != .none) _ = p.advance(); + _ = p.parseQualifiers(); // e.g. `row_major float4x4` + if (p.isTypeStartAt(p.pos)) { + const t = try p.parseType(); + t.norm = norm; + t.array_dims = try p.parseArrayDims(); + try args.append(p.arena, .{ .type = t }); + } else { + try args.append(p.arena, .{ .expr = try p.parseConditional() }); + } + if (!p.eat(.comma)) break; + } + try p.expectCloseAngle(); + return args.items; + } + + fn parseSemantics(p: *Parser, allow_bitfield: bool, bitfield: *?*ast.Expr) Error![]const ast.Semantic { + var sems: std.ArrayList(ast.Semantic) = .empty; + while (p.at(.colon)) { + _ = p.advance(); + const t = p.tok(); + if (allow_bitfield and t.kind != .identifier) { + bitfield.* = try p.parseConditional(); + continue; + } + const ident = try p.expectIdent("semantic"); + const kind: @FieldType(ast.Semantic, "kind") = if (!p.at(.l_paren)) + .semantic + else if (ident.isIdent("register")) + .register + else if (ident.isIdent("packoffset")) + .packoffset + else if (ident.isIdent("read") or ident.isIdent("write")) + .access + else + .semantic; + if (kind == .semantic) { + const values = try p.arena.alloc([]const u8, 1); + values[0] = ident.text; + try sems.append(p.arena, .{ .loc = ident.loc, .kind = .semantic, .values = values }); + continue; + } + _ = try p.expect(.l_paren, "'('"); + var values: std.ArrayList([]const u8) = .empty; + // Access qualifiers keep the qualifier name as the first value. + if (kind == .access) try values.append(p.arena, ident.text); + var cur: std.ArrayList(u8) = .empty; + while (!p.at(.r_paren)) { + const a = p.advance(); + if (a.kind == .eof) return p.fail("expected ')'", .{}); + if (a.kind == .comma) { + try values.append(p.arena, try cur.toOwnedSlice(p.arena)); + continue; + } + try cur.appendSlice(p.arena, a.text); + } + _ = p.advance(); + try values.append(p.arena, cur.items); + try sems.append(p.arena, .{ .loc = ident.loc, .kind = kind, .values = values.items }); + } + return sems.items; + } + + fn parseArrayDims(p: *Parser) Error![]const ?*ast.Expr { + var dims: std.ArrayList(?*ast.Expr) = .empty; + while (p.eat(.l_square)) { + if (p.eat(.r_square)) { + try dims.append(p.arena, null); + continue; + } + try dims.append(p.arena, try p.parseExpr()); + _ = try p.expect(.r_square, "']'"); + } + return dims.items; + } + + // ------------------------------------------------------------------ + // Declarations + + /// `typedef` is used for `typedef struct {...} T;`: the struct body is + /// parsed but declarators are left to the typedef. + const DeclContext = enum { global, member, local, typedef }; + + /// Parse one declaration (possibly several declarators) and append the + /// resulting nodes. + fn parseDeclInto(p: *Parser, out: *std.ArrayList(*ast.Decl), ctx: DeclContext) Error!void { + const start = p.tok(); + if (p.at(.pragma)) { + _ = p.advance(); + return out.append(p.arena, try p.mk(ast.Decl, .{ .loc = start.loc, .kind = .{ .pragma = start.text } })); + } + if (p.eat(.semi)) return; + + var attrs = try p.parseAttributes(); + var template_params: []const ast.TemplateParam = &.{}; + var in_template = false; + if (p.atIdent("template")) { + _ = p.advance(); + try p.pushScope(); + in_template = true; + template_params = try p.parseTemplateParams(); + } + defer if (in_template) p.popScope(); + const more_attrs = try p.parseAttributes(); + if (more_attrs.len > 0) attrs = try std.mem.concat(p.arena, ast.Attribute, &.{ attrs, more_attrs }); + + // Qualifiers may precede a struct definition: `groupshared struct {...} x;`. + const save = p.pos; + const lead_quals = p.parseQualifiers(); + const t = p.tok(); + if (t.isIdent("struct") or t.isIdent("class") or t.isIdent("interface")) { + return p.parseStructDeclInto(out, attrs, lead_quals, template_params, ctx); + } + p.pos = save; + if (t.isIdent("cbuffer") or t.isIdent("tbuffer")) return out.append(p.arena, try p.parseCBuffer()); + if (t.isIdent("typedef")) return p.parseTypedefInto(out); + if (t.isIdent("using")) return out.append(p.arena, try p.parseUsing(template_params)); + if (t.isIdent("namespace")) return out.append(p.arena, try p.parseNamespace()); + if (t.isIdent("enum")) return p.parseEnumInto(out); + if (t.isIdent("static_assert")) { + const d = try p.parseStaticAssert(); + _ = try p.expect(.semi, "';'"); + return out.append(p.arena, d); + } + + const quals = p.parseQualifiers(); + const ty = try p.parseType(); + try p.parseDeclaratorsInto(out, attrs, quals, template_params, ty, ctx); + } + + fn parseTemplateParams(p: *Parser) Error![]const ast.TemplateParam { + _ = try p.expect(.less, "'<'"); + var params: std.ArrayList(ast.TemplateParam) = .empty; + const saved = p.no_greater; + p.no_greater = true; + defer p.no_greater = saved; + while (!p.at(.greater)) { + const loc = p.tok().loc; + if (p.eatIdent("typename") or p.eatIdent("class")) { + const name = if (p.at(.identifier) and !isReserved(p.tok().text)) p.advance() else Token{ .kind = .identifier, .text = "", .loc = loc }; + var default: ?ast.TemplateArg = null; + if (p.eat(.equal)) default = .{ .type = try p.parseType() }; + if (name.text.len > 0) try p.declareName(name.text, .type); + try params.append(p.arena, .{ .loc = loc, .name = name.text, .value_type = null, .default = default }); + } else { + const ty = try p.parseType(); + const name = if (p.at(.identifier) and !isReserved(p.tok().text)) p.advance() else Token{ .kind = .identifier, .text = "", .loc = loc }; + var default: ?ast.TemplateArg = null; + if (p.eat(.equal)) default = .{ .expr = try p.parseConditional() }; + if (name.text.len > 0) try p.declareName(name.text, .value); + try params.append(p.arena, .{ .loc = loc, .name = name.text, .value_type = ty, .default = default }); + } + if (!p.eat(.comma)) break; + } + try p.expectCloseAngle(); + return params.items; + } + + fn parseStructDeclInto(p: *Parser, out: *std.ArrayList(*ast.Decl), lead_attrs: []const ast.Attribute, quals: ast.Qualifiers, template_params: []const ast.TemplateParam, ctx: DeclContext) Error!void { + const kw = p.advance(); + // Attributes may also follow the keyword: `struct [raypayload] P`. + const kw_attrs = try p.parseAttributes(); + const attrs = if (kw_attrs.len == 0) lead_attrs else try std.mem.concat(p.arena, ast.Attribute, &.{ lead_attrs, kw_attrs }); + const keyword: @FieldType(ast.StructDecl, "keyword") = if (kw.isIdent("class")) .class else if (kw.isIdent("interface")) .interface else .@"struct"; + var name: ?[]const u8 = null; + var name_loc = kw.loc; + if (p.at(.identifier) and !isReserved(p.tok().text)) { + const n = p.advance(); + name = n.text; + name_loc = n.loc; + // Declared before the body so members can refer to it. + if (p.lookupName(n.text) == null) { + try p.declareNameOuter(n.text, if (template_params.len > 0) .template_type else .type, template_params.len > 0); + } + } + // Partial or explicit specialization: `struct S { ... }`. + var spec_args: []const ast.TemplateArg = &.{}; + if (name != null and p.at(.less)) spec_args = try p.parseTemplateArgs(); + + p.last_struct_name = name orelse ""; + // `struct S x;` uses an existing struct type; in a typedef the + // declarators belong to the typedef. + if (name != null and !p.at(.l_brace) and !p.at(.colon) and !p.at(.semi)) { + if (ctx == .typedef) return; + const ty = try p.mk(ast.TypeRef, .{ .loc = name_loc, .name = try p.simpleName(name_loc, name.?) }); + return p.parseDeclaratorsInto(out, attrs, quals, &.{}, ty, ctx); + } + + var bases: std.ArrayList(*ast.TypeRef) = .empty; + if (p.eat(.colon)) { + while (true) { + _ = p.eatIdent("public") or p.eatIdent("private") or p.eatIdent("protected"); + try bases.append(p.arena, try p.parseType()); + if (!p.eat(.comma)) break; + } + } + + var members: ?[]const *ast.Decl = null; + if (p.eat(.l_brace)) { + try p.pushScope(); + var list: std.ArrayList(*ast.Decl) = .empty; + while (!p.at(.r_brace) and !p.at(.eof)) { + const before = p.pos; + // Access specifiers: `public:`. + if ((p.atIdent("public") or p.atIdent("private") or p.atIdent("protected")) and p.peek(1).kind == .colon) { + _ = p.advance(); + _ = p.advance(); + continue; + } + p.parseDeclInto(&list, .member) catch |e| switch (e) { + error.OutOfMemory => return error.OutOfMemory, + error.Syntax => p.recover(before), + }; + } + // Nested types stay visible (unqualified) after the struct so + // that `S::Inner` resolves; the name table is flat by last name. + var nested: std.ArrayList(struct { []const u8, NameKind }) = .empty; + for (p.scopes.items[p.scopes.items.len - 1].items) |n| { + if (p.lookupName(n)) |k| if (k == .type or k == .template_type) try nested.append(p.arena, .{ n, k }); + } + p.popScope(); + for (nested.items) |n| try p.declareName(n[0], n[1]); + _ = try p.expect(.r_brace, "'}'"); + members = list.items; + } + + if (name == null) { + p.anon_count += 1; + name = try std.fmt.allocPrint(p.arena, "__anon_struct_{d}", .{p.anon_count}); + } + p.last_struct_name = name.?; + try out.append(p.arena, try p.mk(ast.Decl, .{ .loc = kw.loc, .kind = .{ .@"struct" = .{ + .attrs = attrs, + .keyword = keyword, + .template_params = template_params, + .name = name, + .specialization_args = spec_args, + .bases = bases.items, + .members = members, + } } })); + + if (ctx == .typedef) return; + if (p.eat(.semi)) return; + // `struct S { ... } a, b;` + const ty = try p.mk(ast.TypeRef, .{ .loc = name_loc, .name = try p.simpleName(name_loc, name.?) }); + try p.parseDeclaratorsInto(out, &.{}, quals, &.{}, ty, ctx); + } + + /// Declare a name in the scope enclosing an open template scope (so a + /// template's own name outlives its parameters). + fn declareNameOuter(p: *Parser, name: []const u8, kind: NameKind, in_template: bool) Allocator.Error!void { + if (!in_template or p.scopes.items.len < 2) return p.declareName(name, kind); + const gop = try p.names.getOrPut(p.arena, name); + if (!gop.found_existing) gop.value_ptr.* = .empty; + // Insert below any entries added by the template scope. + try gop.value_ptr.insert(p.arena, 0, kind); + try p.scopes.items[p.scopes.items.len - 2].append(p.arena, name); + } + + fn parseCBuffer(p: *Parser) Error!*ast.Decl { + const kw = p.advance(); + const name = try p.expectIdent("buffer name"); + var bitfield: ?*ast.Expr = null; + const sems = try p.parseSemantics(false, &bitfield); + _ = try p.expect(.l_brace, "'{'"); + var members: std.ArrayList(*ast.Decl) = .empty; + while (!p.at(.r_brace) and !p.at(.eof)) { + const before = p.pos; + p.parseDeclInto(&members, .member) catch |e| switch (e) { + error.OutOfMemory => return error.OutOfMemory, + error.Syntax => p.recover(before), + }; + } + _ = try p.expect(.r_brace, "'}'"); + _ = p.eat(.semi); + return p.mk(ast.Decl, .{ .loc = kw.loc, .kind = .{ .cbuffer = .{ + .tbuffer = kw.isIdent("tbuffer"), + .name = name.text, + .semantics = sems, + .members = members.items, + } } }); + } + + fn parseTypedefInto(p: *Parser, out: *std.ArrayList(*ast.Decl)) Error!void { + const kw = p.advance(); + _ = p.parseQualifiers(); + var ty: *ast.TypeRef = undefined; + if (p.atIdent("struct") or p.atIdent("class")) { + // `typedef struct { ... } Name;` + try p.parseStructDeclInto(out, &.{}, .{}, &.{}, .typedef); + ty = try p.mk(ast.TypeRef, .{ .loc = kw.loc, .name = try p.simpleName(kw.loc, p.last_struct_name) }); + } else ty = try p.parseType(); + while (true) { + const name = try p.expectIdent("typedef name"); + const dims = try p.parseArrayDims(); + try p.declareName(name.text, .type); + try out.append(p.arena, try p.mk(ast.Decl, .{ .loc = kw.loc, .kind = .{ .typedef = .{ .name = name.text, .type = ty, .array_dims = dims } } })); + if (!p.eat(.comma)) break; + } + _ = try p.expect(.semi, "';'"); + } + + fn parseUsing(p: *Parser, template_params: []const ast.TemplateParam) Error!*ast.Decl { + const kw = p.advance(); + if (p.eatIdent("namespace")) { + const n = try p.parseName(); + _ = try p.expect(.semi, "';'"); + return p.mk(ast.Decl, .{ .loc = kw.loc, .kind = .{ .using_namespace = n } }); + } + if (p.peek(1).kind == .colon_colon or p.at(.colon_colon)) { + const n = try p.parseName(); + _ = try p.expect(.semi, "';'"); + return p.mk(ast.Decl, .{ .loc = kw.loc, .kind = .{ .using_decl = n } }); + } + const name = try p.expectIdent("alias name"); + _ = try p.parseAttributes(); // e.g. [[vk::ext_capability(...)]] + _ = try p.expect(.equal, "'='"); + _ = p.parseQualifiers(); + const ty = try p.parseType(); + const dims = try p.parseArrayDims(); + _ = try p.expect(.semi, "';'"); + try p.declareNameOuter(name.text, if (template_params.len > 0) .template_type else .type, template_params.len > 0); + return p.mk(ast.Decl, .{ .loc = kw.loc, .kind = .{ .typedef = .{ .name = name.text, .type = ty, .array_dims = dims, .template_params = template_params } } }); + } + + fn parseNamespace(p: *Parser) Error!*ast.Decl { + const kw = p.advance(); + const name = try p.expectIdent("namespace name"); + _ = try p.expect(.l_brace, "'{'"); + var decls: std.ArrayList(*ast.Decl) = .empty; + while (!p.at(.r_brace) and !p.at(.eof)) { + const before = p.pos; + p.parseDeclInto(&decls, .global) catch |e| switch (e) { + error.OutOfMemory => return error.OutOfMemory, + error.Syntax => p.recover(before), + }; + } + _ = try p.expect(.r_brace, "'}'"); + return p.mk(ast.Decl, .{ .loc = kw.loc, .kind = .{ .namespace = .{ .name = name.text, .decls = decls.items } } }); + } + + fn parseEnumInto(p: *Parser, out: *std.ArrayList(*ast.Decl)) Error!void { + const kw = p.advance(); + const scoped = p.eatIdent("class") or p.eatIdent("struct"); + var name: ?[]const u8 = null; + if (p.at(.identifier) and !isReserved(p.tok().text)) { + name = p.advance().text; + try p.declareName(name.?, .type); + } + var underlying: ?*ast.TypeRef = null; + if (p.eat(.colon)) underlying = try p.parseType(); + _ = try p.expect(.l_brace, "'{'"); + var values: std.ArrayList(@typeInfo(@FieldType(ast.EnumDecl, "values")).pointer.child) = .empty; + while (!p.at(.r_brace)) { + const n = try p.expectIdent("enumerator"); + var value: ?*ast.Expr = null; + if (p.eat(.equal)) value = try p.parseConditional(); + try values.append(p.arena, .{ .loc = n.loc, .name = n.text, .value = value }); + if (!scoped) try p.declareName(n.text, .value); + if (!p.eat(.comma)) break; + } + _ = try p.expect(.r_brace, "'}'"); + try out.append(p.arena, try p.mk(ast.Decl, .{ .loc = kw.loc, .kind = .{ .@"enum" = .{ + .name = name, + .scoped = scoped, + .underlying = underlying, + .values = values.items, + } } })); + if (p.eat(.semi)) return; + const ty = try p.mk(ast.TypeRef, .{ .loc = kw.loc, .name = try p.simpleName(kw.loc, name orelse "int") }); + try p.parseDeclaratorsInto(out, &.{}, .{}, &.{}, ty, .global); + } + + fn parseStaticAssert(p: *Parser) Error!*ast.Decl { + const kw = p.advance(); + _ = try p.expect(.l_paren, "'('"); + const cond = try p.parseAssign(); + var message: ?[]const u8 = null; + if (p.eat(.comma)) message = (try p.expect(.string, "string literal")).text; + _ = try p.expect(.r_paren, "')'"); + return p.mk(ast.Decl, .{ .loc = kw.loc, .kind = .{ .static_assert = .{ .cond = cond, .message = message } } }); + } + + /// After `quals type`: one function, or a list of variable declarators. + fn parseDeclaratorsInto( + p: *Parser, + out: *std.ArrayList(*ast.Decl), + attrs: []const ast.Attribute, + quals: ast.Qualifiers, + template_params: []const ast.TemplateParam, + ty: *ast.TypeRef, + ctx: DeclContext, + ) Error!void { + // Function names may be qualified (`S::method`) or operators; + // variables may be qualified for out-of-line static members. + const name_loc = p.tok().loc; + var fname: ?ast.Name = null; + var qualified_var: ?[]const u8 = null; + if (p.atIdent("operator")) { + fname = try p.parseOperatorName(); + } else if (p.at(.identifier) and (p.peek(1).kind == .colon_colon or p.peek(1).kind == .l_paren or p.peek(1).kind == .less)) { + const save = p.pos; + var n = try p.parseName(); + if (p.at(.colon_colon) and p.peek(1).isIdent("operator")) { + _ = p.advance(); + const op = try p.parseOperatorName(); + n.parts = try std.mem.concat(p.arena, []const u8, &.{ n.parts, op.parts }); + if (n.args.len > 0) n.args = try std.mem.concat(p.arena, []const ast.TemplateArg, &.{ n.args, &.{&.{}} }); + } + if (p.at(.less) and !p.isTemplateName(n.last())) { + p.pos = save; + } else { + // Explicit specialization `f(...)` is not supported yet; + // accept the syntax. + if (p.at(.less)) _ = try p.parseTemplateArgs(); + if (p.at(.l_paren)) { + fname = n; + } else if (n.parts.len > 1 or p.toks[p.pos - 1].kind == .greater) { + // Qualified static member, or a variable template + // specialization `v` (arguments are dropped). + qualified_var = try std.mem.join(p.arena, "::", n.parts); + } else p.pos = save; + } + } + if (fname) |n| return out.append(p.arena, try p.parseFunctionRest(attrs, quals, template_params, ty, n, name_loc, ctx)); + + var first = true; + while (true) { + var name: Token = undefined; + if (first and qualified_var != null) { + name = .{ .kind = .identifier, .text = qualified_var.?, .loc = name_loc }; + } else name = try p.expectIdent("declarator name"); + first = false; + const dims = try p.parseArrayDims(); + var bitfield: ?*ast.Expr = null; + const sems = try p.parseSemantics(ctx == .member, &bitfield); + // Legacy effect syntax: `` and `{ state block }`. + if (ctx != .local and p.at(.less)) try p.skipBalanced(.less, .greater); + var init_expr: ?*ast.Expr = null; + if (ctx != .local and p.at(.l_brace)) { + try p.skipBalanced(.l_brace, .r_brace); + } else if (p.eat(.equal)) init_expr = try p.parseInitializer(); + try out.append(p.arena, try p.mk(ast.Decl, .{ .loc = name.loc, .kind = .{ .@"var" = .{ + .attrs = attrs, + .template_params = template_params, + .quals = quals, + .type = ty, + .name = name.text, + .name_loc = name.loc, + .array_dims = dims, + .semantics = sems, + .bitfield = bitfield, + .init = init_expr, + } } })); + if (ctx == .local) try p.declareName(name.text, .value); + // Variable templates are referenced as `v`. + if (template_params.len > 0 and qualified_var == null) try p.declareNameOuter(name.text, .template_func, true); + if (!p.eat(.comma)) break; + } + _ = try p.expect(.semi, "';'"); + } + + /// Skip a balanced `open ... close` group (legacy annotations and state + /// blocks, which zdxc ignores like dxc does). + fn skipBalanced(p: *Parser, open: Kind, close: Kind) Error!void { + _ = try p.expect(open, "group"); + var depth: u32 = 1; + while (depth > 0) { + const t = p.advance(); + if (t.kind == .eof) return p.fail("unterminated block", .{}); + if (t.kind == open) depth += 1; + if (t.kind == close) depth -= 1; + } + } + + fn parseOperatorName(p: *Parser) Error!ast.Name { + const kw = p.advance(); + const t = p.advance(); + const text = switch (t.kind) { + .l_paren => blk: { + _ = try p.expect(.r_paren, "')'"); + break :blk "operator()"; + }, + .l_square => blk: { + _ = try p.expect(.r_square, "']'"); + break :blk "operator[]"; + }, + .eof, .identifier, .number, .string, .char => return p.fail("expected operator after 'operator'", .{}), + else => try std.mem.concat(p.arena, u8, &.{ "operator", t.text }), + }; + return p.simpleName(kw.loc, text); + } + + fn parseFunctionRest( + p: *Parser, + attrs: []const ast.Attribute, + quals: ast.Qualifiers, + template_params: []const ast.TemplateParam, + ret: *ast.TypeRef, + name: ast.Name, + loc: Loc, + ctx: DeclContext, + ) Error!*ast.Decl { + if (template_params.len > 0) { + try p.declareNameOuter(name.last(), .template_func, true); + } else if (ctx != .member) { + try p.declareName(name.last(), .value); + } + _ = try p.expect(.l_paren, "'('"); + try p.pushScope(); + defer p.popScope(); + var params: std.ArrayList(ast.VarDecl) = .empty; + if (p.atIdent("void") and p.peek(1).kind == .r_paren) _ = p.advance(); + while (!p.at(.r_paren)) { + const pattrs = try p.parseAttributes(); + const pquals = p.parseQualifiers(); + const pty = try p.parseType(); + var pname: []const u8 = ""; + var pname_loc = p.tok().loc; + if (p.at(.identifier) and !isReserved(p.tok().text)) { + const n = p.advance(); + pname = n.text; + pname_loc = n.loc; + try p.declareName(n.text, .value); + } + const dims = try p.parseArrayDims(); + var bitfield: ?*ast.Expr = null; + const sems = try p.parseSemantics(false, &bitfield); + var default: ?*ast.Expr = null; + if (p.eat(.equal)) default = try p.parseAssign(); + try params.append(p.arena, .{ + .attrs = pattrs, + .quals = pquals, + .type = pty, + .name = pname, + .name_loc = pname_loc, + .array_dims = dims, + .semantics = sems, + .init = default, + }); + if (!p.eat(.comma)) break; + } + _ = try p.expect(.r_paren, "')'"); + const ret_dims = try p.parseArrayDims(); + const is_const = p.eatIdent("const"); + var bitfield: ?*ast.Expr = null; + const ret_sems = try p.parseSemantics(false, &bitfield); + var body: ?*ast.Stmt = null; + if (p.at(.l_brace)) { + body = try p.parseCompound(); + _ = p.eat(.semi); + } else { + _ = try p.expect(.semi, "';' or function body"); + } + return p.mk(ast.Decl, .{ .loc = loc, .kind = .{ .func = .{ + .attrs = attrs, + .quals = quals, + .template_params = template_params, + .ret = ret, + .name = name, + .params = params.items, + .ret_semantics = ret_sems, + .ret_dims = ret_dims, + .is_const = is_const, + .body = body, + } } }); + } + + fn parseInitializer(p: *Parser) Error!*ast.Expr { + // Legacy `sampler_state { ... }` initializers are ignored, as in dxc. + if ((p.atIdent("sampler_state") or p.atIdent("SamplerState") or p.atIdent("SamplerComparisonState")) and p.peek(1).kind == .l_brace) { + const kw = p.advance(); + try p.skipBalanced(.l_brace, .r_brace); + return p.mkExpr(kw.loc, .{ .init_list = &.{} }); + } + if (!p.at(.l_brace)) return p.parseAssign(); + const open = p.advance(); + var items: std.ArrayList(*ast.Expr) = .empty; + while (!p.at(.r_brace)) { + try items.append(p.arena, try p.parseInitializer()); + if (!p.eat(.comma)) break; + } + _ = try p.expect(.r_brace, "'}'"); + return p.mkExpr(open.loc, .{ .init_list = items.items }); + } + + // ------------------------------------------------------------------ + // Statements + + fn parseCompound(p: *Parser) Error!*ast.Stmt { + const open = try p.expect(.l_brace, "'{'"); + try p.pushScope(); + defer p.popScope(); + var stmts: std.ArrayList(*ast.Stmt) = .empty; + while (!p.at(.r_brace) and !p.at(.eof)) { + const before = p.pos; + const s = p.parseStmt() catch |e| switch (e) { + error.OutOfMemory => return error.OutOfMemory, + error.Syntax => { + p.recover(before); + continue; + }, + }; + try stmts.append(p.arena, s); + } + _ = try p.expect(.r_brace, "'}'"); + return p.mk(ast.Stmt, .{ .loc = open.loc, .kind = .{ .compound = stmts.items } }); + } + + fn mkStmt(p: *Parser, loc: Loc, attrs: []const ast.Attribute, kind: ast.Stmt.Kind) Allocator.Error!*ast.Stmt { + return p.mk(ast.Stmt, .{ .loc = loc, .attrs = attrs, .kind = kind }); + } + + fn parseStmt(p: *Parser) Error!*ast.Stmt { + const attrs = try p.parseAttributes(); + const t = p.tok(); + if (t.kind == .l_brace) { + const s = try p.parseCompound(); + s.attrs = attrs; + return s; + } + if (t.kind == .semi) { + _ = p.advance(); + return p.mkStmt(t.loc, attrs, .empty); + } + if (t.kind == .pragma) { + _ = p.advance(); + return p.mkStmt(t.loc, attrs, .empty); + } + if (t.kind == .identifier) { + const w = t.text; + if (std.mem.eql(u8, w, "if")) { + _ = p.advance(); + _ = try p.expect(.l_paren, "'('"); + try p.pushScope(); + defer p.popScope(); + const c = try p.parseCondition(); + _ = try p.expect(.r_paren, "')'"); + const then = try p.parseStmt(); + var else_stmt: ?*ast.Stmt = null; + if (p.eatIdent("else")) else_stmt = try p.parseStmt(); + return p.mkStmt(t.loc, attrs, .{ .@"if" = .{ .cond_decl = c.decl, .cond = c.cond, .then = then, .@"else" = else_stmt } }); + } + if (std.mem.eql(u8, w, "for")) { + _ = p.advance(); + _ = try p.expect(.l_paren, "'('"); + // HLSL 2021: the loop variable is scoped to the loop. + try p.pushScope(); + defer p.popScope(); + var init_stmt: ?*ast.Stmt = null; + if (!p.eat(.semi)) init_stmt = try p.parseSimpleStmt(true); + var cond: ?*ast.Expr = null; + if (!p.at(.semi)) cond = try p.parseExpr(); + _ = try p.expect(.semi, "';'"); + var inc: ?*ast.Expr = null; + if (!p.at(.r_paren)) inc = try p.parseExpr(); + _ = try p.expect(.r_paren, "')'"); + const body = try p.parseStmt(); + return p.mkStmt(t.loc, attrs, .{ .@"for" = .{ .init = init_stmt, .cond = cond, .inc = inc, .body = body } }); + } + if (std.mem.eql(u8, w, "while")) { + _ = p.advance(); + _ = try p.expect(.l_paren, "'('"); + try p.pushScope(); + defer p.popScope(); + const c = try p.parseCondition(); + _ = try p.expect(.r_paren, "')'"); + return p.mkStmt(t.loc, attrs, .{ .@"while" = .{ .cond_decl = c.decl, .cond = c.cond, .body = try p.parseStmt() } }); + } + if (std.mem.eql(u8, w, "do")) { + _ = p.advance(); + const body = try p.parseStmt(); + if (!p.eatIdent("while")) return p.fail("expected 'while' after do-statement body", .{}); + _ = try p.expect(.l_paren, "'('"); + const cond = try p.parseExpr(); + _ = try p.expect(.r_paren, "')'"); + _ = try p.expect(.semi, "';'"); + return p.mkStmt(t.loc, attrs, .{ .do_while = .{ .body = body, .cond = cond } }); + } + if (std.mem.eql(u8, w, "switch")) { + _ = p.advance(); + _ = try p.expect(.l_paren, "'('"); + try p.pushScope(); + defer p.popScope(); + const c = try p.parseCondition(); + _ = try p.expect(.r_paren, "')'"); + return p.mkStmt(t.loc, attrs, .{ .@"switch" = .{ .cond_decl = c.decl, .cond = c.cond, .body = try p.parseStmt() } }); + } + if (std.mem.eql(u8, w, "case")) { + _ = p.advance(); + const value = try p.parseConditional(); + _ = try p.expect(.colon, "':'"); + return p.mkStmt(t.loc, attrs, .{ .case = .{ .value = value, .stmt = try p.parseCaseBody() } }); + } + if (std.mem.eql(u8, w, "default") and p.peek(1).kind == .colon) { + _ = p.advance(); + _ = p.advance(); + return p.mkStmt(t.loc, attrs, .{ .default = try p.parseCaseBody() }); + } + if (std.mem.eql(u8, w, "break") or std.mem.eql(u8, w, "continue") or std.mem.eql(u8, w, "discard")) { + _ = p.advance(); + _ = try p.expect(.semi, "';'"); + const kind: ast.Stmt.Kind = if (w[0] == 'b') .@"break" else if (w[0] == 'c') .@"continue" else .discard; + return p.mkStmt(t.loc, attrs, kind); + } + if (std.mem.eql(u8, w, "return")) { + _ = p.advance(); + var value: ?*ast.Expr = null; + if (!p.at(.semi)) value = if (p.at(.l_brace)) try p.parseInitializer() else try p.parseExpr(); + _ = try p.expect(.semi, "';'"); + return p.mkStmt(t.loc, attrs, .{ .@"return" = value }); + } + } + const s = try p.parseSimpleStmt(false); + s.attrs = attrs; + return s; + } + + /// `expr`, or `T name = init` (C++ condition declaration). + fn parseCondition(p: *Parser) Error!struct { decl: ?*ast.Decl, cond: *ast.Expr } { + if (!p.startsLocalDecl()) return .{ .decl = null, .cond = try p.parseExpr() }; + const quals = p.parseQualifiers(); + const ty = try p.parseType(); + const name = try p.expectIdent("variable name"); + _ = try p.expect(.equal, "'='"); + const init_expr = try p.parseAssign(); + try p.declareName(name.text, .value); + const decl = try p.mk(ast.Decl, .{ .loc = name.loc, .kind = .{ .@"var" = .{ + .attrs = &.{}, + .quals = quals, + .type = ty, + .name = name.text, + .name_loc = name.loc, + .array_dims = &.{}, + .semantics = &.{}, + .init = init_expr, + } } }); + const ref = try p.mkExpr(name.loc, .{ .name = try p.simpleName(name.loc, name.text) }); + return .{ .decl = decl, .cond = ref }; + } + + /// A `case`/`default` label applies to the following statement; an + /// immediately following label or `}` yields an empty statement. + fn parseCaseBody(p: *Parser) Error!*ast.Stmt { + if (p.at(.r_brace)) return p.mkStmt(p.tok().loc, &.{}, .empty); + return p.parseStmt(); + } + + /// Declaration or expression statement, ending with `;`. + fn parseSimpleStmt(p: *Parser, in_for: bool) Error!*ast.Stmt { + _ = in_for; + const t = p.tok(); + if (p.startsLocalDecl()) { + var decls: std.ArrayList(*ast.Decl) = .empty; + try p.parseDeclInto(&decls, .local); + return p.mkStmt(t.loc, &.{}, .{ .decl = decls.items }); + } + const e = try p.parseExpr(); + _ = try p.expect(.semi, "';'"); + return p.mkStmt(t.loc, &.{}, .{ .expr = e }); + } + + fn startsLocalDecl(p: *const Parser) bool { + const t = p.tok(); + if (t.kind != .identifier and t.kind != .colon_colon) return false; + if (t.kind == .identifier) { + const w = t.text; + if (std.mem.eql(u8, w, "struct") or std.mem.eql(u8, w, "class") or std.mem.eql(u8, w, "typedef") or + std.mem.eql(u8, w, "using") or std.mem.eql(u8, w, "enum") or std.mem.eql(u8, w, "static_assert")) + return true; + if (qualifierField(w) != null) { + if (!isContextualQualifier(w)) return true; + const n = p.peek(1); + if ((n.kind == .identifier and qualifierField(n.text) != null) or p.isTypeStartAt(p.pos + 1)) return true; + } + } + // `Type name ...` + const end = p.scanType(p.pos) orelse return false; + const n = p.toks[end]; + return n.kind == .identifier and !isReserved(n.text); + } + + // ------------------------------------------------------------------ + // Expressions + + pub fn parseExpr(p: *Parser) Error!*ast.Expr { + var lhs = try p.parseAssign(); + while (p.at(.comma)) { + const op = p.advance(); + const rhs = try p.parseAssign(); + lhs = try p.mkExpr(op.loc, .{ .binary = .{ .op = .comma, .lhs = lhs, .rhs = rhs } }); + } + return lhs; + } + + fn parseAssign(p: *Parser) Error!*ast.Expr { + const lhs = try p.parseConditional(); + const op: ?ast.AssignOp = switch (p.tok().kind) { + .equal => .assign, + .star_equal => .mul, + .slash_equal => .div, + .percent_equal => .rem, + .plus_equal => .add, + .minus_equal => .sub, + .less_less_equal => .shl, + .greater_greater_equal => if (p.no_greater) null else .shr, + .amp_equal => .bit_and, + .caret_equal => .bit_xor, + .pipe_equal => .bit_or, + else => null, + }; + const o = op orelse return lhs; + const t = p.advance(); + const rhs = try p.parseAssign(); + return p.mkExpr(t.loc, .{ .assign = .{ .op = o, .lhs = lhs, .rhs = rhs } }); + } + + fn parseConditional(p: *Parser) Error!*ast.Expr { + const cond = try p.parseBinary(1); + if (!p.at(.question)) return cond; + const q = p.advance(); + const saved = p.no_greater; + p.no_greater = false; + const then = try p.parseExpr(); + p.no_greater = saved; + _ = try p.expect(.colon, "':'"); + const else_expr = try p.parseAssign(); + return p.mkExpr(q.loc, .{ .conditional = .{ .cond = cond, .then = then, .@"else" = else_expr } }); + } + + fn binaryOp(p: *const Parser, k: Kind) ?struct { op: ast.BinaryOp, prec: u8 } { + return switch (k) { + .pipe_pipe => .{ .op = .log_or, .prec = 1 }, + .amp_amp => .{ .op = .log_and, .prec = 2 }, + .pipe => .{ .op = .bit_or, .prec = 3 }, + .caret => .{ .op = .bit_xor, .prec = 4 }, + .amp => .{ .op = .bit_and, .prec = 5 }, + .equal_equal => .{ .op = .eq, .prec = 6 }, + .exclaim_equal => .{ .op = .ne, .prec = 6 }, + .less => .{ .op = .lt, .prec = 7 }, + .greater => if (p.no_greater) null else .{ .op = .gt, .prec = 7 }, + .less_equal => .{ .op = .le, .prec = 7 }, + .greater_equal => if (p.no_greater) null else .{ .op = .ge, .prec = 7 }, + .less_less => .{ .op = .shl, .prec = 8 }, + .greater_greater => if (p.no_greater) null else .{ .op = .shr, .prec = 8 }, + .plus => .{ .op = .add, .prec = 9 }, + .minus => .{ .op = .sub, .prec = 9 }, + .star => .{ .op = .mul, .prec = 10 }, + .slash => .{ .op = .div, .prec = 10 }, + .percent => .{ .op = .rem, .prec = 10 }, + else => null, + }; + } + + fn parseBinary(p: *Parser, min_prec: u8) Error!*ast.Expr { + var lhs = try p.parseUnary(); + while (p.binaryOp(p.tok().kind)) |info| { + if (info.prec < min_prec) break; + const t = p.advance(); + const rhs = try p.parseBinary(info.prec + 1); + lhs = try p.mkExpr(t.loc, .{ .binary = .{ .op = info.op, .lhs = lhs, .rhs = rhs } }); + } + return lhs; + } + + fn parseUnary(p: *Parser) Error!*ast.Expr { + const t = p.tok(); + const op: ?ast.UnaryOp = switch (t.kind) { + .plus => .plus, + .minus => .minus, + .exclaim => .not, + .tilde => .bit_not, + .plus_plus => .pre_inc, + .minus_minus => .pre_dec, + else => null, + }; + if (op) |o| { + _ = p.advance(); + const operand = try p.parseUnary(); + return p.mkExpr(t.loc, .{ .unary = .{ .op = o, .operand = operand } }); + } + // C-style cast: `(type)expr` or `(type[N])expr`. + if (t.kind == .l_paren) { + var type_start = p.pos + 1; + while (p.toks[type_start].kind == .identifier and qualifierField(p.toks[type_start].text) != null) type_start += 1; + if (p.scanType(type_start)) |end| { + var i = end; + while (p.toks[i].kind == .l_square) { + while (p.toks[i].kind != .r_square and p.toks[i].kind != .eof) i += 1; + i += 1; + } + if (p.toks[i].kind == .r_paren) { + _ = p.advance(); + _ = p.parseQualifiers(); + const ty = try p.parseType(); + const dims = try p.parseArrayDims(); + _ = try p.expect(.r_paren, "')'"); + const operand = try p.parseUnary(); + return p.mkExpr(t.loc, .{ .cast = .{ .type = ty, .array_dims = dims, .operand = operand } }); + } + } + } + if (t.isIdent("sizeof")) { + _ = p.advance(); + if (p.at(.l_paren) and p.isTypeStartAt(p.pos + 1)) { + _ = p.advance(); + const ty = try p.parseType(); + ty.array_dims = try p.parseArrayDims(); + _ = try p.expect(.r_paren, "')'"); + return p.mkExpr(t.loc, .{ .sizeof_type = ty }); + } + return p.mkExpr(t.loc, .{ .sizeof_expr = try p.parseUnary() }); + } + return p.parsePostfix(try p.parsePrimary()); + } + + fn parseArgs(p: *Parser) Error![]const *ast.Expr { + _ = try p.expect(.l_paren, "'('"); + const saved = p.no_greater; + p.no_greater = false; + defer p.no_greater = saved; + var args: std.ArrayList(*ast.Expr) = .empty; + while (!p.at(.r_paren)) { + try args.append(p.arena, try p.parseAssign()); + if (!p.eat(.comma)) break; + } + _ = try p.expect(.r_paren, "')'"); + return args.items; + } + + fn parsePostfix(p: *Parser, base: *ast.Expr) Error!*ast.Expr { + var e = base; + while (true) { + const t = p.tok(); + switch (t.kind) { + .l_paren => { + const args = try p.parseArgs(); + e = try p.mkExpr(t.loc, .{ .call = .{ .callee = e, .args = args } }); + }, + .l_square => { + _ = p.advance(); + const saved = p.no_greater; + p.no_greater = false; + const index = try p.parseExpr(); + p.no_greater = saved; + _ = try p.expect(.r_square, "']'"); + e = try p.mkExpr(t.loc, .{ .index = .{ .base = e, .index = index } }); + }, + .period, .arrow => { + _ = p.advance(); + // C++ disambiguator: `obj.template Load(i)`. + const forced_template = p.atIdent("template") and p.peek(1).kind == .identifier; + if (forced_template) _ = p.advance(); + const name = try p.expectMemberName(); + e = try p.mkExpr(t.loc, .{ .member = .{ .base = e, .name = name.text, .name_loc = name.loc } }); + // Method template call: `obj.Load(i)`. + if (p.at(.less) and (forced_template or p.looksLikeTemplateCall())) { + const targs = try p.parseTemplateArgs(); + const args = try p.parseArgs(); + e = try p.mkExpr(t.loc, .{ .call = .{ .callee = e, .args = args, .template_args = targs } }); + } + }, + .plus_plus, .minus_minus => { + _ = p.advance(); + e = try p.mkExpr(t.loc, .{ .unary = .{ .op = if (t.kind == .plus_plus) .post_inc else .post_dec, .operand = e } }); + }, + else => return e, + } + } + } + + fn expectMemberName(p: *Parser) Error!Token { + // Member names may coincide with keywords used as swizzles (e.g. + // `.x`) or contextual keywords; accept any identifier. + if (p.at(.identifier)) return p.advance(); + return p.fail("expected member name, found '{s}'", .{describe(p.tok())}); + } + + /// After a member name, `<` starts template arguments if a matching `>` + /// is followed by `(`. + fn looksLikeTemplateCall(p: *const Parser) bool { + var i = p.pos; + var depth: u32 = 0; + while (true) : (i += 1) { + switch (p.toks[i].kind) { + .less => depth += 1, + .greater => { + depth -= 1; + if (depth == 0) return p.toks[i + 1].kind == .l_paren; + }, + .greater_greater => { + if (depth <= 2) return depth == 2 and p.toks[i + 1].kind == .l_paren; + depth -= 2; + }, + .semi, .l_brace, .r_brace, .eof, .amp_amp, .pipe_pipe => return false, + else => {}, + } + } + } + + fn parsePrimary(p: *Parser) Error!*ast.Expr { + const t = p.tok(); + switch (t.kind) { + .number => { + _ = p.advance(); + return p.mkExpr(t.loc, if (isFloatLiteral(t.text)) .{ .float_lit = t.text } else .{ .int_lit = t.text }); + }, + .string => { + _ = p.advance(); + // Adjacent string literals concatenate. + var text = t.text; + while (p.at(.string)) { + const n = p.advance(); + text = try std.mem.concat(p.arena, u8, &.{ text[0 .. text.len - 1], n.text[1..] }); + } + return p.mkExpr(t.loc, .{ .string_lit = text }); + }, + .char => { + _ = p.advance(); + return p.mkExpr(t.loc, .{ .int_lit = t.text }); + }, + .l_paren => { + _ = p.advance(); + const saved = p.no_greater; + p.no_greater = false; + const e = try p.parseExpr(); + p.no_greater = saved; + _ = try p.expect(.r_paren, "')'"); + return e; + }, + .l_brace => return p.parseInitializer(), + .identifier, .colon_colon => {}, + else => return p.fail("expected expression, found '{s}'", .{describe(t)}), + } + if (t.isIdent("true") or t.isIdent("false")) { + _ = p.advance(); + return p.mkExpr(t.loc, .{ .bool_lit = t.text[0] == 't' }); + } + if (t.isIdent("this")) { + _ = p.advance(); + return p.mkExpr(t.loc, .this); + } + if (t.kind == .identifier and isReserved(t.text)) { + return p.fail("expected expression, found '{s}'", .{t.text}); + } + // Type used as a constructor: `float4(...)`, `vector(...)`. + if (p.scanType(p.pos)) |end| { + if (p.toks[end].kind == .l_paren) { + const ty = try p.parseType(); + const args = try p.parseArgs(); + return p.mkExpr(t.loc, .{ .construct = .{ .type = ty, .args = args } }); + } + } + const name = try p.parseName(); + // Explicit template arguments: `f(x)`. + if (p.at(.less) and p.isTemplateName(name.last())) { + const targs = try p.parseTemplateArgs(); + if (!p.at(.l_paren)) { + // Variable template use: `is_same_v`. + var n = name; + const args = try p.arena.alloc([]const ast.TemplateArg, n.parts.len); + for (args, 0..) |*a, i| a.* = if (n.args.len > 0) n.args[i] else &.{}; + args[args.len - 1] = targs; + n.args = args; + return p.mkExpr(t.loc, .{ .name = n }); + } + const callee = try p.mkExpr(t.loc, .{ .name = name }); + const args = try p.parseArgs(); + return p.mkExpr(t.loc, .{ .call = .{ .callee = callee, .args = args, .template_args = targs } }); + } + return p.mkExpr(t.loc, .{ .name = name }); + } +}; + +fn describe(t: Token) []const u8 { + return if (t.kind == .eof) "end of file" else t.text; +} + +fn isFloatLiteral(text: []const u8) bool { + if (text.len > 1 and text[0] == '0' and (text[1] == 'x' or text[1] == 'X')) return false; + if (std.mem.indexOfAny(u8, text, ".eE") != null) return true; + const last = text[text.len - 1]; + return last == 'f' or last == 'F' or last == 'h' or last == 'H'; +} + +/// Words that can never be identifiers. +fn isReserved(w: []const u8) bool { + const words = [_][]const u8{ + "if", "else", "for", "while", "do", "switch", "case", + "default", "break", "continue", "return", "discard", "struct", "class", + "interface", "cbuffer", "tbuffer", "typedef", "namespace", "enum", "template", + "typename", "true", "false", "this", "sizeof", "static", "const", + "uniform", "extern", "volatile", "groupshared", "inline", "using", "operator", + "static_assert", "inout", "in", "out", + }; + for (words) |k| if (std.mem.eql(u8, w, k)) return true; + return false; +} + +/// Maps a qualifier keyword to its ast.Qualifiers field name. +fn qualifierField(w: []const u8) ?[]const u8 { + const map = [_][2][]const u8{ + .{ "const", "const" }, .{ "static", "static" }, .{ "uniform", "uniform" }, + .{ "extern", "extern" }, .{ "volatile", "volatile" }, .{ "precise", "precise" }, + .{ "groupshared", "groupshared" }, .{ "shared", "shared" }, .{ "in", "in" }, + .{ "out", "out" }, .{ "inout", "in" }, .{ "row_major", "row_major" }, + .{ "column_major", "column_major" }, .{ "nointerpolation", "nointerpolation" }, .{ "linear", "linear" }, + .{ "centroid", "centroid" }, .{ "sample", "sample" }, .{ "noperspective", "noperspective" }, + .{ "globallycoherent", "globallycoherent" }, .{ "reordercoherent", "reordercoherent" }, .{ "inline", "inline" }, + .{ "export", "export" }, .{ "point", "point" }, .{ "line", "line" }, + .{ "triangle", "triangle" }, .{ "lineadj", "lineadj" }, .{ "triangleadj", "triangleadj" }, + .{ "vertices", "vertices" }, .{ "indices", "indices" }, .{ "primitives", "primitives" }, + .{ "payload", "payload" }, + }; + for (map) |e| if (std.mem.eql(u8, w, e[0])) return e[1]; + return null; +} + +/// Qualifiers that are ordinary identifiers unless a type follows. +fn isContextualQualifier(w: []const u8) bool { + const words = [_][]const u8{ + "precise", "shared", "row_major", "column_major", "nointerpolation", "linear", "centroid", + "sample", "noperspective", "globallycoherent", "reordercoherent", "export", "point", "line", + "triangle", "lineadj", "triangleadj", "vertices", "indices", "primitives", "payload", + }; + for (words) |k| if (std.mem.eql(u8, w, k)) return true; + return false; +} + +const scalar_types = [_][]const u8{ + "bool", "int", "uint", "dword", "half", "float", "double", + "min16float", "min10float", "min16int", "min12int", "min16uint", "int16_t", "uint16_t", + "int32_t", "uint32_t", "int64_t", "uint64_t", "float16_t", "float32_t", "float64_t", + "int8_t4_packed", "uint8_t4_packed", +}; + +/// Builtin object and template types (resources, streams, ray tracing...). +const object_types = [_][]const u8{ + "vector", "matrix", "Buffer", + "RWBuffer", "ByteAddressBuffer", "RWByteAddressBuffer", + "StructuredBuffer", "RWStructuredBuffer", "AppendStructuredBuffer", + "ConsumeStructuredBuffer", "Texture1D", "Texture1DArray", + "Texture2D", "Texture2DArray", "Texture2DMS", + "Texture2DMSArray", "Texture3D", "TextureCube", + "TextureCubeArray", "RWTexture1D", "RWTexture1DArray", + "RWTexture2D", "RWTexture2DArray", "RWTexture3D", + "RWTexture2DMS", "RWTexture2DMSArray", "RasterizerOrderedBuffer", + "RasterizerOrderedByteAddressBuffer", "RasterizerOrderedStructuredBuffer", "RasterizerOrderedTexture1D", + "RasterizerOrderedTexture1DArray", "RasterizerOrderedTexture2D", "RasterizerOrderedTexture2DArray", + "RasterizerOrderedTexture3D", "FeedbackTexture2D", "FeedbackTexture2DArray", + "SamplerState", "SamplerComparisonState", "sampler", + "sampler1D", "sampler2D", "sampler3D", + "samplerCUBE", "sampler_state", "texture", + "Texture", "ConstantBuffer", "TextureBuffer", + "PointStream", "LineStream", "TriangleStream", + "InputPatch", "OutputPatch", "RaytracingAccelerationStructure", + "RayDesc", "BuiltInTriangleIntersectionAttributes", "RayQuery", + "string", "void", "SubpassInput", + "SubpassInputMS", "NodeOutput", "NodeOutputArray", + "EmptyNodeOutput", "EmptyNodeOutputArray", "DispatchNodeInputRecord", + "RWDispatchNodeInputRecord", "GroupNodeInputRecords", "RWGroupNodeInputRecords", + "ThreadNodeInputRecord", "RWThreadNodeInputRecord", "EmptyNodeInput", + "GroupNodeOutputRecords", "ThreadNodeOutputRecords", "StateObjectConfig", + "GlobalRootSignature", "LocalRootSignature", "SubobjectToExportsAssociation", + "RaytracingShaderConfig", "RaytracingPipelineConfig", "RaytracingPipelineConfig1", + "TriangleHitGroup", "ProceduralPrimitiveHitGroup", "HitObject", + "SpirvType", "SpirvOpaqueType", "Literal", + "integral_constant", "ext_type", "ext_result_id", + "BufferPointer", "SampledTexture2D", "SampledTexture2DArray", + "SampledTexture1D", "SampledTexture1DArray", "SampledTexture3D", + "SampledTextureCUBE", "SampledTextureCUBEArray", "SampledTexture2DMS", + "SampledTexture2DMSArray", "WorkgroupSpirvPointer", "auto", + "__builtin_LinAlgMatrix", "BuiltInTrianglePositions", +}; + +/// Templates among the builtin object types. +const template_types = [_][]const u8{ + "vector", "matrix", "Buffer", + "RWBuffer", "StructuredBuffer", "RWStructuredBuffer", + "AppendStructuredBuffer", "ConsumeStructuredBuffer", "Texture1D", + "Texture1DArray", "Texture2D", "Texture2DArray", + "Texture2DMS", "Texture2DMSArray", "Texture3D", + "TextureCube", "TextureCubeArray", "RWTexture1D", + "RWTexture1DArray", "RWTexture2D", "RWTexture2DArray", + "RWTexture3D", "RWTexture2DMS", "RWTexture2DMSArray", + "RasterizerOrderedBuffer", "RasterizerOrderedStructuredBuffer", "RasterizerOrderedTexture1D", + "RasterizerOrderedTexture1DArray", "RasterizerOrderedTexture2D", "RasterizerOrderedTexture2DArray", + "RasterizerOrderedTexture3D", "FeedbackTexture2D", "FeedbackTexture2DArray", + "ConstantBuffer", "TextureBuffer", "PointStream", + "LineStream", "TriangleStream", "InputPatch", + "OutputPatch", "RayQuery", "SubpassInput", + "SubpassInputMS", "NodeOutput", "NodeOutputArray", + "DispatchNodeInputRecord", "RWDispatchNodeInputRecord", "GroupNodeInputRecords", + "RWGroupNodeInputRecords", "ThreadNodeInputRecord", "RWThreadNodeInputRecord", + "GroupNodeOutputRecords", "ThreadNodeOutputRecords", "SpirvType", + "SpirvOpaqueType", "Literal", "integral_constant", + "BufferPointer", "SampledTexture2D", "SampledTexture2DArray", + "SampledTexture1D", "SampledTexture1DArray", "SampledTexture3D", + "SampledTextureCUBE", "SampledTextureCUBEArray", "SampledTexture2DMS", + "SampledTexture2DMSArray", "ext_result_id", "ext_type", + "WorkgroupSpirvPointer", +}; + +/// Builtin function templates called with explicit arguments. +const template_funcs = [_][]const u8{ "RawBufferLoad", "RawBufferStore", "static_pointer_cast", "reinterpret_pointer_cast" }; + +fn inList(list: []const []const u8, w: []const u8) bool { + for (list) |k| if (std.mem.eql(u8, w, k)) return true; + return false; +} + +fn isNumericScalar(w: []const u8) bool { + return inList(&scalar_types, w) and !std.mem.endsWith(u8, w, "_packed"); +} + +/// Scalars, `<1-4>` vectors, `<1-4>x<1-4>` matrices and +/// builtin object types. +pub fn isBuiltinType(w: []const u8) bool { + if (inList(&scalar_types, w) or inList(&object_types, w)) return true; + if (w.len < 2) return false; + const last = w[w.len - 1]; + if (last < '1' or last > '4') return false; + if (w.len >= 4 and w[w.len - 2] == 'x' and w[w.len - 3] >= '1' and w[w.len - 3] <= '4') { + if (isNumericScalar(w[0 .. w.len - 3])) return true; + } + return isNumericScalar(w[0 .. w.len - 1]); +} + +fn isBuiltinTemplate(w: []const u8) bool { + return inList(&template_types, w) or inList(&template_funcs, w); +} + +fn unsignedName(a: Allocator, base: []const u8) Allocator.Error![]const u8 { + if (std.mem.eql(u8, base, "int")) return "uint"; + if (std.mem.startsWith(u8, base, "int")) return std.mem.concat(a, u8, &.{ "u", base }); + if (std.mem.startsWith(u8, base, "min16int")) return "min16uint"; + return base; +} + +// ------------------------------------------------------------------------- +// Tests + +const testing = std.testing; +const ast_dump = @import("ast_dump.zig"); + +const TestParse = struct { + mfs: source.MemoryFs = .{}, + sm: source.SourceManager = undefined, + diags: Diagnostics = undefined, + arena: std.heap.ArenaAllocator = undefined, + out: std.Io.Writer.Allocating = undefined, + + fn run(t: *TestParse, text: []const u8) ![]const u8 { + const gpa = testing.allocator; + t.sm = .init(gpa, t.mfs.fileSystem()); + t.diags = .init(gpa); + t.arena = .init(gpa); + const a = t.arena.allocator(); + var pp = Preprocessor.init(a, &t.sm, &t.diags, .{}); + try pp.enterMainText("t.hlsl", text); + var p = try Parser.fromPreprocessor(a, &t.diags, &pp); + const tu = try p.parseTranslationUnit(); + t.out = .init(a); + try ast_dump.dumpUnit(&t.out.writer, tu); + return std.mem.trimEnd(u8, t.out.written(), "\n"); + } + + fn deinit(t: *TestParse) void { + t.arena.deinit(); + t.diags.deinit(); + t.sm.deinit(); + } +}; + +fn expectAst(text: []const u8, expected: []const u8) !void { + var t: TestParse = .{}; + defer t.deinit(); + const got = try t.run(text); + for (t.diags.list.items) |d| std.debug.print("unexpected diagnostic: {s}\n", .{d.message}); + try testing.expectEqualStrings(expected, got); + try testing.expectEqual(@as(u32, 0), t.diags.error_count); +} + +test "declarations vs expressions and casts" { + try expectAst( + \\struct S { float a; }; + \\void f(int a) { S s; (S)a; (a) + 1; float2(1, 2); a < 3; } + , + \\(struct S + \\ (var float a)) + \\(func void f(int a) + \\ { + \\ (var S s) + \\ (cast S a) + \\ (add a 1) + \\ (construct float2 1 2) + \\ (lt a 3)}) + ); +} + +test "templates, operators and bitfields parse" { + try expectAst( + \\template struct Pair { T v[N]; uint flags : 4; }; + \\template T twice(T x) { return x + x; } + \\struct V { float x; V operator+(V o) { V r; r.x = x + o.x; return r; } }; + \\void g() { Pair p; float y = twice(1.0); Texture2D> t; } + , + \\(struct template Pair + \\ (var T v[N]) + \\ (var uint flags : 4)) + \\(func template T twice(T x) + \\ { + \\ (return (add x x))}) + \\(struct V + \\ (var float x) + \\ (func V operator+(V o) + \\ { + \\ (var V r) + \\ (= (. r x) (add x (. o x))) + \\ (return r)})) + \\(func void g() + \\ { + \\ (var Pair p) + \\ (var float y = (call twice 1.0)) + \\ (var Texture2D> t)}) + ); +} + +test "contextual keywords stay identifiers" { + try expectAst( + \\void f(float sample, float line) { float point = sample + line; point = 1; } + , + \\(func void f(float sample, float line) + \\ { + \\ (var float point = (add sample line)) + \\ (= point 1)}) + ); +} + +test "namespaces, enums, typedef, using" { + try expectAst( + \\namespace N { struct T { int i; }; enum class E : uint { A, B = 3 }; } + \\typedef N::T Alias; + \\using F4 = float4; + \\Alias a; F4 b; uint e = (uint)N::E::B; + , + \\(namespace N + \\ (struct T + \\ (var int i)) + \\ (enum class E A B=3)) + \\(typedef N::T Alias) + \\(typedef float4 F4) + \\(var Alias a) + \\(var F4 b) + \\(var uint e = (cast uint N::E::B)) + ); +} + +test "syntax errors recover at statement boundaries" { + var t: TestParse = .{}; + defer t.deinit(); + const got = try t.run("void f() { int a = ; a = 2; }\nint b;"); + try testing.expectEqual(@as(u32, 1), t.diags.error_count); + try testing.expectEqualStrings( + \\(func void f() + \\ { + \\ (= a 2)}) + \\(var int b) + , got); +} + +test "typedef struct forms" { + try expectAst( + \\struct Node; + \\typedef struct Node Node; + \\typedef struct { int a; } Anon[2]; + \\Node n; Anon x; + , + \\(struct Node ) + \\(typedef Node Node) + \\(struct __anon_struct_1 + \\ (var int a)) + \\(typedef __anon_struct_1 Anon[2]) + \\(var Node n) + \\(var Anon x) + ); +} + +test "C++-isms found in the corpus" { + try expectAst( + \\template static const bool is_same_v = false; + \\struct B { float x; }; + \\void f(ByteAddressBuffer buf) { + \\ bool b = is_same_v; + \\ B v = buf.template Load(0); + \\ if (int d = 3) { d = 1; } + \\ float2x2 m = (row_major float2x2)0; + \\} + , + \\(var template const static bool is_same_v = false) + \\(struct B + \\ (var float x)) + \\(func void f(ByteAddressBuffer buf) + \\ { + \\ (var bool b = is_same_v) + \\ (var B v = (call (. buf Load) 0)) + \\ (if (var int d = 3) d + \\ { + \\ (= d 1)}) + \\ (var float2x2 m = (cast float2x2 0))}) + ); +} diff --git a/src/main.zig b/src/main.zig index 5f7dd48..5bd0a43 100644 --- a/src/main.zig +++ b/src/main.zig @@ -28,6 +28,8 @@ const usage = \\ -zdxc-check-hash Verify the container hash of \\ -zdxc-lex Print the tokens of , one per line (no preprocessing; \\ `#line` lines are skipped) + \\ -zdxc-parse Preprocess and parse , reporting syntax errors only + \\ -zdxc-ast Preprocess and parse and dump the syntax tree \\ ; @@ -45,6 +47,8 @@ const Options = struct { empty: bool = false, check_hash: bool = false, lex: bool = false, + parse: bool = false, + dump_ast: bool = false, }; const Cli = struct { @@ -119,6 +123,11 @@ fn parseArgs(cli: Cli, arena: std.mem.Allocator, args: []const [:0]const u8, std opts.check_hash = true; } else if (std.mem.eql(u8, a, "-zdxc-lex")) { opts.lex = true; + } else if (std.mem.eql(u8, a, "-zdxc-parse")) { + opts.parse = true; + } else if (std.mem.eql(u8, a, "-zdxc-ast")) { + opts.parse = true; + opts.dump_ast = true; } else if (std.mem.eql(u8, a, "--version")) { try stdout.print("zdxc {s}\n", .{zdxc.version}); return null; @@ -173,7 +182,7 @@ fn run(cli: Cli, arena: std.mem.Allocator, args: []const [:0]const u8, stdout: * else null; - if (opts.preprocess) { + if (opts.preprocess or opts.parse) { const input = opts.input orelse return cli.fail("no input file", .{}); var disk: front.source.DiskFs = .{ .io = cli.io }; var sm = front.source.SourceManager.init(arena, disk.fileSystem()); @@ -188,7 +197,13 @@ fn run(cli: Cli, arena: std.mem.Allocator, args: []const [:0]const u8, stdout: * .enable_16bit = opts.enable_16bit, }); var out: Io.Writer.Allocating = .init(arena); - if (try pp.enterMainFile(input)) try front.pp_print.print(&pp, &out.writer); + if (try pp.enterMainFile(input)) { + if (opts.parse) { + var parser = try front.parser.Parser.fromPreprocessor(arena, &diags, &pp); + const tu = try parser.parseTranslationUnit(); + if (opts.dump_ast) try front.ast_dump.dumpUnit(stdout, tu); + } else try front.pp_print.print(&pp, &out.writer); + } try diags.render(&sm, cli.stderr); try cli.stderr.flush(); if (diags.hasErrors()) return error.Fatal; diff --git a/src/root.zig b/src/root.zig index 2cf20f6..dbb5d1e 100644 --- a/src/root.zig +++ b/src/root.zig @@ -8,6 +8,9 @@ pub const front = struct { pub const scan = @import("front/scan.zig"); pub const preproc = @import("front/preproc.zig"); pub const pp_print = @import("front/pp_print.zig"); + pub const ast = @import("front/ast.zig"); + pub const parser = @import("front/parser.zig"); + pub const ast_dump = @import("front/ast_dump.zig"); }; pub const dxil = struct { diff --git a/test/oracle/dxc_valid_one.sh b/test/oracle/dxc_valid_one.sh new file mode 100644 index 0000000..d7c34a4 --- /dev/null +++ b/test/oracle/dxc_valid_one.sh @@ -0,0 +1,6 @@ +#!/bin/sh +# Print the file name if dxc compiles it with its RUN-line flags (-HV 2021). +flags=$(sh "$(dirname "$0")/run_flags.sh" "$1") || exit 0 +# shellcheck disable=SC2086 +"$DXC" -HV 2021 $flags "$1" -Fo /dev/null >/dev/null 2>&1 && echo "$1" +exit 0 diff --git a/test/oracle/parse.sh b/test/oracle/parse.sh new file mode 100644 index 0000000..db1a082 --- /dev/null +++ b/test/oracle/parse.sh @@ -0,0 +1,50 @@ +#!/bin/sh +# Parser oracle: every corpus file that dxc compiles successfully (with the +# target and flags from its RUN line, under -HV 2021) must parse without +# syntax errors in zdxc. Prints the pass rate and the most common errors. +# +# Usage: test/oracle/parse.sh +# The list of dxc-valid files is cached in zig-out/oracle/parse-valid.txt; +# delete it to rebuild (takes a few minutes). +set -u + +ZDXC=$(cd "$(dirname "$1")" && pwd)/$(basename "$1") +DXC_ROOT=${DXC_ROOT:-..} +DXC=$(cd "${DXC_BUILD:-$DXC_ROOT/build}" && pwd)/bin/dxc +CACHE=zig-out/oracle/parse-valid.txt +JOBS=${JOBS:-8} +mkdir -p zig-out/oracle + +if [ ! -s "$CACHE" ]; then + echo "building list of dxc-valid corpus files (cached in $CACHE)..." + tmp=$(mktemp) + find "$DXC_ROOT/tools/clang/test/CodeGenHLSL" "$DXC_ROOT/tools/clang/test/CodeGenDXIL" "$DXC_ROOT/tools/clang/test/CodeGenSPIRV" -name '*.hlsl' | sort > "$tmp" + export DXC + xargs -P "$JOBS" -n 1 sh test/oracle/dxc_valid_one.sh < "$tmp" | sort > "$CACHE" + rm -f "$tmp" +fi + +TMP=$(mktemp -d) +trap 'rm -rf "$TMP"' EXIT +pass=0 +fail=0 +while read -r f; do + flags=$(sh test/oracle/run_flags.sh "$f") || continue + # shellcheck disable=SC2086 + if "$ZDXC" -zdxc-parse $flags "$f" > "$TMP/out" 2>&1; then + pass=$((pass + 1)) + else + fail=$((fail + 1)) + echo "$f" >> "$TMP/failed" + grep -m1 -E 'error' "$TMP/out" | sed -E 's/^.*(error|fatal): //' >> "$TMP/errors" + fi +done < "$CACHE" + +if [ "$fail" -gt 0 ]; then + echo "most common first errors:" + sed -E "s/'[^']*'/'X'/g" "$TMP/errors" | sort | uniq -c | sort -rn | head -${SHOW:-15} + echo "first failing files:" + head -${SHOW_FILES:-10} "$TMP/failed" +fi +echo "oracle parse: $pass passed, $fail failed (of $(wc -l < "$CACHE" | tr -d ' ') dxc-valid files)" +[ "$fail" -eq 0 ] diff --git a/test/oracle/parse_errors.sh b/test/oracle/parse_errors.sh new file mode 100644 index 0000000..c9b2027 --- /dev/null +++ b/test/oracle/parse_errors.sh @@ -0,0 +1,9 @@ +#!/bin/sh +# Print the first parse error of every dxc-valid corpus file that zdxc +# fails to parse (run test/oracle/parse.sh first to build the file list). +# Usage: test/oracle/parse_errors.sh +while read -r f; do + flags=$(sh test/oracle/run_flags.sh "$f") || continue + # shellcheck disable=SC2086 + "$1" -zdxc-parse $flags "$f" 2>&1 | grep -m1 -E "error|fatal" | sed -E "s#^[^ ]*: (error|fatal): #$f: #" +done < zig-out/oracle/parse-valid.txt diff --git a/test/oracle/run_flags.sh b/test/oracle/run_flags.sh new file mode 100644 index 0000000..aec3338 --- /dev/null +++ b/test/oracle/run_flags.sh @@ -0,0 +1,12 @@ +#!/bin/sh +# Print the dxc/zdxc flags taken from the first RUN line of an HLSL test: +# -T, -E, -D, -spirv and -enable-16bit-types. Exits 1 if there is no -T. +run=$(grep -m1 'RUN:' "$1") +target=$(printf '%s' "$run" | grep -oE -- '-T *[a-z]+_6_[0-9]+' | head -1 | sed 's/-T *//') +[ -n "$target" ] || exit 1 +entry=$(printf '%s' "$run" | grep -oE -- '-E *[A-Za-z_][A-Za-z0-9_]*' | head -1 | sed 's/-E */-E /') +defs=$(printf '%s' "$run" | grep -oE -- '-D *[A-Za-z_][A-Za-z0-9_]*(=[^ ]*)?' | sed 's/-D */-D/' | tr '\n' ' ') +extra="" +printf '%s' "$run" | grep -q -- '-spirv' && extra="$extra -spirv" +printf '%s' "$run" | grep -q -- '-enable-16bit-types' && extra="$extra -enable-16bit-types" +echo "-T $target $entry $extra $defs"