Skip to content

Commit

Permalink
Fix FFI load-library failure
Browse files Browse the repository at this point in the history
The code didn't handle when an identifier was typedef'ed to
types other than basic ones.
This was the cause of #1044
  • Loading branch information
shirok committed Aug 27, 2024
1 parent f7f053e commit f107bcf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2024-08-27 Shiro Kawai <shiro@acm.org>

* lib/lang/c/type.scm (c-actual-type): Fix the case when identifier
is typedef'ed to an aggregate type. The old code assumed the
inner type was always a basic type.
This was the cause of https://github.com/shirok/Gauche/issues/1044

2024-08-11 Shiro Kawai <shiro@acm.org>

* lib/srfi/190.scm: Proper support of srfi-190, based on
Expand Down
31 changes: 15 additions & 16 deletions lib/lang/c/type.scm
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,22 @@

;; Add qualifier to the 'main' type, which would be attached to the variable.
(define (attach-qualifier c-type qual)
(define (merge-quals new-qs qs)
(if (null? new-qs)
qs
(let1 qs (merge-quals (cdr new-qs) qs)
(if (memq (car new-qs) qs)
qs
(cons (car new-qs) qs)))))
(match c-type
[('.pointer qs inner) `(.pointer ,(cons qual qs) ,inner)]
[('.pointer qs inner) `(.pointer ,(merge-quals qual qs) ,inner)]
[('.array inner . rest) `(.array ,(attach-qualifier inner qual) ,@rest)]
[('.function qs . rest) `(.function ,(cons qual qs) ,@rest)]
[('.struct tag qs . rest) `(.struct ,tag ,(cons qual qs) ,@rest)]
[('.union tag qs . rest) `(.union ,tag ,(cons qual qs) ,@rest)]
[('.enum tag qs . rest) `(.enum ,tag ,(cons qual qs) ,@rest)]
[('.type name qs inner) `(.type ,name ,(cons qual qs) ,inner)]
[(btype qs) `(,btype ,(cons qual qs))]))
[('.function qs . rest) `(.function ,(merge-quals qual qs) ,@rest)]
[('.struct tag qs . rest) `(.struct ,tag ,(merge-quals qual qs) ,@rest)]
[('.union tag qs . rest) `(.union ,tag ,(merge-quals qual qs) ,@rest)]
[('.enum tag qs . rest) `(.enum ,tag ,(merge-quals qual qs) ,@rest)]
[('.type name qs inner) `(.type ,name ,(merge-quals qual qs) ,inner)]
[(btype qs) `(,btype ,(merge-quals qual qs))]))

;; Types and its "weight". The latter is used for type upgrading.
(define-constant *integral-type-weight*
Expand Down Expand Up @@ -391,17 +398,9 @@

;; strip typedefs and returns the actual type
(define (c-actual-type c-type)
(define (merge-quals new-qs qs)
(if (null? new-qs)
qs
(let1 qs (merge-quals (cdr new-qs) qs)
(if (memq (car new-qs) qs)
qs
(cons (car new-qs) qs)))))
(match c-type
[('.type _ quals inner)
(match-let1 (t qs) (c-actual-type inner)
`(,t ,(merge-quals quals qs)))]
(attach-qualifier inner quals)]
[_ c-type]))

;;;
Expand Down

0 comments on commit f107bcf

Please sign in to comment.