1 ;;;; This file contains stuff that implements the portable IR1
2 ;;;; semantics of type tests and coercion. The main thing we do is
3 ;;;; convert complex type operations into simpler code that can be
6 ;;;; This software is part of the SBCL system. See the README file for
9 ;;;; This software is derived from the CMU CL system, which was
10 ;;;; written at Carnegie Mellon University and released into the
11 ;;;; public domain. The software is in the public domain and is
12 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
13 ;;;; files for more information.
17 ;;;; type predicate translation
19 ;;;; We maintain a bidirectional association between type predicates
20 ;;;; and the tested type. The presence of a predicate in this
21 ;;;; association implies that it is desirable to implement tests of
22 ;;;; this type using the predicate. These are either predicates that
23 ;;;; the back end is likely to have special knowledge about, or
24 ;;;; predicates so complex that the only reasonable implentation is
25 ;;;; via function call.
27 ;;;; Some standard types (such as ATOM) are best tested by letting the
28 ;;;; TYPEP source transform do its thing with the expansion. These
29 ;;;; types (and corresponding predicates) are not maintained in this
30 ;;;; association. In this case, there need not be any predicate
31 ;;;; function unless it is required by the Common Lisp specification.
33 ;;;; The mapping between predicates and type structures is considered
34 ;;;; part of the backend; different backends can support different
35 ;;;; sets of predicates.
37 ;;; Establish an association between the type predicate NAME and the
38 ;;; corresponding TYPE. This causes the type predicate to be
39 ;;; recognized for purposes of optimization.
40 (defmacro define-type-predicate
(name type
)
41 `(%define-type-predicate
',name
',type
))
42 (defun %define-type-predicate
(name specifier
)
43 (let ((type (specifier-type specifier
)))
44 (setf (gethash name
*backend-predicate-types
*) type
)
45 (setf *backend-type-predicates
*
46 (cons (cons type name
)
47 (remove name
*backend-type-predicates
*
49 (%deftransform name
'(function (t) *) #'fold-type-predicate
)
54 ;;; If we discover the type argument is constant during IR1
55 ;;; optimization, then give the source transform another chance. The
56 ;;; source transform can't pass, since we give it an explicit
57 ;;; constant. At worst, it will convert to %TYPEP, which will prevent
58 ;;; spurious attempts at transformation (and possible repeated
60 (deftransform typep
((object type
&optional env
) * * :node node
)
61 (unless (constant-lvar-p type
)
62 (give-up-ir1-transform "can't open-code test of non-constant type"))
63 (unless (or (null env
)
64 (and (constant-lvar-p env
) (null (lvar-value env
))))
65 (give-up-ir1-transform "environment argument present and not null"))
66 (multiple-value-bind (expansion fail-p
)
67 (source-transform-typep 'object
(lvar-value type
))
72 ;;; If the lvar OBJECT definitely is or isn't of the specified
73 ;;; type, then return T or NIL as appropriate. Otherwise quietly
74 ;;; GIVE-UP-IR1-TRANSFORM.
75 (defun ir1-transform-type-predicate (object type node
)
76 (declare (type lvar object
) (type ctype type
))
77 (let ((otype (lvar-type object
)))
79 (cond ((typep type
'alien-type-type
)
80 ;; We don't transform alien type tests until here, because
81 ;; once we do that the rest of the type system can no longer
82 ;; reason about them properly -- so we'd miss out on type
84 (delay-ir1-transform node
:optimize
)
85 (let ((alien-type (alien-type-type-alien-type type
)))
86 ;; If it's a lisp-rep-type, the CTYPE should be one already.
87 (aver (not (compute-lisp-rep-type alien-type
)))
88 `(sb!alien
::alien-value-typep object
',alien-type
)))
90 (give-up-ir1-transform)))))
91 (cond ((not (types-equal-or-intersect otype type
))
93 ((csubtypep otype type
)
95 ((eq type
*empty-type
*)
98 (let ((intersect (type-intersection2 type otype
)))
101 (multiple-value-bind (constantp value
)
102 (type-singleton-p intersect
)
104 `(eql object
',value
)
107 ;;; Flush %TYPEP tests whose result is known at compile time.
108 (deftransform %typep
((object type
) * * :node node
)
109 (unless (constant-lvar-p type
)
110 (give-up-ir1-transform))
111 (ir1-transform-type-predicate
113 (ir1-transform-specifier-type (lvar-value type
))
116 ;;; This is the IR1 transform for simple type predicates. It checks
117 ;;; whether the single argument is known to (not) be of the
118 ;;; appropriate type, expanding to T or NIL as appropriate.
119 (deftransform fold-type-predicate
((object) * * :node node
:defun-only t
)
120 (let ((ctype (gethash (leaf-source-name
123 (basic-combination-fun node
))))
124 *backend-predicate-types
*)))
126 (ir1-transform-type-predicate object ctype node
)))
128 ;;; If FIND-CLASSOID is called on a constant class, locate the
129 ;;; CLASSOID-CELL at load time.
130 (deftransform find-classoid
((name) ((constant-arg symbol
)) *)
131 (let* ((name (lvar-value name
))
132 (cell (find-classoid-cell name
:create t
)))
133 `(or (classoid-cell-classoid ',cell
)
134 (error "class not yet defined: ~S" name
))))
136 (defoptimizer (%typep-wrapper constraint-propagate-if
)
137 ((test-value variable type
) node gen
)
138 (declare (ignore test-value gen
))
139 (aver (constant-lvar-p type
))
140 (let ((type (lvar-value type
)))
141 (values variable
(if (ctype-p type
)
143 (handler-case (careful-specifier-type type
)
146 (deftransform %typep-wrapper
((test-value variable type
) * * :node node
)
147 (aver (constant-lvar-p type
))
148 (if (constant-lvar-p test-value
)
149 `',(lvar-value test-value
)
150 (let* ((type (lvar-value type
))
151 (type (if (ctype-p type
)
153 (handler-case (careful-specifier-type type
)
155 (value-type (lvar-type variable
)))
158 ((csubtypep value-type type
)
160 ((not (types-equal-or-intersect value-type type
))
163 (delay-ir1-transform node
:constraint
)
166 ;;;; standard type predicates, i.e. those defined in package COMMON-LISP,
167 ;;;; plus at least one oddball (%INSTANCEP)
169 ;;;; Various other type predicates (e.g. low-level representation
170 ;;;; stuff like SIMPLE-ARRAY-SINGLE-FLOAT-P) are defined elsewhere.
172 ;;; FIXME: This function is only called once, at top level. Why not
173 ;;; just expand all its operations into toplevel code?
174 (defun !define-standard-type-predicates
()
175 (define-type-predicate arrayp array
)
176 ; (The ATOM predicate is handled separately as (NOT CONS).)
177 (define-type-predicate bit-vector-p bit-vector
)
178 (define-type-predicate characterp character
)
179 (define-type-predicate compiled-function-p compiled-function
)
180 (define-type-predicate complexp complex
)
181 (define-type-predicate complex-rational-p
(complex rational
))
182 (define-type-predicate complex-float-p
(complex float
))
183 (define-type-predicate consp cons
)
184 (define-type-predicate floatp float
)
185 (define-type-predicate functionp function
)
186 (define-type-predicate integerp integer
)
187 (define-type-predicate keywordp keyword
)
188 (define-type-predicate listp list
)
189 (define-type-predicate null null
)
190 (define-type-predicate numberp number
)
191 (define-type-predicate rationalp rational
)
192 (define-type-predicate realp real
)
193 (define-type-predicate sequencep sequence
)
194 (define-type-predicate extended-sequence-p extended-sequence
)
195 (define-type-predicate simple-bit-vector-p simple-bit-vector
)
196 (define-type-predicate simple-string-p simple-string
)
197 (define-type-predicate simple-vector-p simple-vector
)
198 (define-type-predicate stringp string
)
199 (define-type-predicate %instancep instance
)
200 (define-type-predicate simple-fun-p simple-fun
)
201 (define-type-predicate closurep closure
)
202 (define-type-predicate funcallable-instance-p funcallable-instance
)
203 (define-type-predicate symbolp symbol
)
204 (define-type-predicate vectorp vector
))
205 (!define-standard-type-predicates
)
207 ;;;; transforms for type predicates not implemented primitively
209 ;;;; See also VM dependent transforms.
211 (define-source-transform atom
(x)
214 (define-source-transform base-char-p
(x)
215 `(typep ,x
'base-char
))
216 ;; CONS is implemented as (and list (not (eql nil))) where the 'and' is
217 ;; built-in to the consp vop. Reduce to just LISTP if possible.
218 (deftransform consp
((x) ((not null
)) * :important nil
) '(listp x
))
220 ;;;; TYPEP source transform
222 ;;; Return a form that tests the variable N-OBJECT for being in the
223 ;;; binds specified by TYPE. BASE is the name of the base type, for
224 ;;; declaration. We make SAFETY locally 0 to inhibit any checking of
226 (defun transform-numeric-bound-test (n-object type base
)
227 (declare (type numeric-type type
))
228 (let ((low (numeric-type-low type
))
229 (high (numeric-type-high type
)))
231 (declare (optimize (safety 0)))
234 `((> (truly-the ,base
,n-object
) ,(car low
)))
235 `((>= (truly-the ,base
,n-object
) ,low
))))
238 `((< (truly-the ,base
,n-object
) ,(car high
)))
239 `((<= (truly-the ,base
,n-object
) ,high
))))))))
241 ;;; Do source transformation of a test of a known numeric type. We can
242 ;;; assume that the type doesn't have a corresponding predicate, since
243 ;;; those types have already been picked off. In particular, CLASS
244 ;;; must be specified, since it is unspecified only in NUMBER and
245 ;;; COMPLEX. Similarly, we assume that COMPLEXP is always specified.
247 ;;; For non-complex types, we just test that the number belongs to the
248 ;;; base type, and then test that it is in bounds. When CLASS is
249 ;;; INTEGER, we check to see whether the range is no bigger than
250 ;;; FIXNUM. If so, we check for FIXNUM instead of INTEGER. This allows
251 ;;; us to use fixnum comparison to test the bounds.
253 ;;; For complex types, we must test for complex, then do the above on
254 ;;; both the real and imaginary parts. When CLASS is float, we need
255 ;;; only check the type of the realpart, since the format of the
256 ;;; realpart and the imagpart must be the same.
257 (defun source-transform-numeric-typep (object type
)
258 (let* ((class (numeric-type-class type
))
260 (integer (containing-integer-type
261 (if (numeric-type-complexp type
)
262 (modified-numeric-type type
266 (float (or (numeric-type-format type
) 'float
))
268 (once-only ((n-object object
))
269 (ecase (numeric-type-complexp type
)
271 (if (and #!-
(or x86 x86-64 arm
) ;; Not implemented elsewhere yet
273 (eql (numeric-type-class type
) 'integer
)
274 (eql (numeric-type-low type
) 0)
275 (fixnump (numeric-type-high type
)))
276 `(fixnum-mod-p ,n-object
,(numeric-type-high type
))
277 `(and (typep ,n-object
',base
)
278 ,(transform-numeric-bound-test n-object type base
))))
280 `(and (complexp ,n-object
)
281 ,(once-only ((n-real `(realpart (truly-the complex
,n-object
)))
282 (n-imag `(imagpart (truly-the complex
,n-object
))))
285 (and (typep ,n-real
',base
)
286 ,@(when (eq class
'integer
)
287 `((typep ,n-imag
',base
)))
288 ,(transform-numeric-bound-test n-real type base
)
289 ,(transform-numeric-bound-test n-imag type
292 ;;; Do the source transformation for a test of a hairy type. AND,
293 ;;; SATISFIES and NOT are converted into the obvious code. We convert
294 ;;; unknown types to %TYPEP, emitting an efficiency note if
296 (defun source-transform-hairy-typep (object type
)
297 (declare (type hairy-type type
))
298 (let ((spec (hairy-type-specifier type
)))
299 (cond ((unknown-type-p type
)
300 (when (policy *lexenv
* (> speed inhibit-warnings
))
301 (compiler-notify "can't open-code test of unknown type ~S"
302 (type-specifier type
)))
303 `(%typep
,object
',spec
))
307 `(if (funcall (global-function ,(second spec
)) ,object
) t nil
))
309 (once-only ((n-obj object
))
310 `(,(first spec
) ,@(mapcar (lambda (x)
314 (defun source-transform-negation-typep (object type
)
315 (declare (type negation-type type
))
316 (let ((spec (type-specifier (negation-type-type type
))))
317 `(not (typep ,object
',spec
))))
319 ;;; Do source transformation for TYPEP of a known union type. If a
320 ;;; union type contains LIST, then we pull that out and make it into a
321 ;;; single LISTP call. Note that if SYMBOL is in the union, then LIST
322 ;;; will be a subtype even without there being any (member NIL). We
323 ;;; currently just drop through to the general code in this case,
324 ;;; rather than trying to optimize it (but FIXME CSR 2004-04-05: it
325 ;;; wouldn't be hard to optimize it after all).
326 ;;; FIXME: if the CONSP|NIL -> LISTP optimization kicks in,
327 ;;; we forgo the array optimizations.
328 (defun source-transform-union-typep (object type
)
329 (let* ((types (union-type-types type
))
330 (type-cons (specifier-type 'cons
))
331 (mtype (find-if #'member-type-p types
))
332 (members (when mtype
(member-type-members mtype
))))
333 (once-only ((n-obj object
))
336 (memq type-cons types
))
339 '(or ,@(mapcar #'type-specifier
341 (remove mtype types
)))
342 (member ,@(remove nil members
)))))
343 (multiple-value-bind (widetags more-types
)
344 (sb!kernel
::widetags-from-union-type types
)
346 `((%other-pointer-subtype-p
,n-obj
',widetags
)))
347 ,@(mapcar (lambda (x)
348 `(typep ,n-obj
',(type-specifier x
)))
351 ;;; Do source transformation for TYPEP of a known intersection type.
352 (defun source-transform-intersection-typep (object type
)
353 (once-only ((n-obj object
))
354 `(and ,@(mapcar (lambda (x)
355 `(typep ,n-obj
',(type-specifier x
)))
356 (intersection-type-types type
)))))
358 ;;; If necessary recurse to check the cons type.
359 (defun source-transform-cons-typep (object type
)
360 (let* ((car-type (cons-type-car-type type
))
361 (cdr-type (cons-type-cdr-type type
))
362 (car-test-p (not (type= car-type
*universal-type
*)))
363 (cdr-test-p (not (type= cdr-type
*universal-type
*))))
364 (if (and (not car-test-p
) (not cdr-test-p
))
366 ;; CONSP can be safely weakened to LISTP if either of the CAR
367 ;; or CDR test (or both) can distinguish LIST from CONS
368 ;; by never returning T when given an input of NIL.
369 (labels ((safely-weakened (ctype)
372 (not (member nil
(member-type-members ctype
))))
374 ;; can't weaken if the specifier is (CONS SYMBOL)
375 (not (ctypep nil ctype
)))
376 ;; these are disjoint from NIL
377 ((or cons-type numeric-type array-type character-set-type
)
380 ;; at least one of them must not spuriously return T
381 (some #'safely-weakened
(compound-type-types ctype
)))
383 ;; require that none spuriously return T
384 (every #'safely-weakened
(compound-type-types ctype
)))
386 ;; hack - (CONS KEYWORD) is weakenable
387 ;; because NIL is not a keyword.
388 (equal (hairy-type-specifier ctype
)
389 '(satisfies keywordp
))))))
390 (let* ((n-obj (sb!xc
:gensym
))
393 `((typep (car ,n-obj
) ',(type-specifier car-type
)))))
396 `((typep (cdr ,n-obj
) ',(type-specifier cdr-type
))))))
397 `(let ((,n-obj
,object
))
398 ;; Being paranoid, perform the safely weakenable test first
399 ;; so that the other part doesn't execute on an object that
400 ;; it would not have gotten, were the CONSP test not weakened.
401 ,(cond ((and car-test-p
(safely-weakened car-type
))
402 `(and (listp ,n-obj
) ,@car-test
,@cdr-test
))
403 ((and cdr-test-p
(safely-weakened cdr-type
))
404 `(and (listp ,n-obj
) ,@cdr-test
,@car-test
))
406 `(and (consp ,n-obj
) ,@car-test
,@cdr-test
)))))))))
408 (defun source-transform-character-set-typep (object type
)
409 (let ((pairs (character-set-type-pairs type
)))
410 (if (and (= (length pairs
) 1)
412 (= (cdar pairs
) (1- sb
!xc
:char-code-limit
)))
413 `(characterp ,object
)
414 (once-only ((n-obj object
))
415 (let ((n-code (gensym "CODE")))
416 `(and (characterp ,n-obj
)
417 (let ((,n-code
(sb!xc
:char-code
,n-obj
)))
419 ,@(loop for pair in pairs
421 `(<= ,(car pair
) ,n-code
,(cdr pair
)))))))))))
424 (defun source-transform-simd-pack-typep (object type
)
425 (if (type= type
(specifier-type 'simd-pack
))
426 `(simd-pack-p ,object
)
427 (once-only ((n-obj object
))
428 (let ((n-tag (gensym "TAG")))
431 (let ((,n-tag
(%simd-pack-tag
,n-obj
)))
433 for type in
(simd-pack-type-element-type type
)
434 for index
= (position type
*simd-pack-element-types
*)
435 collect
`(eql ,n-tag
,index
)))))))))
437 ;;; Return the predicate and type from the most specific entry in
438 ;;; *TYPE-PREDICATES* that is a supertype of TYPE.
439 (defun find-supertype-predicate (type)
440 (declare (type ctype type
))
443 (dolist (x *backend-type-predicates
*)
444 (let ((stype (car x
)))
445 (when (and (csubtypep type stype
)
447 (csubtypep stype res-type
)))
448 (setq res-type stype
)
449 (setq res
(cdr x
)))))
450 (values res res-type
)))
452 ;;; Return forms to test that OBJ has the rank and dimensions
453 ;;; specified by TYPE, where STYPE is the type we have checked against
454 ;;; (which is the same but for dimensions and element type).
456 ;;; Secondary return value is true if passing the generated tests implies that
457 ;;; the array has a header.
458 (defun test-array-dimensions (obj type stype
)
459 (declare (type array-type type stype
))
460 (let ((obj `(truly-the ,(type-specifier stype
) ,obj
))
461 (dims (array-type-dimensions type
)))
462 (unless (or (eq dims
'*)
463 (equal dims
(array-type-dimensions stype
)))
465 (values `((array-header-p ,obj
)
466 ,@(when (eq (array-type-dimensions stype
) '*)
467 `((= (%array-rank
,obj
) ,(length dims
))))
468 ,@(loop for d in dims
471 collect
`(= (%array-dimension
,obj
,i
) ,d
)))
474 (values `((array-header-p ,obj
)
475 (= (%array-rank
,obj
) 0))
477 ((not (array-type-complexp type
))
478 (if (csubtypep stype
(specifier-type 'vector
))
479 (values (unless (eq '* (car dims
))
480 `((= (vector-length ,obj
) ,@dims
)))
482 (values (if (eq '* (car dims
))
483 `((not (array-header-p ,obj
)))
484 `((not (array-header-p ,obj
))
485 (= (vector-length ,obj
) ,@dims
)))
488 (values (unless (eq '* (car dims
))
489 `((if (array-header-p ,obj
)
490 (= (%array-dimension
,obj
0) ,@dims
)
491 (= (vector-length ,obj
) ,@dims
))))
494 ;;; Return forms to test that OBJ has the element-type specified by type
495 ;;; specified by TYPE, where STYPE is the type we have checked against (which
496 ;;; is the same but for dimensions and element type). If HEADERP is true, OBJ
497 ;;; is guaranteed to be an array-header.
498 (defun test-array-element-type (obj type stype headerp
)
499 (declare (type array-type type stype
))
500 (let ((obj `(truly-the ,(type-specifier stype
) ,obj
))
501 (eltype (array-type-specialized-element-type type
)))
502 (unless (or (type= eltype
(array-type-specialized-element-type stype
))
503 (eq eltype
*wild-type
*))
504 (let ((typecode (sb!vm
:saetp-typecode
(find-saetp-by-ctype eltype
))))
505 (with-unique-names (data)
506 (if (and headerp
(not (array-type-complexp stype
)))
507 ;; If we know OBJ is an array header, and that the array is
508 ;; simple, we also know there is exactly one indirection to
510 `((eq (%other-pointer-widetag
(%array-data-vector
,obj
)) ,typecode
))
511 `((do ((,data
,(if headerp
`(%array-data-vector
,obj
) obj
)
512 (%array-data-vector
,data
)))
513 ((not (array-header-p ,data
))
514 (eq (%other-pointer-widetag
,data
) ,typecode
))))))))))
516 ;;; If we can find a type predicate that tests for the type without
517 ;;; dimensions, then use that predicate and test for dimensions.
518 ;;; Otherwise, just do %TYPEP.
519 (defun source-transform-array-typep (obj type
)
520 ;; Intercept (SIMPLE-ARRAY * (*)) because otherwise it tests
521 ;; (AND SIMPLE-ARRAY (NOT ARRAY-HEADER)) to weed out rank 0 and >1.
522 ;; By design the simple arrays of of rank 1 occupy a contiguous
523 ;; range of widetags, and unlike the arbitrary-widetags code for unions,
524 ;; this nonstandard predicate can be generically defined for all backends.
525 (if (and (not (array-type-complexp type
))
526 (eq (array-type-element-type type
) *wild-type
*)
527 (equal (array-type-dimensions type
) '(*)))
528 (return-from source-transform-array-typep
529 `(simple-rank-1-array-*-p
,obj
)))
530 (multiple-value-bind (pred stype
) (find-supertype-predicate type
)
531 (if (and (array-type-p stype
)
532 ;; (If the element type hasn't been defined yet, it's
533 ;; not safe to assume here that it will eventually
534 ;; have (UPGRADED-ARRAY-ELEMENT-TYPE type)=T, so punt.)
535 (not (unknown-type-p (array-type-element-type type
)))
536 (or (eq (array-type-complexp stype
) (array-type-complexp type
))
537 (and (eql (array-type-complexp stype
) :maybe
)
538 (eql (array-type-complexp type
) t
))))
539 (once-only ((n-obj obj
))
540 (multiple-value-bind (tests headerp
)
541 (test-array-dimensions n-obj type stype
)
543 ,@(when (and (eql (array-type-complexp stype
) :maybe
)
544 (eql (array-type-complexp type
) t
))
545 ;; KLUDGE: this is a bit lame; if we get here,
546 ;; we already know that N-OBJ is an array, but
547 ;; (NOT SIMPLE-ARRAY) doesn't know that. On the
548 ;; other hand, this should get compiled down to
549 ;; two widetag tests, so it's only a bit lame.
550 `((typep ,n-obj
'(not simple-array
))))
552 ,@(test-array-element-type n-obj type stype headerp
))))
553 `(%typep
,obj
',(type-specifier type
)))))
555 ;;; Transform a type test against some instance type. The type test is
556 ;;; flushed if the result is known at compile time. If not properly
557 ;;; named, error. If sealed and has no subclasses, just test for
558 ;;; layout-EQ. If a structure then test for layout-EQ and then a
559 ;;; general test based on layout-inherits. If safety is important,
560 ;;; then we also check whether the layout for the object is invalid
561 ;;; and signal an error if so. Otherwise, look up the indirect
562 ;;; class-cell and call CLASS-CELL-TYPEP at runtime.
563 (deftransform %instance-typep
((object spec
) (* *) * :node node
)
564 (aver (constant-lvar-p spec
))
565 (let* ((spec (lvar-value spec
))
566 (class (specifier-type spec
))
567 (name (classoid-name class
))
568 (otype (lvar-type object
))
569 (layout (let ((res (info :type
:compiler-layout name
)))
570 (if (and res
(not (layout-invalid res
)))
574 ;; Flush tests whose result is known at compile time.
575 ((not (types-equal-or-intersect otype class
))
577 ((csubtypep otype class
)
579 ;; If not properly named, error.
580 ((not (and name
(eq (find-classoid name
) class
)))
581 (compiler-error "can't compile TYPEP of anonymous or undefined ~
585 ;; Delay the type transform to give type propagation a chance.
586 (delay-ir1-transform node
:constraint
)
588 ;; Otherwise transform the type test.
589 (binding* (((pred get-layout
)
590 (cond ((csubtypep class
(specifier-type 'funcallable-instance
))
591 (values '(funcallable-instance-p object
)
592 '(%funcallable-instance-layout object
)))
593 ((csubtypep class
(specifier-type 'instance
))
594 (values '(%instancep object
)
595 '(%instance-layout object
)))))
596 (get-layout-or-return-false
598 ;; Test just one of %INSTANCEP or %FUNCALLABLE-INSTANCE-P
599 `(if ,pred
,get-layout
(return-from typep nil
))
600 ;; But if we don't know which is will be, try both.
601 ;; This is less general than LAYOUT-OF,and therefore
602 ;; a little quicker to fail, because objects with
603 ;; {LIST|OTHER}-POINTER-LOWTAG can't possibly pass.
604 `(cond ((%instancep object
)
605 (%instance-layout object
))
606 ((funcallable-instance-p object
)
607 (%funcallable-instance-layout object
))
608 (t (return-from typep nil
)))))
611 ;; It's possible to seal a STANDARD-CLASS, not just a STRUCTURE-CLASS,
612 ;; though probably extremely weird. Also the PRED should be set in
613 ;; that event, but it isn't.
614 ((and (eq (classoid-state class
) :sealed
) layout
615 (not (classoid-subclasses class
)))
616 ;; Sealed and has no subclasses.
617 ;; The crummy dual expressions for the same result are because
618 ;; (BLOCK (RETURN ...)) seems to emit a forward branch in the
619 ;; passing case, but AND emits a forward branch in the failing
620 ;; case which I believe is the better choice.
622 `(and ,pred
(eq ,get-layout
',layout
))
623 `(block typep
(eq ,get-layout-or-return-false
',layout
))))
625 ((and (typep class
'structure-classoid
) layout
)
626 ;; structure type tests; hierarchical layout depths
627 (let* ((depthoid (layout-depthoid layout
))
628 ;; If a structure is apparently an abstract base type,
629 ;; having no constructor, then no instance layout should
630 ;; be EQ to the classoid's layout. It is a slight win
631 ;; to use the depth-based check first, then do the EQ check.
632 ;; There is no loss in the case where both fail, and there
633 ;; is a benefit in a passing case. Always try both though,
634 ;; because (MAKE-INSTANCE 'x) works on any structure class.
636 (let ((dd (layout-info layout
)))
638 (not (dd-default-constructor dd
))
639 (let ((ctors (dd-constructors dd
)))
640 (or (not ctors
) (equal ctors
'((nil))))))))
642 ;; Use DATA-VECTOR-REF directly, since that's what SVREF in
643 ;; a SAFETY 0 lexenv will eventually be transformed to.
644 ;; This can give a large compilation speedup, since
645 ;; %INSTANCE-TYPEPs are frequently created during
646 ;; GENERATE-TYPE-CHECKS, and the normal aref transformation
647 ;; path is pretty heavy.
648 `(and (> (layout-depthoid ,n-layout
) ,depthoid
)
649 (locally (declare (optimize (safety 0)))
650 (eq (data-vector-ref (layout-inherits ,n-layout
)
653 (aver (equal pred
'(%instancep object
)))
655 (let ((,n-layout
,get-layout
))
656 ;; we used to check for invalid layouts here,
657 ;; but in fact that's both unnecessary and
658 ;; wrong; it's unnecessary because structure
659 ;; classes can't be redefined, and it's wrong
660 ;; because it is quite legitimate to pass an
661 ;; object with an invalid layout to a structure
664 `(if ,hierarchy-check t
(eq ,n-layout
',layout
))
665 `(if (eq ,n-layout
',layout
) t
,hierarchy-check
))))))
667 ((and layout
(>= (layout-depthoid layout
) 0))
668 ;; hierarchical layout depths for other things (e.g.
669 ;; CONDITION, STREAM)
670 ;; The quasi-hierarchical types are abstract base types,
671 ;; so perform inheritance check first, and EQ second.
672 ;; Actually, since you can't make an abstract STREAM,
673 ;; maybe we should skip the EQ test? But you *can* make
674 ;; an instance of CONDITION for what it's worth.
675 ;; SEQUENCE is special-cased, but could be handled here.
676 (let* ((depthoid (layout-depthoid layout
))
677 (n-inherits (gensym))
679 `((when (layout-invalid ,n-layout
)
680 (setq ,n-layout
(update-object-layout-or-invalid
682 (if (let ((,n-inherits
(layout-inherits
683 (truly-the layout
,n-layout
))))
684 (declare (optimize (safety 0)))
685 (and (> (vector-length ,n-inherits
) ,depthoid
)
687 (eq (data-vector-ref ,n-inherits
,depthoid
)
690 (eq ,n-layout
',layout
)))))
692 `(and ,pred
(let ((,n-layout
,get-layout
)) ,@guts
))
694 (let ((,n-layout
,get-layout-or-return-false
)) ,@guts
)))))
697 (/noshow
"default case -- ,PRED and CLASS-CELL-TYPEP")
699 (classoid-cell-typep ,get-layout-or-return-false
700 ',(find-classoid-cell name
:create t
)
703 ;;; If the specifier argument is a quoted constant, then we consider
704 ;;; converting into a simple predicate or other stuff. If the type is
705 ;;; constant, but we can't transform the call, then we convert to
706 ;;; %TYPEP. We only pass when the type is non-constant. This allows us
707 ;;; to recognize between calls that might later be transformed
708 ;;; successfully when a constant type is discovered. We don't give an
709 ;;; efficiency note when we pass, since the IR1 transform will give
710 ;;; one if necessary and appropriate.
712 ;;; If the type is TYPE= to a type that has a predicate, then expand
713 ;;; to that predicate. Otherwise, we dispatch off of the type's type.
714 ;;; These transformations can increase space, but it is hard to tell
715 ;;; when, so we ignore policy and always do them.
716 (defun %source-transform-typep
(object type
)
717 (let ((ctype (careful-specifier-type type
)))
718 (or (when (not ctype
)
719 (compiler-warn "illegal type specifier for TYPEP: ~S" type
)
720 (return-from %source-transform-typep
(values nil t
)))
721 (multiple-value-bind (constantp value
) (type-singleton-p ctype
)
723 `(eql ,object
',value
)))
724 (let ((pred (cdr (assoc ctype
*backend-type-predicates
*
726 (when pred
`(,pred
,object
)))
729 (source-transform-hairy-typep object ctype
))
731 (source-transform-negation-typep object ctype
))
733 (source-transform-union-typep object ctype
))
735 (source-transform-intersection-typep object ctype
))
737 `(if (member ,object
',(member-type-members ctype
)) t
))
739 (compiler-warn "illegal type specifier for TYPEP: ~S" type
)
740 (return-from %source-transform-typep
(values nil t
)))
744 (source-transform-numeric-typep object ctype
))
746 `(%instance-typep
,object
',type
))
748 (source-transform-array-typep object ctype
))
750 (source-transform-cons-typep object ctype
))
752 (source-transform-character-set-typep object ctype
))
755 (source-transform-simd-pack-typep object ctype
))
757 `(%typep
,object
',type
))))
759 (defun source-transform-typep (object type
)
760 (let ((name (gensym "OBJECT")))
761 (multiple-value-bind (transform error
)
762 (%source-transform-typep name type
)
765 (values `(let ((,name
,object
))
766 (%typep-wrapper
,transform
,name
',type
)))))))
768 (define-source-transform typep
(object spec
&optional env
)
769 ;; KLUDGE: It looks bad to only do this on explicitly quoted forms,
770 ;; since that would overlook other kinds of constants. But it turns
771 ;; out that the DEFTRANSFORM for TYPEP detects any constant
772 ;; lvar, transforms it into a quoted form, and gives this
773 ;; source transform another chance, so it all works out OK, in a
774 ;; weird roundabout way. -- WHN 2001-03-18
777 (eq (car spec
) 'quote
))
778 (source-transform-typep object
(cadr spec
))
783 ;;; Constant-folding.
786 (defoptimizer (coerce optimizer
) ((x type
) node
)
787 (when (and (constant-lvar-p x
) (constant-lvar-p type
))
788 (let ((value (lvar-value x
)))
789 (when (or (numberp value
) (characterp value
))
790 (constant-fold-call node
)
793 ;;; Drops dimension information from vector types.
794 (defun simplify-vector-type (type)
795 (aver (csubtypep type
(specifier-type '(array * (*)))))
797 (if (csubtypep type
(specifier-type 'simple-array
))
802 (or (eq 'simple-array array-type
)
804 (type-intersection type
(specifier-type 'simple-array
)))))))
806 #+sb-xc-host
'(t bit character
)
807 #-sb-xc-host sb
!kernel
::*specialized-array-element-types
*
808 #+sb-xc-host
(values nil nil nil
)
809 #-sb-xc-host
(values `(,array-type
* (*)) t complexp
))
811 (let ((simplified (specifier-type `(,array-type
,etype
(*)))))
812 (when (csubtypep type simplified
)
813 (return (values (type-specifier simplified
)
817 (deftransform coerce
((x type
) (* *) * :node node
)
818 (unless (constant-lvar-p type
)
819 (give-up-ir1-transform))
820 (let* ((tval (lvar-value type
))
821 (tspec (ir1-transform-specifier-type tval
)))
822 (if (csubtypep (lvar-type x
) tspec
)
824 ;; Note: The THE forms we use to wrap the results make sure that
825 ;; specifiers like (SINGLE-FLOAT 0.0 1.0) can raise a TYPE-ERROR.
827 ((csubtypep tspec
(specifier-type 'double-float
))
828 `(the ,tval
(%double-float x
)))
829 ;; FIXME: #!+long-float (t ,(error "LONG-FLOAT case needed"))
830 ((csubtypep tspec
(specifier-type 'float
))
831 `(the ,tval
(%single-float x
)))
832 ((csubtypep tspec
(specifier-type 'complex
))
833 (multiple-value-bind (part-type result-type
)
834 (cond ((and (numeric-type-p tspec
)
835 (numeric-type-format tspec
))) ; specific FLOAT type
836 ((csubtypep tspec
(specifier-type '(complex float
)))
837 ;; unspecific FLOAT type
839 ((csubtypep tspec
(specifier-type '(complex rational
)))
840 (values 'rational
`(or ,tval rational
)))
842 (values t
`(or ,tval rational
))))
843 (let ((result-type (or result-type tval
)))
845 ((not (typep x
'complex
))
846 (the ,result-type
(complex (coerce x
',part-type
))))
849 (t ; X is COMPLEX, but not of the requested type
851 (complex (coerce (realpart x
) ',part-type
)
852 (coerce (imagpart x
) ',part-type
))))))))
853 ;; Special case STRING and SIMPLE-STRING as they are union types
855 ((member tval
'(string simple-string
))
859 (replace (make-array (length x
) :element-type
'character
) x
))))
860 ((eq tval
'character
)
862 ;; Special case VECTOR
867 (replace (make-array (length x
)) x
))))
868 ;; Handle specialized element types for 1D arrays.
869 ((csubtypep tspec
(specifier-type '(array * (*))))
870 ;; Can we avoid checking for dimension issues like (COERCE FOO
871 ;; '(SIMPLE-VECTOR 5)) returning a vector of length 6?
873 ;; CLHS actually allows this for all code with SAFETY < 3,
874 ;; but we're a conservative bunch.
875 (if (or (policy node
(zerop safety
)) ; no need in unsafe code
876 (and (array-type-p tspec
) ; no need when no dimensions
877 (equal (array-type-dimensions tspec
) '(*))))
879 (multiple-value-bind (vtype etype complexp
) (simplify-vector-type tspec
)
881 (give-up-ir1-transform))
883 (if (typep x
',vtype
)
886 (make-array (length x
) :element-type
',etype
888 (list :fill-pointer t
891 ;; No, duh. Dimension checking required.
892 (give-up-ir1-transform
893 "~@<~S specifies dimensions other than (*) in safe code.~:@>"
895 ((type= tspec
(specifier-type 'list
))
897 ((csubtypep tspec
(specifier-type 'function
))
898 (if (csubtypep (lvar-type x
) (specifier-type 'symbol
))
899 `(coerce-symbol-to-fun x
)
902 (give-up-ir1-transform
903 "~@<open coding coercion to ~S not implemented.~:@>"