Spelling fixes
[emacs.git] / lisp / emacs-lisp / eieio-core.el
blob6fd9c14088e222aaf008a6ec77cd11ca92376c8b
1 ;;; eieio-core.el --- Core implementation for eieio -*- lexical-binding:t -*-
3 ;; Copyright (C) 1995-1996, 1998-2015 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Version: 1.4
7 ;; Keywords: OO, lisp
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; The "core" part of EIEIO is the implementation for the object
27 ;; system (such as eieio-defclass, or eieio-defmethod) but not the
28 ;; base classes for the object system, which are defined in EIEIO.
30 ;; See the commentary for eieio.el for more about EIEIO itself.
32 ;;; Code:
34 (require 'cl-lib)
35 (require 'pcase)
37 ;;;
38 ;; A few functions that are better in the official EIEIO src, but
39 ;; used from the core.
40 (declare-function slot-unbound "eieio")
41 (declare-function slot-missing "eieio")
42 (declare-function child-of-class-p "eieio")
43 (declare-function same-class-p "eieio")
44 (declare-function object-of-class-p "eieio")
47 ;;;
48 ;; Variable declarations.
50 (defvar eieio-hook nil
51 "This hook is executed, then cleared each time `defclass' is called.")
53 (defvar eieio-error-unsupported-class-tags nil
54 "Non-nil to throw an error if an encountered tag is unsupported.
55 This may prevent classes from CLOS applications from being used with EIEIO
56 since EIEIO does not support all CLOS tags.")
58 (defvar eieio-skip-typecheck nil
59 "If non-nil, skip all slot typechecking.
60 Set this to t permanently if a program is functioning well to get a
61 small speed increase. This variable is also used internally to handle
62 default setting for optimization purposes.")
64 (defvar eieio-optimize-primary-methods-flag t
65 "Non-nil means to optimize the method dispatch on primary methods.")
67 (defvar eieio-backward-compatibility t
68 "If nil, drop support for some behaviors of older versions of EIEIO.
69 Currently under control of this var:
70 - Define every class as a var whose value is the class symbol.
71 - Define <class>-child-p and <class>-list-p predicates.
72 - Allow object names in constructors.")
74 (defconst eieio-unbound
75 (if (and (boundp 'eieio-unbound) (symbolp eieio-unbound))
76 eieio-unbound
77 (make-symbol "unbound"))
78 "Uninterned symbol representing an unbound slot in an object.")
80 ;; This is a bootstrap for eieio-default-superclass so it has a value
81 ;; while it is being built itself.
82 (defvar eieio-default-superclass nil)
84 (progn
85 ;; Arrange for field access not to bother checking if the access is indeed
86 ;; made to an eieio--class object.
87 (cl-declaim (optimize (safety 0)))
89 (cl-defstruct (eieio--class
90 (:constructor nil)
91 (:constructor eieio--class-make (name &aux (tag 'defclass)))
92 (:type vector)
93 (:copier nil))
94 ;; We use an untagged cl-struct, with our own hand-made tag as first field
95 ;; (containing the symbol `defclass'). It would be better to use a normal
96 ;; cl-struct with its normal tag (e.g. so that cl-defstruct can define the
97 ;; predicate for us), but that breaks compatibility with .elc files compiled
98 ;; against older versions of EIEIO.
99 tag
100 ;; Fields we could inherit from cl--class (if we used a tagged cl-struct):
101 (name nil :type symbol) ;The type name.
102 (docstring nil :type string)
103 (parents nil :type (or eieio--class (list-of eieio--class)))
104 (slots nil :type (vector cl-slot-descriptor))
105 (index-table nil :type hash-table)
106 ;; Fields specific to EIEIO classes:
107 children
108 initarg-tuples ;; initarg tuples list
109 (class-slots nil :type eieio--slot)
110 class-allocation-values ;; class allocated value vector
111 default-object-cache ;; what a newly created object would look like.
112 ; This will speed up instantiation time as
113 ; only a `copy-sequence' will be needed, instead of
114 ; looping over all the values and setting them from
115 ; the default.
116 options ;; storage location of tagged class option
117 ; Stored outright without modifications or stripping
119 ;; Set it back to the default value.
120 (cl-declaim (optimize (safety 1))))
123 (cl-defstruct (eieio--object
124 (:type vector) ;We manage our own tagging system.
125 (:constructor nil)
126 (:copier nil))
127 ;; `class-tag' holds a symbol, which is not the class name, but is instead
128 ;; properly prefixed as an internal EIEIO thingy and which holds the class
129 ;; object/struct in its `symbol-value' slot.
130 class-tag)
132 (eval-when-compile
133 (defconst eieio--object-num-slots
134 (length (cl-struct-slot-info 'eieio--object))))
136 (defsubst eieio--object-class (obj)
137 (symbol-value (eieio--object-class-tag obj)))
140 ;;; Important macros used internally in eieio.
142 (defmacro eieio--class-v (class) ;Use a macro, so it acts as a GV place.
143 "Internal: Return the class vector from the CLASS symbol."
144 (declare (debug t))
145 ;; No check: If eieio gets this far, it has probably been checked already.
146 `(get ,class 'eieio-class-definition))
148 (defsubst eieio--class-object (class)
149 "Return the class object."
150 (if (symbolp class)
151 ;; Keep the symbol if class-v is nil, for better error messages.
152 (or (eieio--class-v class) class)
153 class))
155 (defsubst eieio--class-p (class)
156 "Return non-nil if CLASS is a valid class object."
157 (condition-case nil
158 (eq (aref class 0) 'defclass)
159 (error nil)))
161 (defun class-p (class)
162 "Return non-nil if CLASS is a valid class vector.
163 CLASS is a symbol." ;FIXME: Is it a vector or a symbol?
164 (and (symbolp class) (eieio--class-p (eieio--class-v class))))
166 (defun eieio--class-print-name (class)
167 "Return a printed representation of CLASS."
168 (format "#<class %s>" (eieio-class-name class)))
170 (defun eieio-class-name (class)
171 "Return a Lisp like symbol name for CLASS."
172 (setq class (eieio--class-object class))
173 (cl-check-type class eieio--class)
174 (eieio--class-name class))
175 (define-obsolete-function-alias 'class-name #'eieio-class-name "24.4")
177 (defalias 'eieio--class-constructor #'identity
178 "Return the symbol representing the constructor of CLASS.")
180 (defmacro eieio--class-option-assoc (list option)
181 "Return from LIST the found OPTION, or nil if it doesn't exist."
182 `(car-safe (cdr (memq ,option ,list))))
184 (defsubst eieio--class-option (class option)
185 "Return the value stored for CLASS' OPTION.
186 Return nil if that option doesn't exist."
187 (eieio--class-option-assoc (eieio--class-options class) option))
189 (defun eieio-object-p (obj)
190 "Return non-nil if OBJ is an EIEIO object."
191 (and (vectorp obj)
192 (> (length obj) 0)
193 (let ((tag (eieio--object-class-tag obj)))
194 (and (symbolp tag)
195 ;; (eq (symbol-function tag) :quick-object-witness-check)
196 (boundp tag)
197 (eieio--class-p (symbol-value tag))))))
199 (define-obsolete-function-alias 'object-p 'eieio-object-p "25.1")
201 (defsubst class-abstract-p (class)
202 "Return non-nil if CLASS is abstract.
203 Abstract classes cannot be instantiated."
204 (eieio--class-option (eieio--class-v class) :abstract))
206 (defsubst eieio--class-method-invocation-order (class)
207 "Return the invocation order of CLASS.
208 Abstract classes cannot be instantiated."
209 (or (eieio--class-option class :method-invocation-order)
210 :breadth-first))
215 ;; Class Creation
217 (defvar eieio-defclass-autoload-map (make-hash-table)
218 "Symbol map of superclasses we find in autoloads.")
220 ;; We autoload this because it's used in `make-autoload'.
221 ;;;###autoload
222 (defun eieio-defclass-autoload (cname _superclasses filename doc)
223 "Create autoload symbols for the EIEIO class CNAME.
224 SUPERCLASSES are the superclasses that CNAME inherits from.
225 DOC is the docstring for CNAME.
226 This function creates a mock-class for CNAME and adds it into
227 SUPERCLASSES as children.
228 It creates an autoload function for CNAME's constructor."
229 ;; Assume we've already debugged inputs.
231 ;; We used to store the list of superclasses in the `parent' slot (as a list
232 ;; of class names). But now this slot holds a list of class objects, and
233 ;; those parents may not exist yet, so the corresponding class objects may
234 ;; simply not exist yet. So instead we just don't store the list of parents
235 ;; here in eieio-defclass-autoload at all, since it seems that they're just
236 ;; not needed before the class is actually loaded.
237 (let* ((oldc (eieio--class-v cname))
238 (newc (eieio--class-make cname)))
239 (if (eieio--class-p oldc)
240 nil ;; Do nothing if we already have this class.
242 ;; turn this into a usable self-pointing symbol
243 (when eieio-backward-compatibility
244 (set cname cname)
245 (make-obsolete-variable cname (format "use '%s instead" cname) "25.1"))
247 ;; Store the new class vector definition into the symbol. We need to
248 ;; do this first so that we can call defmethod for the accessor.
249 ;; The vector will be updated by the following while loop and will not
250 ;; need to be stored a second time.
251 (setf (eieio--class-v cname) newc)
253 ;; Create an autoload on top of our constructor function.
254 (autoload cname filename doc nil nil)
255 (autoload (intern (format "%s-p" cname)) filename "" nil nil)
256 (when eieio-backward-compatibility
257 (autoload (intern (format "%s-child-p" cname)) filename "" nil nil)
258 (autoload (intern (format "%s-list-p" cname)) filename "" nil nil)))))
260 (defsubst eieio-class-un-autoload (cname)
261 "If class CNAME is in an autoload state, load its file."
262 (autoload-do-load (symbol-function cname))) ; cname
264 (cl-deftype list-of (elem-type)
265 `(and list
266 (satisfies (lambda (list)
267 (cl-every (lambda (elem) (cl-typep elem ',elem-type))
268 list)))))
271 (defun eieio-make-class-predicate (class)
272 (lambda (obj)
273 (:documentation
274 (format "Return non-nil if OBJ is an object of type `%S'.\n\n(fn OBJ)"
275 class))
276 (and (eieio-object-p obj)
277 (same-class-p obj class))))
279 (defun eieio-make-child-predicate (class)
280 (lambda (obj)
281 (:documentation
282 (format "Return non-nil if OBJ is an object of type `%S' or a subclass.
283 \n(fn OBJ)" class))
284 (and (eieio-object-p obj)
285 (object-of-class-p obj class))))
287 (defun eieio-defclass-internal (cname superclasses slots options)
288 "Define CNAME as a new subclass of SUPERCLASSES.
289 SLOTS are the slots residing in that class definition, and OPTIONS
290 holds the class options.
291 See `defclass' for more information."
292 ;; Run our eieio-hook each time, and clear it when we are done.
293 ;; This way people can add hooks safely if they want to modify eieio
294 ;; or add definitions when eieio is loaded or something like that.
295 (run-hooks 'eieio-hook)
296 (setq eieio-hook nil)
298 (let* ((oldc (let ((c (eieio--class-v cname))) (if (eieio--class-p c) c)))
299 (newc (if (and oldc (not (eieio--class-default-object-cache oldc)))
300 ;; The oldc class is a stub setup by eieio-defclass-autoload.
301 ;; Reuse it instead of creating a new one, so that existing
302 ;; references stay valid.
303 oldc
304 (eieio--class-make cname)))
305 (groups nil) ;; list of groups id'd from slots
306 (clearparent nil))
308 ;; If this class already existed, and we are updating its structure,
309 ;; make sure we keep the old child list. This can cause bugs, but
310 ;; if no new slots are created, it also saves time, and prevents
311 ;; method table breakage, particularly when the users is only
312 ;; byte compiling an EIEIO file.
313 (if oldc
314 (setf (eieio--class-children newc) (eieio--class-children oldc))
315 ;; If the old class did not exist, but did exist in the autoload map,
316 ;; then adopt those children. This is like the above, but deals with
317 ;; autoloads nicely.
318 (let ((children (gethash cname eieio-defclass-autoload-map)))
319 (when children
320 (setf (eieio--class-children newc) children)
321 (remhash cname eieio-defclass-autoload-map))))
323 (if superclasses
324 (progn
325 (dolist (p superclasses)
326 (if (not (and p (symbolp p)))
327 (error "Invalid parent class %S" p)
328 (let ((c (eieio--class-v p)))
329 (if (not (eieio--class-p c))
330 ;; bad class
331 (error "Given parent class %S is not a class" p)
332 ;; good parent class...
333 ;; save new child in parent
334 (cl-pushnew cname (eieio--class-children c))
335 ;; Get custom groups, and store them into our local copy.
336 (mapc (lambda (g) (cl-pushnew g groups :test #'equal))
337 (eieio--class-option c :custom-groups))
338 ;; Save parent in child.
339 (push c (eieio--class-parents newc))))))
340 ;; Reverse the list of our parents so that they are prioritized in
341 ;; the same order as specified in the code.
342 (cl-callf nreverse (eieio--class-parents newc)))
343 ;; If there is nothing to loop over, then inherit from the
344 ;; default superclass.
345 (unless (eq cname 'eieio-default-superclass)
346 ;; adopt the default parent here, but clear it later...
347 (setq clearparent t)
348 ;; save new child in parent
349 (cl-pushnew cname (eieio--class-children eieio-default-superclass))
350 ;; save parent in child
351 (setf (eieio--class-parents newc) (list eieio-default-superclass))))
353 ;; turn this into a usable self-pointing symbol; FIXME: Why?
354 (when eieio-backward-compatibility
355 (set cname cname)
356 (make-obsolete-variable cname (format "use '%s instead" cname) "25.1"))
358 ;; Create a handy list of the class test too
359 (when eieio-backward-compatibility
360 (let ((csym (intern (concat (symbol-name cname) "-list-p"))))
361 (defalias csym
362 `(lambda (obj)
363 ,(format
364 "Test OBJ to see if it a list of objects which are a child of type %s"
365 cname)
366 (when (listp obj)
367 (let ((ans t)) ;; nil is valid
368 ;; Loop over all the elements of the input list, test
369 ;; each to make sure it is a child of the desired object class.
370 (while (and obj ans)
371 (setq ans (and (eieio-object-p (car obj))
372 (object-of-class-p (car obj) ,cname)))
373 (setq obj (cdr obj)))
374 ans))))
375 (make-obsolete csym (format "use (cl-typep ... '(list-of %s)) instead"
376 cname)
377 "25.1")))
379 ;; Before adding new slots, let's add all the methods and classes
380 ;; in from the parent class.
381 (eieio-copy-parents-into-subclass newc)
383 ;; Store the new class vector definition into the symbol. We need to
384 ;; do this first so that we can call defmethod for the accessor.
385 ;; The vector will be updated by the following while loop and will not
386 ;; need to be stored a second time.
387 (setf (eieio--class-v cname) newc)
389 ;; Query each slot in the declaration list and mangle into the
390 ;; class structure I have defined.
391 (pcase-dolist (`(,name . ,slot) slots)
392 (let* ((init (or (plist-get slot :initform)
393 (if (member :initform slot) nil
394 eieio-unbound)))
395 (initarg (plist-get slot :initarg))
396 (docstr (plist-get slot :documentation))
397 (prot (plist-get slot :protection))
398 (alloc (plist-get slot :allocation))
399 (type (plist-get slot :type))
400 (custom (plist-get slot :custom))
401 (label (plist-get slot :label))
402 (customg (plist-get slot :group))
403 (printer (plist-get slot :printer))
405 (skip-nil (eieio--class-option-assoc options :allow-nil-initform))
408 ;; Clean up the meaning of protection.
409 (setq prot
410 (pcase prot
411 ((or 'nil 'public ':public) nil)
412 ((or 'protected ':protected) 'protected)
413 ((or 'private ':private) 'private)
414 (_ (signal 'invalid-slot-type (list :protection prot)))))
416 ;; The default type specifier is supposed to be t, meaning anything.
417 (if (not type) (setq type t))
419 ;; intern the symbol so we can use it blankly
420 (if eieio-backward-compatibility
421 (and initarg (not (keywordp initarg))
422 (progn
423 (set initarg initarg)
424 (make-obsolete-variable
425 initarg (format "use '%s instead" initarg) "25.1"))))
427 ;; The customgroup should be a list of symbols.
428 (cond ((and (null customg) custom)
429 (setq customg '(default)))
430 ((not (listp customg))
431 (setq customg (list customg))))
432 ;; The customgroup better be a list of symbols.
433 (dolist (cg customg)
434 (unless (symbolp cg)
435 (signal 'invalid-slot-type (list :group cg))))
437 ;; First up, add this slot into our new class.
438 (eieio--add-new-slot
439 newc (cl--make-slot-descriptor
440 name init type
441 `(,@(if docstr `((:documentation . ,docstr)))
442 ,@(if custom `((:custom . ,custom)))
443 ,@(if label `((:label . ,label)))
444 ,@(if customg `((:group . ,customg)))
445 ,@(if printer `((:printer . ,printer)))
446 ,@(if prot `((:protection . ,prot)))))
447 initarg alloc 'defaultoverride skip-nil)
449 ;; We need to id the group, and store them in a group list attribute.
450 (dolist (cg customg)
451 (cl-pushnew cg groups :test #'equal))
454 ;; Now that everything has been loaded up, all our lists are backwards!
455 ;; Fix that up now and then them into vectors.
456 (cl-callf (lambda (slots) (apply #'vector (nreverse slots)))
457 (eieio--class-slots newc))
458 (cl-callf nreverse (eieio--class-initarg-tuples newc))
460 ;; The storage for class-class-allocation-type needs to be turned into
461 ;; a vector now.
462 (cl-callf (lambda (slots) (apply #'vector slots))
463 (eieio--class-class-slots newc))
465 ;; Also, setup the class allocated values.
466 (let* ((slots (eieio--class-class-slots newc))
467 (n (length slots))
468 (v (make-vector n nil)))
469 (dotimes (i n)
470 (setf (aref v i) (eieio-default-eval-maybe
471 (cl--slot-descriptor-initform (aref slots i)))))
472 (setf (eieio--class-class-allocation-values newc) v))
474 ;; Attach slot symbols into a hashtable, and store the index of
475 ;; this slot as the value this table.
476 (let* ((slots (eieio--class-slots newc))
477 ;; (cslots (eieio--class-class-slots newc))
478 (oa (make-hash-table :test #'eq)))
479 ;; (dotimes (cnt (length cslots))
480 ;; (setf (gethash (cl--slot-descriptor-name (aref cslots cnt)) oa) (- -1 cnt)))
481 (dotimes (cnt (length slots))
482 (setf (gethash (cl--slot-descriptor-name (aref slots cnt)) oa) cnt))
483 (setf (eieio--class-index-table newc) oa))
485 ;; Set up a specialized doc string.
486 ;; Use stored value since it is calculated in a non-trivial way
487 (let ((docstring (eieio--class-option-assoc options :documentation)))
488 (setf (eieio--class-docstring newc) docstring)
489 (when eieio-backward-compatibility
490 (put cname 'variable-documentation docstring)))
492 ;; Save the file location where this class is defined.
493 (add-to-list 'current-load-list `(eieio-defclass . ,cname))
495 ;; We have a list of custom groups. Store them into the options.
496 (let ((g (eieio--class-option-assoc options :custom-groups)))
497 (mapc (lambda (cg) (cl-pushnew cg g :test 'equal)) groups)
498 (if (memq :custom-groups options)
499 (setcar (cdr (memq :custom-groups options)) g)
500 (setq options (cons :custom-groups (cons g options)))))
502 ;; Set up the options we have collected.
503 (setf (eieio--class-options newc) options)
505 ;; if this is a superclass, clear out parent (which was set to the
506 ;; default superclass eieio-default-superclass)
507 (if clearparent (setf (eieio--class-parents newc) nil))
509 ;; Create the cached default object.
510 (let ((cache (make-vector (+ (length (eieio--class-slots newc))
511 (eval-when-compile eieio--object-num-slots))
512 nil))
513 ;; We don't strictly speaking need to use a symbol, but the old
514 ;; code used the class's name rather than the class's object, so
515 ;; we follow this preference for using a symbol, which is probably
516 ;; convenient to keep the printed representation of such Elisp
517 ;; objects readable.
518 (tag (intern (format "eieio-class-tag--%s" cname))))
519 (set tag newc)
520 (fset tag :quick-object-witness-check)
521 (setf (eieio--object-class-tag cache) tag)
522 (let ((eieio-skip-typecheck t))
523 ;; All type-checking has been done to our satisfaction
524 ;; before this call. Don't waste our time in this call..
525 (eieio-set-defaults cache t))
526 (setf (eieio--class-default-object-cache newc) cache))
528 ;; Return our new class object
529 ;; newc
530 cname
533 (defsubst eieio-eval-default-p (val)
534 "Whether the default value VAL should be evaluated for use."
535 (and (consp val) (symbolp (car val)) (fboundp (car val))))
537 (defun eieio--perform-slot-validation-for-default (slot skipnil)
538 "For SLOT, signal if its type does not match its default value.
539 If SKIPNIL is non-nil, then if default value is nil return t instead."
540 (let ((value (cl--slot-descriptor-initform slot))
541 (spec (cl--slot-descriptor-type slot)))
542 (if (not (or (eieio-eval-default-p value) ;FIXME: Why?
543 eieio-skip-typecheck
544 (and skipnil (null value))
545 (eieio--perform-slot-validation spec value)))
546 (signal 'invalid-slot-type (list (cl--slot-descriptor-name slot) spec value)))))
548 (defun eieio--slot-override (old new skipnil)
549 (cl-assert (eq (cl--slot-descriptor-name old) (cl--slot-descriptor-name new)))
550 ;; There is a match, and we must override the old value.
551 (let* ((a (cl--slot-descriptor-name old))
552 (tp (cl--slot-descriptor-type old))
553 (d (cl--slot-descriptor-initform new))
554 (type (cl--slot-descriptor-type new))
555 (oprops (cl--slot-descriptor-props old))
556 (nprops (cl--slot-descriptor-props new))
557 (custg (alist-get :group nprops)))
558 ;; If type is passed in, is it the same?
559 (if (not (eq type t))
560 (if (not (equal type tp))
561 (error
562 "Child slot type `%s' does not match inherited type `%s' for `%s'"
563 type tp a))
564 (setf (cl--slot-descriptor-type new) tp))
565 ;; If we have a repeat, only update the initarg...
566 (unless (eq d eieio-unbound)
567 (eieio--perform-slot-validation-for-default new skipnil)
568 (setf (cl--slot-descriptor-initform old) d))
570 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
571 ;; checked and SHOULD match the superclass
572 ;; protection. Otherwise an error is thrown. However
573 ;; I wonder if a more flexible schedule might be
574 ;; implemented.
576 ;; EML - We used to have (if prot... here,
577 ;; but a prot of 'nil means public.
579 (let ((super-prot (alist-get :protection oprops))
580 (prot (alist-get :protection nprops)))
581 (if (not (eq prot super-prot))
582 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
583 prot super-prot a)))
584 ;; End original PLN
586 ;; PLN Tue Jun 26 11:57:06 2007 :
587 ;; Do a non redundant combination of ancient custom
588 ;; groups and new ones.
589 (when custg
590 (let* ((list1 (alist-get :group oprops)))
591 (dolist (elt custg)
592 (unless (memq elt list1)
593 (push elt list1)))
594 (setf (alist-get :group (cl--slot-descriptor-props old)) list1)))
595 ;; End PLN
597 ;; PLN Mon Jun 25 22:44:34 2007 : If a new cust is
598 ;; set, simply replaces the old one.
599 (dolist (prop '(:custom :label :documentation :printer))
600 (when (alist-get prop (cl--slot-descriptor-props new))
601 (setf (alist-get prop (cl--slot-descriptor-props old))
602 (alist-get prop (cl--slot-descriptor-props new))))
604 ) ))
606 (defun eieio--add-new-slot (newc slot init alloc
607 &optional defaultoverride skipnil)
608 "Add into NEWC attribute SLOT.
609 If a slot of that name already exists in NEWC, then do nothing. If it doesn't exist,
610 INIT is the initarg, if any.
611 Argument ALLOC specifies if the slot is allocated per instance, or per class.
612 If optional DEFAULTOVERRIDE is non-nil, then if A exists in NEWC,
613 we must override its value for a default.
614 Optional argument SKIPNIL indicates if type checking should be skipped
615 if default value is nil."
616 ;; Make sure we duplicate those items that are sequences.
617 (let* ((a (cl--slot-descriptor-name slot))
618 (d (cl--slot-descriptor-initform slot))
619 (old (car (cl-member a (eieio--class-slots newc)
620 :key #'cl--slot-descriptor-name)))
621 (cold (car (cl-member a (eieio--class-class-slots newc)
622 :key #'cl--slot-descriptor-name))))
623 (condition-case nil
624 (if (sequencep d) (setq d (copy-sequence d)))
625 ;; This copy can fail on a cons cell with a non-cons in the cdr. Let's
626 ;; skip it if it doesn't work.
627 (error nil))
628 ;; (if (sequencep type) (setq type (copy-sequence type)))
629 ;; (if (sequencep cust) (setq cust (copy-sequence cust)))
630 ;; (if (sequencep custg) (setq custg (copy-sequence custg)))
632 ;; To prevent override information w/out specification of storage,
633 ;; we need to do this little hack.
634 (if cold (setq alloc :class))
636 (if (memq alloc '(nil :instance))
637 ;; In this case, we modify the INSTANCE version of a given slot.
638 (progn
639 ;; Only add this element if it is so-far unique
640 (if (not old)
641 (progn
642 (eieio--perform-slot-validation-for-default slot skipnil)
643 (push slot (eieio--class-slots newc))
645 ;; When defaultoverride is true, we are usually adding new local
646 ;; attributes which must override the default value of any slot
647 ;; passed in by one of the parent classes.
648 (when defaultoverride
649 (eieio--slot-override old slot skipnil)))
650 (when init
651 (cl-pushnew (cons init a) (eieio--class-initarg-tuples newc)
652 :test #'equal)))
654 ;; CLASS ALLOCATED SLOTS
655 (if (not cold)
656 (progn
657 (eieio--perform-slot-validation-for-default slot skipnil)
658 ;; Here we have found a :class version of a slot. This
659 ;; requires a very different approach.
660 (push slot (eieio--class-class-slots newc)))
661 (when defaultoverride
662 ;; There is a match, and we must override the old value.
663 (eieio--slot-override cold slot skipnil))))))
665 (defun eieio-copy-parents-into-subclass (newc)
666 "Copy into NEWC the slots of PARENTS.
667 Follow the rules of not overwriting early parents when applying to
668 the new child class."
669 (let ((sn (eieio--class-option-assoc (eieio--class-options newc)
670 :allow-nil-initform)))
671 (dolist (pcv (eieio--class-parents newc))
672 ;; First, duplicate all the slots of the parent.
673 (let ((pslots (eieio--class-slots pcv))
674 (pinit (eieio--class-initarg-tuples pcv)))
675 (dotimes (i (length pslots))
676 (eieio--add-new-slot newc (cl--copy-slot-descriptor (aref pslots i))
677 (car-safe (car pinit)) nil nil sn)
678 ;; Increment each value.
679 (setq pinit (cdr pinit))
680 )) ;; while/let
681 ;; Now duplicate all the class alloc slots.
682 (let ((pcslots (eieio--class-class-slots pcv)))
683 (dotimes (i (length pcslots))
684 (eieio--add-new-slot newc (cl--copy-slot-descriptor
685 (aref pcslots i))
686 nil :class sn)
687 )))))
690 ;;; Slot type validation
692 ;; This is a hideous hack for replacing `typep' from cl-macs, to avoid
693 ;; requiring the CL library at run-time. It can be eliminated if/when
694 ;; `typep' is merged into Emacs core.
696 (defun eieio--perform-slot-validation (spec value)
697 "Return non-nil if SPEC does not match VALUE."
698 (or (eq spec t) ; t always passes
699 (eq value eieio-unbound) ; unbound always passes
700 (cl-typep value spec)))
702 (defun eieio--validate-slot-value (class slot-idx value slot)
703 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
704 Checks the :type specifier.
705 SLOT is the slot that is being checked, and is only used when throwing
706 an error."
707 (if eieio-skip-typecheck
709 ;; Trim off object IDX junk added in for the object index.
710 (setq slot-idx (- slot-idx (eval-when-compile eieio--object-num-slots)))
711 (let ((st (cl--slot-descriptor-type (aref (eieio--class-slots class)
712 slot-idx))))
713 (if (not (eieio--perform-slot-validation st value))
714 (signal 'invalid-slot-type
715 (list (eieio--class-name class) slot st value))))))
717 (defun eieio--validate-class-slot-value (class slot-idx value slot)
718 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
719 Checks the :type specifier.
720 SLOT is the slot that is being checked, and is only used when throwing
721 an error."
722 (if eieio-skip-typecheck
724 (let ((st (cl--slot-descriptor-type (aref (eieio--class-class-slots class)
725 slot-idx))))
726 (if (not (eieio--perform-slot-validation st value))
727 (signal 'invalid-slot-type
728 (list (eieio--class-name class) slot st value))))))
730 (defun eieio-barf-if-slot-unbound (value instance slotname fn)
731 "Throw a signal if VALUE is a representation of an UNBOUND slot.
732 INSTANCE is the object being referenced. SLOTNAME is the offending
733 slot. If the slot is ok, return VALUE.
734 Argument FN is the function calling this verifier."
735 (if (and (eq value eieio-unbound) (not eieio-skip-typecheck))
736 (slot-unbound instance (eieio--object-class instance) slotname fn)
737 value))
740 ;;; Get/Set slots in an object.
742 (defun eieio-oref (obj slot)
743 "Return the value in OBJ at SLOT in the object vector."
744 (cl-check-type slot symbol)
745 (cl-check-type obj (or eieio-object class))
746 (let* ((class (cond ((symbolp obj)
747 (error "eieio-oref called on a class!")
748 (let ((c (eieio--class-v obj)))
749 (if (eieio--class-p c) (eieio-class-un-autoload obj))
751 (t (eieio--object-class obj))))
752 (c (eieio--slot-name-index class slot)))
753 (if (not c)
754 ;; It might be missing because it is a :class allocated slot.
755 ;; Let's check that info out.
756 (if (setq c (eieio--class-slot-name-index class slot))
757 ;; Oref that slot.
758 (aref (eieio--class-class-allocation-values class) c)
759 ;; The slot-missing method is a cool way of allowing an object author
760 ;; to intercept missing slot definitions. Since it is also the LAST
761 ;; thing called in this fn, its return value would be retrieved.
762 (slot-missing obj slot 'oref)
763 ;;(signal 'invalid-slot-name (list (eieio-object-name obj) slot))
765 (cl-check-type obj eieio-object)
766 (eieio-barf-if-slot-unbound (aref obj c) obj slot 'oref))))
769 (defun eieio-oref-default (obj slot)
770 "Do the work for the macro `oref-default' with similar parameters.
771 Fills in OBJ's SLOT with its default value."
772 (cl-check-type obj (or eieio-object class))
773 (cl-check-type slot symbol)
774 (let* ((cl (cond ((symbolp obj) (eieio--class-v obj))
775 (t (eieio--object-class obj))))
776 (c (eieio--slot-name-index cl slot)))
777 (if (not c)
778 ;; It might be missing because it is a :class allocated slot.
779 ;; Let's check that info out.
780 (if (setq c
781 (eieio--class-slot-name-index cl slot))
782 ;; Oref that slot.
783 (aref (eieio--class-class-allocation-values cl)
785 (slot-missing obj slot 'oref-default)
786 ;;(signal 'invalid-slot-name (list (class-name cl) slot))
788 (eieio-barf-if-slot-unbound
789 (let ((val (cl--slot-descriptor-initform
790 (aref (eieio--class-slots cl)
791 (- c (eval-when-compile eieio--object-num-slots))))))
792 (eieio-default-eval-maybe val))
793 obj (eieio--class-name cl) 'oref-default))))
795 (defun eieio-default-eval-maybe (val)
796 "Check VAL, and return what `oref-default' would provide."
797 ;; FIXME: What the hell is this supposed to do? Shouldn't it evaluate
798 ;; variables as well? Why not just always call `eval'?
799 (cond
800 ;; Is it a function call? If so, evaluate it.
801 ((eieio-eval-default-p val)
802 (eval val))
803 ;;;; check for quoted things, and unquote them
804 ;;((and (consp val) (eq (car val) 'quote))
805 ;; (car (cdr val)))
806 ;; return it verbatim
807 (t val)))
809 (defun eieio-oset (obj slot value)
810 "Do the work for the macro `oset'.
811 Fills in OBJ's SLOT with VALUE."
812 (cl-check-type obj eieio-object)
813 (cl-check-type slot symbol)
814 (let* ((class (eieio--object-class obj))
815 (c (eieio--slot-name-index class slot)))
816 (if (not c)
817 ;; It might be missing because it is a :class allocated slot.
818 ;; Let's check that info out.
819 (if (setq c
820 (eieio--class-slot-name-index class slot))
821 ;; Oset that slot.
822 (progn
823 (eieio--validate-class-slot-value class c value slot)
824 (aset (eieio--class-class-allocation-values class)
825 c value))
826 ;; See oref for comment on `slot-missing'
827 (slot-missing obj slot 'oset value)
828 ;;(signal 'invalid-slot-name (list (eieio-object-name obj) slot))
830 (eieio--validate-slot-value class c value slot)
831 (aset obj c value))))
833 (defun eieio-oset-default (class slot value)
834 "Do the work for the macro `oset-default'.
835 Fills in the default value in CLASS' in SLOT with VALUE."
836 (setq class (eieio--class-object class))
837 (cl-check-type class eieio--class)
838 (cl-check-type slot symbol)
839 (let* ((c (eieio--slot-name-index class slot)))
840 (if (not c)
841 ;; It might be missing because it is a :class allocated slot.
842 ;; Let's check that info out.
843 (if (setq c (eieio--class-slot-name-index class slot))
844 (progn
845 ;; Oref that slot.
846 (eieio--validate-class-slot-value class c value slot)
847 (aset (eieio--class-class-allocation-values class) c
848 value))
849 (signal 'invalid-slot-name (list (eieio--class-name class) slot)))
850 ;; `oset-default' on an instance-allocated slot is allowed by EIEIO but
851 ;; not by CLOS and is mildly inconsistent with the :initform thingy, so
852 ;; it'd be nice to get of it. This said, it is/was used at one place by
853 ;; gnus/registry.el, so it might be used elsewhere as well, so let's
854 ;; keep it for now.
855 ;; FIXME: Generate a compile-time warning for it!
856 ;; (error "Can't `oset-default' an instance-allocated slot: %S of %S"
857 ;; slot class)
858 (eieio--validate-slot-value class c value slot)
859 ;; Set this into the storage for defaults.
860 (if (eieio-eval-default-p value)
861 (error "Can't set default to a sexp that gets evaluated again"))
862 (setf (cl--slot-descriptor-initform
863 ;; FIXME: Apparently we set it both in `slots' and in
864 ;; `object-cache', which seems redundant.
865 (aref (eieio--class-slots class)
866 (- c (eval-when-compile eieio--object-num-slots))))
867 value)
868 ;; Take the value, and put it into our cache object.
869 (eieio-oset (eieio--class-default-object-cache class)
870 slot value)
874 ;;; EIEIO internal search functions
876 (defun eieio--slot-name-index (class slot)
877 "In CLASS find the index of the named SLOT.
878 The slot is a symbol which is installed in CLASS by the `defclass' call.
879 If SLOT is the value created with :initarg instead,
880 reverse-lookup that name, and recurse with the associated slot value."
881 ;; Removed checks to outside this call
882 (let* ((fsi (gethash slot (eieio--class-index-table class))))
883 (if (integerp fsi)
884 (+ (eval-when-compile eieio--object-num-slots) fsi)
885 (let ((fn (eieio--initarg-to-attribute class slot)))
886 (if fn
887 ;; Accessing a slot via its :initarg is accepted by EIEIO
888 ;; (but not CLOS) but is a bad idea (for one: it's slower).
889 ;; FIXME: We should emit a compile-time warning when this happens!
890 (eieio--slot-name-index class fn)
891 nil)))))
893 (defun eieio--class-slot-name-index (class slot)
894 "In CLASS find the index of the named SLOT.
895 The slot is a symbol which is installed in CLASS by the `defclass'
896 call. If SLOT is the value created with :initarg instead,
897 reverse-lookup that name, and recurse with the associated slot value."
898 ;; This will happen less often, and with fewer slots. Do this the
899 ;; storage cheap way.
900 (let ((index nil)
901 (slots (eieio--class-class-slots class)))
902 (dotimes (i (length slots))
903 (if (eq slot (cl--slot-descriptor-name (aref slots i)))
904 (setq index i)))
905 index))
908 ;; Way to assign slots based on a list. Used for constructors, or
909 ;; even resetting an object at run-time
911 (defun eieio-set-defaults (obj &optional set-all)
912 "Take object OBJ, and reset all slots to their defaults.
913 If SET-ALL is non-nil, then when a default is nil, that value is
914 reset. If SET-ALL is nil, the slots are only reset if the default is
915 not nil."
916 (let ((slots (eieio--class-slots (eieio--object-class obj))))
917 (dotimes (i (length slots))
918 (let* ((name (cl--slot-descriptor-name (aref slots i)))
919 (df (eieio-oref-default obj name)))
920 (if (or df set-all)
921 (eieio-oset obj name df))))))
923 (defun eieio--initarg-to-attribute (class initarg)
924 "For CLASS, convert INITARG to the actual attribute name.
925 If there is no translation, pass it in directly (so we can cheat if
926 need be... May remove that later...)"
927 (let ((tuple (assoc initarg (eieio--class-initarg-tuples class))))
928 (if tuple
929 (cdr tuple)
930 nil)))
933 ;; Method Invocation order: C3
934 (defun eieio--c3-candidate (class remaining-inputs)
935 "Return CLASS if it can go in the result now, otherwise nil."
936 ;; Ensure CLASS is not in any position but the first in any of the
937 ;; element lists of REMAINING-INPUTS.
938 (and (not (let ((found nil))
939 (while (and remaining-inputs (not found))
940 (setq found (member class (cdr (car remaining-inputs)))
941 remaining-inputs (cdr remaining-inputs)))
942 found))
943 class))
945 (defun eieio--c3-merge-lists (reversed-partial-result remaining-inputs)
946 "Merge REVERSED-PARTIAL-RESULT REMAINING-INPUTS in a consistent order, if possible.
947 If a consistent order does not exist, signal an error."
948 (setq remaining-inputs (delq nil remaining-inputs))
949 (if (null remaining-inputs)
950 ;; If all remaining inputs are empty lists, we are done.
951 (nreverse reversed-partial-result)
952 ;; Otherwise, we try to find the next element of the result. This
953 ;; is achieved by considering the first element of each
954 ;; (non-empty) input list and accepting a candidate if it is
955 ;; consistent with the rests of the input lists.
956 (let* ((found nil)
957 (tail remaining-inputs)
958 (next (progn
959 (while (and tail (not found))
960 (setq found (eieio--c3-candidate (caar tail)
961 remaining-inputs)
962 tail (cdr tail)))
963 found)))
964 (if next
965 ;; The graph is consistent so far, add NEXT to result and
966 ;; merge input lists, dropping NEXT from their heads where
967 ;; applicable.
968 (eieio--c3-merge-lists
969 (cons next reversed-partial-result)
970 (mapcar (lambda (l) (if (eq (cl-first l) next) (cl-rest l) l))
971 remaining-inputs))
972 ;; The graph is inconsistent, give up
973 (signal 'inconsistent-class-hierarchy (list remaining-inputs))))))
975 (defsubst eieio--class/struct-parents (class)
976 (or (eieio--class-parents class)
977 `(,eieio-default-superclass)))
979 (defun eieio--class-precedence-c3 (class)
980 "Return all parents of CLASS in c3 order."
981 (let ((parents (eieio--class-parents (eieio--class-v class))))
982 (eieio--c3-merge-lists
983 (list class)
984 (append
986 (mapcar #'eieio--class-precedence-c3 parents)
987 `((,eieio-default-superclass)))
988 (list parents))))
991 ;; Method Invocation Order: Depth First
993 (defun eieio--class-precedence-dfs (class)
994 "Return all parents of CLASS in depth-first order."
995 (let* ((parents (eieio--class-parents class))
996 (classes (copy-sequence
997 (apply #'append
998 (list class)
1000 (mapcar
1001 (lambda (parent)
1002 (cons parent
1003 (eieio--class-precedence-dfs parent)))
1004 parents)
1005 `((,eieio-default-superclass))))))
1006 (tail classes))
1007 ;; Remove duplicates.
1008 (while tail
1009 (setcdr tail (delq (car tail) (cdr tail)))
1010 (setq tail (cdr tail)))
1011 classes))
1014 ;; Method Invocation Order: Breadth First
1015 (defun eieio--class-precedence-bfs (class)
1016 "Return all parents of CLASS in breadth-first order."
1017 (let* ((result)
1018 (queue (eieio--class/struct-parents class)))
1019 (while queue
1020 (let ((head (pop queue)))
1021 (unless (member head result)
1022 (push head result)
1023 (unless (eq head eieio-default-superclass)
1024 (setq queue (append queue (eieio--class/struct-parents head)))))))
1025 (cons class (nreverse result)))
1029 ;; Method Invocation Order
1031 (defun eieio--class-precedence-list (class)
1032 "Return (transitively closed) list of parents of CLASS.
1033 The order, in which the parents are returned depends on the
1034 method invocation orders of the involved classes."
1035 (if (or (null class) (eq class eieio-default-superclass))
1037 (unless (eieio--class-default-object-cache class)
1038 (eieio-class-un-autoload (eieio--class-name class)))
1039 (cl-case (eieio--class-method-invocation-order class)
1040 (:depth-first
1041 (eieio--class-precedence-dfs class))
1042 (:breadth-first
1043 (eieio--class-precedence-bfs class))
1044 (:c3
1045 (eieio--class-precedence-c3 class))))
1047 (define-obsolete-function-alias
1048 'class-precedence-list 'eieio--class-precedence-list "24.4")
1051 ;;; Here are some special types of errors
1053 (define-error 'invalid-slot-name "Invalid slot name")
1054 (define-error 'invalid-slot-type "Invalid slot type")
1055 (define-error 'unbound-slot "Unbound slot")
1056 (define-error 'inconsistent-class-hierarchy "Inconsistent class hierarchy")
1058 ;;; Hooking into cl-generic.
1060 (require 'cl-generic)
1062 ;;;; General support to dispatch based on the type of the argument.
1064 (defconst eieio--generic-generalizer
1065 (cl-generic-make-generalizer
1066 ;; Use the exact same tagcode as for cl-struct, so that methods
1067 ;; that dispatch on both kinds of objects get to share this
1068 ;; part of the dispatch code.
1069 50 #'cl--generic-struct-tag
1070 (lambda (tag)
1071 (and (symbolp tag) (boundp tag) (eieio--class-p (symbol-value tag))
1072 (mapcar #'eieio--class-name
1073 (eieio--class-precedence-list (symbol-value tag)))))))
1075 (cl-defmethod cl-generic-generalizers :extra "class" (specializer)
1076 ;; CLHS says:
1077 ;; A class must be defined before it can be used as a parameter
1078 ;; specializer in a defmethod form.
1079 ;; So we can ignore types that are not known to denote classes.
1081 (and (eieio--class-p (eieio--class-object specializer))
1082 (list eieio--generic-generalizer))
1083 (cl-call-next-method)))
1085 ;;;; Dispatch for arguments which are classes.
1087 ;; Since EIEIO does not support metaclasses, users can't easily use the
1088 ;; "dispatch on argument type" for class arguments. That's why EIEIO's
1089 ;; `defmethod' added the :static qualifier. For cl-generic, such a qualifier
1090 ;; would not make much sense (e.g. to which argument should it apply?).
1091 ;; Instead, we add a new "subclass" specializer.
1093 (defun eieio--generic-subclass-specializers (tag)
1094 (when (eieio--class-p tag)
1095 (mapcar (lambda (class)
1096 `(subclass ,(eieio--class-name class)))
1097 (eieio--class-precedence-list tag))))
1099 (defconst eieio--generic-subclass-generalizer
1100 (cl-generic-make-generalizer
1101 60 (lambda (name) `(and (symbolp ,name) (eieio--class-v ,name)))
1102 #'eieio--generic-subclass-specializers))
1104 (cl-defmethod cl-generic-generalizers ((_specializer (head subclass)))
1105 (list eieio--generic-subclass-generalizer))
1108 ;;;### (autoloads nil "eieio-compat" "eieio-compat.el" "0609a7bdcd6f38876b7f5647047ddca9")
1109 ;;; Generated autoloads from eieio-compat.el
1111 (autoload 'eieio--defalias "eieio-compat" "\
1112 Like `defalias', but with less side-effects.
1113 More specifically, it has no side-effects at all when the new function
1114 definition is the same (`eq') as the old one.
1116 \(fn NAME BODY)" nil nil)
1118 (autoload 'defgeneric "eieio-compat" "\
1119 Create a generic function METHOD.
1120 DOC-STRING is the base documentation for this class. A generic
1121 function has no body, as its purpose is to decide which method body
1122 is appropriate to use. Uses `defmethod' to create methods, and calls
1123 `defgeneric' for you. With this implementation the ARGS are
1124 currently ignored. You can use `defgeneric' to apply specialized
1125 top level documentation to a method.
1127 \(fn METHOD ARGS &optional DOC-STRING)" nil t)
1129 (function-put 'defgeneric 'doc-string-elt '3)
1131 (make-obsolete 'defgeneric 'cl-defgeneric '"25.1")
1133 (autoload 'defmethod "eieio-compat" "\
1134 Create a new METHOD through `defgeneric' with ARGS.
1136 The optional second argument KEY is a specifier that
1137 modifies how the method is called, including:
1138 :before - Method will be called before the :primary
1139 :primary - The default if not specified
1140 :after - Method will be called after the :primary
1141 :static - First arg could be an object or class
1142 The next argument is the ARGLIST. The ARGLIST specifies the arguments
1143 to the method as with `defun'. The first argument can have a type
1144 specifier, such as:
1145 ((VARNAME CLASS) ARG2 ...)
1146 where VARNAME is the name of the local variable for the method being
1147 created. The CLASS is a class symbol for a class made with `defclass'.
1148 A DOCSTRING comes after the ARGLIST, and is optional.
1149 All the rest of the args are the BODY of the method. A method will
1150 return the value of the last form in the BODY.
1152 Summary:
1154 (defmethod mymethod [:before | :primary | :after | :static]
1155 ((typearg class-name) arg2 &optional opt &rest rest)
1156 \"doc-string\"
1157 body)
1159 \(fn METHOD &rest ARGS)" nil t)
1161 (function-put 'defmethod 'doc-string-elt '3)
1163 (make-obsolete 'defmethod 'cl-defmethod '"25.1")
1165 (autoload 'eieio--defgeneric-init-form "eieio-compat" "\
1168 \(fn METHOD DOC-STRING)" nil nil)
1170 (autoload 'eieio--defmethod "eieio-compat" "\
1173 \(fn METHOD KIND ARGCLASS CODE)" nil nil)
1175 (autoload 'eieio-defmethod "eieio-compat" "\
1176 Obsolete work part of an old version of the `defmethod' macro.
1178 \(fn METHOD ARGS)" nil nil)
1180 (make-obsolete 'eieio-defmethod 'cl-defmethod '"24.1")
1182 (autoload 'eieio-defgeneric "eieio-compat" "\
1183 Obsolete work part of an old version of the `defgeneric' macro.
1185 \(fn METHOD DOC-STRING)" nil nil)
1187 (make-obsolete 'eieio-defgeneric 'cl-defgeneric '"24.1")
1189 (autoload 'eieio-defclass "eieio-compat" "\
1192 \(fn CNAME SUPERCLASSES SLOTS OPTIONS)" nil nil)
1194 (make-obsolete 'eieio-defclass 'eieio-defclass-internal '"25.1")
1196 ;;;***
1199 (provide 'eieio-core)
1201 ;;; eieio-core.el ends here