doc/list-exports.lisp (best-package-name: Don't sort the whole list.
[sod] / doc / list-exports.lisp
1 #! /bin/sh
2 ":"; ### -*-lisp-*-
3 ":"; CL_SOURCE_REGISTRY=$(pwd)/build/src/:; export CL_SOURCE_REGISTRY
4 ":"; exec cl-launch -X -l "sbcl cmucl" -s asdf -i "(sod-exports::main)" -- "$0" "$@" || exit 1
5
6 (cl:defpackage #:sod-exports
7 (:use #:common-lisp
8 #+cmu #:mop
9 #+sbcl #:sb-mop))
10
11 (cl:in-package #:sod-exports)
12 (eval-when (:compile-toplevel :load-toplevel :execute)
13 (mapc #'asdf:load-system '(:sod :sod-frontend)))
14
15 (defun symbolicate (&rest things)
16 (intern (apply #'concatenate 'string (mapcar #'string things))))
17
18 (defun incomprehensible-form (head tail)
19 (format *error-output* ";; incomprehensible: ~S~%" (cons head tail)))
20
21 (defgeneric form-list-exports (head tail)
22 (:method (head tail)
23 (declare (ignore head tail))
24 nil))
25
26 (defmethod form-list-exports ((head (eql 'cl:export)) tail)
27 (let ((symbols (car tail)))
28 (if (and (consp symbols)
29 (eq (car symbols) 'quote))
30 (let ((thing (cadr symbols)))
31 (if (atom thing) (list thing) thing))
32 (incomprehensible-form head tail))))
33
34 (defmethod form-list-exports ((head (eql 'sod:definst)) tail)
35 (destructuring-bind (code (streamvar &key export) args &body body) tail
36 (declare (ignore streamvar body))
37 (and export
38 (list* (symbolicate code '-inst)
39 (symbolicate 'make- code '-inst)
40 (labels ((dig (tree path)
41 (if (or (atom tree) (null path)) tree
42 (dig (nth (car path) tree) (cdr path))))
43 (cook (arg)
44 (if (consp arg) (car arg)
45 (let ((name (symbol-name arg)))
46 (if (char= (char name 0) #\%)
47 (intern (subseq name 1))
48 arg))))
49 (instify (arg)
50 (symbolicate 'inst- (cook arg))))
51 (loop with state = :mandatory
52 for arg in args
53 if (and (symbolp arg)
54 (char= (char (symbol-name arg) 0) #\&))
55 do (setf state arg)
56 else if (member state '(:mandatory &rest))
57 collect (instify arg)
58 else if (member state '(&optional &aux))
59 collect (instify (dig arg '(0)))
60 else if (eq state '&key)
61 collect (instify (dig arg '(0 1)))
62 else
63 do (error "Confused by ~S." arg)))))))
64
65 (defmethod form-list-exports ((head (eql 'sod::define-tagged-type)) tail)
66 (destructuring-bind (kind what) tail
67 (declare (ignore what))
68 (list kind
69 (symbolicate 'c- kind '-type)
70 (symbolicate 'make- kind '-type))))
71
72 (defmethod form-list-exports ((head (eql 'sod:defctype)) tail)
73 (destructuring-bind (names value &key export) tail
74 (declare (ignore value))
75 (let ((names (if (listp names) names (list names))))
76 (and export
77 (list* (symbolicate 'c-type- (car names)) names)))))
78
79 (defmethod form-list-exports ((head (eql 'sod:define-simple-c-type)) tail)
80 (destructuring-bind (names type &key export) tail
81 (declare (ignore type))
82 (let ((names (if (listp names) names (list names))))
83 (and export
84 (list* (symbolicate 'c-type- (car names)) names)))))
85
86 (defmethod form-list-exports ((head (eql 'cl:macrolet)) tail)
87 (mapcan #'form-exports (cdr tail)))
88
89 (defmethod form-list-exports ((head (eql 'cl:eval-when)) tail)
90 (mapcan #'form-exports (cdr tail)))
91
92 (defmethod form-list-exports ((head (eql 'cl:progn)) tail)
93 (mapcan #'form-exports tail))
94
95 (defgeneric form-exports (form)
96 (:method (form) nil)
97 (:method ((form cons)) (form-list-exports (car form) (cdr form))))
98
99 (defgeneric list-exports (thing))
100
101 (defmethod list-exports ((stream stream))
102 (loop with eof = '#:eof
103 for form = (read stream nil eof)
104 until (eq form eof)
105 when (consp form) nconc (form-exports form)))
106
107 (defmethod list-exports ((path pathname))
108 (mapcar (lambda (each)
109 (cons each (with-open-file (stream each) (list-exports stream))))
110 (directory (merge-pathnames path #p"*.lisp"))))
111
112 (defmethod list-exports ((path string))
113 (list-exports (pathname path)))
114
115 (defun list-exported-symbols (package)
116 (sort (loop for s being the external-symbols of package collect s)
117 #'string< :key #'symbol-name))
118
119 (defun find-symbol-homes (paths package)
120 (let* ((symbols (list-exported-symbols package))
121 (exports-alist (let ((*package* package))
122 (mapcan #'list-exports paths)))
123 (homes (make-hash-table :test #'equal)))
124 (dolist (assoc exports-alist)
125 (let ((home (car assoc)))
126 (dolist (symbol (cdr assoc))
127 (let ((name (symbol-name symbol)))
128 (unless (nth-value 1 (find-symbol name package))
129 (format *error-output* ";; unexported: ~S~%" symbol))
130 (setf (gethash name homes) home)))))
131 (dolist (symbol symbols)
132 (unless (gethash (symbol-name symbol) homes)
133 (format *error-output* ";; mysterious: ~S~%" symbol)))
134 exports-alist))
135
136 (defun boring-setf-expansion-p (symbol)
137 (multiple-value-bind (temps args stores store fetch)
138 (ignore-errors (get-setf-expansion (list symbol)))
139 (declare (ignore temps args stores fetch))
140 (and (consp store)
141 (eq (car store) 'funcall)
142 (consp (cdr store)) (consp (cadr store))
143 (eq (caadr store) 'function)
144 (let ((func (cadadr store)))
145 (and (consp func) (consp (cdr func))
146 (eq (car func) 'setf))))))
147
148 (defun specialized-on-p (func arg what)
149 (some (lambda (method)
150 (let ((spec (nth arg (method-specializers method))))
151 (and (typep spec 'eql-specializer)
152 (eql (eql-specializer-object spec) what))))
153 (generic-function-methods func)))
154
155 (defun categorize (symbol)
156 (let ((things nil))
157 (when (boundp symbol)
158 (push (if (constantp symbol) :constant :variable) things))
159 (when (fboundp symbol)
160 (push (cond ((macro-function symbol) :macro)
161 ((typep (fdefinition symbol) 'generic-function)
162 :generic)
163 (t :function))
164 things)
165 (etypecase (ignore-errors (fdefinition (list 'setf symbol)))
166 (generic-function (push :setf-generic things))
167 (function (push :setf-function things))
168 (null)))
169 (when (find-class symbol nil)
170 (push :class things))
171 (when (or (specialized-on-p #'sod:expand-c-type-spec 0 symbol)
172 (specialized-on-p #'sod:expand-c-type-form 0 symbol))
173 (push :c-type things))
174 (when (or (specialized-on-p #'sod-parser:expand-parser-spec 1 symbol)
175 (specialized-on-p #'sod-parser:expand-parser-form 1 symbol))
176 (push :parser things))
177 (when (get symbol 'optparse::opthandler)
178 (push :opthandler things))
179 (when (get symbol 'optparse::optmacro)
180 (push :optmacro things))
181 (nreverse things)))
182
183 (defun categorize-symbols (paths package)
184 (mapcar (lambda (assoc)
185 (let ((home (car assoc))
186 (symbols (delete-duplicates
187 (sort (mapcan (lambda (sym)
188 (multiple-value-bind
189 (symbol foundp)
190 (find-symbol
191 (symbol-name sym)
192 package)
193 (and foundp (list symbol))))
194 (cdr assoc))
195 #'string< :key #'symbol-name))))
196 (cons home (mapcar (lambda (symbol)
197 (cons symbol (categorize symbol)))
198 symbols))))
199
200 (find-symbol-homes paths package)))
201
202 (defun best-package-name (package)
203
204 ;; We pick the shortest one. Strangely, there's no `find minimal thing
205 ;; according to this valuation' function in Common Lisp.
206 (loop with best = (package-name package)
207 with best-length = (length best)
208 for name in (package-nicknames package)
209 for name-length = (length name)
210 when (< name-length best-length)
211 do (setf best name
212 best-length name-length)
213 finally (return best)))
214
215 (defvar charbuf-size 0)
216
217 (defun exported-symbol-p (symbol &optional (package (symbol-package symbol)))
218 (and package
219 (multiple-value-bind (sym how)
220 (find-symbol (symbol-name symbol) package)
221 (and (eq sym symbol)
222 (eq how :external)))))
223
224 (defun pretty-symbol-name (symbol package)
225 (let ((pkg (symbol-package symbol))
226 (exportp (exported-symbol-p symbol)))
227 (format nil "~(~:[~A:~:[:~;~]~;~2*~]~A~)"
228 (and exportp (eq pkg package))
229 (cond ((keywordp symbol) "")
230 ((eq pkg nil) "#")
231 (t (best-package-name pkg)))
232 (or exportp (null pkg)) (symbol-name symbol))))
233
234 (deftype interesting-class ()
235 '(or standard-class
236 structure-class
237 #.(class-name (class-of (find-class 'condition)))))
238
239 (defun analyse-classes (package)
240 (setf package (find-package package))
241 (let ((classes (mapcan (lambda (symbol)
242 (let ((class (find-class symbol nil)))
243 (and class
244 (typep class 'interesting-class)
245 (list class))))
246 (list-exported-symbols package)))
247 (subs (make-hash-table)))
248 (let ((done (make-hash-table)))
249 (labels ((walk-up (class)
250 (unless (gethash class done)
251 (dolist (super (class-direct-superclasses class))
252 (push class (gethash super subs))
253 (walk-up super))
254 (setf (gethash class done) t))))
255 (dolist (class classes)
256 (walk-up class))))
257 (labels ((walk-down (this super depth)
258 (format t "~v,0T~A~@[ [~{~A~^ ~}]~]~%"
259 (* 2 depth)
260 (pretty-symbol-name (class-name this) package)
261 (mapcar (lambda (class)
262 (pretty-symbol-name (class-name class)
263 package))
264 (remove super
265 (class-direct-superclasses this))))
266 (dolist (sub (sort (copy-list (gethash this subs))
267 #'string< :key #'class-name))
268 (walk-down sub this (1+ depth)))))
269 (walk-down (find-class t) nil 0))))
270
271 (defmacro deep-compare ((left right) &body body)
272 (let ((block (gensym "BLOCK-")) (func (gensym "FUNC-"))
273 (l (gensym "LEFT-")) (r (gensym "RIGHT-")))
274 `(macrolet ((focus (expr &body body)
275 `(flet ((,',func (it) ,expr))
276 (let ((,',l (,',func ,',l))
277 (,',r (,',func ,',r)))
278 ,@body)))
279 (update (expr)
280 `(flet ((,',func (it) ,expr))
281 (psetf ,',l (,',func ,',l)
282 ,',r (,',func ,',r))))
283 (compare (expr)
284 `(cond ((let ((left ,',l) (right ,',r)) ,expr)
285 (return-from ,',block t))
286 ((let ((right ,',l) (left ,',r)) ,expr)
287 (return-from ,',block nil))))
288 (typesw (&rest clauses)
289 (labels ((iter (clauses)
290 (if (null clauses)
291 'nil
292 (destructuring-bind (type &rest body)
293 (car clauses)
294 (if (eq type t)
295 `(progn ,@body)
296 `(if (typep ,',l ',type)
297 (if (typep ,',r ',type)
298 (progn ,@body)
299 (return-from ,',block t))
300 (if (typep ,',r ',type)
301 (return-from ,',block nil)
302 ,(iter (cdr clauses)))))))))
303 (iter clauses))))
304 (let ((,l ,left) (,r ,right))
305 (block ,block
306 ,@body)))))
307
308 (defun order-specializers (la lb)
309 (deep-compare (la lb)
310 (loop (typesw (null (return nil)))
311 (focus (car it)
312 (typesw (eql-specializer
313 (focus (eql-specializer-object it)
314 (typesw (keyword
315 (compare (string< left right)))
316 (symbol
317 (focus (package-name (symbol-package it))
318 (compare (string< left right)))
319 (compare (string< left right)))
320 (t
321 (focus (with-output-to-string (out)
322 (prin1 it out)
323 (write-char #\nul))
324 (compare (string< left right)))))))
325 (class
326 (focus (class-name it)
327 (focus (package-name (symbol-package it))
328 (compare (string< left right)))
329 (compare (string< left right))))
330 (t
331 (error "unexpected things"))))
332 (update (cdr it)))))
333
334 (defun analyse-generic-functions (package)
335 (setf package (find-package package))
336 (flet ((function-name-core (name)
337 (typecase name
338 (symbol (values name t))
339 ((cons (eql setf) t) (values (cadr name) t))
340 (t (values nil nil)))))
341 (let ((methods (make-hash-table))
342 (functions (make-hash-table))
343 (externs (make-hash-table)))
344 (dolist (symbol (list-exported-symbols package))
345 (setf (gethash symbol externs) t))
346 (dolist (symbol (list-exported-symbols package))
347 (flet ((dofunc (func)
348 (when (typep func 'generic-function)
349 (setf (gethash func functions) t)
350 (dolist (method (generic-function-methods func))
351 (setf (gethash method methods) t)))))
352 (dofunc (and (fboundp symbol) (fdefinition symbol)))
353 (dofunc (ignore-errors (fdefinition (list 'setf symbol)))))
354 (when (eq (symbol-package symbol) package)
355 (let ((class (find-class symbol nil)))
356 (when class
357 (dolist
358 (func (specializer-direct-generic-functions class))
359 (multiple-value-bind (name knownp)
360 (function-name-core (generic-function-name func))
361 (when (and knownp
362 (or (not (eq (symbol-package name) package))
363 (gethash name externs)))
364 (setf (gethash func functions) t)
365 (dolist (method (specializer-direct-methods class))
366 (setf (gethash method methods) t)))))))))
367 (let ((funclist nil))
368 (maphash (lambda (func value)
369 (declare (ignore value))
370 (push func funclist))
371 functions)
372 (setf funclist (sort funclist
373 (lambda (a b)
374 (let ((core-a (function-name-core a))
375 (core-b (function-name-core b)))
376 (if (eq core-a core-b)
377 (and (atom a) (consp b))
378 (string< core-a core-b))))
379 :key #'generic-function-name))
380 (dolist (function funclist)
381 (let ((name (generic-function-name function)))
382 (etypecase name
383 (symbol
384 (format t "~A~%" (pretty-symbol-name name package)))
385 ((cons (eql setf) t)
386 (format t "(setf ~A)~%"
387 (pretty-symbol-name (cadr name) package)))))
388 (dolist (method (sort (copy-list
389 (generic-function-methods function))
390 #'order-specializers
391 :key #'method-specializers))
392 (when (gethash method methods)
393 (format t "~2T~{~A~^ ~}~@[ [~{~(~S~)~^ ~}]~]~%"
394 (mapcar
395 (lambda (spec)
396 (etypecase spec
397 (class
398 (let ((name (class-name spec)))
399 (if (eq name t) "t"
400 (pretty-symbol-name name package))))
401 (eql-specializer
402 (let ((obj (eql-specializer-object spec)))
403 (format nil "(eql ~A)"
404 (if (symbolp obj)
405 (pretty-symbol-name obj package)
406 obj))))))
407 (method-specializers method))
408 (method-qualifiers method)))))))))
409
410 (defun check-slot-names (package)
411 (setf package (find-package package))
412 (let* ((symbols (list-exported-symbols package))
413 (classes (mapcan (lambda (symbol)
414 (when (eq (symbol-package symbol) package)
415 (let ((class (find-class symbol nil)))
416 (and class (list class)))))
417 symbols))
418 (offenders (mapcan
419 (lambda (class)
420 (let* ((slot-names
421 (mapcar #'slot-definition-name
422 (class-direct-slots class)))
423 (exported (remove-if
424 (lambda (sym)
425 (or (not (symbol-package sym))
426 (and (not (exported-symbol-p
427 sym))
428 (eq (symbol-package sym)
429 package))))
430 slot-names)))
431 (and exported
432 (list (cons (class-name class)
433 exported)))))
434 classes))
435 (bad-words (remove-duplicates (mapcan (lambda (list)
436 (copy-list (cdr list)))
437 offenders))))
438 (values offenders bad-words)))
439
440 (defun report-symbols (paths package)
441 (setf package (find-package package))
442 (format t "~A~%Package `~(~A~)'~2%"
443 (make-string 77 :initial-element #\-)
444 (package-name package))
445 (dolist (assoc (sort (categorize-symbols paths package) #'string<
446 :key (lambda (assoc)
447 (file-namestring (car assoc)))))
448 (when (cdr assoc)
449 (format t "~A~%" (file-namestring (car assoc)))
450 (dolist (def (cdr assoc))
451 (let ((sym (car def)))
452 (format t " ~A~@[~48T~{~(~A~)~^ ~}~]~%"
453 (pretty-symbol-name sym package)
454 (cdr def))))
455 (terpri)))
456 (multiple-value-bind (alist names) (check-slot-names package)
457 (when names
458 (format t "Leaked slot names: ~{~A~^, ~}~%"
459 (mapcar (lambda (name) (pretty-symbol-name name package))
460 names))
461 (dolist (assoc alist)
462 (format t "~2T~A: ~{~A~^, ~}~%"
463 (pretty-symbol-name (car assoc) package)
464 (mapcar (lambda (name) (pretty-symbol-name name package))
465 (cdr assoc))))
466 (terpri)))
467 (format t "Classes:~%")
468 (analyse-classes package)
469 (terpri)
470 (format t "Methods:~%")
471 (analyse-generic-functions package)
472 (terpri))
473
474 (export 'report-project-symbols)
475 (defun report-project-symbols ()
476 (labels ((components (comp)
477 (asdf:component-children comp))
478 (files (comp)
479 (sort (remove-if-not (lambda (comp)
480 (typep comp 'asdf:cl-source-file))
481 (components comp))
482 #'string< :key #'asdf:component-name))
483 (by-name (comp name)
484 (gethash name (asdf:component-children-by-name comp)))
485 (file-name (file)
486 (slot-value file 'asdf/component:absolute-pathname)))
487 (let* ((sod (asdf:find-system "sod"))
488 (parser-files (files (by-name sod "parser")))
489 (utilities (by-name sod "utilities"))
490 (sod-frontend (asdf:find-system "sod-frontend"))
491 (optparse (by-name sod "optparse"))
492 (frontend (by-name sod-frontend "frontend"))
493 (sod-files (set-difference (files sod) (list optparse utilities))))
494 (report-symbols (mapcar #'file-name sod-files) "SOD")
495 (report-symbols (mapcar #'file-name (list frontend)) "SOD-FRONTEND")
496 (report-symbols (mapcar #'file-name parser-files) "SOD-PARSER")
497 (report-symbols (mapcar #'file-name (list optparse)) "OPTPARSE")
498 (report-symbols (mapcar #'file-name (list utilities)) "SOD-UTILITIES"))))
499
500 (defun main ()
501 (with-open-file (*standard-output* #p"doc/SYMBOLS"
502 :direction :output
503 :if-exists :supersede
504 :if-does-not-exist :create)
505 (report-project-symbols)))
506
507 #+interactive (main)