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.

111 lines
3.9 KiB

  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.Diagnostics;
  20. using ICSharpCode.NRefactory.CSharp;
  21. using Mono.Cecil;
  22. namespace ICSharpCode.Decompiler.Ast.Transforms
  23. {
  24. /// <summary>
  25. /// Base class for AST visitors that need the current type/method context info.
  26. /// </summary>
  27. public abstract class ContextTrackingVisitor<TResult> : DepthFirstAstVisitor<object, TResult>, IAstTransform
  28. {
  29. protected readonly DecompilerContext context;
  30. protected ContextTrackingVisitor(DecompilerContext context)
  31. {
  32. if (context == null)
  33. throw new ArgumentNullException("context");
  34. this.context = context;
  35. }
  36. public override TResult VisitTypeDeclaration(TypeDeclaration typeDeclaration, object data)
  37. {
  38. TypeDefinition oldType = context.CurrentType;
  39. try {
  40. context.CurrentType = typeDeclaration.Annotation<TypeDefinition>();
  41. return base.VisitTypeDeclaration(typeDeclaration, data);
  42. } finally {
  43. context.CurrentType = oldType;
  44. }
  45. }
  46. public override TResult VisitMethodDeclaration(MethodDeclaration methodDeclaration, object data)
  47. {
  48. Debug.Assert(context.CurrentMethod == null);
  49. try {
  50. context.CurrentMethod = methodDeclaration.Annotation<MethodDefinition>();
  51. return base.VisitMethodDeclaration(methodDeclaration, data);
  52. } finally {
  53. context.CurrentMethod = null;
  54. }
  55. }
  56. public override TResult VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration, object data)
  57. {
  58. Debug.Assert(context.CurrentMethod == null);
  59. try {
  60. context.CurrentMethod = constructorDeclaration.Annotation<MethodDefinition>();
  61. return base.VisitConstructorDeclaration(constructorDeclaration, data);
  62. } finally {
  63. context.CurrentMethod = null;
  64. }
  65. }
  66. public override TResult VisitDestructorDeclaration(DestructorDeclaration destructorDeclaration, object data)
  67. {
  68. Debug.Assert(context.CurrentMethod == null);
  69. try {
  70. context.CurrentMethod = destructorDeclaration.Annotation<MethodDefinition>();
  71. return base.VisitDestructorDeclaration(destructorDeclaration, data);
  72. } finally {
  73. context.CurrentMethod = null;
  74. }
  75. }
  76. public override TResult VisitOperatorDeclaration(OperatorDeclaration operatorDeclaration, object data)
  77. {
  78. Debug.Assert(context.CurrentMethod == null);
  79. try {
  80. context.CurrentMethod = operatorDeclaration.Annotation<MethodDefinition>();
  81. return base.VisitOperatorDeclaration(operatorDeclaration, data);
  82. } finally {
  83. context.CurrentMethod = null;
  84. }
  85. }
  86. public override TResult VisitAccessor(Accessor accessor, object data)
  87. {
  88. Debug.Assert(context.CurrentMethod == null);
  89. try {
  90. context.CurrentMethod = accessor.Annotation<MethodDefinition>();
  91. return base.VisitAccessor(accessor, data);
  92. } finally {
  93. context.CurrentMethod = null;
  94. }
  95. }
  96. void IAstTransform.Run(AstNode node)
  97. {
  98. node.AcceptVisitor(this, null);
  99. }
  100. }
  101. }