From e83a3b4a966602a8958415efe409e9564883a38f Mon Sep 17 00:00:00 2001 From: alex-eg Date: Sun, 18 Oct 2026 22:02:55 +0300 Subject: [PATCH] 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). --- sex-fmt-c.scm | 25 ++++++++++++++++++------- tests/codegen.scm | 14 ++++++++++++++ tests/sex-programs/type-shapes.sex | 9 ++++++++- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/sex-fmt-c.scm b/sex-fmt-c.scm index 31f847d..50c4517 100644 --- a/sex-fmt-c.scm +++ b/sex-fmt-c.scm @@ -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 diff --git a/tests/codegen.scm b/tests/codegen.scm index 41f5f49..e80d719 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -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" diff --git a/tests/sex-programs/type-shapes.sex b/tests/sex-programs/type-shapes.sex index ebcc036..6550ad4 100644 --- a/tests/sex-programs/type-shapes.sex +++ b/tests/sex-programs/type-shapes.sex @@ -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))