X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fpcl%2Fdefclass.lisp;h=845edaf2ea3bdca835ae1762650c972b054f5970;hb=e8b69b1dd5564a4237b1bdc1060820c3b820cde2;hp=99fcb3dd1440354f751507366e63dfcb648ca589;hpb=2abf77f6c4c559a3e5b7fc351a4743305381feb6;p=sbcl.git diff --git a/src/pcl/defclass.lisp b/src/pcl/defclass.lisp index 99fcb3d..845edaf 100644 --- a/src/pcl/defclass.lisp +++ b/src/pcl/defclass.lisp @@ -23,38 +23,6 @@ (in-package "SB-PCL") -;;; MAKE-TOP-LEVEL-FORM is used by all PCL macros that appear `at top-level'. -;;; -;;; The original motiviation for this function was to deal with the -;;; bug in the Genera compiler that prevents lambda expressions in -;;; top-level forms other than DEFUN from being compiled. -;;; -;;; 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)) @@ -76,19 +44,14 @@ ;;; ;;; 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)) +;;; installed by the file std-class.lisp +(defmacro defclass (name %direct-superclasses %direct-slots &rest %options) + (let ((supers (copy-tree %direct-superclasses)) + (slots (copy-tree %direct-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)) @@ -96,77 +59,106 @@ legal class name." (cadr option))) (setq metaclass - (case (cadr option) - (cl:standard-class 'standard-class) - (cl:structure-class 'structure-class) - (t (cadr option)))) + (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))))))) + (let ((*initfunctions* ()) + (*readers* ()) ;Truly a crock, but we got + (*writers* ())) ;to have it to live nicely. + (declare (special *initfunctions* *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 should be true, if the class is defined with a + ;; metaclass STRUCTURE-CLASS, such 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 + `(progn + ,@(mapcar (lambda (x) + `(declaim (ftype (function (t) t) ,x))) + *readers*) + ,@(mapcar (lambda (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))))))) + (if defstruct-p + (let* ((include (or (and supers + (fix-super (car supers))) + (and (not (eq name 'structure-object)) + *the-class-structure-object*))) + (defstruct-form (make-structure-class-defstruct-form + name slots 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". + ,(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)))))))) (defun make-initfunction (initform) (declare (special *initfunctions*)) - (cond ((or (eq initform 't) + (cond ((or (eq initform t) (equal initform ''t)) - '(function true)) - ((or (eq initform 'nil) + '(function constantly-t)) + ((or (eq initform nil) (equal initform ''nil)) - '(function false)) - ((or (eql initform '0) + '(function constantly-nil)) + ((or (eql initform 0) (equal initform ''0)) - '(function zero)) + '(function constantly-0)) (t (let ((entry (assoc initform *initfunctions* :test #'equal))) (unless entry @@ -177,7 +169,7 @@ (cadr entry))))) (defun canonicalize-slot-specification (class-name spec) - (declare (special *accessors* *readers* *writers*)) + (declare (special *readers* *writers*)) (cond ((and (symbolp spec) (not (keywordp spec)) (not (memq spec '(t nil)))) @@ -199,8 +191,7 @@ (initform (getf spec :initform unsupplied))) (doplist (key val) spec (case key - (:accessor (push val *accessors*) - (push val readers) + (:accessor (push val readers) (push `(setf ,val) writers)) (:reader (push val readers)) (:writer (push val writers)) @@ -283,10 +274,11 @@ (values (early-collect-slots cpl) cpl (early-collect-default-initargs cpl) - (gathering1 (collecting) + (let (collect) (dolist (definition *early-class-definitions*) (when (memq class-name (ecd-superclass-names definition)) - (gather1 (ecd-class-name definition)))))))) + (push (ecd-class-name definition) collect))) + (nreverse collect))))) (defun early-collect-slots (cpl) (let* ((definitions (mapcar #'early-class-definition cpl)) @@ -319,8 +311,8 @@ (loop (when (null others) (return nil)) (let ((initarg (pop others))) (unless (eq initarg :direct-default-initargs) - (error "The defclass option ~S is not supported by the bootstrap~%~ - object system." + (error "~@" initarg))) (setq default-initargs (nconc default-initargs (reverse (pop others))))))) @@ -346,7 +338,8 @@ ;;; standard slots must be computed the same way in this file as it is ;;; by the full object system later. (defmacro !bootstrap-get-slot (type object slot-name) - `(instance-ref (get-slots ,object) (!bootstrap-slot-index ,type ,slot-name))) + `(clos-slots-ref (get-slots ,object) + (!bootstrap-slot-index ,type ,slot-name))) (defun !bootstrap-set-slot (type object slot-name new-value) (setf (!bootstrap-get-slot type object slot-name) new-value)) @@ -374,18 +367,16 @@ (unless (fboundp 'class-name-of) (setf (symbol-function 'class-name-of) (symbol-function 'early-class-name-of))) -;;; FIXME: Can we then delete EARLY-CLASS-NAME-OF? +(unintern 'early-class-name-of) (defun early-class-direct-subclasses (class) (!bootstrap-get-slot 'class class 'direct-subclasses)) (declaim (notinline load-defclass)) -(defun load-defclass - (name metaclass supers canonical-slots canonical-options accessor-names) +(defun load-defclass (name metaclass supers canonical-slots canonical-options) (setq supers (copy-tree supers) canonical-slots (copy-tree canonical-slots) canonical-options (copy-tree canonical-options)) - (do-standard-defsetfs-for-defclass accessor-names) (when (eq metaclass 'standard-class) (inform-type-system-about-std-class name)) (let ((ecd