From 4b84d1fe0fd25ebf148218fa18971bdfd591f0c7 Mon Sep 17 00:00:00 2001
From: Tristan Gingold <tgingold@free.fr>
Date: Wed, 20 Jul 2022 19:37:51 +0200
Subject: elab-vhdl_context: add iterator for top-level packages

---
 src/synth/elab-vhdl_context.adb | 22 ++++++++++++++++++++++
 src/synth/elab-vhdl_context.ads | 14 ++++++++++++++
 2 files changed, 36 insertions(+)

(limited to 'src')

diff --git a/src/synth/elab-vhdl_context.adb b/src/synth/elab-vhdl_context.adb
index 95b9ddf29..048ac1ae4 100644
--- a/src/synth/elab-vhdl_context.adb
+++ b/src/synth/elab-vhdl_context.adb
@@ -602,4 +602,26 @@ package body Elab.Vhdl_Context is
    begin
       return Syn_Inst.Caller;
    end Get_Caller_Instance;
+
+   procedure Iterate_Top_Level (It : in out Iterator_Top_Level_Type;
+                                Res : out Synth_Instance_Acc)
+   is
+      Obj : Obj_Type;
+   begin
+      loop
+         if It.Next_Idx > Root_Instance.Max_Objs then
+            Res := null;
+            exit;
+         end if;
+
+         Obj := Root_Instance.Objects (It.Next_Idx);
+         It.Next_Idx := It.Next_Idx + 1;
+
+         if Obj.Kind = Obj_Instance then
+            Res := Obj.I_Inst;
+            return;
+         end if;
+      end loop;
+   end Iterate_Top_Level;
+
 end Elab.Vhdl_Context;
diff --git a/src/synth/elab-vhdl_context.ads b/src/synth/elab-vhdl_context.ads
index 0bf2a4b50..404325742 100644
--- a/src/synth/elab-vhdl_context.ads
+++ b/src/synth/elab-vhdl_context.ads
@@ -178,6 +178,13 @@ package Elab.Vhdl_Context is
                                   Caller : Synth_Instance_Acc);
    function Get_Caller_Instance (Syn_Inst : Synth_Instance_Acc)
                                 return Synth_Instance_Acc;
+
+   --  Iterator over top-level packages.
+   type Iterator_Top_Level_Type is private;
+   Iterator_Top_Level_Init : constant Iterator_Top_Level_Type;
+
+   procedure Iterate_Top_Level (It : in out Iterator_Top_Level_Type;
+                                Res : out Synth_Instance_Acc);
 private
    type Destroy_Type is record
       Inst : Synth_Instance_Acc;
@@ -252,4 +259,11 @@ private
       --  Instance for synthesis.
       Objects : Objects_Array (1 .. Max_Objs);
    end record;
+
+   type Iterator_Top_Level_Type is record
+      Next_Idx : Object_Slot_Type;
+   end record;
+
+   Iterator_Top_Level_Init : constant Iterator_Top_Level_Type :=
+     (Next_Idx => 1);
 end Elab.Vhdl_Context;
-- 
cgit v1.2.3