check what a module's importers can see of its types
An importer is given the pub forms and nothing else, so a pub form naming a private type, or one from an import without pub, compiled in its module and failed in every importer with `unknown type name'. The pub forms are checked as the module itself is compiled. A record, struct, union or sum behind a pointer needs only declaring, so its bodyless declaration goes out ahead of them -- the opaque handle that (pub record T) already gave by hand. Held by value it needs its body, and a typedef or an enum has no declaration short of its definition: those are errors at the pub form, saying what would do instead. A name Sex has no declaration for is left to C. An importer may now be handed a declaration after the definition it declares, so a bodyless aggregate no longer replaces known fields. Fixes #57, #58.
This commit is contained in:
196
sex-modules.scm
196
sex-modules.scm
@@ -82,13 +82,203 @@
|
||||
;; pub fns are reduced to prototypes, other pub forms are just pasted.
|
||||
;; Declaration order is kept: a module that declares a type and then a
|
||||
;; pub fn taking it has to paste them that way round, or the prototype
|
||||
;; names a type C has not seen.
|
||||
;; 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-from-file module-path)))
|
||||
(append (check-public-interface raw-forms)
|
||||
(interface-forms raw-forms))))
|
||||
|
||||
(define (interface-forms raw-forms)
|
||||
(reverse (fold process-public-interface-form (list) raw-forms)))
|
||||
|
||||
;;; What importers can see of a module's types
|
||||
;;;
|
||||
;;; They are given the pub forms and nothing else, so a pub form that
|
||||
;;; names a type not among them compiled here and failed there:
|
||||
;;; (pub record shape ((cells (* cell)))) with `cell' private told the
|
||||
;;; importer `unknown type name cell'. A record behind a pointer needs
|
||||
;;; only declaring, and its declaration goes out with the pub forms.
|
||||
;;; Held by value it needs its body; and a typedef or an enum has no
|
||||
;;; declaration short of its definition. Those are errors here, where
|
||||
;;; they were written. A name Sex has no declaration for is one only C
|
||||
;;; knows, and is left to C.
|
||||
|
||||
;;; The bodyless declarations FORMS' importers need, in the order the
|
||||
;;; pub forms ask for them. An error when a pub form needs more than a
|
||||
;;; declaration of a type they cannot see.
|
||||
(define (check-public-interface forms)
|
||||
(let ((visible (visible-types forms))
|
||||
(hidden (hidden-types forms)))
|
||||
(reverse
|
||||
(fold
|
||||
process-public-interface-form
|
||||
(lambda (form acc)
|
||||
(fold (lambda (use acc) (check-type-use form use visible hidden acc))
|
||||
acc
|
||||
(append-map (lambda (type) (type-uses type #f))
|
||||
(interface-types form))))
|
||||
(list)
|
||||
raw-forms))))
|
||||
forms))))
|
||||
|
||||
;;; USE is (name . behind-a-pointer?)
|
||||
(define (check-type-use form use visible hidden acc)
|
||||
(let* ((name (car use))
|
||||
(seen (assq name visible))
|
||||
(kept (assq name hidden)))
|
||||
(cond
|
||||
((and seen (third seen)) acc) ; pub with its body
|
||||
((and seen (cdr use)) acc) ; a pub handle, held by one
|
||||
(seen
|
||||
(sex-error form "only a handle of this type is pub, so hold it through a pointer"
|
||||
name))
|
||||
((not kept) acc)
|
||||
((not (memq (second kept) '(record struct union sum)))
|
||||
(sex-error form (string-append "importers cannot see this "
|
||||
(symbol->string (second kept))
|
||||
", and C declares one only by defining it; "
|
||||
(if (fourth kept)
|
||||
"import its module with pub"
|
||||
"make it pub"))
|
||||
name))
|
||||
((not (cdr use))
|
||||
(sex-error form (string-append "importers cannot see this type, held by value; "
|
||||
(if (fourth kept)
|
||||
"import its module with pub"
|
||||
"make it pub")
|
||||
", or hold it through a pointer")
|
||||
name))
|
||||
((any (lambda (f) (eq? name (cadr f))) acc) acc)
|
||||
(else
|
||||
(cons (copy-form-source!
|
||||
form
|
||||
(list (if (memq (second kept) '(record sum)) 'record (second kept))
|
||||
name))
|
||||
acc)))))
|
||||
|
||||
;;; The type forms a pub form shows its importers
|
||||
;;;
|
||||
;;; (pub fn f ((c (* cell)) (n int)) shape) -> ((* cell) int shape)
|
||||
(define (interface-types form)
|
||||
(match form
|
||||
(('pub 'fn . _)
|
||||
(match (strip-header-comments form 5)
|
||||
(('pub 'fn name (args ...) ret . _) (signature-types args ret))
|
||||
(_ (list))))
|
||||
(('pub 'var . _)
|
||||
(match (strip-header-comments form 4)
|
||||
(('pub 'var name type . _) (list type))
|
||||
(_ (list))))
|
||||
(('pub 'typedef name target) (list target))
|
||||
(('pub (or 'record 'struct 'union) name . rest)
|
||||
(field-types (find list? (remove comment-form? rest))))
|
||||
(('pub 'sum name (variants ...))
|
||||
(append-map (lambda (v)
|
||||
(if (and (pair? v) (pair? (cdr v))) (field-types (second v)) (list)))
|
||||
(remove comment-form? variants)))
|
||||
(('ops type . methods)
|
||||
(append-map interface-types
|
||||
(filter (lambda (m) (and (pair? m) (eq? (car m) 'pub))) methods)))
|
||||
(('pub 'realize spec . methods)
|
||||
(append-map (lambda (m) (interface-types (cons 'pub m)))
|
||||
(remove comment-form? methods)))
|
||||
(_ (list))))
|
||||
|
||||
(define (field-types fields)
|
||||
(if (list? fields) (map second (normalize-fields fields)) (list)))
|
||||
|
||||
(define (signature-types args ret)
|
||||
(cons ret (map arg-type (remove comment-form? args))))
|
||||
|
||||
;;; (f1 float) -> float, (n unsigned int) -> (unsigned int), (int) -> (int)
|
||||
(define (arg-type arg)
|
||||
(cond ((not (named-arg? arg)) arg)
|
||||
((null? (cddr arg)) (second arg))
|
||||
(else (cdr arg))))
|
||||
|
||||
;;; Each name TYPE mentions, and whether it is behind a pointer:
|
||||
;;;
|
||||
;;; (* const cell) -> ((const . #t) (cell . #t))
|
||||
;;; [cell 4] -> ((cell . #f))
|
||||
;;; (fn ((cell)) int) -> ((cell . #t) (int . #t)) ; declaring is enough
|
||||
(define (type-uses type behind?)
|
||||
(cond
|
||||
((symbol? type) (list (cons type behind?)))
|
||||
((not (pair? type)) (list))
|
||||
(else
|
||||
(case (car type)
|
||||
((*) (append-map (lambda (t) (type-uses t #t)) (cdr type)))
|
||||
((¤) (let ((element (array-element-type type)))
|
||||
(if element (type-uses element behind?) (list))))
|
||||
((fn closure)
|
||||
(if (and (pair? (cdr type)) (list? (second type)))
|
||||
(append-map (lambda (t) (type-uses t #t))
|
||||
(signature-types (second type) (last type)))
|
||||
(list)))
|
||||
((struct union enum)
|
||||
(if (and (pair? (cdr type)) (symbol? (second type)))
|
||||
(list (cons (second type) behind?))
|
||||
(list)))
|
||||
(else (append-map (lambda (t) (type-uses t behind?)) type))))))
|
||||
|
||||
;;; The types FORMS declare, as (name kind body?):
|
||||
;;;
|
||||
;;; (pub record tally) -> (tally record #f)
|
||||
;;; (sum colour ((none ()))) -> (colour sum #t)
|
||||
(define (declared-types forms)
|
||||
(filter-map
|
||||
(lambda (form)
|
||||
(and (pair? form)
|
||||
(let ((core (if (pub-form? form) (cdr form) form)))
|
||||
(and (pair? core) (pair? (cdr core))
|
||||
(memq (car core) '(record struct union enum sum typedef))
|
||||
(symbol? (second core))
|
||||
(list (second core)
|
||||
(car core)
|
||||
(or (eq? (car core) 'typedef)
|
||||
(any list? (remove comment-form? (cddr core)))))))))
|
||||
forms))
|
||||
|
||||
(define (pub-form? form) (and (pair? form) (eq? (car form) 'pub)))
|
||||
|
||||
;;; Pub here or through a pub import. A type declared pub both bodyless
|
||||
;;; and whole is whole
|
||||
(define (visible-types forms)
|
||||
(let ((all (append (declared-types (filter pub-form? forms))
|
||||
(append-map exported-types (imported-modules forms #t)))))
|
||||
(map (lambda (entry)
|
||||
(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
|
||||
;;; (name kind body? imported?)
|
||||
(define (hidden-types forms)
|
||||
(append (map (lambda (entry) (append entry (list #f)))
|
||||
(declared-types (remove pub-form? forms)))
|
||||
(map (lambda (entry) (append entry (list #t)))
|
||||
(append-map exported-types (imported-modules forms #f)))))
|
||||
|
||||
;;; The names of the modules FORMS import, with pub or without
|
||||
(define (imported-modules forms pub?)
|
||||
(append-map (lambda (form)
|
||||
(match form
|
||||
(('pub 'import . modules) (if pub? modules (list)))
|
||||
(('import . modules) (if pub? (list) modules))
|
||||
(_ (list))))
|
||||
forms))
|
||||
|
||||
;;; The types a module gives its importers, its pub imports' included.
|
||||
;;; Two modules may import each other, so each is read once
|
||||
(define (exported-types module)
|
||||
(let read ((modules (list module)) (seen (list)) (acc (list)))
|
||||
(if (null? modules)
|
||||
acc
|
||||
(let ((path (locate-module (symbol->string (car modules)))))
|
||||
(if (or (not path) (member path seen))
|
||||
(read (cdr modules) seen acc)
|
||||
(let ((forms (interface-forms (read-from-file path))))
|
||||
(read (append (cdr modules) (imported-modules forms #f))
|
||||
(cons path seen)
|
||||
(append acc (declared-types forms)))))))))
|
||||
|
||||
(define (public-fn-interface raw-form)
|
||||
;; (pub fn name args ret . body) -> prototype, keeping a docstring so
|
||||
|
||||
Reference in New Issue
Block a user