From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.36.228.196 with SMTP id o187mr332460ith.2.1468951218725; Tue, 19 Jul 2016 11:00:18 -0700 (PDT) X-Received: by 10.157.5.98 with SMTP id 89mr760146otw.9.1468951218700; Tue, 19 Jul 2016 11:00:18 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!news.glorb.com!f6no2689379ith.0!news-out.google.com!d130ni8086ith.0!nntp.google.com!f6no2689374ith.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Tue, 19 Jul 2016 11:00:18 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=217.248.72.206; posting-account=Md_OIgoAAAAkZyQ6nYoc3WBIThMpPfV7 NNTP-Posting-Host: 217.248.72.206 References: <59656e9a-8826-490e-9c35-f13f8ff1aa91@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <18aedafc-c66d-4bba-81ce-76dce495f59e@googlegroups.com> Subject: Re: Generic formals and Aspects From: olivermkellogg@gmail.com Injection-Date: Tue, 19 Jul 2016 18:00:18 +0000 Content-Type: text/plain; charset=UTF-8 Xref: news.eternal-september.org comp.lang.ada:31102 Date: 2016-07-19T11:00:18-07:00 List-Id: 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;