- (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.
- ,(when (and
- ;; But it's not so important to get rid of
- ;; "not defined yet" warnings during
- ;; bootstrapping, and machinery like
- ;; INFORM-TYPE-SYSTEM-ABOUT-STD-CLASS
- ;; mightn't be defined yet. So punt then.
- (eq *boot-state* 'complete)
- ;; And although we know enough about
- ;; STANDARD-CLASS, and ANSI imposes enough
- ;; restrictions on the user overloading its
- ;; methods, that (1) we can shortcut the
- ;; method dispatch and do an ordinary
- ;; function call, and (2) be sure we're getting
- ;; it right even when we do it at compile
- ;; time; we don't in general know how to do
- ;; that for other classes. So punt then too.
- (eq metaclass 'standard-class))
- `(eval-when (:compile-toplevel)
- ;; we only need :COMPILE-TOPLEVEL here, because this
- ;; should happen in the compile-time environment
- ;; only.
- ;; Later, INFORM-TYPE-SYSTEM-ABOUT-STD-CLASS is
- ;; called by way of LOAD-DEFCLASS (calling
- ;; ENSURE-CLASS-USING-CLASS) to establish the 'real'
- ;; type predicate.
- (inform-type-system-about-std-class ',name)))
- ,defclass-form))))))))
+ (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 :test #'eq)
+ (error 'simple-program-error
+ :format-control "Multiple ~S options in DEFCLASS ~S."
+ :format-arguments (list 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 'simple-program-error
+ :format-control "~@<The value of the :metaclass option (~S) ~
+ is not a legal class name.~:@>"
+ :format-arguments (list maybe-metaclass)))
+ (setf metaclass maybe-metaclass)))
+ (:default-initargs
+ (let (initargs arg-names)
+ (doplist (key val) (cdr option)
+ (when (member key arg-names :test #'eq)
+ (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))
+ (type t)
+ (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 (eq key :type)
+ (setf type 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* ((type-check-function
+ (if (eq type t)
+ nil
+ `('type-check-function (lambda (value)
+ (declare (type ,type value)
+ (optimize (sb-c:store-coverage-data 0)))
+ value))))
+ (canon `(:name ',name :readers ',readers :writers ',writers
+ :initargs ',initargs
+ ,@type-check-function
+ ',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* :test #'eq)
+ (error 'simple-program-error
+ :format-control "Multiple slots named ~S in DEFCLASS ~S."
+ :format-arguments (list name class-name))))))