1
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
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:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
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.
20
using System.Collections.Generic;
21
using ICSharpCode.NRefactory.Semantics;
22
using ICSharpCode.NRefactory.TypeSystem;
24
namespace ICSharpCode.NRefactory.CSharp.Resolver
27
/// When an <see cref="IResolveVisitorNavigator"/> is searching for specific nodes
28
/// (e.g. all IdentifierExpressions), it has to scan the whole syntax tree for those nodes.
29
/// However, scanning in the ResolveVisitor is expensive (e.g. any lambda that is scanned must be resolved),
30
/// so it makes sense to detect when a whole subtree is scan-only, and skip that tree instead.
32
/// The DetectSkippableNodesNavigator performs this job by running the input IResolveVisitorNavigator
33
/// over the whole AST, and detecting subtrees that are scan-only, and replaces them with Skip.
35
public sealed class DetectSkippableNodesNavigator : IResolveVisitorNavigator
37
readonly Dictionary<AstNode, ResolveVisitorNavigationMode> dict = new Dictionary<AstNode, ResolveVisitorNavigationMode>();
38
IResolveVisitorNavigator navigator;
40
public DetectSkippableNodesNavigator(IResolveVisitorNavigator navigator, AstNode root)
42
this.navigator = navigator;
46
bool Init(AstNode node)
48
var mode = navigator.Scan(node);
49
if (mode == ResolveVisitorNavigationMode.Skip)
52
bool needsResolve = (mode != ResolveVisitorNavigationMode.Scan);
54
for (AstNode child = node.FirstChild; child != null; child = child.NextSibling) {
55
needsResolve |= Init(child);
59
// If this node or any child node needs resolving, store the mode in the dictionary.
66
public ResolveVisitorNavigationMode Scan(AstNode node)
68
ResolveVisitorNavigationMode mode;
69
if (dict.TryGetValue(node, out mode)) {
72
return ResolveVisitorNavigationMode.Skip;
77
public void Resolved(AstNode node, ResolveResult result)
79
navigator.Resolved(node, result);
83
public void ProcessConversion(Expression expression, ResolveResult result, Conversion conversion, IType targetType)
85
navigator.ProcessConversion(expression, result, conversion, targetType);