1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
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.
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.
28 #ifndef V8_VARIABLES_H_
29 #define V8_VARIABLES_H_
36 // 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.
41 class Variable: public ZoneObject {
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.
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.
61 // A slot in the local section on the stack. index() is the variable
62 // index in the stack frame, starting at 0.
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.
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.
76 Variable(Scope* scope,
81 InitializationFlag initialization_flag);
84 static const char* Mode2String(VariableMode mode);
86 bool IsValidLeftHandSide() { return is_valid_LHS_; }
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.
92 Scope* scope() const { return scope_; }
94 Handle<String> name() const { return name_; }
95 VariableMode mode() const { return mode_; }
96 bool is_accessed_from_inner_scope() const {
97 return is_accessed_from_inner_scope_;
99 void MarkAsAccessedFromInnerScope() {
100 ASSERT(mode_ != TEMPORARY);
101 is_accessed_from_inner_scope_ = true;
103 bool is_used() { return is_used_; }
104 void set_is_used(bool flag) { is_used_ = flag; }
106 bool IsVariable(Handle<String> n) const {
107 return !is_this() && name().is_identical_to(n);
110 bool IsUnallocated() const { return location_ == UNALLOCATED; }
111 bool IsParameter() const { return location_ == PARAMETER; }
112 bool IsStackLocal() const { return location_ == LOCAL; }
113 bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
114 bool IsContextSlot() const { return location_ == CONTEXT; }
115 bool IsLookupSlot() const { return location_ == LOOKUP; }
117 bool is_dynamic() const {
118 return (mode_ == DYNAMIC ||
119 mode_ == DYNAMIC_GLOBAL ||
120 mode_ == DYNAMIC_LOCAL);
122 bool is_const_mode() const {
123 return (mode_ == CONST ||
124 mode_ == CONST_HARMONY);
126 bool binding_needs_init() const {
127 return initialization_flag_ == kNeedsInitialization;
130 bool is_global() const;
131 bool is_this() const { return kind_ == THIS; }
132 bool is_arguments() const { return kind_ == ARGUMENTS; }
134 // True if the variable is named eval and not known to be shadowed.
135 bool is_possibly_eval() const {
136 return IsVariable(FACTORY->eval_symbol());
139 Variable* local_if_not_shadowed() const {
140 ASSERT(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
141 return local_if_not_shadowed_;
144 void set_local_if_not_shadowed(Variable* local) {
145 local_if_not_shadowed_ = local;
148 Location location() const { return location_; }
149 int index() const { return index_; }
150 InitializationFlag initialization_flag() const {
151 return initialization_flag_;
154 void AllocateTo(Location location, int index) {
155 location_ = location;
161 Handle<String> name_;
167 // If this field is set, this variable references the stored locally bound
168 // variable, but it might be shadowed by variable bindings introduced by
169 // non-strict 'eval' calls between the reference scope (inclusive) and the
170 // binding scope (exclusive).
171 Variable* local_if_not_shadowed_;
173 // Valid as a LHS? (const and this are not valid LHS, for example)
177 bool is_accessed_from_inner_scope_; // set by variable resolver
179 InitializationFlag initialization_flag_;
183 } } // namespace v8::internal
185 #endif // V8_VARIABLES_H_