Allow variable proxies for the same global variable to share the same

variable object.

Add a map from names to variables for global scopes just like
non-global scopes.  Variables are added to the map by the parser when
it encounters a declaration in a global scope or else at scope
resolution time by a failed variable lookup from the global scope or
an inner one and with no intervening with statements or possible calls
to eval.
Review URL: http://codereview.chromium.org/149245

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@2369 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 72235cf0
......@@ -1576,10 +1576,10 @@ VariableProxy* AstBuildingParser::Declare(Handle<String> name,
// to the calling function context.
if (top_scope_->is_function_scope()) {
// Declare the variable in the function scope.
var = top_scope_->LookupLocal(name);
var = top_scope_->LocalLookup(name);
if (var == NULL) {
// Declare the name.
var = top_scope_->Declare(name, mode);
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
......@@ -3466,8 +3466,8 @@ FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> var_name,
while (!done) {
Handle<String> param_name = ParseIdentifier(CHECK_OK);
if (!is_pre_parsing_) {
top_scope_->AddParameter(top_scope_->Declare(param_name,
Variable::VAR));
top_scope_->AddParameter(top_scope_->DeclareLocal(param_name,
Variable::VAR));
num_parameters++;
}
done = (peek() == Token::RPAREN);
......
This diff is collapsed.
......@@ -35,19 +35,22 @@ namespace v8 {
namespace internal {
// A hash map to support fast local variable declaration and lookup.
class LocalsMap: public HashMap {
// A hash map to support fast variable declaration and lookup.
class VariableMap: public HashMap {
public:
LocalsMap();
VariableMap();
// Dummy constructor. This constructor doesn't set up the map
// properly so don't use it unless you have a good reason.
explicit LocalsMap(bool gotta_love_static_overloading);
explicit VariableMap(bool gotta_love_static_overloading);
virtual ~LocalsMap();
virtual ~VariableMap();
Variable* Declare(Scope* scope, Handle<String> name, Variable::Mode mode,
bool is_valid_LHS, Variable::Kind kind);
Variable* Declare(Scope* scope,
Handle<String> name,
Variable::Mode mode,
bool is_valid_lhs,
Variable::Kind kind);
Variable* Lookup(Handle<String> name);
};
......@@ -59,14 +62,14 @@ class LocalsMap: public HashMap {
// and setup time for scopes that don't need them.
class DynamicScopePart : public ZoneObject {
public:
LocalsMap* GetMap(Variable::Mode mode) {
VariableMap* GetMap(Variable::Mode mode) {
int index = mode - Variable::DYNAMIC;
ASSERT(index >= 0 && index < 3);
return &maps_[index];
}
private:
LocalsMap maps_[3];
VariableMap maps_[3];
};
......@@ -105,7 +108,7 @@ class Scope: public ZoneObject {
// Declarations
// Lookup a variable in this scope. Returns the variable or NULL if not found.
virtual Variable* LookupLocal(Handle<String> name);
virtual Variable* LocalLookup(Handle<String> name);
// Lookup a variable in this scope or outer scopes.
// Returns the variable or NULL if not found.
......@@ -116,9 +119,15 @@ class Scope: public ZoneObject {
// outer scope. Only possible for function scopes; at most one variable.
Variable* DeclareFunctionVar(Handle<String> name);
// Declare a 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.
virtual Variable* Declare(Handle<String> name, Variable::Mode mode);
virtual 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
// scope) by a reference to an unresolved variable with no intervening
// 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 then once in
......@@ -288,25 +297,28 @@ class Scope: public ZoneObject {
Handle<String> scope_name_;
// The variables declared in this scope:
// all user-declared variables (incl. parameters)
LocalsMap locals_;
// compiler-allocated (user-invisible) temporaries
//
// All user-declared variables (incl. parameters). For global scopes
// variables may be implicitly 'declared' by being used (possibly in
// an inner scope) with no intervening with statements or eval calls.
VariableMap variables_;
// Compiler-allocated (user-invisible) temporaries.
ZoneList<Variable*> temps_;
// parameter list in source order
// Parameter list in source order.
ZoneList<Variable*> params_;
// variables that must be looked up dynamically
// Variables that must be looked up dynamically.
DynamicScopePart* dynamics_;
// unresolved variables referred to from this scope
// Unresolved variables referred to from this scope.
ZoneList<VariableProxy*> unresolved_;
// declarations
// Declarations.
ZoneList<Declaration*> decls_;
// convenience variable
// Convenience variable.
VariableProxy* receiver_;
// function variable, if any; function scopes only
// Function variable, if any; function scopes only.
Variable* function_;
// convenience variable; function scopes only
// Convenience variable; function scopes only.
VariableProxy* arguments_;
// convenience variable; function scopes only
// Convenience variable; function scopes only.
VariableProxy* arguments_shadow_;
// Illegal redeclaration.
......
......@@ -143,6 +143,12 @@ class Variable: public ZoneObject {
ARGUMENTS
};
Variable(Scope* scope,
Handle<String> name,
Mode mode,
bool is_valid_lhs,
Kind kind);
// Printing support
static const char* Mode2String(Mode mode);
......@@ -196,9 +202,6 @@ class Variable: public ZoneObject {
SmiAnalysis* type() { return &type_; }
private:
Variable(Scope* scope, Handle<String> name, Mode mode, bool is_valid_LHS,
Kind kind);
Scope* scope_;
Handle<String> name_;
Mode mode_;
......@@ -216,13 +219,10 @@ class Variable: public ZoneObject {
SmiAnalysis type_;
// Code generation.
// rewrite_ is usually a Slot or a Property, but maybe any expression.
// rewrite_ is usually a Slot or a Property, but may be any expression.
Expression* rewrite_;
friend class VariableProxy;
friend class Scope;
friend class LocalsMap;
friend class AstBuildingParser;
friend class Scope; // Has explicit access to rewrite_.
};
......
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