zdxc: mangle template instances, classes and scoped names as dxc

Structs and enums remember their enclosing namespaces and structs, and
struct template instances their name and arguments, so mangled names
spell them as dxc does: U/V for struct/class, ?$Name@<args>@ for
instances (type arguments, $0<n> and $0?<n> integral ones, with names
of their own), each enclosing scope after the name, with back-references.
Exported functions whose signature holds a resource or matrix handle get
dxc's error instead of an invalid module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Peter Li 2026-11-01 00:39:38 -07:00
parent c4771fcdf3
commit a33005fcea
4 changed files with 95 additions and 7 deletions

View File

@ -714,6 +714,23 @@ const Emitter = struct {
var mparams: std.ArrayList(mangle.Param) = .empty;
var ann: std.ArrayList(ParamAnn) = .empty;
const ret_void = f.ret == TypeTable.void_type;
if (!internal) {
// (dxc's check; the validator counts matrix handles as
// resources too.)
const holds = struct {
fn check(em: *Emitter, ty: TypeId) bool {
return isResourceType(em.t, ty) or em.holdsResources(ty) or em.containsLinAlg(ty);
}
}.check;
var bad = holds(e, f.ret);
for (f.params) |p| bad = bad or holds(e, p.ty);
if (bad) {
const mps = try e.arena.alloc(mangle.Param, f.params.len);
for (mps, f.params) |*mp, p| mp.* = .{ .ty = p.ty, .by_ref = p.by_ref };
_ = e.diags.report(.{ .severity = .@"error", .loc = .none }, "Exported function \\01{s} must not contain a resource in parameter or return type.", .{(try mangle.function(e.arena, e.t, f.name, f.ret, mps))[1..]});
return error.Unsupported;
}
}
try ann.append(e.arena, .{ .qual = 1, .comp = if (ret_void) null else try e.compType(e.compOf(f.ret)), .semantic = f.ret_semantic });
for (f.params) |p| {
// Structs go by pointer.

View File

@ -114,7 +114,7 @@ fn typeCode(a: Allocator, t: *TypeTable, ty: TypeId, out: *std.ArrayList(u8), na
if (ty == TypeTable.void_type) return out.append(a, 'X');
switch (t.get(ty)) {
.scalar => |s| try out.appendSlice(a, scalarCode(s)),
.@"enum" => |en| try className(a, 'W', try std.fmt.allocPrint(a, "4{s}", .{en.name}), out, names),
.@"enum" => |en| try qualifiedName(a, t, "W4", en.name, en.scopes, out, names),
.vector => |v| {
var args: std.ArrayList(u8) = .empty;
try args.appendSlice(a, scalarCode(v.elem));
@ -141,7 +141,7 @@ fn typeCode(a: Allocator, t: *TypeTable, ty: TypeId, out: *std.ArrayList(u8), na
for (dims.items) |d| try number(a, d, out);
try typeCode(a, t, et, out, names);
},
.@"struct" => |st| try className(a, 'U', st.name, out, names),
.@"struct" => |st| try qualifiedName(a, t, if (st.is_class) "V" else "U", try structName(a, t, st), st.scopes, out, names),
.resource => |r| {
if (r.elem == TypeTable.void_type or r.kind == .raytracing_acceleration_structure) return className(a, 'U', r.name, out, names);
// Template arguments have names of their own.
@ -166,6 +166,47 @@ fn typeCode(a: Allocator, t: *TypeTable, ty: TypeId, out: *std.ArrayList(u8), na
}
}
/// A struct's own name: `name`, or `?$name@<args>@` for a template
/// instance (its arguments with names of their own).
fn structName(a: Allocator, t: *TypeTable, st: *const types.StructInfo) Allocator.Error![]const u8 {
const base = st.base_name orelse return st.name;
var out: std.ArrayList(u8) = .empty;
var inner: Names = .{};
try inner.add(a, base);
try out.print(a, "?${s}@", .{base});
for (st.template_args) |arg| switch (arg) {
.type => |ty| try typeCode(a, t, ty, &out, &inner),
.value => |v| {
try out.appendSlice(a, "$0");
if (v < 0) try out.append(a, '?');
try number(a, @abs(v), &out);
},
};
try out.append(a, '@');
return out.items;
}
/// `<kind><name><scope>@...@`: the name and each enclosing scope's,
/// innermost first, each a back-reference when already spelled.
fn qualifiedName(a: Allocator, t: *TypeTable, kind: []const u8, name: []const u8, scopes: []const types.ScopeName, out: *std.ArrayList(u8), names: *Names) Allocator.Error!void {
try out.appendSlice(a, kind);
try namePart(a, name, out, names);
for (scopes) |sc| try namePart(a, switch (sc) {
.name => |n| n,
.strct => |st| try structName(a, t, st),
}, out, names);
try out.append(a, '@');
}
/// One part of a qualified name: `name@` (template instances end in `@`
/// themselves), or a back-reference digit.
fn namePart(a: Allocator, name: []const u8, out: *std.ArrayList(u8), names: *Names) Allocator.Error!void {
if (names.find(name)) |k| return out.append(a, '0' + @as(u8, @intCast(k)));
try out.appendSlice(a, name);
if (name[0] != '?') try out.append(a, '@');
try names.add(a, name);
}
/// `<kind><name>@@`, or `<kind><backref>@` for a name already spelled
/// (template instances `?$name@args@` end in `@` themselves).
fn className(a: Allocator, kind: u8, name: []const u8, out: *std.ArrayList(u8), names: *Names) Allocator.Error!void {

View File

@ -112,7 +112,7 @@ pub const Function = struct {
};
/// A bound template argument.
pub const Bound = union(enum) { type: TypeId, value: i64 };
pub const Bound = types.TemplateArg;
pub const FuncSet = struct {
funcs: std.ArrayList(*Function) = .empty,
@ -139,6 +139,8 @@ pub const Scope = struct {
syms: std.StringHashMapUnmanaged(Symbol) = .empty,
kind: enum { global, namespace, function, block, @"struct" } = .block,
name: []const u8 = "",
/// The struct of a struct scope.
strct: ?*const types.StructInfo = null,
};
/// Per-struct data kept by Sema (methods, nested scope).
@ -567,7 +569,7 @@ pub const Sema = struct {
// Register the type before checking members, so the struct
// may name itself.
const info = try s.arena.create(types.StructInfo);
info.* = .{ .name = name.items };
info.* = .{ .name = name.items, .base_name = st.name, .template_args = b.args, .scopes = try s.scopeNames(b.scope), .is_class = st.keyword == .class };
const ty = try s.types.intern(.{ .@"struct" = info });
try b.scope.syms.put(s.arena, name.items, .{ .type = ty });
// The injected class name: `S` alone is this instance.
@ -1702,6 +1704,18 @@ pub const Sema = struct {
}
}
/// The namespaces and structs enclosing `scope`, innermost first.
fn scopeNames(s: *Sema, scope: *const Scope) Allocator.Error![]const types.ScopeName {
var out: std.ArrayList(types.ScopeName) = .empty;
var sc: ?*const Scope = scope;
while (sc) |x| : (sc = x.parent) switch (x.kind) {
.namespace => try out.append(s.arena, .{ .name = x.name }),
.@"struct" => if (x.strct) |st| try out.append(s.arena, .{ .strct = st }),
else => {},
};
return out.items;
}
fn enumDecl(s: *Sema, d: *const ast.Decl, e: ast.EnumDecl) Allocator.Error!void {
const info = try s.arena.create(types.EnumInfo);
var underlying: Scalar = .int;
@ -1709,7 +1723,7 @@ pub const Sema = struct {
const ut = try s.resolveType(u);
underlying = s.types.component(ut) orelse .int;
}
info.* = .{ .name = e.name orelse "<anonymous enum>", .underlying = underlying, .scoped = e.scoped };
info.* = .{ .name = e.name orelse "<anonymous enum>", .underlying = underlying, .scoped = e.scoped, .scopes = try s.scopeNames(s.scope) };
const ty = try s.types.intern(.{ .@"enum" = info });
if (e.name) |n| try s.declare(n, .{ .type = ty }, d.loc);
const sc = try s.arena.create(Scope);
@ -1751,13 +1765,13 @@ pub const Sema = struct {
return;
} else {
info = try s.arena.create(types.StructInfo);
info.* = .{ .name = name };
info.* = .{ .name = name, .scopes = try s.scopeNames(s.scope), .is_class = st.keyword == .class };
ty = try s.types.intern(.{ .@"struct" = info });
try s.declare(name, .{ .type = ty }, d.loc);
}
const members = st.members orelse return;
const sc = try s.arena.create(Scope);
sc.* = .{ .parent = s.scope, .kind = .@"struct", .name = name };
sc.* = .{ .parent = s.scope, .kind = .@"struct", .name = name, .strct = info };
const ss = try s.arena.create(StructSema);
ss.* = .{ .scope = sc, .decl = d };
info.sema = ss;

View File

@ -377,8 +377,22 @@ pub const Interp = packed struct(u8) {
};
/// A struct's layout-relevant information. Methods live in Sema.
/// A template argument: a type or an integral value.
pub const TemplateArg = union(enum) { type: TypeId, value: i64 };
/// A scope enclosing a declaration (for mangled names): a namespace or a
/// struct.
pub const ScopeName = union(enum) { name: []const u8, strct: *const StructInfo };
pub const StructInfo = struct {
name: []const u8,
/// For mangled names: the name without template arguments, the
/// arguments of a template instance, the enclosing scopes (innermost
/// first) and whether it was declared `class`.
base_name: ?[]const u8 = null,
template_args: []const TemplateArg = &.{},
scopes: []const ScopeName = &.{},
is_class: bool = false,
fields: std.ArrayList(Field) = .empty,
/// Direct base structs (HLSL 2021 single inheritance, in practice).
bases: std.ArrayList(*StructInfo) = .empty,
@ -438,6 +452,8 @@ pub const EnumInfo = struct {
name: []const u8,
underlying: Scalar,
scoped: bool,
/// Enclosing scopes, innermost first (mangled names).
scopes: []const ScopeName = &.{},
};
pub const Type = union(enum) {