blob: a7ff28789f17b679eb650094bdec35cc28f744ad [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"
Ben Murdochc7cc0282012-03-05 14:35:55 +000034#include "messages.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000035#include "scopeinfo.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000036
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000037#include "allocation-inl.h"
38
Steve Blocka7e24c12009-10-30 11:49:00 +000039namespace v8 {
40namespace internal {
41
42// ----------------------------------------------------------------------------
43// A Zone allocator for use with LocalsMap.
44
Steve Block44f0eee2011-05-26 01:26:41 +010045// TODO(isolates): It is probably worth it to change the Allocator class to
46// take a pointer to an isolate.
Steve Blocka7e24c12009-10-30 11:49:00 +000047class ZoneAllocator: public Allocator {
48 public:
49 /* nothing to do */
50 virtual ~ZoneAllocator() {}
51
Steve Block44f0eee2011-05-26 01:26:41 +010052 virtual void* New(size_t size) { return ZONE->New(static_cast<int>(size)); }
Steve Blocka7e24c12009-10-30 11:49:00 +000053
54 /* ignored - Zone is freed in one fell swoop */
55 virtual void Delete(void* p) {}
56};
57
58
Ben Murdoch592a9fc2012-03-05 11:04:45 +000059static ZoneAllocator* LocalsMapAllocator = ::new ZoneAllocator();
Steve Blocka7e24c12009-10-30 11:49:00 +000060
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
Ben Murdoch592a9fc2012-03-05 11:04:45 +000080VariableMap::VariableMap() : HashMap(Match, LocalsMapAllocator, 8) {}
Steve Blocka7e24c12009-10-30 11:49:00 +000081VariableMap::~VariableMap() {}
82
83
Ben Murdoch592a9fc2012-03-05 11:04:45 +000084Variable* VariableMap::Declare(
85 Scope* scope,
86 Handle<String> name,
87 VariableMode mode,
88 bool is_valid_lhs,
89 Variable::Kind kind,
90 InitializationFlag initialization_flag) {
Steve Blocka7e24c12009-10-30 11:49:00 +000091 HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), true);
92 if (p->value == NULL) {
93 // The variable has not been declared yet -> insert it.
94 ASSERT(p->key == name.location());
Ben Murdoch592a9fc2012-03-05 11:04:45 +000095 p->value = new Variable(scope,
96 name,
97 mode,
98 is_valid_lhs,
99 kind,
100 initialization_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000101 }
102 return reinterpret_cast<Variable*>(p->value);
103}
104
105
106Variable* VariableMap::Lookup(Handle<String> name) {
107 HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), false);
108 if (p != NULL) {
109 ASSERT(*reinterpret_cast<String**>(p->key) == *name);
110 ASSERT(p->value != NULL);
111 return reinterpret_cast<Variable*>(p->value);
112 }
113 return NULL;
114}
115
116
117// ----------------------------------------------------------------------------
118// Implementation of Scope
119
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000120Scope::Scope(Scope* outer_scope, ScopeType type)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000121 : isolate_(Isolate::Current()),
122 inner_scopes_(4),
123 variables_(),
124 temps_(4),
125 params_(4),
126 unresolved_(16),
127 decls_(4),
128 already_resolved_(false) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000129 SetDefaults(type, outer_scope, Handle<ScopeInfo>::null());
Steve Blocka7e24c12009-10-30 11:49:00 +0000130 // At some point we might want to provide outer scopes to
131 // eval scopes (by walking the stack and reading the scope info).
132 // In that case, the ASSERT below needs to be adjusted.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000133 ASSERT_EQ(type == GLOBAL_SCOPE, outer_scope == NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000134 ASSERT(!HasIllegalRedeclaration());
135}
136
137
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000138Scope::Scope(Scope* inner_scope,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000139 ScopeType type,
140 Handle<ScopeInfo> scope_info)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000141 : isolate_(Isolate::Current()),
142 inner_scopes_(4),
143 variables_(),
144 temps_(4),
145 params_(4),
146 unresolved_(16),
147 decls_(4),
148 already_resolved_(true) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000149 SetDefaults(type, NULL, scope_info);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000150 if (!scope_info.is_null()) {
151 num_heap_slots_ = scope_info_->ContextLength();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100152 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000153 // Ensure at least MIN_CONTEXT_SLOTS to indicate a materialized context.
154 num_heap_slots_ = Max(num_heap_slots_,
155 static_cast<int>(Context::MIN_CONTEXT_SLOTS));
Steve Block44f0eee2011-05-26 01:26:41 +0100156 AddInnerScope(inner_scope);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000157}
Steve Block44f0eee2011-05-26 01:26:41 +0100158
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000159
160Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name)
161 : isolate_(Isolate::Current()),
162 inner_scopes_(1),
163 variables_(),
164 temps_(0),
165 params_(0),
166 unresolved_(0),
167 decls_(0),
168 already_resolved_(true) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000169 SetDefaults(CATCH_SCOPE, NULL, Handle<ScopeInfo>::null());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000170 AddInnerScope(inner_scope);
171 ++num_var_or_const_;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000172 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000173 Variable* variable = variables_.Declare(this,
174 catch_variable_name,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000175 VAR,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000176 true, // Valid left-hand side.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000177 Variable::NORMAL,
178 kCreatedInitialized);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000179 AllocateHeapSlot(variable);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100180}
181
182
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000183void Scope::SetDefaults(ScopeType type,
Ben Murdoch8b112d22011-06-08 16:22:53 +0100184 Scope* outer_scope,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000185 Handle<ScopeInfo> scope_info) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100186 outer_scope_ = outer_scope;
187 type_ = type;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000188 scope_name_ = isolate_->factory()->empty_symbol();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100189 dynamics_ = NULL;
190 receiver_ = NULL;
191 function_ = NULL;
192 arguments_ = NULL;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100193 illegal_redecl_ = NULL;
194 scope_inside_with_ = false;
195 scope_contains_with_ = false;
196 scope_calls_eval_ = false;
197 // Inherit the strict mode from the parent scope.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000198 language_mode_ = (outer_scope != NULL)
199 ? outer_scope->language_mode_ : CLASSIC_MODE;
Ben Murdoch257744e2011-11-30 15:57:28 +0000200 outer_scope_calls_non_strict_eval_ = false;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100201 inner_scope_calls_eval_ = false;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100202 force_eager_compilation_ = false;
Steve Block053d10c2011-06-13 19:13:29 +0100203 num_var_or_const_ = 0;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100204 num_stack_slots_ = 0;
205 num_heap_slots_ = 0;
206 scope_info_ = scope_info;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000207 start_position_ = RelocInfo::kNoPosition;
208 end_position_ = RelocInfo::kNoPosition;
209 if (!scope_info.is_null()) {
210 scope_calls_eval_ = scope_info->CallsEval();
211 language_mode_ = scope_info->language_mode();
212 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100213}
214
215
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000216Scope* Scope::DeserializeScopeChain(Context* context, Scope* global_scope) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000217 // Reconstruct the outer scope chain from a closure's context chain.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000218 Scope* current_scope = NULL;
Steve Block44f0eee2011-05-26 01:26:41 +0100219 Scope* innermost_scope = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000220 bool contains_with = false;
221 while (!context->IsGlobalContext()) {
222 if (context->IsWithContext()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000223 Scope* with_scope = new Scope(current_scope,
224 WITH_SCOPE,
225 Handle<ScopeInfo>::null());
226 current_scope = with_scope;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000227 // All the inner scopes are inside a with.
228 contains_with = true;
229 for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) {
230 s->scope_inside_with_ = true;
Steve Block44f0eee2011-05-26 01:26:41 +0100231 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000232 } else if (context->IsFunctionContext()) {
233 ScopeInfo* scope_info = context->closure()->shared()->scope_info();
234 current_scope = new Scope(current_scope,
235 FUNCTION_SCOPE,
236 Handle<ScopeInfo>(scope_info));
237 } else if (context->IsBlockContext()) {
238 ScopeInfo* scope_info = ScopeInfo::cast(context->extension());
239 current_scope = new Scope(current_scope,
240 BLOCK_SCOPE,
241 Handle<ScopeInfo>(scope_info));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000242 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000243 ASSERT(context->IsCatchContext());
244 String* name = String::cast(context->extension());
245 current_scope = new Scope(current_scope, Handle<String>(name));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000246 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000247 if (contains_with) current_scope->RecordWithStatement();
248 if (innermost_scope == NULL) innermost_scope = current_scope;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000249
250 // Forget about a with when we move to a context for a different function.
251 if (context->previous()->closure() != context->closure()) {
252 contains_with = false;
253 }
254 context = context->previous();
Steve Block44f0eee2011-05-26 01:26:41 +0100255 }
256
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000257 global_scope->AddInnerScope(current_scope);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000258 global_scope->PropagateScopeInfo(false);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000259 return (innermost_scope == NULL) ? global_scope : innermost_scope;
Steve Block44f0eee2011-05-26 01:26:41 +0100260}
261
Ben Murdochb8e0da22011-05-16 14:20:40 +0100262
Ben Murdochf87a2032010-10-22 12:50:53 +0100263bool Scope::Analyze(CompilationInfo* info) {
264 ASSERT(info->function() != NULL);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000265 Scope* scope = info->function()->scope();
266 Scope* top = scope;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100267
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000268 // Traverse the scope tree up to the first unresolved scope or the global
269 // scope and start scope resolution and variable allocation from that scope.
270 while (!top->is_global_scope() &&
271 !top->outer_scope()->already_resolved()) {
272 top = top->outer_scope();
273 }
274
275 // Allocated the variables.
276 top->AllocateVariables(info->global_scope());
Ben Murdochf87a2032010-10-22 12:50:53 +0100277
278#ifdef DEBUG
Steve Block44f0eee2011-05-26 01:26:41 +0100279 if (info->isolate()->bootstrapper()->IsActive()
Ben Murdochf87a2032010-10-22 12:50:53 +0100280 ? FLAG_print_builtin_scopes
281 : FLAG_print_scopes) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000282 scope->Print();
Ben Murdochf87a2032010-10-22 12:50:53 +0100283 }
284#endif
285
Ben Murdochc7cc0282012-03-05 14:35:55 +0000286 if (FLAG_harmony_scoping) {
287 VariableProxy* proxy = scope->CheckAssignmentToConst();
288 if (proxy != NULL) {
289 // Found an assignment to const. Throw a syntax error.
290 MessageLocation location(info->script(),
291 proxy->position(),
292 proxy->position());
293 Isolate* isolate = info->isolate();
294 Factory* factory = isolate->factory();
295 Handle<JSArray> array = factory->NewJSArray(0);
296 Handle<Object> result =
297 factory->NewSyntaxError("harmony_const_assign", array);
298 isolate->Throw(*result, &location);
299 return false;
300 }
301 }
302
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000303 info->SetScope(scope);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000304 return true;
Ben Murdochf87a2032010-10-22 12:50:53 +0100305}
306
307
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000308void Scope::Initialize() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000309 ASSERT(!already_resolved());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100310
Steve Blocka7e24c12009-10-30 11:49:00 +0000311 // Add this scope as a new inner scope of the outer scope.
312 if (outer_scope_ != NULL) {
313 outer_scope_->inner_scopes_.Add(this);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000314 scope_inside_with_ = outer_scope_->scope_inside_with_ || is_with_scope();
Steve Blocka7e24c12009-10-30 11:49:00 +0000315 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000316 scope_inside_with_ = is_with_scope();
Steve Blocka7e24c12009-10-30 11:49:00 +0000317 }
318
319 // Declare convenience variables.
320 // Declare and allocate receiver (even for the global scope, and even
321 // if naccesses_ == 0).
322 // NOTE: When loading parameters in the global scope, we must take
323 // care not to access them as properties of the global object, but
324 // instead load them directly from the stack. Currently, the only
325 // such parameter is 'this' which is passed on the stack when
326 // invoking scripts
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000327 if (is_declaration_scope()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000328 Variable* var =
329 variables_.Declare(this,
330 isolate_->factory()->this_symbol(),
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000331 VAR,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000332 false,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000333 Variable::THIS,
334 kCreatedInitialized);
Ben Murdoch589d6972011-11-30 16:04:58 +0000335 var->AllocateTo(Variable::PARAMETER, -1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000336 receiver_ = var;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000337 } else {
338 ASSERT(outer_scope() != NULL);
339 receiver_ = outer_scope()->receiver();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000340 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000341
342 if (is_function_scope()) {
343 // Declare 'arguments' variable which exists in all functions.
344 // Note that it might never be accessed, in which case it won't be
345 // allocated during variable allocation.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000346 variables_.Declare(this,
347 isolate_->factory()->arguments_symbol(),
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000348 VAR,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000349 true,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000350 Variable::ARGUMENTS,
351 kCreatedInitialized);
Steve Blocka7e24c12009-10-30 11:49:00 +0000352 }
353}
354
355
Ben Murdoch589d6972011-11-30 16:04:58 +0000356Scope* Scope::FinalizeBlockScope() {
357 ASSERT(is_block_scope());
358 ASSERT(temps_.is_empty());
359 ASSERT(params_.is_empty());
360
361 if (num_var_or_const() > 0) return this;
362
363 // Remove this scope from outer scope.
364 for (int i = 0; i < outer_scope_->inner_scopes_.length(); i++) {
365 if (outer_scope_->inner_scopes_[i] == this) {
366 outer_scope_->inner_scopes_.Remove(i);
367 break;
368 }
369 }
370
371 // Reparent inner scopes.
372 for (int i = 0; i < inner_scopes_.length(); i++) {
373 outer_scope()->AddInnerScope(inner_scopes_[i]);
374 }
375
376 // Move unresolved variables
377 for (int i = 0; i < unresolved_.length(); i++) {
378 outer_scope()->unresolved_.Add(unresolved_[i]);
379 }
380
381 return NULL;
382}
383
384
Steve Blocka7e24c12009-10-30 11:49:00 +0000385Variable* Scope::LocalLookup(Handle<String> name) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100386 Variable* result = variables_.Lookup(name);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000387 if (result != NULL || scope_info_.is_null()) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100388 return result;
389 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000390 // If we have a serialized scope info, we might find the variable there.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000391 // There should be no local slot with the given name.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100392 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
393
394 // Check context slot lookup.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000395 VariableMode mode;
396 InitializationFlag init_flag;
397 int index = scope_info_->ContextSlotIndex(*name, &mode, &init_flag);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000398 if (index < 0) {
399 // Check parameters.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000400 mode = VAR;
401 init_flag = kCreatedInitialized;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000402 index = scope_info_->ParameterIndex(*name);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000403 if (index < 0) return NULL;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100404 }
405
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000406 Variable* var =
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000407 variables_.Declare(this,
408 name,
409 mode,
410 true,
411 Variable::NORMAL,
412 init_flag);
Ben Murdoch589d6972011-11-30 16:04:58 +0000413 var->AllocateTo(Variable::CONTEXT, index);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000414 return var;
Steve Blocka7e24c12009-10-30 11:49:00 +0000415}
416
417
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000418Variable* Scope::LookupFunctionVar(Handle<String> name) {
419 if (function_ != NULL && function_->name().is_identical_to(name)) {
420 return function_->var();
421 } else if (!scope_info_.is_null()) {
422 // If we are backed by a scope info, try to lookup the variable there.
423 VariableMode mode;
424 int index = scope_info_->FunctionContextSlotIndex(*name, &mode);
425 if (index < 0) return NULL;
426 Variable* var = DeclareFunctionVar(name, mode);
427 var->AllocateTo(Variable::CONTEXT, index);
428 return var;
429 } else {
430 return NULL;
431 }
432}
433
434
Steve Blocka7e24c12009-10-30 11:49:00 +0000435Variable* Scope::Lookup(Handle<String> name) {
436 for (Scope* scope = this;
437 scope != NULL;
438 scope = scope->outer_scope()) {
439 Variable* var = scope->LocalLookup(name);
440 if (var != NULL) return var;
441 }
442 return NULL;
443}
444
445
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000446Variable* Scope::DeclareFunctionVar(Handle<String> name, VariableMode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000447 ASSERT(is_function_scope() && function_ == NULL);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000448 Variable* function_var = new Variable(
449 this, name, mode, true, Variable::NORMAL, kCreatedInitialized);
Ben Murdoch589d6972011-11-30 16:04:58 +0000450 function_ = new(isolate_->zone()) VariableProxy(isolate_, function_var);
451 return function_var;
Steve Blocka7e24c12009-10-30 11:49:00 +0000452}
453
454
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000455void Scope::DeclareParameter(Handle<String> name, VariableMode mode) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000456 ASSERT(!already_resolved());
457 ASSERT(is_function_scope());
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000458 Variable* var = variables_.Declare(
459 this, name, mode, true, Variable::NORMAL, kCreatedInitialized);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000460 params_.Add(var);
461}
462
463
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000464Variable* Scope::DeclareLocal(Handle<String> name,
465 VariableMode mode,
466 InitializationFlag init_flag) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000467 ASSERT(!already_resolved());
468 // This function handles VAR and CONST modes. DYNAMIC variables are
469 // introduces during variable allocation, INTERNAL variables are allocated
470 // explicitly, and TEMPORARY variables are allocated via NewTemporary().
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000471 ASSERT(mode == VAR ||
472 mode == CONST ||
473 mode == CONST_HARMONY ||
474 mode == LET);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000475 ++num_var_or_const_;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000476 return
477 variables_.Declare(this, name, mode, true, Variable::NORMAL, init_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000478}
479
480
481Variable* Scope::DeclareGlobal(Handle<String> name) {
482 ASSERT(is_global_scope());
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000483 return variables_.Declare(this,
484 name,
485 DYNAMIC_GLOBAL,
Ben Murdoch589d6972011-11-30 16:04:58 +0000486 true,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000487 Variable::NORMAL,
488 kCreatedInitialized);
Steve Blocka7e24c12009-10-30 11:49:00 +0000489}
490
491
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000492VariableProxy* Scope::NewUnresolved(Handle<String> name, int position) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000493 // Note that we must not share the unresolved variables with
494 // the same name because they may be removed selectively via
495 // RemoveUnresolved().
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000496 ASSERT(!already_resolved());
497 VariableProxy* proxy = new(isolate_->zone()) VariableProxy(
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000498 isolate_, name, false, position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000499 unresolved_.Add(proxy);
500 return proxy;
501}
502
503
504void Scope::RemoveUnresolved(VariableProxy* var) {
505 // Most likely (always?) any variable we want to remove
506 // was just added before, so we search backwards.
507 for (int i = unresolved_.length(); i-- > 0;) {
508 if (unresolved_[i] == var) {
509 unresolved_.Remove(i);
510 return;
511 }
512 }
513}
514
515
Ben Murdochb0fe1622011-05-05 13:52:32 +0100516Variable* Scope::NewTemporary(Handle<String> name) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000517 ASSERT(!already_resolved());
Ben Murdoch589d6972011-11-30 16:04:58 +0000518 Variable* var = new Variable(this,
519 name,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000520 TEMPORARY,
Ben Murdoch589d6972011-11-30 16:04:58 +0000521 true,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000522 Variable::NORMAL,
523 kCreatedInitialized);
Steve Blocka7e24c12009-10-30 11:49:00 +0000524 temps_.Add(var);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100525 return var;
Steve Blocka7e24c12009-10-30 11:49:00 +0000526}
527
528
529void Scope::AddDeclaration(Declaration* declaration) {
530 decls_.Add(declaration);
531}
532
533
534void Scope::SetIllegalRedeclaration(Expression* expression) {
Steve Block1e0659c2011-05-24 12:43:12 +0100535 // Record only the first illegal redeclaration.
Steve Blocka7e24c12009-10-30 11:49:00 +0000536 if (!HasIllegalRedeclaration()) {
537 illegal_redecl_ = expression;
538 }
539 ASSERT(HasIllegalRedeclaration());
540}
541
542
543void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) {
544 ASSERT(HasIllegalRedeclaration());
545 illegal_redecl_->Accept(visitor);
546}
547
548
Ben Murdoch589d6972011-11-30 16:04:58 +0000549Declaration* Scope::CheckConflictingVarDeclarations() {
550 int length = decls_.length();
551 for (int i = 0; i < length; i++) {
552 Declaration* decl = decls_[i];
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000553 if (decl->mode() != VAR) continue;
Ben Murdoch589d6972011-11-30 16:04:58 +0000554 Handle<String> name = decl->proxy()->name();
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000555
556 // Iterate through all scopes until and including the declaration scope.
557 Scope* previous = NULL;
558 Scope* current = decl->scope();
559 do {
Ben Murdoch589d6972011-11-30 16:04:58 +0000560 // There is a conflict if there exists a non-VAR binding.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000561 Variable* other_var = current->variables_.Lookup(name);
562 if (other_var != NULL && other_var->mode() != VAR) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000563 return decl;
564 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000565 previous = current;
566 current = current->outer_scope_;
567 } while (!previous->is_declaration_scope());
Ben Murdoch589d6972011-11-30 16:04:58 +0000568 }
569 return NULL;
570}
571
572
Ben Murdochc7cc0282012-03-05 14:35:55 +0000573VariableProxy* Scope::CheckAssignmentToConst() {
574 // Check this scope.
575 if (is_extended_mode()) {
576 for (int i = 0; i < unresolved_.length(); i++) {
577 ASSERT(unresolved_[i]->var() != NULL);
578 if (unresolved_[i]->var()->is_const_mode() &&
579 unresolved_[i]->IsLValue()) {
580 return unresolved_[i];
581 }
582 }
583 }
584
585 // Check inner scopes.
586 for (int i = 0; i < inner_scopes_.length(); i++) {
587 VariableProxy* proxy = inner_scopes_[i]->CheckAssignmentToConst();
588 if (proxy != NULL) return proxy;
589 }
590
591 // No assignments to const found.
592 return NULL;
593}
594
595
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000596void Scope::CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals,
597 ZoneList<Variable*>* context_locals) {
598 ASSERT(stack_locals != NULL);
599 ASSERT(context_locals != NULL);
600
601 // Collect temporaries which are always allocated on the stack.
Steve Blocka7e24c12009-10-30 11:49:00 +0000602 for (int i = 0; i < temps_.length(); i++) {
603 Variable* var = temps_[i];
Steve Block6ded16b2010-05-10 14:33:55 +0100604 if (var->is_used()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000605 ASSERT(var->IsStackLocal());
606 stack_locals->Add(var);
Steve Blocka7e24c12009-10-30 11:49:00 +0000607 }
608 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000609
610 // Collect declared local variables.
Steve Blocka7e24c12009-10-30 11:49:00 +0000611 for (VariableMap::Entry* p = variables_.Start();
612 p != NULL;
613 p = variables_.Next(p)) {
614 Variable* var = reinterpret_cast<Variable*>(p->value);
Steve Block6ded16b2010-05-10 14:33:55 +0100615 if (var->is_used()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000616 if (var->IsStackLocal()) {
617 stack_locals->Add(var);
618 } else if (var->IsContextSlot()) {
619 context_locals->Add(var);
620 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000621 }
622 }
623}
624
625
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000626void Scope::AllocateVariables(Scope* global_scope) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000627 // 1) Propagate scope information.
Ben Murdoch257744e2011-11-30 15:57:28 +0000628 bool outer_scope_calls_non_strict_eval = false;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000629 if (outer_scope_ != NULL) {
630 outer_scope_calls_non_strict_eval =
631 outer_scope_->outer_scope_calls_non_strict_eval() |
632 outer_scope_->calls_non_strict_eval();
Ben Murdoch257744e2011-11-30 15:57:28 +0000633 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000634 PropagateScopeInfo(outer_scope_calls_non_strict_eval);
Steve Blocka7e24c12009-10-30 11:49:00 +0000635
636 // 2) Resolve variables.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000637 ResolveVariablesRecursively(global_scope);
Steve Blocka7e24c12009-10-30 11:49:00 +0000638
639 // 3) Allocate variables.
640 AllocateVariablesRecursively();
641}
642
643
644bool Scope::AllowsLazyCompilation() const {
645 return !force_eager_compilation_ && HasTrivialOuterContext();
646}
647
648
649bool Scope::HasTrivialContext() const {
650 // A function scope has a trivial context if it always is the global
651 // context. We iteratively scan out the context chain to see if
652 // there is anything that makes this scope non-trivial; otherwise we
653 // return true.
654 for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) {
655 if (scope->is_eval_scope()) return false;
656 if (scope->scope_inside_with_) return false;
657 if (scope->num_heap_slots_ > 0) return false;
658 }
659 return true;
660}
661
662
663bool Scope::HasTrivialOuterContext() const {
664 Scope* outer = outer_scope_;
665 if (outer == NULL) return true;
666 // Note that the outer context may be trivial in general, but the current
667 // scope may be inside a 'with' statement in which case the outer context
668 // for this scope is not trivial.
669 return !scope_inside_with_ && outer->HasTrivialContext();
670}
671
672
673int Scope::ContextChainLength(Scope* scope) {
674 int n = 0;
675 for (Scope* s = this; s != scope; s = s->outer_scope_) {
676 ASSERT(s != NULL); // scope must be in the scope chain
677 if (s->num_heap_slots() > 0) n++;
678 }
679 return n;
680}
681
682
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000683Scope* Scope::DeclarationScope() {
684 Scope* scope = this;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000685 while (!scope->is_declaration_scope()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000686 scope = scope->outer_scope();
687 }
688 return scope;
689}
690
691
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000692Handle<ScopeInfo> Scope::GetScopeInfo() {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000693 if (scope_info_.is_null()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000694 scope_info_ = ScopeInfo::Create(this);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000695 }
696 return scope_info_;
697}
698
699
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000700void Scope::GetNestedScopeChain(
701 List<Handle<ScopeInfo> >* chain,
702 int position) {
703 if (!is_eval_scope()) chain->Add(Handle<ScopeInfo>(GetScopeInfo()));
704
705 for (int i = 0; i < inner_scopes_.length(); i++) {
706 Scope* scope = inner_scopes_[i];
707 int beg_pos = scope->start_position();
708 int end_pos = scope->end_position();
709 ASSERT(beg_pos >= 0 && end_pos >= 0);
710 if (beg_pos <= position && position < end_pos) {
711 scope->GetNestedScopeChain(chain, position);
712 return;
713 }
714 }
715}
716
717
Steve Blocka7e24c12009-10-30 11:49:00 +0000718#ifdef DEBUG
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000719static const char* Header(ScopeType type) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000720 switch (type) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000721 case EVAL_SCOPE: return "eval";
722 case FUNCTION_SCOPE: return "function";
723 case GLOBAL_SCOPE: return "global";
724 case CATCH_SCOPE: return "catch";
725 case BLOCK_SCOPE: return "block";
726 case WITH_SCOPE: return "with";
Steve Blocka7e24c12009-10-30 11:49:00 +0000727 }
728 UNREACHABLE();
729 return NULL;
730}
731
732
733static void Indent(int n, const char* str) {
734 PrintF("%*s%s", n, "", str);
735}
736
737
738static void PrintName(Handle<String> name) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000739 SmartArrayPointer<char> s = name->ToCString(DISALLOW_NULLS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000740 PrintF("%s", *s);
741}
742
743
Ben Murdoch589d6972011-11-30 16:04:58 +0000744static void PrintLocation(Variable* var) {
745 switch (var->location()) {
746 case Variable::UNALLOCATED:
747 break;
748 case Variable::PARAMETER:
749 PrintF("parameter[%d]", var->index());
750 break;
751 case Variable::LOCAL:
752 PrintF("local[%d]", var->index());
753 break;
754 case Variable::CONTEXT:
755 PrintF("context[%d]", var->index());
756 break;
757 case Variable::LOOKUP:
758 PrintF("lookup");
759 break;
760 }
761}
762
763
764static void PrintVar(int indent, Variable* var) {
765 if (var->is_used() || !var->IsUnallocated()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000766 Indent(indent, Variable::Mode2String(var->mode()));
767 PrintF(" ");
768 PrintName(var->name());
769 PrintF("; // ");
Ben Murdoch589d6972011-11-30 16:04:58 +0000770 PrintLocation(var);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000771 if (var->has_forced_context_allocation()) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000772 if (!var->IsUnallocated()) PrintF(", ");
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000773 PrintF("forced context allocation");
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000774 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000775 PrintF("\n");
776 }
777}
778
779
Ben Murdoch589d6972011-11-30 16:04:58 +0000780static void PrintMap(int indent, VariableMap* map) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000781 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
782 Variable* var = reinterpret_cast<Variable*>(p->value);
Ben Murdoch589d6972011-11-30 16:04:58 +0000783 PrintVar(indent, var);
Steve Blocka7e24c12009-10-30 11:49:00 +0000784 }
785}
786
787
788void Scope::Print(int n) {
789 int n0 = (n > 0 ? n : 0);
790 int n1 = n0 + 2; // indentation
791
792 // Print header.
793 Indent(n0, Header(type_));
794 if (scope_name_->length() > 0) {
795 PrintF(" ");
796 PrintName(scope_name_);
797 }
798
799 // Print parameters, if any.
800 if (is_function_scope()) {
801 PrintF(" (");
802 for (int i = 0; i < params_.length(); i++) {
803 if (i > 0) PrintF(", ");
804 PrintName(params_[i]->name());
805 }
806 PrintF(")");
807 }
808
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000809 PrintF(" { // (%d, %d)\n", start_position(), end_position());
Steve Blocka7e24c12009-10-30 11:49:00 +0000810
811 // Function name, if any (named function literals, only).
812 if (function_ != NULL) {
813 Indent(n1, "// (local) function name: ");
814 PrintName(function_->name());
815 PrintF("\n");
816 }
817
818 // Scope info.
819 if (HasTrivialOuterContext()) {
820 Indent(n1, "// scope has trivial outer context\n");
821 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000822 switch (language_mode()) {
823 case CLASSIC_MODE:
824 break;
825 case STRICT_MODE:
826 Indent(n1, "// strict mode scope\n");
827 break;
828 case EXTENDED_MODE:
829 Indent(n1, "// extended mode scope\n");
830 break;
831 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000832 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
833 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n");
834 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n");
Ben Murdoch257744e2011-11-30 15:57:28 +0000835 if (outer_scope_calls_non_strict_eval_) {
836 Indent(n1, "// outer scope calls 'eval' in non-strict context\n");
837 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000838 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000839 if (num_stack_slots_ > 0) { Indent(n1, "// ");
840 PrintF("%d stack slots\n", num_stack_slots_); }
841 if (num_heap_slots_ > 0) { Indent(n1, "// ");
842 PrintF("%d heap slots\n", num_heap_slots_); }
843
844 // Print locals.
Steve Blocka7e24c12009-10-30 11:49:00 +0000845 Indent(n1, "// function var\n");
846 if (function_ != NULL) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000847 PrintVar(n1, function_->var());
Steve Blocka7e24c12009-10-30 11:49:00 +0000848 }
849
850 Indent(n1, "// temporary vars\n");
851 for (int i = 0; i < temps_.length(); i++) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000852 PrintVar(n1, temps_[i]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000853 }
854
855 Indent(n1, "// local vars\n");
Ben Murdoch589d6972011-11-30 16:04:58 +0000856 PrintMap(n1, &variables_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000857
858 Indent(n1, "// dynamic vars\n");
859 if (dynamics_ != NULL) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000860 PrintMap(n1, dynamics_->GetMap(DYNAMIC));
861 PrintMap(n1, dynamics_->GetMap(DYNAMIC_LOCAL));
862 PrintMap(n1, dynamics_->GetMap(DYNAMIC_GLOBAL));
Steve Blocka7e24c12009-10-30 11:49:00 +0000863 }
864
865 // Print inner scopes (disable by providing negative n).
866 if (n >= 0) {
867 for (int i = 0; i < inner_scopes_.length(); i++) {
868 PrintF("\n");
869 inner_scopes_[i]->Print(n1);
870 }
871 }
872
873 Indent(n0, "}\n");
874}
875#endif // DEBUG
876
877
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000878Variable* Scope::NonLocal(Handle<String> name, VariableMode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000879 if (dynamics_ == NULL) dynamics_ = new DynamicScopePart();
880 VariableMap* map = dynamics_->GetMap(mode);
881 Variable* var = map->Lookup(name);
882 if (var == NULL) {
883 // Declare a new non-local.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000884 InitializationFlag init_flag = (mode == VAR)
885 ? kCreatedInitialized : kNeedsInitialization;
886 var = map->Declare(NULL,
887 name,
888 mode,
889 true,
890 Variable::NORMAL,
891 init_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000892 // Allocate it by giving it a dynamic lookup.
Ben Murdoch589d6972011-11-30 16:04:58 +0000893 var->AllocateTo(Variable::LOOKUP, -1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000894 }
895 return var;
896}
897
898
Steve Blocka7e24c12009-10-30 11:49:00 +0000899Variable* Scope::LookupRecursive(Handle<String> name,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000900 BindingKind* binding_kind) {
901 ASSERT(binding_kind != NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000902 // Try to find the variable in this scope.
903 Variable* var = LocalLookup(name);
904
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000905 // We found a variable and we are done. (Even if there is an 'eval' in
906 // this scope which introduces the same variable again, the resulting
907 // variable remains the same.)
Steve Blocka7e24c12009-10-30 11:49:00 +0000908 if (var != NULL) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000909 *binding_kind = BOUND;
910 return var;
911 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000912
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000913 // We did not find a variable locally. Check against the function variable,
914 // if any. We can do this for all scopes, since the function variable is
915 // only present - if at all - for function scopes.
916 *binding_kind = UNBOUND;
917 var = LookupFunctionVar(name);
918 if (var != NULL) {
919 *binding_kind = BOUND;
920 } else if (outer_scope_ != NULL) {
921 var = outer_scope_->LookupRecursive(name, binding_kind);
922 if (*binding_kind == BOUND && (is_function_scope() || is_with_scope())) {
923 var->ForceContextAllocation();
Steve Blocka7e24c12009-10-30 11:49:00 +0000924 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000925 } else {
926 ASSERT(is_global_scope());
Steve Blocka7e24c12009-10-30 11:49:00 +0000927 }
928
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000929 if (is_with_scope()) {
930 // The current scope is a with scope, so the variable binding can not be
931 // statically resolved. However, note that it was necessary to do a lookup
932 // in the outer scope anyway, because if a binding exists in an outer scope,
933 // the associated variable has to be marked as potentially being accessed
934 // from inside of an inner with scope (the property may not be in the 'with'
935 // object).
936 *binding_kind = DYNAMIC_LOOKUP;
937 return NULL;
938 } else if (calls_non_strict_eval()) {
939 // A variable binding may have been found in an outer scope, but the current
940 // scope makes a non-strict 'eval' call, so the found variable may not be
941 // the correct one (the 'eval' may introduce a binding with the same name).
942 // In that case, change the lookup result to reflect this situation.
943 if (*binding_kind == BOUND) {
944 *binding_kind = BOUND_EVAL_SHADOWED;
945 } else if (*binding_kind == UNBOUND) {
946 *binding_kind = UNBOUND_EVAL_SHADOWED;
947 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100948 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000949 return var;
950}
951
952
953void Scope::ResolveVariable(Scope* global_scope,
Steve Blocka7e24c12009-10-30 11:49:00 +0000954 VariableProxy* proxy) {
955 ASSERT(global_scope == NULL || global_scope->is_global_scope());
956
957 // If the proxy is already resolved there's nothing to do
958 // (functions and consts may be resolved by the parser).
959 if (proxy->var() != NULL) return;
960
961 // Otherwise, try to resolve the variable.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000962 BindingKind binding_kind;
963 Variable* var = LookupRecursive(proxy->name(), &binding_kind);
964 switch (binding_kind) {
965 case BOUND:
966 // We found a variable binding.
967 break;
Steve Blocka7e24c12009-10-30 11:49:00 +0000968
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000969 case BOUND_EVAL_SHADOWED:
970 // We found a variable variable binding that might be shadowed
971 // by 'eval' introduced variable bindings.
972 if (var->is_global()) {
973 var = NonLocal(proxy->name(), DYNAMIC_GLOBAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000974 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000975 Variable* invalidated = var;
976 var = NonLocal(proxy->name(), DYNAMIC_LOCAL);
977 var->set_local_if_not_shadowed(invalidated);
Steve Blocka7e24c12009-10-30 11:49:00 +0000978 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000979 break;
980
981 case UNBOUND:
982 // No binding has been found. Declare a variable in global scope.
983 ASSERT(global_scope != NULL);
984 var = global_scope->DeclareGlobal(proxy->name());
985 break;
986
987 case UNBOUND_EVAL_SHADOWED:
988 // No binding has been found. But some scope makes a
989 // non-strict 'eval' call.
990 var = NonLocal(proxy->name(), DYNAMIC_GLOBAL);
991 break;
992
993 case DYNAMIC_LOOKUP:
994 // The variable could not be resolved statically.
995 var = NonLocal(proxy->name(), DYNAMIC);
996 break;
Steve Blocka7e24c12009-10-30 11:49:00 +0000997 }
998
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000999 ASSERT(var != NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +00001000 proxy->BindTo(var);
1001}
1002
1003
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001004void Scope::ResolveVariablesRecursively(Scope* global_scope) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001005 ASSERT(global_scope == NULL || global_scope->is_global_scope());
1006
1007 // Resolve unresolved variables for this scope.
1008 for (int i = 0; i < unresolved_.length(); i++) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001009 ResolveVariable(global_scope, unresolved_[i]);
Steve Blocka7e24c12009-10-30 11:49:00 +00001010 }
1011
1012 // Resolve unresolved variables for inner scopes.
1013 for (int i = 0; i < inner_scopes_.length(); i++) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001014 inner_scopes_[i]->ResolveVariablesRecursively(global_scope);
Steve Blocka7e24c12009-10-30 11:49:00 +00001015 }
1016}
1017
1018
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001019bool Scope::PropagateScopeInfo(bool outer_scope_calls_non_strict_eval ) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001020 if (outer_scope_calls_non_strict_eval) {
1021 outer_scope_calls_non_strict_eval_ = true;
1022 }
1023
Ben Murdoch257744e2011-11-30 15:57:28 +00001024 bool calls_non_strict_eval =
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001025 this->calls_non_strict_eval() || outer_scope_calls_non_strict_eval_;
Steve Blocka7e24c12009-10-30 11:49:00 +00001026 for (int i = 0; i < inner_scopes_.length(); i++) {
1027 Scope* inner_scope = inner_scopes_[i];
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001028 if (inner_scope->PropagateScopeInfo(calls_non_strict_eval)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001029 inner_scope_calls_eval_ = true;
1030 }
1031 if (inner_scope->force_eager_compilation_) {
1032 force_eager_compilation_ = true;
1033 }
1034 }
1035
1036 return scope_calls_eval_ || inner_scope_calls_eval_;
1037}
1038
1039
1040bool Scope::MustAllocate(Variable* var) {
1041 // Give var a read/write use if there is a chance it might be accessed
1042 // via an eval() call. This is only possible if the variable has a
1043 // visible name.
1044 if ((var->is_this() || var->name()->length() > 0) &&
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001045 (var->has_forced_context_allocation() ||
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001046 scope_calls_eval_ ||
1047 inner_scope_calls_eval_ ||
1048 scope_contains_with_ ||
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001049 is_catch_scope() ||
1050 is_block_scope())) {
Steve Block6ded16b2010-05-10 14:33:55 +01001051 var->set_is_used(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001052 }
1053 // Global variables do not need to be allocated.
Steve Block6ded16b2010-05-10 14:33:55 +01001054 return !var->is_global() && var->is_used();
Steve Blocka7e24c12009-10-30 11:49:00 +00001055}
1056
1057
1058bool Scope::MustAllocateInContext(Variable* var) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001059 // If var is accessed from an inner scope, or if there is a possibility
1060 // that it might be accessed from the current or an inner scope (through
1061 // an eval() call or a runtime with lookup), it must be allocated in the
Steve Blocka7e24c12009-10-30 11:49:00 +00001062 // context.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001063 //
1064 // Exceptions: temporary variables are never allocated in a context;
1065 // catch-bound variables are always allocated in a context.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001066 if (var->mode() == TEMPORARY) return false;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001067 if (is_catch_scope() || is_block_scope()) return true;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001068 return var->has_forced_context_allocation() ||
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001069 scope_calls_eval_ ||
1070 inner_scope_calls_eval_ ||
1071 scope_contains_with_ ||
1072 var->is_global();
Steve Blocka7e24c12009-10-30 11:49:00 +00001073}
1074
1075
1076bool Scope::HasArgumentsParameter() {
1077 for (int i = 0; i < params_.length(); i++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001078 if (params_[i]->name().is_identical_to(
1079 isolate_->factory()->arguments_symbol())) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001080 return true;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001081 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001082 }
1083 return false;
1084}
1085
1086
1087void Scope::AllocateStackSlot(Variable* var) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001088 var->AllocateTo(Variable::LOCAL, num_stack_slots_++);
Steve Blocka7e24c12009-10-30 11:49:00 +00001089}
1090
1091
1092void Scope::AllocateHeapSlot(Variable* var) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001093 var->AllocateTo(Variable::CONTEXT, num_heap_slots_++);
Steve Blocka7e24c12009-10-30 11:49:00 +00001094}
1095
1096
1097void Scope::AllocateParameterLocals() {
1098 ASSERT(is_function_scope());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001099 Variable* arguments = LocalLookup(isolate_->factory()->arguments_symbol());
Steve Blocka7e24c12009-10-30 11:49:00 +00001100 ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly
Steve Block44f0eee2011-05-26 01:26:41 +01001101
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001102 bool uses_nonstrict_arguments = false;
Steve Block44f0eee2011-05-26 01:26:41 +01001103
Steve Blocka7e24c12009-10-30 11:49:00 +00001104 if (MustAllocate(arguments) && !HasArgumentsParameter()) {
1105 // 'arguments' is used. Unless there is also a parameter called
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001106 // 'arguments', we must be conservative and allocate all parameters to
1107 // the context assuming they will be captured by the arguments object.
1108 // If we have a parameter named 'arguments', a (new) value is always
1109 // assigned to it via the function invocation. Then 'arguments' denotes
1110 // that specific parameter value and cannot be used to access the
1111 // parameters, which is why we don't need to allocate an arguments
1112 // object in that case.
Steve Blocka7e24c12009-10-30 11:49:00 +00001113
1114 // We are using 'arguments'. Tell the code generator that is needs to
1115 // allocate the arguments object by setting 'arguments_'.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001116 arguments_ = arguments;
Steve Blocka7e24c12009-10-30 11:49:00 +00001117
Steve Block44f0eee2011-05-26 01:26:41 +01001118 // In strict mode 'arguments' does not alias formal parameters.
1119 // Therefore in strict mode we allocate parameters as if 'arguments'
1120 // were not used.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001121 uses_nonstrict_arguments = is_classic_mode();
Steve Block44f0eee2011-05-26 01:26:41 +01001122 }
1123
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001124 // The same parameter may occur multiple times in the parameters_ list.
1125 // If it does, and if it is not copied into the context object, it must
1126 // receive the highest parameter index for that parameter; thus iteration
1127 // order is relevant!
1128 for (int i = params_.length() - 1; i >= 0; --i) {
1129 Variable* var = params_[i];
1130 ASSERT(var->scope() == this);
1131 if (uses_nonstrict_arguments) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001132 // Force context allocation of the parameter.
1133 var->ForceContextAllocation();
Steve Blocka7e24c12009-10-30 11:49:00 +00001134 }
1135
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001136 if (MustAllocate(var)) {
1137 if (MustAllocateInContext(var)) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001138 ASSERT(var->IsUnallocated() || var->IsContextSlot());
1139 if (var->IsUnallocated()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001140 AllocateHeapSlot(var);
1141 }
1142 } else {
Ben Murdoch589d6972011-11-30 16:04:58 +00001143 ASSERT(var->IsUnallocated() || var->IsParameter());
1144 if (var->IsUnallocated()) {
1145 var->AllocateTo(Variable::PARAMETER, i);
Steve Blocka7e24c12009-10-30 11:49:00 +00001146 }
1147 }
1148 }
1149 }
1150}
1151
1152
1153void Scope::AllocateNonParameterLocal(Variable* var) {
1154 ASSERT(var->scope() == this);
Ben Murdoch589d6972011-11-30 16:04:58 +00001155 ASSERT(!var->IsVariable(isolate_->factory()->result_symbol()) ||
1156 !var->IsStackLocal());
1157 if (var->IsUnallocated() && MustAllocate(var)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001158 if (MustAllocateInContext(var)) {
1159 AllocateHeapSlot(var);
1160 } else {
1161 AllocateStackSlot(var);
1162 }
1163 }
1164}
1165
1166
1167void Scope::AllocateNonParameterLocals() {
1168 // All variables that have no rewrite yet are non-parameter locals.
1169 for (int i = 0; i < temps_.length(); i++) {
1170 AllocateNonParameterLocal(temps_[i]);
1171 }
1172
1173 for (VariableMap::Entry* p = variables_.Start();
1174 p != NULL;
1175 p = variables_.Next(p)) {
1176 Variable* var = reinterpret_cast<Variable*>(p->value);
1177 AllocateNonParameterLocal(var);
1178 }
1179
1180 // For now, function_ must be allocated at the very end. If it gets
1181 // allocated in the context, it must be the last slot in the context,
1182 // because of the current ScopeInfo implementation (see
1183 // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor).
1184 if (function_ != NULL) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001185 AllocateNonParameterLocal(function_->var());
Steve Blocka7e24c12009-10-30 11:49:00 +00001186 }
1187}
1188
1189
1190void Scope::AllocateVariablesRecursively() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001191 // Allocate variables for inner scopes.
1192 for (int i = 0; i < inner_scopes_.length(); i++) {
1193 inner_scopes_[i]->AllocateVariablesRecursively();
1194 }
1195
Ben Murdochb8e0da22011-05-16 14:20:40 +01001196 // If scope is already resolved, we still need to allocate
1197 // variables in inner scopes which might not had been resolved yet.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001198 if (already_resolved()) return;
Ben Murdochb8e0da22011-05-16 14:20:40 +01001199 // The number of slots required for variables.
1200 num_stack_slots_ = 0;
1201 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
1202
Steve Blocka7e24c12009-10-30 11:49:00 +00001203 // Allocate variables for this scope.
1204 // Parameters must be allocated first, if any.
1205 if (is_function_scope()) AllocateParameterLocals();
1206 AllocateNonParameterLocals();
1207
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001208 // Force allocation of a context for this scope if necessary. For a 'with'
1209 // scope and for a function scope that makes an 'eval' call we need a context,
1210 // even if no local variables were statically allocated in the scope.
1211 bool must_have_context = is_with_scope() ||
1212 (is_function_scope() && calls_eval());
Steve Blocka7e24c12009-10-30 11:49:00 +00001213
1214 // If we didn't allocate any locals in the local context, then we only
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001215 // need the minimal number of slots if we must have a context.
1216 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && !must_have_context) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001217 num_heap_slots_ = 0;
1218 }
1219
1220 // Allocation done.
1221 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
1222}
1223
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001224
1225int Scope::StackLocalCount() const {
1226 return num_stack_slots() -
1227 (function_ != NULL && function_->var()->IsStackLocal() ? 1 : 0);
1228}
1229
1230
1231int Scope::ContextLocalCount() const {
1232 if (num_heap_slots() == 0) return 0;
1233 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS -
1234 (function_ != NULL && function_->var()->IsContextSlot() ? 1 : 0);
1235}
1236
Steve Blocka7e24c12009-10-30 11:49:00 +00001237} } // namespace v8::internal