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