blob: d5a7a9f9ca8d7b86361856fa92bde29bbf796489 [file] [log] [blame]
Ben Murdoch85b71792012-04-11 18:30:58 +01001// 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 "scopeinfo.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000035
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000036#include "allocation-inl.h"
37
Steve Blocka7e24c12009-10-30 11:49:00 +000038namespace v8 {
39namespace internal {
40
41// ----------------------------------------------------------------------------
Ben Murdoch85b71792012-04-11 18:30:58 +010042// A Zone allocator for use with LocalsMap.
43
44// TODO(isolates): It is probably worth it to change the Allocator class to
45// take a pointer to an isolate.
46class ZoneAllocator: public Allocator {
47 public:
48 /* nothing to do */
49 virtual ~ZoneAllocator() {}
50
51 virtual void* New(size_t size) { return ZONE->New(static_cast<int>(size)); }
52
53 /* ignored - Zone is freed in one fell swoop */
54 virtual void Delete(void* p) {}
55};
56
57
58static ZoneAllocator LocalsMapAllocator;
59
60
61// ----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +000062// Implementation of LocalsMap
63//
64// Note: We are storing the handle locations as key values in the hash map.
65// When inserting a new variable via Declare(), we rely on the fact that
66// the handle location remains alive for the duration of that variable
67// use. Because a Variable holding a handle with the same location exists
68// this is ensured.
69
70static bool Match(void* key1, void* key2) {
71 String* name1 = *reinterpret_cast<String**>(key1);
72 String* name2 = *reinterpret_cast<String**>(key2);
73 ASSERT(name1->IsSymbol());
74 ASSERT(name2->IsSymbol());
75 return name1 == name2;
76}
77
78
Ben Murdoch85b71792012-04-11 18:30:58 +010079// Dummy constructor
80VariableMap::VariableMap(bool gotta_love_static_overloading) : HashMap() {}
81
82VariableMap::VariableMap() : HashMap(Match, &LocalsMapAllocator, 8) {}
Steve Blocka7e24c12009-10-30 11:49:00 +000083VariableMap::~VariableMap() {}
84
85
Ben Murdoch85b71792012-04-11 18:30:58 +010086Variable* VariableMap::Declare(Scope* scope,
87 Handle<String> name,
88 Variable::Mode mode,
89 bool is_valid_lhs,
90 Variable::Kind kind) {
91 HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), true);
Steve Blocka7e24c12009-10-30 11:49:00 +000092 if (p->value == NULL) {
93 // The variable has not been declared yet -> insert it.
94 ASSERT(p->key == name.location());
Ben Murdoch85b71792012-04-11 18:30:58 +010095 p->value = new Variable(scope, name, mode, is_valid_lhs, kind);
Steve Blocka7e24c12009-10-30 11:49:00 +000096 }
97 return reinterpret_cast<Variable*>(p->value);
98}
99
100
101Variable* VariableMap::Lookup(Handle<String> name) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100102 HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000103 if (p != NULL) {
104 ASSERT(*reinterpret_cast<String**>(p->key) == *name);
105 ASSERT(p->value != NULL);
106 return reinterpret_cast<Variable*>(p->value);
107 }
108 return NULL;
109}
110
111
112// ----------------------------------------------------------------------------
113// Implementation of Scope
114
Ben Murdoch85b71792012-04-11 18:30:58 +0100115
116// Dummy constructor
117Scope::Scope(Type type)
118 : isolate_(Isolate::Current()),
119 inner_scopes_(0),
120 variables_(false),
121 temps_(0),
122 params_(0),
123 unresolved_(0),
124 decls_(0),
125 already_resolved_(false) {
126 SetDefaults(type, NULL, Handle<SerializedScopeInfo>::null());
127}
128
129
130Scope::Scope(Scope* outer_scope, Type type)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000131 : isolate_(Isolate::Current()),
132 inner_scopes_(4),
133 variables_(),
134 temps_(4),
135 params_(4),
136 unresolved_(16),
137 decls_(4),
138 already_resolved_(false) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100139 SetDefaults(type, outer_scope, Handle<SerializedScopeInfo>::null());
Steve Blocka7e24c12009-10-30 11:49:00 +0000140 // At some point we might want to provide outer scopes to
141 // eval scopes (by walking the stack and reading the scope info).
142 // In that case, the ASSERT below needs to be adjusted.
Ben Murdoch85b71792012-04-11 18:30:58 +0100143 ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL));
Steve Blocka7e24c12009-10-30 11:49:00 +0000144 ASSERT(!HasIllegalRedeclaration());
145}
146
147
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000148Scope::Scope(Scope* inner_scope,
Ben Murdoch85b71792012-04-11 18:30:58 +0100149 Type type,
150 Handle<SerializedScopeInfo> scope_info)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000151 : isolate_(Isolate::Current()),
152 inner_scopes_(4),
153 variables_(),
154 temps_(4),
155 params_(4),
156 unresolved_(16),
157 decls_(4),
158 already_resolved_(true) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100159 ASSERT(!scope_info.is_null());
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000160 SetDefaults(type, NULL, scope_info);
Ben Murdoch85b71792012-04-11 18:30:58 +0100161 if (scope_info->HasHeapAllocatedLocals()) {
162 num_heap_slots_ = scope_info_->NumberOfContextSlots();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100163 }
Steve Block44f0eee2011-05-26 01:26:41 +0100164 AddInnerScope(inner_scope);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000165}
Steve Block44f0eee2011-05-26 01:26:41 +0100166
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000167
168Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name)
169 : isolate_(Isolate::Current()),
170 inner_scopes_(1),
171 variables_(),
172 temps_(0),
173 params_(0),
174 unresolved_(0),
175 decls_(0),
176 already_resolved_(true) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100177 SetDefaults(CATCH_SCOPE, NULL, Handle<SerializedScopeInfo>::null());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000178 AddInnerScope(inner_scope);
179 ++num_var_or_const_;
180 Variable* variable = variables_.Declare(this,
181 catch_variable_name,
Ben Murdoch85b71792012-04-11 18:30:58 +0100182 Variable::VAR,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000183 true, // Valid left-hand side.
Ben Murdoch85b71792012-04-11 18:30:58 +0100184 Variable::NORMAL);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000185 AllocateHeapSlot(variable);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100186}
187
188
Ben Murdoch85b71792012-04-11 18:30:58 +0100189void Scope::SetDefaults(Type type,
Ben Murdoch8b112d22011-06-08 16:22:53 +0100190 Scope* outer_scope,
Ben Murdoch85b71792012-04-11 18:30:58 +0100191 Handle<SerializedScopeInfo> scope_info) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100192 outer_scope_ = outer_scope;
193 type_ = type;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000194 scope_name_ = isolate_->factory()->empty_symbol();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100195 dynamics_ = NULL;
196 receiver_ = NULL;
197 function_ = NULL;
198 arguments_ = NULL;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100199 illegal_redecl_ = NULL;
200 scope_inside_with_ = false;
201 scope_contains_with_ = false;
202 scope_calls_eval_ = false;
203 // Inherit the strict mode from the parent scope.
Ben Murdoch85b71792012-04-11 18:30:58 +0100204 strict_mode_ = (outer_scope != NULL) && outer_scope->strict_mode_;
205 outer_scope_calls_eval_ = false;
Ben Murdoch257744e2011-11-30 15:57:28 +0000206 outer_scope_calls_non_strict_eval_ = false;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100207 inner_scope_calls_eval_ = false;
Ben Murdoch85b71792012-04-11 18:30:58 +0100208 outer_scope_is_eval_scope_ = false;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100209 force_eager_compilation_ = false;
Steve Block053d10c2011-06-13 19:13:29 +0100210 num_var_or_const_ = 0;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100211 num_stack_slots_ = 0;
212 num_heap_slots_ = 0;
213 scope_info_ = scope_info;
214}
215
216
Ben Murdoch85b71792012-04-11 18:30:58 +0100217Scope* Scope::DeserializeScopeChain(CompilationInfo* info,
218 Scope* global_scope) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000219 // Reconstruct the outer scope chain from a closure's context chain.
Ben Murdoch85b71792012-04-11 18:30:58 +0100220 ASSERT(!info->closure().is_null());
221 Context* context = info->closure()->context();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000222 Scope* current_scope = NULL;
Steve Block44f0eee2011-05-26 01:26:41 +0100223 Scope* innermost_scope = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000224 bool contains_with = false;
225 while (!context->IsGlobalContext()) {
226 if (context->IsWithContext()) {
227 // All the inner scopes are inside a with.
228 contains_with = true;
229 for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) {
230 s->scope_inside_with_ = true;
Steve Block44f0eee2011-05-26 01:26:41 +0100231 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000232 } else {
Ben Murdoch85b71792012-04-11 18:30:58 +0100233 if (context->IsFunctionContext()) {
234 SerializedScopeInfo* scope_info =
235 context->closure()->shared()->scope_info();
236 current_scope = new Scope(current_scope, FUNCTION_SCOPE,
237 Handle<SerializedScopeInfo>(scope_info));
238 } else if (context->IsBlockContext()) {
239 SerializedScopeInfo* scope_info =
240 SerializedScopeInfo::cast(context->extension());
241 current_scope = new Scope(current_scope, BLOCK_SCOPE,
242 Handle<SerializedScopeInfo>(scope_info));
243 } else {
244 ASSERT(context->IsCatchContext());
245 String* name = String::cast(context->extension());
246 current_scope = new Scope(current_scope, Handle<String>(name));
247 }
248 if (contains_with) current_scope->RecordWithStatement();
249 if (innermost_scope == NULL) innermost_scope = current_scope;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000250 }
251
252 // Forget about a with when we move to a context for a different function.
253 if (context->previous()->closure() != context->closure()) {
254 contains_with = false;
255 }
256 context = context->previous();
Steve Block44f0eee2011-05-26 01:26:41 +0100257 }
258
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000259 global_scope->AddInnerScope(current_scope);
260 return (innermost_scope == NULL) ? global_scope : innermost_scope;
Steve Block44f0eee2011-05-26 01:26:41 +0100261}
262
Ben Murdochb8e0da22011-05-16 14:20:40 +0100263
Ben Murdochf87a2032010-10-22 12:50:53 +0100264bool Scope::Analyze(CompilationInfo* info) {
265 ASSERT(info->function() != NULL);
Ben Murdoch85b71792012-04-11 18:30:58 +0100266 Scope* top = info->function()->scope();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100267
Ben Murdoch85b71792012-04-11 18:30:58 +0100268 while (top->outer_scope() != NULL) top = top->outer_scope();
269 top->AllocateVariables(info->calling_context());
Ben Murdochf87a2032010-10-22 12:50:53 +0100270
271#ifdef DEBUG
Steve Block44f0eee2011-05-26 01:26:41 +0100272 if (info->isolate()->bootstrapper()->IsActive()
Ben Murdochf87a2032010-10-22 12:50:53 +0100273 ? FLAG_print_builtin_scopes
274 : FLAG_print_scopes) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100275 info->function()->scope()->Print();
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100276 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100277#endif
278
Ben Murdoch85b71792012-04-11 18:30:58 +0100279 info->SetScope(info->function()->scope());
280 return true; // Can not fail.
Ben Murdochf87a2032010-10-22 12:50:53 +0100281}
282
283
Ben Murdoch85b71792012-04-11 18:30:58 +0100284void Scope::Initialize(bool inside_with) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000285 ASSERT(!already_resolved());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100286
Steve Blocka7e24c12009-10-30 11:49:00 +0000287 // Add this scope as a new inner scope of the outer scope.
288 if (outer_scope_ != NULL) {
289 outer_scope_->inner_scopes_.Add(this);
Ben Murdoch85b71792012-04-11 18:30:58 +0100290 scope_inside_with_ = outer_scope_->scope_inside_with_ || inside_with;
Steve Blocka7e24c12009-10-30 11:49:00 +0000291 } else {
Ben Murdoch85b71792012-04-11 18:30:58 +0100292 scope_inside_with_ = inside_with;
Steve Blocka7e24c12009-10-30 11:49:00 +0000293 }
294
295 // Declare convenience variables.
296 // Declare and allocate receiver (even for the global scope, and even
297 // if naccesses_ == 0).
298 // NOTE: When loading parameters in the global scope, we must take
299 // care not to access them as properties of the global object, but
300 // instead load them directly from the stack. Currently, the only
301 // such parameter is 'this' which is passed on the stack when
302 // invoking scripts
Ben Murdoch85b71792012-04-11 18:30:58 +0100303 if (is_catch_scope() || is_block_scope()) {
304 ASSERT(outer_scope() != NULL);
305 receiver_ = outer_scope()->receiver();
306 } else {
307 ASSERT(is_function_scope() ||
308 is_global_scope() ||
309 is_eval_scope());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000310 Variable* var =
311 variables_.Declare(this,
312 isolate_->factory()->this_symbol(),
Ben Murdoch85b71792012-04-11 18:30:58 +0100313 Variable::VAR,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000314 false,
Ben Murdoch85b71792012-04-11 18:30:58 +0100315 Variable::THIS);
Ben Murdoch589d6972011-11-30 16:04:58 +0000316 var->AllocateTo(Variable::PARAMETER, -1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000317 receiver_ = var;
318 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000319
320 if (is_function_scope()) {
321 // Declare 'arguments' variable which exists in all functions.
322 // Note that it might never be accessed, in which case it won't be
323 // allocated during variable allocation.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000324 variables_.Declare(this,
325 isolate_->factory()->arguments_symbol(),
Ben Murdoch85b71792012-04-11 18:30:58 +0100326 Variable::VAR,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000327 true,
Ben Murdoch85b71792012-04-11 18:30:58 +0100328 Variable::ARGUMENTS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 }
330}
331
332
Ben Murdoch589d6972011-11-30 16:04:58 +0000333Scope* Scope::FinalizeBlockScope() {
334 ASSERT(is_block_scope());
335 ASSERT(temps_.is_empty());
336 ASSERT(params_.is_empty());
337
338 if (num_var_or_const() > 0) return this;
339
340 // Remove this scope from outer scope.
341 for (int i = 0; i < outer_scope_->inner_scopes_.length(); i++) {
342 if (outer_scope_->inner_scopes_[i] == this) {
343 outer_scope_->inner_scopes_.Remove(i);
344 break;
345 }
346 }
347
348 // Reparent inner scopes.
349 for (int i = 0; i < inner_scopes_.length(); i++) {
350 outer_scope()->AddInnerScope(inner_scopes_[i]);
351 }
352
353 // Move unresolved variables
354 for (int i = 0; i < unresolved_.length(); i++) {
355 outer_scope()->unresolved_.Add(unresolved_[i]);
356 }
357
358 return NULL;
359}
360
361
Steve Blocka7e24c12009-10-30 11:49:00 +0000362Variable* Scope::LocalLookup(Handle<String> name) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100363 Variable* result = variables_.Lookup(name);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000364 if (result != NULL || scope_info_.is_null()) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100365 return result;
366 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000367 // If we have a serialized scope info, we might find the variable there.
Ben Murdoch85b71792012-04-11 18:30:58 +0100368 //
369 // We should never lookup 'arguments' in this scope as it is implicitly
370 // present in every scope.
371 ASSERT(*name != *isolate_->factory()->arguments_symbol());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000372 // There should be no local slot with the given name.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100373 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
374
375 // Check context slot lookup.
Ben Murdoch85b71792012-04-11 18:30:58 +0100376 Variable::Mode mode;
377 int index = scope_info_->ContextSlotIndex(*name, &mode);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000378 if (index < 0) {
379 // Check parameters.
Ben Murdoch85b71792012-04-11 18:30:58 +0100380 mode = Variable::VAR;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000381 index = scope_info_->ParameterIndex(*name);
Ben Murdoch85b71792012-04-11 18:30:58 +0100382 if (index < 0) {
383 // Check the function name.
384 index = scope_info_->FunctionContextSlotIndex(*name);
385 if (index < 0) return NULL;
386 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100387 }
388
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000389 Variable* var =
Ben Murdoch85b71792012-04-11 18:30:58 +0100390 variables_.Declare(this, name, mode, true, Variable::NORMAL);
Ben Murdoch589d6972011-11-30 16:04:58 +0000391 var->AllocateTo(Variable::CONTEXT, index);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000392 return var;
Steve Blocka7e24c12009-10-30 11:49:00 +0000393}
394
395
396Variable* Scope::Lookup(Handle<String> name) {
397 for (Scope* scope = this;
398 scope != NULL;
399 scope = scope->outer_scope()) {
400 Variable* var = scope->LocalLookup(name);
401 if (var != NULL) return var;
402 }
403 return NULL;
404}
405
406
Ben Murdoch85b71792012-04-11 18:30:58 +0100407Variable* Scope::DeclareFunctionVar(Handle<String> name) {
408 ASSERT(is_function_scope() && function_ == NULL);
409 Variable* function_var =
410 new Variable(this, name, Variable::CONST, true, Variable::NORMAL);
411 function_ = new(isolate_->zone()) VariableProxy(isolate_, function_var);
412 return function_var;
413}
414
415
416void Scope::DeclareParameter(Handle<String> name, Variable::Mode mode) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000417 ASSERT(!already_resolved());
418 ASSERT(is_function_scope());
Ben Murdoch85b71792012-04-11 18:30:58 +0100419 Variable* var =
420 variables_.Declare(this, name, mode, true, Variable::NORMAL);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000421 params_.Add(var);
422}
423
424
Ben Murdoch85b71792012-04-11 18:30:58 +0100425Variable* Scope::DeclareLocal(Handle<String> name, Variable::Mode mode) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000426 ASSERT(!already_resolved());
427 // This function handles VAR and CONST modes. DYNAMIC variables are
428 // introduces during variable allocation, INTERNAL variables are allocated
429 // explicitly, and TEMPORARY variables are allocated via NewTemporary().
Ben Murdoch85b71792012-04-11 18:30:58 +0100430 ASSERT(mode == Variable::VAR ||
431 mode == Variable::CONST ||
432 mode == Variable::LET);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000433 ++num_var_or_const_;
Ben Murdoch85b71792012-04-11 18:30:58 +0100434 return variables_.Declare(this, name, mode, true, Variable::NORMAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000435}
436
437
438Variable* Scope::DeclareGlobal(Handle<String> name) {
439 ASSERT(is_global_scope());
Ben Murdoch85b71792012-04-11 18:30:58 +0100440 return variables_.Declare(this, name, Variable::DYNAMIC_GLOBAL,
Ben Murdoch589d6972011-11-30 16:04:58 +0000441 true,
Ben Murdoch85b71792012-04-11 18:30:58 +0100442 Variable::NORMAL);
443}
444
445
446VariableProxy* Scope::NewUnresolved(Handle<String> name,
447 bool inside_with,
448 int position) {
449 // Note that we must not share the unresolved variables with
450 // the same name because they may be removed selectively via
451 // RemoveUnresolved().
452 ASSERT(!already_resolved());
453 VariableProxy* proxy = new(isolate_->zone()) VariableProxy(
454 isolate_, name, false, inside_with, position);
455 unresolved_.Add(proxy);
456 return proxy;
Steve Blocka7e24c12009-10-30 11:49:00 +0000457}
458
459
Steve Blocka7e24c12009-10-30 11:49:00 +0000460void Scope::RemoveUnresolved(VariableProxy* var) {
461 // Most likely (always?) any variable we want to remove
462 // was just added before, so we search backwards.
463 for (int i = unresolved_.length(); i-- > 0;) {
464 if (unresolved_[i] == var) {
465 unresolved_.Remove(i);
466 return;
467 }
468 }
469}
470
471
Ben Murdochb0fe1622011-05-05 13:52:32 +0100472Variable* Scope::NewTemporary(Handle<String> name) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000473 ASSERT(!already_resolved());
Ben Murdoch589d6972011-11-30 16:04:58 +0000474 Variable* var = new Variable(this,
475 name,
Ben Murdoch85b71792012-04-11 18:30:58 +0100476 Variable::TEMPORARY,
Ben Murdoch589d6972011-11-30 16:04:58 +0000477 true,
Ben Murdoch85b71792012-04-11 18:30:58 +0100478 Variable::NORMAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000479 temps_.Add(var);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100480 return var;
Steve Blocka7e24c12009-10-30 11:49:00 +0000481}
482
483
484void Scope::AddDeclaration(Declaration* declaration) {
485 decls_.Add(declaration);
486}
487
488
489void Scope::SetIllegalRedeclaration(Expression* expression) {
Steve Block1e0659c2011-05-24 12:43:12 +0100490 // Record only the first illegal redeclaration.
Steve Blocka7e24c12009-10-30 11:49:00 +0000491 if (!HasIllegalRedeclaration()) {
492 illegal_redecl_ = expression;
493 }
494 ASSERT(HasIllegalRedeclaration());
495}
496
497
498void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) {
499 ASSERT(HasIllegalRedeclaration());
500 illegal_redecl_->Accept(visitor);
501}
502
503
Ben Murdoch589d6972011-11-30 16:04:58 +0000504Declaration* Scope::CheckConflictingVarDeclarations() {
505 int length = decls_.length();
506 for (int i = 0; i < length; i++) {
507 Declaration* decl = decls_[i];
Ben Murdoch85b71792012-04-11 18:30:58 +0100508 if (decl->mode() != Variable::VAR) continue;
Ben Murdoch589d6972011-11-30 16:04:58 +0000509 Handle<String> name = decl->proxy()->name();
Ben Murdoch85b71792012-04-11 18:30:58 +0100510 bool cond = true;
511 for (Scope* scope = decl->scope(); cond ; scope = scope->outer_scope_) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000512 // There is a conflict if there exists a non-VAR binding.
Ben Murdoch85b71792012-04-11 18:30:58 +0100513 Variable* other_var = scope->variables_.Lookup(name);
514 if (other_var != NULL && other_var->mode() != Variable::VAR) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000515 return decl;
516 }
Ben Murdoch589d6972011-11-30 16:04:58 +0000517
Ben Murdoch85b71792012-04-11 18:30:58 +0100518 // Include declaration scope in the iteration but stop after.
519 if (!scope->is_block_scope() && !scope->is_catch_scope()) cond = false;
Ben Murdochc7cc0282012-03-05 14:35:55 +0000520 }
521 }
Ben Murdochc7cc0282012-03-05 14:35:55 +0000522 return NULL;
523}
524
525
Ben Murdoch85b71792012-04-11 18:30:58 +0100526template<class Allocator>
527void Scope::CollectUsedVariables(List<Variable*, Allocator>* locals) {
528 // Collect variables in this scope.
529 // Note that the function_ variable - if present - is not
530 // collected here but handled separately in ScopeInfo
531 // which is the current user of this function).
Steve Blocka7e24c12009-10-30 11:49:00 +0000532 for (int i = 0; i < temps_.length(); i++) {
533 Variable* var = temps_[i];
Steve Block6ded16b2010-05-10 14:33:55 +0100534 if (var->is_used()) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100535 locals->Add(var);
Steve Blocka7e24c12009-10-30 11:49:00 +0000536 }
537 }
538 for (VariableMap::Entry* p = variables_.Start();
539 p != NULL;
540 p = variables_.Next(p)) {
541 Variable* var = reinterpret_cast<Variable*>(p->value);
Steve Block6ded16b2010-05-10 14:33:55 +0100542 if (var->is_used()) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100543 locals->Add(var);
Steve Blocka7e24c12009-10-30 11:49:00 +0000544 }
545 }
546}
547
548
Ben Murdoch85b71792012-04-11 18:30:58 +0100549// Make sure the method gets instantiated by the template system.
550template void Scope::CollectUsedVariables(
551 List<Variable*, FreeStoreAllocationPolicy>* locals);
552template void Scope::CollectUsedVariables(
553 List<Variable*, PreallocatedStorage>* locals);
554template void Scope::CollectUsedVariables(
555 List<Variable*, ZoneListAllocationPolicy>* locals);
556
557
558void Scope::AllocateVariables(Handle<Context> context) {
559 ASSERT(outer_scope_ == NULL); // eval or global scopes only
560
Steve Blocka7e24c12009-10-30 11:49:00 +0000561 // 1) Propagate scope information.
Ben Murdoch85b71792012-04-11 18:30:58 +0100562 // If we are in an eval scope, we may have other outer scopes about
563 // which we don't know anything at this point. Thus we must be conservative
564 // and assume they may invoke eval themselves. Eventually we could capture
565 // this information in the ScopeInfo and then use it here (by traversing
566 // the call chain stack, at compile time).
567
568 bool eval_scope = is_eval_scope();
569 bool outer_scope_calls_eval = false;
Ben Murdoch257744e2011-11-30 15:57:28 +0000570 bool outer_scope_calls_non_strict_eval = false;
Ben Murdoch85b71792012-04-11 18:30:58 +0100571 if (!is_global_scope()) {
572 context->ComputeEvalScopeInfo(&outer_scope_calls_eval,
573 &outer_scope_calls_non_strict_eval);
Ben Murdoch257744e2011-11-30 15:57:28 +0000574 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100575 PropagateScopeInfo(outer_scope_calls_eval,
576 outer_scope_calls_non_strict_eval,
577 eval_scope);
Steve Blocka7e24c12009-10-30 11:49:00 +0000578
579 // 2) Resolve variables.
Ben Murdoch85b71792012-04-11 18:30:58 +0100580 Scope* global_scope = NULL;
581 if (is_global_scope()) global_scope = this;
582 ResolveVariablesRecursively(global_scope, context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000583
584 // 3) Allocate variables.
585 AllocateVariablesRecursively();
586}
587
588
589bool Scope::AllowsLazyCompilation() const {
590 return !force_eager_compilation_ && HasTrivialOuterContext();
591}
592
593
594bool Scope::HasTrivialContext() const {
595 // A function scope has a trivial context if it always is the global
596 // context. We iteratively scan out the context chain to see if
597 // there is anything that makes this scope non-trivial; otherwise we
598 // return true.
599 for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) {
600 if (scope->is_eval_scope()) return false;
601 if (scope->scope_inside_with_) return false;
602 if (scope->num_heap_slots_ > 0) return false;
603 }
604 return true;
605}
606
607
608bool Scope::HasTrivialOuterContext() const {
609 Scope* outer = outer_scope_;
610 if (outer == NULL) return true;
611 // Note that the outer context may be trivial in general, but the current
612 // scope may be inside a 'with' statement in which case the outer context
613 // for this scope is not trivial.
614 return !scope_inside_with_ && outer->HasTrivialContext();
615}
616
617
618int Scope::ContextChainLength(Scope* scope) {
619 int n = 0;
620 for (Scope* s = this; s != scope; s = s->outer_scope_) {
621 ASSERT(s != NULL); // scope must be in the scope chain
622 if (s->num_heap_slots() > 0) n++;
623 }
624 return n;
625}
626
627
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000628Scope* Scope::DeclarationScope() {
629 Scope* scope = this;
Ben Murdoch85b71792012-04-11 18:30:58 +0100630 while (scope->is_catch_scope() ||
631 scope->is_block_scope()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000632 scope = scope->outer_scope();
633 }
634 return scope;
635}
636
637
Ben Murdoch85b71792012-04-11 18:30:58 +0100638Handle<SerializedScopeInfo> Scope::GetSerializedScopeInfo() {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000639 if (scope_info_.is_null()) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100640 scope_info_ = SerializedScopeInfo::Create(this);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000641 }
642 return scope_info_;
643}
644
645
Steve Blocka7e24c12009-10-30 11:49:00 +0000646#ifdef DEBUG
Ben Murdoch85b71792012-04-11 18:30:58 +0100647static const char* Header(Scope::Type type) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000648 switch (type) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100649 case Scope::EVAL_SCOPE: return "eval";
650 case Scope::FUNCTION_SCOPE: return "function";
651 case Scope::GLOBAL_SCOPE: return "global";
652 case Scope::CATCH_SCOPE: return "catch";
653 case Scope::BLOCK_SCOPE: return "block";
Steve Blocka7e24c12009-10-30 11:49:00 +0000654 }
655 UNREACHABLE();
656 return NULL;
657}
658
659
660static void Indent(int n, const char* str) {
661 PrintF("%*s%s", n, "", str);
662}
663
664
665static void PrintName(Handle<String> name) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000666 SmartArrayPointer<char> s = name->ToCString(DISALLOW_NULLS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000667 PrintF("%s", *s);
668}
669
670
Ben Murdoch589d6972011-11-30 16:04:58 +0000671static void PrintLocation(Variable* var) {
672 switch (var->location()) {
673 case Variable::UNALLOCATED:
674 break;
675 case Variable::PARAMETER:
676 PrintF("parameter[%d]", var->index());
677 break;
678 case Variable::LOCAL:
679 PrintF("local[%d]", var->index());
680 break;
681 case Variable::CONTEXT:
682 PrintF("context[%d]", var->index());
683 break;
684 case Variable::LOOKUP:
685 PrintF("lookup");
686 break;
687 }
688}
689
690
691static void PrintVar(int indent, Variable* var) {
692 if (var->is_used() || !var->IsUnallocated()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000693 Indent(indent, Variable::Mode2String(var->mode()));
694 PrintF(" ");
695 PrintName(var->name());
696 PrintF("; // ");
Ben Murdoch589d6972011-11-30 16:04:58 +0000697 PrintLocation(var);
Ben Murdoch85b71792012-04-11 18:30:58 +0100698 if (var->is_accessed_from_inner_scope()) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000699 if (!var->IsUnallocated()) PrintF(", ");
Ben Murdoch85b71792012-04-11 18:30:58 +0100700 PrintF("inner scope access");
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000701 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000702 PrintF("\n");
703 }
704}
705
706
Ben Murdoch589d6972011-11-30 16:04:58 +0000707static void PrintMap(int indent, VariableMap* map) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000708 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
709 Variable* var = reinterpret_cast<Variable*>(p->value);
Ben Murdoch589d6972011-11-30 16:04:58 +0000710 PrintVar(indent, var);
Steve Blocka7e24c12009-10-30 11:49:00 +0000711 }
712}
713
714
715void Scope::Print(int n) {
716 int n0 = (n > 0 ? n : 0);
717 int n1 = n0 + 2; // indentation
718
719 // Print header.
720 Indent(n0, Header(type_));
721 if (scope_name_->length() > 0) {
722 PrintF(" ");
723 PrintName(scope_name_);
724 }
725
726 // Print parameters, if any.
727 if (is_function_scope()) {
728 PrintF(" (");
729 for (int i = 0; i < params_.length(); i++) {
730 if (i > 0) PrintF(", ");
731 PrintName(params_[i]->name());
732 }
733 PrintF(")");
734 }
735
Ben Murdoch85b71792012-04-11 18:30:58 +0100736 PrintF(" {\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000737
738 // Function name, if any (named function literals, only).
739 if (function_ != NULL) {
740 Indent(n1, "// (local) function name: ");
741 PrintName(function_->name());
742 PrintF("\n");
743 }
744
745 // Scope info.
746 if (HasTrivialOuterContext()) {
747 Indent(n1, "// scope has trivial outer context\n");
748 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100749 if (is_strict_mode()) Indent(n1, "// strict mode scope\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000750 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
751 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n");
752 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n");
Ben Murdoch85b71792012-04-11 18:30:58 +0100753 if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n");
Ben Murdoch257744e2011-11-30 15:57:28 +0000754 if (outer_scope_calls_non_strict_eval_) {
755 Indent(n1, "// outer scope calls 'eval' in non-strict context\n");
756 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000757 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
Ben Murdoch85b71792012-04-11 18:30:58 +0100758 if (outer_scope_is_eval_scope_) {
759 Indent(n1, "// outer scope is 'eval' scope\n");
760 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000761 if (num_stack_slots_ > 0) { Indent(n1, "// ");
762 PrintF("%d stack slots\n", num_stack_slots_); }
763 if (num_heap_slots_ > 0) { Indent(n1, "// ");
764 PrintF("%d heap slots\n", num_heap_slots_); }
765
766 // Print locals.
Steve Blocka7e24c12009-10-30 11:49:00 +0000767 Indent(n1, "// function var\n");
768 if (function_ != NULL) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000769 PrintVar(n1, function_->var());
Steve Blocka7e24c12009-10-30 11:49:00 +0000770 }
771
772 Indent(n1, "// temporary vars\n");
773 for (int i = 0; i < temps_.length(); i++) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000774 PrintVar(n1, temps_[i]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000775 }
776
777 Indent(n1, "// local vars\n");
Ben Murdoch589d6972011-11-30 16:04:58 +0000778 PrintMap(n1, &variables_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000779
780 Indent(n1, "// dynamic vars\n");
781 if (dynamics_ != NULL) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100782 PrintMap(n1, dynamics_->GetMap(Variable::DYNAMIC));
783 PrintMap(n1, dynamics_->GetMap(Variable::DYNAMIC_LOCAL));
784 PrintMap(n1, dynamics_->GetMap(Variable::DYNAMIC_GLOBAL));
Steve Blocka7e24c12009-10-30 11:49:00 +0000785 }
786
787 // Print inner scopes (disable by providing negative n).
788 if (n >= 0) {
789 for (int i = 0; i < inner_scopes_.length(); i++) {
790 PrintF("\n");
791 inner_scopes_[i]->Print(n1);
792 }
793 }
794
795 Indent(n0, "}\n");
796}
797#endif // DEBUG
798
799
Ben Murdoch85b71792012-04-11 18:30:58 +0100800Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000801 if (dynamics_ == NULL) dynamics_ = new DynamicScopePart();
802 VariableMap* map = dynamics_->GetMap(mode);
803 Variable* var = map->Lookup(name);
804 if (var == NULL) {
805 // Declare a new non-local.
Ben Murdoch85b71792012-04-11 18:30:58 +0100806 var = map->Declare(NULL, name, mode, true, Variable::NORMAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000807 // Allocate it by giving it a dynamic lookup.
Ben Murdoch589d6972011-11-30 16:04:58 +0000808 var->AllocateTo(Variable::LOOKUP, -1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000809 }
810 return var;
811}
812
813
Ben Murdoch85b71792012-04-11 18:30:58 +0100814// Lookup a variable starting with this scope. The result is either
815// the statically resolved variable belonging to an outer scope, or
816// NULL. It may be NULL because a) we couldn't find a variable, or b)
817// because the variable is just a guess (and may be shadowed by
818// another variable that is introduced dynamically via an 'eval' call
819// or a 'with' statement).
Steve Blocka7e24c12009-10-30 11:49:00 +0000820Variable* Scope::LookupRecursive(Handle<String> name,
Ben Murdoch85b71792012-04-11 18:30:58 +0100821 bool from_inner_scope,
822 Variable** invalidated_local) {
823 // If we find a variable, but the current scope calls 'eval', the found
824 // variable may not be the correct one (the 'eval' may introduce a
825 // property with the same name). In that case, remember that the variable
826 // found is just a guess.
827 bool guess = scope_calls_eval_;
828
Steve Blocka7e24c12009-10-30 11:49:00 +0000829 // Try to find the variable in this scope.
830 Variable* var = LocalLookup(name);
831
832 if (var != NULL) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100833 // We found a variable. If this is not an inner lookup, we are done.
834 // (Even if there is an 'eval' in this scope which introduces the
835 // same variable again, the resulting variable remains the same.
836 // Note that enclosing 'with' statements are handled at the call site.)
837 if (!from_inner_scope)
838 return var;
Steve Blocka7e24c12009-10-30 11:49:00 +0000839
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000840 } else {
Ben Murdoch85b71792012-04-11 18:30:58 +0100841 // We did not find a variable locally. Check against the function variable,
842 // if any. We can do this for all scopes, since the function variable is
843 // only present - if at all - for function scopes.
844 //
845 // This lookup corresponds to a lookup in the "intermediate" scope sitting
846 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
847 // the name of named function literal is kept in an intermediate scope
848 // in between this scope and the next outer scope.)
849 if (function_ != NULL && function_->name().is_identical_to(name)) {
850 var = function_->var();
851
852 } else if (outer_scope_ != NULL) {
853 var = outer_scope_->LookupRecursive(name, true, invalidated_local);
854 // We may have found a variable in an outer scope. However, if
855 // the current scope is inside a 'with', the actual variable may
856 // be a property introduced via the 'with' statement. Then, the
857 // variable we may have found is just a guess.
858 if (scope_inside_with_)
859 guess = true;
860 }
861
862 // If we did not find a variable, we are done.
863 if (var == NULL)
864 return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000865 }
866
Ben Murdoch85b71792012-04-11 18:30:58 +0100867 ASSERT(var != NULL);
868
869 // If this is a lookup from an inner scope, mark the variable.
870 if (from_inner_scope) {
871 var->MarkAsAccessedFromInnerScope();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100872 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100873
874 // If the variable we have found is just a guess, invalidate the
875 // result. If the found variable is local, record that fact so we
876 // can generate fast code to get it if it is not shadowed by eval.
877 if (guess) {
878 if (!var->is_global()) *invalidated_local = var;
879 var = NULL;
880 }
881
Steve Blocka7e24c12009-10-30 11:49:00 +0000882 return var;
883}
884
885
Ben Murdoch85b71792012-04-11 18:30:58 +0100886void Scope::ResolveVariable(Scope* global_scope,
887 Handle<Context> context,
888 VariableProxy* proxy) {
889 ASSERT(global_scope == NULL || global_scope->is_global_scope());
Steve Blocka7e24c12009-10-30 11:49:00 +0000890
891 // If the proxy is already resolved there's nothing to do
892 // (functions and consts may be resolved by the parser).
Ben Murdoch85b71792012-04-11 18:30:58 +0100893 if (proxy->var() != NULL) return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000894
895 // Otherwise, try to resolve the variable.
Ben Murdoch85b71792012-04-11 18:30:58 +0100896 Variable* invalidated_local = NULL;
897 Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local);
Steve Blocka7e24c12009-10-30 11:49:00 +0000898
Ben Murdoch85b71792012-04-11 18:30:58 +0100899 if (proxy->inside_with()) {
900 // If we are inside a local 'with' statement, all bets are off
901 // and we cannot resolve the proxy to a local variable even if
902 // we found an outer matching variable.
903 // Note that we must do a lookup anyway, because if we find one,
904 // we must mark that variable as potentially accessed from this
905 // inner scope (the property may not be in the 'with' object).
906 var = NonLocal(proxy->name(), Variable::DYNAMIC);
907
908 } else {
909 // We are not inside a local 'with' statement.
910
911 if (var == NULL) {
912 // We did not find the variable. We have a global variable
913 // if we are in the global scope (we know already that we
914 // are outside a 'with' statement) or if there is no way
915 // that the variable might be introduced dynamically (through
916 // a local or outer eval() call, or an outer 'with' statement),
917 // or we don't know about the outer scope (because we are
918 // in an eval scope).
919 if (is_global_scope() ||
920 !(scope_inside_with_ || outer_scope_is_eval_scope_ ||
921 scope_calls_eval_ || outer_scope_calls_eval_)) {
922 // We must have a global variable.
923 ASSERT(global_scope != NULL);
924 var = global_scope->DeclareGlobal(proxy->name());
925
926 } else if (scope_inside_with_) {
927 // If we are inside a with statement we give up and look up
928 // the variable at runtime.
929 var = NonLocal(proxy->name(), Variable::DYNAMIC);
930
931 } else if (invalidated_local != NULL) {
932 // No with statements are involved and we found a local
933 // variable that might be shadowed by eval introduced
934 // variables.
935 var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL);
936 var->set_local_if_not_shadowed(invalidated_local);
937
938 } else if (outer_scope_is_eval_scope_) {
939 // No with statements and we did not find a local and the code
940 // is executed with a call to eval. The context contains
941 // scope information that we can use to determine if the
942 // variable is global if it is not shadowed by eval-introduced
943 // variables.
944 if (context->GlobalIfNotShadowedByEval(proxy->name())) {
945 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
946
947 } else {
948 var = NonLocal(proxy->name(), Variable::DYNAMIC);
949 }
950
Steve Blocka7e24c12009-10-30 11:49:00 +0000951 } else {
Ben Murdoch85b71792012-04-11 18:30:58 +0100952 // No with statements and we did not find a local and the code
953 // is not executed with a call to eval. We know that this
954 // variable is global unless it is shadowed by eval-introduced
955 // variables.
956 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000957 }
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100958 }
959 }
960
Ben Murdoch85b71792012-04-11 18:30:58 +0100961 proxy->BindTo(var);
Steve Blocka7e24c12009-10-30 11:49:00 +0000962}
963
964
Ben Murdoch85b71792012-04-11 18:30:58 +0100965void Scope::ResolveVariablesRecursively(Scope* global_scope,
966 Handle<Context> context) {
967 ASSERT(global_scope == NULL || global_scope->is_global_scope());
Steve Blocka7e24c12009-10-30 11:49:00 +0000968
969 // Resolve unresolved variables for this scope.
970 for (int i = 0; i < unresolved_.length(); i++) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100971 ResolveVariable(global_scope, context, unresolved_[i]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000972 }
973
974 // Resolve unresolved variables for inner scopes.
975 for (int i = 0; i < inner_scopes_.length(); i++) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100976 inner_scopes_[i]->ResolveVariablesRecursively(global_scope, context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000977 }
978}
979
980
Ben Murdoch85b71792012-04-11 18:30:58 +0100981bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval,
982 bool outer_scope_calls_non_strict_eval,
983 bool outer_scope_is_eval_scope) {
984 if (outer_scope_calls_eval) {
985 outer_scope_calls_eval_ = true;
986 }
987
Ben Murdoch257744e2011-11-30 15:57:28 +0000988 if (outer_scope_calls_non_strict_eval) {
989 outer_scope_calls_non_strict_eval_ = true;
990 }
991
Ben Murdoch85b71792012-04-11 18:30:58 +0100992 if (outer_scope_is_eval_scope) {
993 outer_scope_is_eval_scope_ = true;
994 }
995
996 bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_;
997 bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000998 bool calls_non_strict_eval =
Ben Murdoch85b71792012-04-11 18:30:58 +0100999 (scope_calls_eval_ && !is_strict_mode()) ||
1000 outer_scope_calls_non_strict_eval_;
Steve Blocka7e24c12009-10-30 11:49:00 +00001001 for (int i = 0; i < inner_scopes_.length(); i++) {
1002 Scope* inner_scope = inner_scopes_[i];
Ben Murdoch85b71792012-04-11 18:30:58 +01001003 if (inner_scope->PropagateScopeInfo(calls_eval,
1004 calls_non_strict_eval,
1005 is_eval)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001006 inner_scope_calls_eval_ = true;
1007 }
1008 if (inner_scope->force_eager_compilation_) {
1009 force_eager_compilation_ = true;
1010 }
1011 }
1012
1013 return scope_calls_eval_ || inner_scope_calls_eval_;
1014}
1015
1016
1017bool Scope::MustAllocate(Variable* var) {
1018 // Give var a read/write use if there is a chance it might be accessed
1019 // via an eval() call. This is only possible if the variable has a
1020 // visible name.
1021 if ((var->is_this() || var->name()->length() > 0) &&
Ben Murdoch85b71792012-04-11 18:30:58 +01001022 (var->is_accessed_from_inner_scope() ||
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001023 scope_calls_eval_ ||
1024 inner_scope_calls_eval_ ||
1025 scope_contains_with_ ||
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001026 is_catch_scope() ||
1027 is_block_scope())) {
Steve Block6ded16b2010-05-10 14:33:55 +01001028 var->set_is_used(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001029 }
1030 // Global variables do not need to be allocated.
Steve Block6ded16b2010-05-10 14:33:55 +01001031 return !var->is_global() && var->is_used();
Steve Blocka7e24c12009-10-30 11:49:00 +00001032}
1033
1034
1035bool Scope::MustAllocateInContext(Variable* var) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001036 // If var is accessed from an inner scope, or if there is a possibility
1037 // that it might be accessed from the current or an inner scope (through
1038 // an eval() call or a runtime with lookup), it must be allocated in the
Steve Blocka7e24c12009-10-30 11:49:00 +00001039 // context.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001040 //
1041 // Exceptions: temporary variables are never allocated in a context;
1042 // catch-bound variables are always allocated in a context.
Ben Murdoch85b71792012-04-11 18:30:58 +01001043 if (var->mode() == Variable::TEMPORARY) return false;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001044 if (is_catch_scope() || is_block_scope()) return true;
Ben Murdoch85b71792012-04-11 18:30:58 +01001045 return var->is_accessed_from_inner_scope() ||
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001046 scope_calls_eval_ ||
1047 inner_scope_calls_eval_ ||
1048 scope_contains_with_ ||
1049 var->is_global();
Steve Blocka7e24c12009-10-30 11:49:00 +00001050}
1051
1052
1053bool Scope::HasArgumentsParameter() {
1054 for (int i = 0; i < params_.length(); i++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001055 if (params_[i]->name().is_identical_to(
1056 isolate_->factory()->arguments_symbol())) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001057 return true;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001058 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001059 }
1060 return false;
1061}
1062
1063
1064void Scope::AllocateStackSlot(Variable* var) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001065 var->AllocateTo(Variable::LOCAL, num_stack_slots_++);
Steve Blocka7e24c12009-10-30 11:49:00 +00001066}
1067
1068
1069void Scope::AllocateHeapSlot(Variable* var) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001070 var->AllocateTo(Variable::CONTEXT, num_heap_slots_++);
Steve Blocka7e24c12009-10-30 11:49:00 +00001071}
1072
1073
1074void Scope::AllocateParameterLocals() {
1075 ASSERT(is_function_scope());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001076 Variable* arguments = LocalLookup(isolate_->factory()->arguments_symbol());
Steve Blocka7e24c12009-10-30 11:49:00 +00001077 ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly
Steve Block44f0eee2011-05-26 01:26:41 +01001078
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001079 bool uses_nonstrict_arguments = false;
Steve Block44f0eee2011-05-26 01:26:41 +01001080
Steve Blocka7e24c12009-10-30 11:49:00 +00001081 if (MustAllocate(arguments) && !HasArgumentsParameter()) {
1082 // 'arguments' is used. Unless there is also a parameter called
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001083 // 'arguments', we must be conservative and allocate all parameters to
1084 // the context assuming they will be captured by the arguments object.
1085 // If we have a parameter named 'arguments', a (new) value is always
1086 // assigned to it via the function invocation. Then 'arguments' denotes
1087 // that specific parameter value and cannot be used to access the
1088 // parameters, which is why we don't need to allocate an arguments
1089 // object in that case.
Steve Blocka7e24c12009-10-30 11:49:00 +00001090
1091 // We are using 'arguments'. Tell the code generator that is needs to
1092 // allocate the arguments object by setting 'arguments_'.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001093 arguments_ = arguments;
Steve Blocka7e24c12009-10-30 11:49:00 +00001094
Steve Block44f0eee2011-05-26 01:26:41 +01001095 // In strict mode 'arguments' does not alias formal parameters.
1096 // Therefore in strict mode we allocate parameters as if 'arguments'
1097 // were not used.
Ben Murdoch85b71792012-04-11 18:30:58 +01001098 uses_nonstrict_arguments = !is_strict_mode();
Steve Block44f0eee2011-05-26 01:26:41 +01001099 }
1100
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001101 // The same parameter may occur multiple times in the parameters_ list.
1102 // If it does, and if it is not copied into the context object, it must
1103 // receive the highest parameter index for that parameter; thus iteration
1104 // order is relevant!
1105 for (int i = params_.length() - 1; i >= 0; --i) {
1106 Variable* var = params_[i];
1107 ASSERT(var->scope() == this);
1108 if (uses_nonstrict_arguments) {
Ben Murdoch85b71792012-04-11 18:30:58 +01001109 // Give the parameter a use from an inner scope, to force allocation
1110 // to the context.
1111 var->MarkAsAccessedFromInnerScope();
Steve Blocka7e24c12009-10-30 11:49:00 +00001112 }
1113
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001114 if (MustAllocate(var)) {
1115 if (MustAllocateInContext(var)) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001116 ASSERT(var->IsUnallocated() || var->IsContextSlot());
1117 if (var->IsUnallocated()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001118 AllocateHeapSlot(var);
1119 }
1120 } else {
Ben Murdoch589d6972011-11-30 16:04:58 +00001121 ASSERT(var->IsUnallocated() || var->IsParameter());
1122 if (var->IsUnallocated()) {
1123 var->AllocateTo(Variable::PARAMETER, i);
Steve Blocka7e24c12009-10-30 11:49:00 +00001124 }
1125 }
1126 }
1127 }
1128}
1129
1130
1131void Scope::AllocateNonParameterLocal(Variable* var) {
1132 ASSERT(var->scope() == this);
Ben Murdoch589d6972011-11-30 16:04:58 +00001133 ASSERT(!var->IsVariable(isolate_->factory()->result_symbol()) ||
1134 !var->IsStackLocal());
1135 if (var->IsUnallocated() && MustAllocate(var)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001136 if (MustAllocateInContext(var)) {
1137 AllocateHeapSlot(var);
1138 } else {
1139 AllocateStackSlot(var);
1140 }
1141 }
1142}
1143
1144
1145void Scope::AllocateNonParameterLocals() {
1146 // All variables that have no rewrite yet are non-parameter locals.
1147 for (int i = 0; i < temps_.length(); i++) {
1148 AllocateNonParameterLocal(temps_[i]);
1149 }
1150
1151 for (VariableMap::Entry* p = variables_.Start();
1152 p != NULL;
1153 p = variables_.Next(p)) {
1154 Variable* var = reinterpret_cast<Variable*>(p->value);
1155 AllocateNonParameterLocal(var);
1156 }
1157
1158 // For now, function_ must be allocated at the very end. If it gets
1159 // allocated in the context, it must be the last slot in the context,
1160 // because of the current ScopeInfo implementation (see
1161 // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor).
1162 if (function_ != NULL) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001163 AllocateNonParameterLocal(function_->var());
Steve Blocka7e24c12009-10-30 11:49:00 +00001164 }
1165}
1166
1167
1168void Scope::AllocateVariablesRecursively() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001169 // Allocate variables for inner scopes.
1170 for (int i = 0; i < inner_scopes_.length(); i++) {
1171 inner_scopes_[i]->AllocateVariablesRecursively();
1172 }
1173
Ben Murdochb8e0da22011-05-16 14:20:40 +01001174 // If scope is already resolved, we still need to allocate
1175 // variables in inner scopes which might not had been resolved yet.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001176 if (already_resolved()) return;
Ben Murdochb8e0da22011-05-16 14:20:40 +01001177 // The number of slots required for variables.
1178 num_stack_slots_ = 0;
1179 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
1180
Steve Blocka7e24c12009-10-30 11:49:00 +00001181 // Allocate variables for this scope.
1182 // Parameters must be allocated first, if any.
1183 if (is_function_scope()) AllocateParameterLocals();
1184 AllocateNonParameterLocals();
1185
Ben Murdoch85b71792012-04-11 18:30:58 +01001186 // Allocate context if necessary.
1187 bool must_have_local_context = false;
1188 if (scope_calls_eval_ || scope_contains_with_) {
1189 // The context for the eval() call or 'with' statement in this scope.
1190 // Unless we are in the global or an eval scope, we need a local
1191 // context even if we didn't statically allocate any locals in it,
1192 // and the compiler will access the context variable. If we are
1193 // not in an inner scope, the scope is provided from the outside.
1194 must_have_local_context = is_function_scope();
1195 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001196
1197 // If we didn't allocate any locals in the local context, then we only
Ben Murdoch85b71792012-04-11 18:30:58 +01001198 // need the minimal number of slots if we must have a local context.
1199 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS &&
1200 !must_have_local_context) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001201 num_heap_slots_ = 0;
1202 }
1203
1204 // Allocation done.
1205 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
1206}
1207
Steve Blocka7e24c12009-10-30 11:49:00 +00001208} } // namespace v8::internal