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

« back to all changes in this revision

Viewing changes to contrib/NRefactory/Project/Src/Visitors/LookupTableVisitor.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
 
// <file>
2
 
//     <copyright see="prj:///doc/copyright.txt"/>
3
 
//     <license see="prj:///doc/license.txt"/>
4
 
//     <owner name="none" email=""/>
5
 
//     <version>$Revision: 4527 $</version>
6
 
// </file>
7
 
 
8
 
using System;
9
 
using System.Collections.Generic;
10
 
 
11
 
using ICSharpCode.OldNRefactory.Ast;
12
 
 
13
 
namespace ICSharpCode.OldNRefactory.Visitors
14
 
{
15
 
        public sealed class LocalLookupVariable
16
 
        {
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;
27
 
                
28
 
                public LocalLookupVariable(string name, TypeReference typeRef, Location startPos, Location endPos, bool isConst, bool isLoopVariable, Expression initializer, LambdaExpression parentLambdaExpression, bool isQueryContinuation, Location inListPosition)
29
 
                {
30
 
                        this.Name = name;
31
 
                        this.TypeRef = typeRef;
32
 
                        this.StartPos = startPos;
33
 
                        this.EndPos = endPos;
34
 
                        this.IsConst = isConst;
35
 
                        this.IsLoopVariable = isLoopVariable;
36
 
                        this.Initializer = initializer;
37
 
                        this.ParentLambdaExpression = parentLambdaExpression;
38
 
                        this.IsQueryContinuation = isQueryContinuation;
39
 
                        this.InListPosition = inListPosition;
40
 
                }
41
 
                
42
 
        }
43
 
        
44
 
        public sealed class LookupTableVisitor : AbstractAstVisitor
45
 
        {
46
 
                Dictionary<string, List<LocalLookupVariable>> variables;
47
 
                SupportedLanguage language;
48
 
                
49
 
                [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
50
 
                public Dictionary<string, List<LocalLookupVariable>> Variables {
51
 
                        get {
52
 
                                return variables;
53
 
                        }
54
 
                }
55
 
                
56
 
                List<WithStatement> withStatements = new List<WithStatement>();
57
 
                
58
 
                public List<WithStatement> WithStatements {
59
 
                        get {
60
 
                                return withStatements;
61
 
                        }
62
 
                }
63
 
                
64
 
                public LookupTableVisitor(SupportedLanguage language)
65
 
                {
66
 
                        this.language = language;
67
 
                        if (language == SupportedLanguage.VBNet) {
68
 
                                variables = new Dictionary<string, List<LocalLookupVariable>>(StringComparer.InvariantCultureIgnoreCase);
69
 
                        } else {
70
 
                                variables = new Dictionary<string, List<LocalLookupVariable>>(StringComparer.InvariantCulture);
71
 
                        }
72
 
                }
73
 
                
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)
79
 
                {
80
 
                        if (name == null || name.Length == 0) {
81
 
                                return;
82
 
                        }
83
 
                        List<LocalLookupVariable> list;
84
 
                        if (!variables.ContainsKey(name)) {
85
 
                                variables[name] = list = new List<LocalLookupVariable>();
86
 
                        } else {
87
 
                                list = (List<LocalLookupVariable>)variables[name];
88
 
                        }
89
 
                        list.Add(new LocalLookupVariable (name, typeRef, startPos, endPos, isConst, isLoopVariable, initializer, parentLambdaExpression, isQueryContinuation, inListPosition));
90
 
                }
91
 
                
92
 
                public override object VisitWithStatement(WithStatement withStatement, object data)
93
 
                {
94
 
                        withStatements.Add(withStatement);
95
 
                        return base.VisitWithStatement(withStatement, data);
96
 
                }
97
 
                
98
 
                Stack<Location> endLocationStack = new Stack<Location>();
99
 
                
100
 
                Location CurrentEndLocation {
101
 
                        get {
102
 
                                return (endLocationStack.Count == 0) ? Location.Empty : endLocationStack.Peek();
103
 
                        }
104
 
                }
105
 
                
106
 
                public override object VisitCompilationUnit (ICSharpCode.OldNRefactory.Ast.CompilationUnit compilationUnit, object data)
107
 
                {
108
 
                        variables.Clear ();
109
 
                        return base.VisitCompilationUnit (compilationUnit, data);
110
 
                }
111
 
                
112
 
                public override object VisitBlockStatement(BlockStatement blockStatement, object data)
113
 
                {
114
 
                        endLocationStack.Push(blockStatement.EndLocation);
115
 
                        base.VisitBlockStatement(blockStatement, data);
116
 
                        endLocationStack.Pop();
117
 
                        return null;
118
 
                }
119
 
                
120
 
                public override object VisitLocalVariableDeclaration(LocalVariableDeclaration localVariableDeclaration, object data)
121
 
                {
122
 
                        for (int i = 0; i < localVariableDeclaration.Variables.Count; ++i) {
123
 
                                VariableDeclaration varDecl = (VariableDeclaration)localVariableDeclaration.Variables[i];
124
 
                                
125
 
                                AddVariable(localVariableDeclaration.GetTypeForVariable(i),
126
 
                                            varDecl.Name,
127
 
                                            localVariableDeclaration.StartLocation,
128
 
                                            CurrentEndLocation,
129
 
                                            (localVariableDeclaration.Modifier & Modifiers.Const) == Modifiers.Const,
130
 
                                            false, varDecl.Initializer, null, false, localVariableDeclaration.SemicolonPosition);
131
 
                        }
132
 
                        return base.VisitLocalVariableDeclaration(localVariableDeclaration, data);
133
 
                }
134
 
                
135
 
                public override object VisitAnonymousMethodExpression(AnonymousMethodExpression anonymousMethodExpression, object data)
136
 
                {
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);
141
 
                        }
142
 
                        return base.VisitAnonymousMethodExpression(anonymousMethodExpression, data);
143
 
                }
