From b60f58a694d7deac8c87f83579b52aef69260fea Mon Sep 17 00:00:00 2001 From: alex-eg Date: Fri, 16 Oct 2026 17:04:20 +0300 Subject: [PATCH] type an operator's result without qualifiers The spelling was the first operand's, so (+ k 1) with k const was a const int and (+ 1 k) was not; a value has no qualifiers either way. --- infer.scm | 11 +++++++++++ semen.scm | 9 +++++++-- tests/codegen.scm | 9 +++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/infer.scm b/infer.scm index b053c4c..1b4914f 100644 --- a/infer.scm +++ b/infer.scm @@ -55,6 +55,7 @@ type-quals free-tvars decay + unqualified ;; The boundary parse-type @@ -209,6 +210,16 @@ ((fn-type? t) (make-ptr t (list))) (else (resolve type))))) +;;; TYPE as a value has it: qualifiers belong to an object, not to what +;;; is read out of one. `(const int)' -> `int', `(* const char)' as is. +(define (unqualified type) + (let ((t (resolve type))) + (cond ((prim-type? t) (make-prim (prim-name t) (list))) + ((ptr-type? t) (make-ptr (ptr-target t) (list))) + ((agg-type? t) (make-agg (agg-kind t) (agg-name t) (agg-spelling t) (list))) + ((alias-type? t) (make-alias (alias-name t) (alias-expansion t) (list))) + (else t)))) + (define (free-tvars type) (let collect ((t type) (acc (list))) (let ((t (resolve t))) diff --git a/semen.scm b/semen.scm index 53b71cf..b69e147 100644 --- a/semen.scm +++ b/semen.scm @@ -1399,8 +1399,10 @@ ;;; ;;; (var p _ (+ a 1)) a : (ยค int 4) -> int * p = a + 1; ;;; not int p[4] = a + 1; +;;; An operator gives a value, which has no qualifiers: (+ k 1) with +;;; k : (const int) is an int, whichever side k is on (define (decayed written type) - (if (array-type? type) (unparse-type (decay type)) written)) + (if (array-type? type) (unparse-type (decay type)) (value-type written))) (define (promoted-type written) (and written (promoted written (underlying (parse-type written))))) @@ -1410,7 +1412,10 @@ (any (lambda (word) (memq word '(char short bool _Bool))) (prim-name type))) 'int - written)) + (value-type written))) + +(define (value-type written) + (unparse-type (unqualified (parse-type written)))) ;;; `char' and `short' promote to `int', so the ranks start there (define (conversion-rank type) diff --git a/tests/codegen.scm b/tests/codegen.scm index 53671c5..c76191b 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -671,6 +671,15 @@ whitespace aside, THEN?" (test-assert "and says it once, solved to a type already saying it" (emits? (in-fn "(var k (const int) 1) (var p (* const _) (& k))") "const int * p = &k")) + ;; an operator's result is a value, which has no qualifier -- the + ;; spelling was the first operand's, so it had one only that way round + (test-assert "a sum is unqualified" + (emits? (in-fn "(var k (const int) 1) (var j _ (+ k 1))") " int j = k + 1")) + (test-assert "whichever side the qualified operand is on" + (emits? (in-fn "(var k (const int) 1) (var j _ (+ 1 k))") " int j = 1 + k")) + (test-assert "and so is a pointer it gives" + (emits? (in-fn "(var s (* char) 0) (var p (const * char) s) (var q _ (+ p 1))") + "char * q = p + 1")) ;; a compound literal carries its own type, where a brace ;; initializer has none and takes one from its context (test-assert "a compound literal answers a bare wildcard"