| Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_VARIABLES_H_ |
| 6 | #define V8_VARIABLES_H_ |
| 7 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8 | #include "src/ast-value-factory.h" |
| 9 | #include "src/interface.h" |
| 10 | #include "src/zone.h" |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 11 | |
| 12 | namespace v8 { |
| 13 | namespace internal { |
| 14 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 15 | // The AST refers to variables via VariableProxies - placeholders for the actual |
| 16 | // variables. Variables themselves are never directly referred to from the AST, |
| 17 | // they are maintained by scopes, and referred to from VariableProxies and Slots |
| 18 | // after binding and variable allocation. |
| 19 | |
| 20 | class Variable: public ZoneObject { |
| 21 | public: |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 22 | enum Kind { |
| 23 | NORMAL, |
| 24 | THIS, |
| 25 | ARGUMENTS |
| 26 | }; |
| 27 | |
| Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 28 | enum Location { |
| 29 | // Before and during variable allocation, a variable whose location is |
| 30 | // not yet determined. After allocation, a variable looked up as a |
| 31 | // property on the global object (and possibly absent). name() is the |
| 32 | // variable name, index() is invalid. |
| 33 | UNALLOCATED, |
| 34 | |
| 35 | // A slot in the parameter section on the stack. index() is the |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 36 | // parameter index, counting left-to-right. The receiver is index -1; |
| Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 37 | // the first parameter is index 0. |
| 38 | PARAMETER, |
| 39 | |
| 40 | // A slot in the local section on the stack. index() is the variable |
| 41 | // index in the stack frame, starting at 0. |
| 42 | LOCAL, |
| 43 | |
| 44 | // An indexed slot in a heap context. index() is the variable index in |
| 45 | // the context object on the heap, starting at 0. scope() is the |
| 46 | // corresponding scope. |
| 47 | CONTEXT, |
| 48 | |
| 49 | // A named slot in a heap context. name() is the variable name in the |
| 50 | // context object on the heap, with lookup starting at the current |
| 51 | // context. index() is invalid. |
| 52 | LOOKUP |
| 53 | }; |
| 54 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 55 | Variable(Scope* scope, const AstRawString* name, VariableMode mode, |
| 56 | bool is_valid_ref, Kind kind, InitializationFlag initialization_flag, |
| 57 | MaybeAssignedFlag maybe_assigned_flag = kNotAssigned, |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 58 | Interface* interface = Interface::NewValue()); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 59 | |
| 60 | // Printing support |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 61 | static const char* Mode2String(VariableMode mode); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 62 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 63 | bool IsValidReference() { return is_valid_ref_; } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 64 | |
| 65 | // The source code for an eval() call may refer to a variable that is |
| 66 | // in an outer scope about which we don't know anything (it may not |
| 67 | // be the global scope). scope() is NULL in that case. Currently the |
| 68 | // scope is only used to follow the context chain length. |
| Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 69 | Scope* scope() const { return scope_; } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 70 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 71 | Handle<String> name() const { return name_->string(); } |
| 72 | const AstRawString* raw_name() const { return name_; } |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 73 | VariableMode mode() const { return mode_; } |
| 74 | bool has_forced_context_allocation() const { |
| 75 | return force_context_allocation_; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 76 | } |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 77 | void ForceContextAllocation() { |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 78 | DCHECK(mode_ != TEMPORARY); |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 79 | force_context_allocation_ = true; |
| Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 80 | } |
| Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 81 | bool is_used() { return is_used_; } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 82 | void set_is_used() { is_used_ = true; } |
| 83 | MaybeAssignedFlag maybe_assigned() const { return maybe_assigned_; } |
| 84 | void set_maybe_assigned() { maybe_assigned_ = kMaybeAssigned; } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 85 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 86 | int initializer_position() { return initializer_position_; } |
| 87 | void set_initializer_position(int pos) { initializer_position_ = pos; } |
| 88 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 89 | bool IsVariable(Handle<String> n) const { |
| 90 | return !is_this() && name().is_identical_to(n); |
| 91 | } |
| 92 | |
| Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 93 | bool IsUnallocated() const { return location_ == UNALLOCATED; } |
| 94 | bool IsParameter() const { return location_ == PARAMETER; } |
| 95 | bool IsStackLocal() const { return location_ == LOCAL; } |
| 96 | bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); } |
| 97 | bool IsContextSlot() const { return location_ == CONTEXT; } |
| 98 | bool IsLookupSlot() const { return location_ == LOOKUP; } |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 99 | bool IsGlobalObjectProperty() const; |
| Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 100 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 101 | bool is_dynamic() const { return IsDynamicVariableMode(mode_); } |
| 102 | bool is_const_mode() const { return IsImmutableVariableMode(mode_); } |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 103 | bool binding_needs_init() const { |
| 104 | return initialization_flag_ == kNeedsInitialization; |
| 105 | } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 106 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 107 | bool is_this() const { return kind_ == THIS; } |
| 108 | bool is_arguments() const { return kind_ == ARGUMENTS; } |
| 109 | |
| 110 | // True if the variable is named eval and not known to be shadowed. |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 111 | bool is_possibly_eval(Isolate* isolate) const { |
| 112 | return IsVariable(isolate->factory()->eval_string()); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | Variable* local_if_not_shadowed() const { |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 116 | DCHECK(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 117 | return local_if_not_shadowed_; |
| 118 | } |
| 119 | |
| 120 | void set_local_if_not_shadowed(Variable* local) { |
| 121 | local_if_not_shadowed_ = local; |
| 122 | } |
| 123 | |
| Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 124 | Location location() const { return location_; } |
| 125 | int index() const { return index_; } |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 126 | InitializationFlag initialization_flag() const { |
| 127 | return initialization_flag_; |
| 128 | } |
| 129 | Interface* interface() const { return interface_; } |
| Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 130 | |
| 131 | void AllocateTo(Location location, int index) { |
| 132 | location_ = location; |
| 133 | index_ = index; |
| 134 | } |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 135 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 136 | static int CompareIndex(Variable* const* v, Variable* const* w); |
| 137 | |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 138 | private: |
| 139 | Scope* scope_; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 140 | const AstRawString* name_; |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 141 | VariableMode mode_; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 142 | Kind kind_; |
| Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 143 | Location location_; |
| 144 | int index_; |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 145 | int initializer_position_; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 146 | |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 147 | // If this field is set, this variable references the stored locally bound |
| 148 | // variable, but it might be shadowed by variable bindings introduced by |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 149 | // sloppy 'eval' calls between the reference scope (inclusive) and the |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 150 | // binding scope (exclusive). |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 151 | Variable* local_if_not_shadowed_; |
| 152 | |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 153 | // Valid as a reference? (const and this are not valid, for example) |
| 154 | bool is_valid_ref_; |
| Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 155 | |
| 156 | // Usage info. |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 157 | bool force_context_allocation_; // set by variable resolver |
| Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 158 | bool is_used_; |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 159 | InitializationFlag initialization_flag_; |
| Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 160 | MaybeAssignedFlag maybe_assigned_; |
| Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 161 | |
| 162 | // Module type info. |
| 163 | Interface* interface_; |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | |
| 167 | } } // namespace v8::internal |
| 168 | |
| 169 | #endif // V8_VARIABLES_H_ |