forked from alex-eg/sex
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:
@@ -8,6 +8,8 @@
|
||||
(pub include stdio.h)
|
||||
(include stdlib.h)
|
||||
|
||||
(import ledger)
|
||||
|
||||
(pub var greet-count ;; a comment in the header of a public form is
|
||||
;; not part of it: what the importer is given has
|
||||
;; to be `extern int greet-count', not a form
|
||||
@@ -30,12 +32,36 @@
|
||||
(lambda (name field-type)
|
||||
`(printf "%s " ,(symbol->string name))))))
|
||||
|
||||
(var greetings ledger #(ledger : 0))
|
||||
|
||||
(pub fn greet ;; ...and here the prototype would lose its return type
|
||||
((name (* const char))) void
|
||||
"Print a greeting for NAME."
|
||||
(++ greet-count)
|
||||
(ledger-add (& greetings) 1)
|
||||
(printf "hello, %s\n" name))
|
||||
|
||||
;;; Types the importer cannot see, crossing behind a pointer. `cell' is
|
||||
;;; private and `ledger' was imported without `pub', and the importer
|
||||
;;; is handed a declaration of each and nothing else.
|
||||
(record cell ((v int)))
|
||||
|
||||
(var the-cell cell #(cell : 40))
|
||||
|
||||
(pub record board ((first (* cell)) (size int)))
|
||||
|
||||
(pub fn make-board () board
|
||||
(return #(board : (& the-cell) 2)))
|
||||
|
||||
(pub fn board-total ((b (* const board))) int
|
||||
(return (+ (. b first v) (. b size))))
|
||||
|
||||
(pub fn greet-ledger () (* ledger)
|
||||
(return (& greetings)))
|
||||
|
||||
(pub fn ledger-total ((l (* const ledger))) int
|
||||
(return (. l total)))
|
||||
|
||||
;;; A closure type crossing the boundary. Both units generate the
|
||||
;;; struct for this signature independently, so they have to agree on
|
||||
;;; its tag and its layout, or the value is passed wrong and nothing
|
||||
|
||||
Reference in New Issue
Block a user