Function: byte-optimize-or

byte-optimize-or is a byte-compiled function defined in byte-opt.el.gz.

Signature

(byte-optimize-or FORM)

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/byte-opt.el.gz
(defun byte-optimize-or (form)
  (let ((seq nil)
        (new-args nil)
        (args (remq nil (cdr form))))   ; Discard nil arguments.
    (while
        (and args
             (let ((arg (car args)))
               (cond
                (seq                    ; previous arg was always-false
                 (push arg seq)
                 (unless (and (cdr args) (byte-compile-nilconstp arg))
                   (push `(progn . ,(nreverse seq)) new-args)
                   (setq seq nil))
                 t)
                ((and (cdr args) (byte-compile-nilconstp arg))
                 ;; Always-false arg: evaluate unconditionally.
                 (push arg seq)
                 t)
                (t
                 (push arg new-args)
                 ;; If this arg is always true, throw away the remaining args.
                 (not (byte-compile-trueconstp arg))))))
      (setq args (cdr args)))

    (setq new-args (nreverse new-args))
    ;; Keep original form unless the arguments changed.
    (unless (equal new-args (cdr form))
      (setq form (cons (car form) new-args)))

    (pcase form
      ;; (or (progn ... X) ...) -> (progn ... (or X ...))
      (`(,head (progn . ,forms) . ,rest)
       `(progn ,@(butlast forms) (,head ,(car (last forms)) . ,rest)))
      (`(,_) nil)                       ; (or) -> nil
      (`(,_ ,arg) arg)                  ; (or X) -> X
      (_ (byte-optimize-constant-args form)))))