You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.8 KiB

  1. // Copyright (c) 2020 Daniel Grunwald
  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. using System.Collections.Generic;
  19. using System.Linq;
  20. using ICSharpCode.Decompiler.CSharp;
  21. namespace ICSharpCode.Decompiler.IL.Transforms
  22. {
  23. /// <summary>
  24. /// C# cannot represent `isinst T` directly for value-types.
  25. /// This transform un-inlines the argument of `isinst` instructions that can't be directly translated to C#,
  26. /// thus allowing the emulation via "expr is T ? (T)expr : null".
  27. /// </summary>
  28. public class FixLoneIsInst : IILTransform
  29. {
  30. void IILTransform.Run(ILFunction function, ILTransformContext context)
  31. {
  32. var instructionsToFix = new List<IsInst>();
  33. foreach (var isInst in function.Descendants.OfType<IsInst>())
  34. {
  35. if (isInst.Type.IsReferenceType == true)
  36. {
  37. continue; // reference-type isinst is always supported
  38. }
  39. if (SemanticHelper.IsPure(isInst.Argument.Flags))
  40. {
  41. continue; // emulated via "expr is T ? (T)expr : null"
  42. }
  43. if (isInst.Parent is UnboxAny unboxAny && ExpressionBuilder.IsUnboxAnyWithIsInst(unboxAny, isInst))
  44. {
  45. continue; // supported pattern "expr as T?"
  46. }
  47. if (isInst.Parent.MatchCompEqualsNull(out _) || isInst.Parent.MatchCompNotEqualsNull(out _))
  48. {
  49. continue; // supported pattern "expr is T"
  50. }
  51. if (isInst.Parent is Block { Kind: BlockKind.ControlFlow })
  52. {
  53. continue; // supported via StatementBuilder.VisitIsInst
  54. }
  55. instructionsToFix.Add(isInst);
  56. }
  57. // Need to delay fixing until we're done with iteration, because Extract() modifies parents
  58. foreach (var isInst in instructionsToFix)
  59. {
  60. // Use extraction to turn isInst.Argument into a pure instruction, thus making the emulation possible
  61. context.Step("FixLoneIsInst", isInst);
  62. isInst.Argument.Extract(context);
  63. }
  64. }
  65. }
  66. }