comp.lang.ada
 help / color / mirror / Atom feed
* record rep on tagged record question
@ 1997-09-23  0:00 Tom Moran
  1997-09-24  0:00 ` Matthew Heaney
  0 siblings, 1 reply; 4+ messages in thread
From: Tom Moran @ 1997-09-23  0:00 UTC (permalink / raw)



Shouldn't this be legal Ada 95?  One of the three compilers I tried
gives a "raised CONSTRAINT_ERROR" when I try to execute it.  The
message apparently comes from the attempt to elaborate
"Picture:b.Child_Type", since neither the Ada.Text_IO in the main
program nor the one in its exception handler produces any result.
(The other two compilers both give "hi" results.)  I've checked and
Tag'Size for this compiler is 32, so an offset of 4 bytes seems
appropriate.  If I set TAG_SIZE to zero, the compiler complains that
I'm trying to overlay the tag. If I eliminate the 'for Root_Type use
record' rep clause entirely, I get "hi".
 
package b is

    TAG_SIZE : constant := 4;

    type Root_Type is tagged record
        I : Integer;
    end record;
    for Root_Type use record
        I at TAG_SIZE range 0 .. 31;
    end record;


    type Child_Type is new Root_Type with record
        J : Integer;
    end record;

end b;

with ada.exceptions;
with ada.text_io;
with b;
procedure try is
  Picture : b.Child_Type;
begin
  ada.text_io.put_line("hi");
exception
  when oops:others =>
    ada.text_io.put_line("oops:"
      & ada.exceptions.exception_information(oops));
end try;





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: record rep on tagged record question
  1997-09-23  0:00 Tom Moran
@ 1997-09-24  0:00 ` Matthew Heaney
  0 siblings, 0 replies; 4+ messages in thread
From: Matthew Heaney @ 1997-09-24  0:00 UTC (permalink / raw)



In article <3428278b.18967041@SantaClara01.news.InterNex.Net>,
tmoran@bix.com (Tom Moran) wrote:

>Shouldn't this be legal Ada 95?  One of the three compilers I tried
>gives a "raised CONSTRAINT_ERROR" when I try to execute it.  The
>message apparently comes from the attempt to elaborate
>"Picture:b.Child_Type", since neither the Ada.Text_IO in the main
>program nor the one in its exception handler produces any result.
>(The other two compilers both give "hi" results.)  I've checked and
>Tag'Size for this compiler is 32, so an offset of 4 bytes seems
>appropriate.

Why are you writing a representation clause for a tagged record anyway? 
Are you intending to do some I/O using this type?  Have you looked at the
stream I/O facility - it may do what you want without the rep clause (read:
portability) headaches.

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: record rep on tagged record question
@ 1997-10-01  0:00 tmoran
  1997-10-01  0:00 ` Tucker Taft
  0 siblings, 1 reply; 4+ messages in thread
From: tmoran @ 1997-10-01  0:00 UTC (permalink / raw)



>Why are you writing a representation clause for a tagged record anyway?
>Are you intending to do some I/O using this type?  Have you looked at the
  No, I'm doing an API call to the OS, which has definite ideas about
how the record should be structured.  And in the real world problem this
example came from, there are discriminants involved which preclude
simply putting everything into an intermediate record, then using that
as the sole new component in the extended record.
  This is intended as a 'confirming' record rep clause, since the
compiler in question *seems* to do the expected thing, but it would be
nice to be sure.  If 'Position could be used on a type, instead of just
an object, I could make a compile time constraint error test - but it
can't.  RM 13.5.1(21) certainly leads me to believe this rep clause
should be legal.




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: record rep on tagged record question
  1997-10-01  0:00 record rep on tagged record question tmoran
@ 1997-10-01  0:00 ` Tucker Taft
  0 siblings, 0 replies; 4+ messages in thread
From: Tucker Taft @ 1997-10-01  0:00 UTC (permalink / raw)



tmoran@bix.com wrote:

: ...
:   This is intended as a 'confirming' record rep clause, since the
: compiler in question *seems* to do the expected thing, but it would be
: nice to be sure.  

One Ada 95 front end with which I am familiar (;-) places all un-rep-speced
fields after those with rep-clauses, making no attempt to use up
gaps left earlier in the record.  Until very recently, this applied
to the tag field as well, meaning that if you put a rep-clause on
a tagged type, the tag field ended up getting placed *after* all the
rep-claused fields.  Furthermore, there was a bug that if *all* the
fields were rep-claused (except the implicit "tag" field), then
it didn't leave any extra room at the end for the tag, and the tag 
got allocated off the end of the record (gack!).

Hence, a "confirming" rep clause on a tagged type definitely did
not have the desired effect of ensuring the layout was the same
as you would expect.

Upon seeing your example, we now special case the tag, and try to put it 
at offset 0 even in the presence of rep-clauses, if a gap is left there.
If there is no gap there, we still put the tag after the rep-claused
fields (but now we don't forget it when computing the record size!).
We still ignore other gaps left by rep-clauses when placing other
explicit non-rep-claused fields.   

I can't comment on what other Ada 95 front ends do...

: ... If 'Position could be used on a type, instead of just
: an object, I could make a compile time constraint error test - but it
: can't.  

You could perhaps do something like:

    package body XXX is
      ...
    begin
        if False then
             declare
                 Obj : My_Tagged_Type;
             begin
                 pragma Assert(Obj.My_Field'Position = DESIRED_POSITION);
             end;
        end if;
    end XXX;

This might give you a warning if the Assert is known false at compile-time.

I suppose another approach which might accomplish the goal is:

    package body XXX is
      ...
        Check_Obj : My_Tagged_Type;
        pragma Import(Ada, Check_Obj);
        for Check_Obj'Address use System.Null_Address;
        pragma Assert(Check_Obj.My_Field'Position = DESIRED_POSITION);
      ...
    end XXX;

FWIW, one reason 'Position can't be used on a type is that in some cases,
the 'Position of a field may depend on the value of discriminants.
Another reason is that "<type>.<fieldname>" is not normally a legal 
construct, meaning that <type>.<fieldname>'Position would
require a whole lot of special handling in the front end.  
Be that as it may, I admit to often wishing 'Position 
(and 'First/Last_Bit) could be used on types.  

One possibility might be to define an attribute, say Field_Position,
that worked on constrained (to avoid the discriminant problem) record 
subtypes as follows:

    <constrained_subtype>'Field_Position("<fieldname>") 

where the string "<fieldname>" is required to be static, and match some
visible field of the <constrained_subtype>.

: ... RM 13.5.1(21) certainly leads me to believe this rep clause
: should be legal.

The rep-clause is certainly legal.  However, as indicated above,
there is no guarantee that it only "confirms" the desired layout,
but instead may perturb it in undesirable (and in this case,
bug-infested) ways.

--
-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Burlington, MA  USA




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1997-10-01  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-10-01  0:00 record rep on tagged record question tmoran
1997-10-01  0:00 ` Tucker Taft
  -- strict thread matches above, loose matches on Subject: below --
1997-09-23  0:00 Tom Moran
1997-09-24  0:00 ` Matthew Heaney

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