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,22 @@
|
||||
;;; `sex->c', `emits?', `reports?' and `in-fn' come from codegen.scm,
|
||||
;;; which run.scm includes first.
|
||||
|
||||
(import sex-modules)
|
||||
|
||||
(define (interface-of source)
|
||||
"The declarations SOURCE, compiled as a module, hands its importers
|
||||
ahead of its pub forms."
|
||||
(check-public-interface
|
||||
(with-input-from-string source
|
||||
(lambda ()
|
||||
(parameterize ((current-source-file "codegen.sex"))
|
||||
(parse-all (current-input-port)))))))
|
||||
|
||||
(define (interface-reports? source fragment)
|
||||
(let ((m (handle-exceptions e ((condition-property-accessor 'exn 'message) e)
|
||||
(begin (interface-of source) #f))))
|
||||
(and m (string-contains m fragment) #t)))
|
||||
|
||||
(define (ops-src . body)
|
||||
(string-append
|
||||
"(record vec2 ((x float) (y float)))"
|
||||
@@ -457,4 +473,53 @@
|
||||
"switch (t.tag)"))
|
||||
(test-assert "and a scrutinee that is not a sum"
|
||||
(reports? "(fn f () void (var n int 1) (match n ((a ()) (= n 2))))"
|
||||
"match wants a sum"))))
|
||||
"match wants a sum")))
|
||||
|
||||
;; An importer is given the pub forms and nothing else, so a type one
|
||||
;; of them names has to be among them, or C is told no more of it
|
||||
;; than a declaration can say
|
||||
(test-group "what importers can see"
|
||||
(test "a private record behind a pointer is declared for them"
|
||||
'((record cell))
|
||||
(interface-of "(record cell ((v int))) (pub record shape ((c (* cell))))"))
|
||||
(test "once"
|
||||
'((record cell))
|
||||
(interface-of "(record cell ((v int))) (pub fn f ((a (* cell)) (b (* cell))) void)"))
|
||||
(test "a struct as a struct"
|
||||
'((struct cell))
|
||||
(interface-of "(struct cell ((v int))) (pub fn f ((a (* struct cell))) void)"))
|
||||
(test "a pub one needs nothing"
|
||||
'()
|
||||
(interface-of "(pub record cell ((v int))) (pub record shape ((c cell)))"))
|
||||
(test "nor one only C knows"
|
||||
'()
|
||||
(interface-of "(pub fn f ((p (* FILE)) (n size-t)) void)"))
|
||||
(test "nor a private fn's"
|
||||
'()
|
||||
(interface-of "(record cell ((v int))) (fn f ((c cell)) void)"))
|
||||
(test-assert "held by value it is an error"
|
||||
(interface-reports? "(record cell ((v int))) (pub record shape ((c cell)))"
|
||||
"held by value"))
|
||||
(test-assert "naming the line"
|
||||
(interface-reports? "(record cell ((v int)))\n(pub record shape ((c cell)))"
|
||||
"codegen.sex:2:"))
|
||||
(test-assert "in an array too"
|
||||
(interface-reports? "(record cell ((v int))) (pub record shape ((cs [cell 4])))"
|
||||
"held by value"))
|
||||
(test-assert "returned"
|
||||
(interface-reports? "(record cell ((v int))) (pub fn f () cell)" "held by value"))
|
||||
(test-assert "or taken by a pub method"
|
||||
(interface-reports? "(record cell ((v int))) (ops cell (pub fn get ((self cell)) int))"
|
||||
"held by value"))
|
||||
(test-assert "a typedef has no declaration to give, pointer or not"
|
||||
(interface-reports? "(typedef small int) (pub fn f ((p (* small))) void)"
|
||||
"this typedef, and C declares one only by defining it"))
|
||||
(test-assert "nor has an enum"
|
||||
(interface-reports? "(enum mood (up down)) (pub var m (* enum mood))"
|
||||
"this enum, and C declares"))
|
||||
(test-assert "a pub handle is held through a pointer"
|
||||
(interface-reports? "(pub record h) (record h ((n int))) (pub fn f ((x h)) void)"
|
||||
"only a handle"))
|
||||
(test "which is all it takes"
|
||||
'()
|
||||
(interface-of "(pub record h) (record h ((n int))) (pub fn f ((x (* h))) void)"))))
|
||||
|
||||
@@ -17,15 +17,18 @@
|
||||
# alone, its fields left behind and its methods still found.
|
||||
#
|
||||
# The public forms carry comments in their headers, which the reduction
|
||||
# to a prototype and to an extern both have to look past.
|
||||
# to a prototype and to an extern both have to look past. And a type the
|
||||
# app cannot see -- private to greet, or from a module greet imports
|
||||
# without `pub' -- crosses behind a pointer, declared and no more.
|
||||
|
||||
SEXC ?= ../../sexc
|
||||
|
||||
EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood 1\nclosure 15 21 201\nvec2 7 7\nvec2(3 4)\ntally 2\ncolour 123
|
||||
EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood 1\nclosure 15 21 201\nvec2 7 7\nvec2(3 4)\ntally 2\nboard 42\nledger 2\ncolour 123
|
||||
|
||||
check:
|
||||
@$(SEXC) ledger.sex -c -o ledger.o
|
||||
@$(SEXC) greet.sex -c -o greet.o
|
||||
@$(SEXC) greet-app.sex -o greet-app -- greet.o
|
||||
@$(SEXC) greet-app.sex -o greet-app -- greet.o ledger.o
|
||||
@if [ "`./greet-app`" = "`printf '$(EXPECTED)\n'`" ]; then \
|
||||
echo "modules ok"; \
|
||||
else \
|
||||
@@ -34,6 +37,6 @@ check:
|
||||
@$(MAKE) --no-print-directory clean
|
||||
|
||||
clean:
|
||||
@rm -f greet.o greet-app
|
||||
@rm -f greet.o ledger.o greet-app
|
||||
|
||||
.PHONY: check clean
|
||||
|
||||
@@ -47,6 +47,11 @@
|
||||
(printf "tally %d\n" (.count t))
|
||||
(.drop t)
|
||||
|
||||
;; neither cell nor ledger is visible here, only declared
|
||||
(var b board (make-board))
|
||||
(printf "board %d\n" (board-total (& b)))
|
||||
(printf "ledger %d\n" (ledger-total (greet-ledger)))
|
||||
|
||||
(var c colour (colour/rgb 1 2 3))
|
||||
(match c
|
||||
((rgb (r g b)) (printf "colour %d%d%d\n" r g b))
|
||||
|
||||
@@ -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
|
||||
|
||||
8
tests/modules/ledger.sex
Normal file
8
tests/modules/ledger.sex
Normal file
@@ -0,0 +1,8 @@
|
||||
;;; A module greet.sex imports without `pub'. greet-app.sex never
|
||||
;;; imports it, yet holds a (* ledger) that greet hands out: what greet
|
||||
;;; exports has to declare the type for it.
|
||||
|
||||
(pub record ledger ((total int)))
|
||||
|
||||
(pub fn ledger-add ((l (* ledger)) (n int)) void
|
||||
(+= (. l total) n))
|
||||
Reference in New Issue
Block a user