// Copyright (c) 2019 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.Diagnostics; using System.Linq; using ICSharpCode.Decompiler.TypeSystem; namespace ICSharpCode.Decompiler.IL.Transforms { public class FixRemainingIncrements : IILTransform { void IILTransform.Run(ILFunction function, ILTransformContext context) { var callsToFix = new List(); foreach (var call in function.Descendants.OfType()) { if (!UserDefinedCompoundAssign.IsIncrementOrDecrement(call.Method, context.Settings)) continue; if (call.Arguments.Count != 1) continue; if (call.Method.DeclaringType.IsKnownType(KnownTypeCode.Decimal)) { // For decimal, legacy csc can optimize "d + 1m" to "op_Increment(d)". // We can handle these calls in ReplaceMethodCallsWithOperators. continue; } callsToFix.Add(call); } foreach (var call in callsToFix) { // A user-defined increment/decrement that was not handled by TransformAssignment. // This can happen because the variable-being-incremented was optimized out by Roslyn, // e.g. // public void Issue1552Pre(UserType a, UserType b) // { // UserType num = a + b; // Console.WriteLine(++num); // } // can end up being compiled to: // Console.WriteLine(UserType.op_Increment(a + b)); if (call.SlotInfo == StLoc.ValueSlot && call.Parent.SlotInfo == Block.InstructionSlot) { var store = (StLoc)call.Parent; var block = (Block)store.Parent; context.Step($"Fix {call.Method.Name} call at 0x{call.StartILOffset:x4} using {store.Variable.Name}", call); // stloc V(call op_Increment(...)) // -> // stloc V(...) // compound.assign op_Increment(V) call.ReplaceWith(call.Arguments[0]); var compoundAssign = new UserDefinedCompoundAssign(call.Method, CompoundEvalMode.EvaluatesToNewValue, new LdLoca(store.Variable), CompoundTargetKind.Address, new LdcI4(1)).WithILRange(call); block.Instructions.Insert(store.ChildIndex + 1, compoundAssign); context.EndStep(compoundAssign); } else { context.Step($"Fix {call.Method.Name} call at 0x{call.StartILOffset:x4} using new local", call); var newVariable = call.Arguments[0].Extract(context); if (newVariable == null) { Debug.Fail("Failed to extract argument of remaining increment/decrement"); continue; } newVariable.Type = call.GetParameter(0).Type; Debug.Assert(call.Arguments[0].MatchLdLoc(newVariable)); var compoundAssign = new UserDefinedCompoundAssign(call.Method, CompoundEvalMode.EvaluatesToNewValue, new LdLoca(newVariable), CompoundTargetKind.Address, new LdcI4(1)).WithILRange(call); if (context.Settings.UserDefinedCompoundAssignmentOperators && context.CSharpResolver.IsShadowedByInstanceOperator(call.Method, newVariable.Type, null, targetIsVariable: true) && newVariable.StoreInstructions.SingleOrDefault() is StLoc { Parent: Block hoistBlock } hoistedStore) { // A prefix increment here would bind the C# 14 instance operator of the // same name; only a postfix increment with a used result binds the static // one, and that hands back the old value. So the increment becomes a // statement of its own, right after the extracted argument. This can move // the operator call ahead of side effects that originally ran before it; // the alternative is output that silently calls the other operator. hoistBlock.Instructions.Insert(hoistedStore.ChildIndex + 1, compoundAssign); call.ReplaceWith(new LdLoc(newVariable)); } else { call.ReplaceWith(compoundAssign); } context.EndStep(compoundAssign); } } } } }