From 62ca848de821f18ca4becda398b1e692d729dc99 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Sat, 17 Oct 2026 17:35:48 +0300 Subject: [PATCH] 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; } *. --- fmt-c-writer.scm | 24 ++++++++++++++++++------ tests/codegen.scm | 12 ++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/fmt-c-writer.scm b/fmt-c-writer.scm index 91315ad..97a920c 100644 --- a/fmt-c-writer.scm +++ b/fmt-c-writer.scm @@ -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 diff --git a/tests/codegen.scm b/tests/codegen.scm index 8995104..d19a641 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -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"