forked from alex-eg/sex
keep the qualifiers written on a wildcard
(const _) parsed to a type variable, which had nowhere to keep the const, so the type it was solved to came out without one -- for (const _) and for the target of (* const _) alike.
This commit is contained in:
30
infer.scm
30
infer.scm
@@ -52,11 +52,13 @@
|
||||
|
||||
;;; A type variable is a mutable cell. `ref' is #f while unsolved and
|
||||
;;; the type it stands for once bound -- union-find, with the path
|
||||
;;; compression done in `resolve'.
|
||||
;;; compression done in `resolve'. `quals' are the ones written on it --
|
||||
;;; `(const _)' -- and go onto whatever it is bound to.
|
||||
(define-record-type <tvar>
|
||||
(%make-tvar ref)
|
||||
(%make-tvar ref quals)
|
||||
tvar?
|
||||
(ref tvar-ref tvar-ref-set!))
|
||||
(ref tvar-ref tvar-ref-set!)
|
||||
(quals tvar-quals tvar-quals-set!))
|
||||
|
||||
;;; A primitive or otherwise nominal type. `name' is the list of words
|
||||
;;; making it up, so `int', `(unsigned int)' and `(long long)' are all
|
||||
@@ -113,7 +115,7 @@
|
||||
(quals alias-quals))
|
||||
|
||||
(define (fresh-tvar)
|
||||
(%make-tvar #f))
|
||||
(%make-tvar #f (list)))
|
||||
|
||||
;;; Follow a bound variable to what it stands for, compressing the path
|
||||
;;; on the way out. Every procedure that looks at a type's shape starts
|
||||
@@ -197,7 +199,7 @@
|
||||
;; The type-level wildcard. It is a fresh variable wherever it
|
||||
;; appears, which is what makes partial types -- `(* _)', `(¤ _ 4)'
|
||||
;; -- fall out for free rather than needing their own grammar.
|
||||
((equal? words '(_)) (fresh-tvar))
|
||||
((equal? words '(_)) (with-quals (fresh-tvar) quals))
|
||||
((and (null? (cdr words)) (get-underlying-type (car words)))
|
||||
=> (lambda (target)
|
||||
(make-alias (car words) (parse-type target) quals)))
|
||||
@@ -262,20 +264,22 @@
|
||||
(if (list? s) s (list s))))
|
||||
|
||||
;;; Re-qualify a parsed type, for the grouping case `(const (struct s))'
|
||||
;;; where the qualifier is read before the thing it qualifies.
|
||||
;;; where the qualifier is read before the thing it qualifies, and for a
|
||||
;;; variable solved to a type: `(* const _)' against `(const int)' says
|
||||
;;; const once.
|
||||
(define (with-quals type quals)
|
||||
(define (plus written) (append quals (remove (lambda (q) (memq q quals)) written)))
|
||||
(if (null? quals)
|
||||
type
|
||||
(cond ((prim-type? type) (make-prim (prim-name type)
|
||||
(append quals (prim-quals type))))
|
||||
((ptr-type? type) (make-ptr (ptr-target type)
|
||||
(append quals (ptr-quals type))))
|
||||
(cond ((prim-type? type) (make-prim (prim-name type) (plus (prim-quals type))))
|
||||
((ptr-type? type) (make-ptr (ptr-target type) (plus (ptr-quals type))))
|
||||
((agg-type? type) (make-agg (agg-kind type) (agg-name type)
|
||||
(agg-spelling type)
|
||||
(append quals (agg-quals type))))
|
||||
(plus (agg-quals type))))
|
||||
((alias-type? type) (make-alias (alias-name type)
|
||||
(alias-expansion type)
|
||||
(append quals (alias-quals type))))
|
||||
(plus (alias-quals type))))
|
||||
((tvar? type) (tvar-quals-set! type (plus (tvar-quals type))) type)
|
||||
(else type))))
|
||||
|
||||
;;; ---------------------------------------------------------------
|
||||
@@ -406,7 +410,7 @@
|
||||
;; Without recursive types this cannot trigger. It is four lines,
|
||||
;; and the alternative to having it is a hang.
|
||||
((occurs? v t) (sex-error form "recursive type" (unparse-type v)))
|
||||
(else (tvar-ref-set! v t) #t)))
|
||||
(else (tvar-ref-set! v (with-quals t (tvar-quals v))) #t)))
|
||||
|
||||
(define (occurs? v type)
|
||||
(let ((t (resolve type)))
|
||||
|
||||
@@ -605,6 +605,15 @@
|
||||
(emits? "(typedef count int) (fn g () count (return 0))
|
||||
(fn f () void (var x _ (g)))"
|
||||
"count x = g()"))
|
||||
;; a variable had nowhere to keep a qualifier written on it, and
|
||||
;; what it was solved to came out without
|
||||
(test-assert "a qualified wildcard keeps its qualifier"
|
||||
(emits? (in-fn "(var k (const _) 5)") "const int k = 5"))
|
||||
(test-assert "behind a pointer too"
|
||||
(emits? (in-fn "(var p (* const _) (& a))") "const int * p = &a"))
|
||||
(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"))
|
||||
;; 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