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:
11
infer.scm
11
infer.scm
@@ -40,6 +40,7 @@
|
||||
underlying
|
||||
c-primitive?
|
||||
decay
|
||||
unqualified
|
||||
|
||||
;; The boundary
|
||||
parse-type
|
||||
@@ -152,6 +153,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))))
|
||||
|
||||
;;; ---------------------------------------------------------------
|
||||
;;; Surface -> IR
|
||||
;;; ---------------------------------------------------------------
|
||||
|
||||
@@ -1391,8 +1391,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)))))
|
||||
@@ -1402,7 +1404,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)
|
||||
|
||||
@@ -614,6 +614,15 @@
|
||||
(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"
|
||||
|
||||
Reference in New Issue
Block a user