"README.md" did not exist on "79ba59b8994330970f1e7c122647d5852ae1117f"
Newer
Older
;;; -*- Log: code.log; Package: Lisp -*-
;;;
;;; **********************************************************************
;;; This code was written as part of the CMU Common Lisp project at
;;; Carnegie Mellon University, and has been placed in the public domain.
;;; If you want to use this code or any part of CMU Common Lisp, please contact
;;; Scott Fahlman or slisp-group@cs.cmu.edu.
;;;
(ext:file-comment
"$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/code/macros.lisp,v 1.42 1993/07/02 20:59:08 ram Exp $")
;;; **********************************************************************
;;;
;;; This file contains the macros that are part of the standard
;;; Spice Lisp environment.
;;;
;;; Written by Scott Fahlman and Rob MacLachlan.
;;; Modified by Bill Chiles to adhere to the wall.
(export '(defvar defparameter defconstant when unless setf
defsetf psetf shiftf rotatef push pushnew pop
incf decf remf case typecase with-open-file
with-open-stream with-input-from-string with-output-to-string
locally etypecase ctypecase ecase ccase
get-setf-expansion define-setf-expander
define-compiler-macro
;; CLtL1 versions:
define-setf-method get-setf-method get-setf-method-multiple-value))
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
(in-package "EXTENSIONS")
(export '(do-anonymous collect iterate))
(in-package "LISP")
;;; Parse-Body -- Public
;;;
;;; Parse out declarations and doc strings, *not* expanding macros.
;;; Eventually the environment arg should be flushed, since macros can't expand
;;; into declarations anymore.
;;;
(defun parse-body (body environment &optional (doc-string-allowed t))
"This function is to parse the declarations and doc-string out of the body of
a defun-like form. Body is the list of stuff which is to be parsed.
Environment is ignored. If Doc-String-Allowed is true, then a doc string
will be parsed out of the body and returned. If it is false then a string
will terminate the search for declarations. Three values are returned: the
tail of Body after the declarations and doc strings, a list of declare forms,
and the doc-string, or NIL if none."
(declare (ignore environment))
(let ((decls ())
(doc nil))
(do ((tail body (cdr tail)))
((endp tail)
(values tail (nreverse decls) doc))
(let ((form (car tail)))
(cond ((and (stringp form) (cdr tail))
(if doc-string-allowed
(setq doc form)
(return (values tail (nreverse decls) doc))))
((not (and (consp form) (symbolp (car form))))
(return (values tail (nreverse decls) doc)))
((eq (car form) 'declare)
(push form decls))
(t
(return (values tail (nreverse decls) doc))))))))
;;;; DEFMACRO:
;;; Defmacro -- Public
;;;
;;; Parse the definition and make an expander function. The actual
;;; definition is done by %defmacro which we expand into.
;;;
(defmacro defmacro (name lambda-list &body body)
(let ((whole (gensym "WHOLE-"))
(environment (gensym "ENV-")))
(parse-defmacro lambda-list whole body name 'defmacro
:environment environment)
(let ((def `(lambda (,whole ,environment)
,@local-decs
(block ,name
,body))))
`(c::%defmacro ',name #',def ',lambda-list ,doc)))))
;;; %Defmacro, %%Defmacro -- Internal
;;;
;;; Defmacro expands into %Defmacro which is a function that is treated
;;; magically the compiler. After the compiler has gotten the information it
;;; wants out of macro definition, it compiles a call to %%Defmacro which
;;; happens at load time. We have a %Defmacro function which just calls
;;; %%Defmacro in order to keep the interpreter happy.
;;;
;;; Eventually %%Defmacro should deal with clearing old compiler information
;;; for the functional value.
;;;
(defun c::%defmacro (name definition lambda-list doc)
(assert (eval:interpreted-function-p definition))
(setf (eval:interpreted-function-name definition)
(format nil "DEFMACRO ~S" name))
(setf (eval:interpreted-function-arglist definition) lambda-list)
(c::%%defmacro name definition doc))
;;;
(defun c::%%defmacro (name definition doc)
(clear-info function where-from name)
(setf (macro-function name) definition)
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
;;;; DEFINE-COMPILER-MACRO
(defmacro define-compiler-macro (name lambda-list &body body)
"Define a compiler-macro for NAME."
(let ((whole (gensym "WHOLE-"))
(environment (gensym "ENV-")))
(multiple-value-bind
(body local-decs doc)
(parse-defmacro lambda-list whole body name 'define-compiler-macro
:environment environment)
(let ((def `(lambda (,whole ,environment)
,@local-decs
(block ,name
,body))))
`(c::%define-compiler-macro ',name #',def ',lambda-list ,doc)))))
(defun c::%define-compiler-macro (name definition lambda-list doc)
(assert (eval:interpreted-function-p definition))
(setf (eval:interpreted-function-name definition)
(let ((*print-case* :upcase))
(format nil "DEFINE-COMPILER-MACRO ~S" name)))
(setf (eval:interpreted-function-arglist definition) lambda-list)
(c::%%define-compiler-macro name definition doc))
;;;
(defun c::%%define-compiler-macro (name definition doc)
(setf (compiler-macro-function name) definition)
(setf (documentation name 'compiler-macro) doc)
name)
;;; DEFTYPE is a lot like DEFMACRO.
(defmacro deftype (name arglist &body body)
"Syntax like DEFMACRO, but defines a new type."
(unless (symbolp name)
(error "~S -- Type name not a symbol." name))
(let ((whole (gensym "WHOLE-")))
(parse-defmacro arglist whole body name 'deftype
:default-default ''*)
`(eval-when (compile load eval)
(%deftype ',name
#'(lambda (,whole)
,@local-decs
(block ,name ,body))
,@(when doc `(,doc)))))))
;;;
(defun %deftype (name expander &optional doc)
(ecase (info type kind name)
(:primitive
(when *type-system-initialized*
(error "Illegal to redefine standard type: ~S." name)))
(:instance
(warn "Redefining class ~S to be a DEFTYPE." name)
(undefine-structure (layout-info (class-layout (info type class name))))
(setf (info type class name) nil)
(setf (info type compiler-layout name) nil)
(setf (info type kind name) :defined))
(:defined)
((nil)
(setf (info type kind name) :defined)))
(setf (info type expander name) expander)
(when doc
(setf (documentation name 'type) doc))
;; ### Bootstrap hack -- we need to define types before %note-type-defined
;; is defined.
(when (fboundp 'c::%note-type-defined)
;;; And so is DEFINE-SETF-EXPANDER.
(defparameter defsetf-error-string "Setf expander for ~S cannot be called with ~S args.")
(defmacro define-setf-expander (access-fn lambda-list &body body)
"Syntax like DEFMACRO, but creates a Setf-Method generator. The body
must be a form that returns the five magical values."
(unless (symbolp access-fn)
(error "~S -- Access-function name not a symbol in DEFINE-SETF-METHOD."
access-fn))
(let ((whole (gensym "WHOLE-"))
(environment (gensym "ENV-")))
(multiple-value-bind (body local-decs doc)
(parse-defmacro lambda-list whole body access-fn
'define-setf-method
:environment environment)
(%define-setf-macro
',access-fn
#'(lambda (,whole ,environment)
,@local-decs
(block ,access-fn ,body))
nil
',doc)))))
(defmacro define-setf-method (&rest stuff)
"Obsolete, use define-setf-expander."
`(define-setf-expander ,@stuff))
;;; %DEFINE-SETF-MACRO -- Internal
;;;
;;; Do stuff for defining a setf macro.
;;;
(defun %define-setf-macro (name expander inverse doc)
(cond ((not (fboundp `(setf ,name))))
((info function accessor-for name)
(warn "Defining setf macro for destruct slot accessor; redefining as ~
a normal function:~% ~S"
name)
(c::define-function-name name))
((not (eq (symbol-package name) (symbol-package 'aref)))
(warn "Defining setf macro for ~S, but ~S is fbound."
name `(setf ,name))))
(when (or inverse (info setf inverse name))
(setf (info setf inverse name) inverse))
(when (or expander (info setf expander name))
(setf (info setf expander name) expander))
(when doc
(setf (documentation name 'setf) doc))
name)
;;;; Destructuring-bind
(defmacro destructuring-bind (lambda-list arg-list &rest body)
"Bind the variables in LAMBDA-LIST to the contents of ARG-LIST."
(let* ((arg-list-name (gensym "ARG-LIST-")))
(multiple-value-bind
(parse-defmacro lambda-list arg-list-name body nil 'destructuring-bind
:annonymousp t :doc-string-allowed nil)
`(let ((,arg-list-name ,arg-list))
,body))))
;;;; Defun, Defvar, Defparameter, Defconstant:
;;; Defun -- Public
;;;
;;; Very similar to Defmacro, but simpler. We don't have to parse the
;;; lambda-list.
;;;
(defmacro defun (&whole source name lambda-list &body (body decls doc))
(let ((def `(lambda ,lambda-list
,@decls
(block ,(if (and (consp name) (eq (car name) 'setf))
(cadr name)
name)
,@body))))
`(c::%defun ',name #',def ,doc ',source)))
;;; %Defun, %%Defun -- Internal
;;;
;;; Similar to %Defmacro, ...
;;;
(defun c::%%defun (name def doc &optional inline-expansion)
(setf (fdefinition name) def)
(when doc
(if (and (consp name) (eq (first name) 'setf))
(setf (documentation (second name) 'setf) doc)
(setf (documentation name 'function) doc)))
(c::define-function-name name)
(when (eq (info function where-from name) :assumed)
(setf (info function where-from name) :defined)
(when (info function assumed-type name)
(setf (info function assumed-type name) nil)))
(when (or inline-expansion
(info function inline-expansion name))
(setf (info function inline-expansion name) inline-expansion))
name)
;;;
(defun c::%defun (name def doc source)
(declare (ignore source))
(assert (eval:interpreted-function-p def))
(setf (eval:interpreted-function-name def) name)
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
(c::%%defun name def doc))
;;; DEFCONSTANT -- Public
;;;
(defmacro defconstant (var val &optional doc)
"For defining global constants at top level. The DEFCONSTANT says that the
value is constant and may be compiled into code. If the variable already has
a value, and this is not equal to the init, an error is signalled. The third
argument is an optional documentation string for the variable."
`(c::%defconstant ',var ,val ',doc))
;;; %Defconstant, %%Defconstant -- Internal
;;;
;;; Like the other %mumbles except that we currently actually do something
;;; interesting at load time, namely checking if the constant is being
;;; redefined.
;;;
(defun c::%defconstant (name value doc)
(c::%%defconstant name value doc))
;;;
(defun c::%%defconstant (name value doc)
(when doc
(setf (documentation name 'variable) doc))
(when (boundp name)
(unless (equalp (symbol-value name) value)
(cerror "Go ahead and change the value."
"Constant ~S being redefined." name)))
(setf (symbol-value name) value)
(setf (info variable kind name) :constant)
(clear-info variable constant-value name)
name)
(defmacro defvar (var &optional (val nil valp) (doc nil docp))
"For defining global variables at top level. Declares the variable
SPECIAL and, optionally, initializes it. If the variable already has a
value, the old value is not clobbered. The third argument is an optional
documentation string for the variable."
`(progn
(proclaim '(special ,var))
,@(when valp
`((unless (boundp ',var)
(setq ,var ,val))))
,@(when docp
`((setf (documentation ',var 'variable) ',doc)))
',var))
(defmacro defparameter (var val &optional (doc nil docp))
"Defines a parameter that is not normally changed by the program,
but that may be changed without causing an error. Declares the
variable special and sets its value to VAL. The third argument is
an optional documentation string for the parameter."
`(progn
(proclaim '(special ,var))
(setq ,var ,val)
,@(when docp
`((setf (documentation ',var 'variable) ',doc)))
',var))
;;;; ASSORTED CONTROL STRUCTURES
(defmacro when (test &body forms)
"First arg is a predicate. If it is non-null, the rest of the forms are
evaluated as a PROGN."
`(cond (,test nil ,@forms)))
(defmacro unless (test &rest forms)
"First arg is a predicate. If it is null, the rest of the forms are
evaluated as a PROGN."
`(cond ((not ,test) nil ,@forms)))
(defmacro return (&optional (value nil))
`(return-from nil ,value))
(defmacro prog (varlist &body (body decls))
`(block nil
(let ,varlist
,@decls
(tagbody ,@body))))
(defmacro prog* (varlist &body (body decls))
`(block nil
(let* ,varlist
,@decls
(tagbody ,@body))))
;;; Prog1, Prog2 -- Public
;;;
;;; These just turn into a Let.
;;;
(defmacro prog1 (result &rest body)
(let ((n-result (gensym)))
`(let ((,n-result ,result))
,@body
,n-result)))
;;;
(defmacro prog2 (form1 result &rest body)
`(prog1 (progn ,form1 ,result) ,@body))
;;; And, Or -- Public
;;;
;;; AND and OR are defined in terms of IF.
;;;
(defmacro and (&rest forms)
(cond ((endp forms) t)
((endp (rest forms)) (first forms))
(t
`(if ,(first forms)
(and ,@(rest forms))
nil))))
;;;
(defmacro or (&rest forms)
(cond ((endp forms) nil)
((endp (rest forms)) (first forms))
(t
(let ((n-result (gensym)))
`(let ((,n-result ,(first forms)))
(if ,n-result
,n-result
(or ,@(rest forms))))))))
;;; Cond -- Public
;;;
;;; COND also turns into IF.
;;;
(defmacro cond (&rest clauses)
(if (endp clauses)
nil
(let ((clause (first clauses)))
(when (atom clause)
(error "Cond clause is not a list: ~S." clause))
(let ((test (first clause))
(forms (rest clause)))
(if (endp forms)
(let ((n-result (gensym)))
`(let ((,n-result ,test))
(if ,n-result
,n-result
(cond ,@(rest clauses)))))
`(if ,test
(progn ,@forms)
(cond ,@(rest clauses))))))))
;;;; Multiple value macros:
;;; Multiple-Value-XXX -- Public
;;;
;;; All the multiple-value receiving forms are defined in terms of
;;; Multiple-Value-Call.
;;;
(defmacro multiple-value-setq (varlist value-form)
(unless (and (listp varlist) (every #'symbolp varlist))
(error "Varlist is not a list of symbols: ~S." varlist))
(let ((temps (mapcar #'(lambda (x) (declare (ignore x)) (gensym)) varlist)))
`(multiple-value-bind ,temps ,value-form
,@(mapcar #'(lambda (var temp)
`(setq ,var ,temp))
varlist temps)
,(car temps))))
;;;
(defmacro multiple-value-bind (varlist value-form &body body)
(unless (and (listp varlist) (every #'symbolp varlist))
(error "Varlist is not a list of symbols: ~S." varlist))
(if (= (length varlist) 1)
`(let ((,(car varlist) ,value-form))
,@body)
(let ((ignore (gensym)))
`(multiple-value-call #'(lambda (&optional ,@varlist &rest ,ignore)
(declare (ignore ,ignore))
,@body)
,value-form))))
;;;
(defmacro multiple-value-list (value-form)
`(multiple-value-call #'list ,value-form))
(defmacro nth-value (n form)
"Evaluates FORM and returns the Nth value (zero based). This involves no
consing when N is a trivial constant integer."
(if (integerp n)
(let ((dummy-list nil)
(keeper (gensym "KEEPER-")))
;; We build DUMMY-LIST, a list of variables to bind to useless
;; values, then we explicitly IGNORE those bindings and return
;; KEEPER, the only thing we're really interested in right now.
(dotimes (i n)
(push (gensym "IGNORE-") dummy-list))
`(multiple-value-bind (,@dummy-list ,keeper)
,form
(declare (ignore ,@dummy-list))
,keeper))
(once-only ((n n))
`(case (the fixnum ,n)
(0 (nth-value 0 ,form))
(1 (nth-value 1 ,form))
(2 (nth-value 2 ,form))
(T (nth (the fixnum ,n) (multiple-value-list ,form)))))))
;;;; SETF and friends.
;;; Note: The expansions for SETF and friends sometimes create needless
;;; LET-bindings of argument values. The compiler will remove most of
;;; these spurious bindings, so SETF doesn't worry too much about creating
;;; them.
;;; The inverse for a generalized-variable reference function is stored in
;;; one of two ways:
;;;
;;; A SETF inverse property corresponds to the short form of DEFSETF. It is
;;; the name of a function takes the same args as the reference form, plus a
;;; new-value arg at the end.
;;;
;;; A SETF method expander is created by the long form of DEFSETF or
;;; by DEFINE-SETF-METHOD. It is a function that is called on the reference
;;; form and that produces five values: a list of temporary variables, a list
;;; of value forms, a list of the single store-value form, a storing function,
;;; and an accessing function.
(defun get-setf-expansion (form &optional environment)
"Returns five values needed by the SETF machinery: a list of temporary
variables, a list of values with which to fill them, a list of temporaries
for the new values, the setting function, and the accessing function."
(multiple-value-bind
(expansion expanded)
(macroexpand-1 form environment)
(if expanded
(get-setf-expansion expansion environment)
(let ((new-var (gensym)))
(values nil nil (list new-var)
`(setq ,form ,new-var) form)))))
;; Local functions inhibit global setf methods...
((and environment
(let ((name (car form)))
(dolist (x (c::lexenv-functions environment) nil)
(when (and (eq (car x) name)
(not (c::defined-function-p (cdr x))))
(return t)))))
(expand-or-get-setf-inverse form environment))
(get-setf-method-inverse form `(,temp) nil))
((setq temp (info setf expander (car form)))
(funcall temp form environment))
(t
(expand-or-get-setf-inverse form environment)))))
(defun get-setf-method-multiple-value (form &optional env)
"Obsolete: use GET-SETF-EXPANSION."
(get-setf-expansion form env))
;;;
;;; If a macro, expand one level and try again. If not, go for the
;;; SETF function.
(defun expand-or-get-setf-inverse (form environment)
(multiple-value-bind
(expansion expanded)
(macroexpand-1 form environment)
(if expanded
(get-setf-expansion expansion environment)
(get-setf-method-inverse form `(funcall #'(setf ,(car form)))
t))))
(defun get-setf-method-inverse (form inverse setf-function)
(let ((new-var (gensym))
(vars nil)
(vals nil))
(dolist (x (cdr form))
(push (gensym) vars)
(push x vals))
(setq vals (nreverse vals))
(values vars vals (list new-var)
(if setf-function
`(,@inverse ,new-var ,@vars)
`(,@inverse ,@vars ,new-var))
(defun get-setf-method (form &optional environment)
"Obsolete: use GET-SETF-EXPANSION and handle multiple store values."
(multiple-value-bind
(temps value-forms store-vars store-form access-form)
(get-setf-expansion form environment)
(when (cdr store-vars)
(error "GET-SETF-METHOD used for a form with multiple store ~
variables:~% ~S" form))
(values temps value-forms store-vars store-form access-form)))
Loading
Loading full blame...