--  Ada.Exceptions (RM 11.4.1), the part Verada keeps: the name, message and information of
--  the occurrence a handler names (`when X : E`), and its raising again. An occurrence is a
--  copy, taken when the handler starts, of the exception's full name and the message it was
--  raised with. Only memory limits them (Verada.Storage).
with Verada.Storage;

package Ada.Exceptions is
   type Exception_Occurrence is limited private;

   --  the exception's full name, `PACKAGE.NAME` in upper case
   function Exception_Name (X : Exception_Occurrence) return String
     with Global => null,
          Post => Exception_Name'Result'Length < Verada.Storage.Length_Bound;

   --  the message of `raise E with "..."`, whole, or ""
   function Exception_Message (X : Exception_Occurrence) return String
     with Global => null,
          Post => Exception_Message'Result'Length < Verada.Storage.Length_Bound;

   --  the name and the message together: `NAME`, or `NAME: message` when there is a message
   function Exception_Information (X : Exception_Occurrence) return String
     with Global => null,
          Post => Exception_Information'Result'Length <= 2 * Verada.Storage.Length_Bound;

   --  the occurrence X raised again: only X of the handler that names it (`when X : E`), where
   --  it is `raise;`, so the subprogram's Exceptional_Cases cover the handler's exceptions as
   --  for any raise (another occurrence is left out: its exception is not known to the proof)
   procedure Reraise_Occurrence (X : Exception_Occurrence)
     with Import, Convention => Intrinsic, Global => null;

   --  (Verada) the occurrence being handled: what `when X : E` names
   function Current_Occurrence return Exception_Occurrence
     with Global => null;
private
   type String_Access is access String;

   --  the name and the message on the heap (null when empty), freed with the occurrence
   type Exception_Occurrence is limited record
      Name : String_Access;
      Message : String_Access;
   end record
     with Finalizable => (Finalize => Free, Relaxed_Finalization => True),
          Type_Invariant =>
            (Exception_Occurrence.Name = null or else Exception_Occurrence.Name'Length < Verada.Storage.Length_Bound)
            and then (Exception_Occurrence.Message = null
                      or else Exception_Occurrence.Message'Length < Verada.Storage.Length_Bound);

   procedure Free (X : in out Exception_Occurrence)
     with Global => null;

   function Exception_Name (X : Exception_Occurrence) return String is
     (if X.Name = null then "" else X.Name.all);

   function Exception_Message (X : Exception_Occurrence) return String is
     (if X.Message = null then "" else X.Message.all);

   --  (the run time's copies are in memory)
   function Name_Length return Natural
     with Import, Convention => Ada, External_Name => "verada_rt_exception_name_length",
          Global => null,
          Post => Name_Length'Result < Verada.Storage.Length_Bound;

   function Message_Length return Natural
     with Import, Convention => Ada, External_Name => "verada_rt_exception_message_length",
          Global => null,
          Post => Message_Length'Result < Verada.Storage.Length_Bound;

   procedure Read_Name (Item : out String; Count : out Natural)
     with Import, Convention => Ada, External_Name => "verada_rt_exception_name",
          Global => null,
          Post => Count <= Item'Length;

   procedure Read_Message (Item : out String; Count : out Natural)
     with Import, Convention => Ada, External_Name => "verada_rt_exception_message",
          Global => null,
          Post => Count <= Item'Length;
end Ada.Exceptions;
