From 3b7199f61c70a178a7f16f47393e9cb36e9eb425 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Tue, 7 Mar 2017 15:30:59 +0000 Subject: [PATCH] Refactor all renaming pass declaration checking into IsRenameableDecl. --- src/Generator/Passes/RenamePass.cs | 142 ++++++++++------------------- 1 file changed, 48 insertions(+), 94 deletions(-) diff --git a/src/Generator/Passes/RenamePass.cs b/src/Generator/Passes/RenamePass.cs index 94c8d554..626b1c46 100644 --- a/src/Generator/Passes/RenamePass.cs +++ b/src/Generator/Passes/RenamePass.cs @@ -74,29 +74,59 @@ namespace CppSharp.Passes public bool IsRenameableDecl(Declaration decl) { - if (decl is Class) return true; + if (decl is Class) + return true; + + var method = decl as Method; + if (method != null) + { + return Targets.HasFlag(RenameTargets.Method) && + method.Kind == CXXMethodKind.Normal; + } + var function = decl as Function; if (function != null) { - // special case the IDisposable.Dispose that could be added later - return !function.IsOperator && function.Name != "dispose"; + // Special case the IDisposable.Dispose method. + return Targets.HasFlag(RenameTargets.Function) && + (!function.IsOperator && function.Name != "dispose"); } - if (decl is Parameter) return true; - if (decl is Enumeration) return true; + + if (decl is Parameter) + return Targets.HasFlag(RenameTargets.Parameter); + + if (decl is Enumeration.Item) + return Targets.HasFlag(RenameTargets.EnumItem); + + if (decl is Enumeration) + return Targets.HasFlag(RenameTargets.Enum); + var property = decl as Property; - if (property != null) return !property.IsIndexer; - if (decl is Event) return true; - if (decl is TypedefDecl) return true; + if (property != null) + return Targets.HasFlag(RenameTargets.Property) && !property.IsIndexer; + + if (decl is Event) + return Targets.HasFlag(RenameTargets.Event); + + if (decl is TypedefDecl) + return Targets.HasFlag(RenameTargets.Delegate); + if (decl is Namespace && !(decl is TranslationUnit)) return true; - if (decl is Variable) return true; + + if (decl is Variable) + return Targets.HasFlag(RenameTargets.Variable); + var field = decl as Field; if (field != null) { + if (!Targets.HasFlag(RenameTargets.Field)) + return false; var fieldProperty = ((Class) field.Namespace).Properties.FirstOrDefault( p => p.Field == field); return (fieldProperty != null && fieldProperty.IsInRefTypeAndBackedByValueClassField()); } + return false; } @@ -119,6 +149,7 @@ namespace CppSharp.Passes string newName; if (Rename(decl, out newName) && !AreThereConflicts(decl, newName)) decl.Name = newName; + return true; } @@ -128,6 +159,7 @@ namespace CppSharp.Passes declarations.AddRange(decl.Namespace.Classes.Where(c => !c.IsIncomplete)); declarations.AddRange(decl.Namespace.Enums); declarations.AddRange(decl.Namespace.Events); + var function = decl as Function; if (function != null && function.SynthKind != FunctionSynthKind.AdjustedMethod) { @@ -137,11 +169,13 @@ namespace CppSharp.Passes } else declarations.AddRange(decl.Namespace.Functions); + declarations.AddRange(decl.Namespace.Variables); declarations.AddRange(from typedefDecl in decl.Namespace.Typedefs let pointerType = typedefDecl.Type.Desugar() as PointerType where pointerType != null && pointerType.GetFinalPointee() is FunctionType select typedefDecl); + var specialization = decl as ClassTemplateSpecialization; if (specialization != null) declarations.RemoveAll(d => specialization.TemplatedDecl.TemplatedDecl == d); @@ -177,21 +211,6 @@ namespace CppSharp.Passes f => !f.Ignore && f.Parameters.SequenceEqual(function.Parameters, new ParameterComparer())); } - public override bool VisitEnumItemDecl(Enumeration.Item item) - { - if (!Targets.HasFlag(RenameTargets.EnumItem)) - return false; - - string newName; - if (Rename(item, out newName)) - { - item.Name = newName; - return true; - } - - return true; - } - public override bool VisitClassDecl(Class @class) { var result = base.VisitClassDecl(@class); @@ -204,73 +223,6 @@ namespace CppSharp.Passes return result; } - - public override bool VisitFieldDecl(Field field) - { - if (!Targets.HasFlag(RenameTargets.Field)) - return false; - - return base.VisitFieldDecl(field); - } - - public override bool VisitProperty(Property property) - { - if (!Targets.HasFlag(RenameTargets.Property)) - return false; - - return base.VisitProperty(property); - } - - public override bool VisitTypedefDecl(TypedefDecl typedef) - { - if (!Targets.HasFlag(RenameTargets.Delegate)) - return false; - - return base.VisitTypedefDecl(typedef); - } - - public override bool VisitMethodDecl(Method method) - { - if (!Targets.HasFlag(RenameTargets.Method)) - return false; - - if (method.Kind != CXXMethodKind.Normal) - return false; - - return base.VisitMethodDecl(method); - } - - public override bool VisitFunctionDecl(Function function) - { - if (!Targets.HasFlag(RenameTargets.Function)) - return false; - - return base.VisitFunctionDecl(function); - } - - public override bool VisitParameterDecl(Parameter parameter) - { - if (!Targets.HasFlag(RenameTargets.Parameter)) - return false; - - return base.VisitParameterDecl(parameter); - } - - public override bool VisitEvent(Event @event) - { - if (!Targets.HasFlag(RenameTargets.Event)) - return false; - - return base.VisitEvent(@event); - } - - public override bool VisitVariableDecl(Variable variable) - { - if (!Targets.HasFlag(RenameTargets.Variable)) - return false; - - return base.VisitVariableDecl(variable); - } } [Flags] @@ -313,7 +265,8 @@ namespace CppSharp.Passes public override bool Rename(Declaration decl, out string newName) { - if (base.Rename(decl, out newName)) return true; + if (base.Rename(decl, out newName)) + return true; var replace = Regex.Replace(decl.Name, Pattern, Replacement); @@ -335,7 +288,7 @@ namespace CppSharp.Passes } /// - /// Renames a declaration based on a regular expression pattern. + /// Renames a declaration based on a pre-defined pattern. /// public class CaseRenamePass : RenamePass { @@ -349,7 +302,8 @@ namespace CppSharp.Passes public override bool Rename(Declaration decl, out string newName) { - if (base.Rename(decl, out newName)) return true; + if (base.Rename(decl, out newName)) + return true; newName = ConvertCaseString(decl, Pattern); return true;