analyze.scm 61 KB

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