Newer
Older
(unless table
(setf (gethash indicator *compile-time-property-table*)
(setq table (make-hash-table))))
(values (gethash symbol table default))))
(defsetf compile-time-property #+Genera compiler:file-declare
#-Genera set-compile-time-property)
#-Genera
(defun set-compile-time-property (symbol indicator value)
#+Cloe-Runtime
(when system::*file-declaration-environment*
(setf (clos-internals::file-declaration symbol indicator) value))
#+Minima-Developer
(zl:::compiler:file-declare symbol indicator value)
#-(or Cloe-Runtime Minima-Developer)
(let ((table (gethash indicator *compile-time-property-table*)))
(unless table
(setf (gethash indicator *compile-time-property-table*)
(setq table (make-hash-table))))
#-(or Genera (and ansi-90 (not (and Allegro (or :rs6000 (not (version>= 4 1)))))))
(defmacro define-compiler-macro (name lambda-list &body body &environment env)
env
#+Allegro `(excl::defcmacro ,name ,lambda-list ,@body)
#-(or Genera Allegro) (progn name lambda-list body env nil)) ;Suppress compiler warnings.
#+Genera
;;; Support (proclaim '(function ...)) and (proclaim '(ftype ...)).
;;; This is part of deleting spurious multiple-definition warnings about constructors.
;;; Of course, who knows if this will work in other lisps.
(declare (ignore compile-time)) ;Do it at load time as well.
(mapc #'compiler:function-defined (cdr decl)))
(defmacro print-unreadable-object ((object stream &key type identity) &body body)
`(flet ((print-unreadable-object-body () ,@body))
(declare (dynamic-extent #'print-unreadable-object-body))
(print-unreadable-object-1 ,object ,stream ,type ,identity
#'print-unreadable-object-body
',(not (null body)))))
;;; EXTRA-SPACE-REQUIRED is optional because old compiled code didn't always supply it.
(defun print-unreadable-object-1 (object stream type identity continuation
&optional (extra-space-required t))
(write-string "#<" stream)
;; wish TYPE-OF worked in PCL
(when type (lisp:format stream "~S " (class-name (class-of object))))
(funcall continuation)
(when identity
(when extra-space-required (write-char #\space stream))
(#+PCL pcl::printing-random-thing-internal ;; I assume PCL gets this right. -- rsl
#-PCL print-unreadable-object-identity
object stream))
(write-string ">" stream))
(defun print-unreadable-object-identity (object stream)
#+Genera (format stream "~O" (sys:%pointer object))
;; Lucid prints its addresses out in Hex.
#+Lucid (format stream "~X" (sys:%pointer object))
;; Probably aren't any #+(and (not Genera) (not Allegro) (not PCL) (not ANSI-90))
#-(or Genera Allegro Lucid) (declare (ignore object))
#-(or Genera Allegro Lucid) (format stream "???"))
(deftype real (&optional (min '*) (max '*))
(labels ((convert (limit floatp)
(typecase limit
(list (map 'list #'convert limit))
(otherwise limit))))
`(or (float ,(convert min t) ,(convert max t))
(rational ,(convert min nil) ,(convert max nil)))))
'(member :eof))
#+Cloe-Runtime
(defmacro load-time-value (form &optional read-only-p)
(declare (ignore read-only-p))
form)
;;; Use a lambda-list to extract arguments from a list and bind variables.
;;; This "should" signal an error if the list doesn't match the lambda-list.
;;; In implementations that have DESTRUCTURING-BIND, the easiest thing is to use it.
;;; We're actually not using the destructuring part, just the ability to match
;;; a lambda-list to a list of arguments without making a closure and using APPLY.
;;; ANSI CL has DESTRUCTURING-BIND. Cloe and Lucid have it.
;;; Genera has it in both SYMBOLICS-COMMON-LISP and FUTURE-COMMON-LISP,
;;; but the CLIM-LISP package doesn't use either of those.
;;; Last time I checked Franz did not have it
(defmacro bind-to-list (lambda-list list &body body)
(cond ((not (constantp list))
#+Genera `(scl:destructuring-bind ,lambda-list ,list
,(ignore-arglist lambda-list)
,@body)
#+Cloe-Runtime `(cloe:destructuring-bind ,lambda-list ,list
,(ignore-arglist lambda-list)
,@body)
#+Minima `(destructuring-bind ,lambda-list ,list
,(ignore-arglist lambda-list)
,@body)
#+Lucid `(lucid-common-lisp:destructuring-bind ,lambda-list ,list
,(ignore-arglist lambda-list)
,@body)
#+Allegro `(destructuring-bind ,lambda-list ,list
,(ignore-arglist lambda-list)
,@body)
;; For the other systems, I guess we'll just give up and do it the slow way
,@(when (member '&rest lambda-list)
`((declare (dynamic-extent ,(second (member '&rest lambda-list))))))
,(ignore-arglist lambda-list)
,@body))
(declare (dynamic-extent #'bind-to-list-body))
(apply #'bind-to-list-body ,list)))
(t
;; This special case supposedly comes up a lot, but I think it never comes up
;; This optimization plays fast and loose with order of evaluation issues
;; for the default value forms in the lambda-list
(setq list (eval list))
`(symbol-macrolet
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
,(do ((item)
(result nil)
(mode nil))
((null lambda-list) (nreverse result))
(setq item (pop lambda-list))
(cond ((member item '(&optional &rest &key &aux))
(setq mode item))
((member item lambda-list-keywords))
((eq mode '&rest)
(push `(,item ',list) result))
((eq mode '&key)
(multiple-value-bind (variable default supplied-p)
(if (atom item) (values item nil nil)
(values (if (atom (car item)) (car item) (cadar item))
(second item) (third item)))
(do ((l list (cddr l))
(k (parameter-specifier-keyword item)))
((null l)
(push `(,variable ,default) result)
(when supplied-p
(push `(,supplied-p 'nil) result)))
(when (eq (first l) k)
(push `(,variable ',(second l)) result)
(when supplied-p
(push `(,supplied-p 't) result))
(return)))))
(t
(multiple-value-bind (variable default supplied-p)
(if (atom item) (values item nil nil)
(values (first item) (second item) (third item)))
(cond ((null list)
(push `(,variable ,default) result)
(when supplied-p
(push `(,supplied-p 'nil) result)))
(t
(push `(,variable ',(pop list)) result)
(when supplied-p
(push `(,supplied-p 't) result))))))))
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
,@body))))
;;; Optimization to not bother with destructuring bind if none of the variables
;;; will be used
(defun lambda-list-variables-used-in-body (lambda-list body)
;; First collect the variables bound by lambda-list
(let ((variables nil))
(do* ((lambda-list lambda-list (cdr lambda-list))
(item (first lambda-list) (first lambda-list)))
((null lambda-list))
(cond ((member item lambda-list-keywords))
((atom item)
(push item variables))
(t
(push (if (atom (car item)) (car item) (cadar item)) variables)
(when (cddr item) (push (caddr item) variables)))))
(when variables
#+Genera
(lt:mapforms #'(lambda (subform kind usage state)
(declare (ignore usage state))
(when (and (member subform variables)
(not (member subform lt:*mapforms-bound-variables*))
(member kind 'lt:(set symeval)))
(return-from lambda-list-variables-used-in-body t)))
`(progn ,@body)
:bound-variables nil)
#+Cloe-Runtime
(labels ((mapper (subform context)
;; It's not worth worrying about being fooled by shadowing bindings
;; with the same name
(when (and (member subform variables)
(member context '(:access :assign)))
(return-from lambda-list-variables-used-in-body t))
(clos-internals::map-forms-recurse #'mapper subform context)))
(clos-internals::map-forms-toplevel #'mapper `(progn ,@body)))
#+(or Genera Cloe-Runtime) nil
#-(or Genera Cloe-Runtime)
;; We don't know how to do this correctly in other Lisps, since there isn't any
;; standardized code-walker, but as a first approximation we can assume that
;; if the symbols appear textually they are used as variables, and if they don't,
;; they aren't. Tricky use of macros could defeat this, but it should work well enough.
;; Of course this can be fooled by a quoted constant with the same name as a variable
;; into producing an unnecessary binding, but that's only an efficiency issue.
(labels ((analyze (tree)
(if (atom tree)
(member tree variables)
(some #'analyze tree))))
(analyze body)))))
;;; Get the keyword argument name from an &KEY parameter specifier
(defun parameter-specifier-keyword (spec)
(cond ((atom spec)
(t (caar spec))))
;;; This is needed because FIND-CLASS in the compile-file environment doesn't look
;;; also in the run-time environment, at least in Symbolics CLOS, which is pretty
;;; embarrassing when we can't find the class T.
;;; In Lucid 4.0 this produces spurious wrong number of arguments warnings for the calls
;;; to FIND-CLASS. There is no run-time error, it really does accept three arguments.
(defun find-class-that-works (name &optional (errorp t) environment)
#+Allegro (let ((environment (compile-file-environment-p environment)))
(if environment
(or (find-class name nil environment)
(find-class name errorp nil))
(find-class name errorp)))
(find-class name errorp environment)))
#+Allegro
(eval-when (compile)
(warn "~S hacked for lack of environment support in 4.1" 'find-class-that-works))
;;; F-ers
(define-modify-macro minf (&rest other-values) min)
(define-modify-macro maxf (&rest other-values) max)
(defmacro minf-or (place &rest things)
`(if (null ,place) (setf ,place (min ,@things)) (minf ,place ,@things)))
(defmacro maxf-or (place &rest things)
`(if (null ,place) (setf ,place (max ,@things)) (maxf ,place ,@things)))
(define-modify-macro roundf (&optional (divisor 1)) round)
;;; Simple vector support
(defun-inline copy-vector-portion (from-vector from-start to-vector to-start length)
(type simple-vector from-vector to-vector))
(declare (optimize (speed 3) (safety 0)))
(cond (#+Genera (< length 8) #-Genera t
(let (#+(or Genera Minima) (from-vector from-vector)
#+(or Genera Minima) (to-vector to-vector))
#+(or Genera Minima) (declare (type simple-vector from-vector to-vector))
(setf (svref to-vector to-start) (svref from-vector from-start))
(incf from-start)
(incf to-start))))
#+Genera
(t
(si:copy-array-portion from-vector from-start (+ from-start length)
to-vector to-start (+ to-start length)))))
;; VECTOR must be a simple vector, FILL-POINTER serves as its fill pointer.
;; The returned values are a (possibly new) vector and the new fill pointer.
;; The idiom for using this is
;; (MULTIPLE-VALUE-SETQ (VECTOR FP) (SIMPLE-VECTOR-PUSH-EXTEND ELEMENT VECTOR FP)).
(defun simple-vector-push-extend (element vector fill-pointer &optional extension)
(declare (values vector fill-pointer))
(when (= fill-pointer length)
;; Grow the vector
(let ((new-vector (make-array (+ length (max (ash length -1) (or extension 20)))
:element-type (array-element-type vector))))
(copy-vector-portion vector 0 new-vector 0 length)
(setq vector new-vector)))
;; Insert the new element and return the right values
(setf (svref vector fill-pointer) element)
(incf fill-pointer)
(values vector fill-pointer)))
(defun simple-vector-insert-element (element index vector fill-pointer &optional extension)
(declare (values vector fill-pointer))
(declare (type fixnum index fill-pointer)
(type simple-vector vector))
(cond ((= fill-pointer length)
;; Grow the vector, leaving a hole for the new element
(let ((new-vector (make-array (+ length (max (ash length -1) (or extension 20)))
:element-type (array-element-type vector))))
(copy-vector-portion vector 0 new-vector 0 index)
(copy-vector-portion vector index new-vector (1+ index) (- length index))
(setq vector new-vector)))
(t
;; Leave a hole for the new element
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
(optimize (speed 3) (safety 0)))
(setf (svref vector i) (svref vector (1- i)))))))
;; Plug in the new element and return the right values
(setf (svref vector index) element)
(incf fill-pointer)
(values vector fill-pointer)))
;;; Debugging support
(defmacro compiler-warn (format-string &rest format-args)
`(macrolet ((warn-it ()
(warn ,format-string ,@format-args)))
(warn-it)))
;;; Condition support
;;; The idea here is to provide a macro that will arrange for the abort choice
;;; to be on the "Abort" debugger choice, if possible.
;;; I would just use WITH-SIMPLE-RESTART but in Genera that doesn't end up
;;; on the <Abort> key. Note that in Allegro, this choice ends up as an ordinary
;;; proceed option, but in Lucid it ends up on ":A".
(defmacro with-simple-abort-restart ((format-string &rest format-args) &body body)
#{
Genera `(scl:catch-error-restart ((sys:abort) ,format-string ,@format-args)
,@body)
otherwise `(with-simple-restart (abort ,format-string ,@format-args)
,@body)
}
)
(defmacro with-simple-abort-restart-if (condition (format-string &rest format-args) &body body)
(let ((name (gensymbol 'abort-restart-form)))
`(flet ((,name () ,@body))
(declare (dynamic-extent #',name))
(if ,condition
(with-simple-abort-restart (,format-string ,@format-args) (,name))
;; Still establish it as a restart, just don't handle ABORT.
(with-simple-restart (nil ,format-string ,@format-args)
(,name))))))
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
#||
;;; The DEFINE-CLASS-MIXTURE macro is designed to allow some flexibility
;;; in the way the programmer defines class hierarchies. It works
;;; pretty much the same as the Flavors :MIXTURE option to DEFFLAVOR,
;;; except without one of the more mystifying features, namely the
;;; ability to condition the keywords' effects on the value of other
;;; keywords. Here is an example of the kind of this this version
;;; handles:
;;; (define-class-mixture drawing-path
;;; (:stretch-p stretching-drawing-path-mixin)
;;; (:orientation
;;; (nil horizontal-drawing-path-mixin)
;;; (:diagonal "Diagonal drawing paths not yet implemented")
;;; (:horizontal horizontal-drawing-path-mixin)
;;; (:vertical vertical-drawing-path-mixin)))
(defmacro define-class-mixture (name &body specs)
`(define-group ,name define-class-mixture
,@(define-class-mixture-compile-time name specs)))
#+Genera (scl:defprop define-class-mixture "Class Mixture" si:definition-type-name)
;;; Use this to define a class-mixture and a resource at the same time.
;;; This defines a mixture, and a resource of them which has a matcher
;;; and a constructor which will do what you want, namely give you
;;; objects which have the right properties flavors mixed in.
(defmacro define-class-mixture-and-resource (name (&key initializer initial-copies)
&body specs)
(let ((matcher-function-name
(make-symbol (lisp:format nil "~A-~A" name 'matcher)))
(constructor-function-name
(make-symbol (lisp:format nil "~A-~A" name 'constructor))))
`(define-group ,name define-class-mixture-and-resource
(define-class-mixture ,name ,@specs)
(defun ,matcher-function-name (object os &rest args)
(declare (dynamic-extent args))
(declare (ignore os)) ;Object-storage object from resource
(apply #'mixture-typep object ',name args))
(defun ,constructor-function-name (rd &rest args)
(declare (dynamic-extent args))
(declare (ignore rd)) ;resource-descriptor for resource
(apply #'make-mixture-instance ',name args))
(defresource ,name (&rest args)
:constructor ,constructor-function-name
:matcher ,matcher-function-name
:initializer ,initializer
:initial-copies ,initial-copies))))
;;; Create an instance of a class which has a mixture option.
(defun make-mixture-instance (mixture-name &rest args)
(declare (dynamic-extent args))
(apply #'make-instance
(apply (get mixture-name 'mixture-class-name) args)
args))
(defun mixture-typep (instance mixture-name &rest args)
(declare (dynamic-extent args))
(typep instance (apply (get mixture-name 'mixture-class-name) args)))
;;; The guts of DEFINE-CLASS-MIXTURE. This creates a class-name
;;; function for a given mixture definition, plus all the classes you
;;; need to make it work. The class names are heuristically chosen to
;;; be the ones the programmer would likely have chosen for the mixed
;;; flavors. There is no attempt to fix problems this heuristic might
;;; cause, unlike Flavors.
(defun define-class-mixture-compile-time (name specs)
(let ((keywords nil)
(forms nil)
(classes nil)
(trimmed-names nil))
(labels ((compile (form) (push form forms))
(define-class (class-name component-list)
(compile `(defclass ,class-name
(,@component-list ,name)
())))
(substring (string start &optional end)
(setf string (string string))
(subseq string start (or end (length string))))
(trim-off-name-parts (the-class-name)
(let ((pair (assoc the-class-name trimmed-names)))
(when pair (return-from trim-off-name-parts (cdr pair))))
(let* ((class-name (string the-class-name))
(sname (string name))
(nl (length sname)))
(when (and (> (length class-name) 6)
(string-equal class-name "BASIC-" :end1 6))
(setf class-name (substring class-name 6)))
(when (and (> (length class-name) 6)
(string-equal class-name "-MIXIN"
:start1 (- (length class-name) 6)))
(setf class-name (substring class-name 0 (- (length class-name) 6))))
(when (and (> (length class-name) nl)
(string-equal class-name sname :start1 (- (length class-name) nl)))
(setf class-name (substring class-name 0 (- (length class-name) nl))))
(push (cons the-class-name class-name) trimmed-names)
class-name))
(invent-class-name (subclasses)
(when (stringp (first subclasses))
(return-from invent-class-name `(error "Can't decode mixture ~S: ~A"
',name ,(first subclasses))))
(intern
(with-output-to-string (out)
(dolist (class subclasses)
(princ (trim-off-name-parts class) out))
(princ name out))))
(process-specs (specs classes-so-far)
(when (null specs)
(let ((class-name (invent-class-name classes-so-far)))
(when (symbolp class-name)
(unless (member class-name classes)
(push class-name classes)
(define-class class-name classes-so-far))
(return-from process-specs `',class-name))
(return-from process-specs class-name)))
(let* ((first-spec (first specs))
(keyword (first first-spec))
(variable-name (intern (symbol-name keyword)))
(rest (rest first-spec)))
(pushnew variable-name keywords)
(assert (consp rest) ()
"Ill-formatted mixture specification: ~S" specs)
(when (and (first rest) (symbolp (first rest)))
(assert (null (cdr rest)) ()
"A component-class mixture spec must stand alone. ~S is incorrect."
first-spec)
(return-from process-specs
`(if ,variable-name
,(process-specs (cdr specs) (cons (first rest) classes-so-far))
,(process-specs (cdr specs) classes-so-far))))
(assert (and rest (listp rest)) ()
"A specification keyword must be followed by one or more values. ~
~S is incorrect."
first-spec)
`(case ,variable-name
,@(mapcar #'(lambda (subspec)
`((,(first subspec))
,(if (second subspec)
(process-specs (cdr specs)
(cons (second subspec) classes-so-far))
(process-specs (cdr specs) classes-so-far))))
rest)
,@(unless (assoc 'otherwise rest)
`((otherwise (error "~S is illegal as the ~S option to mixture ~S.~%~
The legal values are ~{~S~^, ~}."
,variable-name ,keyword ',name
',(mapcar #'first rest)))))))))
(let ((result (process-specs specs nil)))
(compile `(defun-property (,name mixture-class-name)
(&key ,@keywords &allow-other-keys)
,result))))
(nreverse forms)))
||#