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 2bc07a5a5f
commit d62a1d4c97
6 changed files with 143 additions and 12 deletions

View File

@@ -16,7 +16,9 @@
get-macro get-macro
macro? macro?
apply-macro apply-macro
defmacro) defmacro
macro-definition
restore-macro!)
(define (cat-syms s-1 s-2) (define (cat-syms s-1 s-2)
(fmt #f s-1 s-2)) (fmt #f s-1 s-2))
@@ -91,6 +93,17 @@
(sex-error form "macro takes at least this many arguments" (sex-error form "macro takes at least this many arguments"
name required))))) name required)))))
;;; What a name is defined as a macro to, or #f; and putting that back,
;;; for a macro lent for a while -- a module's, while its interface is
;;; read
(define (macro-definition name)
(get name 'sex-macro))
(define (restore-macro! name definition)
(if definition
(put! name 'sex-macro definition)
(remprop! name 'sex-macro)))
(define (defmacro form) (define (defmacro form)
(let ((arglist (car form)) (let ((arglist (car form))
(body (cdr form))) (body (cdr form)))

View File

@@ -1,6 +1,7 @@
(import (import
scheme scheme
(chicken base) (chicken base)
(chicken condition)
(chicken file) (chicken file)
(chicken load) (chicken load)
(chicken module) (chicken module)
@@ -9,6 +10,7 @@
(chicken string) (chicken string)
matchable matchable
reader reader
sex-macros
srfi-1 srfi-1
types types
utils) utils)
@@ -121,9 +123,78 @@
;; pub fn taking it has to paste them that way round, or the prototype ;; 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 ;; names a type C has not seen. The declarations of the types its
;; importers see only behind a pointer go first. ;; importers see only behind a pointer go first.
(let ((raw-forms (read-module module-path))) (let ((forms (module-forms (read-module module-path))))
(append (check-public-interface raw-forms) (append (check-public-interface forms)
(interface-forms raw-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) (define (interface-forms raw-forms)
(let ((helpers (helper-macros raw-forms))) (let ((helpers (helper-macros raw-forms)))
@@ -138,12 +209,8 @@
;;; the importer expands them all, the way a C header that defines a ;;; the importer expands them all, the way a C header that defines a
;;; macro has to define what it uses too ;;; macro has to define what it uses too
(define (helper-macros raw-forms) (define (helper-macros raw-forms)
(let* ((macro-name (lambda (form) (let* ((private (filter (lambda (form)
(match form (and (macro-name form) (not (pub-form? form))))
(('defmacro (name . _) . _) name)
(('pub 'defmacro (name . _) . _) name)
(_ #f))))
(private (filter (lambda (form) (and (macro-name form) (not (pub-form? form))))
raw-forms)) raw-forms))
(mentions (lambda (form name) (mentions (lambda (form name)
(let walk ((x (cdr form))) (let walk ((x (cdr form)))
@@ -384,7 +451,8 @@
(cond ((null? paths) (concatenate (reverse acc))) (cond ((null? paths) (concatenate (reverse acc)))
((member (car paths) seen) (next (cdr paths) seen acc)) ((member (car paths) seen) (next (cdr paths) seen acc))
(else (else
(let ((forms (interface-forms (read-module (car paths))))) (let ((forms (interface-forms
(module-forms (read-module (car paths))))))
(next (append (cdr paths) (imported-modules forms #f)) (next (append (cdr paths) (imported-modules forms #f))
(cons (car paths) seen) (cons (car paths) seen)
(cons (declared-types forms) acc))))))) (cons (declared-types forms) acc)))))))

View File

@@ -36,6 +36,9 @@
;;; Two modules may import each other with `pub': cycle/a and cycle/b, ;;; Two modules may import each other with `pub': cycle/a and cycle/b,
;;; whose interfaces each import the other back. ;;; whose interfaces each import the other back.
;;; ;;;
;;; gen/made writes its pub forms by macro calls, which are expanded when
;;; its interface is read; the macros it used for that stay its own.
;;;
;;; csi -s check.scm ;;; csi -s check.scm
(import scheme (import scheme
@@ -103,6 +106,20 @@
'("cycle 1 2") '("cycle 1 2")
(output-of "./cycle-app")) (output-of "./cycle-app"))
;;; ...it lists pub forms a macro wrote, and not the macros that wrote them
(test "a module's interface holds what its macros wrote"
'("(pub fn forty-two () int)" "(pub fn seven () int)"
"(pub fn one () int)" "(pub fn two () int)")
(output-of sexc "--public-interface" "gen/made.sex"))
(test-assert "and an importer links with them"
(and (output-of sexc "gen/made.sex" "-c" "-o" "made.o")
(output-of sexc "made-app.sex" "-o" "made-app" "--" "made.o")))
(test "and calls them"
'("made 42 7 1 2")
(output-of "./made-app"))
;;; ...while a realization of a spec importers cannot see is refused by ;;; ...while a realization of a spec importers cannot see is refused by
;;; the module that writes it, not by an importer naming it first ;;; the module that writes it, not by an importer naming it first
(test-assert "a pub realization of a spec imported without pub is refused" (test-assert "a pub realization of a spec imported without pub is refused"
@@ -127,6 +144,7 @@
(for-each delete-file* '("greet.o" "ledger.o" "greet-app" "greet-app.d" (for-each delete-file* '("greet.o" "ledger.o" "greet-app" "greet-app.d"
"page.o" "book.o" "shelf-app" "page.o" "book.o" "shelf-app"
"cycle-a.o" "cycle-b.o" "cycle-app")) "cycle-a.o" "cycle-b.o" "cycle-app"
"made.o" "made-app"))
(test-exit) (test-exit)

View File

@@ -0,0 +1,23 @@
;;; Its pub forms are written by macro calls -- its own, one it imports
;;; without pub, and a splice of two -- and reach importers as written
;;; ones do. One reading a record kept private here cannot expand for an
;;; importer, who is given nothing of it, as before, and still builds
(import makers)
(defmacro (getter name value)
`(pub fn ,name () int (return ,value)))
(defmacro (pair-of a b)
`($ (pub fn ,a () int (return 1)) (pub fn ,b () int (return 2))))
(defmacro (field-count name)
(let ((fields (get-fields name)))
(unless fields (error "no such record" name))
`(pub fn ,(cat name "-fields") () int (return ,(length fields)))))
(record box ((w int) (h int)))
(getter forty-two 42)
(field-count box)
(constant seven 7)
(pair-of one two)

View File

@@ -0,0 +1,3 @@
;;; A macro module: what it writes is up to whoever calls it
(pub defmacro (constant name value)
`(pub fn ,name () int (return ,value)))

View File

@@ -0,0 +1,6 @@
(include stdio.h)
(import gen/made)
(pub fn main () int
(printf "made %d %d %d %d\n" (forty-two) (seven) (one) (two))
(return 0))