karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // 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" |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 32 | #include "interface.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 34 | namespace v8 { |
| 35 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | // The AST refers to variables via VariableProxies - placeholders for the actual |
| 38 | // variables. Variables themselves are never directly referred to from the AST, |
| 39 | // they are maintained by scopes, and referred to from VariableProxies and Slots |
| 40 | // after binding and variable allocation. |
| 41 | |
| 42 | class Variable: public ZoneObject { |
| 43 | public: |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 44 | enum Kind { |
| 45 | NORMAL, |
| 46 | THIS, |
| 47 | ARGUMENTS |
| 48 | }; |
| 49 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 50 | enum Location { |
| 51 | // Before and during variable allocation, a variable whose location is |
| 52 | // not yet determined. After allocation, a variable looked up as a |
| 53 | // property on the global object (and possibly absent). name() is the |
| 54 | // variable name, index() is invalid. |
| 55 | UNALLOCATED, |
| 56 | |
| 57 | // A slot in the parameter section on the stack. index() is the |
| 58 | // parameter index, counting left-to-right. The reciever is index -1; |
| 59 | // the first parameter is index 0. |
| 60 | PARAMETER, |
| 61 | |
| 62 | // A slot in the local section on the stack. index() is the variable |
| 63 | // index in the stack frame, starting at 0. |
| 64 | LOCAL, |
| 65 | |
| 66 | // An indexed slot in a heap context. index() is the variable index in |
| 67 | // the context object on the heap, starting at 0. scope() is the |
| 68 | // corresponding scope. |
| 69 | CONTEXT, |
| 70 | |
| 71 | // A named slot in a heap context. name() is the variable name in the |
| 72 | // context object on the heap, with lookup starting at the current |
| 73 | // context. index() is invalid. |
| 74 | LOOKUP |
| 75 | }; |
| 76 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 77 | Variable(Scope* scope, |
| 78 | Handle<String> name, |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 79 | VariableMode mode, |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 80 | bool is_valid_lhs, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 81 | Kind kind, |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 82 | InitializationFlag initialization_flag, |
| 83 | Interface* interface = Interface::NewValue()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 84 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 85 | // Printing support |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 86 | static const char* Mode2String(VariableMode mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 87 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 88 | bool IsValidLeftHandSide() { return is_valid_LHS_; } |
| 89 | |
| 90 | // The source code for an eval() call may refer to a variable that is |
| 91 | // in an outer scope about which we don't know anything (it may not |
| 92 | // be the global scope). scope() is NULL in that case. Currently the |
| 93 | // scope is only used to follow the context chain length. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 94 | Scope* scope() const { return scope_; } |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 95 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 96 | Handle<String> name() const { return name_; } |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 97 | VariableMode mode() const { return mode_; } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 98 | bool has_forced_context_allocation() const { |
| 99 | return force_context_allocation_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 100 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 101 | void ForceContextAllocation() { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 102 | ASSERT(mode_ != TEMPORARY); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 103 | force_context_allocation_ = true; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 104 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 105 | bool is_used() { return is_used_; } |
| 106 | void set_is_used(bool flag) { is_used_ = flag; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 107 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 108 | int initializer_position() { return initializer_position_; } |
| 109 | void set_initializer_position(int pos) { initializer_position_ = pos; } |
| 110 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 111 | bool IsVariable(Handle<String> n) const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 112 | return !is_this() && name().is_identical_to(n); |
| 113 | } |
| 114 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 115 | bool IsUnallocated() const { return location_ == UNALLOCATED; } |
| 116 | bool IsParameter() const { return location_ == PARAMETER; } |
| 117 | bool IsStackLocal() const { return location_ == LOCAL; } |
| 118 | bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); } |
| 119 | bool IsContextSlot() const { return location_ == CONTEXT; } |
| 120 | bool IsLookupSlot() const { return location_ == LOOKUP; } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 121 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 122 | bool is_dynamic() const { |
| 123 | return (mode_ == DYNAMIC || |
| 124 | mode_ == DYNAMIC_GLOBAL || |
| 125 | mode_ == DYNAMIC_LOCAL); |
| 126 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 127 | bool is_const_mode() const { |
| 128 | return (mode_ == CONST || |
| 129 | mode_ == CONST_HARMONY); |
| 130 | } |
| 131 | bool binding_needs_init() const { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 132 | return initialization_flag_ == kNeedsInitialization; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 133 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 134 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 135 | bool is_global() const; |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 136 | bool is_this() const { return kind_ == THIS; } |
| 137 | bool is_arguments() const { return kind_ == ARGUMENTS; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 138 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 139 | // True if the variable is named eval and not known to be shadowed. |
| 140 | bool is_possibly_eval() const { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 141 | return IsVariable(FACTORY->eval_symbol()); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 142 | } |
| 143 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 144 | Variable* local_if_not_shadowed() const { |
| 145 | ASSERT(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL); |
| 146 | return local_if_not_shadowed_; |
| 147 | } |
| 148 | |
| 149 | void set_local_if_not_shadowed(Variable* local) { |
| 150 | local_if_not_shadowed_ = local; |
| 151 | } |
| 152 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 153 | Location location() const { return location_; } |
| 154 | int index() const { return index_; } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 155 | InitializationFlag initialization_flag() const { |
| 156 | return initialization_flag_; |
| 157 | } |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 158 | Interface* interface() const { return interface_; } |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 159 | |
| 160 | void AllocateTo(Location location, int index) { |
| 161 | location_ = location; |
| 162 | index_ = index; |
| 163 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 164 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 165 | static int CompareIndex(Variable* const* v, Variable* const* w); |
| 166 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 167 | private: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 168 | Scope* scope_; |
| 169 | Handle<String> name_; |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 170 | VariableMode mode_; |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 171 | Kind kind_; |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 172 | Location location_; |
| 173 | int index_; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 174 | int initializer_position_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 175 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 176 | // If this field is set, this variable references the stored locally bound |
| 177 | // variable, but it might be shadowed by variable bindings introduced by |
| 178 | // non-strict 'eval' calls between the reference scope (inclusive) and the |
| 179 | // binding scope (exclusive). |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 180 | Variable* local_if_not_shadowed_; |
| 181 | |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 182 | // Valid as a LHS? (const and this are not valid LHS, for example) |
| 183 | bool is_valid_LHS_; |
| 184 | |
| 185 | // Usage info. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 186 | bool force_context_allocation_; // set by variable resolver |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 187 | bool is_used_; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 188 | InitializationFlag initialization_flag_; |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 189 | |
| 190 | // Module type info. |
| 191 | Interface* interface_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | |
| 195 | } } // namespace v8::internal |
| 196 | |
| 197 | #endif // V8_VARIABLES_H_ |