X-Git-Url: https://git.distorted.org.uk/~mdw/sod/blobdiff_plain/4b8e5c0347115ff30841f1d1e71afe59ecb6c82c..e45a106df3272c787444bc6f7b8920016b7fc677:/src/class-finalize-impl.lisp diff --git a/src/class-finalize-impl.lisp b/src/class-finalize-impl.lisp index b51870c..320534b 100644 --- a/src/class-finalize-impl.lisp +++ b/src/class-finalize-impl.lisp @@ -7,7 +7,7 @@ ;;;----- Licensing notice --------------------------------------------------- ;;; -;;; This file is part of the Sensble Object Design, an object system for C. +;;; This file is part of the Sensible Object Design, an object system for C. ;;; ;;; SOD is free software; you can redistribute it and/or modify ;;; it under the terms of the GNU General Public License as published by @@ -48,6 +48,22 @@ ;; Superclass Linearization for Dylan' for more detail. ;; http://www.webcom.com/haahr/dylan/linearization-oopsla96.html +;;; Utilities. + +(export 'merge-class-lists) +(defun merge-class-lists (lists pick) + "Merge the LISTS of classes, using PICK to break ties. + + This is a convenience wrapper around the main `merge-lists' function. + Given that class linearizations (almost?) always specify a custom + tiebreaker function, this isn't a keyword argument. Also, this wrapper + provides a standard presentation function so that any errors are presented + properly." + (merge-lists lists + :pick pick + :present (lambda (class) + (format nil "`~A'" (sod-class-name class))))) + ;;; Tiebreaker functions. (defun clos-tiebreaker (candidates so-far) @@ -67,7 +83,7 @@ (when (member candidate (sod-class-direct-superclasses class)) (setf winner candidate)))) (unless winner - (error "SOD INTERNAL ERROR: Failed to break tie in CLOS.")) + (error "SOD INTERNAL ERROR: Failed to break tie in CLOS")) winner)) (defun c3-tiebreaker (candidates cpls) @@ -96,10 +112,11 @@ (dolist (candidate candidates) (when (member candidate cpl) (return-from c3-tiebreaker candidate)))) - (error "SOD INTERNAL ERROR: Failed to break tie in C3.")) + (error "SOD INTERNAL ERROR: Failed to break tie in C3")) ;;; Linearization functions. +(export 'clos-cpl) (defun clos-cpl (class) "Compute the class precedence list of CLASS using CLOS linearization rules. @@ -116,12 +133,13 @@ (remove-duplicates (cons class (mappend #'superclasses direct-supers)))))) - (merge-lists (mapcar (lambda (class) - (cons class - (sod-class-direct-superclasses class))) - (superclasses class)) - :pick #'clos-tiebreaker))) + (merge-class-lists + (mapcar (lambda (class) + (cons class (sod-class-direct-superclasses class))) + (superclasses class)) + #'clos-tiebreaker))) +(export 'dylan-cpl) (defun dylan-cpl (class) "Compute the class precedence list of CLASS using Dylan linearization rules. @@ -141,10 +159,12 @@ you're going to lose anyway." (let ((direct-supers (sod-class-direct-superclasses class))) - (merge-lists (cons (cons class direct-supers) - (mapcar #'sod-class-precedence-list direct-supers)) - :pick #'clos-tiebreaker))) + (merge-class-lists + (cons (cons class direct-supers) + (mapcar #'sod-class-precedence-list direct-supers)) + #'clos-tiebreaker))) +(export 'c3-cpl) (defun c3-cpl (class) "Compute the class precedence list of CLASS using C3 linearization rules. @@ -159,11 +179,12 @@ (let* ((direct-supers (sod-class-direct-superclasses class)) (cpls (mapcar #'sod-class-precedence-list direct-supers))) - (merge-lists (cons (cons class direct-supers) cpls) - :pick (lambda (candidates so-far) + (merge-class-lists (cons (cons class direct-supers) cpls) + (lambda (candidates so-far) (declare (ignore so-far)) (c3-tiebreaker candidates cpls))))) +(export 'flavors-cpl) (defun flavors-cpl (class) "Compute the class precedence list of CLASS using Flavors linearization rules. @@ -186,6 +207,7 @@ (walk class) (nreverse done)))) +(export 'python-cpl) (defun python-cpl (class) "Compute the class precedence list of CLASS using the documented Python 2.2 linearization rules. @@ -205,6 +227,7 @@ (walk class) (delete-duplicates (nreverse done))))) +(export 'l*loops-cpl) (defun l*loops-cpl (class) "Compute the class precedence list of CLASS using L*LOOPS linearization rules. @@ -218,13 +241,14 @@ precedence order i.e., the direct-superclasses list orderings." (let ((dfs (flavors-cpl class))) - (cons class (merge-lists (mapcar #'sod-class-precedence-list + (cons class + (merge-class-lists (mapcar #'sod-class-precedence-list (sod-class-direct-superclasses class)) - :pick (lambda (candidates so-far) - (declare (ignore so-far)) - (dolist (class dfs) - (when (member class candidates) - (return class)))))))) + (lambda (candidates so-far) + (declare (ignore so-far)) + (dolist (class dfs) + (when (member class candidates) + (return class)))))))) ;;; Default function. @@ -270,6 +294,27 @@ (cdr class-precedence-list))))))))) ;;;-------------------------------------------------------------------------- +;;; Metaclasses. + +(defmethod guess-metaclass ((class sod-class)) + "Default metaclass-guessing function for classes. + + Return the most specific metaclass of any of the CLASS's direct + superclasses." + + ;; During bootstrapping, our superclasses might not have their own + ;; metaclasses resolved yet. If we find this, then throw `bootstrapping' + ;; so that `shared-initialize' on `sod-class' can catch it (or as a shot + ;; across the bows of anyone else who calls us). + (finalization-error (:bad-metaclass) + (select-minimal-class-property (sod-class-direct-superclasses class) + (lambda (super) + (if (slot-boundp super 'metaclass) + (slot-value super 'metaclass) + (throw 'bootstrapping nil))) + #'sod-subclass-p class "metaclass"))) + +;;;-------------------------------------------------------------------------- ;;; Sanity checking. (defmethod check-sod-class ((class sod-class)) @@ -308,6 +353,41 @@ (error "In `~A~, chain-to class `~A' is not a proper superclass" class chain-link))) + ;; Check that the initargs declare compatible types. Duplicate entries, + ;; even within a class, are harmless, but at most one initarg in any + ;; class should declare a default value. + (with-slots (class-precedence-list) class + (let ((seen (make-hash-table :test #'equal))) + (dolist (super class-precedence-list) + (with-slots (initargs) super + (dolist (initarg (reverse initargs)) + (let* ((initarg-name (sod-initarg-name initarg)) + (initarg-type (sod-initarg-type initarg)) + (initarg-default (sod-initarg-default initarg)) + (found (gethash initarg-name seen)) + (found-type (and found (sod-initarg-type found))) + (found-default (and found (sod-initarg-default found))) + (found-class (and found (sod-initarg-class found))) + (found-location (and found (file-location found)))) + (with-default-error-location (initarg) + (cond ((not found) + (setf (gethash initarg-name seen) initarg)) + ((not (c-type-equal-p initarg-type found-type)) + (cerror* "Inititalization argument `~A' defined ~ + with incompatible types: ~ + ~A in class ~A, and ~ + ~A in class ~A (at ~A)" + initarg-name initarg-type super + found-type found-class found-location)) + ((and initarg-default found-default + (eql super found-class)) + (cerror* "Initialization argument `~A' redefined ~ + with default value ~ + (previous definition at ~A)" + initarg-name found-location)) + (initarg-default + (setf (gethash initarg-name seen) initarg)))))))))) + ;; Check for circularity in the superclass graph. Since the superclasses ;; should already be acyclic, it suffices to check that our class is not ;; a superclass of any of its own direct superclasses. @@ -333,68 +413,110 @@ ;;;-------------------------------------------------------------------------- ;;; Finalization. -(defmethod finalize-sod-class ((class sod-class)) +(defmethod finalize-sod-class :around ((class sod-class)) + "Common functionality for `finalize-sod-class'. - ;; CLONE-AND-HACK WARNING: Note that `bootstrap-classes' has a (very brief) - ;; clone of the CPL and chain establishment code. If the interface changes - ;; then `bootstrap-classes' will need to be changed too. + * If an attempt to finalize the CLASS has been made before, then we + don't try again. Similarly, attempts to finalize a class recursively + will fail. + * A condition handler is established to keep track of whether any errors + are signalled during finalization. The CLASS is only marked as + successfully finalized if no (unhandled) errors are encountered." (with-default-error-location (class) (ecase (sod-class-state class) ((nil) - ;; If this fails, mark the class as a loss. - (setf (sod-class-state class) :broken) - - ;; Finalize all of the superclasses. There's some special pleading - ;; here to make bootstrapping work: we don't try to finalize the - ;; metaclass if we're a root class (no direct superclasses -- because - ;; in that case the metaclass will have to be a subclass of us!), or - ;; if it's equal to us. This is enough to tie the knot at the top of - ;; the class graph. - (with-slots (name direct-superclasses metaclass) class - (dolist (super direct-superclasses) - (finalize-sod-class super)) - (unless (or (null direct-superclasses) - (eq class metaclass)) - (finalize-sod-class metaclass))) - - ;; Stash the class's type. - (setf (sod-class-type class) - (make-class-type (sod-class-name class))) - - ;; Clobber the lists of items if they've not been set. - (dolist (slot '(slots instance-initializers class-initializers - messages methods)) - (unless (slot-boundp class slot) - (setf (slot-value class slot) nil))) - - ;; If the CPL hasn't been done yet, compute it. - (with-slots (class-precedence-list) class - (unless (slot-boundp class 'class-precedence-list) - (setf class-precedence-list (compute-cpl class)))) - - ;; Check that the class is fairly sane. - (check-sod-class class) - - ;; Determine the class's layout. - (with-slots (chain-head chain chains) class - (setf (values chain-head chain chains) (compute-chains class))) - - ;; FIXME: make these slots autovivifying. - (with-slots ((ilayout %ilayout) effective-methods vtables) class - (setf ilayout (compute-ilayout class)) - (setf effective-methods (compute-effective-methods class)) - (setf vtables (compute-vtables class))) - - ;; Done. - (setf (sod-class-state class) :finalized) - t) - + ;; If this fails, leave the class marked as a loss. + (setf (slot-value class 'state) :broken) + + ;; Invoke the finalization method proper. If it signals any + ;; continuable errors, take note of them so that we can report failure + ;; properly. + ;; + ;; Catch: we get called recursively to clean up superclasses and + ;; metaclasses, but there should only be one such handler, so don't + ;; add another. (In turn, this means that other methods mustn't + ;; actually trap their significant errors.) + (let ((have-handler-p (boundp '*finalization-errors*)) + (*finalization-errors* nil) + (*finalization-error-token* nil)) + (catch '%finalization-failed + (if have-handler-p (call-next-method) + (handler-bind ((error (lambda (cond) + (declare (ignore cond)) + (pushnew *finalization-error-token* + *finalization-errors* + :test #'equal) + :decline))) + (call-next-method))) + (when *finalization-errors* (finalization-failed)) + (setf (slot-value class 'state) :finalized) + t))) + + ;; If the class is broken, we're not going to be able to fix it now. (:broken nil) + ;; If we already finalized it, there's no point doing it again. (:finalized t)))) +(defmethod finalize-sod-class ((class sod-class)) + + ;; CLONE-AND-HACK WARNING: Note that `bootstrap-classes' has a (very brief) + ;; clone of the CPL and chain establishment code. If the interface changes + ;; then `bootstrap-classes' will need to be changed too. + + ;; Set up the metaclass if it's not been set already. This is delayed + ;; to give bootstrapping a chance to set up metaclass and superclass + ;; circularities. + (default-slot (class 'metaclass) (guess-metaclass class)) + + ;; Finalize all of the superclasses. There's some special pleading here to + ;; make bootstrapping work: we don't try to finalize the metaclass if we're + ;; a root class (no direct superclasses -- because in that case the + ;; metaclass will have to be a subclass of us!), or if it's equal to us. + ;; This is enough to tie the knot at the top of the class graph. If we + ;; can't manage this then we're doomed. + (flet ((try-finalizing (what other-class) + (unless (finalize-sod-class other-class) + (cerror* "Class `~A' has broken ~A `~A'" class what other-class) + (info-with-location other-class + "Class `~A' defined here" other-class) + (finalization-failed)))) + (let ((supers (sod-class-direct-superclasses class)) + (meta (sod-class-metaclass class))) + (dolist (super supers) + (try-finalizing "direct superclass" super)) + (unless (or (null supers) (eq class meta)) + (try-finalizing "metaclass" meta)))) + + ;; Stash the class's type. + (setf (slot-value class '%type) + (make-class-type (sod-class-name class))) + + ;; Clobber the lists of items if they've not been set. + (dolist (slot '(slots instance-initializers class-initializers + messages methods)) + (unless (slot-boundp class slot) + (setf (slot-value class slot) nil))) + + ;; If the CPL hasn't been done yet, compute it. If we can't manage this + ;; then there's no hope at all. + (unless (slot-boundp class 'class-precedence-list) + (restart-case + (setf (slot-value class 'class-precedence-list) (compute-cpl class)) + (continue () :report "Continue" + (finalization-failed)))) + + ;; Check that the class is fairly sane. + (check-sod-class class) + + ;; Determine the class's layout. + (setf (values (slot-value class 'chain-head) + (slot-value class 'chain) + (slot-value class 'chains)) + (compute-chains class))) + ;;;----- That's all, folks --------------------------------------------------