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 ClassVariable* AsClassVariable() {
110 return reinterpret_cast<ClassVariable*>(this);
113 // True if the variable is named eval and not known to be shadowed.
114 bool is_possibly_eval(Isolate* isolate) const {
115 return IsVariable(isolate->factory()->eval_string());
118 Variable* local_if_not_shadowed() const {
119 DCHECK(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
120 return local_if_not_shadowed_;
123 void set_local_if_not_shadowed(Variable* local) {
124 local_if_not_shadowed_ = local;
127 Location location() const { return location_; }
128 int index() const { return index_; }
129 InitializationFlag initialization_flag() const {
130 return initialization_flag_;
133 void AllocateTo(Location location, int index) {
134 location_ = location;
138 static int CompareIndex(Variable* const* v, Variable* const* w);
140 void RecordStrongModeReference(int start_position, int end_position) {
141 // Record the earliest reference to the variable. Used in error messages for
142 // strong mode references to undeclared variables.
143 if (has_strong_mode_reference_ &&
144 strong_mode_reference_start_position_ < start_position)
146 has_strong_mode_reference_ = true;
147 strong_mode_reference_start_position_ = start_position;
148 strong_mode_reference_end_position_ = end_position;
151 bool has_strong_mode_reference() const { return has_strong_mode_reference_; }
152 int strong_mode_reference_start_position() const {
153 return strong_mode_reference_start_position_;
155 int strong_mode_reference_end_position() const {
156 return strong_mode_reference_end_position_;
161 const AstRawString* name_;
166 int initializer_position_;
167 // Tracks whether the variable is bound to a VariableProxy which is in strong
168 // mode, and if yes, the source location of the reference.
169 bool has_strong_mode_reference_;
170 int strong_mode_reference_start_position_;
171 int strong_mode_reference_end_position_;
173 // If this field is set, this variable references the stored locally bound
174 // variable, but it might be shadowed by variable bindings introduced by
175 // sloppy 'eval' calls between the reference scope (inclusive) and the
176 // binding scope (exclusive).
177 Variable* local_if_not_shadowed_;
180 bool force_context_allocation_; // set by variable resolver
182 InitializationFlag initialization_flag_;
183 MaybeAssignedFlag maybe_assigned_;
186 class ClassVariable : public Variable {
188 ClassVariable(Scope* scope, const AstRawString* name, VariableMode mode,
189 InitializationFlag initialization_flag,
190 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned,
191 int declaration_group_start = -1)
192 : Variable(scope, name, mode, Variable::CLASS, initialization_flag,
193 maybe_assigned_flag),
194 declaration_group_start_(declaration_group_start) {}
196 int declaration_group_start() const { return declaration_group_start_; }
197 void set_declaration_group_start(int declaration_group_start) {
198 declaration_group_start_ = declaration_group_start;
202 // For classes we keep track of consecutive groups of delcarations. They are
203 // needed for strong mode scoping checks. TODO(marja, rossberg): Implement
204 // checks for functions too.
205 int declaration_group_start_;
207 } } // namespace v8::internal
209 #endif // V8_VARIABLES_H_