| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; Copyright (c) 2011, Juan Jose Garcia-Ripoll |
| 6 |
;;;; |
| 7 |
;;;; See file 'LICENSE' for the copyright details. |
| 8 |
|
| 9 |
;;;; CMPTYPE-ASSERT Type assertions automatically generated |
| 10 |
|
| 11 |
(in-package "COMPILER") |
| 12 |
|
| 13 |
(defun c1compiler-typecase (args) |
| 14 |
(let ((form (first args))) |
| 15 |
(multiple-value-bind (constantp value) |
| 16 |
(constant-value-p form *cmp-env*) |
| 17 |
(when constantp |
| 18 |
(loop for (type . forms) in (rest args) |
| 19 |
when (typep value type *cmp-env*) |
| 20 |
do (return-from c1compiler-typecase (c1progn forms)) |
| 21 |
finally (baboon :format-control "COMPILER-TYPECASE form missing a T statement"))))) |
| 22 |
(let* ((var-name (pop args)) |
| 23 |
(var (c1vref var-name)) |
| 24 |
(first-case (car args))) |
| 25 |
;; If the first type, which is supposedly the most specific |
| 26 |
;; already includes the form, we keep it. This optimizes |
| 27 |
;; most cases of CHECKED-VALUE. |
| 28 |
(if (subtypep (var-type var) (car first-case) *cmp-env*) |
| 29 |
(c1progn (cdr first-case)) |
| 30 |
(let* ((types '()) |
| 31 |
(expressions (loop for (type . forms) in args |
| 32 |
for c1form = (c1progn forms) |
| 33 |
for c1form-type = (c1form-primary-type c1form) |
| 34 |
do (push c1form-type types) |
| 35 |
collect (list type c1form)))) |
| 36 |
(make-c1form* 'EXT:COMPILER-TYPECASE |
| 37 |
:type (reduce #'type-or types) |
| 38 |
:args var expressions))))) |
| 39 |
|
| 40 |
(defun c2compiler-typecase (c1form var expressions) |
| 41 |
(declare (ignore c1form)) |
| 42 |
(loop with var-type = (var-type var) |
| 43 |
for (type form) in expressions |
| 44 |
when (or (member type '(t otherwise)) |
| 45 |
(subtypep var-type type *cmp-env*)) |
| 46 |
return (c2expr form))) |
| 47 |
|
| 48 |
(defconstant +simple-type-assertions+ |
| 49 |
'((cons . "if (ecl_unlikely(ECL_ATOM(#0))) FEtype_error_cons(#0);") |
| 50 |
(array . "if (ecl_unlikely(!ECL_ARRAYP(#0))) FEtype_error_array(#0);") |
| 51 |
(list . "if (ecl_unlikely(!ECL_LISTP(#0))) FEtype_error_list(#0);") |
| 52 |
(sequence . "if (ecl_unlikely(!(ECL_LISTP(#0) || ECL_VECTORP(#0)))) |
| 53 |
FEtype_error_sequence(#0);") |
| 54 |
(vector . "if (ecl_unlikely(!ECL_VECTORP(#0))) FEtype_error_vector(#0);"))) |
| 55 |
|
| 56 |
(defun simple-type-assertion (value type) |
| 57 |
(let ((simple-form (cdr (assoc type +simple-type-assertions+)))) |
| 58 |
(if simple-form |
| 59 |
`(ffi:c-inline (,value) (:object) :void ,simple-form |
| 60 |
:one-liner nil) |
| 61 |
`(ffi:c-inline |
| 62 |
((typep ,value ',(si::flatten-function-types type *cmp-env*)) ',type ,value) |
| 63 |
(:bool :object :object) :void |
| 64 |
"if (ecl_unlikely(!(#0))) |
| 65 |
FEwrong_type_argument(#1,#2);" :one-liner nil)))) |
| 66 |
|
| 67 |
(defun expand-type-assertion (value type env compulsory) |
| 68 |
(cond ((or (not (symbolp value)) |
| 69 |
(special-variable-p value) |
| 70 |
(symbol-macro-p value)) |
| 71 |
;; If multiple references to the value cost time and space, |
| 72 |
;; or may cause side effects, we save it. |
| 73 |
(ext:with-clean-symbols (%asserted-value) |
| 74 |
`(let* ((%asserted-value ,value)) |
| 75 |
(declare (:read-only %asserted-value)) |
| 76 |
,(expand-type-assertion '%asserted-value type env compulsory)))) |
| 77 |
(compulsory |
| 78 |
;; The check has to be produced, independent of the declared |
| 79 |
;; value of the variable (for instance, in LAMBDA arguments). |
| 80 |
(simple-type-assertion value type)) |
| 81 |
(t |
| 82 |
;; We may rely on the compiler to choose the appropriate |
| 83 |
;; branch once type propagation has happened. |
| 84 |
`(ext:compiler-typecase ,value |
| 85 |
(,type) |
| 86 |
(t ,(simple-type-assertion value type)))))) |
| 87 |
|
| 88 |
(defun c1checked-value (args) |
| 89 |
(let* ((type (pop args)) |
| 90 |
(value (pop args)) |
| 91 |
form form-type and-type) |
| 92 |
(cond ((or (trivial-type-p type) (not (policy-type-assertions))) |
| 93 |
value) |
| 94 |
((and (consp type) |
| 95 |
(eq (first type) 'values)) |
| 96 |
(c1checked-value (list (values-type-primary-type type) |
| 97 |
value))) |
| 98 |
((and (policy-evaluate-forms) (constantp value *cmp-env*)) |
| 99 |
(if (typep (ext:constant-form-value value *cmp-env*) |
| 100 |
(si::flatten-function-types type *cmp-env*) |
| 101 |
*cmp-env*) |
| 102 |
value |
| 103 |
(progn |
| 104 |
;; warn and generate error. |
| 105 |
(cmpwarn "Failed type assertion for value ~A and type ~A" |
| 106 |
value type) |
| 107 |
(c1expr `(error 'simple-type-error |
| 108 |
:datum ,value |
| 109 |
:expected-type ',type |
| 110 |
:format-control "The constant value ~S is not a ~S" |
| 111 |
:format-arguments (list ,value ',type)))))) |
| 112 |
;; Is the form type contained in the test? |
| 113 |
((progn |
| 114 |
(setf form (c1expr value) |
| 115 |
form-type (c1form-primary-type form) |
| 116 |
and-type (type-and form-type type)) |
| 117 |
(eq and-type form-type)) |
| 118 |
form) |
| 119 |
;; Otherwise, emit a full test |
| 120 |
(t |
| 121 |
(if (null and-type) ; Are the form type and the test disjoint types? |
| 122 |
(cmpwarn "The expression ~S is not of the expected type ~S" |
| 123 |
value type) |
| 124 |
(cmpdebug "Checking type of ~S to be ~S" value type)) |
| 125 |
(let ((full-check |
| 126 |
(ext:with-clean-symbols (%checked-value) |
| 127 |
`(let* ((%checked-value ,value)) |
| 128 |
(declare (:read-only %checked-value)) |
| 129 |
,(expand-type-assertion '%checked-value type *cmp-env* nil) |
| 130 |
,(if (null and-type) |
| 131 |
'%checked-value |
| 132 |
`(ext:truly-the ,type %checked-value)))))) |
| 133 |
(make-c1form* 'ext:CHECKED-VALUE |
| 134 |
:type type |
| 135 |
:args type form (c1expr full-check))))))) |
| 136 |
|
| 137 |
(defun c2checked-value (c1form type value let-form) |
| 138 |
(declare (ignore c1form)) |
| 139 |
(c2expr (if (subtypep (c1form-primary-type value) type *cmp-env*) |
| 140 |
value |
| 141 |
let-form))) |
| 142 |
|
| 143 |
(defmacro optional-type-assertion (value type &environment env) |
| 144 |
"If safety settings are high enough, generates a type check on an |
| 145 |
expression, ensuring that it is satisfied." |
| 146 |
(when (and (policy-type-assertions env) |
| 147 |
(not (trivial-type-p type))) |
| 148 |
(cmpdebug "Checking type of ~A to be ~A" value type) |
| 149 |
`(ext:checked-value ,type ,value))) |
| 150 |
|
| 151 |
(defmacro type-assertion (value type &environment env) |
| 152 |
"Generates a type check on an expression, ensuring that it is satisfied." |
| 153 |
(cmpdebug "Checking type of ~A to be ~A" value type) |
| 154 |
(unless (trivial-type-p type) |
| 155 |
(expand-type-assertion value type env t))) |