diff options
| author | Ben Murdoch <benm@google.com> | 2012-04-12 10:51:47 +0100 |
|---|---|---|
| committer | Ben Murdoch <benm@google.com> | 2012-04-16 16:41:38 +0100 |
| commit | 3ef787dbeca8a5fb1086949cda830dccee07bfbd (patch) | |
| tree | 0a22edd97aa148abffdd405c585b22213fccbc82 /src/scopes.cc | |
| parent | 85b71799222b55eb5dd74ea26efe0c64ab655c8c (diff) | |
| download | android_external_v8-3ef787dbeca8a5fb1086949cda830dccee07bfbd.tar.gz android_external_v8-3ef787dbeca8a5fb1086949cda830dccee07bfbd.tar.bz2 android_external_v8-3ef787dbeca8a5fb1086949cda830dccee07bfbd.zip | |
Merge V8 at 3.9.24.13
Bug: 5688872
Change-Id: Id0aa8d23375030494d3189c31774059c0f5398fc
Diffstat (limited to 'src/scopes.cc')
| -rw-r--r-- | src/scopes.cc | 823 |
1 files changed, 437 insertions, 386 deletions
diff --git a/src/scopes.cc b/src/scopes.cc index d5a7a9f9..859cbd1a 100644 --- a/src/scopes.cc +++ b/src/scopes.cc @@ -1,4 +1,4 @@ -// Copyright 2011 the V8 project authors. All rights reserved. +// Copyright 2012 the V8 project authors. All rights reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: @@ -31,6 +31,7 @@ #include "bootstrapper.h" #include "compiler.h" +#include "messages.h" #include "scopeinfo.h" #include "allocation-inl.h" @@ -39,26 +40,6 @@ namespace v8 { namespace internal { // ---------------------------------------------------------------------------- -// A Zone allocator for use with LocalsMap. - -// TODO(isolates): It is probably worth it to change the Allocator class to -// take a pointer to an isolate. -class ZoneAllocator: public Allocator { - public: - /* nothing to do */ - virtual ~ZoneAllocator() {} - - virtual void* New(size_t size) { return ZONE->New(static_cast<int>(size)); } - - /* ignored - Zone is freed in one fell swoop */ - virtual void Delete(void* p) {} -}; - - -static ZoneAllocator LocalsMapAllocator; - - -// ---------------------------------------------------------------------------- // Implementation of LocalsMap // // Note: We are storing the handle locations as key values in the hash map. @@ -76,30 +57,36 @@ static bool Match(void* key1, void* key2) { } -// Dummy constructor -VariableMap::VariableMap(bool gotta_love_static_overloading) : HashMap() {} - -VariableMap::VariableMap() : HashMap(Match, &LocalsMapAllocator, 8) {} +VariableMap::VariableMap() : ZoneHashMap(Match, 8) {} VariableMap::~VariableMap() {} -Variable* VariableMap::Declare(Scope* scope, - Handle<String> name, - Variable::Mode mode, - bool is_valid_lhs, - Variable::Kind kind) { - HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), true); +Variable* VariableMap::Declare( + Scope* scope, + Handle<String> name, + VariableMode mode, + bool is_valid_lhs, + Variable::Kind kind, + InitializationFlag initialization_flag, + Interface* interface) { + Entry* p = ZoneHashMap::Lookup(name.location(), name->Hash(), true); if (p->value == NULL) { // The variable has not been declared yet -> insert it. ASSERT(p->key == name.location()); - p->value = new Variable(scope, name, mode, is_valid_lhs, kind); + p->value = new Variable(scope, + name, + mode, + is_valid_lhs, + kind, + initialization_flag, + interface); } return reinterpret_cast<Variable*>(p->value); } Variable* VariableMap::Lookup(Handle<String> name) { - HashMap::Entry* p = HashMap::Lookup(name.location(), name->Hash(), false); + Entry* p = ZoneHashMap::Lookup(name.location(), name->Hash(), false); if (p != NULL) { ASSERT(*reinterpret_cast<String**>(p->key) == *name); ASSERT(p->value != NULL); @@ -112,22 +99,7 @@ Variable* VariableMap::Lookup(Handle<String> name) { // ---------------------------------------------------------------------------- // Implementation of Scope - -// Dummy constructor -Scope::Scope(Type type) - : isolate_(Isolate::Current()), - inner_scopes_(0), - variables_(false), - temps_(0), - params_(0), - unresolved_(0), - decls_(0), - already_resolved_(false) { - SetDefaults(type, NULL, Handle<SerializedScopeInfo>::null()); -} - - -Scope::Scope(Scope* outer_scope, Type type) +Scope::Scope(Scope* outer_scope, ScopeType type) : isolate_(Isolate::Current()), inner_scopes_(4), variables_(), @@ -135,19 +107,22 @@ Scope::Scope(Scope* outer_scope, Type type) params_(4), unresolved_(16), decls_(4), + interface_(FLAG_harmony_modules && + (type == MODULE_SCOPE || type == GLOBAL_SCOPE) + ? Interface::NewModule() : NULL), already_resolved_(false) { - SetDefaults(type, outer_scope, Handle<SerializedScopeInfo>::null()); + SetDefaults(type, outer_scope, Handle<ScopeInfo>::null()); // At some point we might want to provide outer scopes to // eval scopes (by walking the stack and reading the scope info). // In that case, the ASSERT below needs to be adjusted. - ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL)); + ASSERT_EQ(type == GLOBAL_SCOPE, outer_scope == NULL); ASSERT(!HasIllegalRedeclaration()); } Scope::Scope(Scope* inner_scope, - Type type, - Handle<SerializedScopeInfo> scope_info) + ScopeType type, + Handle<ScopeInfo> scope_info) : isolate_(Isolate::Current()), inner_scopes_(4), variables_(), @@ -155,12 +130,15 @@ Scope::Scope(Scope* inner_scope, params_(4), unresolved_(16), decls_(4), + interface_(NULL), already_resolved_(true) { - ASSERT(!scope_info.is_null()); SetDefaults(type, NULL, scope_info); - if (scope_info->HasHeapAllocatedLocals()) { - num_heap_slots_ = scope_info_->NumberOfContextSlots(); + if (!scope_info.is_null()) { + num_heap_slots_ = scope_info_->ContextLength(); } + // Ensure at least MIN_CONTEXT_SLOTS to indicate a materialized context. + num_heap_slots_ = Max(num_heap_slots_, + static_cast<int>(Context::MIN_CONTEXT_SLOTS)); AddInnerScope(inner_scope); } @@ -173,22 +151,25 @@ Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name) params_(0), unresolved_(0), decls_(0), + interface_(NULL), already_resolved_(true) { - SetDefaults(CATCH_SCOPE, NULL, Handle<SerializedScopeInfo>::null()); + SetDefaults(CATCH_SCOPE, NULL, Handle<ScopeInfo>::null()); AddInnerScope(inner_scope); ++num_var_or_const_; + num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; Variable* variable = variables_.Declare(this, catch_variable_name, - Variable::VAR, + VAR, true, // Valid left-hand side. - Variable::NORMAL); + Variable::NORMAL, + kCreatedInitialized); AllocateHeapSlot(variable); } -void Scope::SetDefaults(Type type, +void Scope::SetDefaults(ScopeType type, Scope* outer_scope, - Handle<SerializedScopeInfo> scope_info) { + Handle<ScopeInfo> scope_info) { outer_scope_ = outer_scope; type_ = type; scope_name_ = isolate_->factory()->empty_symbol(); @@ -201,53 +182,57 @@ void Scope::SetDefaults(Type type, scope_contains_with_ = false; scope_calls_eval_ = false; // Inherit the strict mode from the parent scope. - strict_mode_ = (outer_scope != NULL) && outer_scope->strict_mode_; - outer_scope_calls_eval_ = false; + language_mode_ = (outer_scope != NULL) + ? outer_scope->language_mode_ : CLASSIC_MODE; outer_scope_calls_non_strict_eval_ = false; inner_scope_calls_eval_ = false; - outer_scope_is_eval_scope_ = false; force_eager_compilation_ = false; num_var_or_const_ = 0; num_stack_slots_ = 0; num_heap_slots_ = 0; scope_info_ = scope_info; + start_position_ = RelocInfo::kNoPosition; + end_position_ = RelocInfo::kNoPosition; + if (!scope_info.is_null()) { + scope_calls_eval_ = scope_info->CallsEval(); + language_mode_ = scope_info->language_mode(); + } } -Scope* Scope::DeserializeScopeChain(CompilationInfo* info, - Scope* global_scope) { +Scope* Scope::DeserializeScopeChain(Context* context, Scope* global_scope) { // Reconstruct the outer scope chain from a closure's context chain. - ASSERT(!info->closure().is_null()); - Context* context = info->closure()->context(); Scope* current_scope = NULL; Scope* innermost_scope = NULL; bool contains_with = false; while (!context->IsGlobalContext()) { if (context->IsWithContext()) { + Scope* with_scope = new Scope(current_scope, + WITH_SCOPE, + Handle<ScopeInfo>::null()); + current_scope = with_scope; // All the inner scopes are inside a with. contains_with = true; for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) { s->scope_inside_with_ = true; } + } else if (context->IsFunctionContext()) { + ScopeInfo* scope_info = context->closure()->shared()->scope_info(); + current_scope = new Scope(current_scope, + FUNCTION_SCOPE, + Handle<ScopeInfo>(scope_info)); + } else if (context->IsBlockContext()) { + ScopeInfo* scope_info = ScopeInfo::cast(context->extension()); + current_scope = new Scope(current_scope, + BLOCK_SCOPE, + Handle<ScopeInfo>(scope_info)); } else { - if (context->IsFunctionContext()) { - SerializedScopeInfo* scope_info = - context->closure()->shared()->scope_info(); - current_scope = new Scope(current_scope, FUNCTION_SCOPE, - Handle<SerializedScopeInfo>(scope_info)); - } else if (context->IsBlockContext()) { - SerializedScopeInfo* scope_info = - SerializedScopeInfo::cast(context->extension()); - current_scope = new Scope(current_scope, BLOCK_SCOPE, - Handle<SerializedScopeInfo>(scope_info)); - } else { - ASSERT(context->IsCatchContext()); - String* name = String::cast(context->extension()); - current_scope = new Scope(current_scope, Handle<String>(name)); - } - if (contains_with) current_scope->RecordWithStatement(); - if (innermost_scope == NULL) innermost_scope = current_scope; + ASSERT(context->IsCatchContext()); + String* name = String::cast(context->extension()); + current_scope = new Scope(current_scope, Handle<String>(name)); } + if (contains_with) current_scope->RecordWithStatement(); + if (innermost_scope == NULL) innermost_scope = current_scope; // Forget about a with when we move to a context for a different function. if (context->previous()->closure() != context->closure()) { @@ -257,39 +242,73 @@ Scope* Scope::DeserializeScopeChain(CompilationInfo* info, } global_scope->AddInnerScope(current_scope); + global_scope->PropagateScopeInfo(false); return (innermost_scope == NULL) ? global_scope : innermost_scope; } bool Scope::Analyze(CompilationInfo* info) { ASSERT(info->function() != NULL); - Scope* top = info->function()->scope(); + Scope* scope = info->function()->scope(); + Scope* top = scope; + + // Traverse the scope tree up to the first unresolved scope or the global + // scope and start scope resolution and variable allocation from that scope. + while (!top->is_global_scope() && + !top->outer_scope()->already_resolved()) { + top = top->outer_scope(); + } - while (top->outer_scope() != NULL) top = top->outer_scope(); - top->AllocateVariables(info->calling_context()); + // Allocate the variables. + { + AstNodeFactory<AstNullVisitor> ast_node_factory(info->isolate()); + if (!top->AllocateVariables(info, &ast_node_factory)) return false; + } #ifdef DEBUG if (info->isolate()->bootstrapper()->IsActive() ? FLAG_print_builtin_scopes : FLAG_print_scopes) { - info->function()->scope()->Print(); + scope->Print(); + } + + if (FLAG_harmony_modules && FLAG_print_interfaces && top->is_global_scope()) { + PrintF("global : "); + top->interface()->Print(); } #endif - info->SetScope(info->function()->scope()); - return true; // Can not fail. + if (FLAG_harmony_scoping) { + VariableProxy* proxy = scope->CheckAssignmentToConst(); + if (proxy != NULL) { + // Found an assignment to const. Throw a syntax error. + MessageLocation location(info->script(), + proxy->position(), + proxy->position()); + Isolate* isolate = info->isolate(); + Factory* factory = isolate->factory(); + Handle<JSArray> array = factory->NewJSArray(0); + Handle<Object> result = + factory->NewSyntaxError("harmony_const_assign", array); + isolate->Throw(*result, &location); + return false; + } + } + + info->SetScope(scope); + return true; } -void Scope::Initialize(bool inside_with) { +void Scope::Initialize() { ASSERT(!already_resolved()); // Add this scope as a new inner scope of the outer scope. if (outer_scope_ != NULL) { outer_scope_->inner_scopes_.Add(this); - scope_inside_with_ = outer_scope_->scope_inside_with_ || inside_with; + scope_inside_with_ = outer_scope_->scope_inside_with_ || is_with_scope(); } else { - scope_inside_with_ = inside_with; + scope_inside_with_ = is_with_scope(); } // Declare convenience variables. @@ -300,21 +319,19 @@ void Scope::Initialize(bool inside_with) { // instead load them directly from the stack. Currently, the only // such parameter is 'this' which is passed on the stack when // invoking scripts - if (is_catch_scope() || is_block_scope()) { - ASSERT(outer_scope() != NULL); - receiver_ = outer_scope()->receiver(); - } else { - ASSERT(is_function_scope() || - is_global_scope() || - is_eval_scope()); + if (is_declaration_scope()) { Variable* var = variables_.Declare(this, isolate_->factory()->this_symbol(), - Variable::VAR, + VAR, false, - Variable::THIS); + Variable::THIS, + kCreatedInitialized); var->AllocateTo(Variable::PARAMETER, -1); receiver_ = var; + } else { + ASSERT(outer_scope() != NULL); + receiver_ = outer_scope()->receiver(); } if (is_function_scope()) { @@ -323,9 +340,10 @@ void Scope::Initialize(bool inside_with) { // allocated during variable allocation. variables_.Declare(this, isolate_->factory()->arguments_symbol(), - Variable::VAR, + VAR, true, - Variable::ARGUMENTS); + Variable::ARGUMENTS, + kCreatedInitialized); } } @@ -365,34 +383,51 @@ Variable* Scope::LocalLookup(Handle<String> name) { return result; } // If we have a serialized scope info, we might find the variable there. - // - // We should never lookup 'arguments' in this scope as it is implicitly - // present in every scope. - ASSERT(*name != *isolate_->factory()->arguments_symbol()); // There should be no local slot with the given name. ASSERT(scope_info_->StackSlotIndex(*name) < 0); // Check context slot lookup. - Variable::Mode mode; - int index = scope_info_->ContextSlotIndex(*name, &mode); + VariableMode mode; + InitializationFlag init_flag; + int index = scope_info_->ContextSlotIndex(*name, &mode, &init_flag); if (index < 0) { // Check parameters. - mode = Variable::VAR; + mode = VAR; + init_flag = kCreatedInitialized; index = scope_info_->ParameterIndex(*name); - if (index < 0) { - // Check the function name. - index = scope_info_->FunctionContextSlotIndex(*name); - if (index < 0) return NULL; - } + if (index < 0) return NULL; } Variable* var = - variables_.Declare(this, name, mode, true, Variable::NORMAL); + variables_.Declare(this, + name, + mode, + true, + Variable::NORMAL, + init_flag); var->AllocateTo(Variable::CONTEXT, index); return var; } +Variable* Scope::LookupFunctionVar(Handle<String> name, + AstNodeFactory<AstNullVisitor>* factory) { + if (function_ != NULL && function_->name().is_identical_to(name)) { + return function_->var(); + } else if (!scope_info_.is_null()) { + // If we are backed by a scope info, try to lookup the variable there. + VariableMode mode; + int index = scope_info_->FunctionContextSlotIndex(*name, &mode); + if (index < 0) return NULL; + Variable* var = DeclareFunctionVar(name, mode, factory); + var->AllocateTo(Variable::CONTEXT, index); + return var; + } else { + return NULL; + } +} + + Variable* Scope::Lookup(Handle<String> name) { for (Scope* scope = this; scope != NULL; @@ -404,56 +439,41 @@ Variable* Scope::Lookup(Handle<String> name) { } -Variable* Scope::DeclareFunctionVar(Handle<String> name) { - ASSERT(is_function_scope() && function_ == NULL); - Variable* function_var = - new Variable(this, name, Variable::CONST, true, Variable::NORMAL); - function_ = new(isolate_->zone()) VariableProxy(isolate_, function_var); - return function_var; -} - - -void Scope::DeclareParameter(Handle<String> name, Variable::Mode mode) { +void Scope::DeclareParameter(Handle<String> name, VariableMode mode) { ASSERT(!already_resolved()); ASSERT(is_function_scope()); - Variable* var = - variables_.Declare(this, name, mode, true, Variable::NORMAL); + Variable* var = variables_.Declare( + this, name, mode, true, Variable::NORMAL, kCreatedInitialized); params_.Add(var); } -Variable* Scope::DeclareLocal(Handle<String> name, Variable::Mode mode) { +Variable* Scope::DeclareLocal(Handle<String> name, + VariableMode mode, + InitializationFlag init_flag, + Interface* interface) { ASSERT(!already_resolved()); // This function handles VAR and CONST modes. DYNAMIC variables are // introduces during variable allocation, INTERNAL variables are allocated // explicitly, and TEMPORARY variables are allocated via NewTemporary(). - ASSERT(mode == Variable::VAR || - mode == Variable::CONST || - mode == Variable::LET); + ASSERT(mode == VAR || + mode == CONST || + mode == CONST_HARMONY || + mode == LET); ++num_var_or_const_; - return variables_.Declare(this, name, mode, true, Variable::NORMAL); + return variables_.Declare( + this, name, mode, true, Variable::NORMAL, init_flag, interface); } Variable* Scope::DeclareGlobal(Handle<String> name) { ASSERT(is_global_scope()); - return variables_.Declare(this, name, Variable::DYNAMIC_GLOBAL, + return variables_.Declare(this, + name, + DYNAMIC_GLOBAL, true, - Variable::NORMAL); -} - - -VariableProxy* Scope::NewUnresolved(Handle<String> name, - bool inside_with, - int position) { - // Note that we must not share the unresolved variables with - // the same name because they may be removed selectively via - // RemoveUnresolved(). - ASSERT(!already_resolved()); - VariableProxy* proxy = new(isolate_->zone()) VariableProxy( - isolate_, name, false, inside_with, position); - unresolved_.Add(proxy); - return proxy; + Variable::NORMAL, + kCreatedInitialized); } @@ -473,9 +493,10 @@ Variable* Scope::NewTemporary(Handle<String> name) { ASSERT(!already_resolved()); Variable* var = new Variable(this, name, - Variable::TEMPORARY, + TEMPORARY, true, - Variable::NORMAL); + Variable::NORMAL, + kCreatedInitialized); temps_.Add(var); return var; } @@ -505,84 +526,97 @@ Declaration* Scope::CheckConflictingVarDeclarations() { int length = decls_.length(); for (int i = 0; i < length; i++) { Declaration* decl = decls_[i]; - if (decl->mode() != Variable::VAR) continue; + if (decl->mode() != VAR) continue; Handle<String> name = decl->proxy()->name(); - bool cond = true; - for (Scope* scope = decl->scope(); cond ; scope = scope->outer_scope_) { + + // Iterate through all scopes until and including the declaration scope. + Scope* previous = NULL; + Scope* current = decl->scope(); + do { // There is a conflict if there exists a non-VAR binding. - Variable* other_var = scope->variables_.Lookup(name); - if (other_var != NULL && other_var->mode() != Variable::VAR) { + Variable* other_var = current->variables_.Lookup(name); + if (other_var != NULL && other_var->mode() != VAR) { return decl; } + previous = current; + current = current->outer_scope_; + } while (!previous->is_declaration_scope()); + } + return NULL; +} + - // Include declaration scope in the iteration but stop after. - if (!scope->is_block_scope() && !scope->is_catch_scope()) cond = false; +VariableProxy* Scope::CheckAssignmentToConst() { + // Check this scope. + if (is_extended_mode()) { + for (int i = 0; i < unresolved_.length(); i++) { + ASSERT(unresolved_[i]->var() != NULL); + if (unresolved_[i]->var()->is_const_mode() && + unresolved_[i]->IsLValue()) { + return unresolved_[i]; + } } } + + // Check inner scopes. + for (int i = 0; i < inner_scopes_.length(); i++) { + VariableProxy* proxy = inner_scopes_[i]->CheckAssignmentToConst(); + if (proxy != NULL) return proxy; + } + + // No assignments to const found. return NULL; } -template<class Allocator> -void Scope::CollectUsedVariables(List<Variable*, Allocator>* locals) { - // Collect variables in this scope. - // Note that the function_ variable - if present - is not - // collected here but handled separately in ScopeInfo - // which is the current user of this function). +void Scope::CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals, + ZoneList<Variable*>* context_locals) { + ASSERT(stack_locals != NULL); + ASSERT(context_locals != NULL); + + // Collect temporaries which are always allocated on the stack. for (int i = 0; i < temps_.length(); i++) { Variable* var = temps_[i]; if (var->is_used()) { - locals->Add(var); + ASSERT(var->IsStackLocal()); + stack_locals->Add(var); } } + + // Collect declared local variables. for (VariableMap::Entry* p = variables_.Start(); p != NULL; p = variables_.Next(p)) { Variable* var = reinterpret_cast<Variable*>(p->value); if (var->is_used()) { - locals->Add(var); + if (var->IsStackLocal()) { + stack_locals->Add(var); + } else if (var->IsContextSlot()) { + context_locals->Add(var); + } } } } -// Make sure the method gets instantiated by the template system. -template void Scope::CollectUsedVariables( - List<Variable*, FreeStoreAllocationPolicy>* locals); -template void Scope::CollectUsedVariables( - List<Variable*, PreallocatedStorage>* locals); -template void Scope::CollectUsedVariables( - List<Variable*, ZoneListAllocationPolicy>* locals); - - -void Scope::AllocateVariables(Handle<Context> context) { - ASSERT(outer_scope_ == NULL); // eval or global scopes only - +bool Scope::AllocateVariables(CompilationInfo* info, + AstNodeFactory<AstNullVisitor>* factory) { // 1) Propagate scope information. - // If we are in an eval scope, we may have other outer scopes about - // which we don't know anything at this point. Thus we must be conservative - // and assume they may invoke eval themselves. Eventually we could capture - // this information in the ScopeInfo and then use it here (by traversing - // the call chain stack, at compile time). - - bool eval_scope = is_eval_scope(); - bool outer_scope_calls_eval = false; bool outer_scope_calls_non_strict_eval = false; - if (!is_global_scope()) { - context->ComputeEvalScopeInfo(&outer_scope_calls_eval, - &outer_scope_calls_non_strict_eval); + if (outer_scope_ != NULL) { + outer_scope_calls_non_strict_eval = + outer_scope_->outer_scope_calls_non_strict_eval() | + outer_scope_->calls_non_strict_eval(); } - PropagateScopeInfo(outer_scope_calls_eval, - outer_scope_calls_non_strict_eval, - eval_scope); + PropagateScopeInfo(outer_scope_calls_non_strict_eval); // 2) Resolve variables. - Scope* global_scope = NULL; - if (is_global_scope()) global_scope = this; - ResolveVariablesRecursively(global_scope, context); + if (!ResolveVariablesRecursively(info, factory)) return false; // 3) Allocate variables. AllocateVariablesRecursively(); + + return true; } @@ -627,30 +661,49 @@ int Scope::ContextChainLength(Scope* scope) { Scope* Scope::DeclarationScope() { Scope* scope = this; - while (scope->is_catch_scope() || - scope->is_block_scope()) { + while (!scope->is_declaration_scope()) { scope = scope->outer_scope(); } return scope; } -Handle<SerializedScopeInfo> Scope::GetSerializedScopeInfo() { +Handle<ScopeInfo> Scope::GetScopeInfo() { if (scope_info_.is_null()) { - scope_info_ = SerializedScopeInfo::Create(this); + scope_info_ = ScopeInfo::Create(this); } return scope_info_; } +void Scope::GetNestedScopeChain( + List<Handle<ScopeInfo> >* chain, + int position) { + if (!is_eval_scope()) chain->Add(Handle<ScopeInfo>(GetScopeInfo())); + + for (int i = 0; i < inner_scopes_.length(); i++) { + Scope* scope = inner_scopes_[i]; + int beg_pos = scope->start_position(); + int end_pos = scope->end_position(); + ASSERT(beg_pos >= 0 && end_pos >= 0); + if (beg_pos <= position && position < end_pos) { + scope->GetNestedScopeChain(chain, position); + return; + } + } +} + + #ifdef DEBUG -static const char* Header(Scope::Type type) { +static const char* Header(ScopeType type) { switch (type) { - case Scope::EVAL_SCOPE: return "eval"; - case Scope::FUNCTION_SCOPE: return "function"; - case Scope::GLOBAL_SCOPE: return "global"; - case Scope::CATCH_SCOPE: return "catch"; - case Scope::BLOCK_SCOPE: return "block"; + case EVAL_SCOPE: return "eval"; + case FUNCTION_SCOPE: return "function"; + case MODULE_SCOPE: return "module"; + case GLOBAL_SCOPE: return "global"; + case CATCH_SCOPE: return "catch"; + case BLOCK_SCOPE: return "block"; + case WITH_SCOPE: return "with"; } UNREACHABLE(); return NULL; @@ -695,9 +748,9 @@ static void PrintVar(int indent, Variable* var) { PrintName(var->name()); PrintF("; // "); PrintLocation(var); - if (var->is_accessed_from_inner_scope()) { + if (var->has_forced_context_allocation()) { if (!var->IsUnallocated()) PrintF(", "); - PrintF("inner scope access"); + PrintF("forced context allocation"); } PrintF("\n"); } @@ -733,7 +786,7 @@ void Scope::Print(int n) { PrintF(")"); } - PrintF(" {\n"); + PrintF(" { // (%d, %d)\n", start_position(), end_position()); // Function name, if any (named function literals, only). if (function_ != NULL) { @@ -746,18 +799,23 @@ void Scope::Print(int n) { if (HasTrivialOuterContext()) { Indent(n1, "// scope has trivial outer context\n"); } - if (is_strict_mode()) Indent(n1, "// strict mode scope\n"); + switch (language_mode()) { + case CLASSIC_MODE: + break; + case STRICT_MODE: + Indent(n1, "// strict mode scope\n"); + break; + case EXTENDED_MODE: + Indent(n1, "// extended mode scope\n"); + break; + } if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n"); if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n"); if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n"); - if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n"); if (outer_scope_calls_non_strict_eval_) { Indent(n1, "// outer scope calls 'eval' in non-strict context\n"); } if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n"); - if (outer_scope_is_eval_scope_) { - Indent(n1, "// outer scope is 'eval' scope\n"); - } if (num_stack_slots_ > 0) { Indent(n1, "// "); PrintF("%d stack slots\n", num_stack_slots_); } if (num_heap_slots_ > 0) { Indent(n1, "// "); @@ -779,9 +837,9 @@ void Scope::Print(int n) { Indent(n1, "// dynamic vars\n"); if (dynamics_ != NULL) { - PrintMap(n1, dynamics_->GetMap(Variable::DYNAMIC)); - PrintMap(n1, dynamics_->GetMap(Variable::DYNAMIC_LOCAL)); - PrintMap(n1, dynamics_->GetMap(Variable::DYNAMIC_GLOBAL)); + PrintMap(n1, dynamics_->GetMap(DYNAMIC)); + PrintMap(n1, dynamics_->GetMap(DYNAMIC_LOCAL)); + PrintMap(n1, dynamics_->GetMap(DYNAMIC_GLOBAL)); } // Print inner scopes (disable by providing negative n). @@ -797,13 +855,20 @@ void Scope::Print(int n) { #endif // DEBUG -Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) { +Variable* Scope::NonLocal(Handle<String> name, VariableMode mode) { if (dynamics_ == NULL) dynamics_ = new DynamicScopePart(); VariableMap* map = dynamics_->GetMap(mode); Variable* var = map->Lookup(name); if (var == NULL) { // Declare a new non-local. - var = map->Declare(NULL, name, mode, true, Variable::NORMAL); + InitializationFlag init_flag = (mode == VAR) + ? kCreatedInitialized : kNeedsInitialization; + var = map->Declare(NULL, + name, + mode, + true, + Variable::NORMAL, + init_flag); // Allocate it by giving it a dynamic lookup. var->AllocateTo(Variable::LOOKUP, -1); } @@ -811,198 +876,178 @@ Variable* Scope::NonLocal(Handle<String> name, Variable::Mode mode) { } -// Lookup a variable starting with this scope. The result is either -// the statically resolved variable belonging to an outer scope, or -// NULL. It may be NULL because a) we couldn't find a variable, or b) -// because the variable is just a guess (and may be shadowed by -// another variable that is introduced dynamically via an 'eval' call -// or a 'with' statement). Variable* Scope::LookupRecursive(Handle<String> name, - bool from_inner_scope, - Variable** invalidated_local) { - // If we find a variable, but the current scope calls 'eval', the found - // variable may not be the correct one (the 'eval' may introduce a - // property with the same name). In that case, remember that the variable - // found is just a guess. - bool guess = scope_calls_eval_; - + BindingKind* binding_kind, + AstNodeFactory<AstNullVisitor>* factory) { + ASSERT(binding_kind != NULL); // Try to find the variable in this scope. Variable* var = LocalLookup(name); + // We found a variable and we are done. (Even if there is an 'eval' in + // this scope which introduces the same variable again, the resulting + // variable remains the same.) if (var != NULL) { - // We found a variable. If this is not an inner lookup, we are done. - // (Even if there is an 'eval' in this scope which introduces the - // same variable again, the resulting variable remains the same. - // Note that enclosing 'with' statements are handled at the call site.) - if (!from_inner_scope) - return var; - - } else { - // We did not find a variable locally. Check against the function variable, - // if any. We can do this for all scopes, since the function variable is - // only present - if at all - for function scopes. - // - // This lookup corresponds to a lookup in the "intermediate" scope sitting - // between this scope and the outer scope. (ECMA-262, 3rd., requires that - // the name of named function literal is kept in an intermediate scope - // in between this scope and the next outer scope.) - if (function_ != NULL && function_->name().is_identical_to(name)) { - var = function_->var(); - - } else if (outer_scope_ != NULL) { - var = outer_scope_->LookupRecursive(name, true, invalidated_local); - // We may have found a variable in an outer scope. However, if - // the current scope is inside a 'with', the actual variable may - // be a property introduced via the 'with' statement. Then, the - // variable we may have found is just a guess. - if (scope_inside_with_) - guess = true; - } - - // If we did not find a variable, we are done. - if (var == NULL) - return NULL; + *binding_kind = BOUND; + return var; } - ASSERT(var != NULL); - - // If this is a lookup from an inner scope, mark the variable. - if (from_inner_scope) { - var->MarkAsAccessedFromInnerScope(); + // We did not find a variable locally. Check against the function variable, + // if any. We can do this for all scopes, since the function variable is + // only present - if at all - for function scopes. + *binding_kind = UNBOUND; + var = LookupFunctionVar(name, factory); + if (var != NULL) { + *binding_kind = BOUND; + } else if (outer_scope_ != NULL) { + var = outer_scope_->LookupRecursive(name, binding_kind, factory); + if (*binding_kind == BOUND && (is_function_scope() || is_with_scope())) { + var->ForceContextAllocation(); + } + } else { + ASSERT(is_global_scope()); } - // If the variable we have found is just a guess, invalidate the - // result. If the found variable is local, record that fact so we - // can generate fast code to get it if it is not shadowed by eval. - if (guess) { - if (!var->is_global()) *invalidated_local = var; - var = NULL; + if (is_with_scope()) { + // The current scope is a with scope, so the variable binding can not be + // statically resolved. However, note that it was necessary to do a lookup + // in the outer scope anyway, because if a binding exists in an outer scope, + // the associated variable has to be marked as potentially being accessed + // from inside of an inner with scope (the property may not be in the 'with' + // object). + *binding_kind = DYNAMIC_LOOKUP; + return NULL; + } else if (calls_non_strict_eval()) { + // A variable binding may have been found in an outer scope, but the current + // scope makes a non-strict 'eval' call, so the found variable may not be + // the correct one (the 'eval' may introduce a binding with the same name). + // In that case, change the lookup result to reflect this situation. + if (*binding_kind == BOUND) { + *binding_kind = BOUND_EVAL_SHADOWED; + } else if (*binding_kind == UNBOUND) { + *binding_kind = UNBOUND_EVAL_SHADOWED; + } } - return var; } -void Scope::ResolveVariable(Scope* global_scope, - Handle<Context> context, - VariableProxy* proxy) { - ASSERT(global_scope == NULL || global_scope->is_global_scope()); +bool Scope::ResolveVariable(CompilationInfo* info, + VariableProxy* proxy, + AstNodeFactory<AstNullVisitor>* factory) { + ASSERT(info->global_scope()->is_global_scope()); // If the proxy is already resolved there's nothing to do // (functions and consts may be resolved by the parser). - if (proxy->var() != NULL) return; + if (proxy->var() != NULL) return true; // Otherwise, try to resolve the variable. - Variable* invalidated_local = NULL; - Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local); - - if (proxy->inside_with()) { - // If we are inside a local 'with' statement, all bets are off - // and we cannot resolve the proxy to a local variable even if - // we found an outer matching variable. - // Note that we must do a lookup anyway, because if we find one, - // we must mark that variable as potentially accessed from this - // inner scope (the property may not be in the 'with' object). - var = NonLocal(proxy->name(), Variable::DYNAMIC); - - } else { - // We are not inside a local 'with' statement. - - if (var == NULL) { - // We did not find the variable. We have a global variable - // if we are in the global scope (we know already that we - // are outside a 'with' statement) or if there is no way - // that the variable might be introduced dynamically (through - // a local or outer eval() call, or an outer 'with' statement), - // or we don't know about the outer scope (because we are - // in an eval scope). - if (is_global_scope() || - !(scope_inside_with_ || outer_scope_is_eval_scope_ || - scope_calls_eval_ || outer_scope_calls_eval_)) { - // We must have a global variable. - ASSERT(global_scope != NULL); - var = global_scope->DeclareGlobal(proxy->name()); - - } else if (scope_inside_with_) { - // If we are inside a with statement we give up and look up - // the variable at runtime. - var = NonLocal(proxy->name(), Variable::DYNAMIC); - - } else if (invalidated_local != NULL) { - // No with statements are involved and we found a local - // variable that might be shadowed by eval introduced - // variables. - var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL); - var->set_local_if_not_shadowed(invalidated_local); - - } else if (outer_scope_is_eval_scope_) { - // No with statements and we did not find a local and the code - // is executed with a call to eval. The context contains - // scope information that we can use to determine if the - // variable is global if it is not shadowed by eval-introduced - // variables. - if (context->GlobalIfNotShadowedByEval(proxy->name())) { - var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL); - - } else { - var = NonLocal(proxy->name(), Variable::DYNAMIC); - } + BindingKind binding_kind; + Variable* var = LookupRecursive(proxy->name(), &binding_kind, factory); + switch (binding_kind) { + case BOUND: + // We found a variable binding. + break; + case BOUND_EVAL_SHADOWED: + // We found a variable variable binding that might be shadowed + // by 'eval' introduced variable bindings. + if (var->is_global()) { + var = NonLocal(proxy->name(), DYNAMIC_GLOBAL); } else { - // No with statements and we did not find a local and the code - // is not executed with a call to eval. We know that this - // variable is global unless it is shadowed by eval-introduced - // variables. - var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL); + Variable* invalidated = var; + var = NonLocal(proxy->name(), DYNAMIC_LOCAL); + var->set_local_if_not_shadowed(invalidated); } - } + break; + + case UNBOUND: + // No binding has been found. Declare a variable in global scope. + var = info->global_scope()->DeclareGlobal(proxy->name()); + break; + + case UNBOUND_EVAL_SHADOWED: + // No binding has been found. But some scope makes a + // non-strict 'eval' call. + var = NonLocal(proxy->name(), DYNAMIC_GLOBAL); + break; + + case DYNAMIC_LOOKUP: + // The variable could not be resolved statically. + var = NonLocal(proxy->name(), DYNAMIC); + break; } + ASSERT(var != NULL); proxy->BindTo(var); + + if (FLAG_harmony_modules) { + bool ok; +#ifdef DEBUG + if (FLAG_print_interface_details) + PrintF("# Resolve %s:\n", var->name()->ToAsciiArray()); +#endif + proxy->interface()->Unify(var->interface(), &ok); + if (!ok) { +#ifdef DEBUG + if (FLAG_print_interfaces) { + PrintF("SCOPES TYPE ERROR\n"); + PrintF("proxy: "); + proxy->interface()->Print(); + PrintF("var: "); + var->interface()->Print(); + } +#endif + + // Inconsistent use of module. Throw a syntax error. + // TODO(rossberg): generate more helpful error message. + MessageLocation location(info->script(), + proxy->position(), + proxy->position()); + Isolate* isolate = Isolate::Current(); + Factory* factory = isolate->factory(); + Handle<JSArray> array = factory->NewJSArray(1); + USE(JSObject::SetElement(array, 0, var->name(), NONE, kStrictMode)); + Handle<Object> result = + factory->NewSyntaxError("module_type_error", array); + isolate->Throw(*result, &location); + return false; + } + } + + return true; } -void Scope::ResolveVariablesRecursively(Scope* global_scope, - Handle<Context> context) { - ASSERT(global_scope == NULL || global_scope->is_global_scope()); +bool Scope::ResolveVariablesRecursively( + CompilationInfo* info, + AstNodeFactory<AstNullVisitor>* factory) { + ASSERT(info->global_scope()->is_global_scope()); // Resolve unresolved variables for this scope. for (int i = 0; i < unresolved_.length(); i++) { - ResolveVariable(global_scope, context, unresolved_[i]); + if (!ResolveVariable(info, unresolved_[i], factory)) return false; } // Resolve unresolved variables for inner scopes. for (int i = 0; i < inner_scopes_.length(); i++) { - inner_scopes_[i]->ResolveVariablesRecursively(global_scope, context); + if (!inner_scopes_[i]->ResolveVariablesRecursively(info, factory)) + return false; } -} + return true; +} -bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval, - bool outer_scope_calls_non_strict_eval, - bool outer_scope_is_eval_scope) { - if (outer_scope_calls_eval) { - outer_scope_calls_eval_ = true; - } +bool Scope::PropagateScopeInfo(bool outer_scope_calls_non_strict_eval ) { if (outer_scope_calls_non_strict_eval) { outer_scope_calls_non_strict_eval_ = true; } - if (outer_scope_is_eval_scope) { - outer_scope_is_eval_scope_ = true; - } - - bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_; - bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_; bool calls_non_strict_eval = - (scope_calls_eval_ && !is_strict_mode()) || - outer_scope_calls_non_strict_eval_; + this->calls_non_strict_eval() || outer_scope_calls_non_strict_eval_; for (int i = 0; i < inner_scopes_.length(); i++) { Scope* inner_scope = inner_scopes_[i]; - if (inner_scope->PropagateScopeInfo(calls_eval, - calls_non_strict_eval, - is_eval)) { + if (inner_scope->PropagateScopeInfo(calls_non_strict_eval)) { inner_scope_calls_eval_ = true; } if (inner_scope->force_eager_compilation_) { @@ -1019,7 +1064,7 @@ bool Scope::MustAllocate(Variable* var) { // via an eval() call. This is only possible if the variable has a // visible name. if ((var->is_this() || var->name()->length() > 0) && - (var->is_accessed_from_inner_scope() || + (var->has_forced_context_allocation() || scope_calls_eval_ || inner_scope_calls_eval_ || scope_contains_with_ || @@ -1040,9 +1085,9 @@ bool Scope::MustAllocateInContext(Variable* var) { // // Exceptions: temporary variables are never allocated in a context; // catch-bound variables are always allocated in a context. - if (var->mode() == Variable::TEMPORARY) return false; + if (var->mode() == TEMPORARY) return false; if (is_catch_scope() || is_block_scope()) return true; - return var->is_accessed_from_inner_scope() || + return var->has_forced_context_allocation() || scope_calls_eval_ || inner_scope_calls_eval_ || scope_contains_with_ || @@ -1095,7 +1140,7 @@ void Scope::AllocateParameterLocals() { // In strict mode 'arguments' does not alias formal parameters. // Therefore in strict mode we allocate parameters as if 'arguments' // were not used. - uses_nonstrict_arguments = !is_strict_mode(); + uses_nonstrict_arguments = is_classic_mode(); } // The same parameter may occur multiple times in the parameters_ list. @@ -1106,9 +1151,8 @@ void Scope::AllocateParameterLocals() { Variable* var = params_[i]; ASSERT(var->scope() == this); if (uses_nonstrict_arguments) { - // Give the parameter a use from an inner scope, to force allocation - // to the context. - var->MarkAsAccessedFromInnerScope(); + // Force context allocation of the parameter. + var->ForceContextAllocation(); } if (MustAllocate(var)) { @@ -1183,21 +1227,15 @@ void Scope::AllocateVariablesRecursively() { if (is_function_scope()) AllocateParameterLocals(); AllocateNonParameterLocals(); - // Allocate context if necessary. - bool must_have_local_context = false; - if (scope_calls_eval_ || scope_contains_with_) { - // The context for the eval() call or 'with' statement in this scope. - // Unless we are in the global or an eval scope, we need a local - // context even if we didn't statically allocate any locals in it, - // and the compiler will access the context variable. If we are - // not in an inner scope, the scope is provided from the outside. - must_have_local_context = is_function_scope(); - } + // Force allocation of a context for this scope if necessary. For a 'with' + // scope and for a function scope that makes an 'eval' call we need a context, + // even if no local variables were statically allocated in the scope. + bool must_have_context = is_with_scope() || + (is_function_scope() && calls_eval()); // If we didn't allocate any locals in the local context, then we only - // need the minimal number of slots if we must have a local context. - if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && - !must_have_local_context) { + // need the minimal number of slots if we must have a context. + if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && !must_have_context) { num_heap_slots_ = 0; } @@ -1205,4 +1243,17 @@ void Scope::AllocateVariablesRecursively() { ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS); } + +int Scope::StackLocalCount() const { + return num_stack_slots() - + (function_ != NULL && function_->var()->IsStackLocal() ? 1 : 0); +} + + +int Scope::ContextLocalCount() const { + if (num_heap_slots() == 0) return 0; + return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - + (function_ != NULL && function_->var()->IsContextSlot() ? 1 : 0); +} + } } // namespace v8::internal |
