From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,8f7d6c5172a1d41b X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" 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") Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: Date: Tue, 23 Jan 2007 19:18:20 +0100 Message-ID: <3vwf1b4b2ntl$.l9n17zmh9v8a$.dlg@40tude.net> NNTP-Posting-Date: 23 Jan 2007 19:18:15 CET NNTP-Posting-Host: 14c57442.newsspool3.arcor-online.net X-Trace: DXC=VK:STZ9fY@lm7>ihJR;B_cMcF=Q^Z^V3h4Fo<]lROoRagUcjd<3m<;bOVKJ1@MKl2n[6LHn;2LCVn[ 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