Commit 3c1d076a authored by nikolaos's avatar nikolaos Committed by Commit bot

[parser] Clean up (pre)parser traits, part 4

This patch moves the following methods from the traits objects to
the (pre)parser implementation objects:

- ExpressionFromIdentifier
- ExpressionFromLiteral
- ExpressionFromString
- FunctionSentExpression
- GetNextSymbol
- GetNumberAsSymbol
- GetSymbol
- NewExpressionList
- NewPropertyList
- NewStatementList
- NewSuperCallReference
- NewSuperPropertyReference
- NewTargetExpression
- ThisExpression

Also, the method GetIterator is specific only to the parser and is
removed from the preparser's implementation.

R=adamk@chromium.org, marja@chromium.org
BUG=
LOG=N

Review-Url: https://codereview.chromium.org/2274113002
Cr-Commit-Position: refs/heads/master@{#38890}
parent 6e67d042
......@@ -1424,7 +1424,7 @@ ParserBase<Impl>::ParseAndClassifyIdentifier(ExpressionClassifier* classifier,
Token::Value next = Next();
if (next == Token::IDENTIFIER || next == Token::ASYNC ||
(next == Token::AWAIT && !parsing_module_ && !is_async_function())) {
IdentifierT name = this->GetSymbol(scanner());
IdentifierT name = impl()->GetSymbol();
// When this function is used to read a formal parameter, we don't always
// know whether the function is going to be strict or sloppy. Indeed for
// arrow functions we don't always know that the identifier we are reading
......@@ -1467,7 +1467,7 @@ ParserBase<Impl>::ParseAndClassifyIdentifier(ExpressionClassifier* classifier,
classifier->RecordLetPatternError(scanner()->location(),
MessageTemplate::kLetInLexicalBinding);
}
return this->GetSymbol(scanner());
return impl()->GetSymbol();
} else {
this->ReportUnexpectedToken(next);
*ok = false;
......@@ -1494,7 +1494,7 @@ ParserBase<Impl>::ParseIdentifierOrStrictReservedWord(
return impl()->EmptyIdentifier();
}
return this->GetSymbol(scanner());
return impl()->GetSymbol();
}
template <typename Impl>
......@@ -1512,7 +1512,7 @@ typename ParserBase<Impl>::IdentifierT ParserBase<Impl>::ParseIdentifierName(
return impl()->EmptyIdentifier();
}
return this->GetSymbol(scanner());
return impl()->GetSymbol();
}
template <typename Impl>
......@@ -1528,7 +1528,7 @@ typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseRegExpLiteral(
int literal_index = function_state_->NextMaterializedLiteralIndex();
IdentifierT js_pattern = this->GetNextSymbol(scanner());
IdentifierT js_pattern = impl()->GetNextSymbol();
Maybe<RegExp::Flags> flags = scanner()->ScanRegExpFlags();
if (flags.IsNothing()) {
Next();
......@@ -1566,7 +1566,7 @@ typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParsePrimaryExpression(
case Token::THIS: {
BindingPatternUnexpectedToken(classifier);
Consume(Token::THIS);
return this->ThisExpression(beg_pos);
return impl()->ThisExpression(beg_pos);
}
case Token::NULL_LITERAL:
......@@ -1575,7 +1575,7 @@ typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParsePrimaryExpression(
case Token::SMI:
case Token::NUMBER:
BindingPatternUnexpectedToken(classifier);
return this->ExpressionFromLiteral(Next(), beg_pos, scanner(), factory());
return impl()->ExpressionFromLiteral(Next(), beg_pos);
case Token::ASYNC:
if (allow_harmony_async_await() &&
......@@ -1596,14 +1596,14 @@ typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParsePrimaryExpression(
case Token::FUTURE_STRICT_RESERVED_WORD: {
// Using eval or arguments in this context is OK even in strict mode.
IdentifierT name = ParseAndClassifyIdentifier(classifier, CHECK_OK);
return this->ExpressionFromIdentifier(name, beg_pos,
scanner()->location().end_pos);
return impl()->ExpressionFromIdentifier(name, beg_pos,
scanner()->location().end_pos);
}
case Token::STRING: {
BindingPatternUnexpectedToken(classifier);
Consume(Token::STRING);
return this->ExpressionFromString(beg_pos, scanner(), factory());
return impl()->ExpressionFromString(beg_pos);
}
case Token::ASSIGN_DIV:
......@@ -1801,8 +1801,7 @@ typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseArrayLiteral(
// '[' Expression? (',' Expression?)* ']'
int pos = peek_position();
typename Traits::Type::ExpressionList values =
this->NewExpressionList(4, zone_);
typename Traits::Type::ExpressionList values = impl()->NewExpressionList(4);
int first_spread_index = -1;
Expect(Token::LBRACK, CHECK_OK);
while (peek() != Token::RBRACK) {
......@@ -1889,17 +1888,17 @@ typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParsePropertyName(
switch (token) {
case Token::STRING:
Consume(Token::STRING);
*name = this->GetSymbol(scanner());
*name = impl()->GetSymbol();
break;
case Token::SMI:
Consume(Token::SMI);
*name = this->GetNumberAsSymbol(scanner());
*name = impl()->GetNumberAsSymbol();
break;
case Token::NUMBER:
Consume(Token::NUMBER);
*name = this->GetNumberAsSymbol(scanner());
*name = impl()->GetNumberAsSymbol();
break;
case Token::LBRACK: {
......@@ -2013,7 +2012,7 @@ ParserBase<Impl>::ParsePropertyDefinition(
MessageTemplate::kAwaitBindingIdentifier);
}
ExpressionT lhs =
this->ExpressionFromIdentifier(*name, next_beg_pos, next_end_pos);
impl()->ExpressionFromIdentifier(*name, next_beg_pos, next_end_pos);
CheckDestructuringElement(lhs, classifier, next_beg_pos, next_end_pos);
ExpressionT value;
......@@ -2152,8 +2151,7 @@ typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseObjectLiteral(
// '{' (PropertyDefinition (',' PropertyDefinition)* ','? )? '}'
int pos = peek_position();
typename Traits::Type::PropertyList properties =
this->NewPropertyList(4, zone_);
typename Traits::Type::PropertyList properties = impl()->NewPropertyList(4);
int number_of_boilerplate_properties = 0;
bool has_computed_names = false;
ObjectLiteralChecker checker(this);
......@@ -2210,8 +2208,7 @@ ParserBase<Impl>::ParseArguments(Scanner::Location* first_spread_arg_loc,
// '(' (AssignmentExpression)*[','] ')'
Scanner::Location spread_arg = Scanner::Location::invalid();
typename Traits::Type::ExpressionList result =
this->NewExpressionList(4, zone_);
typename Traits::Type::ExpressionList result = impl()->NewExpressionList(4);
Expect(Token::LPAREN, CHECK_OK_CUSTOM(NullExpressionList));
bool done = (peek() == Token::RPAREN);
bool was_unspread = false;
......@@ -2333,7 +2330,7 @@ ParserBase<Impl>::ParseAssignmentExpression(bool accept_IN,
// async Identifier => AsyncConciseBody
IdentifierT name =
ParseAndClassifyIdentifier(&arrow_formals_classifier, CHECK_OK);
expression = this->ExpressionFromIdentifier(
expression = impl()->ExpressionFromIdentifier(
name, position(), scanner()->location().end_pos, InferName::kNo);
if (fni_) {
// Remove `async` keyword from inferred name stack.
......@@ -2928,7 +2925,7 @@ ParserBase<Impl>::ParseLeftHandSideExpression(ExpressionClassifier* classifier,
// Explicit calls to the super constructor using super() perform an
// implicit binding assignment to the 'this' variable.
if (is_super_call) {
ExpressionT this_expr = this->ThisExpression(pos);
ExpressionT this_expr = impl()->ThisExpression(pos);
result =
factory()->NewAssignment(Token::INIT, this_expr, result, pos);
}
......@@ -3025,8 +3022,7 @@ ParserBase<Impl>::ParseMemberWithNewPrefixesExpression(
return result;
}
// NewExpression without arguments.
return factory()->NewCallNew(result, this->NewExpressionList(0, zone_),
new_pos);
return factory()->NewCallNew(result, impl()->NewExpressionList(0), new_pos);
}
// No 'new' or 'super' keyword.
return this->ParseMemberExpression(classifier, is_async, ok);
......@@ -3065,7 +3061,7 @@ typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseMemberExpression(
return impl()->EmptyExpression();
}
return this->FunctionSentExpression(factory(), pos);
return impl()->FunctionSentExpression(pos);
}
FunctionKind function_kind = Check(Token::MUL)
......@@ -3112,14 +3108,14 @@ typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseSuperExpression(
IsClassConstructor(kind)) {
if (peek() == Token::PERIOD || peek() == Token::LBRACK) {
scope->RecordSuperPropertyUsage();
return this->NewSuperPropertyReference(factory(), pos);
return impl()->NewSuperPropertyReference(pos);
}
// new super() is never allowed.
// super() is only allowed in derived constructor
if (!is_new && peek() == Token::LPAREN && IsSubclassConstructor(kind)) {
// TODO(rossberg): This might not be the correct FunctionState for the
// method here.
return this->NewSuperCallReference(factory(), pos);
return impl()->NewSuperCallReference(pos);
}
}
......@@ -3156,7 +3152,7 @@ ParserBase<Impl>::ParseNewTargetExpression(bool* ok) {
return impl()->EmptyExpression();
}
return this->NewTargetExpression(pos);
return impl()->NewTargetExpression(pos);
}
template <typename Impl>
......@@ -3432,7 +3428,7 @@ ParserBase<Impl>::ParseArrowFunctionLiteral(
bool is_lazily_parsed = (mode() == PARSE_LAZILY &&
formal_parameters.scope->AllowsLazyParsing());
if (is_lazily_parsed) {
body = this->NewStatementList(0, zone());
body = impl()->NewStatementList(0);
impl()->SkipLazyFunctionBody(&materialized_literal_count,
&expected_property_count, CHECK_OK);
if (formal_parameters.materialized_literals_count > 0) {
......@@ -3454,7 +3450,7 @@ ParserBase<Impl>::ParseArrowFunctionLiteral(
function_state_->return_expr_context());
ReturnExprScope allow_tail_calls(
function_state_, ReturnExprContext::kInsideValidReturnStatement);
body = this->NewStatementList(1, zone());
body = impl()->NewStatementList(1);
this->AddParameterInitializationBlock(formal_parameters, body, is_async,
CHECK_OK);
ExpressionClassifier classifier(this);
......
This diff is collapsed.
......@@ -175,38 +175,6 @@ class ParserBaseTraits<Parser> {
return reinterpret_cast<const Parser*>(this);
}
// Producing data during the recursive descent.
const AstRawString* GetSymbol(Scanner* scanner) const;
const AstRawString* GetNextSymbol(Scanner* scanner) const;
const AstRawString* GetNumberAsSymbol(Scanner* scanner) const;
Expression* ThisExpression(int pos = kNoSourcePosition);
Expression* NewSuperPropertyReference(AstNodeFactory* factory, int pos);
Expression* NewSuperCallReference(AstNodeFactory* factory, int pos);
Expression* NewTargetExpression(int pos);
Expression* FunctionSentExpression(AstNodeFactory* factory, int pos) const;
Literal* ExpressionFromLiteral(Token::Value token, int pos, Scanner* scanner,
AstNodeFactory* factory) const;
Expression* ExpressionFromIdentifier(const AstRawString* name,
int start_position, int end_position,
InferName = InferName::kYes);
Expression* ExpressionFromString(int pos, Scanner* scanner,
AstNodeFactory* factory) const;
Expression* GetIterator(Expression* iterable, AstNodeFactory* factory,
int pos);
ZoneList<v8::internal::Expression*>* NewExpressionList(int size,
Zone* zone) const {
return new(zone) ZoneList<v8::internal::Expression*>(size, zone);
}
ZoneList<ObjectLiteral::Property*>* NewPropertyList(int size,
Zone* zone) const {
return new(zone) ZoneList<ObjectLiteral::Property*>(size, zone);
}
ZoneList<v8::internal::Statement*>* NewStatementList(int size,
Zone* zone) const {
return new(zone) ZoneList<v8::internal::Statement*>(size, zone);
}
V8_INLINE void AddParameterInitializationBlock(
const ParserFormalParameters& parameters,
ZoneList<v8::internal::Statement*>* body, bool is_async, bool* ok);
......@@ -543,6 +511,7 @@ class Parser : public ParserBase<Parser> {
Expression* BuildIteratorNextResult(Expression* iterator, Variable* result,
int pos);
Expression* GetIterator(Expression* iterable, int pos);
// Initialize the components of a for-in / for-of statement.
Statement* InitializeForEachStatement(ForEachStatement* stmt,
......@@ -977,6 +946,64 @@ class Parser : public ParserBase<Parser> {
return factory()->NewTheHoleLiteral(kNoSourcePosition);
}
// Producing data during the recursive descent.
V8_INLINE const AstRawString* GetSymbol() const {
const AstRawString* result = scanner()->CurrentSymbol(ast_value_factory());
DCHECK(result != NULL);
return result;
}
V8_INLINE const AstRawString* GetNextSymbol() const {
return scanner()->NextSymbol(ast_value_factory());
}
V8_INLINE const AstRawString* GetNumberAsSymbol() const {
double double_value = scanner()->DoubleValue();
char array[100];
const char* string = DoubleToCString(double_value, ArrayVector(array));
return ast_value_factory()->GetOneByteString(string);
}
V8_INLINE Expression* ThisExpression(int pos = kNoSourcePosition) {
return NewUnresolved(ast_value_factory()->this_string(), pos, pos + 4,
Variable::THIS);
}
Expression* NewSuperPropertyReference(int pos);
Expression* NewSuperCallReference(int pos);
Expression* NewTargetExpression(int pos);
Expression* FunctionSentExpression(int pos);
Literal* ExpressionFromLiteral(Token::Value token, int pos);
V8_INLINE Expression* ExpressionFromIdentifier(
const AstRawString* name, int start_position, int end_position,
InferName infer = InferName::kYes) {
if (infer == InferName::kYes && fni_ != NULL) {
fni_->PushVariableName(name);
}
return NewUnresolved(name, start_position, end_position);
}
V8_INLINE Expression* ExpressionFromString(int pos) {
const AstRawString* symbol = GetSymbol();
if (fni_ != NULL) fni_->PushLiteralName(symbol);
return factory()->NewStringLiteral(symbol, pos);
}
V8_INLINE ZoneList<v8::internal::Expression*>* NewExpressionList(
int size) const {
return new (zone()) ZoneList<v8::internal::Expression*>(size, zone());
}
V8_INLINE ZoneList<ObjectLiteral::Property*>* NewPropertyList(
int size) const {
return new (zone()) ZoneList<ObjectLiteral::Property*>(size, zone());
}
V8_INLINE ZoneList<v8::internal::Statement*>* NewStatementList(
int size) const {
return new (zone()) ZoneList<v8::internal::Statement*>(size, zone());
}
// Parser's private field members.
Scanner scanner_;
......
......@@ -374,7 +374,7 @@ void Parser::PatternRewriter::VisitArrayLiteral(ArrayLiteral* node,
auto temp = *temp_var = CreateTempVar(current_value_);
auto iterator = CreateTempVar(parser_->GetIterator(
factory()->NewVariableProxy(temp), factory(), kNoSourcePosition));
factory()->NewVariableProxy(temp), kNoSourcePosition));
auto done =
CreateTempVar(factory()->NewBooleanLiteral(false, kNoSourcePosition));
auto result = CreateTempVar();
......
......@@ -41,9 +41,8 @@ namespace internal {
#define DUMMY ) // to make indentation work
#undef DUMMY
PreParserIdentifier ParserBaseTraits<PreParser>::GetSymbol(
Scanner* scanner) const {
switch (scanner->current_token()) {
PreParserIdentifier PreParser::GetSymbol() const {
switch (scanner()->current_token()) {
case Token::ENUM:
return PreParserIdentifier::Enum();
case Token::AWAIT:
......@@ -59,28 +58,20 @@ PreParserIdentifier ParserBaseTraits<PreParser>::GetSymbol(
case Token::ASYNC:
return PreParserIdentifier::Async();
default:
if (scanner->UnescapedLiteralMatches("eval", 4))
if (scanner()->UnescapedLiteralMatches("eval", 4))
return PreParserIdentifier::Eval();
if (scanner->UnescapedLiteralMatches("arguments", 9))
if (scanner()->UnescapedLiteralMatches("arguments", 9))
return PreParserIdentifier::Arguments();
if (scanner->UnescapedLiteralMatches("undefined", 9))
if (scanner()->UnescapedLiteralMatches("undefined", 9))
return PreParserIdentifier::Undefined();
if (scanner->LiteralMatches("prototype", 9))
if (scanner()->LiteralMatches("prototype", 9))
return PreParserIdentifier::Prototype();
if (scanner->LiteralMatches("constructor", 11))
if (scanner()->LiteralMatches("constructor", 11))
return PreParserIdentifier::Constructor();
return PreParserIdentifier::Default();
}
}
PreParserExpression ParserBaseTraits<PreParser>::ExpressionFromString(
int pos, Scanner* scanner, PreParserFactory* factory) const {
if (scanner->UnescapedLiteralMatches("use strict", 10)) {
return PreParserExpression::UseStrictStringLiteral();
}
return PreParserExpression::StringLiteral();
}
PreParser::PreParseResult PreParser::PreParseLazyFunction(
LanguageMode language_mode, FunctionKind kind, bool has_simple_parameters,
bool parsing_module, ParserRecorder* log, Scanner::BookmarkScope* bookmark,
......
......@@ -627,73 +627,6 @@ class ParserBaseTraits<PreParser> {
// A dummy function, just useful as an argument to CHECK_OK_CUSTOM.
static void Void() {}
// Producing data during the recursive descent.
PreParserIdentifier GetSymbol(Scanner* scanner) const;
PreParserIdentifier GetNextSymbol(Scanner* scanner) const {
return PreParserIdentifier::Default();
}
PreParserIdentifier GetNumberAsSymbol(Scanner* scanner) const {
return PreParserIdentifier::Default();
}
PreParserExpression ThisExpression(int pos = kNoSourcePosition) {
return PreParserExpression::This();
}
PreParserExpression NewSuperPropertyReference(PreParserFactory* factory,
int pos) {
return PreParserExpression::Default();
}
PreParserExpression NewSuperCallReference(PreParserFactory* factory,
int pos) {
return PreParserExpression::SuperCallReference();
}
PreParserExpression NewTargetExpression(int pos) {
return PreParserExpression::Default();
}
PreParserExpression FunctionSentExpression(PreParserFactory* factory,
int pos) const {
return PreParserExpression::Default();
}
PreParserExpression ExpressionFromLiteral(Token::Value token, int pos,
Scanner* scanner,
PreParserFactory* factory) const {
return PreParserExpression::Default();
}
PreParserExpression ExpressionFromIdentifier(PreParserIdentifier name,
int start_position,
int end_position,
InferName = InferName::kYes) {
return PreParserExpression::FromIdentifier(name);
}
PreParserExpression ExpressionFromString(int pos, Scanner* scanner,
PreParserFactory* factory) const;
PreParserExpression GetIterator(PreParserExpression iterable,
PreParserFactory* factory, int pos) {
return PreParserExpression::Default();
}
PreParserExpressionList NewExpressionList(int size, Zone* zone) const {
return PreParserExpressionList();
}
PreParserExpressionList NewPropertyList(int size, Zone* zone) const {
return PreParserExpressionList();
}
PreParserStatementList NewStatementList(int size, Zone* zone) const {
return PreParserStatementList();
}
void AddParameterInitializationBlock(
const PreParserFormalParameters& parameters, PreParserStatementList body,
bool is_async, bool* ok) {}
......@@ -1145,6 +1078,67 @@ class PreParser : public ParserBase<PreParser> {
return PreParserExpression::Default();
}
// Producing data during the recursive descent.
PreParserIdentifier GetSymbol() const;
V8_INLINE PreParserIdentifier GetNextSymbol() const {
return PreParserIdentifier::Default();
}
V8_INLINE PreParserIdentifier GetNumberAsSymbol() const {
return PreParserIdentifier::Default();
}
V8_INLINE PreParserExpression ThisExpression(int pos = kNoSourcePosition) {
return PreParserExpression::This();
}
V8_INLINE PreParserExpression NewSuperPropertyReference(int pos) {
return PreParserExpression::Default();
}
V8_INLINE PreParserExpression NewSuperCallReference(int pos) {
return PreParserExpression::SuperCallReference();
}
V8_INLINE PreParserExpression NewTargetExpression(int pos) {
return PreParserExpression::Default();
}
V8_INLINE PreParserExpression FunctionSentExpression(int pos) {
return PreParserExpression::Default();
}
V8_INLINE PreParserExpression ExpressionFromLiteral(Token::Value token,
int pos) {
return PreParserExpression::Default();
}
V8_INLINE PreParserExpression ExpressionFromIdentifier(
PreParserIdentifier name, int start_position, int end_position,
InferName infer = InferName::kYes) {
return PreParserExpression::FromIdentifier(name);
}
V8_INLINE PreParserExpression ExpressionFromString(int pos) {
if (scanner()->UnescapedLiteralMatches("use strict", 10)) {
return PreParserExpression::UseStrictStringLiteral();
}
return PreParserExpression::StringLiteral();
}
V8_INLINE PreParserExpressionList NewExpressionList(int size) const {
return PreParserExpressionList();
}
V8_INLINE PreParserExpressionList NewPropertyList(int size) const {
return PreParserExpressionList();
}
V8_INLINE PreParserStatementList NewStatementList(int size) const {
return PreParserStatementList();
}
// Preparser's private field members.
int* use_counts_;
......
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