34f37ba430d73b867a77ff8e5cf6adb1f5729fe8
[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                                                '(speed . 1)
25                                                '(space . 1)
26                                                '(debug . 1)
27                                                '(inhibit-warnings . 1)))))
28              (:constructor internal-make-lexenv
29                            (funs vars blocks tags
30                                  type-restrictions
31                                  weakend-type-restrictions
32                                  lambda cleanup policy)))
33   ;; an alist of (NAME . WHAT), where WHAT is either a FUNCTIONAL (a
34   ;; local function), a DEFINED-FUN, representing an
35   ;; INLINE/NOTINLINE declaration, or a list (MACRO . <function>) (a
36   ;; local macro, with the specifier expander). Note that NAME may be
37   ;; a (SETF <name>) list, not necessarily a single symbol.
38   (funs nil :type list)
39   ;; an alist translating variable names to LEAF structures. A special
40   ;; binding is indicated by a :SPECIAL GLOBAL-VAR leaf. Each special
41   ;; binding within the code gets a distinct leaf structure, as does
42   ;; the current "global" value on entry to the code compiled.
43   ;; (locally (special ...)) is handled by adding the most recent
44   ;; special binding to the front of the list.
45   ;;
46   ;; If the CDR is (MACRO . <exp>), then <exp> is the expansion of a
47   ;; symbol macro.
48   (vars nil :type list)
49   ;; BLOCKS and TAGS are alists from block and go-tag names to 2-lists
50   ;; of the form (<entry> <continuation>), where <continuation> is the
51   ;; continuation to exit to, and <entry> is the corresponding ENTRY 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. When
58   ;; THING is a continuation, this is used to track the innermost THE
59   ;; type declaration.
60   (type-restrictions nil :type list)
61   (weakend-type-restrictions nil :type list)
62   ;; the lexically enclosing lambda, if any
63   ;;
64   ;; FIXME: This should be :TYPE (OR CLAMBDA NULL), but it was too hard
65   ;; to get CLAMBDA defined in time for the cross-compiler.
66   (lambda nil)
67   ;; the lexically enclosing cleanup, or NIL if none enclosing within Lambda
68   (cleanup nil)
69   ;; the current OPTIMIZE policy
70   (policy *policy* :type policy))
71
72 ;;; support for the idiom (in MACROEXPAND and elsewhere) that NIL is
73 ;;; to be taken as a null lexical environment
74 (defun coerce-to-lexenv (x)
75   (etypecase x
76     (null (make-null-lexenv))
77     (lexenv x)))
78
79 ;;; Is it safe to just grab the lambda expression LAMBDA in isolation,
80 ;;; ignoring the LEXENV?
81 ;;;
82 ;;; Note: The corresponding CMU CL code did something hairier so that
83 ;;; it could save inline definitions of DEFUNs in nontrivial lexical
84 ;;; environments. If it's ever important to try to do that, take a
85 ;;; look at the old CMU CL #'INLINE-SYNTACTIC-CLOSURE.
86 (defun lambda-independent-of-lexenv-p (lambda lexenv)
87   (declare (type list lambda) (type lexenv lexenv))
88   (aver (eql (first lambda) 'lambda)) ; basic sanity check
89   ;; This is a trivial implementation that just makes sure that LEXENV
90   ;; doesn't have anything interesting in it. A more sophisticated
91   ;; implementation could skip things in LEXENV which aren't captured
92   ;; by LAMBDA, but this implementation doesn't try.
93   (and (null (lexenv-blocks lexenv))
94        (null (lexenv-tags lexenv))
95        (null (lexenv-vars lexenv))
96        (null (lexenv-funs lexenv))))