/ICSharpCode.Decompiler/Ast/Transforms/TransformationPipeline.cs

http://github.com/icsharpcode/ILSpy · C# · 65 lines · 43 code · 5 blank · 17 comment · 5 complexity · 0f5eed720697c339182be2e32980512c MD5 · raw file

  1. // Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
  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;
  19. using System.Threading;
  20. using ICSharpCode.NRefactory.CSharp;
  21. namespace ICSharpCode.Decompiler.Ast.Transforms
  22. {
  23. public interface IAstTransform
  24. {
  25. void Run(AstNode compilationUnit);
  26. }
  27. public static class TransformationPipeline
  28. {
  29. public static IAstTransform[] CreatePipeline(DecompilerContext context)
  30. {
  31. return new IAstTransform[] {
  32. new PushNegation(),
  33. new DelegateConstruction(context),
  34. new PatternStatementTransform(context),
  35. new ReplaceMethodCallsWithOperators(context),
  36. new IntroduceUnsafeModifier(),
  37. new AddCheckedBlocks(),
  38. new DeclareVariables(context), // should run after most transforms that modify statements
  39. new ConvertConstructorCallIntoInitializer(), // must run after DeclareVariables
  40. new DecimalConstantTransform(),
  41. new IntroduceUsingDeclarations(context),
  42. new IntroduceExtensionMethods(context), // must run after IntroduceUsingDeclarations
  43. new IntroduceQueryExpressions(context), // must run after IntroduceExtensionMethods
  44. new CombineQueryExpressions(context),
  45. new FlattenSwitchBlocks(),
  46. };
  47. }
  48. public static void RunTransformationsUntil(AstNode node, Predicate<IAstTransform> abortCondition, DecompilerContext context)
  49. {
  50. if (node == null)
  51. return;
  52. foreach (var transform in CreatePipeline(context)) {
  53. context.CancellationToken.ThrowIfCancellationRequested();
  54. if (abortCondition != null && abortCondition(transform))
  55. return;
  56. transform.Run(node);
  57. }
  58. }
  59. }
  60. }