--  A copyable reclaimed type (T30): its objects count the copies made of their value, so the
--  output shows where Verada adjusts. A copy of an object is adjusted once (a declaration, an
--  assignment, an aggregate's component, a function's result taken from an object); a call's
--  result taken by an object is not a copy (Ada lets the anonymous result be omitted,
--  RM 7.6(21), which GNAT does not always do: `Right` below is 1 under GNAT).
with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   package Counted is
      type T is private
        with Finalizable => (Adjust => Copied, Finalize => Gone, Relaxed_Finalization => True);
      function Make (Id : Natural) return T;
      function Id (X : T) return Natural;
      function Copies (X : T) return Natural;
      procedure Copied (X : in out T) with Global => null;
      procedure Gone (X : in out T) with Global => null;
   private
      type T is record
         Id : Natural := 0;
         Copies : Natural := 0;
      end record;
      function Make (Id : Natural) return T is (T'(Id => Id, Copies => 0));
      function Id (X : T) return Natural is (X.Id);
      function Copies (X : T) return Natural is (X.Copies);
   end Counted;

   package body Counted is
      procedure Copied (X : in out T) is
      begin
         if X.Copies < Natural'Last then
            X.Copies := X.Copies + 1;
         end if;
      end Copied;

      procedure Gone (X : in out T) is
      begin
         X.Copies := 0;
      end Gone;
   end Counted;
   use Counted;

   type Pair is record
      Left, Right : T;
   end record;

   function Same (X : T) return T is
      Y : constant T := X;
   begin
      return Y;
   end Same;

   A : constant T := Make (1);
   B : T := A;
   P : Pair := (Left => A, Right => Make (2));
begin
   Put_Line (Natural'Image (Copies (A)) & Natural'Image (Copies (B)));
   B := A;
   Put_Line (Natural'Image (Copies (B)));
   Put_Line (Natural'Image (Copies (P.Left)) & Natural'Image (Copies (P.Right)));
   P.Right := B;
   Put_Line (Natural'Image (Id (P.Right)) & Natural'Image (Copies (P.Right)));
   Put_Line (Natural'Image (Id (Same (A))) & Natural'Image (Copies (Same (A))));
end Main;
