Repository: dotnet/runtime
File: src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_Wasm/WasmTypeNode.cs (C#)

Code before:

        public override ObjectNodeSection GetSection(NodeFactory factory) => ObjectNodeSection.WasmTypeSection;

        protected override string GetName(NodeFactory factory)
            => $"Wasm Type ({string.Join(",", _types.Skip(1))}) -> {_types.FirstOrDefault()}";

        public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
            => new ObjectData(
                Array.Empty<byte>(),
                Array.Empty<Relocation>(),
                1,
                Array.Empty<ISymbolDefinitionNode>());

        public override int CompareToImpl(ISortableNode other, CompilerComparer comparer)
        {
            var wtm = (WasmTypeNode)other;
            ReadOnlySpan<CorInfoWasmType> lhs = _types, rhs = wtm._types;
            // Put shorter signatures earlier in the sort order, on the assumption that they are more likely to be used.
            int result = lhs.Length.CompareTo(rhs.Length);
            if (result == 0)
                result = MemoryExtensions.SequenceCompareTo(lhs, rhs);
            return result;
        }

        public void AppendMangledName(NameMangler nameMangler, Internal.Text.Utf8StringBuilder sb) =>
            sb.Append(GetName(null!));

        public int Offset => 0;
    }
}


Review comment (bug):
Copilot is onto things here.

It's an anti-pattern to do things like this (the common pattern is to do the reverse). This also wouldn't return a valid mangled name (it has spaces it in).

That said, the mangled name of this node is not going to matter much, it would only be used for things like DGML debugging.

Code after:

        public override ObjectNodeSection GetSection(NodeFactory factory) => ObjectNodeSection.WasmTypeSection;

        protected override string GetName(NodeFactory factory)
            => $"Wasm Type ({string.Join(",", _types.Skip(1))}) -> {_types.FirstOrDefault()}";

        public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
            => new ObjectData(
                Array.Empty<byte>(),
                Array.Empty<Relocation>(),
                1,
                Array.Empty<ISymbolDefinitionNode>());

        public override int CompareToImpl(ISortableNode other, CompilerComparer comparer)
        {
            var wtm = (WasmTypeNode)other;
            ReadOnlySpan<CorInfoWasmType> lhs = _types, rhs = wtm._types;
            // Put shorter signatures earlier in the sort order, on the assumption that they are more likely to be used.
            int result = lhs.Length.CompareTo(rhs.Length);
            if (result == 0)
                result = MemoryExtensions.SequenceCompareTo(lhs, rhs);
            return result;
        }

        public void AppendMangledName(NameMangler nameMangler, Internal.Text.Utf8StringBuilder sb)
        {
            sb.Append(nameMangler.CompilationUnitPrefix);
            sb.Append("__wasmtype_"u8);

            foreach (var type in _types)
                sb.Append(type switch {
                    CorInfoWasmType.CORINFO_WASM_TYPE_VOID => 'v',
                    CorInfoWasmType.CORINFO_WASM_TYPE_V128 => 'V',
                    CorInfoWasmType.CORINFO_WASM_TYPE_F64  => 'd',
                    CorInfoWasmType.CORINFO_WASM_TYPE_F32  => 'f',
                    CorInfoWasmType.CORINFO_WASM_TYPE_I64  => 'j',
                    CorInfoWasmType.CORINFO_WASM_TYPE_I32  => 'i',
                    _ => throw new NotImplementedException($"Unknown CorInfoWasmType: {type}"),
                });
        }

        public int Offset => 0;
    }
}