read a member path and a receiver through a pointer chain

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:
2026-10-19 14:48:43 +03:00
parent 33c8eca51f
commit 17aa5f6371
2 changed files with 40 additions and 9 deletions

View File

@@ -1195,10 +1195,12 @@
(let ((name (method-head-name (car form))))
(unless (pair? (cdr form))
(sex-error form "a method call needs a receiver" form))
(let* ((receiver (second form))
(written (expression-type receiver env))
(target (pointer-target written))
(key (ops-key (or target written))))
;; through a pointer chain, as a path reads through one
(let*-values (((receiver written)
(through-pointers (walk-operand (second form) env)
(expression-type (second form) env)))
((target) (pointer-target written))
((key) (ops-key (or target written))))
(unless key
(sex-error form "receiver of this has no type to dispatch on" name))
(let ((method (get-method key name)))
@@ -1208,7 +1210,7 @@
form
`(,method
,(method-receiver method (or target written) (and target #t)
(walk-operand receiver env) form)
receiver form)
,@(walk-operands form (cddr form) env)))))))
;;; A method taking (* Self) wants an address and one taking Self wants
@@ -1254,8 +1256,9 @@
(told? #f))
(if (null? fields)
(copy-form-source! form expr)
(let* ((target (pointer-target type))
(known (type-fields (or target type))))
(let*-values (((expr type) (through-pointers expr type))
((target) (pointer-target type))
((known) (type-fields (or target type))))
(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)))
(unless (or type told?)
@@ -1269,6 +1272,19 @@
(cdr fields)
(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
(define (field-name field)
(if (and (array-form? field) (pair? (cdr field)))
@@ -1568,8 +1584,7 @@
(define (member-path-type type fields)
(if (null? fields)
type
(member-path-type (step-type (or (pointer-target type) type)
(car fields))
(member-path-type (step-type (pointed-through type) (car fields))
(cdr fields))))
;;; What a step of a path gives: its field, or for [xs i] an element of

View File

@@ -1253,6 +1253,22 @@
(emits? poly-src "q->pp[0]->y"))
(test-assert "and every index of one is written"
(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
;; value, and a method could not find its receiver's type
(test-assert "a typedef of a pointer reads with an arrow"