1.0.23.37: more CLOS and classoid thread safety
[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 ;;;
114 ;;; KLUDGE: This should probably become
115 ;;;   (DECLAIM (TYPE (MEMBER NIL :EARLY :BRAID :COMPLETE) *BOOT-STATE*))
116 (defvar *boot-state* nil)
117
118 (/show "pcl/macros.lisp 187")
119
120 (define-compiler-macro find-class (&whole form
121                                    symbol &optional (errorp t) environment)
122   (declare (ignore environment))
123   (if (and (constantp symbol)
124            (legal-class-name-p (setf symbol (constant-form-value symbol)))
125            (constantp errorp)
126            (member *boot-state* '(braid complete)))
127       (let ((errorp (not (null (constant-form-value errorp))))
128             (cell (make-symbol "CLASSOID-CELL")))
129         `(let ((,cell (load-time-value (find-classoid-cell ',symbol :create t))))
130            (or (classoid-cell-pcl-class ,cell)
131                ,(if errorp
132                     `(find-class-from-cell ',symbol ,cell t)
133                     `(when (classoid-cell-classoid ,cell)
134                        (find-class-from-cell ',symbol ,cell nil))))))
135       form))
136
137 (declaim (inline class-classoid))
138 (defun class-classoid (class)
139   (layout-classoid (class-wrapper class)))
140
141 (defun (setf find-class) (new-value name &optional errorp environment)
142   (declare (ignore errorp environment))
143   (cond ((legal-class-name-p name)
144          (with-single-package-locked-error
145              (:symbol name "Using ~A as the class-name argument in ~
146                            (SETF FIND-CLASS)"))
147          (with-world-lock ()
148            (let ((cell (find-classoid-cell name :create new-value)))
149              (cond (new-value
150                     (setf (classoid-cell-pcl-class cell) new-value)
151                     (when (eq *boot-state* 'complete)
152                       (let ((classoid (class-classoid new-value)))
153                         (setf (find-classoid name) classoid)
154                         (%set-class-type-translation new-value classoid))))
155                    (cell
156                     (%clear-classoid name cell)))
157              (when (or (eq *boot-state* 'complete)
158                        (eq *boot-state* 'braid))
159                (update-ctors 'setf-find-class :class new-value :name name))
160              new-value)))
161         (t
162          (error "~S is not a legal class name." name))))
163
164 (/show "pcl/macros.lisp 241")
165
166 (defmacro function-funcall (form &rest args)
167   `(funcall (the function ,form) ,@args))
168
169 (defmacro function-apply (form &rest args)
170   `(apply (the function ,form) ,@args))
171
172 (/show "pcl/macros.lisp 249")
173 \f
174 (defun get-setf-fun-name (name)
175   `(setf ,name))
176
177 (defsetf slot-value set-slot-value)
178 \f
179 (/show "finished with pcl/macros.lisp")