comp.lang.ada
 help / color / mirror / Atom feed
From: Corey Minyard <minyard@acm.org>
Subject: Seemingly wierd conversion for in out parameter
Date: 1998/02/20
Date: 1998-02-20T00:00:00+00:00	[thread overview]
Message-ID: <m2lnv6zsld.fsf@wf-rch.cirr.com> (raw)



I've been playing with some techniques with derived types and I ran
across a rather unusual things.  The following program:

with Text_IO; use Text_IO;
with Ada.Tags; use Ada.Tags;

------------------------------------------------------------------------
procedure Tester is

   type Base is abstract tagged null record;
   type Base_Class is access all Base'Class;

   type Der1 is new Base with null record;
   type Der1_Class is access all Der1'Class;

   type Der2 is new Base with null record;
   type Der2_Class is access all Der2'Class;

   procedure Process (Var : in out Base_Class) is
   begin
      Var := new Der2;
   end Process;

   V1 : Der1_Class := new Der1;
-- This will be a compile error.
--   U1 : Der1_Class := new Der2;

begin

   Put_Line("Base tag = " & Expanded_Name(Base'Tag));
   Put_Line("Der1 tag = " & Expanded_Name(Der1'Tag));
   Put_Line("Der2 tag = " & Expanded_Name(Der2'Tag));

   Put_Line("V1 tag = " &  Expanded_Name(V1'Tag));

   Process(Base_Class(V1));

   Put_Line("V1 tag = " &  Expanded_Name(V1'Tag));

end Tester;
------------------------------------------------------------------------

will compile and produce the following output:

------------------------------------------------------------------------
Base tag = TESTER.BASE
Der1 tag = TESTER.DER1
Der2 tag = TESTER.DER2
V1 tag = TESTER.DER1
V1 tag = TESTER.DER2
------------------------------------------------------------------------

As you can see, the variable V1 should not be able to reference a
tagged type of Der2, but this program does that without any errors
under GNAT (the only compiler I have right now).  I would think that
the compiler wouldn't allow a class conversion passed to an in out
parameter, but it seems to.

I'm certainly no language lawyer, but I would image that doing a cast
would make something not a "variable" any more (RM 6.4.1(5)), thus
this would not be legal.  Is GNAT wrong, or is this legal, or is this
a grey area?

Thanks,

-- 
Corey Minyard               Internet:  minyard@acm.org
  Work: minyard@nortel.ca       UUCP:  minyard@wf-rch.cirr.com




             reply	other threads:[~1998-02-20  0:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-02-20  0:00 Corey Minyard [this message]
1998-02-23  0:00 ` Seemingly wierd conversion for in out parameter Albert K. Lee
1998-02-23  0:00   ` Corey Minyard
1998-02-25  0:00     ` Nick Roberts
1998-02-26  0:00       ` Bernd Holzmueller
1998-02-26  0:00         ` Stephen Leake
1998-02-26  0:00           ` Robert Dewar
replies disabled

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