comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam@spam.com>
Subject: Re: "Must instantiate controlled types at library level." Why?
Date: Wed, 12 May 2004 01:03:41 GMT
Date: 2004-05-12T01:03:41+00:00	[thread overview]
Message-ID: <NJeoc.16758$V97.5496@newsread1.news.pas.earthlink.net> (raw)
In-Reply-To: <Xns94E6C22101F54pchapinsovernet@207.106.92.237>

Peter C. Chapin wrote:

> I have a generic package that contains a controlled type. When I attempt to 
> instantiate that package in the declarative part of a procedure, GNAT 
> (v3.15p) tells me that I must instantiate controlled types at "library 
> level" (or something like that). I worked around this by creating a helper 
> package in which I did the instantiation and then used that helper package in 
> my procedure. However, this experience leads me to wonder why there is this 
> restriction on controlled types.

That's the correct way to create a controlled type. I know it's 
difficult to see why this is needed, but it's a feature of all tagged 
types, not just controlled types. Any tagged type declared at library 
level must be extended at library level. The declarative part of a 
subprogram, including a library level subprogram, is never at library level.

The problem is that a value of an extended type can be stored in a 
variable at a higher nesting level. This could result in dispatching to 
an operation that no longer exists and referencing variables that no 
longer exist. This is easier to demonstrate than describe:

package Library_Level is
    type Taggy is tagged null record;
    procedure Op (T : in out Taggy);

    type Taggy_Ptr is access all Taggy'Class;

    Ptr : Taggy_Ptr;
end Library_Level;

with Library_Level;
procedure Outer is
    procedure Inner is
       type Taggy_Ext is new Library_Level.Taggy with null record;
       -- Illegal type extension
       procedure Op (T : in out Taggy_Ext);

       I : Integer := 7;

       procedure Op (T : in out Taggy_Ext) is
       begin -- Op
          I := I + 1;
       end Op;

       V : Taggy_Ext;
    begin -- Inner
       Library_Level.Ptr := new Library_Level.Taggy'Class'(V);
    end Inner;
begin -- Outer
    Inner;
    Library_Level.Op (T => Library_Level.Ptr.all);
end Outer;

The call to Library_Level.Op would attempt to dispatch to 
Outer.Inner.Op, which is out of scope. The references to I in 
Outer.Inner.Op refer to a variable that no longer exists.

-- 
Jeff Carter
"Blessed is just about anyone with a vested interest in the status quo."
Monty Python's Life of Brian
73




  reply	other threads:[~2004-05-12  1:03 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-11 23:04 "Must instantiate controlled types at library level." Why? Peter C. Chapin
2004-05-12  1:03 ` Jeffrey Carter [this message]
2004-05-12 10:47   ` Peter C. Chapin
2004-05-12 11:25     ` Ludovic Brenta
2004-05-12 14:41       ` Martin Krischik
2004-05-13  2:20       ` Peter C. Chapin
2004-05-12 11:55     ` Martin Krischik
2004-05-13  2:59       ` Peter C. Chapin
2004-05-13  7:10         ` Martin Krischik
2004-05-13 10:36           ` Peter C. Chapin
2004-05-13 11:18             ` Martin Krischik
2004-05-13 22:27               ` Peter C. Chapin
2004-05-13 22:54               ` Freejack
2004-05-14  7:13                 ` Martin Krischik
2004-05-14 13:50                   ` Xenos
2004-05-14 17:27                     ` Georg Bauhaus
2004-05-14 17:58                       ` Xenos
2004-05-14 18:49                     ` Martin Krischik
2004-05-14 19:40                       ` Xenos
2004-05-14 22:47                         ` Ludovic Brenta
2004-05-15  8:34                           ` Martin Krischik
2004-05-16  2:55                           ` Hyman Rosen
2004-05-16 13:48                             ` Ludovic Brenta
2004-05-17  2:30                               ` Hyman Rosen
2004-05-17  5:39                                 ` Martin Dowie
2004-05-17  7:48                                   ` Ludovic Brenta
2004-05-17 15:01                                     ` Hyman Rosen
2004-05-17 16:31                                       ` Georg Bauhaus
2004-05-17 17:40                                         ` Hyman Rosen
2004-05-17 19:17                                           ` Georg Bauhaus
2004-05-17  6:24                                 ` Martin Krischik
2004-05-17 19:48                                   ` James Kanze
2004-05-18  6:27                                     ` Martin Krischik
2004-05-17 12:33                                 ` Dmitry A. Kazakov
2004-05-17 13:46                                   ` Martin Krischik
2004-05-17 15:03                                     ` Dmitry A. Kazakov
2004-05-17 16:02                                   ` Alexander E. Kopilovich
2004-05-18  7:48                                     ` Dmitry A. Kazakov
2004-05-19  1:20                                       ` Alexander E. Kopilovich
2004-05-19  9:59                                         ` Dmitry A. Kazakov
2004-05-19 12:38                                           ` Hyman Rosen
2004-05-19 13:28                                             ` Dmitry A. Kazakov
2004-05-19 13:09                                           ` Georg Bauhaus
2004-05-19 13:44                                             ` Hyman Rosen
2004-05-19 14:17                                               ` Dmitry A. Kazakov
2004-05-19 14:15                                             ` Dmitry A. Kazakov
2004-05-21 11:39                                               ` Georg Bauhaus
2004-05-21 20:33                                                 ` Dmitry A. Kazakov
     [not found]                                                   ` <c8mkor$rlq$1@a1-hrz.uni-duisburg.de>
2004-05-23  1:28                                                     ` Hyman Rosen
2004-05-23  8:55                                                     ` Dmitry A. Kazakov
2004-05-24 11:38                                                       ` Georg Bauhaus
2004-05-24 13:57                                                         ` Dmitry A. Kazakov
2004-05-24 14:40                                                           ` Georg Bauhaus
2004-05-25  8:32                                                             ` Dmitry A. Kazakov
2004-05-25 15:47                                                               ` Georg Bauhaus
     [not found]                                   ` <URJ8Eg0vzF@VB1162.spb.edu>
2004-05-17 16:50                                     ` Marius Amado Alves
2004-05-18  8:27                                       ` Dmitry A. Kazakov
2004-05-15 17:20                     ` Pascal Obry
2004-05-13 19:33             ` Randy Brukardt
replies disabled

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