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"
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 {
21 enum Kind { NORMAL, THIS, NEW_TARGET, ARGUMENTS };
24 // Before and during variable allocation, a variable whose location is
25 // not yet determined. After allocation, a variable looked up as a
26 // property on the global object (and possibly absent). name() is the
27 // variable name, index() is invalid.
30 // A slot in the parameter section on the stack. index() is the
31 // parameter index, counting left-to-right. The receiver is index -1;
32 // the first parameter is index 0.
35 // A slot in the local section on the stack. index() is the variable
36 // index in the stack frame, starting at 0.
39 // An indexed slot in a heap context. index() is the variable index in
40 // the context object on the heap, starting at 0. scope() is the
41 // corresponding scope.
44 // A named slot in a heap context. name() is the variable name in the
45 // context object on the heap, with lookup starting at the current
46 // context. index() is invalid.
50 Variable(Scope* scope, const AstRawString* name, VariableMode mode,
51 bool is_valid_ref, Kind kind, InitializationFlag initialization_flag,
52 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned);
55 static const char* Mode2String(VariableMode mode);
57 bool IsValidReference() { return is_valid_ref_; }
59 // The source code for an eval() call may refer to a variable that is
60 // in an outer scope about which we don't know anything (it may not
61 // be the script scope). scope() is NULL in that case. Currently the
62 // scope is only used to follow the context chain length.
63 Scope* scope() const { return scope_; }
65 Handle<String> name() const { return name_->string(); }
66 const AstRawString* raw_name() const { return name_; }
67 VariableMode mode() const { return mode_; }
68 bool has_forced_context_allocation() const {
69 return force_context_allocation_;
71 void ForceContextAllocation() {
72 DCHECK(mode_ != TEMPORARY);
73 force_context_allocation_ = true;
75 bool is_used() { return is_used_; }
76 void set_is_used() { is_used_ = true; }
77 MaybeAssignedFlag maybe_assigned() const { return maybe_assigned_; }
78 void set_maybe_assigned() { maybe_assigned_ = kMaybeAssigned; }
80 int initializer_position() { return initializer_position_; }
81 void set_initializer_position(int pos) { initializer_position_ = pos; }
83 bool IsVariable(Handle<String> n) const {
84 return !is_this() && name().is_identical_to(n);
87 bool IsUnallocated() const { return location_ == UNALLOCATED; }
88 bool IsParameter() const { return location_ == PARAMETER; }
89 bool IsStackLocal() const { return location_ == LOCAL; }
90 bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
91 bool IsContextSlot() const { return location_ == CONTEXT; }
92 bool IsLookupSlot() const { return location_ == LOOKUP; }
93 bool IsGlobalObjectProperty() const;
95 bool is_dynamic() const { return IsDynamicVariableMode(mode_); }
96 bool is_const_mode() const { return IsImmutableVariableMode(mode_); }
97 bool binding_needs_init() const {
98 return initialization_flag_ == kNeedsInitialization;
101 bool is_this() const { return kind_ == THIS; }
102 bool is_new_target() const { return kind_ == NEW_TARGET; }
103 bool is_arguments() const { return kind_ == ARGUMENTS; }
105 // True if the variable is named eval and not known to be shadowed.
106 bool is_possibly_eval(Isolate* isolate) const {
107 return IsVariable(isolate->factory()->eval_string());
110 Variable* local_if_not_shadowed() const {
111 DCHECK(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
112 return local_if_not_shadowed_;
115 void set_local_if_not_shadowed(Variable* local) {
116 local_if_not_shadowed_ = local;
119 Location location() const { return location_; }
120 int index() const { return index_; }
121 InitializationFlag initialization_flag() const {
122 return initialization_flag_;
125 void AllocateTo(Location location, int index) {
126 location_ = location;
130 static int CompareIndex(Variable* const* v, Variable* const* w);
134 const AstRawString* name_;
139 int initializer_position_;
141 // If this field is set, this variable references the stored locally bound
142 // variable, but it might be shadowed by variable bindings introduced by
143 // sloppy 'eval' calls between the reference scope (inclusive) and the
144 // binding scope (exclusive).
145 Variable* local_if_not_shadowed_;
147 // Valid as a reference? (const and this are not valid, for example)
151 bool force_context_allocation_; // set by variable resolver
153 InitializationFlag initialization_flag_;
154 MaybeAssignedFlag maybe_assigned_;
158 } } // namespace v8::internal
160 #endif // V8_VARIABLES_H_