modules: save fn docstring in prototype

This commit is contained in:
2026-09-19 23:54:50 +03:00
parent ca71ae6e91
commit 05929951cb

View File

@@ -8,6 +8,7 @@
(chicken process-context) (chicken process-context)
(chicken string) (chicken string)
fmt fmt
matchable
reader reader
srfi-1 srfi-1
utils) utils)
@@ -71,22 +72,33 @@
(list) (list)
raw-forms))) raw-forms)))
(define (public-fn-interface form)
;; (pub fn name args ret . body) -> prototype, keeping a docstring so
;; the importer can emit it above the declaration
(match form
(('pub 'fn name args ret)
form)
(('pub 'fn name args ret ('comment . _) . rest)
(public-fn-interface `(pub fn ,name ,args ,ret ,@rest)))
(('pub 'fn name args ret (? string? doc) . _)
`(pub fn ,name ,args ,ret ,doc))
(('pub 'fn name args ret . _)
`(pub fn ,name ,args ,ret))))
;;; TODO: use semen facilities to analyze modules ;;; TODO: use semen facilities to analyze modules
(define (process-public-interface-form form acc) (define (process-public-interface-form form acc)
(case (car form) (match form
((pub) ;; Reduced to a prototype, still `pub', so the importer declares it
(case (cadr form) ;; with external linkage
;; A function is reduced to a prototype and keeps its `pub', so (('pub 'fn . _)
;; the importing unit declares it with external linkage (cons (copy-form-source! form (public-fn-interface form)) acc))
((fn) (('pub 'var name type . _)
(cons (copy-form-source! form (take form 5)) acc)) (cons (copy-form-source! form `(extern var ,name ,type)) acc))
;; A variable becomes an `extern' declaration (('pub (or 'define 'defmacro 'enum 'import 'include 'struct 'typedef 'union) . _)
((var) (cons (copy-form-source! form (cdr form)) acc))
(cons (copy-form-source! form (cons 'extern (take (cdr form) 3))) acc)) (('pub . _)
((define defmacro enum import include struct typedef union) (sex-error form "pub must be followed by a definition" form))
(cons (copy-form-source! form (cdr form)) acc)) (_ acc)))
(else (sex-error form "pub must be followed by a definition" form))))
(else acc)))
(define (load-persistent-module-paths) (define (load-persistent-module-paths)
(let ((sex-module-path-env-var (let ((sex-module-path-env-var