1 | // |
---|
2 | // LocalDeclarationSpaceVisitor.cs |
---|
3 | // |
---|
4 | // Author: |
---|
5 | // Simon Lindgren <simon.n.lindgren@gmail.com> |
---|
6 | // |
---|
7 | // Copyright (c) 2013 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 | using System; |
---|
27 | using System.Collections.Generic; |
---|
28 | |
---|
29 | namespace ICSharpCode.NRefactory.CSharp.Analysis |
---|
30 | { |
---|
31 | public class LocalDeclarationSpaceVisitor : DepthFirstAstVisitor |
---|
32 | { |
---|
33 | LocalDeclarationSpace currentDeclarationSpace; |
---|
34 | Dictionary<AstNode, LocalDeclarationSpace> nodeDeclarationSpaces = new Dictionary<AstNode, LocalDeclarationSpace>(); |
---|
35 | |
---|
36 | public LocalDeclarationSpace GetDeclarationSpace(AstNode node) |
---|
37 | { |
---|
38 | if (node == null) |
---|
39 | throw new ArgumentNullException("node"); |
---|
40 | while (node != null) { |
---|
41 | LocalDeclarationSpace declarationSpace; |
---|
42 | if (nodeDeclarationSpaces.TryGetValue(node, out declarationSpace)) |
---|
43 | return declarationSpace; |
---|
44 | node = node.Parent; |
---|
45 | } |
---|
46 | return null; |
---|
47 | } |
---|
48 | |
---|
49 | #region Visitor |
---|
50 | |
---|
51 | void AddDeclaration(string name, AstNode node) |
---|
52 | { |
---|
53 | if (currentDeclarationSpace != null) |
---|
54 | currentDeclarationSpace.AddDeclaration(name, node); |
---|
55 | } |
---|
56 | |
---|
57 | public override void VisitVariableInitializer(VariableInitializer variableInitializer) |
---|
58 | { |
---|
59 | AddDeclaration(variableInitializer.Name, variableInitializer); |
---|
60 | base.VisitVariableInitializer(variableInitializer); |
---|
61 | } |
---|
62 | |
---|
63 | public override void VisitParameterDeclaration(ParameterDeclaration parameterDeclaration) |
---|
64 | { |
---|
65 | AddDeclaration(parameterDeclaration.Name, parameterDeclaration); |
---|
66 | base.VisitParameterDeclaration(parameterDeclaration); |
---|
67 | } |
---|
68 | |
---|
69 | void VisitNewDeclarationSpace(AstNode node) |
---|
70 | { |
---|
71 | var oldDeclarationSpace = currentDeclarationSpace; |
---|
72 | currentDeclarationSpace = new LocalDeclarationSpace(); |
---|
73 | if (oldDeclarationSpace != null) |
---|
74 | oldDeclarationSpace.AddChildSpace(currentDeclarationSpace); |
---|
75 | |
---|
76 | VisitChildren(node); |
---|
77 | |
---|
78 | nodeDeclarationSpaces.Add(node, currentDeclarationSpace); |
---|
79 | currentDeclarationSpace = oldDeclarationSpace; |
---|
80 | } |
---|
81 | |
---|
82 | #region Declaration space creating nodes |
---|
83 | |
---|
84 | public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration) |
---|
85 | { |
---|
86 | VisitNewDeclarationSpace(methodDeclaration); |
---|
87 | } |
---|
88 | |
---|
89 | public override void VisitBlockStatement(BlockStatement blockStatement) |
---|
90 | { |
---|
91 | VisitNewDeclarationSpace(blockStatement); |
---|
92 | } |
---|
93 | |
---|
94 | public override void VisitSwitchStatement(SwitchStatement switchStatement) |
---|
95 | { |
---|
96 | VisitNewDeclarationSpace(switchStatement); |
---|
97 | } |
---|
98 | |
---|
99 | public override void VisitForeachStatement(ForeachStatement foreachStatement) |
---|
100 | { |
---|
101 | AddDeclaration(foreachStatement.VariableName, foreachStatement); |
---|
102 | VisitNewDeclarationSpace(foreachStatement); |
---|
103 | } |
---|
104 | |
---|
105 | public override void VisitForStatement(ForStatement forStatement) |
---|
106 | { |
---|
107 | VisitNewDeclarationSpace(forStatement); |
---|
108 | } |
---|
109 | |
---|
110 | public override void VisitUsingStatement(UsingStatement usingStatement) |
---|
111 | { |
---|
112 | VisitNewDeclarationSpace(usingStatement); |
---|
113 | } |
---|
114 | |
---|
115 | public override void VisitLambdaExpression(LambdaExpression lambdaExpression) |
---|
116 | { |
---|
117 | VisitNewDeclarationSpace(lambdaExpression); |
---|
118 | } |
---|
119 | |
---|
120 | public override void VisitAnonymousMethodExpression(AnonymousMethodExpression anonymousMethodExpression) |
---|
121 | { |
---|
122 | VisitNewDeclarationSpace(anonymousMethodExpression); |
---|
123 | } |
---|
124 | |
---|
125 | public override void VisitEventDeclaration(EventDeclaration eventDeclaration) |
---|
126 | { |
---|
127 | AddDeclaration(eventDeclaration.Name, eventDeclaration); |
---|
128 | } |
---|
129 | |
---|
130 | public override void VisitCustomEventDeclaration(CustomEventDeclaration eventDeclaration) |
---|
131 | { |
---|
132 | VisitNewDeclarationSpace(eventDeclaration); |
---|
133 | } |
---|
134 | |
---|
135 | #endregion |
---|
136 | #endregion |
---|
137 | } |
---|
138 | } |
---|