144
 
                
145
 
                public override object VisitLambdaExpression(LambdaExpression lambdaExpression, object data)
146
 
                {
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);
151
 
                        }
152
 
                        return base.VisitLambdaExpression(lambdaExpression, data);
153
 
                }
154
 
                
155
 
                public override object VisitQueryExpression(QueryExpression queryExpression, object data)
156
 
                {
157
 
                        endLocationStack.Push(GetQueryVariableEndScope(queryExpression));
158
 
                        base.VisitQueryExpression(queryExpression, data);
159
 
                        endLocationStack.Pop();
160
 
                        return null;
161
 
                }
162
 
                
163
 
                Location GetQueryVariableEndScope(QueryExpression queryExpression)
164
 
                {
165
 
                        return queryExpression.EndLocation;
166
 
                }
167
 
                
168
 
                public override object VisitQueryExpressionFromClause(QueryExpressionFromClause fromClause, object data)
169
 
                {
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);
175
 
                }
176
 
                
177
 
                public override object VisitQueryExpressionJoinClause(QueryExpressionJoinClause joinClause, object data)
178
 
                {
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);
183
 
                        } else {
184
 
                                AddVariable(joinClause.Type, joinClause.Identifier,
185
 
                                            joinClause.StartLocation, joinClause.EndLocation,
186
 
                                            false, true, joinClause.InExpression, null, false, joinClause.StartLocation);
187
 
                                
188
 
                                AddVariable(joinClause.Type, joinClause.IntoIdentifier,
189
 
                                            joinClause.StartLocation, CurrentEndLocation,
190
 
                                            false, false, joinClause.InExpression, null, false, joinClause.StartLocation);
191
 
                        }
192
 
                        return base.VisitQueryExpressionJoinClause(joinClause, data);
193
 
                }
194
 
                
195
 
                public override object VisitQueryExpressionLetClause(QueryExpressionLetClause letClause, object data)
196
 
                {
197
 
                        AddVariable(null, letClause.Identifier,
198
 
                                    letClause.StartLocation, CurrentEndLocation,
199
 
                                    false, false, letClause.Expression, null, false, letClause.StartLocation);
200
 
                        return base.VisitQueryExpressionLetClause(letClause, data);
201
 
                }
202
 
                
203
 
                public override object VisitForNextStatement(ForNextStatement forNextStatement, object data)
