39379d27dc7bb01053c2746adff56a7425374c92
[sbcl.git] / src / pcl / macros.lisp
1 ;;;; macros, global variable definitions, and other miscellaneous support stuff
2 ;;;; used by the rest of the PCL subsystem
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6
7 ;;;; This software is derived from software originally released by Xerox
8 ;;;; Corporation. Copyright and release statements follow. Later modifications
9 ;;;; to the software are in the public domain and are provided with
10 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
11 ;;;; information.
12
13 ;;;; copyright information from original PCL sources:
14 ;;;;
15 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
16 ;;;; All rights reserved.
17 ;;;;
18 ;;;; Use and copying of this software and preparation of derivative works based
19 ;;;; upon this software are permitted. Any distribution of this software or
20 ;;;; derivative works must comply with all applicable United States export
21 ;;;; control laws.
22 ;;;;
23 ;;;; This software is made available AS IS, and Xerox Corporation makes no
24 ;;;; warranty about the software, its performance or its conformity to any
25 ;;;; specification.
26
27 (in-package "SB-PCL")
28 \f
29 (/show "starting pcl/macros.lisp")
30
31 (declaim (declaration
32           ;; These nonstandard declarations seem to be used privately
33           ;; within PCL itself to pass information around, so we can't
34           ;; just delete them.
35           %class
36           ;; This declaration may also be used within PCL to pass
37           ;; information around, I'm not sure. -- WHN 2000-12-30
38           %variable-rebinding))
39
40 (/show "done with DECLAIM DECLARATION")
41
42 (defun get-declaration (name declarations &optional default)
43   (dolist (d declarations default)
44     (dolist (form (cdr d))
45       (when (and (consp form) (eq (car form) name))
46         (return-from get-declaration (cdr form))))))
47
48 (/show "pcl/macros.lisp 85")
49
50 (defmacro doplist ((key val) plist &body body)
51   `(let ((.plist-tail. ,plist) ,key ,val)
52      (loop (when (null .plist-tail.) (return nil))
53            (setq ,key (pop .plist-tail.))
54            (when (null .plist-tail.)
55              (error "malformed plist, odd number of elements"))
56            (setq ,val (pop .plist-tail.))
57            (progn ,@body))))
58
59 (/show "pcl/macros.lisp 101")
60
61 (defmacro dolist-carefully ((var list improper-list-handler) &body body)
62   `(let ((,var nil)
63          (.dolist-carefully. ,list))
64      (loop (when (null .dolist-carefully.) (return nil))
65            (if (consp .dolist-carefully.)
66                (progn
67                  (setq ,var (pop .dolist-carefully.))
68                  ,@body)
69                (,improper-list-handler)))))
70 \f
71 ;;;; FIND-CLASS
72 ;;;;
73 ;;;; This is documented in the CLOS specification.
74
75 (/show "pcl/macros.lisp 119")
76
77 (declaim (inline legal-class-name-p))
78 (defun legal-class-name-p (x)
79   (symbolp x))
80
81 (defvar *create-classes-from-internal-structure-definitions-p* t)
82
83 (defun find-class-from-cell (symbol cell &optional (errorp t))
84   (or (when cell
85         (or (classoid-cell-pcl-class cell)
86             (when *create-classes-from-internal-structure-definitions-p*
87               (let ((classoid (classoid-cell-classoid cell)))
88                 (when (and classoid
89                            (or (condition-classoid-p classoid)
90                                (defstruct-classoid-p classoid)))
91                   (ensure-non-standard-class symbol classoid))))))
92       (cond ((null errorp) nil)
93             ((legal-class-name-p symbol)
94              (error "There is no class named ~
95                      ~/sb-impl::print-symbol-with-prefix/." symbol))
96             (t
97              (error "~S is not a legal class name." symbol)))))
98
99 (defun find-class (symbol &optional (errorp t) environment)
100   (declare (ignore environment))
101   (find-class-from-cell symbol
102                         (find-classoid-cell symbol)
103                         errorp))
104
105 \f
106 ;;; This DEFVAR was originally in defs.lisp, now moved here.
107 ;;;
108 ;;; Possible values are NIL, EARLY, BRAID, or COMPLETE.
109 (declaim (type (member nil early braid complete) **boot-state**))
110 (defglobal **boot-state** nil)
111
112 (/show "pcl/macros.lisp 187")
113
114 (define-compiler-macro find-class (&whole form
115                                    symbol &optional (errorp t) environment)
116   (declare (ignore environment))
117   (if (and (constantp symbol)
118            (legal-class-name-p (setf symbol (constant-form-value symbol)))
119            (constantp errorp)
120            (member **boot-state** '(braid complete)))
121       (let ((errorp (not (null (constant-form-value errorp))))
122             (cell (make-symbol "CLASSOID-CELL")))
123         `(let ((,cell (load-time-value (find-classoid-cell ',symbol :create t))))
124            (or (classoid-cell-pcl-class ,cell)
125                ,(if errorp
126                     `(find-class-from-cell ',symbol ,cell t)
127                     `(when (classoid-cell-classoid ,cell)
128                        (find-class-from-cell ',symbol ,cell nil))))))
129       form))
130
131 (declaim (inline class-classoid))
132 (defun class-classoid (class)
133   (layout-classoid (class-wrapper class)))
134
135 (defun (setf find-class) (new-value name &optional errorp environment)
136   (declare (ignore errorp environment))
137   (cond ((legal-class-name-p name)
138          (with-single-package-locked-error
139              (:symbol name "Using ~A as the class-name argument in ~
140                            (SETF FIND-CLASS)"))
141          (with-world-lock ()
142            (let ((cell (find-classoid-cell name :create new-value)))
143              (cond (new-value
144                     (setf (classoid-cell-pcl-class cell) new-value)
145                     (when (eq **boot-state** 'complete)
146                       (let ((classoid (class-classoid new-value)))
147                         (setf (find-classoid name) classoid)
148                         (%set-class-type-translation new-value classoid))))
149                    (cell
150                     (%clear-classoid name cell)))
151              (when (or (eq **boot-state** 'complete)
152                        (eq **boot-state** 'braid))
153                (update-ctors 'setf-find-class :class new-value :name name))
154              new-value)))
155         (t
156          (error "~S is not a legal class name." name))))
157
158 (/show "pcl/macros.lisp 241")
159
160 (defmacro function-funcall (form &rest args)
161   `(funcall (the function ,form) ,@args))
162
163 (defmacro function-apply (form &rest args)
164   `(apply (the function ,form) ,@args))
165
166 (/show "pcl/macros.lisp 249")
167 \f
168 (defun get-setf-fun-name (name)
169   `(setf ,name))
170
171 (defsetf slot-value set-slot-value)
172 \f
173 (/show "finished with pcl/macros.lisp")