comp.lang.ada
 help / color / mirror / Atom feed
From: bobduff@world.std.com (Robert A Duff)
Subject: Re: Subprogram access types and generic packages
Date: 1997/03/19
Date: 1997-03-19T00:00:00+00:00	[thread overview]
Message-ID: <E7AoFD.1zG@world.std.com> (raw)
In-Reply-To: bond-ya023680001803971613240001@nntp.ucs.ubc.ca


In article <bond-ya023680001803971613240001@nntp.ucs.ubc.ca>,
Greg Bond <bond@ee.ubc.ca> wrote:
>I'm trying to write a generic package that will register an access type
>of one of its internal subprograms with an external subprogram when
>instantiated. I get the error message:
>
>pkg.adb:12:14: access type must not be outside generic body
>
>Why not? 

The rule is the last sentence of 3.10.2(32).  The reasons for the rule
are explained in AARM-3.10.2(32.a).

Workaround: Declare the procedure in the private part of the generic
package.  Move the instantiation to library level (i.e. as a library
unit, or in a library package).

>Here's a simple bit of code that generates this error: (gnatmake
>access_test)
>
>--------------------------------------------------
>access_test.adb
>--------------------------------------------------
>
>with Pkg;
>
>procedure Access_Test is
>
>   Val: constant Integer := 10;
>
>   package Test is new Pkg (Val);

Note that this instantiation could cause a dangling pointer -- Test.Proc
is nested within Access_Test, but a pointer to Test.Proc could survive
after Access_Test returns.  (E.g. suppose "Register" saved its parameter
into a global variable.)  You must do the instantiation at library level
so this can't happen.  And you must declare Proc in the spec of the
generic so that the compiler can check for the error at instantiation
time.

>   use Test;
>
>begin
>   null;
>end Access_Test;
>
>
>--------------------------------------------------
>pkg.ads
>--------------------------------------------------
>
>generic
>   Const: Integer;
>
>package Pkg is
>
>   pragma Elaborate_Body (Pkg);
>
>end Pkg;
>
>--------------------------------------------------
>pkg.adb
>--------------------------------------------------
>
>with Reg; use Reg;
>
>package body Pkg is
>
>   procedure Proc is
>   begin
>      null;
>   end Proc;
>
>begin
>
>   Register (Proc'Access);
>
>end Pkg;
>
>--------------------------------------------------
>reg.ads
>--------------------------------------------------
>
>package Reg is
>
>   type Proc_Alias is access procedure;
>
>   procedure Register (Proc: Proc_Alias);
>
>end Reg;
>
>--------------------------------------------------
>reg.adb
>--------------------------------------------------
>
>package body Reg is
>
>   procedure Register (Proc: in Proc_Alias) is
>   begin
>
>    null;
>
>   end Register;
>
>end Reg;

- Bob




  reply	other threads:[~1997-03-19  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-03-18  0:00 Subprogram access types and generic packages Greg Bond
1997-03-19  0:00 ` Robert A Duff [this message]
1997-03-20  0:00 ` Tucker Taft
replies disabled

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