204
 
                {
205
 
                        if (forNextStatement.EmbeddedStatement.EndLocation.IsEmpty) {
206
 
                                return base.VisitForNextStatement(forNextStatement, data);
207
 
                        } else {
208
 
                                endLocationStack.Push(forNextStatement.EmbeddedStatement.EndLocation);
209
 
                                AddVariable(forNextStatement.TypeReference,
210
 
                                            forNextStatement.VariableName,
211
 
                                            forNextStatement.StartLocation,
212
 
                                            forNextStatement.EndLocation,
213
 
                                            false, false,
214
 
                                            forNextStatement.Start,
215
 
                                            null, 
216
 
                                            false,
217
 
                                            forNextStatement.StartLocation);
218
 
                                
219
 
                                base.VisitForNextStatement(forNextStatement, data);
220
 
                                
221
 
                                endLocationStack.Pop();
222
 
                                return null;
223
 
                        }
224
 
                }
225
 
                
226
 
                public override object VisitFixedStatement(FixedStatement fixedStatement, object data)
227
 
                {
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);
231
 
                        } else {
232
 
                                endLocationStack.Push(fixedStatement.EmbeddedStatement.EndLocation);
233
 
                                base.VisitFixedStatement(fixedStatement, data);
234
 
                                endLocationStack.Pop();
235
 
                                return null;
236
 
                        }
237
 
                }
238
 
                
239
 
                public override object VisitForStatement(ForStatement forStatement, object data)
240
 
                {
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);
244
 
                        } else {
245
 
                                endLocationStack.Push(forStatement.EmbeddedStatement.EndLocation);
246
 
                                base.VisitForStatement(forStatement, data);
247
 
                                endLocationStack.Pop();
248
 
                                return null;
249
 
                        }
250
 
                }
251
 
                
252
 
                public override object VisitUsingStatement(UsingStatement usingStatement, object data)
253
 
                {
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);
257
 
                        } else {
258
 
                                endLocationStack.Push(usingStatement.EmbeddedStatement.EndLocation);
259
 
                                base.VisitUsingStatement(usingStatement, data);
260
 
                                endLocationStack.Pop();
261
 
                                return null;
262
 
                        }
263
 
                }
264
 
                
265
 
                public override object VisitSwitchSection(SwitchSection switchSection, object data)
266
 
                {
267
 
                        if (language == SupportedLanguage.VBNet) {
268
 
                                return VisitBlockStatement(switchSection, data);
269
 
                        } else {
270
 
                                return base.VisitSwitchSection(switchSection, data);
271
 
                        }
272
 
                }
273
 
                
274
 
                public override object VisitForeachStatement(ForeachStatement foreachStatement, object data)
275
 
                {
276
 
                        if (!foreachStatement.EmbeddedStatement.IsNull) {
277
 
                                AddVariable(foreachStatement.TypeReference,
278
 
                                            foreachStatement.VariableName,
279
 
                                            foreachStatement.EmbeddedStatement.StartLocation,
280
 
                                            foreachStatement.EndLocation,
281
 
                                            false, true,
282
 
                                            foreachStatement.Expression,
283
 
                                            null,
284
 
                                            false,
285
 
                                            foreachStatement.EmbeddedStatement.StartLocation);
286
 
                        }
287
 
                        
288
 
                        if (foreachStatement.Expression != null) {
289
 
                                foreachStatement.Expression.AcceptVisitor(this, data);
290
 
                        }
291
 
                        if (foreachStatement.EmbeddedStatement == null) {
292
 
                                return data;
293
 
                        }
294
 
                        return foreachStatement.EmbeddedStatement.AcceptVisitor(this, data);
295
 
                }
296
 
                
297
 
                public override object VisitTryCatchStatement(TryCatchStatement tryCatchStatement, object data)
298
 
                {
299
 
                        if (tryCatchStatement == null) {
300
 
                                return data;
301
 
                        }
302
 
                        if (tryCatchStatement.StatementBlock != null) {
303
 
                                tryCatchStatement.StatementBlock.AcceptVisitor(this, data);
304
 
                        }
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);
314
 
                                                }
315
 
                                                catchClause.StatementBlock.AcceptVisitor(this, data);
316
 
                                        }
317
 
                                }
318
 
                        }
319
 
                        if (tryCatchStatement.FinallyBlock != null) {
320
 
                                return tryCatchStatement.FinallyBlock.AcceptVisitor(this, data);
321
 
                        }
322
 
                        return data;
323
 
                }
324
 
        }
325
 
}