Teach the groveler's cstruct :type to guess type when :auto is given.
authorJoshua Elsasser <joshua@elsasser.org>
Thu, 22 Nov 2012 19:41:45 +0000 (14:41 -0500)
committerLuís Oliveira <loliveira@common-lisp.net>
Sun, 6 Jan 2013 20:53:17 +0000 (20:53 +0000)
This means that integer size and signedness is determined by the
groveler in cases like this:

(cstruct autotype "struct mystruct"
 (myint "myint" :type :auto)
 (myarray "myarray" :type :auto :count :auto))

grovel/common.h
grovel/grovel.lisp

index b44b0c2..895059f 100644 (file)
 
 #define TYPE_SIGNED_P(type) (((type)-1)<0LL)
 #define _64_BIT_VALUE_FITS_SIGNED_P(value) ( (value) <= 0x7FFFFFFFFFFFFFFFLL )
+#define SLOT_SIGNED_P(result, type, slot)                              \
+  do {                                                                         \
+    type slot_signed_p_struct;                                                 \
+    slot_signed_p_struct.slot = -1;                                    \
+    (result) = slot_signed_p_struct.slot < 0;                          \
+  } while (0)
 
 void type_name(FILE *output, int signed_p, int size) {
   if (signed_p) {
index b7d2579..e582fe9 100644 (file)
@@ -65,6 +65,7 @@
 #include <grovel/common.h>
 
 int main(int argc, char**argv) {
+  int autotype_tmp;
   FILE *output = argc > 1 ? fopen(argv[1], \"w\") : stdout;
   fprintf(output, \";;;; This file has been automatically generated by \"
                   \"cffi-grovel.\\n;;;; Do not edit it by hand.\\n\\n\");
@@ -494,7 +495,17 @@ int main(int argc, char**argv) {
         (c-format out "~%  (")
         (c-print-symbol out slot-lisp-name t)
         (c-format out " ")
-        (c-print-symbol out type)
+        (etypecase type
+          ((eql :auto)
+           (format out "~&  SLOT_SIGNED_P(autotype_tmp, ~A, ~A~@[[0]~]);~@*~%~
+                        ~&  type_name(output, autotype_tmp, sizeofslot(~A, ~A~@[[0]~]));~%"
+                   struct-c-name
+                   slot-c-name
+                   (not (null count))))
+          ((and symbol (not null))
+           (c-print-symbol out type))
+          (string
+           (c-format out "~A" type)))
         (etypecase count
           (null t)
           (integer