/NRefactory/ICSharpCode.NRefactory.CSharp/Resolver/DetectSkippableNodesNavigator.cs

http://github.com/icsharpcode/ILSpy · C# · 88 lines · 48 code · 10 blank · 30 comment · 8 complexity · dd57f493757682f88435554339d1e18d MD5 · raw file

  1. // Copyright (c) 2010-2013 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.Collections.Generic;
  20. using ICSharpCode.NRefactory.Semantics;
  21. using ICSharpCode.NRefactory.TypeSystem;
  22. namespace ICSharpCode.NRefactory.CSharp.Resolver
  23. {
  24. /// <summary>
  25. /// When an <see cref="IResolveVisitorNavigator"/> is searching for specific nodes
  26. /// (e.g. all IdentifierExpressions), it has to scan the whole syntax tree for those nodes.
  27. /// However, scanning in the ResolveVisitor is expensive (e.g. any lambda that is scanned must be resolved),
  28. /// so it makes sense to detect when a whole subtree is scan-only, and skip that tree instead.
  29. ///
  30. /// The DetectSkippableNodesNavigator performs this job by running the input IResolveVisitorNavigator
  31. /// over the whole AST, and detecting subtrees that are scan-only, and replaces them with Skip.
  32. /// </summary>
  33. public sealed class DetectSkippableNodesNavigator : IResolveVisitorNavigator
  34. {
  35. readonly Dictionary<AstNode, ResolveVisitorNavigationMode> dict = new Dictionary<AstNode, ResolveVisitorNavigationMode>();
  36. IResolveVisitorNavigator navigator;
  37. public DetectSkippableNodesNavigator(IResolveVisitorNavigator navigator, AstNode root)
  38. {
  39. this.navigator = navigator;
  40. Init(root);
  41. }
  42. bool Init(AstNode node)
  43. {
  44. var mode = navigator.Scan(node);
  45. if (mode == ResolveVisitorNavigationMode.Skip)
  46. return false;
  47. bool needsResolve = (mode != ResolveVisitorNavigationMode.Scan);
  48. for (AstNode child = node.FirstChild; child != null; child = child.NextSibling) {
  49. needsResolve |= Init(child);
  50. }
  51. if (needsResolve) {
  52. // If this node or any child node needs resolving, store the mode in the dictionary.
  53. dict.Add(node, mode);
  54. }
  55. return needsResolve;
  56. }
  57. /// <inheritdoc/>
  58. public ResolveVisitorNavigationMode Scan(AstNode node)
  59. {
  60. ResolveVisitorNavigationMode mode;
  61. if (dict.TryGetValue(node, out mode)) {
  62. return mode;
  63. } else {
  64. return ResolveVisitorNavigationMode.Skip;
  65. }
  66. }
  67. /// <inheritdoc/>
  68. public void Resolved(AstNode node, ResolveResult result)
  69. {
  70. navigator.Resolved(node, result);
  71. }
  72. /// <inheritdoc/>
  73. public void ProcessConversion(Expression expression, ResolveResult result, Conversion conversion, IType targetType)
  74. {
  75. navigator.ProcessConversion(expression, result, conversion, targetType);
  76. }
  77. }
  78. }