OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef V8_VARIABLES_H_ | |
6 #define V8_VARIABLES_H_ | |
7 | |
8 #include "src/ast-value-factory.h" | |
9 #include "src/zone.h" | |
10 | |
11 namespace v8 { | |
12 namespace internal { | |
13 | |
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. | |
18 | |
19 class ClassVariable; | |
20 | |
21 class Variable: public ZoneObject { | |
22 public: | |
23 enum Kind { NORMAL, FUNCTION, CLASS, THIS, ARGUMENTS }; | |
24 | |
25 Variable(Scope* scope, const AstRawString* name, VariableMode mode, Kind kind, | |
26 InitializationFlag initialization_flag, | |
27 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned); | |
28 | |
29 virtual ~Variable() {} | |
30 | |
31 // Printing support | |
32 static const char* Mode2String(VariableMode mode); | |
33 | |
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_; } | |
39 | |
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_; | |
45 } | |
46 void ForceContextAllocation() { | |
47 force_context_allocation_ = true; | |
48 } | |
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; } | |
53 | |
54 int initializer_position() { return initializer_position_; } | |
55 void set_initializer_position(int pos) { initializer_position_ = pos; } | |
56 | |
57 bool IsVariable(Handle<String> n) const { | |
58 return !is_this() && name().is_identical_to(n); | |
59 } | |
60 | |
61 bool IsUnallocated() const { | |
62 return location_ == VariableLocation::UNALLOCATED; | |
63 } | |
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(); | |
71 } | |
72 bool IsLookupSlot() const { return location_ == VariableLocation::LOOKUP; } | |
73 bool IsGlobalObjectProperty() const; | |
74 bool IsStaticGlobalObjectProperty() const; | |
75 | |
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; | |
80 } | |
81 | |
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; } | |
86 | |
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(); | |
95 } | |
96 | |
97 ClassVariable* AsClassVariable() { | |
98 DCHECK(is_class()); | |
99 return reinterpret_cast<ClassVariable*>(this); | |
100 } | |
101 | |
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()); | |
105 } | |
106 | |
107 Variable* local_if_not_shadowed() const { | |
108 DCHECK(mode_ == DYNAMIC_LOCAL && local_if_not_shadowed_ != NULL); | |
109 return local_if_not_shadowed_; | |
110 } | |
111 | |
112 void set_local_if_not_shadowed(Variable* local) { | |
113 local_if_not_shadowed_ = local; | |
114 } | |
115 | |
116 VariableLocation location() const { return location_; } | |
117 int index() const { return index_; } | |
118 InitializationFlag initialization_flag() const { | |
119 return initialization_flag_; | |
120 } | |
121 | |
122 void AllocateTo(VariableLocation location, int index) { | |
123 location_ = location; | |
124 index_ = index; | |
125 } | |
126 | |
127 void SetFromEval() { is_from_eval_ = true; } | |
128 | |
129 static int CompareIndex(Variable* const* v, Variable* const* w); | |
130 | |
131 void RecordStrongModeReference(int start_position, int end_position) { | |
132 // Record the earliest reference to the variable. Used in error messages for | |
133 // strong mode references to undeclared variables. | |
134 if (has_strong_mode_reference_ && | |
135 strong_mode_reference_start_position_ < start_position) | |
136 return; | |
137 has_strong_mode_reference_ = true; | |
138 strong_mode_reference_start_position_ = start_position; | |
139 strong_mode_reference_end_position_ = end_position; | |
140 } | |
141 | |
142 bool has_strong_mode_reference() const { return has_strong_mode_reference_; } | |
143 int strong_mode_reference_start_position() const { | |
144 return strong_mode_reference_start_position_; | |
145 } | |
146 int strong_mode_reference_end_position() const { | |
147 return strong_mode_reference_end_position_; | |
148 } | |
149 PropertyAttributes DeclarationPropertyAttributes() const { | |
150 int property_attributes = NONE; | |
151 if (IsImmutableVariableMode(mode_)) { | |
152 property_attributes |= READ_ONLY; | |
153 } | |
154 if (is_from_eval_) { | |
155 property_attributes |= EVAL_DECLARED; | |
156 } | |
157 return static_cast<PropertyAttributes>(property_attributes); | |
158 } | |
159 | |
160 private: | |
161 Scope* scope_; | |
162 const AstRawString* name_; | |
163 VariableMode mode_; | |
164 Kind kind_; | |
165 VariableLocation location_; | |
166 int index_; | |
167 int initializer_position_; | |
168 // Tracks whether the variable is bound to a VariableProxy which is in strong | |
169 // mode, and if yes, the source location of the reference. | |
170 bool has_strong_mode_reference_; | |
171 int strong_mode_reference_start_position_; | |
172 int strong_mode_reference_end_position_; | |
173 | |
174 // If this field is set, this variable references the stored locally bound | |
175 // variable, but it might be shadowed by variable bindings introduced by | |
176 // sloppy 'eval' calls between the reference scope (inclusive) and the | |
177 // binding scope (exclusive). | |
178 Variable* local_if_not_shadowed_; | |
179 | |
180 // True if this variable is introduced by a sloppy eval | |
181 bool is_from_eval_; | |
182 | |
183 // Usage info. | |
184 bool force_context_allocation_; // set by variable resolver | |
185 bool is_used_; | |
186 InitializationFlag initialization_flag_; | |
187 MaybeAssignedFlag maybe_assigned_; | |
188 }; | |
189 | |
190 class ClassVariable : public Variable { | |
191 public: | |
192 ClassVariable(Scope* scope, const AstRawString* name, VariableMode mode, | |
193 InitializationFlag initialization_flag, | |
194 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned, | |
195 int declaration_group_start = -1) | |
196 : Variable(scope, name, mode, Variable::CLASS, initialization_flag, | |
197 maybe_assigned_flag), | |
198 declaration_group_start_(declaration_group_start) {} | |
199 | |
200 int declaration_group_start() const { return declaration_group_start_; } | |
201 void set_declaration_group_start(int declaration_group_start) { | |
202 declaration_group_start_ = declaration_group_start; | |
203 } | |
204 | |
205 private: | |
206 // For classes we keep track of consecutive groups of delcarations. They are | |
207 // needed for strong mode scoping checks. TODO(marja, rossberg): Implement | |
208 // checks for functions too. | |
209 int declaration_group_start_; | |
210 }; | |
211 } // namespace internal | |
212 } // namespace v8 | |
213 | |
214 #endif // V8_VARIABLES_H_ | |
OLD | NEW |