1 ;;;; the representation of a lexical environment
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
14 ;;; The LEXENV represents the lexical environment used for IR1 conversion.
15 ;;; (This is also what shows up as an ENVIRONMENT value in macroexpansion.)
16 #!-sb-fluid (declaim (inline internal-make-lexenv)) ; only called in one place
18 (:print-function print-lexenv)
19 (:constructor make-null-lexenv ())
20 (:constructor internal-make-lexenv
21 (funs vars blocks tags
23 lambda cleanup handled-conditions
24 disabled-package-locks policy)))
25 ;; an alist of (NAME . WHAT), where WHAT is either a FUNCTIONAL (a
26 ;; local function), a DEFINED-FUN, representing an
27 ;; INLINE/NOTINLINE declaration, or a list (MACRO . <function>) (a
28 ;; local macro, with the specifier expander). Note that NAME may be
29 ;; a (SETF <name>) list, not necessarily a single symbol.
31 ;; an alist translating variable names to LEAF structures. A special
32 ;; binding is indicated by a :SPECIAL GLOBAL-VAR leaf. Each special
33 ;; binding within the code gets a distinct leaf structure, as does
34 ;; the current "global" value on entry to the code compiled.
35 ;; (locally (special ...)) is handled by adding the most recent
36 ;; special binding to the front of the list.
38 ;; If the CDR is (MACRO . <exp>), then <exp> is the expansion of a
41 ;; BLOCKS and TAGS are alists from block and go-tag names to 2-lists
42 ;; of the form (<entry> <continuation>), where <continuation> is the
43 ;; continuation to exit to, and <entry> is the corresponding ENTRY
45 (blocks nil :type list)
47 ;; an alist (THING . CTYPE) which is used to keep track of
48 ;; "pervasive" type declarations. When THING is a leaf, this is for
49 ;; type declarations that pertain to the type in a syntactic extent
50 ;; which does not correspond to a binding of the affected name.
51 (type-restrictions nil :type list)
52 ;; the lexically enclosing lambda, if any
54 ;; FIXME: This should be :TYPE (OR CLAMBDA NULL), but it was too hard
55 ;; to get CLAMBDA defined in time for the cross-compiler.
57 ;; the lexically enclosing cleanup, or NIL if none enclosing within LAMBDA
59 ;; condition types we handle with a handler around the compiler
60 (handled-conditions *handled-conditions*)
61 ;; lexically disabled package locks (list of symbols)
62 (disabled-package-locks *disabled-package-locks*)
63 ;; the current OPTIMIZE policy
64 (policy *policy* :type policy))
66 ;;; support for the idiom (in MACROEXPAND and elsewhere) that NIL is
67 ;;; to be taken as a null lexical environment
68 (defun coerce-to-lexenv (x)
70 (null (make-null-lexenv))
73 (defun null-lexenv-p (lexenv)
74 (equalp (coerce-to-lexenv lexenv) (make-null-lexenv)))
76 (defun print-lexenv (lexenv stream level)
77 (if (null-lexenv-p lexenv)
78 (print-unreadable-object (lexenv stream)
79 (write-string "NULL-LEXENV" stream))
80 (default-structure-print lexenv stream level)))
82 (defun maybe-inline-syntactic-closure (lambda lexenv)
83 (declare (type list lambda) (type lexenv lexenv))
84 (aver (eql (first lambda) 'lambda))
85 ;; We used to have a trivial implementation, verifying that lexenv
86 ;; was effectively null. However, this fails to take account of the
89 ;; (declaim (inline foo))
90 ;; (macrolet ((def (x) `(defun ,x () ...)))
93 ;; which, while too complicated for the cross-compiler to handle in
94 ;; unfriendly foreign lisp environments, would be good to support in
95 ;; the target compiler. -- CSR, 2002-05-13 and 2002-11-02
96 (let ((vars (lexenv-vars lexenv))
97 (funs (lexenv-funs lexenv)))
98 (collect ((decls) (macros) (symbol-macros))
100 ((or (lexenv-blocks lexenv) (lexenv-tags lexenv)) nil)
101 ((and (null vars) (null funs)) `(lambda-with-lexenv
104 ((dolist (x vars nil)
106 ;; KLUDGE: too complicated for cross-compilation
111 ;; only worry about the innermost binding
112 (when (eq x (assoc name vars :test #'eq))
115 (aver (eq (car what) 'macro))
118 ;; A global should not appear in the lexical
119 ;; environment? Is this true? FIXME!
120 (aver (eq (global-var-kind what) :special))
121 (decls `(special ,name)))
123 ;; we can't inline in the presence of this object
126 ((dolist (x funs nil)
128 ;; KLUDGE: too complicated for cross-compilation (and
129 ;; failure of OAOO in comments, *sigh*)
134 ;; again, only worry about the innermost binding, but
135 ;; functions can have name (SETF FOO) so we need to use
136 ;; EQUAL for the test.
137 (when (eq x (assoc name funs :test #'equal))
140 (macros (cons name (function-lambda-expression (cdr what)))))
141 ;; FIXME: Is there a good reason for this not to be
142 ;; DEFINED-FUN (which :INCLUDEs GLOBAL-VAR, in case
143 ;; you're wondering how this ever worked :-)? Maybe
144 ;; in conjunction with an AVERrance that it's not an
145 ;; (AND GLOBAL-VAR (NOT GLOBAL-FUN))? -- CSR,
148 (when (defined-fun-p what)
149 (decls `(,(car (rassoc (defined-fun-inlinep what)
150 *inlinep-translations*))
155 ;; if we get this far, we've successfully dealt with
156 ;; everything in FUNS and VARS, so:
157 `(lambda-with-lexenv ,(decls) ,(macros) ,(symbol-macros)