read a spread self as the pointer it is
(self * const v) typed the method's self as `self', so a call passed a value where the method took a pointer, and a spec written one way refused a realization written the other. A const receiver handed to a method taking (* v) is now refused rather than left to cc's warning.
This commit is contained in:
53
semen.scm
53
semen.scm
@@ -443,17 +443,11 @@
|
|||||||
(define (make-fn-env fn-form)
|
(define (make-fn-env fn-form)
|
||||||
(let ((env (make-hash-table))
|
(let ((env (make-hash-table))
|
||||||
(parameters (make-hash-table)))
|
(parameters (make-hash-table)))
|
||||||
;; `(p (* struct point))' writes the type in one piece and
|
|
||||||
;; `(p * struct point)' spreads it over the rest, so the type is
|
|
||||||
;; the whole tail whenever there is more of it than one element
|
|
||||||
(for-each (lambda (param)
|
(for-each (lambda (param)
|
||||||
(when (and (pair? param) (pair? (cdr param))
|
(when (named-arg? param)
|
||||||
(named-arg? param))
|
|
||||||
(hash-table-set! parameters
|
(hash-table-set! parameters
|
||||||
(first param)
|
(first param)
|
||||||
(if (null? (cddr param))
|
(named-arg-type param))))
|
||||||
(second param)
|
|
||||||
(cdr param)))))
|
|
||||||
(sex-fn-arglist fn-form))
|
(sex-fn-arglist fn-form))
|
||||||
(set! (hash-table-ref env :fn-name) (sex-fn-name fn-form))
|
(set! (hash-table-ref env :fn-name) (sex-fn-name fn-form))
|
||||||
(set! (hash-table-ref env :lambda-counter) 0)
|
(set! (hash-table-ref env :lambda-counter) 0)
|
||||||
@@ -462,13 +456,22 @@
|
|||||||
(set! (hash-table-ref env :scopes) (list parameters))
|
(set! (hash-table-ref env :scopes) (list parameters))
|
||||||
env))
|
env))
|
||||||
|
|
||||||
;;; (fn sum ((a int) (b int)) int ...) -> (fn ((int) (int)) int).
|
;;; `(p (* struct point))' writes the type in one piece and
|
||||||
;;; Anything that is not a plain (name type) -- a variadic tail -- goes
|
;;; `(p * struct point)' spreads it over the rest; both are
|
||||||
;;; through untouched.
|
;;; `(* struct point)'
|
||||||
|
(define (named-arg-type param)
|
||||||
|
(if (null? (cddr param))
|
||||||
|
(second param)
|
||||||
|
(cdr param)))
|
||||||
|
|
||||||
|
;;; (fn sum ((a int) (b int)) int ...) -> (fn ((int) (int)) int), and
|
||||||
|
;;; (self * const v) is ((* const v)) just as (self (* const v)) is.
|
||||||
|
;;; Anything that names nothing -- a variadic tail -- goes through
|
||||||
|
;;; untouched.
|
||||||
(define (arglist-types arglist)
|
(define (arglist-types arglist)
|
||||||
(map (lambda (param)
|
(map (lambda (param)
|
||||||
(if (and (pair? param) (= 2 (length param)) (named-arg? param))
|
(if (named-arg? param)
|
||||||
(list (second param))
|
(list (named-arg-type param))
|
||||||
param))
|
param))
|
||||||
arglist))
|
arglist))
|
||||||
|
|
||||||
@@ -876,17 +879,25 @@
|
|||||||
(copy-form-source!
|
(copy-form-source!
|
||||||
form
|
form
|
||||||
`(,method
|
`(,method
|
||||||
,(method-receiver method (and target #t)
|
,(method-receiver method (or target written) (and target #t)
|
||||||
(walk-statement receiver env) form)
|
(walk-statement receiver env) form)
|
||||||
,@(map (lambda (argument) (walk-statement argument env))
|
,@(map (lambda (argument) (walk-statement argument env))
|
||||||
(cddr form))))))))
|
(cddr form))))))))
|
||||||
|
|
||||||
;;; A method taking (* Self) wants an address and one taking Self wants
|
;;; A method taking (* Self) wants an address and one taking Self wants
|
||||||
;;; a value; the receiver is one or the other, so one of the two gets
|
;;; a value; the receiver is one or the other, so one of the two gets
|
||||||
;;; `&' or `*' put in front of it
|
;;; `&' or `*' put in front of it.
|
||||||
(define (method-receiver method pointer? receiver form)
|
;;;
|
||||||
(let ((wants-pointer? (let ((self (method-self-type method)))
|
;;; OBJECT is what the receiver holds or points at: a `(const v)' handed
|
||||||
(and (pair? self) (eq? '* (car self))))))
|
;;; to a method taking (* v) would be quietly stripped of its const.
|
||||||
|
(define (method-receiver method object pointer? receiver form)
|
||||||
|
(let* ((self (method-self-type method))
|
||||||
|
(wants-pointer? (and (pair? self) (eq? '* (car self)))))
|
||||||
|
(when (and wants-pointer?
|
||||||
|
(const-type? object)
|
||||||
|
(not (const-type? (pointer-target self))))
|
||||||
|
(sex-error form "method may change its receiver, and this one is const"
|
||||||
|
method))
|
||||||
(cond ((eq? wants-pointer? pointer?) receiver)
|
(cond ((eq? wants-pointer? pointer?) receiver)
|
||||||
((not wants-pointer?) `(* ,receiver))
|
((not wants-pointer?) `(* ,receiver))
|
||||||
;; `&(f())' is not C, and the method cannot be given a
|
;; `&(f())' is not C, and the method cannot be given a
|
||||||
@@ -937,8 +948,10 @@
|
|||||||
(eq? 'fn (car signature))
|
(eq? 'fn (car signature))
|
||||||
(pair? (cdr signature))
|
(pair? (cdr signature))
|
||||||
(pair? (second signature))
|
(pair? (second signature))
|
||||||
(let ((self (car (second signature))))
|
(unwrap-type (car (second signature))))))
|
||||||
(if (pair? self) (car self) self)))))
|
|
||||||
|
(define (const-type? type)
|
||||||
|
(and (pair? type) (eq? 'const (car type))))
|
||||||
|
|
||||||
|
|
||||||
(define (as-closure-type type)
|
(define (as-closure-type type)
|
||||||
|
|||||||
@@ -13,7 +13,8 @@
|
|||||||
"(record vec2 ((x float) (y float)))"
|
"(record vec2 ((x float) (y float)))"
|
||||||
"(ops vec2"
|
"(ops vec2"
|
||||||
" (pub fn length ((self (* const vec2))) float (return 1.0))"
|
" (pub fn length ((self (* const vec2))) float (return 1.0))"
|
||||||
" (fn sum-of ((self vec2)) float (return (. self x))))"
|
" (fn sum-of ((self vec2)) float (return (. self x)))"
|
||||||
|
" (fn scale ((self (* vec2))) void (= (. self x) 2.0)))"
|
||||||
"(fn mk () vec2 (return #(vec2 : 1.0 2.0)))"
|
"(fn mk () vec2 (return #(vec2 : 1.0 2.0)))"
|
||||||
"(fn f () void (var v vec2 #(vec2 : 1.0 2.0)) (var p (* vec2) (& v)) "
|
"(fn f () void (var v vec2 #(vec2 : 1.0 2.0)) (var p (* vec2) (& v)) "
|
||||||
(apply string-append body) ")"))
|
(apply string-append body) ")"))
|
||||||
@@ -111,6 +112,19 @@
|
|||||||
(reports? (ops-src "(.nope v)") "no such method on"))
|
(reports? (ops-src "(.nope v)") "no such method on"))
|
||||||
(test-assert "and so is a receiver with no type to dispatch on"
|
(test-assert "and so is a receiver with no type to dispatch on"
|
||||||
(reports? "(fn f () void (var n int 1) (.get n))" "no such method on"))
|
(reports? "(fn f () void (var n int 1) (.get n))" "no such method on"))
|
||||||
|
(test-assert "a self spread over the parameter is a pointer self too"
|
||||||
|
(emits? (string-append
|
||||||
|
"(record w ((x int)))"
|
||||||
|
"(ops w (fn get ((self * const w)) int (return (. self x))))"
|
||||||
|
"(fn f () int (var a w #(w : 1)) (var p (* const w) (& a))"
|
||||||
|
" (return (+ (.get a) (.get p))))")
|
||||||
|
"wːget(&a) + wːget(p)"))
|
||||||
|
(test-assert "a const receiver is not handed to a method that may change it"
|
||||||
|
(reports? (ops-src "(var c (* const vec2) p) (.scale c)")
|
||||||
|
"this one is const"))
|
||||||
|
(test-assert "one that cannot change it takes it"
|
||||||
|
(emits? (ops-src "(var c (* const vec2) p) (var n float (.length c))")
|
||||||
|
"vec2ːlength(c)"))
|
||||||
(test-assert "an address cannot be taken of a call"
|
(test-assert "an address cannot be taken of a call"
|
||||||
(reports? (ops-src "(.length (mk))") "receiver is not an lvalue"))
|
(reports? (ops-src "(.length (mk))") "receiver is not an lvalue"))
|
||||||
;; the inner call has to be typed to find the outer one's receiver
|
;; the inner call has to be typed to find the outer one's receiver
|
||||||
@@ -231,6 +245,11 @@
|
|||||||
"(spec (p T) (fn s ((a (* const T))) void))"
|
"(spec (p T) (fn s ((a (* const T))) void))"
|
||||||
"(realize (p v) (fn s ((a v)) void (return)))")
|
"(realize (p v) (fn s ((a v)) void (return)))")
|
||||||
"arguments do not match"))
|
"arguments do not match"))
|
||||||
|
(test-assert "but not on how a pointer is spelled"
|
||||||
|
(emits? (string-append "(record v ((x int)))"
|
||||||
|
"(spec (p T) (fn s ((a * const T)) void))"
|
||||||
|
"(realize (p v) (fn s ((a (* const v))) void (return)))")
|
||||||
|
"void vːpːs (const v * a)"))
|
||||||
(test-assert "a method name already taken on the type is refused"
|
(test-assert "a method name already taken on the type is refused"
|
||||||
(reports? (string-append "(record v ((x int)))"
|
(reports? (string-append "(record v ((x int)))"
|
||||||
"(ops v (fn s ((a (* const v))) void (return)))"
|
"(ops v (fn s ((a (* const v))) void (return)))"
|
||||||
|
|||||||
Reference in New Issue
Block a user