| 1 | ;;; proclaim.lisp |
|---|
| 2 | ;;; |
|---|
| 3 | ;;; Copyright (C) 2003-2006 Peter Graves |
|---|
| 4 | ;;; $Id: proclaim.lisp 11391 2008-11-15 22:38:34Z vvoutilainen $ |
|---|
| 5 | ;;; |
|---|
| 6 | ;;; This program is free software; you can redistribute it and/or |
|---|
| 7 | ;;; modify it under the terms of the GNU General Public License |
|---|
| 8 | ;;; as published by the Free Software Foundation; either version 2 |
|---|
| 9 | ;;; of the License, or (at your option) any later version. |
|---|
| 10 | ;;; |
|---|
| 11 | ;;; This program is distributed in the hope that it will be useful, |
|---|
| 12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | ;;; GNU General Public License for more details. |
|---|
| 15 | ;;; |
|---|
| 16 | ;;; You should have received a copy of the GNU General Public License |
|---|
| 17 | ;;; along with this program; if not, write to the Free Software |
|---|
| 18 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 19 | ;;; |
|---|
| 20 | ;;; As a special exception, the copyright holders of this library give you |
|---|
| 21 | ;;; permission to link this library with independent modules to produce an |
|---|
| 22 | ;;; executable, regardless of the license terms of these independent |
|---|
| 23 | ;;; modules, and to copy and distribute the resulting executable under |
|---|
| 24 | ;;; terms of your choice, provided that you also meet, for each linked |
|---|
| 25 | ;;; independent module, the terms and conditions of the license of that |
|---|
| 26 | ;;; module. An independent module is a module which is not derived from |
|---|
| 27 | ;;; or based on this library. If you modify this library, you may extend |
|---|
| 28 | ;;; this exception to your version of the library, but you are not |
|---|
| 29 | ;;; obligated to do so. If you do not wish to do so, delete this |
|---|
| 30 | ;;; exception statement from your version. |
|---|
| 31 | |
|---|
| 32 | (in-package #:system) |
|---|
| 33 | |
|---|
| 34 | (export '(check-declaration-type proclaimed-type proclaimed-ftype ftype-result-type)) |
|---|
| 35 | |
|---|
| 36 | (defmacro declaim (&rest decls) |
|---|
| 37 | `(eval-when (:compile-toplevel :load-toplevel :execute) |
|---|
| 38 | ,@(mapcar (lambda (decl) `(proclaim ',decl)) |
|---|
| 39 | decls))) |
|---|
| 40 | |
|---|
| 41 | (defun declaration-error (name) |
|---|
| 42 | (error 'simple-error |
|---|
| 43 | :format-control "The symbol ~S cannot be both the name of a type and the name of a declaration." |
|---|
| 44 | :format-arguments (list name))) |
|---|
| 45 | |
|---|
| 46 | (defvar *declaration-types* (make-hash-table :test 'eq)) |
|---|
| 47 | |
|---|
| 48 | ;; "A symbol cannot be both the name of a type and the name of a declaration. |
|---|
| 49 | ;; Defining a symbol as the name of a class, structure, condition, or type, |
|---|
| 50 | ;; when the symbol has been declared as a declaration name, or vice versa, |
|---|
| 51 | ;; signals an error." |
|---|
| 52 | (defun check-declaration-type (name) |
|---|
| 53 | (when (gethash1 name (the hash-table *declaration-types*)) |
|---|
| 54 | (declaration-error name))) |
|---|
| 55 | |
|---|
| 56 | (defun proclaim (declaration-specifier) |
|---|
| 57 | (unless (symbolp (car declaration-specifier)) |
|---|
| 58 | (%type-error (car declaration-specifier) 'symbol)) |
|---|
| 59 | ;; (cdr declaration-specifier) must be a proper list. |
|---|
| 60 | (unless (listp (cddr declaration-specifier)) |
|---|
| 61 | (%type-error (cddr declaration-specifier) 'list)) |
|---|
| 62 | (case (car declaration-specifier) |
|---|
| 63 | (SPECIAL |
|---|
| 64 | (dolist (name (cdr declaration-specifier)) |
|---|
| 65 | (%defvar name))) |
|---|
| 66 | (OPTIMIZE |
|---|
| 67 | (dolist (spec (cdr declaration-specifier)) |
|---|
| 68 | (let ((val 3) |
|---|
| 69 | (quality spec)) |
|---|
| 70 | (when (consp spec) |
|---|
| 71 | (setf quality (%car spec) |
|---|
| 72 | val (cadr spec))) |
|---|
| 73 | (when (and (fixnump val) |
|---|
| 74 | (<= 0 val 3)) |
|---|
| 75 | (case quality |
|---|
| 76 | (SPEED |
|---|
| 77 | (setf *speed* val)) |
|---|
| 78 | (SPACE |
|---|
| 79 | (setf *space* val)) |
|---|
| 80 | (SAFETY |
|---|
| 81 | (setf *safety* val)) |
|---|
| 82 | (DEBUG |
|---|
| 83 | (setf *debug* val))))))) |
|---|
| 84 | (FTYPE |
|---|
| 85 | (unless (cdr declaration-specifier) |
|---|
| 86 | (error "No type specified in FTYPE declaration: ~S" declaration-specifier)) |
|---|
| 87 | (apply 'proclaim-ftype (cdr declaration-specifier))) |
|---|
| 88 | (TYPE |
|---|
| 89 | (unless (cdr declaration-specifier) |
|---|
| 90 | (error "No type specified in TYPE declaration: ~S" declaration-specifier)) |
|---|
| 91 | (apply 'proclaim-type (cdr declaration-specifier))) |
|---|
| 92 | ((INLINE NOTINLINE) |
|---|
| 93 | (dolist (name (cdr declaration-specifier)) |
|---|
| 94 | (when (symbolp name) ; FIXME Need to support non-symbol function names. |
|---|
| 95 | (setf (get name '%inline) (car declaration-specifier))))) |
|---|
| 96 | (DECLARATION |
|---|
| 97 | (dolist (name (cdr declaration-specifier)) |
|---|
| 98 | (when (or (get name 'deftype-definition) |
|---|
| 99 | (find-class name nil)) |
|---|
| 100 | (declaration-error name)) |
|---|
| 101 | (setf (gethash name (the hash-table *declaration-types*)) name))) |
|---|
| 102 | (:explain |
|---|
| 103 | (dolist (spec (cdr declaration-specifier)) |
|---|
| 104 | (let ((val t) |
|---|
| 105 | (quality spec)) |
|---|
| 106 | (when (consp spec) |
|---|
| 107 | (setf quality (%car spec)) |
|---|
| 108 | (when (= (length spec) 2) |
|---|
| 109 | (setf val (%cadr spec)))) |
|---|
| 110 | (if val |
|---|
| 111 | (pushnew quality *explain*) |
|---|
| 112 | (setf *explain* (remove quality *explain*)))))))) |
|---|
| 113 | |
|---|
| 114 | (defun proclaim-type (type &rest names) |
|---|
| 115 | (dolist (name names) |
|---|
| 116 | (setf (get name 'proclaimed-type) type))) |
|---|
| 117 | |
|---|
| 118 | (defun proclaimed-type (name) |
|---|
| 119 | (get name 'proclaimed-type)) |
|---|
| 120 | |
|---|
| 121 | (declaim (type hash-table *proclaimed-ftypes*)) |
|---|
| 122 | (defconst *proclaimed-ftypes* (make-hash-table :test 'equal)) |
|---|
| 123 | |
|---|
| 124 | (declaim (inline proclaim-ftype-1)) |
|---|
| 125 | (defun proclaim-ftype-1 (ftype name) |
|---|
| 126 | (declare (optimize speed)) |
|---|
| 127 | (if (symbolp name) |
|---|
| 128 | (setf (get name 'proclaimed-ftype) ftype) |
|---|
| 129 | (setf (gethash name *proclaimed-ftypes*) ftype))) |
|---|
| 130 | (declaim (notinline proclaim-ftype-1)) |
|---|
| 131 | |
|---|
| 132 | (defun proclaim-ftype (ftype &rest names) |
|---|
| 133 | (declare (optimize speed)) |
|---|
| 134 | (declare (inline proclaim-ftype-1)) |
|---|
| 135 | (dolist (name names) |
|---|
| 136 | (proclaim-ftype-1 ftype name))) |
|---|
| 137 | |
|---|
| 138 | (defun proclaimed-ftype (name) |
|---|
| 139 | (if (symbolp name) |
|---|
| 140 | (get name 'proclaimed-ftype) |
|---|
| 141 | (gethash1 name *proclaimed-ftypes*))) |
|---|
| 142 | |
|---|
| 143 | (defun ftype-result-type (ftype) |
|---|
| 144 | (if (atom ftype) |
|---|
| 145 | '* |
|---|
| 146 | (let ((result-type (third ftype))) |
|---|
| 147 | (if result-type |
|---|
| 148 | result-type |
|---|
| 149 | '*)))) |
|---|