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.
This commit is contained in:
2026-10-16 17:04:20 +03:00
parent 20fc8cf7ad
commit d208ca2371
3 changed files with 27 additions and 2 deletions

View File

@@ -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)))

View File

@@ -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)

View File

@@ -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"