blob: 4e87b25e11606c9b71989731f93dfef381bf12fc [file] [log] [blame]
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000030#include "scopes.h"
31
32#include "bootstrapper.h"
33#include "compiler.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000034#include "scopeinfo.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035
whesse@chromium.org030d38e2011-07-13 13:23:34 +000036#include "allocation-inl.h"
37
kasperl@chromium.org71affb52009-05-26 05:44:31 +000038namespace v8 {
39namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
41// ----------------------------------------------------------------------------
42// A Zone allocator for use with LocalsMap.
43
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000044// TODO(isolates): It is probably worth it to change the Allocator class to
45// take a pointer to an isolate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046class ZoneAllocator: public Allocator {
47 public:
48 /* nothing to do */
49 virtual ~ZoneAllocator() {}
50
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000051 virtual void* New(size_t size) { return ZONE->New(static_cast<int>(size)); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052
53 /* ignored - Zone is freed in one fell swoop */
54 virtual void Delete(void* p) {}
55};
56
57
58static ZoneAllocator LocalsMapAllocator;
59
60
61// ----------------------------------------------------------------------------
62// 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
79// Dummy constructor
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000080VariableMap::VariableMap(bool gotta_love_static_overloading) : HashMap() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000081
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000082VariableMap::VariableMap() : HashMap(Match, &LocalsMapAllocator, 8) {}
83VariableMap::~VariableMap() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000084
85
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000086Variable* VariableMap::Declare(Scope* scope,
87 Handle<String> name,
88 Variable::Mode mode,
89 bool is_valid_lhs,
90 Variable::Kind kind) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091 HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), true);
92 if (p->value == NULL) {
93 // The variable has not been declared yet -> insert it.
94 ASSERT(p->key == name.location());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000095 p->value = new Variable(scope, name, mode, is_valid_lhs, kind);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096 }
97 return reinterpret_cast<Variable*>(p->value);
98}
99
100
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000101Variable* VariableMap::Lookup(Handle<String> name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000102 HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), false);
103 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
115
116// Dummy constructor
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000117Scope::Scope(Type type)
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000118 : 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) {
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000126 SetDefaults(type, NULL, Handle<SerializedScopeInfo>::null());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000127}
128
129
130Scope::Scope(Scope* outer_scope, Type type)
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +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) {
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000139 SetDefaults(type, outer_scope, Handle<SerializedScopeInfo>::null());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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.
143 ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL));
144 ASSERT(!HasIllegalRedeclaration());
145}
146
147
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000148Scope::Scope(Scope* inner_scope,
149 Type type,
150 Handle<SerializedScopeInfo> scope_info)
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +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) {
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000159 ASSERT(!scope_info.is_null());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000160 SetDefaults(type, NULL, scope_info);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000161 if (scope_info->HasHeapAllocatedLocals()) {
162 num_heap_slots_ = scope_info_->NumberOfContextSlots();
163 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000164 AddInnerScope(inner_scope);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000165}
166
167
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000168Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name)
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000169 : isolate_(Isolate::Current()),
170 inner_scopes_(1),
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000171 variables_(),
172 temps_(0),
173 params_(0),
174 unresolved_(0),
175 decls_(0),
176 already_resolved_(true) {
177 SetDefaults(CATCH_SCOPE, NULL, Handle<SerializedScopeInfo>::null());
178 AddInnerScope(inner_scope);
179 ++num_var_or_const_;
180 Variable* variable = variables_.Declare(this,
181 catch_variable_name,
182 Variable::VAR,
183 true, // Valid left-hand side.
184 Variable::NORMAL);
185 AllocateHeapSlot(variable);
186}
187
188
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000189void Scope::SetDefaults(Type type,
190 Scope* outer_scope,
191 Handle<SerializedScopeInfo> scope_info) {
192 outer_scope_ = outer_scope;
193 type_ = type;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000194 scope_name_ = isolate_->factory()->empty_symbol();
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000195 dynamics_ = NULL;
196 receiver_ = NULL;
197 function_ = NULL;
198 arguments_ = NULL;
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000199 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.
204 strict_mode_ = (outer_scope != NULL) && outer_scope->strict_mode_;
205 outer_scope_calls_eval_ = false;
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000206 outer_scope_calls_non_strict_eval_ = false;
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000207 inner_scope_calls_eval_ = false;
208 outer_scope_is_eval_scope_ = false;
209 force_eager_compilation_ = false;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000210 num_var_or_const_ = 0;
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000211 num_stack_slots_ = 0;
212 num_heap_slots_ = 0;
213 scope_info_ = scope_info;
214}
215
216
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000217Scope* Scope::DeserializeScopeChain(CompilationInfo* info,
218 Scope* global_scope) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000219 // Reconstruct the outer scope chain from a closure's context chain.
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000220 ASSERT(!info->closure().is_null());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000221 Context* context = info->closure()->context();
222 Scope* current_scope = NULL;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000223 Scope* innermost_scope = NULL;
ricow@chromium.org4f693d62011-07-04 14:01:31 +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;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000231 }
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000232 } else {
233 if (context->IsFunctionContext()) {
234 SerializedScopeInfo* scope_info =
235 context->closure()->shared()->scope_info();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000236 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));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000243 } 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;
250 }
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();
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000257 }
258
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000259 global_scope->AddInnerScope(current_scope);
260 return (innermost_scope == NULL) ? global_scope : innermost_scope;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000261}
262
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000263
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000264bool Scope::Analyze(CompilationInfo* info) {
265 ASSERT(info->function() != NULL);
266 Scope* top = info->function()->scope();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000267
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000268 while (top->outer_scope() != NULL) top = top->outer_scope();
269 top->AllocateVariables(info->calling_context());
270
271#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000272 if (info->isolate()->bootstrapper()->IsActive()
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000273 ? FLAG_print_builtin_scopes
274 : FLAG_print_scopes) {
275 info->function()->scope()->Print();
276 }
277#endif
278
279 info->SetScope(info->function()->scope());
280 return true; // Can not fail.
281}
282
283
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284void Scope::Initialize(bool inside_with) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000285 ASSERT(!already_resolved());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000286
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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);
290 scope_inside_with_ = outer_scope_->scope_inside_with_ || inside_with;
291 } else {
292 scope_inside_with_ = inside_with;
293 }
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
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000303 if (is_catch_scope() || is_block_scope()) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000304 ASSERT(outer_scope() != NULL);
305 receiver_ = outer_scope()->receiver();
306 } else {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000307 ASSERT(is_function_scope() ||
308 is_global_scope() ||
309 is_eval_scope());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000310 Variable* var =
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000311 variables_.Declare(this,
312 isolate_->factory()->this_symbol(),
313 Variable::VAR,
314 false,
315 Variable::THIS);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000316 var->AllocateTo(Variable::PARAMETER, -1);
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000317 receiver_ = var;
318 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319
320 if (is_function_scope()) {
321 // Declare 'arguments' variable which exists in all functions.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000322 // Note that it might never be accessed, in which case it won't be
323 // allocated during variable allocation.
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000324 variables_.Declare(this,
325 isolate_->factory()->arguments_symbol(),
326 Variable::VAR,
327 true,
328 Variable::ARGUMENTS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329 }
330}
331
332
fschneider@chromium.org1805e212011-09-05 10:49:12 +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
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000362Variable* Scope::LocalLookup(Handle<String> name) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000363 Variable* result = variables_.Lookup(name);
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000364 if (result != NULL || scope_info_.is_null()) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000365 return result;
366 }
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000367 // If we have a serialized scope info, we might find the variable there.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000368 //
369 // We should never lookup 'arguments' in this scope as it is implicitly
370 // present in every scope.
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000371 ASSERT(*name != *isolate_->factory()->arguments_symbol());
whesse@chromium.org7b260152011-06-20 15:33:18 +0000372 // There should be no local slot with the given name.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000373 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
374
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000375 // Check context slot lookup.
376 Variable::Mode mode;
377 int index = scope_info_->ContextSlotIndex(*name, &mode);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000378 if (index < 0) {
379 // Check parameters.
380 mode = Variable::VAR;
381 index = scope_info_->ParameterIndex(*name);
382 if (index < 0) {
383 // Check the function name.
384 index = scope_info_->FunctionContextSlotIndex(*name);
385 if (index < 0) return NULL;
386 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000387 }
388
whesse@chromium.org7b260152011-06-20 15:33:18 +0000389 Variable* var =
390 variables_.Declare(this, name, mode, true, Variable::NORMAL);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000391 var->AllocateTo(Variable::CONTEXT, index);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000392 return var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393}
394
395
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000396Variable* Scope::Lookup(Handle<String> name) {
397 for (Scope* scope = this;
398 scope != NULL;
399 scope = scope->outer_scope()) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000400 Variable* var = scope->LocalLookup(name);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000401 if (var != NULL) return var;
402 }
403 return NULL;
404}
405
406
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000407Variable* Scope::DeclareFunctionVar(Handle<String> name) {
408 ASSERT(is_function_scope() && function_ == NULL);
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000409 Variable* function_var =
410 new Variable(this, name, Variable::CONST, true, Variable::NORMAL);
411 function_ = new(isolate_->zone()) VariableProxy(isolate_, function_var);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000412 return function_var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413}
414
415
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000416void Scope::DeclareParameter(Handle<String> name, Variable::Mode mode) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000417 ASSERT(!already_resolved());
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000418 ASSERT(is_function_scope());
419 Variable* var =
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000420 variables_.Declare(this, name, mode, true, Variable::NORMAL);
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000421 params_.Add(var);
422}
423
424
425Variable* Scope::DeclareLocal(Handle<String> name, Variable::Mode mode) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000426 ASSERT(!already_resolved());
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000427 // 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().
danno@chromium.orgb6451162011-08-17 14:33:23 +0000430 ASSERT(mode == Variable::VAR ||
431 mode == Variable::CONST ||
432 mode == Variable::LET);
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000433 ++num_var_or_const_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000434 return variables_.Declare(this, name, mode, true, Variable::NORMAL);
435}
436
437
438Variable* Scope::DeclareGlobal(Handle<String> name) {
439 ASSERT(is_global_scope());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000440 return variables_.Declare(this, name, Variable::DYNAMIC_GLOBAL,
441 true,
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000442 Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000443}
444
445
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000446VariableProxy* Scope::NewUnresolved(Handle<String> name,
447 bool inside_with,
448 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000449 // Note that we must not share the unresolved variables with
450 // the same name because they may be removed selectively via
451 // RemoveUnresolved().
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000452 ASSERT(!already_resolved());
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000453 VariableProxy* proxy = new(isolate_->zone()) VariableProxy(
454 isolate_, name, false, inside_with, position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000455 unresolved_.Add(proxy);
456 return proxy;
457}
458
459
460void 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
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000472Variable* Scope::NewTemporary(Handle<String> name) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000473 ASSERT(!already_resolved());
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000474 Variable* var = new Variable(this,
475 name,
476 Variable::TEMPORARY,
477 true,
478 Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479 temps_.Add(var);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000480 return var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481}
482
483
484void Scope::AddDeclaration(Declaration* declaration) {
485 decls_.Add(declaration);
486}
487
488
489void Scope::SetIllegalRedeclaration(Expression* expression) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000490 // Record only the first illegal redeclaration.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491 if (!HasIllegalRedeclaration()) {
492 illegal_redecl_ = expression;
493 }
494 ASSERT(HasIllegalRedeclaration());
495}
496
497
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000498void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000499 ASSERT(HasIllegalRedeclaration());
500 illegal_redecl_->Accept(visitor);
501}
502
503
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000504Declaration* Scope::CheckConflictingVarDeclarations() {
505 int length = decls_.length();
506 for (int i = 0; i < length; i++) {
507 Declaration* decl = decls_[i];
508 if (decl->mode() != Variable::VAR) continue;
509 Handle<String> name = decl->proxy()->name();
510 bool cond = true;
511 for (Scope* scope = decl->scope(); cond ; scope = scope->outer_scope_) {
512 // There is a conflict if there exists a non-VAR binding.
513 Variable* other_var = scope->variables_.Lookup(name);
514 if (other_var != NULL && other_var->mode() != Variable::VAR) {
515 return decl;
516 }
517
518 // Include declaration scope in the iteration but stop after.
519 if (!scope->is_block_scope() && !scope->is_catch_scope()) cond = false;
520 }
521 }
522 return NULL;
523}
524
525
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526template<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).
532 for (int i = 0; i < temps_.length(); i++) {
533 Variable* var = temps_[i];
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000534 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000535 locals->Add(var);
536 }
537 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000538 for (VariableMap::Entry* p = variables_.Start();
539 p != NULL;
540 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000541 Variable* var = reinterpret_cast<Variable*>(p->value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000542 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543 locals->Add(var);
544 }
545 }
546}
547
548
549// 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);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000554template void Scope::CollectUsedVariables(
555 List<Variable*, ZoneListAllocationPolicy>* locals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556
557
ager@chromium.org381abbb2009-02-25 13:23:22 +0000558void Scope::AllocateVariables(Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559 ASSERT(outer_scope_ == NULL); // eval or global scopes only
560
561 // 1) Propagate scope information.
562 // 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).
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000567
ager@chromium.org381abbb2009-02-25 13:23:22 +0000568 bool eval_scope = is_eval_scope();
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000569 bool outer_scope_calls_eval = false;
570 bool outer_scope_calls_non_strict_eval = false;
571 if (!is_global_scope()) {
572 context->ComputeEvalScopeInfo(&outer_scope_calls_eval,
573 &outer_scope_calls_non_strict_eval);
574 }
575 PropagateScopeInfo(outer_scope_calls_eval,
576 outer_scope_calls_non_strict_eval,
577 eval_scope);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578
579 // 2) Resolve variables.
580 Scope* global_scope = NULL;
581 if (is_global_scope()) global_scope = this;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000582 ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583
584 // 3) Allocate variables.
585 AllocateVariablesRecursively();
586}
587
588
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000589bool 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
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000628Scope* Scope::DeclarationScope() {
629 Scope* scope = this;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000630 while (scope->is_catch_scope() ||
631 scope->is_block_scope()) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000632 scope = scope->outer_scope();
633 }
634 return scope;
635}
636
637
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000638Handle<SerializedScopeInfo> Scope::GetSerializedScopeInfo() {
639 if (scope_info_.is_null()) {
640 scope_info_ = SerializedScopeInfo::Create(this);
641 }
642 return scope_info_;
643}
644
645
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000646#ifdef DEBUG
647static const char* Header(Scope::Type type) {
648 switch (type) {
649 case Scope::EVAL_SCOPE: return "eval";
650 case Scope::FUNCTION_SCOPE: return "function";
651 case Scope::GLOBAL_SCOPE: return "global";
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000652 case Scope::CATCH_SCOPE: return "catch";
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000653 case Scope::BLOCK_SCOPE: return "block";
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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) {
666 SmartPointer<char> s = name->ToCString(DISALLOW_NULLS);
667 PrintF("%s", *s);
668}
669
670
jkummerow@chromium.org486075a2011-09-07 12:44:28 +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()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693 Indent(indent, Variable::Mode2String(var->mode()));
694 PrintF(" ");
695 PrintName(var->name());
696 PrintF("; // ");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000697 PrintLocation(var);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000698 if (var->is_accessed_from_inner_function_scope()) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000699 if (!var->IsUnallocated()) PrintF(", ");
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000700 PrintF("inner scope access");
701 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702 PrintF("\n");
703 }
704}
705
706
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000707static void PrintMap(int indent, VariableMap* map) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000708 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000709 Variable* var = reinterpret_cast<Variable*>(p->value);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000710 PrintVar(indent, var);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000711 }
712}
713
714
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000715void 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
736 PrintF(" {\n");
737
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 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000749 if (is_strict_mode()) Indent(n1, "// strict mode scope\n");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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");
753 if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n");
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000754 if (outer_scope_calls_non_strict_eval_) {
755 Indent(n1, "// outer scope calls 'eval' in non-strict context\n");
756 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000757 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
ager@chromium.org381abbb2009-02-25 13:23:22 +0000758 if (outer_scope_is_eval_scope_) {
759 Indent(n1, "// outer scope is 'eval' scope\n");
760 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767 Indent(n1, "// function var\n");
768 if (function_ != NULL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000769 PrintVar(n1, function_->var());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000770 }
771
772 Indent(n1, "// temporary vars\n");
773 for (int i = 0; i < temps_.length(); i++) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000774 PrintVar(n1, temps_[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000775 }
776
777 Indent(n1, "// local vars\n");
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000778 PrintMap(n1, &variables_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000780 Indent(n1, "// dynamic vars\n");
781 if (dynamics_ != NULL) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000782 PrintMap(n1, dynamics_->GetMap(Variable::DYNAMIC));
783 PrintMap(n1, dynamics_->GetMap(Variable::DYNAMIC_LOCAL));
784 PrintMap(n1, dynamics_->GetMap(Variable::DYNAMIC_GLOBAL));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000785 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000786
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
ager@chromium.org381abbb2009-02-25 13:23:22 +0000800Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000801 if (dynamics_ == NULL) dynamics_ = new DynamicScopePart();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000802 VariableMap* map = dynamics_->GetMap(mode);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000803 Variable* var = map->Lookup(name);
804 if (var == NULL) {
805 // Declare a new non-local.
ager@chromium.org3e875802009-06-29 08:26:34 +0000806 var = map->Declare(NULL, name, mode, true, Variable::NORMAL);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000807 // Allocate it by giving it a dynamic lookup.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +0000808 var->AllocateTo(Variable::LOOKUP, -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000809 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000810 return var;
811}
812
813
814// Lookup a variable starting with this scope. The result is either
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000815// 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).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000820Variable* Scope::LookupRecursive(Handle<String> name,
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000821 bool from_inner_function,
ager@chromium.org381abbb2009-02-25 13:23:22 +0000822 Variable** invalidated_local) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000823 // 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
829 // Try to find the variable in this scope.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000830 Variable* var = LocalLookup(name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000831
832 if (var != NULL) {
833 // 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.)
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000837 if (!from_inner_function)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000838 return var;
839
840 } else {
841 // 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
ager@chromium.org32912102009-01-16 10:38:43 +0000848 // in between this scope and the next outer scope.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000849 if (function_ != NULL && function_->name().is_identical_to(name)) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000850 var = function_->var();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000851
852 } else if (outer_scope_ != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000853 var = outer_scope_->LookupRecursive(
854 name,
855 is_function_scope() || from_inner_function,
856 invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857 // We may have found a variable in an outer scope. However, if
858 // the current scope is inside a 'with', the actual variable may
859 // be a property introduced via the 'with' statement. Then, the
860 // variable we may have found is just a guess.
861 if (scope_inside_with_)
862 guess = true;
863 }
864
865 // If we did not find a variable, we are done.
866 if (var == NULL)
867 return NULL;
868 }
869
870 ASSERT(var != NULL);
871
872 // If this is a lookup from an inner scope, mark the variable.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000873 if (from_inner_function) {
874 var->MarkAsAccessedFromInnerFunctionScope();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000875 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000877 // If the variable we have found is just a guess, invalidate the
878 // result. If the found variable is local, record that fact so we
879 // can generate fast code to get it if it is not shadowed by eval.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000880 if (guess) {
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000881 if (!var->is_global()) *invalidated_local = var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882 var = NULL;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000883 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884
885 return var;
886}
887
888
ager@chromium.org381abbb2009-02-25 13:23:22 +0000889void Scope::ResolveVariable(Scope* global_scope,
890 Handle<Context> context,
891 VariableProxy* proxy) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892 ASSERT(global_scope == NULL || global_scope->is_global_scope());
893
894 // If the proxy is already resolved there's nothing to do
895 // (functions and consts may be resolved by the parser).
896 if (proxy->var() != NULL) return;
897
898 // Otherwise, try to resolve the variable.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000899 Variable* invalidated_local = NULL;
900 Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901
902 if (proxy->inside_with()) {
903 // If we are inside a local 'with' statement, all bets are off
904 // and we cannot resolve the proxy to a local variable even if
905 // we found an outer matching variable.
906 // Note that we must do a lookup anyway, because if we find one,
907 // we must mark that variable as potentially accessed from this
908 // inner scope (the property may not be in the 'with' object).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000909 var = NonLocal(proxy->name(), Variable::DYNAMIC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000910
911 } else {
912 // We are not inside a local 'with' statement.
913
914 if (var == NULL) {
915 // We did not find the variable. We have a global variable
916 // if we are in the global scope (we know already that we
917 // are outside a 'with' statement) or if there is no way
918 // that the variable might be introduced dynamically (through
919 // a local or outer eval() call, or an outer 'with' statement),
920 // or we don't know about the outer scope (because we are
921 // in an eval scope).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000922 if (is_global_scope() ||
923 !(scope_inside_with_ || outer_scope_is_eval_scope_ ||
924 scope_calls_eval_ || outer_scope_calls_eval_)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000925 // We must have a global variable.
926 ASSERT(global_scope != NULL);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000927 var = global_scope->DeclareGlobal(proxy->name());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000928
929 } else if (scope_inside_with_) {
930 // If we are inside a with statement we give up and look up
931 // the variable at runtime.
932 var = NonLocal(proxy->name(), Variable::DYNAMIC);
933
934 } else if (invalidated_local != NULL) {
935 // No with statements are involved and we found a local
936 // variable that might be shadowed by eval introduced
937 // variables.
938 var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL);
939 var->set_local_if_not_shadowed(invalidated_local);
940
941 } else if (outer_scope_is_eval_scope_) {
942 // No with statements and we did not find a local and the code
943 // is executed with a call to eval. The context contains
944 // scope information that we can use to determine if the
945 // variable is global if it is not shadowed by eval-introduced
946 // variables.
947 if (context->GlobalIfNotShadowedByEval(proxy->name())) {
948 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
949
950 } else {
951 var = NonLocal(proxy->name(), Variable::DYNAMIC);
952 }
953
954 } else {
955 // No with statements and we did not find a local and the code
956 // is not executed with a call to eval. We know that this
957 // variable is global unless it is shadowed by eval-introduced
958 // variables.
959 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000960 }
961 }
962 }
963
964 proxy->BindTo(var);
965}
966
967
ager@chromium.org381abbb2009-02-25 13:23:22 +0000968void Scope::ResolveVariablesRecursively(Scope* global_scope,
969 Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000970 ASSERT(global_scope == NULL || global_scope->is_global_scope());
971
972 // Resolve unresolved variables for this scope.
973 for (int i = 0; i < unresolved_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000974 ResolveVariable(global_scope, context, unresolved_[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975 }
976
977 // Resolve unresolved variables for inner scopes.
978 for (int i = 0; i < inner_scopes_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000979 inner_scopes_[i]->ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980 }
981}
982
983
ager@chromium.org381abbb2009-02-25 13:23:22 +0000984bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval,
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000985 bool outer_scope_calls_non_strict_eval,
ager@chromium.org381abbb2009-02-25 13:23:22 +0000986 bool outer_scope_is_eval_scope) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987 if (outer_scope_calls_eval) {
988 outer_scope_calls_eval_ = true;
989 }
990
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000991 if (outer_scope_calls_non_strict_eval) {
992 outer_scope_calls_non_strict_eval_ = true;
993 }
994
ager@chromium.org381abbb2009-02-25 13:23:22 +0000995 if (outer_scope_is_eval_scope) {
996 outer_scope_is_eval_scope_ = true;
997 }
998
999 bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_;
1000 bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001001 bool calls_non_strict_eval =
1002 (scope_calls_eval_ && !is_strict_mode()) ||
1003 outer_scope_calls_non_strict_eval_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001004 for (int i = 0; i < inner_scopes_.length(); i++) {
1005 Scope* inner_scope = inner_scopes_[i];
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001006 if (inner_scope->PropagateScopeInfo(calls_eval,
1007 calls_non_strict_eval,
1008 is_eval)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001009 inner_scope_calls_eval_ = true;
1010 }
1011 if (inner_scope->force_eager_compilation_) {
1012 force_eager_compilation_ = true;
1013 }
1014 }
1015
1016 return scope_calls_eval_ || inner_scope_calls_eval_;
1017}
1018
1019
1020bool Scope::MustAllocate(Variable* var) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001021 // Give var a read/write use if there is a chance it might be accessed
1022 // via an eval() call. This is only possible if the variable has a
1023 // visible name.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024 if ((var->is_this() || var->name()->length() > 0) &&
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001025 (var->is_accessed_from_inner_function_scope() ||
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001026 scope_calls_eval_ ||
1027 inner_scope_calls_eval_ ||
1028 scope_contains_with_ ||
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001029 is_catch_scope() ||
1030 is_block_scope())) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001031 var->set_is_used(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001032 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001033 // Global variables do not need to be allocated.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001034 return !var->is_global() && var->is_used();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001035}
1036
1037
1038bool Scope::MustAllocateInContext(Variable* var) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001039 // If var is accessed from an inner scope, or if there is a possibility
1040 // that it might be accessed from the current or an inner scope (through
1041 // an eval() call or a runtime with lookup), it must be allocated in the
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001042 // context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001043 //
1044 // Exceptions: temporary variables are never allocated in a context;
1045 // catch-bound variables are always allocated in a context.
1046 if (var->mode() == Variable::TEMPORARY) return false;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001047 if (is_catch_scope() || is_block_scope()) return true;
1048 return var->is_accessed_from_inner_function_scope() ||
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001049 scope_calls_eval_ ||
1050 inner_scope_calls_eval_ ||
1051 scope_contains_with_ ||
1052 var->is_global();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053}
1054
1055
1056bool Scope::HasArgumentsParameter() {
1057 for (int i = 0; i < params_.length(); i++) {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001058 if (params_[i]->name().is_identical_to(
1059 isolate_->factory()->arguments_symbol())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001060 return true;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001061 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001062 }
1063 return false;
1064}
1065
1066
1067void Scope::AllocateStackSlot(Variable* var) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001068 var->AllocateTo(Variable::LOCAL, num_stack_slots_++);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001069}
1070
1071
1072void Scope::AllocateHeapSlot(Variable* var) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001073 var->AllocateTo(Variable::CONTEXT, num_heap_slots_++);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001074}
1075
1076
1077void Scope::AllocateParameterLocals() {
1078 ASSERT(is_function_scope());
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001079 Variable* arguments = LocalLookup(isolate_->factory()->arguments_symbol());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001080 ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001081
whesse@chromium.org7b260152011-06-20 15:33:18 +00001082 bool uses_nonstrict_arguments = false;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001083
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001084 if (MustAllocate(arguments) && !HasArgumentsParameter()) {
1085 // 'arguments' is used. Unless there is also a parameter called
whesse@chromium.org7b260152011-06-20 15:33:18 +00001086 // 'arguments', we must be conservative and allocate all parameters to
1087 // the context assuming they will be captured by the arguments object.
1088 // If we have a parameter named 'arguments', a (new) value is always
1089 // assigned to it via the function invocation. Then 'arguments' denotes
1090 // that specific parameter value and cannot be used to access the
1091 // parameters, which is why we don't need to allocate an arguments
1092 // object in that case.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093
1094 // We are using 'arguments'. Tell the code generator that is needs to
1095 // allocate the arguments object by setting 'arguments_'.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001096 arguments_ = arguments;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001098 // In strict mode 'arguments' does not alias formal parameters.
1099 // Therefore in strict mode we allocate parameters as if 'arguments'
1100 // were not used.
whesse@chromium.org7b260152011-06-20 15:33:18 +00001101 uses_nonstrict_arguments = !is_strict_mode();
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001102 }
1103
whesse@chromium.org7b260152011-06-20 15:33:18 +00001104 // The same parameter may occur multiple times in the parameters_ list.
1105 // If it does, and if it is not copied into the context object, it must
1106 // receive the highest parameter index for that parameter; thus iteration
1107 // order is relevant!
1108 for (int i = params_.length() - 1; i >= 0; --i) {
1109 Variable* var = params_[i];
1110 ASSERT(var->scope() == this);
1111 if (uses_nonstrict_arguments) {
1112 // Give the parameter a use from an inner scope, to force allocation
1113 // to the context.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001114 var->MarkAsAccessedFromInnerFunctionScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001115 }
1116
whesse@chromium.org7b260152011-06-20 15:33:18 +00001117 if (MustAllocate(var)) {
1118 if (MustAllocateInContext(var)) {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001119 ASSERT(var->IsUnallocated() || var->IsContextSlot());
1120 if (var->IsUnallocated()) {
whesse@chromium.org7b260152011-06-20 15:33:18 +00001121 AllocateHeapSlot(var);
1122 }
1123 } else {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001124 ASSERT(var->IsUnallocated() || var->IsParameter());
1125 if (var->IsUnallocated()) {
1126 var->AllocateTo(Variable::PARAMETER, i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001127 }
1128 }
1129 }
1130 }
1131}
1132
1133
1134void Scope::AllocateNonParameterLocal(Variable* var) {
1135 ASSERT(var->scope() == this);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00001136 ASSERT(!var->IsVariable(isolate_->factory()->result_symbol()) ||
1137 !var->IsStackLocal());
1138 if (var->IsUnallocated() && MustAllocate(var)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001139 if (MustAllocateInContext(var)) {
1140 AllocateHeapSlot(var);
1141 } else {
1142 AllocateStackSlot(var);
1143 }
1144 }
1145}
1146
1147
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148void Scope::AllocateNonParameterLocals() {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001149 // All variables that have no rewrite yet are non-parameter locals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001150 for (int i = 0; i < temps_.length(); i++) {
1151 AllocateNonParameterLocal(temps_[i]);
1152 }
1153
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001154 for (VariableMap::Entry* p = variables_.Start();
1155 p != NULL;
1156 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001157 Variable* var = reinterpret_cast<Variable*>(p->value);
1158 AllocateNonParameterLocal(var);
1159 }
1160
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001161 // For now, function_ must be allocated at the very end. If it gets
1162 // allocated in the context, it must be the last slot in the context,
1163 // because of the current ScopeInfo implementation (see
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001164 // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor).
1165 if (function_ != NULL) {
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001166 AllocateNonParameterLocal(function_->var());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167 }
1168}
1169
1170
1171void Scope::AllocateVariablesRecursively() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001172 // Allocate variables for inner scopes.
1173 for (int i = 0; i < inner_scopes_.length(); i++) {
1174 inner_scopes_[i]->AllocateVariablesRecursively();
1175 }
1176
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001177 // If scope is already resolved, we still need to allocate
1178 // variables in inner scopes which might not had been resolved yet.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001179 if (already_resolved()) return;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001180 // The number of slots required for variables.
1181 num_stack_slots_ = 0;
1182 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
1183
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001184 // Allocate variables for this scope.
1185 // Parameters must be allocated first, if any.
1186 if (is_function_scope()) AllocateParameterLocals();
1187 AllocateNonParameterLocals();
1188
1189 // Allocate context if necessary.
1190 bool must_have_local_context = false;
1191 if (scope_calls_eval_ || scope_contains_with_) {
1192 // The context for the eval() call or 'with' statement in this scope.
1193 // Unless we are in the global or an eval scope, we need a local
1194 // context even if we didn't statically allocate any locals in it,
1195 // and the compiler will access the context variable. If we are
1196 // not in an inner scope, the scope is provided from the outside.
1197 must_have_local_context = is_function_scope();
1198 }
1199
1200 // If we didn't allocate any locals in the local context, then we only
1201 // need the minimal number of slots if we must have a local context.
1202 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS &&
1203 !must_have_local_context) {
1204 num_heap_slots_ = 0;
1205 }
1206
1207 // Allocation done.
1208 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
1209}
1210
1211} } // namespace v8::internal