diff --git a/semen.scm b/semen.scm index 861b98e..1581911 100644 --- a/semen.scm +++ b/semen.scm @@ -1457,11 +1457,12 @@ ;;; `(* const char)' is flat: everything after the `*' is the target. ;;; `(& x)' builds the nested `(* (struct point))' and `unparse-type' ;;; writes the flat `(* struct point)', so both spellings turn up. +;;; (* T) -> T, and so for a typedef of one: (typedef pt-ref (* pt)) (define (pointer-target type) - (and (list? type) - (>= (length type) 2) - (eq? '* (car type)) - (unwrap-type (cdr type)))) + (cond ((and (list? type) (>= (length type) 2) (eq? '* (car type))) + (unwrap-type (cdr type))) + ((and (symbol? type) (get-underlying-type type)) => pointer-target) + (else #f))) ;;; 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 diff --git a/tests/codegen.scm b/tests/codegen.scm index d867082..e552bf8 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -68,6 +68,14 @@ whitespace aside, THEN?" (let ((next (string-skip c char-whitespace? (+ at (string-length first))))) (and next (string-prefix? then (substring c next))))))) +;;; A record reached through a typedef of a pointer to it +(define pt-ref-src + (string-append + "(record pt ((x int)))" + "(ops pt (fn get ((self (* pt))) int (return (. self x))))" + "(typedef pt-ref (* pt))" + "(fn f ((q pt-ref)) int (return (+ (. q x) (. (* q) x) (.get q))))")) + ;;; Paths through arrays of a struct and of pointers to one (define poly-src (string-append @@ -1055,6 +1063,14 @@ whitespace aside, THEN?" (emits? poly-src "q->pp[0]->y")) (test-assert "and every index of one is written" (emits? poly-src "q->grid[1][2].x")) + ;; 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" + (emits? pt-ref-src "q->x")) + (test-assert "dereferences to what it points at" + (not (warns? pt-ref-src "cannot type"))) + (test-assert "and calls its target's methods" + (emits? pt-ref-src "ptːget(q)")) (test-assert "a pointer reads with an arrow" (emits? (in-line "(= (. pp x) 1)") "pp->x = 1")) ;; each step asks the type in hand, so one path may be both