| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: CLOS -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; Copyright (c) 1992, Giuseppe Attardi. |
| 6 |
;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll. |
| 7 |
;;;; |
| 8 |
;;;; See file 'LICENSE' for the copyright details. |
| 9 |
|
| 10 |
(in-package "CLOS") |
| 11 |
|
| 12 |
;;; ---------------------------------------------------------------------- |
| 13 |
;;; DEFCLASS |
| 14 |
|
| 15 |
(defun parse-default-initargs (default-initargs) |
| 16 |
(declare (si::c-local)) |
| 17 |
(do* ((output-list nil) |
| 18 |
(scan default-initargs (cddr scan)) |
| 19 |
(already-supplied '())) |
| 20 |
((endp scan) `(list ,@(nreverse output-list))) |
| 21 |
(when (endp (rest scan)) |
| 22 |
(si::simple-program-error "Wrong number of elements in :DEFAULT-INITARGS option.")) |
| 23 |
(let ((slot-name (first scan)) |
| 24 |
(initform (second scan))) |
| 25 |
(if (member slot-name already-supplied) |
| 26 |
(si::simple-program-error "~S is duplicated in :DEFAULT-INITARGS form ~S" |
| 27 |
slot-name default-initargs) |
| 28 |
(push slot-name already-supplied)) |
| 29 |
(push `(list ',slot-name ',initform ,(make-function-initform initform)) |
| 30 |
output-list)))) |
| 31 |
|
| 32 |
(defmacro defclass (&whole form &rest args) |
| 33 |
(unless (>= (length args) 3) |
| 34 |
(si::simple-program-error "Illegal defclass form: the class name, the superclasses and the slots should always be provided")) |
| 35 |
(let* ((name (pop args)) |
| 36 |
(superclasses (pop args)) |
| 37 |
(slots (pop args)) |
| 38 |
(options args)) |
| 39 |
(unless (and (listp superclasses) (listp slots)) |
| 40 |
(si::simple-program-error "Illegal defclass form: superclasses and slots should be lists")) |
| 41 |
(unless (and (symbolp name) (every #'symbolp superclasses)) |
| 42 |
(si::simple-program-error "Illegal defclass form: superclasses and class name are not valid")) |
| 43 |
;; According to the X3J13 cleanup issue |
| 44 |
;; CLOS-MACRO-COMPILATION:MINIMAL, at compile time we only need |
| 45 |
;; to ensure that |
| 46 |
;; * The class name may appear in subsequent type declarations. |
| 47 |
;; * The class name can be used as a specializer in subsequent |
| 48 |
;; DEFMETHOD forms. |
| 49 |
;; Doing more at compile time leads to problems with the mop as |
| 50 |
;; for example validate-superclass methods might not be installed |
| 51 |
;; at compile time. |
| 52 |
(si:create-type-name name) |
| 53 |
(ext:register-with-pde |
| 54 |
form |
| 55 |
`(load-defclass ',name ',superclasses |
| 56 |
,(compress-slot-forms slots) |
| 57 |
,(process-class-options options))))) |
| 58 |
|
| 59 |
(defun compress-slot-forms (slot-definitions) |
| 60 |
(declare (si::c-local)) |
| 61 |
;; Here we compose the final form. The slots list, and the default initargs |
| 62 |
;; may contain object that need to be evaluated. Hence, it cannot be always |
| 63 |
;; quoted. |
| 64 |
(let ((const '()) |
| 65 |
(output '()) |
| 66 |
(non-const nil)) |
| 67 |
(dolist (slotd (parse-slots slot-definitions)) |
| 68 |
(let* ((initfun (getf slotd :initfunction nil)) |
| 69 |
(copy (copy-list slotd))) |
| 70 |
(remf copy :initfunction) |
| 71 |
(cond ((atom initfun) |
| 72 |
(push copy const) |
| 73 |
(push (ext:maybe-quote copy) output)) |
| 74 |
((eq (first initfun) 'constantly) |
| 75 |
(push copy const) |
| 76 |
(push (ext:maybe-quote copy) output)) |
| 77 |
(t |
| 78 |
(push `(list* :initfunction ,initfun ,(ext:maybe-quote copy)) |
| 79 |
output) |
| 80 |
(setf non-const t))))) |
| 81 |
(if non-const |
| 82 |
`(list ,@(nreverse output)) |
| 83 |
(ext:maybe-quote (nreverse const))))) |
| 84 |
|
| 85 |
(defun uncompress-slot-forms (slot-definitions) |
| 86 |
(loop for slotd in slot-definitions |
| 87 |
for initform = (getf slotd :initform slotd) |
| 88 |
collect (if (eq initform slotd) |
| 89 |
slotd |
| 90 |
(if (getf slotd :initfunction) |
| 91 |
slotd |
| 92 |
(list* :initfunction (constantly (eval initform)) |
| 93 |
slotd))))) |
| 94 |
|
| 95 |
(defun process-class-options (class-args) |
| 96 |
(let ((options '()) |
| 97 |
(processed-options '())) |
| 98 |
(dolist (option class-args) |
| 99 |
(let ((option-name (first option)) |
| 100 |
option-value) |
| 101 |
(if (member option-name processed-options) |
| 102 |
(si:simple-program-error |
| 103 |
"Option ~s for DEFCLASS specified more than once" |
| 104 |
option-name) |
| 105 |
(push option-name processed-options)) |
| 106 |
(setq option-value |
| 107 |
(case option-name |
| 108 |
((:metaclass :documentation) |
| 109 |
(ext:maybe-quote (second option))) |
| 110 |
(:default-initargs |
| 111 |
(setf option-name :direct-default-initargs) |
| 112 |
(parse-default-initargs (rest option))) |
| 113 |
(otherwise |
| 114 |
(ext:maybe-quote (rest option)))) |
| 115 |
options (list* (ext:maybe-quote option-name) |
| 116 |
option-value options)))) |
| 117 |
(and options `(list ,@options)))) |
| 118 |
|
| 119 |
(defun load-defclass (name superclasses slot-definitions options) |
| 120 |
(apply #'ensure-class name :direct-superclasses superclasses |
| 121 |
:direct-slots (uncompress-slot-forms slot-definitions) |
| 122 |
options)) |
| 123 |
|
| 124 |
;;; ---------------------------------------------------------------------- |
| 125 |
;;; ENSURE-CLASS |
| 126 |
;;; |
| 127 |
(defun ensure-class (name &rest initargs) |
| 128 |
(let* ((old-class nil) |
| 129 |
new-class) |
| 130 |
;; Only classes which have a PROPER name are redefined. If a class |
| 131 |
;; with the same name is register, but the name of the class does not |
| 132 |
;; correspond to the registered name, a new class is returned. |
| 133 |
;; [Hyperspec 7.7 for DEFCLASS] |
| 134 |
(when name |
| 135 |
(when (and (setf old-class (find-class name nil)) |
| 136 |
(not (eq (class-name old-class) name))) |
| 137 |
(setf old-class nil))) |
| 138 |
(setf new-class (apply #'ensure-class-using-class old-class name initargs)) |
| 139 |
new-class)) |
| 140 |
|
| 141 |
#+(or) ;#+cross |
| 142 |
(eval-when (compile) |
| 143 |
(defun ensure-class (name &rest initargs) |
| 144 |
(warn "Ignoring definition for class ~S" name))) |
| 145 |
|