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, 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_arguments() const { return kind_ == ARGUMENTS; }
108 // For script scopes, the "this" binding is provided by a ScriptContext added
109 // to the global's ScriptContextTable. This binding might not statically
110 // resolve to a Variable::THIS binding, instead being DYNAMIC_LOCAL. However
111 // any variable named "this" does indeed refer to a Variable::THIS binding;
112 // the grammar ensures this to be the case. So wherever a "this" binding
113 // might be provided by the global, use HasThisName instead of is_this().
114 bool HasThisName(Isolate* isolate) const {
115 return is_this() || *name() == *isolate->factory()->this_string();
118 ClassVariable* AsClassVariable() {
120 return reinterpret_cast<ClassVariable*>(this);
123 // True if the variable is named eval and not known to be shadowed.
124 bool is_possibly_eval(Isolate* isolate) const {
125 return IsVariable(isolate->factory()->eval_string());
128 Variable* local_if_not_shadowed() const {
129 DCHECK(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
130 return local_if_not_shadowed_;
133 void set_local_if_not_shadowed(Variable* local) {
134 local_if_not_shadowed_ = local;
137 Location location() const { return location_; }
138 int index() const { return index_; }
139 InitializationFlag initialization_flag() const {
140 return initialization_flag_;
143 void AllocateTo(Location location, int index) {
144 location_ = location;
148 static int CompareIndex(Variable* const* v, Variable* const* w);
150 void RecordStrongModeReference(int start_position, int end_position) {
151 // Record the earliest reference to the variable. Used in error messages for
152 // strong mode references to undeclared variables.
153 if (has_strong_mode_reference_ &&
154 strong_mode_reference_start_position_ < start_position)
156 has_strong_mode_reference_ = true;
157 strong_mode_reference_start_position_ = start_position;
158 strong_mode_reference_end_position_ = end_position;
161 bool has_strong_mode_reference() const { return has_strong_mode_reference_; }
162 int strong_mode_reference_start_position() const {
163 return strong_mode_reference_start_position_;
165 int strong_mode_reference_end_position() const {
166 return strong_mode_reference_end_position_;
171 const AstRawString* name_;
176 int initializer_position_;
177 // Tracks whether the variable is bound to a VariableProxy which is in strong
178 // mode, and if yes, the source location of the reference.
179 bool has_strong_mode_reference_;
180 int strong_mode_reference_start_position_;
181 int strong_mode_reference_end_position_;
183 // If this field is set, this variable references the stored locally bound
184 // variable, but it might be shadowed by variable bindings introduced by
185 // sloppy 'eval' calls between the reference scope (inclusive) and the
186 // binding scope (exclusive).
187 Variable* local_if_not_shadowed_;
190 bool force_context_allocation_; // set by variable resolver
192 InitializationFlag initialization_flag_;
193 MaybeAssignedFlag maybe_assigned_;
196 class ClassVariable : public Variable {
198 ClassVariable(Scope* scope, const AstRawString* name, VariableMode mode,
199 InitializationFlag initialization_flag,
200 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned,
201 int declaration_group_start = -1)
202 : Variable(scope, name, mode, Variable::CLASS, initialization_flag,
203 maybe_assigned_flag),
204 declaration_group_start_(declaration_group_start) {}
206 int declaration_group_start() const { return declaration_group_start_; }
207 void set_declaration_group_start(int declaration_group_start) {
208 declaration_group_start_ = declaration_group_start;
212 // For classes we keep track of consecutive groups of delcarations. They are
213 // needed for strong mode scoping checks. TODO(marja, rossberg): Implement
214 // checks for functions too.
215 int declaration_group_start_;
217 } } // namespace v8::internal
219 #endif // V8_VARIABLES_H_