chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] [Swig-dev] Future of SWIG chicken module


From: Felix Winkelmann
Subject: Re: [Chicken-users] [Swig-dev] Future of SWIG chicken module
Date: Tue, 10 Feb 2004 08:21:49 +0100
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.6) Gecko/20040113

John Lenz wrote:

I still think it would be a cool thing tho to still call SWIG directly from chicken, although I don't know how well that would work. If chicken is asked to parse some header files, it could first look for SWIG and if found, it could run SWIG passing it the input file. SWIG would then still generate a seperate file containing all the wrapper functions which would output in the same directory as the rest of the files chicken was exporting. SWIG would then have to return all the . scm files that it created for chicken to continue processing. Maybe just have a seperate #>SWIG <# or something. There are still some problems with this tho which would need to be investigated...

Perhaps a simple command-line option? So

csc -swig foo.scm

could invoke swig and expand

#>?      /* (#! as well? probably emit proper swig directive...) */
...
<#

into

(declare (foreign-declare "#include \"foo_wrap.c\"")) ; wrappers
(include "foo_init.scm") ; the setup code (which currently is written to 
foo.scm)

<code using it>



----------------------------------------------------------------------
Well, the one big thing we need to fix is the type checking. The chicken SWIG module is completly broken... it will accept any type to any argument that takes a pointer. Secondly, it does the incorrect thing on multiple inheritance.

To see the problem with multiple inheritance, take a look at this  example

(define-foreign-type foo (pointer "Foo"))
(define-foreign-type bar (pointer "Bar"))
(define-foreign-type joo (pointer "Joo"))

#>
 class Foo { public:  int a; };
 class Bar { public:  int b; };
 class Joo : public Foo, public Bar { public: int c; };

 Foo *newJoo() {
   Joo *j = new Joo();
   j->a = 1; j->b = 2; j->c = 3;
   return j;
 }

 int heya(Bar *b) { return b->b; }
<#

(define new-Joo (foreign-lambda joo "newJoo"))
(define heya (foreign-lambda int "heya" bar))

(display (heya (new-Joo)))

That should display 2 but it displays 1. The problem is, inside the class Joo, the Bar base class is not at the beginning of the memory assigned to Joo so when doing a direct cast from C_word to Bar *, the pointer is pointing at the Foo part of Joo. SWIG can automaticly detect this and will convert the pointer to the correct type, but again the SWIG chicken module currently does not.

Right. But note that the above example will not work in C++ as well,
unless newJoo is declared to return a pointer to Joo (the compiler
will signal an error - at least VC++ 6.0 does). Doing an explicit
reinterpret_cast from Foo* to Bar* will make it compile but print 1.
Incidentally, compiled with Chicken's built-in parser the example will
print 1 as well. But that's ok. It's not aimed at catching all of C++s
semantic pitfalls...

Anyway, thanks for your feedback. I would be very happy to see the
SWIG chicken module fixed.


cheers,
felix





reply via email to

[Prev in Thread] Current Thread [Next in Thread]