~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/LocalReferenceFinder.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// LocalReferenceFinder.cs
 
3
//
 
4
// Author:
 
5
//       Simon Lindgren <simon.n.lindgren@gmail.com>
 
6
//
 
7
// Copyright (c) 2012 Simon Lindgren
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
//
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
//
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System.Collections.Generic;
 
28
using System.Linq;
 
29
using ICSharpCode.NRefactory.CSharp.Resolver;
 
30
using ICSharpCode.NRefactory.Semantics;
 
31
using ICSharpCode.NRefactory.TypeSystem;
 
32
using ICSharpCode.NRefactory.Utils;
 
33
using System.Diagnostics;
 
34
 
 
35
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
36
{
 
37
 
 
38
        /// <summary>
 
39
        /// Finds references to <see cref="IVariable">IVariables</see>.
 
40
        /// </summary>
 
41
        /// <remarks>
 
42
        /// This class is more efficient than <see cref="FindReferences"/>
 
43
        /// if there is already a resolved tree or if multiple searches needs
 
44
        /// to be performed.
 
45
        /// </remarks>          
 
46
        public class LocalReferenceFinder
 
47
        {
 
48
                LocalReferenceLocator locator;
 
49
 
 
50
                MultiDictionary<IVariable, ReferenceResult> references = new MultiDictionary<IVariable, ReferenceResult>();
 
51
                
 
52
                HashSet<AstNode> visitedRoots = new HashSet<AstNode>();
 
53
                
 
54
                public LocalReferenceFinder(CSharpAstResolver resolver)
 
55
                {
 
56
                        locator = new LocalReferenceLocator(resolver, this);
 
57
                }
 
58
 
 
59
                public LocalReferenceFinder(BaseRefactoringContext context) : this(context.Resolver)
 
60
                {
 
61
                }
 
62
 
 
63
                void VisitIfNeccessary(AstNode rootNode)
 
64
                {
 
65
                        // If any of the parent nodes are recorded as visited,
 
66
                        // we don't need to traverse rootNode this time.
 
67
                        var tmpRoot = rootNode;
 
68
                        while(tmpRoot != null){
 
69
                                if (visitedRoots.Contains(tmpRoot))
 
70
                                        return;
 
71
                                tmpRoot = tmpRoot.Parent;
 
72
                        }
 
73
 
 
74
                        locator.ProccessRoot (rootNode);
 
75
                }
 
76
 
 
77
                /// <summary>
 
78
                /// Finds the references to <paramref name="variable"/>.
 
79
                /// </summary>
 
80
                /// <param name='rootNode'>
 
81
                /// Root node for the search.
 
82
                /// </param>
 
83
                /// <param name='variable'>
 
84
                /// The variable to find references for.
 
85
                /// </param>
 
86
                /// <remarks>
 
87
                /// When a single <see cref="LocalReferenceFinder"/> is reused for multiple
 
88
                /// searches, which references outside of <paramref name="rootNode"/> are
 
89
                /// or are not reported is undefined.
 
90
                /// </remarks>
 
91
                public IList<ReferenceResult> FindReferences(AstNode rootNode, IVariable variable)
 
92
                {
 
93
                        lock (locator) {
 
94
                                VisitIfNeccessary(rootNode);
 
95
                                var lookup = (ILookup<IVariable, ReferenceResult>)references;
 
96
                                if (!lookup.Contains(variable))
 
97
                                        return new List<ReferenceResult>();
 
98
                                // Clone the list for thread safety
 
99
                                return references[variable].ToList();
 
100
                        }
 
101
                }
 
102
 
 
103
                class LocalReferenceLocator : DepthFirstAstVisitor
 
104
                {
 
105
                        CSharpAstResolver resolver;
 
106
 
 
107
                        LocalReferenceFinder referenceFinder;
 
108
 
 
109
                        public LocalReferenceLocator(CSharpAstResolver resolver, LocalReferenceFinder referenceFinder)
 
110
                        {
 
111
                                this.resolver = resolver;
 
112
                                this.referenceFinder = referenceFinder;
 
113
                        }
 
114
 
 
115
                        IList<IVariable> processedVariables = new List<IVariable>();
 
116
 
 
117
                        public void ProccessRoot (AstNode rootNode)
 
118
                        {
 
119
                                rootNode.AcceptVisitor(this);
 
120
                                referenceFinder.visitedRoots.Add(rootNode);
 
121
                        }
 
122
 
 
123
                        protected override void VisitChildren(AstNode node)
 
124
                        {
 
125
                                if (referenceFinder.visitedRoots.Contains(node))
 
126
                                        return;
 
127
                                var localResolveResult = resolver.Resolve(node) as LocalResolveResult;
 
128
                                if (localResolveResult != null && !processedVariables.Contains(localResolveResult.Variable)) {
 
129
                                        referenceFinder.references.Add(localResolveResult.Variable, new ReferenceResult(node, localResolveResult));
 
130
 
 
131
                                        processedVariables.Add(localResolveResult.Variable);
 
132
                                        base.VisitChildren(node);
 
133
                                        Debug.Assert(processedVariables.Contains(localResolveResult.Variable), "Variable should still be in the list of processed variables.");
 
134
                                        processedVariables.Remove(localResolveResult.Variable);
 
135
                                } else {
 
136
                                        base.VisitChildren(node);
 
137
                                }
 
138
                        }
 
139
                }
 
140
        }
 
141
 
 
142
        public class ReferenceResult
 
143
        {
 
144
                public ReferenceResult (AstNode node, LocalResolveResult resolveResult)
 
145
                {
 
146
                        Node = node;
 
147
                        ResolveResult = resolveResult;
 
148
                }
 
149
 
 
150
                public AstNode Node { get; private set; }
 
151
 
 
152
                public LocalResolveResult ResolveResult { get; private set; }
 
153
        }
 
154
}