diff --git a/src/Generator/Passes/RenamePass.cs b/src/Generator/Passes/RenamePass.cs index 70953f0e..fa180548 100644 --- a/src/Generator/Passes/RenamePass.cs +++ b/src/Generator/Passes/RenamePass.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Text; using System.Text.RegularExpressions; using CppSharp.AST; @@ -27,8 +29,12 @@ namespace CppSharp.Passes { if (decl is Class) return true; if (decl is Field) return true; - if (decl is Method) return true; - if (decl is Function) return true; + var function = decl as Function; + if (function != null) + { + // special case the IDisposable.Dispose that could be added later + return !function.IsOperator && function.Name != "dispose"; + } if (decl is Parameter) return true; if (decl is Enumeration) return true; if (decl is Property) return true; @@ -36,6 +42,23 @@ namespace CppSharp.Passes return false; } + public override bool VisitClassDecl(Class @class) + { + if (@class.IsDynamic) + { + // HACK: entries in v-tables are not shared (as objects) with the virtual methods they represent; + // this is why this pass has to rename entries in the v-table as well; + // this should be fixed in the parser: it should reuse method objects + foreach (var method in VTables.GatherVTableMethodEntries(@class).Where( + e => e.Method != null && IsRenameableDecl(e.Method)).Select(e => e.Method)) + { + Rename(method); + } + } + + return base.VisitClassDecl(@class); + } + public override bool VisitDeclaration(Declaration decl) { if (!IsRenameableDecl(decl)) @@ -49,16 +72,28 @@ namespace CppSharp.Passes if (decl.Name == null) return true; + return Rename(decl); + } + + private bool Rename(Declaration decl) + { string newName; - if (Rename(decl.Name, out newName)) - { + if (Rename(decl.Name, out newName) && AreThereConflicts(decl, newName)) decl.Name = newName; - return true; - } - return true; } + private static bool AreThereConflicts(Declaration decl, string newName) + { + List declarations = new List(); + declarations.AddRange(decl.Namespace.Classes); + declarations.AddRange(decl.Namespace.Enums); + declarations.AddRange(decl.Namespace.Events); + declarations.AddRange(decl.Namespace.Functions); + declarations.AddRange(decl.Namespace.Variables); + return declarations.All(d => d == decl || d.Name != newName); + } + public override bool VisitEnumItem(Enumeration.Item item) { if (!Targets.HasFlag(RenameTargets.EnumItem)) @@ -82,6 +117,14 @@ namespace CppSharp.Passes return base.VisitFieldDecl(field); } + public override bool VisitProperty(Property property) + { + if (!Targets.HasFlag(RenameTargets.Property)) + return false; + + return base.VisitProperty(property); + } + public override bool VisitMethodDecl(Method method) { if (!Targets.HasFlag(RenameTargets.Method)) @@ -129,7 +172,8 @@ namespace CppSharp.Passes Enum = 1 << 5, EnumItem = 1 << 6, Event = 1 << 7, - Any = Function | Method | Parameter | Class | Field | Enum | EnumItem | Event, + Property = 1 << 8, + Any = Function | Method | Parameter | Class | Field | Enum | EnumItem | Event | Property, } /// diff --git a/tests/VTables/VTables.Tests.cs b/tests/VTables/VTables.Tests.cs index 67bc3d15..6e932713 100644 --- a/tests/VTables/VTables.Tests.cs +++ b/tests/VTables/VTables.Tests.cs @@ -4,7 +4,7 @@ using VTables; public class FooDerived : Foo { - public override int Vfoo() + public override int vfoo() { Console.WriteLine("Hello from FooDerived"); return 10; @@ -18,7 +18,7 @@ public class VTablesTests public void TestFoo() { var foo = new Foo(); - Assert.That(foo.Vfoo(), Is.EqualTo(5)); + Assert.That(foo.vfoo(), Is.EqualTo(5)); Assert.That(foo.Vbar(), Is.EqualTo(3)); Assert.That(foo.CallFoo(), Is.EqualTo(7)); diff --git a/tests/VTables/VTables.h b/tests/VTables/VTables.h index 1d66f211..822108a7 100644 --- a/tests/VTables/VTables.h +++ b/tests/VTables/VTables.h @@ -7,6 +7,10 @@ class DLL_API Foo { public: + class Vfoo + { + + }; Foo(); virtual int vfoo();