| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya. |
| 6 |
;;;; Copyright (c) 1990, Giuseppe Attardi. |
| 7 |
;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll. |
| 8 |
;;;; |
| 9 |
;;;; See file 'LICENSE' for the copyright details. |
| 10 |
|
| 11 |
;;;; defines SYS:DEFMACRO*, the defmacro preprocessor |
| 12 |
|
| 13 |
(in-package "SYSTEM") |
| 14 |
|
| 15 |
#+ecl-min |
| 16 |
(si::fset 'push |
| 17 |
#'(ext::lambda-block push (args env) |
| 18 |
(let* ((what (second args)) |
| 19 |
(where (caddr args))) |
| 20 |
`(setq ,where (cons ,what ,where)))) |
| 21 |
t) |
| 22 |
|
| 23 |
#+ecl-min |
| 24 |
(si::fset 'pop |
| 25 |
#'(ext::lambda-block pop (args env) |
| 26 |
(let ((where (cadr args))) |
| 27 |
`(let* ((l ,where) |
| 28 |
(v (car l))) |
| 29 |
(setq ,where (cdr l)) |
| 30 |
v))) |
| 31 |
t) |
| 32 |
|
| 33 |
#+ecl-min |
| 34 |
(si::fset 'incf |
| 35 |
#'(ext::lambda-block incf (args env) |
| 36 |
(let* ((where (second args)) |
| 37 |
(what (caddr args))) |
| 38 |
(if what |
| 39 |
`(setq ,where (+ ,where ,what)) |
| 40 |
`(setq ,where (1+ ,where))))) |
| 41 |
t) |
| 42 |
|
| 43 |
#+ecl-min |
| 44 |
(si::fset 'decf |
| 45 |
#'(ext::lambda-block decf (args env) |
| 46 |
(let* ((where (second args)) |
| 47 |
(what (caddr args))) |
| 48 |
(if what |
| 49 |
`(setq ,where (- ,where ,what)) |
| 50 |
`(setq ,where (1- ,where))))) |
| 51 |
t) |
| 52 |
|
| 53 |
(defun sys::search-keyword (list key) |
| 54 |
(cond ((atom list) 'missing-keyword) |
| 55 |
((atom (cdr list)) 'missing-keyword) |
| 56 |
((eq (car list) key) (cadr list)) |
| 57 |
(t (search-keyword (cddr list) key)))) |
| 58 |
|
| 59 |
(defun check-keyword (tail keywords &optional (allow-other-keys nil aok-flag)) |
| 60 |
(do (head |
| 61 |
arg |
| 62 |
(err nil)) |
| 63 |
((null tail) |
| 64 |
(when (and err (not allow-other-keys)) |
| 65 |
(error "The key ~s is not allowed" err))) |
| 66 |
(if (atom tail) |
| 67 |
(error "keyword list is not a proper list") |
| 68 |
(setq head (car tail) tail (cdr tail))) |
| 69 |
(if (atom tail) |
| 70 |
(error "keyword list is not a proper list") |
| 71 |
(setq arg (car tail) tail (cdr tail))) |
| 72 |
(cond ((eq head :allow-other-keys) |
| 73 |
(when (not aok-flag) |
| 74 |
(setq allow-other-keys arg aok-flag t))) |
| 75 |
((not (member head keywords)) |
| 76 |
(setq err head))))) |
| 77 |
|
| 78 |
(defun dm-too-many-arguments (*current-form*) |
| 79 |
(simple-program-error "Too many arguments supplied to a macro or a destructuring-bind form:~%~s" |
| 80 |
*current-form*)) |
| 81 |
|
| 82 |
(defun dm-too-few-arguments (form-or-nil) |
| 83 |
(if form-or-nil |
| 84 |
(let ((*current-form* form-or-nil)) |
| 85 |
(simple-program-error "Too few arguments supplied to a macro or a destructuring-bind form:~%~S" |
| 86 |
*current-form*)) |
| 87 |
(simple-program-error "Too few arguments supplied to a inlined lambda form."))) |
| 88 |
|
| 89 |
(defun sys::destructure (vl context &aux |
| 90 |
(basis-form (gensym)) |
| 91 |
(destructure-symbols (list basis-form))) |
| 92 |
(declare (special *dl* *arg-check*)) |
| 93 |
(labels ((tempsym () |
| 94 |
(let ((x (gensym))) |
| 95 |
(push x destructure-symbols) |
| 96 |
x)) |
| 97 |
(dm-vl (vl whole context) |
| 98 |
(multiple-value-bind (reqs opts rest key-flag keys allow-other-keys auxs) |
| 99 |
(si::process-lambda-list |
| 100 |
vl (case context |
| 101 |
((defmacro define-compiler-macro define-setf-expander deftype) |
| 102 |
'macro) |
| 103 |
(otherwise 'destructuring-bind))) |
| 104 |
(let* ((pointer (tempsym)) |
| 105 |
(cons-pointer `(truly-the cons ,pointer)) |
| 106 |
(unsafe-car `(car ,cons-pointer)) |
| 107 |
(unsafe-cdr `(cdr ,cons-pointer)) |
| 108 |
(unsafe-pop `(setq ,pointer ,unsafe-cdr)) |
| 109 |
(no-check nil) |
| 110 |
(ppn (+ (length reqs) (first opts))) |
| 111 |
all-keywords) |
| 112 |
;; In macros, eliminate the name of the macro from the list |
| 113 |
(dm-v pointer (case context |
| 114 |
(define-compiler-macro |
| 115 |
`(if (and (eq (car ,whole) 'cl:funcall) |
| 116 |
(eq (caadr ,whole) 'cl:function)) |
| 117 |
(cddr (truly-the cons ,whole)) |
| 118 |
(cdr (truly-the cons ,whole)))) |
| 119 |
((defmacro define-setf-expander deftype) |
| 120 |
`(cdr (truly-the cons ,whole))) |
| 121 |
(otherwise whole))) |
| 122 |
(dolist (v (cdr reqs)) |
| 123 |
(dm-v v `(progn |
| 124 |
(if (null ,pointer) |
| 125 |
(dm-too-few-arguments ,basis-form)) |
| 126 |
(prog1 ,unsafe-car ,unsafe-pop)))) |
| 127 |
(dotimes (i (pop opts)) |
| 128 |
(let* ((x (first opts)) |
| 129 |
(init (second opts)) |
| 130 |
(sv (third opts))) |
| 131 |
(setq opts (cdddr opts)) |
| 132 |
(cond (sv |
| 133 |
(dm-v x `(if ,pointer ,unsafe-car ,init)) |
| 134 |
(dm-v sv `(and ,pointer (progn ,unsafe-pop t)))) |
| 135 |
(t |
| 136 |
(dm-v x `(if ,pointer |
| 137 |
(prog1 ,unsafe-car ,unsafe-pop) |
| 138 |
,init)))))) |
| 139 |
(when rest |
| 140 |
(dm-v rest pointer) |
| 141 |
(setq no-check t)) |
| 142 |
(dotimes (i (pop keys)) |
| 143 |
(let* ((temp (tempsym)) |
| 144 |
(k (first keys)) |
| 145 |
(v (second keys)) |
| 146 |
(init (third keys)) |
| 147 |
(sv (fourth keys))) |
| 148 |
(setq no-check t) |
| 149 |
(setq keys (cddddr keys)) |
| 150 |
(dm-v temp `(search-keyword ,pointer ',k)) |
| 151 |
(dm-v v `(if (eq ,temp 'missing-keyword) ,init ,temp)) |
| 152 |
(when sv (dm-v sv `(not (eq ,temp 'missing-keyword)))) |
| 153 |
(push k all-keywords))) |
| 154 |
(do ((l (rest auxs) (cddr l))) ((endp l)) |
| 155 |
(let* ((v (first l)) |
| 156 |
(init (second l))) |
| 157 |
(dm-v v init))) |
| 158 |
(cond (key-flag |
| 159 |
(push `(check-keyword ,pointer ',all-keywords |
| 160 |
,@(if allow-other-keys '(t) '())) |
| 161 |
*arg-check*)) |
| 162 |
((not no-check) |
| 163 |
(push `(if ,pointer (dm-too-many-arguments ,basis-form)) |
| 164 |
*arg-check*))) |
| 165 |
ppn))) |
| 166 |
|
| 167 |
(dm-v (v init) |
| 168 |
(cond ((and v (symbolp v)) |
| 169 |
(let ((push-val (if init (list v init) v))) |
| 170 |
(push push-val *dl*))) |
| 171 |
((and v (atom v)) |
| 172 |
(error "destructure: ~A is not a list nor a symbol" v)) |
| 173 |
((eq (first v) '&whole) |
| 174 |
(let ((whole-var (second v))) |
| 175 |
(if (listp whole-var) |
| 176 |
(let ((new-whole (tempsym))) |
| 177 |
(dm-v new-whole init) |
| 178 |
(dm-vl whole-var new-whole nil) |
| 179 |
(setq whole-var new-whole)) |
| 180 |
(dm-v whole-var init)) |
| 181 |
(dm-vl (cddr v) whole-var nil))) |
| 182 |
(t |
| 183 |
(let* ((temp (tempsym)) |
| 184 |
(push-val (if init (list temp init) temp))) |
| 185 |
(push push-val *dl*) |
| 186 |
(dm-vl v temp nil)))))) |
| 187 |
|
| 188 |
(let* ((whole basis-form) |
| 189 |
(*dl* nil) |
| 190 |
(*arg-check* nil)) |
| 191 |
(declare (special *dl* *arg-check*)) |
| 192 |
(cond ((listp vl) |
| 193 |
(when (eq (first vl) '&whole) |
| 194 |
(let ((named-whole (second vl))) |
| 195 |
(setq vl (cddr vl)) |
| 196 |
(if (listp named-whole) |
| 197 |
(dm-vl named-whole whole nil) |
| 198 |
(setq *dl* (list (list named-whole whole))))))) |
| 199 |
((symbolp vl) |
| 200 |
(setq vl (list '&rest vl))) |
| 201 |
(t (error "The destructuring-lambda-list ~s is not a list." vl))) |
| 202 |
(values (dm-vl vl whole context) whole |
| 203 |
(nreverse *dl*) |
| 204 |
*arg-check* |
| 205 |
destructure-symbols)))) |
| 206 |
|
| 207 |
;;; valid lambda-list to DEFMACRO is: |
| 208 |
;;; |
| 209 |
;;; ( [ &whole sym ] |
| 210 |
;;; [ &environment sym ] |
| 211 |
;;; { v }* |
| 212 |
;;; [ &optional { sym | ( v [ init [ v ] ] ) }* ] |
| 213 |
;;; { [ { &rest | &body } v ] |
| 214 |
;;; [ &key { sym | ( { sym | ( key v ) } [ init [ v ]] ) }* |
| 215 |
;;; [ &allow-other-keys ]] |
| 216 |
;;; [ &aux { sym | ( v [ init ] ) }* ] |
| 217 |
;;; | . sym } |
| 218 |
;;; ) |
| 219 |
;;; |
| 220 |
;;; where v is short for { defmacro-lambda-list | sym }. |
| 221 |
;;; A symbol may be accepted as a DEFMACRO lambda-list, in which case |
| 222 |
;;; (DEFMACRO <name> <symbol> ... ) is equivalent to |
| 223 |
;;; (DEFMACRO <name> (&REST <symbol>) ...). |
| 224 |
;;; Defmacro-lambda-list is defined as: |
| 225 |
;;; |
| 226 |
;;; ( { v }* |
| 227 |
;;; [ &optional { sym | ( v [ init [ v ] ] ) }* ] |
| 228 |
;;; { [ { &rest | &body } v ] |
| 229 |
;;; [ &key { sym | ( { sym | ( key v ) } [ init [ v ]] ) }* |
| 230 |
;;; [ &allow-other-keys ]] |
| 231 |
;;; [ &aux { sym | ( v [ init ] ) }* ] |
| 232 |
;;; | . sym } |
| 233 |
;;; ) |
| 234 |
(defun find-documentation (body) |
| 235 |
(nth-value 3 (process-declarations body t))) |
| 236 |
|
| 237 |
(defun remove-documentation (body) |
| 238 |
(multiple-value-bind (decls body doc) |
| 239 |
(process-declarations body t) |
| 240 |
(when decls (push `(declare ,@decls) body)) |
| 241 |
(values body doc))) |
| 242 |
|
| 243 |
(defun find-declarations (body &optional (doc t)) |
| 244 |
(multiple-value-bind (decls body doc) |
| 245 |
(process-declarations body doc) |
| 246 |
(values (if decls `((declare ,@decls)) nil) |
| 247 |
body doc))) |
| 248 |
|
| 249 |
;; Optional argument context can be 'cl:define-setf-expander, |
| 250 |
;; 'cl:define-compiler-macro, 'cl:deftype or 'cl:defmacro (default) |
| 251 |
(defun sys::expand-defmacro (name vl body &optional (context 'cl:defmacro)) |
| 252 |
(multiple-value-bind (decls body doc) |
| 253 |
(find-declarations body) |
| 254 |
;; We turn (a . b) into (a &rest b) |
| 255 |
;; This is required because MEMBER (used below) does not like improper lists |
| 256 |
(let ((cell (last vl))) |
| 257 |
(when (rest cell) |
| 258 |
(setq vl (nconc (butlast vl 0) (list '&rest (rest cell)))))) |
| 259 |
;; If we find an &environment variable in the lambda list, we take not of the |
| 260 |
;; name and remove it from the list so that DESTRUCTURE does not get confused |
| 261 |
(let ((env (member '&environment vl :test #'eq))) |
| 262 |
(if env |
| 263 |
(setq vl (nconc (ldiff vl env) (cddr env)) |
| 264 |
env (second env)) |
| 265 |
(setq env (gensym) |
| 266 |
decls (list* `(declare (ignore ,env)) decls))) |
| 267 |
(multiple-value-bind (ppn whole dl arg-check ignorables) |
| 268 |
(destructure vl context) |
| 269 |
(let ((function |
| 270 |
(if (eql context 'cl:defmacro) |
| 271 |
`(ext::lambda-block ,name (,whole ,env &aux ,@dl) |
| 272 |
(declare (ignorable ,@ignorables)) |
| 273 |
,@decls |
| 274 |
,@arg-check |
| 275 |
,@body) |
| 276 |
`(lambda (,whole ,env &aux ,@dl) |
| 277 |
(declare (ignorable ,@ignorables)) |
| 278 |
,@decls |
| 279 |
(block ,(si::function-block-name name) |
| 280 |
,@arg-check ,@body))))) |
| 281 |
(values function |
| 282 |
ppn |
| 283 |
doc)))))) |
| 284 |
|
| 285 |
#+ecl-min |
| 286 |
(si::fset 'defmacro |
| 287 |
#'(ext::lambda-block defmacro (def env) |
| 288 |
(declare (ignore env)) |
| 289 |
(let* ((name (second def)) |
| 290 |
(vl (third def)) |
| 291 |
(body (cdddr def)) |
| 292 |
(function)) |
| 293 |
(multiple-value-bind (function pprint doc) |
| 294 |
(sys::expand-defmacro name vl body) |
| 295 |
(declare (ignore doc)) |
| 296 |
(setq function `(function ,function)) |
| 297 |
(when *dump-defmacro-definitions* |
| 298 |
(print function) |
| 299 |
(setq function `(si::bc-disassemble ,function))) |
| 300 |
(ext:register-with-pde def `(si::fset ',name ,function t ,pprint))))) |
| 301 |
t) |
| 302 |
|
| 303 |
;;; valid lambda-list to DESTRUCTURING-BIND is: |
| 304 |
;;; |
| 305 |
;;; ( [ &whole sym ] |
| 306 |
;;; { v }* |
| 307 |
;;; [ &optional { sym | ( v [ init [ v ] ] ) }* ] |
| 308 |
;;; { [ { &rest | &body } v ] |
| 309 |
;;; [ &key { sym | ( { sym | ( key v ) } [ init [ v ]] ) }* |
| 310 |
;;; [ &allow-other-keys ]] |
| 311 |
;;; [ &aux { sym | ( v [ init ] ) }* ] |
| 312 |
;;; | . sym } |
| 313 |
;;; ) |
| 314 |
;;; |
| 315 |
;;; where v is short for { destructuring-bind-lambda-list | sym }. |
| 316 |
;;; A symbol may be accepted as a DESTRUCTURING-BIND lambda-list, in which case |
| 317 |
;;; (DESTRUCTURING-BIND <name> <symbol> ... ) is equivalent to |
| 318 |
;;; (DESTRUCTURING-BIND <name> (&REST <symbol>) ...). |
| 319 |
;;; Destructuring-bind-lambda-list is defined as: |
| 320 |
;;; |
| 321 |
;;; ( [ &whole sym ] |
| 322 |
;;; { v }* |
| 323 |
;;; [ &optional { sym | ( v [ init [ v ] ] ) }* ] |
| 324 |
;;; { [ { &rest | &body } v ] |
| 325 |
;;; [ &key { sym | ( { sym | ( key v ) } [ init [ v ]] ) }* |
| 326 |
;;; [ &allow-other-keys ]] |
| 327 |
;;; [ &aux { sym | ( v [ init ] ) }* ] |
| 328 |
;;; | . sym } |
| 329 |
;;; ) |
| 330 |
|
| 331 |
(defmacro destructuring-bind (vl list &body body) |
| 332 |
(multiple-value-bind (decls body) |
| 333 |
(find-declarations body) |
| 334 |
(multiple-value-bind (ppn whole dl arg-check ignorables) |
| 335 |
(destructure vl nil) |
| 336 |
(declare (ignore ppn)) |
| 337 |
`(let* ((,whole ,list) ,@dl) |
| 338 |
(declare (ignorable ,@ignorables)) |
| 339 |
,@decls |
| 340 |
,@arg-check |
| 341 |
,@body)))) |
| 342 |
|
| 343 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 344 |
;;; |
| 345 |
;;; MACROLET HELPER |
| 346 |
;;; |
| 347 |
|
| 348 |
(defun cmp-env-for-bytecodes (old-env) |
| 349 |
"Produce an environment which is safe to pass to the bytecodes |
| 350 |
compiler. We remove all blocks and tags and ensure that |
| 351 |
references to local variables will cause an error. This |
| 352 |
environment can be used to bytecompile the functions in MACROLET |
| 353 |
or SYMBOL-MACRO forms, and also to evaluate other forms." |
| 354 |
(declare (si::c-local)) |
| 355 |
(flet ((local-var-error-function (name) |
| 356 |
#'(lambda (whole env) |
| 357 |
(declare (ignore whole env)) |
| 358 |
(error |
| 359 |
"In a MACROLET function you tried to access a local variable, ~A, |
| 360 |
from the function in which it appears." name))) |
| 361 |
(local-fun-error-function (name) |
| 362 |
#'(lambda (whole env) |
| 363 |
(declare (ignore whole env)) |
| 364 |
(error |
| 365 |
"In a MACROLET function you tried to access a local function, ~A, |
| 366 |
from the function in which it appears." name)))) |
| 367 |
(cons (do ((env (car old-env) (cdr env)) |
| 368 |
(variables '())) |
| 369 |
((endp env) (nreverse variables)) |
| 370 |
(let ((i (car env))) |
| 371 |
(if (consp i) |
| 372 |
(let ((name (first i))) |
| 373 |
(if (not (keywordp name)) |
| 374 |
(push (if (second i) |
| 375 |
i |
| 376 |
(list name 'si::symbol-macro (local-var-error-function name))) |
| 377 |
variables)))))) |
| 378 |
(do ((env (cdr old-env) (cdr env)) |
| 379 |
(macros '())) |
| 380 |
((endp env) (nreverse macros)) |
| 381 |
(let ((i (car env))) |
| 382 |
(if (consp i) |
| 383 |
(push (if (eq (second i) 'SI::MACRO) |
| 384 |
i |
| 385 |
(list (first i) 'SI:MACRO (local-fun-error-function (first i)))) |
| 386 |
macros))))))) |
| 387 |
|
| 388 |
(defun macrolet-functions (definitions old-env) |
| 389 |
(declare (si::c-local)) |
| 390 |
(let ((env (cmp-env-for-bytecodes old-env))) |
| 391 |
(si::eval-with-env |
| 392 |
(cons 'list |
| 393 |
(mapcar #'(lambda (x) |
| 394 |
(let* ((name (first x)) |
| 395 |
(llist (second x)) |
| 396 |
(def (cddr x))) |
| 397 |
`(list ',name ,(si::expand-defmacro name llist def)))) |
| 398 |
definitions)) |
| 399 |
env nil t))) |
| 400 |
|
| 401 |
(defun cmp-env-register-macrolet (definitions old-env) |
| 402 |
(let ((macros (cdr old-env))) |
| 403 |
(dolist (record (macrolet-functions definitions old-env)) |
| 404 |
(push (list (first record) 'si::macro (second record)) |
| 405 |
macros)) |
| 406 |
(rplacd (truly-the cons old-env) macros))) |