blob: c6e2a4650e1e1112fd54fc699fdd3c0476409b2b [file] [log] [blame]
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000030#include "scopes.h"
31
32#include "bootstrapper.h"
33#include "compiler.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000034#include "prettyprinter.h"
35#include "scopeinfo.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
whesse@chromium.org030d38e2011-07-13 13:23:34 +000037#include "allocation-inl.h"
38
kasperl@chromium.org71affb52009-05-26 05:44:31 +000039namespace v8 {
40namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041
42// ----------------------------------------------------------------------------
43// A Zone allocator for use with LocalsMap.
44
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000045// TODO(isolates): It is probably worth it to change the Allocator class to
46// take a pointer to an isolate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047class ZoneAllocator: public Allocator {
48 public:
49 /* nothing to do */
50 virtual ~ZoneAllocator() {}
51
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000052 virtual void* New(size_t size) { return ZONE->New(static_cast<int>(size)); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053
54 /* ignored - Zone is freed in one fell swoop */
55 virtual void Delete(void* p) {}
56};
57
58
59static ZoneAllocator LocalsMapAllocator;
60
61
62// ----------------------------------------------------------------------------
63// Implementation of LocalsMap
64//
65// Note: We are storing the handle locations as key values in the hash map.
66// When inserting a new variable via Declare(), we rely on the fact that
67// the handle location remains alive for the duration of that variable
68// use. Because a Variable holding a handle with the same location exists
69// this is ensured.
70
71static bool Match(void* key1, void* key2) {
72 String* name1 = *reinterpret_cast<String**>(key1);
73 String* name2 = *reinterpret_cast<String**>(key2);
74 ASSERT(name1->IsSymbol());
75 ASSERT(name2->IsSymbol());
76 return name1 == name2;
77}
78
79
80// Dummy constructor
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000081VariableMap::VariableMap(bool gotta_love_static_overloading) : HashMap() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000082
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000083VariableMap::VariableMap() : HashMap(Match, &LocalsMapAllocator, 8) {}
84VariableMap::~VariableMap() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000085
86
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000087Variable* VariableMap::Declare(Scope* scope,
88 Handle<String> name,
89 Variable::Mode mode,
90 bool is_valid_lhs,
91 Variable::Kind kind) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092 HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), true);
93 if (p->value == NULL) {
94 // The variable has not been declared yet -> insert it.
95 ASSERT(p->key == name.location());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000096 p->value = new Variable(scope, name, mode, is_valid_lhs, kind);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097 }
98 return reinterpret_cast<Variable*>(p->value);
99}
100
101
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000102Variable* VariableMap::Lookup(Handle<String> name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103 HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), false);
104 if (p != NULL) {
105 ASSERT(*reinterpret_cast<String**>(p->key) == *name);
106 ASSERT(p->value != NULL);
107 return reinterpret_cast<Variable*>(p->value);
108 }
109 return NULL;
110}
111
112
113// ----------------------------------------------------------------------------
114// Implementation of Scope
115
116
117// Dummy constructor
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000118Scope::Scope(Type type)
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000119 : inner_scopes_(0),
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000120 variables_(false),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121 temps_(0),
122 params_(0),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123 unresolved_(0),
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000124 decls_(0),
125 already_resolved_(false) {
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000126 SetDefaults(type, NULL, Handle<SerializedScopeInfo>::null());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000127}
128
129
130Scope::Scope(Scope* outer_scope, Type type)
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000131 : inner_scopes_(4),
132 variables_(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133 temps_(4),
134 params_(4),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135 unresolved_(16),
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000136 decls_(4),
137 already_resolved_(false) {
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000138 SetDefaults(type, outer_scope, Handle<SerializedScopeInfo>::null());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139 // At some point we might want to provide outer scopes to
140 // eval scopes (by walking the stack and reading the scope info).
141 // In that case, the ASSERT below needs to be adjusted.
142 ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL));
143 ASSERT(!HasIllegalRedeclaration());
144}
145
146
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000147Scope::Scope(Scope* inner_scope, Handle<SerializedScopeInfo> scope_info)
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000148 : inner_scopes_(4),
149 variables_(),
150 temps_(4),
151 params_(4),
152 unresolved_(16),
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000153 decls_(4),
154 already_resolved_(true) {
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000155 ASSERT(!scope_info.is_null());
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000156 SetDefaults(FUNCTION_SCOPE, NULL, scope_info);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000157 if (scope_info->HasHeapAllocatedLocals()) {
158 num_heap_slots_ = scope_info_->NumberOfContextSlots();
159 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000160 AddInnerScope(inner_scope);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000161}
162
163
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000164Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name)
165 : inner_scopes_(1),
166 variables_(),
167 temps_(0),
168 params_(0),
169 unresolved_(0),
170 decls_(0),
171 already_resolved_(true) {
172 SetDefaults(CATCH_SCOPE, NULL, Handle<SerializedScopeInfo>::null());
173 AddInnerScope(inner_scope);
174 ++num_var_or_const_;
175 Variable* variable = variables_.Declare(this,
176 catch_variable_name,
177 Variable::VAR,
178 true, // Valid left-hand side.
179 Variable::NORMAL);
180 AllocateHeapSlot(variable);
181}
182
183
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000184void Scope::SetDefaults(Type type,
185 Scope* outer_scope,
186 Handle<SerializedScopeInfo> scope_info) {
187 outer_scope_ = outer_scope;
188 type_ = type;
189 scope_name_ = FACTORY->empty_symbol();
190 dynamics_ = NULL;
191 receiver_ = NULL;
192 function_ = NULL;
193 arguments_ = NULL;
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000194 illegal_redecl_ = NULL;
195 scope_inside_with_ = false;
196 scope_contains_with_ = false;
197 scope_calls_eval_ = false;
198 // Inherit the strict mode from the parent scope.
199 strict_mode_ = (outer_scope != NULL) && outer_scope->strict_mode_;
200 outer_scope_calls_eval_ = false;
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000201 outer_scope_calls_non_strict_eval_ = false;
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000202 inner_scope_calls_eval_ = false;
203 outer_scope_is_eval_scope_ = false;
204 force_eager_compilation_ = false;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000205 num_var_or_const_ = 0;
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000206 num_stack_slots_ = 0;
207 num_heap_slots_ = 0;
208 scope_info_ = scope_info;
209}
210
211
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000212Scope* Scope::DeserializeScopeChain(CompilationInfo* info,
213 Scope* global_scope) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000214 // Reconstruct the outer scope chain from a closure's context chain.
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000215 ASSERT(!info->closure().is_null());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000216 Context* context = info->closure()->context();
217 Scope* current_scope = NULL;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000218 Scope* innermost_scope = NULL;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000219 bool contains_with = false;
220 while (!context->IsGlobalContext()) {
221 if (context->IsWithContext()) {
222 // All the inner scopes are inside a with.
223 contains_with = true;
224 for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) {
225 s->scope_inside_with_ = true;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000226 }
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000227 } else {
228 if (context->IsFunctionContext()) {
229 SerializedScopeInfo* scope_info =
230 context->closure()->shared()->scope_info();
231 current_scope =
232 new Scope(current_scope, Handle<SerializedScopeInfo>(scope_info));
233 } else {
234 ASSERT(context->IsCatchContext());
235 String* name = String::cast(context->extension());
236 current_scope = new Scope(current_scope, Handle<String>(name));
237 }
238 if (contains_with) current_scope->RecordWithStatement();
239 if (innermost_scope == NULL) innermost_scope = current_scope;
240 }
241
242 // Forget about a with when we move to a context for a different function.
243 if (context->previous()->closure() != context->closure()) {
244 contains_with = false;
245 }
246 context = context->previous();
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000247 }
248
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000249 global_scope->AddInnerScope(current_scope);
250 return (innermost_scope == NULL) ? global_scope : innermost_scope;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000251}
252
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000253
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000254bool Scope::Analyze(CompilationInfo* info) {
255 ASSERT(info->function() != NULL);
256 Scope* top = info->function()->scope();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000257
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000258 while (top->outer_scope() != NULL) top = top->outer_scope();
259 top->AllocateVariables(info->calling_context());
260
261#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000262 if (info->isolate()->bootstrapper()->IsActive()
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000263 ? FLAG_print_builtin_scopes
264 : FLAG_print_scopes) {
265 info->function()->scope()->Print();
266 }
267#endif
268
269 info->SetScope(info->function()->scope());
270 return true; // Can not fail.
271}
272
273
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274void Scope::Initialize(bool inside_with) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000275 ASSERT(!already_resolved());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000276
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277 // Add this scope as a new inner scope of the outer scope.
278 if (outer_scope_ != NULL) {
279 outer_scope_->inner_scopes_.Add(this);
280 scope_inside_with_ = outer_scope_->scope_inside_with_ || inside_with;
281 } else {
282 scope_inside_with_ = inside_with;
283 }
284
285 // Declare convenience variables.
286 // Declare and allocate receiver (even for the global scope, and even
287 // if naccesses_ == 0).
288 // NOTE: When loading parameters in the global scope, we must take
289 // care not to access them as properties of the global object, but
290 // instead load them directly from the stack. Currently, the only
291 // such parameter is 'this' which is passed on the stack when
292 // invoking scripts
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000293 if (is_catch_scope()) {
294 ASSERT(outer_scope() != NULL);
295 receiver_ = outer_scope()->receiver();
296 } else {
297 Variable* var =
298 variables_.Declare(this, FACTORY->this_symbol(), Variable::VAR,
299 false, Variable::THIS);
300 var->set_rewrite(new Slot(var, Slot::PARAMETER, -1));
301 receiver_ = var;
302 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303
304 if (is_function_scope()) {
305 // Declare 'arguments' variable which exists in all functions.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000306 // Note that it might never be accessed, in which case it won't be
307 // allocated during variable allocation.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000308 variables_.Declare(this, FACTORY->arguments_symbol(), Variable::VAR,
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000309 true, Variable::ARGUMENTS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000310 }
311}
312
313
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000314Variable* Scope::LocalLookup(Handle<String> name) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000315 Variable* result = variables_.Lookup(name);
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000316 if (result != NULL || scope_info_.is_null()) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000317 return result;
318 }
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000319 // If we have a serialized scope info, we might find the variable there.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000320 //
321 // We should never lookup 'arguments' in this scope as it is implicitly
322 // present in every scope.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000323 ASSERT(*name != *FACTORY->arguments_symbol());
whesse@chromium.org7b260152011-06-20 15:33:18 +0000324 // There should be no local slot with the given name.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000325 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
326
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000327 // Check context slot lookup.
328 Variable::Mode mode;
329 int index = scope_info_->ContextSlotIndex(*name, &mode);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000330 if (index < 0) {
331 // Check parameters.
332 mode = Variable::VAR;
333 index = scope_info_->ParameterIndex(*name);
334 if (index < 0) {
335 // Check the function name.
336 index = scope_info_->FunctionContextSlotIndex(*name);
337 if (index < 0) return NULL;
338 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000339 }
340
whesse@chromium.org7b260152011-06-20 15:33:18 +0000341 Variable* var =
342 variables_.Declare(this, name, mode, true, Variable::NORMAL);
343 var->set_rewrite(new Slot(var, Slot::CONTEXT, index));
344 return var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345}
346
347
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000348Variable* Scope::Lookup(Handle<String> name) {
349 for (Scope* scope = this;
350 scope != NULL;
351 scope = scope->outer_scope()) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000352 Variable* var = scope->LocalLookup(name);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000353 if (var != NULL) return var;
354 }
355 return NULL;
356}
357
358
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000359Variable* Scope::DeclareFunctionVar(Handle<String> name) {
360 ASSERT(is_function_scope() && function_ == NULL);
ager@chromium.org3e875802009-06-29 08:26:34 +0000361 function_ = new Variable(this, name, Variable::CONST, true, Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362 return function_;
363}
364
365
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000366void Scope::DeclareParameter(Handle<String> name) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000367 ASSERT(!already_resolved());
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000368 ASSERT(is_function_scope());
369 Variable* var =
370 variables_.Declare(this, name, Variable::VAR, true, Variable::NORMAL);
371 params_.Add(var);
372}
373
374
375Variable* Scope::DeclareLocal(Handle<String> name, Variable::Mode mode) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000376 ASSERT(!already_resolved());
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000377 // This function handles VAR and CONST modes. DYNAMIC variables are
378 // introduces during variable allocation, INTERNAL variables are allocated
379 // explicitly, and TEMPORARY variables are allocated via NewTemporary().
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380 ASSERT(mode == Variable::VAR || mode == Variable::CONST);
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000381 ++num_var_or_const_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000382 return variables_.Declare(this, name, mode, true, Variable::NORMAL);
383}
384
385
386Variable* Scope::DeclareGlobal(Handle<String> name) {
387 ASSERT(is_global_scope());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000388 return variables_.Declare(this, name, Variable::DYNAMIC_GLOBAL, true,
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000389 Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390}
391
392
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000393VariableProxy* Scope::NewUnresolved(Handle<String> name,
394 bool inside_with,
395 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396 // Note that we must not share the unresolved variables with
397 // the same name because they may be removed selectively via
398 // RemoveUnresolved().
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000399 ASSERT(!already_resolved());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000400 VariableProxy* proxy = new VariableProxy(name, false, inside_with, position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000401 unresolved_.Add(proxy);
402 return proxy;
403}
404
405
406void Scope::RemoveUnresolved(VariableProxy* var) {
407 // Most likely (always?) any variable we want to remove
408 // was just added before, so we search backwards.
409 for (int i = unresolved_.length(); i-- > 0;) {
410 if (unresolved_[i] == var) {
411 unresolved_.Remove(i);
412 return;
413 }
414 }
415}
416
417
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000418Variable* Scope::NewTemporary(Handle<String> name) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000419 ASSERT(!already_resolved());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000420 Variable* var =
421 new Variable(this, name, Variable::TEMPORARY, true, Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000422 temps_.Add(var);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000423 return var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000424}
425
426
427void Scope::AddDeclaration(Declaration* declaration) {
428 decls_.Add(declaration);
429}
430
431
432void Scope::SetIllegalRedeclaration(Expression* expression) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000433 // Record only the first illegal redeclaration.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000434 if (!HasIllegalRedeclaration()) {
435 illegal_redecl_ = expression;
436 }
437 ASSERT(HasIllegalRedeclaration());
438}
439
440
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000441void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442 ASSERT(HasIllegalRedeclaration());
443 illegal_redecl_->Accept(visitor);
444}
445
446
447template<class Allocator>
448void Scope::CollectUsedVariables(List<Variable*, Allocator>* locals) {
449 // Collect variables in this scope.
450 // Note that the function_ variable - if present - is not
451 // collected here but handled separately in ScopeInfo
452 // which is the current user of this function).
453 for (int i = 0; i < temps_.length(); i++) {
454 Variable* var = temps_[i];
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000455 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000456 locals->Add(var);
457 }
458 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000459 for (VariableMap::Entry* p = variables_.Start();
460 p != NULL;
461 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000462 Variable* var = reinterpret_cast<Variable*>(p->value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000463 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464 locals->Add(var);
465 }
466 }
467}
468
469
470// Make sure the method gets instantiated by the template system.
471template void Scope::CollectUsedVariables(
472 List<Variable*, FreeStoreAllocationPolicy>* locals);
473template void Scope::CollectUsedVariables(
474 List<Variable*, PreallocatedStorage>* locals);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000475template void Scope::CollectUsedVariables(
476 List<Variable*, ZoneListAllocationPolicy>* locals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000477
478
ager@chromium.org381abbb2009-02-25 13:23:22 +0000479void Scope::AllocateVariables(Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000480 ASSERT(outer_scope_ == NULL); // eval or global scopes only
481
482 // 1) Propagate scope information.
483 // If we are in an eval scope, we may have other outer scopes about
484 // which we don't know anything at this point. Thus we must be conservative
485 // and assume they may invoke eval themselves. Eventually we could capture
486 // this information in the ScopeInfo and then use it here (by traversing
487 // the call chain stack, at compile time).
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000488
ager@chromium.org381abbb2009-02-25 13:23:22 +0000489 bool eval_scope = is_eval_scope();
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000490 bool outer_scope_calls_eval = false;
491 bool outer_scope_calls_non_strict_eval = false;
492 if (!is_global_scope()) {
493 context->ComputeEvalScopeInfo(&outer_scope_calls_eval,
494 &outer_scope_calls_non_strict_eval);
495 }
496 PropagateScopeInfo(outer_scope_calls_eval,
497 outer_scope_calls_non_strict_eval,
498 eval_scope);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000499
500 // 2) Resolve variables.
501 Scope* global_scope = NULL;
502 if (is_global_scope()) global_scope = this;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000503 ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000504
505 // 3) Allocate variables.
506 AllocateVariablesRecursively();
507}
508
509
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510bool Scope::AllowsLazyCompilation() const {
511 return !force_eager_compilation_ && HasTrivialOuterContext();
512}
513
514
515bool Scope::HasTrivialContext() const {
516 // A function scope has a trivial context if it always is the global
517 // context. We iteratively scan out the context chain to see if
518 // there is anything that makes this scope non-trivial; otherwise we
519 // return true.
520 for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) {
521 if (scope->is_eval_scope()) return false;
522 if (scope->scope_inside_with_) return false;
523 if (scope->num_heap_slots_ > 0) return false;
524 }
525 return true;
526}
527
528
529bool Scope::HasTrivialOuterContext() const {
530 Scope* outer = outer_scope_;
531 if (outer == NULL) return true;
532 // Note that the outer context may be trivial in general, but the current
533 // scope may be inside a 'with' statement in which case the outer context
534 // for this scope is not trivial.
535 return !scope_inside_with_ && outer->HasTrivialContext();
536}
537
538
539int Scope::ContextChainLength(Scope* scope) {
540 int n = 0;
541 for (Scope* s = this; s != scope; s = s->outer_scope_) {
542 ASSERT(s != NULL); // scope must be in the scope chain
543 if (s->num_heap_slots() > 0) n++;
544 }
545 return n;
546}
547
548
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000549Scope* Scope::DeclarationScope() {
550 Scope* scope = this;
551 while (scope->is_catch_scope()) {
552 scope = scope->outer_scope();
553 }
554 return scope;
555}
556
557
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558#ifdef DEBUG
559static const char* Header(Scope::Type type) {
560 switch (type) {
561 case Scope::EVAL_SCOPE: return "eval";
562 case Scope::FUNCTION_SCOPE: return "function";
563 case Scope::GLOBAL_SCOPE: return "global";
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000564 case Scope::CATCH_SCOPE: return "catch";
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565 }
566 UNREACHABLE();
567 return NULL;
568}
569
570
571static void Indent(int n, const char* str) {
572 PrintF("%*s%s", n, "", str);
573}
574
575
576static void PrintName(Handle<String> name) {
577 SmartPointer<char> s = name->ToCString(DISALLOW_NULLS);
578 PrintF("%s", *s);
579}
580
581
582static void PrintVar(PrettyPrinter* printer, int indent, Variable* var) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000583 if (var->is_used() || var->rewrite() != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584 Indent(indent, Variable::Mode2String(var->mode()));
585 PrintF(" ");
586 PrintName(var->name());
587 PrintF("; // ");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000588 if (var->rewrite() != NULL) {
589 PrintF("%s, ", printer->Print(var->rewrite()));
590 if (var->is_accessed_from_inner_scope()) PrintF(", ");
591 }
592 if (var->is_accessed_from_inner_scope()) PrintF("inner scope access");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000593 PrintF("\n");
594 }
595}
596
597
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000598static void PrintMap(PrettyPrinter* printer, int indent, VariableMap* map) {
599 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000600 Variable* var = reinterpret_cast<Variable*>(p->value);
601 PrintVar(printer, indent, var);
602 }
603}
604
605
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000606void Scope::Print(int n) {
607 int n0 = (n > 0 ? n : 0);
608 int n1 = n0 + 2; // indentation
609
610 // Print header.
611 Indent(n0, Header(type_));
612 if (scope_name_->length() > 0) {
613 PrintF(" ");
614 PrintName(scope_name_);
615 }
616
617 // Print parameters, if any.
618 if (is_function_scope()) {
619 PrintF(" (");
620 for (int i = 0; i < params_.length(); i++) {
621 if (i > 0) PrintF(", ");
622 PrintName(params_[i]->name());
623 }
624 PrintF(")");
625 }
626
627 PrintF(" {\n");
628
629 // Function name, if any (named function literals, only).
630 if (function_ != NULL) {
631 Indent(n1, "// (local) function name: ");
632 PrintName(function_->name());
633 PrintF("\n");
634 }
635
636 // Scope info.
637 if (HasTrivialOuterContext()) {
638 Indent(n1, "// scope has trivial outer context\n");
639 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000640 if (is_strict_mode()) Indent(n1, "// strict mode scope\n");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
642 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n");
643 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n");
644 if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n");
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000645 if (outer_scope_calls_non_strict_eval_) {
646 Indent(n1, "// outer scope calls 'eval' in non-strict context\n");
647 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000648 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
ager@chromium.org381abbb2009-02-25 13:23:22 +0000649 if (outer_scope_is_eval_scope_) {
650 Indent(n1, "// outer scope is 'eval' scope\n");
651 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 if (num_stack_slots_ > 0) { Indent(n1, "// ");
653 PrintF("%d stack slots\n", num_stack_slots_); }
654 if (num_heap_slots_ > 0) { Indent(n1, "// ");
655 PrintF("%d heap slots\n", num_heap_slots_); }
656
657 // Print locals.
658 PrettyPrinter printer;
659 Indent(n1, "// function var\n");
660 if (function_ != NULL) {
661 PrintVar(&printer, n1, function_);
662 }
663
664 Indent(n1, "// temporary vars\n");
665 for (int i = 0; i < temps_.length(); i++) {
666 PrintVar(&printer, n1, temps_[i]);
667 }
668
669 Indent(n1, "// local vars\n");
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000670 PrintMap(&printer, n1, &variables_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000672 Indent(n1, "// dynamic vars\n");
673 if (dynamics_ != NULL) {
674 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC));
675 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_LOCAL));
676 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_GLOBAL));
677 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678
679 // Print inner scopes (disable by providing negative n).
680 if (n >= 0) {
681 for (int i = 0; i < inner_scopes_.length(); i++) {
682 PrintF("\n");
683 inner_scopes_[i]->Print(n1);
684 }
685 }
686
687 Indent(n0, "}\n");
688}
689#endif // DEBUG
690
691
ager@chromium.org381abbb2009-02-25 13:23:22 +0000692Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000693 if (dynamics_ == NULL) dynamics_ = new DynamicScopePart();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000694 VariableMap* map = dynamics_->GetMap(mode);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000695 Variable* var = map->Lookup(name);
696 if (var == NULL) {
697 // Declare a new non-local.
ager@chromium.org3e875802009-06-29 08:26:34 +0000698 var = map->Declare(NULL, name, mode, true, Variable::NORMAL);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000699 // Allocate it by giving it a dynamic lookup.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000700 var->set_rewrite(new Slot(var, Slot::LOOKUP, -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000701 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702 return var;
703}
704
705
706// Lookup a variable starting with this scope. The result is either
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000707// the statically resolved variable belonging to an outer scope, or
708// NULL. It may be NULL because a) we couldn't find a variable, or b)
709// because the variable is just a guess (and may be shadowed by
710// another variable that is introduced dynamically via an 'eval' call
711// or a 'with' statement).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000712Variable* Scope::LookupRecursive(Handle<String> name,
713 bool inner_lookup,
714 Variable** invalidated_local) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000715 // If we find a variable, but the current scope calls 'eval', the found
716 // variable may not be the correct one (the 'eval' may introduce a
717 // property with the same name). In that case, remember that the variable
718 // found is just a guess.
719 bool guess = scope_calls_eval_;
720
721 // Try to find the variable in this scope.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000722 Variable* var = LocalLookup(name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723
724 if (var != NULL) {
725 // We found a variable. If this is not an inner lookup, we are done.
726 // (Even if there is an 'eval' in this scope which introduces the
727 // same variable again, the resulting variable remains the same.
728 // Note that enclosing 'with' statements are handled at the call site.)
729 if (!inner_lookup)
730 return var;
731
732 } else {
733 // We did not find a variable locally. Check against the function variable,
734 // if any. We can do this for all scopes, since the function variable is
735 // only present - if at all - for function scopes.
736 //
737 // This lookup corresponds to a lookup in the "intermediate" scope sitting
738 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
739 // the name of named function literal is kept in an intermediate scope
ager@chromium.org32912102009-01-16 10:38:43 +0000740 // in between this scope and the next outer scope.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000741 if (function_ != NULL && function_->name().is_identical_to(name)) {
742 var = function_;
743
744 } else if (outer_scope_ != NULL) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000745 var = outer_scope_->LookupRecursive(name, true, invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000746 // We may have found a variable in an outer scope. However, if
747 // the current scope is inside a 'with', the actual variable may
748 // be a property introduced via the 'with' statement. Then, the
749 // variable we may have found is just a guess.
750 if (scope_inside_with_)
751 guess = true;
752 }
753
754 // If we did not find a variable, we are done.
755 if (var == NULL)
756 return NULL;
757 }
758
759 ASSERT(var != NULL);
760
761 // If this is a lookup from an inner scope, mark the variable.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000762 if (inner_lookup) {
763 var->MarkAsAccessedFromInnerScope();
764 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000765
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000766 // If the variable we have found is just a guess, invalidate the
767 // result. If the found variable is local, record that fact so we
768 // can generate fast code to get it if it is not shadowed by eval.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000769 if (guess) {
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000770 if (!var->is_global()) *invalidated_local = var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000771 var = NULL;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000772 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773
774 return var;
775}
776
777
ager@chromium.org381abbb2009-02-25 13:23:22 +0000778void Scope::ResolveVariable(Scope* global_scope,
779 Handle<Context> context,
780 VariableProxy* proxy) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000781 ASSERT(global_scope == NULL || global_scope->is_global_scope());
782
783 // If the proxy is already resolved there's nothing to do
784 // (functions and consts may be resolved by the parser).
785 if (proxy->var() != NULL) return;
786
787 // Otherwise, try to resolve the variable.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000788 Variable* invalidated_local = NULL;
789 Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000790
791 if (proxy->inside_with()) {
792 // If we are inside a local 'with' statement, all bets are off
793 // and we cannot resolve the proxy to a local variable even if
794 // we found an outer matching variable.
795 // Note that we must do a lookup anyway, because if we find one,
796 // we must mark that variable as potentially accessed from this
797 // inner scope (the property may not be in the 'with' object).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000798 var = NonLocal(proxy->name(), Variable::DYNAMIC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000799
800 } else {
801 // We are not inside a local 'with' statement.
802
803 if (var == NULL) {
804 // We did not find the variable. We have a global variable
805 // if we are in the global scope (we know already that we
806 // are outside a 'with' statement) or if there is no way
807 // that the variable might be introduced dynamically (through
808 // a local or outer eval() call, or an outer 'with' statement),
809 // or we don't know about the outer scope (because we are
810 // in an eval scope).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000811 if (is_global_scope() ||
812 !(scope_inside_with_ || outer_scope_is_eval_scope_ ||
813 scope_calls_eval_ || outer_scope_calls_eval_)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000814 // We must have a global variable.
815 ASSERT(global_scope != NULL);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000816 var = global_scope->DeclareGlobal(proxy->name());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000817
818 } else if (scope_inside_with_) {
819 // If we are inside a with statement we give up and look up
820 // the variable at runtime.
821 var = NonLocal(proxy->name(), Variable::DYNAMIC);
822
823 } else if (invalidated_local != NULL) {
824 // No with statements are involved and we found a local
825 // variable that might be shadowed by eval introduced
826 // variables.
827 var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL);
828 var->set_local_if_not_shadowed(invalidated_local);
829
830 } else if (outer_scope_is_eval_scope_) {
831 // No with statements and we did not find a local and the code
832 // is executed with a call to eval. The context contains
833 // scope information that we can use to determine if the
834 // variable is global if it is not shadowed by eval-introduced
835 // variables.
836 if (context->GlobalIfNotShadowedByEval(proxy->name())) {
837 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
838
839 } else {
840 var = NonLocal(proxy->name(), Variable::DYNAMIC);
841 }
842
843 } else {
844 // No with statements and we did not find a local and the code
845 // is not executed with a call to eval. We know that this
846 // variable is global unless it is shadowed by eval-introduced
847 // variables.
848 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000849 }
850 }
851 }
852
853 proxy->BindTo(var);
854}
855
856
ager@chromium.org381abbb2009-02-25 13:23:22 +0000857void Scope::ResolveVariablesRecursively(Scope* global_scope,
858 Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000859 ASSERT(global_scope == NULL || global_scope->is_global_scope());
860
861 // Resolve unresolved variables for this scope.
862 for (int i = 0; i < unresolved_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000863 ResolveVariable(global_scope, context, unresolved_[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000864 }
865
866 // Resolve unresolved variables for inner scopes.
867 for (int i = 0; i < inner_scopes_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000868 inner_scopes_[i]->ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869 }
870}
871
872
ager@chromium.org381abbb2009-02-25 13:23:22 +0000873bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval,
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000874 bool outer_scope_calls_non_strict_eval,
ager@chromium.org381abbb2009-02-25 13:23:22 +0000875 bool outer_scope_is_eval_scope) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876 if (outer_scope_calls_eval) {
877 outer_scope_calls_eval_ = true;
878 }
879
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000880 if (outer_scope_calls_non_strict_eval) {
881 outer_scope_calls_non_strict_eval_ = true;
882 }
883
ager@chromium.org381abbb2009-02-25 13:23:22 +0000884 if (outer_scope_is_eval_scope) {
885 outer_scope_is_eval_scope_ = true;
886 }
887
888 bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_;
889 bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_;
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000890 bool calls_non_strict_eval =
891 (scope_calls_eval_ && !is_strict_mode()) ||
892 outer_scope_calls_non_strict_eval_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893 for (int i = 0; i < inner_scopes_.length(); i++) {
894 Scope* inner_scope = inner_scopes_[i];
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000895 if (inner_scope->PropagateScopeInfo(calls_eval,
896 calls_non_strict_eval,
897 is_eval)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898 inner_scope_calls_eval_ = true;
899 }
900 if (inner_scope->force_eager_compilation_) {
901 force_eager_compilation_ = true;
902 }
903 }
904
905 return scope_calls_eval_ || inner_scope_calls_eval_;
906}
907
908
909bool Scope::MustAllocate(Variable* var) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000910 // Give var a read/write use if there is a chance it might be accessed
911 // via an eval() call. This is only possible if the variable has a
912 // visible name.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913 if ((var->is_this() || var->name()->length() > 0) &&
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000914 (var->is_accessed_from_inner_scope() ||
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000915 scope_calls_eval_ ||
916 inner_scope_calls_eval_ ||
917 scope_contains_with_ ||
918 is_catch_scope())) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000919 var->set_is_used(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000920 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000921 // Global variables do not need to be allocated.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000922 return !var->is_global() && var->is_used();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000923}
924
925
926bool Scope::MustAllocateInContext(Variable* var) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000927 // If var is accessed from an inner scope, or if there is a possibility
928 // that it might be accessed from the current or an inner scope (through
929 // an eval() call or a runtime with lookup), it must be allocated in the
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000930 // context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000931 //
932 // Exceptions: temporary variables are never allocated in a context;
933 // catch-bound variables are always allocated in a context.
934 if (var->mode() == Variable::TEMPORARY) return false;
935 if (is_catch_scope()) return true;
936 return var->is_accessed_from_inner_scope() ||
937 scope_calls_eval_ ||
938 inner_scope_calls_eval_ ||
939 scope_contains_with_ ||
940 var->is_global();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000941}
942
943
944bool Scope::HasArgumentsParameter() {
945 for (int i = 0; i < params_.length(); i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000946 if (params_[i]->name().is_identical_to(FACTORY->arguments_symbol()))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947 return true;
948 }
949 return false;
950}
951
952
953void Scope::AllocateStackSlot(Variable* var) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000954 var->set_rewrite(new Slot(var, Slot::LOCAL, num_stack_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955}
956
957
958void Scope::AllocateHeapSlot(Variable* var) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000959 var->set_rewrite(new Slot(var, Slot::CONTEXT, num_heap_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000960}
961
962
963void Scope::AllocateParameterLocals() {
964 ASSERT(is_function_scope());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000965 Variable* arguments = LocalLookup(FACTORY->arguments_symbol());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966 ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000967
whesse@chromium.org7b260152011-06-20 15:33:18 +0000968 bool uses_nonstrict_arguments = false;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000969
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000970 if (MustAllocate(arguments) && !HasArgumentsParameter()) {
971 // 'arguments' is used. Unless there is also a parameter called
whesse@chromium.org7b260152011-06-20 15:33:18 +0000972 // 'arguments', we must be conservative and allocate all parameters to
973 // the context assuming they will be captured by the arguments object.
974 // If we have a parameter named 'arguments', a (new) value is always
975 // assigned to it via the function invocation. Then 'arguments' denotes
976 // that specific parameter value and cannot be used to access the
977 // parameters, which is why we don't need to allocate an arguments
978 // object in that case.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000979
980 // We are using 'arguments'. Tell the code generator that is needs to
981 // allocate the arguments object by setting 'arguments_'.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000982 arguments_ = arguments;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000983
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000984 // In strict mode 'arguments' does not alias formal parameters.
985 // Therefore in strict mode we allocate parameters as if 'arguments'
986 // were not used.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000987 uses_nonstrict_arguments = !is_strict_mode();
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000988 }
989
whesse@chromium.org7b260152011-06-20 15:33:18 +0000990 // The same parameter may occur multiple times in the parameters_ list.
991 // If it does, and if it is not copied into the context object, it must
992 // receive the highest parameter index for that parameter; thus iteration
993 // order is relevant!
994 for (int i = params_.length() - 1; i >= 0; --i) {
995 Variable* var = params_[i];
996 ASSERT(var->scope() == this);
997 if (uses_nonstrict_arguments) {
998 // Give the parameter a use from an inner scope, to force allocation
999 // to the context.
1000 var->MarkAsAccessedFromInnerScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001 }
1002
whesse@chromium.org7b260152011-06-20 15:33:18 +00001003 if (MustAllocate(var)) {
1004 if (MustAllocateInContext(var)) {
1005 ASSERT(var->rewrite() == NULL || var->IsContextSlot());
1006 if (var->rewrite() == NULL) {
1007 AllocateHeapSlot(var);
1008 }
1009 } else {
1010 ASSERT(var->rewrite() == NULL || var->IsParameter());
1011 if (var->rewrite() == NULL) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001012 var->set_rewrite(new Slot(var, Slot::PARAMETER, i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001013 }
1014 }
1015 }
1016 }
1017}
1018
1019
1020void Scope::AllocateNonParameterLocal(Variable* var) {
1021 ASSERT(var->scope() == this);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001022 ASSERT(var->rewrite() == NULL ||
whesse@chromium.org7b260152011-06-20 15:33:18 +00001023 !var->IsVariable(FACTORY->result_symbol()) ||
1024 var->AsSlot() == NULL ||
1025 var->AsSlot()->type() != Slot::LOCAL);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001026 if (var->rewrite() == NULL && MustAllocate(var)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001027 if (MustAllocateInContext(var)) {
1028 AllocateHeapSlot(var);
1029 } else {
1030 AllocateStackSlot(var);
1031 }
1032 }
1033}
1034
1035
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036void Scope::AllocateNonParameterLocals() {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001037 // All variables that have no rewrite yet are non-parameter locals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001038 for (int i = 0; i < temps_.length(); i++) {
1039 AllocateNonParameterLocal(temps_[i]);
1040 }
1041
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001042 for (VariableMap::Entry* p = variables_.Start();
1043 p != NULL;
1044 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001045 Variable* var = reinterpret_cast<Variable*>(p->value);
1046 AllocateNonParameterLocal(var);
1047 }
1048
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001049 // For now, function_ must be allocated at the very end. If it gets
1050 // allocated in the context, it must be the last slot in the context,
1051 // because of the current ScopeInfo implementation (see
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001052 // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor).
1053 if (function_ != NULL) {
1054 AllocateNonParameterLocal(function_);
1055 }
1056}
1057
1058
1059void Scope::AllocateVariablesRecursively() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001060 // Allocate variables for inner scopes.
1061 for (int i = 0; i < inner_scopes_.length(); i++) {
1062 inner_scopes_[i]->AllocateVariablesRecursively();
1063 }
1064
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001065 // If scope is already resolved, we still need to allocate
1066 // variables in inner scopes which might not had been resolved yet.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001067 if (already_resolved()) return;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001068 // The number of slots required for variables.
1069 num_stack_slots_ = 0;
1070 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
1071
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001072 // Allocate variables for this scope.
1073 // Parameters must be allocated first, if any.
1074 if (is_function_scope()) AllocateParameterLocals();
1075 AllocateNonParameterLocals();
1076
1077 // Allocate context if necessary.
1078 bool must_have_local_context = false;
1079 if (scope_calls_eval_ || scope_contains_with_) {
1080 // The context for the eval() call or 'with' statement in this scope.
1081 // Unless we are in the global or an eval scope, we need a local
1082 // context even if we didn't statically allocate any locals in it,
1083 // and the compiler will access the context variable. If we are
1084 // not in an inner scope, the scope is provided from the outside.
1085 must_have_local_context = is_function_scope();
1086 }
1087
1088 // If we didn't allocate any locals in the local context, then we only
1089 // need the minimal number of slots if we must have a local context.
1090 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS &&
1091 !must_have_local_context) {
1092 num_heap_slots_ = 0;
1093 }
1094
1095 // Allocation done.
1096 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
1097}
1098
1099} } // namespace v8::internal