blob: 56c8dabd378e033647a5563d63721e1224363047 [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_VARIABLES_H_
29#define V8_VARIABLES_H_
30
31#include "zone.h"
32
33namespace v8 {
34namespace internal {
35
Steve Blocka7e24c12009-10-30 11:49:00 +000036// The AST refers to variables via VariableProxies - placeholders for the actual
37// variables. Variables themselves are never directly referred to from the AST,
38// they are maintained by scopes, and referred to from VariableProxies and Slots
39// after binding and variable allocation.
40
41class Variable: public ZoneObject {
42 public:
43 enum Mode {
44 // User declared variables:
45 VAR, // declared via 'var', and 'function' declarations
46
47 CONST, // declared via 'const' declarations
48
Ben Murdoch69a99ed2011-11-30 16:03:39 +000049 LET, // declared via 'let' declarations
50
Steve Blocka7e24c12009-10-30 11:49:00 +000051 // Variables introduced by the compiler:
52 DYNAMIC, // always require dynamic lookup (we don't know
53 // the declaration)
54
55 DYNAMIC_GLOBAL, // requires dynamic lookup, but we know that the
56 // variable is global unless it has been shadowed
57 // by an eval-introduced variable
58
59 DYNAMIC_LOCAL, // requires dynamic lookup, but we know that the
60 // variable is local and where it is unless it
61 // has been shadowed by an eval-introduced
62 // variable
63
64 INTERNAL, // like VAR, but not user-visible (may or may not
65 // be in a context)
66
67 TEMPORARY // temporary variables (not user-visible), never
68 // in a context
69 };
70
71 enum Kind {
72 NORMAL,
73 THIS,
74 ARGUMENTS
75 };
76
Ben Murdoch589d6972011-11-30 16:04:58 +000077 enum Location {
78 // Before and during variable allocation, a variable whose location is
79 // not yet determined. After allocation, a variable looked up as a
80 // property on the global object (and possibly absent). name() is the
81 // variable name, index() is invalid.
82 UNALLOCATED,
83
84 // A slot in the parameter section on the stack. index() is the
85 // parameter index, counting left-to-right. The reciever is index -1;
86 // the first parameter is index 0.
87 PARAMETER,
88
89 // A slot in the local section on the stack. index() is the variable
90 // index in the stack frame, starting at 0.
91 LOCAL,
92
93 // An indexed slot in a heap context. index() is the variable index in
94 // the context object on the heap, starting at 0. scope() is the
95 // corresponding scope.
96 CONTEXT,
97
98 // A named slot in a heap context. name() is the variable name in the
99 // context object on the heap, with lookup starting at the current
100 // context. index() is invalid.
101 LOOKUP
102 };
103
Steve Blocka7e24c12009-10-30 11:49:00 +0000104 Variable(Scope* scope,
105 Handle<String> name,
106 Mode mode,
107 bool is_valid_lhs,
108 Kind kind);
109
110 // Printing support
111 static const char* Mode2String(Mode mode);
112
Steve Blocka7e24c12009-10-30 11:49:00 +0000113 bool IsValidLeftHandSide() { return is_valid_LHS_; }
114
115 // The source code for an eval() call may refer to a variable that is
116 // in an outer scope about which we don't know anything (it may not
117 // be the global scope). scope() is NULL in that case. Currently the
118 // scope is only used to follow the context chain length.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100119 Scope* scope() const { return scope_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000120
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100121 Handle<String> name() const { return name_; }
122 Mode mode() const { return mode_; }
123 bool is_accessed_from_inner_scope() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000124 return is_accessed_from_inner_scope_;
125 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100126 void MarkAsAccessedFromInnerScope() {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000127 ASSERT(mode_ != TEMPORARY);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100128 is_accessed_from_inner_scope_ = true;
129 }
Steve Block6ded16b2010-05-10 14:33:55 +0100130 bool is_used() { return is_used_; }
131 void set_is_used(bool flag) { is_used_ = flag; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000132
133 bool IsVariable(Handle<String> n) const {
134 return !is_this() && name().is_identical_to(n);
135 }
136
Ben Murdoch589d6972011-11-30 16:04:58 +0000137 bool IsUnallocated() const { return location_ == UNALLOCATED; }
138 bool IsParameter() const { return location_ == PARAMETER; }
139 bool IsStackLocal() const { return location_ == LOCAL; }
140 bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
141 bool IsContextSlot() const { return location_ == CONTEXT; }
142 bool IsLookupSlot() const { return location_ == LOOKUP; }
Steve Block6ded16b2010-05-10 14:33:55 +0100143
Steve Blocka7e24c12009-10-30 11:49:00 +0000144 bool is_dynamic() const {
145 return (mode_ == DYNAMIC ||
146 mode_ == DYNAMIC_GLOBAL ||
147 mode_ == DYNAMIC_LOCAL);
148 }
149
150 bool is_global() const;
151 bool is_this() const { return kind_ == THIS; }
152 bool is_arguments() const { return kind_ == ARGUMENTS; }
153
154 // True if the variable is named eval and not known to be shadowed.
155 bool is_possibly_eval() const {
Steve Block44f0eee2011-05-26 01:26:41 +0100156 return IsVariable(FACTORY->eval_symbol()) &&
Steve Blocka7e24c12009-10-30 11:49:00 +0000157 (mode_ == DYNAMIC || mode_ == DYNAMIC_GLOBAL);
158 }
159
160 Variable* local_if_not_shadowed() const {
161 ASSERT(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
162 return local_if_not_shadowed_;
163 }
164
165 void set_local_if_not_shadowed(Variable* local) {
166 local_if_not_shadowed_ = local;
167 }
168
Ben Murdoch589d6972011-11-30 16:04:58 +0000169 Location location() const { return location_; }
170 int index() const { return index_; }
171
172 void AllocateTo(Location location, int index) {
173 location_ = location;
174 index_ = index;
175 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000176
Steve Blocka7e24c12009-10-30 11:49:00 +0000177 private:
178 Scope* scope_;
179 Handle<String> name_;
180 Mode mode_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000181 Kind kind_;
Ben Murdoch589d6972011-11-30 16:04:58 +0000182 Location location_;
183 int index_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000184
185 Variable* local_if_not_shadowed_;
186
Steve Block1e0659c2011-05-24 12:43:12 +0100187 // Valid as a LHS? (const and this are not valid LHS, for example)
188 bool is_valid_LHS_;
189
190 // Usage info.
191 bool is_accessed_from_inner_scope_; // set by variable resolver
192 bool is_used_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000193};
194
195
196} } // namespace v8::internal
197
198#endif // V8_VARIABLES_H_