public static void RenameLocals(Block block, StringComparer nameComparer) { FindVariableDeclarationsVisitor fvdv = new FindVariableDeclarationsVisitor(); block.Accept(fvdv); List <DeclarationStatement> list = new List <DeclarationStatement>(); foreach (DeclarationStatement decl in fvdv.Declarations) { DeclarationStatement conflict = null; int conflictIndex = -1; for (int i = 0; i < list.Count; i++) { if (nameComparer.Equals(list[i].Declaration.Name, decl.Declaration.Name)) { conflict = list[i]; conflictIndex = i; break; } } if (conflict == null) { list.Add(decl); } else { // Handle conflict: try if "moveup" would be sufficient if (IsSameType(decl.Declaration.Type, conflict.Declaration.Type, nameComparer)) { // create declaration at beginning of class and // replace decl & conflict by assignment DeclarationStatement newDecl = new DeclarationStatement(conflict.LexicalInfo); newDecl.Declaration = new Declaration(conflict.Declaration.LexicalInfo, conflict.Declaration.Name, conflict.Declaration.Type); block.Insert(0, newDecl); ReplaceWithInitializer(decl); ReplaceWithInitializer(conflict); list[conflictIndex] = newDecl; } else { string newName = FindFreeName(decl.Declaration.Name, list, fvdv.Declarations, nameComparer); decl.ParentNode.Accept(new RenameLocalsVisitor(decl.Declaration.Name, newName, nameComparer)); decl.Declaration.Name = newName; } } } }
public static void RenameLocals(Block block, StringComparer nameComparer) { FindVariableDeclarationsVisitor fvdv = new FindVariableDeclarationsVisitor(); block.Accept(fvdv); List<DeclarationStatement> list = new List<DeclarationStatement>(); foreach (DeclarationStatement decl in fvdv.Declarations) { DeclarationStatement conflict = null; int conflictIndex = -1; for (int i = 0; i < list.Count; i++) { if (nameComparer.Equals(list[i].Declaration.Name, decl.Declaration.Name)) { conflict = list[i]; conflictIndex = i; break; } } if (conflict == null) { list.Add(decl); } else { // Handle conflict: try if "moveup" would be sufficient if (IsSameType(decl.Declaration.Type, conflict.Declaration.Type, nameComparer)) { // create declaration at beginning of class and // replace decl & conflict by assignment DeclarationStatement newDecl = new DeclarationStatement(conflict.LexicalInfo); newDecl.Declaration = new Declaration(conflict.Declaration.LexicalInfo, conflict.Declaration.Name, conflict.Declaration.Type); block.Insert(0, newDecl); ReplaceWithInitializer(decl); ReplaceWithInitializer(conflict); list[conflictIndex] = newDecl; } else { string newName = FindFreeName(decl.Declaration.Name, list, fvdv.Declarations, nameComparer); decl.ParentNode.Accept(new RenameLocalsVisitor(decl.Declaration.Name, newName, nameComparer)); decl.Declaration.Name = newName; } } } }