dispatch . on the type it reads from

Which of C's two member operators applies is a question the compiler
can already answer -- (.length v) and (.length p) have picked & or *
off the receiver's type since methods moved into the head -- so making
the programmer answer it again asks them to restate what semen knows,
and to revisit every reader of a field the day it becomes a pointer.

A path resolves a step at a time, since only the type in hand answers
for the next field. A receiver nothing can type is read as a value and
warned about rather than refused: refusing leaves no spelling at all
for a struct out of a header nobody has parsed.

Two things had to come along. A parameter written (p * struct point)
was recorded with nothing but the `*' for a type, which left half the
paths in list-macros unanswerable. And fmt-c parenthesised a member
access written inside another, which went unnoticed while only match
nested them -- its bindings have been reading ((s.u).circle).r since
sums went in.
This commit is contained in:
2026-10-05 02:08:15 +03:00
parent 95f39e4b44
commit fb4b8aa1f9
4 changed files with 125 additions and 15 deletions

View File

@@ -433,9 +433,17 @@
(define (make-fn-env fn-form)
(let ((env (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)
(when (and (pair? param) (pair? (cdr param)))
(hash-table-set! parameters (first param) (second param))))
(when (and (pair? param) (pair? (cdr param))
(named-arg? param))
(hash-table-set! parameters
(first param)
(if (null? (cddr param))
(second param)
(cdr param)))))
(sex-fn-arglist fn-form))
(set! (hash-table-ref env :fn-name) (sex-fn-name fn-form))
(set! (hash-table-ref env :lambda-counter) 0)
@@ -603,6 +611,8 @@
((match) (walk-match form env))
((dot-access) (walk-member-path form env))
(else
(if (method-head? (car form))
(walk-method-call form env)
@@ -876,6 +886,32 @@
method))
(else `(& ,receiver)))))
;;; `(. p x)' is `p->x' when p is a pointer and `p.x' when it is not:
;;; the receiver's type says which, so a path reads the same however
;;; what it starts from is held. One step at a time, since only the
;;; type in hand can answer for the next field:
;;;
;;; (. l a x) l : (struct line), l.a : (* struct point) -> l.a->x
;;;
;;; A type we cannot work out is read as a value, which is what the
;;; path would have meant before any of this.
(define (walk-member-path form env)
(let ((base (second form)))
(let step ((expr (walk-statement base env))
(type (expression-type base env))
(fields (cddr form))
(told? #f))
(if (null? fields)
(copy-form-source! form expr)
(let ((target (pointer-target type)))
(unless (or type told?)
(sex-warning form "cannot type this; `.' reads it as a value"
base))
(step (list (if target '-> 'dot-access) expr (car fields))
(field-type (or target type) (car fields))
(cdr fields)
(or told? (not type))))))))
(define (lvalue? expr)
(cond ((symbol? expr) #t)
((vector? expr) #t) ; a compound literal is one
@@ -1081,22 +1117,36 @@
(eq? '* (car type))
(unwrap-type (cdr type))))
;;; A path looks through a pointer wherever it meets one, the way the
;;; access itself does: `l.a->x' and `l.b.x' are both (. l a x) and
;;; both answer with x's type
(define (member-path-type type fields)
(if (null? fields)
type
(member-path-type (field-type type (car fields)) (cdr fields))))
(member-path-type (field-type (or (pointer-target type) type)
(car fields))
(cdr fields))))
(define (field-type type field)
(let ((name (cond ((symbol? type) type)
((and (list? type)
(= 2 (length type))
(memq (car type) '(struct union)))
(second type))
(else #f))))
(and name
(let* ((fields (get-fields name))
(entry (and fields (assq field fields))))
(and entry (second entry))))))
(let* ((fields (type-fields type))
(entry (and fields (assq field fields))))
(and entry (second entry))))
;;; `(struct point)' names its fields in the type database. A sum's
;;; payload writes its union out in place -- `(union ((circle (struct
;;; shapeːcircle)) ...))' -- and carries them with it, so there is
;;; nothing to look up
(define (type-fields type)
(cond ((and (list? type)
(= 2 (length type))
(memq (car type) '(struct union))
(list? (second type)))
(second type))
;; `ops-key' is already the "name of the aggregate" question,
;; const and all: (* const expr) derefs to (const expr), which
;; has the fields `expr' has
((ops-key type) => get-fields)
(else #f)))
(define (make-lambda-name enclosing-fn-name counter)
(string->symbol