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"
10 #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 {
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.
35 // A slot in the parameter section on the stack. index() is the
36 // parameter index, counting left-to-right. The receiver is index -1;
37 // the first parameter is index 0.
40 // A slot in the local section on the stack. index() is the variable
41 // index in the stack frame, starting at 0.
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.
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.
55 Variable(Scope* scope,
56 const AstString* name,
60 InitializationFlag initialization_flag,
61 Interface* interface = Interface::NewValue());
64 static const char* Mode2String(VariableMode mode);
66 bool IsValidReference() { return is_valid_ref_; }
68 // The source code for an eval() call may refer to a variable that is
69 // in an outer scope about which we don't know anything (it may not
70 // be the global scope). scope() is NULL in that case. Currently the
71 // scope is only used to follow the context chain length.
72 Scope* scope() const { return scope_; }
74 Handle<String> name() const { return name_->string(); }
75 const AstString* raw_name() const { return name_; }
76 VariableMode mode() const { return mode_; }
77 bool has_forced_context_allocation() const {
78 return force_context_allocation_;
80 void ForceContextAllocation() {
81 ASSERT(mode_ != TEMPORARY);
82 force_context_allocation_ = true;
84 bool is_used() { return is_used_; }
85 void set_is_used(bool flag) { is_used_ = flag; }
87 int initializer_position() { return initializer_position_; }
88 void set_initializer_position(int pos) { initializer_position_ = pos; }
90 bool IsVariable(Handle<String> n) const {
91 return !is_this() && name().is_identical_to(n);
94 bool IsUnallocated() const { return location_ == UNALLOCATED; }
95 bool IsParameter() const { return location_ == PARAMETER; }
96 bool IsStackLocal() const { return location_ == LOCAL; }
97 bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
98 bool IsContextSlot() const { return location_ == CONTEXT; }
99 bool IsLookupSlot() const { return location_ == LOOKUP; }
100 bool IsGlobalObjectProperty() const;
102 bool is_dynamic() const { return IsDynamicVariableMode(mode_); }
103 bool is_const_mode() const { return IsImmutableVariableMode(mode_); }
104 bool binding_needs_init() const {
105 return initialization_flag_ == kNeedsInitialization;
108 bool is_this() const { return kind_ == THIS; }
109 bool is_arguments() const { return kind_ == ARGUMENTS; }
111 // True if the variable is named eval and not known to be shadowed.
112 bool is_possibly_eval(Isolate* isolate) const {
113 return IsVariable(isolate->factory()->eval_string());
116 Variable* local_if_not_shadowed() const {
117 ASSERT(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
118 return local_if_not_shadowed_;
121 void set_local_if_not_shadowed(Variable* local) {
122 local_if_not_shadowed_ = local;
125 Location location() const { return location_; }
126 int index() const { return index_; }
127 InitializationFlag initialization_flag() const {
128 return initialization_flag_;
130 Interface* interface() const { return interface_; }
132 void AllocateTo(Location location, int index) {
133 location_ = location;
137 static int CompareIndex(Variable* const* v, Variable* const* w);
141 const AstString* name_;
146 int initializer_position_;
148 // If this field is set, this variable references the stored locally bound
149 // variable, but it might be shadowed by variable bindings introduced by
150 // sloppy 'eval' calls between the reference scope (inclusive) and the
151 // binding scope (exclusive).
152 Variable* local_if_not_shadowed_;
154 // Valid as a reference? (const and this are not valid, for example)
158 bool force_context_allocation_; // set by variable resolver
160 InitializationFlag initialization_flag_;
163 Interface* interface_;
167 } } // namespace v8::internal
169 #endif // V8_VARIABLES_H_