From 85e222e9699855668e1157f31e3938ebeffec088 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Sun, 12 Mar 2023 02:24:50 -0300 Subject: Fix enum generation --- cmamut.lisp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'cmamut.lisp') diff --git a/cmamut.lisp b/cmamut.lisp index e37da7d..4f90a37 100644 --- a/cmamut.lisp +++ b/cmamut.lisp @@ -2,6 +2,7 @@ (defparameter +target-package+ (gensym)) +; TODO: Handle cases where the header file cannot be found (defun get-raw-spec (filename) (sb-ext:run-program "/usr/bin/c2ffi" (list filename "--macro-file" "defines.h" @@ -15,6 +16,7 @@ (delete-file "defines.h") (concatenate `(simple-vector ,(+ (length spec) (length defines))) spec defines))) +; TODO: Handle different sizes of enum (defun cook-type (raw-type type-associations) (let ((tag (gethash "tag" raw-type))) (cond ((string= tag ":void") @@ -61,7 +63,7 @@ ((string= tag ":union") (list 'sb-alien:union (intern (gethash "name" raw-type) +target-package+))) ((string= tag ":enum") - (list 'sb-alien:enum (gethash "name" raw-type))) + 'sb-alien:int) ((or (string= tag "struct") (string= tag "union")) (cook-composite raw-type type-associations)) ((string= tag "__builtin_va_list") @@ -104,7 +106,9 @@ ,(gethash "value" raw-const))) (defun cook-enum (raw-enum enum-references) - (let ((name (gethash (gethash "id" raw-enum) enum-references)) + (let ((name (if (> (gethash "id" raw-enum) 0) + (gethash (gethash "id" raw-enum) enum-references) + (gethash "name" raw-enum))) (raw-fields (gethash "fields" raw-enum)) (cooked-fields (queue:new))) (dotimes (j (length raw-fields)) @@ -119,9 +123,10 @@ (defun generate-enum-references (raw-typedefs) (let ((enum-references (make-hash-table :test #'equal :size 256))) (dolist (i raw-typedefs) - (let ((base-type (gethash "type" i))) - (when (string= (gethash "tag" base-type) ":enum") - (setf (gethash (gethash "id" base-type) enum-references) + (let ((inner-type (gethash "type" i))) + (when (string= (gethash "tag" inner-type) ":enum") + (setf (gethash "name" inner-type) (gethash "name" i)) + (setf (gethash (gethash "id" inner-type) enum-references) (gethash "name" i))))) enum-references)) @@ -129,8 +134,6 @@ (defun generate-type-associations (raw-typedefs) (let ((type-associations (make-hash-table :test #'equal :size 256))) (dolist (i raw-typedefs) - (when (string= (gethash "tag" (gethash "type" i)) ":enum") - (setf (gethash "name" (gethash "type" i)) (gethash "name" i))) (setf (gethash (gethash "name" i) type-associations) (gethash "type" i))) (labels ((flatten-typedef (k v) -- cgit v1.2.3