]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/cl-generic.el
* lisp/emacs-lisp/cl-generic.el: Accomodate future changes
[gnu-emacs] / lisp / emacs-lisp / cl-generic.el
1 ;;; cl-generic.el --- CLOS-style generic functions for Elisp -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Version: 1.0
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; This implements the most of CLOS's multiple-dispatch generic functions.
26 ;; To use it you need either (require 'cl-generic) or (require 'cl-lib).
27 ;; The main entry points are: `cl-defgeneric' and `cl-defmethod'.
28
29 ;; Missing elements:
30 ;; - We don't support make-method, call-method, define-method-combination.
31 ;; CLOS's define-method-combination is IMO overly complicated, and it suffers
32 ;; from a significant problem: the method-combination code returns a sexp
33 ;; that needs to be `eval'uated or compiled. IOW it requires run-time
34 ;; code generation. Given how rarely method-combinations are used,
35 ;; I just provided a cl-generic-combine-methods generic function, to which
36 ;; people can add methods if they are really desperate for such functionality.
37 ;; - In defgeneric we don't support the options:
38 ;; declare, :method-combination, :generic-function-class, :method-class.
39 ;; Added elements:
40 ;; - We support aliases to generic functions.
41 ;; - cl-generic-generalizers. This generic function lets you extend the kind
42 ;; of thing on which to dispatch. There is support in this file for
43 ;; dispatch on:
44 ;; - (eql <val>)
45 ;; - (head <val>) which checks that the arg is a cons with <val> as its head.
46 ;; - plain old types
47 ;; - type of CL structs
48 ;; eieio-core adds dispatch on:
49 ;; - class of eieio objects
50 ;; - actual class argument, using the syntax (subclass <class>).
51 ;; - cl-generic-combine-methods (i.s.o define-method-combination and
52 ;; compute-effective-method).
53 ;; - cl-generic-call-method (which replaces make-method and call-method).
54 ;; - The standard method combination supports ":extra STRING" qualifiers
55 ;; which simply allows adding more methods for the same
56 ;; specializers&qualifiers.
57 ;; - Methods can dispatch on the context. For that, a method needs to specify
58 ;; context arguments, introduced by `&context' (which need to come right
59 ;; after the mandatory arguments and before anything like
60 ;; &optional/&rest/&key). Each context argument is given as (EXP SPECIALIZER)
61 ;; which means that EXP is taken as an expression which computes some context
62 ;; and this value is then used to dispatch.
63 ;; E.g. (foo &context (major-mode (eql c-mode))) is an arglist specifying
64 ;; that this method will only be applicable when `major-mode' has value
65 ;; `c-mode'.
66
67 ;; Efficiency considerations: overall, I've made an effort to make this fairly
68 ;; efficient for the expected case (e.g. no constant redefinition of methods).
69 ;; - Generic functions which do not dispatch on any argument are implemented
70 ;; optimally (just as efficient as plain old functions).
71 ;; - Generic functions which only dispatch on one argument are fairly efficient
72 ;; (not a lot of room for improvement without changes to the byte-compiler,
73 ;; I think).
74 ;; - Multiple dispatch is implemented rather naively. There's an extra `apply'
75 ;; function call for every dispatch; we don't optimize each dispatch
76 ;; based on the set of candidate methods remaining; we don't optimize the
77 ;; order in which we performs the dispatches either;
78 ;; If/when this becomes a problem, we can try and optimize it.
79 ;; - call-next-method could be made more efficient, but isn't too terrible.
80
81 ;; TODO:
82 ;;
83 ;; - A generic "filter" generalizer (e.g. could be used to cleanly add methods
84 ;; to cl-generic-combine-methods with a specializer that says it applies only
85 ;; when some particular qualifier is used).
86 ;; - A way to dispatch on the context (e.g. the major-mode, some global
87 ;; variable, you name it).
88
89 ;;; Code:
90
91 ;; Note: For generic functions that dispatch on several arguments (i.e. those
92 ;; which use the multiple-dispatch feature), we always use the same "tagcodes"
93 ;; and the same set of arguments on which to dispatch. This works, but is
94 ;; often suboptimal since after one dispatch, the remaining dispatches can
95 ;; usually be simplified, or even completely skipped.
96
97 (eval-when-compile (require 'cl-lib))
98 (eval-when-compile (require 'cl-macs)) ;For cl--find-class.
99 (eval-when-compile (require 'pcase))
100
101 (cl-defstruct (cl--generic-generalizer
102 (:constructor nil)
103 (:constructor cl-generic-make-generalizer
104 (name priority tagcode-function specializers-function)))
105 (name nil :type string)
106 (priority nil :type integer)
107 tagcode-function
108 specializers-function)
109
110
111 (defmacro cl-generic-define-generalizer
112 (name priority tagcode-function specializers-function)
113 "Define a new kind of generalizer.
114 NAME is the name of the variable that will hold it.
115 PRIORITY defines which generalizer takes precedence.
116 The catch-all generalizer has priority 0.
117 Then `eql' generalizer has priority 100.
118 TAGCODE-FUNCTION takes as first argument a varname and should return
119 a chunk of code that computes the tag of the value held in that variable.
120 Further arguments are reserved for future use.
121 SPECIALIZERS-FUNCTION takes as first argument a tag value TAG
122 and should return a list of specializers that match TAG.
123 Further arguments are reserved for future use."
124 (declare (indent 1) (debug (symbolp body)))
125 `(defconst ,name
126 (cl-generic-make-generalizer
127 ',name ,priority ,tagcode-function ,specializers-function)))
128
129 (cl-generic-define-generalizer cl--generic-t-generalizer
130 0 (lambda (_name &rest _) nil) (lambda (_tag &rest _) '(t)))
131
132 (cl-defstruct (cl--generic-method
133 (:constructor nil)
134 (:constructor cl--generic-make-method
135 (specializers qualifiers uses-cnm function))
136 (:predicate nil))
137 (specializers nil :read-only t :type list)
138 (qualifiers nil :read-only t :type (list-of atom))
139 ;; USES-CNM is a boolean indicating if FUNCTION expects an extra argument
140 ;; holding the next-method.
141 (uses-cnm nil :read-only t :type boolean)
142 (function nil :read-only t :type function))
143
144 (cl-defstruct (cl--generic
145 (:constructor nil)
146 (:constructor cl--generic-make (name))
147 (:predicate nil))
148 (name nil :type symbol :read-only t) ;Pointer back to the symbol.
149 ;; `dispatches' holds a list of (ARGNUM . TAGCODES) where ARGNUM is the index
150 ;; of the corresponding argument and TAGCODES is a list of (PRIORITY . EXP)
151 ;; where the EXPs are expressions (to be `or'd together) to compute the tag
152 ;; on which to dispatch and PRIORITY is the priority of each expression to
153 ;; decide in which order to sort them.
154 ;; The most important dispatch is last in the list (and the least is first).
155 (dispatches nil :type (list-of (cons natnum (list-of generalizers))))
156 (method-table nil :type (list-of cl--generic-method))
157 (options nil :type list))
158
159 (defun cl-generic-function-options (generic)
160 "Return the options of the generic function GENERIC."
161 (cl--generic-options generic))
162
163 (defmacro cl--generic (name)
164 `(get ,name 'cl--generic))
165
166 (defun cl-generic-ensure-function (name &optional noerror)
167 (let (generic
168 (origname name))
169 (while (and (null (setq generic (cl--generic name)))
170 (fboundp name)
171 (null noerror)
172 (symbolp (symbol-function name)))
173 (setq name (symbol-function name)))
174 (unless (or (not (fboundp name))
175 (autoloadp (symbol-function name))
176 (and (functionp name) generic)
177 noerror)
178 (error "%s is already defined as something else than a generic function"
179 origname))
180 (if generic
181 (cl-assert (eq name (cl--generic-name generic)))
182 (setf (cl--generic name) (setq generic (cl--generic-make name)))
183 (defalias name (cl--generic-make-function generic)))
184 generic))
185
186 ;;;###autoload
187 (defmacro cl-defgeneric (name args &rest options-and-methods)
188 "Create a generic function NAME.
189 DOC-STRING is the base documentation for this class. A generic
190 function has no body, as its purpose is to decide which method body
191 is appropriate to use. Specific methods are defined with `cl-defmethod'.
192 With this implementation the ARGS are currently ignored.
193 OPTIONS-AND-METHODS currently understands:
194 - (:documentation DOCSTRING)
195 - (declare DECLARATIONS)
196 - (:argument-precedence-order &rest ARGS)
197 - (:method [QUALIFIERS...] ARGS &rest BODY)
198 BODY, if present, is used as the body of a default method.
199
200 \(fn NAME ARGS [DOC-STRING] [OPTIONS-AND-METHODS...] &rest BODY)"
201 (declare (indent 2) (doc-string 3))
202 (let* ((doc (if (stringp (car-safe options-and-methods))
203 (pop options-and-methods)))
204 (declarations nil)
205 (methods ())
206 (options ())
207 next-head)
208 (while (progn (setq next-head (car-safe (car options-and-methods)))
209 (or (keywordp next-head)
210 (eq next-head 'declare)))
211 (pcase next-head
212 (`:documentation
213 (when doc (error "Multiple doc strings for %S" name))
214 (setq doc (cadr (pop options-and-methods))))
215 (`declare
216 (when declarations (error "Multiple `declare' for %S" name))
217 (setq declarations (pop options-and-methods)))
218 (`:method (push (cdr (pop options-and-methods)) methods))
219 (_ (push (pop options-and-methods) options))))
220 (when options-and-methods
221 ;; Anything remaining is assumed to be a default method body.
222 (push `(,args ,@options-and-methods) methods))
223 (when (eq 'setf (car-safe name))
224 (require 'gv)
225 (setq name (gv-setter (cadr name))))
226 `(progn
227 ,@(mapcar (lambda (declaration)
228 (let ((f (cdr (assq (car declaration)
229 defun-declarations-alist))))
230 (cond
231 (f (apply (car f) name args (cdr declaration)))
232 (t (message "Warning: Unknown defun property `%S' in %S"
233 (car declaration) name)
234 nil))))
235 (cdr declarations))
236 (defalias ',name
237 (cl-generic-define ',name ',args ',(nreverse options))
238 ,(help-add-fundoc-usage doc args))
239 ,@(mapcar (lambda (method) `(cl-defmethod ,name ,@method))
240 (nreverse methods)))))
241
242 ;;;###autoload
243 (defun cl-generic-define (name args options)
244 (pcase-let* ((generic (cl-generic-ensure-function name 'noerror))
245 (`(,spec-args . ,_) (cl--generic-split-args args))
246 (mandatory (mapcar #'car spec-args))
247 (apo (assq :argument-precedence-order options)))
248 (unless (fboundp name)
249 ;; If the generic function was fmakunbound, throw away previous methods.
250 (setf (cl--generic-dispatches generic) nil)
251 (setf (cl--generic-method-table generic) nil))
252 (when apo
253 (dolist (arg (cdr apo))
254 (let ((pos (memq arg mandatory)))
255 (unless pos (error "%S is not a mandatory argument" arg))
256 (let* ((argno (- (length mandatory) (length pos)))
257 (dispatches (cl--generic-dispatches generic))
258 (dispatch (or (assq argno dispatches) (list argno))))
259 (setf (cl--generic-dispatches generic)
260 (cons dispatch (delq dispatch dispatches)))))))
261 (setf (cl--generic-options generic) options)
262 (cl--generic-make-function generic)))
263
264 (defmacro cl-generic-current-method-specializers ()
265 "List of (VAR . TYPE) where TYPE is var's specializer.
266 This macro can only be used within the lexical scope of a cl-generic method."
267 (error "cl-generic-current-method-specializers used outside of a method"))
268
269 (eval-and-compile ;Needed while compiling the cl-defmethod calls below!
270 (defun cl--generic-fgrep (vars sexp) ;Copied from pcase.el.
271 "Check which of the symbols VARS appear in SEXP."
272 (let ((res '()))
273 (while (consp sexp)
274 (dolist (var (cl--generic-fgrep vars (pop sexp)))
275 (unless (memq var res) (push var res))))
276 (and (memq sexp vars) (not (memq sexp res)) (push sexp res))
277 res))
278
279 (defun cl--generic-split-args (args)
280 "Return (SPEC-ARGS . PLAIN-ARGS)."
281 (let ((plain-args ())
282 (specializers nil)
283 (mandatory t))
284 (dolist (arg args)
285 (push (pcase arg
286 ((or '&optional '&rest '&key) (setq mandatory nil) arg)
287 ('&context
288 (unless mandatory
289 (error "&context not immediately after mandatory args"))
290 (setq mandatory 'context) nil)
291 ((let 'nil mandatory) arg)
292 ((let 'context mandatory)
293 (unless (consp arg)
294 (error "Invalid &context arg: %S" arg))
295 (push `((&context . ,(car arg)) . ,(cadr arg)) specializers)
296 nil)
297 (`(,name . ,type)
298 (push (cons name (car type)) specializers)
299 name)
300 (_
301 (push (cons arg t) specializers)
302 arg))
303 plain-args))
304 (cons (nreverse specializers)
305 (nreverse (delq nil plain-args)))))
306
307 (defun cl--generic-lambda (args body)
308 "Make the lambda expression for a method with ARGS and BODY."
309 (pcase-let* ((`(,spec-args . ,plain-args)
310 (cl--generic-split-args args))
311 (fun `(cl-function (lambda ,plain-args ,@body)))
312 (macroenv (cons `(cl-generic-current-method-specializers
313 . ,(lambda () spec-args))
314 macroexpand-all-environment)))
315 (require 'cl-lib) ;Needed to expand `cl-flet' and `cl-function'.
316 ;; First macroexpand away the cl-function stuff (e.g. &key and
317 ;; destructuring args, `declare' and whatnot).
318 (pcase (macroexpand fun macroenv)
319 (`#'(lambda ,args . ,body)
320 (let* ((parsed-body (macroexp-parse-body body))
321 (cnm (make-symbol "cl--cnm"))
322 (nmp (make-symbol "cl--nmp"))
323 (nbody (macroexpand-all
324 `(cl-flet ((cl-call-next-method ,cnm)
325 (cl-next-method-p ,nmp))
326 ,@(cdr parsed-body))
327 macroenv))
328 ;; FIXME: Rather than `grep' after the fact, the
329 ;; macroexpansion should directly set some flag when cnm
330 ;; is used.
331 ;; FIXME: Also, optimize the case where call-next-method is
332 ;; only called with explicit arguments.
333 (uses-cnm (cl--generic-fgrep (list cnm nmp) nbody)))
334 (cons (not (not uses-cnm))
335 `#'(lambda (,@(if uses-cnm (list cnm)) ,@args)
336 ,@(car parsed-body)
337 ,(if (not (memq nmp uses-cnm))
338 nbody
339 `(let ((,nmp (lambda ()
340 (cl--generic-isnot-nnm-p ,cnm))))
341 ,nbody))))))
342 (f (error "Unexpected macroexpansion result: %S" f))))))
343
344
345 ;;;###autoload
346 (defmacro cl-defmethod (name args &rest body)
347 "Define a new method for generic function NAME.
348 I.e. it defines the implementation of NAME to use for invocations where the
349 value of the dispatch argument matches the specified TYPE.
350 The dispatch argument has to be one of the mandatory arguments, and
351 all methods of NAME have to use the same argument for dispatch.
352 The dispatch argument and TYPE are specified in ARGS where the corresponding
353 formal argument appears as (VAR TYPE) rather than just VAR.
354
355 The optional second argument QUALIFIER is a specifier that
356 modifies how the method is combined with other methods, including:
357 :before - Method will be called before the primary
358 :after - Method will be called after the primary
359 :around - Method will be called around everything else
360 The absence of QUALIFIER means this is a \"primary\" method.
361
362 Other than a type, TYPE can also be of the form `(eql VAL)' in
363 which case this method will be invoked when the argument is `eql' to VAL.
364
365 \(fn NAME [QUALIFIER] ARGS &rest [DOCSTRING] BODY)"
366 (declare (doc-string 3) (indent 2)
367 (debug
368 (&define ; this means we are defining something
369 [&or name ("setf" :name setf name)]
370 ;; ^^ This is the methods symbol
371 [ &optional keywordp ] ; this is key :before etc
372 list ; arguments
373 [ &optional stringp ] ; documentation string
374 def-body))) ; part to be debugged
375 (let ((qualifiers nil))
376 (while (not (listp args))
377 (push args qualifiers)
378 (setq args (pop body)))
379 (when (eq 'setf (car-safe name))
380 (require 'gv)
381 (setq name (gv-setter (cadr name))))
382 (pcase-let* ((`(,uses-cnm . ,fun) (cl--generic-lambda args body)))
383 `(progn
384 ,(and (get name 'byte-obsolete-info)
385 (or (not (fboundp 'byte-compile-warning-enabled-p))
386 (byte-compile-warning-enabled-p 'obsolete))
387 (let* ((obsolete (get name 'byte-obsolete-info)))
388 (macroexp--warn-and-return
389 (macroexp--obsolete-warning name obsolete "generic function")
390 nil)))
391 ;; You could argue that `defmethod' modifies rather than defines the
392 ;; function, so warnings like "not known to be defined" are fair game.
393 ;; But in practice, it's common to use `cl-defmethod'
394 ;; without a previous `cl-defgeneric'.
395 (declare-function ,name "")
396 (cl-generic-define-method ',name ',(nreverse qualifiers) ',args
397 ,uses-cnm ,fun)))))
398
399 (defun cl--generic-member-method (specializers qualifiers methods)
400 (while
401 (and methods
402 (let ((m (car methods)))
403 (not (and (equal (cl--generic-method-specializers m) specializers)
404 (equal (cl--generic-method-qualifiers m) qualifiers)))))
405 (setq methods (cdr methods)))
406 methods)
407
408 ;;;###autoload
409 (defun cl-generic-define-method (name qualifiers args uses-cnm function)
410 (pcase-let*
411 ((generic (cl-generic-ensure-function name))
412 (`(,spec-args . ,_) (cl--generic-split-args args))
413 (specializers (mapcar (lambda (spec-arg)
414 (if (eq '&context (car-safe (car spec-arg)))
415 spec-arg (cdr spec-arg)))
416 spec-args))
417 (method (cl--generic-make-method
418 specializers qualifiers uses-cnm function))
419 (mt (cl--generic-method-table generic))
420 (me (cl--generic-member-method specializers qualifiers mt))
421 (dispatches (cl--generic-dispatches generic))
422 (i 0))
423 (dolist (spec-arg spec-args)
424 (let* ((key (if (eq '&context (car-safe (car spec-arg)))
425 (car spec-arg) i))
426 (generalizers (cl-generic-generalizers (cdr spec-arg)))
427 (x (assoc key dispatches)))
428 (unless x
429 (setq x (cons key (cl-generic-generalizers t)))
430 (setf (cl--generic-dispatches generic)
431 (setq dispatches (cons x dispatches))))
432 (dolist (generalizer generalizers)
433 (unless (member generalizer (cdr x))
434 (setf (cdr x)
435 (sort (cons generalizer (cdr x))
436 (lambda (x y)
437 (> (cl--generic-generalizer-priority x)
438 (cl--generic-generalizer-priority y)))))))
439 (setq i (1+ i))))
440 ;; We used to (setcar me method), but that can cause false positives in
441 ;; the hash-consing table of the method-builder (bug#20644).
442 ;; See also the related FIXME in cl--generic-build-combined-method.
443 (setf (cl--generic-method-table generic)
444 (if (null me)
445 (cons method mt)
446 ;; Keep the ordering; important for methods with :extra qualifiers.
447 (mapcar (lambda (x) (if (eq x (car me)) method x)) mt)))
448 (cl-pushnew `(cl-defmethod . (,(cl--generic-name generic) . ,specializers))
449 current-load-list :test #'equal)
450 ;; FIXME: Try to avoid re-constructing a new function if the old one
451 ;; is still valid (e.g. still empty method cache)?
452 (let ((gfun (cl--generic-make-function generic))
453 ;; Prevent `defalias' from recording this as the definition site of
454 ;; the generic function.
455 current-load-list)
456 ;; For aliases, cl--generic-name gives us the actual name.
457 (let ((purify-flag
458 ;; BEWARE! Don't purify this function definition, since that leads
459 ;; to memory corruption if the hash-tables it holds are modified
460 ;; (the GC doesn't trace those pointers).
461 nil))
462 ;; But do use `defalias', so that it interacts properly with nadvice,
463 ;; e.g. for tracing/debug-on-entry.
464 (defalias (cl--generic-name generic) gfun)))))
465
466 (defmacro cl--generic-with-memoization (place &rest code)
467 (declare (indent 1) (debug t))
468 (gv-letplace (getter setter) place
469 `(or ,getter
470 ,(macroexp-let2 nil val (macroexp-progn code)
471 `(progn
472 ,(funcall setter val)
473 ,val)))))
474
475 (defvar cl--generic-dispatchers (make-hash-table :test #'equal))
476
477 (defun cl--generic-get-dispatcher (dispatch)
478 (cl--generic-with-memoization
479 (gethash dispatch cl--generic-dispatchers)
480 ;; (message "cl--generic-get-dispatcher (%S)" dispatch)
481 (let* ((dispatch-arg (car dispatch))
482 (generalizers (cdr dispatch))
483 (lexical-binding t)
484 (tagcodes
485 (mapcar (lambda (generalizer)
486 (funcall (cl--generic-generalizer-tagcode-function
487 generalizer)
488 'arg))
489 generalizers))
490 (typescodes
491 (mapcar
492 (lambda (generalizer)
493 `(funcall ',(cl--generic-generalizer-specializers-function
494 generalizer)
495 ,(funcall (cl--generic-generalizer-tagcode-function
496 generalizer)
497 'arg)))
498 generalizers))
499 (tag-exp
500 ;; Minor optimization: since this tag-exp is
501 ;; only used to lookup the method-cache, it
502 ;; doesn't matter if the default value is some
503 ;; constant or nil.
504 `(or ,@(if (macroexp-const-p (car (last tagcodes)))
505 (butlast tagcodes)
506 tagcodes)))
507 (fixedargs '(arg))
508 (dispatch-idx dispatch-arg)
509 (bindings nil))
510 (when (eq '&context (car-safe dispatch-arg))
511 (setq bindings `((arg ,(cdr dispatch-arg))))
512 (setq fixedargs nil)
513 (setq dispatch-idx 0))
514 (dotimes (i dispatch-idx)
515 (push (make-symbol (format "arg%d" (- dispatch-idx i 1))) fixedargs))
516 ;; FIXME: For generic functions with a single method (or with 2 methods,
517 ;; one of which always matches), using a tagcode + hash-table is
518 ;; overkill: better just use a `cl-typep' test.
519 (byte-compile
520 `(lambda (generic dispatches-left methods)
521 (let ((method-cache (make-hash-table :test #'eql)))
522 (lambda (,@fixedargs &rest args)
523 (let ,bindings
524 (apply (cl--generic-with-memoization
525 (gethash ,tag-exp method-cache)
526 (cl--generic-cache-miss
527 generic ',dispatch-arg dispatches-left methods
528 ,(if (cdr typescodes)
529 `(append ,@typescodes) (car typescodes))))
530 ,@fixedargs args)))))))))
531
532 (defun cl--generic-make-function (generic)
533 (cl--generic-make-next-function generic
534 (cl--generic-dispatches generic)
535 (cl--generic-method-table generic)))
536
537 (defun cl--generic-make-next-function (generic dispatches methods)
538 (let* ((dispatch
539 (progn
540 (while (and dispatches
541 (let ((x (nth 1 (car dispatches))))
542 ;; No need to dispatch for t specializers.
543 (or (null x) (equal x cl--generic-t-generalizer))))
544 (setq dispatches (cdr dispatches)))
545 (pop dispatches))))
546 (if (not (and dispatch
547 ;; If there's no method left, there's no point checking
548 ;; further arguments.
549 methods))
550 (cl--generic-build-combined-method generic methods)
551 (let ((dispatcher (cl--generic-get-dispatcher dispatch)))
552 (funcall dispatcher generic dispatches methods)))))
553
554 (defvar cl--generic-combined-method-memoization
555 (make-hash-table :test #'equal :weakness 'value)
556 "Table storing previously built combined-methods.
557 This is particularly useful when many different tags select the same set
558 of methods, since this table then allows us to share a single combined-method
559 for all those different tags in the method-cache.")
560
561 (define-error 'cl--generic-cyclic-definition "Cyclic definition: %S")
562
563 (defun cl--generic-build-combined-method (generic methods)
564 (if (null methods)
565 ;; Special case needed to fix a circularity during bootstrap.
566 (cl--generic-standard-method-combination generic methods)
567 (let ((f
568 (cl--generic-with-memoization
569 ;; FIXME: Since the fields of `generic' are modified, this
570 ;; hash-table won't work right, because the hashes will change!
571 ;; It's not terribly serious, but reduces the effectiveness of
572 ;; the table.
573 (gethash (cons generic methods)
574 cl--generic-combined-method-memoization)
575 (puthash (cons generic methods) :cl--generic--under-construction
576 cl--generic-combined-method-memoization)
577 (condition-case nil
578 (cl-generic-combine-methods generic methods)
579 ;; Special case needed to fix a circularity during bootstrap.
580 (cl--generic-cyclic-definition
581 (cl--generic-standard-method-combination generic methods))))))
582 (if (eq f :cl--generic--under-construction)
583 (signal 'cl--generic-cyclic-definition
584 (list (cl--generic-name generic)))
585 f))))
586
587 (defun cl--generic-no-next-method-function (generic method)
588 (lambda (&rest args)
589 (apply #'cl-no-next-method generic method args)))
590
591 (defun cl-generic-call-method (generic method &optional fun)
592 "Return a function that calls METHOD.
593 FUN is the function that should be called when METHOD calls
594 `call-next-method'."
595 (if (not (cl--generic-method-uses-cnm method))
596 (cl--generic-method-function method)
597 (let ((met-fun (cl--generic-method-function method))
598 (next (or fun (cl--generic-no-next-method-function
599 generic method))))
600 (lambda (&rest args)
601 (apply met-fun
602 ;; FIXME: This sucks: passing just `next' would
603 ;; be a lot more efficient than the lambda+apply
604 ;; quasi-η, but we need this to implement the
605 ;; "if call-next-method is called with no
606 ;; arguments, then use the previous arguments".
607 (lambda (&rest cnm-args)
608 (apply next (or cnm-args args)))
609 args)))))
610
611 ;; Standard CLOS name.
612 (defalias 'cl-method-qualifiers #'cl--generic-method-qualifiers)
613
614 (defun cl--generic-standard-method-combination (generic methods)
615 (let ((mets-by-qual ()))
616 (dolist (method methods)
617 (let ((qualifiers (cl-method-qualifiers method)))
618 (if (eq (car qualifiers) :extra) (setq qualifiers (cddr qualifiers)))
619 (unless (member qualifiers '(() (:after) (:before) (:around)))
620 (error "Unsupported qualifiers in function %S: %S"
621 (cl--generic-name generic) qualifiers))
622 (push method (alist-get (car qualifiers) mets-by-qual))))
623 (cond
624 ((null mets-by-qual)
625 (lambda (&rest args)
626 (apply #'cl-no-applicable-method generic args)))
627 ((null (alist-get nil mets-by-qual))
628 (lambda (&rest args)
629 (apply #'cl-no-primary-method generic args)))
630 (t
631 (let* ((fun nil)
632 (ab-call (lambda (m) (cl-generic-call-method generic m)))
633 (before
634 (mapcar ab-call (reverse (cdr (assoc :before mets-by-qual)))))
635 (after (mapcar ab-call (cdr (assoc :after mets-by-qual)))))
636 (dolist (method (cdr (assoc nil mets-by-qual)))
637 (setq fun (cl-generic-call-method generic method fun)))
638 (when (or after before)
639 (let ((next fun))
640 (setq fun (lambda (&rest args)
641 (dolist (bf before)
642 (apply bf args))
643 (prog1
644 (apply next args)
645 (dolist (af after)
646 (apply af args)))))))
647 (dolist (method (cdr (assoc :around mets-by-qual)))
648 (setq fun (cl-generic-call-method generic method fun)))
649 fun)))))
650
651 (defun cl--generic-arg-specializer (method dispatch-arg)
652 (or (if (integerp dispatch-arg)
653 (nth dispatch-arg
654 (cl--generic-method-specializers method))
655 (cdr (assoc dispatch-arg
656 (cl--generic-method-specializers method))))
657 t))
658
659 (defun cl--generic-cache-miss (generic
660 dispatch-arg dispatches-left methods-left types)
661 (let ((methods '()))
662 (dolist (method methods-left)
663 (let* ((specializer (cl--generic-arg-specializer method dispatch-arg))
664 (m (member specializer types)))
665 (when m
666 (push (cons (length m) method) methods))))
667 ;; Sort the methods, most specific first.
668 ;; It would be tempting to sort them once and for all in the method-table
669 ;; rather than here, but the order might depend on the actual argument
670 ;; (e.g. for multiple inheritance with defclass).
671 (setq methods (nreverse (mapcar #'cdr (sort methods #'car-less-than-car))))
672 (cl--generic-make-next-function generic dispatches-left methods)))
673
674 (cl-defgeneric cl-generic-generalizers (specializer)
675 "Return a list of generalizers for a given SPECIALIZER.
676 To each kind of `specializer', corresponds a `generalizer' which describes
677 how to extract a \"tag\" from an object which will then let us check if this
678 object matches the specializer. A typical example of a \"tag\" would be the
679 type of an object. It's called a `generalizer' because it
680 takes a specific object and returns a more general approximation,
681 denoting a set of objects to which it belongs.
682 A generalizer gives us the chunk of code which the
683 dispatch function needs to use to extract the \"tag\" of an object, as well
684 as a function which turns this tag into an ordered list of
685 `specializers' that this object matches.
686 The code which extracts the tag should be as fast as possible.
687 The tags should be chosen according to the following rules:
688 - The tags should not be too specific: similar objects which match the
689 same list of specializers should ideally use the same (`eql') tag.
690 This insures that the cached computation of the applicable
691 methods for one object can be reused for other objects.
692 - Corollary: objects which don't match any of the relevant specializers
693 should ideally all use the same tag (typically nil).
694 This insures that this cache does not grow unnecessarily large.
695 - Two different generalizers G1 and G2 should not use the same tag
696 unless they use it for the same set of objects. IOW, if G1.tag(X1) =
697 G2.tag(X2) then G1.tag(X1) = G2.tag(X1) = G1.tag(X2) = G2.tag(X2).
698 - If G1.priority > G2.priority and G1.tag(X1) = G1.tag(X2) and this tag is
699 non-nil, then you have to make sure that the G2.tag(X1) = G2.tag(X2).
700 This is because the method-cache is only indexed with the first non-nil
701 tag (by order of decreasing priority).")
702
703 (cl-defgeneric cl-generic-combine-methods (generic methods)
704 "Build the effective method made of METHODS.
705 It should return a function that expects the same arguments as the methods, and
706 calls those methods in some appropriate order.
707 GENERIC is the generic function (mostly used for its name).
708 METHODS is the list of the selected methods.
709 The METHODS list is sorted from most specific first to most generic last.
710 The function can use `cl-generic-call-method' to create functions that call those
711 methods.")
712
713 (unless (ignore-errors (cl-generic-generalizers t))
714 ;; Temporary definition to let the next defmethod succeed.
715 (fset 'cl-generic-generalizers
716 (lambda (specializer)
717 (if (eq t specializer) (list cl--generic-t-generalizer))))
718 (fset 'cl-generic-combine-methods #'cl--generic-standard-method-combination))
719
720 (cl-defmethod cl-generic-generalizers (specializer)
721 "Support for the catch-all t specializer."
722 (if (eq specializer t) (list cl--generic-t-generalizer)
723 (error "Unknown specializer %S" specializer)))
724
725 (eval-when-compile
726 ;; This macro is brittle and only really important in order to be
727 ;; able to preload cl-generic without also preloading the byte-compiler,
728 ;; So we use `eval-when-compile' so as not keep it available longer than
729 ;; strictly needed.
730 (defmacro cl--generic-prefill-dispatchers (arg-or-context specializer)
731 (unless (integerp arg-or-context)
732 (setq arg-or-context `(&context . ,arg-or-context)))
733 (unless (fboundp 'cl--generic-get-dispatcher)
734 (require 'cl-generic))
735 (let ((fun (cl--generic-get-dispatcher
736 `(,arg-or-context ,@(cl-generic-generalizers specializer)
737 ,cl--generic-t-generalizer))))
738 ;; Recompute dispatch at run-time, since the generalizers may be slightly
739 ;; different (e.g. byte-compiled rather than interpreted).
740 ;; FIXME: There is a risk that the run-time generalizer is not equivalent
741 ;; to the compile-time one, in which case `fun' may not be correct
742 ;; any more!
743 `(let ((dispatch `(,',arg-or-context
744 ,@(cl-generic-generalizers ',specializer)
745 ,cl--generic-t-generalizer)))
746 ;; (message "Prefilling for %S with \n%S" dispatch ',fun)
747 (puthash dispatch ',fun cl--generic-dispatchers)))))
748
749 (cl-defmethod cl-generic-combine-methods (generic methods)
750 "Standard support for :after, :before, :around, and `:extra NAME' qualifiers."
751 (cl--generic-standard-method-combination generic methods))
752
753 (defconst cl--generic-nnm-sample (cl--generic-no-next-method-function t t))
754 (defconst cl--generic-cnm-sample
755 (funcall (cl--generic-build-combined-method
756 nil (list (cl--generic-make-method () () t #'identity)))))
757
758 (defun cl--generic-isnot-nnm-p (cnm)
759 "Return non-nil if CNM is the function that calls `cl-no-next-method'."
760 ;; ¡Big Gross Ugly Hack!
761 ;; `next-method-p' just sucks, we should let it die. But EIEIO did support
762 ;; it, and some packages use it, so we need to support it.
763 (catch 'found
764 (cl-assert (function-equal cnm cl--generic-cnm-sample))
765 (if (byte-code-function-p cnm)
766 (let ((cnm-constants (aref cnm 2))
767 (sample-constants (aref cl--generic-cnm-sample 2)))
768 (dotimes (i (length sample-constants))
769 (when (function-equal (aref sample-constants i)
770 cl--generic-nnm-sample)
771 (throw 'found
772 (not (function-equal (aref cnm-constants i)
773 cl--generic-nnm-sample))))))
774 (cl-assert (eq 'closure (car-safe cl--generic-cnm-sample)))
775 (let ((cnm-env (cadr cnm)))
776 (dolist (vb (cadr cl--generic-cnm-sample))
777 (when (function-equal (cdr vb) cl--generic-nnm-sample)
778 (throw 'found
779 (not (function-equal (cdar cnm-env)
780 cl--generic-nnm-sample))))
781 (setq cnm-env (cdr cnm-env)))))
782 (error "Haven't found no-next-method-sample in cnm-sample")))
783
784 ;;; Define some pre-defined generic functions, used internally.
785
786 (define-error 'cl-no-method "No method for %S")
787 (define-error 'cl-no-next-method "No next method for %S" 'cl-no-method)
788 (define-error 'cl-no-primary-method "No primary method for %S" 'cl-no-method)
789 (define-error 'cl-no-applicable-method "No applicable method for %S"
790 'cl-no-method)
791
792 (cl-defgeneric cl-no-next-method (generic method &rest args)
793 "Function called when `cl-call-next-method' finds no next method."
794 (signal 'cl-no-next-method `(,(cl--generic-name generic) ,method ,@args)))
795
796 (cl-defgeneric cl-no-applicable-method (generic &rest args)
797 "Function called when a method call finds no applicable method."
798 (signal 'cl-no-applicable-method `(,(cl--generic-name generic) ,@args)))
799
800 (cl-defgeneric cl-no-primary-method (generic &rest args)
801 "Function called when a method call finds no primary method."
802 (signal 'cl-no-primary-method `(,(cl--generic-name generic) ,@args)))
803
804 (defun cl-call-next-method (&rest _args)
805 "Function to call the next applicable method.
806 Can only be used from within the lexical body of a primary or around method."
807 (error "cl-call-next-method only allowed inside primary and around methods"))
808
809 (defun cl-next-method-p ()
810 "Return non-nil if there is a next method.
811 Can only be used from within the lexical body of a primary or around method."
812 (declare (obsolete "make sure there's always a next method, or catch `cl-no-next-method' instead" "25.1"))
813 (error "cl-next-method-p only allowed inside primary and around methods"))
814
815 ;;;###autoload
816 (defun cl-find-method (generic qualifiers specializers)
817 (car (cl--generic-member-method
818 specializers qualifiers
819 (cl--generic-method-table (cl--generic generic)))))
820
821 ;;; Add support for describe-function
822
823 (defun cl--generic-search-method (met-name)
824 "For `find-function-regexp-alist'. Searches for a cl-defmethod.
825 MET-NAME is a cons (SYMBOL . SPECIALIZERS)."
826 (let ((base-re (concat "(\\(?:cl-\\)?defmethod[ \t]+"
827 (regexp-quote (format "%s" (car met-name)))
828 "\\_>")))
829 (or
830 (re-search-forward
831 (concat base-re "[^&\"\n]*"
832 (mapconcat (lambda (specializer)
833 (regexp-quote
834 (format "%S" (if (consp specializer)
835 (nth 1 specializer) specializer))))
836 (remq t (cdr met-name))
837 "[ \t\n]*)[^&\"\n]*"))
838 nil t)
839 (re-search-forward base-re nil t))))
840
841 ;; WORKAROUND: This can't be a defconst due to bug#21237.
842 (defvar cl--generic-find-defgeneric-regexp "(\\(?:cl-\\)?defgeneric[ \t]+%s\\>")
843
844 (with-eval-after-load 'find-func
845 (defvar find-function-regexp-alist)
846 (add-to-list 'find-function-regexp-alist
847 `(cl-defmethod . ,#'cl--generic-search-method))
848 (add-to-list 'find-function-regexp-alist
849 `(cl-defgeneric . cl--generic-find-defgeneric-regexp)))
850
851 (defun cl--generic-method-info (method)
852 (let* ((specializers (cl--generic-method-specializers method))
853 (qualifiers (cl--generic-method-qualifiers method))
854 (uses-cnm (cl--generic-method-uses-cnm method))
855 (function (cl--generic-method-function method))
856 (args (help-function-arglist function 'names))
857 (docstring (documentation function))
858 (qual-string
859 (if (null qualifiers) ""
860 (cl-assert (consp qualifiers))
861 (let ((s (prin1-to-string qualifiers)))
862 (concat (substring s 1 -1) " "))))
863 (doconly (if docstring
864 (let ((split (help-split-fundoc docstring nil)))
865 (if split (cdr split) docstring))))
866 (combined-args ()))
867 (if uses-cnm (setq args (cdr args)))
868 (dolist (specializer specializers)
869 (let ((arg (if (eq '&rest (car args))
870 (intern (format "arg%d" (length combined-args)))
871 (pop args))))
872 (push (if (eq specializer t) arg (list arg specializer))
873 combined-args)))
874 (setq combined-args (append (nreverse combined-args) args))
875 (list qual-string combined-args doconly)))
876
877 (add-hook 'help-fns-describe-function-functions #'cl--generic-describe)
878 (defun cl--generic-describe (function)
879 ;; Supposedly this is called from help-fns, so help-fns should be loaded at
880 ;; this point.
881 (declare-function help-fns-short-filename "help-fns" (filename))
882 (let ((generic (if (symbolp function) (cl--generic function))))
883 (when generic
884 (require 'help-mode) ;Needed for `help-function-def' button!
885 (save-excursion
886 (insert "\n\nThis is a generic function.\n\n")
887 (insert (propertize "Implementations:\n\n" 'face 'bold))
888 ;; Loop over fanciful generics
889 (dolist (method (cl--generic-method-table generic))
890 (let* ((info (cl--generic-method-info method)))
891 ;; FIXME: Add hyperlinks for the types as well.
892 (insert (format "%s%S" (nth 0 info) (nth 1 info)))
893 (let* ((met-name (cons function
894 (cl--generic-method-specializers method)))
895 (file (find-lisp-object-file-name met-name 'cl-defmethod)))
896 (when file
897 (insert (substitute-command-keys " in `"))
898 (help-insert-xref-button (help-fns-short-filename file)
899 'help-function-def met-name file
900 'cl-defmethod)
901 (insert (substitute-command-keys "'.\n"))))
902 (insert "\n" (or (nth 2 info) "Undocumented") "\n\n")))))))
903
904 (defun cl--generic-specializers-apply-to-type-p (specializers type)
905 "Return non-nil if a method with SPECIALIZERS applies to TYPE."
906 (let ((applies nil))
907 (dolist (specializer specializers)
908 (if (memq (car-safe specializer) '(subclass eieio--static))
909 (setq specializer (nth 1 specializer)))
910 ;; Don't include the methods that are "too generic", such as those
911 ;; applying to `eieio-default-superclass'.
912 (and (not (memq specializer '(t eieio-default-superclass)))
913 (or (equal type specializer)
914 (when (symbolp specializer)
915 (let ((sclass (cl--find-class specializer))
916 (tclass (cl--find-class type)))
917 (when (and sclass tclass)
918 (member specializer (cl--generic-class-parents tclass))))))
919 (setq applies t)))
920 applies))
921
922 (defun cl--generic-all-functions (&optional type)
923 "Return a list of all generic functions.
924 Optional TYPE argument returns only those functions that contain
925 methods for TYPE."
926 (let ((l nil))
927 (mapatoms
928 (lambda (symbol)
929 (let ((generic (and (fboundp symbol) (cl--generic symbol))))
930 (and generic
931 (catch 'found
932 (if (null type) (throw 'found t))
933 (dolist (method (cl--generic-method-table generic))
934 (if (cl--generic-specializers-apply-to-type-p
935 (cl--generic-method-specializers method) type)
936 (throw 'found t))))
937 (push symbol l)))))
938 l))
939
940 (defun cl--generic-method-documentation (function type)
941 "Return info for all methods of FUNCTION (a symbol) applicable to TYPE.
942 The value returned is a list of elements of the form
943 \(QUALIFIERS ARGS DOC)."
944 (let ((generic (cl--generic function))
945 (docs ()))
946 (when generic
947 (dolist (method (cl--generic-method-table generic))
948 (when (cl--generic-specializers-apply-to-type-p
949 (cl--generic-method-specializers method) type)
950 (push (cl--generic-method-info method) docs))))
951 docs))
952
953 ;;; Support for (head <val>) specializers.
954
955 ;; For both the `eql' and the `head' specializers, the dispatch
956 ;; is unsatisfactory. Basically, in the "common&fast case", we end up doing
957 ;;
958 ;; (let ((tag (gethash value <tagcode-hashtable>)))
959 ;; (funcall (gethash tag <method-cache>)))
960 ;;
961 ;; whereas we'd like to just do
962 ;;
963 ;; (funcall (gethash value <method-cache>)))
964 ;;
965 ;; but the problem is that the method-cache is normally "open ended", so
966 ;; a nil means "not computed yet" and if we bump into it, we dutifully fill the
967 ;; corresponding entry, whereas we'd want to just fallback on some default
968 ;; effective method (so as not to fill the cache with lots of redundant
969 ;; entries).
970
971 (defvar cl--generic-head-used (make-hash-table :test #'eql))
972
973 (cl-generic-define-generalizer cl--generic-head-generalizer
974 80 (lambda (name &rest _) `(gethash (car-safe ,name) cl--generic-head-used))
975 (lambda (tag &rest _) (if (eq (car-safe tag) 'head) (list tag))))
976
977 (cl-defmethod cl-generic-generalizers :extra "head" (specializer)
978 "Support for the `(head VAL)' specializers."
979 ;; We have to implement `head' here using the :extra qualifier,
980 ;; since we can't use the `head' specializer to implement itself.
981 (if (not (eq (car-safe specializer) 'head))
982 (cl-call-next-method)
983 (cl--generic-with-memoization
984 (gethash (cadr specializer) cl--generic-head-used) specializer)
985 (list cl--generic-head-generalizer)))
986
987 (cl--generic-prefill-dispatchers 0 (head eql))
988
989 ;;; Support for (eql <val>) specializers.
990
991 (defvar cl--generic-eql-used (make-hash-table :test #'eql))
992
993 (cl-generic-define-generalizer cl--generic-eql-generalizer
994 100 (lambda (name &rest _) `(gethash ,name cl--generic-eql-used))
995 (lambda (tag &rest _) (if (eq (car-safe tag) 'eql) (list tag))))
996
997 (cl-defmethod cl-generic-generalizers ((specializer (head eql)))
998 "Support for the `(eql VAL)' specializers."
999 (puthash (cadr specializer) specializer cl--generic-eql-used)
1000 (list cl--generic-eql-generalizer))
1001
1002 (cl--generic-prefill-dispatchers 0 (eql nil))
1003 (cl--generic-prefill-dispatchers window-system (eql nil))
1004
1005 ;;; Support for cl-defstructs specializers.
1006
1007 (defun cl--generic-struct-tag (name &rest _)
1008 ;; It's tempting to use (and (vectorp ,name) (aref ,name 0))
1009 ;; but that would suffer from some problems:
1010 ;; - the vector may have size 0.
1011 ;; - when called on an actual vector (rather than an object), we'd
1012 ;; end up returning an arbitrary value, possibly colliding with
1013 ;; other tagcode's values.
1014 ;; - it can also result in returning all kinds of irrelevant
1015 ;; values which would end up filling up the method-cache with
1016 ;; lots of irrelevant/redundant entries.
1017 ;; FIXME: We could speed this up by introducing a dedicated
1018 ;; vector type at the C level, so we could do something like
1019 ;; (and (vector-objectp ,name) (aref ,name 0))
1020 `(and (vectorp ,name)
1021 (> (length ,name) 0)
1022 (let ((tag (aref ,name 0)))
1023 (and (symbolp tag)
1024 (eq (symbol-function tag) :quick-object-witness-check)
1025 tag))))
1026
1027 (defun cl--generic-class-parents (class)
1028 (let ((parents ())
1029 (classes (list class)))
1030 ;; BFS precedence. FIXME: Use a topological sort.
1031 (while (let ((class (pop classes)))
1032 (cl-pushnew (cl--class-name class) parents)
1033 (setq classes
1034 (append classes
1035 (cl--class-parents class)))))
1036 (nreverse parents)))
1037
1038 (defun cl--generic-struct-specializers (tag &rest _)
1039 (and (symbolp tag) (boundp tag)
1040 (let ((class (symbol-value tag)))
1041 (when (cl-typep class 'cl-structure-class)
1042 (cl--generic-class-parents class)))))
1043
1044 (cl-generic-define-generalizer cl--generic-struct-generalizer
1045 50 #'cl--generic-struct-tag
1046 #'cl--generic-struct-specializers)
1047
1048 (cl-defmethod cl-generic-generalizers :extra "cl-struct" (type)
1049 "Support for dispatch on cl-struct types."
1050 (or
1051 (when (symbolp type)
1052 ;; Use the "cl--struct-class*" (inlinable) functions/macros rather than
1053 ;; the "cl-struct-*" variants which aren't inlined, so that dispatch can
1054 ;; take place without requiring cl-lib.
1055 (let ((class (cl--find-class type)))
1056 (and (cl-typep class 'cl-structure-class)
1057 (or (null (cl--struct-class-type class))
1058 (error "Can't dispatch on cl-struct %S: type is %S"
1059 type (cl--struct-class-type class)))
1060 (progn (cl-assert (null (cl--struct-class-named class))) t)
1061 (list cl--generic-struct-generalizer))))
1062 (cl-call-next-method)))
1063
1064 (cl--generic-prefill-dispatchers 0 cl--generic-generalizer)
1065
1066 ;;; Dispatch on "system types".
1067
1068 (defconst cl--generic-typeof-types
1069 ;; Hand made from the source code of `type-of'.
1070 '((integer number) (symbol) (string array sequence) (cons list sequence)
1071 ;; Markers aren't `numberp', yet they are accepted wherever integers are
1072 ;; accepted, pretty much.
1073 (marker) (overlay) (float number) (window-configuration)
1074 (process) (window) (subr) (compiled-function) (buffer)
1075 (char-table array sequence)
1076 (bool-vector array sequence)
1077 (frame) (hash-table) (font-spec) (font-entity) (font-object)
1078 (vector array sequence)
1079 ;; Plus, hand made:
1080 (null symbol list sequence)
1081 (list sequence)
1082 (array sequence)
1083 (sequence)
1084 (number)))
1085
1086 (cl-generic-define-generalizer cl--generic-typeof-generalizer
1087 ;; FIXME: We could also change `type-of' to return `null' for nil.
1088 10 (lambda (name &rest _) `(if ,name (type-of ,name) 'null))
1089 (lambda (tag &rest _)
1090 (and (symbolp tag) (assq tag cl--generic-typeof-types))))
1091
1092 (cl-defmethod cl-generic-generalizers :extra "typeof" (type)
1093 "Support for dispatch on builtin types."
1094 ;; FIXME: Add support for other types accepted by `cl-typep' such
1095 ;; as `character', `atom', `face', `function', ...
1096 (or
1097 (and (assq type cl--generic-typeof-types)
1098 (progn
1099 ;; FIXME: While this wrinkle in the semantics can be occasionally
1100 ;; problematic, this warning is more often annoying than helpful.
1101 ;;(if (memq type '(vector array sequence))
1102 ;; (message "`%S' also matches CL structs and EIEIO classes"
1103 ;; type))
1104 (list cl--generic-typeof-generalizer)))
1105 (cl-call-next-method)))
1106
1107 (cl--generic-prefill-dispatchers 0 integer)
1108
1109 ;; Local variables:
1110 ;; generated-autoload-file: "cl-loaddefs.el"
1111 ;; End:
1112
1113 (provide 'cl-generic)
1114 ;;; cl-generic.el ends here