blob: ad8b6a5e5e072dffc5ea95608f96e6a96b83b323 [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
Ben Murdochf87a2032010-10-22 12:50:53 +010030#include "scopes.h"
31
32#include "bootstrapper.h"
33#include "compiler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "scopeinfo.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000035
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000036#include "allocation-inl.h"
37
Steve Blocka7e24c12009-10-30 11:49:00 +000038namespace v8 {
39namespace internal {
40
41// ----------------------------------------------------------------------------
42// A Zone allocator for use with LocalsMap.
43
Steve Block44f0eee2011-05-26 01:26:41 +010044// TODO(isolates): It is probably worth it to change the Allocator class to
45// take a pointer to an isolate.
Steve Blocka7e24c12009-10-30 11:49:00 +000046class ZoneAllocator: public Allocator {
47 public:
48 /* nothing to do */
49 virtual ~ZoneAllocator() {}
50
Steve Block44f0eee2011-05-26 01:26:41 +010051 virtual void* New(size_t size) { return ZONE->New(static_cast<int>(size)); }
Steve Blocka7e24c12009-10-30 11:49:00 +000052
53 /* ignored - Zone is freed in one fell swoop */
54 virtual void Delete(void* p) {}
55};
56
57
Ben Murdoch592a9fc2012-03-05 11:04:45 +000058static ZoneAllocator* LocalsMapAllocator = ::new ZoneAllocator();
Steve Blocka7e24c12009-10-30 11:49:00 +000059
60
61// ----------------------------------------------------------------------------
62// Implementation of LocalsMap
63//
64// Note: We are storing the handle locations as key values in the hash map.
65// When inserting a new variable via Declare(), we rely on the fact that
66// the handle location remains alive for the duration of that variable
67// use. Because a Variable holding a handle with the same location exists
68// this is ensured.
69
70static bool Match(void* key1, void* key2) {
71 String* name1 = *reinterpret_cast<String**>(key1);
72 String* name2 = *reinterpret_cast<String**>(key2);
73 ASSERT(name1->IsSymbol());
74 ASSERT(name2->IsSymbol());
75 return name1 == name2;
76}
77
78
Ben Murdoch592a9fc2012-03-05 11:04:45 +000079VariableMap::VariableMap() : HashMap(Match, LocalsMapAllocator, 8) {}
Steve Blocka7e24c12009-10-30 11:49:00 +000080VariableMap::~VariableMap() {}
81
82
Ben Murdoch592a9fc2012-03-05 11:04:45 +000083Variable* VariableMap::Declare(
84 Scope* scope,
85 Handle<String> name,
86 VariableMode mode,
87 bool is_valid_lhs,
88 Variable::Kind kind,
89 InitializationFlag initialization_flag) {
Steve Blocka7e24c12009-10-30 11:49:00 +000090 HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), true);
91 if (p->value == NULL) {
92 // The variable has not been declared yet -> insert it.
93 ASSERT(p->key == name.location());
Ben Murdoch592a9fc2012-03-05 11:04:45 +000094 p->value = new Variable(scope,
95 name,
96 mode,
97 is_valid_lhs,
98 kind,
99 initialization_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000100 }
101 return reinterpret_cast<Variable*>(p->value);
102}
103
104
105Variable* VariableMap::Lookup(Handle<String> name) {
106 HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), false);
107 if (p != NULL) {
108 ASSERT(*reinterpret_cast<String**>(p->key) == *name);
109 ASSERT(p->value != NULL);
110 return reinterpret_cast<Variable*>(p->value);
111 }
112 return NULL;
113}
114
115
116// ----------------------------------------------------------------------------
117// Implementation of Scope
118
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000119Scope::Scope(Scope* outer_scope, ScopeType type)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000120 : isolate_(Isolate::Current()),
121 inner_scopes_(4),
122 variables_(),
123 temps_(4),
124 params_(4),
125 unresolved_(16),
126 decls_(4),
127 already_resolved_(false) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000128 SetDefaults(type, outer_scope, Handle<ScopeInfo>::null());
Steve Blocka7e24c12009-10-30 11:49:00 +0000129 // At some point we might want to provide outer scopes to
130 // eval scopes (by walking the stack and reading the scope info).
131 // In that case, the ASSERT below needs to be adjusted.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000132 ASSERT_EQ(type == GLOBAL_SCOPE, outer_scope == NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000133 ASSERT(!HasIllegalRedeclaration());
134}
135
136
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000137Scope::Scope(Scope* inner_scope,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000138 ScopeType type,
139 Handle<ScopeInfo> scope_info)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000140 : isolate_(Isolate::Current()),
141 inner_scopes_(4),
142 variables_(),
143 temps_(4),
144 params_(4),
145 unresolved_(16),
146 decls_(4),
147 already_resolved_(true) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000148 SetDefaults(type, NULL, scope_info);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000149 if (!scope_info.is_null()) {
150 num_heap_slots_ = scope_info_->ContextLength();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100151 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000152 // Ensure at least MIN_CONTEXT_SLOTS to indicate a materialized context.
153 num_heap_slots_ = Max(num_heap_slots_,
154 static_cast<int>(Context::MIN_CONTEXT_SLOTS));
Steve Block44f0eee2011-05-26 01:26:41 +0100155 AddInnerScope(inner_scope);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000156}
Steve Block44f0eee2011-05-26 01:26:41 +0100157
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000158
159Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name)
160 : isolate_(Isolate::Current()),
161 inner_scopes_(1),
162 variables_(),
163 temps_(0),
164 params_(0),
165 unresolved_(0),
166 decls_(0),
167 already_resolved_(true) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000168 SetDefaults(CATCH_SCOPE, NULL, Handle<ScopeInfo>::null());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000169 AddInnerScope(inner_scope);
170 ++num_var_or_const_;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000171 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000172 Variable* variable = variables_.Declare(this,
173 catch_variable_name,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000174 VAR,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000175 true, // Valid left-hand side.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000176 Variable::NORMAL,
177 kCreatedInitialized);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000178 AllocateHeapSlot(variable);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100179}
180
181
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000182void Scope::SetDefaults(ScopeType type,
Ben Murdoch8b112d22011-06-08 16:22:53 +0100183 Scope* outer_scope,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000184 Handle<ScopeInfo> scope_info) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100185 outer_scope_ = outer_scope;
186 type_ = type;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000187 scope_name_ = isolate_->factory()->empty_symbol();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100188 dynamics_ = NULL;
189 receiver_ = NULL;
190 function_ = NULL;
191 arguments_ = NULL;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100192 illegal_redecl_ = NULL;
193 scope_inside_with_ = false;
194 scope_contains_with_ = false;
195 scope_calls_eval_ = false;
196 // Inherit the strict mode from the parent scope.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000197 language_mode_ = (outer_scope != NULL)
198 ? outer_scope->language_mode_ : CLASSIC_MODE;
Ben Murdoch257744e2011-11-30 15:57:28 +0000199 outer_scope_calls_non_strict_eval_ = false;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100200 inner_scope_calls_eval_ = false;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100201 force_eager_compilation_ = false;
Steve Block053d10c2011-06-13 19:13:29 +0100202 num_var_or_const_ = 0;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100203 num_stack_slots_ = 0;
204 num_heap_slots_ = 0;
205 scope_info_ = scope_info;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000206 start_position_ = RelocInfo::kNoPosition;
207 end_position_ = RelocInfo::kNoPosition;
208 if (!scope_info.is_null()) {
209 scope_calls_eval_ = scope_info->CallsEval();
210 language_mode_ = scope_info->language_mode();
211 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100212}
213
214
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000215Scope* Scope::DeserializeScopeChain(Context* context, Scope* global_scope) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000216 // Reconstruct the outer scope chain from a closure's context chain.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000217 Scope* current_scope = NULL;
Steve Block44f0eee2011-05-26 01:26:41 +0100218 Scope* innermost_scope = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000219 bool contains_with = false;
220 while (!context->IsGlobalContext()) {
221 if (context->IsWithContext()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000222 Scope* with_scope = new Scope(current_scope,
223 WITH_SCOPE,
224 Handle<ScopeInfo>::null());
225 current_scope = with_scope;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000226 // All the inner scopes are inside a with.
227 contains_with = true;
228 for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) {
229 s->scope_inside_with_ = true;
Steve Block44f0eee2011-05-26 01:26:41 +0100230 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000231 } else if (context->IsFunctionContext()) {
232 ScopeInfo* scope_info = context->closure()->shared()->scope_info();
233 current_scope = new Scope(current_scope,
234 FUNCTION_SCOPE,
235 Handle<ScopeInfo>(scope_info));
236 } else if (context->IsBlockContext()) {
237 ScopeInfo* scope_info = ScopeInfo::cast(context->extension());
238 current_scope = new Scope(current_scope,
239 BLOCK_SCOPE,
240 Handle<ScopeInfo>(scope_info));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000241 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000242 ASSERT(context->IsCatchContext());
243 String* name = String::cast(context->extension());
244 current_scope = new Scope(current_scope, Handle<String>(name));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000245 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000246 if (contains_with) current_scope->RecordWithStatement();
247 if (innermost_scope == NULL) innermost_scope = current_scope;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000248
249 // Forget about a with when we move to a context for a different function.
250 if (context->previous()->closure() != context->closure()) {
251 contains_with = false;
252 }
253 context = context->previous();
Steve Block44f0eee2011-05-26 01:26:41 +0100254 }
255
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000256 global_scope->AddInnerScope(current_scope);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000257 global_scope->PropagateScopeInfo(false);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000258 return (innermost_scope == NULL) ? global_scope : innermost_scope;
Steve Block44f0eee2011-05-26 01:26:41 +0100259}
260
Ben Murdochb8e0da22011-05-16 14:20:40 +0100261
Ben Murdochf87a2032010-10-22 12:50:53 +0100262bool Scope::Analyze(CompilationInfo* info) {
263 ASSERT(info->function() != NULL);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000264 Scope* scope = info->function()->scope();
265 Scope* top = scope;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100266
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000267 // Traverse the scope tree up to the first unresolved scope or the global
268 // scope and start scope resolution and variable allocation from that scope.
269 while (!top->is_global_scope() &&
270 !top->outer_scope()->already_resolved()) {
271 top = top->outer_scope();
272 }
273
274 // Allocated the variables.
275 top->AllocateVariables(info->global_scope());
Ben Murdochf87a2032010-10-22 12:50:53 +0100276
277#ifdef DEBUG
Steve Block44f0eee2011-05-26 01:26:41 +0100278 if (info->isolate()->bootstrapper()->IsActive()
Ben Murdochf87a2032010-10-22 12:50:53 +0100279 ? FLAG_print_builtin_scopes
280 : FLAG_print_scopes) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000281 scope->Print();
Ben Murdochf87a2032010-10-22 12:50:53 +0100282 }
283#endif
284
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000285 info->SetScope(scope);
Ben Murdochf87a2032010-10-22 12:50:53 +0100286 return true; // Can not fail.
287}
288
289
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000290void Scope::Initialize() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000291 ASSERT(!already_resolved());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100292
Steve Blocka7e24c12009-10-30 11:49:00 +0000293 // Add this scope as a new inner scope of the outer scope.
294 if (outer_scope_ != NULL) {
295 outer_scope_->inner_scopes_.Add(this);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000296 scope_inside_with_ = outer_scope_->scope_inside_with_ || is_with_scope();
Steve Blocka7e24c12009-10-30 11:49:00 +0000297 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000298 scope_inside_with_ = is_with_scope();
Steve Blocka7e24c12009-10-30 11:49:00 +0000299 }
300
301 // Declare convenience variables.
302 // Declare and allocate receiver (even for the global scope, and even
303 // if naccesses_ == 0).
304 // NOTE: When loading parameters in the global scope, we must take
305 // care not to access them as properties of the global object, but
306 // instead load them directly from the stack. Currently, the only
307 // such parameter is 'this' which is passed on the stack when
308 // invoking scripts
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000309 if (is_declaration_scope()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000310 Variable* var =
311 variables_.Declare(this,
312 isolate_->factory()->this_symbol(),
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000313 VAR,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000314 false,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000315 Variable::THIS,
316 kCreatedInitialized);
Ben Murdoch589d6972011-11-30 16:04:58 +0000317 var->AllocateTo(Variable::PARAMETER, -1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000318 receiver_ = var;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000319 } else {
320 ASSERT(outer_scope() != NULL);
321 receiver_ = outer_scope()->receiver();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000322 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000323
324 if (is_function_scope()) {
325 // Declare 'arguments' variable which exists in all functions.
326 // Note that it might never be accessed, in which case it won't be
327 // allocated during variable allocation.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000328 variables_.Declare(this,
329 isolate_->factory()->arguments_symbol(),
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000330 VAR,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000331 true,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000332 Variable::ARGUMENTS,
333 kCreatedInitialized);
Steve Blocka7e24c12009-10-30 11:49:00 +0000334 }
335}
336
337
Ben Murdoch589d6972011-11-30 16:04:58 +0000338Scope* Scope::FinalizeBlockScope() {
339 ASSERT(is_block_scope());
340 ASSERT(temps_.is_empty());
341 ASSERT(params_.is_empty());
342
343 if (num_var_or_const() > 0) return this;
344
345 // Remove this scope from outer scope.
346 for (int i = 0; i < outer_scope_->inner_scopes_.length(); i++) {
347 if (outer_scope_->inner_scopes_[i] == this) {
348 outer_scope_->inner_scopes_.Remove(i);
349 break;
350 }
351 }
352
353 // Reparent inner scopes.
354 for (int i = 0; i < inner_scopes_.length(); i++) {
355 outer_scope()->AddInnerScope(inner_scopes_[i]);
356 }
357
358 // Move unresolved variables
359 for (int i = 0; i < unresolved_.length(); i++) {
360 outer_scope()->unresolved_.Add(unresolved_[i]);
361 }
362
363 return NULL;
364}
365
366
Steve Blocka7e24c12009-10-30 11:49:00 +0000367Variable* Scope::LocalLookup(Handle<String> name) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100368 Variable* result = variables_.Lookup(name);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000369 if (result != NULL || scope_info_.is_null()) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100370 return result;
371 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000372 // If we have a serialized scope info, we might find the variable there.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000373 // There should be no local slot with the given name.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100374 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
375
376 // Check context slot lookup.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000377 VariableMode mode;
378 InitializationFlag init_flag;
379 int index = scope_info_->ContextSlotIndex(*name, &mode, &init_flag);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000380 if (index < 0) {
381 // Check parameters.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000382 mode = VAR;
383 init_flag = kCreatedInitialized;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000384 index = scope_info_->ParameterIndex(*name);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000385 if (index < 0) return NULL;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100386 }
387
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000388 Variable* var =
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000389 variables_.Declare(this,
390 name,
391 mode,
392 true,
393 Variable::NORMAL,
394 init_flag);
Ben Murdoch589d6972011-11-30 16:04:58 +0000395 var->AllocateTo(Variable::CONTEXT, index);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000396 return var;
Steve Blocka7e24c12009-10-30 11:49:00 +0000397}
398
399
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000400Variable* Scope::LookupFunctionVar(Handle<String> name) {
401 if (function_ != NULL && function_->name().is_identical_to(name)) {
402 return function_->var();
403 } else if (!scope_info_.is_null()) {
404 // If we are backed by a scope info, try to lookup the variable there.
405 VariableMode mode;
406 int index = scope_info_->FunctionContextSlotIndex(*name, &mode);
407 if (index < 0) return NULL;
408 Variable* var = DeclareFunctionVar(name, mode);
409 var->AllocateTo(Variable::CONTEXT, index);
410 return var;
411 } else {
412 return NULL;
413 }
414}
415
416
Steve Blocka7e24c12009-10-30 11:49:00 +0000417Variable* Scope::Lookup(Handle<String> name) {
418 for (Scope* scope = this;
419 scope != NULL;
420 scope = scope->outer_scope()) {
421 Variable* var = scope->LocalLookup(name);
422 if (var != NULL) return var;
423 }
424 return NULL;
425}
426
427
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000428Variable* Scope::DeclareFunctionVar(Handle<String> name, VariableMode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000429 ASSERT(is_function_scope() && function_ == NULL);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000430 Variable* function_var = new Variable(
431 this, name, mode, true, Variable::NORMAL, kCreatedInitialized);
Ben Murdoch589d6972011-11-30 16:04:58 +0000432 function_ = new(isolate_->zone()) VariableProxy(isolate_, function_var);
433 return function_var;
Steve Blocka7e24c12009-10-30 11:49:00 +0000434}
435
436
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000437void Scope::DeclareParameter(Handle<String> name, VariableMode mode) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000438 ASSERT(!already_resolved());
439 ASSERT(is_function_scope());
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000440 Variable* var = variables_.Declare(
441 this, name, mode, true, Variable::NORMAL, kCreatedInitialized);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000442 params_.Add(var);
443}
444
445
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000446Variable* Scope::DeclareLocal(Handle<String> name,
447 VariableMode mode,
448 InitializationFlag init_flag) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000449 ASSERT(!already_resolved());
450 // This function handles VAR and CONST modes. DYNAMIC variables are
451 // introduces during variable allocation, INTERNAL variables are allocated
452 // explicitly, and TEMPORARY variables are allocated via NewTemporary().
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000453 ASSERT(mode == VAR ||
454 mode == CONST ||
455 mode == CONST_HARMONY ||
456 mode == LET);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000457 ++num_var_or_const_;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000458 return
459 variables_.Declare(this, name, mode, true, Variable::NORMAL, init_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000460}
461
462
463Variable* Scope::DeclareGlobal(Handle<String> name) {
464 ASSERT(is_global_scope());
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000465 return variables_.Declare(this,
466 name,
467 DYNAMIC_GLOBAL,
Ben Murdoch589d6972011-11-30 16:04:58 +0000468 true,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000469 Variable::NORMAL,
470 kCreatedInitialized);
Steve Blocka7e24c12009-10-30 11:49:00 +0000471}
472
473
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000474VariableProxy* Scope::NewUnresolved(Handle<String> name, int position) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000475 // Note that we must not share the unresolved variables with
476 // the same name because they may be removed selectively via
477 // RemoveUnresolved().
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000478 ASSERT(!already_resolved());
479 VariableProxy* proxy = new(isolate_->zone()) VariableProxy(
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000480 isolate_, name, false, position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000481 unresolved_.Add(proxy);
482 return proxy;
483}
484
485
486void Scope::RemoveUnresolved(VariableProxy* var) {
487 // Most likely (always?) any variable we want to remove
488 // was just added before, so we search backwards.
489 for (int i = unresolved_.length(); i-- > 0;) {
490 if (unresolved_[i] == var) {
491 unresolved_.Remove(i);
492 return;
493 }
494 }
495}
496
497
Ben Murdochb0fe1622011-05-05 13:52:32 +0100498Variable* Scope::NewTemporary(Handle<String> name) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000499 ASSERT(!already_resolved());
Ben Murdoch589d6972011-11-30 16:04:58 +0000500 Variable* var = new Variable(this,
501 name,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000502 TEMPORARY,
Ben Murdoch589d6972011-11-30 16:04:58 +0000503 true,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000504 Variable::NORMAL,
505 kCreatedInitialized);
Steve Blocka7e24c12009-10-30 11:49:00 +0000506 temps_.Add(var);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100507 return var;
Steve Blocka7e24c12009-10-30 11:49:00 +0000508}
509
510
511void Scope::AddDeclaration(Declaration* declaration) {
512 decls_.Add(declaration);
513}
514
515
516void Scope::SetIllegalRedeclaration(Expression* expression) {
Steve Block1e0659c2011-05-24 12:43:12 +0100517 // Record only the first illegal redeclaration.
Steve Blocka7e24c12009-10-30 11:49:00 +0000518 if (!HasIllegalRedeclaration()) {
519 illegal_redecl_ = expression;
520 }
521 ASSERT(HasIllegalRedeclaration());
522}
523
524
525void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) {
526 ASSERT(HasIllegalRedeclaration());
527 illegal_redecl_->Accept(visitor);
528}
529
530
Ben Murdoch589d6972011-11-30 16:04:58 +0000531Declaration* Scope::CheckConflictingVarDeclarations() {
532 int length = decls_.length();
533 for (int i = 0; i < length; i++) {
534 Declaration* decl = decls_[i];
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000535 if (decl->mode() != VAR) continue;
Ben Murdoch589d6972011-11-30 16:04:58 +0000536 Handle<String> name = decl->proxy()->name();
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000537
538 // Iterate through all scopes until and including the declaration scope.
539 Scope* previous = NULL;
540 Scope* current = decl->scope();
541 do {
Ben Murdoch589d6972011-11-30 16:04:58 +0000542 // There is a conflict if there exists a non-VAR binding.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000543 Variable* other_var = current->variables_.Lookup(name);
544 if (other_var != NULL && other_var->mode() != VAR) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000545 return decl;
546 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000547 previous = current;
548 current = current->outer_scope_;
549 } while (!previous->is_declaration_scope());
Ben Murdoch589d6972011-11-30 16:04:58 +0000550 }
551 return NULL;
552}
553
554
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000555void Scope::CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals,
556 ZoneList<Variable*>* context_locals) {
557 ASSERT(stack_locals != NULL);
558 ASSERT(context_locals != NULL);
559
560 // Collect temporaries which are always allocated on the stack.
Steve Blocka7e24c12009-10-30 11:49:00 +0000561 for (int i = 0; i < temps_.length(); i++) {
562 Variable* var = temps_[i];
Steve Block6ded16b2010-05-10 14:33:55 +0100563 if (var->is_used()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000564 ASSERT(var->IsStackLocal());
565 stack_locals->Add(var);
Steve Blocka7e24c12009-10-30 11:49:00 +0000566 }
567 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000568
569 // Collect declared local variables.
Steve Blocka7e24c12009-10-30 11:49:00 +0000570 for (VariableMap::Entry* p = variables_.Start();
571 p != NULL;
572 p = variables_.Next(p)) {
573 Variable* var = reinterpret_cast<Variable*>(p->value);
Steve Block6ded16b2010-05-10 14:33:55 +0100574 if (var->is_used()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000575 if (var->IsStackLocal()) {
576 stack_locals->Add(var);
577 } else if (var->IsContextSlot()) {
578 context_locals->Add(var);
579 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000580 }
581 }
582}
583
584
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000585void Scope::AllocateVariables(Scope* global_scope) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000586 // 1) Propagate scope information.
Ben Murdoch257744e2011-11-30 15:57:28 +0000587 bool outer_scope_calls_non_strict_eval = false;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000588 if (outer_scope_ != NULL) {
589 outer_scope_calls_non_strict_eval =
590 outer_scope_->outer_scope_calls_non_strict_eval() |
591 outer_scope_->calls_non_strict_eval();
Ben Murdoch257744e2011-11-30 15:57:28 +0000592 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000593 PropagateScopeInfo(outer_scope_calls_non_strict_eval);
Steve Blocka7e24c12009-10-30 11:49:00 +0000594
595 // 2) Resolve variables.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000596 ResolveVariablesRecursively(global_scope);
Steve Blocka7e24c12009-10-30 11:49:00 +0000597
598 // 3) Allocate variables.
599 AllocateVariablesRecursively();
600}
601
602
603bool Scope::AllowsLazyCompilation() const {
604 return !force_eager_compilation_ && HasTrivialOuterContext();
605}
606
607
608bool Scope::HasTrivialContext() const {
609 // A function scope has a trivial context if it always is the global
610 // context. We iteratively scan out the context chain to see if
611 // there is anything that makes this scope non-trivial; otherwise we
612 // return true.
613 for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) {
614 if (scope->is_eval_scope()) return false;
615 if (scope->scope_inside_with_) return false;
616 if (scope->num_heap_slots_ > 0) return false;
617 }
618 return true;
619}
620
621
622bool Scope::HasTrivialOuterContext() const {
623 Scope* outer = outer_scope_;
624 if (outer == NULL) return true;
625 // Note that the outer context may be trivial in general, but the current
626 // scope may be inside a 'with' statement in which case the outer context
627 // for this scope is not trivial.
628 return !scope_inside_with_ && outer->HasTrivialContext();
629}
630
631
632int Scope::ContextChainLength(Scope* scope) {
633 int n = 0;
634 for (Scope* s = this; s != scope; s = s->outer_scope_) {
635 ASSERT(s != NULL); // scope must be in the scope chain
636 if (s->num_heap_slots() > 0) n++;
637 }
638 return n;
639}
640
641
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000642Scope* Scope::DeclarationScope() {
643 Scope* scope = this;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000644 while (!scope->is_declaration_scope()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000645 scope = scope->outer_scope();
646 }
647 return scope;
648}
649
650
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000651Handle<ScopeInfo> Scope::GetScopeInfo() {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000652 if (scope_info_.is_null()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000653 scope_info_ = ScopeInfo::Create(this);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000654 }
655 return scope_info_;
656}
657
658
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000659void Scope::GetNestedScopeChain(
660 List<Handle<ScopeInfo> >* chain,
661 int position) {
662 if (!is_eval_scope()) chain->Add(Handle<ScopeInfo>(GetScopeInfo()));
663
664 for (int i = 0; i < inner_scopes_.length(); i++) {
665 Scope* scope = inner_scopes_[i];
666 int beg_pos = scope->start_position();
667 int end_pos = scope->end_position();
668 ASSERT(beg_pos >= 0 && end_pos >= 0);
669 if (beg_pos <= position && position < end_pos) {
670 scope->GetNestedScopeChain(chain, position);
671 return;
672 }
673 }
674}
675
676
Steve Blocka7e24c12009-10-30 11:49:00 +0000677#ifdef DEBUG
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000678static const char* Header(ScopeType type) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000679 switch (type) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000680 case EVAL_SCOPE: return "eval";
681 case FUNCTION_SCOPE: return "function";
682 case GLOBAL_SCOPE: return "global";
683 case CATCH_SCOPE: return "catch";
684 case BLOCK_SCOPE: return "block";
685 case WITH_SCOPE: return "with";
Steve Blocka7e24c12009-10-30 11:49:00 +0000686 }
687 UNREACHABLE();
688 return NULL;
689}
690
691
692static void Indent(int n, const char* str) {
693 PrintF("%*s%s", n, "", str);
694}
695
696
697static void PrintName(Handle<String> name) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000698 SmartArrayPointer<char> s = name->ToCString(DISALLOW_NULLS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000699 PrintF("%s", *s);
700}
701
702
Ben Murdoch589d6972011-11-30 16:04:58 +0000703static void PrintLocation(Variable* var) {
704 switch (var->location()) {
705 case Variable::UNALLOCATED:
706 break;
707 case Variable::PARAMETER:
708 PrintF("parameter[%d]", var->index());
709 break;
710 case Variable::LOCAL:
711 PrintF("local[%d]", var->index());
712 break;
713 case Variable::CONTEXT:
714 PrintF("context[%d]", var->index());
715 break;
716 case Variable::LOOKUP:
717 PrintF("lookup");
718 break;
719 }
720}
721
722
723static void PrintVar(int indent, Variable* var) {
724 if (var->is_used() || !var->IsUnallocated()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000725 Indent(indent, Variable::Mode2String(var->mode()));
726 PrintF(" ");
727 PrintName(var->name());
728 PrintF("; // ");
Ben Murdoch589d6972011-11-30 16:04:58 +0000729 PrintLocation(var);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000730 if (var->has_forced_context_allocation()) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000731 if (!var->IsUnallocated()) PrintF(", ");
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000732 PrintF("forced context allocation");
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000733 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000734 PrintF("\n");
735 }
736}
737
738
Ben Murdoch589d6972011-11-30 16:04:58 +0000739static void PrintMap(int indent, VariableMap* map) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000740 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
741 Variable* var = reinterpret_cast<Variable*>(p->value);
Ben Murdoch589d6972011-11-30 16:04:58 +0000742 PrintVar(indent, var);
Steve Blocka7e24c12009-10-30 11:49:00 +0000743 }
744}
745
746
747void Scope::Print(int n) {
748 int n0 = (n > 0 ? n : 0);
749 int n1 = n0 + 2; // indentation
750
751 // Print header.
752 Indent(n0, Header(type_));
753 if (scope_name_->length() > 0) {
754 PrintF(" ");
755 PrintName(scope_name_);
756 }
757
758 // Print parameters, if any.
759 if (is_function_scope()) {
760 PrintF(" (");
761 for (int i = 0; i < params_.length(); i++) {
762 if (i > 0) PrintF(", ");
763 PrintName(params_[i]->name());
764 }
765 PrintF(")");
766 }
767
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000768 PrintF(" { // (%d, %d)\n", start_position(), end_position());
Steve Blocka7e24c12009-10-30 11:49:00 +0000769
770 // Function name, if any (named function literals, only).
771 if (function_ != NULL) {
772 Indent(n1, "// (local) function name: ");
773 PrintName(function_->name());
774 PrintF("\n");
775 }
776
777 // Scope info.
778 if (HasTrivialOuterContext()) {
779 Indent(n1, "// scope has trivial outer context\n");
780 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000781 switch (language_mode()) {
782 case CLASSIC_MODE:
783 break;
784 case STRICT_MODE:
785 Indent(n1, "// strict mode scope\n");
786 break;
787 case EXTENDED_MODE:
788 Indent(n1, "// extended mode scope\n");
789 break;
790 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000791 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
792 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n");
793 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n");
Ben Murdoch257744e2011-11-30 15:57:28 +0000794 if (outer_scope_calls_non_strict_eval_) {
795 Indent(n1, "// outer scope calls 'eval' in non-strict context\n");
796 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000797 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000798 if (num_stack_slots_ > 0) { Indent(n1, "// ");
799 PrintF("%d stack slots\n", num_stack_slots_); }
800 if (num_heap_slots_ > 0) { Indent(n1, "// ");
801 PrintF("%d heap slots\n", num_heap_slots_); }
802
803 // Print locals.
Steve Blocka7e24c12009-10-30 11:49:00 +0000804 Indent(n1, "// function var\n");
805 if (function_ != NULL) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000806 PrintVar(n1, function_->var());
Steve Blocka7e24c12009-10-30 11:49:00 +0000807 }
808
809 Indent(n1, "// temporary vars\n");
810 for (int i = 0; i < temps_.length(); i++) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000811 PrintVar(n1, temps_[i]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000812 }
813
814 Indent(n1, "// local vars\n");
Ben Murdoch589d6972011-11-30 16:04:58 +0000815 PrintMap(n1, &variables_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000816
817 Indent(n1, "// dynamic vars\n");
818 if (dynamics_ != NULL) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000819 PrintMap(n1, dynamics_->GetMap(DYNAMIC));
820 PrintMap(n1, dynamics_->GetMap(DYNAMIC_LOCAL));
821 PrintMap(n1, dynamics_->GetMap(DYNAMIC_GLOBAL));
Steve Blocka7e24c12009-10-30 11:49:00 +0000822 }
823
824 // Print inner scopes (disable by providing negative n).
825 if (n >= 0) {
826 for (int i = 0; i < inner_scopes_.length(); i++) {
827 PrintF("\n");
828 inner_scopes_[i]->Print(n1);
829 }
830 }
831
832 Indent(n0, "}\n");
833}
834#endif // DEBUG
835
836
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000837Variable* Scope::NonLocal(Handle<String> name, VariableMode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000838 if (dynamics_ == NULL) dynamics_ = new DynamicScopePart();
839 VariableMap* map = dynamics_->GetMap(mode);
840 Variable* var = map->Lookup(name);
841 if (var == NULL) {
842 // Declare a new non-local.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000843 InitializationFlag init_flag = (mode == VAR)
844 ? kCreatedInitialized : kNeedsInitialization;
845 var = map->Declare(NULL,
846 name,
847 mode,
848 true,
849 Variable::NORMAL,
850 init_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000851 // Allocate it by giving it a dynamic lookup.
Ben Murdoch589d6972011-11-30 16:04:58 +0000852 var->AllocateTo(Variable::LOOKUP, -1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000853 }
854 return var;
855}
856
857
Steve Blocka7e24c12009-10-30 11:49:00 +0000858Variable* Scope::LookupRecursive(Handle<String> name,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000859 BindingKind* binding_kind) {
860 ASSERT(binding_kind != NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000861 // Try to find the variable in this scope.
862 Variable* var = LocalLookup(name);
863
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000864 // We found a variable and we are done. (Even if there is an 'eval' in
865 // this scope which introduces the same variable again, the resulting
866 // variable remains the same.)
Steve Blocka7e24c12009-10-30 11:49:00 +0000867 if (var != NULL) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000868 *binding_kind = BOUND;
869 return var;
870 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000871
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000872 // We did not find a variable locally. Check against the function variable,
873 // if any. We can do this for all scopes, since the function variable is
874 // only present - if at all - for function scopes.
875 *binding_kind = UNBOUND;
876 var = LookupFunctionVar(name);
877 if (var != NULL) {
878 *binding_kind = BOUND;
879 } else if (outer_scope_ != NULL) {
880 var = outer_scope_->LookupRecursive(name, binding_kind);
881 if (*binding_kind == BOUND && (is_function_scope() || is_with_scope())) {
882 var->ForceContextAllocation();
Steve Blocka7e24c12009-10-30 11:49:00 +0000883 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000884 } else {
885 ASSERT(is_global_scope());
Steve Blocka7e24c12009-10-30 11:49:00 +0000886 }
887
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000888 if (is_with_scope()) {
889 // The current scope is a with scope, so the variable binding can not be
890 // statically resolved. However, note that it was necessary to do a lookup
891 // in the outer scope anyway, because if a binding exists in an outer scope,
892 // the associated variable has to be marked as potentially being accessed
893 // from inside of an inner with scope (the property may not be in the 'with'
894 // object).
895 *binding_kind = DYNAMIC_LOOKUP;
896 return NULL;
897 } else if (calls_non_strict_eval()) {
898 // A variable binding may have been found in an outer scope, but the current
899 // scope makes a non-strict 'eval' call, so the found variable may not be
900 // the correct one (the 'eval' may introduce a binding with the same name).
901 // In that case, change the lookup result to reflect this situation.
902 if (*binding_kind == BOUND) {
903 *binding_kind = BOUND_EVAL_SHADOWED;
904 } else if (*binding_kind == UNBOUND) {
905 *binding_kind = UNBOUND_EVAL_SHADOWED;
906 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100907 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000908 return var;
909}
910
911
912void Scope::ResolveVariable(Scope* global_scope,
Steve Blocka7e24c12009-10-30 11:49:00 +0000913 VariableProxy* proxy) {
914 ASSERT(global_scope == NULL || global_scope->is_global_scope());
915
916 // If the proxy is already resolved there's nothing to do
917 // (functions and consts may be resolved by the parser).
918 if (proxy->var() != NULL) return;
919
920 // Otherwise, try to resolve the variable.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000921 BindingKind binding_kind;
922 Variable* var = LookupRecursive(proxy->name(), &binding_kind);
923 switch (binding_kind) {
924 case BOUND:
925 // We found a variable binding.
926 break;
Steve Blocka7e24c12009-10-30 11:49:00 +0000927
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000928 case BOUND_EVAL_SHADOWED:
929 // We found a variable variable binding that might be shadowed
930 // by 'eval' introduced variable bindings.
931 if (var->is_global()) {
932 var = NonLocal(proxy->name(), DYNAMIC_GLOBAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000933 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000934 Variable* invalidated = var;
935 var = NonLocal(proxy->name(), DYNAMIC_LOCAL);
936 var->set_local_if_not_shadowed(invalidated);
Steve Blocka7e24c12009-10-30 11:49:00 +0000937 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000938 break;
939
940 case UNBOUND:
941 // No binding has been found. Declare a variable in global scope.
942 ASSERT(global_scope != NULL);
943 var = global_scope->DeclareGlobal(proxy->name());
944 break;
945
946 case UNBOUND_EVAL_SHADOWED:
947 // No binding has been found. But some scope makes a
948 // non-strict 'eval' call.
949 var = NonLocal(proxy->name(), DYNAMIC_GLOBAL);
950 break;
951
952 case DYNAMIC_LOOKUP:
953 // The variable could not be resolved statically.
954 var = NonLocal(proxy->name(), DYNAMIC);
955 break;
Steve Blocka7e24c12009-10-30 11:49:00 +0000956 }
957
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000958 ASSERT(var != NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000959 proxy->BindTo(var);
960}
961
962
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000963void Scope::ResolveVariablesRecursively(Scope* global_scope) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000964 ASSERT(global_scope == NULL || global_scope->is_global_scope());
965
966 // Resolve unresolved variables for this scope.
967 for (int i = 0; i < unresolved_.length(); i++) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000968 ResolveVariable(global_scope, unresolved_[i]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000969 }
970
971 // Resolve unresolved variables for inner scopes.
972 for (int i = 0; i < inner_scopes_.length(); i++) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000973 inner_scopes_[i]->ResolveVariablesRecursively(global_scope);
Steve Blocka7e24c12009-10-30 11:49:00 +0000974 }
975}
976
977
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000978bool Scope::PropagateScopeInfo(bool outer_scope_calls_non_strict_eval ) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000979 if (outer_scope_calls_non_strict_eval) {
980 outer_scope_calls_non_strict_eval_ = true;
981 }
982
Ben Murdoch257744e2011-11-30 15:57:28 +0000983 bool calls_non_strict_eval =
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000984 this->calls_non_strict_eval() || outer_scope_calls_non_strict_eval_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000985 for (int i = 0; i < inner_scopes_.length(); i++) {
986 Scope* inner_scope = inner_scopes_[i];
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000987 if (inner_scope->PropagateScopeInfo(calls_non_strict_eval)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000988 inner_scope_calls_eval_ = true;
989 }
990 if (inner_scope->force_eager_compilation_) {
991 force_eager_compilation_ = true;
992 }
993 }
994
995 return scope_calls_eval_ || inner_scope_calls_eval_;
996}
997
998
999bool Scope::MustAllocate(Variable* var) {
1000 // Give var a read/write use if there is a chance it might be accessed
1001 // via an eval() call. This is only possible if the variable has a
1002 // visible name.
1003 if ((var->is_this() || var->name()->length() > 0) &&
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001004 (var->has_forced_context_allocation() ||
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001005 scope_calls_eval_ ||
1006 inner_scope_calls_eval_ ||
1007 scope_contains_with_ ||
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001008 is_catch_scope() ||
1009 is_block_scope())) {
Steve Block6ded16b2010-05-10 14:33:55 +01001010 var->set_is_used(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001011 }
1012 // Global variables do not need to be allocated.
Steve Block6ded16b2010-05-10 14:33:55 +01001013 return !var->is_global() && var->is_used();
Steve Blocka7e24c12009-10-30 11:49:00 +00001014}
1015
1016
1017bool Scope::MustAllocateInContext(Variable* var) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001018 // If var is accessed from an inner scope, or if there is a possibility
1019 // that it might be accessed from the current or an inner scope (through
1020 // an eval() call or a runtime with lookup), it must be allocated in the
Steve Blocka7e24c12009-10-30 11:49:00 +00001021 // context.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001022 //
1023 // Exceptions: temporary variables are never allocated in a context;
1024 // catch-bound variables are always allocated in a context.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001025 if (var->mode() == TEMPORARY) return false;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001026 if (is_catch_scope() || is_block_scope()) return true;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001027 return var->has_forced_context_allocation() ||
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001028 scope_calls_eval_ ||
1029 inner_scope_calls_eval_ ||
1030 scope_contains_with_ ||
1031 var->is_global();
Steve Blocka7e24c12009-10-30 11:49:00 +00001032}
1033
1034
1035bool Scope::HasArgumentsParameter() {
1036 for (int i = 0; i < params_.length(); i++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001037 if (params_[i]->name().is_identical_to(
1038 isolate_->factory()->arguments_symbol())) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001039 return true;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001040 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001041 }
1042 return false;
1043}
1044
1045
1046void Scope::AllocateStackSlot(Variable* var) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001047 var->AllocateTo(Variable::LOCAL, num_stack_slots_++);
Steve Blocka7e24c12009-10-30 11:49:00 +00001048}
1049
1050
1051void Scope::AllocateHeapSlot(Variable* var) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001052 var->AllocateTo(Variable::CONTEXT, num_heap_slots_++);
Steve Blocka7e24c12009-10-30 11:49:00 +00001053}
1054
1055
1056void Scope::AllocateParameterLocals() {
1057 ASSERT(is_function_scope());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001058 Variable* arguments = LocalLookup(isolate_->factory()->arguments_symbol());
Steve Blocka7e24c12009-10-30 11:49:00 +00001059 ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly
Steve Block44f0eee2011-05-26 01:26:41 +01001060
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001061 bool uses_nonstrict_arguments = false;
Steve Block44f0eee2011-05-26 01:26:41 +01001062
Steve Blocka7e24c12009-10-30 11:49:00 +00001063 if (MustAllocate(arguments) && !HasArgumentsParameter()) {
1064 // 'arguments' is used. Unless there is also a parameter called
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001065 // 'arguments', we must be conservative and allocate all parameters to
1066 // the context assuming they will be captured by the arguments object.
1067 // If we have a parameter named 'arguments', a (new) value is always
1068 // assigned to it via the function invocation. Then 'arguments' denotes
1069 // that specific parameter value and cannot be used to access the
1070 // parameters, which is why we don't need to allocate an arguments
1071 // object in that case.
Steve Blocka7e24c12009-10-30 11:49:00 +00001072
1073 // We are using 'arguments'. Tell the code generator that is needs to
1074 // allocate the arguments object by setting 'arguments_'.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001075 arguments_ = arguments;
Steve Blocka7e24c12009-10-30 11:49:00 +00001076
Steve Block44f0eee2011-05-26 01:26:41 +01001077 // In strict mode 'arguments' does not alias formal parameters.
1078 // Therefore in strict mode we allocate parameters as if 'arguments'
1079 // were not used.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001080 uses_nonstrict_arguments = is_classic_mode();
Steve Block44f0eee2011-05-26 01:26:41 +01001081 }
1082
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001083 // The same parameter may occur multiple times in the parameters_ list.
1084 // If it does, and if it is not copied into the context object, it must
1085 // receive the highest parameter index for that parameter; thus iteration
1086 // order is relevant!
1087 for (int i = params_.length() - 1; i >= 0; --i) {
1088 Variable* var = params_[i];
1089 ASSERT(var->scope() == this);
1090 if (uses_nonstrict_arguments) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001091 // Force context allocation of the parameter.
1092 var->ForceContextAllocation();
Steve Blocka7e24c12009-10-30 11:49:00 +00001093 }
1094
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001095 if (MustAllocate(var)) {
1096 if (MustAllocateInContext(var)) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001097 ASSERT(var->IsUnallocated() || var->IsContextSlot());
1098 if (var->IsUnallocated()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001099 AllocateHeapSlot(var);
1100 }
1101 } else {
Ben Murdoch589d6972011-11-30 16:04:58 +00001102 ASSERT(var->IsUnallocated() || var->IsParameter());
1103 if (var->IsUnallocated()) {
1104 var->AllocateTo(Variable::PARAMETER, i);
Steve Blocka7e24c12009-10-30 11:49:00 +00001105 }
1106 }
1107 }
1108 }
1109}
1110
1111
1112void Scope::AllocateNonParameterLocal(Variable* var) {
1113 ASSERT(var->scope() == this);
Ben Murdoch589d6972011-11-30 16:04:58 +00001114 ASSERT(!var->IsVariable(isolate_->factory()->result_symbol()) ||
1115 !var->IsStackLocal());
1116 if (var->IsUnallocated() && MustAllocate(var)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001117 if (MustAllocateInContext(var)) {
1118 AllocateHeapSlot(var);
1119 } else {
1120 AllocateStackSlot(var);
1121 }
1122 }
1123}
1124
1125
1126void Scope::AllocateNonParameterLocals() {
1127 // All variables that have no rewrite yet are non-parameter locals.
1128 for (int i = 0; i < temps_.length(); i++) {
1129 AllocateNonParameterLocal(temps_[i]);
1130 }
1131
1132 for (VariableMap::Entry* p = variables_.Start();
1133 p != NULL;
1134 p = variables_.Next(p)) {
1135 Variable* var = reinterpret_cast<Variable*>(p->value);
1136 AllocateNonParameterLocal(var);
1137 }
1138
1139 // For now, function_ must be allocated at the very end. If it gets
1140 // allocated in the context, it must be the last slot in the context,
1141 // because of the current ScopeInfo implementation (see
1142 // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor).
1143 if (function_ != NULL) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001144 AllocateNonParameterLocal(function_->var());
Steve Blocka7e24c12009-10-30 11:49:00 +00001145 }
1146}
1147
1148
1149void Scope::AllocateVariablesRecursively() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001150 // Allocate variables for inner scopes.
1151 for (int i = 0; i < inner_scopes_.length(); i++) {
1152 inner_scopes_[i]->AllocateVariablesRecursively();
1153 }
1154
Ben Murdochb8e0da22011-05-16 14:20:40 +01001155 // If scope is already resolved, we still need to allocate
1156 // variables in inner scopes which might not had been resolved yet.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001157 if (already_resolved()) return;
Ben Murdochb8e0da22011-05-16 14:20:40 +01001158 // The number of slots required for variables.
1159 num_stack_slots_ = 0;
1160 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
1161
Steve Blocka7e24c12009-10-30 11:49:00 +00001162 // Allocate variables for this scope.
1163 // Parameters must be allocated first, if any.
1164 if (is_function_scope()) AllocateParameterLocals();
1165 AllocateNonParameterLocals();
1166
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001167 // Force allocation of a context for this scope if necessary. For a 'with'
1168 // scope and for a function scope that makes an 'eval' call we need a context,
1169 // even if no local variables were statically allocated in the scope.
1170 bool must_have_context = is_with_scope() ||
1171 (is_function_scope() && calls_eval());
Steve Blocka7e24c12009-10-30 11:49:00 +00001172
1173 // If we didn't allocate any locals in the local context, then we only
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001174 // need the minimal number of slots if we must have a context.
1175 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && !must_have_context) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001176 num_heap_slots_ = 0;
1177 }
1178
1179 // Allocation done.
1180 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
1181}
1182
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001183
1184int Scope::StackLocalCount() const {
1185 return num_stack_slots() -
1186 (function_ != NULL && function_->var()->IsStackLocal() ? 1 : 0);
1187}
1188
1189
1190int Scope::ContextLocalCount() const {
1191 if (num_heap_slots() == 0) return 0;
1192 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS -
1193 (function_ != NULL && function_->var()->IsContextSlot() ? 1 : 0);
1194}
1195
Steve Blocka7e24c12009-10-30 11:49:00 +00001196} } // namespace v8::internal