Пример #1
0
        public void Run(ILFunction function, ILTransformContext context)
        {
            if (!context.Settings.SwitchStatementOnString)
            {
                return;
            }

            HashSet <BlockContainer> changedContainers = new HashSet <BlockContainer>();

            foreach (var block in function.Descendants.OfType <Block>())
            {
                bool changed = false;
                for (int i = block.Instructions.Count - 1; i >= 0; i--)
                {
                    if (SimplifyCascadingIfStatements(block.Instructions, ref i))
                    {
                        changed = true;
                        continue;
                    }
                    if (MatchLegacySwitchOnStringWithHashtable(block.Instructions, ref i))
                    {
                        changed = true;
                        continue;
                    }
                    if (MatchLegacySwitchOnStringWithDict(block.Instructions, ref i))
                    {
                        changed = true;
                        continue;
                    }
                    if (MatchRoslynSwitchOnString(block.Instructions, ref i))
                    {
                        changed = true;
                        continue;
                    }
                }
                if (!changed)
                {
                    continue;
                }
                SwitchDetection.SimplifySwitchInstruction(block);
                if (block.Parent is BlockContainer container)
                {
                    changedContainers.Add(container);
                }
            }

            foreach (var container in changedContainers)
            {
                container.SortBlocks(deleteUnreachableBlocks: true);
            }
        }
        public void Run(ILFunction function, ILTransformContext context)
        {
            if (!context.Settings.LiftNullables)
            {
                return;
            }

            HashSet <BlockContainer> changedContainers = new HashSet <BlockContainer>();

            foreach (var block in function.Descendants.OfType <Block>())
            {
                bool changed = false;
                for (int i = block.Instructions.Count - 1; i >= 0; i--)
                {
                    SwitchInstruction newSwitch;
                    if (MatchSwitchOnNullable(block.Instructions, i, out newSwitch))
                    {
                        newSwitch.ILRange = block.Instructions[i - 2].ILRange;
                        block.Instructions[i + 1].ReplaceWith(newSwitch);
                        block.Instructions.RemoveRange(i - 2, 3);
                        i      -= 2;
                        changed = true;
                        continue;
                    }
                    if (MatchRoslynSwitchOnNullable(block.Instructions, i, out newSwitch))
                    {
                        newSwitch.ILRange = block.Instructions[i - 1].ILRange;
                        block.Instructions[i - 1].ReplaceWith(newSwitch);
                        block.Instructions.RemoveRange(i, 2);
                        i--;
                        changed = true;
                        continue;
                    }
                }
                if (!changed)
                {
                    continue;
                }
                SwitchDetection.SimplifySwitchInstruction(block);
                if (block.Parent is BlockContainer container)
                {
                    changedContainers.Add(container);
                }
            }

            foreach (var container in changedContainers)
            {
                container.SortBlocks(deleteUnreachableBlocks: true);
            }
        }