/[cmucl]/src/pcl/macros.lisp
ViewVC logotype

Contents of /src/pcl/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (show annotations)
Sun Apr 25 12:46:28 1999 UTC (15 years ago) by pw
Branch: MAIN
Changes since 1.12: +6 -4 lines
Revision 1.12 for FIND-CLASS broke the PCL build process. This is a
better fix (suggested by Douglas) in that FIND-CLASS-CELL as called
from FIND-CLASS is not allowed to produce any side effects. This
restores the build process and lets (find-class :x nil) => NIL
as relied on by some codes.
1 ;;;-*-Mode:LISP; Package:(PCL (LISP WALKER)); Base:10; Syntax:Common-lisp -*-
2 ;;;
3 ;;; *************************************************************************
4 ;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
5 ;;; All rights reserved.
6 ;;;
7 ;;; Use and copying of this software and preparation of derivative works
8 ;;; based upon this software are permitted. Any distribution of this
9 ;;; software or derivative works must comply with all applicable United
10 ;;; States export control laws.
11 ;;;
12 ;;; This software is made available AS IS, and Xerox Corporation makes no
13 ;;; warranty about the software, its performance or its conformity to any
14 ;;; specification.
15 ;;;
16 ;;; Any person obtaining a copy of this software is requested to send their
17 ;;; name and post office or electronic mail address to:
18 ;;; CommonLoops Coordinator
19 ;;; Xerox PARC
20 ;;; 3333 Coyote Hill Rd.
21 ;;; Palo Alto, CA 94304
22 ;;; (or send Arpanet mail to CommonLoops-Coordinator.pa@Xerox.arpa)
23 ;;;
24 ;;; Suggestions, comments and requests for improvements are also welcome.
25 ;;; *************************************************************************
26 ;;;
27 #+cmu
28 (ext:file-comment
29 "$Header: /tiger/var/lib/cvsroots/cmucl/src/pcl/macros.lisp,v 1.13 1999/04/25 12:46:28 pw Exp $")
30 ;;;
31 ;;; Macros global variable definitions, and other random support stuff used
32 ;;; by the rest of the system.
33 ;;;
34 ;;; For simplicity (not having to use eval-when a lot), this file must be
35 ;;; loaded before it can be compiled.
36 ;;;
37
38 (in-package :pcl)
39
40 (proclaim '(declaration
41 #-Genera values ;I use this so that Zwei can remind
42 ;me what values a function returns.
43
44 #-Genera arglist ;Tells me what the pretty arglist
45 ;of something (which probably takes
46 ;&rest args) is.
47
48 #-Genera indentation ;Tells ZWEI how to indent things
49 ;like defclass.
50 class
51 variable-rebinding
52 pcl-fast-call
53 method-name
54 method-lambda-list
55 ))
56
57 ;;; Age old functions which CommonLisp cleaned-up away. They probably exist
58 ;;; in other packages in all CommonLisp implementations, but I will leave it
59 ;;; to the compiler to optimize into calls to them.
60 ;;;
61 ;;; Common Lisp BUG:
62 ;;; Some Common Lisps define these in the Lisp package which causes
63 ;;; all sorts of lossage. Common Lisp should explictly specify which
64 ;;; symbols appear in the Lisp package.
65 ;;;
66 (eval-when (compile load eval)
67
68 (defmacro memq (item list) `(member ,item ,list :test #'eq))
69 (defmacro assq (item list) `(assoc ,item ,list :test #'eq))
70 (defmacro rassq (item list) `(rassoc ,item ,list :test #'eq))
71 (defmacro delq (item list) `(delete ,item ,list :test #'eq))
72 (defmacro posq (item list) `(position ,item ,list :test #'eq))
73 (defmacro neq (x y) `(not (eq ,x ,y)))
74
75
76 (defun make-caxr (n form)
77 (if (< n 4)
78 `(,(nth n '(car cadr caddr cadddr)) ,form)
79 (make-caxr (- n 4) `(cddddr ,form))))
80
81 (defun make-cdxr (n form)
82 (cond ((zerop n) form)
83 ((< n 5) `(,(nth n '(identity cdr cddr cdddr cddddr)) ,form))
84 (t (make-cdxr (- n 4) `(cddddr ,form)))))
85 )
86
87 (defun true (&rest ignore) (declare (ignore ignore)) t)
88 (defun false (&rest ignore) (declare (ignore ignore)) nil)
89 (defun zero (&rest ignore) (declare (ignore ignore)) 0)
90
91 (defun make-plist (keys vals)
92 (if (null vals)
93 ()
94 (list* (car keys)
95 (car vals)
96 (make-plist (cdr keys) (cdr vals)))))
97
98 (defun remtail (list tail)
99 (if (eq list tail) () (cons (car list) (remtail (cdr list) tail))))
100
101 ;;; ONCE-ONLY does the same thing as it does in zetalisp. I should have just
102 ;;; lifted it from there but I am honest. Not only that but this one is
103 ;;; written in Common Lisp. I feel a lot like bootstrapping, or maybe more
104 ;;; like rebuilding Rome.
105 (defmacro once-only (vars &body body)
106 (let ((gensym-var (gensym))
107 (run-time-vars (gensym))
108 (run-time-vals (gensym))
109 (expand-time-val-forms ()))
110 (dolist (var vars)
111 (push `(if (or (symbolp ,var)
112 (numberp ,var)
113 (and (listp ,var)
114 (member (car ,var) '(quote function))))
115 ,var
116 (let ((,gensym-var (gensym)))
117 (push ,gensym-var ,run-time-vars)
118 (push ,var ,run-time-vals)
119 ,gensym-var))
120 expand-time-val-forms))
121 `(let* (,run-time-vars
122 ,run-time-vals
123 (wrapped-body
124 (let ,(mapcar #'list vars (reverse expand-time-val-forms))
125 ,@body)))
126 `(let ,(mapcar #'list (reverse ,run-time-vars)
127 (reverse ,run-time-vals))
128 ,wrapped-body))))
129
130 (eval-when (compile load eval)
131 (defun extract-declarations (body &optional environment)
132 ;;(declare (values documentation declarations body))
133 (let (documentation declarations form)
134 (when (and (stringp (car body))
135 (cdr body))
136 (setq documentation (pop body)))
137 (block outer
138 (loop
139 (when (null body) (return-from outer nil))
140 (setq form (car body))
141 (when (block inner
142 (loop (cond ((not (listp form))
143 (return-from outer nil))
144 ((eq (car form) 'declare)
145 (return-from inner 't))
146 (t
147 (multiple-value-bind (newform macrop)
148 (macroexpand-1 form environment)
149 (if (or (not (eq newform form)) macrop)
150 (setq form newform)
151 (return-from outer nil)))))))
152 (pop body)
153 (dolist (declaration (cdr form))
154 (push declaration declarations)))))
155 (values documentation
156 (and declarations `((declare ,.(nreverse declarations))))
157 body)))
158 )
159
160 (defun get-declaration (name declarations &optional default)
161 (dolist (d declarations default)
162 (dolist (form (cdr d))
163 (when (and (consp form) (eq (car form) name))
164 (return-from get-declaration (cdr form))))))
165
166
167 #+Lucid
168 (eval-when (compile load eval)
169 (eval `(defstruct ,(intern "FASLESCAPE" (find-package 'lucid)))))
170
171 (defvar *keyword-package* (find-package 'keyword))
172
173 (defun make-keyword (symbol)
174 (intern (symbol-name symbol) *keyword-package*))
175
176 (eval-when (compile load eval)
177
178 (defun string-append (&rest strings)
179 (setq strings (copy-list strings)) ;The explorer can't even
180 ;rplaca an &rest arg?
181 (do ((string-loc strings (cdr string-loc)))
182 ((null string-loc)
183 (apply #'concatenate 'string strings))
184 (rplaca string-loc (string (car string-loc)))))
185 )
186
187 (defun symbol-append (sym1 sym2 &optional (package *package*))
188 (intern (string-append sym1 sym2) package))
189
190 (defmacro check-member (place list &key (test #'eql) (pretty-name place))
191 (once-only (place list)
192 `(or (member ,place ,list :test ,test)
193 (error "The value of ~A, ~S is not one of ~S."
194 ',pretty-name ,place ,list))))
195
196 (defmacro alist-entry (alist key make-entry-fn)
197 (once-only (alist key)
198 `(or (assq ,key ,alist)
199 (progn (setf ,alist (cons (,make-entry-fn ,key) ,alist))
200 (car ,alist)))))
201
202 ;;; A simple version of destructuring-bind.
203
204 ;;; This does no more error checking than CAR and CDR themselves do. Some
205 ;;; attempt is made to be smart about preserving intermediate values. It
206 ;;; could be better, although the only remaining case should be easy for
207 ;;; the compiler to spot since it compiles to PUSH POP.
208 ;;;
209 ;;; Common Lisp BUG:
210 ;;; Common Lisp should have destructuring-bind.
211 ;;;
212 (defmacro destructuring-bind (pattern form &body body)
213 (multiple-value-bind (ignore declares body)
214 (extract-declarations body)
215 (declare (ignore ignore))
216 (multiple-value-bind (setqs binds)
217 (destructure pattern form)
218 `(let ,binds
219 ,@declares
220 ,@setqs
221 (progn .destructure-form.)
222 . ,body))))
223
224 (eval-when (compile load eval)
225 (defun destructure (pattern form)
226 ;;(declare (values setqs binds))
227 (let ((*destructure-vars* ())
228 (setqs ()))
229 (declare (special *destructure-vars*))
230 (setq *destructure-vars* '(.destructure-form.)
231 setqs (list `(setq .destructure-form. ,form))
232 form '.destructure-form.)
233 (values (nconc setqs (nreverse (destructure-internal pattern form)))
234 (delete nil *destructure-vars*))))
235
236 (defun destructure-internal (pattern form)
237 ;; When we are called, pattern must be a list. Form should be a symbol
238 ;; which we are free to setq containing the value to be destructured.
239 ;; Optimizations are performed for the last element of pattern cases.
240 ;; we assume that the compiler is smart about gensyms which are bound
241 ;; but only for a short period of time.
242 (declare (special *destructure-vars*))
243 (let ((gensym (gensym))
244 (pending-pops 0)
245 (var nil)
246 (setqs ()))
247 (labels
248 ((make-pop (var form pop-into)
249 (prog1
250 (cond ((zerop pending-pops)
251 `(progn ,(and var `(setq ,var (car ,form)))
252 ,(and pop-into `(setq ,pop-into (cdr ,form)))))
253 ((null pop-into)
254 (and var `(setq ,var ,(make-caxr pending-pops form))))
255 (t
256 `(progn (setq ,pop-into ,(make-cdxr pending-pops form))
257 ,(and var `(setq ,var (pop ,pop-into))))))
258 (setq pending-pops 0))))
259 (do ((pat pattern (cdr pat)))
260 ((null pat) ())
261 (if (symbolp (setq var (car pat)))
262 (progn
263 #-:coral (unless (memq var '(nil ignore))
264 (push var *destructure-vars*))
265 #+:coral (push var *destructure-vars*)
266 (cond ((null (cdr pat))
267 (push (make-pop var form ()) setqs))
268 ((symbolp (cdr pat))
269 (push (make-pop var form (cdr pat)) setqs)
270 (push (cdr pat) *destructure-vars*)
271 (return ()))
272 #-:coral
273 ((memq var '(nil ignore)) (incf pending-pops))
274 #-:coral
275 ((memq (cadr pat) '(nil ignore))
276 (push (make-pop var form ()) setqs)
277 (incf pending-pops 1))
278 (t
279 (push (make-pop var form form) setqs))))
280 (progn
281 (push `(let ((,gensym ()))
282 ,(make-pop gensym
283 form
284 (if (symbolp (cdr pat)) (cdr pat) form))
285 ,@(nreverse
286 (destructure-internal (car pat) gensym)))
287 setqs)
288 (when (symbolp (cdr pat))
289 (push (cdr pat) *destructure-vars*)
290 (return)))))
291 setqs)))
292 )
293
294
295 (defmacro collecting-once (&key initial-value)
296 `(let* ((head ,initial-value)
297 (tail ,(and initial-value `(last head))))
298 (values #'(lambda (value)
299 (if (null head)
300 (setq head (setq tail (list value)))
301 (unless (memq value head)
302 (setq tail
303 (cdr (rplacd tail (list value)))))))
304 #'(lambda nil head))))
305
306 (defmacro doplist ((key val) plist &body body &environment env)
307 (multiple-value-bind (doc decls bod)
308 (extract-declarations body env)
309 (declare (ignore doc))
310 `(let ((.plist-tail. ,plist) ,key ,val)
311 ,@decls
312 (loop (when (null .plist-tail.) (return nil))
313 (setq ,key (pop .plist-tail.))
314 (when (null .plist-tail.)
315 (error "Malformed plist in doplist, odd number of elements."))
316 (setq ,val (pop .plist-tail.))
317 (progn ,@bod)))))
318
319 (defmacro if* (condition true &rest false)
320 `(if ,condition ,true (progn ,@false)))
321
322 (defmacro dolist-carefully ((var list improper-list-handler) &body body)
323 `(let ((,var nil)
324 (.dolist-carefully. ,list))
325 (loop (when (null .dolist-carefully.) (return nil))
326 (if (consp .dolist-carefully.)
327 (progn
328 (setq ,var (pop .dolist-carefully.))
329 ,@body)
330 (,improper-list-handler)))))
331
332 ;;
333 ;;;;;; printing-random-thing
334 ;;
335 ;;; Similar to printing-random-object in the lisp machine but much simpler
336 ;;; and machine independent.
337 (defmacro printing-random-thing ((thing stream) &body body)
338 #+cmu17
339 `(print-unreadable-object (,thing ,stream :identity t) ,@body)
340 #-cmu17
341 (once-only (thing stream)
342 `(progn
343 (when *print-readably*
344 (error "~S cannot be printed readably." thing))
345 (format ,stream "#<")
346 ,@body
347 (format ,stream " ")
348 (printing-random-thing-internal ,thing ,stream)
349 (format ,stream ">"))))
350
351 (defun printing-random-thing-internal (thing stream)
352 (declare (ignore thing stream))
353 nil)
354
355 ;;
356 ;;;;;;
357 ;;
358
359 (defun capitalize-words (string &optional (dashes-p t))
360 (let ((string (copy-seq (string string))))
361 (declare (string string))
362 (do* ((flag t flag)
363 (length (length string) length)
364 (char nil char)
365 (i 0 (+ i 1)))
366 ((= i length) string)
367 (setq char (elt string i))
368 (cond ((both-case-p char)
369 (if flag
370 (and (setq flag (lower-case-p char))
371 (setf (elt string i) (char-upcase char)))
372 (and (not flag) (setf (elt string i) (char-downcase char))))
373 (setq flag nil))
374 ((char-equal char #\-)
375 (setq flag t)
376 (unless dashes-p (setf (elt string i) #\space)))
377 (t (setq flag nil))))))
378
379 #-(or lucid kcl)
380 (eval-when (compile load eval)
381 ;(warn "****** Things will go faster if you fix define-compiler-macro")
382 )
383
384 #-cmu
385 (defmacro define-compiler-macro (name arglist &body body)
386 #+(or lucid kcl)
387 `(#+lucid lcl:def-compiler-macro #+kcl si::define-compiler-macro
388 ,name ,arglist
389 ,@body)
390 #-(or kcl lucid)
391 (declare (ignore name arglist body))
392 #-(or kcl lucid)
393 nil)
394
395
396 ;;;
397 ;;; FIND-CLASS
398 ;;;
399 ;;; This is documented in the CLOS specification.
400 ;;;
401 (defvar *find-class* (make-hash-table :test #'eq))
402
403 (defun function-returning-nil (x)
404 (declare (ignore x))
405 nil)
406
407 (defmacro find-class-cell-class (cell)
408 `(car ,cell))
409
410 (defmacro find-class-cell-predicate (cell)
411 `(cadr ,cell))
412
413 (defmacro find-class-cell-make-instance-function-keys (cell)
414 `(cddr ,cell))
415
416 (defmacro make-find-class-cell (class-name)
417 (declare (ignore class-name))
418 '(list* nil #'function-returning-nil nil))
419
420 (defun find-class-cell (symbol &optional dont-create-p)
421 (or (gethash symbol *find-class*)
422 (unless dont-create-p
423 (unless (legal-class-name-p symbol)
424 (error "~S is not a legal class name." symbol))
425 (setf (gethash symbol *find-class*) (make-find-class-cell symbol)))))
426
427 (defvar *create-classes-from-internal-structure-definitions-p* t)
428
429 (defun find-class-from-cell (symbol cell &optional (errorp t))
430 (or (find-class-cell-class cell)
431 (and *create-classes-from-internal-structure-definitions-p*
432 (structure-type-p symbol)
433 (find-structure-class symbol))
434 (cond ((null errorp) nil)
435 ((legal-class-name-p symbol)
436 (error "No class named: ~S." symbol))
437 (t
438 (error "~S is not a legal class name." symbol)))))
439
440 (defun find-class-predicate-from-cell (symbol cell &optional (errorp t))
441 (unless (find-class-cell-class cell)
442 (find-class-from-cell symbol cell errorp))
443 (find-class-cell-predicate cell))
444
445 (defun legal-class-name-p (x)
446 (and (symbolp x)
447 (not (keywordp x))))
448
449 (defun find-class (symbol &optional (errorp t) environment)
450 "Returns the PCL class metaobject named by SYMBOL. An error of type
451 SIMPLE-ERROR is signaled if the class does not exist unless ERRORP
452 is NIL in which case NIL is returned. SYMBOL cannot be a keyword."
453 (declare (ignore environment))
454 (find-class-from-cell
455 symbol (find-class-cell symbol t) errorp))
456
457 (defun find-class-predicate (symbol &optional (errorp t) environment)
458 (declare (ignore environment))
459 (find-class-predicate-from-cell
460 symbol (find-class-cell symbol errorp) errorp))
461
462 (defvar *boot-state* nil) ; duplicate defvar to defs.lisp
463
464 ; Use this definition in any CL implementation supporting
465 ; both define-compiler-macro and load-time-value.
466 #+cmu ; Note that in CMU, lisp:find-class /= pcl:find-class
467 (define-compiler-macro find-class (&whole form
468 symbol &optional (errorp t) environment)
469 (declare (ignore environment))
470 (if (and (constantp symbol)
471 (legal-class-name-p (eval symbol))
472 (constantp errorp)
473 (member *boot-state* '(braid complete)))
474 (let ((symbol (eval symbol))
475 (errorp (not (null (eval errorp))))
476 (class-cell (make-symbol "CLASS-CELL")))
477 `(let ((,class-cell (load-time-value (find-class-cell ',symbol))))
478 (or (find-class-cell-class ,class-cell)
479 #-cmu17
480 (find-class-from-cell ',symbol ,class-cell ,errorp)
481 #+cmu17
482 ,(if errorp
483 `(find-class-from-cell ',symbol ,class-cell t)
484 `(and (kernel:class-cell-class
485 ',(kernel:find-class-cell symbol))
486 (find-class-from-cell ',symbol ,class-cell nil))))))
487 form))
488
489 (defun (setf find-class) (new-value symbol)
490 (if (legal-class-name-p symbol)
491 (let ((cell (find-class-cell symbol)))
492 (setf (find-class-cell-class cell) new-value)
493 (when (or (eq *boot-state* 'complete)
494 (eq *boot-state* 'braid))
495 (when (and new-value (class-wrapper new-value))
496 (setf (find-class-cell-predicate cell)
497 (symbol-function (class-predicate-name new-value))))
498 (when (and new-value (not (forward-referenced-class-p new-value)))
499
500 (dolist (keys+aok (find-class-cell-make-instance-function-keys cell))
501 (update-initialize-info-internal
502 (initialize-info new-value (car keys+aok) nil (cdr keys+aok))
503 'make-instance-function))))
504 new-value)
505 (error "~S is not a legal class name." symbol)))
506
507 (defun (setf find-class-predicate) (new-value symbol)
508 (if (legal-class-name-p symbol)
509 (setf (find-class-cell-predicate (find-class-cell symbol)) new-value)
510 (error "~S is not a legal class name." symbol)))
511
512 (defmacro gathering1 (gatherer &body body)
513 `(gathering ((.gathering1. ,gatherer))
514 (macrolet ((gather1 (x) `(gather ,x .gathering1.)))
515 ,@body)))
516
517
518 ;;;
519 ;;; These are augmented definitions of list-elements and list-tails from
520 ;;; iterate.lisp. These versions provide the extra :by keyword which can
521 ;;; be used to specify the step function through the list.
522 ;;;
523 (defmacro *list-elements (list &key (by #'cdr))
524 `(let ((tail ,list))
525 #'(lambda (finish)
526 (if (endp tail)
527 (funcall finish)
528 (prog1 (car tail)
529 (setq tail (funcall ,by tail)))))))
530
531 (defmacro *list-tails (list &key (by #'cdr))
532 `(let ((tail ,list))
533 #'(lambda (finish)
534 (prog1 (if (endp tail)
535 (funcall finish)
536 tail)
537 (setq tail (funcall ,by tail))))))
538
539 (defmacro function-funcall (form &rest args)
540 #-cmu `(funcall ,form ,@args)
541 #+cmu `(funcall (the function ,form) ,@args))
542
543 (defmacro function-apply (form &rest args)
544 #-cmu `(apply ,form ,@args)
545 #+cmu `(apply (the function ,form) ,@args))
546
547
548 (defsetf slot-value set-slot-value)
549
550 (defvar *redefined-functions* nil)
551
552 (defmacro original-definition (name)
553 `(get ,name ':definition-before-pcl))
554
555 (defun redefine-function (name new)
556 (pushnew name *redefined-functions*)
557 (unless (original-definition name)
558 (setf (original-definition name)
559 (symbol-function name)))
560 (setf (symbol-function name)
561 (symbol-function new)))
562

  ViewVC Help
Powered by ViewVC 1.1.5