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 void set_mode(VariableMode mode) {
71 // Don't use this unless you have a very good reason
74 bool has_forced_context_allocation() const {
75 return force_context_allocation_;
77 void ForceContextAllocation() {
78 DCHECK(mode_ != TEMPORARY);
79 force_context_allocation_ = true;
81 bool is_used() { return is_used_; }
82 void set_is_used() { is_used_ = true; }
83 MaybeAssignedFlag maybe_assigned() const { return maybe_assigned_; }
84 void set_maybe_assigned() { maybe_assigned_ = kMaybeAssigned; }
86 int initializer_position() { return initializer_position_; }
87 void set_initializer_position(int pos) { initializer_position_ = pos; }
89 bool IsVariable(Handle<String> n) const {
90 return !is_this() && name().is_identical_to(n);
93 bool IsUnallocated() const { return location_ == UNALLOCATED; }
94 bool IsParameter() const { return location_ == PARAMETER; }
95 bool IsStackLocal() const { return location_ == LOCAL; }
96 bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
97 bool IsContextSlot() const { return location_ == CONTEXT; }
98 bool IsLookupSlot() const { return location_ == LOOKUP; }
99 bool IsGlobalObjectProperty() const;
101 bool is_dynamic() const { return IsDynamicVariableMode(mode_); }
102 bool is_const_mode() const { return IsImmutableVariableMode(mode_); }
103 bool binding_needs_init() const {
104 return initialization_flag_ == kNeedsInitialization;
107 bool is_function() const { return kind_ == FUNCTION; }
108 bool is_class() const { return kind_ == CLASS; }
109 bool is_this() const { return kind_ == THIS; }
110 bool is_new_target() const { return kind_ == NEW_TARGET; }
111 bool is_arguments() const { return kind_ == ARGUMENTS; }
113 ClassVariable* AsClassVariable() {
115 return reinterpret_cast<ClassVariable*>(this);
118 // True if the variable is named eval and not known to be shadowed.
119 bool is_possibly_eval(Isolate* isolate) const {
120 return IsVariable(isolate->factory()->eval_string());
123 Variable* local_if_not_shadowed() const {
124 DCHECK(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
125 return local_if_not_shadowed_;
128 void set_local_if_not_shadowed(Variable* local) {
129 local_if_not_shadowed_ = local;
132 Location location() const { return location_; }
133 int index() const { return index_; }
134 InitializationFlag initialization_flag() const {
135 return initialization_flag_;
138 void AllocateTo(Location location, int index) {
139 location_ = location;
143 static int CompareIndex(Variable* const* v, Variable* const* w);
145 void RecordStrongModeReference(int start_position, int end_position) {
146 // Record the earliest reference to the variable. Used in error messages for
147 // strong mode references to undeclared variables.
148 if (has_strong_mode_reference_ &&
149 strong_mode_reference_start_position_ < start_position)
151 has_strong_mode_reference_ = true;
152 strong_mode_reference_start_position_ = start_position;
153 strong_mode_reference_end_position_ = end_position;
156 bool has_strong_mode_reference() const { return has_strong_mode_reference_; }
157 int strong_mode_reference_start_position() const {
158 return strong_mode_reference_start_position_;
160 int strong_mode_reference_end_position() const {
161 return strong_mode_reference_end_position_;
166 const AstRawString* name_;
171 int initializer_position_;
172 // Tracks whether the variable is bound to a VariableProxy which is in strong
173 // mode, and if yes, the source location of the reference.
174 bool has_strong_mode_reference_;
175 int strong_mode_reference_start_position_;
176 int strong_mode_reference_end_position_;
178 // If this field is set, this variable references the stored locally bound
179 // variable, but it might be shadowed by variable bindings introduced by
180 // sloppy 'eval' calls between the reference scope (inclusive) and the
181 // binding scope (exclusive).
182 Variable* local_if_not_shadowed_;
185 bool force_context_allocation_; // set by variable resolver
187 InitializationFlag initialization_flag_;
188 MaybeAssignedFlag maybe_assigned_;
191 class ClassVariable : public Variable {
193 ClassVariable(Scope* scope, const AstRawString* name, VariableMode mode,
194 InitializationFlag initialization_flag,
195 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned,
196 int declaration_group_start = -1)
197 : Variable(scope, name, mode, Variable::CLASS, initialization_flag,
198 maybe_assigned_flag),
199 declaration_group_start_(declaration_group_start) {}
201 int declaration_group_start() const { return declaration_group_start_; }
202 void set_declaration_group_start(int declaration_group_start) {
203 declaration_group_start_ = declaration_group_start;
207 // For classes we keep track of consecutive groups of delcarations. They are
208 // needed for strong mode scoping checks. TODO(marja, rossberg): Implement
209 // checks for functions too.
210 int declaration_group_start_;
212 } } // namespace v8::internal
214 #endif // V8_VARIABLES_H_