expand a module's toplevel macro calls for its importers

Its interface was read as written, so pub forms a macro wrote never
reached them, which list-macros' is-public? flag exists for. The
module's macros, and those its imports give it, are lent for the
reading and taken back after. One reading a type the module keeps to
itself cannot expand there, and gives importers nothing, as before.
This commit is contained in:
2026-10-19 14:18:56 +03:00
parent 7c897b6dc6
commit 1dd1cf0ae9
6 changed files with 141 additions and 11 deletions

View File

@@ -1,6 +1,7 @@
(import
scheme
(chicken base)
(chicken condition)
(chicken file)
(chicken load)
(chicken module)
@@ -9,6 +10,7 @@
(chicken string)
matchable
reader
sex-macros
srfi-1
types
utils)
@@ -118,9 +120,78 @@
;; pub fn taking it has to paste them that way round, or the prototype
;; names a type C has not seen. The declarations of the types its
;; importers see only behind a pointer go first.
(let ((raw-forms (read-module module-path)))
(append (check-public-interface raw-forms)
(interface-forms raw-forms))))
(let ((forms (module-forms (read-module module-path))))
(append (check-public-interface forms)
(interface-forms forms))))
;;; A module's forms with its toplevel macro calls expanded, so that the
;;; pub forms a macro writes reach importers as written ones do:
;;; (getter forty-two 42) gives them (pub fn forty-two () int). The
;;; macros are the module's own and those its imports give it, lent for
;;; the expanding and taken back after, so the importer is left with the
;;; pub ones alone. One that reads the type database sees what the
;;; importer knows, which a type private to the module is not; a call
;;; that cannot expand for that -- (print-struct box), box being the
;;; module's -- gives importers nothing, as before, and is the module's
;;; own compile's to check
(define +modules-expanded+ (list))
(define (module-forms raw-forms)
(cond ((assq raw-forms +modules-expanded+) => cdr)
(else (let ((forms (expanded raw-forms)))
(set! +modules-expanded+
(cons (cons raw-forms forms) +modules-expanded+))
forms))))
(define (expanded raw-forms)
(let ((lent (list)))
(define (lend! form)
(let ((name (macro-name form)))
(unless (assq name lent)
(set! lent (cons (cons name (macro-definition name)) lent)))
(defmacro (if (pub-form? form) (cddr form) (cdr form)))))
(define (expand form)
(cond ((macro-name form) (lend! form) (list form))
((splice? form) (append-map expand (cdr form)))
((macro? form)
(let ((result (handle-exceptions exn #f (apply-macro form))))
(cond ((or (not result) (null? result)) (list))
(else (stamp-form-source! result (form-source form))
(expand result)))))
(else (list form))))
(for-each lend! (imported-macro-forms raw-forms))
(let ((forms (append-map expand raw-forms)))
(for-each (lambda (entry) (restore-macro! (car entry) (cdr entry))) lent)
forms)))
(define (splice? form)
(and (pair? form) (eq? (car form) '$) (list? form)))
;;; The macros importing FORMS' modules gives: each one's pub macros and
;;; their helpers, and those of the modules it imports with pub
(define (imported-macro-forms forms)
(let next ((paths (append (imported-modules forms #t)
(imported-modules forms #f)))
(seen (list))
(acc (list)))
(cond ((null? paths) (concatenate (reverse acc)))
((member (car paths) seen) (next (cdr paths) seen acc))
(else
(let* ((raw (read-module (car paths)))
(helpers (helper-macros raw)))
(next (append (cdr paths) (imported-modules raw #t))
(cons (car paths) seen)
(cons (filter (lambda (form)
(and (macro-name form)
(or (pub-form? form) (memq form helpers))))
raw)
acc)))))))
(define (macro-name form)
(match form
(('defmacro (name . _) . _) name)
(('pub 'defmacro (name . _) . _) name)
(_ #f)))
(define (interface-forms raw-forms)
(let ((helpers (helper-macros raw-forms)))
@@ -135,12 +206,8 @@
;;; the importer expands them all, the way a C header that defines a
;;; macro has to define what it uses too
(define (helper-macros raw-forms)
(let* ((macro-name (lambda (form)
(match form
(('defmacro (name . _) . _) name)
(('pub 'defmacro (name . _) . _) name)
(_ #f))))
(private (filter (lambda (form) (and (macro-name form) (not (pub-form? form))))
(let* ((private (filter (lambda (form)
(and (macro-name form) (not (pub-form? form))))
raw-forms))
(mentions (lambda (form name)
(let walk ((x (cdr form)))
@@ -392,7 +459,7 @@
(define (module-exports path)
(cond ((assoc path +module-exports+) => cdr)
(else (let* ((forms (interface-forms (read-module path)))
(else (let* ((forms (interface-forms (module-forms (read-module path))))
(exports (cons (declared-types forms)
(imported-modules forms #f))))
(set! +module-exports+ (cons (cons path exports) +module-exports+))