Browse Source

Improve automatic variable names.

pull/1/head^2
Daniel Grunwald 15 years ago
parent
commit
7d4252373c
  1. 27
      ICSharpCode.Decompiler/Ast/AstMetodBodyBuilder.cs

27
ICSharpCode.Decompiler/Ast/AstMetodBodyBuilder.cs

@ -30,6 +30,23 @@ namespace Decompiler
} }
} }
static readonly Dictionary<string, string> typeNameToVariableNameDict = new Dictionary<string, string> {
{ "System.Boolean", "flag" },
{ "System.Byte", "b" },
{ "System.SByte", "b" },
{ "System.Int16", "num" },
{ "System.Int32", "num" },
{ "System.Int64", "num" },
{ "System.UInt16", "num" },
{ "System.UInt32", "num" },
{ "System.UInt64", "num" },
{ "System.Single", "num" },
{ "System.Double", "num" },
{ "System.Decimal", "num" },
{ "System.String", "text" },
{ "System.Object", "obj" },
};
public BlockStatement CreateMetodBody() public BlockStatement CreateMetodBody()
{ {
Ast.BlockStatement astBlock = new Ast.BlockStatement(); Ast.BlockStatement astBlock = new Ast.BlockStatement();
@ -45,24 +62,16 @@ namespace Decompiler
List<string> intNames = new List<string>(new string[] {"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t"}); List<string> intNames = new List<string>(new string[] {"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t"});
Dictionary<string, int> typeNames = new Dictionary<string, int>(); Dictionary<string, int> typeNames = new Dictionary<string, int>();
int boolFlagId = 1;
foreach(VariableDefinition varDef in methodDef.Body.Variables) { foreach(VariableDefinition varDef in methodDef.Body.Variables) {
if (string.IsNullOrEmpty(varDef.Name)) { if (string.IsNullOrEmpty(varDef.Name)) {
if (varDef.VariableType.FullName == Constants.Int32 && intNames.Count > 0) { if (varDef.VariableType.FullName == Constants.Int32 && intNames.Count > 0) {
varDef.Name = intNames[0]; varDef.Name = intNames[0];
intNames.RemoveAt(0); intNames.RemoveAt(0);
} else if (varDef.VariableType.FullName == Constants.Boolean) {
if (boolFlagId == 1) {
varDef.Name = "flag";
} else {
varDef.Name = "flag" + boolFlagId;
}
boolFlagId++;
} else { } else {
string name; string name;
if (varDef.VariableType.IsArray) { if (varDef.VariableType.IsArray) {
name = "array"; name = "array";
} else { } else if (!typeNameToVariableNameDict.TryGetValue(varDef.VariableType.FullName, out name)) {
name = varDef.VariableType.Name; name = varDef.VariableType.Name;
name = char.ToLower(name[0]) + name.Substring(1); name = char.ToLower(name[0]) + name.Substring(1);
} }

Loading…
Cancel
Save