0.8.20.12: policy control, the uncontroversial parts
[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              (:constructor make-null-lexenv ())
19              (:constructor internal-make-lexenv
20                            (funs vars blocks tags
21                                  type-restrictions
22                                  lambda cleanup handled-conditions
23                                  disabled-package-locks policy)))
24   ;; an alist of (NAME . WHAT), where WHAT is either a FUNCTIONAL (a
25   ;; local function), a DEFINED-FUN, representing an
26   ;; INLINE/NOTINLINE declaration, or a list (MACRO . <function>) (a
27   ;; local macro, with the specifier expander). Note that NAME may be
28   ;; a (SETF <name>) list, not necessarily a single symbol.
29   (funs nil :type list)
30   ;; an alist translating variable names to LEAF structures. A special
31   ;; binding is indicated by a :SPECIAL GLOBAL-VAR leaf. Each special
32   ;; binding within the code gets a distinct leaf structure, as does
33   ;; the current "global" value on entry to the code compiled.
34   ;; (locally (special ...)) is handled by adding the most recent
35   ;; special binding to the front of the list.
36   ;;
37   ;; If the CDR is (MACRO . <exp>), then <exp> is the expansion of a
38   ;; symbol macro.
39   (vars nil :type list)
40   ;; BLOCKS and TAGS are alists from block and go-tag names to 2-lists
41   ;; of the form (<entry> <continuation>), where <continuation> is the
42   ;; continuation to exit to, and <entry> is the corresponding ENTRY
43   ;; node.
44   (blocks nil :type list)
45   (tags nil :type list)
46   ;; an alist (THING . CTYPE) which is used to keep track of
47   ;; "pervasive" type declarations. When THING is a leaf, this is for
48   ;; type declarations that pertain to the type in a syntactic extent
49   ;; which does not correspond to a binding of the affected name.
50   (type-restrictions nil :type list)
51   ;; the lexically enclosing lambda, if any
52   ;;
53   ;; FIXME: This should be :TYPE (OR CLAMBDA NULL), but it was too hard
54   ;; to get CLAMBDA defined in time for the cross-compiler.
55   (lambda nil)
56   ;; the lexically enclosing cleanup, or NIL if none enclosing within LAMBDA
57   (cleanup nil)
58   ;; condition types we handle with a handler around the compiler
59   (handled-conditions *handled-conditions*)
60   ;; lexically disabled package locks (list of symbols)
61   (disabled-package-locks *disabled-package-locks*)
62   ;; the current OPTIMIZE policy
63   (policy *policy* :type policy))
64
65 ;;; support for the idiom (in MACROEXPAND and elsewhere) that NIL is
66 ;;; to be taken as a null lexical environment
67 (defun coerce-to-lexenv (x)
68   (etypecase x
69     (null (make-null-lexenv))
70     (lexenv x)))
71
72 (defun maybe-inline-syntactic-closure (lambda lexenv)
73   (declare (type list lambda) (type lexenv lexenv))
74   (aver (eql (first lambda) 'lambda))
75   ;; We used to have a trivial implementation, verifying that lexenv
76   ;; was effectively null. However, this fails to take account of the
77   ;; idiom
78   ;;
79   ;; (declaim (inline foo))
80   ;; (macrolet ((def (x) `(defun ,x () ...)))
81   ;;   (def foo))
82   ;;
83   ;; which, while too complicated for the cross-compiler to handle in
84   ;; unfriendly foreign lisp environments, would be good to support in
85   ;; the target compiler. -- CSR, 2002-05-13 and 2002-11-02
86   (let ((vars (lexenv-vars lexenv))
87         (funs (lexenv-funs lexenv)))
88     (collect ((decls) (macros) (symbol-macros))
89       (cond
90         ((or (lexenv-blocks lexenv) (lexenv-tags lexenv)) nil)
91         ((and (null vars) (null funs)) `(lambda-with-lexenv
92                                          nil nil nil
93                                          ,@(cdr lambda)))
94         ((dolist (x vars nil)
95            #+sb-xc-host
96            ;; KLUDGE: too complicated for cross-compilation
97            (return t)
98            #-sb-xc-host
99            (let ((name (car x))
100                  (what (cdr x)))
101              ;; only worry about the innermost binding
102              (when (eq x (assoc name vars :test #'eq))
103                (typecase what
104                  (cons
105                   (aver (eq (car what) 'macro))
106                   (symbol-macros x))
107                  (global-var
108                   ;; A global should not appear in the lexical
109                   ;; environment? Is this true? FIXME!
110                   (aver (eq (global-var-kind what) :special))
111                   (decls `(special ,name)))
112                  (t
113                   ;; we can't inline in the presence of this object
114                   (return t))))))
115          nil)
116         ((dolist (x funs nil)
117            #+sb-xc-host
118            ;; KLUDGE: too complicated for cross-compilation (and
119            ;; failure of OAOO in comments, *sigh*)
120            (return t)
121            #-sb-xc-host
122            (let ((name (car x))
123                  (what (cdr x)))
124              ;; again, only worry about the innermost binding, but
125              ;; functions can have name (SETF FOO) so we need to use
126              ;; EQUAL for the test.
127              (when (eq x (assoc name funs :test #'equal))
128                (typecase what
129                  (cons
130                   (macros (cons name (function-lambda-expression (cdr what)))))
131                  ;; FIXME: Is there a good reason for this not to be
132                  ;; DEFINED-FUN (which :INCLUDEs GLOBAL-VAR, in case
133                  ;; you're wondering how this ever worked :-)? Maybe
134                  ;; in conjunction with an AVERrance that it's not an
135                  ;; (AND GLOBAL-VAR (NOT GLOBAL-FUN))? -- CSR,
136                  ;; 2002-07-08
137                  (global-var
138                   (when (defined-fun-p what)
139                     (decls `(,(car (rassoc (defined-fun-inlinep what)
140                                            *inlinep-translations*))
141                               ,name))))
142                  (t (return t))))))
143          nil)
144         (t
145          ;; if we get this far, we've successfully dealt with
146          ;; everything in FUNS and VARS, so:
147          `(lambda-with-lexenv ,(decls) ,(macros) ,(symbol-macros)
148                               ,@(cdr lambda)))))))
149