comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: [Revisited] How to get around "access type must not be outside generic unit" (was: How to get around "access type must not be outside generic unit")
Date: Tue, 23 Jan 2007 19:18:20 +0100
Date: 2007-01-23T19:18:15+01:00	[thread overview]
Message-ID: <3vwf1b4b2ntl$.l9n17zmh9v8a$.dlg@40tude.net> (raw)
In-Reply-To: oghf54-6ho.ln1@hal.boavista.snafu.de

On Sun, 17 Dec 2006 11:07:51 +0100, Michael Erdmann wrote:

> after installing the latest version of GNAT on my linux box i encounter
> a problem with old source code. The Fragment below shows the problem:
> 
> ---- usage.ads
> generic
>    type Data_Type is private;
> package Usage is
> 
>    function Install return Boolean ;
>    procedure Doit( X : in out Data_Type );
> end Usage;
> 
> ---- usage.adb
> with Base;   use Base;
> 
> package body Usage is
> 
>    function Install return Boolean is
>    begin
>       return True;
>    end Install;
> 
>    procedure Doit( X : in out Data_Type ) is
>    begin
>       null;
>    end Doit;
> 
> begin
>    Register("Doit", Install'Access);    <<<< offending line
> end Usage;

I have some thought about it, and now I see it as a real problem.

The use case I have in mind is binding to a C library. To consider is
Register taking some access to a subprogram with C convention used to
interface a some C library.

This is a quite frequent pattern which now is broken, for a good reason
BTW, because it was unsafe. Obviously Register cannot be touched. So the
only solution is declaration of a local access type with a consequent
Unchecked_Conversion on it:

package body Usage is
   type Ptr is access function return Boolean;
   pragma Convention (C, Ptr);
   function To_Global is
      new Ada.Unchecked_Conversion (Ptr, C_Type_Of_Install);
   function Install return Boolean;
   pragma Convention (C, Install);
   function Install return Boolean is ... end Install;
   ...
begin
   Register (..., To_Global (Install'Access);

This is:

1. Ugly and error-prone
2. Defeats otherwise reasonable accessibility checks

Potential solution:
------------------------
1. Drop all accessibility checks for access to subprogram with

   pragma Convention (C, ...).

   Rationale: C subprograms are global

2. Prohibit pragma Convention (C, ...) in generic bodies for any subprogram
types not mentioned in the specification AND in all local scopes.

   Rationale: any subprogram of C convention shall be library-level

3. Prohibit instantiation of generic units with pragma Convention (C, ...)
used in specifications otherwise as at the library level.

The result should be the package Usage required to be instantiated on
library level, with Install exposed in the specification of. Explicit
pragma Convention (C, ...) in the specification would reject instantiations
without looking into bodies.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  parent reply	other threads:[~2007-01-23 18:18 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-17 10:07 How to get around "access type must not be outside generic unit" Michael Erdmann
2006-12-17 12:45 ` Pascal Obry
2006-12-17 14:28   ` Michael Erdmann
2006-12-17 15:53     ` Pascal Obry
2006-12-17 20:49     ` Robert A Duff
2006-12-18  3:35   ` Brian May
2006-12-18  7:49     ` Jean-Pierre Rosen
2006-12-18 23:15       ` Brian May
2006-12-19  1:48         ` Randy Brukardt
2006-12-19  7:41         ` Jean-Pierre Rosen
2006-12-18 20:32     ` Michael Erdmann
2006-12-18 20:57       ` Randy Brukardt
2007-01-23 18:18 ` Dmitry A. Kazakov [this message]
2007-01-24  0:57   ` [Revisited] How to get around "access type must not be outside generic unit" (was: How to get around "access type must not be outside generic unit") Randy Brukardt
2007-01-24 11:42     ` [Revisited] How to get around "access type must not be outside generic unit" Dmitry A. Kazakov
2007-01-24 11:58       ` Ludovic Brenta
2007-01-24 13:46         ` Dmitry A. Kazakov
2007-01-24 20:50       ` Randy Brukardt
2007-01-25 11:07         ` Dmitry A. Kazakov
replies disabled

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