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, ...@@ -1576,10 +1576,10 @@ VariableProxy* AstBuildingParser::Declare(Handle<String> name,
// to the calling function context. // to the calling function context.
if (top_scope_->is_function_scope()) { if (top_scope_->is_function_scope()) {
// Declare the variable in the function scope. // Declare the variable in the function scope.
var = top_scope_->LookupLocal(name); var = top_scope_->LocalLookup(name);
if (var == NULL) { if (var == NULL) {
// Declare the name. // Declare the name.
var = top_scope_->Declare(name, mode); 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
...@@ -3466,7 +3466,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> var_name, ...@@ -3466,7 +3466,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> var_name,
while (!done) { while (!done) {
Handle<String> param_name = ParseIdentifier(CHECK_OK); Handle<String> param_name = ParseIdentifier(CHECK_OK);
if (!is_pre_parsing_) { if (!is_pre_parsing_) {
top_scope_->AddParameter(top_scope_->Declare(param_name, top_scope_->AddParameter(top_scope_->DeclareLocal(param_name,
Variable::VAR)); Variable::VAR));
num_parameters++; num_parameters++;
} }
......
This diff is collapsed.
...@@ -35,19 +35,22 @@ namespace v8 { ...@@ -35,19 +35,22 @@ namespace v8 {
namespace internal { namespace internal {
// A hash map to support fast local variable declaration and lookup. // A hash map to support fast variable declaration and lookup.
class LocalsMap: public HashMap { class VariableMap: public HashMap {
public: public:
LocalsMap(); VariableMap();
// Dummy constructor. This constructor doesn't set up the map // Dummy constructor. This constructor doesn't set up the map
// properly so don't use it unless you have a good reason. // 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, Variable* Declare(Scope* scope,
bool is_valid_LHS, Variable::Kind kind); Handle<String> name,
Variable::Mode mode,
bool is_valid_lhs,
Variable::Kind kind);
Variable* Lookup(Handle<String> name); Variable* Lookup(Handle<String> name);
}; };
...@@ -59,14 +62,14 @@ class LocalsMap: public HashMap { ...@@ -59,14 +62,14 @@ class LocalsMap: public HashMap {
// and setup time for scopes that don't need them. // and setup time for scopes that don't need them.
class DynamicScopePart : public ZoneObject { class DynamicScopePart : public ZoneObject {
public: public:
LocalsMap* GetMap(Variable::Mode mode) { VariableMap* GetMap(Variable::Mode mode) {
int index = mode - Variable::DYNAMIC; int index = mode - Variable::DYNAMIC;
ASSERT(index >= 0 && index < 3); ASSERT(index >= 0 && index < 3);
return &maps_[index]; return &maps_[index];
} }
private: private:
LocalsMap maps_[3]; VariableMap maps_[3];
}; };
...@@ -105,7 +108,7 @@ class Scope: public ZoneObject { ...@@ -105,7 +108,7 @@ class Scope: public ZoneObject {
// Declarations // Declarations
// Lookup a variable in this scope. Returns the variable or NULL if not found. // 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. // Lookup a variable in this scope or outer scopes.
// Returns the variable or NULL if not found. // Returns the variable or NULL if not found.
...@@ -116,9 +119,15 @@ class Scope: public ZoneObject { ...@@ -116,9 +119,15 @@ 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 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* 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 // Add a parameter to the parameter list. The parameter must have been
// declared via Declare. The same parameter may occur more then once in // declared via Declare. The same parameter may occur more then once in
...@@ -288,25 +297,28 @@ class Scope: public ZoneObject { ...@@ -288,25 +297,28 @@ class Scope: public ZoneObject {
Handle<String> scope_name_; Handle<String> scope_name_;
// The variables declared in this scope: // The variables declared in this scope:
// all user-declared variables (incl. parameters) //
LocalsMap locals_; // All user-declared variables (incl. parameters). For global scopes
// compiler-allocated (user-invisible) temporaries // 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_; ZoneList<Variable*> temps_;
// parameter list in source order // Parameter list in source order.
ZoneList<Variable*> params_; ZoneList<Variable*> params_;
// variables that must be looked up dynamically // Variables that must be looked up dynamically.
DynamicScopePart* dynamics_; DynamicScopePart* dynamics_;
// unresolved variables referred to from this scope // Unresolved variables referred to from this scope.
ZoneList<VariableProxy*> unresolved_; ZoneList<VariableProxy*> unresolved_;
// declarations // Declarations.
ZoneList<Declaration*> decls_; ZoneList<Declaration*> decls_;
// convenience variable // Convenience variable.
VariableProxy* receiver_; VariableProxy* receiver_;
// function variable, if any; function scopes only // Function variable, if any; function scopes only.
Variable* function_; Variable* function_;
// convenience variable; function scopes only // Convenience variable; function scopes only.
VariableProxy* arguments_; VariableProxy* arguments_;
// convenience variable; function scopes only // Convenience variable; function scopes only.
VariableProxy* arguments_shadow_; VariableProxy* arguments_shadow_;
// Illegal redeclaration. // Illegal redeclaration.
......
...@@ -143,6 +143,12 @@ class Variable: public ZoneObject { ...@@ -143,6 +143,12 @@ class Variable: public ZoneObject {
ARGUMENTS ARGUMENTS
}; };
Variable(Scope* scope,
Handle<String> name,
Mode mode,
bool is_valid_lhs,
Kind kind);
// Printing support // Printing support
static const char* Mode2String(Mode mode); static const char* Mode2String(Mode mode);
...@@ -196,9 +202,6 @@ class Variable: public ZoneObject { ...@@ -196,9 +202,6 @@ class Variable: public ZoneObject {
SmiAnalysis* type() { return &type_; } SmiAnalysis* type() { return &type_; }
private: private:
Variable(Scope* scope, Handle<String> name, Mode mode, bool is_valid_LHS,
Kind kind);
Scope* scope_; Scope* scope_;
Handle<String> name_; Handle<String> name_;
Mode mode_; Mode mode_;
...@@ -216,13 +219,10 @@ class Variable: public ZoneObject { ...@@ -216,13 +219,10 @@ class Variable: public ZoneObject {
SmiAnalysis type_; SmiAnalysis type_;
// Code generation. // 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_; Expression* rewrite_;
friend class VariableProxy; friend class Scope; // Has explicit access to rewrite_.
friend class Scope;
friend class LocalsMap;
friend class AstBuildingParser;
}; };
......
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