v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
variables.h
Go to the documentation of this file.
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
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 #include "interface.h"
33 
34 namespace v8 {
35 namespace internal {
36 
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:
44  enum Kind {
48  };
49 
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.
56 
57  // A slot in the parameter section on the stack. index() is the
58  // parameter index, counting left-to-right. The receiver is index -1;
59  // the first parameter is index 0.
61 
62  // A slot in the local section on the stack. index() is the variable
63  // index in the stack frame, starting at 0.
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.
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.
75  };
76 
80  bool is_valid_lhs,
81  Kind kind,
84 
85  // Printing support
86  static const char* Mode2String(VariableMode mode);
87 
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.
94  Scope* scope() const { return scope_; }
95 
96  Handle<String> name() const { return name_; }
97  VariableMode mode() const { return mode_; }
99  return force_context_allocation_;
100  }
102  ASSERT(mode_ != TEMPORARY);
103  force_context_allocation_ = true;
104  }
105  bool is_used() { return is_used_; }
106  void set_is_used(bool flag) { is_used_ = flag; }
107 
108  int initializer_position() { return initializer_position_; }
109  void set_initializer_position(int pos) { initializer_position_ = pos; }
110 
111  bool IsVariable(Handle<String> n) const {
112  return !is_this() && name().is_identical_to(n);
113  }
114 
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; }
121  bool IsGlobalObjectProperty() const;
122 
123  bool is_dynamic() const { return IsDynamicVariableMode(mode_); }
124  bool is_const_mode() const { return IsImmutableVariableMode(mode_); }
125  bool binding_needs_init() const {
126  return initialization_flag_ == kNeedsInitialization;
127  }
128 
129  bool is_this() const { return kind_ == THIS; }
130  bool is_arguments() const { return kind_ == ARGUMENTS; }
131 
132  // True if the variable is named eval and not known to be shadowed.
133  bool is_possibly_eval(Isolate* isolate) const {
134  return IsVariable(isolate->factory()->eval_string());
135  }
136 
138  ASSERT(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
139  return local_if_not_shadowed_;
140  }
141 
143  local_if_not_shadowed_ = local;
144  }
145 
146  Location location() const { return location_; }
147  int index() const { return index_; }
149  return initialization_flag_;
150  }
151  Interface* interface() const { return interface_; }
152 
154  location_ = location;
155  index_ = index;
156  }
157 
158  static int CompareIndex(Variable* const* v, Variable* const* w);
159 
160  private:
161  Scope* scope_;
162  Handle<String> name_;
163  VariableMode mode_;
164  Kind kind_;
165  Location location_;
166  int index_;
167  int initializer_position_;
168 
169  // If this field is set, this variable references the stored locally bound
170  // variable, but it might be shadowed by variable bindings introduced by
171  // sloppy 'eval' calls between the reference scope (inclusive) and the
172  // binding scope (exclusive).
173  Variable* local_if_not_shadowed_;
174 
175  // Valid as a LHS? (const and this are not valid LHS, for example)
176  bool is_valid_LHS_;
177 
178  // Usage info.
179  bool force_context_allocation_; // set by variable resolver
180  bool is_used_;
181  InitializationFlag initialization_flag_;
182 
183  // Module type info.
184  Interface* interface_;
185 };
186 
187 
188 } } // namespace v8::internal
189 
190 #endif // V8_VARIABLES_H_
void AllocateTo(Location location, int index)
Definition: variables.h:153
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter NULL
Definition: flags.cc:269
bool IsValidLeftHandSide()
Definition: variables.h:88
void ForceContextAllocation()
Definition: variables.h:101
Variable * local_if_not_shadowed() const
Definition: variables.h:137
void set_local_if_not_shadowed(Variable *local)
Definition: variables.h:142
Location location() const
Definition: variables.h:146
bool IsStackAllocated() const
Definition: variables.h:118
InitializationFlag initialization_flag() const
Definition: variables.h:148
#define ASSERT(condition)
Definition: checks.h:329
bool is_const_mode() const
Definition: variables.h:124
Interface * interface() const
Definition: variables.h:151
bool is_this() const
Definition: variables.h:129
bool IsLookupSlot() const
Definition: variables.h:120
Factory * factory()
Definition: isolate.h:995
Handle< String > name() const
Definition: variables.h:96
kInstanceClassNameOffset flag
Definition: objects-inl.h:5115
bool is_dynamic() const
Definition: variables.h:123
static Interface * NewValue()
Definition: interface.h:66
VariableMode mode() const
Definition: variables.h:97
bool has_forced_context_allocation() const
Definition: variables.h:98
static int CompareIndex(Variable *const *v, Variable *const *w)
Definition: variables.cc:94
Scope * scope() const
Definition: variables.h:94
int index() const
Definition: variables.h:147
bool IsContextSlot() const
Definition: variables.h:119
static const char * Mode2String(VariableMode mode)
Definition: variables.cc:40
Variable(Scope *scope, Handle< String > name, VariableMode mode, bool is_valid_lhs, Kind kind, InitializationFlag initialization_flag, Interface *interface=Interface::NewValue())
Definition: variables.cc:58
bool IsVariable(Handle< String > n) const
Definition: variables.h:111
bool IsStackLocal() const
Definition: variables.h:117
bool IsParameter() const
Definition: variables.h:116
bool is_arguments() const
Definition: variables.h:130
bool binding_needs_init() const
Definition: variables.h:125
bool IsUnallocated() const
Definition: variables.h:115
bool IsDynamicVariableMode(VariableMode mode)
Definition: v8globals.h:498
void set_is_used(bool flag)
Definition: variables.h:106
bool IsGlobalObjectProperty() const
Definition: variables.cc:85
bool IsImmutableVariableMode(VariableMode mode)
Definition: v8globals.h:513
bool is_possibly_eval(Isolate *isolate) const
Definition: variables.h:133
void set_initializer_position(int pos)
Definition: variables.h:109