diff --git a/semen.scm b/semen.scm index e7ff9b49..a51ec966 100644 --- a/semen.scm +++ b/semen.scm @@ -1822,13 +1822,15 @@ (define (lift-closure base form env) (match form (('closure arglist ret-type captures . body) + ;; its call helper would have to forward the rest, which C cannot + (when (any (lambda (arg) (or (eq? arg '...) (equal? arg '(...)))) arglist) + (sex-error form "a closure cannot be variadic; a lambda can")) (check-capture-names form arglist (map capture-name captures) body) (let* ((caps (map (lambda (c) (capture-binding c form env)) captures)) (record (suffixed base "_captures")) (code (suffixed base "_code")) (construct (suffixed base "_make")) - (type `(closure ,(map (lambda (p) (list (second p))) arglist) - ,ret-type)) + (type `(closure ,(arglist-types arglist) ,ret-type)) (closure (register-closure-type! type form))) (values construct @@ -1841,7 +1843,7 @@ (list `(struct ,record ,(map capture-field caps)))) (list - `(fn ,code ((ƛe (* void)) ,@arglist) ,ret-type + `(fn ,code ((ƛe (* void)) ,@(named-parameters arglist)) ,ret-type ,@(if (null? caps) (list) `((var ƛcaptures (* (struct ,record)) ƛe) @@ -1865,6 +1867,17 @@ (return ƛc)))))))) (else (sex-error form "malformed closure" form)))) +;;; A definition names every parameter before C23, so one written as a +;;; type alone gets a name nothing reads: ((int) (b int)) -> ((ƛu0 int) (b int)) +(define (named-parameters arglist) + (map (lambda (arg index) + (if (or (named-arg? arg) (comment-form? arg)) + arg + (list (string->symbol (string-append "ƛu" (number->string index))) + (unwrap-type arg)))) + arglist + (iota (length arglist)))) + ;;; Captures are a closure's other parameters, and C's rule for those ;;; holds of them: no two alike, and none redeclared at the top of the ;;; body. A block inside may shadow one, as it may a parameter. diff --git a/tests/codegen.scm b/tests/codegen.scm index 6ec78944..ff51b63d 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -433,6 +433,18 @@ (test-assert "while a block inside may shadow it" (emits? (in-fn "(var c (closure () int) (closure () int (a) (do (var a int 5) (g a)) (return a)))") "int a = 5;")) + ;; the closure's type was built from each parameter's second element, + ;; which a type alone has none of + (test-assert "a parameter may name nothing" + (emits? (in-fn "(var c (closure ((int) (int)) int) (closure ((int) (b int)) int () (return b)))") + "struct ƛint__int")) + (test-assert "and its code names it, a definition needing one before C23" + (emits? (in-fn "(var c (closure ((int) (int)) int) (closure ((int) (b int)) int () (return b)))") + "int ƛu0, int b)")) + ;; the call helper would have to forward the rest, which C cannot + (test-assert "a closure cannot be variadic" + (reports? (in-fn "(var c _ (closure ((n int) ...) int () (return n)))") + "a closure cannot be variadic; a lambda can")) ;; The environment is one shared union, declared in the prelude -- ;; its layout is part of the ABI two units agree on, so it cannot ;; depend on what either file contains. `sex->c' has no prelude, so diff --git a/tests/sex-programs/closures.sex b/tests/sex-programs/closures.sex index adca6323..94943922 100644 --- a/tests/sex-programs/closures.sex +++ b/tests/sex-programs/closures.sex @@ -12,7 +12,8 @@ "Captured pointer: 11 then 12" "From a bare fn: 20 42 7" "Captured array: 6 3, still 6" - "Captured const: 6") + "Captured const: 6" + "Unnamed parameter: 2") (return 0) ;;; A closure is a code pointer beside its captures, so what this @@ -135,4 +136,7 @@ (var k (const int) 5) (var plus-k (closure ((int)) int) (closure ((v int)) int (k) (return (+ v k)))) (printf "Captured const: %d\n" (plus-k 1)) + + (var second (closure ((int) (int)) int) (closure ((int) (b int)) int () (return b))) + (printf "Unnamed parameter: %d\n" (second 1 2)) (return 0))