blob: eb0deb4964bb8cbc8ad5d1a151e381053c42bff1 [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
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000149Scope::Scope(Scope* inner_scope,
150 Type type,
151 Handle<SerializedScopeInfo> scope_info)
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000152 : isolate_(Isolate::Current()),
153 inner_scopes_(4),
154 variables_(),
155 temps_(4),
156 params_(4),
157 unresolved_(16),
158 decls_(4),
159 already_resolved_(true) {
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000160 ASSERT(!scope_info.is_null());
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000161 SetDefaults(type, NULL, scope_info);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000162 if (scope_info->HasHeapAllocatedLocals()) {
163 num_heap_slots_ = scope_info_->NumberOfContextSlots();
164 }
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000165 AddInnerScope(inner_scope);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000166}
167
168
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000169Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name)
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000170 : isolate_(Isolate::Current()),
171 inner_scopes_(1),
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000172 variables_(),
173 temps_(0),
174 params_(0),
175 unresolved_(0),
176 decls_(0),
177 already_resolved_(true) {
178 SetDefaults(CATCH_SCOPE, NULL, Handle<SerializedScopeInfo>::null());
179 AddInnerScope(inner_scope);
180 ++num_var_or_const_;
181 Variable* variable = variables_.Declare(this,
182 catch_variable_name,
183 Variable::VAR,
184 true, // Valid left-hand side.
185 Variable::NORMAL);
186 AllocateHeapSlot(variable);
187}
188
189
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000190void Scope::SetDefaults(Type type,
191 Scope* outer_scope,
192 Handle<SerializedScopeInfo> scope_info) {
193 outer_scope_ = outer_scope;
194 type_ = type;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000195 scope_name_ = isolate_->factory()->empty_symbol();
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000196 dynamics_ = NULL;
197 receiver_ = NULL;
198 function_ = NULL;
199 arguments_ = NULL;
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000200 illegal_redecl_ = NULL;
201 scope_inside_with_ = false;
202 scope_contains_with_ = false;
203 scope_calls_eval_ = false;
204 // Inherit the strict mode from the parent scope.
205 strict_mode_ = (outer_scope != NULL) && outer_scope->strict_mode_;
206 outer_scope_calls_eval_ = false;
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000207 outer_scope_calls_non_strict_eval_ = false;
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000208 inner_scope_calls_eval_ = false;
209 outer_scope_is_eval_scope_ = false;
210 force_eager_compilation_ = false;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000211 num_var_or_const_ = 0;
sgjesse@chromium.org34755092011-04-07 08:41:03 +0000212 num_stack_slots_ = 0;
213 num_heap_slots_ = 0;
214 scope_info_ = scope_info;
215}
216
217
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000218Scope* Scope::DeserializeScopeChain(CompilationInfo* info,
219 Scope* global_scope) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000220 // Reconstruct the outer scope chain from a closure's context chain.
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000221 ASSERT(!info->closure().is_null());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000222 Context* context = info->closure()->context();
223 Scope* current_scope = NULL;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000224 Scope* innermost_scope = NULL;
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000225 bool contains_with = false;
226 while (!context->IsGlobalContext()) {
227 if (context->IsWithContext()) {
228 // All the inner scopes are inside a with.
229 contains_with = true;
230 for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) {
231 s->scope_inside_with_ = true;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000232 }
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000233 } else {
234 if (context->IsFunctionContext()) {
235 SerializedScopeInfo* scope_info =
236 context->closure()->shared()->scope_info();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000237 current_scope = new Scope(current_scope, FUNCTION_SCOPE,
238 Handle<SerializedScopeInfo>(scope_info));
239 } else if (context->IsBlockContext()) {
240 SerializedScopeInfo* scope_info =
241 SerializedScopeInfo::cast(context->extension());
242 current_scope = new Scope(current_scope, BLOCK_SCOPE,
243 Handle<SerializedScopeInfo>(scope_info));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000244 } else {
245 ASSERT(context->IsCatchContext());
246 String* name = String::cast(context->extension());
247 current_scope = new Scope(current_scope, Handle<String>(name));
248 }
249 if (contains_with) current_scope->RecordWithStatement();
250 if (innermost_scope == NULL) innermost_scope = current_scope;
251 }
252
253 // Forget about a with when we move to a context for a different function.
254 if (context->previous()->closure() != context->closure()) {
255 contains_with = false;
256 }
257 context = context->previous();
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000258 }
259
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000260 global_scope->AddInnerScope(current_scope);
261 return (innermost_scope == NULL) ? global_scope : innermost_scope;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000262}
263
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000264
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000265bool Scope::Analyze(CompilationInfo* info) {
266 ASSERT(info->function() != NULL);
267 Scope* top = info->function()->scope();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000268
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000269 while (top->outer_scope() != NULL) top = top->outer_scope();
270 top->AllocateVariables(info->calling_context());
271
272#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000273 if (info->isolate()->bootstrapper()->IsActive()
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000274 ? FLAG_print_builtin_scopes
275 : FLAG_print_scopes) {
276 info->function()->scope()->Print();
277 }
278#endif
279
280 info->SetScope(info->function()->scope());
281 return true; // Can not fail.
282}
283
284
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000285void Scope::Initialize(bool inside_with) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000286 ASSERT(!already_resolved());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000287
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288 // Add this scope as a new inner scope of the outer scope.
289 if (outer_scope_ != NULL) {
290 outer_scope_->inner_scopes_.Add(this);
291 scope_inside_with_ = outer_scope_->scope_inside_with_ || inside_with;
292 } else {
293 scope_inside_with_ = inside_with;
294 }
295
296 // Declare convenience variables.
297 // Declare and allocate receiver (even for the global scope, and even
298 // if naccesses_ == 0).
299 // NOTE: When loading parameters in the global scope, we must take
300 // care not to access them as properties of the global object, but
301 // instead load them directly from the stack. Currently, the only
302 // such parameter is 'this' which is passed on the stack when
303 // invoking scripts
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000304 if (is_catch_scope() || is_block_scope()) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000305 ASSERT(outer_scope() != NULL);
306 receiver_ = outer_scope()->receiver();
307 } else {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000308 ASSERT(is_function_scope() ||
309 is_global_scope() ||
310 is_eval_scope());
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000311 Variable* var =
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000312 variables_.Declare(this,
313 isolate_->factory()->this_symbol(),
314 Variable::VAR,
315 false,
316 Variable::THIS);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000317 var->set_rewrite(NewSlot(var, Slot::PARAMETER, -1));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000318 receiver_ = var;
319 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000320
321 if (is_function_scope()) {
322 // Declare 'arguments' variable which exists in all functions.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000323 // Note that it might never be accessed, in which case it won't be
324 // allocated during variable allocation.
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000325 variables_.Declare(this,
326 isolate_->factory()->arguments_symbol(),
327 Variable::VAR,
328 true,
329 Variable::ARGUMENTS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330 }
331}
332
333
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000334Variable* Scope::LocalLookup(Handle<String> name) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000335 Variable* result = variables_.Lookup(name);
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000336 if (result != NULL || scope_info_.is_null()) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000337 return result;
338 }
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000339 // If we have a serialized scope info, we might find the variable there.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000340 //
341 // We should never lookup 'arguments' in this scope as it is implicitly
342 // present in every scope.
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000343 ASSERT(*name != *isolate_->factory()->arguments_symbol());
whesse@chromium.org7b260152011-06-20 15:33:18 +0000344 // There should be no local slot with the given name.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000345 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
346
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000347 // Check context slot lookup.
348 Variable::Mode mode;
349 int index = scope_info_->ContextSlotIndex(*name, &mode);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000350 if (index < 0) {
351 // Check parameters.
352 mode = Variable::VAR;
353 index = scope_info_->ParameterIndex(*name);
354 if (index < 0) {
355 // Check the function name.
356 index = scope_info_->FunctionContextSlotIndex(*name);
357 if (index < 0) return NULL;
358 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000359 }
360
whesse@chromium.org7b260152011-06-20 15:33:18 +0000361 Variable* var =
362 variables_.Declare(this, name, mode, true, Variable::NORMAL);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000363 var->set_rewrite(NewSlot(var, Slot::CONTEXT, index));
whesse@chromium.org7b260152011-06-20 15:33:18 +0000364 return var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365}
366
367
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000368Variable* Scope::Lookup(Handle<String> name) {
369 for (Scope* scope = this;
370 scope != NULL;
371 scope = scope->outer_scope()) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000372 Variable* var = scope->LocalLookup(name);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000373 if (var != NULL) return var;
374 }
375 return NULL;
376}
377
378
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000379Variable* Scope::DeclareFunctionVar(Handle<String> name) {
380 ASSERT(is_function_scope() && function_ == NULL);
ager@chromium.org3e875802009-06-29 08:26:34 +0000381 function_ = new Variable(this, name, Variable::CONST, true, Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000382 return function_;
383}
384
385
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000386void Scope::DeclareParameter(Handle<String> name) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000387 ASSERT(!already_resolved());
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000388 ASSERT(is_function_scope());
389 Variable* var =
390 variables_.Declare(this, name, Variable::VAR, true, Variable::NORMAL);
391 params_.Add(var);
392}
393
394
395Variable* Scope::DeclareLocal(Handle<String> name, Variable::Mode mode) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000396 ASSERT(!already_resolved());
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000397 // This function handles VAR and CONST modes. DYNAMIC variables are
398 // introduces during variable allocation, INTERNAL variables are allocated
399 // explicitly, and TEMPORARY variables are allocated via NewTemporary().
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400 ASSERT(mode == Variable::VAR || mode == Variable::CONST);
ricow@chromium.orgc54d3652011-05-30 09:20:16 +0000401 ++num_var_or_const_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000402 return variables_.Declare(this, name, mode, true, Variable::NORMAL);
403}
404
405
406Variable* Scope::DeclareGlobal(Handle<String> name) {
407 ASSERT(is_global_scope());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000408 return variables_.Declare(this, name, Variable::DYNAMIC_GLOBAL, true,
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000409 Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000410}
411
412
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000413VariableProxy* Scope::NewUnresolved(Handle<String> name,
414 bool inside_with,
415 int position) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000416 // Note that we must not share the unresolved variables with
417 // the same name because they may be removed selectively via
418 // RemoveUnresolved().
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000419 ASSERT(!already_resolved());
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000420 VariableProxy* proxy = new(isolate_->zone()) VariableProxy(
421 isolate_, name, false, inside_with, position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000422 unresolved_.Add(proxy);
423 return proxy;
424}
425
426
427void Scope::RemoveUnresolved(VariableProxy* var) {
428 // Most likely (always?) any variable we want to remove
429 // was just added before, so we search backwards.
430 for (int i = unresolved_.length(); i-- > 0;) {
431 if (unresolved_[i] == var) {
432 unresolved_.Remove(i);
433 return;
434 }
435 }
436}
437
438
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000439Variable* Scope::NewTemporary(Handle<String> name) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000440 ASSERT(!already_resolved());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000441 Variable* var =
442 new Variable(this, name, Variable::TEMPORARY, true, Variable::NORMAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000443 temps_.Add(var);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000444 return var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445}
446
447
448void Scope::AddDeclaration(Declaration* declaration) {
449 decls_.Add(declaration);
450}
451
452
453void Scope::SetIllegalRedeclaration(Expression* expression) {
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000454 // Record only the first illegal redeclaration.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000455 if (!HasIllegalRedeclaration()) {
456 illegal_redecl_ = expression;
457 }
458 ASSERT(HasIllegalRedeclaration());
459}
460
461
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000462void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000463 ASSERT(HasIllegalRedeclaration());
464 illegal_redecl_->Accept(visitor);
465}
466
467
468template<class Allocator>
469void Scope::CollectUsedVariables(List<Variable*, Allocator>* locals) {
470 // Collect variables in this scope.
471 // Note that the function_ variable - if present - is not
472 // collected here but handled separately in ScopeInfo
473 // which is the current user of this function).
474 for (int i = 0; i < temps_.length(); i++) {
475 Variable* var = temps_[i];
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000476 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000477 locals->Add(var);
478 }
479 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000480 for (VariableMap::Entry* p = variables_.Start();
481 p != NULL;
482 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000483 Variable* var = reinterpret_cast<Variable*>(p->value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000484 if (var->is_used()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485 locals->Add(var);
486 }
487 }
488}
489
490
491// Make sure the method gets instantiated by the template system.
492template void Scope::CollectUsedVariables(
493 List<Variable*, FreeStoreAllocationPolicy>* locals);
494template void Scope::CollectUsedVariables(
495 List<Variable*, PreallocatedStorage>* locals);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000496template void Scope::CollectUsedVariables(
497 List<Variable*, ZoneListAllocationPolicy>* locals);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498
499
ager@chromium.org381abbb2009-02-25 13:23:22 +0000500void Scope::AllocateVariables(Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000501 ASSERT(outer_scope_ == NULL); // eval or global scopes only
502
503 // 1) Propagate scope information.
504 // If we are in an eval scope, we may have other outer scopes about
505 // which we don't know anything at this point. Thus we must be conservative
506 // and assume they may invoke eval themselves. Eventually we could capture
507 // this information in the ScopeInfo and then use it here (by traversing
508 // the call chain stack, at compile time).
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000509
ager@chromium.org381abbb2009-02-25 13:23:22 +0000510 bool eval_scope = is_eval_scope();
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000511 bool outer_scope_calls_eval = false;
512 bool outer_scope_calls_non_strict_eval = false;
513 if (!is_global_scope()) {
514 context->ComputeEvalScopeInfo(&outer_scope_calls_eval,
515 &outer_scope_calls_non_strict_eval);
516 }
517 PropagateScopeInfo(outer_scope_calls_eval,
518 outer_scope_calls_non_strict_eval,
519 eval_scope);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520
521 // 2) Resolve variables.
522 Scope* global_scope = NULL;
523 if (is_global_scope()) global_scope = this;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000524 ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525
526 // 3) Allocate variables.
527 AllocateVariablesRecursively();
528}
529
530
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000531bool Scope::AllowsLazyCompilation() const {
532 return !force_eager_compilation_ && HasTrivialOuterContext();
533}
534
535
536bool Scope::HasTrivialContext() const {
537 // A function scope has a trivial context if it always is the global
538 // context. We iteratively scan out the context chain to see if
539 // there is anything that makes this scope non-trivial; otherwise we
540 // return true.
541 for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) {
542 if (scope->is_eval_scope()) return false;
543 if (scope->scope_inside_with_) return false;
544 if (scope->num_heap_slots_ > 0) return false;
545 }
546 return true;
547}
548
549
550bool Scope::HasTrivialOuterContext() const {
551 Scope* outer = outer_scope_;
552 if (outer == NULL) return true;
553 // Note that the outer context may be trivial in general, but the current
554 // scope may be inside a 'with' statement in which case the outer context
555 // for this scope is not trivial.
556 return !scope_inside_with_ && outer->HasTrivialContext();
557}
558
559
560int Scope::ContextChainLength(Scope* scope) {
561 int n = 0;
562 for (Scope* s = this; s != scope; s = s->outer_scope_) {
563 ASSERT(s != NULL); // scope must be in the scope chain
564 if (s->num_heap_slots() > 0) n++;
565 }
566 return n;
567}
568
569
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000570Scope* Scope::DeclarationScope() {
571 Scope* scope = this;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000572 while (scope->is_catch_scope() ||
573 scope->is_block_scope()) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000574 scope = scope->outer_scope();
575 }
576 return scope;
577}
578
579
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000580Handle<SerializedScopeInfo> Scope::GetSerializedScopeInfo() {
581 if (scope_info_.is_null()) {
582 scope_info_ = SerializedScopeInfo::Create(this);
583 }
584 return scope_info_;
585}
586
587
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588#ifdef DEBUG
589static const char* Header(Scope::Type type) {
590 switch (type) {
591 case Scope::EVAL_SCOPE: return "eval";
592 case Scope::FUNCTION_SCOPE: return "function";
593 case Scope::GLOBAL_SCOPE: return "global";
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000594 case Scope::CATCH_SCOPE: return "catch";
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000595 case Scope::BLOCK_SCOPE: return "block";
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000596 }
597 UNREACHABLE();
598 return NULL;
599}
600
601
602static void Indent(int n, const char* str) {
603 PrintF("%*s%s", n, "", str);
604}
605
606
607static void PrintName(Handle<String> name) {
608 SmartPointer<char> s = name->ToCString(DISALLOW_NULLS);
609 PrintF("%s", *s);
610}
611
612
613static void PrintVar(PrettyPrinter* printer, int indent, Variable* var) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000614 if (var->is_used() || var->rewrite() != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000615 Indent(indent, Variable::Mode2String(var->mode()));
616 PrintF(" ");
617 PrintName(var->name());
618 PrintF("; // ");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000619 if (var->rewrite() != NULL) {
620 PrintF("%s, ", printer->Print(var->rewrite()));
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000621 if (var->is_accessed_from_inner_function_scope()) PrintF(", ");
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000622 }
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000623 if (var->is_accessed_from_inner_function_scope()) {
624 PrintF("inner scope access");
625 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000626 PrintF("\n");
627 }
628}
629
630
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000631static void PrintMap(PrettyPrinter* printer, int indent, VariableMap* map) {
632 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000633 Variable* var = reinterpret_cast<Variable*>(p->value);
634 PrintVar(printer, indent, var);
635 }
636}
637
638
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639void Scope::Print(int n) {
640 int n0 = (n > 0 ? n : 0);
641 int n1 = n0 + 2; // indentation
642
643 // Print header.
644 Indent(n0, Header(type_));
645 if (scope_name_->length() > 0) {
646 PrintF(" ");
647 PrintName(scope_name_);
648 }
649
650 // Print parameters, if any.
651 if (is_function_scope()) {
652 PrintF(" (");
653 for (int i = 0; i < params_.length(); i++) {
654 if (i > 0) PrintF(", ");
655 PrintName(params_[i]->name());
656 }
657 PrintF(")");
658 }
659
660 PrintF(" {\n");
661
662 // Function name, if any (named function literals, only).
663 if (function_ != NULL) {
664 Indent(n1, "// (local) function name: ");
665 PrintName(function_->name());
666 PrintF("\n");
667 }
668
669 // Scope info.
670 if (HasTrivialOuterContext()) {
671 Indent(n1, "// scope has trivial outer context\n");
672 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000673 if (is_strict_mode()) Indent(n1, "// strict mode scope\n");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000674 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
675 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n");
676 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n");
677 if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n");
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000678 if (outer_scope_calls_non_strict_eval_) {
679 Indent(n1, "// outer scope calls 'eval' in non-strict context\n");
680 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000681 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
ager@chromium.org381abbb2009-02-25 13:23:22 +0000682 if (outer_scope_is_eval_scope_) {
683 Indent(n1, "// outer scope is 'eval' scope\n");
684 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000685 if (num_stack_slots_ > 0) { Indent(n1, "// ");
686 PrintF("%d stack slots\n", num_stack_slots_); }
687 if (num_heap_slots_ > 0) { Indent(n1, "// ");
688 PrintF("%d heap slots\n", num_heap_slots_); }
689
690 // Print locals.
691 PrettyPrinter printer;
692 Indent(n1, "// function var\n");
693 if (function_ != NULL) {
694 PrintVar(&printer, n1, function_);
695 }
696
697 Indent(n1, "// temporary vars\n");
698 for (int i = 0; i < temps_.length(); i++) {
699 PrintVar(&printer, n1, temps_[i]);
700 }
701
702 Indent(n1, "// local vars\n");
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000703 PrintMap(&printer, n1, &variables_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000704
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000705 Indent(n1, "// dynamic vars\n");
706 if (dynamics_ != NULL) {
707 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC));
708 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_LOCAL));
709 PrintMap(&printer, n1, dynamics_->GetMap(Variable::DYNAMIC_GLOBAL));
710 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711
712 // Print inner scopes (disable by providing negative n).
713 if (n >= 0) {
714 for (int i = 0; i < inner_scopes_.length(); i++) {
715 PrintF("\n");
716 inner_scopes_[i]->Print(n1);
717 }
718 }
719
720 Indent(n0, "}\n");
721}
722#endif // DEBUG
723
724
ager@chromium.org381abbb2009-02-25 13:23:22 +0000725Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000726 if (dynamics_ == NULL) dynamics_ = new DynamicScopePart();
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000727 VariableMap* map = dynamics_->GetMap(mode);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000728 Variable* var = map->Lookup(name);
729 if (var == NULL) {
730 // Declare a new non-local.
ager@chromium.org3e875802009-06-29 08:26:34 +0000731 var = map->Declare(NULL, name, mode, true, Variable::NORMAL);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000732 // Allocate it by giving it a dynamic lookup.
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000733 var->set_rewrite(NewSlot(var, Slot::LOOKUP, -1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000734 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000735 return var;
736}
737
738
739// Lookup a variable starting with this scope. The result is either
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000740// the statically resolved variable belonging to an outer scope, or
741// NULL. It may be NULL because a) we couldn't find a variable, or b)
742// because the variable is just a guess (and may be shadowed by
743// another variable that is introduced dynamically via an 'eval' call
744// or a 'with' statement).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000745Variable* Scope::LookupRecursive(Handle<String> name,
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000746 bool from_inner_function,
ager@chromium.org381abbb2009-02-25 13:23:22 +0000747 Variable** invalidated_local) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748 // If we find a variable, but the current scope calls 'eval', the found
749 // variable may not be the correct one (the 'eval' may introduce a
750 // property with the same name). In that case, remember that the variable
751 // found is just a guess.
752 bool guess = scope_calls_eval_;
753
754 // Try to find the variable in this scope.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000755 Variable* var = LocalLookup(name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000756
757 if (var != NULL) {
758 // We found a variable. If this is not an inner lookup, we are done.
759 // (Even if there is an 'eval' in this scope which introduces the
760 // same variable again, the resulting variable remains the same.
761 // Note that enclosing 'with' statements are handled at the call site.)
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000762 if (!from_inner_function)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000763 return var;
764
765 } else {
766 // We did not find a variable locally. Check against the function variable,
767 // if any. We can do this for all scopes, since the function variable is
768 // only present - if at all - for function scopes.
769 //
770 // This lookup corresponds to a lookup in the "intermediate" scope sitting
771 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
772 // the name of named function literal is kept in an intermediate scope
ager@chromium.org32912102009-01-16 10:38:43 +0000773 // in between this scope and the next outer scope.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000774 if (function_ != NULL && function_->name().is_identical_to(name)) {
775 var = function_;
776
777 } else if (outer_scope_ != NULL) {
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000778 var = outer_scope_->LookupRecursive(
779 name,
780 is_function_scope() || from_inner_function,
781 invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000782 // We may have found a variable in an outer scope. However, if
783 // the current scope is inside a 'with', the actual variable may
784 // be a property introduced via the 'with' statement. Then, the
785 // variable we may have found is just a guess.
786 if (scope_inside_with_)
787 guess = true;
788 }
789
790 // If we did not find a variable, we are done.
791 if (var == NULL)
792 return NULL;
793 }
794
795 ASSERT(var != NULL);
796
797 // If this is a lookup from an inner scope, mark the variable.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000798 if (from_inner_function) {
799 var->MarkAsAccessedFromInnerFunctionScope();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000800 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000801
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000802 // If the variable we have found is just a guess, invalidate the
803 // result. If the found variable is local, record that fact so we
804 // can generate fast code to get it if it is not shadowed by eval.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000805 if (guess) {
ager@chromium.orgfe22fc42009-11-05 11:55:54 +0000806 if (!var->is_global()) *invalidated_local = var;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000807 var = NULL;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000808 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000809
810 return var;
811}
812
813
ager@chromium.org381abbb2009-02-25 13:23:22 +0000814void Scope::ResolveVariable(Scope* global_scope,
815 Handle<Context> context,
816 VariableProxy* proxy) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000817 ASSERT(global_scope == NULL || global_scope->is_global_scope());
818
819 // If the proxy is already resolved there's nothing to do
820 // (functions and consts may be resolved by the parser).
821 if (proxy->var() != NULL) return;
822
823 // Otherwise, try to resolve the variable.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000824 Variable* invalidated_local = NULL;
825 Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000826
827 if (proxy->inside_with()) {
828 // If we are inside a local 'with' statement, all bets are off
829 // and we cannot resolve the proxy to a local variable even if
830 // we found an outer matching variable.
831 // Note that we must do a lookup anyway, because if we find one,
832 // we must mark that variable as potentially accessed from this
833 // inner scope (the property may not be in the 'with' object).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000834 var = NonLocal(proxy->name(), Variable::DYNAMIC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000835
836 } else {
837 // We are not inside a local 'with' statement.
838
839 if (var == NULL) {
840 // We did not find the variable. We have a global variable
841 // if we are in the global scope (we know already that we
842 // are outside a 'with' statement) or if there is no way
843 // that the variable might be introduced dynamically (through
844 // a local or outer eval() call, or an outer 'with' statement),
845 // or we don't know about the outer scope (because we are
846 // in an eval scope).
ager@chromium.org381abbb2009-02-25 13:23:22 +0000847 if (is_global_scope() ||
848 !(scope_inside_with_ || outer_scope_is_eval_scope_ ||
849 scope_calls_eval_ || outer_scope_calls_eval_)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000850 // We must have a global variable.
851 ASSERT(global_scope != NULL);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000852 var = global_scope->DeclareGlobal(proxy->name());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000853
854 } else if (scope_inside_with_) {
855 // If we are inside a with statement we give up and look up
856 // the variable at runtime.
857 var = NonLocal(proxy->name(), Variable::DYNAMIC);
858
859 } else if (invalidated_local != NULL) {
860 // No with statements are involved and we found a local
861 // variable that might be shadowed by eval introduced
862 // variables.
863 var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL);
864 var->set_local_if_not_shadowed(invalidated_local);
865
866 } else if (outer_scope_is_eval_scope_) {
867 // No with statements and we did not find a local and the code
868 // is executed with a call to eval. The context contains
869 // scope information that we can use to determine if the
870 // variable is global if it is not shadowed by eval-introduced
871 // variables.
872 if (context->GlobalIfNotShadowedByEval(proxy->name())) {
873 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
874
875 } else {
876 var = NonLocal(proxy->name(), Variable::DYNAMIC);
877 }
878
879 } else {
880 // No with statements and we did not find a local and the code
881 // is not executed with a call to eval. We know that this
882 // variable is global unless it is shadowed by eval-introduced
883 // variables.
884 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 }
886 }
887 }
888
889 proxy->BindTo(var);
890}
891
892
ager@chromium.org381abbb2009-02-25 13:23:22 +0000893void Scope::ResolveVariablesRecursively(Scope* global_scope,
894 Handle<Context> context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895 ASSERT(global_scope == NULL || global_scope->is_global_scope());
896
897 // Resolve unresolved variables for this scope.
898 for (int i = 0; i < unresolved_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000899 ResolveVariable(global_scope, context, unresolved_[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900 }
901
902 // Resolve unresolved variables for inner scopes.
903 for (int i = 0; i < inner_scopes_.length(); i++) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000904 inner_scopes_[i]->ResolveVariablesRecursively(global_scope, context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905 }
906}
907
908
ager@chromium.org381abbb2009-02-25 13:23:22 +0000909bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval,
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000910 bool outer_scope_calls_non_strict_eval,
ager@chromium.org381abbb2009-02-25 13:23:22 +0000911 bool outer_scope_is_eval_scope) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000912 if (outer_scope_calls_eval) {
913 outer_scope_calls_eval_ = true;
914 }
915
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000916 if (outer_scope_calls_non_strict_eval) {
917 outer_scope_calls_non_strict_eval_ = true;
918 }
919
ager@chromium.org381abbb2009-02-25 13:23:22 +0000920 if (outer_scope_is_eval_scope) {
921 outer_scope_is_eval_scope_ = true;
922 }
923
924 bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_;
925 bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_;
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000926 bool calls_non_strict_eval =
927 (scope_calls_eval_ && !is_strict_mode()) ||
928 outer_scope_calls_non_strict_eval_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929 for (int i = 0; i < inner_scopes_.length(); i++) {
930 Scope* inner_scope = inner_scopes_[i];
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000931 if (inner_scope->PropagateScopeInfo(calls_eval,
932 calls_non_strict_eval,
933 is_eval)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934 inner_scope_calls_eval_ = true;
935 }
936 if (inner_scope->force_eager_compilation_) {
937 force_eager_compilation_ = true;
938 }
939 }
940
941 return scope_calls_eval_ || inner_scope_calls_eval_;
942}
943
944
945bool Scope::MustAllocate(Variable* var) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000946 // Give var a read/write use if there is a chance it might be accessed
947 // via an eval() call. This is only possible if the variable has a
948 // visible name.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000949 if ((var->is_this() || var->name()->length() > 0) &&
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000950 (var->is_accessed_from_inner_function_scope() ||
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000951 scope_calls_eval_ ||
952 inner_scope_calls_eval_ ||
953 scope_contains_with_ ||
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000954 is_catch_scope() ||
955 is_block_scope())) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000956 var->set_is_used(true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000957 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000958 // Global variables do not need to be allocated.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000959 return !var->is_global() && var->is_used();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000960}
961
962
963bool Scope::MustAllocateInContext(Variable* var) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000964 // If var is accessed from an inner scope, or if there is a possibility
965 // that it might be accessed from the current or an inner scope (through
966 // an eval() call or a runtime with lookup), it must be allocated in the
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000967 // context.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000968 //
969 // Exceptions: temporary variables are never allocated in a context;
970 // catch-bound variables are always allocated in a context.
971 if (var->mode() == Variable::TEMPORARY) return false;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000972 if (is_catch_scope() || is_block_scope()) return true;
973 return var->is_accessed_from_inner_function_scope() ||
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000974 scope_calls_eval_ ||
975 inner_scope_calls_eval_ ||
976 scope_contains_with_ ||
977 var->is_global();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978}
979
980
981bool Scope::HasArgumentsParameter() {
982 for (int i = 0; i < params_.length(); i++) {
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000983 if (params_[i]->name().is_identical_to(
984 isolate_->factory()->arguments_symbol())) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000985 return true;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000986 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987 }
988 return false;
989}
990
991
992void Scope::AllocateStackSlot(Variable* var) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000993 var->set_rewrite(NewSlot(var, Slot::LOCAL, num_stack_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000994}
995
996
997void Scope::AllocateHeapSlot(Variable* var) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000998 var->set_rewrite(NewSlot(var, Slot::CONTEXT, num_heap_slots_++));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000999}
1000
1001
1002void Scope::AllocateParameterLocals() {
1003 ASSERT(is_function_scope());
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001004 Variable* arguments = LocalLookup(isolate_->factory()->arguments_symbol());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001005 ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001006
whesse@chromium.org7b260152011-06-20 15:33:18 +00001007 bool uses_nonstrict_arguments = false;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001008
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001009 if (MustAllocate(arguments) && !HasArgumentsParameter()) {
1010 // 'arguments' is used. Unless there is also a parameter called
whesse@chromium.org7b260152011-06-20 15:33:18 +00001011 // 'arguments', we must be conservative and allocate all parameters to
1012 // the context assuming they will be captured by the arguments object.
1013 // If we have a parameter named 'arguments', a (new) value is always
1014 // assigned to it via the function invocation. Then 'arguments' denotes
1015 // that specific parameter value and cannot be used to access the
1016 // parameters, which is why we don't need to allocate an arguments
1017 // object in that case.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001018
1019 // We are using 'arguments'. Tell the code generator that is needs to
1020 // allocate the arguments object by setting 'arguments_'.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001021 arguments_ = arguments;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001023 // In strict mode 'arguments' does not alias formal parameters.
1024 // Therefore in strict mode we allocate parameters as if 'arguments'
1025 // were not used.
whesse@chromium.org7b260152011-06-20 15:33:18 +00001026 uses_nonstrict_arguments = !is_strict_mode();
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001027 }
1028
whesse@chromium.org7b260152011-06-20 15:33:18 +00001029 // The same parameter may occur multiple times in the parameters_ list.
1030 // If it does, and if it is not copied into the context object, it must
1031 // receive the highest parameter index for that parameter; thus iteration
1032 // order is relevant!
1033 for (int i = params_.length() - 1; i >= 0; --i) {
1034 Variable* var = params_[i];
1035 ASSERT(var->scope() == this);
1036 if (uses_nonstrict_arguments) {
1037 // Give the parameter a use from an inner scope, to force allocation
1038 // to the context.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001039 var->MarkAsAccessedFromInnerFunctionScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001040 }
1041
whesse@chromium.org7b260152011-06-20 15:33:18 +00001042 if (MustAllocate(var)) {
1043 if (MustAllocateInContext(var)) {
1044 ASSERT(var->rewrite() == NULL || var->IsContextSlot());
1045 if (var->rewrite() == NULL) {
1046 AllocateHeapSlot(var);
1047 }
1048 } else {
1049 ASSERT(var->rewrite() == NULL || var->IsParameter());
1050 if (var->rewrite() == NULL) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001051 var->set_rewrite(NewSlot(var, Slot::PARAMETER, i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001052 }
1053 }
1054 }
1055 }
1056}
1057
1058
1059void Scope::AllocateNonParameterLocal(Variable* var) {
1060 ASSERT(var->scope() == this);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001061 ASSERT(var->rewrite() == NULL ||
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001062 !var->IsVariable(isolate_->factory()->result_symbol()) ||
whesse@chromium.org7b260152011-06-20 15:33:18 +00001063 var->AsSlot() == NULL ||
1064 var->AsSlot()->type() != Slot::LOCAL);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001065 if (var->rewrite() == NULL && MustAllocate(var)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001066 if (MustAllocateInContext(var)) {
1067 AllocateHeapSlot(var);
1068 } else {
1069 AllocateStackSlot(var);
1070 }
1071 }
1072}
1073
1074
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001075void Scope::AllocateNonParameterLocals() {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001076 // All variables that have no rewrite yet are non-parameter locals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077 for (int i = 0; i < temps_.length(); i++) {
1078 AllocateNonParameterLocal(temps_[i]);
1079 }
1080
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001081 for (VariableMap::Entry* p = variables_.Start();
1082 p != NULL;
1083 p = variables_.Next(p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001084 Variable* var = reinterpret_cast<Variable*>(p->value);
1085 AllocateNonParameterLocal(var);
1086 }
1087
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001088 // For now, function_ must be allocated at the very end. If it gets
1089 // allocated in the context, it must be the last slot in the context,
1090 // because of the current ScopeInfo implementation (see
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001091 // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor).
1092 if (function_ != NULL) {
1093 AllocateNonParameterLocal(function_);
1094 }
1095}
1096
1097
1098void Scope::AllocateVariablesRecursively() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001099 // Allocate variables for inner scopes.
1100 for (int i = 0; i < inner_scopes_.length(); i++) {
1101 inner_scopes_[i]->AllocateVariablesRecursively();
1102 }
1103
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001104 // If scope is already resolved, we still need to allocate
1105 // variables in inner scopes which might not had been resolved yet.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001106 if (already_resolved()) return;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001107 // The number of slots required for variables.
1108 num_stack_slots_ = 0;
1109 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
1110
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001111 // Allocate variables for this scope.
1112 // Parameters must be allocated first, if any.
1113 if (is_function_scope()) AllocateParameterLocals();
1114 AllocateNonParameterLocals();
1115
1116 // Allocate context if necessary.
1117 bool must_have_local_context = false;
1118 if (scope_calls_eval_ || scope_contains_with_) {
1119 // The context for the eval() call or 'with' statement in this scope.
1120 // Unless we are in the global or an eval scope, we need a local
1121 // context even if we didn't statically allocate any locals in it,
1122 // and the compiler will access the context variable. If we are
1123 // not in an inner scope, the scope is provided from the outside.
1124 must_have_local_context = is_function_scope();
1125 }
1126
1127 // If we didn't allocate any locals in the local context, then we only
1128 // need the minimal number of slots if we must have a local context.
1129 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS &&
1130 !must_have_local_context) {
1131 num_heap_slots_ = 0;
1132 }
1133
1134 // Allocation done.
1135 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
1136}
1137
1138} } // namespace v8::internal