comp.lang.ada
 help / color / mirror / Atom feed
* Novice Question on Record Representation
@ 1992-12-16 22:57 Josh Lam
  0 siblings, 0 replies; 6+ messages in thread
From: Josh Lam @ 1992-12-16 22:57 UTC (permalink / raw)


There is a type in another package I need to use in a record in my package.
Unfortunately, that type is private and I need to use record representation
for my record.

package Some_Package is

 type Some_Int is new integer;
 for Some_Int'size use 16;

 type Some_Type is private

 -- etc etc

 private

  type Some_Type is new integer;
  for Some_Type'size use 32;

end Some_Package;


with Some_Package;
package My_Package is

 type My_Rec (Choice: Boolean := True) is record
   case Choice is
     when True =>
       A : Some_Package.Some_Int;
       B : Some_Package.Some_Int;
       C : Some_Package.Some_Type;
     when False =>
       D : Some_Package.Some_Int;
   end case;
 end record;
   
 for My_Rec use record
    A      at 0 range 0..15;
    B      at 0 range 16..31;
    C      at 1 range 0..31;     -- of course there is an error here!
    Choice at 2 range 0..31;
    D      at 0 range 0..15;
 end record;

-- etc etc

end My_Package;


I know that I cannot do the above cos C is of Some_Package.Some_Type which
is a private type.

So, other than yelling at the person and getting him to change his type
to public, what are my alternatives given that I need to use record
representation. Please also let me know some pros and cons (if any) to any
alternative.

Thanks in advance!

-- 
Josh Lam
Honeywell Inc
lam@saifr00.cfsat.honeywell.com

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

* Re: Novice Question on Record Representation
@ 1992-12-17 13:28 agate!spool.mu.edu!sdd.hp.com!saimiri.primate.wisc.edu!aplcen.apl.jhu.edu
  0 siblings, 0 replies; 6+ messages in thread
From: agate!spool.mu.edu!sdd.hp.com!saimiri.primate.wisc.edu!aplcen.apl.jhu.edu @ 1992-12-17 13:28 UTC (permalink / raw)


>Josh Lam
>Honeywell Inc
>lam@saifr00.cfsat.honeywell.com

-- OOH, OOH, OOH!!! Something technical on comp.lang.ada!  Wow!
-- Today should become a federal holiday! :-)

package Some_Package is

 type Some_Int is new short_integer;
 for Some_Int'size use 16;

 type Some_Type is private;

 private

  type Some_Type is new integer;
  for Some_Type'size use 32;

end Some_Package;


