fd02f009e5767d6e018861f67f6592f7c43725cc
[sod] / doc / list-exports.lisp
1 (defun symbolicate (&rest things)
2 (intern (apply #'concatenate 'string (mapcar #'string things))))
3
4 (defun incomprehensible-form (head tail)
5 (format *error-output* ";; incomprehensible: ~S~%" (cons head tail)))
6
7 (defgeneric form-list-exports (head tail)
8 (:method (head tail)
9 (declare (ignore head tail))
10 nil))
11
12 (defmethod form-list-exports ((head (eql 'export)) tail)
13 (let ((symbols (car tail)))
14 (if (and (consp symbols)
15 (eq (car symbols) 'quote))
16 (let ((thing (cadr symbols)))
17 (if (atom thing) (list thing) thing))
18 (incomprehensible-form head tail))))
19
20 (defmethod form-list-exports ((head (eql 'definst)) tail)
21 (destructuring-bind (code (streamvar &key export) args &body body) tail
22 (declare (ignore streamvar body))
23 (and export
24 (list* (symbolicate code '-inst)
25 (symbolicate 'make- code '-inst)
26 (mapcar (lambda (arg)
27 (symbolicate 'inst- arg))
28 args)))))
29
30 (defmethod form-list-exports ((head (eql 'define-tagged-type)) tail)
31 (destructuring-bind (kind what) tail
32 (declare (ignore what))
33 (list kind
34 (symbolicate 'c- kind '-type)
35 (symbolicate 'make- kind '-type))))
36
37 (defmethod form-list-exports ((head (eql 'macrolet)) tail)
38 (mapcan #'form-exports (cdr tail)))
39
40 (defmethod form-list-exports ((head (eql 'eval-when)) tail)
41 (mapcan #'form-exports (cdr tail)))
42
43 (defmethod form-list-exports ((head (eql 'progn)) tail)
44 (mapcan #'form-exports tail))
45
46 (defgeneric form-exports (form)
47 (:method (form) nil)
48 (:method ((form cons)) (form-list-exports (car form) (cdr form))))
49
50 (defgeneric list-exports (thing))
51
52 (defmethod list-exports ((stream stream))
53 (loop with eof = '#:eof
54 for form = (read stream nil eof)
55 until (eq form eof)
56 when (consp form) nconc (form-exports form)))
57
58 (defmethod list-exports ((path pathname))
59 (mapcar (lambda (each)
60 (cons each (with-open-file (stream each) (list-exports stream))))
61 (directory (merge-pathnames path #p"*.lisp"))))
62
63 (defmethod list-exports ((path string))
64 (list-exports (pathname path)))
65
66 (defun list-exported-symbols (package)
67 (sort (loop for s being the external-symbols of package collect s)
68 #'string< :key #'symbol-name))
69
70 (defun find-symbol-homes (paths package)
71 (let* ((symbols (list-exported-symbols package))
72 (exports-alist (mapcan #'list-exports paths))
73 (homes (make-hash-table :test #'equal)))
74 (dolist (assoc exports-alist)
75 (let ((home (car assoc)))
76 (dolist (symbol (cdr assoc))
77 (let ((name (symbol-name symbol)))
78 (unless (nth-value 1 (find-symbol name package))
79 (format *error-output* ";; unexported: ~S~%" symbol))
80 (setf (gethash name homes) home)))))
81 (dolist (symbol symbols)
82 (unless (gethash (symbol-name symbol) homes)
83 (format *error-output* ";; mysterious: ~S~%" symbol)))
84 exports-alist))
85
86 (defun boring-setf-expansion-p (symbol)
87 (multiple-value-bind (temps args stores store fetch)
88 (ignore-errors (get-setf-expansion (list symbol)))
89 (declare (ignore temps args stores fetch))
90 (and (consp store)
91 (eq (car store) 'funcall)
92 (consp (cdr store)) (consp (cadr store))
93 (eq (caadr store) 'function)
94 (let ((func (cadadr store)))
95 (and (consp func) (consp (cdr func))
96 (eq (car func) 'setf))))))
97
98 (defun specialized-on-p (func arg what)
99 (some (lambda (method)
100 (let ((spec (nth arg (sb-mop:method-specializers method))))
101 (and (typep spec 'sb-mop:eql-specializer)
102 (eql (sb-mop:eql-specializer-object spec) what))))
103 (sb-mop:generic-function-methods func)))
104
105 (defun categorize (symbol)
106 (let ((things nil))
107 (when (boundp symbol)
108 (push (if (constantp symbol) :constant :variable) things))
109 (when (fboundp symbol)
110 (push (cond ((macro-function symbol) :macro)
111 ((typep (fdefinition symbol) 'generic-function)
112 :generic)
113 (t :function))
114 things)
115 (when (or ;;(not (boring-setf-expansion-p symbol))
116 (ignore-errors (fdefinition (list 'setf symbol))))
117 (push :setf things)))
118 (when (find-class symbol nil)
119 (push :class things))
120 (when (or (specialized-on-p #'sod:expand-c-type-spec 0 symbol)
121 (specialized-on-p #'sod:expand-c-type-form 0 symbol))
122 (push :c-type things))
123 (when (or (specialized-on-p #'sod-parser:expand-parser-spec 1 symbol)
124 (specialized-on-p #'sod-parser:expand-parser-form 1 symbol))
125 (push :parser things))
126 (nreverse things)))
127
128 (defun categorize-symbols (paths package)
129 (mapcar (lambda (assoc)
130 (let ((home (car assoc))
131 (symbols (sort (mapcan (lambda (sym)
132 (multiple-value-bind
133 (symbol foundp)
134 (find-symbol (symbol-name sym)
135 package)
136 (and foundp (list symbol))))
137 (cdr assoc))
138 #'string< :key #'symbol-name)))
139 (cons home (mapcar (lambda (symbol)
140 (cons symbol (categorize symbol)))
141 symbols))))
142
143 (find-symbol-homes paths package)))
144
145 (defun best-package-name (package)
146 (car (sort (cons (package-name package)
147 (copy-list (package-nicknames package)))
148 #'< :key #'length)))
149
150 (defvar charbuf-size 0)
151
152 (defun pretty-symbol-name (symbol package)
153 (let* ((pkg (symbol-package symbol))
154 (exportp (member symbol (list-exported-symbols pkg))))
155 (format nil "~(~:[~A:~:[:~;~]~;~2*~]~A~)"
156 (and exportp (eq pkg package)) (best-package-name pkg)
157 exportp (symbol-name symbol))))
158
159 (defun analyse-classes (package)
160 (setf package (find-package package))
161 (let ((classes (mapcan (lambda (symbol)
162 (let ((class (find-class symbol nil)))
163 (and class
164 (typep class '(or standard-class
165 structure-class))
166 (list class))))
167 (list-exported-symbols package)))
168 (subs (make-hash-table)))
169 (let ((done (make-hash-table)))
170 (labels ((walk-up (class)
171 (unless (gethash class done)
172 (dolist (super (sb-mop:class-direct-superclasses class))
173 (push class (gethash super subs))
174 (walk-up super))
175 (setf (gethash class done) t))))
176 (dolist (class classes)
177 (walk-up class))))
178 (labels ((walk-down (this super depth)
179 (format t "~v,0T~A~@[ [~{~A~^ ~}]~]~%"
180 (* 2 depth)
181 (pretty-symbol-name (class-name this) package)
182 (mapcar (lambda (class)
183 (pretty-symbol-name (class-name class)
184 package))
185 (remove super
186 (sb-mop:class-direct-superclasses this))))
187 (dolist (sub (sort (copy-list (gethash this subs))
188 #'string< :key #'class-name))
189 (walk-down sub this (1+ depth)))))
190 (walk-down (find-class t) nil 0))))
191
192 (defun report-symbols (paths package)
193 (setf package (find-package package))
194 (format t "~A~%Package `~(~A~)'~2%"
195 (make-string 77 :initial-element #\-)
196 (package-name package))
197 (dolist (assoc (categorize-symbols paths package))
198 (when (cdr assoc)
199 (format t "~A~%" (file-namestring (car assoc)))
200 (dolist (def (cdr assoc))
201 (let ((sym (car def)))
202 (format t " ~A~@[~48T~{~(~A~)~^ ~}~]~%"
203 (pretty-symbol-name sym package)
204 (cdr def))))
205 (terpri)))
206 (analyse-classes package)
207 (terpri))
208
209 (defun report-project-symbols ()
210 (labels ((components (comp)
211 (slot-value comp 'asdf::components))
212 (files (comp)
213 (sort (remove-if-not (lambda (comp)
214 (typep comp 'asdf:cl-source-file))
215 (components comp))
216 #'string< :key #'asdf:component-name))
217 (by-name (comp name)
218 (find name (components comp)
219 :test #'string= :key #'asdf:component-name))
220 (file-name (file)
221 (slot-value file 'asdf::absolute-pathname)))
222 (let* ((sod (asdf:find-system "sod"))
223 (parser-files (files (by-name sod "parser")))
224 (utilities (by-name sod "utilities"))
225 (sod-files (remove utilities (files sod))))
226 (report-symbols (mapcar #'file-name sod-files) "SOD")
227 (report-symbols (mapcar #'file-name parser-files) "SOD-PARSER")
228 (report-symbols (mapcar #'file-name (list utilities)) "SOD-UTILITIES"))))