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.
21 class Variable: public ZoneObject {
23 enum Kind { NORMAL, FUNCTION, CLASS, THIS, NEW_TARGET, ARGUMENTS };
26 // Before and during variable allocation, a variable whose location is
27 // not yet determined. After allocation, a variable looked up as a
28 // property on the global object (and possibly absent). name() is the
29 // variable name, index() is invalid.
32 // A slot in the parameter section on the stack. index() is the
33 // parameter index, counting left-to-right. The receiver is index -1;
34 // the first parameter is index 0.
37 // A slot in the local section on the stack. index() is the variable
38 // index in the stack frame, starting at 0.
41 // An indexed slot in a heap context. index() is the variable index in
42 // the context object on the heap, starting at 0. scope() is the
43 // corresponding scope.
46 // A named slot in a heap context. name() is the variable name in the
47 // context object on the heap, with lookup starting at the current
48 // context. index() is invalid.
52 Variable(Scope* scope, const AstRawString* name, VariableMode mode, Kind kind,
53 InitializationFlag initialization_flag,
54 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned);
56 virtual ~Variable() {}
59 static const char* Mode2String(VariableMode mode);
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_function() const { return kind_ == FUNCTION; }
104 bool is_class() const { return kind_ == CLASS; }
105 bool is_this() const { return kind_ == THIS; }
106 bool is_new_target() const { return kind_ == NEW_TARGET; }
107 bool is_arguments() const { return kind_ == ARGUMENTS; }
109 ClassVariable* AsClassVariable() {
111 return reinterpret_cast<ClassVariable*>(this);
114 // True if the variable is named eval and not known to be shadowed.
115 bool is_possibly_eval(Isolate* isolate) const {
116 return IsVariable(isolate->factory()->eval_string());
119 Variable* local_if_not_shadowed() const {
120 DCHECK(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
121 return local_if_not_shadowed_;
124 void set_local_if_not_shadowed(Variable* local) {
125 local_if_not_shadowed_ = local;
128 Location location() const { return location_; }
129 int index() const { return index_; }
130 InitializationFlag initialization_flag() const {
131 return initialization_flag_;
134 void AllocateTo(Location location, int index) {
135 location_ = location;
139 static int CompareIndex(Variable* const* v, Variable* const* w);
141 void RecordStrongModeReference(int start_position, int end_position) {
142 // Record the earliest reference to the variable. Used in error messages for
143 // strong mode references to undeclared variables.
144 if (has_strong_mode_reference_ &&
145 strong_mode_reference_start_position_ < start_position)
147 has_strong_mode_reference_ = true;
148 strong_mode_reference_start_position_ = start_position;
149 strong_mode_reference_end_position_ = end_position;
152 bool has_strong_mode_reference() const { return has_strong_mode_reference_; }
153 int strong_mode_reference_start_position() const {
154 return strong_mode_reference_start_position_;
156 int strong_mode_reference_end_position() const {
157 return strong_mode_reference_end_position_;
162 const AstRawString* name_;
167 int initializer_position_;
168 // Tracks whether the variable is bound to a VariableProxy which is in strong
169 // mode, and if yes, the source location of the reference.
170 bool has_strong_mode_reference_;
171 int strong_mode_reference_start_position_;
172 int strong_mode_reference_end_position_;
174 // If this field is set, this variable references the stored locally bound
175 // variable, but it might be shadowed by variable bindings introduced by
176 // sloppy 'eval' calls between the reference scope (inclusive) and the
177 // binding scope (exclusive).
178 Variable* local_if_not_shadowed_;
181 bool force_context_allocation_; // set by variable resolver
183 InitializationFlag initialization_flag_;
184 MaybeAssignedFlag maybe_assigned_;
187 class ClassVariable : public Variable {
189 ClassVariable(Scope* scope, const AstRawString* name, VariableMode mode,
190 Kind kind, InitializationFlag initialization_flag,
191 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned,
192 int declaration_group_start = -1)
193 : Variable(scope, name, mode, kind, initialization_flag,
194 maybe_assigned_flag),
195 declaration_group_start_(declaration_group_start),
196 corresponding_outer_class_variable_(nullptr) {}
198 int declaration_group_start() const { return declaration_group_start_; }
200 ClassVariable* corresponding_outer_class_variable() const {
201 return corresponding_outer_class_variable_;
203 void set_corresponding_outer_class_variable(ClassVariable* var) {
204 corresponding_outer_class_variable_ = var;
208 // For classes we keep track of consecutive groups of delcarations. They are
209 // needed for strong mode scoping checks. TODO(marja, rossberg): Implement
210 // checks for functions too.
211 int declaration_group_start_;
212 ClassVariable* corresponding_outer_class_variable_;
214 } } // namespace v8::internal
216 #endif // V8_VARIABLES_H_