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/interface.h"
14 // The AST refers to variables via VariableProxies - placeholders for the actual
15 // variables. Variables themselves are never directly referred to from the AST,
16 // they are maintained by scopes, and referred to from VariableProxies and Slots
17 // after binding and variable allocation.
19 class Variable: public ZoneObject {
28 // Before and during variable allocation, a variable whose location is
29 // not yet determined. After allocation, a variable looked up as a
30 // property on the global object (and possibly absent). name() is the
31 // variable name, index() is invalid.
34 // A slot in the parameter section on the stack. index() is the
35 // parameter index, counting left-to-right. The receiver is index -1;
36 // the first parameter is index 0.
39 // A slot in the local section on the stack. index() is the variable
40 // index in the stack frame, starting at 0.
43 // An indexed slot in a heap context. index() is the variable index in
44 // the context object on the heap, starting at 0. scope() is the
45 // corresponding scope.
48 // A named slot in a heap context. name() is the variable name in the
49 // context object on the heap, with lookup starting at the current
50 // context. index() is invalid.
54 Variable(Scope* scope,
59 InitializationFlag initialization_flag,
60 Interface* interface = Interface::NewValue());
63 static const char* Mode2String(VariableMode mode);
65 bool IsValidReference() { return is_valid_ref_; }
67 // The source code for an eval() call may refer to a variable that is
68 // in an outer scope about which we don't know anything (it may not
69 // be the global scope). scope() is NULL in that case. Currently the
70 // scope is only used to follow the context chain length.
71 Scope* scope() const { return scope_; }
73 Handle<String> name() const { return name_; }
74 VariableMode mode() const { return mode_; }
75 bool has_forced_context_allocation() const {
76 return force_context_allocation_;
78 void ForceContextAllocation() {
79 ASSERT(mode_ != TEMPORARY);
80 force_context_allocation_ = true;
82 bool is_used() { return is_used_; }
83 void set_is_used(bool flag) { is_used_ = flag; }
85 int initializer_position() { return initializer_position_; }
86 void set_initializer_position(int pos) { initializer_position_ = pos; }
88 bool IsVariable(Handle<String> n) const {
89 return !is_this() && name().is_identical_to(n);
92 bool IsUnallocated() const { return location_ == UNALLOCATED; }
93 bool IsParameter() const { return location_ == PARAMETER; }
94 bool IsStackLocal() const { return location_ == LOCAL; }
95 bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
96 bool IsContextSlot() const { return location_ == CONTEXT; }
97 bool IsLookupSlot() const { return location_ == LOOKUP; }
98 bool IsGlobalObjectProperty() const;
100 bool is_dynamic() const { return IsDynamicVariableMode(mode_); }
101 bool is_const_mode() const { return IsImmutableVariableMode(mode_); }
102 bool binding_needs_init() const {
103 return initialization_flag_ == kNeedsInitialization;
106 bool is_this() const { return kind_ == THIS; }
107 bool is_arguments() const { return kind_ == ARGUMENTS; }
109 // True if the variable is named eval and not known to be shadowed.
110 bool is_possibly_eval(Isolate* isolate) const {
111 return IsVariable(isolate->factory()->eval_string());
114 Variable* local_if_not_shadowed() const {
115 ASSERT(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
116 return local_if_not_shadowed_;
119 void set_local_if_not_shadowed(Variable* local) {
120 local_if_not_shadowed_ = local;
123 Location location() const { return location_; }
124 int index() const { return index_; }
125 InitializationFlag initialization_flag() const {
126 return initialization_flag_;
128 Interface* interface() const { return interface_; }
130 void AllocateTo(Location location, int index) {
131 location_ = location;
135 static int CompareIndex(Variable* const* v, Variable* const* w);
139 Handle<String> name_;
144 int initializer_position_;
146 // If this field is set, this variable references the stored locally bound
147 // variable, but it might be shadowed by variable bindings introduced by
148 // sloppy 'eval' calls between the reference scope (inclusive) and the
149 // binding scope (exclusive).
150 Variable* local_if_not_shadowed_;
152 // Valid as a reference? (const and this are not valid, for example)
156 bool force_context_allocation_; // set by variable resolver
158 InitializationFlag initialization_flag_;
161 Interface* interface_;
165 } } // namespace v8::internal
167 #endif // V8_VARIABLES_H_