| 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 |
;;;; The structure routines. |
| 12 |
|
| 13 |
(in-package "SYSTEM") |
| 14 |
|
| 15 |
(defun slot-access-function (conc-name slot-name) |
| 16 |
(if conc-name |
| 17 |
(intern (base-string-concatenate conc-name slot-name)) |
| 18 |
slot-name)) |
| 19 |
|
| 20 |
#+threads |
| 21 |
(defun make-atomic-accessors (name conc-name type slot-descriptions removep) |
| 22 |
;; If the structure is implemented as a CLOS instance, we can define |
| 23 |
;; atomic compare-and-swap expansions for its slots |
| 24 |
(when (null type) |
| 25 |
(let (accessors) |
| 26 |
(dolist (slotd slot-descriptions accessors) |
| 27 |
(let ((access-function (slot-access-function conc-name (nth 0 slotd))) |
| 28 |
(read-only (nth 3 slotd)) |
| 29 |
(offset (nth 4 slotd))) |
| 30 |
(if (and (null read-only) (null removep)) |
| 31 |
(push `(mp::define-cas-expander ,access-function (x) |
| 32 |
(let ((old (gensym)) (new (gensym))) |
| 33 |
(values nil nil old new |
| 34 |
`(mp:compare-and-swap-structure ,x ',',name ,',offset ,old ,new) |
| 35 |
`(sys:structure-ref ,x ',',name ,',offset)))) |
| 36 |
accessors) |
| 37 |
;; Don't create a CAS expansion if the slot is read-only. |
| 38 |
(push `(mp::remcas ',access-function) accessors))))))) |
| 39 |
|
| 40 |
(defun si::structure-type-error (value slot-type struct-name slot-name) |
| 41 |
(error 'simple-type-error |
| 42 |
:format-control "Slot ~A in structure ~A only admits values of type ~A." |
| 43 |
:format-arguments (list slot-name struct-name slot-type) |
| 44 |
:datum value |
| 45 |
:expected-type slot-type)) |
| 46 |
|
| 47 |
(defun make-access-function (name conc-name type named slot-descr) |
| 48 |
(declare (ignore named) |
| 49 |
(si::c-local)) |
| 50 |
(let* ((slot-name (nth 0 slot-descr)) |
| 51 |
;; (default-init (nth 1 slot-descr)) |
| 52 |
;; (slot-type (nth 2 slot-descr)) |
| 53 |
(read-only (nth 3 slot-descr)) |
| 54 |
(offset (nth 4 slot-descr)) |
| 55 |
(access-function (slot-access-function conc-name slot-name))) |
| 56 |
(if (eql access-function (sixth slot-descr)) |
| 57 |
(return-from make-access-function nil) |
| 58 |
(setf (sixth slot-descr) access-function)) |
| 59 |
(cond ((null type) |
| 60 |
;; If TYPE is NIL, |
| 61 |
;; the slot is at the offset in the structure-body. |
| 62 |
(fset access-function #'(lambda (x) |
| 63 |
(sys:structure-ref x name offset)))) |
| 64 |
((subtypep type '(OR LIST VECTOR)) |
| 65 |
;; If TYPE is VECTOR, (VECTOR ... ) or LIST, ELT is used. |
| 66 |
(fset access-function |
| 67 |
#'(lambda (x) (elt x offset)))) |
| 68 |
(t (error "~S is an illegal structure type." type))) |
| 69 |
(cond (read-only |
| 70 |
(fmakunbound `(setf ,access-function)) |
| 71 |
(set-documentation access-function 'SETF nil)) |
| 72 |
;; The following is used by the compiler to expand inline |
| 73 |
;; the accessor |
| 74 |
(t |
| 75 |
(do-setf-structure-method access-function (or type name) |
| 76 |
offset))))) |
| 77 |
|
| 78 |
|
| 79 |
(defun do-setf-structure-method (access-function type index) |
| 80 |
(declare (si::c-local)) |
| 81 |
(put-sysprop access-function 'STRUCTURE-ACCESS (cons type index)) |
| 82 |
(do-defsetf access-function |
| 83 |
(cond ((or (eq type 'list) (eq type 'vector)) |
| 84 |
#'(lambda (newvalue struct) |
| 85 |
`(sys::elt-set ,struct ,index ,newvalue))) |
| 86 |
((consp type) |
| 87 |
#'(lambda (newvalue struct) |
| 88 |
`(si::aset (the ,type ,struct) ,index ,newvalue))) |
| 89 |
(t |
| 90 |
#'(lambda (newvalue struct) |
| 91 |
`(sys::structure-set ,struct ',type ,index ,newvalue)))))) |
| 92 |
|
| 93 |
(defun process-boa-lambda-list (slot-names slot-descriptions boa-list assertions) |
| 94 |
(declare (si::c-local)) |
| 95 |
(let ((mentioned-slots '()) |
| 96 |
(aux)) |
| 97 |
;; With a call to PROCESS-LAMBDA-LIST we ensure that the lambda list is |
| 98 |
;; syntactically correct. This simplifies notably the code in the loop. |
| 99 |
(process-lambda-list (setq boa-list (copy-list boa-list)) 'FUNCTION) |
| 100 |
;; Search for &optional or &key arguments without initialization. Also, |
| 101 |
;; record all slot names which are initialized by means of the BOA call. |
| 102 |
(do* ((i boa-list (rest i)) |
| 103 |
(slot (first i) (first i)) |
| 104 |
(modify nil)) |
| 105 |
((endp i)) |
| 106 |
(cond ((or (eq slot '&optional) (eq slot '&key)) |
| 107 |
(setq modify t)) |
| 108 |
((eq slot '&rest) |
| 109 |
(setq modify nil)) |
| 110 |
((eq slot '&aux) |
| 111 |
(setq aux t modify nil)) |
| 112 |
((eq slot '&allow-other-keys) |
| 113 |
) |
| 114 |
((atom slot) |
| 115 |
(push slot mentioned-slots) |
| 116 |
(when modify |
| 117 |
(setf (first i) |
| 118 |
(list slot (second (assoc slot slot-descriptions))))) |
| 119 |
(when aux |
| 120 |
(setf assertions (delete slot assertions :key 'cadadr)))) |
| 121 |
(t |
| 122 |
(let ((slot-name (first slot))) |
| 123 |
(when (consp slot-name) |
| 124 |
(setq slot-name (second slot-name))) |
| 125 |
(push slot-name mentioned-slots) |
| 126 |
(when (endp (rest slot)) |
| 127 |
(when modify |
| 128 |
(setf (rest slot) |
| 129 |
(list (second (assoc slot-name slot-descriptions))))) |
| 130 |
(when aux |
| 131 |
(setf assertions (delete slot assertions :key 'cadadr)))))))) |
| 132 |
;; For all slots not mentioned above, add the default values from |
| 133 |
;; the DEFSTRUCT slot description. |
| 134 |
(let ((other-slots (nset-difference |
| 135 |
(delete-if #'consp (copy-list slot-names)) |
| 136 |
mentioned-slots))) |
| 137 |
(do ((l other-slots (cdr l))) |
| 138 |
((endp l)) |
| 139 |
(let* ((slot (assoc (car l) slot-descriptions)) |
| 140 |
(slot-init (second slot))) |
| 141 |
(when slot-init |
| 142 |
(setf (car l) (list (car l) slot-init))))) |
| 143 |
(when other-slots |
| 144 |
(unless aux |
| 145 |
(push '&aux other-slots)) |
| 146 |
(setf boa-list (nconc boa-list other-slots))) |
| 147 |
(values boa-list assertions)))) |
| 148 |
|
| 149 |
(defun make-constructor (name constructor type named slot-descriptions env) |
| 150 |
(declare (ignore named) |
| 151 |
(si::c-local)) |
| 152 |
;; CONSTRUCTOR := constructor-name | (constructor-name boa-lambda-list) |
| 153 |
(let* ((boa-constructor-p (consp constructor)) |
| 154 |
(keys (unless boa-constructor-p (list '&key))) |
| 155 |
(constructor-name (if boa-constructor-p (first constructor) constructor)) |
| 156 |
(slot-names '()) |
| 157 |
(assertions '())) |
| 158 |
(dolist (slot slot-descriptions |
| 159 |
(setq slot-names (nreverse slot-names) keys (nreverse keys))) |
| 160 |
(push |
| 161 |
(cond ((null slot) |
| 162 |
;; If slot-description is NIL, it is padding for initial-offset. |
| 163 |
nil) |
| 164 |
((eql (first slot) 'TYPED-STRUCTURE-NAME) |
| 165 |
;; This slot is the name of a typed structure with name. |
| 166 |
(list 'QUOTE (second slot))) |
| 167 |
(t |
| 168 |
(let* ((slot-name (first slot)) |
| 169 |
(slot-type (third slot)) |
| 170 |
(offset (fifth slot)) |
| 171 |
(init-form (second slot)) |
| 172 |
(var-name slot-name)) |
| 173 |
;; Unless BOA constructors are used, we should avoid using |
| 174 |
;; slot names as lambda variables in the constructor. |
| 175 |
(unless boa-constructor-p |
| 176 |
(setq var-name (copy-symbol slot-name)) |
| 177 |
(push (if init-form (list var-name init-form) var-name) |
| 178 |
keys)) |
| 179 |
;; We insert type checks for every slot and only in the |
| 180 |
;; case of BOA lists we remove some of these checks for |
| 181 |
;; uninitialized slots. |
| 182 |
(unless (eq 'T slot-type) |
| 183 |
(push `(unless (typep ,var-name ',(flatten-function-types slot-type env)) |
| 184 |
(structure-type-error ,var-name ',slot-type ',name ',slot-name)) |
| 185 |
assertions)) |
| 186 |
var-name))) |
| 187 |
slot-names)) |
| 188 |
(when boa-constructor-p |
| 189 |
(setf (values keys assertions) |
| 190 |
(process-boa-lambda-list slot-names slot-descriptions |
| 191 |
(second constructor) assertions))) |
| 192 |
(cond ((null type) |
| 193 |
`(defun ,constructor-name ,keys |
| 194 |
,@assertions |
| 195 |
#-CLOS |
| 196 |
(sys:make-structure ',name ,@slot-names) |
| 197 |
#+CLOS |
| 198 |
;; the class is defined by an enclosing LET form |
| 199 |
(sys:make-structure .structure-constructor-class. ,@slot-names))) |
| 200 |
((subtypep type '(VECTOR T)) |
| 201 |
`(defun ,constructor-name ,keys |
| 202 |
(vector ,@slot-names))) |
| 203 |
((subtypep type 'VECTOR) |
| 204 |
`(defun ,constructor-name ,keys |
| 205 |
(make-array ',(list (length slot-names)) |
| 206 |
:element-type ',(closest-sequence-type type) |
| 207 |
:initial-contents (list ,@slot-names)))) |
| 208 |
((eq type 'LIST) |
| 209 |
`(defun ,constructor-name ,keys |
| 210 |
(list ,@slot-names))) |
| 211 |
((error "~S is an illegal structure type" type))))) |
| 212 |
|
| 213 |
|
| 214 |
(defun make-predicate (name type named name-offset) |
| 215 |
(declare (si::c-local)) |
| 216 |
(cond ((null type) |
| 217 |
#'(lambda (x) |
| 218 |
(structure-subtype-p x name))) |
| 219 |
((or (eq type 'VECTOR) |
| 220 |
(and (consp type) (eq (car type) 'VECTOR))) |
| 221 |
;; The name is at the NAME-OFFSET in the vector. |
| 222 |
(unless named (error "The structure should be named.")) |
| 223 |
#'(lambda (x) |
| 224 |
(and (vectorp x) |
| 225 |
(> (length x) name-offset) |
| 226 |
;; AKCL has (aref (the (vector t) x).) |
| 227 |
;; which fails with strings |
| 228 |
(eq (elt x name-offset) name)))) |
| 229 |
((eq type 'LIST) |
| 230 |
;; The name is at the NAME-OFFSET in the list. |
| 231 |
(unless named (error "The structure should be named.")) |
| 232 |
(if (= name-offset 0) |
| 233 |
#'(lambda (x) |
| 234 |
(and (consp x) (eq (car x) name))) |
| 235 |
#'(lambda (x) |
| 236 |
(do ((i name-offset (1- i)) |
| 237 |
(y x (cdr y))) |
| 238 |
((= i 0) (and (consp y) (eq (car y) name))) |
| 239 |
(declare (fixnum i)) |
| 240 |
(unless (consp y) (return nil)))))) |
| 241 |
((error "~S is an illegal structure type.")))) |
| 242 |
|
| 243 |
|
| 244 |
;;; PARSE-SLOT-DESCRIPTION parses the given slot-description |
| 245 |
;;; and returns a list of the form: |
| 246 |
;;; (slot-name default-init slot-type read-only offset accessor-name) |
| 247 |
|
| 248 |
(defun parse-slot-description (slot-description offset &optional read-only) |
| 249 |
(declare (si::c-local)) |
| 250 |
(when (atom slot-description) |
| 251 |
(return-from parse-slot-description |
| 252 |
(list slot-description nil t read-only offset nil))) |
| 253 |
(do ((slot-type T) |
| 254 |
(slot-name (first slot-description)) |
| 255 |
(default-init (second slot-description)) |
| 256 |
(os (cddr slot-description) (cddr os))) |
| 257 |
((endp os) |
| 258 |
(list slot-name default-init slot-type read-only offset nil)) |
| 259 |
(when (endp (cdr os)) |
| 260 |
(error "~S is an illegal structure slot option." os)) |
| 261 |
(let ((option (first os)) |
| 262 |
(value (second os))) |
| 263 |
(case option |
| 264 |
(:TYPE (setq slot-type value)) |
| 265 |
(:READ-ONLY (setq read-only value)) |
| 266 |
(t (error "~S is an illegal structure slot option." os)))))) |
| 267 |
|
| 268 |
;;; OVERWRITE-SLOT-DESCRIPTIONS overwrites the old slot-descriptions |
| 269 |
;;; with the new descriptions which are specified in the |
| 270 |
;;; :include defstruct option. |
| 271 |
|
| 272 |
(defun overwrite-slot-descriptions (new-slots old-slots) |
| 273 |
(declare (si::c-local)) |
| 274 |
(do* ((output '()) |
| 275 |
(old-slots old-slots (rest old-slots))) |
| 276 |
((null old-slots) |
| 277 |
(nreverse output)) |
| 278 |
(let* ((old-slot (first old-slots)) |
| 279 |
(slot-name (first old-slot)) |
| 280 |
(new-slot (first (member slot-name new-slots :key #'car)))) |
| 281 |
(if (null new-slot) |
| 282 |
(setf new-slot old-slot) |
| 283 |
(let* ((old-read-only (fourth old-slot)) |
| 284 |
(new-read-only (fourth new-slot))) |
| 285 |
(cond ((and (null new-read-only) |
| 286 |
old-read-only) |
| 287 |
(error "Tried to turn a read only slot ~A into writeable." |
| 288 |
slot-name)) |
| 289 |
((eq new-read-only :unknown) |
| 290 |
(setf new-read-only old-read-only))) |
| 291 |
(setf new-slot (copy-list new-slot) |
| 292 |
(fourth new-slot) new-read-only |
| 293 |
(fifth new-slot) (fifth old-slot) ; preserve offset |
| 294 |
(sixth new-slot) (sixth old-slot)))) |
| 295 |
(push new-slot output)))) |
| 296 |
|
| 297 |
(defun %struct-layout-compatible-p (old-slot-descriptions new-slot-descriptions) |
| 298 |
(declare (si::c-local)) |
| 299 |
(do* ((old-defs old-slot-descriptions (cdr old-defs)) |
| 300 |
(new-defs new-slot-descriptions (cdr new-defs)) |
| 301 |
(old-def (car old-defs)) |
| 302 |
(new-def (car new-defs))) |
| 303 |
((or (null old-defs) (null new-defs)) |
| 304 |
;; Structures must have the same number of compatible slots. |
| 305 |
(and (null old-defs) |
| 306 |
(null new-defs))) |
| 307 |
(or (cond ((or (null old-def) |
| 308 |
(null new-def)) |
| 309 |
(eql old-def new-def)) |
| 310 |
((or (eql (car old-def) 'typed-structure-name) |
| 311 |
(eql (car new-def) 'typed-structure-name)) |
| 312 |
(and (null (cddr old-def)) |
| 313 |
(equal old-def new-def))) |
| 314 |
(t |
| 315 |
(destructuring-bind (old-slot-name old-init old-type |
| 316 |
old-read-only old-offset old-ac) |
| 317 |
old-def |
| 318 |
(declare (ignore old-init old-read-only old-ac)) |
| 319 |
(destructuring-bind (new-slot-name new-init new-type |
| 320 |
new-read-only new-offset new-ac) |
| 321 |
new-def |
| 322 |
(declare (ignore new-init new-read-only new-ac)) |
| 323 |
;; Names are compared with STRING= because: |
| 324 |
;; a) structure may be macroexpanded with gensym's |
| 325 |
;; b) keyword initargs and accessor names ignore the package |
| 326 |
;; -- jd 2021-12-10 |
| 327 |
(and (string= old-slot-name new-slot-name) |
| 328 |
(= old-offset new-offset) |
| 329 |
(and (multiple-value-bind (subtypep certain) |
| 330 |
(subtypep old-type new-type) |
| 331 |
(or (not certain) subtypep)) |
| 332 |
(multiple-value-bind (subtypep certain) |
| 333 |
(subtypep new-type old-type) |
| 334 |
(or (not certain) subtypep)))))))) |
| 335 |
(return-from %struct-layout-compatible-p nil)))) |
| 336 |
|
| 337 |
(defun define-structure (name conc-name type named slots slot-descriptions |
| 338 |
copier include print-function print-object constructors |
| 339 |
offset name-offset documentation predicate) |
| 340 |
(mapl (lambda (descriptions) |
| 341 |
(destructuring-bind (arg-desc . descs) descriptions |
| 342 |
(let ((name (car arg-desc))) |
| 343 |
(when (and arg-desc |
| 344 |
(not (eql name 'typed-structure-name)) |
| 345 |
(member name descs :key #'car :test #'equal)) |
| 346 |
(error "Duplicate slot name ~a." name))))) |
| 347 |
slot-descriptions) |
| 348 |
(when (get-sysprop name 'is-a-structure) |
| 349 |
(let ((old-slot-descriptions (get-sysprop name 'structure-slot-descriptions))) |
| 350 |
(unless (%struct-layout-compatible-p old-slot-descriptions slot-descriptions) |
| 351 |
(error "Attempt to redefine the structure ~S incompatibly with the current definition." name)))) |
| 352 |
(create-type-name name) |
| 353 |
;; We are going to modify this list!!! |
| 354 |
(setf slot-descriptions (copy-tree slot-descriptions)) |
| 355 |
#+clos |
| 356 |
(unless type |
| 357 |
(clos:ensure-class |
| 358 |
name |
| 359 |
:direct-superclasses (and include (list include)) |
| 360 |
:direct-slots (mapcar |
| 361 |
#'(lambda (sd) |
| 362 |
(if sd |
| 363 |
(list* :name (first sd) |
| 364 |
:initform (second sd) |
| 365 |
:initargs |
| 366 |
(list |
| 367 |
(intern (symbol-name (first sd)) |
| 368 |
(find-package 'KEYWORD))) |
| 369 |
(when (third sd) (list :type (third sd)))) |
| 370 |
nil)) ; for initial offset slots |
| 371 |
slot-descriptions) |
| 372 |
:metaclass 'structure-class)) |
| 373 |
#+clos |
| 374 |
(when print-function |
| 375 |
(clos::install-method 'print-object nil (list name t) '(obj stream) |
| 376 |
#'(lambda (obj stream) |
| 377 |
(funcall print-function obj stream 0) |
| 378 |
obj))) |
| 379 |
#+clos |
| 380 |
(when print-object |
| 381 |
(clos::install-method 'print-object nil (list name t) '(obj stream) |
| 382 |
#'(lambda (obj stream) |
| 383 |
(funcall print-object obj stream) |
| 384 |
obj))) |
| 385 |
(when predicate |
| 386 |
(fset predicate (make-predicate name type named name-offset))) |
| 387 |
(put-sysprop name 'DEFSTRUCT-FORM `(defstruct ,name ,@slots)) |
| 388 |
(put-sysprop name 'IS-A-STRUCTURE t) |
| 389 |
(put-sysprop name 'STRUCTURE-SLOT-DESCRIPTIONS slot-descriptions) |
| 390 |
(put-sysprop name 'STRUCTURE-INCLUDE include) |
| 391 |
(put-sysprop name 'STRUCTURE-PRINT-FUNCTION print-function) |
| 392 |
(put-sysprop name 'STRUCTURE-TYPE type) |
| 393 |
(put-sysprop name 'STRUCTURE-NAMED named) |
| 394 |
(put-sysprop name 'STRUCTURE-OFFSET offset) |
| 395 |
(put-sysprop name 'STRUCTURE-CONSTRUCTORS constructors) |
| 396 |
#+clos |
| 397 |
(when *keep-documentation* |
| 398 |
(set-documentation name 'STRUCTURE documentation)) |
| 399 |
(dolist (x slot-descriptions) |
| 400 |
(and x |
| 401 |
(not (eql (car x) 'TYPED-STRUCTURE-NAME)) |
| 402 |
(funcall #'make-access-function name conc-name type named x))) |
| 403 |
(when copier |
| 404 |
(fset copier #'copy-structure)) |
| 405 |
#+clos |
| 406 |
(unless type |
| 407 |
(find-class name))) |
| 408 |
|
| 409 |
;;; The DEFSTRUCT macro. |
| 410 |
|
| 411 |
(defmacro defstruct (&whole whole name&opts &rest slots &environment env) |
| 412 |
"Syntax: (defstruct |
| 413 |
{name | (name {:conc-name | (:conc-name prefix-string) | |
| 414 |
:constructor | (:constructor symbol [lambda-list]) | |
| 415 |
:copier | (:copier symbol) | |
| 416 |
:predicate | (:predicate symbol) | |
| 417 |
(:include symbol) | |
| 418 |
(:print-function function) | |
| 419 |
(:print-object function) | |
| 420 |
(:type {vector | (vector type) | list}) | |
| 421 |
:named | |
| 422 |
(:initial-offset number)}*)} |
| 423 |
[doc] |
| 424 |
{slot-name | |
| 425 |
(slot-name [default-value-form] {:type type | :read-only flag}*) }* |
| 426 |
) |
| 427 |
Defines a structure named by NAME. The doc-string DOC, if supplied, is saved |
| 428 |
as a STRUCTURE doc and can be retrieved by (documentation 'NAME 'structure)." |
| 429 |
(let*((slot-descriptions slots) |
| 430 |
(name (if (consp name&opts) (first name&opts) name&opts)) |
| 431 |
(options (when (consp name&opts) (rest name&opts))) |
| 432 |
(conc-name (base-string-concatenate name "-")) |
| 433 |
(default-constructor (intern (base-string-concatenate "MAKE-" name))) |
| 434 |
(copier (intern (base-string-concatenate "COPY-" name))) |
| 435 |
(predicate (intern (base-string-concatenate name "-P"))) |
| 436 |
constructors no-constructor |
| 437 |
predicate-specified |
| 438 |
include |
| 439 |
print-function print-object type named initial-offset |
| 440 |
offset name-offset |
| 441 |
documentation |
| 442 |
(atomic-accessors t)) |
| 443 |
|
| 444 |
;; Parse the defstruct options. |
| 445 |
(do ((os options (cdr os)) (o) (v)) |
| 446 |
((endp os)) |
| 447 |
(cond ((and (consp (car os)) (not (endp (cdar os)))) |
| 448 |
(setq o (caar os) v (cadar os)) |
| 449 |
(case o |
| 450 |
(:CONC-NAME |
| 451 |
(if (null v) |
| 452 |
(setq conc-name nil) |
| 453 |
(setq conc-name v))) |
| 454 |
(:CONSTRUCTOR |
| 455 |
(if (null v) |
| 456 |
(setq no-constructor t) |
| 457 |
(if (endp (cddar os)) |
| 458 |
(setq constructors (cons v constructors)) |
| 459 |
(setq constructors (cons (cdar os) constructors))))) |
| 460 |
(:COPIER (setq copier v)) |
| 461 |
(:PREDICATE |
| 462 |
(setq predicate v) |
| 463 |
(setq predicate-specified t)) |
| 464 |
(:INCLUDE |
| 465 |
(setq include (cdar os)) |
| 466 |
(unless (get-sysprop v 'IS-A-STRUCTURE) |
| 467 |
(error "~S is an illegal included structure." v))) |
| 468 |
(:PRINT-FUNCTION (setq print-function (if (symbolp v) |
| 469 |
`(quote ,v) |
| 470 |
`(function ,v)))) |
| 471 |
(:PRINT-OBJECT (setq print-object (if (symbolp v) |
| 472 |
`(quote ,v) |
| 473 |
`(function ,v)))) |
| 474 |
(:TYPE (setq type v)) |
| 475 |
(:INITIAL-OFFSET (setq initial-offset v)) |
| 476 |
(:ATOMIC-ACCESSORS (setq atomic-accessors v)) |
| 477 |
(t (error "~S is an illegal defstruct option." o)))) |
| 478 |
(t |
| 479 |
(if (consp (car os)) |
| 480 |
(setq o (caar os)) |
| 481 |
(setq o (car os))) |
| 482 |
(case o |
| 483 |
(:CONSTRUCTOR |
| 484 |
(setq constructors |
| 485 |
(cons default-constructor constructors))) |
| 486 |
(:CONC-NAME |
| 487 |
(setq conc-name nil)) |
| 488 |
((:COPIER :PREDICATE :PRINT-FUNCTION :PRINT-OBJECT :ATOMIC-ACCESSORS)) |
| 489 |
(:NAMED (setq named t)) |
| 490 |
(t (error "~S is an illegal defstruct option." o)))))) |
| 491 |
|
| 492 |
;; Skip the documentation string. |
| 493 |
(when (and (not (endp slot-descriptions)) |
| 494 |
(stringp (car slot-descriptions))) |
| 495 |
(setq documentation (car slot-descriptions)) |
| 496 |
(setq slot-descriptions (cdr slot-descriptions))) |
| 497 |
|
| 498 |
;; Check the include option. |
| 499 |
(when (and include |
| 500 |
(not (equal type (get-sysprop (car include) 'STRUCTURE-TYPE)))) |
| 501 |
(error "~S is an illegal structure include." (car include))) |
| 502 |
|
| 503 |
;; Set OFFSET. |
| 504 |
(setq offset (if include |
| 505 |
(get-sysprop (car include) 'STRUCTURE-OFFSET) |
| 506 |
0)) |
| 507 |
|
| 508 |
;; Increment OFFSET. |
| 509 |
(when (and type initial-offset) |
| 510 |
(setq offset (+ offset initial-offset))) |
| 511 |
(when (and type named) |
| 512 |
(unless (or (subtypep '(vector symbol) type) |
| 513 |
(subtypep type 'list)) |
| 514 |
(error "Structure cannot have type ~S and be :NAMED." type)) |
| 515 |
(setq name-offset offset) |
| 516 |
(setq offset (1+ offset))) |
| 517 |
|
| 518 |
;; Parse slot-descriptions, incrementing OFFSET for each one. |
| 519 |
(do ((ds slot-descriptions (cdr ds)) |
| 520 |
(sds nil)) |
| 521 |
((endp ds) |
| 522 |
(setq slot-descriptions (nreverse sds))) |
| 523 |
(push (parse-slot-description (car ds) offset) sds) |
| 524 |
(setq offset (1+ offset))) |
| 525 |
|
| 526 |
;; If TYPE is non-NIL and structure is named, |
| 527 |
;; add the slot for the structure-name to the slot-descriptions. |
| 528 |
(when (and type named) |
| 529 |
(setq slot-descriptions |
| 530 |
(cons (list 'TYPED-STRUCTURE-NAME name) slot-descriptions))) |
| 531 |
|
| 532 |
;; Pad the slot-descriptions with the initial-offset number of NILs. |
| 533 |
(when (and type initial-offset) |
| 534 |
(setq slot-descriptions |
| 535 |
(append (make-list initial-offset) slot-descriptions))) |
| 536 |
|
| 537 |
;; Append the slot-descriptions of the included structure. |
| 538 |
;; The slot-descriptions in the include option are also counted. |
| 539 |
(cond ((null include)) |
| 540 |
((endp (cdr include)) |
| 541 |
(setq slot-descriptions |
| 542 |
(append (get-sysprop (car include) 'STRUCTURE-SLOT-DESCRIPTIONS) |
| 543 |
slot-descriptions))) |
| 544 |
(t |
| 545 |
(setq slot-descriptions |
| 546 |
(append (overwrite-slot-descriptions |
| 547 |
(mapcar #'(lambda (sd) |
| 548 |
(parse-slot-description sd 0 :unknown)) |
| 549 |
(cdr include)) |
| 550 |
(get-sysprop (car include) 'STRUCTURE-SLOT-DESCRIPTIONS)) |
| 551 |
slot-descriptions)))) |
| 552 |
|
| 553 |
(cond (no-constructor |
| 554 |
;; If a constructor option is NIL, |
| 555 |
;; no constructor should have been specified. |
| 556 |
(when constructors |
| 557 |
(error "Contradictory constructor options."))) |
| 558 |
((null constructors) |
| 559 |
;; If no constructor is specified, |
| 560 |
;; the default-constructor is made. |
| 561 |
(setq constructors (list default-constructor)))) |
| 562 |
|
| 563 |
;; Check the named option and set the predicate. |
| 564 |
(when (and type (not named)) |
| 565 |
(when (and predicate-specified predicate) |
| 566 |
(error "~S is an illegal structure predicate." |
| 567 |
predicate)) |
| 568 |
(setq predicate nil)) |
| 569 |
|
| 570 |
(when include (setq include (car include))) |
| 571 |
|
| 572 |
;; Check the print-function. |
| 573 |
(when (and print-function type) |
| 574 |
(error "A print function is supplied to a typed structure.")) |
| 575 |
|
| 576 |
;; |
| 577 |
;; The constructors rely on knowing the structure class. For toplevel |
| 578 |
;; forms we can use LOAD-TIME-VALUE. For non-toplevel forms, we can not |
| 579 |
;; as the class might be defined _after_ the system decides to evaluate |
| 580 |
;; LOAD-TIME-VALUE. |
| 581 |
;; |
| 582 |
(let ((core `(define-structure ',name ',conc-name ',type ',named ',slots |
| 583 |
',slot-descriptions ',copier ',include |
| 584 |
,print-function ,print-object ',constructors |
| 585 |
',offset ',name-offset |
| 586 |
',documentation ',predicate)) |
| 587 |
(constructors (mapcar #'(lambda (constructor) |
| 588 |
(make-constructor name constructor type named |
| 589 |
slot-descriptions env)) |
| 590 |
constructors))) |
| 591 |
`(progn |
| 592 |
(eval-when (:compile-toplevel :load-toplevel) |
| 593 |
,core |
| 594 |
,(si::register-with-pde whole) |
| 595 |
,@(subst `(load-time-value (find-class ',name)) |
| 596 |
'.structure-constructor-class. |
| 597 |
constructors)) |
| 598 |
(eval-when (:execute) |
| 599 |
(let ((.structure-constructor-class. ,core)) |
| 600 |
,@constructors)) |
| 601 |
|
| 602 |
#+threads |
| 603 |
(eval-when (:compile-toplevel :load-toplevel :execute) |
| 604 |
;; When ATOMIC-ACCESSORS is NIL then MP:REMCAS is collected instead. |
| 605 |
,@(make-atomic-accessors name conc-name type slot-descriptions (not atomic-accessors))) |
| 606 |
',name)))) |