blob: f20bd399c542aefc74400fd957ff9c6a7a8fa229 [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:
Steve Blocka7e24c12009-10-30 11:49:00 +000043 enum Kind {
44 NORMAL,
45 THIS,
46 ARGUMENTS
47 };
48
Ben Murdoch589d6972011-11-30 16:04:58 +000049 enum Location {
50 // Before and during variable allocation, a variable whose location is
51 // not yet determined. After allocation, a variable looked up as a
52 // property on the global object (and possibly absent). name() is the
53 // variable name, index() is invalid.
54 UNALLOCATED,
55
56 // A slot in the parameter section on the stack. index() is the
57 // parameter index, counting left-to-right. The reciever is index -1;
58 // the first parameter is index 0.
59 PARAMETER,
60
61 // A slot in the local section on the stack. index() is the variable
62 // index in the stack frame, starting at 0.
63 LOCAL,
64
65 // An indexed slot in a heap context. index() is the variable index in
66 // the context object on the heap, starting at 0. scope() is the
67 // corresponding scope.
68 CONTEXT,
69
70 // A named slot in a heap context. name() is the variable name in the
71 // context object on the heap, with lookup starting at the current
72 // context. index() is invalid.
73 LOOKUP
74 };
75
Steve Blocka7e24c12009-10-30 11:49:00 +000076 Variable(Scope* scope,
77 Handle<String> name,
Ben Murdoch592a9fc2012-03-05 11:04:45 +000078 VariableMode mode,
Steve Blocka7e24c12009-10-30 11:49:00 +000079 bool is_valid_lhs,
Ben Murdoch592a9fc2012-03-05 11:04:45 +000080 Kind kind,
81 InitializationFlag initialization_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +000082
83 // Printing support
Ben Murdoch592a9fc2012-03-05 11:04:45 +000084 static const char* Mode2String(VariableMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +000085
Steve Blocka7e24c12009-10-30 11:49:00 +000086 bool IsValidLeftHandSide() { return is_valid_LHS_; }
87
88 // The source code for an eval() call may refer to a variable that is
89 // in an outer scope about which we don't know anything (it may not
90 // be the global scope). scope() is NULL in that case. Currently the
91 // scope is only used to follow the context chain length.
Kristian Monsen0d5e1162010-09-30 15:31:59 +010092 Scope* scope() const { return scope_; }
Steve Blocka7e24c12009-10-30 11:49:00 +000093
Kristian Monsen0d5e1162010-09-30 15:31:59 +010094 Handle<String> name() const { return name_; }
Ben Murdoch592a9fc2012-03-05 11:04:45 +000095 VariableMode mode() const { return mode_; }
96 bool has_forced_context_allocation() const {
97 return force_context_allocation_;
Steve Blocka7e24c12009-10-30 11:49:00 +000098 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +000099 void ForceContextAllocation() {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000100 ASSERT(mode_ != TEMPORARY);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000101 force_context_allocation_ = true;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100102 }
Steve Block6ded16b2010-05-10 14:33:55 +0100103 bool is_used() { return is_used_; }
104 void set_is_used(bool flag) { is_used_ = flag; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000105
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000106 int initializer_position() { return initializer_position_; }
107 void set_initializer_position(int pos) { initializer_position_ = pos; }
108
Steve Blocka7e24c12009-10-30 11:49:00 +0000109 bool IsVariable(Handle<String> n) const {
110 return !is_this() && name().is_identical_to(n);
111 }
112
Ben Murdoch589d6972011-11-30 16:04:58 +0000113 bool IsUnallocated() const { return location_ == UNALLOCATED; }
114 bool IsParameter() const { return location_ == PARAMETER; }
115 bool IsStackLocal() const { return location_ == LOCAL; }
116 bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
117 bool IsContextSlot() const { return location_ == CONTEXT; }
118 bool IsLookupSlot() const { return location_ == LOOKUP; }
Steve Block6ded16b2010-05-10 14:33:55 +0100119
Steve Blocka7e24c12009-10-30 11:49:00 +0000120 bool is_dynamic() const {
121 return (mode_ == DYNAMIC ||
122 mode_ == DYNAMIC_GLOBAL ||
123 mode_ == DYNAMIC_LOCAL);
124 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000125 bool is_const_mode() const {
126 return (mode_ == CONST ||
127 mode_ == CONST_HARMONY);
128 }
129 bool binding_needs_init() const {
130 return initialization_flag_ == kNeedsInitialization;
131 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000132
133 bool is_global() const;
134 bool is_this() const { return kind_ == THIS; }
135 bool is_arguments() const { return kind_ == ARGUMENTS; }
136
137 // True if the variable is named eval and not known to be shadowed.
138 bool is_possibly_eval() const {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000139 return IsVariable(FACTORY->eval_symbol());
Steve Blocka7e24c12009-10-30 11:49:00 +0000140 }
141
142 Variable* local_if_not_shadowed() const {
143 ASSERT(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
144 return local_if_not_shadowed_;
145 }
146
147 void set_local_if_not_shadowed(Variable* local) {
148 local_if_not_shadowed_ = local;
149 }
150
Ben Murdoch589d6972011-11-30 16:04:58 +0000151 Location location() const { return location_; }
152 int index() const { return index_; }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000153 InitializationFlag initialization_flag() const {
154 return initialization_flag_;
155 }
Ben Murdoch589d6972011-11-30 16:04:58 +0000156
157 void AllocateTo(Location location, int index) {
158 location_ = location;
159 index_ = index;
160 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000161
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000162 static int CompareIndex(Variable* const* v, Variable* const* w);
163
Steve Blocka7e24c12009-10-30 11:49:00 +0000164 private:
165 Scope* scope_;
166 Handle<String> name_;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000167 VariableMode mode_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000168 Kind kind_;
Ben Murdoch589d6972011-11-30 16:04:58 +0000169 Location location_;
170 int index_;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000171 int initializer_position_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000172
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000173 // If this field is set, this variable references the stored locally bound
174 // variable, but it might be shadowed by variable bindings introduced by
175 // non-strict 'eval' calls between the reference scope (inclusive) and the
176 // binding scope (exclusive).
Steve Blocka7e24c12009-10-30 11:49:00 +0000177 Variable* local_if_not_shadowed_;
178
Steve Block1e0659c2011-05-24 12:43:12 +0100179 // Valid as a LHS? (const and this are not valid LHS, for example)
180 bool is_valid_LHS_;
181
182 // Usage info.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000183 bool force_context_allocation_; // set by variable resolver
Steve Block1e0659c2011-05-24 12:43:12 +0100184 bool is_used_;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000185 InitializationFlag initialization_flag_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000186};
187
188
189} } // namespace v8::internal
190
191#endif // V8_VARIABLES_H_