blob: 390a0b6e11ba8f1ccd4081fda789f582c8bc1554 [file] [log] [blame]
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000030#include "scopes.h"
31
32#include "bootstrapper.h"
33#include "compiler.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000034#include "prettyprinter.h"
35#include "scopeinfo.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
whesse@chromium.org030d38e2011-07-13 13:23:34 +000037#include "allocation-inl.h"
38
kasperl@chromium.org71affb52009-05-26 05:44:31 +000039namespace v8 {
40namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041
42// ----------------------------------------------------------------------------
43// A Zone allocator for use with LocalsMap.
44
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000045// TODO(isolates): It is probably worth it to change the Allocator class to
46// take a pointer to an isolate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047class ZoneAllocator: public Allocator {
48 public:
49 /* nothing to do */
50 virtual ~ZoneAllocator() {}
51
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000052 virtual void* New(size_t size) { return ZONE->New(static_cast<int>(size)); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053
54 /* ignored - Zone is freed in one fell swoop */
55 virtual void Delete(void* p) {}
56};
57
58
59static ZoneAllocator LocalsMapAllocator;
60
61
62// ----------------------------------------------------------------------------
63// Implementation of LocalsMap
64//
65// Note: We are storing the handle locations as key values in the hash map.
66// When inserting a new variable via Declare(), we rely on the fact that
67// the handle location remains alive for the duration of that variable
68// use. Because a Variable holding a handle with the same location exists
69// this is ensured.
70
71static bool Match(void* key1, void* key2) {
72 String* name1 = *reinterpret_cast<String**>(key1);
73 String* name2 = *reinterpret_cast<String**>(key2);
74 ASSERT(name1->IsSymbol());
75 ASSERT(name2->IsSymbol());
76 return name1 == name2;
77}
78
79
80// Dummy constructor
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000081VariableMap::VariableMap(bool gotta_love_static_overloading) : HashMap() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000082
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000083VariableMap::VariableMap() : HashMap(Match, &LocalsMapAllocator, 8) {}
84VariableMap::~VariableMap() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000085
86
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000087Variable* VariableMap::Declare(Scope* scope,
88 Handle<String> name,
89 Variable::Mode mode,
90 bool is_valid_lhs,
91 Variable::Kind kind) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092 HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), true);
93 if (p->value == NULL) {
94 // The variable has not been declared yet -> insert it.
95 ASSERT(p->key == name.location());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000096 p->value = new Variable(scope, name, mode, is_valid_lhs, kind);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097 }
98 return reinterpret_cast<Variable*>(p->value);
99}
100
101
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000102Variable* VariableMap::Lookup(Handle<String> name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103 HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), false);
104 if (p != NULL) {
105 ASSERT(*reinterpret_cast<String**>(p->key) == *name);
106 ASSERT(p->value != NULL);
107 return reinterpret_cast<Variable*>(p->value);
108 }
109 return NULL;
110}
111
112
113// ----------------------------------------------------------------------------
114// Implementation of Scope
115
116
117// Dummy constructor
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000118Scope::Scope(Type type)
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000119 : isolate_(Isolate::Current()),
120 inner_scopes_(0),
121 variables_(false),
122 temps_(0),
123 params_(0),
124 unresolved_(0),
125 decls_(0),
126 already_resolved_(false) {
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000127 SetDefaults(type, NULL, Handle<SerializedScopeInfo>::null());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000128}
129
130
131Scope::Scope(Scope* outer_scope, Type type)
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000132 : isolate_(Isolate::Current()),
133 inner_scopes_(4),
134 variables_(),
135 temps_(4),
136 params_(4),
137 unresolved_(16),
138 decls_(4),
139 already_resolved_(false) {
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000140 SetDefaults(type, outer_scope, Handle<SerializedScopeInfo>::null());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141 // At some point we might want to provide outer scopes to
142 // eval scopes (by walking the stack and reading the scope info).
143 // In that case, the ASSERT below needs to be adjusted.
144 ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL));
145 ASSERT(!HasIllegalRedeclaration());
146}
147
148
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000149Scope::Scope(Scope* inner_scope, Handle<SerializedScopeInfo> scope_info)
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000150 : isolate_(Isolate::Current()),
151 inner_scopes_(4),
152 variables_(),
153 temps_(4),
154 params_(4),
155 unresolved_(16),
156 decls_(4),
157 already_resolved_(true) {
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000158 ASSERT(!scope_info.is_null());
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000159 SetDefaults(FUNCTION_SCOPE, NULL, scope_info);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000160 if (scope_info->HasHeapAllocatedLocals()) {
161 num_heap_slots_ = scope_info_->NumberOfContextSlots();
162 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000163 AddInnerScope(inner_scope);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000164}
165
166
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000167Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name)
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000168 : isolate_(Isolate::Current()),
169 inner_scopes_(1),
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000170 variables_(),
171 temps_(0),
172 params_(0),
173 unresolved_(0),
174 decls_(0),
175 already_resolved_(true) {
176 SetDefaults(CATCH_SCOPE, NULL, Handle<SerializedScopeInfo>::null());
177 AddInnerScope(inner_scope);
178 ++num_var_or_const_;
179 Variable* variable = variables_.Declare(this,
180 catch_variable_name,
181 Variable::VAR,
182 true, // Valid left-hand side.
183 Variable::NORMAL);
184 AllocateHeapSlot(variable);
185}
186
187
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000188void Scope::SetDefaults(Type type,
189 Scope* outer_scope,
190 Handle<SerializedScopeInfo> scope_info) {
191 outer_scope_ = outer_scope;
192 type_ = type;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000193 scope_name_ = isolate_->factory()->empty_symbol();
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000194 dynamics_ = NULL;
195 receiver_ = NULL;
196 function_ = NULL;
197 arguments_ = NULL;
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000198 illegal_redecl_ = NULL;
199 scope_inside_with_ = false;
200 scope_contains_with_ = false;
201 scope_calls_eval_ = false;
202 // Inherit the strict mode from the parent scope.
203 strict_mode_ = (outer_scope != NULL) && outer_scope->strict_mode_;
204 outer_scope_calls_eval_ = false;
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000205 outer_scope_calls_non_strict_eval_ = false;
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000206 inner_scope_calls_eval_ = false;
207 outer_scope_is_eval_scope_ = false;
208 force_eager_compilation_ = false;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000209 num_var_or_const_ = 0;
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000210 num_stack_slots_ = 0;
211 num_heap_slots_ = 0;
212 scope_info_ = scope_info;
213}
214
215
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000216Scope* Scope::DeserializeScopeChain(CompilationInfo* info,
217 Scope* global_scope) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000218 // Reconstruct the outer scope chain from a closure's context chain.
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000219 ASSERT(!info->closure().is_null());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000220 Context* context = info->closure()->context();
221 Scope* current_scope = NULL;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000222 Scope* innermost_scope = NULL;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000223 bool contains_with = false;
224 while (!context->IsGlobalContext()) {
225 if (context->IsWithContext()) {
226 // All the inner scopes are inside a with.
227 contains_with = true;
228 for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) {
229 s->scope_inside_with_ = true;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000230 }
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000231 } else {
232 if (context->IsFunctionContext()) {
233 SerializedScopeInfo* scope_info =
234 context->closure()->shared()->scope_info();
235 current_scope =
236 new Scope(current_scope, Handle<SerializedScopeInfo>(scope_info));
237 } else {
238 ASSERT(context->IsCatchContext());
239 String* name = String::cast(context->extension());
240 current_scope = new Scope(current_scope, Handle<String>(name));
241 }
242 if (contains_with) current_scope->RecordWithStatement();
243 if (innermost_scope == NULL) innermost_scope = current_scope;
244 }
245
246 // Forget about a with when we move to a context for a different function.
247 if (context->previous()->closure() != context->closure()) {
248 contains_with = false;
249 }
250 context = context->previous();
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000251 }
252
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000253 global_scope->AddInnerScope(current_scope);
254 return (innermost_scope == NULL) ? global_scope : innermost_scope;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000255}
256
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000257
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000258bool Scope::Analyze(CompilationInfo* info) {
259 ASSERT(info->function() != NULL);
260 Scope* top = info->function()->scope();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000261
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000262 while (top->outer_scope() != NULL) top = top->outer_scope();
263 top->AllocateVariables(info->calling_context());
264
265#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000266 if (info->isolate()->bootstrapper()->IsActive()
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000267 ? FLAG_print_builtin_scopes
268 : FLAG_print_scopes) {
269 info->function()->scope()->Print();
270 }
271#endif
272
273 info->SetScope(info->function()->scope());
274 return true; // Can not fail.
275}
276
277
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278void Scope::Initialize(bool inside_with) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000279 ASSERT(!already_resolved());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000280
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281 // Add this scope as a new inner scope of the outer scope.
282 if (outer_scope_ != NULL) {
283 outer_scope_->inner_scopes_.Add(this);
284 scope_inside_with_ = outer_scope_->scope_inside_with_ || inside_with;
285 } else {
286 scope_inside_with_ = inside_with;
287 }
288
289 // Declare convenience variables.
290 // Declare and allocate receiver (even for the global scope, and even
291 // if naccesses_ == 0).
292 // NOTE: When loading parameters in the global scope, we must take
293 // care not to access them as properties of the global object, but
294 // instead load them directly from the stack. Currently, the only
295 // such parameter is 'this' which is passed on the stack when
296 // invoking scripts
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000297 if (is_catch_scope()) {
298 ASSERT(outer_scope() != NULL);
299 receiver_ = outer_scope()->receiver();
300 } else {
301 Variable* var =
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000302 variables_.Declare(this,
303 isolate_->factory()->this_symbol(),
304 Variable::VAR,
305 false,
306 Variable::THIS);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000307 var->set_rewrite(NewSlot(var, Slot::PARAMETER, -1));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000308 receiver_ = var;
309 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000310
311 if (is_function_scope()) {
312 // Declare 'arguments' variable which exists in all functions.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000313 // Note that it might never be accessed, in which case it won't be
314 // allocated during variable allocation.
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000315 variables_.Declare(this,
316 isolate_->factory()->arguments_symbol(),
317 Variable::VAR,
318 true,
319 Variable::ARGUMENTS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000320 }
321}
322
323
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000324Variable* Scope::LocalLookup(Handle<String> name) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000325 Variable* result = variables_.Lookup(name);
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000326 if (result != NULL || scope_info_.is_null()) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000327 return result;
328 }
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000329 // If we have a serialized scope info, we might find the variable there.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000330 //
331 // We should never lookup 'arguments' in this scope as it is implicitly
332 // present in every scope.
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000333 ASSERT(*name != *isolate_->factory()->arguments_symbol());
whesse@chromium.org7b260152011-06-20 15:33:18 +0000334 // There should be no local slot with the given name.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000335 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
336
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000337 // Check context slot lookup.
338 Variable::Mode mode;
339 int index = scope_info_->ContextSlotIndex(*name, &mode);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000340 if (index < 0) {
341 // Check parameters.
342 mode = Variable::VAR;
343 index = scope_info_->ParameterIndex(*name);
344 if (index < 0) {
345 // Check the function name.
346 index = scope_info_->FunctionContextSlotIndex(*name);
347 if (index < 0) return NULL;
348 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000349 }
350
whesse@chromium.org7b260152011-06-20 15:33:18 +0000351 Variable* var =
352 variables_.Declare(this, name, mode, true, Variable::NORMAL);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000353 var->set_rewrite(NewSlot(var, Slot::CONTEXT, index));
whesse@chromium.org7b260152011-06-20 15:33:18 +0000354 return var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000355}
356
357
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000358Variable* Scope::Lookup(Handle<String> name) {
359 for (Scope* scope = this;
360 scope != NULL;
361 scope = scope->outer_scope()) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000362 Variable* var = scope->LocalLookup(name);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000363 if (var != NULL) return var;
364 }
365 return NULL;
366}
367
368
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000369Variable* Scope::DeclareFunctionVar(Handle<String> name) {
370 ASSERT(is_function_scope() && function_ == NULL);
ager@chromium.org3e875802009-06-29 08:26:34 +0000371 function_ = new Variable(this, name, Variable::CONST, true, Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372 return function_;
373}
374
375
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000376void Scope::DeclareParameter(Handle<String> name) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000377 ASSERT(!already_resolved());
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000378 ASSERT(is_function_scope());
379 Variable* var =
380 variables_.Declare(this, name, Variable::VAR, true, Variable::NORMAL);
381 params_.Add(var);
382}
383
384
385Variable* Scope::DeclareLocal(Handle<String> name, Variable::Mode mode) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000386 ASSERT(!already_resolved());
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000387 // This function handles VAR and CONST modes. DYNAMIC variables are
388 // introduces during variable allocation, INTERNAL variables are allocated
389 // explicitly, and TEMPORARY variables are allocated via NewTemporary().
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390 ASSERT(mode == Variable::VAR || mode == Variable::CONST);
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000391 ++num_var_or_const_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000392 return variables_.Declare(this, name, mode, true, Variable::NORMAL);
393}
394
395
396Variable* Scope::DeclareGlobal(Handle<String> name) {
397 ASSERT(is_global_scope());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000398 return variables_.Declare(this, name, Variable::DYNAMIC_GLOBAL, true,
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000399 Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400}
401
402
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000403VariableProxy* Scope::NewUnresolved(Handle<String> name,
404 bool inside_with,
405 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406 // Note that we must not share the unresolved variables with
407 // the same name because they may be removed selectively via
408 // RemoveUnresolved().
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000409 ASSERT(!already_resolved());
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000410 VariableProxy* proxy = new(isolate_->zone()) VariableProxy(
411 isolate_, name, false, inside_with, position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000412 unresolved_.Add(proxy);
413 return proxy;
414}
415
416
417void Scope::RemoveUnresolved(VariableProxy* var) {
418 // Most likely (always?) any variable we want to remove
419 // was just added before, so we search backwards.
420 for (int i = unresolved_.length(); i-- > 0;) {
421 if (unresolved_[i] == var) {
422 unresolved_.Remove(i);
423 return;
424 }
425 }
426}
427
428
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000429Variable* Scope::NewTemporary(Handle<String> name) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000430 ASSERT(!already_resolved());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000431 Variable* var =
432 new Variable(this, name, Variable::TEMPORARY, true, Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433 temps_.Add(var);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000434 return var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000435}
436
437
438void Scope::AddDeclaration(Declaration* declaration) {
439 decls_.Add(declaration);
440}
441
442
443void Scope::SetIllegalRedeclaration(Expression* expression) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000444 // Record only the first illegal redeclaration.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445 if (!HasIllegalRedeclaration()) {
446 illegal_redecl_ = expression;
447 }
448 ASSERT(HasIllegalRedeclaration());
449}
450
451
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000452void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000453 ASSERT(HasIllegalRedeclaration());
454 illegal_redecl_->Accept(visitor);
455}
456
457
458template<class Allocator>
459void Scope::CollectUsedVariables(List<Variable*, Allocator>* locals) {
460 // Collect variables in this scope.
461 // Note that the function_ variable - if present - is not
462 // collected here but handled separately in ScopeInfo
463 // which is the current user of this function).
464 for (int i = 0; i < temps_.length(); i++) {
465 Variable* var = temps_[i];
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000466 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467 locals->Add(var);
468 }
469 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000470 for (VariableMap::Entry* p = variables_.Start();
471 p != NULL;
472 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473 Variable* var = reinterpret_cast<Variable*>(p->value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000474 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 locals->Add(var);
476 }
477 }
478}
479
480
481// Make sure the method gets instantiated by the template system.
482template void Scope::CollectUsedVariables(
483 List<Variable*, FreeStoreAllocationPolicy>* locals);
484template void Scope::CollectUsedVariables(
485 List<Variable*, PreallocatedStorage>* locals);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000486template void Scope::CollectUsedVariables(
487 List<Variable*, ZoneListAllocationPolicy>* locals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488
489
ager@chromium.org381abbb2009-02-25 13:23:22 +0000490void Scope::AllocateVariables(Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491 ASSERT(outer_scope_ == NULL); // eval or global scopes only
492
493 // 1) Propagate scope information.
494 // If we are in an eval scope, we may have other outer scopes about
495 // which we don't know anything at this point. Thus we must be conservative
496 // and assume they may invoke eval themselves. Eventually we could capture
497 // this information in the ScopeInfo and then use it here (by traversing
498 // the call chain stack, at compile time).
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000499
ager@chromium.org381abbb2009-02-25 13:23:22 +0000500 bool eval_scope = is_eval_scope();
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000501 bool outer_scope_calls_eval = false;
502 bool outer_scope_calls_non_strict_eval = false;
503 if (!is_global_scope()) {
504 context->ComputeEvalScopeInfo(&outer_scope_calls_eval,
505 &outer_scope_calls_non_strict_eval);
506 }
507 PropagateScopeInfo(outer_scope_calls_eval,
508 outer_scope_calls_non_strict_eval,
509 eval_scope);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510
511 // 2) Resolve variables.
512 Scope* global_scope = NULL;
513 if (is_global_scope()) global_scope = this;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000514 ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515
516 // 3) Allocate variables.
517 AllocateVariablesRecursively();
518}
519
520
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521bool Scope::AllowsLazyCompilation() const {
522 return !force_eager_compilation_ && HasTrivialOuterContext();
523}
524
525
526bool Scope::HasTrivialContext() const {
527 // A function scope has a trivial context if it always is the global
528 // context. We iteratively scan out the context chain to see if
529 // there is anything that makes this scope non-trivial; otherwise we
530 // return true.
531 for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) {
532 if (scope->is_eval_scope()) return false;
533 if (scope->scope_inside_with_) return false;
534 if (scope->num_heap_slots_ > 0) return false;
535 }
536 return true;
537}
538
539
540bool Scope::HasTrivialOuterContext() const {
541 Scope* outer = outer_scope_;
542 if (outer == NULL) return true;
543 // Note that the outer context may be trivial in general, but the current
544 // scope may be inside a 'with' statement in which case the outer context
545 // for this scope is not trivial.
546 return !scope_inside_with_ && outer->HasTrivialContext();
547}
548
549
550int Scope::ContextChainLength(Scope* scope) {
551 int n = 0;
552 for (Scope* s = this; s != scope; s = s->outer_scope_) {
553 ASSERT(s != NULL); // scope must be in the scope chain
554 if (s->num_heap_slots() > 0) n++;
555 }
556 return n;
557}
558
559
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000560Scope* Scope::DeclarationScope() {
561 Scope* scope = this;
562 while (scope->is_catch_scope()) {
563 scope = scope->outer_scope();
564 }
565 return scope;
566}
567
568
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569#ifdef DEBUG
570static const char* Header(Scope::Type type) {
571 switch (type) {
572 case Scope::EVAL_SCOPE: return "eval";
573 case Scope::FUNCTION_SCOPE: return "function";
574 case Scope::GLOBAL_SCOPE: return "global";
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000575 case Scope::CATCH_SCOPE: return "catch";
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576 }
577 UNREACHABLE();
578 return NULL;
579}
580
581
582static void Indent(int n, const char* str) {
583 PrintF("%*s%s", n, "", str);
584}
585
586
587static void PrintName(Handle<String> name) {
588 SmartPointer<char> s = name->ToCString(DISALLOW_NULLS);
589 PrintF("%s", *s);
590}
591
592
593static void PrintVar(PrettyPrinter* printer, int indent, Variable* var) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000594 if (var->is_used() || var->rewrite() != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000595 Indent(indent, Variable::Mode2String(var->mode()));
596 PrintF(" ");
597 PrintName(var->name());
598 PrintF("; // ");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000599 if (var->rewrite() != NULL) {
600 PrintF("%s, ", printer->Print(var->rewrite()));
601 if (var->is_accessed_from_inner_scope()) PrintF(", ");
602 }
603 if (var->is_accessed_from_inner_scope()) PrintF("inner scope access");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604 PrintF("\n");
605 }
606}
607
608
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000609static void PrintMap(PrettyPrinter* printer, int indent, VariableMap* map) {
610 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000611 Variable* var = reinterpret_cast<Variable*>(p->value);
612 PrintVar(printer, indent, var);
613 }
614}
615
616
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617void Scope::Print(int n) {
618 int n0 = (n > 0 ? n : 0);
619 int n1 = n0 + 2; // indentation
620
621 // Print header.
622 Indent(n0, Header(type_));
623 if (scope_name_->length() > 0) {
624 PrintF(" ");
625 PrintName(scope_name_);
626 }
627
628 // Print parameters, if any.
629 if (is_function_scope()) {
630 PrintF(" (");
631 for (int i = 0; i < params_.length(); i++) {
632 if (i > 0) PrintF(", ");
633 PrintName(params_[i]->name());
634 }
635 PrintF(")");
636 }
637
638 PrintF(" {\n");
639
640 // Function name, if any (named function literals, only).
641 if (function_ != NULL) {
642 Indent(n1, "// (local) function name: ");
643 PrintName(function_->name());
644 PrintF("\n");
645 }
646
647 // Scope info.
648 if (HasTrivialOuterContext()) {
649 Indent(n1, "// scope has trivial outer context\n");
650 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000651 if (is_strict_mode()) Indent(n1, "// strict mode scope\n");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
653 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n");
654 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n");
655 if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n");
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000656 if (outer_scope_calls_non_strict_eval_) {
657 Indent(n1, "// outer scope calls 'eval' in non-strict context\n");
658 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
ager@chromium.org381abbb2009-02-25 13:23:22 +0000660 if (outer_scope_is_eval_scope_) {
661 Indent(n1, "// outer scope is 'eval' scope\n");
662 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000663 if (num_stack_slots_ > 0) { Indent(n1, "// ");
664 PrintF("%d stack slots\n", num_stack_slots_); }
665 if (num_heap_slots_ > 0) { Indent(n1, "// ");
666 PrintF("%d heap slots\n", num_heap_slots_); }
667
668 // Print locals.
669 PrettyPrinter printer;
670 Indent(n1, "// function var\n");
671 if (function_ != NULL) {
672 PrintVar(&printer, n1, function_);
673 }
674
675 Indent(n1, "// temporary vars\n");
676 for (int i = 0; i < temps_.length(); i++) {
677 PrintVar(&printer, n1, temps_[i]);
678 }
679
680 Indent(n1, "// local vars\n");
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000681 PrintMap(&printer, n1, &variables_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000682
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000683 Indent(n1, "// dynamic vars\n");
684 if (dynamics_ != NULL) {
685 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC));
686 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_LOCAL));
687 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_GLOBAL));
688 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689
690 // Print inner scopes (disable by providing negative n).
691 if (n >= 0) {
692 for (int i = 0; i < inner_scopes_.length(); i++) {
693 PrintF("\n");
694 inner_scopes_[i]->Print(n1);
695 }
696 }
697
698 Indent(n0, "}\n");
699}
700#endif // DEBUG
701
702
ager@chromium.org381abbb2009-02-25 13:23:22 +0000703Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000704 if (dynamics_ == NULL) dynamics_ = new DynamicScopePart();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000705 VariableMap* map = dynamics_->GetMap(mode);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000706 Variable* var = map->Lookup(name);
707 if (var == NULL) {
708 // Declare a new non-local.
ager@chromium.org3e875802009-06-29 08:26:34 +0000709 var = map->Declare(NULL, name, mode, true, Variable::NORMAL);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000710 // Allocate it by giving it a dynamic lookup.
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000711 var->set_rewrite(NewSlot(var, Slot::LOOKUP, -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000712 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713 return var;
714}
715
716
717// Lookup a variable starting with this scope. The result is either
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000718// the statically resolved variable belonging to an outer scope, or
719// NULL. It may be NULL because a) we couldn't find a variable, or b)
720// because the variable is just a guess (and may be shadowed by
721// another variable that is introduced dynamically via an 'eval' call
722// or a 'with' statement).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000723Variable* Scope::LookupRecursive(Handle<String> name,
724 bool inner_lookup,
725 Variable** invalidated_local) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000726 // If we find a variable, but the current scope calls 'eval', the found
727 // variable may not be the correct one (the 'eval' may introduce a
728 // property with the same name). In that case, remember that the variable
729 // found is just a guess.
730 bool guess = scope_calls_eval_;
731
732 // Try to find the variable in this scope.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000733 Variable* var = LocalLookup(name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000734
735 if (var != NULL) {
736 // We found a variable. If this is not an inner lookup, we are done.
737 // (Even if there is an 'eval' in this scope which introduces the
738 // same variable again, the resulting variable remains the same.
739 // Note that enclosing 'with' statements are handled at the call site.)
740 if (!inner_lookup)
741 return var;
742
743 } else {
744 // We did not find a variable locally. Check against the function variable,
745 // if any. We can do this for all scopes, since the function variable is
746 // only present - if at all - for function scopes.
747 //
748 // This lookup corresponds to a lookup in the "intermediate" scope sitting
749 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
750 // the name of named function literal is kept in an intermediate scope
ager@chromium.org32912102009-01-16 10:38:43 +0000751 // in between this scope and the next outer scope.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000752 if (function_ != NULL && function_->name().is_identical_to(name)) {
753 var = function_;
754
755 } else if (outer_scope_ != NULL) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000756 var = outer_scope_->LookupRecursive(name, true, invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000757 // We may have found a variable in an outer scope. However, if
758 // the current scope is inside a 'with', the actual variable may
759 // be a property introduced via the 'with' statement. Then, the
760 // variable we may have found is just a guess.
761 if (scope_inside_with_)
762 guess = true;
763 }
764
765 // If we did not find a variable, we are done.
766 if (var == NULL)
767 return NULL;
768 }
769
770 ASSERT(var != NULL);
771
772 // If this is a lookup from an inner scope, mark the variable.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000773 if (inner_lookup) {
774 var->MarkAsAccessedFromInnerScope();
775 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000776
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000777 // If the variable we have found is just a guess, invalidate the
778 // result. If the found variable is local, record that fact so we
779 // can generate fast code to get it if it is not shadowed by eval.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000780 if (guess) {
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000781 if (!var->is_global()) *invalidated_local = var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000782 var = NULL;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000783 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000784
785 return var;
786}
787
788
ager@chromium.org381abbb2009-02-25 13:23:22 +0000789void Scope::ResolveVariable(Scope* global_scope,
790 Handle<Context> context,
791 VariableProxy* proxy) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000792 ASSERT(global_scope == NULL || global_scope->is_global_scope());
793
794 // If the proxy is already resolved there's nothing to do
795 // (functions and consts may be resolved by the parser).
796 if (proxy->var() != NULL) return;
797
798 // Otherwise, try to resolve the variable.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000799 Variable* invalidated_local = NULL;
800 Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000801
802 if (proxy->inside_with()) {
803 // If we are inside a local 'with' statement, all bets are off
804 // and we cannot resolve the proxy to a local variable even if
805 // we found an outer matching variable.
806 // Note that we must do a lookup anyway, because if we find one,
807 // we must mark that variable as potentially accessed from this
808 // inner scope (the property may not be in the 'with' object).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000809 var = NonLocal(proxy->name(), Variable::DYNAMIC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000810
811 } else {
812 // We are not inside a local 'with' statement.
813
814 if (var == NULL) {
815 // We did not find the variable. We have a global variable
816 // if we are in the global scope (we know already that we
817 // are outside a 'with' statement) or if there is no way
818 // that the variable might be introduced dynamically (through
819 // a local or outer eval() call, or an outer 'with' statement),
820 // or we don't know about the outer scope (because we are
821 // in an eval scope).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000822 if (is_global_scope() ||
823 !(scope_inside_with_ || outer_scope_is_eval_scope_ ||
824 scope_calls_eval_ || outer_scope_calls_eval_)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000825 // We must have a global variable.
826 ASSERT(global_scope != NULL);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000827 var = global_scope->DeclareGlobal(proxy->name());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000828
829 } else if (scope_inside_with_) {
830 // If we are inside a with statement we give up and look up
831 // the variable at runtime.
832 var = NonLocal(proxy->name(), Variable::DYNAMIC);
833
834 } else if (invalidated_local != NULL) {
835 // No with statements are involved and we found a local
836 // variable that might be shadowed by eval introduced
837 // variables.
838 var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL);
839 var->set_local_if_not_shadowed(invalidated_local);
840
841 } else if (outer_scope_is_eval_scope_) {
842 // No with statements and we did not find a local and the code
843 // is executed with a call to eval. The context contains
844 // scope information that we can use to determine if the
845 // variable is global if it is not shadowed by eval-introduced
846 // variables.
847 if (context->GlobalIfNotShadowedByEval(proxy->name())) {
848 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
849
850 } else {
851 var = NonLocal(proxy->name(), Variable::DYNAMIC);
852 }
853
854 } else {
855 // No with statements and we did not find a local and the code
856 // is not executed with a call to eval. We know that this
857 // variable is global unless it is shadowed by eval-introduced
858 // variables.
859 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860 }
861 }
862 }
863
864 proxy->BindTo(var);
865}
866
867
ager@chromium.org381abbb2009-02-25 13:23:22 +0000868void Scope::ResolveVariablesRecursively(Scope* global_scope,
869 Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870 ASSERT(global_scope == NULL || global_scope->is_global_scope());
871
872 // Resolve unresolved variables for this scope.
873 for (int i = 0; i < unresolved_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000874 ResolveVariable(global_scope, context, unresolved_[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000875 }
876
877 // Resolve unresolved variables for inner scopes.
878 for (int i = 0; i < inner_scopes_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000879 inner_scopes_[i]->ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880 }
881}
882
883
ager@chromium.org381abbb2009-02-25 13:23:22 +0000884bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval,
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000885 bool outer_scope_calls_non_strict_eval,
ager@chromium.org381abbb2009-02-25 13:23:22 +0000886 bool outer_scope_is_eval_scope) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887 if (outer_scope_calls_eval) {
888 outer_scope_calls_eval_ = true;
889 }
890
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000891 if (outer_scope_calls_non_strict_eval) {
892 outer_scope_calls_non_strict_eval_ = true;
893 }
894
ager@chromium.org381abbb2009-02-25 13:23:22 +0000895 if (outer_scope_is_eval_scope) {
896 outer_scope_is_eval_scope_ = true;
897 }
898
899 bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_;
900 bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_;
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000901 bool calls_non_strict_eval =
902 (scope_calls_eval_ && !is_strict_mode()) ||
903 outer_scope_calls_non_strict_eval_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000904 for (int i = 0; i < inner_scopes_.length(); i++) {
905 Scope* inner_scope = inner_scopes_[i];
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000906 if (inner_scope->PropagateScopeInfo(calls_eval,
907 calls_non_strict_eval,
908 is_eval)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000909 inner_scope_calls_eval_ = true;
910 }
911 if (inner_scope->force_eager_compilation_) {
912 force_eager_compilation_ = true;
913 }
914 }
915
916 return scope_calls_eval_ || inner_scope_calls_eval_;
917}
918
919
920bool Scope::MustAllocate(Variable* var) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000921 // Give var a read/write use if there is a chance it might be accessed
922 // via an eval() call. This is only possible if the variable has a
923 // visible name.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000924 if ((var->is_this() || var->name()->length() > 0) &&
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000925 (var->is_accessed_from_inner_scope() ||
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000926 scope_calls_eval_ ||
927 inner_scope_calls_eval_ ||
928 scope_contains_with_ ||
929 is_catch_scope())) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000930 var->set_is_used(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000931 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000932 // Global variables do not need to be allocated.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000933 return !var->is_global() && var->is_used();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934}
935
936
937bool Scope::MustAllocateInContext(Variable* var) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000938 // If var is accessed from an inner scope, or if there is a possibility
939 // that it might be accessed from the current or an inner scope (through
940 // an eval() call or a runtime with lookup), it must be allocated in the
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000941 // context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000942 //
943 // Exceptions: temporary variables are never allocated in a context;
944 // catch-bound variables are always allocated in a context.
945 if (var->mode() == Variable::TEMPORARY) return false;
946 if (is_catch_scope()) return true;
947 return var->is_accessed_from_inner_scope() ||
948 scope_calls_eval_ ||
949 inner_scope_calls_eval_ ||
950 scope_contains_with_ ||
951 var->is_global();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000952}
953
954
955bool Scope::HasArgumentsParameter() {
956 for (int i = 0; i < params_.length(); i++) {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000957 if (params_[i]->name().is_identical_to(
958 isolate_->factory()->arguments_symbol())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959 return true;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000960 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000961 }
962 return false;
963}
964
965
966void Scope::AllocateStackSlot(Variable* var) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000967 var->set_rewrite(NewSlot(var, Slot::LOCAL, num_stack_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968}
969
970
971void Scope::AllocateHeapSlot(Variable* var) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000972 var->set_rewrite(NewSlot(var, Slot::CONTEXT, num_heap_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973}
974
975
976void Scope::AllocateParameterLocals() {
977 ASSERT(is_function_scope());
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000978 Variable* arguments = LocalLookup(isolate_->factory()->arguments_symbol());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000979 ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000980
whesse@chromium.org7b260152011-06-20 15:33:18 +0000981 bool uses_nonstrict_arguments = false;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000982
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000983 if (MustAllocate(arguments) && !HasArgumentsParameter()) {
984 // 'arguments' is used. Unless there is also a parameter called
whesse@chromium.org7b260152011-06-20 15:33:18 +0000985 // 'arguments', we must be conservative and allocate all parameters to
986 // the context assuming they will be captured by the arguments object.
987 // If we have a parameter named 'arguments', a (new) value is always
988 // assigned to it via the function invocation. Then 'arguments' denotes
989 // that specific parameter value and cannot be used to access the
990 // parameters, which is why we don't need to allocate an arguments
991 // object in that case.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000992
993 // We are using 'arguments'. Tell the code generator that is needs to
994 // allocate the arguments object by setting 'arguments_'.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000995 arguments_ = arguments;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000996
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000997 // In strict mode 'arguments' does not alias formal parameters.
998 // Therefore in strict mode we allocate parameters as if 'arguments'
999 // were not used.
whesse@chromium.org7b260152011-06-20 15:33:18 +00001000 uses_nonstrict_arguments = !is_strict_mode();
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001001 }
1002
whesse@chromium.org7b260152011-06-20 15:33:18 +00001003 // The same parameter may occur multiple times in the parameters_ list.
1004 // If it does, and if it is not copied into the context object, it must
1005 // receive the highest parameter index for that parameter; thus iteration
1006 // order is relevant!
1007 for (int i = params_.length() - 1; i >= 0; --i) {
1008 Variable* var = params_[i];
1009 ASSERT(var->scope() == this);
1010 if (uses_nonstrict_arguments) {
1011 // Give the parameter a use from an inner scope, to force allocation
1012 // to the context.
1013 var->MarkAsAccessedFromInnerScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001014 }
1015
whesse@chromium.org7b260152011-06-20 15:33:18 +00001016 if (MustAllocate(var)) {
1017 if (MustAllocateInContext(var)) {
1018 ASSERT(var->rewrite() == NULL || var->IsContextSlot());
1019 if (var->rewrite() == NULL) {
1020 AllocateHeapSlot(var);
1021 }
1022 } else {
1023 ASSERT(var->rewrite() == NULL || var->IsParameter());
1024 if (var->rewrite() == NULL) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001025 var->set_rewrite(NewSlot(var, Slot::PARAMETER, i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001026 }
1027 }
1028 }
1029 }
1030}
1031
1032
1033void Scope::AllocateNonParameterLocal(Variable* var) {
1034 ASSERT(var->scope() == this);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001035 ASSERT(var->rewrite() == NULL ||
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001036 !var->IsVariable(isolate_->factory()->result_symbol()) ||
whesse@chromium.org7b260152011-06-20 15:33:18 +00001037 var->AsSlot() == NULL ||
1038 var->AsSlot()->type() != Slot::LOCAL);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001039 if (var->rewrite() == NULL && MustAllocate(var)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001040 if (MustAllocateInContext(var)) {
1041 AllocateHeapSlot(var);
1042 } else {
1043 AllocateStackSlot(var);
1044 }
1045 }
1046}
1047
1048
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001049void Scope::AllocateNonParameterLocals() {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001050 // All variables that have no rewrite yet are non-parameter locals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051 for (int i = 0; i < temps_.length(); i++) {
1052 AllocateNonParameterLocal(temps_[i]);
1053 }
1054
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001055 for (VariableMap::Entry* p = variables_.Start();
1056 p != NULL;
1057 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058 Variable* var = reinterpret_cast<Variable*>(p->value);
1059 AllocateNonParameterLocal(var);
1060 }
1061
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001062 // For now, function_ must be allocated at the very end. If it gets
1063 // allocated in the context, it must be the last slot in the context,
1064 // because of the current ScopeInfo implementation (see
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001065 // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor).
1066 if (function_ != NULL) {
1067 AllocateNonParameterLocal(function_);
1068 }
1069}
1070
1071
1072void Scope::AllocateVariablesRecursively() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001073 // Allocate variables for inner scopes.
1074 for (int i = 0; i < inner_scopes_.length(); i++) {
1075 inner_scopes_[i]->AllocateVariablesRecursively();
1076 }
1077
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001078 // If scope is already resolved, we still need to allocate
1079 // variables in inner scopes which might not had been resolved yet.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001080 if (already_resolved()) return;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001081 // The number of slots required for variables.
1082 num_stack_slots_ = 0;
1083 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
1084
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085 // Allocate variables for this scope.
1086 // Parameters must be allocated first, if any.
1087 if (is_function_scope()) AllocateParameterLocals();
1088 AllocateNonParameterLocals();
1089
1090 // Allocate context if necessary.
1091 bool must_have_local_context = false;
1092 if (scope_calls_eval_ || scope_contains_with_) {
1093 // The context for the eval() call or 'with' statement in this scope.
1094 // Unless we are in the global or an eval scope, we need a local
1095 // context even if we didn't statically allocate any locals in it,
1096 // and the compiler will access the context variable. If we are
1097 // not in an inner scope, the scope is provided from the outside.
1098 must_have_local_context = is_function_scope();
1099 }
1100
1101 // If we didn't allocate any locals in the local context, then we only
1102 // need the minimal number of slots if we must have a local context.
1103 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS &&
1104 !must_have_local_context) {
1105 num_heap_slots_ = 0;
1106 }
1107
1108 // Allocation done.
1109 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
1110}
1111
1112} } // namespace v8::internal