|
| 1 | +// Copyright (c) 2025 Siegfried Pammer |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 4 | +// software and associated documentation files (the "Software"), to deal in the Software |
| 5 | +// without restriction, including without limitation the rights to use, copy, modify, merge, |
| 6 | +// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
| 7 | +// to whom the Software is furnished to do so, subject to the following conditions: |
| 8 | +// |
| 9 | +// The above copyright notice and this permission notice shall be included in all copies or |
| 10 | +// substantial portions of the Software. |
| 11 | +// |
| 12 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 13 | +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 14 | +// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| 15 | +// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 16 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 17 | +// DEALINGS IN THE SOFTWARE. |
| 18 | + |
| 19 | +#nullable enable |
| 20 | + |
| 21 | +using System; |
| 22 | +using System.Diagnostics.CodeAnalysis; |
| 23 | +using System.Linq; |
| 24 | + |
| 25 | +using ICSharpCode.Decompiler.TypeSystem; |
| 26 | +using ICSharpCode.Decompiler.Util; |
| 27 | + |
| 28 | +namespace ICSharpCode.Decompiler.IL.Transforms |
| 29 | +{ |
| 30 | + // call ThrowInvalidOperationException(...) |
| 31 | + // leave IL_0000 (ldloc temp) |
| 32 | + // |
| 33 | + // -or- |
| 34 | + // |
| 35 | + // call ThrowSwitchExpressionException(...) |
| 36 | + // leave IL_0000 (ldloc temp) |
| 37 | + // |
| 38 | + // -to- |
| 39 | + // |
| 40 | + // throw(newobj SwitchExpressionException(...)) |
| 41 | + class SwitchExpressionDefaultCaseTransform : IILTransform |
| 42 | + { |
| 43 | + void IILTransform.Run(ILFunction function, ILTransformContext context) |
| 44 | + { |
| 45 | + IMethod? FindConstructor(string fullTypeName, params Type[] argumentTypes) |
| 46 | + { |
| 47 | + IType exceptionType = context.TypeSystem.FindType(new FullTypeName(fullTypeName)); |
| 48 | + var types = argumentTypes.SelectArray(context.TypeSystem.FindType); |
| 49 | + |
| 50 | + foreach (var ctor in exceptionType.GetConstructors(m => !m.IsStatic && m.Parameters.Count == argumentTypes.Length)) |
| 51 | + { |
| 52 | + bool found = true; |
| 53 | + foreach (var pair in ctor.Parameters.Select(p => p.Type).Zip(types)) |
| 54 | + { |
| 55 | + if (!NormalizeTypeVisitor.IgnoreNullability.EquivalentTypes(pair.Item1, pair.Item2)) |
| 56 | + { |
| 57 | + found = false; |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | + if (found) |
| 62 | + return ctor; |
| 63 | + } |
| 64 | + |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + IMethod[] exceptionCtorTable = new IMethod[2]; |
| 69 | + |
| 70 | + exceptionCtorTable[0] = FindConstructor("System.InvalidOperationException")!; |
| 71 | + exceptionCtorTable[1] = FindConstructor("System.Runtime.CompilerServices.SwitchExpressionException", typeof(object))!; |
| 72 | + |
| 73 | + if (exceptionCtorTable[0] == null && exceptionCtorTable[1] == null) |
| 74 | + return; |
| 75 | + |
| 76 | + bool MatchThrowHelperCall(ILInstruction inst, [NotNullWhen(true)] out IMethod? exceptionCtor, out ILInstruction? value) |
| 77 | + { |
| 78 | + exceptionCtor = null; |
| 79 | + value = null; |
| 80 | + if (inst is not Call call) |
| 81 | + return false; |
| 82 | + if (call.Method.DeclaringType.FullName != "<PrivateImplementationDetails>") |
| 83 | + return false; |
| 84 | + switch (call.Arguments.Count) |
| 85 | + { |
| 86 | + case 0: |
| 87 | + if (call.Method.Name != "ThrowInvalidOperationException") |
| 88 | + return false; |
| 89 | + exceptionCtor = exceptionCtorTable[0]; |
| 90 | + break; |
| 91 | + case 1: |
| 92 | + if (call.Method.Name != "ThrowSwitchExpressionException") |
| 93 | + return false; |
| 94 | + exceptionCtor = exceptionCtorTable[1]; |
| 95 | + value = call.Arguments[0]; |
| 96 | + break; |
| 97 | + default: |
| 98 | + return false; |
| 99 | + } |
| 100 | + return exceptionCtor != null; |
| 101 | + } |
| 102 | + |
| 103 | + foreach (var block in function.Descendants.OfType<Block>()) |
| 104 | + { |
| 105 | + if (block.Parent is not BlockContainer container) |
| 106 | + continue; |
| 107 | + if (container.EntryPoint is not { IncomingEdgeCount: 1, Instructions: [SwitchInstruction inst] }) |
| 108 | + continue; |
| 109 | + if (block.Instructions.Count != 2 || block.IncomingEdgeCount != 1 || !block.FinalInstruction.MatchNop()) |
| 110 | + continue; |
| 111 | + var defaultSection = inst.GetDefaultSection(); |
| 112 | + if (defaultSection.Body != block && (defaultSection.Body is not Branch b || b.TargetBlock != block)) |
| 113 | + continue; |
| 114 | + if (!MatchThrowHelperCall(block.Instructions[0], out IMethod? exceptionCtor, out ILInstruction? value)) |
| 115 | + continue; |
| 116 | + if (block.Instructions[1] is not Leave { Value: LdLoc { Variable: { Kind: VariableKind.Local or VariableKind.StackSlot, LoadCount: 1, InitialValueIsInitialized: true } } }) |
| 117 | + { |
| 118 | + continue; |
| 119 | + } |
| 120 | + context.Step("SwitchExpressionDefaultCaseTransform", block.Instructions[0]); |
| 121 | + var newObj = new NewObj(exceptionCtor); |
| 122 | + if (value != null) |
| 123 | + newObj.Arguments.Add(value); |
| 124 | + block.Instructions[0] = new Throw(newObj).WithILRange(block.Instructions[0]).WithILRange(block.Instructions[1]); |
| 125 | + block.Instructions.RemoveAt(1); |
| 126 | + defaultSection.IsCompilerGeneratedDefaultSection = true; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments