nest a function type's declarator round the name

A function type's return was written ahead of its declarator, so one
returning a function pointer came out int (*)(int) get (void), and a
variable of such a type as void (*)() (*g)(int).
This commit is contained in:
2026-10-18 22:02:55 +03:00
parent 17d1467d1e
commit e83a3b4a96
3 changed files with 40 additions and 8 deletions

View File

@@ -783,9 +783,12 @@
(c-in-stmt (apply c-begin body)))
fl))
;; MODIFIED FROM UPSTREAM fmt-c: the name and parameters are the
;; return type's declarator, so a function returning a function pointer
;; is int (*get (void))(int), not int (*)(int) get (void)
(define (c-prototype type name params . o)
(c-wrap-stmt
(cat (c-type type) " " (c-expr name) " (" (c-param-list params) ")"
(cat (c-type type (cat (c-expr name) " (" (c-param-list params) ")"))
(fmt-join/prefix c-expr o " "))))
(define (c-static x) (cat "static " (c-expr x)))
@@ -803,10 +806,14 @@
(cond
((pair? type)
(case (car type)
;; MODIFIED FROM UPSTREAM fmt-c: the declarator goes round the
;; name, inside the return type's own, so a function type
;; returning one nests: void (*(*g)(int))()
((%fun)
(cat (c-type (cadr type) #f)
" (*" (or name "") ")("
(fmt-join (lambda (x) (c-type x #f)) (caddr type) ", ") ")"))
(c-type (cadr type)
(cat "(*" (or name "") ")("
(fmt-join (lambda (x) (c-type x #f)) (caddr type) ", ")
")")))
;; MODIFIED FROM UPSTREAM fmt-c: a nameless declarator -- an
;; array parameter of a function type, where C has no room
;; for a name -- arrives as #f, and upstream printed it
@@ -842,10 +849,14 @@
(cond
((pair? type)
(case (car type)
;; MODIFIED FROM UPSTREAM fmt-c: the declarator goes round the
;; name, inside the return type's own, so a function type
;; returning one nests: void (*(*g)(int))()
((%fun)
(cat (c-type (cadr type) #f)
" (*" (or name "") ")("
(fmt-join (lambda (x) (c-type x #f)) (caddr type) ", ") ")"))
(c-type (cadr type)
(cat "(*" (or name "") ")("
(fmt-join (lambda (x) (c-type x #f)) (caddr type) ", ")
")")))
;; MODIFIED FROM UPSTREAM fmt-c: a nameless declarator -- an
;; array parameter of a function type, where C has no room
;; for a name -- arrives as #f, and upstream printed it

View File

@@ -885,6 +885,20 @@ whitespace aside, THEN?"
(test-assert "behind as many stars as are written"
(emits? "(struct h ((f (* * (struct ((c int)))))))" "} * * f;")))
;; A function type's return was written ahead of its declarator, so a
;; function returning one came out `int (*)(int) get (void)'
(test-group "a function type returning one"
(test-assert "a function returning a function pointer"
(emits? "(fn inc ((x int)) int (return x)) (fn get () (fn ((int)) int) (return inc))"
"int (*get (void))(int)"))
(test-assert "and its prototype"
(emits? "(pub fn get () (fn ((int)) int))" "int (*get (void))(int);"))
(test-assert "a variable of one"
(emits? "(var g (fn ((int)) (fn () void)))" "void (*(*g)(int))()"))
(test-assert "and a parameter"
(emits? "(fn take ((f (fn ((int)) (fn () void)))) void (return))"
"take (void (*(*f)(int))())")))
;; 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"

View File

@@ -9,7 +9,8 @@
"unnamed parameters: 7 -1 2"
"typedefs: hi 4 2"
"sizeof a pointer type: 1 1"
"dimensions: 6 12 5")
"dimensions: 6 12 5"
"returned function: 42")
(return 0)
;;; Three questions about a written type that used to be answered in
@@ -29,6 +30,9 @@
(typedef vec4 [float 4])
(typedef unary (fn ((small)) small))
(fn successor ((n int)) int (return (+ n 1)))
(fn stepper () (fn ((int)) int) (return successor))
(define CAP 2)
;;; a parameter that names nothing is a type, however many words it
@@ -101,4 +105,7 @@
(var g [int 2 3] #(#(0 1 2) #(3 4 5)))
(printf "dimensions: %zu %zu %d\n"
(/ (sizeof g) (sizeof int)) (sizeof [g 0]) [g 1 2])
;; a function type is a declarator wrapped round the name
(printf "returned function: %d\n" ((stepper) 41))
(return 0))