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 autolearn=ham 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!newsfeed.stanford.edu!headwall.stanford.edu!newsfeed.news2me.com!nntp1.phx1.gblx.net!nntp.gblx.net!nntp.gblx.net!nntp.abs.net!news.abs.net!not-for-mail Newsgroups: comp.lang.ada Subject: Re: Default rep specs for record types - documented?? References: From: Stephen Leake Message-ID: User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (windows-nt) Cancel-Lock: sha1:uh+BEFrLqrH7LCeR0iIWetTIY2Y= MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Date: Sat, 05 Nov 2005 05:25:19 -0500 NNTP-Posting-Host: 66.159.65.1 X-Complaints-To: abuse@toad.net X-Trace: news.abs.net 1131186332 66.159.65.1 (Sat, 05 Nov 2005 05:25:32 EST) NNTP-Posting-Date: Sat, 05 Nov 2005 05:25:32 EST Xref: g2news1.google.com comp.lang.ada:6230 Date: 2005-11-05T05:25:19-05:00 List-Id: "Frank J. Lhota" writes: > Stephen Leake wrote: >> 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. No, the fault lies with me, who wrote the code. The code is, as you point out, wrong. Part of the point of using Ada is to make it harder to make mistakes like this. That's why I want a warning when I make this mistake. > 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? Of course; that is the point of rep clauses. Which is why the code is wrong. > 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. This is _not_ internal data; that's why there is a rep clause. > 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. The compiler can't protect me entirely, but it can easily warn me about this particular mistake; leaving a component out of the rep clause. Compare this to a case statement on an enumeration type; the compiler warns you when you add an enumeral but forget to update the case statement. Programmers should be careful, but the compiler should help whenever it reasonably can. >> 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. Right, and I could integrate that with Emacs. But I'd rather go all the way and have the compiler do everything for me. More importantly, I want the compiler to generate the "reasonable packed rep clause", not "the most efficient for data storage" or whatever other criteria it uses in the absence of a rep clause. This matters more when I'm describing bit fields in a register; without a rep clause the compiler will use at least a byte for each field. So the generated rep clause will not be a very good starting point. -- -- Stephe