read each module once per unit

A pub import chain had its interface check read every module below
again, so the deepest was parsed once per level above it.
This commit is contained in:
2026-10-14 20:08:16 +03:00
parent dcd93c0d9e
commit 70aafb70e6

View File

@@ -78,16 +78,18 @@
(file-readable? module-path) (file-readable? module-path)
module-path)))) module-path))))
;;; Every module file read for this unit, first read first: what its ;;; Every module file read for this unit, with its forms, last read
;;; output depends on, besides the unit itself ;;; first: what its output depends on, besides the unit itself. A module
(define +module-dependencies+ (list)) ;;; that an import chain and the interface check both reach is read once
(define +modules-read+ (list))
(define (module-dependencies) (reverse +module-dependencies+)) (define (module-dependencies) (reverse (map car +modules-read+)))
(define (read-module path) (define (read-module path)
(unless (member path +module-dependencies+) (cond ((assoc path +modules-read+) => cdr)
(set! +module-dependencies+ (cons path +module-dependencies+))) (else (let ((forms (read-from-file path)))
(read-from-file path)) (set! +modules-read+ (cons (cons path forms) +modules-read+))
forms))))
(define (read-public-interface module-path) (define (read-public-interface module-path)
;; pub fns are reduced to prototypes, other pub forms are just pasted. ;; pub fns are reduced to prototypes, other pub forms are just pasted.
@@ -134,7 +136,8 @@
(define (check-type-use form use visible hidden acc) (define (check-type-use form use visible hidden acc)
(let* ((name (car use)) (let* ((name (car use))
(seen (assq name visible)) (seen (assq name visible))
(kept (assq name hidden))) (kept (assq name hidden))
(remedy (if (and kept (fourth kept)) "import its module with pub" "make it pub")))
(cond (cond
((and seen (third seen)) acc) ; pub with its body ((and seen (third seen)) acc) ; pub with its body
((and seen (cdr use)) acc) ; a pub handle, held by one ((and seen (cdr use)) acc) ; a pub handle, held by one
@@ -146,16 +149,11 @@
(sex-error form (string-append "importers cannot see this " (sex-error form (string-append "importers cannot see this "
(symbol->string (second kept)) (symbol->string (second kept))
", and C declares one only by defining it; " ", and C declares one only by defining it; "
(if (fourth kept) remedy)
"import its module with pub"
"make it pub"))
name)) name))
((not (cdr use)) ((not (cdr use))
(sex-error form (string-append "importers cannot see this type, held by value; " (sex-error form (string-append "importers cannot see this type, held by value; "
(if (fourth kept) remedy ", or hold it through a pointer")
"import its module with pub"
"make it pub")
", or hold it through a pointer")
name)) name))
((any (lambda (f) (eq? name (cadr f))) acc) acc) ((any (lambda (f) (eq? name (cadr f))) acc) acc)
(else (else
@@ -257,14 +255,11 @@
(define (pub-form? form) (and (pair? form) (eq? (car form) 'pub))) (define (pub-form? form) (and (pair? form) (eq? (car form) 'pub)))
;;; Pub here or through a pub import. A type declared pub both bodyless ;;; Pub here or through a pub import. A type declared pub both bodyless
;;; and whole is whole ;;; and whole is whole: its whole entries come first, for `assq'
(define (visible-types forms) (define (visible-types forms)
(let ((all (append (declared-types (filter pub-form? forms)) (let ((all (append (declared-types (filter pub-form? forms))
(append-map exported-types (imported-modules forms #t))))) (append-map exported-types (imported-modules forms #t)))))
(map (lambda (entry) (append (filter third all) (remove third all))))
(list (first entry) (second entry)
(any third (filter (lambda (e) (eq? (first e) (first entry))) all))))
all)))
;;; Declared here without pub, or brought by an import without it, as ;;; Declared here without pub, or brought by an import without it, as
;;; (name kind body? imported?) ;;; (name kind body? imported?)
@@ -286,16 +281,16 @@
;;; The types a module gives its importers, its pub imports' included. ;;; The types a module gives its importers, its pub imports' included.
;;; Two modules may import each other, so each is read once ;;; Two modules may import each other, so each is read once
(define (exported-types module) (define (exported-types module)
(let read ((modules (list module)) (seen (list)) (acc (list))) (let next ((modules (list module)) (seen (list)) (acc (list)))
(if (null? modules) (if (null? modules)
acc (concatenate (reverse acc))
(let ((path (locate-module (symbol->string (car modules))))) (let ((path (locate-module (symbol->string (car modules)))))
(if (or (not path) (member path seen)) (if (or (not path) (member path seen))
(read (cdr modules) seen acc) (next (cdr modules) seen acc)
(let ((forms (interface-forms (read-module path)))) (let ((forms (interface-forms (read-module path))))
(read (append (cdr modules) (imported-modules forms #f)) (next (append (cdr modules) (imported-modules forms #f))
(cons path seen) (cons path seen)
(append acc (declared-types forms))))))))) (cons (declared-types forms) acc))))))))
(define (public-fn-interface raw-form) (define (public-fn-interface raw-form)
;; (pub fn name args ret . body) -> prototype, keeping a docstring so ;; (pub fn name args ret . body) -> prototype, keeping a docstring so