* lisp/mouse.el (minor-mode-menu-from-indicator):
[emacs.git] / lisp / emacs-lisp / eieio-core.el
blob59d834837b05c6fb5c018ebd239318c872226a85
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 (:include cl--class)
93 (:copier nil))
94 children
95 initarg-tuples ;; initarg tuples list
96 (class-slots nil :type eieio--slot)
97 class-allocation-values ;; class allocated value vector
98 default-object-cache ;; what a newly created object would look like.
99 ; This will speed up instantiation time as
100 ; only a `copy-sequence' will be needed, instead of
101 ; looping over all the values and setting them from
102 ; the default.
103 options ;; storage location of tagged class option
104 ; Stored outright without modifications or stripping
106 ;; Set it back to the default value.
107 (cl-declaim (optimize (safety 1))))
110 (cl-defstruct (eieio--object
111 (:type vector) ;We manage our own tagging system.
112 (:constructor nil)
113 (:copier nil))
114 ;; `class-tag' holds a symbol, which is not the class name, but is instead
115 ;; properly prefixed as an internal EIEIO thingy and which holds the class
116 ;; object/struct in its `symbol-value' slot.
117 class-tag)
119 (eval-when-compile
120 (defconst eieio--object-num-slots
121 (length (cl-struct-slot-info 'eieio--object))))
123 (defsubst eieio--object-class (obj)
124 (symbol-value (eieio--object-class-tag obj)))
127 ;;; Important macros used internally in eieio.
129 (defmacro eieio--class-v (class) ;Use a macro, so it acts as a GV place.
130 "Internal: Return the class vector from the CLASS symbol."
131 (declare (debug t))
132 ;; No check: If eieio gets this far, it has probably been checked already.
133 `(get ,class 'eieio-class-definition))
135 (defsubst eieio--class-object (class)
136 "Return the class object."
137 (if (symbolp class)
138 ;; Keep the symbol if class-v is nil, for better error messages.
139 (or (eieio--class-v class) class)
140 class))
142 (defun class-p (class)
143 "Return non-nil if CLASS is a valid class vector.
144 CLASS is a symbol." ;FIXME: Is it a vector or a symbol?
145 (and (symbolp class) (eieio--class-p (eieio--class-v class))))
147 (defun eieio--class-print-name (class)
148 "Return a printed representation of CLASS."
149 (format "#<class %s>" (eieio-class-name class)))
151 (defun eieio-class-name (class)
152 "Return a Lisp like symbol name for CLASS."
153 (setq class (eieio--class-object class))
154 (cl-check-type class eieio--class)
155 (eieio--class-name class))
156 (define-obsolete-function-alias 'class-name #'eieio-class-name "24.4")
158 (defalias 'eieio--class-constructor #'identity
159 "Return the symbol representing the constructor of CLASS.")
161 (defmacro eieio--class-option-assoc (list option)
162 "Return from LIST the found OPTION, or nil if it doesn't exist."
163 `(car-safe (cdr (memq ,option ,list))))
165 (defsubst eieio--class-option (class option)
166 "Return the value stored for CLASS' OPTION.
167 Return nil if that option doesn't exist."
168 (eieio--class-option-assoc (eieio--class-options class) option))
170 (defun eieio-object-p (obj)
171 "Return non-nil if OBJ is an EIEIO object."
172 (and (vectorp obj)
173 (> (length obj) 0)
174 (let ((tag (eieio--object-class-tag obj)))
175 (and (symbolp tag)
176 ;; (eq (symbol-function tag) :quick-object-witness-check)
177 (boundp tag)
178 (eieio--class-p (symbol-value tag))))))
180 (define-obsolete-function-alias 'object-p 'eieio-object-p "25.1")
182 (defun class-abstract-p (class)
183 "Return non-nil if CLASS is abstract.
184 Abstract classes cannot be instantiated."
185 (eieio--class-option (eieio--class-v class) :abstract))
187 (defsubst eieio--class-method-invocation-order (class)
188 "Return the invocation order of CLASS.
189 Abstract classes cannot be instantiated."
190 (or (eieio--class-option class :method-invocation-order)
191 :breadth-first))
196 ;; Class Creation
198 (defvar eieio-defclass-autoload-map (make-hash-table)
199 "Symbol map of superclasses we find in autoloads.")
201 ;; We autoload this because it's used in `make-autoload'.
202 ;;;###autoload
203 (defun eieio-defclass-autoload (cname _superclasses filename doc)
204 "Create autoload symbols for the EIEIO class CNAME.
205 SUPERCLASSES are the superclasses that CNAME inherits from.
206 DOC is the docstring for CNAME.
207 This function creates a mock-class for CNAME and adds it into
208 SUPERCLASSES as children.
209 It creates an autoload function for CNAME's constructor."
210 ;; Assume we've already debugged inputs.
212 ;; We used to store the list of superclasses in the `parent' slot (as a list
213 ;; of class names). But now this slot holds a list of class objects, and
214 ;; those parents may not exist yet, so the corresponding class objects may
215 ;; simply not exist yet. So instead we just don't store the list of parents
216 ;; here in eieio-defclass-autoload at all, since it seems that they're just
217 ;; not needed before the class is actually loaded.
218 (let* ((oldc (eieio--class-v cname))
219 (newc (eieio--class-make cname)))
220 (if (eieio--class-p oldc)
221 nil ;; Do nothing if we already have this class.
223 ;; turn this into a usable self-pointing symbol
224 (when eieio-backward-compatibility
225 (set cname cname)
226 (make-obsolete-variable cname (format "use '%s instead" cname) "25.1"))
228 ;; Store the new class vector definition into the symbol. We need to
229 ;; do this first so that we can call defmethod for the accessor.
230 ;; The vector will be updated by the following while loop and will not
231 ;; need to be stored a second time.
232 (setf (eieio--class-v cname) newc)
234 ;; Create an autoload on top of our constructor function.
235 (autoload cname filename doc nil nil)
236 (autoload (intern (format "%s-p" cname)) filename "" nil nil)
237 (when eieio-backward-compatibility
238 (autoload (intern (format "%s-child-p" cname)) filename "" nil nil)
239 (autoload (intern (format "%s-list-p" cname)) filename "" nil nil)))))
241 (defsubst eieio-class-un-autoload (cname)
242 "If class CNAME is in an autoload state, load its file."
243 (autoload-do-load (symbol-function cname))) ; cname
245 (cl-deftype list-of (elem-type)
246 `(and list
247 (satisfies (lambda (list)
248 (cl-every (lambda (elem) (cl-typep elem ',elem-type))
249 list)))))
252 (defun eieio-make-class-predicate (class)
253 (lambda (obj)
254 (:documentation
255 (format "Return non-nil if OBJ is an object of type `%S'.\n\n(fn OBJ)"
256 class))
257 (and (eieio-object-p obj)
258 (same-class-p obj class))))
260 (defun eieio-make-child-predicate (class)
261 (lambda (obj)
262 (:documentation
263 (format "Return non-nil if OBJ is an object of type `%S' or a subclass.
264 \n(fn OBJ)" class))
265 (and (eieio-object-p obj)
266 (object-of-class-p obj class))))
268 (defun eieio-defclass-internal (cname superclasses slots options)
269 "Define CNAME as a new subclass of SUPERCLASSES.
270 SLOTS are the slots residing in that class definition, and OPTIONS
271 holds the class options.
272 See `defclass' for more information."
273 ;; Run our eieio-hook each time, and clear it when we are done.
274 ;; This way people can add hooks safely if they want to modify eieio
275 ;; or add definitions when eieio is loaded or something like that.
276 (run-hooks 'eieio-hook)
277 (setq eieio-hook nil)
279 (let* ((oldc (let ((c (eieio--class-v cname))) (if (eieio--class-p c) c)))
280 (newc (if (and oldc (not (eieio--class-default-object-cache oldc)))
281 ;; The oldc class is a stub setup by eieio-defclass-autoload.
282 ;; Reuse it instead of creating a new one, so that existing
283 ;; references stay valid.
284 oldc
285 (eieio--class-make cname)))
286 (groups nil) ;; list of groups id'd from slots
287 (clearparent nil))
289 ;; If this class already existed, and we are updating its structure,
290 ;; make sure we keep the old child list. This can cause bugs, but
291 ;; if no new slots are created, it also saves time, and prevents
292 ;; method table breakage, particularly when the users is only
293 ;; byte compiling an EIEIO file.
294 (if oldc
295 (setf (eieio--class-children newc) (eieio--class-children oldc))
296 ;; If the old class did not exist, but did exist in the autoload map,
297 ;; then adopt those children. This is like the above, but deals with
298 ;; autoloads nicely.
299 (let ((children (gethash cname eieio-defclass-autoload-map)))
300 (when children
301 (setf (eieio--class-children newc) children)
302 (remhash cname eieio-defclass-autoload-map))))
304 (if superclasses
305 (progn
306 (dolist (p superclasses)
307 (if (not (and p (symbolp p)))
308 (error "Invalid parent class %S" p)
309 (let ((c (eieio--class-v p)))
310 (if (not (eieio--class-p c))
311 ;; bad class
312 (error "Given parent class %S is not a class" p)
313 ;; good parent class...
314 ;; save new child in parent
315 (cl-pushnew cname (eieio--class-children c))
316 ;; Get custom groups, and store them into our local copy.
317 (mapc (lambda (g) (cl-pushnew g groups :test #'equal))
318 (eieio--class-option c :custom-groups))
319 ;; Save parent in child.
320 (push c (eieio--class-parents newc))))))
321 ;; Reverse the list of our parents so that they are prioritized in
322 ;; the same order as specified in the code.
323 (cl-callf nreverse (eieio--class-parents newc)))
324 ;; If there is nothing to loop over, then inherit from the
325 ;; default superclass.
326 (unless (eq cname 'eieio-default-superclass)
327 ;; adopt the default parent here, but clear it later...
328 (setq clearparent t)
329 ;; save new child in parent
330 (cl-pushnew cname (eieio--class-children eieio-default-superclass))
331 ;; save parent in child
332 (setf (eieio--class-parents newc) (list eieio-default-superclass))))
334 ;; turn this into a usable self-pointing symbol; FIXME: Why?
335 (when eieio-backward-compatibility
336 (set cname cname)
337 (make-obsolete-variable cname (format "use '%s instead" cname) "25.1"))
339 ;; Create a handy list of the class test too
340 (when eieio-backward-compatibility
341 (let ((csym (intern (concat (symbol-name cname) "-list-p"))))
342 (defalias csym
343 `(lambda (obj)
344 ,(format
345 "Test OBJ to see if it a list of objects which are a child of type %s"
346 cname)
347 (when (listp obj)
348 (let ((ans t)) ;; nil is valid
349 ;; Loop over all the elements of the input list, test
350 ;; each to make sure it is a child of the desired object class.
351 (while (and obj ans)
352 (setq ans (and (eieio-object-p (car obj))
353 (object-of-class-p (car obj) ,cname)))
354 (setq obj (cdr obj)))
355 ans))))
356 (make-obsolete csym (format "use (cl-typep ... '(list-of %s)) instead"
357 cname)
358 "25.1")))
360 ;; Before adding new slots, let's add all the methods and classes
361 ;; in from the parent class.
362 (eieio-copy-parents-into-subclass newc)
364 ;; Store the new class vector definition into the symbol. We need to
365 ;; do this first so that we can call defmethod for the accessor.
366 ;; The vector will be updated by the following while loop and will not
367 ;; need to be stored a second time.
368 (setf (eieio--class-v cname) newc)
370 ;; Query each slot in the declaration list and mangle into the
371 ;; class structure I have defined.
372 (pcase-dolist (`(,name . ,slot) slots)
373 (let* ((init (or (plist-get slot :initform)
374 (if (member :initform slot) nil
375 eieio-unbound)))
376 (initarg (plist-get slot :initarg))
377 (docstr (plist-get slot :documentation))
378 (prot (plist-get slot :protection))
379 (alloc (plist-get slot :allocation))
380 (type (plist-get slot :type))
381 (custom (plist-get slot :custom))
382 (label (plist-get slot :label))
383 (customg (plist-get slot :group))
384 (printer (plist-get slot :printer))
386 (skip-nil (eieio--class-option-assoc options :allow-nil-initform))
389 ;; Clean up the meaning of protection.
390 (setq prot
391 (pcase prot
392 ((or 'nil 'public ':public) nil)
393 ((or 'protected ':protected) 'protected)
394 ((or 'private ':private) 'private)
395 (_ (signal 'invalid-slot-type (list :protection prot)))))
397 ;; The default type specifier is supposed to be t, meaning anything.
398 (if (not type) (setq type t))
400 ;; intern the symbol so we can use it blankly
401 (if eieio-backward-compatibility
402 (and initarg (not (keywordp initarg))
403 (progn
404 (set initarg initarg)
405 (make-obsolete-variable
406 initarg (format "use '%s instead" initarg) "25.1"))))
408 ;; The customgroup should be a list of symbols.
409 (cond ((and (null customg) custom)
410 (setq customg '(default)))
411 ((not (listp customg))
412 (setq customg (list customg))))
413 ;; The customgroup better be a list of symbols.
414 (dolist (cg customg)
415 (unless (symbolp cg)
416 (signal 'invalid-slot-type (list :group cg))))
418 ;; First up, add this slot into our new class.
419 (eieio--add-new-slot
420 newc (cl--make-slot-descriptor
421 name init type
422 `(,@(if docstr `((:documentation . ,docstr)))
423 ,@(if custom `((:custom . ,custom)))
424 ,@(if label `((:label . ,label)))
425 ,@(if customg `((:group . ,customg)))
426 ,@(if printer `((:printer . ,printer)))
427 ,@(if prot `((:protection . ,prot)))))
428 initarg alloc 'defaultoverride skip-nil)
430 ;; We need to id the group, and store them in a group list attribute.
431 (dolist (cg customg)
432 (cl-pushnew cg groups :test #'equal))
435 ;; Now that everything has been loaded up, all our lists are backwards!
436 ;; Fix that up now and then them into vectors.
437 (cl-callf (lambda (slots) (apply #'vector (nreverse slots)))
438 (eieio--class-slots newc))
439 (cl-callf nreverse (eieio--class-initarg-tuples newc))
441 ;; The storage for class-class-allocation-type needs to be turned into
442 ;; a vector now.
443 (cl-callf (lambda (slots) (apply #'vector slots))
444 (eieio--class-class-slots newc))
446 ;; Also, setup the class allocated values.
447 (let* ((slots (eieio--class-class-slots newc))
448 (n (length slots))
449 (v (make-vector n nil)))
450 (dotimes (i n)
451 (setf (aref v i) (eieio-default-eval-maybe
452 (cl--slot-descriptor-initform (aref slots i)))))
453 (setf (eieio--class-class-allocation-values newc) v))
455 ;; Attach slot symbols into a hashtable, and store the index of
456 ;; this slot as the value this table.
457 (let* ((slots (eieio--class-slots newc))
458 ;; (cslots (eieio--class-class-slots newc))
459 (oa (make-hash-table :test #'eq)))
460 ;; (dotimes (cnt (length cslots))
461 ;; (setf (gethash (cl--slot-descriptor-name (aref cslots cnt)) oa) (- -1 cnt)))
462 (dotimes (cnt (length slots))
463 (setf (gethash (cl--slot-descriptor-name (aref slots cnt)) oa) cnt))
464 (setf (eieio--class-index-table newc) oa))
466 ;; Set up a specialized doc string.
467 ;; Use stored value since it is calculated in a non-trivial way
468 (let ((docstring (eieio--class-option-assoc options :documentation)))
469 (setf (eieio--class-docstring newc) docstring)
470 (when eieio-backward-compatibility
471 (put cname 'variable-documentation docstring)))
473 ;; Save the file location where this class is defined.
474 (add-to-list 'current-load-list `(eieio-defclass . ,cname))
476 ;; We have a list of custom groups. Store them into the options.
477 (let ((g (eieio--class-option-assoc options :custom-groups)))
478 (mapc (lambda (cg) (cl-pushnew cg g :test 'equal)) groups)
479 (if (memq :custom-groups options)
480 (setcar (cdr (memq :custom-groups options)) g)
481 (setq options (cons :custom-groups (cons g options)))))
483 ;; Set up the options we have collected.
484 (setf (eieio--class-options newc) options)
486 ;; if this is a superclass, clear out parent (which was set to the
487 ;; default superclass eieio-default-superclass)
488 (if clearparent (setf (eieio--class-parents newc) nil))
490 ;; Create the cached default object.
491 (let ((cache (make-vector (+ (length (eieio--class-slots newc))
492 (eval-when-compile eieio--object-num-slots))
493 nil))
494 ;; We don't strictly speaking need to use a symbol, but the old
495 ;; code used the class's name rather than the class's object, so
496 ;; we follow this preference for using a symbol, which is probably
497 ;; convenient to keep the printed representation of such Elisp
498 ;; objects readable.
499 (tag (intern (format "eieio-class-tag--%s" cname))))
500 (set tag newc)
501 (fset tag :quick-object-witness-check)
502 (setf (eieio--object-class-tag cache) tag)
503 (let ((eieio-skip-typecheck t))
504 ;; All type-checking has been done to our satisfaction
505 ;; before this call. Don't waste our time in this call..
506 (eieio-set-defaults cache t))
507 (setf (eieio--class-default-object-cache newc) cache))
509 ;; Return our new class object
510 ;; newc
511 cname
514 (defsubst eieio-eval-default-p (val)
515 "Whether the default value VAL should be evaluated for use."
516 (and (consp val) (symbolp (car val)) (fboundp (car val))))
518 (defun eieio--perform-slot-validation-for-default (slot skipnil)
519 "For SLOT, signal if its type does not match its default value.
520 If SKIPNIL is non-nil, then if default value is nil return t instead."
521 (let ((value (cl--slot-descriptor-initform slot))
522 (spec (cl--slot-descriptor-type slot)))
523 (if (not (or (eieio-eval-default-p value) ;FIXME: Why?
524 eieio-skip-typecheck
525 (and skipnil (null value))
526 (eieio--perform-slot-validation spec value)))
527 (signal 'invalid-slot-type (list (cl--slot-descriptor-name slot) spec value)))))
529 (defun eieio--slot-override (old new skipnil)
530 (cl-assert (eq (cl--slot-descriptor-name old) (cl--slot-descriptor-name new)))
531 ;; There is a match, and we must override the old value.
532 (let* ((a (cl--slot-descriptor-name old))
533 (tp (cl--slot-descriptor-type old))
534 (d (cl--slot-descriptor-initform new))
535 (type (cl--slot-descriptor-type new))
536 (oprops (cl--slot-descriptor-props old))
537 (nprops (cl--slot-descriptor-props new))
538 (custg (alist-get :group nprops)))
539 ;; If type is passed in, is it the same?
540 (if (not (eq type t))
541 (if (not (equal type tp))
542 (error
543 "Child slot type `%s' does not match inherited type `%s' for `%s'"
544 type tp a))
545 (setf (cl--slot-descriptor-type new) tp))
546 ;; If we have a repeat, only update the initarg...
547 (unless (eq d eieio-unbound)
548 (eieio--perform-slot-validation-for-default new skipnil)
549 (setf (cl--slot-descriptor-initform old) d))
551 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
552 ;; checked and SHOULD match the superclass
553 ;; protection. Otherwise an error is thrown. However
554 ;; I wonder if a more flexible schedule might be
555 ;; implemented.
557 ;; EML - We used to have (if prot... here,
558 ;; but a prot of 'nil means public.
560 (let ((super-prot (alist-get :protection oprops))
561 (prot (alist-get :protection nprops)))
562 (if (not (eq prot super-prot))
563 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
564 prot super-prot a)))
565 ;; End original PLN
567 ;; PLN Tue Jun 26 11:57:06 2007 :
568 ;; Do a non redundant combination of ancient custom
569 ;; groups and new ones.
570 (when custg
571 (let* ((list1 (alist-get :group oprops)))
572 (dolist (elt custg)
573 (unless (memq elt list1)
574 (push elt list1)))
575 (setf (alist-get :group (cl--slot-descriptor-props old)) list1)))
576 ;; End PLN
578 ;; PLN Mon Jun 25 22:44:34 2007 : If a new cust is
579 ;; set, simply replaces the old one.
580 (dolist (prop '(:custom :label :documentation :printer))
581 (when (alist-get prop (cl--slot-descriptor-props new))
582 (setf (alist-get prop (cl--slot-descriptor-props old))
583 (alist-get prop (cl--slot-descriptor-props new))))
585 ) ))
587 (defun eieio--add-new-slot (newc slot init alloc
588 &optional defaultoverride skipnil)
589 "Add into NEWC attribute SLOT.
590 If a slot of that name already exists in NEWC, then do nothing. If it doesn't exist,
591 INIT is the initarg, if any.
592 Argument ALLOC specifies if the slot is allocated per instance, or per class.
593 If optional DEFAULTOVERRIDE is non-nil, then if A exists in NEWC,
594 we must override its value for a default.
595 Optional argument SKIPNIL indicates if type checking should be skipped
596 if default value is nil."
597 ;; Make sure we duplicate those items that are sequences.
598 (let* ((a (cl--slot-descriptor-name slot))
599 (d (cl--slot-descriptor-initform slot))
600 (old (car (cl-member a (eieio--class-slots newc)
601 :key #'cl--slot-descriptor-name)))
602 (cold (car (cl-member a (eieio--class-class-slots newc)
603 :key #'cl--slot-descriptor-name))))
604 (condition-case nil
605 (if (sequencep d) (setq d (copy-sequence d)))
606 ;; This copy can fail on a cons cell with a non-cons in the cdr. Let's
607 ;; skip it if it doesn't work.
608 (error nil))
609 ;; (if (sequencep type) (setq type (copy-sequence type)))
610 ;; (if (sequencep cust) (setq cust (copy-sequence cust)))
611 ;; (if (sequencep custg) (setq custg (copy-sequence custg)))
613 ;; To prevent override information w/out specification of storage,
614 ;; we need to do this little hack.
615 (if cold (setq alloc :class))
617 (if (memq alloc '(nil :instance))
618 ;; In this case, we modify the INSTANCE version of a given slot.
619 (progn
620 ;; Only add this element if it is so-far unique
621 (if (not old)
622 (progn
623 (eieio--perform-slot-validation-for-default slot skipnil)
624 (push slot (eieio--class-slots newc))
626 ;; When defaultoverride is true, we are usually adding new local
627 ;; attributes which must override the default value of any slot
628 ;; passed in by one of the parent classes.
629 (when defaultoverride
630 (eieio--slot-override old slot skipnil)))
631 (when init
632 (cl-pushnew (cons init a) (eieio--class-initarg-tuples newc)
633 :test #'equal)))
635 ;; CLASS ALLOCATED SLOTS
636 (if (not cold)
637 (progn
638 (eieio--perform-slot-validation-for-default slot skipnil)
639 ;; Here we have found a :class version of a slot. This
640 ;; requires a very different approach.
641 (push slot (eieio--class-class-slots newc)))
642 (when defaultoverride
643 ;; There is a match, and we must override the old value.
644 (eieio--slot-override cold slot skipnil))))))
646 (defun eieio-copy-parents-into-subclass (newc)
647 "Copy into NEWC the slots of PARENTS.
648 Follow the rules of not overwriting early parents when applying to
649 the new child class."
650 (let ((sn (eieio--class-option-assoc (eieio--class-options newc)
651 :allow-nil-initform)))
652 (dolist (pcv (eieio--class-parents newc))
653 ;; First, duplicate all the slots of the parent.
654 (let ((pslots (eieio--class-slots pcv))
655 (pinit (eieio--class-initarg-tuples pcv)))
656 (dotimes (i (length pslots))
657 (let* ((sd (cl--copy-slot-descriptor (aref pslots i)))
658 (init (car (rassq (cl--slot-descriptor-name sd) pinit))))
659 (eieio--add-new-slot newc sd init nil nil sn))
660 )) ;; while/let
661 ;; Now duplicate all the class alloc slots.
662 (let ((pcslots (eieio--class-class-slots pcv)))
663 (dotimes (i (length pcslots))
664 (eieio--add-new-slot newc (cl--copy-slot-descriptor
665 (aref pcslots i))
666 nil :class sn)
667 )))))
670 ;;; Slot type validation
672 ;; This is a hideous hack for replacing `typep' from cl-macs, to avoid
673 ;; requiring the CL library at run-time. It can be eliminated if/when
674 ;; `typep' is merged into Emacs core.
676 (defun eieio--perform-slot-validation (spec value)
677 "Return non-nil if SPEC does not match VALUE."
678 (or (eq spec t) ; t always passes
679 (eq value eieio-unbound) ; unbound always passes
680 (cl-typep value spec)))
682 (defun eieio--validate-slot-value (class slot-idx value slot)
683 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
684 Checks the :type specifier.
685 SLOT is the slot that is being checked, and is only used when throwing
686 an error."
687 (if eieio-skip-typecheck
689 ;; Trim off object IDX junk added in for the object index.
690 (setq slot-idx (- slot-idx (eval-when-compile eieio--object-num-slots)))
691 (let ((st (cl--slot-descriptor-type (aref (eieio--class-slots class)
692 slot-idx))))
693 (if (not (eieio--perform-slot-validation st value))
694 (signal 'invalid-slot-type
695 (list (eieio--class-name class) slot st value))))))
697 (defun eieio--validate-class-slot-value (class slot-idx value slot)
698 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
699 Checks the :type specifier.
700 SLOT is the slot that is being checked, and is only used when throwing
701 an error."
702 (if eieio-skip-typecheck
704 (let ((st (cl--slot-descriptor-type (aref (eieio--class-class-slots class)
705 slot-idx))))
706 (if (not (eieio--perform-slot-validation st value))
707 (signal 'invalid-slot-type
708 (list (eieio--class-name class) slot st value))))))
710 (defun eieio-barf-if-slot-unbound (value instance slotname fn)
711 "Throw a signal if VALUE is a representation of an UNBOUND slot.
712 INSTANCE is the object being referenced. SLOTNAME is the offending
713 slot. If the slot is ok, return VALUE.
714 Argument FN is the function calling this verifier."
715 (if (and (eq value eieio-unbound) (not eieio-skip-typecheck))
716 (slot-unbound instance (eieio--object-class instance) slotname fn)
717 value))
720 ;;; Get/Set slots in an object.
722 (defun eieio-oref (obj slot)
723 "Return the value in OBJ at SLOT in the object vector."
724 (cl-check-type slot symbol)
725 (cl-check-type obj (or eieio-object class))
726 (let* ((class (cond ((symbolp obj)
727 (error "eieio-oref called on a class!")
728 (let ((c (eieio--class-v obj)))
729 (if (eieio--class-p c) (eieio-class-un-autoload obj))
731 (t (eieio--object-class obj))))
732 (c (eieio--slot-name-index class slot)))
733 (if (not c)
734 ;; It might be missing because it is a :class allocated slot.
735 ;; Let's check that info out.
736 (if (setq c (eieio--class-slot-name-index class slot))
737 ;; Oref that slot.
738 (aref (eieio--class-class-allocation-values class) c)
739 ;; The slot-missing method is a cool way of allowing an object author
740 ;; to intercept missing slot definitions. Since it is also the LAST
741 ;; thing called in this fn, its return value would be retrieved.
742 (slot-missing obj slot 'oref)
743 ;;(signal 'invalid-slot-name (list (eieio-object-name obj) slot))
745 (cl-check-type obj eieio-object)
746 (eieio-barf-if-slot-unbound (aref obj c) obj slot 'oref))))
749 (defun eieio-oref-default (obj slot)
750 "Do the work for the macro `oref-default' with similar parameters.
751 Fills in OBJ's SLOT with its default value."
752 (cl-check-type obj (or eieio-object class))
753 (cl-check-type slot symbol)
754 (let* ((cl (cond ((symbolp obj) (eieio--class-v obj))
755 (t (eieio--object-class obj))))
756 (c (eieio--slot-name-index cl slot)))
757 (if (not c)
758 ;; It might be missing because it is a :class allocated slot.
759 ;; Let's check that info out.
760 (if (setq c
761 (eieio--class-slot-name-index cl slot))
762 ;; Oref that slot.
763 (aref (eieio--class-class-allocation-values cl)
765 (slot-missing obj slot 'oref-default)
766 ;;(signal 'invalid-slot-name (list (class-name cl) slot))
768 (eieio-barf-if-slot-unbound
769 (let ((val (cl--slot-descriptor-initform
770 (aref (eieio--class-slots cl)
771 (- c (eval-when-compile eieio--object-num-slots))))))
772 (eieio-default-eval-maybe val))
773 obj (eieio--class-name cl) 'oref-default))))
775 (defun eieio-default-eval-maybe (val)
776 "Check VAL, and return what `oref-default' would provide."
777 ;; FIXME: What the hell is this supposed to do? Shouldn't it evaluate
778 ;; variables as well? Why not just always call `eval'?
779 (cond
780 ;; Is it a function call? If so, evaluate it.
781 ((eieio-eval-default-p val)
782 (eval val))
783 ;;;; check for quoted things, and unquote them
784 ;;((and (consp val) (eq (car val) 'quote))
785 ;; (car (cdr val)))
786 ;; return it verbatim
787 (t val)))
789 (defun eieio-oset (obj slot value)
790 "Do the work for the macro `oset'.
791 Fills in OBJ's SLOT with VALUE."
792 (cl-check-type obj eieio-object)
793 (cl-check-type slot symbol)
794 (let* ((class (eieio--object-class obj))
795 (c (eieio--slot-name-index class slot)))
796 (if (not c)
797 ;; It might be missing because it is a :class allocated slot.
798 ;; Let's check that info out.
799 (if (setq c
800 (eieio--class-slot-name-index class slot))
801 ;; Oset that slot.
802 (progn
803 (eieio--validate-class-slot-value class c value slot)
804 (aset (eieio--class-class-allocation-values class)
805 c value))
806 ;; See oref for comment on `slot-missing'
807 (slot-missing obj slot 'oset value)
808 ;;(signal 'invalid-slot-name (list (eieio-object-name obj) slot))
810 (eieio--validate-slot-value class c value slot)
811 (aset obj c value))))
813 (defun eieio-oset-default (class slot value)
814 "Do the work for the macro `oset-default'.
815 Fills in the default value in CLASS' in SLOT with VALUE."
816 (setq class (eieio--class-object class))
817 (cl-check-type class eieio--class)
818 (cl-check-type slot symbol)
819 (let* ((c (eieio--slot-name-index class slot)))
820 (if (not c)
821 ;; It might be missing because it is a :class allocated slot.
822 ;; Let's check that info out.
823 (if (setq c (eieio--class-slot-name-index class slot))
824 (progn
825 ;; Oref that slot.
826 (eieio--validate-class-slot-value class c value slot)
827 (aset (eieio--class-class-allocation-values class) c
828 value))
829 (signal 'invalid-slot-name (list (eieio--class-name class) slot)))
830 ;; `oset-default' on an instance-allocated slot is allowed by EIEIO but
831 ;; not by CLOS and is mildly inconsistent with the :initform thingy, so
832 ;; it'd be nice to get of it. This said, it is/was used at one place by
833 ;; gnus/registry.el, so it might be used elsewhere as well, so let's
834 ;; keep it for now.
835 ;; FIXME: Generate a compile-time warning for it!
836 ;; (error "Can't `oset-default' an instance-allocated slot: %S of %S"
837 ;; slot class)
838 (eieio--validate-slot-value class c value slot)
839 ;; Set this into the storage for defaults.
840 (if (eieio-eval-default-p value)
841 (error "Can't set default to a sexp that gets evaluated again"))
842 (setf (cl--slot-descriptor-initform
843 ;; FIXME: Apparently we set it both in `slots' and in
844 ;; `object-cache', which seems redundant.
845 (aref (eieio--class-slots class)
846 (- c (eval-when-compile eieio--object-num-slots))))
847 value)
848 ;; Take the value, and put it into our cache object.
849 (eieio-oset (eieio--class-default-object-cache class)
850 slot value)
854 ;;; EIEIO internal search functions
856 (defun eieio--slot-name-index (class slot)
857 "In CLASS find the index of the named SLOT.
858 The slot is a symbol which is installed in CLASS by the `defclass' call.
859 If SLOT is the value created with :initarg instead,
860 reverse-lookup that name, and recurse with the associated slot value."
861 ;; Removed checks to outside this call
862 (let* ((fsi (gethash slot (eieio--class-index-table class))))
863 (if (integerp fsi)
864 (+ (eval-when-compile eieio--object-num-slots) fsi)
865 (let ((fn (eieio--initarg-to-attribute class slot)))
866 (if fn
867 ;; Accessing a slot via its :initarg is accepted by EIEIO
868 ;; (but not CLOS) but is a bad idea (for one: it's slower).
869 ;; FIXME: We should emit a compile-time warning when this happens!
870 (eieio--slot-name-index class fn)
871 nil)))))
873 (defun eieio--class-slot-name-index (class slot)
874 "In CLASS find the index of the named SLOT.
875 The slot is a symbol which is installed in CLASS by the `defclass'
876 call. If SLOT is the value created with :initarg instead,
877 reverse-lookup that name, and recurse with the associated slot value."
878 ;; This will happen less often, and with fewer slots. Do this the
879 ;; storage cheap way.
880 (let ((index nil)
881 (slots (eieio--class-class-slots class)))
882 (dotimes (i (length slots))
883 (if (eq slot (cl--slot-descriptor-name (aref slots i)))
884 (setq index i)))
885 index))
888 ;; Way to assign slots based on a list. Used for constructors, or
889 ;; even resetting an object at run-time
891 (defun eieio-set-defaults (obj &optional set-all)
892 "Take object OBJ, and reset all slots to their defaults.
893 If SET-ALL is non-nil, then when a default is nil, that value is
894 reset. If SET-ALL is nil, the slots are only reset if the default is
895 not nil."
896 (let ((slots (eieio--class-slots (eieio--object-class obj))))
897 (dotimes (i (length slots))
898 (let* ((name (cl--slot-descriptor-name (aref slots i)))
899 (df (eieio-oref-default obj name)))
900 (if (or df set-all)
901 (eieio-oset obj name df))))))
903 (defun eieio--initarg-to-attribute (class initarg)
904 "For CLASS, convert INITARG to the actual attribute name.
905 If there is no translation, pass it in directly (so we can cheat if
906 need be... May remove that later...)"
907 (let ((tuple (assoc initarg (eieio--class-initarg-tuples class))))
908 (if tuple
909 (cdr tuple)
910 nil)))
913 ;; Method Invocation order: C3
914 (defun eieio--c3-candidate (class remaining-inputs)
915 "Return CLASS if it can go in the result now, otherwise nil."
916 ;; Ensure CLASS is not in any position but the first in any of the
917 ;; element lists of REMAINING-INPUTS.
918 (and (not (let ((found nil))
919 (while (and remaining-inputs (not found))
920 (setq found (member class (cdr (car remaining-inputs)))
921 remaining-inputs (cdr remaining-inputs)))
922 found))
923 class))
925 (defun eieio--c3-merge-lists (reversed-partial-result remaining-inputs)
926 "Merge REVERSED-PARTIAL-RESULT REMAINING-INPUTS in a consistent order, if possible.
927 If a consistent order does not exist, signal an error."
928 (setq remaining-inputs (delq nil remaining-inputs))
929 (if (null remaining-inputs)
930 ;; If all remaining inputs are empty lists, we are done.
931 (nreverse reversed-partial-result)
932 ;; Otherwise, we try to find the next element of the result. This
933 ;; is achieved by considering the first element of each
934 ;; (non-empty) input list and accepting a candidate if it is
935 ;; consistent with the rests of the input lists.
936 (let* ((found nil)
937 (tail remaining-inputs)
938 (next (progn
939 (while (and tail (not found))
940 (setq found (eieio--c3-candidate (caar tail)
941 remaining-inputs)
942 tail (cdr tail)))
943 found)))
944 (if next
945 ;; The graph is consistent so far, add NEXT to result and
946 ;; merge input lists, dropping NEXT from their heads where
947 ;; applicable.
948 (eieio--c3-merge-lists
949 (cons next reversed-partial-result)
950 (mapcar (lambda (l) (if (eq (cl-first l) next) (cl-rest l) l))
951 remaining-inputs))
952 ;; The graph is inconsistent, give up
953 (signal 'inconsistent-class-hierarchy (list remaining-inputs))))))
955 (defsubst eieio--class/struct-parents (class)
956 (or (eieio--class-parents class)
957 `(,eieio-default-superclass)))
959 (defun eieio--class-precedence-c3 (class)
960 "Return all parents of CLASS in c3 order."
961 (let ((parents (eieio--class-parents (eieio--class-v class))))
962 (eieio--c3-merge-lists
963 (list class)
964 (append
966 (mapcar #'eieio--class-precedence-c3 parents)
967 `((,eieio-default-superclass)))
968 (list parents))))
971 ;; Method Invocation Order: Depth First
973 (defun eieio--class-precedence-dfs (class)
974 "Return all parents of CLASS in depth-first order."
975 (let* ((parents (eieio--class-parents class))
976 (classes (copy-sequence
977 (apply #'append
978 (list class)
980 (mapcar
981 (lambda (parent)
982 (cons parent
983 (eieio--class-precedence-dfs parent)))
984 parents)
985 `((,eieio-default-superclass))))))
986 (tail classes))
987 ;; Remove duplicates.
988 (while tail
989 (setcdr tail (delq (car tail) (cdr tail)))
990 (setq tail (cdr tail)))
991 classes))
994 ;; Method Invocation Order: Breadth First
995 (defun eieio--class-precedence-bfs (class)
996 "Return all parents of CLASS in breadth-first order."
997 (let* ((result)
998 (queue (eieio--class/struct-parents class)))
999 (while queue
1000 (let ((head (pop queue)))
1001 (unless (member head result)
1002 (push head result)
1003 (unless (eq head eieio-default-superclass)
1004 (setq queue (append queue (eieio--class/struct-parents head)))))))
1005 (cons class (nreverse result)))
1009 ;; Method Invocation Order
1011 (defun eieio--class-precedence-list (class)
1012 "Return (transitively closed) list of parents of CLASS.
1013 The order, in which the parents are returned depends on the
1014 method invocation orders of the involved classes."
1015 (if (or (null class) (eq class eieio-default-superclass))
1017 (unless (eieio--class-default-object-cache class)
1018 (eieio-class-un-autoload (eieio--class-name class)))
1019 (cl-case (eieio--class-method-invocation-order class)
1020 (:depth-first
1021 (eieio--class-precedence-dfs class))
1022 (:breadth-first
1023 (eieio--class-precedence-bfs class))
1024 (:c3
1025 (eieio--class-precedence-c3 class))))
1027 (define-obsolete-function-alias
1028 'class-precedence-list 'eieio--class-precedence-list "24.4")
1031 ;;; Here are some special types of errors
1033 (define-error 'invalid-slot-name "Invalid slot name")
1034 (define-error 'invalid-slot-type "Invalid slot type")
1035 (define-error 'unbound-slot "Unbound slot")
1036 (define-error 'inconsistent-class-hierarchy "Inconsistent class hierarchy")
1038 ;;; Hooking into cl-generic.
1040 (require 'cl-generic)
1042 ;;;; General support to dispatch based on the type of the argument.
1044 (defconst eieio--generic-generalizer
1045 (cl-generic-make-generalizer
1046 ;; Use the exact same tagcode as for cl-struct, so that methods
1047 ;; that dispatch on both kinds of objects get to share this
1048 ;; part of the dispatch code.
1049 50 #'cl--generic-struct-tag
1050 (lambda (tag)
1051 (and (symbolp tag) (boundp tag) (eieio--class-p (symbol-value tag))
1052 (mapcar #'eieio--class-name
1053 (eieio--class-precedence-list (symbol-value tag)))))))
1055 (cl-defmethod cl-generic-generalizers :extra "class" (specializer)
1056 ;; CLHS says:
1057 ;; A class must be defined before it can be used as a parameter
1058 ;; specializer in a defmethod form.
1059 ;; So we can ignore types that are not known to denote classes.
1061 (and (eieio--class-p (eieio--class-object specializer))
1062 (list eieio--generic-generalizer))
1063 (cl-call-next-method)))
1065 ;;;; Dispatch for arguments which are classes.
1067 ;; Since EIEIO does not support metaclasses, users can't easily use the
1068 ;; "dispatch on argument type" for class arguments. That's why EIEIO's
1069 ;; `defmethod' added the :static qualifier. For cl-generic, such a qualifier
1070 ;; would not make much sense (e.g. to which argument should it apply?).
1071 ;; Instead, we add a new "subclass" specializer.
1073 (defun eieio--generic-subclass-specializers (tag)
1074 (when (eieio--class-p tag)
1075 (mapcar (lambda (class)
1076 `(subclass ,(eieio--class-name class)))
1077 (eieio--class-precedence-list tag))))
1079 (defconst eieio--generic-subclass-generalizer
1080 (cl-generic-make-generalizer
1081 60 (lambda (name) `(and (symbolp ,name) (eieio--class-v ,name)))
1082 #'eieio--generic-subclass-specializers))
1084 (cl-defmethod cl-generic-generalizers ((_specializer (head subclass)))
1085 (list eieio--generic-subclass-generalizer))
1088 ;;;### (autoloads nil "eieio-compat" "eieio-compat.el" "0609a7bdcd6f38876b7f5647047ddca9")
1089 ;;; Generated autoloads from eieio-compat.el
1091 (autoload 'eieio--defalias "eieio-compat" "\
1092 Like `defalias', but with less side-effects.
1093 More specifically, it has no side-effects at all when the new function
1094 definition is the same (`eq') as the old one.
1096 \(fn NAME BODY)" nil nil)
1098 (autoload 'defgeneric "eieio-compat" "\
1099 Create a generic function METHOD.
1100 DOC-STRING is the base documentation for this class. A generic
1101 function has no body, as its purpose is to decide which method body
1102 is appropriate to use. Uses `defmethod' to create methods, and calls
1103 `defgeneric' for you. With this implementation the ARGS are
1104 currently ignored. You can use `defgeneric' to apply specialized
1105 top level documentation to a method.
1107 \(fn METHOD ARGS &optional DOC-STRING)" nil t)
1109 (function-put 'defgeneric 'doc-string-elt '3)
1111 (make-obsolete 'defgeneric 'cl-defgeneric '"25.1")
1113 (autoload 'defmethod "eieio-compat" "\
1114 Create a new METHOD through `defgeneric' with ARGS.
1116 The optional second argument KEY is a specifier that
1117 modifies how the method is called, including:
1118 :before - Method will be called before the :primary
1119 :primary - The default if not specified
1120 :after - Method will be called after the :primary
1121 :static - First arg could be an object or class
1122 The next argument is the ARGLIST. The ARGLIST specifies the arguments
1123 to the method as with `defun'. The first argument can have a type
1124 specifier, such as:
1125 ((VARNAME CLASS) ARG2 ...)
1126 where VARNAME is the name of the local variable for the method being
1127 created. The CLASS is a class symbol for a class made with `defclass'.
1128 A DOCSTRING comes after the ARGLIST, and is optional.
1129 All the rest of the args are the BODY of the method. A method will
1130 return the value of the last form in the BODY.
1132 Summary:
1134 (defmethod mymethod [:before | :primary | :after | :static]
1135 ((typearg class-name) arg2 &optional opt &rest rest)
1136 \"doc-string\"
1137 body)
1139 \(fn METHOD &rest ARGS)" nil t)
1141 (function-put 'defmethod 'doc-string-elt '3)
1143 (make-obsolete 'defmethod 'cl-defmethod '"25.1")
1145 (autoload 'eieio--defgeneric-init-form "eieio-compat" "\
1148 \(fn METHOD DOC-STRING)" nil nil)
1150 (autoload 'eieio--defmethod "eieio-compat" "\
1153 \(fn METHOD KIND ARGCLASS CODE)" nil nil)
1155 (autoload 'eieio-defmethod "eieio-compat" "\
1156 Obsolete work part of an old version of the `defmethod' macro.
1158 \(fn METHOD ARGS)" nil nil)
1160 (make-obsolete 'eieio-defmethod 'cl-defmethod '"24.1")
1162 (autoload 'eieio-defgeneric "eieio-compat" "\
1163 Obsolete work part of an old version of the `defgeneric' macro.
1165 \(fn METHOD DOC-STRING)" nil nil)
1167 (make-obsolete 'eieio-defgeneric 'cl-defgeneric '"24.1")
1169 (autoload 'eieio-defclass "eieio-compat" "\
1172 \(fn CNAME SUPERCLASSES SLOTS OPTIONS)" nil nil)
1174 (make-obsolete 'eieio-defclass 'eieio-defclass-internal '"25.1")
1176 ;;;***
1179 (provide 'eieio-core)
1181 ;;; eieio-core.el ends here