Reviewers: Søren Gjesse,

Description:
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.

[email protected]
BUG=
TEST=


Please review this at http://codereview.chromium.org/6976025/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M src/parser.cc
  M src/scopes.h
  M src/scopes.cc


Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index e2f8da650de94742acb39f1259f8b03dba3b66d0..fc44741914b642a5988baa81b664d38f84dce572 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -1310,7 +1310,7 @@ VariableProxy* Parser::Declare(Handle<String> 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<String> 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",
Index: src/scopes.cc
diff --git a/src/scopes.cc b/src/scopes.cc
index 61024426e3ca9b76545e25b3b5ef373733f2697a..e159257164086840e690f66f4006a202fb00df54 100644
--- a/src/scopes.cc
+++ b/src/scopes.cc
@@ -366,17 +366,22 @@ Variable* Scope::DeclareFunctionVar(Handle<String> name) {
 }


-Variable* Scope::DeclareLocal(Handle<String> 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<String> 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<String> 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<String> name) {
 }


-void Scope::AddParameter(Variable* var) {
-  ASSERT(is_function_scope());
-  ASSERT(LocalLookup(var->name()) == var);
-  params_.Add(var);
-}
-
-
 VariableProxy* Scope::NewUnresolved(Handle<String> name,
                                     bool inside_with,
                                     int position) {
Index: src/scopes.h
diff --git a/src/scopes.h b/src/scopes.h
index faa6fd97efd95dc5ad859a7363d32c59acd8a180..6f73f50382b57156a38c2409d2489db39223b6c9 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<String> 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<String> 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<String> name,
-                                 Variable::Mode mode,
-                                 LocalType type);
+  Variable* DeclareLocal(Handle<String> 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<String> 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<String> name,
                                        bool inside_with,


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to