comp.lang.ada
 help / color / mirror / Atom feed
* Are 'pragma Export' and access types mutually exclusive?
@ 1997-08-08  0:00 Tom Weis
  1997-08-08  0:00 ` Robert A Duff
  1997-08-09  0:00 ` Robert Dewar
  0 siblings, 2 replies; 3+ messages in thread
From: Tom Weis @ 1997-08-08  0:00 UTC (permalink / raw)



I'm using gnat-3.09-sparc-sun-sun0s4.1.3-bin.tar.gz.
 
I would like to define a procedure in a package and make
it visible:
  1) to a C program (via pragma Export)
  2) to an Ada program (via an access type)
 
The GNAT compiler gives an error when I try to make
a procedure visible in both fashions simultaneously:
 
Error Message:
-------------
package1.ads:10:25: subprogram has invalid convention for context
 
 
If I comment out the 'pragma Export' the compilation
error goes away and the program compiles and links.
Should "pragma Export (C, proc)" and "proc'access" be
mutually exclusive?  Are all bets off the table when you
invoke a pragma with respect to other Ada primitives?
 
P.S. I'm using this simplified version of the package to
reduce the size of the example code.  My hope would be to
eventually have a structure with multiple access types
populated by many functions from multiple packages.  So
I'm not interested in responses which say use
Package.Procedure to refer to the procedure in Ada. I'd
like to keep the discussion on point.  Thanks.
 
--================================================
-- package1.ads
--================================================
package Package1 is
 
   procedure Integer_Procedure1(Arg_In : in out Integer);
   procedure Integer_Procedure2(Arg_In : in out Integer);
   pragma Export(C, Integer_Procedure2,
                    "Integer_Procedure2");
 
   type fcn_access
     is access procedure (Arg_In: in out Integer);
 
   fcn1 : fcn_access := Integer_Procedure1'access;
   fcn2 : fcn_access := Integer_Procedure2'access;
 
end Package1;
--================================================
-- package1.adb
--================================================
with Ada.Integer_Text_IO;
with Ada.Text_IO;
 
package body Package1 is
 
   procedure Integer_Procedure1(Arg_In : in out Integer)
is
   begin
      Ada.Integer_Text_IO.Put(Arg_In);
      Ada.Text_IO.New_Line;
   end Integer_Procedure1;
 
   procedure Integer_Procedure2(Arg_In : in out Integer)
is
   begin
      Ada.Integer_Text_IO.Put(Arg_In);
      Ada.Text_IO.New_Line;
   end Integer_Procedure2;
 
end Package1;
--================================================
-- main.adb
--================================================
with Ada.Integer_Text_IO;
with Ada.Text_IO;
with Package1;
 
procedure main is
   Integer_Arg : Integer := 10;
begin
   -- Call Package1.Integer_Procedure1 with access type
   Package1.fcn1.all(Integer_Arg);
end main;




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

* Re: Are 'pragma Export' and access types mutually exclusive?
  1997-08-08  0:00 Are 'pragma Export' and access types mutually exclusive? Tom Weis
@ 1997-08-08  0:00 ` Robert A Duff
  1997-08-09  0:00 ` Robert Dewar
  1 sibling, 0 replies; 3+ messages in thread
From: Robert A Duff @ 1997-08-08  0:00 UTC (permalink / raw)



In article <33EBA058.5680@mathworks.com>,
Tom Weis  <tom_weis@mathworks.com> wrote:
>   procedure Integer_Procedure1(Arg_In : in out Integer);
>   procedure Integer_Procedure2(Arg_In : in out Integer);
>   pragma Export(C, Integer_Procedure2,
>                    "Integer_Procedure2");
> 
>   type fcn_access
>     is access procedure (Arg_In: in out Integer);
> 
>   fcn1 : fcn_access := Integer_Procedure1'access;
>   fcn2 : fcn_access := Integer_Procedure2'access;

You need to make the access type have the same calling convention as the
subprograms.  By default, fcn_access has convention Ada, so use "pragma
Convention(C, fcn_access);".  You will then need to use pragma
Convention on Integer_Procedure1, too.

If X is of type fcn_access, then when the compiler sees a call to X.all,
it needs to know where to put the parameters and so forth -- that is, it
needs to know the calling convention.  It can't be both C and Ada -- it
has to be one or the other.  The compiler doesn't know where that access
value came from, so the matching-conventions rule is necessary.

- Bob




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

* Re: Are 'pragma Export' and access types mutually exclusive?
  1997-08-08  0:00 Are 'pragma Export' and access types mutually exclusive? Tom Weis
  1997-08-08  0:00 ` Robert A Duff
@ 1997-08-09  0:00 ` Robert Dewar
  1 sibling, 0 replies; 3+ messages in thread
From: Robert Dewar @ 1997-08-09  0:00 UTC (permalink / raw)



Tom asks a question about export. here is a simplifid version of his
program:

procedure z is

  type x is access procedure;

  procedure l;
  pragma Export (C, L);

  procedure l is begin null; end;
  xx : x;

begin
  xx := l'access;
end;


This program is most certainly illegal, since conventions must match for
the use of access (that's obvious if you think about it, you cannot have
an access values that sometimes points to convention C things, and sometimes
to convention Ada things (how would you call such a beast if the calling
conventions were different?)

The program is easily fixed by making sure the conventions match.






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

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

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-08-08  0:00 Are 'pragma Export' and access types mutually exclusive? Tom Weis
1997-08-08  0:00 ` Robert A Duff
1997-08-09  0:00 ` Robert Dewar

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