mirror of https://github.com/icsharpcode/ILSpy.git
Browse Source
This way we avoid having to extract later, as we will never inline if the `isinst` argument if this could result in it being unrepresentable in C#. This commit also refactors inlining restrictions to avoid requiring special cases in ILInlining itself. But when making this change, I discovered that this broke our pattern-matching tests, and that the weird IL with double `isinst` is indeed generated by the C# compiler for `if (genericParam is StringComparison.Ordinal)` style code. So instead we also allow `isinst` with a `box(expr-without-side-effects)` argument to be represented with the `expr is T ? (T)expr : null` emulation.null-coalescing-assignment
10 changed files with 154 additions and 142 deletions
@ -0,0 +1,62 @@
@@ -0,0 +1,62 @@
|
||||
#nullable enable |
||||
// Copyright (c) 2025 Daniel Grunwald
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||
// software and associated documentation files (the "Software"), to deal in the Software
|
||||
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all copies or
|
||||
// substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
using System.Diagnostics; |
||||
|
||||
using ICSharpCode.Decompiler.CSharp; |
||||
|
||||
namespace ICSharpCode.Decompiler.IL; |
||||
|
||||
partial class IsInst |
||||
{ |
||||
internal override bool SatisfiesSlotRestrictionForInlining(int childIndex, ILInstruction newChild) |
||||
{ |
||||
Debug.Assert(childIndex == 0); |
||||
Debug.Assert(base.SatisfiesSlotRestrictionForInlining(childIndex, newChild)); |
||||
if (this.Type.IsReferenceType == true) |
||||
{ |
||||
return true; // reference-type isinst is always supported
|
||||
} |
||||
if (SemanticHelper.IsPure(newChild.Flags)) |
||||
{ |
||||
return true; // emulated via "expr is T ? (T)expr : null"
|
||||
} |
||||
else if (newChild is Box box && SemanticHelper.IsPure(box.Argument.Flags)) |
||||
{ |
||||
// Also emulated via "expr is T ? (T)expr : null".
|
||||
// This duplicates the boxing side-effect, but that's harmless as one of the boxes is only
|
||||
// used in the `expr is T` type test where the object identity can never be observed.
|
||||
// This appears as part of C# pattern matching, inlining early makes those code patterns easier to detect.
|
||||
return true; |
||||
} |
||||
if (this.Parent is UnboxAny unboxAny && ExpressionBuilder.IsUnboxAnyWithIsInst(unboxAny, this.Type)) |
||||
{ |
||||
return true; // supported pattern "expr as T?"
|
||||
} |
||||
if (this.Parent != null && (this.Parent.MatchCompEqualsNull(out _) || this.Parent.MatchCompNotEqualsNull(out _))) |
||||
{ |
||||
return true; // supported pattern "expr is T"
|
||||
} |
||||
if (this.Parent is Block { Kind: BlockKind.ControlFlow }) |
||||
{ |
||||
return true; // supported via StatementBuilder.VisitIsInst
|
||||
} |
||||
return false; |
||||
} |
||||
} |
||||
@ -1,69 +0,0 @@
@@ -1,69 +0,0 @@
|
||||
// Copyright (c) 2020 Daniel Grunwald
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||
// software and associated documentation files (the "Software"), to deal in the Software
|
||||
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all copies or
|
||||
// substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
|
||||
using ICSharpCode.Decompiler.CSharp; |
||||
|
||||
namespace ICSharpCode.Decompiler.IL.Transforms |
||||
{ |
||||
/// <summary>
|
||||
/// C# cannot represent `isinst T` directly for value-types.
|
||||
/// This transform un-inlines the argument of `isinst` instructions that can't be directly translated to C#,
|
||||
/// thus allowing the emulation via "expr is T ? (T)expr : null".
|
||||
/// </summary>
|
||||
public class FixLoneIsInst : IILTransform |
||||
{ |
||||
void IILTransform.Run(ILFunction function, ILTransformContext context) |
||||
{ |
||||
var instructionsToFix = new List<IsInst>(); |
||||
foreach (var isInst in function.Descendants.OfType<IsInst>()) |
||||
{ |
||||
if (isInst.Type.IsReferenceType == true) |
||||
{ |
||||
continue; // reference-type isinst is always supported
|
||||
} |
||||
if (SemanticHelper.IsPure(isInst.Argument.Flags)) |
||||
{ |
||||
continue; // emulated via "expr is T ? (T)expr : null"
|
||||
} |
||||
if (isInst.Parent is UnboxAny unboxAny && ExpressionBuilder.IsUnboxAnyWithIsInst(unboxAny, isInst)) |
||||
{ |
||||
continue; // supported pattern "expr as T?"
|
||||
} |
||||
if (isInst.Parent.MatchCompEqualsNull(out _) || isInst.Parent.MatchCompNotEqualsNull(out _)) |
||||
{ |
||||
continue; // supported pattern "expr is T"
|
||||
} |
||||
if (isInst.Parent is Block { Kind: BlockKind.ControlFlow }) |
||||
{ |
||||
continue; // supported via StatementBuilder.VisitIsInst
|
||||
} |
||||
instructionsToFix.Add(isInst); |
||||
} |
||||
// Need to delay fixing until we're done with iteration, because Extract() modifies parents
|
||||
foreach (var isInst in instructionsToFix) |
||||
{ |
||||
// Use extraction to turn isInst.Argument into a pure instruction, thus making the emulation possible
|
||||
context.Step("FixLoneIsInst", isInst); |
||||
isInst.Argument.Extract(context); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue