comp.lang.ada
 help / color / mirror / Atom feed
* Controlled Types & GNAT 3.09
@ 1997-03-11  0:00 Alexander V. Konstantinou
  1997-03-13  0:00 ` Robert Dewar
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Alexander V. Konstantinou @ 1997-03-11  0:00 UTC (permalink / raw)



-- Following is a "gnatchop-friendly" example of controlled types use.
-- When compiled with GNATMAKE 3.09 (970121) on
--	SunOS 5.5.1 Generic_103640-04 sun4m sparc
-- [*sutton:test-controlled] gnatmake test_controlled.adb 
-- gcc -c test_controlled.adb
-- gcc -c bar_controlled.adb
-- gcc -c foo_controlled.adb
-- gnatbind -x test_controlled.ali
-- gnatlink test_controlled.ali
-- 
-- produces the following output :
-- [*sutton:test-controlled] ./test_controlled 
-- Initialize(Foo)
-- Initialize(Foo)
-- Initialize(Foo)
-- Initialize(Bar)
-- Initialize(Foo)
-- Initialize(Bar)
-- Main Begin ----
-- F1 := F2
-- Finalize(Foo)
-- Adjust(Foo)
-- B1 := B2
-- Main End ----
-- [*sutton:test-controlled] 
--
-- Note that the Finalize and Adjust functions where never called on B1.
-- Is that a GNAT bug, or is it a problem with my understanding of controlled
-- types ?

with Ada.Text_IO; use Ada.Text_IO;
with Foo_Controlled;
with Bar_Controlled;

procedure Test_Controlled is
   F1, F2 : Foo_Controlled.Foo;
   B1, B2 : Bar_Controlled.Bar;
begin
   Put_Line("Main Begin ----");

   Put_Line("F1 := F2");
   F1 := F2;

   Put_Line("B1 := B2");
   B1 := B2;

   Put_Line("Main End ----");

end Test_Controlled;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Finalization;

package Foo_Controlled  is

   type Foo is private;
   procedure Initialize(F: in out Foo);
   procedure Adjust(F: in out Foo);
   procedure Finalize(F: in out Foo);

private

   type Integer_Access_Type is access Integer;

   type Foo is new Ada.Finalization.Controlled with
      record
         P : Integer_Access_Type;
      end record;


end Foo_Controlled;
with Unchecked_Deallocation;
package body Foo_Controlled is

   procedure Free is new Unchecked_Deallocation(Integer, Integer_Access_Type);

   procedure Initialize(F: in out Foo) is
   begin
      Put_Line("Initialize(Foo)");
      F.P := new Integer'(100);
   end Initialize;

   procedure Adjust(F: in out Foo) is
   begin
      Put_Line("Adjust(Foo)");
   end Adjust;

   procedure Finalize(F: in out Foo) is
   begin
      Put_Line("Finalize(Foo)");
      Free(F.P);
   end Finalize;

end Foo_Controlled;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Finalization;
with Foo_Controlled;

package Bar_Controlled is

   type Bar is private;

   procedure Initialize(B: in out Bar);
   procedure Adjust(B: in out Bar);
   procedure Finalize(B: in out Bar);

private
   type Bar is new Ada.Finalization.Controlled with
      record
         I : Integer;
         F : Foo_Controlled.Foo;
      end record;


end Bar_Controlled;
package body Bar_Controlled is

   procedure Initialize(B: in out Bar) is
   begin
      Put_Line("Initialize(Bar)");
   end Initialize;

   procedure Adjust(B: in out Bar) is
   begin
      Put_Line("Adjust(Bar)");
   end Adjust;

   procedure Finalize(B: in out Bar) is
   begin
      Put_Line("Finalize(Bar)");
   end Finalize;

end Bar_Controlled;

-- 
Alexander V. Konstantinou              http://www.cs.columbia.edu/~akonstan/
akonstan@cs.columbia.edu               akonstan@acm.org




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

* Re: Controlled Types & GNAT 3.09
  1997-03-11  0:00 Controlled Types & GNAT 3.09 Alexander V. Konstantinou
@ 1997-03-13  0:00 ` Robert Dewar
  1997-03-14  0:00 ` Jerome Desquilbet
  1997-03-14  0:00 ` Pascal Ledru
  2 siblings, 0 replies; 8+ messages in thread
From: Robert Dewar @ 1997-03-13  0:00 UTC (permalink / raw)




just so people know, this was submitted in proper form as a bug report
to report@gnat.com, but we have not looked at it yet.





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

* Re: Controlled Types & GNAT 3.09
  1997-03-11  0:00 Controlled Types & GNAT 3.09 Alexander V. Konstantinou
  1997-03-13  0:00 ` Robert Dewar
@ 1997-03-14  0:00 ` Jerome Desquilbet
  1997-03-14  0:00   ` Robert A Duff
  1997-03-14  0:00 ` Pascal Ledru
  2 siblings, 1 reply; 8+ messages in thread
From: Jerome Desquilbet @ 1997-03-14  0:00 UTC (permalink / raw)
  To: Alexander V. Konstantinou


Alexander,

There's a bug in GNAT, but it is not where you think. The program you
propose should write:

Main Begin ----
F1 := F2
B1 := B2
Main End ----

And that's all! Why?

You declared:

> package Foo_Controlled  is
> 
>    type Foo is private;
>    procedure Initialize(F: in out Foo);
>    procedure Adjust(F: in out Foo);
>    procedure Finalize(F: in out Foo);
> 
> private

[...]

> end Foo_Controlled;

and same kind of declaration for Bar.
At the place you declare Initialize, Adjust and Finalize, you are in the
partial view of the type. You simply declare subprograms, that have
nothing to do with the inherited subprograms from Controlled, because at
the place you declare them, the type is not known as inheriting from
Controlled.

Put Initialize, Adjust and Finalize in the full view (i.e. at the end of
the private part in your example) and it will work.

Regards,

  Jerome.

PS: tested with Apex 2.2.2B.
______________________________________________________________________
Jerome Desquilbet                             jDesquilbet@Rational.COM
 ' ^




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

* Re: Controlled Types & GNAT 3.09
  1997-03-11  0:00 Controlled Types & GNAT 3.09 Alexander V. Konstantinou
  1997-03-13  0:00 ` Robert Dewar
  1997-03-14  0:00 ` Jerome Desquilbet
@ 1997-03-14  0:00 ` Pascal Ledru
  1997-03-15  0:00   ` Alexander V. Konstantinou
  2 siblings, 1 reply; 8+ messages in thread
From: Pascal Ledru @ 1997-03-14  0:00 UTC (permalink / raw)



You may want to look at the ARM in details,
but this is what I think should happen. Somebody else may or may not
confirm.

Initialize(Foo)
Initialize(Foo)
Initialize(Foo)
Initialize(Bar)
Initialize(Foo)
Initialize(Bar)
Main Begin ---
F1 := F2
Finalize(Foo)
Adjust(Foo)
B1 := B2
Finalize(Bar)
Finalize(Foo)
Adjust(Foo)
Adjust(Bar)
Main End --- 

then B1 and B2 are finalize first (see order of Finalization 7.6.1 (11)
)
Finalize(Bar)
Finalize(Foo)
Finalize(Bar)
Finalize(Foo)
Program_Error (see ARM 7.6.1(17) )




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

* Re: Controlled Types & GNAT 3.09
  1997-03-14  0:00 ` Jerome Desquilbet
@ 1997-03-14  0:00   ` Robert A Duff
  1997-03-14  0:00     ` Tom Moran
  0 siblings, 1 reply; 8+ messages in thread
From: Robert A Duff @ 1997-03-14  0:00 UTC (permalink / raw)



In article <33293975.41C6@Rational.COM>,
Jerome Desquilbet  <jDesquilbet@Rational.COM> wrote:
>You declared:
>
>> package Foo_Controlled  is
>> 
>>    type Foo is private;
>>    procedure Initialize(F: in out Foo);
>>    procedure Adjust(F: in out Foo);
>>    procedure Finalize(F: in out Foo);
>> 
>> private
>
>[...]
>
>> end Foo_Controlled;
>
>and same kind of declaration for Bar.
>At the place you declare Initialize, Adjust and Finalize, you are in the
>partial view of the type. You simply declare subprograms, that have
>nothing to do with the inherited subprograms from Controlled, because at
>the place you declare them, the type is not known as inheriting from
>Controlled.

No, that's not right.  If the full type for Foo is controlled, then the
above Initialize overrides the controlled one, and gets called
automatically as one would expect.  Same for the Adjust and Finalize.

>Put Initialize, Adjust and Finalize in the full view (i.e. at the end of
>the private part in your example) and it will work.

No, that's not necessary.  (It might be a good idea, though -- you
probably want to prevent clients from calling Initialize and friends
explicitly.)

>PS: tested with Apex 2.2.2B.

Sounds like a bug in Apex, then.  :-(

- Bob




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

* Re: Controlled Types & GNAT 3.09
  1997-03-14  0:00   ` Robert A Duff
@ 1997-03-14  0:00     ` Tom Moran
  0 siblings, 0 replies; 8+ messages in thread
From: Tom Moran @ 1997-03-14  0:00 UTC (permalink / raw)



Is Text_IO guaranteed to be callable at the time of an Initialize or
Finalize in this example?




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

* Re: Controlled Types & GNAT 3.09
  1997-03-14  0:00 ` Pascal Ledru
@ 1997-03-15  0:00   ` Alexander V. Konstantinou
  1997-03-21  0:00     ` Jerome Desquilbet
  0 siblings, 1 reply; 8+ messages in thread
From: Alexander V. Konstantinou @ 1997-03-15  0:00 UTC (permalink / raw)



I am forwarding a message sent to me by the GNAT developers.  It appears
that the GNAT language lawyers believe that the use of Controlled types
in my example is a valid one.

Alexander V. Konstantinou (akonstan@cs.columbia.edu)

--- begin forwarded message ---
From dismukes@gnat.com  Fri Mar 14 17:41:00 1997
Date: Fri, 14 Mar 97 17:40:17 EST
From: dismukes@gnat.com (Gary Dismukes)
To: akonstan@sutton.cs.columbia.edu, chat@gnat.com
Subject: Re: controlled types in GNAT 3.09

Alexander,

The problem you reported involving controlled types has
been fixed in the current (i.e., not yet released) version
of GNAT and leads to the output shown below.

-- Gary Dismukes

Initialize(Foo)
Initialize(Foo)
Initialize(Foo)
Initialize(Bar)
Initialize(Foo)
Initialize(Bar)
Main Begin ----
F1 := F2
Finalize(Foo)
Adjust(Foo)
B1 := B2
Finalize(Bar)
Finalize(Foo)
Adjust(Foo)
Adjust(Bar)
Main End ----
Finalize(Bar)
Finalize(Foo)
Finalize(Bar)
Finalize(Foo)
Finalize(Foo)
Finalize(Foo)

--- end forwarded message ---
-- 
Alexander V. Konstantinou              http://www.cs.columbia.edu/~akonstan/
akonstan@cs.columbia.edu               akonstan@acm.org




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

* Re: Controlled Types & GNAT 3.09
  1997-03-15  0:00   ` Alexander V. Konstantinou
@ 1997-03-21  0:00     ` Jerome Desquilbet
  0 siblings, 0 replies; 8+ messages in thread
From: Jerome Desquilbet @ 1997-03-21  0:00 UTC (permalink / raw)



Alexander V. Konstantinou wrote:
> 
> I am forwarding a message sent to me by the GNAT developers.  It 
> appears
> that the GNAT language lawyers believe that the use of Controlled 
> types
> in my example is a valid one.

I still think that your example shouldn't produce this output.

The bug in GNAT has been fixed the wrong way: if Initialize, Adjust and
Finalize are declared in the partial view (where the type is not known
as being Controlled), then the Initialize, Adjust and Finalize you
declare are not the related to the operation with same name in the
Controlled type. 

This is true for any tagged type construction like yours:

For example:


package Base is
    type Object is tagged null record;
    procedure Primitive (A_Base : in Object);
end Base;

with Base;
package Derived is
    type Object is private;
    procedure Primitive (A_Derived : in Object);
private
    type Object is new Base.Object with null record;
end Derived;


Users of Derived DON'T KNOW that Derived.Object is derived from
Base.Object, right
Does GNAT compiles the following with Derived declared as above???


with Base;
with Derived;
procedure Try_Base_And_Derived is
    A_Derived : Derived.Object;
begin
    Base.Primitive (Base.Object'Class (A_Derived));
end Try_Base_And_Derived;


I hope the answer is no...
Why would it be different because you derived from Controlled instead of
some other tagged type???

  Jerome.

______________________________________________________________________
Jerome Desquilbet                             jDesquilbet@Rational.COM
 ' ^




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

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

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-03-11  0:00 Controlled Types & GNAT 3.09 Alexander V. Konstantinou
1997-03-13  0:00 ` Robert Dewar
1997-03-14  0:00 ` Jerome Desquilbet
1997-03-14  0:00   ` Robert A Duff
1997-03-14  0:00     ` Tom Moran
1997-03-14  0:00 ` Pascal Ledru
1997-03-15  0:00   ` Alexander V. Konstantinou
1997-03-21  0:00     ` Jerome Desquilbet

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