From ba08695355c37dee2822a9ff5775a19c9bd71fd8 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Fri, 16 Oct 2026 17:04:20 +0300 Subject: [PATCH] give importers the private macros a pub one expands into Only pub macros crossed, so one built on a private macro expanded in the importer into a call of a C function nothing defines. --- sex-modules.scm | 34 +++++++++++++++++++++++++++++++++- tests/modules/check.scm | 5 +++-- tests/modules/shelf-app.sex | 1 + tests/modules/shelf/page.sex | 5 +++++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/sex-modules.scm b/sex-modules.scm index 466e8c53..ae073d20 100644 --- a/sex-modules.scm +++ b/sex-modules.scm @@ -115,7 +115,39 @@ (interface-forms raw-forms)))) (define (interface-forms raw-forms) - (reverse (fold process-public-interface-form (list) raw-forms))) + (let ((helpers (helper-macros raw-forms))) + (reverse (fold (lambda (form acc) + (if (memq form helpers) + (cons form acc) + (process-public-interface-form form acc))) + (list) + raw-forms)))) + +;;; The private macros a pub one expands into, and the ones those do: +;;; 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)))) + raw-forms)) + (mentions (lambda (form name) + (let walk ((x (cdr form))) + (or (eq? x name) (and (pair? x) (or (walk (car x)) (walk (cdr x))))))))) + (let grow ((needed (list)) + (users (filter (lambda (form) (and (macro-name form) (pub-form? form))) + raw-forms))) + (let ((more (filter (lambda (form) + (and (not (memq form needed)) + (any (lambda (user) (mentions user (macro-name form))) + users))) + private))) + (if (null? more) + needed + (grow (append needed more) more)))))) ;;; What importers can see of a module's types ;;; diff --git a/tests/modules/check.scm b/tests/modules/check.scm index b882c745..8aabec4c 100644 --- a/tests/modules/check.scm +++ b/tests/modules/check.scm @@ -30,7 +30,8 @@ ;;; ;;; A module in a directory of its own finds its imports beside itself, ;;; not beside the unit importing it: shelf-app imports shelf/book, and -;;; book its siblings page, with `pub', and spine, without. +;;; book its siblings page, with `pub', and spine, without. A pub macro +;;; of page's expands into a private one, which crosses with it. ;;; ;;; make check-modules @@ -95,7 +96,7 @@ "--" (built "book.o") (built "page.o")))) (test "and the app runs through it" - '("pages 2 3") + '("pages 2 3" "number 2") (output-of (built "shelf-app"))) (test "the app runs through both" diff --git a/tests/modules/shelf-app.sex b/tests/modules/shelf-app.sex index d97ca100..5d4977c1 100644 --- a/tests/modules/shelf-app.sex +++ b/tests/modules/shelf-app.sex @@ -6,4 +6,5 @@ (printf "pages %d %d\n" (. (second-page (& b)) number) (. (page-after (second-page (& b))) number)) + (printf "number %d\n" (page-number (second-page (& b)))) (return 0)) diff --git a/tests/modules/shelf/page.sex b/tests/modules/shelf/page.sex index 42c9585d..0ff9fdf4 100644 --- a/tests/modules/shelf/page.sex +++ b/tests/modules/shelf/page.sex @@ -2,3 +2,8 @@ (pub fn page-after ((p page)) page (return #(page : (+ (. p number) 1)))) + +;;; number-of is private, but page-number expands into it, so importers +;;; are given both, the way a C header carries a helper macro too +(defmacro (number-of p) `(. ,p number)) +(pub defmacro (page-number p) `(number-of ,p))