From: kmillikin@chromium.org Date: Mon, 30 May 2011 07:33:12 +0000 (+0000) Subject: Simplify the Scope API. X-Git-Tag: upstream/4.7.83~19289 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=add593da2294a5c90f5746dc378e62c3857cb03a;p=platform%2Fupstream%2Fv8.git Simplify the Scope API. Eliminate the LocalType enum in favor of a pair of functions, one for var and const declarations and one for parameters. Move the responsibility for adding a parameter variable to the Scope's internal data structure into the Scope and out of the parser. git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@8091 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- diff --git a/src/parser.cc b/src/parser.cc index e2f8da6..fc44741 100644 --- a/src/parser.cc +++ b/src/parser.cc @@ -1310,7 +1310,7 @@ VariableProxy* Parser::Declare(Handle name, var = top_scope_->LocalLookup(name); if (var == NULL) { // Declare the name. - var = top_scope_->DeclareLocal(name, mode, Scope::VAR_OR_CONST); + var = top_scope_->DeclareLocal(name, mode); } else { // The name was declared before; check for conflicting // re-declarations. If the previous declaration was a const or the @@ -3573,10 +3573,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(Handle var_name, reserved_loc = scanner().location(); } - Variable* parameter = top_scope_->DeclareLocal(param_name, - Variable::VAR, - Scope::PARAMETER); - top_scope_->AddParameter(parameter); + top_scope_->DeclareParameter(param_name); num_parameters++; if (num_parameters > kMaxNumFunctionParameters) { ReportMessageAt(scanner().location(), "too_many_parameters", diff --git a/src/scopes.cc b/src/scopes.cc index 6102442..e159257 100644 --- a/src/scopes.cc +++ b/src/scopes.cc @@ -366,17 +366,22 @@ Variable* Scope::DeclareFunctionVar(Handle name) { } -Variable* Scope::DeclareLocal(Handle name, - Variable::Mode mode, - LocalType type) { - // DYNAMIC variables are introduces during variable allocation, - // INTERNAL variables are allocated explicitly, and TEMPORARY - // variables are allocated via NewTemporary(). +void Scope::DeclareParameter(Handle name) { ASSERT(!resolved()); + ASSERT(is_function_scope()); + Variable* var = + variables_.Declare(this, name, Variable::VAR, true, Variable::NORMAL); + params_.Add(var); +} + + +Variable* Scope::DeclareLocal(Handle name, Variable::Mode mode) { + ASSERT(!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); - if (type == VAR_OR_CONST) { - num_var_or_const_++; - } + ++num_var_or_const_; return variables_.Declare(this, name, mode, true, Variable::NORMAL); } @@ -388,13 +393,6 @@ Variable* Scope::DeclareGlobal(Handle name) { } -void Scope::AddParameter(Variable* var) { - ASSERT(is_function_scope()); - ASSERT(LocalLookup(var->name()) == var); - params_.Add(var); -} - - VariableProxy* Scope::NewUnresolved(Handle name, bool inside_with, int position) { diff --git a/src/scopes.h b/src/scopes.h index faa6fd9..6f73f50 100644 --- a/src/scopes.h +++ b/src/scopes.h @@ -1,4 +1,4 @@ -// Copyright 2010 the V8 project authors. All rights reserved. +// Copyright 2011 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: @@ -95,11 +95,6 @@ class Scope: public ZoneObject { GLOBAL_SCOPE // the top-level scope for a program or a top-level eval }; - enum LocalType { - PARAMETER, - VAR_OR_CONST - }; - Scope(Scope* outer_scope, Type type); virtual ~Scope() { } @@ -137,11 +132,14 @@ class Scope: public ZoneObject { // outer scope. Only possible for function scopes; at most one variable. Variable* DeclareFunctionVar(Handle name); + // Declare a parameter in this scope. When there are duplicated + // parameters the rightmost one 'wins'. However, the implementation + // expects all parameters to be declared and from left to right. + void DeclareParameter(Handle name); + // Declare a local variable in this scope. If the variable has been // declared before, the previously declared variable is returned. - virtual Variable* DeclareLocal(Handle name, - Variable::Mode mode, - LocalType type); + Variable* DeclareLocal(Handle name, Variable::Mode mode); // Declare an implicit global variable in this scope which must be a // global scope. The variable was introduced (possibly from an inner @@ -149,12 +147,6 @@ class Scope: public ZoneObject { // with statements or eval calls. Variable* DeclareGlobal(Handle name); - // Add a parameter to the parameter list. The parameter must have been - // declared via Declare. The same parameter may occur more than once in - // the parameter list; they must be added in source order, from left to - // right. - void AddParameter(Variable* var); - // Create a new unresolved variable. virtual VariableProxy* NewUnresolved(Handle name, bool inside_with,