0.pre7.38:
[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 internal-make-lexenv
22                            (functions variables blocks tags type-restrictions
23                                       lambda cleanup policy options)))
24   ;; an alist of (NAME . WHAT), where WHAT is either a FUNCTIONAL (a
25   ;; local function), a DEFINED-FUNCTION, 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   (functions 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   (variables 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 node.
43   (blocks nil :type list)
44   (tags nil :type list)
45   ;; an alist (THING . CTYPE) which is used to keep track of
46   ;; "pervasive" type declarations. When THING is a leaf, this is for
47   ;; type declarations that pertain to the type in a syntactic extent
48   ;; which does not correspond to a binding of the affected name. When
49   ;; THING is a continuation, this is used to track the innermost THE
50   ;; type declaration.
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   ;; the current OPTIMIZE policy
60   (policy *policy* :type policy)
61   ;; an alist of miscellaneous options that are associated with the
62   ;; lexical environment
63   (options nil :type list))
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 ;;; Is it safe to just grab the lambda expression LAMBDA in isolation,
73 ;;; ignoring the LEXENV?
74 ;;;
75 ;;; Note: The corresponding CMU CL code did something hairier so that
76 ;;; it could save inline definitions of DEFUNs in nontrivial lexical
77 ;;; environments. If it's ever important to try to do that, take a
78 ;;; look at the old CMU CL #'INLINE-SYNTACTIC-CLOSURE.
79 (defun lambda-independent-of-lexenv-p (lambda lexenv)
80   (declare (type list lambda) (type lexenv lexenv))
81   (aver (eql (first lambda) 'lambda)) ; basic sanity check
82   ;; This is a trivial implementation that just makes sure that LEXENV
83   ;; doesn't have anything interesting in it. A more sophisticated
84   ;; implementation could skip things in LEXENV which aren't captured
85   ;; by LAMBDA, but this implementation doesn't try.
86   (and (null (lexenv-blocks lexenv))
87        (null (lexenv-tags lexenv))
88        (null (lexenv-variables lexenv))
89        (null (lexenv-functions lexenv))))