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
parent a4cf6da5
...@@ -1310,7 +1310,7 @@ VariableProxy* Parser::Declare(Handle<String> name, ...@@ -1310,7 +1310,7 @@ VariableProxy* Parser::Declare(Handle<String> name,
var = top_scope_->LocalLookup(name); var = top_scope_->LocalLookup(name);
if (var == NULL) { if (var == NULL) {
// Declare the name. // Declare the name.
var = top_scope_->DeclareLocal(name, mode, Scope::VAR_OR_CONST); var = top_scope_->DeclareLocal(name, mode);
} else { } else {
// The name was declared before; check for conflicting // The name was declared before; check for conflicting
// re-declarations. If the previous declaration was a const or the // re-declarations. If the previous declaration was a const or the
...@@ -3573,10 +3573,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> var_name, ...@@ -3573,10 +3573,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> var_name,
reserved_loc = scanner().location(); reserved_loc = scanner().location();
} }
Variable* parameter = top_scope_->DeclareLocal(param_name, top_scope_->DeclareParameter(param_name);
Variable::VAR,
Scope::PARAMETER);
top_scope_->AddParameter(parameter);
num_parameters++; num_parameters++;
if (num_parameters > kMaxNumFunctionParameters) { if (num_parameters > kMaxNumFunctionParameters) {
ReportMessageAt(scanner().location(), "too_many_parameters", ReportMessageAt(scanner().location(), "too_many_parameters",
......
...@@ -366,17 +366,22 @@ Variable* Scope::DeclareFunctionVar(Handle<String> name) { ...@@ -366,17 +366,22 @@ Variable* Scope::DeclareFunctionVar(Handle<String> name) {
} }
Variable* Scope::DeclareLocal(Handle<String> name, void Scope::DeclareParameter(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().
ASSERT(!resolved()); 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); 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); return variables_.Declare(this, name, mode, true, Variable::NORMAL);
} }
...@@ -388,13 +393,6 @@ Variable* Scope::DeclareGlobal(Handle<String> name) { ...@@ -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, VariableProxy* Scope::NewUnresolved(Handle<String> name,
bool inside_with, bool inside_with,
int position) { int position) {
......
// 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 // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are // modification, are permitted provided that the following conditions are
// met: // met:
...@@ -95,11 +95,6 @@ class Scope: public ZoneObject { ...@@ -95,11 +95,6 @@ class Scope: public ZoneObject {
GLOBAL_SCOPE // the top-level scope for a program or a top-level eval 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); Scope(Scope* outer_scope, Type type);
virtual ~Scope() { } virtual ~Scope() { }
...@@ -137,11 +132,14 @@ class Scope: public ZoneObject { ...@@ -137,11 +132,14 @@ class Scope: public ZoneObject {
// outer scope. Only possible for function scopes; at most one variable. // outer scope. Only possible for function scopes; at most one variable.
Variable* DeclareFunctionVar(Handle<String> name); 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 // Declare a local variable in this scope. If the variable has been
// declared before, the previously declared variable is returned. // declared before, the previously declared variable is returned.
virtual Variable* DeclareLocal(Handle<String> name, Variable* DeclareLocal(Handle<String> name, Variable::Mode mode);
Variable::Mode mode,
LocalType type);
// Declare an implicit global variable in this scope which must be a // Declare an implicit global variable in this scope which must be a
// global scope. The variable was introduced (possibly from an inner // global scope. The variable was introduced (possibly from an inner
...@@ -149,12 +147,6 @@ class Scope: public ZoneObject { ...@@ -149,12 +147,6 @@ class Scope: public ZoneObject {
// with statements or eval calls. // with statements or eval calls.
Variable* DeclareGlobal(Handle<String> name); 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. // Create a new unresolved variable.
virtual VariableProxy* NewUnresolved(Handle<String> name, virtual VariableProxy* NewUnresolved(Handle<String> name,
bool inside_with, bool inside_with,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment