2
// <copyright see="prj:///doc/copyright.txt"/>
3
// <license see="prj:///doc/license.txt"/>
4
// <owner name="none" email=""/>
5
// <version>$Revision: 4527 $</version>
9
using System.Collections.Generic;
11
using ICSharpCode.OldNRefactory.Ast;
13
namespace ICSharpCode.OldNRefactory.Visitors
15
public sealed class LocalLookupVariable
17
public readonly string Name;
18
public readonly TypeReference TypeRef;
19
public readonly Location StartPos;
20
public readonly Location EndPos;
21
public readonly bool IsConst;
22
public readonly bool IsLoopVariable;
23
public readonly Expression Initializer;
24
public readonly LambdaExpression ParentLambdaExpression;
25
public readonly bool IsQueryContinuation;
26
public readonly Location InListPosition;
28
public LocalLookupVariable(string name, TypeReference typeRef, Location startPos, Location endPos, bool isConst, bool isLoopVariable, Expression initializer, LambdaExpression parentLambdaExpression, bool isQueryContinuation, Location inListPosition)
31
this.TypeRef = typeRef;
32
this.StartPos = startPos;
34
this.IsConst = isConst;
35
this.IsLoopVariable = isLoopVariable;
36
this.Initializer = initializer;
37
this.ParentLambdaExpression = parentLambdaExpression;
38
this.IsQueryContinuation = isQueryContinuation;
39
this.InListPosition = inListPosition;
44
public sealed class LookupTableVisitor : AbstractAstVisitor
46
Dictionary<string, List<LocalLookupVariable>> variables;
47
SupportedLanguage language;
49
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
50
public Dictionary<string, List<LocalLookupVariable>> Variables {
56
List<WithStatement> withStatements = new List<WithStatement>();
58
public List<WithStatement> WithStatements {
60
return withStatements;
64
public LookupTableVisitor(SupportedLanguage language)
66
this.language = language;
67
if (language == SupportedLanguage.VBNet) {
68
variables = new Dictionary<string, List<LocalLookupVariable>>(StringComparer.InvariantCultureIgnoreCase);
70
variables = new Dictionary<string, List<LocalLookupVariable>>(StringComparer.InvariantCulture);
74
public void AddVariable(TypeReference typeRef, string name,
75
Location startPos, Location endPos, bool isConst,
76
bool isLoopVariable, Expression initializer,
77
LambdaExpression parentLambdaExpression,
78
bool isQueryContinuation, Location inListPosition)
80
if (name == null || name.Length == 0) {
83
List<LocalLookupVariable> list;
84
if (!variables.ContainsKey(name)) {
85
variables[name] = list = new List<LocalLookupVariable>();
87
list = (List<LocalLookupVariable>)variables[name];
89
list.Add(new LocalLookupVariable (name, typeRef, startPos, endPos, isConst, isLoopVariable, initializer, parentLambdaExpression, isQueryContinuation, inListPosition));
92
public override object VisitWithStatement(WithStatement withStatement, object data)
94
withStatements.Add(withStatement);
95
return base.VisitWithStatement(withStatement, data);
98
Stack<Location> endLocationStack = new Stack<Location>();
100
Location CurrentEndLocation {
102
return (endLocationStack.Count == 0) ? Location.Empty : endLocationStack.Peek();
106
public override object VisitCompilationUnit (ICSharpCode.OldNRefactory.Ast.CompilationUnit compilationUnit, object data)
109
return base.VisitCompilationUnit (compilationUnit, data);
112
public override object VisitBlockStatement(BlockStatement blockStatement, object data)
114
endLocationStack.Push(blockStatement.EndLocation);
115
base.VisitBlockStatement(blockStatement, data);
116
endLocationStack.Pop();
120
public override object VisitLocalVariableDeclaration(LocalVariableDeclaration localVariableDeclaration, object data)
122
for (int i = 0; i < localVariableDeclaration.Variables.Count; ++i) {
123
VariableDeclaration varDecl = (VariableDeclaration)localVariableDeclaration.Variables[i];
125
AddVariable(localVariableDeclaration.GetTypeForVariable(i),
127
localVariableDeclaration.StartLocation,
129
(localVariableDeclaration.Modifier & Modifiers.Const) == Modifiers.Const,
130
false, varDecl.Initializer, null, false, localVariableDeclaration.SemicolonPosition);
132
return base.VisitLocalVariableDeclaration(localVariableDeclaration, data);
135
public override object VisitAnonymousMethodExpression(AnonymousMethodExpression anonymousMethodExpression, object data)
137
foreach (ParameterDeclarationExpression p in anonymousMethodExpression.Parameters) {
138
AddVariable(p.TypeReference, p.ParameterName,
139
anonymousMethodExpression.StartLocation, anonymousMethodExpression.EndLocation,
140
false, false, null, null, false, anonymousMethodExpression.EndLocation);
142
return base.VisitAnonymousMethodExpression(anonymousMethodExpression, data);
145
public override object VisitLambdaExpression(LambdaExpression lambdaExpression, object data)
147
foreach (ParameterDeclarationExpression p in lambdaExpression.Parameters) {
148
AddVariable(p.TypeReference, p.ParameterName,
149
lambdaExpression.StartLocation, lambdaExpression.ExtendedEndLocation,
150
false, false, null, lambdaExpression, false, lambdaExpression.StartLocation);
152
return base.VisitLambdaExpression(lambdaExpression, data);
155
public override object VisitQueryExpression(QueryExpression queryExpression, object data)
157
endLocationStack.Push(GetQueryVariableEndScope(queryExpression));
158
base.VisitQueryExpression(queryExpression, data);
159
endLocationStack.Pop();
163
Location GetQueryVariableEndScope(QueryExpression queryExpression)
165
return queryExpression.EndLocation;
168
public override object VisitQueryExpressionFromClause(QueryExpressionFromClause fromClause, object data)
170
QueryExpression parent = fromClause.Parent as QueryExpression;
171
AddVariable(fromClause.Type, fromClause.Identifier,
172
fromClause.StartLocation, new Location (CurrentEndLocation.Column + 1, CurrentEndLocation.Line),
173
false, true, fromClause.InExpression, null, parent != null && parent.IsQueryContinuation, fromClause.StartLocation);
174
return base.VisitQueryExpressionFromClause(fromClause, data);
177
public override object VisitQueryExpressionJoinClause(QueryExpressionJoinClause joinClause, object data)
179
if (string.IsNullOrEmpty(joinClause.IntoIdentifier)) {
180
AddVariable(joinClause.Type, joinClause.Identifier,
181
joinClause.StartLocation, CurrentEndLocation,
182
false, true, joinClause.InExpression, null, false, joinClause.StartLocation);
184
AddVariable(joinClause.Type, joinClause.Identifier,
185
joinClause.StartLocation, joinClause.EndLocation,
186
false, true, joinClause.InExpression, null, false, joinClause.StartLocation);
188
AddVariable(joinClause.Type, joinClause.IntoIdentifier,
189
joinClause.StartLocation, CurrentEndLocation,
190
false, false, joinClause.InExpression, null, false, joinClause.StartLocation);
192
return base.VisitQueryExpressionJoinClause(joinClause, data);
195
public override object VisitQueryExpressionLetClause(QueryExpressionLetClause letClause, object data)
197
AddVariable(null, letClause.Identifier,
198
letClause.StartLocation, CurrentEndLocation,
199
false, false, letClause.Expression, null, false, letClause.StartLocation);
200
return base.VisitQueryExpressionLetClause(letClause, data);
203
public override object VisitForNextStatement(ForNextStatement forNextStatement, object data)
205
if (forNextStatement.EmbeddedStatement.EndLocation.IsEmpty) {
206
return base.VisitForNextStatement(forNextStatement, data);
208
endLocationStack.Push(forNextStatement.EmbeddedStatement.EndLocation);
209
AddVariable(forNextStatement.TypeReference,
210
forNextStatement.VariableName,
211
forNextStatement.StartLocation,
212
forNextStatement.EndLocation,
214
forNextStatement.Start,
217
forNextStatement.StartLocation);
219
base.VisitForNextStatement(forNextStatement, data);
221
endLocationStack.Pop();
226
public override object VisitFixedStatement(FixedStatement fixedStatement, object data)
228
// uses LocalVariableDeclaration, we just have to put the end location on the stack
229
if (fixedStatement.EmbeddedStatement.EndLocation.IsEmpty) {
230
return base.VisitFixedStatement(fixedStatement, data);
232
endLocationStack.Push(fixedStatement.EmbeddedStatement.EndLocation);
233
base.VisitFixedStatement(fixedStatement, data);
234
endLocationStack.Pop();
239
public override object VisitForStatement(ForStatement forStatement, object data)
241
// uses LocalVariableDeclaration, we just have to put the end location on the stack
242
if (forStatement.EmbeddedStatement.EndLocation.IsEmpty) {
243
return base.VisitForStatement(forStatement, data);
245
endLocationStack.Push(forStatement.EmbeddedStatement.EndLocation);
246
base.VisitForStatement(forStatement, data);
247
endLocationStack.Pop();
252
public override object VisitUsingStatement(UsingStatement usingStatement, object data)
254
// uses LocalVariableDeclaration, we just have to put the end location on the stack
255
if (usingStatement.EmbeddedStatement.EndLocation.IsEmpty) {
256
return base.VisitUsingStatement(usingStatement, data);
258
endLocationStack.Push(usingStatement.EmbeddedStatement.EndLocation);
259
base.VisitUsingStatement(usingStatement, data);
260
endLocationStack.Pop();
265
public override object VisitSwitchSection(SwitchSection switchSection, object data)
267
if (language == SupportedLanguage.VBNet) {
268
return VisitBlockStatement(switchSection, data);
270
return base.VisitSwitchSection(switchSection, data);
274
public override object VisitForeachStatement(ForeachStatement foreachStatement, object data)
276
if (!foreachStatement.EmbeddedStatement.IsNull) {
277
AddVariable(foreachStatement.TypeReference,
278
foreachStatement.VariableName,
279
foreachStatement.EmbeddedStatement.StartLocation,
280
foreachStatement.EndLocation,
282
foreachStatement.Expression,
285
foreachStatement.EmbeddedStatement.StartLocation);
288
if (foreachStatement.Expression != null) {
289
foreachStatement.Expression.AcceptVisitor(this, data);
291
if (foreachStatement.EmbeddedStatement == null) {
294
return foreachStatement.EmbeddedStatement.AcceptVisitor(this, data);
297
public override object VisitTryCatchStatement(TryCatchStatement tryCatchStatement, object data)
299
if (tryCatchStatement == null) {
302
if (tryCatchStatement.StatementBlock != null) {
303
tryCatchStatement.StatementBlock.AcceptVisitor(this, data);
305
if (tryCatchStatement.CatchClauses != null) {
306
foreach (CatchClause catchClause in tryCatchStatement.CatchClauses) {
307
if (catchClause != null) {
308
if (catchClause.TypeReference != null && catchClause.VariableName != null) {
309
AddVariable(catchClause.TypeReference,
310
catchClause.VariableName,
311
catchClause.StartLocation,
312
catchClause.StatementBlock.EndLocation,
313
false, false, null, null, false, catchClause.StartLocation);
315
catchClause.StatementBlock.AcceptVisitor(this, data);
319
if (tryCatchStatement.FinallyBlock != null) {
320
return tryCatchStatement.FinallyBlock.AcceptVisitor(this, data);