From bf38d80af888d6e749fdbe53f1cd1bdcd48a7f11 Mon Sep 17 00:00:00 2001 From: triton Date: Sun, 10 Mar 2013 00:51:15 +0000 Subject: [PATCH] Update the existing passes to use the new pass interface. --- src/Generator/Passes/CheckFlagEnumsPass.cs | 14 +- .../Passes/CleanInvalidDeclNamesPass.cs | 119 ++------------ .../Passes/DuplicatedNamesCheckerPass.cs | 22 +-- .../Passes/FunctionToInstanceMethodPass.cs | 2 +- .../Passes/FunctionToStaticMethodPass.cs | 6 +- src/Generator/Passes/RenamePass.cs | 152 +++++++----------- .../Passes/ResolveIncompleteDeclsPass.cs | 32 +--- src/Generator/Passes/SortDeclarationsPass.cs | 2 +- 8 files changed, 103 insertions(+), 246 deletions(-) diff --git a/src/Generator/Passes/CheckFlagEnumsPass.cs b/src/Generator/Passes/CheckFlagEnumsPass.cs index f77a82b3..a067497d 100644 --- a/src/Generator/Passes/CheckFlagEnumsPass.cs +++ b/src/Generator/Passes/CheckFlagEnumsPass.cs @@ -9,19 +9,19 @@ namespace Cxxi.Passes // If the enumeration only has power of two values, assume it's // a flags enum. - bool isFlags = true; - bool hasBigRange = false; + var isFlags = true; + var hasBigRange = false; foreach (var item in @enum.Items) { - if (item.Name.Length >= 1 && Char.IsDigit(item.Name[0])) - item.Name = String.Format("_{0}", item.Name); + var value = item.Value; - long value = item.Value; if (value >= 4) hasBigRange = true; + if (value <= 1 || value.IsPowerOfTwo()) continue; + isFlags = false; } @@ -31,7 +31,7 @@ namespace Cxxi.Passes return isFlags && hasBigRange; } - public override bool ProcessEnum(Enumeration @enum) + public override bool VisitEnumDecl(Enumeration @enum) { if (IsFlagEnum(@enum)) { @@ -39,7 +39,7 @@ namespace Cxxi.Passes return true; } - return false; + return base.VisitEnumDecl(@enum); } } diff --git a/src/Generator/Passes/CleanInvalidDeclNamesPass.cs b/src/Generator/Passes/CleanInvalidDeclNamesPass.cs index b1bc3840..821ef050 100644 --- a/src/Generator/Passes/CleanInvalidDeclNamesPass.cs +++ b/src/Generator/Passes/CleanInvalidDeclNamesPass.cs @@ -14,31 +14,12 @@ namespace Cxxi.Passes typeRefs = new TypeRefsVisitor(); } - public override bool ProcessUnit(TranslationUnit unit) + public override bool VisitTranslationUnit(TranslationUnit unit) { - if (unit.Ignore) - return false; - - if (unit.IsSystemHeader) - return false; - typeRefs = new TypeRefsVisitor(); - ProcessNamespace(unit); - unit.TypeReferences = typeRefs; - return true; - } - - private void ProcessNamespace(Namespace @namespace) - { - ProcessEnums(@namespace.Enums); - ProcessFunctions(@namespace.Functions); - ProcessClasses(@namespace.Classes); - ProcessTypedefs(@namespace, @namespace.Typedefs); - - foreach (var inner in @namespace.Namespaces) - ProcessNamespace(inner); + return base.VisitTranslationUnit(unit); } string CheckName(string name) @@ -56,90 +37,29 @@ namespace Cxxi.Passes return name; } - public override bool ProcessDeclaration(Declaration decl) + public override bool VisitDeclaration(Declaration decl) { typeRefs.Process(decl); decl.Name = CheckName(decl.Name); StringHelpers.CleanupText(ref decl.DebugText); - return true; - } - - private void ProcessDeclarations(IEnumerable decls) - where T : Declaration - { - foreach (T decl in decls) - ProcessDeclaration(decl); - } - - private void ProcessClasses(List classes) - { - ProcessDeclarations(classes); - - foreach (var @class in classes) - { - ProcessFields(@class.Fields); - ProcessMethods(@class.Methods); - } - } - - private void ProcessFields(List fields) - { - ProcessDeclarations(fields); - - foreach (var field in fields) - ProcessField(field); + return base.VisitDeclaration(decl); } - private void ProcessMethods(List methods) + public override bool VisitTypedefDecl(TypedefDecl typedef) { - ProcessDeclarations(methods); + var @class = typedef.Namespace.FindClass(typedef.Name); - foreach (var method in methods) - ProcessFunction(method); - } - - private void ProcessFunctions(List functions) - { - ProcessDeclarations(functions); - - foreach (var function in functions) - ProcessFunction(function); - } - - public override bool ProcessFunction(Function function) - { - foreach (var param in function.Parameters) - ProcessDeclaration(param); - - return true; - } + // Clang will walk the typedef'd tag decl and the typedef decl, + // so we ignore the class and process just the typedef. - private void ProcessTypedefs(Namespace @namespace, List typedefs) - { - ProcessDeclarations(typedefs); - - foreach (var typedef in typedefs) - { - var @class = @namespace.FindClass(typedef.Name); - - // Clang will walk the typedef'd tag decl and the typedef decl, - // so we ignore the class and process just the typedef. + if (@class != null) + typedef.ExplicityIgnored = true; - if (@class != null) - typedef.ExplicityIgnored = true; + if (typedef.Type == null) + typedef.ExplicityIgnored = true; - if (typedef.Type == null) - typedef.ExplicityIgnored = true; - } - } - - public void ProcessEnums(List enumerations) - { - ProcessDeclarations(enumerations); - - foreach (var @enum in enumerations) - ProcessEnum(@enum); + return base.VisitTypedefDecl(typedef); } private static void CheckEnumName(Enumeration @enum) @@ -161,21 +81,16 @@ namespace Cxxi.Passes @enum.Name = prefix; } - public override bool ProcessEnum(Enumeration @enum) + public override bool VisitEnumDecl(Enumeration @enum) { CheckEnumName(@enum); - var result = base.ProcessEnum(@enum); - - foreach (var item in @enum.Items) - ProcessEnumItem(item); - - return result; + return base.VisitEnumDecl(@enum); } - public override bool ProcessEnumItem(Enumeration.Item item) + public override bool VisitEnumItem(Enumeration.Item item) { item.Name = CheckName(item.Name); - return true; + return base.VisitEnumItem(item); } } diff --git a/src/Generator/Passes/DuplicatedNamesCheckerPass.cs b/src/Generator/Passes/DuplicatedNamesCheckerPass.cs index cd409ab5..7479133b 100644 --- a/src/Generator/Passes/DuplicatedNamesCheckerPass.cs +++ b/src/Generator/Passes/DuplicatedNamesCheckerPass.cs @@ -12,30 +12,18 @@ namespace Cxxi.Passes names = new Dictionary(); } - public override bool ProcessClass(Class @class) + public override bool VisitClassDecl(Class @class) { if (@class.Ignore) return false; names.Clear(); - - foreach (var baseClass in @class.Bases) - if (baseClass.IsClass) - ProcessClass(baseClass.Class); - - CheckDuplicates(@class.Fields); - CheckDuplicates(@class.Methods); - CheckDuplicates(@class.Properties); - - return true; + return base.VisitClassDecl(@class); } - void CheckDuplicates(IEnumerable decls) + public override bool VisitDeclaration(Declaration decl) { - foreach (var decl in decls) - { - if (decl.Ignore) continue; - CheckDuplicate(decl); - } + CheckDuplicate(decl); + return base.VisitDeclaration(decl); } void CheckDuplicate(Declaration decl) diff --git a/src/Generator/Passes/FunctionToInstanceMethodPass.cs b/src/Generator/Passes/FunctionToInstanceMethodPass.cs index e1456082..d555f7e2 100644 --- a/src/Generator/Passes/FunctionToInstanceMethodPass.cs +++ b/src/Generator/Passes/FunctionToInstanceMethodPass.cs @@ -11,7 +11,7 @@ namespace Cxxi.Passes /// /// Processes a function declaration. /// - public override bool ProcessFunction(Function function) + public override bool VisitFunctionDecl(Function function) { if (function.Ignore) return false; diff --git a/src/Generator/Passes/FunctionToStaticMethodPass.cs b/src/Generator/Passes/FunctionToStaticMethodPass.cs index 86ec8018..8931ff23 100644 --- a/src/Generator/Passes/FunctionToStaticMethodPass.cs +++ b/src/Generator/Passes/FunctionToStaticMethodPass.cs @@ -12,7 +12,7 @@ namespace Cxxi.Passes /// /// Processes a function declaration. /// - public override bool ProcessFunction(Function function) + public override bool VisitFunctionDecl(Function function) { if (function.Ignore) return false; @@ -26,14 +26,14 @@ namespace Cxxi.Passes return false; // Clean up the name of the function now that it will be a static method. - function.Name = function.Name.Substring(@class.Name.Length); + var name = function.Name.Substring(@class.Name.Length); function.ExplicityIgnored = true; // Create a new fake method so it acts as a static method. var method = new Method() { Namespace = @class.Namespace, - Name = function.Name, + Name = name, OriginalName = function.OriginalName, Access = AccessSpecifier.Public, Kind = CXXMethodKind.Normal, diff --git a/src/Generator/Passes/RenamePass.cs b/src/Generator/Passes/RenamePass.cs index f27b762b..c9e8d928 100644 --- a/src/Generator/Passes/RenamePass.cs +++ b/src/Generator/Passes/RenamePass.cs @@ -19,6 +19,57 @@ namespace Cxxi.Passes { Targets = targets; } + + public abstract bool Rename(string name, out string newName); + + public override bool VisitDeclaration(Declaration decl) + { + if (!Targets.HasFlag(RenameTargets.Any)) + return true; + + string newName; + if (Rename(decl.Name, out newName)) + { + decl.Name = newName; + return true; + } + + return true; + } + + public override bool VisitEnumItem(Enumeration.Item item) + { + if (!Targets.HasFlag(RenameTargets.EnumItem)) + return false; + + string newName; + if (Rename(item.Name, out newName)) + { + item.Name = newName; + return true; + } + + return true; + } + + public override bool VisitFieldDecl(Field field) + { + if (!Targets.HasFlag(RenameTargets.Field)) + return false; + + return base.VisitFieldDecl(field); + } + + public override bool VisitMethodDecl(Method method) + { + if (!Targets.HasFlag(RenameTargets.Method)) + return false; + + if (method.Kind != CXXMethodKind.Normal) + return false; + + return base.VisitMethodDecl(method); + } } [Flags] @@ -54,52 +105,7 @@ namespace Cxxi.Passes Targets = targets; } - public override bool ProcessDeclaration(Declaration decl) - { - if (!Targets.HasFlag(RenameTargets.Any)) - return false; - - string newName; - if (Rename(decl.Name, out newName)) - { - decl.Name = newName; - return true; - } - - return false; - } - - public override bool ProcessEnumItem(Enumeration.Item item) - { - if (!Targets.HasFlag(RenameTargets.EnumItem)) - return false; - - string newName; - if (Rename(item.Name, out newName)) - { - item.Name = newName; - return true; - } - - return false; - } - - public override bool ProcessField(Field field) - { - if (!Targets.HasFlag(RenameTargets.Field)) - return false; - - string newName; - if (Rename(field.Name, out newName)) - { - field.Name = newName; - return true; - } - - return false; - } - - bool Rename(string name, out string newName) + public override bool Rename(string name, out string newName) { var replace = Regex.Replace(name, Pattern, Replacement); @@ -133,59 +139,23 @@ namespace Cxxi.Passes Pattern = pattern; } - private void Rename(ref T decl) where T : Declaration + public override bool Rename(string name, out string newName) { + newName = null; + switch (Pattern) { - case RenameCasePattern.LowerCamelCase: - decl.Name = ConvertCaseString(decl.Name, RenameCasePattern.LowerCamelCase); - break; - case RenameCasePattern.UpperCamelCase: - decl.Name = ConvertCaseString(decl.Name, RenameCasePattern.UpperCamelCase); - break; + case RenameCasePattern.LowerCamelCase: + newName = ConvertCaseString(name, RenameCasePattern.LowerCamelCase); + return true; + case RenameCasePattern.UpperCamelCase: + newName = ConvertCaseString(name, RenameCasePattern.UpperCamelCase); + return true; } - } - - public override bool ProcessDeclaration(Declaration decl) - { - if (!Targets.HasFlag(RenameTargets.Any)) - return false; - - Rename(ref decl); - return true; - } - - public override bool ProcessFunction(Function function) - { - if (!Targets.HasFlag(RenameTargets.Function)) - return false; - - Rename(ref function); - return true; - } - - public override bool ProcessField(Field field) - { - if (!Targets.HasFlag(RenameTargets.Field)) - return false; - - Rename(ref field); return false; } - public override bool ProcessMethod(Method method) - { - if (!Targets.HasFlag(RenameTargets.Method)) - return false; - - if (method.Kind != CXXMethodKind.Normal) - return false; - - Rename(ref method); - return true; - } - /// /// Converts the phrase to specified convention. /// diff --git a/src/Generator/Passes/ResolveIncompleteDeclsPass.cs b/src/Generator/Passes/ResolveIncompleteDeclsPass.cs index f59919d3..e42a0855 100644 --- a/src/Generator/Passes/ResolveIncompleteDeclsPass.cs +++ b/src/Generator/Passes/ResolveIncompleteDeclsPass.cs @@ -12,35 +12,26 @@ namespace Cxxi.Passes typeMapDatabase = database; } - public override bool ProcessClass(Class @class) + public override bool VisitClassDecl(Class @class) { if (@class.Ignore) - return true; + return false; if (!@class.IsIncomplete) - return true; + return false; if (@class.CompleteDeclaration != null) - return true; + return false; @class.CompleteDeclaration = Library.FindCompleteClass(@class.Name); if (@class.CompleteDeclaration == null) Console.WriteLine("Unresolved declaration: {0}", @class.Name); - foreach (var field in @class.Fields) - ProcessField(field); - - foreach (var method in @class.Methods) - ProcessMethod(method); - - //foreach (var prop in @class.Properties) - // ProcessProperty(prop); - - return true; + return base.VisitClassDecl(@class); } - public override bool ProcessField(Field field) + public override bool VisitFieldDecl(Field field) { var type = field.Type; @@ -56,7 +47,7 @@ namespace Cxxi.Passes return true; } - public override bool ProcessFunction(Function function) + public override bool VisitFunctionDecl(Function function) { var ret = function.ReturnType; @@ -76,19 +67,12 @@ namespace Cxxi.Passes Console.WriteLine("Function '{0}' was ignored due to {1} param", function.Name, msg); } - - ProcessDeclaration(param); } return true; } - public override bool ProcessMethod(Method method) - { - return ProcessFunction(method); - } - - public override bool ProcessTypedef(TypedefDecl typedef) + public override bool VisitTypedefDecl(TypedefDecl typedef) { string msg; if (HasInvalidType(typedef.Type, out msg)) diff --git a/src/Generator/Passes/SortDeclarationsPass.cs b/src/Generator/Passes/SortDeclarationsPass.cs index f0b7b8ab..4f3d0262 100644 --- a/src/Generator/Passes/SortDeclarationsPass.cs +++ b/src/Generator/Passes/SortDeclarationsPass.cs @@ -18,7 +18,7 @@ namespace Cxxi.Passes SortDeclarations(childNamespace); } - public override bool ProcessUnit(TranslationUnit unit) + public override bool VisitTranslationUnit(TranslationUnit unit) { SortDeclarations(unit); return true;