File: ToVBNetRenameConflictingVariables.cs

package info (click to toggle)
monodevelop 2.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 54,088 kB
  • ctags: 67,523
  • sloc: cs: 436,824; xml: 18,137; makefile: 5,722; sh: 1,085; sed: 2
file content (91 lines) | stat: -rw-r--r-- 3,112 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
//     <version>$Revision: 4570 $</version>
// </file>

using System;
using System.Collections.Generic;
using ICSharpCode.NRefactory.Ast;

namespace ICSharpCode.NRefactory.Visitors
{
	/// <summary>
	/// Renames local variables if they conflict with other locals, fields or parameters.
	/// </summary>
	static class ToVBNetRenameConflictingVariablesVisitor
	{
		public static void RenameConflicting(ParametrizedNode method)
		{
			// variable name => case sensitive variable name
			// value is null if there are multiple casings for the variable -> the variable is conflicting
			Dictionary<string, string> caseInsensitive = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
			
			LookupTableVisitor ltv = new LookupTableVisitor(SupportedLanguage.CSharp);
			method.AcceptVisitor(ltv, null);
			
			// add method parameters to caseInsensitive
			foreach (ParameterDeclarationExpression pde in method.Parameters) {
				AddVariableToDict(caseInsensitive, pde.ParameterName, true);
			}
			
			// add local variables to caseInsensitive
			foreach (KeyValuePair<string, List<LocalLookupVariable>> var in ltv.Variables) {
				AddVariableToDict(caseInsensitive, var.Key, true);
			}
			
			// add used identifiers to caseInsensitive
			FindIdentifiersVisitor fvv = new FindIdentifiersVisitor();
			method.AcceptVisitor(fvv, null);
			
			foreach (KeyValuePair<string, string> pair in fvv.usedIdentifiers) {
				AddVariableToDict(caseInsensitive, pair.Key, false);
			}
			
			int index = 0;
			foreach (ParameterDeclarationExpression pde in method.Parameters) {
				if (caseInsensitive[pde.ParameterName] == null) {
					RenameVariable(method, pde.ParameterName, ref index);
				}
			}
			foreach (KeyValuePair<string, List<LocalLookupVariable>> var in ltv.Variables) {
				if (caseInsensitive[var.Key] == null) {
					RenameVariable(method, var.Key, ref index);
				}
			}
		}
		
		static void RenameVariable(INode method, string from, ref int index)
		{
			index += 1;
			method.AcceptVisitor(new RenameLocalVariableVisitor(from, from + "__" + index, StringComparer.Ordinal), null);
		}
		
		static void AddVariableToDict(Dictionary<string, string> caseInsensitive, string varName, bool hasDeclaration)
		{
			string existing;
			if (caseInsensitive.TryGetValue(varName, out existing)) {
				if (existing != null && existing != varName) {
					caseInsensitive[varName] = null;
				}
			} else {
				if (hasDeclaration) {
					caseInsensitive.Add(varName, varName);
				}
			}
		}
		
		sealed class FindIdentifiersVisitor : AbstractAstVisitor
		{
			// use dictionary as HashSet to remember used identifiers
			internal readonly Dictionary<string, string> usedIdentifiers = new Dictionary<string, string>();
			
			public override object VisitIdentifierExpression(IdentifierExpression identifierExpression, object data)
			{
				usedIdentifiers[identifierExpression.Identifier] = null;
				return null;
			}
		}
	}
}