]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/eieio-core.el
Merge from origin/emacs-24
[gnu-emacs] / lisp / emacs-lisp / eieio-core.el
1 ;;; eieio-core.el --- Core implementation for eieio -*- lexical-binding:t -*-
2
3 ;; Copyright (C) 1995-1996, 1998-2015 Free Software Foundation, Inc.
4
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Version: 1.4
7 ;; Keywords: OO, lisp
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25 ;;
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.
29 ;;
30 ;; See the commentary for eieio.el for more about EIEIO itself.
31
32 ;;; Code:
33
34 (require 'cl-lib)
35 (require 'pcase)
36
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
44 \f
45 ;;;
46 ;; Variable declarations.
47 ;;
48 (defvar eieio-hook nil
49 "This hook is executed, then cleared each time `defclass' is called.")
50
51 (defvar eieio-error-unsupported-class-tags nil
52 "Non-nil to throw an error if an encountered tag is unsupported.
53 This may prevent classes from CLOS applications from being used with EIEIO
54 since EIEIO does not support all CLOS tags.")
55
56 (defvar eieio-skip-typecheck nil
57 "If non-nil, skip all slot typechecking.
58 Set this to t permanently if a program is functioning well to get a
59 small speed increase. This variable is also used internally to handle
60 default setting for optimization purposes.")
61
62 (defvar eieio-optimize-primary-methods-flag t
63 "Non-nil means to optimize the method dispatch on primary methods.")
64
65 (defvar eieio-backward-compatibility t
66 "If nil, drop support for some behaviors of older versions of EIEIO.
67 Currently under control of this var:
68 - Define every class as a var whose value is the class symbol.
69 - Define <class>-child-p and <class>-list-p predicates.
70 - Allow object names in constructors.")
71
72 (defconst eieio-unbound
73 (if (and (boundp 'eieio-unbound) (symbolp eieio-unbound))
74 eieio-unbound
75 (make-symbol "unbound"))
76 "Uninterned symbol representing an unbound slot in an object.")
77
78 ;; This is a bootstrap for eieio-default-superclass so it has a value
79 ;; while it is being built itself.
80 (defvar eieio-default-superclass nil)
81
82 (progn
83 ;; Arrange for field access not to bother checking if the access is indeed
84 ;; made to an eieio--class object.
85 (cl-declaim (optimize (safety 0)))
86 (cl-defstruct (eieio--class
87 (:constructor nil)
88 (:constructor eieio--class-make (symbol &aux (tag 'defclass)))
89 (:type vector)
90 (:copier nil))
91 ;; We use an untagged cl-struct, with our own hand-made tag as first field
92 ;; (containing the symbol `defclass'). It would be better to use a normal
93 ;; cl-struct with its normal tag (e.g. so that cl-defstruct can define the
94 ;; predicate for us), but that breaks compatibility with .elc files compiled
95 ;; against older versions of EIEIO.
96 tag
97 symbol ;; symbol (self-referencing)
98 parent children
99 symbol-hashtable ;; hashtable permitting fast access to variable position indexes
100 ;; @todo
101 ;; the word "public" here is leftovers from the very first version.
102 ;; Get rid of it!
103 public-a ;; class attribute index
104 public-d ;; class attribute defaults index
105 public-doc ;; class documentation strings for attributes
106 public-type ;; class type for a slot
107 public-custom ;; class custom type for a slot
108 public-custom-label ;; class custom group for a slot
109 public-custom-group ;; class custom group for a slot
110 public-printer ;; printer for a slot
111 protection ;; protection for a slot
112 initarg-tuples ;; initarg tuples list
113 class-allocation-a ;; class allocated attributes
114 class-allocation-doc ;; class allocated documentation
115 class-allocation-type ;; class allocated value type
116 class-allocation-custom ;; class allocated custom descriptor
117 class-allocation-custom-label ;; class allocated custom descriptor
118 class-allocation-custom-group ;; class allocated custom group
119 class-allocation-printer ;; class allocated printer for a slot
120 class-allocation-protection ;; class allocated protection list
121 class-allocation-values ;; class allocated value vector
122 default-object-cache ;; what a newly created object would look like.
123 ; This will speed up instantiation time as
124 ; only a `copy-sequence' will be needed, instead of
125 ; looping over all the values and setting them from
126 ; the default.
127 options ;; storage location of tagged class option
128 ; Stored outright without modifications or stripping
129 )
130 ;; Set it back to the default value.
131 (cl-declaim (optimize (safety 1))))
132
133
134 (cl-defstruct (eieio--object
135 (:type vector) ;We manage our own tagging system.
136 (:constructor nil)
137 (:copier nil))
138 ;; `class-tag' holds a symbol, which is not the class name, but is instead
139 ;; properly prefixed as an internal EIEIO thingy and which holds the class
140 ;; object/struct in its `symbol-value' slot.
141 class-tag)
142
143 (eval-and-compile
144 (defconst eieio--object-num-slots
145 (length (get 'eieio--object 'cl-struct-slots))))
146
147 (defsubst eieio--object-class-object (obj)
148 (symbol-value (eieio--object-class-tag obj)))
149
150 (defsubst eieio--object-class-name (obj)
151 ;; FIXME: Most uses of this function should be changed to use
152 ;; eieio--object-class-object instead!
153 (eieio--class-symbol (eieio--object-class-object obj)))
154
155 \f
156 ;;; Important macros used internally in eieio.
157 ;;
158 (defmacro eieio--check-type (type obj)
159 (unless (symbolp obj)
160 (error "eieio--check-type wants OBJ to be a variable"))
161 `(if (not ,(cond
162 ((eq 'or (car-safe type))
163 `(or ,@(mapcar (lambda (type) `(,type ,obj)) (cdr type))))
164 (t `(,type ,obj))))
165 (signal 'wrong-type-argument (list ',type ,obj))))
166
167 (defmacro eieio--class-v (class) ;Use a macro, so it acts as a GV place.
168 "Internal: Return the class vector from the CLASS symbol."
169 (declare (debug t))
170 ;; No check: If eieio gets this far, it has probably been checked already.
171 `(get ,class 'eieio-class-definition))
172
173 (defsubst eieio--class-object (class)
174 "Return the class object."
175 (if (symbolp class)
176 ;; Keep the symbol if class-v is nil, for better error messages.
177 (or (eieio--class-v class) class)
178 class))
179
180 (defsubst eieio--class-p (class)
181 "Return non-nil if CLASS is a valid class object."
182 (condition-case nil
183 (eq (aref class 0) 'defclass)
184 (error nil)))
185
186 (defsubst eieio-class-object (class)
187 "Check that CLASS is a class and return the corresponding object."
188 (let ((c (eieio--class-object class)))
189 (eieio--check-type eieio--class-p c)
190 c))
191
192 (defsubst class-p (class)
193 "Return non-nil if CLASS is a valid class vector.
194 CLASS is a symbol." ;FIXME: Is it a vector or a symbol?
195 ;; this new method is faster since it doesn't waste time checking lots of
196 ;; things.
197 (condition-case nil
198 (eq (aref (eieio--class-v class) 0) 'defclass)
199 (error nil)))
200
201 (defun eieio-class-name (class)
202 "Return a Lisp like symbol name for CLASS."
203 ;; FIXME: What's a "Lisp like symbol name"?
204 ;; FIXME: CLOS returns a symbol, but the code returns a string.
205 (if (eieio--class-p class) (setq class (eieio--class-symbol class)))
206 (eieio--check-type class-p class)
207 ;; I think this is supposed to return a symbol, but to me CLASS is a symbol,
208 ;; and I wanted a string. Arg!
209 (format "#<class %s>" (symbol-name class)))
210 (define-obsolete-function-alias 'class-name #'eieio-class-name "24.4")
211
212 (defalias 'eieio--class-constructor #'identity
213 "Return the symbol representing the constructor of CLASS.")
214
215 (defmacro eieio--class-option-assoc (list option)
216 "Return from LIST the found OPTION, or nil if it doesn't exist."
217 `(car-safe (cdr (memq ,option ,list))))
218
219 (defsubst eieio--class-option (class option)
220 "Return the value stored for CLASS' OPTION.
221 Return nil if that option doesn't exist."
222 (eieio--class-option-assoc (eieio--class-options class) option))
223
224 (defsubst eieio-object-p (obj)
225 "Return non-nil if OBJ is an EIEIO object."
226 (and (vectorp obj)
227 (condition-case nil
228 (eq (aref (eieio--object-class-object obj) 0) 'defclass)
229 (error nil))))
230
231 (defalias 'object-p 'eieio-object-p)
232
233 (defsubst class-abstract-p (class)
234 "Return non-nil if CLASS is abstract.
235 Abstract classes cannot be instantiated."
236 (eieio--class-option (eieio--class-v class) :abstract))
237
238 (defsubst eieio--class-method-invocation-order (class)
239 "Return the invocation order of CLASS.
240 Abstract classes cannot be instantiated."
241 (or (eieio--class-option class :method-invocation-order)
242 :breadth-first))
243
244
245 \f
246 ;;;
247 ;; Class Creation
248
249 (defvar eieio-defclass-autoload-map (make-hash-table)
250 "Symbol map of superclasses we find in autoloads.")
251
252 ;; We autoload this because it's used in `make-autoload'.
253 ;;;###autoload
254 (defun eieio-defclass-autoload (cname _superclasses filename doc)
255 "Create autoload symbols for the EIEIO class CNAME.
256 SUPERCLASSES are the superclasses that CNAME inherits from.
257 DOC is the docstring for CNAME.
258 This function creates a mock-class for CNAME and adds it into
259 SUPERCLASSES as children.
260 It creates an autoload function for CNAME's constructor."
261 ;; Assume we've already debugged inputs.
262
263 ;; We used to store the list of superclasses in the `parent' slot (as a list
264 ;; of class names). But now this slot holds a list of class objects, and
265 ;; those parents may not exist yet, so the corresponding class objects may
266 ;; simply not exist yet. So instead we just don't store the list of parents
267 ;; here in eieio-defclass-autoload at all, since it seems that they're just
268 ;; not needed before the class is actually loaded.
269 (let* ((oldc (when (class-p cname) (eieio--class-v cname)))
270 (newc (eieio--class-make cname))
271 )
272 (if oldc
273 nil ;; Do nothing if we already have this class.
274
275 ;; turn this into a usable self-pointing symbol
276 (when eieio-backward-compatibility
277 (set cname cname)
278 (make-obsolete-variable cname (format "use '%s instead" cname) "25.1"))
279
280 ;; Store the new class vector definition into the symbol. We need to
281 ;; do this first so that we can call defmethod for the accessor.
282 ;; The vector will be updated by the following while loop and will not
283 ;; need to be stored a second time.
284 (setf (eieio--class-v cname) newc)
285
286 ;; Create an autoload on top of our constructor function.
287 (autoload cname filename doc nil nil)
288 (autoload (intern (format "%s-p" cname)) filename "" nil nil)
289 (when eieio-backward-compatibility
290 (autoload (intern (format "%s-child-p" cname)) filename "" nil nil)
291 (autoload (intern (format "%s-list-p" cname)) filename "" nil nil)))))
292
293 (defsubst eieio-class-un-autoload (cname)
294 "If class CNAME is in an autoload state, load its file."
295 (autoload-do-load (symbol-function cname))) ; cname
296
297 (cl-deftype list-of (elem-type)
298 `(and list
299 (satisfies (lambda (list)
300 (cl-every (lambda (elem) (cl-typep elem ',elem-type))
301 list)))))
302
303 (declare-function eieio--defmethod "eieio-generic" (method kind argclass code))
304
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)
315
316 (let* ((pname superclasses)
317 (oldc (when (class-p cname) (eieio--class-v cname)))
318 (newc (if (and oldc (not (eieio--class-default-object-cache oldc)))
319 ;; The oldc class is a stub setup by eieio-defclass-autoload.
320 ;; Reuse it instead of creating a new one, so that existing
321 ;; references are still valid.
322 oldc
323 (eieio--class-make cname)))
324 (groups nil) ;; list of groups id'd from slots
325 (clearparent nil))
326
327 ;; If this class already existed, and we are updating its structure,
328 ;; make sure we keep the old child list. This can cause bugs, but
329 ;; if no new slots are created, it also saves time, and prevents
330 ;; method table breakage, particularly when the users is only
331 ;; byte compiling an EIEIO file.
332 (if oldc
333 (setf (eieio--class-children newc) (eieio--class-children oldc))
334 ;; If the old class did not exist, but did exist in the autoload map,
335 ;; then adopt those children. This is like the above, but deals with
336 ;; autoloads nicely.
337 (let ((children (gethash cname eieio-defclass-autoload-map)))
338 (when children
339 (setf (eieio--class-children newc) children)
340 (remhash cname eieio-defclass-autoload-map))))
341
342 (if pname
343 (progn
344 (dolist (p pname)
345 (if (and p (symbolp p))
346 (if (not (class-p p))
347 ;; bad class
348 (error "Given parent class %S is not a class" p)
349 ;; good parent class...
350 ;; save new child in parent
351 (cl-pushnew cname (eieio--class-children (eieio--class-v p)))
352 ;; Get custom groups, and store them into our local copy.
353 (mapc (lambda (g) (cl-pushnew g groups :test #'equal))
354 (eieio--class-option (eieio--class-v p) :custom-groups))
355 ;; save parent in child
356 (push (eieio--class-v p) (eieio--class-parent newc)))
357 (error "Invalid parent class %S" p)))
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))))
370
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"))
375
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")))
396
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 superclasses)
400
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)
406
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))
422
423 (skip-nil (eieio--class-option-assoc options :allow-nil-initform))
424 )
425
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)))))
433
434 ;; The default type specifier is supposed to be t, meaning anything.
435 (if (not type) (setq type t))
436
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"))))
444
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)
455
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)
459
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))
463 ))
464
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))
478
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))
483
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))
487
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))
502
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))
507
508 ;; Save the file location where this class is defined.
509 (let ((fname (if load-in-progress
510 load-file-name
511 buffer-file-name)))
512 (when fname
513 (when (string-match "\\.elc\\'" fname)
514 (setq fname (substring fname 0 (1- (length fname)))))
515 (put cname 'class-location fname)))
516
517 ;; We have a list of custom groups. Store them into the options.
518 (let ((g (eieio--class-option-assoc options :custom-groups)))
519 (mapc (lambda (cg) (cl-pushnew cg g :test 'equal)) groups)
520 (if (memq :custom-groups options)
521 (setcar (cdr (memq :custom-groups options)) g)
522 (setq options (cons :custom-groups (cons g options)))))
523
524 ;; Set up the options we have collected.
525 (setf (eieio--class-options newc) options)
526
527 ;; if this is a superclass, clear out parent (which was set to the
528 ;; default superclass eieio-default-superclass)
529 (if clearparent (setf (eieio--class-parent newc) nil))
530
531 ;; Create the cached default object.
532 (let ((cache (make-vector (+ (length (eieio--class-public-a newc))
533 (eval-when-compile eieio--object-num-slots))
534 nil))
535 ;; We don't strictly speaking need to use a symbol, but the old
536 ;; code used the class's name rather than the class's object, so
537 ;; we follow this preference for using a symbol, which is probably
538 ;; convenient to keep the printed representation of such Elisp
539 ;; objects readable.
540 (tag (intern (format "eieio-class-tag--%s" cname))))
541 (set tag newc)
542 (setf (eieio--object-class-tag cache) tag)
543 (let ((eieio-skip-typecheck t))
544 ;; All type-checking has been done to our satisfaction
545 ;; before this call. Don't waste our time in this call..
546 (eieio-set-defaults cache t))
547 (setf (eieio--class-default-object-cache newc) cache))
548
549 ;; Return our new class object
550 ;; newc
551 cname
552 ))
553
554 (defsubst eieio-eval-default-p (val)
555 "Whether the default value VAL should be evaluated for use."
556 (and (consp val) (symbolp (car val)) (fboundp (car val))))
557
558 (defun eieio--perform-slot-validation-for-default (slot spec value skipnil)
559 "For SLOT, signal if SPEC does not match VALUE.
560 If SKIPNIL is non-nil, then if VALUE is nil return t instead."
561 (if (not (or (eieio-eval-default-p value) ;FIXME: Why?
562 eieio-skip-typecheck
563 (and skipnil (null value))
564 (eieio--perform-slot-validation spec value)))
565 (signal 'invalid-slot-type (list slot spec value))))
566
567 (defun eieio--add-new-slot (newc a d doc type cust label custg print prot init alloc
568 &optional defaultoverride skipnil)
569 "Add into NEWC attribute A.
570 If A already exists in NEWC, then do nothing. If it doesn't exist,
571 then also add in D (default), DOC, TYPE, CUST, LABEL, CUSTG, PRINT, PROT, and INIT arg.
572 Argument ALLOC specifies if the slot is allocated per instance, or per class.
573 If optional DEFAULTOVERRIDE is non-nil, then if A exists in NEWC,
574 we must override its value for a default.
575 Optional argument SKIPNIL indicates if type checking should be skipped
576 if default value is nil."
577 ;; Make sure we duplicate those items that are sequences.
578 (condition-case nil
579 (if (sequencep d) (setq d (copy-sequence d)))
580 ;; This copy can fail on a cons cell with a non-cons in the cdr. Let's skip it if it doesn't work.
581 (error nil))
582 (if (sequencep type) (setq type (copy-sequence type)))
583 (if (sequencep cust) (setq cust (copy-sequence cust)))
584 (if (sequencep custg) (setq custg (copy-sequence custg)))
585
586 ;; To prevent override information w/out specification of storage,
587 ;; we need to do this little hack.
588 (if (member a (eieio--class-class-allocation-a newc)) (setq alloc :class))
589
590 (if (or (not alloc) (and (symbolp alloc) (eq alloc :instance)))
591 ;; In this case, we modify the INSTANCE version of a given slot.
592
593 (progn
594
595 ;; Only add this element if it is so-far unique
596 (if (not (member a (eieio--class-public-a newc)))
597 (progn
598 (eieio--perform-slot-validation-for-default a type d skipnil)
599 (push a (eieio--class-public-a newc))
600 (push d (eieio--class-public-d newc))
601 (push doc (eieio--class-public-doc newc))
602 (push type (eieio--class-public-type newc))
603 (push cust (eieio--class-public-custom newc))
604 (push label (eieio--class-public-custom-label newc))
605 (push custg (eieio--class-public-custom-group newc))
606 (push print (eieio--class-public-printer newc))
607 (push prot (eieio--class-protection newc))
608 (setf (eieio--class-initarg-tuples newc) (cons (cons init a) (eieio--class-initarg-tuples newc)))
609 )
610 ;; When defaultoverride is true, we are usually adding new local
611 ;; attributes which must override the default value of any slot
612 ;; passed in by one of the parent classes.
613 (when defaultoverride
614 ;; There is a match, and we must override the old value.
615 (let* ((ca (eieio--class-public-a newc))
616 (np (member a ca))
617 (num (- (length ca) (length np)))
618 (dp (if np (nthcdr num (eieio--class-public-d newc))
619 nil))
620 (tp (if np (nth num (eieio--class-public-type newc))))
621 )
622 (if (not np)
623 (error "EIEIO internal error overriding default value for %s"
624 a)
625 ;; If type is passed in, is it the same?
626 (if (not (eq type t))
627 (if (not (equal type tp))
628 (error
629 "Child slot type `%s' does not match inherited type `%s' for `%s'"
630 type tp a)))
631 ;; If we have a repeat, only update the initarg...
632 (unless (eq d eieio-unbound)
633 (eieio--perform-slot-validation-for-default a tp d skipnil)
634 (setcar dp d))
635 ;; If we have a new initarg, check for it.
636 (when init
637 (let* ((inits (eieio--class-initarg-tuples newc))
638 (inita (rassq a inits)))
639 ;; Replace the CAR of the associate INITA.
640 ;;(message "Initarg: %S replace %s" inita init)
641 (setcar inita init)
642 ))
643
644 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
645 ;; checked and SHOULD match the superclass
646 ;; protection. Otherwise an error is thrown. However
647 ;; I wonder if a more flexible schedule might be
648 ;; implemented.
649 ;;
650 ;; EML - We used to have (if prot... here,
651 ;; but a prot of 'nil means public.
652 ;;
653 (let ((super-prot (nth num (eieio--class-protection newc)))
654 )
655 (if (not (eq prot super-prot))
656 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
657 prot super-prot a)))
658 ;; End original PLN
659
660 ;; PLN Tue Jun 26 11:57:06 2007 :
661 ;; Do a non redundant combination of ancient custom
662 ;; groups and new ones.
663 (when custg
664 (let* ((groups
665 (nthcdr num (eieio--class-public-custom-group newc)))
666 (list1 (car groups))
667 (list2 (if (listp custg) custg (list custg))))
668 (if (< (length list1) (length list2))
669 (setq list1 (prog1 list2 (setq list2 list1))))
670 (dolist (elt list2)
671 (unless (memq elt list1)
672 (push elt list1)))
673 (setcar groups list1)))
674 ;; End PLN
675
676 ;; PLN Mon Jun 25 22:44:34 2007 : If a new cust is
677 ;; set, simply replaces the old one.
678 (when cust
679 ;; (message "Custom type redefined to %s" cust)
680 (setcar (nthcdr num (eieio--class-public-custom newc)) cust))
681
682 ;; If a new label is specified, it simply replaces
683 ;; the old one.
684 (when label
685 ;; (message "Custom label redefined to %s" label)
686 (setcar (nthcdr num (eieio--class-public-custom-label newc)) label))
687 ;; End PLN
688
689 ;; PLN Sat Jun 30 17:24:42 2007 : when a new
690 ;; doc is specified, simply replaces the old one.
691 (when doc
692 ;;(message "Documentation redefined to %s" doc)
693 (setcar (nthcdr num (eieio--class-public-doc newc))
694 doc))
695 ;; End PLN
696
697 ;; If a new printer is specified, it simply replaces
698 ;; the old one.
699 (when print
700 ;; (message "printer redefined to %s" print)
701 (setcar (nthcdr num (eieio--class-public-printer newc)) print))
702
703 )))
704 ))
705
706 ;; CLASS ALLOCATED SLOTS
707 (let ((value (eieio-default-eval-maybe d)))
708 (if (not (member a (eieio--class-class-allocation-a newc)))
709 (progn
710 (eieio--perform-slot-validation-for-default a type value skipnil)
711 ;; Here we have found a :class version of a slot. This
712 ;; requires a very different approach.
713 (push a (eieio--class-class-allocation-a newc))
714 (push doc (eieio--class-class-allocation-doc newc))
715 (push type (eieio--class-class-allocation-type newc))
716 (push cust (eieio--class-class-allocation-custom newc))
717 (push label (eieio--class-class-allocation-custom-label newc))
718 (push custg (eieio--class-class-allocation-custom-group newc))
719 (push prot (eieio--class-class-allocation-protection newc))
720 ;; Default value is stored in the 'values section, since new objects
721 ;; can't initialize from this element.
722 (push value (eieio--class-class-allocation-values newc)))
723 (when defaultoverride
724 ;; There is a match, and we must override the old value.
725 (let* ((ca (eieio--class-class-allocation-a newc))
726 (np (member a ca))
727 (num (- (length ca) (length np)))
728 (dp (if np
729 (nthcdr num
730 (eieio--class-class-allocation-values newc))
731 nil))
732 (tp (if np (nth num (eieio--class-class-allocation-type newc))
733 nil)))
734 (if (not np)
735 (error "EIEIO internal error overriding default value for %s"
736 a)
737 ;; If type is passed in, is it the same?
738 (if (not (eq type t))
739 (if (not (equal type tp))
740 (error
741 "Child slot type `%s' does not match inherited type `%s' for `%s'"
742 type tp a)))
743 ;; EML - Note: the only reason to override a class bound slot
744 ;; is to change the default, so allow unbound in.
745
746 ;; If we have a repeat, only update the value...
747 (eieio--perform-slot-validation-for-default a tp value skipnil)
748 (setcar dp value))
749
750 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
751 ;; checked and SHOULD match the superclass
752 ;; protection. Otherwise an error is thrown. However
753 ;; I wonder if a more flexible schedule might be
754 ;; implemented.
755 (let ((super-prot
756 (car (nthcdr num (eieio--class-class-allocation-protection newc)))))
757 (if (not (eq prot super-prot))
758 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
759 prot super-prot a)))
760 ;; Do a non redundant combination of ancient custom groups
761 ;; and new ones.
762 (when custg
763 (let* ((groups
764 (nthcdr num (eieio--class-class-allocation-custom-group newc)))
765 (list1 (car groups))
766 (list2 (if (listp custg) custg (list custg))))
767 (if (< (length list1) (length list2))
768 (setq list1 (prog1 list2 (setq list2 list1))))
769 (dolist (elt list2)
770 (unless (memq elt list1)
771 (push elt list1)))
772 (setcar groups list1)))
773
774 ;; PLN Sat Jun 30 17:24:42 2007 : when a new
775 ;; doc is specified, simply replaces the old one.
776 (when doc
777 ;;(message "Documentation redefined to %s" doc)
778 (setcar (nthcdr num (eieio--class-class-allocation-doc newc))
779 doc))
780 ;; End PLN
781
782 ;; If a new printer is specified, it simply replaces
783 ;; the old one.
784 (when print
785 ;; (message "printer redefined to %s" print)
786 (setcar (nthcdr num (eieio--class-class-allocation-printer newc)) print))
787
788 ))
789 ))
790 ))
791
792 (defun eieio-copy-parents-into-subclass (newc _parents)
793 "Copy into NEWC the slots of PARENTS.
794 Follow the rules of not overwriting early parents when applying to
795 the new child class."
796 (let ((sn (eieio--class-option-assoc (eieio--class-options newc)
797 :allow-nil-initform)))
798 (dolist (pcv (eieio--class-parent newc))
799 ;; First, duplicate all the slots of the parent.
800 (let ((pa (eieio--class-public-a pcv))
801 (pd (eieio--class-public-d pcv))
802 (pdoc (eieio--class-public-doc pcv))
803 (ptype (eieio--class-public-type pcv))
804 (pcust (eieio--class-public-custom pcv))
805 (plabel (eieio--class-public-custom-label pcv))
806 (pcustg (eieio--class-public-custom-group pcv))
807 (printer (eieio--class-public-printer pcv))
808 (pprot (eieio--class-protection pcv))
809 (pinit (eieio--class-initarg-tuples pcv))
810 (i 0))
811 (while pa
812 (eieio--add-new-slot newc
813 (car pa) (car pd) (car pdoc) (aref ptype i)
814 (car pcust) (car plabel) (car pcustg)
815 (car printer)
816 (car pprot) (car-safe (car pinit)) nil nil sn)
817 ;; Increment each value.
818 (setq pa (cdr pa)
819 pd (cdr pd)
820 pdoc (cdr pdoc)
821 i (1+ i)
822 pcust (cdr pcust)
823 plabel (cdr plabel)
824 pcustg (cdr pcustg)
825 printer (cdr printer)
826 pprot (cdr pprot)
827 pinit (cdr pinit))
828 )) ;; while/let
829 ;; Now duplicate all the class alloc slots.
830 (let ((pa (eieio--class-class-allocation-a pcv))
831 (pdoc (eieio--class-class-allocation-doc pcv))
832 (ptype (eieio--class-class-allocation-type pcv))
833 (pcust (eieio--class-class-allocation-custom pcv))
834 (plabel (eieio--class-class-allocation-custom-label pcv))
835 (pcustg (eieio--class-class-allocation-custom-group pcv))
836 (printer (eieio--class-class-allocation-printer pcv))
837 (pprot (eieio--class-class-allocation-protection pcv))
838 (pval (eieio--class-class-allocation-values pcv))
839 (i 0))
840 (while pa
841 (eieio--add-new-slot newc
842 (car pa) (aref pval i) (car pdoc) (aref ptype i)
843 (car pcust) (car plabel) (car pcustg)
844 (car printer)
845 (car pprot) nil :class sn)
846 ;; Increment each value.
847 (setq pa (cdr pa)
848 pdoc (cdr pdoc)
849 pcust (cdr pcust)
850 plabel (cdr plabel)
851 pcustg (cdr pcustg)
852 printer (cdr printer)
853 pprot (cdr pprot)
854 i (1+ i))
855 )))))
856
857 \f
858 ;;; Slot type validation
859
860 ;; This is a hideous hack for replacing `typep' from cl-macs, to avoid
861 ;; requiring the CL library at run-time. It can be eliminated if/when
862 ;; `typep' is merged into Emacs core.
863
864 (defun eieio--perform-slot-validation (spec value)
865 "Return non-nil if SPEC does not match VALUE."
866 (or (eq spec t) ; t always passes
867 (eq value eieio-unbound) ; unbound always passes
868 (cl-typep value spec)))
869
870 (defun eieio--validate-slot-value (class slot-idx value slot)
871 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
872 Checks the :type specifier.
873 SLOT is the slot that is being checked, and is only used when throwing
874 an error."
875 (if eieio-skip-typecheck
876 nil
877 ;; Trim off object IDX junk added in for the object index.
878 (setq slot-idx (- slot-idx (eval-when-compile eieio--object-num-slots)))
879 (let ((st (aref (eieio--class-public-type class) slot-idx)))
880 (if (not (eieio--perform-slot-validation st value))
881 (signal 'invalid-slot-type
882 (list (eieio--class-symbol class) slot st value))))))
883
884 (defun eieio--validate-class-slot-value (class slot-idx value slot)
885 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
886 Checks the :type specifier.
887 SLOT is the slot that is being checked, and is only used when throwing
888 an error."
889 (if eieio-skip-typecheck
890 nil
891 (let ((st (aref (eieio--class-class-allocation-type class)
892 slot-idx)))
893 (if (not (eieio--perform-slot-validation st value))
894 (signal 'invalid-slot-type
895 (list (eieio--class-symbol class) slot st value))))))
896
897 (defun eieio-barf-if-slot-unbound (value instance slotname fn)
898 "Throw a signal if VALUE is a representation of an UNBOUND slot.
899 INSTANCE is the object being referenced. SLOTNAME is the offending
900 slot. If the slot is ok, return VALUE.
901 Argument FN is the function calling this verifier."
902 (if (and (eq value eieio-unbound) (not eieio-skip-typecheck))
903 (slot-unbound instance (eieio--object-class-name instance) slotname fn)
904 value))
905
906 \f
907 ;;; Get/Set slots in an object.
908 ;;
909 (defun eieio-oref (obj slot)
910 "Return the value in OBJ at SLOT in the object vector."
911 (eieio--check-type (or eieio-object-p class-p) obj)
912 (eieio--check-type symbolp slot)
913 (if (class-p obj) (eieio-class-un-autoload obj))
914 (let* ((class (cond ((symbolp obj)
915 (error "eieio-oref called on a class!")
916 (eieio--class-v obj))
917 (t (eieio--object-class-object obj))))
918 (c (eieio--slot-name-index class obj slot)))
919 (if (not c)
920 ;; It might be missing because it is a :class allocated slot.
921 ;; Let's check that info out.
922 (if (setq c (eieio--class-slot-name-index class slot))
923 ;; Oref that slot.
924 (aref (eieio--class-class-allocation-values class) c)
925 ;; The slot-missing method is a cool way of allowing an object author
926 ;; to intercept missing slot definitions. Since it is also the LAST
927 ;; thing called in this fn, its return value would be retrieved.
928 (slot-missing obj slot 'oref)
929 ;;(signal 'invalid-slot-name (list (eieio-object-name obj) slot))
930 )
931 (eieio--check-type eieio-object-p obj)
932 (eieio-barf-if-slot-unbound (aref obj c) obj slot 'oref))))
933
934
935 (defun eieio-oref-default (obj slot)
936 "Do the work for the macro `oref-default' with similar parameters.
937 Fills in OBJ's SLOT with its default value."
938 (eieio--check-type (or eieio-object-p class-p) obj)
939 (eieio--check-type symbolp slot)
940 (let* ((cl (cond ((symbolp obj) (eieio--class-v obj))
941 (t (eieio--object-class-object obj))))
942 (c (eieio--slot-name-index cl obj slot)))
943 (if (not c)
944 ;; It might be missing because it is a :class allocated slot.
945 ;; Let's check that info out.
946 (if (setq c
947 (eieio--class-slot-name-index cl slot))
948 ;; Oref that slot.
949 (aref (eieio--class-class-allocation-values cl)
950 c)
951 (slot-missing obj slot 'oref-default)
952 ;;(signal 'invalid-slot-name (list (class-name cl) slot))
953 )
954 (eieio-barf-if-slot-unbound
955 (let ((val (nth (- c (eval-when-compile eieio--object-num-slots))
956 (eieio--class-public-d cl))))
957 (eieio-default-eval-maybe val))
958 obj (eieio--class-symbol cl) 'oref-default))))
959
960 (defun eieio-default-eval-maybe (val)
961 "Check VAL, and return what `oref-default' would provide."
962 ;; FIXME: What the hell is this supposed to do? Shouldn't it evaluate
963 ;; variables as well? Why not just always call `eval'?
964 (cond
965 ;; Is it a function call? If so, evaluate it.
966 ((eieio-eval-default-p val)
967 (eval val))
968 ;;;; check for quoted things, and unquote them
969 ;;((and (consp val) (eq (car val) 'quote))
970 ;; (car (cdr val)))
971 ;; return it verbatim
972 (t val)))
973
974 (defun eieio-oset (obj slot value)
975 "Do the work for the macro `oset'.
976 Fills in OBJ's SLOT with VALUE."
977 (eieio--check-type eieio-object-p obj)
978 (eieio--check-type symbolp slot)
979 (let* ((class (eieio--object-class-object obj))
980 (c (eieio--slot-name-index class obj slot)))
981 (if (not c)
982 ;; It might be missing because it is a :class allocated slot.
983 ;; Let's check that info out.
984 (if (setq c
985 (eieio--class-slot-name-index class slot))
986 ;; Oset that slot.
987 (progn
988 (eieio--validate-class-slot-value class c value slot)
989 (aset (eieio--class-class-allocation-values class)
990 c value))
991 ;; See oref for comment on `slot-missing'
992 (slot-missing obj slot 'oset value)
993 ;;(signal 'invalid-slot-name (list (eieio-object-name obj) slot))
994 )
995 (eieio--validate-slot-value class c value slot)
996 (aset obj c value))))
997
998 (defun eieio-oset-default (class slot value)
999 "Do the work for the macro `oset-default'.
1000 Fills in the default value in CLASS' in SLOT with VALUE."
1001 (setq class (eieio--class-object class))
1002 (eieio--check-type eieio--class-p class)
1003 (eieio--check-type symbolp slot)
1004 (let* ((c (eieio--slot-name-index class nil slot)))
1005 (if (not c)
1006 ;; It might be missing because it is a :class allocated slot.
1007 ;; Let's check that info out.
1008 (if (setq c (eieio--class-slot-name-index class slot))
1009 (progn
1010 ;; Oref that slot.
1011 (eieio--validate-class-slot-value class c value slot)
1012 (aset (eieio--class-class-allocation-values class) c
1013 value))
1014 (signal 'invalid-slot-name (list (eieio--class-symbol class) slot)))
1015 (eieio--validate-slot-value class c value slot)
1016 ;; Set this into the storage for defaults.
1017 (setcar (nthcdr (- c (eval-when-compile eieio--object-num-slots))
1018 (eieio--class-public-d class))
1019 value)
1020 ;; Take the value, and put it into our cache object.
1021 (eieio-oset (eieio--class-default-object-cache class)
1022 slot value)
1023 )))
1024
1025 \f
1026 ;;; EIEIO internal search functions
1027 ;;
1028 (defun eieio--slot-name-index (class obj slot)
1029 "In CLASS for OBJ find the index of the named SLOT.
1030 The slot is a symbol which is installed in CLASS by the `defclass'
1031 call. OBJ can be nil, but if it is an object, and the slot in question
1032 is protected, access will be allowed if OBJ is a child of the currently
1033 scoped class.
1034 If SLOT is the value created with :initarg instead,
1035 reverse-lookup that name, and recurse with the associated slot value."
1036 ;; Removed checks to outside this call
1037 (let* ((fsym (gethash slot (eieio--class-symbol-hashtable class)))
1038 (fsi (car fsym)))
1039 (if (integerp fsi)
1040 (+ (eval-when-compile eieio--object-num-slots) fsi)
1041 (let ((fn (eieio--initarg-to-attribute class slot)))
1042 (if fn (eieio--slot-name-index class obj fn) nil)))))
1043
1044 (defun eieio--class-slot-name-index (class slot)
1045 "In CLASS find the index of the named SLOT.
1046 The slot is a symbol which is installed in CLASS by the `defclass'
1047 call. If SLOT is the value created with :initarg instead,
1048 reverse-lookup that name, and recurse with the associated slot value."
1049 ;; This will happen less often, and with fewer slots. Do this the
1050 ;; storage cheap way.
1051 (let* ((a (eieio--class-class-allocation-a class))
1052 (l1 (length a))
1053 (af (memq slot a))
1054 (l2 (length af)))
1055 ;; Slot # is length of the total list, minus the remaining list of
1056 ;; the found slot.
1057 (if af (- l1 l2))))
1058
1059 ;;;
1060 ;; Way to assign slots based on a list. Used for constructors, or
1061 ;; even resetting an object at run-time
1062 ;;
1063 (defun eieio-set-defaults (obj &optional set-all)
1064 "Take object OBJ, and reset all slots to their defaults.
1065 If SET-ALL is non-nil, then when a default is nil, that value is
1066 reset. If SET-ALL is nil, the slots are only reset if the default is
1067 not nil."
1068 (let ((pub (eieio--class-public-a (eieio--object-class-object obj))))
1069 (while pub
1070 (let ((df (eieio-oref-default obj (car pub))))
1071 (if (or df set-all)
1072 (eieio-oset obj (car pub) df)))
1073 (setq pub (cdr pub)))))
1074
1075 (defun eieio--initarg-to-attribute (class initarg)
1076 "For CLASS, convert INITARG to the actual attribute name.
1077 If there is no translation, pass it in directly (so we can cheat if
1078 need be... May remove that later...)"
1079 (let ((tuple (assoc initarg (eieio--class-initarg-tuples class))))
1080 (if tuple
1081 (cdr tuple)
1082 nil)))
1083
1084 ;;;
1085 ;; Method Invocation order: C3
1086 (defun eieio--c3-candidate (class remaining-inputs)
1087 "Return CLASS if it can go in the result now, otherwise nil."
1088 ;; Ensure CLASS is not in any position but the first in any of the
1089 ;; element lists of REMAINING-INPUTS.
1090 (and (not (let ((found nil))
1091 (while (and remaining-inputs (not found))
1092 (setq found (member class (cdr (car remaining-inputs)))
1093 remaining-inputs (cdr remaining-inputs)))
1094 found))
1095 class))
1096
1097 (defun eieio--c3-merge-lists (reversed-partial-result remaining-inputs)
1098 "Merge REVERSED-PARTIAL-RESULT REMAINING-INPUTS in a consistent order, if possible.
1099 If a consistent order does not exist, signal an error."
1100 (if (let ((tail remaining-inputs)
1101 (found nil))
1102 (while (and tail (not found))
1103 (setq found (car tail) tail (cdr tail)))
1104 (not found))
1105 ;; If all remaining inputs are empty lists, we are done.
1106 (nreverse reversed-partial-result)
1107 ;; Otherwise, we try to find the next element of the result. This
1108 ;; is achieved by considering the first element of each
1109 ;; (non-empty) input list and accepting a candidate if it is
1110 ;; consistent with the rests of the input lists.
1111 (let* ((found nil)
1112 (tail remaining-inputs)
1113 (next (progn
1114 (while (and tail (not found))
1115 (setq found (and (car tail)
1116 (eieio--c3-candidate (caar tail)
1117 remaining-inputs))
1118 tail (cdr tail)))
1119 found)))
1120 (if next
1121 ;; The graph is consistent so far, add NEXT to result and
1122 ;; merge input lists, dropping NEXT from their heads where
1123 ;; applicable.
1124 (eieio--c3-merge-lists
1125 (cons next reversed-partial-result)
1126 (mapcar (lambda (l) (if (eq (cl-first l) next) (cl-rest l) l))
1127 remaining-inputs))
1128 ;; The graph is inconsistent, give up
1129 (signal 'inconsistent-class-hierarchy (list remaining-inputs))))))
1130
1131 (defun eieio--class-precedence-c3 (class)
1132 "Return all parents of CLASS in c3 order."
1133 (let ((parents (eieio--class-parent (eieio--class-v class))))
1134 (eieio--c3-merge-lists
1135 (list class)
1136 (append
1137 (or
1138 (mapcar #'eieio--class-precedence-c3 parents)
1139 `((,eieio-default-superclass)))
1140 (list parents))))
1141 )
1142 ;;;
1143 ;; Method Invocation Order: Depth First
1144
1145 (defun eieio--class-precedence-dfs (class)
1146 "Return all parents of CLASS in depth-first order."
1147 (let* ((parents (eieio--class-parent class))
1148 (classes (copy-sequence
1149 (apply #'append
1150 (list class)
1151 (or
1152 (mapcar
1153 (lambda (parent)
1154 (cons parent
1155 (eieio--class-precedence-dfs parent)))
1156 parents)
1157 `((,eieio-default-superclass))))))
1158 (tail classes))
1159 ;; Remove duplicates.
1160 (while tail
1161 (setcdr tail (delq (car tail) (cdr tail)))
1162 (setq tail (cdr tail)))
1163 classes))
1164
1165 ;;;
1166 ;; Method Invocation Order: Breadth First
1167 (defun eieio--class-precedence-bfs (class)
1168 "Return all parents of CLASS in breadth-first order."
1169 (let* ((result)
1170 (queue (or (eieio--class-parent class)
1171 `(,eieio-default-superclass))))
1172 (while queue
1173 (let ((head (pop queue)))
1174 (unless (member head result)
1175 (push head result)
1176 (unless (eq head eieio-default-superclass)
1177 (setq queue (append queue (or (eieio--class-parent head)
1178 `(,eieio-default-superclass))))))))
1179 (cons class (nreverse result)))
1180 )
1181
1182 ;;;
1183 ;; Method Invocation Order
1184
1185 (defun eieio--class-precedence-list (class)
1186 "Return (transitively closed) list of parents of CLASS.
1187 The order, in which the parents are returned depends on the
1188 method invocation orders of the involved classes."
1189 (if (or (null class) (eq class eieio-default-superclass))
1190 nil
1191 (unless (eieio--class-default-object-cache class)
1192 (eieio-class-un-autoload (eieio--class-symbol class)))
1193 (cl-case (eieio--class-method-invocation-order class)
1194 (:depth-first
1195 (eieio--class-precedence-dfs class))
1196 (:breadth-first
1197 (eieio--class-precedence-bfs class))
1198 (:c3
1199 (eieio--class-precedence-c3 class))))
1200 )
1201 (define-obsolete-function-alias
1202 'class-precedence-list 'eieio--class-precedence-list "24.4")
1203
1204 \f
1205 ;;; Here are some special types of errors
1206 ;;
1207 (define-error 'invalid-slot-name "Invalid slot name")
1208 (define-error 'invalid-slot-type "Invalid slot type")
1209 (define-error 'unbound-slot "Unbound slot")
1210 (define-error 'inconsistent-class-hierarchy "Inconsistent class hierarchy")
1211
1212 ;;; Hooking into cl-generic.
1213
1214 (require 'cl-generic)
1215
1216 ;;;; General support to dispatch based on the type of the argument.
1217
1218 (add-function :before-until cl-generic-tagcode-function
1219 #'eieio--generic-tagcode)
1220 (defun eieio--generic-tagcode (type name)
1221 ;; CLHS says:
1222 ;; A class must be defined before it can be used as a parameter
1223 ;; specializer in a defmethod form.
1224 ;; So we can ignore types that are not known to denote classes.
1225 (and (class-p type)
1226 ;; Prefer (aref ,name 0) over (eieio--class-tag ,name) so that
1227 ;; the tagcode is identical to the tagcode used for cl-struct.
1228 `(50 . (and (vectorp ,name) (aref ,name 0)))))
1229
1230 (add-function :before-until cl-generic-tag-types-function
1231 #'eieio--generic-tag-types)
1232 (defun eieio--generic-tag-types (tag)
1233 (and (symbolp tag) (boundp tag) (eieio--class-p (symbol-value tag))
1234 (mapcar #'eieio--class-symbol
1235 (eieio--class-precedence-list (symbol-value tag)))))
1236
1237 ;;;; Dispatch for arguments which are classes.
1238
1239 ;; Since EIEIO does not support metaclasses, users can't easily use the
1240 ;; "dispatch on argument type" for class arguments. That's why EIEIO's
1241 ;; `defmethod' added the :static qualifier. For cl-generic, such a qualifier
1242 ;; would not make much sense (e.g. to which argument should it apply?).
1243 ;; Instead, we add a new "subclass" specializer.
1244
1245 (add-function :before-until cl-generic-tagcode-function
1246 #'eieio--generic-subclass-tagcode)
1247 (defun eieio--generic-subclass-tagcode (type name)
1248 (when (eq 'subclass (car-safe type))
1249 `(60 . (and (symbolp ,name) (eieio--class-v ,name)))))
1250
1251 (add-function :before-until cl-generic-tag-types-function
1252 #'eieio--generic-subclass-tag-types)
1253 (defun eieio--generic-subclass-tag-types (tag)
1254 (when (eieio--class-p tag)
1255 (mapcar (lambda (class)
1256 `(subclass
1257 ,(if (symbolp class) class (eieio--class-symbol class))))
1258 (eieio--class-precedence-list tag))))
1259
1260 \f
1261 ;;;### (autoloads nil "eieio-compat" "eieio-compat.el" "b568ffb3c90ed5d0ae673f0051d608ee")
1262 ;;; Generated autoloads from eieio-compat.el
1263
1264 (autoload 'eieio--defalias "eieio-compat" "\
1265 Like `defalias', but with less side-effects.
1266 More specifically, it has no side-effects at all when the new function
1267 definition is the same (`eq') as the old one.
1268
1269 \(fn NAME BODY)" nil nil)
1270
1271 (autoload 'defgeneric "eieio-compat" "\
1272 Create a generic function METHOD.
1273 DOC-STRING is the base documentation for this class. A generic
1274 function has no body, as its purpose is to decide which method body
1275 is appropriate to use. Uses `defmethod' to create methods, and calls
1276 `defgeneric' for you. With this implementation the ARGS are
1277 currently ignored. You can use `defgeneric' to apply specialized
1278 top level documentation to a method.
1279
1280 \(fn METHOD ARGS &optional DOC-STRING)" nil t)
1281
1282 (function-put 'defgeneric 'doc-string-elt '3)
1283
1284 (make-obsolete 'defgeneric 'cl-defgeneric '"25.1")
1285
1286 (autoload 'defmethod "eieio-compat" "\
1287 Create a new METHOD through `defgeneric' with ARGS.
1288
1289 The optional second argument KEY is a specifier that
1290 modifies how the method is called, including:
1291 :before - Method will be called before the :primary
1292 :primary - The default if not specified
1293 :after - Method will be called after the :primary
1294 :static - First arg could be an object or class
1295 The next argument is the ARGLIST. The ARGLIST specifies the arguments
1296 to the method as with `defun'. The first argument can have a type
1297 specifier, such as:
1298 ((VARNAME CLASS) ARG2 ...)
1299 where VARNAME is the name of the local variable for the method being
1300 created. The CLASS is a class symbol for a class made with `defclass'.
1301 A DOCSTRING comes after the ARGLIST, and is optional.
1302 All the rest of the args are the BODY of the method. A method will
1303 return the value of the last form in the BODY.
1304
1305 Summary:
1306
1307 (defmethod mymethod [:before | :primary | :after | :static]
1308 ((typearg class-name) arg2 &optional opt &rest rest)
1309 \"doc-string\"
1310 body)
1311
1312 \(fn METHOD &rest ARGS)" nil t)
1313
1314 (function-put 'defmethod 'doc-string-elt '3)
1315
1316 (make-obsolete 'defmethod 'cl-defmethod '"25.1")
1317
1318 (autoload 'eieio--defgeneric-init-form "eieio-compat" "\
1319
1320
1321 \(fn METHOD DOC-STRING)" nil nil)
1322
1323 (autoload 'eieio--defmethod "eieio-compat" "\
1324
1325
1326 \(fn METHOD KIND ARGCLASS CODE)" nil nil)
1327
1328 (autoload 'eieio-defmethod "eieio-compat" "\
1329 Obsolete work part of an old version of the `defmethod' macro.
1330
1331 \(fn METHOD ARGS)" nil nil)
1332
1333 (make-obsolete 'eieio-defmethod 'cl-defmethod '"24.1")
1334
1335 (autoload 'eieio-defgeneric "eieio-compat" "\
1336 Obsolete work part of an old version of the `defgeneric' macro.
1337
1338 \(fn METHOD DOC-STRING)" nil nil)
1339
1340 (make-obsolete 'eieio-defgeneric 'cl-defgeneric '"24.1")
1341
1342 (autoload 'eieio-defclass "eieio-compat" "\
1343
1344
1345 \(fn CNAME SUPERCLASSES SLOTS OPTIONS)" nil nil)
1346
1347 (make-obsolete 'eieio-defclass 'eieio-defclass-internal '"25.1")
1348
1349 ;;;***
1350 \f
1351
1352 (provide 'eieio-core)
1353
1354 ;;; eieio-core.el ends here