emacs-lisp/package.el: Move the compatibility-table building logic.
[emacs.git] / lisp / emacs-lisp / eieio-core.el
blobe71c54d412317909ff6859678e7919d1775696f9
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)))
88 (cl-defstruct (eieio--class
89 (:constructor nil)
90 (:constructor eieio--class-make (symbol &aux (tag 'defclass)))
91 (:type vector)
92 (:copier nil))
93 ;; We use an untagged cl-struct, with our own hand-made tag as first field
94 ;; (containing the symbol `defclass'). It would be better to use a normal
95 ;; cl-struct with its normal tag (e.g. so that cl-defstruct can define the
96 ;; predicate for us), but that breaks compatibility with .elc files compiled
97 ;; against older versions of EIEIO.
98 tag
99 symbol ;; symbol (self-referencing)
100 parent children
101 symbol-hashtable ;; hashtable permitting fast access to variable position indexes
102 ;; @todo
103 ;; the word "public" here is leftovers from the very first version.
104 ;; Get rid of it!
105 public-a ;; class attribute index
106 public-d ;; class attribute defaults index
107 public-doc ;; class documentation strings for attributes
108 public-type ;; class type for a slot
109 public-custom ;; class custom type for a slot
110 public-custom-label ;; class custom group for a slot
111 public-custom-group ;; class custom group for a slot
112 public-printer ;; printer for a slot
113 protection ;; protection for a slot
114 initarg-tuples ;; initarg tuples list
115 class-allocation-a ;; class allocated attributes
116 class-allocation-doc ;; class allocated documentation
117 class-allocation-type ;; class allocated value type
118 class-allocation-custom ;; class allocated custom descriptor
119 class-allocation-custom-label ;; class allocated custom descriptor
120 class-allocation-custom-group ;; class allocated custom group
121 class-allocation-printer ;; class allocated printer for a slot
122 class-allocation-protection ;; class allocated protection list
123 class-allocation-values ;; class allocated value vector
124 default-object-cache ;; what a newly created object would look like.
125 ; This will speed up instantiation time as
126 ; only a `copy-sequence' will be needed, instead of
127 ; looping over all the values and setting them from
128 ; the default.
129 options ;; storage location of tagged class option
130 ; Stored outright without modifications or stripping
132 ;; Set it back to the default value.
133 (cl-declaim (optimize (safety 1))))
136 (cl-defstruct (eieio--object
137 (:type vector) ;We manage our own tagging system.
138 (:constructor nil)
139 (:copier nil))
140 ;; `class-tag' holds a symbol, which is not the class name, but is instead
141 ;; properly prefixed as an internal EIEIO thingy and which holds the class
142 ;; object/struct in its `symbol-value' slot.
143 class-tag)
145 (eval-and-compile
146 (defconst eieio--object-num-slots
147 (length (get 'eieio--object 'cl-struct-slots))))
149 (defsubst eieio--object-class-object (obj)
150 (symbol-value (eieio--object-class-tag obj)))
152 (defsubst eieio--object-class-name (obj)
153 ;; FIXME: Most uses of this function should be changed to use
154 ;; eieio--object-class-object instead!
155 (eieio--class-symbol (eieio--object-class-object obj)))
158 ;;; Important macros used internally in eieio.
160 (defmacro eieio--class-v (class) ;Use a macro, so it acts as a GV place.
161 "Internal: Return the class vector from the CLASS symbol."
162 (declare (debug t))
163 ;; No check: If eieio gets this far, it has probably been checked already.
164 `(get ,class 'eieio-class-definition))
166 (defsubst eieio--class-object (class)
167 "Return the class object."
168 (if (symbolp class)
169 ;; Keep the symbol if class-v is nil, for better error messages.
170 (or (eieio--class-v class) class)
171 class))
173 (defsubst eieio--class-p (class)
174 "Return non-nil if CLASS is a valid class object."
175 (condition-case nil
176 (eq (aref class 0) 'defclass)
177 (error nil)))
179 (defun class-p (class)
180 "Return non-nil if CLASS is a valid class vector.
181 CLASS is a symbol." ;FIXME: Is it a vector or a symbol?
182 (and (symbolp class) (eieio--class-p (eieio--class-v class))))
184 (defun eieio-class-name (class)
185 "Return a Lisp like symbol name for CLASS."
186 ;; FIXME: What's a "Lisp like symbol name"?
187 ;; FIXME: CLOS returns a symbol, but the code returns a string.
188 (if (eieio--class-p class) (setq class (eieio--class-symbol class)))
189 (cl-check-type class class)
190 ;; I think this is supposed to return a symbol, but to me CLASS is a symbol,
191 ;; and I wanted a string. Arg!
192 (format "#<class %s>" (symbol-name class)))
193 (define-obsolete-function-alias 'class-name #'eieio-class-name "24.4")
195 (defalias 'eieio--class-constructor #'identity
196 "Return the symbol representing the constructor of CLASS.")
198 (defmacro eieio--class-option-assoc (list option)
199 "Return from LIST the found OPTION, or nil if it doesn't exist."
200 `(car-safe (cdr (memq ,option ,list))))
202 (defsubst eieio--class-option (class option)
203 "Return the value stored for CLASS' OPTION.
204 Return nil if that option doesn't exist."
205 (eieio--class-option-assoc (eieio--class-options class) option))
207 (defun eieio-object-p (obj)
208 "Return non-nil if OBJ is an EIEIO object."
209 (and (vectorp obj)
210 (> (length obj) 0)
211 (let ((tag (eieio--object-class-tag obj)))
212 (and (symbolp tag)
213 ;; (eq (symbol-function tag) :quick-object-witness-check)
214 (boundp tag)
215 (eieio--class-p (symbol-value tag))))))
217 (define-obsolete-function-alias 'object-p 'eieio-object-p "25.1")
219 (defsubst class-abstract-p (class)
220 "Return non-nil if CLASS is abstract.
221 Abstract classes cannot be instantiated."
222 (eieio--class-option (eieio--class-v class) :abstract))
224 (defsubst eieio--class-method-invocation-order (class)
225 "Return the invocation order of CLASS.
226 Abstract classes cannot be instantiated."
227 (or (eieio--class-option class :method-invocation-order)
228 :breadth-first))
233 ;; Class Creation
235 (defvar eieio-defclass-autoload-map (make-hash-table)
236 "Symbol map of superclasses we find in autoloads.")
238 ;; We autoload this because it's used in `make-autoload'.
239 ;;;###autoload
240 (defun eieio-defclass-autoload (cname _superclasses filename doc)
241 "Create autoload symbols for the EIEIO class CNAME.
242 SUPERCLASSES are the superclasses that CNAME inherits from.
243 DOC is the docstring for CNAME.
244 This function creates a mock-class for CNAME and adds it into
245 SUPERCLASSES as children.
246 It creates an autoload function for CNAME's constructor."
247 ;; Assume we've already debugged inputs.
249 ;; We used to store the list of superclasses in the `parent' slot (as a list
250 ;; of class names). But now this slot holds a list of class objects, and
251 ;; those parents may not exist yet, so the corresponding class objects may
252 ;; simply not exist yet. So instead we just don't store the list of parents
253 ;; here in eieio-defclass-autoload at all, since it seems that they're just
254 ;; not needed before the class is actually loaded.
255 (let* ((oldc (eieio--class-v cname))
256 (newc (eieio--class-make cname)))
257 (if (eieio--class-p oldc)
258 nil ;; Do nothing if we already have this class.
260 ;; turn this into a usable self-pointing symbol
261 (when eieio-backward-compatibility
262 (set cname cname)
263 (make-obsolete-variable cname (format "use '%s instead" cname) "25.1"))
265 ;; Store the new class vector definition into the symbol. We need to
266 ;; do this first so that we can call defmethod for the accessor.
267 ;; The vector will be updated by the following while loop and will not
268 ;; need to be stored a second time.
269 (setf (eieio--class-v cname) newc)
271 ;; Create an autoload on top of our constructor function.
272 (autoload cname filename doc nil nil)
273 (autoload (intern (format "%s-p" cname)) filename "" nil nil)
274 (when eieio-backward-compatibility
275 (autoload (intern (format "%s-child-p" cname)) filename "" nil nil)
276 (autoload (intern (format "%s-list-p" cname)) filename "" nil nil)))))
278 (defsubst eieio-class-un-autoload (cname)
279 "If class CNAME is in an autoload state, load its file."
280 (autoload-do-load (symbol-function cname))) ; cname
282 (cl-deftype list-of (elem-type)
283 `(and list
284 (satisfies (lambda (list)
285 (cl-every (lambda (elem) (cl-typep elem ',elem-type))
286 list)))))
289 (defun eieio-make-class-predicate (class)
290 (lambda (obj)
291 (:documentation
292 (format "Return non-nil if OBJ is an object of type `%S'.\n\n(fn OBJ)"
293 class))
294 (and (eieio-object-p obj)
295 (same-class-p obj class))))
297 (defun eieio-make-child-predicate (class)
298 (lambda (obj)
299 (:documentation
300 (format "Return non-nil if OBJ is an object of type `%S' or a subclass.
301 \n(fn OBJ)" class))
302 (and (eieio-object-p obj)
303 (object-of-class-p obj class))))
305 (defun eieio-defclass-internal (cname superclasses slots options)
306 "Define CNAME as a new subclass of SUPERCLASSES.
307 SLOTS are the slots residing in that class definition, and OPTIONS
308 holds the class options.
309 See `defclass' for more information."
310 ;; Run our eieio-hook each time, and clear it when we are done.
311 ;; This way people can add hooks safely if they want to modify eieio
312 ;; or add definitions when eieio is loaded or something like that.
313 (run-hooks 'eieio-hook)
314 (setq eieio-hook nil)
316 (let* ((oldc (let ((c (eieio--class-v cname))) (if (eieio--class-p c) c)))
317 (newc (if (and oldc (not (eieio--class-default-object-cache oldc)))
318 ;; The oldc class is a stub setup by eieio-defclass-autoload.
319 ;; Reuse it instead of creating a new one, so that existing
320 ;; references are still valid.
321 oldc
322 (eieio--class-make cname)))
323 (groups nil) ;; list of groups id'd from slots
324 (clearparent nil))
326 ;; If this class already existed, and we are updating its structure,
327 ;; make sure we keep the old child list. This can cause bugs, but
328 ;; if no new slots are created, it also saves time, and prevents
329 ;; method table breakage, particularly when the users is only
330 ;; byte compiling an EIEIO file.
331 (if oldc
332 (setf (eieio--class-children newc) (eieio--class-children oldc))
333 ;; If the old class did not exist, but did exist in the autoload map,
334 ;; then adopt those children. This is like the above, but deals with
335 ;; autoloads nicely.
336 (let ((children (gethash cname eieio-defclass-autoload-map)))
337 (when children
338 (setf (eieio--class-children newc) children)
339 (remhash cname eieio-defclass-autoload-map))))
341 (if superclasses
342 (progn
343 (dolist (p superclasses)
344 (if (not (and p (symbolp p)))
345 (error "Invalid parent class %S" p)
346 (let ((c (eieio--class-v p)))
347 (if (not (eieio--class-p c))
348 ;; bad class
349 (error "Given parent class %S is not a class" p)
350 ;; good parent class...
351 ;; save new child in parent
352 (cl-pushnew cname (eieio--class-children c))
353 ;; Get custom groups, and store them into our local copy.
354 (mapc (lambda (g) (cl-pushnew g groups :test #'equal))
355 (eieio--class-option c :custom-groups))
356 ;; Save parent in child.
357 (push c (eieio--class-parent newc))))))
358 ;; Reverse the list of our parents so that they are prioritized in
359 ;; the same order as specified in the code.
360 (cl-callf nreverse (eieio--class-parent newc)))
361 ;; If there is nothing to loop over, then inherit from the
362 ;; default superclass.
363 (unless (eq cname 'eieio-default-superclass)
364 ;; adopt the default parent here, but clear it later...
365 (setq clearparent t)
366 ;; save new child in parent
367 (cl-pushnew cname (eieio--class-children eieio-default-superclass))
368 ;; save parent in child
369 (setf (eieio--class-parent newc) (list eieio-default-superclass))))
371 ;; turn this into a usable self-pointing symbol; FIXME: Why?
372 (when eieio-backward-compatibility
373 (set cname cname)
374 (make-obsolete-variable cname (format "use '%s instead" cname) "25.1"))
376 ;; Create a handy list of the class test too
377 (when eieio-backward-compatibility
378 (let ((csym (intern (concat (symbol-name cname) "-list-p"))))
379 (defalias csym
380 `(lambda (obj)
381 ,(format
382 "Test OBJ to see if it a list of objects which are a child of type %s"
383 cname)
384 (when (listp obj)
385 (let ((ans t)) ;; nil is valid
386 ;; Loop over all the elements of the input list, test
387 ;; each to make sure it is a child of the desired object class.
388 (while (and obj ans)
389 (setq ans (and (eieio-object-p (car obj))
390 (object-of-class-p (car obj) ,cname)))
391 (setq obj (cdr obj)))
392 ans))))
393 (make-obsolete csym (format "use (cl-typep ... '(list-of %s)) instead"
394 cname)
395 "25.1")))
397 ;; Before adding new slots, let's add all the methods and classes
398 ;; in from the parent class.
399 (eieio-copy-parents-into-subclass newc)
401 ;; Store the new class vector definition into the symbol. We need to
402 ;; do this first so that we can call defmethod for the accessor.
403 ;; The vector will be updated by the following while loop and will not
404 ;; need to be stored a second time.
405 (setf (eieio--class-v cname) newc)
407 ;; Query each slot in the declaration list and mangle into the
408 ;; class structure I have defined.
409 (pcase-dolist (`(,name . ,slot) slots)
410 (let* ((init (or (plist-get slot :initform)
411 (if (member :initform slot) nil
412 eieio-unbound)))
413 (initarg (plist-get slot :initarg))
414 (docstr (plist-get slot :documentation))
415 (prot (plist-get slot :protection))
416 (alloc (plist-get slot :allocation))
417 (type (plist-get slot :type))
418 (custom (plist-get slot :custom))
419 (label (plist-get slot :label))
420 (customg (plist-get slot :group))
421 (printer (plist-get slot :printer))
423 (skip-nil (eieio--class-option-assoc options :allow-nil-initform))
426 ;; Clean up the meaning of protection.
427 (setq prot
428 (pcase prot
429 ((or 'nil 'public ':public) nil)
430 ((or 'protected ':protected) 'protected)
431 ((or 'private ':private) 'private)
432 (_ (signal 'invalid-slot-type (list :protection prot)))))
434 ;; The default type specifier is supposed to be t, meaning anything.
435 (if (not type) (setq type t))
437 ;; intern the symbol so we can use it blankly
438 (if eieio-backward-compatibility
439 (and initarg (not (keywordp initarg))
440 (progn
441 (set initarg initarg)
442 (make-obsolete-variable
443 initarg (format "use '%s instead" initarg) "25.1"))))
445 ;; The customgroup should be a list of symbols
446 (cond ((null customg)
447 (setq customg '(default)))
448 ((not (listp customg))
449 (setq customg (list customg))))
450 ;; The customgroup better be a symbol, or list of symbols.
451 (mapc (lambda (cg)
452 (if (not (symbolp cg))
453 (signal 'invalid-slot-type (list :group cg))))
454 customg)
456 ;; First up, add this slot into our new class.
457 (eieio--add-new-slot newc name init docstr type custom label customg printer
458 prot initarg alloc 'defaultoverride skip-nil)
460 ;; We need to id the group, and store them in a group list attribute.
461 (dolist (cg customg)
462 (cl-pushnew cg groups :test 'equal))
465 ;; Now that everything has been loaded up, all our lists are backwards!
466 ;; Fix that up now.
467 (cl-callf nreverse (eieio--class-public-a newc))
468 (cl-callf nreverse (eieio--class-public-d newc))
469 (cl-callf nreverse (eieio--class-public-doc newc))
470 (cl-callf (lambda (types) (apply #'vector (nreverse types)))
471 (eieio--class-public-type newc))
472 (cl-callf nreverse (eieio--class-public-custom newc))
473 (cl-callf nreverse (eieio--class-public-custom-label newc))
474 (cl-callf nreverse (eieio--class-public-custom-group newc))
475 (cl-callf nreverse (eieio--class-public-printer newc))
476 (cl-callf nreverse (eieio--class-protection newc))
477 (cl-callf nreverse (eieio--class-initarg-tuples newc))
479 ;; The storage for class-class-allocation-type needs to be turned into
480 ;; a vector now.
481 (cl-callf (lambda (cat) (apply #'vector cat))
482 (eieio--class-class-allocation-type newc))
484 ;; Also, take class allocated values, and vectorize them for speed.
485 (cl-callf (lambda (cavs) (apply #'vector cavs))
486 (eieio--class-class-allocation-values newc))
488 ;; Attach slot symbols into a hashtable, and store the index of
489 ;; this slot as the value this table.
490 (let* ((cnt 0)
491 (pubsyms (eieio--class-public-a newc))
492 (prots (eieio--class-protection newc))
493 (oa (make-hash-table :test #'eq)))
494 (while pubsyms
495 (let ((newsym (list cnt)))
496 (setf (gethash (car pubsyms) oa) newsym)
497 (setq cnt (1+ cnt))
498 (if (car prots) (setcdr newsym (car prots))))
499 (setq pubsyms (cdr pubsyms)
500 prots (cdr prots)))
501 (setf (eieio--class-symbol-hashtable newc) oa))
503 ;; Set up a specialized doc string.
504 ;; Use stored value since it is calculated in a non-trivial way
505 (put cname 'variable-documentation
506 (eieio--class-option-assoc options :documentation))
508 ;; Save the file location where this class is defined.
509 (add-to-list 'current-load-list `(eieio-defclass . ,cname))
511 ;; We have a list of custom groups. Store them into the options.
512 (let ((g (eieio--class-option-assoc options :custom-groups)))
513 (mapc (lambda (cg) (cl-pushnew cg g :test 'equal)) groups)
514 (if (memq :custom-groups options)
515 (setcar (cdr (memq :custom-groups options)) g)
516 (setq options (cons :custom-groups (cons g options)))))
518 ;; Set up the options we have collected.
519 (setf (eieio--class-options newc) options)
521 ;; if this is a superclass, clear out parent (which was set to the
522 ;; default superclass eieio-default-superclass)
523 (if clearparent (setf (eieio--class-parent newc) nil))
525 ;; Create the cached default object.
526 (let ((cache (make-vector (+ (length (eieio--class-public-a newc))
527 (eval-when-compile eieio--object-num-slots))
528 nil))
529 ;; We don't strictly speaking need to use a symbol, but the old
530 ;; code used the class's name rather than the class's object, so
531 ;; we follow this preference for using a symbol, which is probably
532 ;; convenient to keep the printed representation of such Elisp
533 ;; objects readable.
534 (tag (intern (format "eieio-class-tag--%s" cname))))
535 (set tag newc)
536 (fset tag :quick-object-witness-check)
537 (setf (eieio--object-class-tag cache) tag)
538 (let ((eieio-skip-typecheck t))
539 ;; All type-checking has been done to our satisfaction
540 ;; before this call. Don't waste our time in this call..
541 (eieio-set-defaults cache t))
542 (setf (eieio--class-default-object-cache newc) cache))
544 ;; Return our new class object
545 ;; newc
546 cname
549 (defsubst eieio-eval-default-p (val)
550 "Whether the default value VAL should be evaluated for use."
551 (and (consp val) (symbolp (car val)) (fboundp (car val))))
553 (defun eieio--perform-slot-validation-for-default (slot spec value skipnil)
554 "For SLOT, signal if SPEC does not match VALUE.
555 If SKIPNIL is non-nil, then if VALUE is nil return t instead."
556 (if (not (or (eieio-eval-default-p value) ;FIXME: Why?
557 eieio-skip-typecheck
558 (and skipnil (null value))
559 (eieio--perform-slot-validation spec value)))
560 (signal 'invalid-slot-type (list slot spec value))))
562 (defun eieio--add-new-slot (newc a d doc type cust label custg print prot init alloc
563 &optional defaultoverride skipnil)
564 "Add into NEWC attribute A.
565 If A already exists in NEWC, then do nothing. If it doesn't exist,
566 then also add in D (default), DOC, TYPE, CUST, LABEL, CUSTG, PRINT, PROT, and INIT arg.
567 Argument ALLOC specifies if the slot is allocated per instance, or per class.
568 If optional DEFAULTOVERRIDE is non-nil, then if A exists in NEWC,
569 we must override its value for a default.
570 Optional argument SKIPNIL indicates if type checking should be skipped
571 if default value is nil."
572 ;; Make sure we duplicate those items that are sequences.
573 (condition-case nil
574 (if (sequencep d) (setq d (copy-sequence d)))
575 ;; This copy can fail on a cons cell with a non-cons in the cdr. Let's skip it if it doesn't work.
576 (error nil))
577 (if (sequencep type) (setq type (copy-sequence type)))
578 (if (sequencep cust) (setq cust (copy-sequence cust)))
579 (if (sequencep custg) (setq custg (copy-sequence custg)))
581 ;; To prevent override information w/out specification of storage,
582 ;; we need to do this little hack.
583 (if (member a (eieio--class-class-allocation-a newc)) (setq alloc :class))
585 (if (or (not alloc) (and (symbolp alloc) (eq alloc :instance)))
586 ;; In this case, we modify the INSTANCE version of a given slot.
588 (progn
590 ;; Only add this element if it is so-far unique
591 (if (not (member a (eieio--class-public-a newc)))
592 (progn
593 (eieio--perform-slot-validation-for-default a type d skipnil)
594 (push a (eieio--class-public-a newc))
595 (push d (eieio--class-public-d newc))
596 (push doc (eieio--class-public-doc newc))
597 (push type (eieio--class-public-type newc))
598 (push cust (eieio--class-public-custom newc))
599 (push label (eieio--class-public-custom-label newc))
600 (push custg (eieio--class-public-custom-group newc))
601 (push print (eieio--class-public-printer newc))
602 (push prot (eieio--class-protection newc))
603 (setf (eieio--class-initarg-tuples newc) (cons (cons init a) (eieio--class-initarg-tuples newc)))
605 ;; When defaultoverride is true, we are usually adding new local
606 ;; attributes which must override the default value of any slot
607 ;; passed in by one of the parent classes.
608 (when defaultoverride
609 ;; There is a match, and we must override the old value.
610 (let* ((ca (eieio--class-public-a newc))
611 (np (member a ca))
612 (num (- (length ca) (length np)))
613 (dp (if np (nthcdr num (eieio--class-public-d newc))
614 nil))
615 (tp (if np (nth num (eieio--class-public-type newc))))
617 (if (not np)
618 (error "EIEIO internal error overriding default value for %s"
620 ;; If type is passed in, is it the same?
621 (if (not (eq type t))
622 (if (not (equal type tp))
623 (error
624 "Child slot type `%s' does not match inherited type `%s' for `%s'"
625 type tp a)))
626 ;; If we have a repeat, only update the initarg...
627 (unless (eq d eieio-unbound)
628 (eieio--perform-slot-validation-for-default a tp d skipnil)
629 (setcar dp d))
630 ;; If we have a new initarg, check for it.
631 (when init
632 (let* ((inits (eieio--class-initarg-tuples newc))
633 (inita (rassq a inits)))
634 ;; Replace the CAR of the associate INITA.
635 ;;(message "Initarg: %S replace %s" inita init)
636 (setcar inita init)
639 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
640 ;; checked and SHOULD match the superclass
641 ;; protection. Otherwise an error is thrown. However
642 ;; I wonder if a more flexible schedule might be
643 ;; implemented.
645 ;; EML - We used to have (if prot... here,
646 ;; but a prot of 'nil means public.
648 (let ((super-prot (nth num (eieio--class-protection newc)))
650 (if (not (eq prot super-prot))
651 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
652 prot super-prot a)))
653 ;; End original PLN
655 ;; PLN Tue Jun 26 11:57:06 2007 :
656 ;; Do a non redundant combination of ancient custom
657 ;; groups and new ones.
658 (when custg
659 (let* ((groups
660 (nthcdr num (eieio--class-public-custom-group newc)))
661 (list1 (car groups))
662 (list2 (if (listp custg) custg (list custg))))
663 (if (< (length list1) (length list2))
664 (setq list1 (prog1 list2 (setq list2 list1))))
665 (dolist (elt list2)
666 (unless (memq elt list1)
667 (push elt list1)))
668 (setcar groups list1)))
669 ;; End PLN
671 ;; PLN Mon Jun 25 22:44:34 2007 : If a new cust is
672 ;; set, simply replaces the old one.
673 (when cust
674 ;; (message "Custom type redefined to %s" cust)
675 (setcar (nthcdr num (eieio--class-public-custom newc)) cust))
677 ;; If a new label is specified, it simply replaces
678 ;; the old one.
679 (when label
680 ;; (message "Custom label redefined to %s" label)
681 (setcar (nthcdr num (eieio--class-public-custom-label newc)) label))
682 ;; End PLN
684 ;; PLN Sat Jun 30 17:24:42 2007 : when a new
685 ;; doc is specified, simply replaces the old one.
686 (when doc
687 ;;(message "Documentation redefined to %s" doc)
688 (setcar (nthcdr num (eieio--class-public-doc newc))
689 doc))
690 ;; End PLN
692 ;; If a new printer is specified, it simply replaces
693 ;; the old one.
694 (when print
695 ;; (message "printer redefined to %s" print)
696 (setcar (nthcdr num (eieio--class-public-printer newc)) print))
701 ;; CLASS ALLOCATED SLOTS
702 (let ((value (eieio-default-eval-maybe d)))
703 (if (not (member a (eieio--class-class-allocation-a newc)))
704 (progn
705 (eieio--perform-slot-validation-for-default a type value skipnil)
706 ;; Here we have found a :class version of a slot. This
707 ;; requires a very different approach.
708 (push a (eieio--class-class-allocation-a newc))
709 (push doc (eieio--class-class-allocation-doc newc))
710 (push type (eieio--class-class-allocation-type newc))
711 (push cust (eieio--class-class-allocation-custom newc))
712 (push label (eieio--class-class-allocation-custom-label newc))
713 (push custg (eieio--class-class-allocation-custom-group newc))
714 (push prot (eieio--class-class-allocation-protection newc))
715 ;; Default value is stored in the 'values section, since new objects
716 ;; can't initialize from this element.
717 (push value (eieio--class-class-allocation-values newc)))
718 (when defaultoverride
719 ;; There is a match, and we must override the old value.
720 (let* ((ca (eieio--class-class-allocation-a newc))
721 (np (member a ca))
722 (num (- (length ca) (length np)))
723 (dp (if np
724 (nthcdr num
725 (eieio--class-class-allocation-values newc))
726 nil))
727 (tp (if np (nth num (eieio--class-class-allocation-type newc))
728 nil)))
729 (if (not np)
730 (error "EIEIO internal error overriding default value for %s"
732 ;; If type is passed in, is it the same?
733 (if (not (eq type t))
734 (if (not (equal type tp))
735 (error
736 "Child slot type `%s' does not match inherited type `%s' for `%s'"
737 type tp a)))
738 ;; EML - Note: the only reason to override a class bound slot
739 ;; is to change the default, so allow unbound in.
741 ;; If we have a repeat, only update the value...
742 (eieio--perform-slot-validation-for-default a tp value skipnil)
743 (setcar dp value))
745 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
746 ;; checked and SHOULD match the superclass
747 ;; protection. Otherwise an error is thrown. However
748 ;; I wonder if a more flexible schedule might be
749 ;; implemented.
750 (let ((super-prot
751 (car (nthcdr num (eieio--class-class-allocation-protection newc)))))
752 (if (not (eq prot super-prot))
753 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
754 prot super-prot a)))
755 ;; Do a non redundant combination of ancient custom groups
756 ;; and new ones.
757 (when custg
758 (let* ((groups
759 (nthcdr num (eieio--class-class-allocation-custom-group newc)))
760 (list1 (car groups))
761 (list2 (if (listp custg) custg (list custg))))
762 (if (< (length list1) (length list2))
763 (setq list1 (prog1 list2 (setq list2 list1))))
764 (dolist (elt list2)
765 (unless (memq elt list1)
766 (push elt list1)))
767 (setcar groups list1)))
769 ;; PLN Sat Jun 30 17:24:42 2007 : when a new
770 ;; doc is specified, simply replaces the old one.
771 (when doc
772 ;;(message "Documentation redefined to %s" doc)
773 (setcar (nthcdr num (eieio--class-class-allocation-doc newc))
774 doc))
775 ;; End PLN
777 ;; If a new printer is specified, it simply replaces
778 ;; the old one.
779 (when print
780 ;; (message "printer redefined to %s" print)
781 (setcar (nthcdr num (eieio--class-class-allocation-printer newc)) print))
787 (defun eieio-copy-parents-into-subclass (newc)
788 "Copy into NEWC the slots of PARENTS.
789 Follow the rules of not overwriting early parents when applying to
790 the new child class."
791 (let ((sn (eieio--class-option-assoc (eieio--class-options newc)
792 :allow-nil-initform)))
793 (dolist (pcv (eieio--class-parent newc))
794 ;; First, duplicate all the slots of the parent.
795 (let ((pa (eieio--class-public-a pcv))
796 (pd (eieio--class-public-d pcv))
797 (pdoc (eieio--class-public-doc pcv))
798 (ptype (eieio--class-public-type pcv))
799 (pcust (eieio--class-public-custom pcv))
800 (plabel (eieio--class-public-custom-label pcv))
801 (pcustg (eieio--class-public-custom-group pcv))
802 (printer (eieio--class-public-printer pcv))
803 (pprot (eieio--class-protection pcv))
804 (pinit (eieio--class-initarg-tuples pcv))
805 (i 0))
806 (while pa
807 (eieio--add-new-slot newc
808 (car pa) (car pd) (car pdoc) (aref ptype i)
809 (car pcust) (car plabel) (car pcustg)
810 (car printer)
811 (car pprot) (car-safe (car pinit)) nil nil sn)
812 ;; Increment each value.
813 (setq pa (cdr pa)
814 pd (cdr pd)
815 pdoc (cdr pdoc)
816 i (1+ i)
817 pcust (cdr pcust)
818 plabel (cdr plabel)
819 pcustg (cdr pcustg)
820 printer (cdr printer)
821 pprot (cdr pprot)
822 pinit (cdr pinit))
823 )) ;; while/let
824 ;; Now duplicate all the class alloc slots.
825 (let ((pa (eieio--class-class-allocation-a pcv))
826 (pdoc (eieio--class-class-allocation-doc pcv))
827 (ptype (eieio--class-class-allocation-type pcv))
828 (pcust (eieio--class-class-allocation-custom pcv))
829 (plabel (eieio--class-class-allocation-custom-label pcv))
830 (pcustg (eieio--class-class-allocation-custom-group pcv))
831 (printer (eieio--class-class-allocation-printer pcv))
832 (pprot (eieio--class-class-allocation-protection pcv))
833 (pval (eieio--class-class-allocation-values pcv))
834 (i 0))
835 (while pa
836 (eieio--add-new-slot newc
837 (car pa) (aref pval i) (car pdoc) (aref ptype i)
838 (car pcust) (car plabel) (car pcustg)
839 (car printer)
840 (car pprot) nil :class sn)
841 ;; Increment each value.
842 (setq pa (cdr pa)
843 pdoc (cdr pdoc)
844 pcust (cdr pcust)
845 plabel (cdr plabel)
846 pcustg (cdr pcustg)
847 printer (cdr printer)
848 pprot (cdr pprot)
849 i (1+ i))
850 )))))
853 ;;; Slot type validation
855 ;; This is a hideous hack for replacing `typep' from cl-macs, to avoid
856 ;; requiring the CL library at run-time. It can be eliminated if/when
857 ;; `typep' is merged into Emacs core.
859 (defun eieio--perform-slot-validation (spec value)
860 "Return non-nil if SPEC does not match VALUE."
861 (or (eq spec t) ; t always passes
862 (eq value eieio-unbound) ; unbound always passes
863 (cl-typep value spec)))
865 (defun eieio--validate-slot-value (class slot-idx value slot)
866 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
867 Checks the :type specifier.
868 SLOT is the slot that is being checked, and is only used when throwing
869 an error."
870 (if eieio-skip-typecheck
872 ;; Trim off object IDX junk added in for the object index.
873 (setq slot-idx (- slot-idx (eval-when-compile eieio--object-num-slots)))
874 (let ((st (aref (eieio--class-public-type class) slot-idx)))
875 (if (not (eieio--perform-slot-validation st value))
876 (signal 'invalid-slot-type
877 (list (eieio--class-symbol class) slot st value))))))
879 (defun eieio--validate-class-slot-value (class slot-idx value slot)
880 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
881 Checks the :type specifier.
882 SLOT is the slot that is being checked, and is only used when throwing
883 an error."
884 (if eieio-skip-typecheck
886 (let ((st (aref (eieio--class-class-allocation-type class)
887 slot-idx)))
888 (if (not (eieio--perform-slot-validation st value))
889 (signal 'invalid-slot-type
890 (list (eieio--class-symbol class) slot st value))))))
892 (defun eieio-barf-if-slot-unbound (value instance slotname fn)
893 "Throw a signal if VALUE is a representation of an UNBOUND slot.
894 INSTANCE is the object being referenced. SLOTNAME is the offending
895 slot. If the slot is ok, return VALUE.
896 Argument FN is the function calling this verifier."
897 (if (and (eq value eieio-unbound) (not eieio-skip-typecheck))
898 (slot-unbound instance (eieio--object-class-name instance) slotname fn)
899 value))
902 ;;; Get/Set slots in an object.
904 (defun eieio-oref (obj slot)
905 "Return the value in OBJ at SLOT in the object vector."
906 (cl-check-type slot symbol)
907 (cl-check-type obj (or eieio-object class))
908 (let* ((class (cond ((symbolp obj)
909 (error "eieio-oref called on a class!")
910 (let ((c (eieio--class-v obj)))
911 (if (eieio--class-p c) (eieio-class-un-autoload obj))
913 (t (eieio--object-class-object obj))))
914 (c (eieio--slot-name-index class slot)))
915 (if (not c)
916 ;; It might be missing because it is a :class allocated slot.
917 ;; Let's check that info out.
918 (if (setq c (eieio--class-slot-name-index class slot))
919 ;; Oref that slot.
920 (aref (eieio--class-class-allocation-values class) c)
921 ;; The slot-missing method is a cool way of allowing an object author
922 ;; to intercept missing slot definitions. Since it is also the LAST
923 ;; thing called in this fn, its return value would be retrieved.
924 (slot-missing obj slot 'oref)
925 ;;(signal 'invalid-slot-name (list (eieio-object-name obj) slot))
927 (cl-check-type obj eieio-object)
928 (eieio-barf-if-slot-unbound (aref obj c) obj slot 'oref))))
931 (defun eieio-oref-default (obj slot)
932 "Do the work for the macro `oref-default' with similar parameters.
933 Fills in OBJ's SLOT with its default value."
934 (cl-check-type obj (or eieio-object class))
935 (cl-check-type slot symbol)
936 (let* ((cl (cond ((symbolp obj) (eieio--class-v obj))
937 (t (eieio--object-class-object obj))))
938 (c (eieio--slot-name-index cl slot)))
939 (if (not c)
940 ;; It might be missing because it is a :class allocated slot.
941 ;; Let's check that info out.
942 (if (setq c
943 (eieio--class-slot-name-index cl slot))
944 ;; Oref that slot.
945 (aref (eieio--class-class-allocation-values cl)
947 (slot-missing obj slot 'oref-default)
948 ;;(signal 'invalid-slot-name (list (class-name cl) slot))
950 (eieio-barf-if-slot-unbound
951 (let ((val (nth (- c (eval-when-compile eieio--object-num-slots))
952 (eieio--class-public-d cl))))
953 (eieio-default-eval-maybe val))
954 obj (eieio--class-symbol cl) 'oref-default))))
956 (defun eieio-default-eval-maybe (val)
957 "Check VAL, and return what `oref-default' would provide."
958 ;; FIXME: What the hell is this supposed to do? Shouldn't it evaluate
959 ;; variables as well? Why not just always call `eval'?
960 (cond
961 ;; Is it a function call? If so, evaluate it.
962 ((eieio-eval-default-p val)
963 (eval val))
964 ;;;; check for quoted things, and unquote them
965 ;;((and (consp val) (eq (car val) 'quote))
966 ;; (car (cdr val)))
967 ;; return it verbatim
968 (t val)))
970 (defun eieio-oset (obj slot value)
971 "Do the work for the macro `oset'.
972 Fills in OBJ's SLOT with VALUE."
973 (cl-check-type obj eieio-object)
974 (cl-check-type slot symbol)
975 (let* ((class (eieio--object-class-object obj))
976 (c (eieio--slot-name-index class slot)))
977 (if (not c)
978 ;; It might be missing because it is a :class allocated slot.
979 ;; Let's check that info out.
980 (if (setq c
981 (eieio--class-slot-name-index class slot))
982 ;; Oset that slot.
983 (progn
984 (eieio--validate-class-slot-value class c value slot)
985 (aset (eieio--class-class-allocation-values class)
986 c value))
987 ;; See oref for comment on `slot-missing'
988 (slot-missing obj slot 'oset value)
989 ;;(signal 'invalid-slot-name (list (eieio-object-name obj) slot))
991 (eieio--validate-slot-value class c value slot)
992 (aset obj c value))))
994 (defun eieio-oset-default (class slot value)
995 "Do the work for the macro `oset-default'.
996 Fills in the default value in CLASS' in SLOT with VALUE."
997 (setq class (eieio--class-object class))
998 (cl-check-type class eieio--class)
999 (cl-check-type slot symbol)
1000 (let* ((c (eieio--slot-name-index class slot)))
1001 (if (not c)
1002 ;; It might be missing because it is a :class allocated slot.
1003 ;; Let's check that info out.
1004 (if (setq c (eieio--class-slot-name-index class slot))
1005 (progn
1006 ;; Oref that slot.
1007 (eieio--validate-class-slot-value class c value slot)
1008 (aset (eieio--class-class-allocation-values class) c
1009 value))
1010 (signal 'invalid-slot-name (list (eieio--class-symbol class) slot)))
1011 (eieio--validate-slot-value class c value slot)
1012 ;; Set this into the storage for defaults.
1013 (if (eieio-eval-default-p value)
1014 (error "Can't set default to a sexp that gets evaluated again"))
1015 (setcar (nthcdr (- c (eval-when-compile eieio--object-num-slots))
1016 (eieio--class-public-d class))
1017 value)
1018 ;; Take the value, and put it into our cache object.
1019 (eieio-oset (eieio--class-default-object-cache class)
1020 slot value)
1024 ;;; EIEIO internal search functions
1026 (defun eieio--slot-name-index (class slot)
1027 "In CLASS find the index of the named SLOT.
1028 The slot is a symbol which is installed in CLASS by the `defclass' call.
1029 If SLOT is the value created with :initarg instead,
1030 reverse-lookup that name, and recurse with the associated slot value."
1031 ;; Removed checks to outside this call
1032 (let* ((fsym (gethash slot (eieio--class-symbol-hashtable class)))
1033 (fsi (car fsym)))
1034 (if (integerp fsi)
1035 (+ (eval-when-compile eieio--object-num-slots) fsi)
1036 (let ((fn (eieio--initarg-to-attribute class slot)))
1037 (if fn (eieio--slot-name-index class fn) nil)))))
1039 (defun eieio--class-slot-name-index (class slot)
1040 "In CLASS find the index of the named SLOT.
1041 The slot is a symbol which is installed in CLASS by the `defclass'
1042 call. If SLOT is the value created with :initarg instead,
1043 reverse-lookup that name, and recurse with the associated slot value."
1044 ;; This will happen less often, and with fewer slots. Do this the
1045 ;; storage cheap way.
1046 (let* ((a (eieio--class-class-allocation-a class))
1047 (l1 (length a))
1048 (af (memq slot a))
1049 (l2 (length af)))
1050 ;; Slot # is length of the total list, minus the remaining list of
1051 ;; the found slot.
1052 (if af (- l1 l2))))
1055 ;; Way to assign slots based on a list. Used for constructors, or
1056 ;; even resetting an object at run-time
1058 (defun eieio-set-defaults (obj &optional set-all)
1059 "Take object OBJ, and reset all slots to their defaults.
1060 If SET-ALL is non-nil, then when a default is nil, that value is
1061 reset. If SET-ALL is nil, the slots are only reset if the default is
1062 not nil."
1063 (let ((pub (eieio--class-public-a (eieio--object-class-object obj))))
1064 (while pub
1065 (let ((df (eieio-oref-default obj (car pub))))
1066 (if (or df set-all)
1067 (eieio-oset obj (car pub) df)))
1068 (setq pub (cdr pub)))))
1070 (defun eieio--initarg-to-attribute (class initarg)
1071 "For CLASS, convert INITARG to the actual attribute name.
1072 If there is no translation, pass it in directly (so we can cheat if
1073 need be... May remove that later...)"
1074 (let ((tuple (assoc initarg (eieio--class-initarg-tuples class))))
1075 (if tuple
1076 (cdr tuple)
1077 nil)))
1080 ;; Method Invocation order: C3
1081 (defun eieio--c3-candidate (class remaining-inputs)
1082 "Return CLASS if it can go in the result now, otherwise nil."
1083 ;; Ensure CLASS is not in any position but the first in any of the
1084 ;; element lists of REMAINING-INPUTS.
1085 (and (not (let ((found nil))
1086 (while (and remaining-inputs (not found))
1087 (setq found (member class (cdr (car remaining-inputs)))
1088 remaining-inputs (cdr remaining-inputs)))
1089 found))
1090 class))
1092 (defun eieio--c3-merge-lists (reversed-partial-result remaining-inputs)
1093 "Merge REVERSED-PARTIAL-RESULT REMAINING-INPUTS in a consistent order, if possible.
1094 If a consistent order does not exist, signal an error."
1095 (if (let ((tail remaining-inputs)
1096 (found nil))
1097 (while (and tail (not found))
1098 (setq found (car tail) tail (cdr tail)))
1099 (not found))
1100 ;; If all remaining inputs are empty lists, we are done.
1101 (nreverse reversed-partial-result)
1102 ;; Otherwise, we try to find the next element of the result. This
1103 ;; is achieved by considering the first element of each
1104 ;; (non-empty) input list and accepting a candidate if it is
1105 ;; consistent with the rests of the input lists.
1106 (let* ((found nil)
1107 (tail remaining-inputs)
1108 (next (progn
1109 (while (and tail (not found))
1110 (setq found (and (car tail)
1111 (eieio--c3-candidate (caar tail)
1112 remaining-inputs))
1113 tail (cdr tail)))
1114 found)))
1115 (if next
1116 ;; The graph is consistent so far, add NEXT to result and
1117 ;; merge input lists, dropping NEXT from their heads where
1118 ;; applicable.
1119 (eieio--c3-merge-lists
1120 (cons next reversed-partial-result)
1121 (mapcar (lambda (l) (if (eq (cl-first l) next) (cl-rest l) l))
1122 remaining-inputs))
1123 ;; The graph is inconsistent, give up
1124 (signal 'inconsistent-class-hierarchy (list remaining-inputs))))))
1126 (defun eieio--class-precedence-c3 (class)
1127 "Return all parents of CLASS in c3 order."
1128 (let ((parents (eieio--class-parent (eieio--class-v class))))
1129 (eieio--c3-merge-lists
1130 (list class)
1131 (append
1133 (mapcar #'eieio--class-precedence-c3 parents)
1134 `((,eieio-default-superclass)))
1135 (list parents))))
1138 ;; Method Invocation Order: Depth First
1140 (defun eieio--class-precedence-dfs (class)
1141 "Return all parents of CLASS in depth-first order."
1142 (let* ((parents (eieio--class-parent class))
1143 (classes (copy-sequence
1144 (apply #'append
1145 (list class)
1147 (mapcar
1148 (lambda (parent)
1149 (cons parent
1150 (eieio--class-precedence-dfs parent)))
1151 parents)
1152 `((,eieio-default-superclass))))))
1153 (tail classes))
1154 ;; Remove duplicates.
1155 (while tail
1156 (setcdr tail (delq (car tail) (cdr tail)))
1157 (setq tail (cdr tail)))
1158 classes))
1161 ;; Method Invocation Order: Breadth First
1162 (defun eieio--class-precedence-bfs (class)
1163 "Return all parents of CLASS in breadth-first order."
1164 (let* ((result)
1165 (queue (or (eieio--class-parent class)
1166 `(,eieio-default-superclass))))
1167 (while queue
1168 (let ((head (pop queue)))
1169 (unless (member head result)
1170 (push head result)
1171 (unless (eq head eieio-default-superclass)
1172 (setq queue (append queue (or (eieio--class-parent head)
1173 `(,eieio-default-superclass))))))))
1174 (cons class (nreverse result)))
1178 ;; Method Invocation Order
1180 (defun eieio--class-precedence-list (class)
1181 "Return (transitively closed) list of parents of CLASS.
1182 The order, in which the parents are returned depends on the
1183 method invocation orders of the involved classes."
1184 (if (or (null class) (eq class eieio-default-superclass))
1186 (unless (eieio--class-default-object-cache class)
1187 (eieio-class-un-autoload (eieio--class-symbol class)))
1188 (cl-case (eieio--class-method-invocation-order class)
1189 (:depth-first
1190 (eieio--class-precedence-dfs class))
1191 (:breadth-first
1192 (eieio--class-precedence-bfs class))
1193 (:c3
1194 (eieio--class-precedence-c3 class))))
1196 (define-obsolete-function-alias
1197 'class-precedence-list 'eieio--class-precedence-list "24.4")
1200 ;;; Here are some special types of errors
1202 (define-error 'invalid-slot-name "Invalid slot name")
1203 (define-error 'invalid-slot-type "Invalid slot type")
1204 (define-error 'unbound-slot "Unbound slot")
1205 (define-error 'inconsistent-class-hierarchy "Inconsistent class hierarchy")
1207 ;;; Hooking into cl-generic.
1209 (require 'cl-generic)
1211 ;;;; General support to dispatch based on the type of the argument.
1213 (add-function :before-until cl-generic-tagcode-function
1214 #'eieio--generic-tagcode)
1215 (defun eieio--generic-tagcode (type name)
1216 ;; CLHS says:
1217 ;; A class must be defined before it can be used as a parameter
1218 ;; specializer in a defmethod form.
1219 ;; So we can ignore types that are not known to denote classes.
1220 (and (eieio--class-p (eieio--class-object type))
1221 ;; Use the exact same code as for cl-struct, so that methods
1222 ;; that dispatch on both kinds of objects get to share this
1223 ;; part of the dispatch code.
1224 `(50 . ,(cl--generic-struct-tag name))))
1226 (add-function :before-until cl-generic-tag-types-function
1227 #'eieio--generic-tag-types)
1228 (defun eieio--generic-tag-types (tag)
1229 (and (symbolp tag) (boundp tag) (eieio--class-p (symbol-value tag))
1230 (mapcar #'eieio--class-symbol
1231 (eieio--class-precedence-list (symbol-value tag)))))
1233 ;;;; Dispatch for arguments which are classes.
1235 ;; Since EIEIO does not support metaclasses, users can't easily use the
1236 ;; "dispatch on argument type" for class arguments. That's why EIEIO's
1237 ;; `defmethod' added the :static qualifier. For cl-generic, such a qualifier
1238 ;; would not make much sense (e.g. to which argument should it apply?).
1239 ;; Instead, we add a new "subclass" specializer.
1241 (add-function :before-until cl-generic-tagcode-function
1242 #'eieio--generic-subclass-tagcode)
1243 (defun eieio--generic-subclass-tagcode (type name)
1244 (when (eq 'subclass (car-safe type))
1245 `(60 . (and (symbolp ,name) (eieio--class-v ,name)))))
1247 (add-function :before-until cl-generic-tag-types-function
1248 #'eieio--generic-subclass-tag-types)
1249 (defun eieio--generic-subclass-tag-types (tag)
1250 (when (eieio--class-p tag)
1251 (mapcar (lambda (class)
1252 `(subclass
1253 ,(if (symbolp class) class (eieio--class-symbol class))))
1254 (eieio--class-precedence-list tag))))
1257 ;;;### (autoloads nil "eieio-compat" "eieio-compat.el" "5b04c9a8fff2bd3f3d3ac54aba0f65b7")
1258 ;;; Generated autoloads from eieio-compat.el
1260 (autoload 'eieio--defalias "eieio-compat" "\
1261 Like `defalias', but with less side-effects.
1262 More specifically, it has no side-effects at all when the new function
1263 definition is the same (`eq') as the old one.
1265 \(fn NAME BODY)" nil nil)
1267 (autoload 'defgeneric "eieio-compat" "\
1268 Create a generic function METHOD.
1269 DOC-STRING is the base documentation for this class. A generic
1270 function has no body, as its purpose is to decide which method body
1271 is appropriate to use. Uses `defmethod' to create methods, and calls
1272 `defgeneric' for you. With this implementation the ARGS are
1273 currently ignored. You can use `defgeneric' to apply specialized
1274 top level documentation to a method.
1276 \(fn METHOD ARGS &optional DOC-STRING)" nil t)
1278 (function-put 'defgeneric 'doc-string-elt '3)
1280 (make-obsolete 'defgeneric 'cl-defgeneric '"25.1")
1282 (autoload 'defmethod "eieio-compat" "\
1283 Create a new METHOD through `defgeneric' with ARGS.
1285 The optional second argument KEY is a specifier that
1286 modifies how the method is called, including:
1287 :before - Method will be called before the :primary
1288 :primary - The default if not specified
1289 :after - Method will be called after the :primary
1290 :static - First arg could be an object or class
1291 The next argument is the ARGLIST. The ARGLIST specifies the arguments
1292 to the method as with `defun'. The first argument can have a type
1293 specifier, such as:
1294 ((VARNAME CLASS) ARG2 ...)
1295 where VARNAME is the name of the local variable for the method being
1296 created. The CLASS is a class symbol for a class made with `defclass'.
1297 A DOCSTRING comes after the ARGLIST, and is optional.
1298 All the rest of the args are the BODY of the method. A method will
1299 return the value of the last form in the BODY.
1301 Summary:
1303 (defmethod mymethod [:before | :primary | :after | :static]
1304 ((typearg class-name) arg2 &optional opt &rest rest)
1305 \"doc-string\"
1306 body)
1308 \(fn METHOD &rest ARGS)" nil t)
1310 (function-put 'defmethod 'doc-string-elt '3)
1312 (make-obsolete 'defmethod 'cl-defmethod '"25.1")
1314 (autoload 'eieio--defgeneric-init-form "eieio-compat" "\
1317 \(fn METHOD DOC-STRING)" nil nil)
1319 (autoload 'eieio--defmethod "eieio-compat" "\
1322 \(fn METHOD KIND ARGCLASS CODE)" nil nil)
1324 (autoload 'eieio-defmethod "eieio-compat" "\
1325 Obsolete work part of an old version of the `defmethod' macro.
1327 \(fn METHOD ARGS)" nil nil)
1329 (make-obsolete 'eieio-defmethod 'cl-defmethod '"24.1")
1331 (autoload 'eieio-defgeneric "eieio-compat" "\
1332 Obsolete work part of an old version of the `defgeneric' macro.
1334 \(fn METHOD DOC-STRING)" nil nil)
1336 (make-obsolete 'eieio-defgeneric 'cl-defgeneric '"24.1")
1338 (autoload 'eieio-defclass "eieio-compat" "\
1341 \(fn CNAME SUPERCLASSES SLOTS OPTIONS)" nil nil)
1343 (make-obsolete 'eieio-defclass 'eieio-defclass-internal '"25.1")
1345 ;;;***
1348 (provide 'eieio-core)
1350 ;;; eieio-core.el ends here