From acbd4928f5d60dc0c01094c4a6368e33a231bc53 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Mon, 19 Oct 2026 14:48:43 +0300 Subject: [PATCH] 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. --- semen.scm | 33 ++++++++++++++++++++++++--------- tests/codegen.scm | 16 ++++++++++++++++ 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/semen.scm b/semen.scm index e066025a..a2bba583 100644 --- a/semen.scm +++ b/semen.scm @@ -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 diff --git a/tests/codegen.scm b/tests/codegen.scm index df1f429e..a4cd6991 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -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"