read a member path and a receiver through a pointer chain
All checks were successful
Sex CI / build-linux (pull_request) Successful in 5m34s
All checks were successful
Sex CI / build-linux (pull_request) Successful in 5m34s
A pointer to a pointer has no fields, so (. pp x) has one meaning, and it came out pp->x; it is (*pp)->x, and a method receiver alike.
This commit is contained in:
33
semen.scm
33
semen.scm
@@ -1195,10 +1195,12 @@
|
|||||||
(let ((name (method-head-name (car form))))
|
(let ((name (method-head-name (car form))))
|
||||||
(unless (pair? (cdr form))
|
(unless (pair? (cdr form))
|
||||||
(sex-error form "a method call needs a receiver" form))
|
(sex-error form "a method call needs a receiver" form))
|
||||||
(let* ((receiver (second form))
|
;; through a pointer chain, as a path reads through one
|
||||||
(written (expression-type receiver env))
|
(let*-values (((receiver written)
|
||||||
(target (pointer-target written))
|
(through-pointers (walk-operand (second form) env)
|
||||||
(key (ops-key (or target written))))
|
(expression-type (second form) env)))
|
||||||
|
((target) (pointer-target written))
|
||||||
|
((key) (ops-key (or target written))))
|
||||||
(unless key
|
(unless key
|
||||||
(sex-error form "receiver of this has no type to dispatch on" name))
|
(sex-error form "receiver of this has no type to dispatch on" name))
|
||||||
(let ((method (get-method key name)))
|
(let ((method (get-method key name)))
|
||||||
@@ -1208,7 +1210,7 @@
|
|||||||
form
|
form
|
||||||
`(,method
|
`(,method
|
||||||
,(method-receiver method (or target written) (and target #t)
|
,(method-receiver method (or target written) (and target #t)
|
||||||
(walk-operand receiver env) form)
|
receiver form)
|
||||||
,@(walk-operands form (cddr form) env)))))))
|
,@(walk-operands form (cddr form) env)))))))
|
||||||
|
|
||||||
;;; A method taking (* Self) wants an address and one taking Self wants
|
;;; A method taking (* Self) wants an address and one taking Self wants
|
||||||
@@ -1254,8 +1256,9 @@
|
|||||||
(told? #f))
|
(told? #f))
|
||||||
(if (null? fields)
|
(if (null? fields)
|
||||||
(copy-form-source! form expr)
|
(copy-form-source! form expr)
|
||||||
(let* ((target (pointer-target type))
|
(let*-values (((expr type) (through-pointers expr type))
|
||||||
(known (type-fields (or target type))))
|
((target) (pointer-target type))
|
||||||
|
((known) (type-fields (or target type))))
|
||||||
(when (and (pair? known) (not (assq (field-name (car fields)) known)))
|
(when (and (pair? known) (not (assq (field-name (car fields)) known)))
|
||||||
(sex-error form "no field of this name in" (or target type) (car fields)))
|
(sex-error form "no field of this name in" (or target type) (car fields)))
|
||||||
(unless (or type told?)
|
(unless (or type told?)
|
||||||
@@ -1269,6 +1272,19 @@
|
|||||||
(cdr fields)
|
(cdr fields)
|
||||||
(or told? (not type))))))))
|
(or told? (not type))))))))
|
||||||
|
|
||||||
|
;;; A pointer to a pointer has no fields: EXPR read through every pointer
|
||||||
|
;;; but the last, so a step writes -> once. pp : (* * pt) -> (* pp), (* pt)
|
||||||
|
(define (through-pointers expr type)
|
||||||
|
(let ((target (pointer-target type)))
|
||||||
|
(if (and target (pointer-target target))
|
||||||
|
(through-pointers (list '* expr) target)
|
||||||
|
(values expr type))))
|
||||||
|
|
||||||
|
;;; What a path reads fields of: TYPE, or what its pointers lead to
|
||||||
|
(define (pointed-through type)
|
||||||
|
(let ((target (pointer-target type)))
|
||||||
|
(if target (pointed-through target) type)))
|
||||||
|
|
||||||
;;; A field of a path, indexed or not: xs, [xs 0] -> xs
|
;;; A field of a path, indexed or not: xs, [xs 0] -> xs
|
||||||
(define (field-name field)
|
(define (field-name field)
|
||||||
(if (and (array-form? field) (pair? (cdr field)))
|
(if (and (array-form? field) (pair? (cdr field)))
|
||||||
@@ -1568,8 +1584,7 @@
|
|||||||
(define (member-path-type type fields)
|
(define (member-path-type type fields)
|
||||||
(if (null? fields)
|
(if (null? fields)
|
||||||
type
|
type
|
||||||
(member-path-type (step-type (or (pointer-target type) type)
|
(member-path-type (step-type (pointed-through type) (car fields))
|
||||||
(car fields))
|
|
||||||
(cdr fields))))
|
(cdr fields))))
|
||||||
|
|
||||||
;;; What a step of a path gives: its field, or for [xs i] an element of
|
;;; What a step of a path gives: its field, or for [xs i] an element of
|
||||||
|
|||||||
@@ -1253,6 +1253,22 @@
|
|||||||
(emits? poly-src "q->pp[0]->y"))
|
(emits? poly-src "q->pp[0]->y"))
|
||||||
(test-assert "and every index of one is written"
|
(test-assert "and every index of one is written"
|
||||||
(emits? poly-src "q->grid[1][2].x"))
|
(emits? poly-src "q->grid[1][2].x"))
|
||||||
|
;; a pointer to a pointer has no fields, so a path reads through
|
||||||
|
;; every pointer to the aggregate, where it wrote pp->x
|
||||||
|
(test-assert "a pointer to a pointer is read through"
|
||||||
|
(emits? "(struct pt ((x int))) (fn f ((pp (* * struct pt))) int (return (. pp x)))"
|
||||||
|
"(*pp)->x"))
|
||||||
|
(test-assert "however many there are"
|
||||||
|
(emits? "(struct pt ((x int))) (fn f ((p3 (* * * struct pt))) int (return (. p3 x)))"
|
||||||
|
"(*(*p3))->x"))
|
||||||
|
(test-assert "and typed through"
|
||||||
|
(emits? "(struct pt ((x float))) (fn f ((pp (* * struct pt))) void (var v _ (. pp x)))"
|
||||||
|
"float v = (*pp)->x"))
|
||||||
|
(test-assert "a method's receiver too"
|
||||||
|
(emits? (string-append "(record pt ((x int)))"
|
||||||
|
"(ops pt (fn get ((self (* pt))) int (return (. self x))))"
|
||||||
|
"(fn f ((pp (* * pt))) int (return (.get pp)))")
|
||||||
|
"ptːget(*pp)"))
|
||||||
;; only a written (* T) was a pointer, so a typedef of one read as a
|
;; only a written (* T) was a pointer, so a typedef of one read as a
|
||||||
;; value, and a method could not find its receiver's type
|
;; value, and a method could not find its receiver's type
|
||||||
(test-assert "a typedef of a pointer reads with an arrow"
|
(test-assert "a typedef of a pointer reads with an arrow"
|
||||||
|
|||||||
Reference in New Issue
Block a user