1.0.31.9: some PCL micro-optimizations
[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           ;; As of sbcl-0.7.0.6, SBCL actively uses this declaration
33           ;; to propagate information needed to set up nice debug
34           ;; names (as seen e.g. in BACKTRACE) for method functions.
35           %method-name
36           ;; These nonstandard declarations seem to be used privately
37           ;; within PCL itself to pass information around, so we can't
38           ;; just delete them.
39           %class
40           %method-lambda-list
41           ;; This declaration may also be used within PCL to pass
42           ;; information around, I'm not sure. -- WHN 2000-12-30
43           %variable-rebinding))
44
45 (/show "done with DECLAIM DECLARATION")
46
47 (defun get-declaration (name declarations &optional default)
48   (dolist (d declarations default)
49     (dolist (form (cdr d))
50       (when (and (consp form) (eq (car form) name))
51         (return-from get-declaration (cdr form))))))
52
53 (/show "pcl/macros.lisp 85")
54
55 (defmacro doplist ((key val) plist &body body)
56   `(let ((.plist-tail. ,plist) ,key ,val)
57      (loop (when (null .plist-tail.) (return nil))
58            (setq ,key (pop .plist-tail.))
59            (when (null .plist-tail.)
60              (error "malformed plist, odd number of elements"))
61            (setq ,val (pop .plist-tail.))
62            (progn ,@body))))
63
64 (/show "pcl/macros.lisp 101")
65
66 (defmacro dolist-carefully ((var list improper-list-handler) &body body)
67   `(let ((,var nil)
68          (.dolist-carefully. ,list))
69      (loop (when (null .dolist-carefully.) (return nil))
70            (if (consp .dolist-carefully.)
71                (progn
72                  (setq ,var (pop .dolist-carefully.))
73                  ,@body)
74                (,improper-list-handler)))))
75 \f
76 ;;;; FIND-CLASS
77 ;;;;
78 ;;;; This is documented in the CLOS specification.
79
80 (/show "pcl/macros.lisp 119")
81
82 (declaim (inline legal-class-name-p))
83 (defun legal-class-name-p (x)
84   (symbolp x))
85
86 (defvar *create-classes-from-internal-structure-definitions-p* t)
87
88 (defun find-class-from-cell (symbol cell &optional (errorp t))
89   (or (when cell
90         (or (classoid-cell-pcl-class cell)
91             (when *create-classes-from-internal-structure-definitions-p*
92               (let ((classoid (classoid-cell-classoid cell)))
93                 (when (and classoid
94                            (or (condition-classoid-p classoid)
95                                (defstruct-classoid-p classoid)))
96                   (ensure-non-standard-class symbol classoid))))))
97       (cond ((null errorp) nil)
98             ((legal-class-name-p symbol)
99              (error "There is no class named ~S." symbol))
100             (t
101              (error "~S is not a legal class name." symbol)))))
102
103 (defun find-class (symbol &optional (errorp t) environment)
104   (declare (ignore environment))
105   (find-class-from-cell symbol
106                         (find-classoid-cell symbol)
107                         errorp))
108
109 \f
110 ;;; This DEFVAR was originally in defs.lisp, now moved here.
111 ;;;
112 ;;; Possible values are NIL, EARLY, BRAID, or COMPLETE.
113 (declaim (type (member nil early braid complete) **boot-state**))
114 (defglobal **boot-state** nil)
115
116 (/show "pcl/macros.lisp 187")
117
118 (define-compiler-macro find-class (&whole form
119                                    symbol &optional (errorp t) environment)
120   (declare (ignore environment))
121   (if (and (constantp symbol)
122            (legal-class-name-p (setf symbol (constant-form-value symbol)))
123            (constantp errorp)
124            (member **boot-state** '(braid complete)))
125       (let ((errorp (not (null (constant-form-value errorp))))
126             (cell (make-symbol "CLASSOID-CELL")))
127         `(let ((,cell (load-time-value (find-classoid-cell ',symbol :create t))))
128            (or (classoid-cell-pcl-class ,cell)
129                ,(if errorp
130                     `(find-class-from-cell ',symbol ,cell t)
131                     `(when (classoid-cell-classoid ,cell)
132                        (find-class-from-cell ',symbol ,cell nil))))))
133       form))
134
135 (declaim (inline class-classoid))
136 (defun class-classoid (class)
137   (layout-classoid (class-wrapper class)))
138
139 (defun (setf find-class) (new-value name &optional errorp environment)
140   (declare (ignore errorp environment))
141   (cond ((legal-class-name-p name)
142          (with-single-package-locked-error
143              (:symbol name "Using ~A as the class-name argument in ~
144                            (SETF FIND-CLASS)"))
145          (with-world-lock ()
146            (let ((cell (find-classoid-cell name :create new-value)))
147              (cond (new-value
148                     (setf (classoid-cell-pcl-class cell) new-value)
149                     (when (eq **boot-state** 'complete)
150                       (let ((classoid (class-classoid new-value)))
151                         (setf (find-classoid name) classoid)
152                         (%set-class-type-translation new-value classoid))))
153                    (cell
154                     (%clear-classoid name cell)))
155              (when (or (eq **boot-state** 'complete)
156                        (eq **boot-state** 'braid))
157                (update-ctors 'setf-find-class :class new-value :name name))
158              new-value)))
159         (t
160          (error "~S is not a legal class name." name))))
161
162 (/show "pcl/macros.lisp 241")
163
164 (defmacro function-funcall (form &rest args)
165   `(funcall (the function ,form) ,@args))
166
167 (defmacro function-apply (form &rest args)
168   `(apply (the function ,form) ,@args))
169
170 (/show "pcl/macros.lisp 249")
171 \f
172 (defun get-setf-fun-name (name)
173   `(setf ,name))
174
175 (defsetf slot-value set-slot-value)
176 \f
177 (/show "finished with pcl/macros.lisp")