X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fcode%2Fclass.lisp;h=082484c79879a6d5daa02e330dfd28c4e4cc4d1d;hb=6a0601ab48635465ad3400c290e5cfbca28e5367;hp=4cd2a924c9c3fba490bcd87a897f3c18efcd2678;hpb=0aecc2b20142e08068c3434273500131cb13fe2d;p=sbcl.git diff --git a/src/code/class.lisp b/src/code/class.lisp index 4cd2a92..082484c 100644 --- a/src/code/class.lisp +++ b/src/code/class.lisp @@ -84,9 +84,10 @@ ;;; Note: This bound is set somewhat less than MOST-POSITIVE-FIXNUM ;;; in order to guarantee that several hash values can be added without ;;; overflowing into a bignum. -(def!constant layout-clos-hash-max (ash sb!xc:most-positive-fixnum -3) +(def!constant layout-clos-hash-limit (1+ (ash sb!xc:most-positive-fixnum -3)) #!+sb-doc - "the inclusive upper bound on LAYOUT-CLOS-HASH values") + "the exclusive upper bound on LAYOUT-CLOS-HASH values") +(def!type layout-clos-hash () '(integer 0 #.layout-clos-hash-limit)) ;;; a list of conses, initialized by genesis ;;; @@ -100,7 +101,9 @@ ;;; cold-load time. (defvar *forward-referenced-layouts*) (!cold-init-forms - (setq *forward-referenced-layouts* (make-hash-table :test 'equal)) + (setq *forward-referenced-layouts* (make-hash-table :test 'equal + #-sb-xc-host #-sb-xc-host + :synchronized t)) #-sb-xc-host (progn (/show0 "processing *!INITIAL-LAYOUTS*") (dolist (x *!initial-layouts*) @@ -113,6 +116,13 @@ ;;; type checking and garbage collection. Whenever a class is ;;; incompatibly redefined, a new layout is allocated. If two object's ;;; layouts are EQ, then they are exactly the same type. +;;; +;;; *** IMPORTANT *** +;;; +;;; If you change the slots of LAYOUT, you need to alter genesis as +;;; well, since the initialization of layout slots is hardcoded there. +;;; +;;; FIXME: ...it would be better to automate this, of course... (def!struct (layout ;; KLUDGE: A special hack keeps this from being ;; called when building code for the @@ -136,26 +146,9 @@ ;; DEF!STRUCT setup. -- WHN 19990930 #+sb-xc-host make-load-form-for-layout)) - ;; hash bits which should be set to constant pseudo-random values - ;; for use by CLOS. Sleazily accessed via %INSTANCE-REF, see - ;; LAYOUT-CLOS-HASH. - ;; - ;; FIXME: We should get our story straight on what the type of these - ;; values is. (declared INDEX here, described as <= - ;; LAYOUT-CLOS-HASH-MAX by the doc string of that constant, - ;; generated as strictly positive in RANDOM-LAYOUT-CLOS-HASH..) - ;; - ;; KLUDGE: The fact that the slots here start at offset 1 is known - ;; to the LAYOUT-CLOS-HASH function and to the LAYOUT-dumping code - ;; in GENESIS. - (clos-hash-0 (random-layout-clos-hash) :type index) - (clos-hash-1 (random-layout-clos-hash) :type index) - (clos-hash-2 (random-layout-clos-hash) :type index) - (clos-hash-3 (random-layout-clos-hash) :type index) - (clos-hash-4 (random-layout-clos-hash) :type index) - (clos-hash-5 (random-layout-clos-hash) :type index) - (clos-hash-6 (random-layout-clos-hash) :type index) - (clos-hash-7 (random-layout-clos-hash) :type index) + ;; a pseudo-random hash value for use by CLOS. KLUDGE: The fact + ;; that this slot is at offset 1 is known to GENESIS. + (clos-hash (random-layout-clos-hash) :type layout-clos-hash) ;; the class that this is a layout for (classoid (missing-arg) :type classoid) ;; The value of this slot can be: @@ -199,7 +192,21 @@ (pure nil :type (member t nil 0)) ;; Number of raw words at the end. ;; This slot is known to the C runtime support code. - (n-untagged-slots 0 :type index)) + (n-untagged-slots 0 :type index) + ;; Definition location + (source-location nil) + ;; Information about slots in the class to PCL: this provides fast + ;; access to slot-definitions and locations by name, etc. + (slot-table #(nil) :type simple-vector) + ;; True IFF the layout belongs to a standand-instance or a + ;; standard-funcallable-instance -- that is, true only if the layout + ;; is really a wrapper. + ;; + ;; FIXME: If we unify wrappers and layouts this can go away, since + ;; it is only used in SB-PCL::EMIT-FETCH-WRAPPERS, which can then + ;; use INSTANCE-SLOTS-LAYOUT instead (if there is are no slot + ;; layouts, there are no slots for it to pull.) + (for-std-class-p nil :type boolean :read-only t)) (def!method print-object ((layout layout) stream) (print-unreadable-object (layout stream :type t :identity t) @@ -214,23 +221,6 @@ ;;;; support for the hash values used by CLOS when working with LAYOUTs -(def!constant layout-clos-hash-length 8) -#!-sb-fluid (declaim (inline layout-clos-hash)) -(defun layout-clos-hash (layout i) - ;; FIXME: Either this I should be declared to be `(MOD - ;; ,LAYOUT-CLOS-HASH-LENGTH), or this is used in some inner loop - ;; where we can't afford to check that kind of thing and therefore - ;; should have some insane level of optimization. (This is true both - ;; of this function and of the SETF function below.) - (declare (type layout layout) (type index i)) - ;; FIXME: LAYOUT slots should have type `(MOD ,LAYOUT-CLOS-HASH-MAX), - ;; not INDEX. - (truly-the index (%instance-ref layout (1+ i)))) -#!-sb-fluid (declaim (inline (setf layout-clos-hash))) -(defun (setf layout-clos-hash) (new-value layout i) - (declare (type layout layout) (type index new-value i)) - (setf (%instance-ref layout (1+ i)) new-value)) - ;;; a generator for random values suitable for the CLOS-HASH slots of ;;; LAYOUTs. We use our own RANDOM-STATE here because we'd like ;;; pseudo-random values to come the same way in the target even when @@ -245,7 +235,10 @@ ;; They're declared as INDEX.. Or is this a hack to try to avoid ;; having to use bignum arithmetic? Or what? An explanation would be ;; nice. - (1+ (random layout-clos-hash-max + ;; + ;; an explanation is provided in Kiczales and Rodriguez, "Efficient + ;; Method Dispatch in PCL", 1990. -- CSR, 2005-11-30 + (1+ (random (1- layout-clos-hash-limit) (if (boundp '*layout-clos-hash-random-state*) *layout-clos-hash-random-state* (setf *layout-clos-hash-random-state* @@ -481,7 +474,7 @@ (when classoid-layout (modify-classoid classoid) (when subclasses - (dohash (subclass subclass-layout subclasses) + (dohash ((subclass subclass-layout) subclasses :locked t) (modify-classoid subclass) (when invalidate (invalidate-layout subclass-layout)))) @@ -504,7 +497,9 @@ (let* ((super (layout-classoid super-layout)) (subclasses (or (classoid-subclasses super) (setf (classoid-subclasses super) - (make-hash-table :test 'eq))))) + (make-hash-table :test 'eq + #-sb-xc-host #-sb-xc-host + :synchronized t))))) (when (and (eq (classoid-state super) :sealed) (not (gethash classoid subclasses))) (warn "unsealing sealed class ~S in order to subclass it" @@ -604,7 +599,7 @@ (when (zerop count) (push successor free-objs)))))) (cond ((endp free-objs) - (dohash (obj info obj-info) + (dohash ((obj info) obj-info) (unless (zerop (first info)) (error "Topological sort failed due to constraint on ~S." obj))) @@ -668,29 +663,13 @@ ;; during cold-load. (translation nil :type (or ctype (member nil :initializing)))) -;;; FIXME: In CMU CL, this was a class with a print function, but not -;;; necessarily a structure class (e.g. CONDITIONs). In SBCL, -;;; we let CLOS handle our print functions, so that is no longer needed. -;;; Is there any need for this class any more? -(def!struct (slot-classoid (:include classoid) - (:constructor nil))) - ;;; STRUCTURE-CLASS represents what we need to know about structure ;;; classes. Non-structure "typed" defstructs are a special case, and ;;; don't have a corresponding class. -(def!struct (basic-structure-classoid (:include slot-classoid) - (:constructor nil))) - -(def!struct (structure-classoid (:include basic-structure-classoid) +(def!struct (structure-classoid (:include classoid) (:constructor make-structure-classoid)) ;; If true, a default keyword constructor for this structure. (constructor nil :type (or function null))) - -;;; FUNCALLABLE-STRUCTURE-CLASS is used to represent funcallable -;;; structures, which are used to implement generic functions. -(def!struct (funcallable-structure-classoid - (:include basic-structure-classoid) - (:constructor make-funcallable-structure-classoid))) ;;;; classoid namespace @@ -779,7 +758,18 @@ NIL is returned when no such class exists." (remhash name *forward-referenced-layouts*) (%note-type-defined name) - (setf (info :type :kind name) :instance) + ;; we need to handle things like + ;; (setf (find-class 'foo) (find-class 'integer)) + ;; and + ;; (setf (find-class 'integer) (find-class 'integer)) + (cond + ((built-in-classoid-p new-value) + (setf (info :type :kind name) (or (info :type :kind name) :defined)) + (let ((translation (built-in-classoid-translation new-value))) + (when translation + (setf (info :type :translator name) + (lambda (c) (declare (ignore c)) translation))))) + (t (setf (info :type :kind name) :instance))) (setf (classoid-cell-classoid (find-classoid-cell name)) new-value) (unless (eq (info :type :compiler-layout name) (classoid-layout new-value)) @@ -816,6 +806,38 @@ NIL is returned when no such class exists." (!define-type-class classoid) +;;; We might be passed classoids with invalid layouts; in any pairwise +;;; class comparison, we must ensure that both are valid before +;;; proceeding. +(defun ensure-classoid-valid (classoid layout) + (aver (eq classoid (layout-classoid layout))) + (when (layout-invalid layout) + (if (typep classoid 'standard-classoid) + (let ((class (classoid-pcl-class classoid))) + (cond + ((sb!pcl:class-finalized-p class) + (sb!pcl::force-cache-flushes class)) + ((sb!pcl::class-has-a-forward-referenced-superclass-p class) + (error "Invalid, unfinalizeable class ~S (classoid ~S)." + class classoid)) + (t (sb!pcl:finalize-inheritance class)))) + (error "Don't know how to ensure validity of ~S (not ~ + a STANDARD-CLASSOID)." classoid)))) + +(defun ensure-both-classoids-valid (class1 class2) + (do ((layout1 (classoid-layout class1) (classoid-layout class1)) + (layout2 (classoid-layout class2) (classoid-layout class2)) + (i 0 (+ i 1))) + ((and (not (layout-invalid layout1)) (not (layout-invalid layout2)))) + (aver (< i 2)) + (ensure-classoid-valid class1 layout1) + (ensure-classoid-valid class2 layout2))) + +(defun update-object-layout-or-invalid (object layout) + (if (typep (classoid-of object) 'standard-classoid) + (sb!pcl::check-wrapper-validity object) + (sb!c::%layout-invalid-error object layout))) + ;;; Simple methods for TYPE= and SUBTYPEP should never be called when ;;; the two classes are equal, since there are EQ checks in those ;;; operations. @@ -825,6 +847,7 @@ NIL is returned when no such class exists." (!define-type-method (classoid :simple-subtypep) (class1 class2) (aver (not (eq class1 class2))) + (ensure-both-classoids-valid class1 class2) (let ((subclasses (classoid-subclasses class2))) (if (and subclasses (gethash class1 subclasses)) (values t t) @@ -839,7 +862,7 @@ NIL is returned when no such class exists." (o-sub (classoid-subclasses other))) (if (and s-sub o-sub) (collect ((res *empty-type* type-union)) - (dohash (subclass layout s-sub) + (dohash ((subclass layout) s-sub :locked t) (declare (ignore layout)) (when (gethash subclass o-sub) (res (specifier-type subclass)))) @@ -848,6 +871,7 @@ NIL is returned when no such class exists." (!define-type-method (classoid :simple-intersection2) (class1 class2) (declare (type classoid class1 class2)) + (ensure-both-classoids-valid class1 class2) (cond ((eq class1 class2) class1) ;; If one is a subclass of the other, then that is the @@ -861,8 +885,8 @@ NIL is returned when no such class exists." ;; Otherwise, we can't in general be sure that the ;; intersection is empty, since a subclass of both might be ;; defined. But we can eliminate it for some special cases. - ((or (basic-structure-classoid-p class1) - (basic-structure-classoid-p class2)) + ((or (structure-classoid-p class1) + (structure-classoid-p class2)) ;; No subclass of both can be defined. *empty-type*) ((eq (classoid-state class1) :sealed) @@ -906,12 +930,16 @@ NIL is returned when no such class exists." ;;;; PCL stuff -(def!struct (std-classoid (:include classoid) - (:constructor nil))) -(def!struct (standard-classoid (:include std-classoid) +;;; the CLASSOID that we use to represent type information for +;;; STANDARD-CLASS and FUNCALLABLE-STANDARD-CLASS. The type system +;;; side does not need to distinguish between STANDARD-CLASS and +;;; FUNCALLABLE-STANDARD-CLASS. +(def!struct (standard-classoid (:include classoid) (:constructor make-standard-classoid))) -(def!struct (random-pcl-classoid (:include std-classoid) - (:constructor make-random-pcl-classoid))) +;;; a metaclass for classes which aren't standardlike but will never +;;; change either. +(def!struct (static-classoid (:include classoid) + (:constructor make-static-classoid))) ;;;; built-in classes @@ -1054,7 +1082,9 @@ NIL is returned when no such class exists." :inherits (array) :prototype-form (make-array nil)) (sequence - :translation (or cons (member nil) vector)) + :translation (or cons (member nil) vector extended-sequence) + :state :read-only + :depth 2) (vector :translation vector :codes (#.sb!vm:complex-vector-widetag) :direct-superclasses (array sequence) @@ -1423,11 +1453,14 @@ NIL is returned when no such class exists." ;;; Mark LAYOUT as invalid. Setting DEPTHOID -1 helps cause unsafe ;;; structure type tests to fail. Remove class from all superclasses ;;; too (might not be registered, so might not be in subclasses of the -;;; nominal superclasses.) +;;; nominal superclasses.) We set the layout-clos-hash slots to 0 to +;;; invalidate the wrappers for specialized dispatch functions, which +;;; use those slots as indexes into tables. (defun invalidate-layout (layout) (declare (type layout layout)) (setf (layout-invalid layout) t (layout-depthoid layout) -1) + (setf (layout-clos-hash layout) 0) (let ((inherits (layout-inherits layout)) (classoid (layout-classoid layout))) (modify-classoid classoid) @@ -1445,7 +1478,7 @@ NIL is returned when no such class exists." ;;; late in the build-order.lisp-expr sequence, and be put in ;;; !COLD-INIT-FORMS there? (defun !class-finalize () - (dohash (name layout *forward-referenced-layouts*) + (dohash ((name layout) *forward-referenced-layouts*) (let ((class (find-classoid name nil))) (cond ((not class) (setf (layout-classoid layout) (make-undefined-classoid name))) @@ -1476,6 +1509,13 @@ NIL is returned when no such class exists." (let ((layout (classoid-layout (find-classoid name)))) (dolist (code codes) (setf (svref res code) layout))))))) + (setq *null-classoid-layout* + ;; KLUDGE: we use (LET () ...) instead of a LOCALLY here to + ;; work around a bug in the LOCALLY handling in the fopcompiler + ;; (present in 0.9.13-0.9.14.18). -- JES, 2006-07-16 + (let () + (declare (notinline find-classoid)) + (classoid-layout (find-classoid 'null)))) #-sb-xc-host (/show0 "done setting *BUILT-IN-CLASS-CODES*")) (!defun-from-collected-cold-init-forms !classes-cold-init)