keep an aggregate written in place behind a pointer

A type was flattened word by word, a struct's body included, so
(* (struct ((c int)))) came out struct c{ int; } *.
This commit is contained in:
2026-10-17 17:35:48 +03:00
parent d9b9316eb0
commit 62ca848de8
2 changed files with 30 additions and 6 deletions

View File

@@ -7,7 +7,7 @@
(chicken module)
(chicken string)
(chicken syntax)
brev-separate ; fn, flatten
brev-separate ; fn
fmt
sex-fmt-c
matchable
@@ -456,6 +456,7 @@ forms, and what remains."
(define (has-pointer-star? form)
(and (pair? form)
(not (structured-type? form))
(not (inline-aggregate? form))
(or (memq '* form)
(any has-pointer-star? (filter pair? form)))))
@@ -485,11 +486,22 @@ forms, and what remains."
(sex-error type "a fn type is a function pointer already: write (fn ...), not (* (fn ...))" type)
(sex-error type "a pointer to an array is not supported" type))))
(if (atom? type) (atom-to-fmt-c type)
(flatten
(tree-map atom-to-fmt-c
(flatten
(list-join (reverse (list-split type '*))
'(*)))))))
(type-words (list-join (reverse (list-split type '*)) '(*)))))
;;; A type's words, flat, except that an aggregate written out in full is
;;; one of them, walked: (* (struct ((c int)))) is `struct {...} *'
(define (type-words items)
(append-map (lambda (item)
(cond ((inline-aggregate? item) (list (walk-type item)))
((pair? item) (type-words item))
((null? item) (list))
(else (list (atom-to-fmt-c item)))))
items))
;;; (struct ((c int))), (union u (...)), (enum (red green)): a body, not
;;; just a tag
(define (inline-aggregate? form)
(and (pair? form) (memq (car form) '(struct union enum)) (any list? (cdr form))))
(define (walk-fn-def form)
(match form

View File

@@ -847,6 +847,18 @@ whitespace aside, THEN?"
(test-assert "and negation beside a binary operator"
(emits? (in-fn "(var n int (+ (- a) b))") "-a + b")))
;; A type was flattened word by word, a struct written out in it
;; included: (* (struct ((c int)))) came out `struct c{ int; } *'
(test-group "a pointer to an aggregate written in place"
(test-assert "keeps its fields"
(followed-by? "(struct h ((b (* (struct ((c int)))))))" "int c;" "} * b;"))
(test-assert "a pointer among them as well"
(emits? "(struct h ((b (* (struct ((p (* int)) (n int)))))))" "int * p;"))
(test-assert "and a qualifier before it"
(emits? "(struct h ((e (* const (union ((u int) (v float)))))))" "const union{"))
(test-assert "behind as many stars as are written"
(emits? "(struct h ((f (* * (struct ((c int)))))))" "} * * f;")))
;; c-struct/aux wrote the tag and the first `*' after it, and
;; dropped the rest of the chain
(test-group "a pointer chain to a struct"