Sanitize field and locals names for CppCodeGen

This commit is contained in:
Jan Kotas 2015-10-14 21:20:39 -07:00
Родитель bf7cb7c788
Коммит a345623fce
3 изменённых файлов: 35 добавлений и 38 удалений

Просмотреть файл

@ -30,7 +30,7 @@ namespace ILToNative
//
// Turn a name into a valid C/C++ identifier
//
private string SanitizeName(string s, bool typeName = false)
internal string SanitizeName(string s, bool typeName = false)
{
StringBuilder sb = null;
for (int i = 0; i < s.Length; i++)

Просмотреть файл

@ -237,11 +237,16 @@ namespace ILToNative.CppCodeGen
return _compilation.NameMangler.GetMangledMethodName(method);
}
public string GetCppFieldName(FieldDesc field)
{
return _compilation.NameMangler.SanitizeName(field.Name);
}
public string GetCppStaticFieldName(FieldDesc field)
{
TypeDesc type = field.OwningType;
string typeName = GetCppTypeName(type);
return typeName.Replace("::", "__") + "__" + SanitizeName(field.Name);
return typeName.Replace("::", "__") + "__" + _compilation.NameMangler.SanitizeName(field.Name);
}
enum SpecialMethodKind
@ -355,28 +360,28 @@ namespace ILToNative.CppCodeGen
return;
}
var ilImporter = new ILImporter(_compilation, this, method, methodIL);
CompilerTypeSystemContext typeSystemContext = _compilation.TypeSystemContext;
if (!_compilation.Options.NoLineNumbers)
{
IEnumerable<ILSequencePoint> sequencePoints = typeSystemContext.GetSequencePointsForMethod(method);
if (sequencePoints != null)
ilImporter.SetSequencePoints(sequencePoints);
}
IEnumerable<LocalVariable> localVariables = typeSystemContext.GetLocalVariableNamesForMethod(method);
if (localVariables != null)
ilImporter.SetLocalVariables(localVariables);
IEnumerable<string> parameters = typeSystemContext.GetParameterNamesForMethod(method);
if (parameters != null)
ilImporter.SetParameterNames(parameters);
string methodCode;
try
{
var ilImporter = new ILImporter(_compilation, this, method, methodIL);
CompilerTypeSystemContext typeSystemContext = _compilation.TypeSystemContext;
if (!_compilation.Options.NoLineNumbers)
{
IEnumerable<ILSequencePoint> sequencePoints = typeSystemContext.GetSequencePointsForMethod(method);
if (sequencePoints != null)
ilImporter.SetSequencePoints(sequencePoints);
}
IEnumerable<LocalVariable> localVariables = typeSystemContext.GetLocalVariableNamesForMethod(method);
if (localVariables != null)
ilImporter.SetLocalVariables(localVariables);
IEnumerable<string> parameters = typeSystemContext.GetParameterNamesForMethod(method);
if (parameters != null)
ilImporter.SetParameterNames(parameters);
methodCode = ilImporter.Compile();
}
catch (Exception e)
@ -389,17 +394,6 @@ namespace ILToNative.CppCodeGen
_compilation.GetRegisteredMethod(method).MethodCode = methodCode;
}
// Turn a name into a valid CPP identifier
private static string SanitizeName(string s)
{
// TODO: Handle Unicode, etc.
s = s.Replace("`", "_");
s = s.Replace("<", "_");
s = s.Replace(">", "_");
s = s.Replace("$", "_");
return s;
}
TextWriter Out
{
get
@ -589,7 +583,7 @@ namespace ILToNative.CppCodeGen
}
else
{
Out.WriteLine(GetCppSignatureTypeName(field.FieldType) + " " + field.Name + ";");
Out.WriteLine(GetCppSignatureTypeName(field.FieldType) + " " + GetCppFieldName(field) + ";");
}
}
if (t.Type.GetMethod(".cctor", null) != null)
@ -677,7 +671,9 @@ namespace ILToNative.CppCodeGen
sb.Append(GetCppMethodName(method));
sb.Append("(void * pThis) { return (__slot__");
sb.Append(GetCppMethodName(method));
sb.Append(")(((System::Delegate *)pThis)->m_functionPointer);");
sb.Append(")(((");
sb.Append(GetCppSignatureTypeName(_compilation.TypeSystemContext.GetWellKnownType(WellKnownType.MulticastDelegate)));
sb.Append(")pThis)->m_functionPointer);");
sb.AppendLine(" };");
return sb.ToString();

Просмотреть файл

@ -154,6 +154,7 @@ namespace Internal.IL
foreach (var v in localVariables)
{
LocalVariable modifiedLocal = v;
modifiedLocal.Name = _compilation.NameMangler.SanitizeName(modifiedLocal.Name);
if (!names.Add(v.Name))
{
modifiedLocal.Name = string.Format("{0}_local{1}", v.Name, v.Slot);
@ -1421,7 +1422,7 @@ namespace Internal.IL
{
Append(thisPtr.Value.Name);
Append(".");
Append(field.Name);
Append(_writer.GetCppFieldName(field));
}
else
{
@ -1430,7 +1431,7 @@ namespace Internal.IL
Append("*)");
Append(thisPtr.Value.Name);
Append(")->");
Append(field.Name);
Append(_writer.GetCppFieldName(field));
}
Finish();
@ -1481,7 +1482,7 @@ namespace Internal.IL
Append("*)");
Append(thisPtr.Value.Name);
Append(")->");
Append(field.Name);
Append(_writer.GetCppFieldName(field));
}
Finish();
@ -1529,7 +1530,7 @@ namespace Internal.IL
Append("*)");
Append(thisPtr.Value.Name);
Append(")->");
Append(field.Name);
Append(_writer.GetCppFieldName(field));
}
Append("=");
if (!fieldType.IsValueType)