blob: 390a0b6e11ba8f1ccd4081fda789f582c8bc1554 [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +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
Ben Murdochf87a2032010-10-22 12:50:53 +010030#include "scopes.h"
31
32#include "bootstrapper.h"
33#include "compiler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "prettyprinter.h"
35#include "scopeinfo.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000036
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000037#include "allocation-inl.h"
38
Steve Blocka7e24c12009-10-30 11:49:00 +000039namespace v8 {
40namespace internal {
41
42// ----------------------------------------------------------------------------
43// A Zone allocator for use with LocalsMap.
44
Steve Block44f0eee2011-05-26 01:26:41 +010045// TODO(isolates): It is probably worth it to change the Allocator class to
46// take a pointer to an isolate.
Steve Blocka7e24c12009-10-30 11:49:00 +000047class ZoneAllocator: public Allocator {
48 public:
49 /* nothing to do */
50 virtual ~ZoneAllocator() {}
51
Steve Block44f0eee2011-05-26 01:26:41 +010052 virtual void* New(size_t size) { return ZONE->New(static_cast<int>(size)); }
Steve Blocka7e24c12009-10-30 11:49:00 +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
81VariableMap::VariableMap(bool gotta_love_static_overloading) : HashMap() {}
82
83VariableMap::VariableMap() : HashMap(Match, &LocalsMapAllocator, 8) {}
84VariableMap::~VariableMap() {}
85
86
87Variable* VariableMap::Declare(Scope* scope,
88 Handle<String> name,
89 Variable::Mode mode,
90 bool is_valid_lhs,
91 Variable::Kind kind) {
92 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());
96 p->value = new Variable(scope, name, mode, is_valid_lhs, kind);
97 }
98 return reinterpret_cast<Variable*>(p->value);
99}
100
101
102Variable* VariableMap::Lookup(Handle<String> name) {
103 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
118Scope::Scope(Type type)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000119 : isolate_(Isolate::Current()),
120 inner_scopes_(0),
121 variables_(false),
122 temps_(0),
123 params_(0),
124 unresolved_(0),
125 decls_(0),
126 already_resolved_(false) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100127 SetDefaults(type, NULL, Handle<SerializedScopeInfo>::null());
Steve Blocka7e24c12009-10-30 11:49:00 +0000128}
129
130
131Scope::Scope(Scope* outer_scope, Type type)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000132 : isolate_(Isolate::Current()),
133 inner_scopes_(4),
134 variables_(),
135 temps_(4),
136 params_(4),
137 unresolved_(16),
138 decls_(4),
139 already_resolved_(false) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100140 SetDefaults(type, outer_scope, Handle<SerializedScopeInfo>::null());
Steve Blocka7e24c12009-10-30 11:49:00 +0000141 // At some point we might want to provide outer scopes to
142 // eval scopes (by walking the stack and reading the scope info).
143 // In that case, the ASSERT below needs to be adjusted.
144 ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL));
145 ASSERT(!HasIllegalRedeclaration());
146}
147
148
Ben Murdoch8b112d22011-06-08 16:22:53 +0100149Scope::Scope(Scope* inner_scope, Handle<SerializedScopeInfo> scope_info)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000150 : isolate_(Isolate::Current()),
151 inner_scopes_(4),
152 variables_(),
153 temps_(4),
154 params_(4),
155 unresolved_(16),
156 decls_(4),
157 already_resolved_(true) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100158 ASSERT(!scope_info.is_null());
Steve Block44f0eee2011-05-26 01:26:41 +0100159 SetDefaults(FUNCTION_SCOPE, NULL, scope_info);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100160 if (scope_info->HasHeapAllocatedLocals()) {
161 num_heap_slots_ = scope_info_->NumberOfContextSlots();
162 }
Steve Block44f0eee2011-05-26 01:26:41 +0100163 AddInnerScope(inner_scope);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000164}
Steve Block44f0eee2011-05-26 01:26:41 +0100165
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000166
167Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name)
168 : isolate_(Isolate::Current()),
169 inner_scopes_(1),
170 variables_(),
171 temps_(0),
172 params_(0),
173 unresolved_(0),
174 decls_(0),
175 already_resolved_(true) {
176 SetDefaults(CATCH_SCOPE, NULL, Handle<SerializedScopeInfo>::null());
177 AddInnerScope(inner_scope);
178 ++num_var_or_const_;
179 Variable* variable = variables_.Declare(this,
180 catch_variable_name,
181 Variable::VAR,
182 true, // Valid left-hand side.
183 Variable::NORMAL);
184 AllocateHeapSlot(variable);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100185}
186
187
Ben Murdoch8b112d22011-06-08 16:22:53 +0100188void Scope::SetDefaults(Type type,
189 Scope* outer_scope,
190 Handle<SerializedScopeInfo> scope_info) {
191 outer_scope_ = outer_scope;
192 type_ = type;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000193 scope_name_ = isolate_->factory()->empty_symbol();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100194 dynamics_ = NULL;
195 receiver_ = NULL;
196 function_ = NULL;
197 arguments_ = NULL;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100198 illegal_redecl_ = NULL;
199 scope_inside_with_ = false;
200 scope_contains_with_ = false;
201 scope_calls_eval_ = false;
202 // Inherit the strict mode from the parent scope.
203 strict_mode_ = (outer_scope != NULL) && outer_scope->strict_mode_;
204 outer_scope_calls_eval_ = false;
Ben Murdoch257744e2011-11-30 15:57:28 +0000205 outer_scope_calls_non_strict_eval_ = false;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100206 inner_scope_calls_eval_ = false;
207 outer_scope_is_eval_scope_ = false;
208 force_eager_compilation_ = false;
Steve Block053d10c2011-06-13 19:13:29 +0100209 num_var_or_const_ = 0;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100210 num_stack_slots_ = 0;
211 num_heap_slots_ = 0;
212 scope_info_ = scope_info;
213}
214
215
Steve Block44f0eee2011-05-26 01:26:41 +0100216Scope* Scope::DeserializeScopeChain(CompilationInfo* info,
217 Scope* global_scope) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000218 // Reconstruct the outer scope chain from a closure's context chain.
Steve Block44f0eee2011-05-26 01:26:41 +0100219 ASSERT(!info->closure().is_null());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000220 Context* context = info->closure()->context();
221 Scope* current_scope = NULL;
Steve Block44f0eee2011-05-26 01:26:41 +0100222 Scope* innermost_scope = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000223 bool contains_with = false;
224 while (!context->IsGlobalContext()) {
225 if (context->IsWithContext()) {
226 // All the inner scopes are inside a with.
227 contains_with = true;
228 for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) {
229 s->scope_inside_with_ = true;
Steve Block44f0eee2011-05-26 01:26:41 +0100230 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000231 } else {
232 if (context->IsFunctionContext()) {
233 SerializedScopeInfo* scope_info =
234 context->closure()->shared()->scope_info();
235 current_scope =
236 new Scope(current_scope, Handle<SerializedScopeInfo>(scope_info));
237 } else {
238 ASSERT(context->IsCatchContext());
239 String* name = String::cast(context->extension());
240 current_scope = new Scope(current_scope, Handle<String>(name));
241 }
242 if (contains_with) current_scope->RecordWithStatement();
243 if (innermost_scope == NULL) innermost_scope = current_scope;
244 }
245
246 // Forget about a with when we move to a context for a different function.
247 if (context->previous()->closure() != context->closure()) {
248 contains_with = false;
249 }
250 context = context->previous();
Steve Block44f0eee2011-05-26 01:26:41 +0100251 }
252
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000253 global_scope->AddInnerScope(current_scope);
254 return (innermost_scope == NULL) ? global_scope : innermost_scope;
Steve Block44f0eee2011-05-26 01:26:41 +0100255}
256
Ben Murdochb8e0da22011-05-16 14:20:40 +0100257
Ben Murdochf87a2032010-10-22 12:50:53 +0100258bool Scope::Analyze(CompilationInfo* info) {
259 ASSERT(info->function() != NULL);
260 Scope* top = info->function()->scope();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100261
Ben Murdochf87a2032010-10-22 12:50:53 +0100262 while (top->outer_scope() != NULL) top = top->outer_scope();
263 top->AllocateVariables(info->calling_context());
264
265#ifdef DEBUG
Steve Block44f0eee2011-05-26 01:26:41 +0100266 if (info->isolate()->bootstrapper()->IsActive()
Ben Murdochf87a2032010-10-22 12:50:53 +0100267 ? FLAG_print_builtin_scopes
268 : FLAG_print_scopes) {
269 info->function()->scope()->Print();
270 }
271#endif
272
273 info->SetScope(info->function()->scope());
274 return true; // Can not fail.
275}
276
277
Steve Blocka7e24c12009-10-30 11:49:00 +0000278void Scope::Initialize(bool inside_with) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000279 ASSERT(!already_resolved());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100280
Steve Blocka7e24c12009-10-30 11:49:00 +0000281 // Add this scope as a new inner scope of the outer scope.
282 if (outer_scope_ != NULL) {
283 outer_scope_->inner_scopes_.Add(this);
284 scope_inside_with_ = outer_scope_->scope_inside_with_ || inside_with;
285 } else {
286 scope_inside_with_ = inside_with;
287 }
288
289 // Declare convenience variables.
290 // Declare and allocate receiver (even for the global scope, and even
291 // if naccesses_ == 0).
292 // NOTE: When loading parameters in the global scope, we must take
293 // care not to access them as properties of the global object, but
294 // instead load them directly from the stack. Currently, the only
295 // such parameter is 'this' which is passed on the stack when
296 // invoking scripts
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000297 if (is_catch_scope()) {
298 ASSERT(outer_scope() != NULL);
299 receiver_ = outer_scope()->receiver();
300 } else {
301 Variable* var =
302 variables_.Declare(this,
303 isolate_->factory()->this_symbol(),
304 Variable::VAR,
305 false,
306 Variable::THIS);
307 var->set_rewrite(NewSlot(var, Slot::PARAMETER, -1));
308 receiver_ = var;
309 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000310
311 if (is_function_scope()) {
312 // Declare 'arguments' variable which exists in all functions.
313 // Note that it might never be accessed, in which case it won't be
314 // allocated during variable allocation.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000315 variables_.Declare(this,
316 isolate_->factory()->arguments_symbol(),
317 Variable::VAR,
318 true,
319 Variable::ARGUMENTS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000320 }
321}
322
323
Steve Blocka7e24c12009-10-30 11:49:00 +0000324Variable* Scope::LocalLookup(Handle<String> name) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100325 Variable* result = variables_.Lookup(name);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000326 if (result != NULL || scope_info_.is_null()) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100327 return result;
328 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000329 // If we have a serialized scope info, we might find the variable there.
330 //
331 // We should never lookup 'arguments' in this scope as it is implicitly
332 // present in every scope.
333 ASSERT(*name != *isolate_->factory()->arguments_symbol());
334 // There should be no local slot with the given name.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100335 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
336
337 // Check context slot lookup.
338 Variable::Mode mode;
339 int index = scope_info_->ContextSlotIndex(*name, &mode);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000340 if (index < 0) {
341 // Check parameters.
342 mode = Variable::VAR;
343 index = scope_info_->ParameterIndex(*name);
344 if (index < 0) {
345 // Check the function name.
346 index = scope_info_->FunctionContextSlotIndex(*name);
347 if (index < 0) return NULL;
348 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100349 }
350
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000351 Variable* var =
352 variables_.Declare(this, name, mode, true, Variable::NORMAL);
353 var->set_rewrite(NewSlot(var, Slot::CONTEXT, index));
354 return var;
Steve Blocka7e24c12009-10-30 11:49:00 +0000355}
356
357
358Variable* Scope::Lookup(Handle<String> name) {
359 for (Scope* scope = this;
360 scope != NULL;
361 scope = scope->outer_scope()) {
362 Variable* var = scope->LocalLookup(name);
363 if (var != NULL) return var;
364 }
365 return NULL;
366}
367
368
369Variable* Scope::DeclareFunctionVar(Handle<String> name) {
370 ASSERT(is_function_scope() && function_ == NULL);
371 function_ = new Variable(this, name, Variable::CONST, true, Variable::NORMAL);
372 return function_;
373}
374
375
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000376void Scope::DeclareParameter(Handle<String> name) {
377 ASSERT(!already_resolved());
378 ASSERT(is_function_scope());
379 Variable* var =
380 variables_.Declare(this, name, Variable::VAR, true, Variable::NORMAL);
381 params_.Add(var);
382}
383
384
385Variable* Scope::DeclareLocal(Handle<String> name, Variable::Mode mode) {
386 ASSERT(!already_resolved());
387 // This function handles VAR and CONST modes. DYNAMIC variables are
388 // introduces during variable allocation, INTERNAL variables are allocated
389 // explicitly, and TEMPORARY variables are allocated via NewTemporary().
Steve Blocka7e24c12009-10-30 11:49:00 +0000390 ASSERT(mode == Variable::VAR || mode == Variable::CONST);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000391 ++num_var_or_const_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000392 return variables_.Declare(this, name, mode, true, Variable::NORMAL);
393}
394
395
396Variable* Scope::DeclareGlobal(Handle<String> name) {
397 ASSERT(is_global_scope());
Leon Clarkee46be812010-01-19 14:06:41 +0000398 return variables_.Declare(this, name, Variable::DYNAMIC_GLOBAL, true,
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 Variable::NORMAL);
400}
401
402
Ben Murdoch8b112d22011-06-08 16:22:53 +0100403VariableProxy* Scope::NewUnresolved(Handle<String> name,
404 bool inside_with,
405 int position) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000406 // Note that we must not share the unresolved variables with
407 // the same name because they may be removed selectively via
408 // RemoveUnresolved().
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000409 ASSERT(!already_resolved());
410 VariableProxy* proxy = new(isolate_->zone()) VariableProxy(
411 isolate_, name, false, inside_with, position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000412 unresolved_.Add(proxy);
413 return proxy;
414}
415
416
417void Scope::RemoveUnresolved(VariableProxy* var) {
418 // Most likely (always?) any variable we want to remove
419 // was just added before, so we search backwards.
420 for (int i = unresolved_.length(); i-- > 0;) {
421 if (unresolved_[i] == var) {
422 unresolved_.Remove(i);
423 return;
424 }
425 }
426}
427
428
Ben Murdochb0fe1622011-05-05 13:52:32 +0100429Variable* Scope::NewTemporary(Handle<String> name) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000430 ASSERT(!already_resolved());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100431 Variable* var =
432 new Variable(this, name, Variable::TEMPORARY, true, Variable::NORMAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000433 temps_.Add(var);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100434 return var;
Steve Blocka7e24c12009-10-30 11:49:00 +0000435}
436
437
438void Scope::AddDeclaration(Declaration* declaration) {
439 decls_.Add(declaration);
440}
441
442
443void Scope::SetIllegalRedeclaration(Expression* expression) {
Steve Block1e0659c2011-05-24 12:43:12 +0100444 // Record only the first illegal redeclaration.
Steve Blocka7e24c12009-10-30 11:49:00 +0000445 if (!HasIllegalRedeclaration()) {
446 illegal_redecl_ = expression;
447 }
448 ASSERT(HasIllegalRedeclaration());
449}
450
451
452void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) {
453 ASSERT(HasIllegalRedeclaration());
454 illegal_redecl_->Accept(visitor);
455}
456
457
458template<class Allocator>
459void Scope::CollectUsedVariables(List<Variable*, Allocator>* locals) {
460 // Collect variables in this scope.
461 // Note that the function_ variable - if present - is not
462 // collected here but handled separately in ScopeInfo
463 // which is the current user of this function).
464 for (int i = 0; i < temps_.length(); i++) {
465 Variable* var = temps_[i];
Steve Block6ded16b2010-05-10 14:33:55 +0100466 if (var->is_used()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000467 locals->Add(var);
468 }
469 }
470 for (VariableMap::Entry* p = variables_.Start();
471 p != NULL;
472 p = variables_.Next(p)) {
473 Variable* var = reinterpret_cast<Variable*>(p->value);
Steve Block6ded16b2010-05-10 14:33:55 +0100474 if (var->is_used()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000475 locals->Add(var);
476 }
477 }
478}
479
480
481// Make sure the method gets instantiated by the template system.
482template void Scope::CollectUsedVariables(
483 List<Variable*, FreeStoreAllocationPolicy>* locals);
484template void Scope::CollectUsedVariables(
485 List<Variable*, PreallocatedStorage>* locals);
486template void Scope::CollectUsedVariables(
487 List<Variable*, ZoneListAllocationPolicy>* locals);
488
489
490void Scope::AllocateVariables(Handle<Context> context) {
491 ASSERT(outer_scope_ == NULL); // eval or global scopes only
492
493 // 1) Propagate scope information.
494 // If we are in an eval scope, we may have other outer scopes about
495 // which we don't know anything at this point. Thus we must be conservative
496 // and assume they may invoke eval themselves. Eventually we could capture
497 // this information in the ScopeInfo and then use it here (by traversing
498 // the call chain stack, at compile time).
Ben Murdoch257744e2011-11-30 15:57:28 +0000499
Steve Blocka7e24c12009-10-30 11:49:00 +0000500 bool eval_scope = is_eval_scope();
Ben Murdoch257744e2011-11-30 15:57:28 +0000501 bool outer_scope_calls_eval = false;
502 bool outer_scope_calls_non_strict_eval = false;
503 if (!is_global_scope()) {
504 context->ComputeEvalScopeInfo(&outer_scope_calls_eval,
505 &outer_scope_calls_non_strict_eval);
506 }
507 PropagateScopeInfo(outer_scope_calls_eval,
508 outer_scope_calls_non_strict_eval,
509 eval_scope);
Steve Blocka7e24c12009-10-30 11:49:00 +0000510
511 // 2) Resolve variables.
512 Scope* global_scope = NULL;
513 if (is_global_scope()) global_scope = this;
514 ResolveVariablesRecursively(global_scope, context);
515
516 // 3) Allocate variables.
517 AllocateVariablesRecursively();
518}
519
520
521bool Scope::AllowsLazyCompilation() const {
522 return !force_eager_compilation_ && HasTrivialOuterContext();
523}
524
525
526bool Scope::HasTrivialContext() const {
527 // A function scope has a trivial context if it always is the global
528 // context. We iteratively scan out the context chain to see if
529 // there is anything that makes this scope non-trivial; otherwise we
530 // return true.
531 for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) {
532 if (scope->is_eval_scope()) return false;
533 if (scope->scope_inside_with_) return false;
534 if (scope->num_heap_slots_ > 0) return false;
535 }
536 return true;
537}
538
539
540bool Scope::HasTrivialOuterContext() const {
541 Scope* outer = outer_scope_;
542 if (outer == NULL) return true;
543 // Note that the outer context may be trivial in general, but the current
544 // scope may be inside a 'with' statement in which case the outer context
545 // for this scope is not trivial.
546 return !scope_inside_with_ && outer->HasTrivialContext();
547}
548
549
550int Scope::ContextChainLength(Scope* scope) {
551 int n = 0;
552 for (Scope* s = this; s != scope; s = s->outer_scope_) {
553 ASSERT(s != NULL); // scope must be in the scope chain
554 if (s->num_heap_slots() > 0) n++;
555 }
556 return n;
557}
558
559
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000560Scope* Scope::DeclarationScope() {
561 Scope* scope = this;
562 while (scope->is_catch_scope()) {
563 scope = scope->outer_scope();
564 }
565 return scope;
566}
567
568
Steve Blocka7e24c12009-10-30 11:49:00 +0000569#ifdef DEBUG
570static const char* Header(Scope::Type type) {
571 switch (type) {
572 case Scope::EVAL_SCOPE: return "eval";
573 case Scope::FUNCTION_SCOPE: return "function";
574 case Scope::GLOBAL_SCOPE: return "global";
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000575 case Scope::CATCH_SCOPE: return "catch";
Steve Blocka7e24c12009-10-30 11:49:00 +0000576 }
577 UNREACHABLE();
578 return NULL;
579}
580
581
582static void Indent(int n, const char* str) {
583 PrintF("%*s%s", n, "", str);
584}
585
586
587static void PrintName(Handle<String> name) {
588 SmartPointer<char> s = name->ToCString(DISALLOW_NULLS);
589 PrintF("%s", *s);
590}
591
592
593static void PrintVar(PrettyPrinter* printer, int indent, Variable* var) {
Steve Block6ded16b2010-05-10 14:33:55 +0100594 if (var->is_used() || var->rewrite() != NULL) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000595 Indent(indent, Variable::Mode2String(var->mode()));
596 PrintF(" ");
597 PrintName(var->name());
598 PrintF("; // ");
Steve Block6ded16b2010-05-10 14:33:55 +0100599 if (var->rewrite() != NULL) {
600 PrintF("%s, ", printer->Print(var->rewrite()));
601 if (var->is_accessed_from_inner_scope()) PrintF(", ");
602 }
603 if (var->is_accessed_from_inner_scope()) PrintF("inner scope access");
Steve Blocka7e24c12009-10-30 11:49:00 +0000604 PrintF("\n");
605 }
606}
607
608
609static void PrintMap(PrettyPrinter* printer, int indent, VariableMap* map) {
610 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
611 Variable* var = reinterpret_cast<Variable*>(p->value);
612 PrintVar(printer, indent, var);
613 }
614}
615
616
617void Scope::Print(int n) {
618 int n0 = (n > 0 ? n : 0);
619 int n1 = n0 + 2; // indentation
620
621 // Print header.
622 Indent(n0, Header(type_));
623 if (scope_name_->length() > 0) {
624 PrintF(" ");
625 PrintName(scope_name_);
626 }
627
628 // Print parameters, if any.
629 if (is_function_scope()) {
630 PrintF(" (");
631 for (int i = 0; i < params_.length(); i++) {
632 if (i > 0) PrintF(", ");
633 PrintName(params_[i]->name());
634 }
635 PrintF(")");
636 }
637
638 PrintF(" {\n");
639
640 // Function name, if any (named function literals, only).
641 if (function_ != NULL) {
642 Indent(n1, "// (local) function name: ");
643 PrintName(function_->name());
644 PrintF("\n");
645 }
646
647 // Scope info.
648 if (HasTrivialOuterContext()) {
649 Indent(n1, "// scope has trivial outer context\n");
650 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000651 if (is_strict_mode()) Indent(n1, "// strict mode scope\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000652 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
653 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n");
654 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n");
655 if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n");
Ben Murdoch257744e2011-11-30 15:57:28 +0000656 if (outer_scope_calls_non_strict_eval_) {
657 Indent(n1, "// outer scope calls 'eval' in non-strict context\n");
658 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000659 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
660 if (outer_scope_is_eval_scope_) {
661 Indent(n1, "// outer scope is 'eval' scope\n");
662 }
663 if (num_stack_slots_ > 0) { Indent(n1, "// ");
664 PrintF("%d stack slots\n", num_stack_slots_); }
665 if (num_heap_slots_ > 0) { Indent(n1, "// ");
666 PrintF("%d heap slots\n", num_heap_slots_); }
667
668 // Print locals.
669 PrettyPrinter printer;
670 Indent(n1, "// function var\n");
671 if (function_ != NULL) {
672 PrintVar(&printer, n1, function_);
673 }
674
675 Indent(n1, "// temporary vars\n");
676 for (int i = 0; i < temps_.length(); i++) {
677 PrintVar(&printer, n1, temps_[i]);
678 }
679
680 Indent(n1, "// local vars\n");
681 PrintMap(&printer, n1, &variables_);
682
683 Indent(n1, "// dynamic vars\n");
684 if (dynamics_ != NULL) {
685 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC));
686 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_LOCAL));
687 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_GLOBAL));
688 }
689
690 // Print inner scopes (disable by providing negative n).
691 if (n >= 0) {
692 for (int i = 0; i < inner_scopes_.length(); i++) {
693 PrintF("\n");
694 inner_scopes_[i]->Print(n1);
695 }
696 }
697
698 Indent(n0, "}\n");
699}
700#endif // DEBUG
701
702
703Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) {
704 if (dynamics_ == NULL) dynamics_ = new DynamicScopePart();
705 VariableMap* map = dynamics_->GetMap(mode);
706 Variable* var = map->Lookup(name);
707 if (var == NULL) {
708 // Declare a new non-local.
709 var = map->Declare(NULL, name, mode, true, Variable::NORMAL);
710 // Allocate it by giving it a dynamic lookup.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000711 var->set_rewrite(NewSlot(var, Slot::LOOKUP, -1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000712 }
713 return var;
714}
715
716
717// Lookup a variable starting with this scope. The result is either
Steve Blockd0582a62009-12-15 09:54:21 +0000718// the statically resolved variable belonging to an outer scope, or
719// NULL. It may be NULL because a) we couldn't find a variable, or b)
720// because the variable is just a guess (and may be shadowed by
721// another variable that is introduced dynamically via an 'eval' call
722// or a 'with' statement).
Steve Blocka7e24c12009-10-30 11:49:00 +0000723Variable* Scope::LookupRecursive(Handle<String> name,
724 bool inner_lookup,
725 Variable** invalidated_local) {
726 // If we find a variable, but the current scope calls 'eval', the found
727 // variable may not be the correct one (the 'eval' may introduce a
728 // property with the same name). In that case, remember that the variable
729 // found is just a guess.
730 bool guess = scope_calls_eval_;
731
732 // Try to find the variable in this scope.
733 Variable* var = LocalLookup(name);
734
735 if (var != NULL) {
736 // We found a variable. If this is not an inner lookup, we are done.
737 // (Even if there is an 'eval' in this scope which introduces the
738 // same variable again, the resulting variable remains the same.
739 // Note that enclosing 'with' statements are handled at the call site.)
740 if (!inner_lookup)
741 return var;
742
743 } else {
744 // We did not find a variable locally. Check against the function variable,
745 // if any. We can do this for all scopes, since the function variable is
746 // only present - if at all - for function scopes.
747 //
748 // This lookup corresponds to a lookup in the "intermediate" scope sitting
749 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
750 // the name of named function literal is kept in an intermediate scope
751 // in between this scope and the next outer scope.)
752 if (function_ != NULL && function_->name().is_identical_to(name)) {
753 var = function_;
754
755 } else if (outer_scope_ != NULL) {
756 var = outer_scope_->LookupRecursive(name, true, invalidated_local);
757 // We may have found a variable in an outer scope. However, if
758 // the current scope is inside a 'with', the actual variable may
759 // be a property introduced via the 'with' statement. Then, the
760 // variable we may have found is just a guess.
761 if (scope_inside_with_)
762 guess = true;
763 }
764
765 // If we did not find a variable, we are done.
766 if (var == NULL)
767 return NULL;
768 }
769
770 ASSERT(var != NULL);
771
772 // If this is a lookup from an inner scope, mark the variable.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100773 if (inner_lookup) {
774 var->MarkAsAccessedFromInnerScope();
775 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000776
Steve Blockd0582a62009-12-15 09:54:21 +0000777 // If the variable we have found is just a guess, invalidate the
778 // result. If the found variable is local, record that fact so we
779 // can generate fast code to get it if it is not shadowed by eval.
Steve Blocka7e24c12009-10-30 11:49:00 +0000780 if (guess) {
Steve Blockd0582a62009-12-15 09:54:21 +0000781 if (!var->is_global()) *invalidated_local = var;
Steve Blocka7e24c12009-10-30 11:49:00 +0000782 var = NULL;
783 }
784
785 return var;
786}
787
788
789void Scope::ResolveVariable(Scope* global_scope,
790 Handle<Context> context,
791 VariableProxy* proxy) {
792 ASSERT(global_scope == NULL || global_scope->is_global_scope());
793
794 // If the proxy is already resolved there's nothing to do
795 // (functions and consts may be resolved by the parser).
796 if (proxy->var() != NULL) return;
797
798 // Otherwise, try to resolve the variable.
799 Variable* invalidated_local = NULL;
800 Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local);
801
802 if (proxy->inside_with()) {
803 // If we are inside a local 'with' statement, all bets are off
804 // and we cannot resolve the proxy to a local variable even if
805 // we found an outer matching variable.
806 // Note that we must do a lookup anyway, because if we find one,
807 // we must mark that variable as potentially accessed from this
808 // inner scope (the property may not be in the 'with' object).
809 var = NonLocal(proxy->name(), Variable::DYNAMIC);
810
811 } else {
812 // We are not inside a local 'with' statement.
813
814 if (var == NULL) {
815 // We did not find the variable. We have a global variable
816 // if we are in the global scope (we know already that we
817 // are outside a 'with' statement) or if there is no way
818 // that the variable might be introduced dynamically (through
819 // a local or outer eval() call, or an outer 'with' statement),
820 // or we don't know about the outer scope (because we are
821 // in an eval scope).
822 if (is_global_scope() ||
823 !(scope_inside_with_ || outer_scope_is_eval_scope_ ||
824 scope_calls_eval_ || outer_scope_calls_eval_)) {
825 // We must have a global variable.
826 ASSERT(global_scope != NULL);
827 var = global_scope->DeclareGlobal(proxy->name());
828
829 } else if (scope_inside_with_) {
830 // If we are inside a with statement we give up and look up
831 // the variable at runtime.
832 var = NonLocal(proxy->name(), Variable::DYNAMIC);
833
834 } else if (invalidated_local != NULL) {
835 // No with statements are involved and we found a local
836 // variable that might be shadowed by eval introduced
837 // variables.
838 var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL);
839 var->set_local_if_not_shadowed(invalidated_local);
840
841 } else if (outer_scope_is_eval_scope_) {
842 // No with statements and we did not find a local and the code
843 // is executed with a call to eval. The context contains
844 // scope information that we can use to determine if the
845 // variable is global if it is not shadowed by eval-introduced
846 // variables.
847 if (context->GlobalIfNotShadowedByEval(proxy->name())) {
848 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
849
850 } else {
851 var = NonLocal(proxy->name(), Variable::DYNAMIC);
852 }
853
854 } else {
855 // No with statements and we did not find a local and the code
856 // is not executed with a call to eval. We know that this
857 // variable is global unless it is shadowed by eval-introduced
858 // variables.
859 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
860 }
861 }
862 }
863
864 proxy->BindTo(var);
865}
866
867
868void Scope::ResolveVariablesRecursively(Scope* global_scope,
869 Handle<Context> context) {
870 ASSERT(global_scope == NULL || global_scope->is_global_scope());
871
872 // Resolve unresolved variables for this scope.
873 for (int i = 0; i < unresolved_.length(); i++) {
874 ResolveVariable(global_scope, context, unresolved_[i]);
875 }
876
877 // Resolve unresolved variables for inner scopes.
878 for (int i = 0; i < inner_scopes_.length(); i++) {
879 inner_scopes_[i]->ResolveVariablesRecursively(global_scope, context);
880 }
881}
882
883
884bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval,
Ben Murdoch257744e2011-11-30 15:57:28 +0000885 bool outer_scope_calls_non_strict_eval,
Steve Blocka7e24c12009-10-30 11:49:00 +0000886 bool outer_scope_is_eval_scope) {
887 if (outer_scope_calls_eval) {
888 outer_scope_calls_eval_ = true;
889 }
890
Ben Murdoch257744e2011-11-30 15:57:28 +0000891 if (outer_scope_calls_non_strict_eval) {
892 outer_scope_calls_non_strict_eval_ = true;
893 }
894
Steve Blocka7e24c12009-10-30 11:49:00 +0000895 if (outer_scope_is_eval_scope) {
896 outer_scope_is_eval_scope_ = true;
897 }
898
899 bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_;
900 bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000901 bool calls_non_strict_eval =
902 (scope_calls_eval_ && !is_strict_mode()) ||
903 outer_scope_calls_non_strict_eval_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000904 for (int i = 0; i < inner_scopes_.length(); i++) {
905 Scope* inner_scope = inner_scopes_[i];
Ben Murdoch257744e2011-11-30 15:57:28 +0000906 if (inner_scope->PropagateScopeInfo(calls_eval,
907 calls_non_strict_eval,
908 is_eval)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000909 inner_scope_calls_eval_ = true;
910 }
911 if (inner_scope->force_eager_compilation_) {
912 force_eager_compilation_ = true;
913 }
914 }
915
916 return scope_calls_eval_ || inner_scope_calls_eval_;
917}
918
919
920bool Scope::MustAllocate(Variable* var) {
921 // Give var a read/write use if there is a chance it might be accessed
922 // via an eval() call. This is only possible if the variable has a
923 // visible name.
924 if ((var->is_this() || var->name()->length() > 0) &&
Ben Murdochb8e0da22011-05-16 14:20:40 +0100925 (var->is_accessed_from_inner_scope() ||
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000926 scope_calls_eval_ ||
927 inner_scope_calls_eval_ ||
928 scope_contains_with_ ||
929 is_catch_scope())) {
Steve Block6ded16b2010-05-10 14:33:55 +0100930 var->set_is_used(true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000931 }
932 // Global variables do not need to be allocated.
Steve Block6ded16b2010-05-10 14:33:55 +0100933 return !var->is_global() && var->is_used();
Steve Blocka7e24c12009-10-30 11:49:00 +0000934}
935
936
937bool Scope::MustAllocateInContext(Variable* var) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000938 // If var is accessed from an inner scope, or if there is a possibility
939 // that it might be accessed from the current or an inner scope (through
940 // an eval() call or a runtime with lookup), it must be allocated in the
Steve Blocka7e24c12009-10-30 11:49:00 +0000941 // context.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000942 //
943 // Exceptions: temporary variables are never allocated in a context;
944 // catch-bound variables are always allocated in a context.
945 if (var->mode() == Variable::TEMPORARY) return false;
946 if (is_catch_scope()) return true;
947 return var->is_accessed_from_inner_scope() ||
948 scope_calls_eval_ ||
949 inner_scope_calls_eval_ ||
950 scope_contains_with_ ||
951 var->is_global();
Steve Blocka7e24c12009-10-30 11:49:00 +0000952}
953
954
955bool Scope::HasArgumentsParameter() {
956 for (int i = 0; i < params_.length(); i++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000957 if (params_[i]->name().is_identical_to(
958 isolate_->factory()->arguments_symbol())) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000959 return true;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000960 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000961 }
962 return false;
963}
964
965
966void Scope::AllocateStackSlot(Variable* var) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000967 var->set_rewrite(NewSlot(var, Slot::LOCAL, num_stack_slots_++));
Steve Blocka7e24c12009-10-30 11:49:00 +0000968}
969
970
971void Scope::AllocateHeapSlot(Variable* var) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000972 var->set_rewrite(NewSlot(var, Slot::CONTEXT, num_heap_slots_++));
Steve Blocka7e24c12009-10-30 11:49:00 +0000973}
974
975
976void Scope::AllocateParameterLocals() {
977 ASSERT(is_function_scope());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000978 Variable* arguments = LocalLookup(isolate_->factory()->arguments_symbol());
Steve Blocka7e24c12009-10-30 11:49:00 +0000979 ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly
Steve Block44f0eee2011-05-26 01:26:41 +0100980
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000981 bool uses_nonstrict_arguments = false;
Steve Block44f0eee2011-05-26 01:26:41 +0100982
Steve Blocka7e24c12009-10-30 11:49:00 +0000983 if (MustAllocate(arguments) && !HasArgumentsParameter()) {
984 // 'arguments' is used. Unless there is also a parameter called
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000985 // 'arguments', we must be conservative and allocate all parameters to
986 // the context assuming they will be captured by the arguments object.
987 // If we have a parameter named 'arguments', a (new) value is always
988 // assigned to it via the function invocation. Then 'arguments' denotes
989 // that specific parameter value and cannot be used to access the
990 // parameters, which is why we don't need to allocate an arguments
991 // object in that case.
Steve Blocka7e24c12009-10-30 11:49:00 +0000992
993 // We are using 'arguments'. Tell the code generator that is needs to
994 // allocate the arguments object by setting 'arguments_'.
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100995 arguments_ = arguments;
Steve Blocka7e24c12009-10-30 11:49:00 +0000996
Steve Block44f0eee2011-05-26 01:26:41 +0100997 // In strict mode 'arguments' does not alias formal parameters.
998 // Therefore in strict mode we allocate parameters as if 'arguments'
999 // were not used.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001000 uses_nonstrict_arguments = !is_strict_mode();
Steve Block44f0eee2011-05-26 01:26:41 +01001001 }
1002
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001003 // The same parameter may occur multiple times in the parameters_ list.
1004 // If it does, and if it is not copied into the context object, it must
1005 // receive the highest parameter index for that parameter; thus iteration
1006 // order is relevant!
1007 for (int i = params_.length() - 1; i >= 0; --i) {
1008 Variable* var = params_[i];
1009 ASSERT(var->scope() == this);
1010 if (uses_nonstrict_arguments) {
1011 // Give the parameter a use from an inner scope, to force allocation
1012 // to the context.
1013 var->MarkAsAccessedFromInnerScope();
Steve Blocka7e24c12009-10-30 11:49:00 +00001014 }
1015
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001016 if (MustAllocate(var)) {
1017 if (MustAllocateInContext(var)) {
1018 ASSERT(var->rewrite() == NULL || var->IsContextSlot());
1019 if (var->rewrite() == NULL) {
1020 AllocateHeapSlot(var);
1021 }
1022 } else {
1023 ASSERT(var->rewrite() == NULL || var->IsParameter());
1024 if (var->rewrite() == NULL) {
1025 var->set_rewrite(NewSlot(var, Slot::PARAMETER, i));
Steve Blocka7e24c12009-10-30 11:49:00 +00001026 }
1027 }
1028 }
1029 }
1030}
1031
1032
1033void Scope::AllocateNonParameterLocal(Variable* var) {
1034 ASSERT(var->scope() == this);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001035 ASSERT(var->rewrite() == NULL ||
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001036 !var->IsVariable(isolate_->factory()->result_symbol()) ||
1037 var->AsSlot() == NULL ||
1038 var->AsSlot()->type() != Slot::LOCAL);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001039 if (var->rewrite() == NULL && MustAllocate(var)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001040 if (MustAllocateInContext(var)) {
1041 AllocateHeapSlot(var);
1042 } else {
1043 AllocateStackSlot(var);
1044 }
1045 }
1046}
1047
1048
1049void Scope::AllocateNonParameterLocals() {
1050 // All variables that have no rewrite yet are non-parameter locals.
1051 for (int i = 0; i < temps_.length(); i++) {
1052 AllocateNonParameterLocal(temps_[i]);
1053 }
1054
1055 for (VariableMap::Entry* p = variables_.Start();
1056 p != NULL;
1057 p = variables_.Next(p)) {
1058 Variable* var = reinterpret_cast<Variable*>(p->value);
1059 AllocateNonParameterLocal(var);
1060 }
1061
1062 // For now, function_ must be allocated at the very end. If it gets
1063 // allocated in the context, it must be the last slot in the context,
1064 // because of the current ScopeInfo implementation (see
1065 // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor).
1066 if (function_ != NULL) {
1067 AllocateNonParameterLocal(function_);
1068 }
1069}
1070
1071
1072void Scope::AllocateVariablesRecursively() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001073 // Allocate variables for inner scopes.
1074 for (int i = 0; i < inner_scopes_.length(); i++) {
1075 inner_scopes_[i]->AllocateVariablesRecursively();
1076 }
1077
Ben Murdochb8e0da22011-05-16 14:20:40 +01001078 // If scope is already resolved, we still need to allocate
1079 // variables in inner scopes which might not had been resolved yet.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001080 if (already_resolved()) return;
Ben Murdochb8e0da22011-05-16 14:20:40 +01001081 // The number of slots required for variables.
1082 num_stack_slots_ = 0;
1083 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
1084
Steve Blocka7e24c12009-10-30 11:49:00 +00001085 // Allocate variables for this scope.
1086 // Parameters must be allocated first, if any.
1087 if (is_function_scope()) AllocateParameterLocals();
1088 AllocateNonParameterLocals();
1089
1090 // Allocate context if necessary.
1091 bool must_have_local_context = false;
1092 if (scope_calls_eval_ || scope_contains_with_) {
1093 // The context for the eval() call or 'with' statement in this scope.
1094 // Unless we are in the global or an eval scope, we need a local
1095 // context even if we didn't statically allocate any locals in it,
1096 // and the compiler will access the context variable. If we are
1097 // not in an inner scope, the scope is provided from the outside.
1098 must_have_local_context = is_function_scope();
1099 }
1100
1101 // If we didn't allocate any locals in the local context, then we only
1102 // need the minimal number of slots if we must have a local context.
1103 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS &&
1104 !must_have_local_context) {
1105 num_heap_slots_ = 0;
1106 }
1107
1108 // Allocation done.
1109 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
1110}
1111
1112} } // namespace v8::internal