Commit cad32c19 authored by marja@chromium.org's avatar marja@chromium.org

(Pre)Parser: Move FunctionState, BlockState and Scope handling to ParserBase.

Notes:
- This removes Parser::FunctionState and PreParser::FunctionState and adds
ParserBase::FunctionState etc.
- Also the scope stacks and function state stacks are moved to ParserBase.
- PreParser::FunctionState didn't add and subtract
JSFunction::kLiteralsPrefixSize (unlike Parser::FunctionState). Since the
actual value of NextMaterializedLiteralIndex is not used in the Preparser,
this change is valid.
- Traits no longer need functions like is_classic_mode(), since now there is a
 unified way of getting the information from the FunctionState / Scope.

R=ulan@chromium.org
BUG=v8:3126
LOG=N

Review URL: https://codereview.chromium.org/135213007

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@19361 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 8f170a66
......@@ -462,58 +462,6 @@ class TargetScope BASE_EMBEDDED {
};
// ----------------------------------------------------------------------------
// FunctionState and BlockState together implement the parser's scope stack.
// The parser's current scope is in scope_. The BlockState and
// FunctionState constructors push on the scope stack and the destructors
// pop. They are also used to hold the parser's per-function and per-block
// state.
class Parser::BlockState BASE_EMBEDDED {
public:
BlockState(Scope** scope_stack, Scope* scope)
: scope_stack_(scope_stack),
outer_scope_(*scope_stack) {
*scope_stack = scope;
}
~BlockState() { *scope_stack_ = outer_scope_; }
private:
Scope** scope_stack_;
Scope* outer_scope_;
};
Parser::FunctionState::FunctionState(FunctionState** function_state_stack,
Scope** scope_stack, Scope* scope,
Zone* zone)
: next_materialized_literal_index_(JSFunction::kLiteralsPrefixSize),
next_handler_index_(0),
expected_property_count_(0),
generator_object_variable_(NULL),
function_state_stack_(function_state_stack),
outer_function_state_(*function_state_stack),
scope_stack_(scope_stack),
outer_scope_(*scope_stack),
isolate_(zone->isolate()),
saved_ast_node_id_(isolate_->ast_node_id()),
factory_(zone) {
*scope_stack_ = scope;
*function_state_stack = this;
isolate_->set_ast_node_id(BailoutId::FirstUsable().ToInt());
}
Parser::FunctionState::~FunctionState() {
*scope_stack_ = outer_scope_;
*function_state_stack_ = outer_function_state_;
if (outer_function_state_ != NULL) {
isolate_->set_ast_node_id(saved_ast_node_id_);
}
}
// ----------------------------------------------------------------------------
// The CHECK_OK macro is a convenient macro to enforce error
// handling for functions that may fail (by returning !*ok).
......@@ -537,16 +485,6 @@ Parser::FunctionState::~FunctionState() {
// ----------------------------------------------------------------------------
// Implementation of Parser
bool ParserTraits::is_classic_mode() const {
return parser_->scope_->is_classic_mode();
}
bool ParserTraits::is_generator() const {
return parser_->function_state_->is_generator();
}
bool ParserTraits::IsEvalOrArguments(Handle<String> identifier) const {
return identifier.is_identical_to(
parser_->isolate()->factory()->eval_string()) ||
......@@ -555,11 +493,6 @@ bool ParserTraits::IsEvalOrArguments(Handle<String> identifier) const {
}
int ParserTraits::NextMaterializedLiteralIndex() {
return parser_->function_state_->NextMaterializedLiteralIndex();
}
void ParserTraits::ReportMessageAt(Scanner::Location source_location,
const char* message,
Vector<const char*> args) {
......@@ -623,14 +556,6 @@ Handle<String> ParserTraits::NextLiteralString(PretenureFlag tenured) {
}
Expression* ParserTraits::NewRegExpLiteral(Handle<String> js_pattern,
Handle<String> js_flags,
int literal_index,
int pos) {
return parser_->factory()->NewRegExpLiteral(
js_pattern, js_flags, literal_index, pos);
}
Parser::Parser(CompilationInfo* info)
: ParserBase<ParserTraits>(&scanner_,
info->isolate()->stack_guard()->real_climit(),
......@@ -640,9 +565,7 @@ Parser::Parser(CompilationInfo* info)
script_(info->script()),
scanner_(isolate_->unicode_cache()),
reusable_preparser_(NULL),
scope_(NULL),
original_scope_(NULL),
function_state_(NULL),
target_stack_(NULL),
extension_(info->extension()),
pre_parse_data_(NULL),
......@@ -4066,8 +3989,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
// Calling a generator returns a generator object. That object is stored
// in a temporary variable, a definition that is used by "yield"
// expressions. Presence of a variable for the generator object in the
// FunctionState indicates that this function is a generator.
// expressions. This also marks the FunctionState as a generator.
Variable* temp = scope_->DeclarationScope()->NewTemporary(
isolate()->factory()->dot_generator_object_string());
function_state.set_generator_object_variable(temp);
......
......@@ -409,18 +409,41 @@ class SingletonLogger;
class ParserTraits {
public:
typedef Parser* ParserType;
// Return types for traversing functions.
typedef Handle<String> IdentifierType;
typedef Expression* ExpressionType;
struct Type {
typedef v8::internal::Parser* Parser;
// Types used by FunctionState and BlockState.
typedef v8::internal::Scope Scope;
typedef AstNodeFactory<AstConstructionVisitor> Factory;
typedef Variable GeneratorVariable;
typedef v8::internal::Zone Zone;
// Return types for traversing functions.
typedef Handle<String> Identifier;
typedef v8::internal::Expression* Expression;
};
explicit ParserTraits(Parser* parser) : parser_(parser) {}
// Custom operations executed when FunctionStates are created and destructed.
template<typename FS>
static void SetUpFunctionState(FS* function_state, Zone* zone) {
Isolate* isolate = zone->isolate();
function_state->isolate_ = isolate;
function_state->saved_ast_node_id_ = isolate->ast_node_id();
isolate->set_ast_node_id(BailoutId::FirstUsable().ToInt());
}
template<typename FS>
static void TearDownFunctionState(FS* function_state) {
if (function_state->outer_function_state_ != NULL) {
function_state->isolate_->set_ast_node_id(
function_state->saved_ast_node_id_);
}
}
// Helper functions for recursive descent.
bool is_classic_mode() const;
bool is_generator() const;
bool IsEvalOrArguments(Handle<String> identifier) const;
int NextMaterializedLiteralIndex();
// Reporting errors.
void ReportMessageAt(Scanner::Location source_location,
......@@ -432,20 +455,16 @@ class ParserTraits {
Vector<Handle<String> > args);
// "null" return type creators.
static IdentifierType EmptyIdentifier() {
static Handle<String> EmptyIdentifier() {
return Handle<String>();
}
static ExpressionType EmptyExpression() {
static Expression* EmptyExpression() {
return NULL;
}
// Producing data during the recursive descent.
IdentifierType GetSymbol();
IdentifierType NextLiteralString(PretenureFlag tenured);
ExpressionType NewRegExpLiteral(IdentifierType js_pattern,
IdentifierType js_flags,
int literal_index,
int pos);
Handle<String> GetSymbol();
Handle<String> NextLiteralString(PretenureFlag tenured);
private:
Parser* parser_;
......@@ -494,68 +513,6 @@ class Parser : public ParserBase<ParserTraits> {
kHasNoInitializers
};
class BlockState;
class FunctionState BASE_EMBEDDED {
public:
FunctionState(FunctionState** function_state_stack,
Scope** scope_stack, Scope* scope,
Zone* zone);
~FunctionState();
int NextMaterializedLiteralIndex() {
return next_materialized_literal_index_++;
}
int materialized_literal_count() {
return next_materialized_literal_index_ - JSFunction::kLiteralsPrefixSize;
}
int NextHandlerIndex() { return next_handler_index_++; }
int handler_count() { return next_handler_index_; }
void AddProperty() { expected_property_count_++; }
int expected_property_count() { return expected_property_count_; }
void set_generator_object_variable(Variable *variable) {
ASSERT(variable != NULL);
ASSERT(!is_generator());
generator_object_variable_ = variable;
}
Variable* generator_object_variable() const {
return generator_object_variable_;
}
bool is_generator() const {
return generator_object_variable_ != NULL;
}
AstNodeFactory<AstConstructionVisitor>* factory() { return &factory_; }
private:
// Used to assign an index to each literal that needs materialization in
// the function. Includes regexp literals, and boilerplate for object and
// array literals.
int next_materialized_literal_index_;
// Used to assign a per-function index to try and catch handlers.
int next_handler_index_;
// Properties count estimation.
int expected_property_count_;
// For generators, the variable that holds the generator object. This
// variable is used by yield expressions and return statements. NULL
// indicates that this function is not a generator.
Variable* generator_object_variable_;
FunctionState** function_state_stack_;
FunctionState* outer_function_state_;
Scope** scope_stack_;
Scope* outer_scope_;
Isolate* isolate_;
int saved_ast_node_id_;
AstNodeFactory<AstConstructionVisitor> factory_;
};
class ParsingModeScope BASE_EMBEDDED {
public:
ParsingModeScope(Parser* parser, Mode mode)
......@@ -770,19 +727,13 @@ class Parser : public ParserBase<ParserTraits> {
PreParser::PreParseResult LazyParseFunctionLiteral(
SingletonLogger* logger);
AstNodeFactory<AstConstructionVisitor>* factory() {
return function_state_->factory();
}
Isolate* isolate_;
ZoneList<Handle<String> > symbol_cache_;
Handle<Script> script_;
Scanner scanner_;
PreParser* reusable_preparser_;
Scope* scope_; // Scope stack.
Scope* original_scope_; // for ES5 function declarations in sloppy eval
FunctionState* function_state_; // Function state stack.
Target* target_stack_; // for break, continue statements
v8::Extension* extension_;
ScriptDataImpl* pre_parse_data_;
......
......@@ -55,21 +55,6 @@ int isfinite(double value);
namespace v8 {
namespace internal {
bool PreParserTraits::is_classic_mode() const {
return pre_parser_->scope_->language_mode() == CLASSIC_MODE;
}
bool PreParserTraits::is_generator() const {
return pre_parser_->function_state_->is_generator();
}
int PreParserTraits::NextMaterializedLiteralIndex() {
return pre_parser_->function_state_->NextMaterializedLiteralIndex();
}
void PreParserTraits::ReportMessageAt(Scanner::Location location,
const char* message,
Vector<const char*> args) {
......@@ -126,10 +111,12 @@ PreParser::PreParseResult PreParser::PreParseLazyFunction(
LanguageMode mode, bool is_generator, ParserRecorder* log) {
log_ = log;
// Lazy functions always have trivial outer scopes (no with/catch scopes).
FunctionState top_scope(&function_state_, &scope_, GLOBAL_SCOPE);
PreParserScope top_scope(scope_, GLOBAL_SCOPE);
FunctionState top_state(&function_state_, &scope_, &top_scope);
scope_->SetLanguageMode(mode);
FunctionState function_scope(&function_state_, &scope_, FUNCTION_SCOPE);
function_scope.set_is_generator(is_generator);
PreParserScope function_scope(scope_, FUNCTION_SCOPE);
FunctionState function_state(&function_state_, &scope_, &function_scope);
function_state.set_is_generator(is_generator);
ASSERT_EQ(Token::LBRACE, scanner()->current_token());
bool ok = true;
int start_position = peek_position();
......@@ -602,7 +589,8 @@ PreParser::Statement PreParser::ParseWithStatement(bool* ok) {
ParseExpression(true, CHECK_OK);
Expect(Token::RPAREN, CHECK_OK);
BlockState block_state(&scope_, WITH_SCOPE);
PreParserScope with_scope(scope_, WITH_SCOPE);
BlockState block_state(&scope_, &with_scope);
ParseStatement(CHECK_OK);
return Statement::Default();
}
......@@ -776,7 +764,8 @@ PreParser::Statement PreParser::ParseTryStatement(bool* ok) {
ParseIdentifier(kDontAllowEvalOrArguments, CHECK_OK);
Expect(Token::RPAREN, CHECK_OK);
{
BlockState block_state(&scope_, WITH_SCOPE);
PreParserScope with_scope(scope_, WITH_SCOPE);
BlockState block_state(&scope_, &with_scope);
ParseBlock(CHECK_OK);
}
tok = peek();
......@@ -1341,8 +1330,9 @@ PreParser::Expression PreParser::ParseFunctionLiteral(
// Parse function body.
ScopeType outer_scope_type = scope_->type();
bool inside_with = scope_->inside_with();
FunctionState function_scope(&function_state_, &scope_, FUNCTION_SCOPE);
function_scope.set_is_generator(is_generator);
PreParserScope function_scope(scope_, FUNCTION_SCOPE);
FunctionState function_state(&function_state_, &scope_, &function_scope);
function_state.set_is_generator(is_generator);
// FormalParameterList ::
// '(' (Identifier)*[','] ')'
Expect(Token::LPAREN, CHECK_OK);
......@@ -1452,7 +1442,7 @@ void PreParser::ParseLazyFunctionLiteralBody(bool* ok) {
int body_end = scanner()->peek_location().end_pos;
log_->LogFunction(body_start, body_end,
function_state_->materialized_literal_count(),
function_state_->expected_properties(),
function_state_->expected_property_count(),
scope_->language_mode());
}
......
This diff is collapsed.
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