1 ;;;; This file defines the initialization and related protocols.
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from software originally released by Xerox
7 ;;;; Corporation. Copyright and release statements follow. Later modifications
8 ;;;; to the software are in the public domain and are provided with
9 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
12 ;;;; copyright information from original PCL sources:
14 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
15 ;;;; All rights reserved.
17 ;;;; Use and copying of this software and preparation of derivative works based
18 ;;;; upon this software are permitted. Any distribution of this software or
19 ;;;; derivative works must comply with all applicable United States export
22 ;;;; This software is made available AS IS, and Xerox Corporation makes no
23 ;;;; warranty about the software, its performance or its conformity to any
28 (defmethod make-instance ((class symbol) &rest initargs)
29 (apply #'make-instance (find-class class) initargs))
31 (defmethod make-instance ((class class) &rest initargs)
32 (unless (class-finalized-p class) (finalize-inheritance class))
33 (setq initargs (default-initargs class initargs))
37 (list (list* 'allocate-instance class initargs)
38 (list* 'initialize-instance (class-prototype class) initargs)
39 (list* 'shared-initialize (class-prototype class) t initargs)))
41 (let* ((info (initialize-info class initargs))
42 (valid-p (initialize-info-valid-p info)))
43 (when (and (consp valid-p) (eq (car valid-p) :invalid))
44 (error "Invalid initialization argument ~S for class ~S"
45 (cdr valid-p) (class-name class))))
46 (let ((instance (apply #'allocate-instance class initargs)))
47 (apply #'initialize-instance instance initargs)
50 (defvar *default-initargs-flag* (list nil))
52 (defmethod default-initargs ((class slot-class) supplied-initargs)
53 (call-initialize-function
54 (initialize-info-default-initargs-function
55 (initialize-info class supplied-initargs))
56 nil supplied-initargs)
58 ;; This implementation of default initargs is critically dependent
59 ;; on all-default-initargs not having any duplicate initargs in it.
60 (let ((all-default (class-default-initargs class))
61 (miss *default-initargs-flag*))
62 (flet ((getf* (plist key)
65 (if (eq (car plist) key)
67 (setq plist (cddr plist))))))
68 (labels ((default-1 (tail)
71 (if (eq (getf* supplied-initargs (caar tail)) miss)
73 (funcall (cadar tail))
74 (default-1 (cdr tail)))
75 (default-1 (cdr tail))))))
76 (append supplied-initargs (default-1 all-default)))))
79 (defmethod initialize-instance ((instance slot-object) &rest initargs)
80 (apply #'shared-initialize instance t initargs))
82 (defmethod reinitialize-instance ((instance slot-object) &rest initargs)
85 (class-of instance) initargs
86 (list (list* 'reinitialize-instance instance initargs)
87 (list* 'shared-initialize instance nil initargs)))
89 (let* ((class (class-of instance))
90 (info (initialize-info class initargs))
91 (valid-p (initialize-info-ri-valid-p info)))
92 (when (and (consp valid-p) (eq (car valid-p) :invalid))
93 (error "Invalid initialization argument ~S for class ~S"
94 (cdr valid-p) (class-name class))))
95 (apply #'shared-initialize instance nil initargs)
98 (defmethod update-instance-for-different-class ((previous std-object)
101 ;; First we must compute the newly added slots. The spec defines
102 ;; newly added slots as "those local slots for which no slot of
103 ;; the same name exists in the previous class."
104 (let ((added-slots '())
105 (current-slotds (class-slots (class-of current)))
106 (previous-slot-names (mapcar #'slot-definition-name
107 (class-slots (class-of previous)))))
108 (dolist (slotd current-slotds)
109 (if (and (not (memq (slot-definition-name slotd) previous-slot-names))
110 (eq (slot-definition-allocation slotd) ':instance))
111 (push (slot-definition-name slotd) added-slots)))
113 (class-of current) initargs
114 (list (list* 'update-instance-for-different-class previous current initargs)
115 (list* 'shared-initialize current added-slots initargs)))
116 (apply #'shared-initialize current added-slots initargs)))
118 (defmethod update-instance-for-redefined-class ((instance std-object)
124 (class-of instance) initargs
125 (list (list* 'update-instance-for-redefined-class
126 instance added-slots discarded-slots property-list initargs)
127 (list* 'shared-initialize instance added-slots initargs)))
128 (apply #'shared-initialize instance added-slots initargs))
130 (defmethod shared-initialize
131 ((instance slot-object) slot-names &rest initargs)
132 (when (eq slot-names t)
133 (return-from shared-initialize
134 (call-initialize-function
135 (initialize-info-shared-initialize-t-function
136 (initialize-info (class-of instance) initargs))
138 (when (eq slot-names nil)
139 (return-from shared-initialize
140 (call-initialize-function
141 (initialize-info-shared-initialize-nil-function
142 (initialize-info (class-of instance) initargs))
144 ;; Initialize the instance's slots in a two step process:
145 ;; (1) A slot for which one of the initargs in initargs can set
146 ;; the slot, should be set by that initarg. If more than
147 ;; one initarg in initargs can set the slot, the leftmost
148 ;; one should set it.
149 ;; (2) Any slot not set by step 1, may be set from its initform
150 ;; by step 2. Only those slots specified by the slot-names
151 ;; argument are set. If slot-names is:
153 ;; then any slot not set in step 1 is set from its
155 ;; <list of slot names>
156 ;; then any slot in the list, and not set in step 1
157 ;; is set from its initform.
159 ;; then no slots are set from initforms.
160 (let* ((class (class-of instance))
161 (slotds (class-slots class))
162 (std-p (pcl-instance-p instance)))
163 (dolist (slotd slotds)
164 (let ((slot-name (slot-definition-name slotd))
165 (slot-initargs (slot-definition-initargs slotd)))
167 ;; Try to initialize the slot from one of the initargs.
168 ;; If we succeed return T, otherwise return nil.
169 (doplist (initarg val) initargs
170 (when (memq initarg slot-initargs)
171 (setf (slot-value-using-class class
176 ;; Try to initialize the slot from its initform.
178 (or (eq slot-names t)
179 (memq slot-name slot-names))
180 (or (and (not std-p) (eq slot-names t))
181 (not (slot-boundp-using-class class instance slotd))))
182 (let ((initfunction (slot-definition-initfunction slotd)))
184 (setf (slot-value-using-class class instance slotd)
185 (funcall initfunction))))))))
188 ;;; If initargs are valid return nil, otherwise signal an error.
189 (defun check-initargs-1 (class initargs call-list
190 &optional (plist-p t) (error-p t))
191 (multiple-value-bind (legal allow-other-keys)
192 (check-initargs-values class call-list)
193 (unless allow-other-keys
195 (check-initargs-2-plist initargs class legal error-p)
196 (check-initargs-2-list initargs class legal error-p)))))
198 (defun check-initargs-values (class call-list)
199 (let ((methods (mapcan #'(lambda (call)
201 (copy-list (compute-applicable-methods
202 (gdefinition (car call))
206 (legal (apply #'append (mapcar #'slot-definition-initargs
207 (class-slots class)))))
208 ;; Add to the set of slot-filling initargs the set of
209 ;; initargs that are accepted by the methods. If at
210 ;; any point we come across &allow-other-keys, we can
212 (dolist (method methods)
213 (multiple-value-bind (nreq nopt keysp restp allow-other-keys keys)
214 (analyze-lambda-list (if (consp method)
215 (early-method-lambda-list method)
216 (method-lambda-list method)))
217 (declare (ignore nreq nopt keysp restp))
218 (when allow-other-keys
219 (return-from check-initargs-values (values nil t)))
220 (setq legal (append keys legal))))
223 (defun check-initargs-2-plist (initargs class legal &optional (error-p t))
224 (unless (getf initargs :allow-other-keys)
225 ;; Now check the supplied-initarg-names and the default initargs
226 ;; against the total set that we know are legal.
227 (doplist (key val) initargs
228 (unless (memq key legal)
230 (error "Invalid initialization argument ~S for class ~S"
233 (return-from check-initargs-2-plist nil)))))
236 (defun check-initargs-2-list (initkeys class legal &optional (error-p t))
237 (unless (memq :allow-other-keys initkeys)
238 ;; Now check the supplied-initarg-names and the default initargs
239 ;; against the total set that we know are legal.
240 (dolist (key initkeys)
241 (unless (memq key legal)
243 (error "Invalid initialization argument ~S for class ~S"
246 (return-from check-initargs-2-list nil)))))