comp.lang.ada
 help / color / mirror / Atom feed
From: olivermkellogg@gmail.com
Subject: Re: Generic formals and Aspects
Date: Tue, 19 Jul 2016 11:00:18 -0700 (PDT)
Date: 2016-07-19T11:00:18-07:00	[thread overview]
Message-ID: <18aedafc-c66d-4bba-81ce-76dce495f59e@googlegroups.com> (raw)
In-Reply-To: <caca39ba-60af-4f2f-8cf1-e6d53ef45a93@googlegroups.com>

On Tuesday, July 19, 2016 at 6:05:12 PM UTC+2, Shark8 wrote:
> Try the following:
> 
> 
> ------ SPEC ------
>     generic
> 	type Discrete_Type is (<>);  -- CANDIDATE
> 
>     package Big_Endian_Integer_Buffer is
> 	
> 	function Get return Discrete_Type;
> 	procedure Set (Value : Discrete_Type);
> 	
>     Private
> 	type Internal is new Discrete_Type
> 	  with Static_Predicate => Internal'Size in 16 | 32 | 64;
> 	
> 	Size_In_Bytes : constant Positive := Internal'Size / 8;
> 
> 	type Buffer_Type is array (1 .. Size_In_Bytes) of Interfaces.Unsigned_8
> 	  with Component_Size => 8;
> 
> 	Buffer : Buffer_Type := (others => 0);
>     end Big_Endian_Integer_Buffer;
>     
> 
> ------ BODY ------
>     package body Big_Endian_Integer_Buffer is
> 	
> 	function Get return Discrete_Type is
> 	    Result : Discrete_Type
> 	      with Import, Address => Buffer'Address;
> 	begin
> 	    Return Discrete_Type(Result);
> 	End Get;
> 	
> 	procedure Set (Value : Discrete_Type) is
> 	    Temp : Internal
> 	      with Import, Address => Buffer'Address;
> 	begin
> 	    Temp := Internal(Value);
> 	End Set;
> 	
>     end Big_Endian_Integer_Buffer;

Interesting.

Using your modified spec with the following test program:

-- File: beib_test.adb
with Interfaces;
with Big_Endian_Integer_Buffer;

procedure BEIB_Test is

   package Pass is new Big_Endian_Integer_Buffer (Interfaces.Unsigned_64);

   type RGB_T is mod 2 ** 24 with Size => 24;

   package Fail is new Big_Endian_Integer_Buffer (RGB_T);

   P : Interfaces.Unsigned_64;
   F : RGB_T;

begin
   P := Pass.Get;
   F := Fail.Get;
end BEIB_Test;

I get the following message from GNAT:

beib_test.ads:6:04: instantiation error at big_endian_integer_buffer.ads:16
beib_test.ads:6:04: expression is not predicate-static (RM 3.2.4(16-22))
[...]

Line 6 is at the Pass instantiation. (The same error happens at the Fail instantiation.)

Hmm, too bad... I would have preferred getting a compile time error.

That said, after changing Static_Predicate to Dynamic_Predicate in the spec, the test program builds okay.
However, it runs without failure. (I would have expected a failure on the Fail instantiation.)

For completeness, here is the generic body that I am using:

-- File:  big_endian_integer_buffer.adb
with System, Ada.Unchecked_Conversion;

package body Big_Endian_Integer_Buffer is

   use type System.Bit_Order;

   function Get return Discrete_Type is
      function To_Discrete is new Ada.Unchecked_Conversion
        (Buffer_Type, Discrete_Type);
   begin
      if System.Default_Bit_Order = System.High_Order_First then
         return To_Discrete (Buffer);
      end if;
      declare
         Native_Buf : Buffer_Type;
      begin
         for I in 1 .. Size_In_Bytes loop
            Native_Buf (I) := Buffer (Size_In_Bytes - I + 1);
         end loop;
         return To_Discrete (Native_Buf);
      end;
   end Get;

   procedure Set (Value : Discrete_Type) is
      function To_Buffer is new Ada.Unchecked_Conversion
        (Discrete_Type, Buffer_Type);
   begin
      if System.Default_Bit_Order = System.High_Order_First then
         Buffer := To_Buffer (Value);
         return;
      end if;
      declare
         Native_Buf : constant Buffer_Type := To_Buffer (Value);
      begin
         for I in 1 .. Size_In_Bytes loop
            Buffer (I) := Native_Buf (Size_In_Bytes - I + 1);
         end loop;
      end;
   end Set;

end Big_Endian_Integer_Buffer;


  reply	other threads:[~2016-07-19 18:00 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-17 13:17 Generic formals and Aspects Simon Wright
2014-10-17 16:04 ` Shark8
2014-10-17 18:51   ` Simon Wright
2014-10-18  1:43 ` Shark8
2016-07-19 15:49 ` olivermkellogg
2016-07-19 16:04   ` J-P. Rosen
2016-07-19 16:05   ` Shark8
2016-07-19 18:00     ` olivermkellogg [this message]
2016-07-19 19:23       ` Randy Brukardt
2016-07-20 15:09         ` olivermkellogg
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox