0.9.13.36: global policy / null-lexenv confusion fix
[sbcl.git] / src / compiler / lexenv.lisp
1 ;;;; the representation of a lexical environment
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
11
12 (in-package "SB!C")
13
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
17 (def!struct (lexenv
18              (:print-function print-lexenv)
19              (:constructor make-null-lexenv ())
20              (:constructor internal-make-lexenv
21                            (funs vars blocks tags
22                                  type-restrictions
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.
30   (funs nil :type list)
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.
37   ;;
38   ;; If the CDR is (MACRO . <exp>), then <exp> is the expansion of a
39   ;; symbol macro.
40   (vars nil :type list)
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
44   ;; node.
45   (blocks nil :type list)
46   (tags 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
53   ;;
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.
56   (lambda nil)
57   ;; the lexically enclosing cleanup, or NIL if none enclosing within LAMBDA
58   (cleanup nil)
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. this is null in the null environment,
64   ;; and the global policy is stored in *POLICY*. (Because we want to
65   ;; be able to affect it from :WITH-COMPILATION-UNIT.) NIL here also
66   ;; works as a convenient null-lexenv identifier.
67   (%policy nil :type policy))
68
69 (defun lexenv-policy (lexenv)
70   (or (lexenv-%policy lexenv) *policy*))
71
72 (defun null-lexenv-p (lexenv)
73   (not (lexenv-%policy lexenv)))
74
75 ;;; support for the idiom (in MACROEXPAND and elsewhere) that NIL is
76 ;;; to be taken as a null lexical environment
77 (defun coerce-to-lexenv (x)
78   (etypecase x
79     (null (make-null-lexenv))
80     (lexenv x)))
81
82 (defun print-lexenv (lexenv stream level)
83   (if (null-lexenv-p lexenv)
84       (print-unreadable-object (lexenv stream)
85         (write-string "NULL-LEXENV" stream))
86       (default-structure-print lexenv stream level)))
87
88 (defun maybe-inline-syntactic-closure (lambda lexenv)
89   (declare (type list lambda) (type lexenv lexenv))
90   (aver (eql (first lambda) 'lambda))
91   ;; We used to have a trivial implementation, verifying that lexenv
92   ;; was effectively null. However, this fails to take account of the
93   ;; idiom
94   ;;
95   ;; (declaim (inline foo))
96   ;; (macrolet ((def (x) `(defun ,x () ...)))
97   ;;   (def foo))
98   ;;
99   ;; which, while too complicated for the cross-compiler to handle in
100   ;; unfriendly foreign lisp environments, would be good to support in
101   ;; the target compiler. -- CSR, 2002-05-13 and 2002-11-02
102   (let ((vars (lexenv-vars lexenv))
103         (funs (lexenv-funs lexenv)))
104     (collect ((decls) (macros) (symbol-macros))
105       (cond
106         ((or (lexenv-blocks lexenv) (lexenv-tags lexenv)) nil)
107         ((and (null vars) (null funs)) `(lambda-with-lexenv
108                                          nil nil nil
109                                          ,@(cdr lambda)))
110         ((dolist (x vars nil)
111            #+sb-xc-host
112            ;; KLUDGE: too complicated for cross-compilation
113            (return t)
114            #-sb-xc-host
115            (let ((name (car x))
116                  (what (cdr x)))
117              ;; only worry about the innermost binding
118              (when (eq x (assoc name vars :test #'eq))
119                (typecase what
120                  (cons
121                   (aver (eq (car what) 'macro))
122                   (symbol-macros x))
123                  (global-var
124                   ;; A global should not appear in the lexical
125                   ;; environment? Is this true? FIXME!
126                   (aver (eq (global-var-kind what) :special))
127                   (decls `(special ,name)))
128                  (t
129                   ;; we can't inline in the presence of this object
130                   (return t))))))
131          nil)
132         ((dolist (x funs nil)
133            #+sb-xc-host
134            ;; KLUDGE: too complicated for cross-compilation (and
135            ;; failure of OAOO in comments, *sigh*)
136            (return t)
137            #-sb-xc-host
138            (let ((name (car x))
139                  (what (cdr x)))
140              ;; again, only worry about the innermost binding, but
141              ;; functions can have name (SETF FOO) so we need to use
142              ;; EQUAL for the test.
143              (when (eq x (assoc name funs :test #'equal))
144                (typecase what
145                  (cons
146                   (macros (cons name (function-lambda-expression (cdr what)))))
147                  ;; FIXME: Is there a good reason for this not to be
148                  ;; DEFINED-FUN (which :INCLUDEs GLOBAL-VAR, in case
149                  ;; you're wondering how this ever worked :-)? Maybe
150                  ;; in conjunction with an AVERrance that it's not an
151                  ;; (AND GLOBAL-VAR (NOT GLOBAL-FUN))? -- CSR,
152                  ;; 2002-07-08
153                  (global-var
154                   (when (defined-fun-p what)
155                     (decls `(,(car (rassoc (defined-fun-inlinep what)
156                                            *inlinep-translations*))
157                               ,name))))
158                  (t (return t))))))
159          nil)
160         (t
161          ;; if we get this far, we've successfully dealt with
162          ;; everything in FUNS and VARS, so:
163          `(lambda-with-lexenv ,(decls) ,(macros) ,(symbol-macros)
164                               ,@(cdr lambda)))))))
165