1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef V8_VARIABLES_H_
6 #define V8_VARIABLES_H_
8 #include "src/ast-value-factory.h"
9 #include "src/interface.h"
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.
20 class Variable: public ZoneObject {
22 enum Kind { NORMAL, THIS, NEW_TARGET, ARGUMENTS };
25 // Before and during variable allocation, a variable whose location is
26 // not yet determined. After allocation, a variable looked up as a
27 // property on the global object (and possibly absent). name() is the
28 // variable name, index() is invalid.
31 // A slot in the parameter section on the stack. index() is the
32 // parameter index, counting left-to-right. The receiver is index -1;
33 // the first parameter is index 0.
36 // A slot in the local section on the stack. index() is the variable
37 // index in the stack frame, starting at 0.
40 // An indexed slot in a heap context. index() is the variable index in
41 // the context object on the heap, starting at 0. scope() is the
42 // corresponding scope.
45 // A named slot in a heap context. name() is the variable name in the
46 // context object on the heap, with lookup starting at the current
47 // context. index() is invalid.
51 Variable(Scope* scope, const AstRawString* name, VariableMode mode,
52 bool is_valid_ref, Kind kind, InitializationFlag initialization_flag,
53 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned,
54 Interface* interface = Interface::NewValue());
57 static const char* Mode2String(VariableMode mode);
59 bool IsValidReference() { return is_valid_ref_; }
61 // The source code for an eval() call may refer to a variable that is
62 // in an outer scope about which we don't know anything (it may not
63 // be the script scope). scope() is NULL in that case. Currently the
64 // scope is only used to follow the context chain length.
65 Scope* scope() const { return scope_; }
67 Handle<String> name() const { return name_->string(); }
68 const AstRawString* raw_name() const { return name_; }
69 VariableMode mode() const { return mode_; }
70 bool has_forced_context_allocation() const {
71 return force_context_allocation_;
73 void ForceContextAllocation() {
74 DCHECK(mode_ != TEMPORARY);
75 force_context_allocation_ = true;
77 bool is_used() { return is_used_; }
78 void set_is_used() { is_used_ = true; }
79 MaybeAssignedFlag maybe_assigned() const { return maybe_assigned_; }
80 void set_maybe_assigned() { maybe_assigned_ = kMaybeAssigned; }
82 int initializer_position() { return initializer_position_; }
83 void set_initializer_position(int pos) { initializer_position_ = pos; }
85 bool IsVariable(Handle<String> n) const {
86 return !is_this() && name().is_identical_to(n);
89 bool IsUnallocated() const { return location_ == UNALLOCATED; }
90 bool IsParameter() const { return location_ == PARAMETER; }
91 bool IsStackLocal() const { return location_ == LOCAL; }
92 bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
93 bool IsContextSlot() const { return location_ == CONTEXT; }
94 bool IsLookupSlot() const { return location_ == LOOKUP; }
95 bool IsGlobalObjectProperty() const;
97 bool is_dynamic() const { return IsDynamicVariableMode(mode_); }
98 bool is_const_mode() const { return IsImmutableVariableMode(mode_); }
99 bool binding_needs_init() const {
100 return initialization_flag_ == kNeedsInitialization;
103 bool is_this() const { return kind_ == THIS; }
104 bool is_new_target() const { return kind_ == NEW_TARGET; }
105 bool is_arguments() const { return kind_ == ARGUMENTS; }
107 // True if the variable is named eval and not known to be shadowed.
108 bool is_possibly_eval(Isolate* isolate) const {
109 return IsVariable(isolate->factory()->eval_string());
112 Variable* local_if_not_shadowed() const {
113 DCHECK(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
114 return local_if_not_shadowed_;
117 void set_local_if_not_shadowed(Variable* local) {
118 local_if_not_shadowed_ = local;
121 Location location() const { return location_; }
122 int index() const { return index_; }
123 InitializationFlag initialization_flag() const {
124 return initialization_flag_;
126 Interface* interface() const { return interface_; }
128 void AllocateTo(Location location, int index) {
129 location_ = location;
133 static int CompareIndex(Variable* const* v, Variable* const* w);
137 const AstRawString* name_;
142 int initializer_position_;
144 // If this field is set, this variable references the stored locally bound
145 // variable, but it might be shadowed by variable bindings introduced by
146 // sloppy 'eval' calls between the reference scope (inclusive) and the
147 // binding scope (exclusive).
148 Variable* local_if_not_shadowed_;
150 // Valid as a reference? (const and this are not valid, for example)
154 bool force_context_allocation_; // set by variable resolver
156 InitializationFlag initialization_flag_;
157 MaybeAssignedFlag maybe_assigned_;
160 Interface* interface_;
164 } } // namespace v8::internal
166 #endif // V8_VARIABLES_H_