comp.lang.ada
 help / color / mirror / Atom feed
* Re: Q: Interfacing C/ADA: Indirect Calls
  2001-04-18 12:54 Q: Interfacing C/ADA: Indirect Calls Tilman Gloetzner
@ 2001-04-18 11:46 ` Florian Weimer
  2001-04-18 13:56   ` Tilman Glotzner
  0 siblings, 1 reply; 4+ messages in thread
From: Florian Weimer @ 2001-04-18 11:46 UTC (permalink / raw)


Tilman Gloetzner <Tilman.Gloetzner@uundz.de> writes:

> use_indirect.adb:10:08: subprogram has invalid convention for context
> use_indirect.adb:16:08: subprogram has invalid convention for context

I think you have to insert

   pragma Convention (C, Indirect_Func);

to change the convention of the access-to-subprogram type.



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

* Q: Interfacing C/ADA: Indirect Calls
@ 2001-04-18 12:54 Tilman Gloetzner
  2001-04-18 11:46 ` Florian Weimer
  0 siblings, 1 reply; 4+ messages in thread
From: Tilman Gloetzner @ 2001-04-18 12:54 UTC (permalink / raw)


Hello,

here comes another question concerning Ada interfacing C. 
The idea is to call 2 C-functions inidrectly from an Ada program. The
test
program I wrote does not compile (I am using gnat running on linux):

gnatmake -c  use_indirect.adb -o use_indirect.o  
gcc -c use_indirect.adb
use_indirect.adb:10:08: subprogram has invalid convention for context
use_indirect.adb:16:08: subprogram has invalid convention for context
gnatmake: "use_indirect.adb" compilation error
make: *** [use_indirect.o] Error 4

The user guide nor the reference manual seem not to explicitly cover
this (at least
I did not find anything about indirect calls to code in other
languages).


Thanks in anticipation,

Tilman



========================================================================
/* C procedures */
#include <stdio.h>
 
 
int function1(int p1,int p2) {
 return(p1+p2);
}
 
int function2(int p1, int p2) {
 return(p1*p2);
}     

-- --------------- pacakage ind_call -----------------
with Interfaces.C;use Interfaces.C;
package ind_call is
 type Indirect_Func is access function(param_1 : in Int;
                                      param_2 : in Int)
                                     return Int;
 function C_Function1(p1: in Int; p2: in Int) return Int;
 function C_Function2(p1: in Int; p2: in Int) return Int;
 pragma Import(C,C_Function1,"function1");
 pragma Import(C,C_Function2,"function2");
end ind_call;  

----------- "main" program -----------------
with TEXT_IO;use TEXT_io;
with Interfaces.C; use Interfaces.C;
with ind_call;use ind_call;
 
procedure use_indirect is
 package Int_io is new INTEGER_IO(Int);
 f1: Indirect_Func;
 I:  Int;
begin
 f1 := C_Function1'Access;  -- this does not compile
 I  := f1(2,3);
 Text_io.Put("Function1 = ");
 Int_io.Put(I);
 New_line;
 f1 := C_Function2'Access;  -- this does not compile
 I  := f1(2,3);
 Text_io.Put("Function2 = ");
 Int_io.Put(I);
 New_line;
end use_indirect;



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

* Re: Q: Interfacing C/ADA: Indirect Calls
  2001-04-18 11:46 ` Florian Weimer
@ 2001-04-18 13:56   ` Tilman Glotzner
  2001-04-18 14:53     ` Marin David Condic
  0 siblings, 1 reply; 4+ messages in thread
From: Tilman Glotzner @ 2001-04-18 13:56 UTC (permalink / raw)


That works --  Great.

For my curiousity: What exactly is an access type ? Apperently, it is not a
reference to an object by memory address (like C references an object).

Thanks,

Tilman

"Florian Weimer" <fw@deneb.enyo.de> schrieb im Newsbeitrag
news:87n19emnv8.fsf@deneb.enyo.de...
> Tilman Gloetzner <Tilman.Gloetzner@uundz.de> writes:
>
> > use_indirect.adb:10:08: subprogram has invalid convention for context
> > use_indirect.adb:16:08: subprogram has invalid convention for context
>
> I think you have to insert
>
>    pragma Convention (C, Indirect_Func);
>
> to change the convention of the access-to-subprogram type.





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

* Re: Q: Interfacing C/ADA: Indirect Calls
  2001-04-18 13:56   ` Tilman Glotzner
@ 2001-04-18 14:53     ` Marin David Condic
  0 siblings, 0 replies; 4+ messages in thread
From: Marin David Condic @ 2001-04-18 14:53 UTC (permalink / raw)


It is very much like a reference to an object by memory address. In some
implementations of Ada, it will degenerate to exactly that. (As has been
observed in the past, access types are not simply addresses, because they do
more and may contain more information than just an address) For practical
usage, you can *think* of them as if they were just addresses, only with a
bunch of restrictions. An access object can only contain an object address
of some specific type. (Could be to a class-wide tagged type, so it is a bit
more flexible than might initially appear.) They used to be used pretty much
exclusively for dynamically allocated objects (roughly equivalent to
malloc's in C) but in Ada95 they can now point to just about anything. The
language rules that apply to access types are there for your safety. (You
can circumvent them, but you should have a good reason to do so.) They
attempt to guarantee that you can't leave around dangling references to
things that no longer exist.

Access types can be very powerful, but are not the most simple part of the
language. There is often a temptation on the part of C/C++ programmers to
over-utilize them. (For example, in passing parameters to subroutines to
achieve "pass by reference" when Ada is perfectly willing to handle all this
for you without you're having to get pointers to everything in sight.) If
you employ the use of access types, try to limit their scope and isolate
them as best you can. (For example, in building a linked list package) You
*really* don't need them anywhere near as much as you do in C/C++ so if you
find yourself using them all over the place, check your assumptions.
Otherwise, you can generally think of them as being similar to SomeType*
variables.

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com
Web:      http://www.mcondic.com/


"Tilman Glotzner" <Tilman.Gloetzner@uundz.de> wrote in message
news:9bk6ef$reo$01$1@news.t-online.com...
> That works --  Great.
>
> For my curiousity: What exactly is an access type ? Apperently, it is not
a
> reference to an object by memory address (like C references an object).
>
> Thanks,
>
> Tilman
>
> "Florian Weimer" <fw@deneb.enyo.de> schrieb im Newsbeitrag
> news:87n19emnv8.fsf@deneb.enyo.de...
> > Tilman Gloetzner <Tilman.Gloetzner@uundz.de> writes:
> >
> > > use_indirect.adb:10:08: subprogram has invalid convention for context
> > > use_indirect.adb:16:08: subprogram has invalid convention for context
> >
> > I think you have to insert
> >
> >    pragma Convention (C, Indirect_Func);
> >
> > to change the convention of the access-to-subprogram type.
>
>





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

end of thread, other threads:[~2001-04-18 14:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-04-18 12:54 Q: Interfacing C/ADA: Indirect Calls Tilman Gloetzner
2001-04-18 11:46 ` Florian Weimer
2001-04-18 13:56   ` Tilman Glotzner
2001-04-18 14:53     ` Marin David Condic

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