-;;; Now this function is used to grab other functionality as well. This
-;;; includes:
-;;; - Preventing the grouping of top-level forms. For example, a
-;;; DEFCLASS followed by a DEFMETHOD may not want to be grouped
-;;; into the same top-level form.
-;;; - Telling the programming environment what the pretty version
-;;; of the name of this form is. This is used by WARN.
-;;;
-;;; FIXME: It's not clear that this adds value any more. Couldn't
-;;; we just use EVAL-WHEN?
-(defun make-top-level-form (name times form)
- (flet ((definition-name ()
- (if (and (listp name)
- (memq (car name)
- '(defmethod defclass class
- method method-combination)))
- (format nil "~A~{ ~S~}"
- (capitalize-words (car name) ()) (cdr name))
- (format nil "~S" name))))
- ;; FIXME: It appears that we're just consing up a string and then
- ;; throwing it away?!
- (definition-name)
- (if (or (member 'compile times)
- (member ':compile-toplevel times))
- `(eval-when ,times ,form)
- form)))
-
-(defun make-progn (&rest forms)
- (let ((progn-form nil))
- (labels ((collect-forms (forms)
- (unless (null forms)
- (collect-forms (cdr forms))
- (if (and (listp (car forms))
- (eq (caar forms) 'progn))
- (collect-forms (cdar forms))
- (push (car forms) progn-form)))))
- (collect-forms forms)
- (cons 'progn progn-form))))
-\f
-;;; Like the DEFMETHOD macro, the expansion of the DEFCLASS macro is fixed.
-;;; DEFCLASS always expands into a call to LOAD-DEFCLASS. Until the meta-
-;;; braid is set up, LOAD-DEFCLASS has a special definition which simply
-;;; collects all class definitions up, when the metabraid is initialized it
-;;; is done from those class definitions.
-;;;
-;;; After the metabraid has been setup, and the protocol for defining classes
-;;; has been defined, the real definition of LOAD-DEFCLASS is installed by the
-;;; file defclass.lisp
-(defmacro defclass (name direct-superclasses direct-slots &rest options)
- (declare (indentation 2 4 3 1))
- (expand-defclass name direct-superclasses direct-slots options))
-
-(defun expand-defclass (name supers slots options)
- (declare (special *defclass-times* *boot-state* *the-class-structure-class*))
- (setq supers (copy-tree supers)
- slots (copy-tree slots)
- options (copy-tree options))
- (let ((metaclass 'standard-class))
- (dolist (option options)
- (if (not (listp option))
- (error "~S is not a legal defclass option." option)
- (when (eq (car option) ':metaclass)
- (unless (legal-class-name-p (cadr option))
- (error "The value of the :metaclass option (~S) is not a~%~
- legal class name."
- (cadr option)))
- (setq metaclass
- (case (cadr option)
- (cl:standard-class 'standard-class)
- (cl:structure-class 'structure-class)
- (t (cadr option))))
- (setf options (remove option options))
- (return t))))
-
- (let ((*initfunctions* ())
- (*accessors* ()) ;Truly a crock, but we got
- (*readers* ()) ;to have it to live nicely.
- (*writers* ()))
- (declare (special *initfunctions* *accessors* *readers* *writers*))
- (let ((canonical-slots
- (mapcar #'(lambda (spec)
- (canonicalize-slot-specification name spec))
- slots))
- (other-initargs
- (mapcar #'(lambda (option)
- (canonicalize-defclass-option name option))
- options))
- (defstruct-p (and (eq *boot-state* 'complete)
- (let ((mclass (find-class metaclass nil)))
- (and mclass
- (*subtypep mclass
- *the-class-structure-class*))))))
- (do-standard-defsetfs-for-defclass *accessors*)
- (let ((defclass-form
- (make-top-level-form `(defclass ,name)
- (if defstruct-p '(:load-toplevel :execute) *defclass-times*)
- `(progn
- ,@(mapcar #'(lambda (x)
- `(declaim (ftype (function (t) t) ,x)))
- *readers*)
- ,@(mapcar #'(lambda (x)
- #-setf (when (consp x)
- (setq x (get-setf-function-name (cadr x))))
- `(declaim (ftype (function (t t) t) ,x)))
- *writers*)
- (let ,(mapcar #'cdr *initfunctions*)
- (load-defclass ',name
- ',metaclass
- ',supers
- (list ,@canonical-slots)
- (list ,@(apply #'append
- (when defstruct-p
- '(:from-defclass-p t))
- other-initargs))
- ',*accessors*))))))
- (if defstruct-p
- (progn
- (eval defclass-form) ; define the class now, so that
- `(progn ; the defstruct can be compiled.
- ,(class-defstruct-form (find-class name))
- ,defclass-form))
- (progn
- (when (and (eq *boot-state* 'complete)
- (not (member 'compile *defclass-times*)))
- (inform-type-system-about-std-class name))
- defclass-form)))))))
+;;; After the metabraid has been setup, and the protocol for defining
+;;; classes has been defined, the real definition of LOAD-DEFCLASS is
+;;; installed by the file std-class.lisp
+(defmacro defclass (&environment env name direct-superclasses direct-slots &rest options)
+ (let (*initfunctions-for-this-defclass*
+ *readers-for-this-defclass* ;Truly a crock, but we got
+ *writers-for-this-defclass* ;to have it to live nicely.
+ *slot-names-for-this-defclass*)
+ ;; FIXME: It would be nice to collect all errors from the
+ ;; expansion of a defclass and signal them in a single go.
+ (multiple-value-bind (metaclass canonical-options)
+ (canonize-defclass-options name options)
+ (let ((canonical-slots (canonize-defclass-slots name direct-slots env))
+ ;; DEFSTRUCT-P should be true if the class is defined
+ ;; with a metaclass STRUCTURE-CLASS, so that a DEFSTRUCT
+ ;; is compiled for the class.
+ (defstruct-p (and (eq *boot-state* 'complete)
+ (let ((mclass (find-class metaclass nil)))
+ (and mclass
+ (*subtypep
+ mclass
+ *the-class-structure-class*))))))
+ (let* ((defclass-form
+ `(let ,(mapcar #'cdr *initfunctions-for-this-defclass*)
+ (load-defclass ',name
+ ',metaclass
+ ',direct-superclasses
+ (list ,@canonical-slots)
+ (list ,@(apply #'append
+ (when defstruct-p
+ '(:from-defclass-p t))
+ canonical-options))
+ ',*readers-for-this-defclass*
+ ',*writers-for-this-defclass*
+ ',*slot-names-for-this-defclass*
+ (sb-c:source-location)))))
+ (if defstruct-p
+ (progn
+ ;; FIXME: (YUK!) Why do we do this? Because in order
+ ;; to make the defstruct form, we need to know what
+ ;; the accessors for the slots are, so we need already
+ ;; to have hooked into the CLOS machinery.
+ ;;
+ ;; There may be a better way to do this: it would
+ ;; involve knowing enough about PCL to ask "what will
+ ;; my slot names and accessors be"; failing this, we
+ ;; currently just evaluate the whole kaboodle, and
+ ;; then use CLASS-DIRECT-SLOTS. -- CSR, 2002-06-07
+ (eval defclass-form)
+ (let* ((include (or (and direct-superclasses
+ (fix-super (car direct-superclasses)))
+ (and (not (eq name 'structure-object))
+ *the-class-structure-object*)))
+ (defstruct-form (make-structure-class-defstruct-form
+ name (class-direct-slots (find-class name))
+ include)))
+ `(progn
+ (eval-when (:compile-toplevel :load-toplevel :execute)
+ ,defstruct-form) ; really compile the defstruct-form
+ (eval-when (:compile-toplevel :load-toplevel :execute)
+ ,defclass-form))))
+ `(progn
+ ;; By telling the type system at compile time about
+ ;; the existence of a class named NAME, we can avoid
+ ;; various bogus warnings about "type isn't defined yet"
+ ;; for code elsewhere in the same file which uses
+ ;; the name of the type.
+ ;;
+ ;; We only need to do this at compile time, because
+ ;; at load and execute time we write the actual
+ ;; full-blown class, so the "a class of this name is
+ ;; coming" note we write here would be irrelevant.
+ (eval-when (:compile-toplevel)
+ (%compiler-defclass ',name
+ ',*readers-for-this-defclass*
+ ',*writers-for-this-defclass*
+ ',*slot-names-for-this-defclass*))
+ (eval-when (:load-toplevel :execute)
+ ,defclass-form))))))))
+
+(defun canonize-defclass-options (class-name options)
+ (maplist (lambda (sublist)
+ (let ((option-name (first (pop sublist))))
+ (when (member option-name sublist :key #'first)
+ (error "Multiple ~S options in DEFCLASS ~S."
+ option-name class-name))))
+ options)
+ (let (metaclass
+ default-initargs
+ documentation
+ canonized-options)
+ (dolist (option options)
+ (unless (listp option)
+ (error "~S is not a legal defclass option." option))
+ (case (first option)
+ (:metaclass
+ (let ((maybe-metaclass (second option)))
+ (unless (and maybe-metaclass (legal-class-name-p maybe-metaclass))
+ (error "~@<The value of the :metaclass option (~S) ~
+ is not a legal class name.~:@>"
+ maybe-metaclass))
+ (setf metaclass maybe-metaclass)))
+ (:default-initargs
+ (let (initargs arg-names)
+ (doplist (key val) (cdr option)
+ (when (member key arg-names)
+ (error 'simple-program-error
+ :format-control "~@<Duplicate initialization argument ~
+ name ~S in :DEFAULT-INITARGS of ~
+ DEFCLASS ~S.~:>"
+ :format-arguments (list key class-name)))
+ (push key arg-names)
+ (push ``(,',key ,',val ,,(make-initfunction val)) initargs))
+ (setf default-initargs t)
+ (push `(:direct-default-initargs (list ,@(nreverse initargs)))
+ canonized-options)))
+ (:documentation
+ (unless (stringp (second option))
+ (error "~S is not a legal :documentation value" (second option)))
+ (setf documentation t)
+ (push `(:documentation ,(second option)) canonized-options))
+ (otherwise
+ (push `(',(car option) ',(cdr option)) canonized-options))))
+ (values (or metaclass 'standard-class) (nreverse canonized-options))))
+
+(defun canonize-defclass-slots (class-name slots env)
+ (let (canonized-specs)
+ (dolist (spec slots)
+ (when (atom spec)
+ (setf spec (list spec)))
+ (when (and (cdr spec) (null (cddr spec)))
+ (error 'simple-program-error
+ :format-control "~@<in DEFCLASS ~S, the slot specification ~S ~
+ is invalid; the probable intended meaning may ~
+ be achieved by specifiying ~S instead.~:>"
+ :format-arguments (list class-name spec
+ `(,(car spec) :initform ,(cadr spec)))))
+ (let* ((name (car spec))
+ (plist (cdr spec))
+ (readers ())
+ (writers ())
+ (initargs ())
+ (others ())
+ (unsupplied (list nil))
+ (initform unsupplied))
+ (check-slot-name-for-defclass name class-name env)
+ (push name *slot-names-for-this-defclass*)
+ (flet ((note-reader (x)
+ (unless (symbolp x)
+ (error 'simple-program-error
+ :format-control "Slot reader name ~S for slot ~S in ~
+ DEFCLASS ~S is not a symbol."
+ :format-arguments (list x name class-name)))
+ (push x readers)
+ (push x *readers-for-this-defclass*))
+ (note-writer (x)
+ (push x writers)
+ (push x *writers-for-this-defclass*)))
+ (doplist (key val) plist
+ (case key
+ (:accessor (note-reader val) (note-writer `(setf ,val)))
+ (:reader (note-reader val))
+ (:writer (note-writer val))
+ (:initarg
+ (unless (symbolp val)
+ (error 'simple-program-error
+ :format-control "Slot initarg name ~S for slot ~S in ~
+ DEFCLASS ~S is not a symbol."
+ :format-arguments (list val name class-name)))
+ (push val initargs))
+ (otherwise
+ (when (member key '(:initform :allocation :type :documentation))
+ (when (eq key :initform)
+ (setf initform val))
+ (when (get-properties others (list key))
+ (error 'simple-program-error
+ :format-control "Duplicate slot option ~S for slot ~
+ ~S in DEFCLASS ~S."
+ :format-arguments (list key name class-name))))
+ ;; For non-standard options multiple entries go in a list
+ (push val (getf others key))))))
+ ;; Unwrap singleton lists (AMOP 5.4.2)
+ (do ((head others (cddr head)))
+ ((null head))
+ (unless (cdr (second head))
+ (setf (second head) (car (second head)))))
+ (let ((canon `(:name ',name :readers ',readers :writers ',writers
+ :initargs ',initargs ',others)))
+ (push (if (eq initform unsupplied)
+ `(list* ,@canon)
+ `(list* :initfunction ,(make-initfunction initform)
+ ,@canon))
+ canonized-specs))))
+ (nreverse canonized-specs)))
+
+
+(defun check-slot-name-for-defclass (name class-name env)
+ (flet ((slot-name-illegal (reason)
+ (error 'simple-program-error
+ :format-control
+ (format nil "~~@<In DEFCLASS ~~S, the slot name ~~S ~
+ is ~A.~~@:>" reason)
+ :format-arguments (list class-name name))))
+ (cond ((not (symbolp name))
+ (slot-name-illegal "not a symbol"))
+ ((keywordp name)
+ (slot-name-illegal "a keyword"))
+ ((constantp name env)
+ (slot-name-illegal "a constant"))
+ ((member name *slot-names-for-this-defclass*)
+ (error 'simple-program-error
+ :format-control "Multiple slots named ~S in DEFCLASS ~S."
+ :format-arguments (list name class-name))))))