de4dot-cex/de4dot.code/renamer/VariableNameState.cs

166 lines
5.4 KiB
C#
Raw Permalink Normal View History

2011-11-18 23:55:54 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2011-11-18 23:55:54 +08:00
This file is part of de4dot.
de4dot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
de4dot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
*/
using dnlib.DotNet;
2011-11-18 23:55:54 +08:00
namespace de4dot.code.renamer {
public class VariableNameState {
2012-01-27 12:39:25 +08:00
ExistingNames existingVariableNames;
ExistingNames existingMethodNames;
ExistingNames existingPropertyNames;
ExistingNames existingEventNames;
TypeNames variableNameCreator; // For fields and method args
TypeNames propertyNameCreator;
NameCreator eventNameCreator;
NameCreator genericPropertyNameCreator;
public NameCreator staticMethodNameCreator;
public NameCreator instanceMethodNameCreator;
2013-01-19 20:03:57 +08:00
public static VariableNameState Create() {
2012-01-27 12:39:25 +08:00
var vns = new VariableNameState();
vns.existingVariableNames = new ExistingNames();
vns.existingMethodNames = new ExistingNames();
vns.existingPropertyNames = new ExistingNames();
vns.existingEventNames = new ExistingNames();
vns.variableNameCreator = new VariableNameCreator();
vns.propertyNameCreator = new PropertyNameCreator();
vns.eventNameCreator = new NameCreator("Event_");
vns.genericPropertyNameCreator = new NameCreator("Prop_");
vns.staticMethodNameCreator = new NameCreator("smethod_");
vns.instanceMethodNameCreator = new NameCreator("method_");
return vns;
}
VariableNameState() {
}
// Cloning only params will speed up the method param renaming code
2013-01-19 20:03:57 +08:00
public VariableNameState CloneParamsOnly() {
2012-01-27 12:39:25 +08:00
var vns = new VariableNameState();
vns.existingVariableNames = new ExistingNames();
vns.variableNameCreator = new VariableNameCreator();
2013-01-19 20:03:57 +08:00
vns.existingVariableNames.Merge(existingVariableNames);
vns.variableNameCreator.Merge(variableNameCreator);
2012-01-27 12:39:25 +08:00
return vns;
2011-11-18 23:55:54 +08:00
}
2013-01-19 20:03:57 +08:00
public VariableNameState Merge(VariableNameState other) {
2014-04-17 01:15:11 +08:00
if (this == other)
return this;
2013-01-19 20:03:57 +08:00
existingVariableNames.Merge(other.existingVariableNames);
existingMethodNames.Merge(other.existingMethodNames);
existingPropertyNames.Merge(other.existingPropertyNames);
existingEventNames.Merge(other.existingEventNames);
variableNameCreator.Merge(other.variableNameCreator);
propertyNameCreator.Merge(other.propertyNameCreator);
eventNameCreator.Merge(other.eventNameCreator);
genericPropertyNameCreator.Merge(other.genericPropertyNameCreator);
staticMethodNameCreator.Merge(other.staticMethodNameCreator);
instanceMethodNameCreator.Merge(other.instanceMethodNameCreator);
return this;
2011-11-21 17:36:23 +08:00
}
2013-01-19 20:03:57 +08:00
public void MergeMethods(VariableNameState other) {
existingMethodNames.Merge(other.existingMethodNames);
2011-11-22 22:56:48 +08:00
}
2013-01-19 20:03:57 +08:00
public void MergeProperties(VariableNameState other) {
existingPropertyNames.Merge(other.existingPropertyNames);
2011-11-22 22:56:48 +08:00
}
2013-01-19 20:03:57 +08:00
public void MergeEvents(VariableNameState other) {
existingEventNames.Merge(other.existingEventNames);
2011-11-22 22:56:48 +08:00
}
2013-01-19 20:03:57 +08:00
public string GetNewPropertyName(PropertyDef propertyDef) {
2012-11-22 16:14:51 +08:00
var propType = propertyDef.PropertySig.GetRetType();
2011-11-21 17:36:23 +08:00
string newName;
2013-01-19 20:03:57 +08:00
if (IsGeneric(propType))
newName = existingPropertyNames.GetName(propertyDef.Name, genericPropertyNameCreator);
2011-11-21 17:36:23 +08:00
else
2013-01-19 20:03:57 +08:00
newName = existingPropertyNames.GetName(propertyDef.Name, () => propertyNameCreator.Create(propType));
AddPropertyName(newName);
2011-11-21 17:36:23 +08:00
return newName;
}
2013-01-19 20:03:57 +08:00
static bool IsGeneric(TypeSig type) {
2012-11-04 07:50:24 +08:00
while (type != null) {
if (type.IsGenericParameter)
return true;
2012-11-04 07:50:24 +08:00
type = type.Next;
}
2012-11-04 07:50:24 +08:00
return false;
}
2013-01-19 20:03:57 +08:00
public string GetNewEventName(EventDef eventDef) {
string newName = eventNameCreator.Create();
AddEventName(newName);
2011-11-21 17:36:23 +08:00
return newName;
2011-11-18 23:55:54 +08:00
}
2013-01-19 20:03:57 +08:00
public void AddFieldName(string fieldName) {
existingVariableNames.Add(fieldName);
2011-11-18 23:55:54 +08:00
}
2013-01-19 20:03:57 +08:00
public void AddParamName(string paramName) {
existingVariableNames.Add(paramName);
2011-11-21 17:36:23 +08:00
}
2013-01-19 20:03:57 +08:00
public void AddMethodName(string methodName) {
existingMethodNames.Add(methodName);
2011-11-21 17:36:23 +08:00
}
2013-01-19 20:03:57 +08:00
public void AddPropertyName(string propName) {
existingPropertyNames.Add(propName);
2011-11-21 17:36:23 +08:00
}
2013-01-19 20:03:57 +08:00
public void AddEventName(string eventName) {
existingEventNames.Add(eventName);
2011-11-21 17:36:23 +08:00
}
2013-01-19 20:03:57 +08:00
public bool IsMethodNameUsed(string methodName) {
return existingMethodNames.Exists(methodName);
2011-11-21 17:36:23 +08:00
}
2013-01-19 20:03:57 +08:00
public bool IsPropertyNameUsed(string propName) {
return existingPropertyNames.Exists(propName);
2011-11-21 17:36:23 +08:00
}
2013-01-19 20:03:57 +08:00
public bool IsEventNameUsed(string eventName) {
return existingEventNames.Exists(eventName);
2011-11-21 17:36:23 +08:00
}
2013-01-19 20:03:57 +08:00
public string GetNewFieldName(FieldDef field) {
return existingVariableNames.GetName(field.Name, () => variableNameCreator.Create(field.FieldSig.GetFieldType()));
2011-11-18 23:55:54 +08:00
}
2013-01-19 20:03:57 +08:00
public string GetNewFieldName(string oldName, INameCreator nameCreator) {
return existingVariableNames.GetName(oldName, () => nameCreator.Create());
2011-11-18 23:55:54 +08:00
}
2011-11-21 17:36:23 +08:00
2013-01-19 20:03:57 +08:00
public string GetNewParamName(string oldName, Parameter param) {
return existingVariableNames.GetName(oldName, () => variableNameCreator.Create(param.Type));
2011-11-21 17:36:23 +08:00
}
2013-01-19 20:03:57 +08:00
public string GetNewMethodName(string oldName, INameCreator nameCreator) {
return existingMethodNames.GetName(oldName, nameCreator);
2011-11-21 17:36:23 +08:00
}
2011-11-18 23:55:54 +08:00
}
}