+;;;; instances with ALTERNATE-METACLASS
+;;;;
+;;;; The CMU CL support for structures with ALTERNATE-METACLASS was a
+;;;; fairly general extension embedded in the main DEFSTRUCT code, and
+;;;; the result was an fairly impressive mess as ALTERNATE-METACLASS
+;;;; extension mixed with ANSI CL generality (e.g. :TYPE and :INCLUDE)
+;;;; and CMU CL implementation hairiness (esp. raw slots). This SBCL
+;;;; version is much less ambitious, noticing that ALTERNATE-METACLASS
+;;;; is only used to implement CONDITION, STANDARD-INSTANCE, and
+;;;; GENERIC-FUNCTION, and defining a simple specialized
+;;;; separate-from-DEFSTRUCT macro to provide only enough
+;;;; functionality to support those.
+;;;;
+;;;; KLUDGE: The defining macro here is so specialized that it's ugly
+;;;; in its own way. It also violates once-and-only-once by knowing
+;;;; much about structures and layouts that is already known by the
+;;;; main DEFSTRUCT macro. Hopefully it will go away presently
+;;;; (perhaps when CL:CLASS and SB-PCL:CLASS meet) as per FIXME below.
+;;;; -- WHN 2001-10-28
+;;;;
+;;;; FIXME: There seems to be no good reason to shoehorn CONDITION,
+;;;; STANDARD-INSTANCE, and GENERIC-FUNCTION into mutated structures
+;;;; instead of just implementing them as primitive objects. (This
+;;;; reduced-functionality macro seems pretty close to the
+;;;; functionality of DEFINE-PRIMITIVE-OBJECT..)
+
+(defun make-dd-with-alternate-metaclass (&key (class-name (missing-arg))
+ (superclass-name (missing-arg))
+ (metaclass-name (missing-arg))
+ (dd-type (missing-arg))
+ metaclass-constructor
+ slot-names)
+ (let* ((dd (make-defstruct-description class-name))
+ (conc-name (concatenate 'string (symbol-name class-name) "-"))
+ (dd-slots (let ((reversed-result nil)
+ ;; The index starts at 1 for ordinary
+ ;; named slots because slot 0 is
+ ;; magical, used for LAYOUT in
+ ;; CONDITIONs or for something (?) in
+ ;; funcallable instances.
+ (index 1))
+ (dolist (slot-name slot-names)
+ (push (make-defstruct-slot-description
+ :name slot-name
+ :index index
+ :accessor-name (symbolicate conc-name slot-name))
+ reversed-result)
+ (incf index))
+ (nreverse reversed-result))))
+ (setf (dd-alternate-metaclass dd) (list superclass-name
+ metaclass-name
+ metaclass-constructor)
+ (dd-slots dd) dd-slots
+ (dd-length dd) (1+ (length slot-names))
+ (dd-type dd) dd-type)
+ dd))
+
+(sb!xc:defmacro !defstruct-with-alternate-metaclass
+ (class-name &key
+ (slot-names (missing-arg))
+ (boa-constructor (missing-arg))
+ (superclass-name (missing-arg))
+ (metaclass-name (missing-arg))
+ (metaclass-constructor (missing-arg))
+ (dd-type (missing-arg))
+ predicate
+ (runtime-type-checks-p t))
+
+ (declare (type (and list (not null)) slot-names))
+ (declare (type (and symbol (not null))
+ boa-constructor
+ superclass-name
+ metaclass-name
+ metaclass-constructor))
+ (declare (type symbol predicate))
+ (declare (type (member structure funcallable-structure) dd-type))
+
+ (let* ((dd (make-dd-with-alternate-metaclass
+ :class-name class-name
+ :slot-names slot-names
+ :superclass-name superclass-name
+ :metaclass-name metaclass-name
+ :metaclass-constructor metaclass-constructor
+ :dd-type dd-type))
+ (dd-slots (dd-slots dd))
+ (dd-length (1+ (length slot-names)))
+ (object-gensym (gensym "OBJECT"))
+ (new-value-gensym (gensym "NEW-VALUE-"))
+ (delayed-layout-form `(%delayed-get-compiler-layout ,class-name)))
+ (multiple-value-bind (raw-maker-form raw-reffer-operator)
+ (ecase dd-type
+ (structure
+ (values `(let ((,object-gensym (%make-instance ,dd-length)))
+ (setf (%instance-layout ,object-gensym)
+ ,delayed-layout-form)
+ ,object-gensym)
+ '%instance-ref))
+ (funcallable-structure
+ (values `(%make-funcallable-instance ,dd-length
+ ,delayed-layout-form)
+ '%funcallable-instance-info)))
+ `(progn
+
+ (eval-when (:compile-toplevel :load-toplevel :execute)
+ (%compiler-set-up-layout ',dd))
+
+ ;; slot readers and writers
+ (declaim (inline ,@(mapcar #'dsd-accessor-name dd-slots)))
+ ,@(mapcar (lambda (dsd)
+ `(defun ,(dsd-accessor-name dsd) (,object-gensym)
+ ,@(when runtime-type-checks-p
+ `((declare (type ,class-name ,object-gensym))))
+ (,raw-reffer-operator ,object-gensym
+ ,(dsd-index dsd))))
+ dd-slots)
+ (declaim (inline ,@(mapcar (lambda (dsd)
+ `(setf ,(dsd-accessor-name dsd)))
+ dd-slots)))
+ ,@(mapcar (lambda (dsd)
+ `(defun (setf ,(dsd-accessor-name dsd)) (,new-value-gensym
+ ,object-gensym)
+ ,@(when runtime-type-checks-p
+ `((declare (type ,class-name ,object-gensym))))
+ (setf (,raw-reffer-operator ,object-gensym
+ ,(dsd-index dsd))
+ ,new-value-gensym)))
+ dd-slots)
+
+ ;; constructor
+ (defun ,boa-constructor ,slot-names
+ (let ((,object-gensym ,raw-maker-form))
+ ,@(mapcar (lambda (slot-name)
+ (let ((dsd (find (symbol-name slot-name) dd-slots
+ :key (lambda (x)
+ (symbol-name (dsd-name x)))
+ :test #'string=)))
+ ;; KLUDGE: bug 117 bogowarning. Neither
+ ;; DECLAREing the type nor TRULY-THE cut
+ ;; the mustard -- it still gives warnings.
+ (enforce-type dsd defstruct-slot-description)
+ `(setf (,(dsd-accessor-name dsd) ,object-gensym)
+ ,slot-name)))
+ slot-names)
+ ,object-gensym))
+
+ ;; predicate
+ ,@(when predicate
+ ;; Just delegate to the compiler's type optimization
+ ;; code, which knows how to generate inline type tests
+ ;; for the whole CMU CL INSTANCE menagerie.
+ `(defun ,predicate (,object-gensym)
+ (typep ,object-gensym ',class-name)))))))
+\f