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