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 force_context_allocation_ = true;
49 bool is_used() { return is_used_; }
50 void set_is_used() { is_used_ = true; }
51 MaybeAssignedFlag maybe_assigned() const { return maybe_assigned_; }
52 void set_maybe_assigned() { maybe_assigned_ = kMaybeAssigned; }
54 int initializer_position() { return initializer_position_; }
55 void set_initializer_position(int pos) { initializer_position_ = pos; }
57 bool IsVariable(Handle<String> n) const {
58 return !is_this() && name().is_identical_to(n);
61 bool IsUnallocated() const {
62 return location_ == VariableLocation::UNALLOCATED;
64 bool IsParameter() const { return location_ == VariableLocation::PARAMETER; }
65 bool IsStackLocal() const { return location_ == VariableLocation::LOCAL; }
66 bool IsStackAllocated() const { return IsParameter() || IsStackLocal(); }
67 bool IsContextSlot() const { return location_ == VariableLocation::CONTEXT; }
68 bool IsGlobalSlot() const { return location_ == VariableLocation::GLOBAL; }
69 bool IsUnallocatedOrGlobalSlot() const {
70 return IsUnallocated() || IsGlobalSlot();
72 bool IsLookupSlot() const { return location_ == VariableLocation::LOOKUP; }
73 bool IsGlobalObjectProperty() const;
74 bool IsStaticGlobalObjectProperty() const;
76 bool is_dynamic() const { return IsDynamicVariableMode(mode_); }
77 bool is_const_mode() const { return IsImmutableVariableMode(mode_); }
78 bool binding_needs_init() const {
79 return initialization_flag_ == kNeedsInitialization;
82 bool is_function() const { return kind_ == FUNCTION; }
83 bool is_class() const { return kind_ == CLASS; }
84 bool is_this() const { return kind_ == THIS; }
85 bool is_arguments() const { return kind_ == ARGUMENTS; }
87 // For script scopes, the "this" binding is provided by a ScriptContext added
88 // to the global's ScriptContextTable. This binding might not statically
89 // resolve to a Variable::THIS binding, instead being DYNAMIC_LOCAL. However
90 // any variable named "this" does indeed refer to a Variable::THIS binding;
91 // the grammar ensures this to be the case. So wherever a "this" binding
92 // might be provided by the global, use HasThisName instead of is_this().
93 bool HasThisName(Isolate* isolate) const {
94 return is_this() || *name() == *isolate->factory()->this_string();
97 ClassVariable* AsClassVariable() {
99 return reinterpret_cast<ClassVariable*>(this);
102 // True if the variable is named eval and not known to be shadowed.
103 bool is_possibly_eval(Isolate* isolate) const {
104 return IsVariable(isolate->factory()->eval_string());
107 Variable* local_if_not_shadowed() const {
108 DCHECK(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL);
109 return local_if_not_shadowed_;
112 void set_local_if_not_shadowed(Variable* local) {
113 local_if_not_shadowed_ = local;
116 VariableLocation location() const { return location_; }
117 int index() const { return index_; }
118 InitializationFlag initialization_flag() const {
119 return initialization_flag_;
122 void AllocateTo(VariableLocation location, int index) {
123 location_ = location;
127 static int CompareIndex(Variable* const* v, Variable* const* w);
129 void RecordStrongModeReference(int start_position, int end_position) {
130 // Record the earliest reference to the variable. Used in error messages for
131 // strong mode references to undeclared variables.
132 if (has_strong_mode_reference_ &&
133 strong_mode_reference_start_position_ < start_position)
135 has_strong_mode_reference_ = true;
136 strong_mode_reference_start_position_ = start_position;
137 strong_mode_reference_end_position_ = end_position;
140 bool has_strong_mode_reference() const { return has_strong_mode_reference_; }
141 int strong_mode_reference_start_position() const {
142 return strong_mode_reference_start_position_;
144 int strong_mode_reference_end_position() const {
145 return strong_mode_reference_end_position_;
150 const AstRawString* name_;
153 VariableLocation location_;
155 int initializer_position_;
156 // Tracks whether the variable is bound to a VariableProxy which is in strong
157 // mode, and if yes, the source location of the reference.
158 bool has_strong_mode_reference_;
159 int strong_mode_reference_start_position_;
160 int strong_mode_reference_end_position_;
162 // If this field is set, this variable references the stored locally bound
163 // variable, but it might be shadowed by variable bindings introduced by
164 // sloppy 'eval' calls between the reference scope (inclusive) and the
165 // binding scope (exclusive).
166 Variable* local_if_not_shadowed_;
169 bool force_context_allocation_; // set by variable resolver
171 InitializationFlag initialization_flag_;
172 MaybeAssignedFlag maybe_assigned_;
175 class ClassVariable : public Variable {
177 ClassVariable(Scope* scope, const AstRawString* name, VariableMode mode,
178 InitializationFlag initialization_flag,
179 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned,
180 int declaration_group_start = -1)
181 : Variable(scope, name, mode, Variable::CLASS, initialization_flag,
182 maybe_assigned_flag),
183 declaration_group_start_(declaration_group_start) {}
185 int declaration_group_start() const { return declaration_group_start_; }
186 void set_declaration_group_start(int declaration_group_start) {
187 declaration_group_start_ = declaration_group_start;
191 // For classes we keep track of consecutive groups of delcarations. They are
192 // needed for strong mode scoping checks. TODO(marja, rossberg): Implement
193 // checks for functions too.
194 int declaration_group_start_;
196 } } // namespace v8::internal
198 #endif // V8_VARIABLES_H_