with Some_Package;
package My_Package is

 XX : constant Integer := Integer(Some_Package.Some_Type'Size);

 type My_Rec (Choice: Boolean := True) is record
   case Choice is
     when True =>
       A : Some_Package.Some_Int;
       B : Some_Package.Some_Int;
       C : Some_Package.Some_Type;
     when False =>
       D : Some_Package.Some_Int;
   end case;
 end record;
   
-- for My_Rec use record
--    A      at 0 range 0..15;
--    B      at 0 range 16..31;
--    C      at 1 range 0..31;     -- of course there is an error here!
--    Choice at 2 range 0..31;
--    D      at 0 range 0..15;
-- end record;

 -- Is the storage unit on your machine really 32 bits?  the '0' in "A at 0" 
 -- refers to which storage unit to start the component at.  But the answer
 -- to your question is that you CAN use rep specs with private types.  The
 -- attributes of private types are also available (such as 'SIZE).  The 
 -- problem in your example is overlapping components of your record.  Try:

 for My_Rec use record
    A      at 0 range 0..15;
    B      at 2 range 0..15;
    C      at 4 range 0..31;     -- of course there is an error here! NOT!
    Choice at 8 range 0..31;
    D      at 0 range 0..15;
 end record;

 -- This works for me.  Yes I actually compiled it.

-- etc etc

end My_Package;

--I know that I cannot do the above cos C is of Some_Package.Some_Type which
--is a private type.
--
--So, other than yelling at the person and getting him to change his type
--to public, what are my alternatives given that I need to use record
--representation. Please also let me know some pros and cons (if any) to any
--alternative.
--
--Thanks in advance!
--

--Thor
--dlc@ddsdx2.jhuapl.edu
--collard@capsrv.jhuapl.edu

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

* Re: Novice Question on Record Representation
@ 1992-12-17 14:29 Douglas N. Surber
  0 siblings, 0 replies; 6+ messages in thread
From: Douglas N. Surber @ 1992-12-17 14:29 UTC (permalink / raw)


In <1992Dec16.225712.23791@saifr00.cfsat.honeywell.com> lam@saifr00.cfsat.honey
well.com (Josh Lam) writes:

>There is a type in another package I need to use in a record in my package.
>Unfortunately, that type is private and I need to use record representation
>for my record.

> for My_Rec use record
>    A      at 0 range 0..15;
>    B      at 0 range 16..31;
>    C      at 1 range 0..31;     -- of course there is an error here!
>    Choice at 2 range 0..31;
>    D      at 0 range 0..15;
> end record;

Try the following small kludge:

    package P1 is
	type T is private;
	T_Size : constant := 32;
    private
	type T is new Integer;
	subtype Assertion is boolean range true .. true;
	a1 : constant Assertion := T_Size = T'Size;
    end P1;

    with P1;
    package P2 is
	type T is 
	    record
		F : P1.T;
	    end record;
	for T use record
	    F at 0 range 0 .. P1.T_Size - 1;
	end record;
    end P2;

You can't use P1.T'Size directly since it isn't static outside of the
private part (at least according to the Alsys compiler).  By using the
Assertion, you assure that T_Size has the right value.  Yes, you may have
to tweak its value when you port, but you can't get a clean compile with
the wrong value so it is safe.


--
Douglas Surber             "Would you rather debug at
Lockheed                    compile time or run time?"
Houston, TX                      --Michael B. Feldman

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

* Re: Novice Question on Record Representation
@ 1992-12-17 18:24 Gary Morris @pulsar
  0 siblings, 0 replies; 6+ messages in thread
From: Gary Morris @pulsar @ 1992-12-17 18:24 UTC (permalink / raw)


In <dnsurber.724602575@node_26400> dnsurber@lescsse.jsc.nasa.gov (Douglas N. Su
rber) writes:
>    package P1 is
>	type T is private;
>	T_Size : constant := 32;
>    private
>	type T is new Integer;
>	subtype Assertion is boolean range true .. true;
>	a1 : constant Assertion := T_Size = T'Size;
>    end P1;

>By using the Assertion, you assure that T_Size has the right value.  Yes,
>you may have to tweak its value when you port, but you can't get a clean
>compile with the wrong value so it is safe. 

Unfortunately, you can't rely on assertion like this.  The constant a1 is
not used anywhere and an optimizing compiler could remove a1 and the
expression used for its initial value (TSize = T'Size).  So the range check
never occurs and no exception or compile time warning is generated.  This
optimization is allowed by 11.6(7), when the only effect of a predefined
operation is to raise an exception. 

--GaryM
-- 
Gary Morris                      Internet: garym@telesoft.com
Ada Software Development         UUCP:     uunet!telesoft!garym
Alsys West (TeleSoft)            Phone:    +1 619-457-2700
San Diego, CA, USA               Fax:      +1 619-452-2117

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

* Re: Novice Question on Record Representation
@ 1992-12-17 22:32 Douglas N. Surber
  0 siblings, 0 replies; 6+ messages in thread
From: Douglas N. Surber @ 1992-12-17 22:32 UTC (permalink / raw)


In <1992Dec17.182403.6353@telesoft.com> garym@telesoft.com (Gary Morris @pulsar
) writes:

>Unfortunately, you can't rely on assertion like this.  The constant a1 is
>not used anywhere and an optimizing compiler could remove a1 and the
>expression used for its initial value (TSize = T'Size).  So the range check
>never occurs and no exception or compile time warning is generated.  This
>optimization is allowed by 11.6(7), when the only effect of a predefined
>operation is to raise an exception. 

But a1 could be used in the package body, therefore it can't be optimized
out during compilation of the package spec.  As I recall there is no
requirement to issue a compile time warning in cases like this, but I
am not aware of any compiler that doesn't.  Does anyone else know of any?
I use the subtype Assertion a lot in my code and it occasionally catches
things, but I guess I really shouldn't rely on it.


--
Douglas Surber             "Would you rather debug at
Lockheed                    compile time or run time?"
Houston, TX                      --Michael B. Feldman

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

* Re: Novice Question on Record Representation
@ 1992-12-18  0:22 zaphod.mps.ohio-state.edu!cis.ohio-state.edu!udecc.engr.udayton.edu!black
  0 siblings, 0 replies; 6+ messages in thread
From: zaphod.mps.ohio-state.edu!cis.ohio-state.edu!udecc.engr.udayton.edu!black @ 1992-12-18  0:22 UTC (permalink / raw)


Why do you say, "of course" there is an error in this?  You should be able
to layout a private type just as easily as any other -- of course, you need
knowledge of that private type which you otherwise might not need; but, you're
using a rep clause so you must need it.

However, you may be getting an error if your machine's storage unit is a byte
(like most machines).  In this case, you are laying out field A to take bytes
0 and 1; field B takes bytes 2 and 3; field C starts with byte 1 again, thus
takes bytes 1,2,3, and 4.  Bytes 1,2,3 are already allocated, however. 
That is an error.  If your storage unit is a long word (32 bits), this should
work...

-- Chris

In article <1992Dec16.225712.23791@saifr00.cfsat.honeywell.com>, lam@saifr00.cf
sat.honeywell.com (Josh Lam) writes:
|> There is a type in another package I need to use in a record in my package.
|> Unfortunately, that type is private and I need to use record representation
|> for my record.
|> 
|> package Some_Package is
|> 
|>  type Some_Int is new integer;
|>  for Some_Int'size use 16;
|> 
|>  type Some_Type is private
|> 
|>  -- etc etc
|> 
|>  private
|> 
|>   type Some_Type is new integer;
|>   for Some_Type'size use 32;
|> 
|> end Some_Package;
|> 
|> 
|> with Some_Package;
|> package My_Package is
|> 
|>  type My_Rec (Choice: Boolean := True) is record
|>    case Choice is
|>      when True =>
|>        A : Some_Package.Some_Int;
|>        B : Some_Package.Some_Int;
|>        C : Some_Package.Some_Type;
|>      when False =>
|>        D : Some_Package.Some_Int;
|>    end case;
|>  end record;
|>    
|>  for My_Rec use record
|>     A      at 0 range 0..15;
|>     B      at 0 range 16..31;
|>     C      at 1 range 0..31;     -- of course there is an error here!
|>     Choice at 2 range 0..31;
|>     D      at 0 range 0..15;
|>  end record;
|> 
|> -- etc etc
|> 
|> end My_Package;
|> 
|> 
|> I know that I cannot do the above cos C is of Some_Package.Some_Type which
|> is a private type.
|> 
|> So, other than yelling at the person and getting him to change his type
|> to public, what are my alternatives given that I need to use record
|> representation. Please also let me know some pros and cons (if any) to any
|> alternative.
|> 
|> Thanks in advance!
|> 
|> -- 
|> Josh Lam
|> Honeywell Inc
|> lam@saifr00.cfsat.honeywell.com
|> 

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

end of thread, other threads:[~1992-12-18  0:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1992-12-18  0:22 Novice Question on Record Representation zaphod.mps.ohio-state.edu!cis.ohio-state.edu!udecc.engr.udayton.edu!black
  -- strict thread matches above, loose matches on Subject: below --
1992-12-17 22:32 Douglas N. Surber
1992-12-17 18:24 Gary Morris @pulsar
1992-12-17 14:29 Douglas N. Surber
1992-12-17 13:28 agate!spool.mu.edu!sdd.hp.com!saimiri.primate.wisc.edu!aplcen.apl.jhu.edu
1992-12-16 22:57 Josh Lam

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