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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,fd173879a595bde X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!cyclone1.gnilink.net!spamkiller2.gnilink.net!gnilink.net!trndny09.POSTED!0f19ed38!not-for-mail From: "Frank J. Lhota" Reply-To: NOSPAM.lhota@adarose.com User-Agent: Mozilla Thunderbird 1.0.7 (Windows/20050923) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Default rep specs for record types - documented?? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Message-ID: Date: Fri, 04 Nov 2005 16:52:37 GMT NNTP-Posting-Host: 151.203.236.121 X-Complaints-To: abuse@verizon.net X-Trace: trndny09 1131123157 151.203.236.121 (Fri, 04 Nov 2005 11:52:37 EST) NNTP-Posting-Date: Fri, 04 Nov 2005 11:52:37 EST Xref: g2news1.google.com comp.lang.ada:6201 Date: 2005-11-04T16:52:37+00:00 List-Id: Stephen Leake wrote: > I've submitted the following to AdaCore: > > I had a record: > > record_byte_length : constant := 4; > > type Record_Type is record > field_1 : Integer_16; > field_2 : Integer_16; > end record; > > for record_type use record > Field_1 at 0 range 0 .. 15; > field_2 at 2 range 0 .. 15; > end record; > for record_type'size use record_byte_length * 8; I would recommend writing the 'Size rep clause as follows: for record_type'size use 2 * Integer_16'Size; Then we can skip the record_byte_length declaration. > Then I needed to add a field, which should go before the others: > > record_byte_length : constant := 6; > > type Record_Type is record > field_0 : Integer_16; > field_1 : Integer_16; > field_2 : Integer_16; > end record; > > for record_type use record > Field_1 at 0 range 0 .. 15; > field_2 at 2 range 0 .. 15; > end record; > for record_type'size use record_byte_length * 8; > > > I forgot to add Field_0 to the rep clause, and the compiler put it at > the end (where it didn't belong). Most importantly, the compiler > didn't warn me. The fault lies with the code, not the compiler. Where in this code is there any indication that Field_0 "does not belong" at the end? This representation clause requires that Field_1 and Field_2 occupy the first four bytes of the record, so where else *could* the compiler place field_0 besides the end? The raison d'�tre behind record representation clauses is to make sure that a record layout matches some external data format. For internal data, you would be better off forgoing the representation clauses and letting the compiler choose the optimal record layout. Now the Ada compiler cannot possibly know anything about external data formats. The programmer must therefore be especially careful in coding the representation clause, for this is the compiler's only clue as to the desired record layout. Writing a bad representation clause is one of the errors that the compiler cannot reasonably protect the protect the programmer from making. > One solution is to have the compiler write the representation clause > for me; in this case, if we make reasonable assumptions about > 'record_type', there is only one reasonable rep clause. Both ObjectAda and GNAT have options by which Ada representation clauses are generated for all record declarations in the sources. These compiler generated representation clauses can serve as a good starting point, but it is still up to the programmer to make the needed modifications in order to get the required data layout. -- "All things extant in this world, Gods of Heaven, gods of Earth, Let everything be as it should be; Thus shall it be!" - Magical chant from "Magical Shopping Arcade Abenobashi" "Drizzle, Drazzle, Drozzle, Drome, Time for the this one to come home!" - Mr. Lizard from "Tutor Turtle"