| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: CLOS -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; Copyright (c) 1992, Giuseppe Attardi. |
| 6 |
;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll. |
| 7 |
;;;; |
| 8 |
;;;; See file 'LICENSE' for the copyright details. |
| 9 |
|
| 10 |
(in-package "CLOS") |
| 11 |
|
| 12 |
;;; ---------------------------------------------------------------------- |
| 13 |
;;; Load forms |
| 14 |
;;; |
| 15 |
;;; ECL extends the ANSI specification by allowing to use |
| 16 |
;;; MAKE-LOAD-FORM on almost any kind of lisp object. |
| 17 |
;;; |
| 18 |
|
| 19 |
(defun make-load-form-saving-slots (object &key slot-names environment) |
| 20 |
(declare (ignore environment)) |
| 21 |
(let* ((class (class-of object)) |
| 22 |
(class-name (class-name class)) |
| 23 |
(initialization-form (list 'progn)) |
| 24 |
(slots (class-slots class)) |
| 25 |
(decls (when (typep object 'structure-object) |
| 26 |
(get-sysprop class-name 'si::structure-slot-descriptions)))) |
| 27 |
(do ((slot #1=(pop slots) #1#) |
| 28 |
(desc #2=(pop decls) #2#)) |
| 29 |
((null slot) (values `(allocate-instance ,class) |
| 30 |
(nreverse initialization-form))) |
| 31 |
(let ((slot-name (slot-definition-name slot))) |
| 32 |
(when (or (and (null slot-names) |
| 33 |
(eq (slot-definition-allocation slot) :instance)) |
| 34 |
(member slot-name slot-names)) |
| 35 |
(flet ((primitive-set (val) |
| 36 |
(if (typep object 'structure-object) |
| 37 |
`(si::structure-set ,object ',class-name ,(nth 4 desc) ',val) |
| 38 |
`(setf (slot-value ,object ',slot-name) ',val))) |
| 39 |
(primitive-nil () |
| 40 |
(if (typep object 'structure-object) |
| 41 |
`(si::structure-set ,object ',class-name ,(nth 4 desc) nil) |
| 42 |
`(slot-makunbound ,object ',slot-name)))) |
| 43 |
(push |
| 44 |
(if (slot-boundp object slot-name) |
| 45 |
(primitive-set (slot-value object slot-name)) |
| 46 |
(primitive-nil)) |
| 47 |
initialization-form))))))) |
| 48 |
|
| 49 |
(defmethod make-load-form ((object t) &optional env) |
| 50 |
(flet ((maybe-quote (object) |
| 51 |
(if (or (consp object) (symbolp object)) |
| 52 |
(list 'quote object) |
| 53 |
object))) |
| 54 |
(unless (si::need-to-make-load-form-p object) |
| 55 |
(return-from make-load-form (maybe-quote object))) |
| 56 |
(typecase object |
| 57 |
(compiled-function |
| 58 |
(multiple-value-bind (lex code data name) |
| 59 |
(si::bc-split object) |
| 60 |
(unless code |
| 61 |
(error "Cannot externalize object ~a" object)) |
| 62 |
(values `(si::bc-join ,(make-load-form lex env) |
| 63 |
',code ; An specialized array, no load form |
| 64 |
,(make-load-form data env) |
| 65 |
,(make-load-form name env))))) |
| 66 |
(array |
| 67 |
(let ((init-forms '())) |
| 68 |
(values `(make-array ',(array-dimensions object) |
| 69 |
:element-type ',(array-element-type object) |
| 70 |
:adjustable ',(adjustable-array-p object) |
| 71 |
:initial-contents |
| 72 |
',(loop for i from 0 below (array-total-size object) |
| 73 |
collect (let ((x (row-major-aref object i))) |
| 74 |
(if (si::need-to-make-load-form-p x) |
| 75 |
(progn (push `(setf (row-major-aref ,object ,i) ',x) |
| 76 |
init-forms) |
| 77 |
0) |
| 78 |
x)))) |
| 79 |
(and init-forms `(progn ,@init-forms))))) |
| 80 |
(cons |
| 81 |
(values `(cons nil nil) |
| 82 |
`(progn (rplaca ,(maybe-quote object) |
| 83 |
,(maybe-quote (car object))) |
| 84 |
(rplacd ,(maybe-quote object) |
| 85 |
,(maybe-quote (cdr object)))))) |
| 86 |
(hash-table |
| 87 |
(let* ((content (ext:hash-table-content object)) |
| 88 |
(make-form `(make-hash-table |
| 89 |
:size ,(hash-table-size object) |
| 90 |
:rehash-size ,(hash-table-rehash-size object) |
| 91 |
:rehash-threshold ,(hash-table-rehash-threshold object) |
| 92 |
:test ',(hash-table-test object)))) |
| 93 |
(if (si::need-to-make-load-form-p content) |
| 94 |
(values |
| 95 |
make-form |
| 96 |
`(dolist (i ',(loop for key being each hash-key in object |
| 97 |
using (hash-value obj) |
| 98 |
collect (cons key obj))) |
| 99 |
(setf (gethash (car i) ,object) (cdr i)))) |
| 100 |
(values |
| 101 |
`(ext:hash-table-fill ,make-form ',content) |
| 102 |
nil)))) |
| 103 |
(random-state |
| 104 |
(let ((state (ext:random-state-array object))) |
| 105 |
(values `(make-random-state ,state) nil))) |
| 106 |
(t |
| 107 |
(no-make-load-form object))))) |
| 108 |
|
| 109 |
(defmethod make-load-form ((object standard-object) &optional environment) |
| 110 |
(declare (ignore environment)) |
| 111 |
(no-make-load-form object)) |
| 112 |
|
| 113 |
(defmethod make-load-form ((object structure-object) &optional environment) |
| 114 |
(declare (ignore environment)) |
| 115 |
(no-make-load-form object)) |
| 116 |
|
| 117 |
(defmethod make-load-form ((object condition) &optional environment) |
| 118 |
(declare (ignore environment)) |
| 119 |
(no-make-load-form object)) |
| 120 |
|
| 121 |
(defun no-make-load-form (object) |
| 122 |
(declare (si::c-local)) |
| 123 |
(error "No adequate specialization of MAKE-LOAD-FORM for an object type ~A" |
| 124 |
(type-of object))) |
| 125 |
|
| 126 |
(defmethod make-load-form ((class class) &optional environment) |
| 127 |
(declare (ignore environment)) |
| 128 |
(let ((name (class-name class))) |
| 129 |
(if (and name (eq (find-class name) class)) |
| 130 |
`(find-class ',name) |
| 131 |
(error "Cannot externalize anonymous class ~A" class)))) |
| 132 |
|
| 133 |
(defmethod make-load-form ((package package) &optional environment) |
| 134 |
(declare (ignore environment)) |
| 135 |
`(find-package ,(package-name package))) |
| 136 |
|
| 137 |
;;; ---------------------------------------------------------------------- |
| 138 |
;;; Printing |
| 139 |
;;; ---------------------------------------------------------------------- |
| 140 |
|
| 141 |
(defmethod print-object ((instance t) stream) |
| 142 |
(let ((*package* (find-package "CL"))) |
| 143 |
(print-unreadable-object (instance stream) |
| 144 |
(format stream "~S" |
| 145 |
(class-name (si:instance-class instance))))) |
| 146 |
instance) |
| 147 |
|
| 148 |
(defmethod print-object ((instance standard-object) stream) |
| 149 |
(print-unreadable-object (instance stream :identity t) |
| 150 |
(let ((*package* (find-package "CL"))) |
| 151 |
(format stream "a ~S" |
| 152 |
(class-name (si:instance-class instance))))) |
| 153 |
instance) |
| 154 |
|
| 155 |
(defmethod print-object ((class class) stream) |
| 156 |
(print-unreadable-object (class stream) |
| 157 |
(let ((*package* (find-package "CL"))) |
| 158 |
(format stream "The ~S ~S" |
| 159 |
(class-name (si:instance-class class)) (class-name class)))) |
| 160 |
class) |
| 161 |
|
| 162 |
(defmethod print-object ((gf standard-generic-function) stream) |
| 163 |
(print-unreadable-object (gf stream :type t) |
| 164 |
(prin1 (generic-function-name gf) stream)) |
| 165 |
gf) |
| 166 |
|
| 167 |
(defmethod print-object ((m standard-method) stream) |
| 168 |
(print-unreadable-object (m stream :type t) |
| 169 |
(format stream "~A ~{~S ~}~S" |
| 170 |
(let ((gf (method-generic-function m))) |
| 171 |
(if gf |
| 172 |
(generic-function-name gf) |
| 173 |
'UNNAMED)) |
| 174 |
(method-qualifiers m) |
| 175 |
(loop for spec in (method-specializers m) |
| 176 |
collect (cond ((and (classp spec) |
| 177 |
(class-name spec))) |
| 178 |
((typep spec 'eql-specializer) |
| 179 |
`(eql ,(eql-specializer-object spec))) |
| 180 |
(t spec))))) |
| 181 |
m) |
| 182 |
|
| 183 |
(defun ext::float-nan-string (x) |
| 184 |
(unless (ext:float-nan-p x) |
| 185 |
(signal 'type-error :datum x :expected-type 'float-nan)) |
| 186 |
|
| 187 |
(cond |
| 188 |
((null *print-readably*) |
| 189 |
(etypecase x |
| 190 |
(single-float "#<single-float quiet NaN>") |
| 191 |
(double-float "#<double-float quiet NaN>") |
| 192 |
(long-float "#<long-float quiet NaN>") |
| 193 |
(short-float "#<short-float quiet NaN>"))) |
| 194 |
#+ieee-floating-point |
| 195 |
(*read-eval* |
| 196 |
(etypecase x |
| 197 |
(single-float "#.(coerce (si:nan) 'single-float)") |
| 198 |
(double-float "#.(coerce (si:nan) 'double-float)") |
| 199 |
(long-float "#.(coerce (si:nan) 'long-float)") |
| 200 |
(short-float "#.(coerce (si:nan) 'short-float)"))) |
| 201 |
(t (error 'print-not-readable :object x)))) |
| 202 |
|
| 203 |
(defun ext::float-infinity-string (x) |
| 204 |
(unless (ext:float-infinity-p x) |
| 205 |
(signal 'type-error :datum x :expected-type 'float-infinity)) |
| 206 |
|
| 207 |
(cond |
| 208 |
((null *print-readably*) |
| 209 |
(etypecase x |
| 210 |
(ext:negative-single-float "#<single-float negative infinity>") |
| 211 |
(ext:positive-single-float "#<single-float positive infinity>") |
| 212 |
(ext:negative-double-float "#<double-float negative infinity>") |
| 213 |
(ext:positive-double-float "#<double-float positive infinity>") |
| 214 |
(ext:negative-long-float "#<long-float negative infinity>") |
| 215 |
(ext:positive-long-float "#<long-float positive infinity>") |
| 216 |
(ext:negative-short-float "#<short-float negative infinity>") |
| 217 |
(ext:positive-short-float "#<short-float positive infinity>"))) |
| 218 |
#+ieee-floating-point |
| 219 |
(*read-eval* |
| 220 |
(etypecase x |
| 221 |
(ext:negative-single-float "#.ext::single-float-negative-infinity") |
| 222 |
(ext:positive-single-float "#.ext::single-float-positive-infinity") |
| 223 |
(ext:negative-double-float "#.ext::double-float-negative-infinity") |
| 224 |
(ext:positive-double-float "#.ext::double-float-positive-infinity") |
| 225 |
(ext:negative-long-float "#.ext::long-float-negative-infinity") |
| 226 |
(ext:positive-long-float "#.ext::long-float-positive-infinity") |
| 227 |
(ext:negative-short-float "#.ext::short-float-negative-infinity") |
| 228 |
(ext:positive-short-float "#.ext::short-float-positive-infinity"))) |
| 229 |
(t (error 'print-not-readable :object x)))) |
| 230 |
|
| 231 |
;;; ---------------------------------------------------------------------- |
| 232 |
;;; Describe |
| 233 |
;;; ---------------------------------------------------------------------- |
| 234 |
|
| 235 |
(defmethod describe-object ((obj t) (stream t)) |
| 236 |
(let* ((class (class-of obj)) |
| 237 |
(slotds (class-slots class))) |
| 238 |
(format stream "~%~A is an instance of class ~A" |
| 239 |
obj (class-name class)) |
| 240 |
(do ((scan slotds (cdr scan)) |
| 241 |
(i 0 (1+ i)) |
| 242 |
(sv)) |
| 243 |
((null scan)) |
| 244 |
(declare (fixnum i)) |
| 245 |
(setq sv (si:instance-ref obj i)) |
| 246 |
(print (slot-definition-name (car scan)) stream) (princ ": " stream) |
| 247 |
(if (si:sl-boundp sv) |
| 248 |
(prin1 sv stream) |
| 249 |
(prin1 "Unbound" stream)))) |
| 250 |
obj) |
| 251 |
|
| 252 |
(defmethod describe-object ((obj class) (stream t)) |
| 253 |
(let* ((class (si:instance-class obj)) |
| 254 |
(slotds (class-slots class))) |
| 255 |
(format stream "~%~A is an instance of class ~A" |
| 256 |
obj (class-name class)) |
| 257 |
(do ((scan slotds (cdr scan)) |
| 258 |
(i 0 (1+ i)) |
| 259 |
(sv)) |
| 260 |
((null scan)) |
| 261 |
(declare (fixnum i)) |
| 262 |
(print (slot-definition-name (car scan)) stream) (princ ": " stream) |
| 263 |
(case (slot-definition-name (car scan)) |
| 264 |
((superiors inferiors) |
| 265 |
(princ "(" stream) |
| 266 |
(do* ((scan (si:instance-ref obj i) (cdr scan)) |
| 267 |
(e (car scan) (car scan))) |
| 268 |
((null scan)) |
| 269 |
(prin1 (class-name e) stream) |
| 270 |
(when (cdr scan) (princ " " stream))) |
| 271 |
(princ ")" stream)) |
| 272 |
(otherwise |
| 273 |
(setq sv (si:instance-ref obj i)) |
| 274 |
(if (si:sl-boundp sv) |
| 275 |
(prin1 sv stream) |
| 276 |
(prin1 "Unbound" stream)))))) |
| 277 |
obj) |
| 278 |
|
| 279 |
;; ---------------------------------------------------------------------- |