type a call by the type of what it calls
Only a function's name was asked what it returns, so a call through a local or a field holding a pointer had no type, a local named like a function took the function's, and a closure's call helper, which nothing registered, left _ nothing to read.
This commit is contained in:
20
semen.scm
20
semen.scm
@@ -1400,7 +1400,19 @@
|
||||
(cond
|
||||
((method-head? (car expr)) (method-call-type expr env))
|
||||
((receiver-closure-type (car expr) env) => third)
|
||||
(else (and (symbol? (car expr)) (get-return-type (car expr))))))))))
|
||||
;; a function's name, unless a local hides it
|
||||
((and (symbol? (car expr))
|
||||
(not (lookup-name env (car expr)))
|
||||
(get-return-type (car expr)))
|
||||
=> identity)
|
||||
;; a pointer to one: a local, a parameter, a field, a call's result
|
||||
(else (function-return (expression-type (car expr) env)))))))))
|
||||
|
||||
;;; (fn ((int)) int) -> int, and through a typedef of one
|
||||
(define (function-return type)
|
||||
(cond ((and (pair? type) (eq? (car type) 'fn) (= 3 (length type))) (third type))
|
||||
((and (symbol? type) (get-underlying-type type)) => function-return)
|
||||
(else #f)))
|
||||
|
||||
;;; (.length-sq (.sub a b)) types the inner call to find the outer
|
||||
;;; one's receiver, so a method call has to answer like any other
|
||||
@@ -1835,6 +1847,12 @@
|
||||
(unwrap-type arg)))
|
||||
(second type)
|
||||
(iota (length (second type))))))
|
||||
;; what it returns, for `_' to read: it goes out as written, and
|
||||
;; nothing else registers it
|
||||
(add-name-type! helper
|
||||
`(fn (((struct ,closure))
|
||||
,@(map (lambda (p) (list (second p))) params))
|
||||
,returns))
|
||||
(unless (hash-table-ref/default +closure-calls+ helper #f)
|
||||
(hash-table-set! +closure-calls+ helper #t)
|
||||
(let ((call `((dot-access ƛc code)
|
||||
|
||||
@@ -821,6 +821,25 @@ whitespace aside, THEN?"
|
||||
(test-assert "and a closure's"
|
||||
(reports? (in-fn "(var c (closure ((int)) int) (closure ((x int)) _ () (return x)))")
|
||||
"a function's types are written out"))
|
||||
;; a call was typed by asking the global table what a function of
|
||||
;; that name returns, which a local pointer is not
|
||||
(test-assert "a call through a local function pointer"
|
||||
(emits? (string-append "(fn area ((w int) (h int)) int (return (* w h)))"
|
||||
"(fn f () void (var p (fn ((int) (int)) int) area) (var r _ (p 3 4)))")
|
||||
"int r = p(3, 4)"))
|
||||
(test-assert "through a parameter"
|
||||
(emits? "(fn ap ((g (fn ((int)) double)) (x int)) void (var r _ (g x)))" "double r = g(x)"))
|
||||
(test-assert "through a field"
|
||||
(emits? (string-append "(struct ops ((op (fn ((int)) float))))"
|
||||
"(fn f ((o (struct ops))) void (var r _ ((. o op) 1)))")
|
||||
"float r = o.op(1)"))
|
||||
(test-assert "a closure's call, which goes through a helper nothing typed"
|
||||
(emits? "(fn f ((c (closure ((int)) int))) void (var r _ (c 4)))"
|
||||
"int r = ƛint_int_call(c, 4)"))
|
||||
(test-assert "and one returning a closure is called as one"
|
||||
(emits? (string-append "(fn mk ((k int)) (closure ((int)) int) (return (closure ((b int)) int (k) (return (+ k b)))))"
|
||||
"(fn f () void (var fp (fn ((int)) (closure ((int)) int)) mk) (var r _ ((fp 3) 4)))")
|
||||
"int r = ƛint_int_call(fp(3), 4)"))
|
||||
(test-assert "while a function is the pointer it already is"
|
||||
(emits? "(fn g ((n int)) int (return n)) (fn f () void (var p _ g))"
|
||||
"int (*p)(int) = g"))
|
||||
|
||||
Reference in New Issue
Block a user