blob: 859cbd1ae61c8323c4f9edfb215ac1b8bfd53cc8 [file] [log] [blame]
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001// Copyright 2012 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// ----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +000043// Implementation of LocalsMap
44//
45// Note: We are storing the handle locations as key values in the hash map.
46// When inserting a new variable via Declare(), we rely on the fact that
47// the handle location remains alive for the duration of that variable
48// use. Because a Variable holding a handle with the same location exists
49// this is ensured.
50
51static bool Match(void* key1, void* key2) {
52 String* name1 = *reinterpret_cast<String**>(key1);
53 String* name2 = *reinterpret_cast<String**>(key2);
54 ASSERT(name1->IsSymbol());
55 ASSERT(name2->IsSymbol());
56 return name1 == name2;
57}
58
59
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +010060VariableMap::VariableMap() : ZoneHashMap(Match, 8) {}
Steve Blocka7e24c12009-10-30 11:49:00 +000061VariableMap::~VariableMap() {}
62
63
Ben Murdoch592a9fc2012-03-05 11:04:45 +000064Variable* VariableMap::Declare(
65 Scope* scope,
66 Handle<String> name,
67 VariableMode mode,
68 bool is_valid_lhs,
69 Variable::Kind kind,
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +010070 InitializationFlag initialization_flag,
71 Interface* interface) {
72 Entry* p = ZoneHashMap::Lookup(name.location(), name->Hash(), true);
Steve Blocka7e24c12009-10-30 11:49:00 +000073 if (p->value == NULL) {
74 // The variable has not been declared yet -> insert it.
75 ASSERT(p->key == name.location());
Ben Murdoch592a9fc2012-03-05 11:04:45 +000076 p->value = new Variable(scope,
77 name,
78 mode,
79 is_valid_lhs,
80 kind,
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +010081 initialization_flag,
82 interface);
Steve Blocka7e24c12009-10-30 11:49:00 +000083 }
84 return reinterpret_cast<Variable*>(p->value);
85}
86
87
88Variable* VariableMap::Lookup(Handle<String> name) {
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +010089 Entry* p = ZoneHashMap::Lookup(name.location(), name->Hash(), false);
Steve Blocka7e24c12009-10-30 11:49:00 +000090 if (p != NULL) {
91 ASSERT(*reinterpret_cast<String**>(p->key) == *name);
92 ASSERT(p->value != NULL);
93 return reinterpret_cast<Variable*>(p->value);
94 }
95 return NULL;
96}
97
98
99// ----------------------------------------------------------------------------
100// Implementation of Scope
101
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000102Scope::Scope(Scope* outer_scope, ScopeType type)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000103 : isolate_(Isolate::Current()),
104 inner_scopes_(4),
105 variables_(),
106 temps_(4),
107 params_(4),
108 unresolved_(16),
109 decls_(4),
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100110 interface_(FLAG_harmony_modules &&
111 (type == MODULE_SCOPE || type == GLOBAL_SCOPE)
112 ? Interface::NewModule() : NULL),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000113 already_resolved_(false) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000114 SetDefaults(type, outer_scope, Handle<ScopeInfo>::null());
Steve Blocka7e24c12009-10-30 11:49:00 +0000115 // At some point we might want to provide outer scopes to
116 // eval scopes (by walking the stack and reading the scope info).
117 // In that case, the ASSERT below needs to be adjusted.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000118 ASSERT_EQ(type == GLOBAL_SCOPE, outer_scope == NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000119 ASSERT(!HasIllegalRedeclaration());
120}
121
122
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000123Scope::Scope(Scope* inner_scope,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000124 ScopeType type,
125 Handle<ScopeInfo> scope_info)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000126 : isolate_(Isolate::Current()),
127 inner_scopes_(4),
128 variables_(),
129 temps_(4),
130 params_(4),
131 unresolved_(16),
132 decls_(4),
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100133 interface_(NULL),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000134 already_resolved_(true) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000135 SetDefaults(type, NULL, scope_info);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000136 if (!scope_info.is_null()) {
137 num_heap_slots_ = scope_info_->ContextLength();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100138 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000139 // Ensure at least MIN_CONTEXT_SLOTS to indicate a materialized context.
140 num_heap_slots_ = Max(num_heap_slots_,
141 static_cast<int>(Context::MIN_CONTEXT_SLOTS));
Steve Block44f0eee2011-05-26 01:26:41 +0100142 AddInnerScope(inner_scope);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000143}
Steve Block44f0eee2011-05-26 01:26:41 +0100144
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000145
146Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name)
147 : isolate_(Isolate::Current()),
148 inner_scopes_(1),
149 variables_(),
150 temps_(0),
151 params_(0),
152 unresolved_(0),
153 decls_(0),
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100154 interface_(NULL),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000155 already_resolved_(true) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000156 SetDefaults(CATCH_SCOPE, NULL, Handle<ScopeInfo>::null());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000157 AddInnerScope(inner_scope);
158 ++num_var_or_const_;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000159 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000160 Variable* variable = variables_.Declare(this,
161 catch_variable_name,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000162 VAR,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000163 true, // Valid left-hand side.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000164 Variable::NORMAL,
165 kCreatedInitialized);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000166 AllocateHeapSlot(variable);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100167}
168
169
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000170void Scope::SetDefaults(ScopeType type,
Ben Murdoch8b112d22011-06-08 16:22:53 +0100171 Scope* outer_scope,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000172 Handle<ScopeInfo> scope_info) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100173 outer_scope_ = outer_scope;
174 type_ = type;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000175 scope_name_ = isolate_->factory()->empty_symbol();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100176 dynamics_ = NULL;
177 receiver_ = NULL;
178 function_ = NULL;
179 arguments_ = NULL;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100180 illegal_redecl_ = NULL;
181 scope_inside_with_ = false;
182 scope_contains_with_ = false;
183 scope_calls_eval_ = false;
184 // Inherit the strict mode from the parent scope.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000185 language_mode_ = (outer_scope != NULL)
186 ? outer_scope->language_mode_ : CLASSIC_MODE;
Ben Murdoch257744e2011-11-30 15:57:28 +0000187 outer_scope_calls_non_strict_eval_ = false;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100188 inner_scope_calls_eval_ = false;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100189 force_eager_compilation_ = false;
Steve Block053d10c2011-06-13 19:13:29 +0100190 num_var_or_const_ = 0;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100191 num_stack_slots_ = 0;
192 num_heap_slots_ = 0;
193 scope_info_ = scope_info;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000194 start_position_ = RelocInfo::kNoPosition;
195 end_position_ = RelocInfo::kNoPosition;
196 if (!scope_info.is_null()) {
197 scope_calls_eval_ = scope_info->CallsEval();
198 language_mode_ = scope_info->language_mode();
199 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100200}
201
202
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000203Scope* Scope::DeserializeScopeChain(Context* context, Scope* global_scope) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000204 // Reconstruct the outer scope chain from a closure's context chain.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000205 Scope* current_scope = NULL;
Steve Block44f0eee2011-05-26 01:26:41 +0100206 Scope* innermost_scope = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000207 bool contains_with = false;
208 while (!context->IsGlobalContext()) {
209 if (context->IsWithContext()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000210 Scope* with_scope = new Scope(current_scope,
211 WITH_SCOPE,
212 Handle<ScopeInfo>::null());
213 current_scope = with_scope;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000214 // All the inner scopes are inside a with.
215 contains_with = true;
216 for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) {
217 s->scope_inside_with_ = true;
Steve Block44f0eee2011-05-26 01:26:41 +0100218 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000219 } else if (context->IsFunctionContext()) {
220 ScopeInfo* scope_info = context->closure()->shared()->scope_info();
221 current_scope = new Scope(current_scope,
222 FUNCTION_SCOPE,
223 Handle<ScopeInfo>(scope_info));
224 } else if (context->IsBlockContext()) {
225 ScopeInfo* scope_info = ScopeInfo::cast(context->extension());
226 current_scope = new Scope(current_scope,
227 BLOCK_SCOPE,
228 Handle<ScopeInfo>(scope_info));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000229 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000230 ASSERT(context->IsCatchContext());
231 String* name = String::cast(context->extension());
232 current_scope = new Scope(current_scope, Handle<String>(name));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000233 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000234 if (contains_with) current_scope->RecordWithStatement();
235 if (innermost_scope == NULL) innermost_scope = current_scope;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000236
237 // Forget about a with when we move to a context for a different function.
238 if (context->previous()->closure() != context->closure()) {
239 contains_with = false;
240 }
241 context = context->previous();
Steve Block44f0eee2011-05-26 01:26:41 +0100242 }
243
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000244 global_scope->AddInnerScope(current_scope);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000245 global_scope->PropagateScopeInfo(false);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000246 return (innermost_scope == NULL) ? global_scope : innermost_scope;
Steve Block44f0eee2011-05-26 01:26:41 +0100247}
248
Ben Murdochb8e0da22011-05-16 14:20:40 +0100249
Ben Murdochf87a2032010-10-22 12:50:53 +0100250bool Scope::Analyze(CompilationInfo* info) {
251 ASSERT(info->function() != NULL);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000252 Scope* scope = info->function()->scope();
253 Scope* top = scope;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100254
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000255 // Traverse the scope tree up to the first unresolved scope or the global
256 // scope and start scope resolution and variable allocation from that scope.
257 while (!top->is_global_scope() &&
258 !top->outer_scope()->already_resolved()) {
259 top = top->outer_scope();
260 }
261
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100262 // Allocate the variables.
263 {
264 AstNodeFactory<AstNullVisitor> ast_node_factory(info->isolate());
265 if (!top->AllocateVariables(info, &ast_node_factory)) return false;
266 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100267
268#ifdef DEBUG
Steve Block44f0eee2011-05-26 01:26:41 +0100269 if (info->isolate()->bootstrapper()->IsActive()
Ben Murdochf87a2032010-10-22 12:50:53 +0100270 ? FLAG_print_builtin_scopes
271 : FLAG_print_scopes) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000272 scope->Print();
Ben Murdochf87a2032010-10-22 12:50:53 +0100273 }
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100274
275 if (FLAG_harmony_modules && FLAG_print_interfaces && top->is_global_scope()) {
276 PrintF("global : ");
277 top->interface()->Print();
278 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100279#endif
280
Ben Murdochc7cc0282012-03-05 14:35:55 +0000281 if (FLAG_harmony_scoping) {
282 VariableProxy* proxy = scope->CheckAssignmentToConst();
283 if (proxy != NULL) {
284 // Found an assignment to const. Throw a syntax error.
285 MessageLocation location(info->script(),
286 proxy->position(),
287 proxy->position());
288 Isolate* isolate = info->isolate();
289 Factory* factory = isolate->factory();
290 Handle<JSArray> array = factory->NewJSArray(0);
291 Handle<Object> result =
292 factory->NewSyntaxError("harmony_const_assign", array);
293 isolate->Throw(*result, &location);
294 return false;
295 }
296 }
297
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000298 info->SetScope(scope);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000299 return true;
Ben Murdochf87a2032010-10-22 12:50:53 +0100300}
301
302
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000303void Scope::Initialize() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000304 ASSERT(!already_resolved());
Ben Murdochb8e0da22011-05-16 14:20:40 +0100305
Steve Blocka7e24c12009-10-30 11:49:00 +0000306 // Add this scope as a new inner scope of the outer scope.
307 if (outer_scope_ != NULL) {
308 outer_scope_->inner_scopes_.Add(this);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000309 scope_inside_with_ = outer_scope_->scope_inside_with_ || is_with_scope();
Steve Blocka7e24c12009-10-30 11:49:00 +0000310 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000311 scope_inside_with_ = is_with_scope();
Steve Blocka7e24c12009-10-30 11:49:00 +0000312 }
313
314 // Declare convenience variables.
315 // Declare and allocate receiver (even for the global scope, and even
316 // if naccesses_ == 0).
317 // NOTE: When loading parameters in the global scope, we must take
318 // care not to access them as properties of the global object, but
319 // instead load them directly from the stack. Currently, the only
320 // such parameter is 'this' which is passed on the stack when
321 // invoking scripts
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000322 if (is_declaration_scope()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000323 Variable* var =
324 variables_.Declare(this,
325 isolate_->factory()->this_symbol(),
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000326 VAR,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000327 false,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000328 Variable::THIS,
329 kCreatedInitialized);
Ben Murdoch589d6972011-11-30 16:04:58 +0000330 var->AllocateTo(Variable::PARAMETER, -1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000331 receiver_ = var;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000332 } else {
333 ASSERT(outer_scope() != NULL);
334 receiver_ = outer_scope()->receiver();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000335 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000336
337 if (is_function_scope()) {
338 // Declare 'arguments' variable which exists in all functions.
339 // Note that it might never be accessed, in which case it won't be
340 // allocated during variable allocation.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000341 variables_.Declare(this,
342 isolate_->factory()->arguments_symbol(),
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000343 VAR,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000344 true,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000345 Variable::ARGUMENTS,
346 kCreatedInitialized);
Steve Blocka7e24c12009-10-30 11:49:00 +0000347 }
348}
349
350
Ben Murdoch589d6972011-11-30 16:04:58 +0000351Scope* Scope::FinalizeBlockScope() {
352 ASSERT(is_block_scope());
353 ASSERT(temps_.is_empty());
354 ASSERT(params_.is_empty());
355
356 if (num_var_or_const() > 0) return this;
357
358 // Remove this scope from outer scope.
359 for (int i = 0; i < outer_scope_->inner_scopes_.length(); i++) {
360 if (outer_scope_->inner_scopes_[i] == this) {
361 outer_scope_->inner_scopes_.Remove(i);
362 break;
363 }
364 }
365
366 // Reparent inner scopes.
367 for (int i = 0; i < inner_scopes_.length(); i++) {
368 outer_scope()->AddInnerScope(inner_scopes_[i]);
369 }
370
371 // Move unresolved variables
372 for (int i = 0; i < unresolved_.length(); i++) {
373 outer_scope()->unresolved_.Add(unresolved_[i]);
374 }
375
376 return NULL;
377}
378
379
Steve Blocka7e24c12009-10-30 11:49:00 +0000380Variable* Scope::LocalLookup(Handle<String> name) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100381 Variable* result = variables_.Lookup(name);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000382 if (result != NULL || scope_info_.is_null()) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100383 return result;
384 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000385 // If we have a serialized scope info, we might find the variable there.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000386 // There should be no local slot with the given name.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100387 ASSERT(scope_info_->StackSlotIndex(*name) < 0);
388
389 // Check context slot lookup.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000390 VariableMode mode;
391 InitializationFlag init_flag;
392 int index = scope_info_->ContextSlotIndex(*name, &mode, &init_flag);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000393 if (index < 0) {
394 // Check parameters.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000395 mode = VAR;
396 init_flag = kCreatedInitialized;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000397 index = scope_info_->ParameterIndex(*name);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000398 if (index < 0) return NULL;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100399 }
400
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000401 Variable* var =
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000402 variables_.Declare(this,
403 name,
404 mode,
405 true,
406 Variable::NORMAL,
407 init_flag);
Ben Murdoch589d6972011-11-30 16:04:58 +0000408 var->AllocateTo(Variable::CONTEXT, index);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000409 return var;
Steve Blocka7e24c12009-10-30 11:49:00 +0000410}
411
412
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100413Variable* Scope::LookupFunctionVar(Handle<String> name,
414 AstNodeFactory<AstNullVisitor>* factory) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000415 if (function_ != NULL && function_->name().is_identical_to(name)) {
416 return function_->var();
417 } else if (!scope_info_.is_null()) {
418 // If we are backed by a scope info, try to lookup the variable there.
419 VariableMode mode;
420 int index = scope_info_->FunctionContextSlotIndex(*name, &mode);
421 if (index < 0) return NULL;
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100422 Variable* var = DeclareFunctionVar(name, mode, factory);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000423 var->AllocateTo(Variable::CONTEXT, index);
424 return var;
425 } else {
426 return NULL;
427 }
428}
429
430
Steve Blocka7e24c12009-10-30 11:49:00 +0000431Variable* Scope::Lookup(Handle<String> name) {
432 for (Scope* scope = this;
433 scope != NULL;
434 scope = scope->outer_scope()) {
435 Variable* var = scope->LocalLookup(name);
436 if (var != NULL) return var;
437 }
438 return NULL;
439}
440
441
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000442void Scope::DeclareParameter(Handle<String> name, VariableMode mode) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000443 ASSERT(!already_resolved());
444 ASSERT(is_function_scope());
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000445 Variable* var = variables_.Declare(
446 this, name, mode, true, Variable::NORMAL, kCreatedInitialized);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000447 params_.Add(var);
448}
449
450
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000451Variable* Scope::DeclareLocal(Handle<String> name,
452 VariableMode mode,
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100453 InitializationFlag init_flag,
454 Interface* interface) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000455 ASSERT(!already_resolved());
456 // This function handles VAR and CONST modes. DYNAMIC variables are
457 // introduces during variable allocation, INTERNAL variables are allocated
458 // explicitly, and TEMPORARY variables are allocated via NewTemporary().
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000459 ASSERT(mode == VAR ||
460 mode == CONST ||
461 mode == CONST_HARMONY ||
462 mode == LET);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000463 ++num_var_or_const_;
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100464 return variables_.Declare(
465 this, name, mode, true, Variable::NORMAL, init_flag, interface);
Steve Blocka7e24c12009-10-30 11:49:00 +0000466}
467
468
469Variable* Scope::DeclareGlobal(Handle<String> name) {
470 ASSERT(is_global_scope());
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000471 return variables_.Declare(this,
472 name,
473 DYNAMIC_GLOBAL,
Ben Murdoch589d6972011-11-30 16:04:58 +0000474 true,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000475 Variable::NORMAL,
476 kCreatedInitialized);
Steve Blocka7e24c12009-10-30 11:49:00 +0000477}
478
479
Steve Blocka7e24c12009-10-30 11:49:00 +0000480void Scope::RemoveUnresolved(VariableProxy* var) {
481 // Most likely (always?) any variable we want to remove
482 // was just added before, so we search backwards.
483 for (int i = unresolved_.length(); i-- > 0;) {
484 if (unresolved_[i] == var) {
485 unresolved_.Remove(i);
486 return;
487 }
488 }
489}
490
491
Ben Murdochb0fe1622011-05-05 13:52:32 +0100492Variable* Scope::NewTemporary(Handle<String> name) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000493 ASSERT(!already_resolved());
Ben Murdoch589d6972011-11-30 16:04:58 +0000494 Variable* var = new Variable(this,
495 name,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000496 TEMPORARY,
Ben Murdoch589d6972011-11-30 16:04:58 +0000497 true,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000498 Variable::NORMAL,
499 kCreatedInitialized);
Steve Blocka7e24c12009-10-30 11:49:00 +0000500 temps_.Add(var);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100501 return var;
Steve Blocka7e24c12009-10-30 11:49:00 +0000502}
503
504
505void Scope::AddDeclaration(Declaration* declaration) {
506 decls_.Add(declaration);
507}
508
509
510void Scope::SetIllegalRedeclaration(Expression* expression) {
Steve Block1e0659c2011-05-24 12:43:12 +0100511 // Record only the first illegal redeclaration.
Steve Blocka7e24c12009-10-30 11:49:00 +0000512 if (!HasIllegalRedeclaration()) {
513 illegal_redecl_ = expression;
514 }
515 ASSERT(HasIllegalRedeclaration());
516}
517
518
519void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) {
520 ASSERT(HasIllegalRedeclaration());
521 illegal_redecl_->Accept(visitor);
522}
523
524
Ben Murdoch589d6972011-11-30 16:04:58 +0000525Declaration* Scope::CheckConflictingVarDeclarations() {
526 int length = decls_.length();
527 for (int i = 0; i < length; i++) {
528 Declaration* decl = decls_[i];
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000529 if (decl->mode() != VAR) continue;
Ben Murdoch589d6972011-11-30 16:04:58 +0000530 Handle<String> name = decl->proxy()->name();
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000531
532 // Iterate through all scopes until and including the declaration scope.
533 Scope* previous = NULL;
534 Scope* current = decl->scope();
535 do {
Ben Murdoch589d6972011-11-30 16:04:58 +0000536 // There is a conflict if there exists a non-VAR binding.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000537 Variable* other_var = current->variables_.Lookup(name);
538 if (other_var != NULL && other_var->mode() != VAR) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000539 return decl;
540 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000541 previous = current;
542 current = current->outer_scope_;
543 } while (!previous->is_declaration_scope());
Ben Murdoch589d6972011-11-30 16:04:58 +0000544 }
545 return NULL;
546}
547
548
Ben Murdochc7cc0282012-03-05 14:35:55 +0000549VariableProxy* Scope::CheckAssignmentToConst() {
550 // Check this scope.
551 if (is_extended_mode()) {
552 for (int i = 0; i < unresolved_.length(); i++) {
553 ASSERT(unresolved_[i]->var() != NULL);
554 if (unresolved_[i]->var()->is_const_mode() &&
555 unresolved_[i]->IsLValue()) {
556 return unresolved_[i];
557 }
558 }
559 }
560
561 // Check inner scopes.
562 for (int i = 0; i < inner_scopes_.length(); i++) {
563 VariableProxy* proxy = inner_scopes_[i]->CheckAssignmentToConst();
564 if (proxy != NULL) return proxy;
565 }
566
567 // No assignments to const found.
568 return NULL;
569}
570
571
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000572void Scope::CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals,
573 ZoneList<Variable*>* context_locals) {
574 ASSERT(stack_locals != NULL);
575 ASSERT(context_locals != NULL);
576
577 // Collect temporaries which are always allocated on the stack.
Steve Blocka7e24c12009-10-30 11:49:00 +0000578 for (int i = 0; i < temps_.length(); i++) {
579 Variable* var = temps_[i];
Steve Block6ded16b2010-05-10 14:33:55 +0100580 if (var->is_used()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000581 ASSERT(var->IsStackLocal());
582 stack_locals->Add(var);
Steve Blocka7e24c12009-10-30 11:49:00 +0000583 }
584 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000585
586 // Collect declared local variables.
Steve Blocka7e24c12009-10-30 11:49:00 +0000587 for (VariableMap::Entry* p = variables_.Start();
588 p != NULL;
589 p = variables_.Next(p)) {
590 Variable* var = reinterpret_cast<Variable*>(p->value);
Steve Block6ded16b2010-05-10 14:33:55 +0100591 if (var->is_used()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000592 if (var->IsStackLocal()) {
593 stack_locals->Add(var);
594 } else if (var->IsContextSlot()) {
595 context_locals->Add(var);
596 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000597 }
598 }
599}
600
601
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100602bool Scope::AllocateVariables(CompilationInfo* info,
603 AstNodeFactory<AstNullVisitor>* factory) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000604 // 1) Propagate scope information.
Ben Murdoch257744e2011-11-30 15:57:28 +0000605 bool outer_scope_calls_non_strict_eval = false;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000606 if (outer_scope_ != NULL) {
607 outer_scope_calls_non_strict_eval =
608 outer_scope_->outer_scope_calls_non_strict_eval() |
609 outer_scope_->calls_non_strict_eval();
Ben Murdoch257744e2011-11-30 15:57:28 +0000610 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000611 PropagateScopeInfo(outer_scope_calls_non_strict_eval);
Steve Blocka7e24c12009-10-30 11:49:00 +0000612
613 // 2) Resolve variables.
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100614 if (!ResolveVariablesRecursively(info, factory)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000615
616 // 3) Allocate variables.
617 AllocateVariablesRecursively();
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100618
619 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000620}
621
622
623bool Scope::AllowsLazyCompilation() const {
624 return !force_eager_compilation_ && HasTrivialOuterContext();
625}
626
627
628bool Scope::HasTrivialContext() const {
629 // A function scope has a trivial context if it always is the global
630 // context. We iteratively scan out the context chain to see if
631 // there is anything that makes this scope non-trivial; otherwise we
632 // return true.
633 for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) {
634 if (scope->is_eval_scope()) return false;
635 if (scope->scope_inside_with_) return false;
636 if (scope->num_heap_slots_ > 0) return false;
637 }
638 return true;
639}
640
641
642bool Scope::HasTrivialOuterContext() const {
643 Scope* outer = outer_scope_;
644 if (outer == NULL) return true;
645 // Note that the outer context may be trivial in general, but the current
646 // scope may be inside a 'with' statement in which case the outer context
647 // for this scope is not trivial.
648 return !scope_inside_with_ && outer->HasTrivialContext();
649}
650
651
652int Scope::ContextChainLength(Scope* scope) {
653 int n = 0;
654 for (Scope* s = this; s != scope; s = s->outer_scope_) {
655 ASSERT(s != NULL); // scope must be in the scope chain
656 if (s->num_heap_slots() > 0) n++;
657 }
658 return n;
659}
660
661
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000662Scope* Scope::DeclarationScope() {
663 Scope* scope = this;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000664 while (!scope->is_declaration_scope()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000665 scope = scope->outer_scope();
666 }
667 return scope;
668}
669
670
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000671Handle<ScopeInfo> Scope::GetScopeInfo() {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000672 if (scope_info_.is_null()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000673 scope_info_ = ScopeInfo::Create(this);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000674 }
675 return scope_info_;
676}
677
678
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000679void Scope::GetNestedScopeChain(
680 List<Handle<ScopeInfo> >* chain,
681 int position) {
682 if (!is_eval_scope()) chain->Add(Handle<ScopeInfo>(GetScopeInfo()));
683
684 for (int i = 0; i < inner_scopes_.length(); i++) {
685 Scope* scope = inner_scopes_[i];
686 int beg_pos = scope->start_position();
687 int end_pos = scope->end_position();
688 ASSERT(beg_pos >= 0 && end_pos >= 0);
689 if (beg_pos <= position && position < end_pos) {
690 scope->GetNestedScopeChain(chain, position);
691 return;
692 }
693 }
694}
695
696
Steve Blocka7e24c12009-10-30 11:49:00 +0000697#ifdef DEBUG
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000698static const char* Header(ScopeType type) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000699 switch (type) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000700 case EVAL_SCOPE: return "eval";
701 case FUNCTION_SCOPE: return "function";
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100702 case MODULE_SCOPE: return "module";
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000703 case GLOBAL_SCOPE: return "global";
704 case CATCH_SCOPE: return "catch";
705 case BLOCK_SCOPE: return "block";
706 case WITH_SCOPE: return "with";
Steve Blocka7e24c12009-10-30 11:49:00 +0000707 }
708 UNREACHABLE();
709 return NULL;
710}
711
712
713static void Indent(int n, const char* str) {
714 PrintF("%*s%s", n, "", str);
715}
716
717
718static void PrintName(Handle<String> name) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000719 SmartArrayPointer<char> s = name->ToCString(DISALLOW_NULLS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000720 PrintF("%s", *s);
721}
722
723
Ben Murdoch589d6972011-11-30 16:04:58 +0000724static void PrintLocation(Variable* var) {
725 switch (var->location()) {
726 case Variable::UNALLOCATED:
727 break;
728 case Variable::PARAMETER:
729 PrintF("parameter[%d]", var->index());
730 break;
731 case Variable::LOCAL:
732 PrintF("local[%d]", var->index());
733 break;
734 case Variable::CONTEXT:
735 PrintF("context[%d]", var->index());
736 break;
737 case Variable::LOOKUP:
738 PrintF("lookup");
739 break;
740 }
741}
742
743
744static void PrintVar(int indent, Variable* var) {
745 if (var->is_used() || !var->IsUnallocated()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000746 Indent(indent, Variable::Mode2String(var->mode()));
747 PrintF(" ");
748 PrintName(var->name());
749 PrintF("; // ");
Ben Murdoch589d6972011-11-30 16:04:58 +0000750 PrintLocation(var);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000751 if (var->has_forced_context_allocation()) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000752 if (!var->IsUnallocated()) PrintF(", ");
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000753 PrintF("forced context allocation");
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000754 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000755 PrintF("\n");
756 }
757}
758
759
Ben Murdoch589d6972011-11-30 16:04:58 +0000760static void PrintMap(int indent, VariableMap* map) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000761 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
762 Variable* var = reinterpret_cast<Variable*>(p->value);
Ben Murdoch589d6972011-11-30 16:04:58 +0000763 PrintVar(indent, var);
Steve Blocka7e24c12009-10-30 11:49:00 +0000764 }
765}
766
767
768void Scope::Print(int n) {
769 int n0 = (n > 0 ? n : 0);
770 int n1 = n0 + 2; // indentation
771
772 // Print header.
773 Indent(n0, Header(type_));
774 if (scope_name_->length() > 0) {
775 PrintF(" ");
776 PrintName(scope_name_);
777 }
778
779 // Print parameters, if any.
780 if (is_function_scope()) {
781 PrintF(" (");
782 for (int i = 0; i < params_.length(); i++) {
783 if (i > 0) PrintF(", ");
784 PrintName(params_[i]->name());
785 }
786 PrintF(")");
787 }
788
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000789 PrintF(" { // (%d, %d)\n", start_position(), end_position());
Steve Blocka7e24c12009-10-30 11:49:00 +0000790
791 // Function name, if any (named function literals, only).
792 if (function_ != NULL) {
793 Indent(n1, "// (local) function name: ");
794 PrintName(function_->name());
795 PrintF("\n");
796 }
797
798 // Scope info.
799 if (HasTrivialOuterContext()) {
800 Indent(n1, "// scope has trivial outer context\n");
801 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000802 switch (language_mode()) {
803 case CLASSIC_MODE:
804 break;
805 case STRICT_MODE:
806 Indent(n1, "// strict mode scope\n");
807 break;
808 case EXTENDED_MODE:
809 Indent(n1, "// extended mode scope\n");
810 break;
811 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000812 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
813 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n");
814 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n");
Ben Murdoch257744e2011-11-30 15:57:28 +0000815 if (outer_scope_calls_non_strict_eval_) {
816 Indent(n1, "// outer scope calls 'eval' in non-strict context\n");
817 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000818 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000819 if (num_stack_slots_ > 0) { Indent(n1, "// ");
820 PrintF("%d stack slots\n", num_stack_slots_); }
821 if (num_heap_slots_ > 0) { Indent(n1, "// ");
822 PrintF("%d heap slots\n", num_heap_slots_); }
823
824 // Print locals.
Steve Blocka7e24c12009-10-30 11:49:00 +0000825 Indent(n1, "// function var\n");
826 if (function_ != NULL) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000827 PrintVar(n1, function_->var());
Steve Blocka7e24c12009-10-30 11:49:00 +0000828 }
829
830 Indent(n1, "// temporary vars\n");
831 for (int i = 0; i < temps_.length(); i++) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000832 PrintVar(n1, temps_[i]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000833 }
834
835 Indent(n1, "// local vars\n");
Ben Murdoch589d6972011-11-30 16:04:58 +0000836 PrintMap(n1, &variables_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000837
838 Indent(n1, "// dynamic vars\n");
839 if (dynamics_ != NULL) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000840 PrintMap(n1, dynamics_->GetMap(DYNAMIC));
841 PrintMap(n1, dynamics_->GetMap(DYNAMIC_LOCAL));
842 PrintMap(n1, dynamics_->GetMap(DYNAMIC_GLOBAL));
Steve Blocka7e24c12009-10-30 11:49:00 +0000843 }
844
845 // Print inner scopes (disable by providing negative n).
846 if (n >= 0) {
847 for (int i = 0; i < inner_scopes_.length(); i++) {
848 PrintF("\n");
849 inner_scopes_[i]->Print(n1);
850 }
851 }
852
853 Indent(n0, "}\n");
854}
855#endif // DEBUG
856
857
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000858Variable* Scope::NonLocal(Handle<String> name, VariableMode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000859 if (dynamics_ == NULL) dynamics_ = new DynamicScopePart();
860 VariableMap* map = dynamics_->GetMap(mode);
861 Variable* var = map->Lookup(name);
862 if (var == NULL) {
863 // Declare a new non-local.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000864 InitializationFlag init_flag = (mode == VAR)
865 ? kCreatedInitialized : kNeedsInitialization;
866 var = map->Declare(NULL,
867 name,
868 mode,
869 true,
870 Variable::NORMAL,
871 init_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000872 // Allocate it by giving it a dynamic lookup.
Ben Murdoch589d6972011-11-30 16:04:58 +0000873 var->AllocateTo(Variable::LOOKUP, -1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000874 }
875 return var;
876}
877
878
Steve Blocka7e24c12009-10-30 11:49:00 +0000879Variable* Scope::LookupRecursive(Handle<String> name,
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100880 BindingKind* binding_kind,
881 AstNodeFactory<AstNullVisitor>* factory) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000882 ASSERT(binding_kind != NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000883 // Try to find the variable in this scope.
884 Variable* var = LocalLookup(name);
885
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000886 // We found a variable and we are done. (Even if there is an 'eval' in
887 // this scope which introduces the same variable again, the resulting
888 // variable remains the same.)
Steve Blocka7e24c12009-10-30 11:49:00 +0000889 if (var != NULL) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000890 *binding_kind = BOUND;
891 return var;
892 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000893
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000894 // We did not find a variable locally. Check against the function variable,
895 // if any. We can do this for all scopes, since the function variable is
896 // only present - if at all - for function scopes.
897 *binding_kind = UNBOUND;
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100898 var = LookupFunctionVar(name, factory);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000899 if (var != NULL) {
900 *binding_kind = BOUND;
901 } else if (outer_scope_ != NULL) {
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100902 var = outer_scope_->LookupRecursive(name, binding_kind, factory);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000903 if (*binding_kind == BOUND && (is_function_scope() || is_with_scope())) {
904 var->ForceContextAllocation();
Steve Blocka7e24c12009-10-30 11:49:00 +0000905 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000906 } else {
907 ASSERT(is_global_scope());
Steve Blocka7e24c12009-10-30 11:49:00 +0000908 }
909
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000910 if (is_with_scope()) {
911 // The current scope is a with scope, so the variable binding can not be
912 // statically resolved. However, note that it was necessary to do a lookup
913 // in the outer scope anyway, because if a binding exists in an outer scope,
914 // the associated variable has to be marked as potentially being accessed
915 // from inside of an inner with scope (the property may not be in the 'with'
916 // object).
917 *binding_kind = DYNAMIC_LOOKUP;
918 return NULL;
919 } else if (calls_non_strict_eval()) {
920 // A variable binding may have been found in an outer scope, but the current
921 // scope makes a non-strict 'eval' call, so the found variable may not be
922 // the correct one (the 'eval' may introduce a binding with the same name).
923 // In that case, change the lookup result to reflect this situation.
924 if (*binding_kind == BOUND) {
925 *binding_kind = BOUND_EVAL_SHADOWED;
926 } else if (*binding_kind == UNBOUND) {
927 *binding_kind = UNBOUND_EVAL_SHADOWED;
928 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100929 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000930 return var;
931}
932
933
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100934bool Scope::ResolveVariable(CompilationInfo* info,
935 VariableProxy* proxy,
936 AstNodeFactory<AstNullVisitor>* factory) {
937 ASSERT(info->global_scope()->is_global_scope());
Steve Blocka7e24c12009-10-30 11:49:00 +0000938
939 // If the proxy is already resolved there's nothing to do
940 // (functions and consts may be resolved by the parser).
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100941 if (proxy->var() != NULL) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000942
943 // Otherwise, try to resolve the variable.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000944 BindingKind binding_kind;
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100945 Variable* var = LookupRecursive(proxy->name(), &binding_kind, factory);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000946 switch (binding_kind) {
947 case BOUND:
948 // We found a variable binding.
949 break;
Steve Blocka7e24c12009-10-30 11:49:00 +0000950
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000951 case BOUND_EVAL_SHADOWED:
952 // We found a variable variable binding that might be shadowed
953 // by 'eval' introduced variable bindings.
954 if (var->is_global()) {
955 var = NonLocal(proxy->name(), DYNAMIC_GLOBAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000956 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000957 Variable* invalidated = var;
958 var = NonLocal(proxy->name(), DYNAMIC_LOCAL);
959 var->set_local_if_not_shadowed(invalidated);
Steve Blocka7e24c12009-10-30 11:49:00 +0000960 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000961 break;
962
963 case UNBOUND:
964 // No binding has been found. Declare a variable in global scope.
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100965 var = info->global_scope()->DeclareGlobal(proxy->name());
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000966 break;
967
968 case UNBOUND_EVAL_SHADOWED:
969 // No binding has been found. But some scope makes a
970 // non-strict 'eval' call.
971 var = NonLocal(proxy->name(), DYNAMIC_GLOBAL);
972 break;
973
974 case DYNAMIC_LOOKUP:
975 // The variable could not be resolved statically.
976 var = NonLocal(proxy->name(), DYNAMIC);
977 break;
Steve Blocka7e24c12009-10-30 11:49:00 +0000978 }
979
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000980 ASSERT(var != NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000981 proxy->BindTo(var);
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100982
983 if (FLAG_harmony_modules) {
984 bool ok;
985#ifdef DEBUG
986 if (FLAG_print_interface_details)
987 PrintF("# Resolve %s:\n", var->name()->ToAsciiArray());
988#endif
989 proxy->interface()->Unify(var->interface(), &ok);
990 if (!ok) {
991#ifdef DEBUG
992 if (FLAG_print_interfaces) {
993 PrintF("SCOPES TYPE ERROR\n");
994 PrintF("proxy: ");
995 proxy->interface()->Print();
996 PrintF("var: ");
997 var->interface()->Print();
998 }
999#endif
1000
1001 // Inconsistent use of module. Throw a syntax error.
1002 // TODO(rossberg): generate more helpful error message.
1003 MessageLocation location(info->script(),
1004 proxy->position(),
1005 proxy->position());
1006 Isolate* isolate = Isolate::Current();
1007 Factory* factory = isolate->factory();
1008 Handle<JSArray> array = factory->NewJSArray(1);
1009 USE(JSObject::SetElement(array, 0, var->name(), NONE, kStrictMode));
1010 Handle<Object> result =
1011 factory->NewSyntaxError("module_type_error", array);
1012 isolate->Throw(*result, &location);
1013 return false;
1014 }
1015 }
1016
1017 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001018}
1019
1020
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001021bool Scope::ResolveVariablesRecursively(
1022 CompilationInfo* info,
1023 AstNodeFactory<AstNullVisitor>* factory) {
1024 ASSERT(info->global_scope()->is_global_scope());
Steve Blocka7e24c12009-10-30 11:49:00 +00001025
1026 // Resolve unresolved variables for this scope.
1027 for (int i = 0; i < unresolved_.length(); i++) {
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001028 if (!ResolveVariable(info, unresolved_[i], factory)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001029 }
1030
1031 // Resolve unresolved variables for inner scopes.
1032 for (int i = 0; i < inner_scopes_.length(); i++) {
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001033 if (!inner_scopes_[i]->ResolveVariablesRecursively(info, factory))
1034 return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001035 }
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001036
1037 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00001038}
1039
1040
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001041bool Scope::PropagateScopeInfo(bool outer_scope_calls_non_strict_eval ) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001042 if (outer_scope_calls_non_strict_eval) {
1043 outer_scope_calls_non_strict_eval_ = true;
1044 }
1045
Ben Murdoch257744e2011-11-30 15:57:28 +00001046 bool calls_non_strict_eval =
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001047 this->calls_non_strict_eval() || outer_scope_calls_non_strict_eval_;
Steve Blocka7e24c12009-10-30 11:49:00 +00001048 for (int i = 0; i < inner_scopes_.length(); i++) {
1049 Scope* inner_scope = inner_scopes_[i];
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001050 if (inner_scope->PropagateScopeInfo(calls_non_strict_eval)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001051 inner_scope_calls_eval_ = true;
1052 }
1053 if (inner_scope->force_eager_compilation_) {
1054 force_eager_compilation_ = true;
1055 }
1056 }
1057
1058 return scope_calls_eval_ || inner_scope_calls_eval_;
1059}
1060
1061
1062bool Scope::MustAllocate(Variable* var) {
1063 // Give var a read/write use if there is a chance it might be accessed
1064 // via an eval() call. This is only possible if the variable has a
1065 // visible name.
1066 if ((var->is_this() || var->name()->length() > 0) &&
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001067 (var->has_forced_context_allocation() ||
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001068 scope_calls_eval_ ||
1069 inner_scope_calls_eval_ ||
1070 scope_contains_with_ ||
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001071 is_catch_scope() ||
1072 is_block_scope())) {
Steve Block6ded16b2010-05-10 14:33:55 +01001073 var->set_is_used(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001074 }
1075 // Global variables do not need to be allocated.
Steve Block6ded16b2010-05-10 14:33:55 +01001076 return !var->is_global() && var->is_used();
Steve Blocka7e24c12009-10-30 11:49:00 +00001077}
1078
1079
1080bool Scope::MustAllocateInContext(Variable* var) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001081 // If var is accessed from an inner scope, or if there is a possibility
1082 // that it might be accessed from the current or an inner scope (through
1083 // an eval() call or a runtime with lookup), it must be allocated in the
Steve Blocka7e24c12009-10-30 11:49:00 +00001084 // context.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001085 //
1086 // Exceptions: temporary variables are never allocated in a context;
1087 // catch-bound variables are always allocated in a context.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001088 if (var->mode() == TEMPORARY) return false;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001089 if (is_catch_scope() || is_block_scope()) return true;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001090 return var->has_forced_context_allocation() ||
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001091 scope_calls_eval_ ||
1092 inner_scope_calls_eval_ ||
1093 scope_contains_with_ ||
1094 var->is_global();
Steve Blocka7e24c12009-10-30 11:49:00 +00001095}
1096
1097
1098bool Scope::HasArgumentsParameter() {
1099 for (int i = 0; i < params_.length(); i++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001100 if (params_[i]->name().is_identical_to(
1101 isolate_->factory()->arguments_symbol())) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001102 return true;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001103 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001104 }
1105 return false;
1106}
1107
1108
1109void Scope::AllocateStackSlot(Variable* var) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001110 var->AllocateTo(Variable::LOCAL, num_stack_slots_++);
Steve Blocka7e24c12009-10-30 11:49:00 +00001111}
1112
1113
1114void Scope::AllocateHeapSlot(Variable* var) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001115 var->AllocateTo(Variable::CONTEXT, num_heap_slots_++);
Steve Blocka7e24c12009-10-30 11:49:00 +00001116}
1117
1118
1119void Scope::AllocateParameterLocals() {
1120 ASSERT(is_function_scope());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001121 Variable* arguments = LocalLookup(isolate_->factory()->arguments_symbol());
Steve Blocka7e24c12009-10-30 11:49:00 +00001122 ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly
Steve Block44f0eee2011-05-26 01:26:41 +01001123
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001124 bool uses_nonstrict_arguments = false;
Steve Block44f0eee2011-05-26 01:26:41 +01001125
Steve Blocka7e24c12009-10-30 11:49:00 +00001126 if (MustAllocate(arguments) && !HasArgumentsParameter()) {
1127 // 'arguments' is used. Unless there is also a parameter called
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001128 // 'arguments', we must be conservative and allocate all parameters to
1129 // the context assuming they will be captured by the arguments object.
1130 // If we have a parameter named 'arguments', a (new) value is always
1131 // assigned to it via the function invocation. Then 'arguments' denotes
1132 // that specific parameter value and cannot be used to access the
1133 // parameters, which is why we don't need to allocate an arguments
1134 // object in that case.
Steve Blocka7e24c12009-10-30 11:49:00 +00001135
1136 // We are using 'arguments'. Tell the code generator that is needs to
1137 // allocate the arguments object by setting 'arguments_'.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001138 arguments_ = arguments;
Steve Blocka7e24c12009-10-30 11:49:00 +00001139
Steve Block44f0eee2011-05-26 01:26:41 +01001140 // In strict mode 'arguments' does not alias formal parameters.
1141 // Therefore in strict mode we allocate parameters as if 'arguments'
1142 // were not used.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001143 uses_nonstrict_arguments = is_classic_mode();
Steve Block44f0eee2011-05-26 01:26:41 +01001144 }
1145
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001146 // The same parameter may occur multiple times in the parameters_ list.
1147 // If it does, and if it is not copied into the context object, it must
1148 // receive the highest parameter index for that parameter; thus iteration
1149 // order is relevant!
1150 for (int i = params_.length() - 1; i >= 0; --i) {
1151 Variable* var = params_[i];
1152 ASSERT(var->scope() == this);
1153 if (uses_nonstrict_arguments) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001154 // Force context allocation of the parameter.
1155 var->ForceContextAllocation();
Steve Blocka7e24c12009-10-30 11:49:00 +00001156 }
1157
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001158 if (MustAllocate(var)) {
1159 if (MustAllocateInContext(var)) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001160 ASSERT(var->IsUnallocated() || var->IsContextSlot());
1161 if (var->IsUnallocated()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001162 AllocateHeapSlot(var);
1163 }
1164 } else {
Ben Murdoch589d6972011-11-30 16:04:58 +00001165 ASSERT(var->IsUnallocated() || var->IsParameter());
1166 if (var->IsUnallocated()) {
1167 var->AllocateTo(Variable::PARAMETER, i);
Steve Blocka7e24c12009-10-30 11:49:00 +00001168 }
1169 }
1170 }
1171 }
1172}
1173
1174
1175void Scope::AllocateNonParameterLocal(Variable* var) {
1176 ASSERT(var->scope() == this);
Ben Murdoch589d6972011-11-30 16:04:58 +00001177 ASSERT(!var->IsVariable(isolate_->factory()->result_symbol()) ||
1178 !var->IsStackLocal());
1179 if (var->IsUnallocated() && MustAllocate(var)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001180 if (MustAllocateInContext(var)) {
1181 AllocateHeapSlot(var);
1182 } else {
1183 AllocateStackSlot(var);
1184 }
1185 }
1186}
1187
1188
1189void Scope::AllocateNonParameterLocals() {
1190 // All variables that have no rewrite yet are non-parameter locals.
1191 for (int i = 0; i < temps_.length(); i++) {
1192 AllocateNonParameterLocal(temps_[i]);
1193 }
1194
1195 for (VariableMap::Entry* p = variables_.Start();
1196 p != NULL;
1197 p = variables_.Next(p)) {
1198 Variable* var = reinterpret_cast<Variable*>(p->value);
1199 AllocateNonParameterLocal(var);
1200 }
1201
1202 // For now, function_ must be allocated at the very end. If it gets
1203 // allocated in the context, it must be the last slot in the context,
1204 // because of the current ScopeInfo implementation (see
1205 // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor).
1206 if (function_ != NULL) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001207 AllocateNonParameterLocal(function_->var());
Steve Blocka7e24c12009-10-30 11:49:00 +00001208 }
1209}
1210
1211
1212void Scope::AllocateVariablesRecursively() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001213 // Allocate variables for inner scopes.
1214 for (int i = 0; i < inner_scopes_.length(); i++) {
1215 inner_scopes_[i]->AllocateVariablesRecursively();
1216 }
1217
Ben Murdochb8e0da22011-05-16 14:20:40 +01001218 // If scope is already resolved, we still need to allocate
1219 // variables in inner scopes which might not had been resolved yet.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001220 if (already_resolved()) return;
Ben Murdochb8e0da22011-05-16 14:20:40 +01001221 // The number of slots required for variables.
1222 num_stack_slots_ = 0;
1223 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
1224
Steve Blocka7e24c12009-10-30 11:49:00 +00001225 // Allocate variables for this scope.
1226 // Parameters must be allocated first, if any.
1227 if (is_function_scope()) AllocateParameterLocals();
1228 AllocateNonParameterLocals();
1229
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001230 // Force allocation of a context for this scope if necessary. For a 'with'
1231 // scope and for a function scope that makes an 'eval' call we need a context,
1232 // even if no local variables were statically allocated in the scope.
1233 bool must_have_context = is_with_scope() ||
1234 (is_function_scope() && calls_eval());
Steve Blocka7e24c12009-10-30 11:49:00 +00001235
1236 // If we didn't allocate any locals in the local context, then we only
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001237 // need the minimal number of slots if we must have a context.
1238 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && !must_have_context) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001239 num_heap_slots_ = 0;
1240 }
1241
1242 // Allocation done.
1243 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
1244}
1245
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001246
1247int Scope::StackLocalCount() const {
1248 return num_stack_slots() -
1249 (function_ != NULL && function_->var()->IsStackLocal() ? 1 : 0);
1250}
1251
1252
1253int Scope::ContextLocalCount() const {
1254 if (num_heap_slots() == 0) return 0;
1255 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS -
1256 (function_ != NULL && function_->var()->IsContextSlot() ? 1 : 0);
1257}
1258
Steve Blocka7e24c12009-10-30 11:49:00 +00001259} } // namespace v8::internal