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 };
25 Variable(Scope* scope, const AstRawString* name, VariableMode mode, Kind kind,
26 InitializationFlag initialization_flag,
27 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned);
29 virtual ~Variable() {}
32 static const char* Mode2String(VariableMode mode);
34 // The source code for an eval() call may refer to a variable that is
35 // in an outer scope about which we don't know anything (it may not
36 // be the script scope). scope() is NULL in that case. Currently the
37 // scope is only used to follow the context chain length.
38 Scope* scope() const { return scope_; }
40 Handle<String> name() const { return name_->string(); }
41 const AstRawString* raw_name() const { return name_; }
42 VariableMode mode() const { return mode_; }
43 bool has_forced_context_allocation() const {
44 return force_context_allocation_;
46 void ForceContextAllocation() {
47 DCHECK(mode_ != TEMPORARY);
48 force_context_allocation_ = true;
50 bool is_used() { return is_used_; }
51 void set_is_used() { is_used_ = true; }
52 MaybeAssignedFlag maybe_assigned() const { return maybe_assigned_; }
53 void set_maybe_assigned() { maybe_assigned_ = kMaybeAssigned; }
55 int initializer_position() { return initializer_position_; }
56 void set_initializer_position(int pos) { initializer_position_ = pos; }
58 bool IsVariable(Handle<String> n) const {
59 return !is_this() && name().is_identical_to(n);
62 bool IsUnallocated() const {
63 return location_ == VariableLocation::UNALLOCATED;
65 bool IsParameter() const { return location_ == VariableLocation::PARAMETER; }
66 bool IsStackLocal() const { return location_ == VariableLocation::LOCAL; }
67 bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
68 bool IsContextSlot() const { return location_ == VariableLocation::CONTEXT; }
69 bool IsGlobalSlot() const { return location_ == VariableLocation::GLOBAL; }
70 bool IsUnallocatedOrGlobalSlot() const {
71 return IsUnallocated() || IsGlobalSlot();
73 bool IsLookupSlot() const { return location_ == VariableLocation::LOOKUP; }
74 bool IsGlobalObjectProperty() const;
75 bool IsStaticGlobalObjectProperty() const;
77 bool is_dynamic() const { return IsDynamicVariableMode(mode_); }
78 bool is_const_mode() const { return IsImmutableVariableMode(mode_); }
79 bool binding_needs_init() const {
80 return initialization_flag_ == kNeedsInitialization;
83 bool is_function() const { return kind_ == FUNCTION; }
84 bool is_class() const { return kind_ == CLASS; }
85 bool is_this() const { return kind_ == THIS; }
86 bool is_arguments() const { return kind_ == ARGUMENTS; }
88 // For script scopes, the "this" binding is provided by a ScriptContext added
89 // to the global's ScriptContextTable. This binding might not statically
90 // resolve to a Variable::THIS binding, instead being DYNAMIC_LOCAL. However
91 // any variable named "this" does indeed refer to a Variable::THIS binding;
92 // the grammar ensures this to be the case. So wherever a "this" binding
93 // might be provided by the global, use HasThisName instead of is_this().
94 bool HasThisName(Isolate* isolate) const {
95 return is_this() || *name() == *isolate->factory()->this_string();
98 ClassVariable* AsClassVariable() {
100 return reinterpret_cast<ClassVariable*>(this);
103 // True if the variable is named eval and not known to be shadowed.
104 bool is_possibly_eval(Isolate* isolate) const {
105 return IsVariable(isolate->factory()->eval_string());
108 Variable* local_if_not_shadowed() const {
109 DCHECK(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
110 return local_if_not_shadowed_;
113 void set_local_if_not_shadowed(Variable* local) {
114 local_if_not_shadowed_ = local;
117 VariableLocation location() const { return location_; }
118 int index() const { return index_; }
119 InitializationFlag initialization_flag() const {
120 return initialization_flag_;
123 void AllocateTo(VariableLocation location, int index) {
124 location_ = location;
128 static int CompareIndex(Variable* const* v, Variable* const* w);
130 void RecordStrongModeReference(int start_position, int end_position) {
131 // Record the earliest reference to the variable. Used in error messages for
132 // strong mode references to undeclared variables.
133 if (has_strong_mode_reference_ &&
134 strong_mode_reference_start_position_ < start_position)
136 has_strong_mode_reference_ = true;
137 strong_mode_reference_start_position_ = start_position;
138 strong_mode_reference_end_position_ = end_position;
141 bool has_strong_mode_reference() const { return has_strong_mode_reference_; }
142 int strong_mode_reference_start_position() const {
143 return strong_mode_reference_start_position_;
145 int strong_mode_reference_end_position() const {
146 return strong_mode_reference_end_position_;
151 const AstRawString* name_;
154 VariableLocation location_;
156 int initializer_position_;
157 // Tracks whether the variable is bound to a VariableProxy which is in strong
158 // mode, and if yes, the source location of the reference.
159 bool has_strong_mode_reference_;
160 int strong_mode_reference_start_position_;
161 int strong_mode_reference_end_position_;
163 // If this field is set, this variable references the stored locally bound
164 // variable, but it might be shadowed by variable bindings introduced by
165 // sloppy 'eval' calls between the reference scope (inclusive) and the
166 // binding scope (exclusive).
167 Variable* local_if_not_shadowed_;
170 bool force_context_allocation_; // set by variable resolver
172 InitializationFlag initialization_flag_;
173 MaybeAssignedFlag maybe_assigned_;
176 class ClassVariable : public Variable {
178 ClassVariable(Scope* scope, const AstRawString* name, VariableMode mode,
179 InitializationFlag initialization_flag,
180 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned,
181 int declaration_group_start = -1)
182 : Variable(scope, name, mode, Variable::CLASS, initialization_flag,
183 maybe_assigned_flag),
184 declaration_group_start_(declaration_group_start) {}
186 int declaration_group_start() const { return declaration_group_start_; }
187 void set_declaration_group_start(int declaration_group_start) {
188 declaration_group_start_ = declaration_group_start;
192 // For classes we keep track of consecutive groups of delcarations. They are
193 // needed for strong mode scoping checks. TODO(marja, rossberg): Implement
194 // checks for functions too.
195 int declaration_group_start_;
197 } } // namespace v8::internal
199 #endif // V8_VARIABLES_H_