From 8427ff8e924a5134155700f6476a4246468f6b9f Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Wed, 21 Nov 2018 01:22:24 +0200 Subject: [PATCH] Optimized the walking of the managed AST. Signed-off-by: Dimitar Dobrev --- src/AST/ClassExtensions.cs | 4 +- src/AST/DeclIterator.cs | 67 -------- src/AST/DeclarationsList.cs | 145 ++++++++++++++++++ src/AST/Namespace.cs | 74 +++------ src/Generator.Tests/AST/TestAST.cs | 4 +- src/Generator/Driver.cs | 3 +- src/Generator/Library.cs | 2 +- .../GenerateAbstractImplementationsPass.cs | 4 +- .../Passes/HandleDefaultParamValuesPass.cs | 2 +- .../Passes/MarkUsedClassInternalsPass.cs | 3 +- .../Passes/MultipleInheritancePass.cs | 2 +- .../Passes/ResolveIncompleteDeclsPass.cs | 2 +- src/Generator/Passes/SortDeclarationsPass.cs | 18 --- src/Parser/ASTConverter.cs | 16 +- 14 files changed, 191 insertions(+), 155 deletions(-) delete mode 100644 src/AST/DeclIterator.cs create mode 100644 src/AST/DeclarationsList.cs delete mode 100644 src/Generator/Passes/SortDeclarationsPass.cs diff --git a/src/AST/ClassExtensions.cs b/src/AST/ClassExtensions.cs index 76ad2c81..a3eedbbc 100644 --- a/src/AST/ClassExtensions.cs +++ b/src/AST/ClassExtensions.cs @@ -184,13 +184,13 @@ namespace CppSharp.AST Class @interface = null; if (specialization == null) { - @interface = @class.Namespace.Classes.Find( + @interface = @class.Namespace.Classes.FirstOrDefault( c => c.OriginalClass == @class && c.IsInterface); } else { Class template = specialization.TemplatedDecl.TemplatedClass; - Class templatedInterface = @class.Namespace.Classes.Find( + Class templatedInterface = @class.Namespace.Classes.FirstOrDefault( c => c.OriginalClass == template && c.IsInterface); if (templatedInterface != null) @interface = templatedInterface.Specializations.FirstOrDefault( diff --git a/src/AST/DeclIterator.cs b/src/AST/DeclIterator.cs deleted file mode 100644 index c94584f9..00000000 --- a/src/AST/DeclIterator.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; - -namespace CppSharp.AST -{ - public struct DeclIterator : IEnumerable where T : Declaration - { - private readonly List Declarations; - - public DeclIterator(List declarations) - { - Declarations = declarations; - } - - public int Count - { - get { return Declarations.OfType().ToArray().Length; } - } - - public T this[int index] - { - get { return Declarations.OfType().ToArray()[index]; } - } - - public void Add(T declaration) - { - Declarations.Add(declaration); - } - - public void AddRange(IEnumerable range) - { - Declarations.AddRange(range); - } - - public T Find(Func predicate) - { - return Declarations.OfType().SingleOrDefault(predicate); - } - - public int FindIndex(Predicate predicate) - { - return Declarations.OfType().ToList().FindIndex(predicate); - } - - public bool Exists(Func predicate) - { - return Declarations.OfType().Any(predicate); - } - - public IEnumerator GetEnumerator() - { - return Declarations.OfType().GetEnumerator(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - - public void Replace(T decl, T newDecl) - { - Declarations[Declarations.FindIndex(d => d == decl)] = newDecl; - } - } -} diff --git a/src/AST/DeclarationsList.cs b/src/AST/DeclarationsList.cs new file mode 100644 index 00000000..3e22c228 --- /dev/null +++ b/src/AST/DeclarationsList.cs @@ -0,0 +1,145 @@ +using System.Collections.Generic; +using System.Collections.ObjectModel; + +namespace CppSharp.AST +{ + public class DeclarationsList : ObservableCollection + { + public IEnumerable Namespaces => OfType(Kind.Namespace); + + public IEnumerable Enums => OfType(Kind.Enum); + + public IEnumerable Functions => OfType(Kind.Function); + + public IEnumerable Classes => OfType(Kind.Class); + + public IEnumerable