| 1 | ;;; defstruct.lisp |
|---|
| 2 | ;;; |
|---|
| 3 | ;;; Copyright (C) 2003-2007 Peter Graves <peter@armedbear.org> |
|---|
| 4 | ;;; $Id: defstruct.lisp 14162 2012-10-03 21:28:11Z ehuelsmann $ |
|---|
| 5 | ;;; |
|---|
| 6 | ;;; This program is free software; you can redistribute it and/or |
|---|
| 7 | ;;; modify it under the terms of the GNU General Public License |
|---|
| 8 | ;;; as published by the Free Software Foundation; either version 2 |
|---|
| 9 | ;;; of the License, or (at your option) any later version. |
|---|
| 10 | ;;; |
|---|
| 11 | ;;; This program is distributed in the hope that it will be useful, |
|---|
| 12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | ;;; GNU General Public License for more details. |
|---|
| 15 | ;;; |
|---|
| 16 | ;;; You should have received a copy of the GNU General Public License |
|---|
| 17 | ;;; along with this program; if not, write to the Free Software |
|---|
| 18 | ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 19 | ;;; |
|---|
| 20 | ;;; As a special exception, the copyright holders of this library give you |
|---|
| 21 | ;;; permission to link this library with independent modules to produce an |
|---|
| 22 | ;;; executable, regardless of the license terms of these independent |
|---|
| 23 | ;;; modules, and to copy and distribute the resulting executable under |
|---|
| 24 | ;;; terms of your choice, provided that you also meet, for each linked |
|---|
| 25 | ;;; independent module, the terms and conditions of the license of that |
|---|
| 26 | ;;; module. An independent module is a module which is not derived from |
|---|
| 27 | ;;; or based on this library. If you modify this library, you may extend |
|---|
| 28 | ;;; this exception to your version of the library, but you are not |
|---|
| 29 | ;;; obligated to do so. If you do not wish to do so, delete this |
|---|
| 30 | ;;; exception statement from your version. |
|---|
| 31 | |
|---|
| 32 | (in-package "SYSTEM") |
|---|
| 33 | |
|---|
| 34 | (export 'compiler-defstruct) |
|---|
| 35 | |
|---|
| 36 | ;;; DEFSTRUCT-DESCRIPTION |
|---|
| 37 | |
|---|
| 38 | (defmacro dd-name (x) `(aref ,x 0)) |
|---|
| 39 | (defmacro dd-conc-name (x) `(aref ,x 1)) |
|---|
| 40 | (defmacro dd-default-constructor (x) `(aref ,x 2)) |
|---|
| 41 | (defmacro dd-constructors (x) `(aref ,x 3)) |
|---|
| 42 | (defmacro dd-copier (x) `(aref ,x 4)) |
|---|
| 43 | (defmacro dd-include (x) `(aref ,x 5)) |
|---|
| 44 | (defmacro dd-type (x) `(aref ,x 6)) |
|---|
| 45 | (defmacro dd-named (x) `(aref ,x 7)) |
|---|
| 46 | (defmacro dd-initial-offset (x) `(aref ,x 8)) |
|---|
| 47 | (defmacro dd-predicate (x) `(aref ,x 9)) |
|---|
| 48 | (defmacro dd-print-function (x) `(aref ,x 10)) |
|---|
| 49 | (defmacro dd-print-object (x) `(aref ,x 11)) |
|---|
| 50 | (defmacro dd-direct-slots (x) `(aref ,x 12)) |
|---|
| 51 | (defmacro dd-slots (x) `(aref ,x 13)) |
|---|
| 52 | (defmacro dd-inherited-accessors (x) `(aref ,x 14)) |
|---|
| 53 | |
|---|
| 54 | (defun make-defstruct-description (&key name |
|---|
| 55 | conc-name |
|---|
| 56 | default-constructor |
|---|
| 57 | constructors |
|---|
| 58 | copier |
|---|
| 59 | include |
|---|
| 60 | type |
|---|
| 61 | named |
|---|
| 62 | initial-offset |
|---|
| 63 | predicate |
|---|
| 64 | print-function |
|---|
| 65 | print-object |
|---|
| 66 | direct-slots |
|---|
| 67 | slots |
|---|
| 68 | inherited-accessors) |
|---|
| 69 | (let ((dd (make-array 15))) |
|---|
| 70 | (setf (dd-name dd) name |
|---|
| 71 | (dd-conc-name dd) conc-name |
|---|
| 72 | (dd-default-constructor dd) default-constructor |
|---|
| 73 | (dd-constructors dd) constructors |
|---|
| 74 | (dd-copier dd) copier |
|---|
| 75 | (dd-include dd) include |
|---|
| 76 | (dd-type dd) type |
|---|
| 77 | (dd-named dd) named |
|---|
| 78 | (dd-initial-offset dd) initial-offset |
|---|
| 79 | (dd-predicate dd) predicate |
|---|
| 80 | (dd-print-function dd) print-function |
|---|
| 81 | (dd-print-object dd) print-object |
|---|
| 82 | (dd-direct-slots dd) direct-slots |
|---|
| 83 | (dd-slots dd) slots |
|---|
| 84 | (dd-inherited-accessors dd) inherited-accessors) |
|---|
| 85 | dd)) |
|---|
| 86 | |
|---|
| 87 | ;;; DEFSTRUCT-SLOT-DESCRIPTION |
|---|
| 88 | |
|---|
| 89 | (defmacro dsd-name (x) `(aref ,x 1)) |
|---|
| 90 | (defmacro dsd-index (x) `(aref ,x 2)) |
|---|
| 91 | (defmacro dsd-reader (x) `(aref ,x 3)) |
|---|
| 92 | (defmacro dsd-initform (x) `(aref ,x 4)) |
|---|
| 93 | (defmacro dsd-type (x) `(aref ,x 5)) |
|---|
| 94 | (defmacro dsd-read-only (x) `(aref ,x 6)) |
|---|
| 95 | |
|---|
| 96 | (defun make-defstruct-slot-description (&key name |
|---|
| 97 | index |
|---|
| 98 | reader |
|---|
| 99 | initform |
|---|
| 100 | (type t) |
|---|
| 101 | read-only) |
|---|
| 102 | (let ((dsd (make-array 7))) |
|---|
| 103 | (setf (aref dsd 0) 'defstruct-slot-description |
|---|
| 104 | (dsd-name dsd) name |
|---|
| 105 | (dsd-index dsd) index |
|---|
| 106 | (dsd-reader dsd) reader |
|---|
| 107 | (dsd-initform dsd) initform |
|---|
| 108 | (dsd-type dsd) type |
|---|
| 109 | (dsd-read-only dsd) read-only) |
|---|
| 110 | dsd)) |
|---|
| 111 | |
|---|
| 112 | (defvar *dd-name*) |
|---|
| 113 | (defvar *dd-conc-name*) |
|---|
| 114 | (defvar *dd-default-constructor*) |
|---|
| 115 | (defvar *dd-constructors*) |
|---|
| 116 | (defvar *dd-copier*) |
|---|
| 117 | (defvar *dd-include*) |
|---|
| 118 | (defvar *dd-type*) |
|---|
| 119 | (defvar *dd-default-slot-type* t) |
|---|
| 120 | (defvar *dd-named*) |
|---|
| 121 | (defvar *dd-initial-offset*) |
|---|
| 122 | (defvar *dd-predicate*) |
|---|
| 123 | (defvar *dd-print-function*) |
|---|
| 124 | (defvar *dd-print-object*) |
|---|
| 125 | (defvar *dd-direct-slots*) |
|---|
| 126 | (defvar *dd-slots*) |
|---|
| 127 | (defvar *dd-inherited-accessors*) |
|---|
| 128 | (defvar *dd-documentation*) |
|---|
| 129 | |
|---|
| 130 | (defun keywordify (symbol) |
|---|
| 131 | (intern (symbol-name symbol) +keyword-package+)) |
|---|
| 132 | |
|---|
| 133 | (defun define-keyword-constructor (constructor) |
|---|
| 134 | (let* ((constructor-name (car constructor)) |
|---|
| 135 | (keys ()) |
|---|
| 136 | (values ())) |
|---|
| 137 | (dolist (slot *dd-slots*) |
|---|
| 138 | (let ((name (dsd-name slot)) |
|---|
| 139 | (initform (dsd-initform slot))) |
|---|
| 140 | (if (or name (dsd-reader slot)) |
|---|
| 141 | (let ((dummy (gensym))) |
|---|
| 142 | (push (list (list (keywordify name) dummy) initform) keys) |
|---|
| 143 | (push dummy values)) |
|---|
| 144 | (push initform values)))) |
|---|
| 145 | (setf keys (cons '&key (nreverse keys)) |
|---|
| 146 | values (nreverse values)) |
|---|
| 147 | (cond ((eq *dd-type* 'list) |
|---|
| 148 | `((defun ,constructor-name ,keys |
|---|
| 149 | (list ,@values)))) |
|---|
| 150 | ((or (eq *dd-type* 'vector) |
|---|
| 151 | (and (consp *dd-type*) (eq (car *dd-type*) 'vector))) |
|---|
| 152 | (let ((element-type (if (consp *dd-type*) (cadr *dd-type*) t))) |
|---|
| 153 | `((defun ,constructor-name ,keys |
|---|
| 154 | (make-array ,(length values) |
|---|
| 155 | :element-type ',element-type |
|---|
| 156 | :initial-contents (list ,@values)))))) |
|---|
| 157 | ((<= 1 (length values) 6) |
|---|
| 158 | `((defun ,constructor-name ,keys |
|---|
| 159 | (make-structure (truly-the symbol ',*dd-name*) ,@values)))) |
|---|
| 160 | (t |
|---|
| 161 | `((defun ,constructor-name ,keys |
|---|
| 162 | (%make-structure (truly-the symbol ',*dd-name*) (list ,@values)))))))) |
|---|
| 163 | |
|---|
| 164 | (defun find-dsd (name) |
|---|
| 165 | (dolist (dsd *dd-slots*) |
|---|
| 166 | (when (string= name (dsd-name dsd)) |
|---|
| 167 | (return dsd)))) |
|---|
| 168 | |
|---|
| 169 | (defun get-slot (name) |
|---|
| 170 | ;; (let ((res (find name (dd-slots defstruct) :test #'string= :key #'dsd-name))) |
|---|
| 171 | (let ((res nil)) |
|---|
| 172 | (dolist (dsd *dd-slots*) |
|---|
| 173 | (when (string= name (dsd-name dsd)) |
|---|
| 174 | (setf res dsd) |
|---|
| 175 | (return))) |
|---|
| 176 | (if res |
|---|
| 177 | (values (dsd-type res) (dsd-initform res)) |
|---|
| 178 | (values t nil)))) |
|---|
| 179 | |
|---|
| 180 | (defun define-boa-constructor (constructor) |
|---|
| 181 | (multiple-value-bind (req opt restp rest keyp keys allowp auxp aux) |
|---|
| 182 | (parse-lambda-list (cadr constructor)) |
|---|
| 183 | (let ((arglist ()) |
|---|
| 184 | (vars ()) |
|---|
| 185 | (types ()) |
|---|
| 186 | (skipped-vars ())) |
|---|
| 187 | (dolist (arg req) |
|---|
| 188 | (push arg arglist) |
|---|
| 189 | (push arg vars) |
|---|
| 190 | (push (get-slot arg) types)) |
|---|
| 191 | (when opt |
|---|
| 192 | (push '&optional arglist) |
|---|
| 193 | (dolist (arg opt) |
|---|
| 194 | (cond ((consp arg) |
|---|
| 195 | (destructuring-bind |
|---|
| 196 | (name |
|---|
| 197 | &optional |
|---|
| 198 | (def (nth-value 1 (get-slot name))) |
|---|
| 199 | (supplied-test nil supplied-test-p)) |
|---|
| 200 | arg |
|---|
| 201 | (push `(,name ,def ,@(if supplied-test-p `(,supplied-test) nil)) arglist) |
|---|
| 202 | (push name vars) |
|---|
| 203 | (push (get-slot name) types))) |
|---|
| 204 | (t |
|---|
| 205 | (multiple-value-bind (type default) (get-slot arg) |
|---|
| 206 | (push `(,arg ,default) arglist) |
|---|
| 207 | (push arg vars) |
|---|
| 208 | (push type types)))))) |
|---|
| 209 | (when restp |
|---|
| 210 | (push '&rest arglist) |
|---|
| 211 | (push rest arglist) |
|---|
| 212 | (push rest vars) |
|---|
| 213 | (push 'list types)) |
|---|
| 214 | (when keyp |
|---|
| 215 | (push '&key arglist) |
|---|
| 216 | (dolist (key keys) |
|---|
| 217 | (if (consp key) |
|---|
| 218 | (destructuring-bind (wot |
|---|
| 219 | &optional |
|---|
| 220 | (def nil def-p) |
|---|
| 221 | (supplied-test nil supplied-test-p)) |
|---|
| 222 | key |
|---|
| 223 | (let ((name (if (consp wot) |
|---|
| 224 | (destructuring-bind (key var) wot |
|---|
| 225 | (declare (ignore key)) |
|---|
| 226 | var) |
|---|
| 227 | wot))) |
|---|
| 228 | (multiple-value-bind (type slot-def) |
|---|
| 229 | (get-slot name) |
|---|
| 230 | (push `(,wot ,(if def-p def slot-def) |
|---|
| 231 | ,@(if supplied-test-p `(,supplied-test) nil)) |
|---|
| 232 | arglist) |
|---|
| 233 | (push name vars) |
|---|
| 234 | (push type types)))) |
|---|
| 235 | (multiple-value-bind (type default) (get-slot key) |
|---|
| 236 | (push `(,key ,default) arglist) |
|---|
| 237 | (push key vars) |
|---|
| 238 | (push type types))))) |
|---|
| 239 | (when allowp |
|---|
| 240 | (push '&allow-other-keys arglist)) |
|---|
| 241 | (when auxp |
|---|
| 242 | (push '&aux arglist) |
|---|
| 243 | (dolist (arg aux) |
|---|
| 244 | (push arg arglist) |
|---|
| 245 | (if (and (consp arg) (eql (length arg) 2)) |
|---|
| 246 | (let ((var (first arg))) |
|---|
| 247 | (push var vars) |
|---|
| 248 | (push (get-slot var) types)) |
|---|
| 249 | (push (if (consp arg) (first arg) arg) skipped-vars)))) |
|---|
| 250 | (setq arglist (nreverse arglist)) |
|---|
| 251 | (setq vars (nreverse vars)) |
|---|
| 252 | (setq types (nreverse types)) |
|---|
| 253 | (setq skipped-vars (nreverse skipped-vars)) |
|---|
| 254 | (let ((values ())) |
|---|
| 255 | (dolist (dsd *dd-slots*) |
|---|
| 256 | (let ((name (dsd-name dsd)) |
|---|
| 257 | var) |
|---|
| 258 | (cond ((find name skipped-vars :test #'string=) |
|---|
| 259 | (push nil values)) |
|---|
| 260 | ((setf var (find name vars :test #'string=)) |
|---|
| 261 | (push var values)) |
|---|
| 262 | (t |
|---|
| 263 | (push (dsd-initform dsd) values))))) |
|---|
| 264 | (setf values (nreverse values)) |
|---|
| 265 | (let* ((constructor-name (car constructor))) |
|---|
| 266 | (cond ((eq *dd-type* 'list) |
|---|
| 267 | `((defun ,constructor-name ,arglist |
|---|
| 268 | (list ,@values)))) |
|---|
| 269 | ((or (eq *dd-type* 'vector) |
|---|
| 270 | (and (consp *dd-type*) (eq (car *dd-type*) 'vector))) |
|---|
| 271 | (let ((element-type (if (consp *dd-type*) (cadr *dd-type*) t))) |
|---|
| 272 | `((defun ,constructor-name ,arglist |
|---|
| 273 | (make-array ,(length values) |
|---|
| 274 | :element-type ',element-type |
|---|
| 275 | :initial-contents (list ,@values)))))) |
|---|
| 276 | ((<= 1 (length values) 6) |
|---|
| 277 | `((declaim (inline ,constructor-name)) |
|---|
| 278 | (defun ,constructor-name ,arglist |
|---|
| 279 | (make-structure (truly-the symbol ',*dd-name*) ,@values)))) |
|---|
| 280 | (t |
|---|
| 281 | `((declaim (inline ,constructor-name)) |
|---|
| 282 | (defun ,constructor-name ,arglist |
|---|
| 283 | (%make-structure (truly-the symbol ',*dd-name*) (list ,@values))))))))))) |
|---|
| 284 | |
|---|
| 285 | (defun default-constructor-name () |
|---|
| 286 | (intern (concatenate 'string "MAKE-" (symbol-name *dd-name*)))) |
|---|
| 287 | |
|---|
| 288 | (defun define-constructors () |
|---|
| 289 | (if *dd-constructors* |
|---|
| 290 | (let ((results ())) |
|---|
| 291 | (dolist (constructor *dd-constructors*) |
|---|
| 292 | (when (car constructor) |
|---|
| 293 | (setf results (nconc results |
|---|
| 294 | (if (cadr constructor) |
|---|
| 295 | (define-boa-constructor constructor) |
|---|
| 296 | (define-keyword-constructor constructor)))))) |
|---|
| 297 | results) |
|---|
| 298 | (define-keyword-constructor (cons (default-constructor-name) nil)))) |
|---|
| 299 | |
|---|
| 300 | (defun name-index () |
|---|
| 301 | (dolist (dsd *dd-slots*) |
|---|
| 302 | (let ((name (dsd-name dsd)) |
|---|
| 303 | (initform (dsd-initform dsd))) |
|---|
| 304 | (when (and (null name) |
|---|
| 305 | (equal initform (list 'quote *dd-name*))) |
|---|
| 306 | (return-from name-index (dsd-index dsd))))) |
|---|
| 307 | ;; We shouldn't get here. |
|---|
| 308 | nil) |
|---|
| 309 | |
|---|
| 310 | (defun define-predicate () |
|---|
| 311 | (when (and *dd-predicate* |
|---|
| 312 | (or *dd-named* (null *dd-type*))) |
|---|
| 313 | (let ((pred (if (symbolp *dd-predicate*) |
|---|
| 314 | *dd-predicate* |
|---|
| 315 | (intern *dd-predicate*)))) |
|---|
| 316 | (cond ((eq *dd-type* 'list) |
|---|
| 317 | (let ((index (name-index))) |
|---|
| 318 | `((defun ,pred (object) |
|---|
| 319 | (and (consp object) |
|---|
| 320 | (> (length object) ,index) |
|---|
| 321 | (eq (nth ,index object) ',*dd-name*)))))) |
|---|
| 322 | ((or (eq *dd-type* 'vector) |
|---|
| 323 | (and (consp *dd-type*) (eq (car *dd-type*) 'vector))) |
|---|
| 324 | (let ((index (name-index))) |
|---|
| 325 | `((defun ,pred (object) |
|---|
| 326 | (and (vectorp object) |
|---|
| 327 | (> (length object) ,index) |
|---|
| 328 | (eq (aref object ,index) ',*dd-name*)))))) |
|---|
| 329 | (t |
|---|
| 330 | `((defun ,pred (object) |
|---|
| 331 | (simple-typep object ',*dd-name*)))))))) |
|---|
| 332 | |
|---|
| 333 | (defun make-list-reader (index) |
|---|
| 334 | #'(lambda (instance) |
|---|
| 335 | (elt instance index))) |
|---|
| 336 | |
|---|
| 337 | (defun make-vector-reader (index) |
|---|
| 338 | #'(lambda (instance) |
|---|
| 339 | (aref instance index))) |
|---|
| 340 | |
|---|
| 341 | (defun make-structure-reader (index structure-type) |
|---|
| 342 | (declare (ignore structure-type)) |
|---|
| 343 | #'(lambda (instance) |
|---|
| 344 | ;; (unless (typep instance structure-type) |
|---|
| 345 | ;; (error 'type-error |
|---|
| 346 | ;; :datum instance |
|---|
| 347 | ;; :expected-type structure-type)) |
|---|
| 348 | (structure-ref instance index))) |
|---|
| 349 | |
|---|
| 350 | (defun define-reader (slot) |
|---|
| 351 | (let ((accessor-name (dsd-reader slot)) |
|---|
| 352 | (index (dsd-index slot)) |
|---|
| 353 | (type (dsd-type slot))) |
|---|
| 354 | (cond ((eq *dd-type* 'list) |
|---|
| 355 | `((declaim (ftype (function * ,type) ,accessor-name)) |
|---|
| 356 | (setf (symbol-function ',accessor-name) |
|---|
| 357 | (make-list-reader ,index)))) |
|---|
| 358 | ((or (eq *dd-type* 'vector) |
|---|
| 359 | (and (consp *dd-type*) (eq (car *dd-type*) 'vector))) |
|---|
| 360 | `((declaim (ftype (function * ,type) ,accessor-name)) |
|---|
| 361 | (setf (symbol-function ',accessor-name) |
|---|
| 362 | (make-vector-reader ,index)) |
|---|
| 363 | (define-source-transform ,accessor-name (instance) |
|---|
| 364 | `(aref (truly-the ,',*dd-type* ,instance) ,,index)))) |
|---|
| 365 | (t |
|---|
| 366 | `((declaim (ftype (function * ,type) ,accessor-name)) |
|---|
| 367 | (setf (symbol-function ',accessor-name) |
|---|
| 368 | (make-structure-reader ,index ',*dd-name*)) |
|---|
| 369 | (define-source-transform ,accessor-name (instance) |
|---|
| 370 | ,(if (eq type 't) |
|---|
| 371 | ``(structure-ref (the ,',*dd-name* ,instance) ,,index) |
|---|
| 372 | ``(the ,',type |
|---|
| 373 | (structure-ref (the ,',*dd-name* ,instance) ,,index))))))))) |
|---|
| 374 | |
|---|
| 375 | (defun make-list-writer (index) |
|---|
| 376 | #'(lambda (value instance) |
|---|
| 377 | (%set-elt instance index value))) |
|---|
| 378 | |
|---|
| 379 | (defun make-vector-writer (index) |
|---|
| 380 | #'(lambda (value instance) |
|---|
| 381 | (aset instance index value))) |
|---|
| 382 | |
|---|
| 383 | (defun make-structure-writer (index structure-type) |
|---|
| 384 | (declare (ignore structure-type)) |
|---|
| 385 | #'(lambda (value instance) |
|---|
| 386 | ;; (unless (typep instance structure-type) |
|---|
| 387 | ;; (error 'type-error |
|---|
| 388 | ;; :datum instance |
|---|
| 389 | ;; :expected-type structure-type)) |
|---|
| 390 | (structure-set instance index value))) |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | |
|---|
| 394 | (defun define-writer (slot) |
|---|
| 395 | (let ((accessor-name (dsd-reader slot)) |
|---|
| 396 | (index (dsd-index slot))) |
|---|
| 397 | (cond ((eq *dd-type* 'list) |
|---|
| 398 | `((setf (get ',accessor-name 'setf-function) |
|---|
| 399 | (make-list-writer ,index)))) |
|---|
| 400 | ((or (eq *dd-type* 'vector) |
|---|
| 401 | (and (consp *dd-type*) (eq (car *dd-type*) 'vector))) |
|---|
| 402 | `((setf (get ',accessor-name 'setf-function) |
|---|
| 403 | (make-vector-writer ,index)) |
|---|
| 404 | (define-source-transform (setf ,accessor-name) (value instance) |
|---|
| 405 | `(aset (truly-the ,',*dd-type* ,instance) ,,index ,value)))) |
|---|
| 406 | (t |
|---|
| 407 | `((setf (get ',accessor-name 'setf-function) |
|---|
| 408 | (make-structure-writer ,index ',*dd-name*)) |
|---|
| 409 | (define-source-transform (setf ,accessor-name) (value instance) |
|---|
| 410 | `(structure-set (the ,',*dd-name* ,instance) |
|---|
| 411 | ,,index ,value))))))) |
|---|
| 412 | |
|---|
| 413 | (defun define-access-functions () |
|---|
| 414 | (let ((result ())) |
|---|
| 415 | (dolist (slot *dd-slots*) |
|---|
| 416 | (let ((accessor-name (dsd-reader slot))) |
|---|
| 417 | (unless (null accessor-name) |
|---|
| 418 | (unless (assoc accessor-name *dd-inherited-accessors*) |
|---|
| 419 | (setf result (nconc result (define-reader slot))) |
|---|
| 420 | (unless (dsd-read-only slot) |
|---|
| 421 | (setf result (nconc result (define-writer slot)))))))) |
|---|
| 422 | result)) |
|---|
| 423 | |
|---|
| 424 | (defun define-copier () |
|---|
| 425 | (when *dd-copier* |
|---|
| 426 | (cond ((eq *dd-type* 'list) |
|---|
| 427 | `((setf (fdefinition ',*dd-copier*) #'copy-list))) |
|---|
| 428 | ((or (eq *dd-type* 'vector) |
|---|
| 429 | (and (consp *dd-type*) (eq (car *dd-type*) 'vector))) |
|---|
| 430 | `((setf (fdefinition ',*dd-copier*) #'copy-seq))) |
|---|
| 431 | (t |
|---|
| 432 | `((setf (fdefinition ',*dd-copier*) #'copy-structure)))))) |
|---|
| 433 | |
|---|
| 434 | (defun define-print-function () |
|---|
| 435 | (cond (*dd-print-function* |
|---|
| 436 | (if (cadr *dd-print-function*) |
|---|
| 437 | `((defmethod print-object ((instance ,*dd-name*) stream) |
|---|
| 438 | (funcall (function ,(cadr *dd-print-function*)) |
|---|
| 439 | instance stream *current-print-level*))) |
|---|
| 440 | `((defmethod print-object ((instance ,*dd-name*) stream) |
|---|
| 441 | (write-string (%write-to-string instance) stream))))) |
|---|
| 442 | (*dd-print-object* |
|---|
| 443 | (if (cadr *dd-print-object*) |
|---|
| 444 | `((defmethod print-object ((instance ,*dd-name*) stream) |
|---|
| 445 | (funcall (function ,(cadr *dd-print-object*)) |
|---|
| 446 | instance stream))) |
|---|
| 447 | `((defmethod print-object ((instance ,*dd-name*) stream) |
|---|
| 448 | (write-string (%write-to-string instance) stream))))) |
|---|
| 449 | (t |
|---|
| 450 | nil))) |
|---|
| 451 | |
|---|
| 452 | (defun parse-1-option (option) |
|---|
| 453 | (case (car option) |
|---|
| 454 | (:conc-name |
|---|
| 455 | (setf *dd-conc-name* (if (symbolp (cadr option)) |
|---|
| 456 | (cadr option) |
|---|
| 457 | (make-symbol (string (cadr option)))))) |
|---|
| 458 | (:constructor |
|---|
| 459 | (let* ((args (cdr option)) |
|---|
| 460 | (numargs (length args))) |
|---|
| 461 | (case numargs |
|---|
| 462 | (0 ; Use default name. |
|---|
| 463 | (push (list (default-constructor-name) nil) *dd-constructors*)) |
|---|
| 464 | (1 |
|---|
| 465 | (push (list (car args) nil) *dd-constructors*)) |
|---|
| 466 | (2 |
|---|
| 467 | (push args *dd-constructors*))))) |
|---|
| 468 | (:copier |
|---|
| 469 | (when (eql (length option) 2) |
|---|
| 470 | (setf *dd-copier* (cadr option)))) |
|---|
| 471 | (:include |
|---|
| 472 | (setf *dd-include* (cdr option))) |
|---|
| 473 | (:initial-offset |
|---|
| 474 | (setf *dd-initial-offset* (cadr option))) |
|---|
| 475 | (:predicate |
|---|
| 476 | (when (eql (length option) 2) |
|---|
| 477 | (setf *dd-predicate* (cadr option)))) |
|---|
| 478 | (:print-function |
|---|
| 479 | (setf *dd-print-function* option)) |
|---|
| 480 | (:print-object |
|---|
| 481 | (setf *dd-print-object* option)) |
|---|
| 482 | (:type |
|---|
| 483 | (setf *dd-type* (cadr option)) |
|---|
| 484 | (when (and (consp *dd-type*) (eq (car *dd-type*) 'vector)) |
|---|
| 485 | (unless (eq (second *dd-type*) '*) |
|---|
| 486 | (setf *dd-default-slot-type* (second *dd-type*))))))) |
|---|
| 487 | |
|---|
| 488 | (defun parse-name-and-options (name-and-options) |
|---|
| 489 | (setf *dd-name* (the symbol (car name-and-options))) |
|---|
| 490 | (setf *dd-conc-name* (make-symbol (concatenate 'string (symbol-name *dd-name*) "-"))) |
|---|
| 491 | (setf *dd-copier* (intern (concatenate 'string "COPY-" (symbol-name *dd-name*)))) |
|---|
| 492 | (setf *dd-predicate* (concatenate 'string (symbol-name *dd-name*) "-P")) |
|---|
| 493 | (let ((options (cdr name-and-options))) |
|---|
| 494 | (dolist (option options) |
|---|
| 495 | (cond ((consp option) |
|---|
| 496 | (parse-1-option option)) |
|---|
| 497 | ((eq option :named) |
|---|
| 498 | (setf *dd-named* t)) |
|---|
| 499 | ((member option '(:constructor :copier :predicate :named :conc-name)) |
|---|
| 500 | (parse-1-option (list option))) |
|---|
| 501 | (t |
|---|
| 502 | (error "Unrecognized DEFSTRUCT option: ~S." option)))))) |
|---|
| 503 | |
|---|
| 504 | (defun compiler-defstruct (name &key |
|---|
| 505 | conc-name |
|---|
| 506 | default-constructor |
|---|
| 507 | constructors |
|---|
| 508 | copier |
|---|
| 509 | include |
|---|
| 510 | type |
|---|
| 511 | named |
|---|
| 512 | initial-offset |
|---|
| 513 | predicate |
|---|
| 514 | print-function |
|---|
| 515 | print-object |
|---|
| 516 | direct-slots |
|---|
| 517 | slots |
|---|
| 518 | inherited-accessors |
|---|
| 519 | documentation) |
|---|
| 520 | (let ((description |
|---|
| 521 | (make-defstruct-description :name name |
|---|
| 522 | :conc-name conc-name |
|---|
| 523 | :default-constructor default-constructor |
|---|
| 524 | :constructors constructors |
|---|
| 525 | :copier copier |
|---|
| 526 | :include include |
|---|
| 527 | :type type |
|---|
| 528 | :named named |
|---|
| 529 | :initial-offset initial-offset |
|---|
| 530 | :predicate predicate |
|---|
| 531 | :print-function print-function |
|---|
| 532 | :print-object print-object |
|---|
| 533 | :direct-slots direct-slots |
|---|
| 534 | :slots slots |
|---|
| 535 | :inherited-accessors inherited-accessors)) |
|---|
| 536 | (old (get name 'structure-definition))) |
|---|
| 537 | (when old |
|---|
| 538 | (unless |
|---|
| 539 | ;; Assert that the structure definitions are exactly the same |
|---|
| 540 | ;; we need to support this type of redefinition during bootstrap |
|---|
| 541 | ;; building ourselves |
|---|
| 542 | (and (equalp (aref old 0) (aref description 0)) |
|---|
| 543 | ;; the CONC-NAME slot is an uninterned symbol if not supplied |
|---|
| 544 | ;; thus different on each redefinition round. Check that the |
|---|
| 545 | ;; names are equal, because it produces the same end result |
|---|
| 546 | ;; when they are. |
|---|
| 547 | (string= (aref old 1) (aref description 1)) |
|---|
| 548 | (equalp (aref old 5) (aref description 5)) |
|---|
| 549 | (equalp (aref old 6) (aref description 6)) |
|---|
| 550 | (equalp (aref old 7) (aref description 7)) |
|---|
| 551 | (equalp (aref old 8) (aref description 8)) |
|---|
| 552 | (every (lambda (x y) |
|---|
| 553 | (and (equalp (dsd-name x) (dsd-name y)) |
|---|
| 554 | (equalp (dsd-index x) (dsd-index y)) |
|---|
| 555 | (equalp (dsd-type x) (dsd-type y)))) |
|---|
| 556 | (append (aref old 12) (aref old 13)) |
|---|
| 557 | (append (aref description 12) |
|---|
| 558 | (aref description 13)))) |
|---|
| 559 | (error 'program-error |
|---|
| 560 | :format-control "Structure redefinition not supported ~ |
|---|
| 561 | in DEFSTRUCT for ~A" |
|---|
| 562 | :format-arguments (list name))) |
|---|
| 563 | ;; Since they're the same, continue with the old one. |
|---|
| 564 | (setf description old)) |
|---|
| 565 | (setf (get name 'structure-definition) description)) |
|---|
| 566 | (%set-documentation name 'structure documentation) |
|---|
| 567 | (when (or (null type) named) |
|---|
| 568 | (let ((structure-class |
|---|
| 569 | (make-structure-class name direct-slots slots (car include)))) |
|---|
| 570 | (%set-documentation name 'type documentation) |
|---|
| 571 | (%set-documentation structure-class t documentation))) |
|---|
| 572 | (when default-constructor |
|---|
| 573 | (proclaim `(ftype (function * t) ,default-constructor)))) |
|---|
| 574 | |
|---|
| 575 | (defmacro defstruct (name-and-options &rest slots) |
|---|
| 576 | (let ((*dd-name* nil) |
|---|
| 577 | (*dd-conc-name* nil) |
|---|
| 578 | (*dd-default-constructor* nil) |
|---|
| 579 | (*dd-constructors* nil) |
|---|
| 580 | (*dd-copier* nil) |
|---|
| 581 | (*dd-include* nil) |
|---|
| 582 | (*dd-type* nil) |
|---|
| 583 | (*dd-default-slot-type* t) |
|---|
| 584 | (*dd-named* nil) |
|---|
| 585 | (*dd-initial-offset* nil) |
|---|
| 586 | (*dd-predicate* nil) |
|---|
| 587 | (*dd-print-function* nil) |
|---|
| 588 | (*dd-print-object* nil) |
|---|
| 589 | (*dd-direct-slots* ()) |
|---|
| 590 | (*dd-slots* ()) |
|---|
| 591 | (*dd-inherited-accessors* ()) |
|---|
| 592 | (*dd-documentation* nil)) |
|---|
| 593 | (parse-name-and-options (if (atom name-and-options) |
|---|
| 594 | (list name-and-options) |
|---|
| 595 | name-and-options)) |
|---|
| 596 | (check-declaration-type *dd-name*) |
|---|
| 597 | (if *dd-constructors* |
|---|
| 598 | (dolist (constructor *dd-constructors*) |
|---|
| 599 | (unless (cadr constructor) |
|---|
| 600 | (setf *dd-default-constructor* (car constructor)) |
|---|
| 601 | (return))) |
|---|
| 602 | (setf *dd-default-constructor* (default-constructor-name))) |
|---|
| 603 | (when (stringp (car slots)) |
|---|
| 604 | (setf *dd-documentation* (pop slots))) |
|---|
| 605 | (dolist (slot slots) |
|---|
| 606 | (let* ((name (if (atom slot) slot (car slot))) |
|---|
| 607 | (reader (if *dd-conc-name* |
|---|
| 608 | (intern (concatenate 'string |
|---|
| 609 | (symbol-name *dd-conc-name*) |
|---|
| 610 | (symbol-name name))) |
|---|
| 611 | name)) |
|---|
| 612 | (initform (if (atom slot) nil (cadr slot))) |
|---|
| 613 | (dsd (apply #'make-defstruct-slot-description |
|---|
| 614 | :name name |
|---|
| 615 | :reader reader |
|---|
| 616 | :initform initform |
|---|
| 617 | (cond |
|---|
| 618 | ((atom slot) |
|---|
| 619 | (list :type *dd-default-slot-type*)) |
|---|
| 620 | ((getf (cddr slot) :type) |
|---|
| 621 | (cddr slot)) |
|---|
| 622 | (t |
|---|
| 623 | (list* :type *dd-default-slot-type* (cddr slot))))))) |
|---|
| 624 | (push dsd *dd-direct-slots*))) |
|---|
| 625 | (setf *dd-direct-slots* (nreverse *dd-direct-slots*)) |
|---|
| 626 | (let ((index 0)) |
|---|
| 627 | (when *dd-include* |
|---|
| 628 | (let ((dd (get (car *dd-include*) 'structure-definition))) |
|---|
| 629 | (unless dd |
|---|
| 630 | (error 'simple-error |
|---|
| 631 | :format-control "Class ~S is undefined." |
|---|
| 632 | :format-arguments (list (car *dd-include*)))) |
|---|
| 633 | (dolist (dsd (dd-slots dd)) |
|---|
| 634 | ;; MUST COPY SLOT DESCRIPTION! |
|---|
| 635 | (setf dsd (copy-seq dsd)) |
|---|
| 636 | (setf (dsd-index dsd) index |
|---|
| 637 | (dsd-reader dsd) |
|---|
| 638 | (if *dd-conc-name* |
|---|
| 639 | (intern (concatenate 'string |
|---|
| 640 | (symbol-name *dd-conc-name*) |
|---|
| 641 | (symbol-name (dsd-name dsd)))) |
|---|
| 642 | (dsd-name dsd))) |
|---|
| 643 | (push dsd *dd-slots*) |
|---|
| 644 | (incf index)) |
|---|
| 645 | (setf *dd-inherited-accessors* (dd-inherited-accessors dd)) |
|---|
| 646 | (dolist (dsd (dd-direct-slots dd)) |
|---|
| 647 | (push (cons (dsd-reader dsd) (dsd-name dsd)) |
|---|
| 648 | *dd-inherited-accessors*))) |
|---|
| 649 | (when (cdr *dd-include*) |
|---|
| 650 | (dolist (slot (cdr *dd-include*)) |
|---|
| 651 | (let* ((name (if (atom slot) slot (car slot))) |
|---|
| 652 | (initform (if (atom slot) nil (cadr slot))) |
|---|
| 653 | (dsd (find-dsd name))) |
|---|
| 654 | (when dsd |
|---|
| 655 | (setf (dsd-initform dsd) initform)))))) |
|---|
| 656 | (when *dd-initial-offset* |
|---|
| 657 | (dotimes (i *dd-initial-offset*) |
|---|
| 658 | (push (make-defstruct-slot-description :name nil |
|---|
| 659 | :index index |
|---|
| 660 | :reader nil |
|---|
| 661 | :initform nil |
|---|
| 662 | :type t |
|---|
| 663 | :read-only t) |
|---|
| 664 | *dd-slots*) |
|---|
| 665 | (incf index))) |
|---|
| 666 | (when *dd-named* |
|---|
| 667 | (push (make-defstruct-slot-description :name nil |
|---|
| 668 | :index index |
|---|
| 669 | :reader nil |
|---|
| 670 | :initform (list 'quote *dd-name*) |
|---|
| 671 | :type t |
|---|
| 672 | :read-only t) |
|---|
| 673 | *dd-slots*) |
|---|
| 674 | (incf index)) |
|---|
| 675 | (dolist (dsd *dd-direct-slots*) |
|---|
| 676 | (setf (dsd-index dsd) index) |
|---|
| 677 | (push dsd *dd-slots*) |
|---|
| 678 | (incf index))) |
|---|
| 679 | (setf *dd-slots* (nreverse *dd-slots*)) |
|---|
| 680 | `(progn |
|---|
| 681 | (eval-when (:compile-toplevel :load-toplevel :execute) |
|---|
| 682 | (compiler-defstruct ',*dd-name* |
|---|
| 683 | :conc-name ',*dd-conc-name* |
|---|
| 684 | :default-constructor ',*dd-default-constructor* |
|---|
| 685 | ,@(if *dd-constructors* `(:constructors ',*dd-constructors*)) |
|---|
| 686 | :copier ',*dd-copier* |
|---|
| 687 | ,@(if *dd-include* `(:include ',*dd-include*)) |
|---|
| 688 | ,@(if *dd-type* `(:type ',*dd-type*)) |
|---|
| 689 | ,@(if *dd-named* `(:named ,*dd-named*)) |
|---|
| 690 | ,@(if *dd-initial-offset* `(:initial-offset ,*dd-initial-offset*)) |
|---|
| 691 | :predicate ',*dd-predicate* |
|---|
| 692 | ,@(if *dd-print-function* `(:print-function ',*dd-print-function*)) |
|---|
| 693 | ,@(if *dd-print-object* `(:print-object ',*dd-print-object*)) |
|---|
| 694 | :direct-slots ',*dd-direct-slots* |
|---|
| 695 | :slots ',*dd-slots* |
|---|
| 696 | :inherited-accessors ',*dd-inherited-accessors* |
|---|
| 697 | :documentation ',*dd-documentation*)) |
|---|
| 698 | ,@(define-constructors) |
|---|
| 699 | ,@(define-predicate) |
|---|
| 700 | ,@(define-access-functions) |
|---|
| 701 | ,@(define-copier) |
|---|
| 702 | ,@(when (or *dd-print-function* *dd-print-object*) |
|---|
| 703 | `((require "PRINT-OBJECT"))) |
|---|
| 704 | ,@(define-print-function) |
|---|
| 705 | ',*dd-name*))) |
|---|
| 706 | |
|---|
| 707 | (defun defstruct-default-constructor (arg) |
|---|
| 708 | (let ((type (cond ((symbolp arg) |
|---|
| 709 | arg) |
|---|
| 710 | ((classp arg) |
|---|
| 711 | (class-name arg)) |
|---|
| 712 | (t |
|---|
| 713 | (type-of arg))))) |
|---|
| 714 | (when type |
|---|
| 715 | (let ((dd (get type 'structure-definition))) |
|---|
| 716 | (and dd (dd-default-constructor dd)))))) |
|---|