;; this code expects that all pretty types
;; have already been removed from the ast!
(define impc:ti:rename-all-shadow-vars
(lambda (symname full-ast syms)
(letrec ((f (lambda (ast fname)
(cond ((atom? ast) ast)
((null? ast) ast)
((list? ast)
(cond ((member (car ast) *impc:letslist*)
;; first find and replace all shadow vars
(let* ((replace-pairs
(cl:remove
#f
(flatten
(map (lambda (x)
(let* ((pair (regex:type-split (symbol->string (car x)) ":"))
(sym (string->symbol (car pair))))
(if (and (not (equal? sym symname))
(or (member sym syms)
(impc:ti:namedtype-exists? (symbol->string sym))
(impc:ti:genericfunc-exists? sym)
(impc:ti:xtmacro-exists? (symbol->string sym))
(impc:ti:polyfunc-exists? (symbol->string sym))
(and (not (equal? sym fname))
(impc:ti:closure-exists? (symbol->string sym)))
(impc:ti:globalvar-exists? (symbol->string sym))))
(let ((shadow (impc:ti:gen-shadow sym)))
(set! syms (cons shadow syms))
(if (null? (cdr pair))
(cons sym shadow)
(list (cons sym shadow)
(cons (car x)
(string->symbol
(string-append
(symbol->string shadow) ":" (cadr pair)))))))
(begin
(set! syms (cons sym syms))
#f))))
(cadr ast)))))
(newast (replace-all ast replace-pairs)))
;; now make sure we have code coverage!
(cons (car newast)
(cons (map (lambda (x) (cons (car x) (f (cdr x) fname))) (cadr newast))
(f (cddr newast) fname)))))
((member (car ast) *impc:lambdaslist*)
(let* ((replace-pairs
(cl:remove
#f
(flatten
(map (lambda (x)
(let* ((pair (regex:type-split (symbol->string x) ":"))
(sym (string->symbol (car pair))))
(if (or (member sym syms)
(impc:ti:namedtype-exists? (symbol->string sym))
(impc:ti:genericfunc-exists? sym)
(impc:ti:xtmacro-exists? (symbol->string sym))
(impc:ti:polyfunc-exists? (symbol->string sym))
(and (not (equal? sym fname))
(impc:ti:closure-exists? (symbol->string sym)))
(impc:ti:globalvar-exists? (symbol->string sym)))
(let ((shadow (impc:ti:gen-shadow sym)))
(set! syms (cons shadow syms))
(if (null? (cdr pair))
(cons x shadow)
(list (cons sym shadow)
(cons x
(string->symbol
(string-append
(symbol->string shadow) ":" (cadr pair)))))))
(begin
(set! syms (cons sym syms))
#f))))
(cadr ast)))))
(newast (replace-all ast replace-pairs)))
(cons (car ast)
(cons (cadr newast)
(f (cddr newast) fname)))))
((pair? ast)
(cons (f (car ast) fname)
(f (cdr ast) fname)))
(else ast)))))))
(if (equal? (car full-ast) 'let)
(f full-ast (caaadr full-ast))
(f full-ast '___no_sym___)))))