source: branches/0.15.x/abcl/src/org/armedbear/lisp/defstruct.lisp

Last change on this file was 11391, checked in by vvoutilainen, 17 years ago

ABCL license is GPL + Classpath exception. This was intended
by Peter Graves, the original author. For reference, see
http://sourceforge.net/mailarchive/forum.php?thread_name=20040721115302.839%40prufrock&forum_name=armedbear-j-announce

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 25.1 KB
Line 
1;;; defstruct.lisp
2;;;
3;;; Copyright (C) 2003-2007 Peter Graves <peter@armedbear.org>
4;;; $Id: defstruct.lisp 11391 2008-11-15 22:38:34Z vvoutilainen $
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
53(defun make-defstruct-description (&key name
54                                        conc-name
55                                        default-constructor
56                                        constructors
57                                        copier
58                                        include
59                                        type
60                                        named
61                                        initial-offset
62                                        predicate
63                                        print-function
64                                        print-object
65                                        direct-slots
66                                        slots)
67  (let ((dd (make-array 14)))
68    (setf (dd-name dd) name
69          (dd-conc-name dd) conc-name
70          (dd-default-constructor dd) default-constructor
71          (dd-constructors dd) constructors
72          (dd-copier dd) copier
73          (dd-include dd) include
74          (dd-type dd) type
75          (dd-named dd) named
76          (dd-initial-offset dd) initial-offset
77          (dd-predicate dd) predicate
78          (dd-print-function dd) print-function
79          (dd-print-object dd) print-object
80          (dd-direct-slots dd) direct-slots
81          (dd-slots dd) slots)
82    dd))
83
84;;; DEFSTRUCT-SLOT-DESCRIPTION
85
86(defmacro dsd-name (x)      `(aref ,x 1))
87(defmacro dsd-index (x)     `(aref ,x 2))
88(defmacro dsd-reader (x)    `(aref ,x 3))
89(defmacro dsd-initform (x)  `(aref ,x 4))
90(defmacro dsd-type (x)      `(aref ,x 5))
91(defmacro dsd-read-only (x) `(aref ,x 6))
92
93(defun make-defstruct-slot-description (&key name
94                                             index
95                                             reader
96                                             initform
97                                             (type t)
98                                             read-only)
99  (let ((dsd (make-array 7)))
100    (setf (aref dsd 0) 'defstruct-slot-description
101          (dsd-name dsd) name
102          (dsd-index dsd) index
103          (dsd-reader dsd) reader
104          (dsd-initform dsd) initform
105          (dsd-type dsd) type
106          (dsd-read-only dsd) read-only)
107    dsd))
108
109(defvar *dd-name*)
110(defvar *dd-conc-name*)
111(defvar *dd-default-constructor*)
112(defvar *dd-constructors*)
113(defvar *dd-copier*)
114(defvar *dd-include*)
115(defvar *dd-type*)
116(defvar *dd-named*)
117(defvar *dd-initial-offset*)
118(defvar *dd-predicate*)
119(defvar *dd-print-function*)
120(defvar *dd-print-object*)
121(defvar *dd-direct-slots*)
122(defvar *dd-slots*)
123
124(defun keywordify (symbol)
125  (intern (symbol-name symbol) +keyword-package+))
126
127(defun define-keyword-constructor (constructor)
128  (let* ((constructor-name (car constructor))
129         (keys ())
130         (values ()))
131    (dolist (slot *dd-slots*)
132      (let ((name (dsd-name slot))
133            (initform (dsd-initform slot)))
134        (if (or name (dsd-reader slot))
135            (let ((dummy (gensym)))
136              (push (list (list (keywordify name) dummy) initform) keys)
137              (push dummy values))
138            (push initform values))))
139    (setf keys (cons '&key (nreverse keys))
140          values (nreverse values))
141    (cond ((eq *dd-type* 'list)
142           `((defun ,constructor-name ,keys
143               (list ,@values))))
144          ((or (eq *dd-type* 'vector)
145               (and (consp *dd-type*) (eq (car *dd-type*) 'vector)))
146           (let ((element-type (if (consp *dd-type*) (cadr *dd-type*) t)))
147             `((defun ,constructor-name ,keys
148                 (make-array ,(length values)
149                             :element-type ',element-type
150                             :initial-contents (list ,@values))))))
151          ((<= 1 (length values) 6)
152           `((defun ,constructor-name ,keys
153               (make-structure (truly-the symbol ',*dd-name*) ,@values))))
154          (t
155           `((defun ,constructor-name ,keys
156               (%make-structure (truly-the symbol ',*dd-name*) (list ,@values))))))))
157
158(defun find-dsd (name)
159  (dolist (dsd *dd-slots*)
160    (when (string= name (dsd-name dsd))
161      (return dsd))))
162
163(defun get-slot (name)
164;;   (let ((res (find name (dd-slots defstruct) :test #'string= :key #'dsd-name)))
165  (let ((res nil))
166    (dolist (dsd *dd-slots*)
167      (when (string= name (dsd-name dsd))
168        (setf res dsd)
169        (return)))
170    (if res
171        (values (dsd-type res) (dsd-initform res))
172        (values t nil))))
173
174(defun define-boa-constructor (constructor)
175  (multiple-value-bind (req opt restp rest keyp keys allowp auxp aux)
176    (parse-lambda-list (cadr constructor))
177    (let ((arglist ())
178          (vars ())
179          (types ())
180          (skipped-vars ()))
181      (dolist (arg req)
182        (push arg arglist)
183        (push arg vars)
184        (push (get-slot arg) types))
185      (when opt
186        (push '&optional arglist)
187        (dolist (arg opt)
188          (cond ((consp arg)
189                 (destructuring-bind
190                  (name
191                   &optional
192                   (def (nth-value 1 (get-slot name)))
193                   (supplied-test nil supplied-test-p))
194                  arg
195                  (push `(,name ,def ,@(if supplied-test-p `(,supplied-test) nil)) arglist)
196                  (push name vars)
197                  (push (get-slot name) types)))
198                (t
199                 (multiple-value-bind (type default) (get-slot arg)
200                   (push `(,arg ,default) arglist)
201                   (push arg vars)
202                   (push type types))))))
203      (when restp
204        (push '&rest arglist)
205        (push rest arglist)
206        (push rest vars)
207        (push 'list types))
208      (when keyp
209        (push '&key arglist)
210        (dolist (key keys)
211          (if (consp key)
212              (destructuring-bind (wot
213                                   &optional
214                                   (def nil def-p)
215                                   (supplied-test nil supplied-test-p))
216                                  key
217                                  (let ((name (if (consp wot)
218                                                  (destructuring-bind (key var) wot
219                                                                      (declare (ignore key))
220                                                                      var)
221                                                  wot)))
222                                    (multiple-value-bind (type slot-def)
223                                      (get-slot name)
224                                      (push `(,wot ,(if def-p def slot-def)
225                                                   ,@(if supplied-test-p `(,supplied-test) nil))
226                                            arglist)
227                                      (push name vars)
228                                      (push type types))))
229              (multiple-value-bind (type default) (get-slot key)
230                (push `(,key ,default) arglist)
231                (push key vars)
232                (push type types)))))
233      (when allowp
234        (push '&allow-other-keys arglist))
235      (when auxp
236        (push '&aux arglist)
237        (dolist (arg aux)
238          (push arg arglist)
239          (if (and (consp arg) (eql (length arg) 2))
240              (let ((var (first arg)))
241                (push var vars)
242                (push (get-slot var) types))
243              (push (if (consp arg) (first arg) arg) skipped-vars))))
244      (setq arglist (nreverse arglist))
245      (setq vars (nreverse vars))
246      (setq types (nreverse types))
247      (setq skipped-vars (nreverse skipped-vars))
248      (let ((values ()))
249        (dolist (dsd *dd-slots*)
250          (let ((name (dsd-name dsd))
251                var)
252            (cond ((find name skipped-vars :test #'string=)
253                   (push nil values))
254                  ((setf var (find name vars :test #'string=))
255                   (push var values))
256                  (t
257                   (push (dsd-initform dsd) values)))))
258        (setf values (nreverse values))
259        (let* ((constructor-name (car constructor)))
260          (cond ((eq *dd-type* 'list)
261                 `((defun ,constructor-name ,arglist
262                     (list ,@values))))
263                ((or (eq *dd-type* 'vector)
264                     (and (consp *dd-type*) (eq (car *dd-type*) 'vector)))
265                 (let ((element-type (if (consp *dd-type*) (cadr *dd-type*) t)))
266                   `((defun ,constructor-name ,arglist
267                       (make-array ,(length values)
268                                   :element-type ',element-type
269                                   :initial-contents (list ,@values))))))
270                ((<= 1 (length values) 6)
271                 `((declaim (inline ,constructor-name))
272                   (defun ,constructor-name ,arglist
273                     (make-structure (truly-the symbol ',*dd-name*) ,@values))))
274                (t
275                 `((declaim (inline ,constructor-name))
276                   (defun ,constructor-name ,arglist
277                     (%make-structure (truly-the symbol ',*dd-name*) (list ,@values)))))))))))
278
279(defun default-constructor-name ()
280  (intern (concatenate 'string "MAKE-" (symbol-name *dd-name*))))
281
282(defun define-constructors ()
283  (if *dd-constructors*
284      (let ((results ()))
285        (dolist (constructor *dd-constructors*)
286          (when (car constructor)
287            (setf results (nconc results
288                                 (if (cadr constructor)
289                                     (define-boa-constructor constructor)
290                                     (define-keyword-constructor constructor))))))
291        results)
292      (define-keyword-constructor (cons (default-constructor-name) nil))))
293
294(defun name-index ()
295  (dolist (dsd *dd-slots*)
296    (let ((name (dsd-name dsd))
297          (initform (dsd-initform dsd)))
298      (when (and (null name)
299                 (equal initform (list 'quote *dd-name*)))
300        (return-from name-index (dsd-index dsd)))))
301  ;; We shouldn't get here.
302  nil)
303
304(defun define-predicate ()
305  (when (and *dd-predicate*
306             (or *dd-named* (null *dd-type*)))
307    (let ((pred (if (symbolp *dd-predicate*)
308                    *dd-predicate*
309                    (intern *dd-predicate*))))
310      (cond ((eq *dd-type* 'list)
311             (let ((index (name-index)))
312               `((defun ,pred (object)
313                   (and (consp object)
314                        (> (length object) ,index)
315                        (eq (nth ,index object) ',*dd-name*))))))
316            ((or (eq *dd-type* 'vector)
317                 (and (consp *dd-type*) (eq (car *dd-type*) 'vector)))
318             (let ((index (name-index)))
319               `((defun ,pred (object)
320                   (and (vectorp object)
321                        (> (length object) ,index)
322                        (eq (aref object ,index) ',*dd-name*))))))
323            (t
324             `((defun ,pred (object)
325                 (simple-typep object ',*dd-name*))))))))
326
327(defun define-reader (slot)
328  (let ((accessor-name (if *dd-conc-name*
329                           (intern (concatenate 'string
330                                                (symbol-name *dd-conc-name*)
331                                                (symbol-name (dsd-name slot))))
332                           (dsd-name slot)))
333        (index (dsd-index slot))
334        (type (dsd-type slot)))
335    (cond ((eq *dd-type* 'list)
336           `((declaim (ftype (function * ,type) ,accessor-name))
337             (defun ,accessor-name (instance) (elt instance ,index))))
338          ((or (eq *dd-type* 'vector)
339               (and (consp *dd-type*) (eq (car *dd-type*) 'vector)))
340           `((declaim (ftype (function * ,type) ,accessor-name))
341             (defun ,accessor-name (instance) (aref instance ,index))))
342          (t
343           `((declaim (ftype (function * ,type) ,accessor-name))
344             (defun ,accessor-name (instance) (structure-ref instance ,index))
345             (define-source-transform ,accessor-name (instance)
346               ,(if (eq type 't)
347                    ``(structure-ref ,instance ,,index)
348                    ``(the ,',type (structure-ref ,instance ,,index)))))))))
349
350(defun define-writer (slot)
351  (let ((accessor-name (if *dd-conc-name*
352                           (intern (concatenate 'string
353                                                (symbol-name *dd-conc-name*)
354                                                (symbol-name (dsd-name slot))))
355                           (dsd-name slot)))
356        (index (dsd-index slot)))
357    (cond ((eq *dd-type* 'list)
358           `((defun (setf ,accessor-name) (value instance)
359               (%set-elt instance ,index value))))
360          ((or (eq *dd-type* 'vector)
361               (and (consp *dd-type*) (eq (car *dd-type*) 'vector)))
362           `((defun (setf ,accessor-name) (value instance)
363               (aset instance ,index value))))
364          (t
365           `((defun (setf ,accessor-name) (value instance)
366               (structure-set instance ,index value))
367             (define-source-transform (setf ,accessor-name) (value instance)
368               `(structure-set ,instance ,,index ,value)))))))
369
370(defun define-access-functions ()
371  (let ((result ()))
372    (dolist (slot *dd-slots*)
373      (setf result (nconc result (define-reader slot)))
374      (unless (dsd-read-only slot)
375        (setf result (nconc result (define-writer slot)))))
376    result))
377
378(defun define-copier ()
379  (when *dd-copier*
380    (cond ((eq *dd-type* 'list)
381           `((setf (fdefinition ',*dd-copier*) #'copy-list)))
382          ((or (eq *dd-type* 'vector)
383               (and (consp *dd-type*) (eq (car *dd-type*) 'vector)))
384           `((setf (fdefinition ',*dd-copier*) #'copy-seq)))
385          (t
386           `((setf (fdefinition ',*dd-copier*) #'copy-structure))))))
387
388(defun define-print-function ()
389  (cond (*dd-print-function*
390         (if (cadr *dd-print-function*)
391             `((defmethod print-object ((instance ,*dd-name*) stream)
392                 (funcall (function ,(cadr *dd-print-function*))
393                          instance stream *current-print-level*)))
394             `((defmethod print-object ((instance ,*dd-name*) stream)
395                 (write-string (%write-to-string instance) stream)))))
396        (*dd-print-object*
397         (if (cadr *dd-print-object*)
398             `((defmethod print-object ((instance ,*dd-name*) stream)
399                 (funcall (function ,(cadr *dd-print-object*))
400                          instance stream)))
401             `((defmethod print-object ((instance ,*dd-name*) stream)
402                 (write-string (%write-to-string instance) stream)))))
403        (t
404         nil)))
405
406(defun parse-1-option (option)
407  (case (car option)
408    (:conc-name
409     (setf *dd-conc-name* (if (symbolp (cadr option))
410                              (cadr option)
411                              (make-symbol (string (cadr option))))))
412    (:constructor
413     (let* ((args (cdr option))
414            (numargs (length args)))
415       (case numargs
416         (0 ; Use default name.
417          (push (list (default-constructor-name) nil) *dd-constructors*))
418         (1
419          (push (list (car args) nil) *dd-constructors*))
420         (2
421          (push args *dd-constructors*)))))
422    (:copier
423     (when (eql (length option) 2)
424       (setf *dd-copier* (cadr option))))
425    (:include
426     (setf *dd-include* (cdr option)))
427    (:initial-offset
428     (setf *dd-initial-offset* (cadr option)))
429    (:predicate
430     (when (eql (length option) 2)
431       (setf *dd-predicate* (cadr option))))
432    (:print-function
433     (setf *dd-print-function* option))
434    (:print-object
435     (setf *dd-print-object* option))
436    (:type
437     (setf *dd-type* (cadr option)))))
438
439(defun parse-name-and-options (name-and-options)
440  (setf *dd-name* (the symbol (car name-and-options)))
441  (setf *dd-conc-name* (make-symbol (concatenate 'string (symbol-name *dd-name*) "-")))
442  (setf *dd-copier* (intern (concatenate 'string "COPY-" (symbol-name *dd-name*))))
443  (setf *dd-predicate* (concatenate 'string (symbol-name *dd-name*) "-P"))
444  (let ((options (cdr name-and-options)))
445    (dolist (option options)
446      (cond ((consp option)
447             (parse-1-option option))
448            ((eq option :named)
449             (setf *dd-named* t))
450            ((member option '(:constructor :copier :predicate :named :conc-name))
451             (parse-1-option (list option)))
452            (t
453             (error "Unrecognized DEFSTRUCT option: ~S." option))))))
454
455(defun compiler-defstruct (name &key
456                                conc-name
457                                default-constructor
458                                constructors
459                                copier
460                                include
461                                type
462                                named
463                                initial-offset
464                                predicate
465                                print-function
466                                print-object
467                                direct-slots
468                                slots)
469  (setf (get name 'structure-definition)
470        (make-defstruct-description :name name
471                                    :conc-name conc-name
472                                    :default-constructor default-constructor
473                                    :constructors constructors
474                                    :copier copier
475                                    :include include
476                                    :type type
477                                    :named named
478                                    :initial-offset initial-offset
479                                    :predicate predicate
480                                    :print-function print-function
481                                    :print-object print-object
482                                    :direct-slots direct-slots
483                                    :slots slots))
484  (when (or (null type) named)
485    (make-structure-class name direct-slots slots (car include)))
486  (when default-constructor
487    (proclaim `(ftype (function * t) ,default-constructor))))
488
489(defmacro defstruct (name-and-options &rest slots)
490  (let ((*dd-name* nil)
491        (*dd-conc-name* nil)
492        (*dd-default-constructor* nil)
493        (*dd-constructors* nil)
494        (*dd-copier* nil)
495        (*dd-include* nil)
496        (*dd-type* nil)
497        (*dd-named* nil)
498        (*dd-initial-offset* nil)
499        (*dd-predicate* nil)
500        (*dd-print-function* nil)
501        (*dd-print-object* nil)
502        (*dd-direct-slots* ())
503        (*dd-slots* ()))
504    (parse-name-and-options (if (atom name-and-options)
505                                (list name-and-options)
506                                name-and-options))
507    (check-declaration-type *dd-name*)
508    (if *dd-constructors*
509        (dolist (constructor *dd-constructors*)
510          (unless (cadr constructor)
511            (setf *dd-default-constructor* (car constructor))
512            (return)))
513        (setf *dd-default-constructor* (default-constructor-name)))
514    (when (stringp (car slots))
515      (%set-documentation *dd-name* 'structure (pop slots)))
516    (dolist (slot slots)
517      (let* ((name (if (atom slot) slot (car slot)))
518             (reader (if *dd-conc-name*
519                         (intern (concatenate 'string
520                                              (symbol-name *dd-conc-name*)
521                                              (symbol-name name)))
522                         name))
523             (initform (if (atom slot) nil (cadr slot)))
524             (dsd (apply #'make-defstruct-slot-description
525                         :name name
526                         :reader reader
527                         :initform initform
528                         (if (atom slot) nil (cddr slot)))))
529        (push dsd *dd-direct-slots*)))
530    (setf *dd-direct-slots* (nreverse *dd-direct-slots*))
531    (let ((index 0))
532      (when *dd-include*
533        (let ((dd (get (car *dd-include*) 'structure-definition)))
534          (unless dd
535            (error 'simple-error
536                   :format-control "Class ~S is undefined."
537                   :format-arguments (list (car *dd-include*))))
538          (dolist (dsd (dd-slots dd))
539            ;; MUST COPY SLOT DESCRIPTION!
540            (setf dsd (copy-seq dsd))
541            (setf (dsd-index dsd) index)
542            (push dsd *dd-slots*)
543            (incf index)))
544        (when (cdr *dd-include*)
545          (dolist (slot (cdr *dd-include*))
546            (let* ((name (if (atom slot) slot (car slot)))
547                   (initform (if (atom slot) nil (cadr slot)))
548                   (dsd (find-dsd name)))
549              (when dsd
550                (setf (dsd-initform dsd) initform))))))
551      (when *dd-initial-offset*
552        (dotimes (i *dd-initial-offset*)
553          (push (make-defstruct-slot-description :name nil
554                                                 :index index
555                                                 :reader nil
556                                                 :initform nil
557                                                 :type t
558                                                 :read-only t)
559                *dd-slots*)
560          (incf index)))
561      (when *dd-named*
562        (push (make-defstruct-slot-description :name nil
563                                               :index index
564                                               :reader nil
565                                               :initform (list 'quote *dd-name*)
566                                               :type t
567                                               :read-only t)
568              *dd-slots*)
569        (incf index))
570      (dolist (dsd *dd-direct-slots*)
571        (setf (dsd-index dsd) index)
572        (push dsd *dd-slots*)
573        (incf index)))
574    (setf *dd-slots* (nreverse *dd-slots*))
575    `(progn
576       (eval-when (:compile-toplevel :load-toplevel :execute)
577         (compiler-defstruct ',*dd-name*
578                             :conc-name ',*dd-conc-name*
579                             :default-constructor ',*dd-default-constructor*
580                             ,@(if *dd-constructors* `(:constructors ',*dd-constructors*))
581                             :copier ',*dd-copier*
582                             ,@(if *dd-include* `(:include ',*dd-include*))
583                             ,@(if *dd-type* `(:type ',*dd-type*))
584                             ,@(if *dd-named* `(:named ,*dd-named*))
585                             ,@(if *dd-initial-offset* `(:initial-offset ,*dd-initial-offset*))
586                             :predicate ',*dd-predicate*
587                             ,@(if *dd-print-function* `(:print-function ',*dd-print-function*))
588                             ,@(if *dd-print-object* `(:print-object ',*dd-print-object*))
589                             :direct-slots ',*dd-direct-slots*
590                             :slots ',*dd-slots*))
591       ,@(define-constructors)
592       ,@(define-predicate)
593       ,@(define-access-functions)
594       ,@(define-copier)
595       ,@(define-print-function)
596       ',*dd-name*)))
597
598(defun defstruct-default-constructor (arg)
599  (let ((type (cond ((symbolp arg)
600                     arg)
601                    ((classp arg)
602                     (class-name arg))
603                    (t
604                     (type-of arg)))))
605    (when type
606      (let ((dd (get type 'structure-definition)))
607        (and dd (dd-default-constructor dd))))))
Note: See TracBrowser for help on using the repository browser.