#216 [cffi] Fix CFFI conversion of constant expressions

open
nobody
None
5
2012-12-21
2008-09-02
Dmitry Kalyanov
No

CFFI converts constant expressions in #define'd constants and in enumeration items into equivalent lisp expressions. This patch (hopefully) fixes this conversion.
This patch implements a simple naive parser for constant expressions (replacing even more naive approach with a more correct one), memoization of previously met constant values (so that code like
enum { A = 123, B = A + 10 };
would translate into correct lisp expression). Still broken things are: not supporting ternary operator, somewhat wrong operators precedence order and associativity, but fixing those would require a full parser.
Also, one small fix goes into SWIG's parser: it would incorrectly parse character constant expressions in enumeration items: in
enum { A = '1' };
for '1' it would get node with enumvalue="1", type="int" instead of enumvalue="'1'", type="int", which makes correct parsing of such expressions impossible. Not fixed is parsing of expressions like
enum { A = '1' + 1 };
which would parse into enumvalue="1+1", type="int" instead of enumvalue="'1'+1", type="int" (again, which makes correct generation of lisp-side code impossible).

Discussion

  • Logged In: YES
    user_id=2201504
    Originator: YES

    Attaching a better variant of patch.
    Changes:
    1) Using custom macro define-constant instead of cl:defconstant (see http://www.sbcl.org/manual/Defining-Constants.html\)
    2) Better handling of character constants and arithemitcs with them
    3) Better handling of escape sequences in characters and strings;
    4) Safe way of specifying string literals with non-ascii characters.
    5) Change to c parser to retain the single quotes in character constants. This probably fixes some problems not only with cffi, but with some other modules.
    File Added: swig-full.patch

     
  • Version 2 of the patch

     
    Attachments
  • Olly Betts
    Olly Betts
    2008-09-17

    I don't have the time to check right now, but the fix for character constants sounds like this bug:

    http://sourceforge.net/tracker2/?func=detail&aid=1474463&group_id=1645&atid=101645

    I applied a fix for that but had to revert it. I forget the exact details, but it was discussed on swig-dev at the time.

    Anyway, this patch could well fix that bug. Or it could have the same flaw my fix did (I've checked and it isn't the same change I made).