| 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) 2009, Juan Jose Garcia-Ripoll. |
| 6 |
;;;; |
| 7 |
;;;; See file 'LICENSE' for the copyright details. |
| 8 |
|
| 9 |
;;;; |
| 10 |
;;;; CMPENVAPI -- API for creating and manipulating environments |
| 11 |
;;;; |
| 12 |
|
| 13 |
(in-package "COMPILER") |
| 14 |
|
| 15 |
(defun cmp-env-root (&optional (env *cmp-env-root*)) |
| 16 |
"Provide a root environment for toplevel forms storing all declarations |
| 17 |
that are susceptible to be changed by PROCLAIM." |
| 18 |
(let ((env (cmp-env-copy env))) |
| 19 |
(add-default-optimizations env))) |
| 20 |
|
| 21 |
(defun cmp-env-copy (&optional (env *cmp-env*)) |
| 22 |
(cons (car env) (cdr env))) |
| 23 |
|
| 24 |
(defmacro cmp-env-variables (&optional (env '*cmp-env*)) |
| 25 |
`(car ,env)) |
| 26 |
|
| 27 |
(defmacro cmp-env-functions (&optional (env '*cmp-env*)) |
| 28 |
`(cdr ,env)) |
| 29 |
|
| 30 |
(defun cmp-env-register-var (var &optional (env *cmp-env*) (boundp t)) |
| 31 |
(push (list (var-name var) |
| 32 |
(if (member (var-kind var) '(special global)) |
| 33 |
:special |
| 34 |
t) |
| 35 |
boundp |
| 36 |
var) |
| 37 |
(cmp-env-variables env)) |
| 38 |
env) |
| 39 |
|
| 40 |
(defun cmp-env-add-declaration (type arguments &optional (env *cmp-env*)) |
| 41 |
(push (list* :declare type arguments) |
| 42 |
(cmp-env-variables env)) |
| 43 |
env) |
| 44 |
|
| 45 |
(defun cmp-env-extend-declaration (type arguments &optional (env *cmp-env*) default) |
| 46 |
(let ((x (cmp-env-search-declaration type env default))) |
| 47 |
(cmp-env-add-declaration type (append arguments x) env) |
| 48 |
env)) |
| 49 |
|
| 50 |
(defun cmp-env-register-function (fun &optional (env *cmp-env*)) |
| 51 |
(push (list (fun-name fun) 'function fun) |
| 52 |
(cmp-env-functions env)) |
| 53 |
env) |
| 54 |
|
| 55 |
(defun cmp-env-register-global-macro (name function) |
| 56 |
(cmp-env-register-macro name function *cmp-env*) |
| 57 |
(cmp-env-register-macro name function *cmp-env-root*) |
| 58 |
(values)) |
| 59 |
|
| 60 |
(defun cmp-env-register-macro (name function &optional (env *cmp-env*)) |
| 61 |
(push (list name 'si:macro function) |
| 62 |
(cmp-env-functions env)) |
| 63 |
env) |
| 64 |
|
| 65 |
(defun cmp-env-register-ftype (name declaration &optional (env *cmp-env*)) |
| 66 |
(push (list* :declare name declaration) |
| 67 |
(cmp-env-functions env)) |
| 68 |
env) |
| 69 |
|
| 70 |
(defun cmp-env-register-symbol-macro (name form &optional (env *cmp-env*)) |
| 71 |
(cmp-env-register-symbol-macro-function name |
| 72 |
#'(lambda (whole env) (declare (ignore env whole)) form) |
| 73 |
env)) |
| 74 |
|
| 75 |
(defun cmp-env-register-symbol-macro-function (name function &optional (env *cmp-env*)) |
| 76 |
(when (or (constant-variable-p name) (special-variable-p name)) |
| 77 |
(cmperr "Cannot bind the special or constant variable ~A with symbol-macrolet." name)) |
| 78 |
(push (list name 'si:symbol-macro function) |
| 79 |
(cmp-env-variables env)) |
| 80 |
env) |
| 81 |
|
| 82 |
(defun cmp-env-register-block (blk &optional (env *cmp-env*)) |
| 83 |
(push (list :block (blk-name blk) blk) |
| 84 |
(cmp-env-variables env)) |
| 85 |
env) |
| 86 |
|
| 87 |
(defun cmp-env-register-tag (name tag &optional (env *cmp-env*)) |
| 88 |
(push (list :tag (list name) tag) |
| 89 |
(cmp-env-variables env)) |
| 90 |
env) |
| 91 |
|
| 92 |
(defun cmp-env-register-compiler-macro (name macro-function) |
| 93 |
(push (list name 'si::compiler-macro macro-function) |
| 94 |
(cmp-env-functions *cmp-env-root*)) |
| 95 |
(values)) |
| 96 |
|
| 97 |
(defun cmp-env-register-type (name definition &optional (env *cmp-env*)) |
| 98 |
(push (list :type name definition) |
| 99 |
(cmp-env-variables env)) |
| 100 |
env) |
| 101 |
|
| 102 |
(defun cmp-env-register-types (definitions &optional (env *cmp-env*)) |
| 103 |
(dolist (def definitions) |
| 104 |
(setf env (cmp-env-register-type (car def) (cdr def) env))) |
| 105 |
env) |
| 106 |
|
| 107 |
(defun cmp-env-search-function (name &optional (env *cmp-env*)) |
| 108 |
(let ((cfb nil) |
| 109 |
(unw nil) |
| 110 |
(found nil)) |
| 111 |
(dolist (record (cmp-env-functions env)) |
| 112 |
(cond ((eq record 'SI:FUNCTION-BOUNDARY) |
| 113 |
(setf cfb t)) |
| 114 |
((eq record 'SI:UNWIND-PROTECT-BOUNDARY) |
| 115 |
(setf unw t)) |
| 116 |
((atom record) |
| 117 |
(baboon :format-control "Unknown record found in environment~%~S" |
| 118 |
:format-arguments (list record))) |
| 119 |
;; We have to use EQUAL because the name can be a list (SETF whatever) |
| 120 |
((and (equal (first record) name) (not (eq (second record) 'si::compiler-macro))) |
| 121 |
(setf found (first (last record))) |
| 122 |
(return)))) |
| 123 |
(values found cfb unw))) |
| 124 |
|
| 125 |
(defun cmp-env-search-variables (type name env) |
| 126 |
(let ((cfb nil) |
| 127 |
(unw nil) |
| 128 |
(found nil)) |
| 129 |
(dolist (record (cmp-env-variables env)) |
| 130 |
(cond ((eq record 'SI:FUNCTION-BOUNDARY) |
| 131 |
(setf cfb t)) |
| 132 |
((eq record 'SI:UNWIND-PROTECT-BOUNDARY) |
| 133 |
(setf unw t)) |
| 134 |
((atom record) |
| 135 |
(baboon :format-control "Unknown record found in environment~%~S" |
| 136 |
:format-arguments (list record))) |
| 137 |
((not (eq (first record) type))) |
| 138 |
((eq type :block) |
| 139 |
(when (eq name (second record)) |
| 140 |
(setf found record) |
| 141 |
(return))) |
| 142 |
((eq type :tag) |
| 143 |
(when (member name (second record) :test #'eql) |
| 144 |
(setf found record) |
| 145 |
(return))) |
| 146 |
((eq name 'si:symbol-macro) |
| 147 |
(when (eq (second record) 'si:symbol-macro) |
| 148 |
(setf found record)) |
| 149 |
(return)) |
| 150 |
(t |
| 151 |
(when (not (eq (second record) 'si:symbol-macro)) |
| 152 |
(setf found record)) |
| 153 |
(return)))) |
| 154 |
(values (first (last found)) cfb unw))) |
| 155 |
|
| 156 |
(defun cmp-env-search-block (name &optional (env *cmp-env*)) |
| 157 |
(cmp-env-search-variables :block name env)) |
| 158 |
|
| 159 |
(defun cmp-env-search-tag (name &optional (env *cmp-env*)) |
| 160 |
(cmp-env-search-variables :tag name env)) |
| 161 |
|
| 162 |
(defun cmp-env-search-symbol-macro (name &optional (env *cmp-env*)) |
| 163 |
(cmp-env-search-variables name 'si:symbol-macro env)) |
| 164 |
|
| 165 |
(defun cmp-env-search-var (name &optional (env *cmp-env*)) |
| 166 |
(cmp-env-search-variables name t env)) |
| 167 |
|
| 168 |
(defun cmp-env-search-macro (name &optional (env *cmp-env*)) |
| 169 |
(let ((f (cmp-env-search-function name env))) |
| 170 |
(if (functionp f) |
| 171 |
f |
| 172 |
nil))) |
| 173 |
|
| 174 |
;;; Like macro-function except it searches the lexical environment, |
| 175 |
;;; to determine if the macro is shadowed by a function or a macro. |
| 176 |
(defun cmp-macro-function (name) |
| 177 |
(or (cmp-env-search-macro name) |
| 178 |
(macro-function name))) |
| 179 |
|
| 180 |
(defun cmp-env-search-compiler-macro (name &optional (env *cmp-env*)) |
| 181 |
(dolist (record (cmp-env-functions env)) |
| 182 |
(when (and (consp record) |
| 183 |
(equal (first record) name) |
| 184 |
(eq (second record) 'si::compiler-macro)) |
| 185 |
(return-from cmp-env-search-compiler-macro (third record))))) |
| 186 |
|
| 187 |
(defun cmp-compiler-macro-function (name) |
| 188 |
(or (cmp-env-search-compiler-macro name) |
| 189 |
(compiler-macro-function name))) |
| 190 |
|
| 191 |
(defun cmp-env-search-ftype (name &optional (env *cmp-env*)) |
| 192 |
(dolist (i env nil) |
| 193 |
(when (and (consp i) |
| 194 |
(eq (pop i) :declare) |
| 195 |
(same-fname-p (pop i) name)) |
| 196 |
(return i)))) |
| 197 |
|
| 198 |
(defun cmp-env-mark (mark &optional (env *cmp-env*)) |
| 199 |
(cons (cons mark (car env)) |
| 200 |
(cons mark (cdr env)))) |
| 201 |
|
| 202 |
(defun cmp-env-new-variables (new-env old-env) |
| 203 |
(loop for i in (ldiff (cmp-env-variables new-env) |
| 204 |
(cmp-env-variables old-env)) |
| 205 |
when (and (consp i) (var-p (fourth i))) |
| 206 |
collect (fourth i))) |
| 207 |
|
| 208 |
(defun cmp-env-search-declaration (kind &optional (env *cmp-env*) default) |
| 209 |
(loop for i in (car env) |
| 210 |
when (and (consp i) |
| 211 |
(eq (first i) :declare) |
| 212 |
(eq (second i) kind)) |
| 213 |
return (cddr i) |
| 214 |
finally (return default))) |
| 215 |
|
| 216 |
(defun cmp-env-search-type (name &optional (env *cmp-env*) (default name)) |
| 217 |
(loop for i in (car env) |
| 218 |
when (and (consp i) |
| 219 |
(eq (first i) :type) |
| 220 |
(eq (second i) name)) |
| 221 |
return (third i) |
| 222 |
finally (return default))) |
| 223 |
|