From 0595fcfc9ec8a7a6fd8bf1c58eb58ddc5c154ca7 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Sun, 21 Feb 2021 00:19:16 +0000 Subject: [PATCH] Add MarkEventsWithUniqueIdPass pass for global event handling. --- src/Generator/Driver.cs | 2 ++ .../Passes/MarkEventsWithUniqueIdPass.cs | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/Generator/Passes/MarkEventsWithUniqueIdPass.cs diff --git a/src/Generator/Driver.cs b/src/Generator/Driver.cs index b9ed0393..f4b04b21 100644 --- a/src/Generator/Driver.cs +++ b/src/Generator/Driver.cs @@ -291,6 +291,8 @@ namespace CppSharp } Context.TranslationUnitPasses.AddPass(new HandleVariableInitializerPass()); + + TranslationUnitPasses.AddPass(new MarkEventsWithUniqueIdPass()); } public void ProcessCode() diff --git a/src/Generator/Passes/MarkEventsWithUniqueIdPass.cs b/src/Generator/Passes/MarkEventsWithUniqueIdPass.cs new file mode 100644 index 00000000..3132a36e --- /dev/null +++ b/src/Generator/Passes/MarkEventsWithUniqueIdPass.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using CppSharp.AST; +using CppSharp.AST.Extensions; +using CppSharp.Passes; +using Type = CppSharp.AST.Type; + +namespace CppSharp +{ + /// + /// This pass sets each event in the AST with a global unique ID. + /// + public class MarkEventsWithUniqueIdPass : TranslationUnitPass + { + private int eventId = 1; + public Dictionary EventIds = new Dictionary(); + + public override bool VisitEvent(Event @event) + { + if (AlreadyVisited(@event)) + return true; + + if (@event.GlobalId != 0) + throw new NotSupportedException(); + + @event.GlobalId = eventId++; + return base.VisitEvent(@event); + } + } +} \ No newline at end of file