Function: byte-optimize-and

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

Signature

(byte-optimize-and FORM)

Source Code

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

    (setq new-args (nreverse new-args))
    (if (equal new-args (cdr form))
        ;; Input is unchanged: keep original form, and don't represent
        ;; a nil result explicitly because that would lead to infinite
        ;; growth when the optimizer is iterated.
        (setq nil-result nil)
      (setq form (cons (car form) new-args)))

    (let ((new-form
           (pcase form
             ;; (and (progn ... X) ...) -> (progn ... (and X ...))
             (`(,head (progn . ,forms) . ,rest)
              `(progn ,@(butlast forms) (,head ,(car (last forms)) . ,rest)))
             (`(,_) t)                   ; (and) -> t
             (`(,_ ,arg) arg)            ; (and X) -> X
             (_ (byte-optimize-constant-args form)))))
      (if nil-result
          `(progn ,new-form nil)
        new-form))))