Have `-Wformat' better recognize the `gettext' procedure.
[bpt/guile.git] / module / language / tree-il / analyze.scm
CommitLineData
cf10678f
AW
1;;; TREE-IL -> GLIL compiler
2
b4af80a4 3;; Copyright (C) 2001, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
cf10678f 4
53befeb7
NJ
5;;;; This library is free software; you can redistribute it and/or
6;;;; modify it under the terms of the GNU Lesser General Public
7;;;; License as published by the Free Software Foundation; either
8;;;; version 3 of the License, or (at your option) any later version.
bcae9a98 9;;;;
53befeb7
NJ
10;;;; This library is distributed in the hope that it will be useful,
11;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13;;;; Lesser General Public License for more details.
bcae9a98 14;;;;
53befeb7
NJ
15;;;; You should have received a copy of the GNU Lesser General Public
16;;;; License along with this library; if not, write to the Free Software
17;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
cf10678f
AW
18
19;;; Code:
20
21(define-module (language tree-il analyze)
66d3e9a3 22 #:use-module (srfi srfi-1)
4b856371 23 #:use-module (srfi srfi-9)
bcae9a98 24 #:use-module (srfi srfi-11)
60273407 25 #:use-module (srfi srfi-26)
5cbf2e1d 26 #:use-module (ice-9 vlist)
16a3b316 27 #:use-module (ice-9 match)
cf10678f 28 #:use-module (system base syntax)
4b856371 29 #:use-module (system base message)
af5ed549 30 #:use-module (system vm program)
cf10678f 31 #:use-module (language tree-il)
99480e11 32 #:use-module (system base pmatch)
4b856371 33 #:export (analyze-lexicals
48b1db75
LC
34 analyze-tree
35 unused-variable-analysis
bcae9a98 36 unused-toplevel-analysis
ae03cf1f 37 unbound-variable-analysis
75365375
LC
38 arity-analysis
39 format-analysis))
cf10678f 40
66d3e9a3
AW
41;; Allocation is the process of assigning storage locations for lexical
42;; variables. A lexical variable has a distinct "address", or storage
43;; location, for each procedure in which it is referenced.
44;;
45;; A variable is "local", i.e., allocated on the stack, if it is
46;; referenced from within the procedure that defined it. Otherwise it is
47;; a "closure" variable. For example:
48;;
49;; (lambda (a) a) ; a will be local
50;; `a' is local to the procedure.
51;;
52;; (lambda (a) (lambda () a))
53;; `a' is local to the outer procedure, but a closure variable with
54;; respect to the inner procedure.
55;;
56;; If a variable is ever assigned, it needs to be heap-allocated
57;; ("boxed"). This is so that closures and continuations capture the
58;; variable's identity, not just one of the values it may have over the
59;; course of program execution. If the variable is never assigned, there
60;; is no distinction between value and identity, so closing over its
61;; identity (whether through closures or continuations) can make a copy
62;; of its value instead.
63;;
64;; Local variables are stored on the stack within a procedure's call
65;; frame. Their index into the stack is determined from their linear
66;; postion within a procedure's binding path:
cf10678f
AW
67;; (let (0 1)
68;; (let (2 3) ...)
69;; (let (2) ...))
70;; (let (2 3 4) ...))
71;; etc.
72;;
5af166bd
AW
73;; This algorithm has the problem that variables are only allocated
74;; indices at the end of the binding path. If variables bound early in
75;; the path are not used in later portions of the path, their indices
76;; will not be recycled. This problem is particularly egregious in the
77;; expansion of `or':
78;;
79;; (or x y z)
80;; -> (let ((a x)) (if a a (let ((b y)) (if b b z))))
81;;
b6d93b11
AW
82;; As you can see, the `a' binding is only used in the ephemeral
83;; `consequent' clause of the first `if', but its index would be
84;; reserved for the whole of the `or' expansion. So we have a hack for
85;; this specific case. A proper solution would be some sort of liveness
86;; analysis, and not our linear allocation algorithm.
5af166bd 87;;
282d128c
AW
88;; Closure variables are captured when a closure is created, and stored in a
89;; vector inline to the closure object itself. Each closure variable has a
90;; unique index into that vector.
66d3e9a3 91;;
9059993f
AW
92;; There is one more complication. Procedures bound by <fix> may, in
93;; some cases, be rendered inline to their parent procedure. That is to
94;; say,
95;;
96;; (letrec ((lp (lambda () (lp)))) (lp))
97;; => (fix ((lp (lambda () (lp)))) (lp))
98;; => goto FIX-BODY; LP: goto LP; FIX-BODY: goto LP;
99;; ^ jump over the loop ^ the fixpoint lp ^ starting off the loop
100;;
101;; The upshot is that we don't have to allocate any space for the `lp'
102;; closure at all, as it can be rendered inline as a loop. So there is
103;; another kind of allocation, "label allocation", in which the
104;; procedure is simply a label, placed at the start of the lambda body.
105;; The label is the gensym under which the lambda expression is bound.
106;;
107;; The analyzer checks to see that the label is called with the correct
108;; number of arguments. Calls to labels compile to rename + goto.
109;; Lambda, the ultimate goto!
110;;
66d3e9a3
AW
111;;
112;; The return value of `analyze-lexicals' is a hash table, the
113;; "allocation".
114;;
115;; The allocation maps gensyms -- recall that each lexically bound
116;; variable has a unique gensym -- to storage locations ("addresses").
117;; Since one gensym may have many storage locations, if it is referenced
118;; in many procedures, it is a two-level map.
119;;
120;; The allocation also stored information on how many local variables
9059993f
AW
121;; need to be allocated for each procedure, lexicals that have been
122;; translated into labels, and information on what free variables to
123;; capture from its lexical parent procedure.
66d3e9a3 124;;
8a4ca0ea
AW
125;; In addition, we have a conflation: while we're traversing the code,
126;; recording information to pass to the compiler, we take the
127;; opportunity to generate labels for each lambda-case clause, so that
128;; generated code can skip argument checks at runtime if they match at
129;; compile-time.
130;;
282d128c
AW
131;; Also, while we're a-traversing and an-allocating, we check prompt
132;; handlers to see if the "continuation" argument is used. If not, we
133;; mark the prompt as being "escape-only". This allows us to implement
134;; `catch' and `throw' using `prompt' and `control', but without causing
135;; a continuation to be reified. Heh heh.
136;;
66d3e9a3
AW
137;; That is:
138;;
139;; sym -> {lambda -> address}
8a4ca0ea
AW
140;; lambda -> (labels . free-locs)
141;; lambda-case -> (gensym . nlocs)
282d128c 142;; prompt -> escape-only?
66d3e9a3 143;;
9059993f 144;; address ::= (local? boxed? . index)
8a4ca0ea 145;; labels ::= ((sym . lambda) ...)
66d3e9a3
AW
146;; free-locs ::= ((sym0 . address0) (sym1 . address1) ...)
147;; free variable addresses are relative to parent proc.
148
149(define (make-hashq k v)
150 (let ((res (make-hash-table)))
151 (hashq-set! res k v)
152 res))
cf10678f
AW
153
154(define (analyze-lexicals x)
66d3e9a3
AW
155 ;; bound-vars: lambda -> (sym ...)
156 ;; all identifiers bound within a lambda
9059993f 157 (define bound-vars (make-hash-table))
66d3e9a3
AW
158 ;; free-vars: lambda -> (sym ...)
159 ;; all identifiers referenced in a lambda, but not bound
160 ;; NB, this includes identifiers referenced by contained lambdas
9059993f 161 (define free-vars (make-hash-table))
66d3e9a3
AW
162 ;; assigned: sym -> #t
163 ;; variables that are assigned
d97b69d9 164 (define assigned (make-hash-table))
5af166bd 165 ;; refcounts: sym -> count
66d3e9a3 166 ;; allows us to detect the or-expansion in O(1) time
9059993f 167 (define refcounts (make-hash-table))
8a4ca0ea 168 ;; labels: sym -> lambda
9059993f 169 ;; for determining if fixed-point procedures can be rendered as
8a4ca0ea 170 ;; labels.
9059993f
AW
171 (define labels (make-hash-table))
172
66d3e9a3 173 ;; returns variables referenced in expr
d97b69d9 174 (define (analyze! x proc labels-in-proc tail? tail-call-args)
aa9c1985 175 (define (step y) (analyze! y proc '() #f #f))
d97b69d9
AW
176 (define (step-tail y) (analyze! y proc labels-in-proc tail? #f))
177 (define (step-tail-call y args) (analyze! y proc labels-in-proc #f
178 (and tail? args)))
179 (define (recur/labels x new-proc labels)
180 (analyze! x new-proc (append labels labels-in-proc) #t #f))
181 (define (recur x new-proc) (analyze! x new-proc '() tail? #f))
cf10678f
AW
182 (record-case x
183 ((<application> proc args)
d97b69d9
AW
184 (apply lset-union eq? (step-tail-call proc args)
185 (map step args)))
cf10678f 186
b6d93b11
AW
187 ((<conditional> test consequent alternate)
188 (lset-union eq? (step test) (step-tail consequent) (step-tail alternate)))
cf10678f 189
e5f5113c 190 ((<lexical-ref> gensym)
5af166bd 191 (hashq-set! refcounts gensym (1+ (hashq-ref refcounts gensym 0)))
d97b69d9
AW
192 (if (not (and tail-call-args
193 (memq gensym labels-in-proc)
8a4ca0ea
AW
194 (let ((p (hashq-ref labels gensym)))
195 (and p
196 (let lp ((c (lambda-body p)))
197 (and c (lambda-case? c)
198 (or
199 ;; for now prohibit optional &
200 ;; keyword arguments; can relax this
201 ;; restriction later
202 (and (= (length (lambda-case-req c))
203 (length tail-call-args))
204 (not (lambda-case-opt c))
205 (not (lambda-case-kw c))
1e2a8edb 206 (not (lambda-case-rest c)))
3a88cb3b 207 (lp (lambda-case-alternate c)))))))))
d97b69d9 208 (hashq-set! labels gensym #f))
66d3e9a3 209 (list gensym))
cf10678f 210
e5f5113c 211 ((<lexical-set> gensym exp)
66d3e9a3 212 (hashq-set! assigned gensym #t)
d97b69d9 213 (hashq-set! labels gensym #f)
66d3e9a3 214 (lset-adjoin eq? (step exp) gensym))
cf10678f 215
e5f5113c 216 ((<module-set> exp)
cf10678f
AW
217 (step exp))
218
e5f5113c 219 ((<toplevel-set> exp)
cf10678f
AW
220 (step exp))
221
e5f5113c 222 ((<toplevel-define> exp)
cf10678f
AW
223 (step exp))
224
225 ((<sequence> exps)
d97b69d9
AW
226 (let lp ((exps exps) (ret '()))
227 (cond ((null? exps) '())
228 ((null? (cdr exps))
229 (lset-union eq? ret (step-tail (car exps))))
230 (else
231 (lp (cdr exps) (lset-union eq? ret (step (car exps))))))))
cf10678f 232
8a4ca0ea
AW
233 ((<lambda> body)
234 ;; order is important here
235 (hashq-set! bound-vars x '())
236 (let ((free (recur body x)))
237 (hashq-set! bound-vars x (reverse! (hashq-ref bound-vars x)))
238 (hashq-set! free-vars x free)
239 free))
240
93f63467 241 ((<lambda-case> opt kw inits gensyms body alternate)
8a4ca0ea 242 (hashq-set! bound-vars proc
93f63467 243 (append (reverse gensyms) (hashq-ref bound-vars proc)))
8a4ca0ea
AW
244 (lset-union
245 eq?
246 (lset-difference eq?
b0c8c187
AW
247 (lset-union eq?
248 (apply lset-union eq? (map step inits))
8a4ca0ea 249 (step-tail body))
93f63467 250 gensyms)
3a88cb3b 251 (if alternate (step-tail alternate) '())))
66d3e9a3 252
93f63467 253 ((<let> gensyms vals body)
66d3e9a3 254 (hashq-set! bound-vars proc
93f63467 255 (append (reverse gensyms) (hashq-ref bound-vars proc)))
66d3e9a3 256 (lset-difference eq?
d97b69d9 257 (apply lset-union eq? (step-tail body) (map step vals))
93f63467 258 gensyms))
cf10678f 259
93f63467 260 ((<letrec> gensyms vals body)
66d3e9a3 261 (hashq-set! bound-vars proc
93f63467
AW
262 (append (reverse gensyms) (hashq-ref bound-vars proc)))
263 (for-each (lambda (sym) (hashq-set! assigned sym #t)) gensyms)
66d3e9a3 264 (lset-difference eq?
d97b69d9 265 (apply lset-union eq? (step-tail body) (map step vals))
93f63467 266 gensyms))
66d3e9a3 267
93f63467 268 ((<fix> gensyms vals body)
d97b69d9 269 ;; Try to allocate these procedures as labels.
8a4ca0ea 270 (for-each (lambda (sym val) (hashq-set! labels sym val))
93f63467 271 gensyms vals)
c21c89b1 272 (hashq-set! bound-vars proc
93f63467 273 (append (reverse gensyms) (hashq-ref bound-vars proc)))
d97b69d9
AW
274 ;; Step into subexpressions.
275 (let* ((var-refs
276 (map
277 ;; Since we're trying to label-allocate the lambda,
278 ;; pretend it's not a closure, and just recurse into its
279 ;; body directly. (Otherwise, recursing on a closure
280 ;; that references one of the fix's bound vars would
281 ;; prevent label allocation.)
282 (lambda (x)
283 (record-case x
8a4ca0ea
AW
284 ((<lambda> body)
285 ;; just like the closure case, except here we use
286 ;; recur/labels instead of recur
287 (hashq-set! bound-vars x '())
93f63467 288 (let ((free (recur/labels body x gensyms)))
8a4ca0ea
AW
289 (hashq-set! bound-vars x (reverse! (hashq-ref bound-vars x)))
290 (hashq-set! free-vars x free)
291 free))))
d97b69d9 292 vals))
93f63467
AW
293 (vars-with-refs (map cons gensyms var-refs))
294 (body-refs (recur/labels body proc gensyms)))
d97b69d9
AW
295 (define (delabel-dependents! sym)
296 (let ((refs (assq-ref vars-with-refs sym)))
297 (if refs
298 (for-each (lambda (sym)
299 (if (hashq-ref labels sym)
300 (begin
301 (hashq-set! labels sym #f)
302 (delabel-dependents! sym))))
303 refs))))
304 ;; Stepping into the lambdas and the body might have made some
305 ;; procedures not label-allocatable -- which might have
306 ;; knock-on effects. For example:
307 ;; (fix ((a (lambda () (b)))
308 ;; (b (lambda () a)))
309 ;; (a))
310 ;; As far as `a' is concerned, both `a' and `b' are
311 ;; label-allocatable. But `b' references `a' not in a proc-tail
312 ;; position, which makes `a' not label-allocatable. The
313 ;; knock-on effect is that, when back-propagating this
314 ;; information to `a', `b' will also become not
315 ;; label-allocatable, as it is referenced within `a', which is
316 ;; allocated as a closure. This is a transitive relationship.
317 (for-each (lambda (sym)
318 (if (not (hashq-ref labels sym))
319 (delabel-dependents! sym)))
93f63467 320 gensyms)
d97b69d9
AW
321 ;; Now lift bound variables with label-allocated lambdas to the
322 ;; parent procedure.
323 (for-each
324 (lambda (sym val)
325 (if (hashq-ref labels sym)
326 ;; Remove traces of the label-bound lambda. The free
327 ;; vars will propagate up via the return val.
328 (begin
329 (hashq-set! bound-vars proc
330 (append (hashq-ref bound-vars val)
331 (hashq-ref bound-vars proc)))
332 (hashq-remove! bound-vars val)
333 (hashq-remove! free-vars val))))
93f63467 334 gensyms vals)
d97b69d9
AW
335 (lset-difference eq?
336 (apply lset-union eq? body-refs var-refs)
93f63467 337 gensyms)))
c21c89b1 338
8a4ca0ea
AW
339 ((<let-values> exp body)
340 (lset-union eq? (step exp) (step body)))
66d3e9a3 341
8da6ab34 342 ((<dynwind> body winder unwinder)
282d128c
AW
343 (lset-union eq? (step body) (step winder) (step unwinder)))
344
d7c53a86
AW
345 ((<dynlet> fluids vals body)
346 (apply lset-union eq? (step body) (map step (append fluids vals))))
347
706a705e
AW
348 ((<dynref> fluid)
349 (step fluid))
350
351 ((<dynset> fluid exp)
352 (lset-union eq? (step fluid) (step exp)))
353
07a0c7d5 354 ((<prompt> tag body handler)
b9204185 355 (lset-union eq? (step tag) (step body) (step-tail handler)))
282d128c 356
2d026f04
AW
357 ((<abort> tag args tail)
358 (apply lset-union eq? (step tag) (step tail) (map step args)))
282d128c 359
66d3e9a3
AW
360 (else '())))
361
9059993f 362 ;; allocation: sym -> {lambda -> address}
3004fe26
MW
363 ;; lambda -> (labels . free-locs)
364 ;; lambda-case -> (gensym . nlocs)
9059993f
AW
365 (define allocation (make-hash-table))
366
66d3e9a3
AW
367 (define (allocate! x proc n)
368 (define (recur y) (allocate! y proc n))
369 (record-case x
370 ((<application> proc args)
371 (apply max (recur proc) (map recur args)))
cf10678f 372
b6d93b11
AW
373 ((<conditional> test consequent alternate)
374 (max (recur test) (recur consequent) (recur alternate)))
cf10678f 375
e5f5113c 376 ((<lexical-set> exp)
66d3e9a3
AW
377 (recur exp))
378
e5f5113c 379 ((<module-set> exp)
66d3e9a3
AW
380 (recur exp))
381
e5f5113c 382 ((<toplevel-set> exp)
66d3e9a3
AW
383 (recur exp))
384
e5f5113c 385 ((<toplevel-define> exp)
66d3e9a3
AW
386 (recur exp))
387
388 ((<sequence> exps)
389 (apply max (map recur exps)))
390
8a4ca0ea 391 ((<lambda> body)
66d3e9a3
AW
392 ;; allocate closure vars in order
393 (let lp ((c (hashq-ref free-vars x)) (n 0))
394 (if (pair? c)
395 (begin
396 (hashq-set! (hashq-ref allocation (car c))
397 x
398 `(#f ,(hashq-ref assigned (car c)) . ,n))
399 (lp (cdr c) (1+ n)))))
400
8a4ca0ea 401 (let ((nlocs (allocate! body x 0))
66d3e9a3
AW
402 (free-addresses
403 (map (lambda (v)
404 (hashq-ref (hashq-ref allocation v) proc))
9059993f
AW
405 (hashq-ref free-vars x)))
406 (labels (filter cdr
407 (map (lambda (sym)
408 (cons sym (hashq-ref labels sym)))
409 (hashq-ref bound-vars x)))))
66d3e9a3 410 ;; set procedure allocations
8a4ca0ea 411 (hashq-set! allocation x (cons labels free-addresses)))
66d3e9a3 412 n)
cf10678f 413
93f63467 414 ((<lambda-case> opt kw inits gensyms body alternate)
8a4ca0ea 415 (max
93f63467
AW
416 (let lp ((gensyms gensyms) (n n))
417 (if (null? gensyms)
b0c8c187
AW
418 (let ((nlocs (apply
419 max
b0c8c187
AW
420 (allocate! body proc n)
421 ;; inits not logically at the end, but they
422 ;; are the list...
9a9d82c2 423 (map (lambda (x) (allocate! x proc n)) inits))))
8a4ca0ea
AW
424 ;; label and nlocs for the case
425 (hashq-set! allocation x (cons (gensym ":LCASE") nlocs))
426 nlocs)
427 (begin
93f63467 428 (hashq-set! allocation (car gensyms)
8a4ca0ea 429 (make-hashq
93f63467
AW
430 proc `(#t ,(hashq-ref assigned (car gensyms)) . ,n)))
431 (lp (cdr gensyms) (1+ n)))))
3a88cb3b 432 (if alternate (allocate! alternate proc n) n)))
8a4ca0ea 433
93f63467 434 ((<let> gensyms vals body)
66d3e9a3
AW
435 (let ((nmax (apply max (map recur vals))))
436 (cond
437 ;; the `or' hack
438 ((and (conditional? body)
93f63467
AW
439 (= (length gensyms) 1)
440 (let ((v (car gensyms)))
66d3e9a3
AW
441 (and (not (hashq-ref assigned v))
442 (= (hashq-ref refcounts v 0) 2)
443 (lexical-ref? (conditional-test body))
444 (eq? (lexical-ref-gensym (conditional-test body)) v)
b6d93b11
AW
445 (lexical-ref? (conditional-consequent body))
446 (eq? (lexical-ref-gensym (conditional-consequent body)) v))))
93f63467 447 (hashq-set! allocation (car gensyms)
66d3e9a3
AW
448 (make-hashq proc `(#t #f . ,n)))
449 ;; the 1+ for this var
b6d93b11 450 (max nmax (1+ n) (allocate! (conditional-alternate body) proc n)))
66d3e9a3 451 (else
93f63467
AW
452 (let lp ((gensyms gensyms) (n n))
453 (if (null? gensyms)
66d3e9a3 454 (max nmax (allocate! body proc n))
93f63467 455 (let ((v (car gensyms)))
cf10678f
AW
456 (hashq-set!
457 allocation v
66d3e9a3
AW
458 (make-hashq proc
459 `(#t ,(hashq-ref assigned v) . ,n)))
93f63467 460 (lp (cdr gensyms) (1+ n)))))))))
66d3e9a3 461
93f63467
AW
462 ((<letrec> gensyms vals body)
463 (let lp ((gensyms gensyms) (n n))
464 (if (null? gensyms)
66d3e9a3
AW
465 (let ((nmax (apply max
466 (map (lambda (x)
467 (allocate! x proc n))
468 vals))))
469 (max nmax (allocate! body proc n)))
93f63467 470 (let ((v (car gensyms)))
66d3e9a3
AW
471 (hashq-set!
472 allocation v
473 (make-hashq proc
474 `(#t ,(hashq-ref assigned v) . ,n)))
93f63467 475 (lp (cdr gensyms) (1+ n))))))
cf10678f 476
93f63467
AW
477 ((<fix> gensyms vals body)
478 (let lp ((in gensyms) (n n))
d97b69d9 479 (if (null? in)
93f63467 480 (let lp ((gensyms gensyms) (vals vals) (nmax n))
d97b69d9 481 (cond
93f63467 482 ((null? gensyms)
d97b69d9 483 (max nmax (allocate! body proc n)))
93f63467 484 ((hashq-ref labels (car gensyms))
8a4ca0ea 485 ;; allocate lambda body inline to proc
93f63467 486 (lp (cdr gensyms)
d97b69d9
AW
487 (cdr vals)
488 (record-case (car vals)
8a4ca0ea
AW
489 ((<lambda> body)
490 (max nmax (allocate! body proc n))))))
d97b69d9
AW
491 (else
492 ;; allocate closure
93f63467 493 (lp (cdr gensyms)
d97b69d9
AW
494 (cdr vals)
495 (max nmax (allocate! (car vals) proc n))))))
496
497 (let ((v (car in)))
498 (cond
499 ((hashq-ref assigned v)
500 (error "fixpoint procedures may not be assigned" x))
501 ((hashq-ref labels v)
502 ;; no binding, it's a label
503 (lp (cdr in) n))
504 (else
505 ;; allocate closure binding
506 (hashq-set! allocation v (make-hashq proc `(#t #f . ,n)))
507 (lp (cdr in) (1+ n))))))))
c21c89b1 508
8a4ca0ea
AW
509 ((<let-values> exp body)
510 (max (recur exp) (recur body)))
66d3e9a3 511
8da6ab34 512 ((<dynwind> body winder unwinder)
282d128c
AW
513 (max (recur body) (recur winder) (recur unwinder)))
514
d7c53a86
AW
515 ((<dynlet> fluids vals body)
516 (apply max (recur body) (map recur (append fluids vals))))
517
706a705e
AW
518 ((<dynref> fluid)
519 (recur fluid))
520
521 ((<dynset> fluid exp)
522 (max (recur fluid) (recur exp)))
523
07a0c7d5 524 ((<prompt> tag body handler)
282d128c 525 (let ((cont-var (and (lambda-case? handler)
93f63467
AW
526 (pair? (lambda-case-gensyms handler))
527 (car (lambda-case-gensyms handler)))))
282d128c
AW
528 (hashq-set! allocation x
529 (and cont-var (zero? (hashq-ref refcounts cont-var 0))))
07a0c7d5 530 (max (recur tag) (recur body) (recur handler))))
282d128c 531
2d026f04
AW
532 ((<abort> tag args tail)
533 (apply max (recur tag) (recur tail) (map recur args)))
282d128c 534
66d3e9a3 535 (else n)))
cf10678f 536
d97b69d9 537 (analyze! x #f '() #t #f)
66d3e9a3 538 (allocate! x #f 0)
cf10678f
AW
539
540 allocation)
4b856371
LC
541
542\f
48b1db75
LC
543;;;
544;;; Tree analyses for warnings.
545;;;
546
547(define-record-type <tree-analysis>
548 (make-tree-analysis leaf down up post init)
549 tree-analysis?
795ab688
LC
550 (leaf tree-analysis-leaf) ;; (lambda (x result env locs) ...)
551 (down tree-analysis-down) ;; (lambda (x result env locs) ...)
552 (up tree-analysis-up) ;; (lambda (x result env locs) ...)
48b1db75
LC
553 (post tree-analysis-post) ;; (lambda (result env) ...)
554 (init tree-analysis-init)) ;; arbitrary value
555
556(define (analyze-tree analyses tree env)
557 "Run all tree analyses listed in ANALYSES on TREE for ENV, using
795ab688
LC
558`tree-il-fold'. Return TREE. The leaf/down/up procedures of each analysis are
559passed a ``location stack', which is the stack of `tree-il-src' values for each
560parent tree (a list); it can be used to approximate source location when
561accurate information is missing from a given `tree-il' element."
562
563 (define (traverse proc update-locs)
564 ;; Return a tree traversing procedure that returns a list of analysis
565 ;; results prepended by the location stack.
48b1db75 566 (lambda (x results)
795ab688
LC
567 (let ((locs (update-locs x (car results))))
568 (cons locs ;; the location stack
569 (map (lambda (analysis result)
570 ((proc analysis) x result env locs))
571 analyses
572 (cdr results))))))
573
574 ;; Keeping/extending/shrinking the location stack.
575 (define (keep-locs x locs) locs)
576 (define (extend-locs x locs) (cons (tree-il-src x) locs))
577 (define (shrink-locs x locs) (cdr locs))
48b1db75
LC
578
579 (let ((results
795ab688
LC
580 (tree-il-fold (traverse tree-analysis-leaf keep-locs)
581 (traverse tree-analysis-down extend-locs)
582 (traverse tree-analysis-up shrink-locs)
583 (cons '() ;; empty location stack
584 (map tree-analysis-init analyses))
48b1db75
LC
585 tree)))
586
587 (for-each (lambda (analysis result)
588 ((tree-analysis-post analysis) result env))
589 analyses
795ab688 590 (cdr results)))
48b1db75
LC
591
592 tree)
593
594\f
4b856371
LC
595;;;
596;;; Unused variable analysis.
597;;;
598
599;; <binding-info> records are used during tree traversals in
795ab688
LC
600;; `unused-variable-analysis'. They contain a list of the local vars
601;; currently in scope, and a list of locals vars that have been referenced.
4b856371 602(define-record-type <binding-info>
795ab688 603 (make-binding-info vars refs)
4b856371
LC
604 binding-info?
605 (vars binding-info-vars) ;; ((GENSYM NAME LOCATION) ...)
795ab688 606 (refs binding-info-refs)) ;; (GENSYM ...)
4b856371 607
3a1a883b
LC
608(define (gensym? sym)
609 ;; Return #t if SYM is (likely) a generated symbol.
610 (string-any #\space (symbol->string sym)))
611
48b1db75 612(define unused-variable-analysis
ae03cf1f 613 ;; Report unused variables in the given tree.
48b1db75 614 (make-tree-analysis
795ab688 615 (lambda (x info env locs)
48b1db75
LC
616 ;; X is a leaf: extend INFO's refs accordingly.
617 (let ((refs (binding-info-refs info))
795ab688 618 (vars (binding-info-vars info)))
48b1db75
LC
619 (record-case x
620 ((<lexical-ref> gensym)
a670e672 621 (make-binding-info vars (vhash-consq gensym #t refs)))
48b1db75
LC
622 (else info))))
623
795ab688 624 (lambda (x info env locs)
48b1db75
LC
625 ;; Going down into X: extend INFO's variable list
626 ;; accordingly.
627 (let ((refs (binding-info-refs info))
628 (vars (binding-info-vars info))
48b1db75
LC
629 (src (tree-il-src x)))
630 (define (extend inner-vars inner-names)
a670e672
LC
631 (fold (lambda (var name vars)
632 (vhash-consq var (list name src) vars))
633 vars
634 inner-vars
635 inner-names))
636
48b1db75
LC
637 (record-case x
638 ((<lexical-set> gensym)
a670e672 639 (make-binding-info vars (vhash-consq gensym #t refs)))
93f63467 640 ((<lambda-case> req opt inits rest kw gensyms)
48b1db75 641 (let ((names `(,@req
632e7c32 642 ,@(or opt '())
48b1db75
LC
643 ,@(if rest (list rest) '())
644 ,@(if kw (map cadr (cdr kw)) '()))))
93f63467
AW
645 (make-binding-info (extend gensyms names) refs)))
646 ((<let> gensyms names)
647 (make-binding-info (extend gensyms names) refs))
648 ((<letrec> gensyms names)
649 (make-binding-info (extend gensyms names) refs))
650 ((<fix> gensyms names)
651 (make-binding-info (extend gensyms names) refs))
48b1db75
LC
652 (else info))))
653
795ab688 654 (lambda (x info env locs)
48b1db75
LC
655 ;; Leaving X's scope: shrink INFO's variable list
656 ;; accordingly and reported unused nested variables.
657 (let ((refs (binding-info-refs info))
795ab688 658 (vars (binding-info-vars info)))
48b1db75 659 (define (shrink inner-vars refs)
a670e672
LC
660 (vlist-for-each
661 (lambda (var)
662 (let ((gensym (car var)))
663 ;; Don't report lambda parameters as unused.
664 (if (and (memq gensym inner-vars)
665 (not (vhash-assq gensym refs))
666 (not (lambda-case? x)))
667 (let ((name (cadr var))
668 ;; We can get approximate source location by going up
669 ;; the LOCS location stack.
670 (loc (or (caddr var)
671 (find pair? locs))))
3a1a883b
LC
672 (if (and (not (gensym? name))
673 (not (eq? name '_)))
674 (warning 'unused-variable loc name))))))
a670e672
LC
675 vars)
676 (vlist-drop vars (length inner-vars)))
48b1db75
LC
677
678 ;; For simplicity, we leave REFS untouched, i.e., with
679 ;; names of variables that are now going out of scope.
680 ;; It doesn't hurt as these are unique names, it just
681 ;; makes REFS unnecessarily fat.
682 (record-case x
93f63467
AW
683 ((<lambda-case> gensyms)
684 (make-binding-info (shrink gensyms refs) refs))
685 ((<let> gensyms)
686 (make-binding-info (shrink gensyms refs) refs))
687 ((<letrec> gensyms)
688 (make-binding-info (shrink gensyms refs) refs))
689 ((<fix> gensyms)
690 (make-binding-info (shrink gensyms refs) refs))
48b1db75
LC
691 (else info))))
692
693 (lambda (result env) #t)
a670e672 694 (make-binding-info vlist-null vlist-null)))
f67ddf9d
LC
695
696\f
bcae9a98
LC
697;;;
698;;; Unused top-level variable analysis.
699;;;
700
628ddb80 701;; <reference-graph> record top-level definitions that are made, references to
bcae9a98
LC
702;; top-level definitions and their context (the top-level definition in which
703;; the reference appears), as well as the current context (the top-level
704;; definition we're currently in). The second part (`refs' below) is
628ddb80
LC
705;; effectively a graph from which we can determine unused top-level definitions.
706(define-record-type <reference-graph>
707 (make-reference-graph refs defs toplevel-context)
708 reference-graph?
709 (defs reference-graph-defs) ;; ((NAME . LOC) ...)
710 (refs reference-graph-refs) ;; ((REF-CONTEXT REF ...) ...)
711 (toplevel-context reference-graph-toplevel-context)) ;; NAME | #f
712
5cbf2e1d
LC
713(define (graph-reachable-nodes root refs reachable)
714 ;; Add to REACHABLE the nodes reachable from ROOT in graph REFS. REFS is a
715 ;; vhash mapping nodes to the list of their children: for instance,
716 ;; ((A -> (B C)) (B -> (A)) (C -> ())) corresponds to
bcae9a98
LC
717 ;;
718 ;; ,-------.
719 ;; v |
720 ;; A ----> B
721 ;; |
722 ;; v
723 ;; C
5cbf2e1d
LC
724 ;;
725 ;; REACHABLE is a vhash of nodes known to be otherwise reachable.
bcae9a98
LC
726
727 (let loop ((root root)
5cbf2e1d
LC
728 (path vlist-null)
729 (result reachable))
730 (if (or (vhash-assq root path)
731 (vhash-assq root result))
bcae9a98 732 result
5cbf2e1d
LC
733 (let* ((children (or (and=> (vhash-assq root refs) cdr) '()))
734 (path (vhash-consq root #t path))
735 (result (fold (lambda (kid result)
736 (loop kid path result))
737 result
738 children)))
739 (fold (lambda (kid result)
740 (vhash-consq kid #t result))
741 result
742 children)))))
bcae9a98 743
628ddb80 744(define (graph-reachable-nodes* roots refs)
bcae9a98 745 ;; Return the list of nodes in REFS reachable from the nodes listed in ROOTS.
5cbf2e1d
LC
746 (vlist-fold (lambda (root+true result)
747 (let* ((root (car root+true))
748 (reachable (graph-reachable-nodes root refs result)))
749 (vhash-consq root #t reachable)))
750 vlist-null
751 roots))
752
753(define (partition* pred vhash)
754 ;; Partition VHASH according to PRED. Return the two resulting vhashes.
755 (let ((result
756 (vlist-fold (lambda (k+v result)
757 (let ((k (car k+v))
758 (v (cdr k+v))
759 (r1 (car result))
760 (r2 (cdr result)))
761 (if (pred k)
762 (cons (vhash-consq k v r1) r2)
763 (cons r1 (vhash-consq k v r2)))))
764 (cons vlist-null vlist-null)
765 vhash)))
766 (values (car result) (cdr result))))
bcae9a98
LC
767
768(define unused-toplevel-analysis
769 ;; Report unused top-level definitions that are not exported.
770 (let ((add-ref-from-context
628ddb80
LC
771 (lambda (graph name)
772 ;; Add an edge CTX -> NAME in GRAPH.
773 (let* ((refs (reference-graph-refs graph))
774 (defs (reference-graph-defs graph))
775 (ctx (reference-graph-toplevel-context graph))
5cbf2e1d
LC
776 (ctx-refs (or (and=> (vhash-assq ctx refs) cdr) '())))
777 (make-reference-graph (vhash-consq ctx (cons name ctx-refs) refs)
628ddb80 778 defs ctx)))))
bcae9a98
LC
779 (define (macro-variable? name env)
780 (and (module? env)
781 (let ((var (module-variable env name)))
782 (and var (variable-bound? var)
783 (macro? (variable-ref var))))))
784
785 (make-tree-analysis
628ddb80 786 (lambda (x graph env locs)
bcae9a98 787 ;; X is a leaf.
628ddb80 788 (let ((ctx (reference-graph-toplevel-context graph)))
bcae9a98
LC
789 (record-case x
790 ((<toplevel-ref> name src)
628ddb80
LC
791 (add-ref-from-context graph name))
792 (else graph))))
bcae9a98 793
628ddb80 794 (lambda (x graph env locs)
bcae9a98 795 ;; Going down into X.
628ddb80
LC
796 (let ((ctx (reference-graph-toplevel-context graph))
797 (refs (reference-graph-refs graph))
798 (defs (reference-graph-defs graph)))
bcae9a98
LC
799 (record-case x
800 ((<toplevel-define> name src)
801 (let ((refs refs)
5cbf2e1d
LC
802 (defs (vhash-consq name (or src (find pair? locs))
803 defs)))
628ddb80 804 (make-reference-graph refs defs name)))
bcae9a98 805 ((<toplevel-set> name src)
628ddb80
LC
806 (add-ref-from-context graph name))
807 (else graph))))
bcae9a98 808
628ddb80 809 (lambda (x graph env locs)
bcae9a98
LC
810 ;; Leaving X's scope.
811 (record-case x
812 ((<toplevel-define>)
628ddb80
LC
813 (let ((refs (reference-graph-refs graph))
814 (defs (reference-graph-defs graph)))
815 (make-reference-graph refs defs #f)))
816 (else graph)))
bcae9a98 817
628ddb80
LC
818 (lambda (graph env)
819 ;; Process the resulting reference graph: determine all private definitions
bcae9a98
LC
820 ;; not reachable from any public definition. Macros
821 ;; (syntax-transformers), which are globally bound, never considered
822 ;; unused since we can't tell whether a macro is actually used; in
628ddb80 823 ;; addition, macros are considered roots of the graph since they may use
bcae9a98
LC
824 ;; private bindings. FIXME: The `make-syntax-transformer' calls don't
825 ;; contain any literal `toplevel-ref' of the global bindings they use so
826 ;; this strategy fails.
827 (define (exported? name)
828 (if (module? env)
829 (module-variable (module-public-interface env) name)
830 #t))
831
832 (let-values (((public-defs private-defs)
5cbf2e1d
LC
833 (partition* (lambda (name)
834 (or (exported? name)
835 (macro-variable? name env)))
836 (reference-graph-defs graph))))
837 (let* ((roots (vhash-consq #f #t public-defs))
628ddb80
LC
838 (refs (reference-graph-refs graph))
839 (reachable (graph-reachable-nodes* roots refs))
5cbf2e1d
LC
840 (unused (vlist-filter (lambda (name+src)
841 (not (vhash-assq (car name+src)
842 reachable)))
843 private-defs)))
844 (vlist-for-each (lambda (name+loc)
845 (let ((name (car name+loc))
846 (loc (cdr name+loc)))
3a1a883b
LC
847 (if (not (gensym? name))
848 (warning 'unused-toplevel loc name))))
5cbf2e1d
LC
849 unused))))
850
851 (make-reference-graph vlist-null vlist-null #f))))
bcae9a98
LC
852
853\f
f67ddf9d
LC
854;;;
855;;; Unbound variable analysis.
856;;;
857
858;; <toplevel-info> records are used during tree traversal in search of
859;; possibly unbound variable. They contain a list of references to
795ab688
LC
860;; potentially unbound top-level variables, and a list of the top-level
861;; defines that have been encountered.
f67ddf9d 862(define-record-type <toplevel-info>
795ab688 863 (make-toplevel-info refs defs)
f67ddf9d
LC
864 toplevel-info?
865 (refs toplevel-info-refs) ;; ((VARIABLE-NAME . LOCATION) ...)
795ab688 866 (defs toplevel-info-defs)) ;; (VARIABLE-NAME ...)
f67ddf9d 867
6bb891dc 868(define (goops-toplevel-definition proc args env)
b6d2306d
LC
869 ;; If application of PROC to ARGS is a GOOPS top-level definition, return
870 ;; the name of the variable being defined; otherwise return #f. This
871 ;; assumes knowledge of the current implementation of `define-class' et al.
6bb891dc 872 (define (toplevel-define-arg args)
16a3b316
LC
873 (match args
874 ((($ <const> _ (and (? symbol?) exp)) _)
875 exp)
876 (_ #f)))
877
878 (match proc
879 (($ <module-ref> _ '(oop goops) 'toplevel-define! #f)
880 (toplevel-define-arg args))
881 (($ <toplevel-ref> _ 'toplevel-define!)
6bb891dc
LC
882 ;; This may be the result of expanding one of the GOOPS macros within
883 ;; `oop/goops.scm'.
16a3b316 884 (and (eq? env (resolve-module '(oop goops)))
6bb891dc 885 (toplevel-define-arg args)))
16a3b316 886 (_ #f)))
b6d2306d 887
48b1db75 888(define unbound-variable-analysis
ae03cf1f 889 ;; Report possibly unbound variables in the given tree.
48b1db75 890 (make-tree-analysis
795ab688 891 (lambda (x info env locs)
48b1db75
LC
892 ;; X is a leaf: extend INFO's refs accordingly.
893 (let ((refs (toplevel-info-refs info))
795ab688 894 (defs (toplevel-info-defs info)))
48b1db75
LC
895 (define (bound? name)
896 (or (and (module? env)
897 (module-variable env name))
04ea6fb5 898 (vhash-assq name defs)))
48b1db75
LC
899
900 (record-case x
901 ((<toplevel-ref> name src)
902 (if (bound? name)
903 info
904 (let ((src (or src (find pair? locs))))
04ea6fb5 905 (make-toplevel-info (vhash-consq name src refs)
795ab688 906 defs))))
48b1db75
LC
907 (else info))))
908
795ab688 909 (lambda (x info env locs)
48b1db75
LC
910 ;; Going down into X.
911 (let* ((refs (toplevel-info-refs info))
912 (defs (toplevel-info-defs info))
795ab688 913 (src (tree-il-src x)))
48b1db75
LC
914 (define (bound? name)
915 (or (and (module? env)
916 (module-variable env name))
04ea6fb5 917 (vhash-assq name defs)))
48b1db75
LC
918
919 (record-case x
920 ((<toplevel-set> name src)
921 (if (bound? name)
795ab688 922 (make-toplevel-info refs defs)
48b1db75 923 (let ((src (find pair? locs)))
04ea6fb5 924 (make-toplevel-info (vhash-consq name src refs)
795ab688 925 defs))))
48b1db75 926 ((<toplevel-define> name)
1e1808c9 927 (make-toplevel-info (vhash-delq name refs)
04ea6fb5 928 (vhash-consq name #t defs)))
48b1db75
LC
929
930 ((<application> proc args)
931 ;; Check for a dynamic top-level definition, as is
932 ;; done by code expanded from GOOPS macros.
933 (let ((name (goops-toplevel-definition proc args
934 env)))
935 (if (symbol? name)
1e1808c9 936 (make-toplevel-info (vhash-delq name refs)
04ea6fb5 937 (vhash-consq name #t defs))
795ab688 938 (make-toplevel-info refs defs))))
48b1db75 939 (else
795ab688 940 (make-toplevel-info refs defs)))))
48b1db75 941
795ab688 942 (lambda (x info env locs)
48b1db75 943 ;; Leaving X's scope.
bcae9a98 944 info)
48b1db75
LC
945
946 (lambda (toplevel env)
947 ;; Post-process the result.
04ea6fb5
LC
948 (vlist-for-each (lambda (name+loc)
949 (let ((name (car name+loc))
950 (loc (cdr name+loc)))
951 (warning 'unbound-variable loc name)))
952 (vlist-reverse (toplevel-info-refs toplevel))))
48b1db75 953
04ea6fb5 954 (make-toplevel-info vlist-null vlist-null)))
ae03cf1f
LC
955
956\f
957;;;
958;;; Arity analysis.
959;;;
960
af5ed549 961;; <arity-info> records contain information about lexical definitions of
ae03cf1f
LC
962;; procedures currently in scope, top-level procedure definitions that have
963;; been encountered, and calls to top-level procedures that have been
964;; encountered.
965(define-record-type <arity-info>
966 (make-arity-info toplevel-calls lexical-lambdas toplevel-lambdas)
967 arity-info?
968 (toplevel-calls toplevel-procedure-calls) ;; ((NAME . APPLICATION) ...)
969 (lexical-lambdas lexical-lambdas) ;; ((GENSYM . DEFINITION) ...)
970 (toplevel-lambdas toplevel-lambdas)) ;; ((NAME . DEFINITION) ...)
971
972(define (validate-arity proc application lexical?)
973 ;; Validate the argument count of APPLICATION, a tree-il application of
974 ;; PROC, emitting a warning in case of argument count mismatch.
975
af5ed549
LC
976 (define (filter-keyword-args keywords allow-other-keys? args)
977 ;; Filter keyword arguments from ARGS and return the resulting list.
978 ;; KEYWORDS is the list of allowed keywords, and ALLOW-OTHER-KEYS?
979 ;; specified whethere keywords not listed in KEYWORDS are allowed.
980 (let loop ((args args)
981 (result '()))
982 (if (null? args)
983 (reverse result)
984 (let ((arg (car args)))
985 (if (and (const? arg)
986 (or (memq (const-exp arg) keywords)
987 (and allow-other-keys?
988 (keyword? (const-exp arg)))))
989 (loop (if (pair? (cdr args))
990 (cddr args)
991 '())
992 result)
993 (loop (cdr args)
994 (cons arg result)))))))
995
99480e11
LC
996 (define (arities proc)
997 ;; Return the arities of PROC, which can be either a tree-il or a
ae03cf1f
LC
998 ;; procedure.
999 (define (len x)
1000 (or (and (or (null? x) (pair? x))
1001 (length x))
1002 0))
af5ed549 1003 (cond ((program? proc)
1e23b461 1004 (values (procedure-name proc)
99480e11
LC
1005 (map (lambda (a)
1006 (list (arity:nreq a) (arity:nopt a) (arity:rest? a)
1007 (map car (arity:kw a))
1008 (arity:allow-other-keys? a)))
1009 (program-arities proc))))
ae03cf1f 1010 ((procedure? proc)
3fc7e2c1 1011 (let ((arity (procedure-minimum-arity proc)))
ae03cf1f 1012 (values (procedure-name proc)
99480e11
LC
1013 (list (list (car arity) (cadr arity) (caddr arity)
1014 #f #f)))))
ae03cf1f 1015 (else
99480e11
LC
1016 (let loop ((name #f)
1017 (proc proc)
1018 (arities '()))
1019 (if (not proc)
1020 (values name (reverse arities))
1021 (record-case proc
3a88cb3b
AW
1022 ((<lambda-case> req opt rest kw alternate)
1023 (loop name alternate
99480e11
LC
1024 (cons (list (len req) (len opt) rest
1025 (and (pair? kw) (map car (cdr kw)))
1026 (and (pair? kw) (car kw)))
1027 arities)))
1028 ((<lambda> meta body)
1029 (loop (assoc-ref meta 'name) body arities))
1030 (else
1031 (values #f #f))))))))
ae03cf1f
LC
1032
1033 (let ((args (application-args application))
1034 (src (tree-il-src application)))
99480e11
LC
1035 (call-with-values (lambda () (arities proc))
1036 (lambda (name arities)
1037 (define matches?
1038 (find (lambda (arity)
1039 (pmatch arity
1040 ((,req ,opt ,rest? ,kw ,aok?)
1041 (let ((args (if (pair? kw)
1042 (filter-keyword-args kw aok? args)
1043 args)))
1044 (if (and req opt)
1045 (let ((count (length args)))
1046 (and (>= count req)
1047 (or rest?
1048 (<= count (+ req opt)))))
1049 #t)))
1050 (else #t)))
1051 arities))
1052
1053 (if (not matches?)
1054 (warning 'arity-mismatch src
1055 (or name (with-output-to-string (lambda () (write proc))))
1056 lexical?)))))
ae03cf1f
LC
1057 #t)
1058
1059(define arity-analysis
1060 ;; Report arity mismatches in the given tree.
1061 (make-tree-analysis
795ab688 1062 (lambda (x info env locs)
ae03cf1f
LC
1063 ;; X is a leaf.
1064 info)
795ab688 1065 (lambda (x info env locs)
ae03cf1f
LC
1066 ;; Down into X.
1067 (define (extend lexical-name val info)
1068 ;; If VAL is a lambda, add NAME to the lexical-lambdas of INFO.
1069 (let ((toplevel-calls (toplevel-procedure-calls info))
1070 (lexical-lambdas (lexical-lambdas info))
1071 (toplevel-lambdas (toplevel-lambdas info)))
1072 (record-case val
1073 ((<lambda> body)
1074 (make-arity-info toplevel-calls
df685ee4
LC
1075 (vhash-consq lexical-name val
1076 lexical-lambdas)
ae03cf1f
LC
1077 toplevel-lambdas))
1078 ((<lexical-ref> gensym)
1079 ;; lexical alias
df685ee4 1080 (let ((val* (vhash-assq gensym lexical-lambdas)))
ae03cf1f
LC
1081 (if (pair? val*)
1082 (extend lexical-name (cdr val*) info)
1083 info)))
1084 ((<toplevel-ref> name)
1085 ;; top-level alias
1086 (make-arity-info toplevel-calls
df685ee4
LC
1087 (vhash-consq lexical-name val
1088 lexical-lambdas)
ae03cf1f
LC
1089 toplevel-lambdas))
1090 (else info))))
1091
1092 (let ((toplevel-calls (toplevel-procedure-calls info))
1093 (lexical-lambdas (lexical-lambdas info))
1094 (toplevel-lambdas (toplevel-lambdas info)))
1095
1096 (record-case x
1097 ((<toplevel-define> name exp)
1098 (record-case exp
1099 ((<lambda> body)
1100 (make-arity-info toplevel-calls
1101 lexical-lambdas
df685ee4 1102 (vhash-consq name exp toplevel-lambdas)))
ae03cf1f
LC
1103 ((<toplevel-ref> name)
1104 ;; alias for another toplevel
df685ee4 1105 (let ((proc (vhash-assq name toplevel-lambdas)))
ae03cf1f
LC
1106 (make-arity-info toplevel-calls
1107 lexical-lambdas
df685ee4
LC
1108 (vhash-consq (toplevel-define-name x)
1109 (if (pair? proc)
1110 (cdr proc)
1111 exp)
1112 toplevel-lambdas))))
ae03cf1f 1113 (else info)))
93f63467
AW
1114 ((<let> gensyms vals)
1115 (fold extend info gensyms vals))
1116 ((<letrec> gensyms vals)
1117 (fold extend info gensyms vals))
1118 ((<fix> gensyms vals)
1119 (fold extend info gensyms vals))
ae03cf1f
LC
1120
1121 ((<application> proc args src)
1122 (record-case proc
1123 ((<lambda> body)
1124 (validate-arity proc x #t)
1125 info)
1126 ((<toplevel-ref> name)
df685ee4 1127 (make-arity-info (vhash-consq name x toplevel-calls)
ae03cf1f
LC
1128 lexical-lambdas
1129 toplevel-lambdas))
1130 ((<lexical-ref> gensym)
df685ee4 1131 (let ((proc (vhash-assq gensym lexical-lambdas)))
ae03cf1f
LC
1132 (if (pair? proc)
1133 (record-case (cdr proc)
1134 ((<toplevel-ref> name)
1135 ;; alias to toplevel
df685ee4 1136 (make-arity-info (vhash-consq name x toplevel-calls)
ae03cf1f
LC
1137 lexical-lambdas
1138 toplevel-lambdas))
1139 (else
1140 (validate-arity (cdr proc) x #t)
1141 info))
1142
1143 ;; If GENSYM wasn't found, it may be because it's an
1144 ;; argument of the procedure being compiled.
1145 info)))
1146 (else info)))
1147 (else info))))
1148
795ab688 1149 (lambda (x info env locs)
ae03cf1f
LC
1150 ;; Up from X.
1151 (define (shrink name val info)
1152 ;; Remove NAME from the lexical-lambdas of INFO.
1153 (let ((toplevel-calls (toplevel-procedure-calls info))
1154 (lexical-lambdas (lexical-lambdas info))
1155 (toplevel-lambdas (toplevel-lambdas info)))
1156 (make-arity-info toplevel-calls
df685ee4
LC
1157 (if (vhash-assq name lexical-lambdas)
1158 (vlist-tail lexical-lambdas)
1159 lexical-lambdas)
ae03cf1f
LC
1160 toplevel-lambdas)))
1161
1162 (let ((toplevel-calls (toplevel-procedure-calls info))
1163 (lexical-lambdas (lexical-lambdas info))
1164 (toplevel-lambdas (toplevel-lambdas info)))
1165 (record-case x
93f63467
AW
1166 ((<let> gensyms vals)
1167 (fold shrink info gensyms vals))
1168 ((<letrec> gensyms vals)
1169 (fold shrink info gensyms vals))
1170 ((<fix> gensyms vals)
1171 (fold shrink info gensyms vals))
ae03cf1f
LC
1172
1173 (else info))))
1174
1175 (lambda (result env)
1176 ;; Post-processing: check all top-level procedure calls that have been
1177 ;; encountered.
1178 (let ((toplevel-calls (toplevel-procedure-calls result))
1179 (toplevel-lambdas (toplevel-lambdas result)))
df685ee4
LC
1180 (vlist-for-each
1181 (lambda (name+application)
1182 (let* ((name (car name+application))
1183 (application (cdr name+application))
1184 (proc
1185 (or (and=> (vhash-assq name toplevel-lambdas) cdr)
1186 (and (module? env)
1187 (false-if-exception
1188 (module-ref env name)))))
1189 (proc*
1190 ;; handle toplevel aliases
1191 (if (toplevel-ref? proc)
1192 (let ((name (toplevel-ref-name proc)))
1193 (and (module? env)
1194 (false-if-exception
1195 (module-ref env name))))
1196 proc)))
1197 (if (or (lambda? proc*) (procedure? proc*))
1198 (validate-arity proc* application (lambda? proc*)))))
1199 toplevel-calls)))
1200
1201 (make-arity-info vlist-null vlist-null vlist-null)))
75365375
LC
1202
1203\f
1204;;;
1205;;; `format' argument analysis.
1206;;;
1207
8e6c15a6
LC
1208(define &syntax-error
1209 ;; The `throw' key for syntax errors.
1210 (gensym "format-string-syntax-error"))
1211
75365375 1212(define (format-string-argument-count fmt)
e0697241
LC
1213 ;; Return the minimum and maxium number of arguments that should
1214 ;; follow format string FMT (or, ahem, a good estimate thereof) or
1215 ;; `any' if the format string can be followed by any number of
1216 ;; arguments.
1217
1218 (define (drop-group chars end)
1219 ;; Drop characters from CHARS until "~END" is encountered.
1220 (let loop ((chars chars)
1221 (tilde? #f))
1222 (if (null? chars)
8e6c15a6 1223 (throw &syntax-error 'unterminated-iteration)
e0697241
LC
1224 (if tilde?
1225 (if (eq? (car chars) end)
1226 (cdr chars)
1227 (loop (cdr chars) #f))
1228 (if (eq? (car chars) #\~)
1229 (loop (cdr chars) #t)
1230 (loop (cdr chars) #f))))))
1231
1232 (define (digit? char)
1233 ;; Return true if CHAR is a digit, #f otherwise.
1234 (memq char '(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)))
1235
1236 (define (previous-number chars)
1237 ;; Return the previous series of digits found in CHARS.
1238 (let ((numbers (take-while digit? chars)))
1239 (and (not (null? numbers))
1240 (string->number (list->string (reverse numbers))))))
1241
1242 (let loop ((chars (string->list fmt))
1243 (state 'literal)
1244 (params '())
1245 (conditions '())
1246 (end-group #f)
1247 (min-count 0)
1248 (max-count 0))
75365375 1249 (if (null? chars)
e0697241 1250 (if end-group
8e6c15a6 1251 (throw &syntax-error 'unterminated-conditional)
e0697241
LC
1252 (values min-count max-count))
1253 (case state
1254 ((tilde)
1255 (case (car chars)
1256 ((#\~ #\% #\& #\t #\_ #\newline #\( #\))
1257 (loop (cdr chars) 'literal '()
1258 conditions end-group
1259 min-count max-count))
1260 ((#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\, #\: #\@)
1261 (loop (cdr chars)
1262 'tilde (cons (car chars) params)
1263 conditions end-group
1264 min-count max-count))
1265 ((#\v #\V) (loop (cdr chars)
1266 'tilde (cons (car chars) params)
1267 conditions end-group
1268 (+ 1 min-count)
1269 (+ 1 max-count)))
1270 ((#\[)
1271 (loop chars 'literal '() '()
1272 (let ((selector (previous-number params))
1273 (at? (memq #\@ params)))
1274 (lambda (chars conds)
1275 ;; end of group
1276 (let ((mins (map car conds))
1277 (maxs (map cdr conds))
1278 (sel? (and selector
1279 (< selector (length conds)))))
1280 (if (and (every number? mins)
1281 (every number? maxs))
1282 (loop chars 'literal '() conditions end-group
1283 (+ min-count
1284 (if sel?
1285 (car (list-ref conds selector))
1286 (+ (if at? 0 1)
1287 (if (null? mins)
1288 0
1289 (apply min mins)))))
1290 (+ max-count
1291 (if sel?
1292 (cdr (list-ref conds selector))
1293 (+ (if at? 0 1)
1294 (if (null? maxs)
1295 0
1296 (apply max maxs))))))
8e6c15a6 1297 (values 'any 'any))))) ;; XXX: approximation
e0697241
LC
1298 0 0))
1299 ((#\;)
8e6c15a6
LC
1300 (if end-group
1301 (loop (cdr chars) 'literal '()
1302 (cons (cons min-count max-count) conditions)
1303 end-group
1304 0 0)
1305 (throw &syntax-error 'unexpected-semicolon)))
e0697241
LC
1306 ((#\])
1307 (if end-group
1308 (end-group (cdr chars)
1309 (reverse (cons (cons min-count max-count)
1310 conditions)))
8e6c15a6 1311 (throw &syntax-error 'unexpected-conditional-termination)))
e0697241
LC
1312 ((#\{) (if (memq #\@ params)
1313 (values min-count 'any)
1314 (loop (drop-group (cdr chars) #\})
1315 'literal '()
1316 conditions end-group
1317 (+ 1 min-count) (+ 1 max-count))))
1318 ((#\*) (if (memq #\@ params)
1319 (values 'any 'any) ;; it's unclear what to do here
1320 (loop (cdr chars)
1321 'literal '()
1322 conditions end-group
1323 (+ (or (previous-number params) 1)
1324 min-count)
1325 (+ (or (previous-number params) 1)
1326 max-count))))
1327 ((#\? #\k)
1328 ;; We don't have enough info to determine the exact number
1329 ;; of args, but we could determine a lower bound (TODO).
1330 (values 'any 'any))
b4af80a4
LC
1331 ((#\h #\H)
1332 (let ((argc (if (memq #\: params) 2 1)))
1333 (loop (cdr chars) 'literal '()
1334 conditions end-group
1335 (+ argc min-count)
1336 (+ argc max-count))))
e0697241
LC
1337 (else (loop (cdr chars) 'literal '()
1338 conditions end-group
1339 (+ 1 min-count) (+ 1 max-count)))))
1340 ((literal)
1341 (case (car chars)
1342 ((#\~) (loop (cdr chars) 'tilde '()
1343 conditions end-group
1344 min-count max-count))
1345 (else (loop (cdr chars) 'literal '()
1346 conditions end-group
1347 min-count max-count))))
1348 (else (error "computer bought the farm" state))))))
75365375 1349
afc98031
LC
1350(define (gettext? proc env)
1351 "Return #t when PROC designates the `gettext' procedure in ENV."
1352 (match proc
1353 (($ <toplevel-ref> _ name)
1354 (let ((var (false-if-exception (module-variable env name))))
1355 (if var
1356 (eq? (variable-ref var) gettext)
1357 (eq? name '_)))) ; special hack to support local aliases
1358 (($ <module-ref> _ module name public?)
1359 (let ((m (false-if-exception (if public?
1360 (resolve-interface module)
1361 (resolve-module module)))))
1362 (and m
1363 (eq? (false-if-exception (module-ref module name))
1364 gettext))))
1365 (_ #f)))
1366
1367(define (const-fmt x env)
16a3b316
LC
1368 ;; Return the literal format pattern for X, or #f.
1369 (match x
1370 (($ <const> _ exp)
3936cebc 1371 exp)
afc98031
LC
1372 (($ <application> _ (? (cut gettext? <> env))
1373 (($ <const> _ (? string? fmt))))
3936cebc 1374 ;; Gettexted literals, like `(_ "foo")'.
16a3b316
LC
1375 fmt)
1376 (_ #f)))
56e31389 1377
75365375
LC
1378(define format-analysis
1379 ;; Report arity mismatches in the given tree.
1380 (make-tree-analysis
1381 (lambda (x _ env locs)
1382 ;; X is a leaf.
1383 #t)
1384
1385 (lambda (x _ env locs)
1386 ;; Down into X.
1387 (define (check-format-args args loc)
1388 (pmatch args
1389 ((,port ,fmt . ,rest)
afc98031 1390 (guard (const-fmt fmt env))
60f01304
LC
1391 (if (and (const? port)
1392 (not (boolean? (const-exp port))))
1393 (warning 'format loc 'wrong-port (const-exp port)))
afc98031 1394 (let ((fmt (const-fmt fmt env))
e0697241 1395 (count (length rest)))
60f01304 1396 (if (string? fmt)
8e6c15a6
LC
1397 (catch &syntax-error
1398 (lambda ()
1399 (let-values (((min max)
1400 (format-string-argument-count fmt)))
1401 (and min max
1402 (or (and (or (eq? min 'any) (>= count min))
1403 (or (eq? max 'any) (<= count max)))
1404 (warning 'format loc 'wrong-format-arg-count
1405 fmt min max count)))))
1406 (lambda (_ key)
1407 (warning 'format loc 'syntax-error key fmt)))
60f01304
LC
1408 (warning 'format loc 'wrong-format-string fmt))))
1409 ((,port ,fmt . ,rest)
56e31389
AW
1410 (if (and (const? port)
1411 (not (boolean? (const-exp port))))
3a822fff 1412 (warning 'format loc 'wrong-port (const-exp port)))
5414d333
AW
1413 ;; Warn on non-literal format strings, unless they refer to a
1414 ;; lexical variable named "fmt".
1415 (if (record-case fmt
1416 ((<lexical-ref> name)
1417 (not (eq? name 'fmt)))
1418 (else #t))
1419 (warning 'format loc 'non-literal-format-string)))
60f01304
LC
1420 (else
1421 (warning 'format loc 'wrong-num-args (length args)))))
75365375 1422
60273407
LC
1423 (define (check-simple-format-args args loc)
1424 ;; Check the arguments to the `simple-format' procedure, which is
1425 ;; less capable than that of (ice-9 format).
1426
1427 (define allowed-chars
1428 '(#\A #\S #\a #\s #\~ #\%))
1429
1430 (define (format-chars fmt)
1431 (let loop ((chars (string->list fmt))
1432 (result '()))
1433 (match chars
1434 (()
1435 (reverse result))
1436 ((#\~ opt rest ...)
1437 (loop rest (cons opt result)))
1438 ((_ rest ...)
1439 (loop rest result)))))
1440
1441 (match args
1442 ((port ($ <const> _ (? string? fmt)) _ ...)
1443 (let ((opts (format-chars fmt)))
1444 (or (every (cut memq <> allowed-chars) opts)
1445 (begin
1446 (warning 'format loc 'simple-format fmt
1447 (find (negate (cut memq <> allowed-chars)) opts))
1448 #f))))
afc98031
LC
1449 ((port ($ <application> _ (? (cut gettext? <> env)) (fmt))
1450 args ...)
60273407
LC
1451 (check-simple-format-args `(,port ,fmt ,args) loc))
1452 (_ #t)))
1453
75365375
LC
1454 (define (resolve-toplevel name)
1455 (and (module? env)
1456 (false-if-exception (module-ref env name))))
1457
16a3b316
LC
1458 (match x
1459 (($ <application> src ($ <toplevel-ref> _ name) args)
1460 (let ((proc (resolve-toplevel name)))
60273407
LC
1461 (if (or (and (eq? proc (@ (guile) simple-format))
1462 (check-simple-format-args args
1463 (or src (find pair? locs))))
1464 (eq? proc (@ (ice-9 format) format)))
1465 (check-format-args args (or src (find pair? locs))))))
1466 (($ <application> src ($ <module-ref> _ '(ice-9 format) 'format) args)
1467 (check-format-args args (or src (find pair? locs))))
1468 (($ <application> src ($ <module-ref> _ '(guile)
1469 (or 'format 'simple-format))
1470 args)
1471 (and (check-simple-format-args args
1472 (or src (find pair? locs)))
1473 (check-format-args args (or src (find pair? locs)))))
16a3b316 1474 (_ #t))
75365375
LC
1475 #t)
1476
1477 (lambda (x _ env locs)
1478 ;; Up from X.
1479 #t)
1480
1481 (lambda (_ env)
1482 ;; Post-processing.
1483 #t)
1484
1485 #t))