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, FUNCTION, CLASS, 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, Kind kind,
51 InitializationFlag initialization_flag,
52 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned);
55 static const char* Mode2String(VariableMode mode);
57 // The source code for an eval() call may refer to a variable that is
58 // in an outer scope about which we don't know anything (it may not
59 // be the script scope). scope() is NULL in that case. Currently the
60 // scope is only used to follow the context chain length.
61 Scope* scope() const { return scope_; }
63 Handle<String> name() const { return name_->string(); }
64 const AstRawString* raw_name() const { return name_; }
65 VariableMode mode() const { return mode_; }
66 bool has_forced_context_allocation() const {
67 return force_context_allocation_;
69 void ForceContextAllocation() {
70 DCHECK(mode_ != TEMPORARY);
71 force_context_allocation_ = true;
73 bool is_used() { return is_used_; }
74 void set_is_used() { is_used_ = true; }
75 MaybeAssignedFlag maybe_assigned() const { return maybe_assigned_; }
76 void set_maybe_assigned() { maybe_assigned_ = kMaybeAssigned; }
78 int initializer_position() { return initializer_position_; }
79 void set_initializer_position(int pos) { initializer_position_ = pos; }
81 bool IsVariable(Handle<String> n) const {
82 return !is_this() && name().is_identical_to(n);
85 bool IsUnallocated() const { return location_ == UNALLOCATED; }
86 bool IsParameter() const { return location_ == PARAMETER; }
87 bool IsStackLocal() const { return location_ == LOCAL; }
88 bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
89 bool IsContextSlot() const { return location_ == CONTEXT; }
90 bool IsLookupSlot() const { return location_ == LOOKUP; }
91 bool IsGlobalObjectProperty() const;
93 bool is_dynamic() const { return IsDynamicVariableMode(mode_); }
94 bool is_const_mode() const { return IsImmutableVariableMode(mode_); }
95 bool binding_needs_init() const {
96 return initialization_flag_ == kNeedsInitialization;
99 bool is_function() const { return kind_ == FUNCTION; }
100 bool is_class() const { return kind_ == CLASS; }
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);
132 void RecordStrongModeReference(int start_position, int end_position) {
133 // Record the earliest reference to the variable. Used in error messages for
134 // strong mode references to undeclared variables.
135 if (has_strong_mode_reference_ &&
136 strong_mode_reference_start_position_ < start_position)
138 has_strong_mode_reference_ = true;
139 strong_mode_reference_start_position_ = start_position;
140 strong_mode_reference_end_position_ = end_position;
143 bool has_strong_mode_reference() const { return has_strong_mode_reference_; }
144 int strong_mode_reference_start_position() const {
145 return strong_mode_reference_start_position_;
147 int strong_mode_reference_end_position() const {
148 return strong_mode_reference_end_position_;
153 const AstRawString* name_;
158 int initializer_position_;
159 // Tracks whether the variable is bound to a VariableProxy which is in strong
160 // mode, and if yes, the source location of the reference.
161 bool has_strong_mode_reference_;
162 int strong_mode_reference_start_position_;
163 int strong_mode_reference_end_position_;
165 // If this field is set, this variable references the stored locally bound
166 // variable, but it might be shadowed by variable bindings introduced by
167 // sloppy 'eval' calls between the reference scope (inclusive) and the
168 // binding scope (exclusive).
169 Variable* local_if_not_shadowed_;
172 bool force_context_allocation_; // set by variable resolver
174 InitializationFlag initialization_flag_;
175 MaybeAssignedFlag maybe_assigned_;
179 } } // namespace v8::internal
181 #endif // V8_VARIABLES_H_