Commit c374d413 authored by nikolaos's avatar nikolaos Committed by Commit bot

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

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

- BuildIteratorResult
- BuildUnaryExpression
- EmptyExpression
- EmptyFunctionLiteral
- EmptyIdentifier
- EmptyIdentifierString
- EmptyLiteral
- EmptyObjectLiteralProperty
- GetLiteralTheHole
- NewThrowReferenceError
- NewThrowSyntaxError
- NewThrowTypeError
- NullExpressionList
- ReportMessageAt

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

Review-Url: https://codereview.chromium.org/2268413002
Cr-Commit-Position: refs/heads/master@{#38862}
parent 62630927
This diff is collapsed.
......@@ -367,15 +367,15 @@ bool Parser::ShortcutNumericLiteralBinaryExpression(Expression** x,
return false;
}
Expression* ParserBaseTraits<Parser>::BuildUnaryExpression(
Expression* expression, Token::Value op, int pos, AstNodeFactory* factory) {
Expression* Parser::BuildUnaryExpression(Expression* expression,
Token::Value op, int pos) {
DCHECK(expression != NULL);
if (expression->IsLiteral()) {
const AstValue* literal = expression->AsLiteral()->raw_value();
if (op == Token::NOT) {
// Convert the literal to a boolean condition and negate it.
bool condition = literal->BooleanValue();
return factory->NewBooleanLiteral(!condition, pos);
return factory()->NewBooleanLiteral(!condition, pos);
} else if (literal->IsNumber()) {
// Compute some expressions involving only number literals.
double value = literal->AsNumber();
......@@ -384,9 +384,10 @@ Expression* ParserBaseTraits<Parser>::BuildUnaryExpression(
case Token::ADD:
return expression;
case Token::SUB:
return factory->NewNumberLiteral(-value, pos, has_dot);
return factory()->NewNumberLiteral(-value, pos, has_dot);
case Token::BIT_NOT:
return factory->NewNumberLiteral(~DoubleToInt32(value), pos, has_dot);
return factory()->NewNumberLiteral(~DoubleToInt32(value), pos,
has_dot);
default:
break;
}
......@@ -394,53 +395,33 @@ Expression* ParserBaseTraits<Parser>::BuildUnaryExpression(
}
// Desugar '+foo' => 'foo*1'
if (op == Token::ADD) {
return factory->NewBinaryOperation(
Token::MUL, expression, factory->NewNumberLiteral(1, pos, true), pos);
return factory()->NewBinaryOperation(
Token::MUL, expression, factory()->NewNumberLiteral(1, pos, true), pos);
}
// The same idea for '-foo' => 'foo*(-1)'.
if (op == Token::SUB) {
return factory->NewBinaryOperation(
Token::MUL, expression, factory->NewNumberLiteral(-1, pos), pos);
return factory()->NewBinaryOperation(
Token::MUL, expression, factory()->NewNumberLiteral(-1, pos), pos);
}
// ...and one more time for '~foo' => 'foo^(~0)'.
if (op == Token::BIT_NOT) {
return factory->NewBinaryOperation(
Token::BIT_XOR, expression, factory->NewNumberLiteral(~0, pos), pos);
return factory()->NewBinaryOperation(
Token::BIT_XOR, expression, factory()->NewNumberLiteral(~0, pos), pos);
}
return factory->NewUnaryOperation(op, expression, pos);
return factory()->NewUnaryOperation(op, expression, pos);
}
Expression* ParserBaseTraits<Parser>::BuildIteratorResult(Expression* value,
bool done) {
Expression* Parser::BuildIteratorResult(Expression* value, bool done) {
int pos = kNoSourcePosition;
AstNodeFactory* factory = delegate()->factory();
Zone* zone = delegate()->zone();
if (value == nullptr) value = factory->NewUndefinedLiteral(pos);
auto args = new (zone) ZoneList<Expression*>(2, zone);
args->Add(value, zone);
args->Add(factory->NewBooleanLiteral(done, pos), zone);
if (value == nullptr) value = factory()->NewUndefinedLiteral(pos);
return factory->NewCallRuntime(Runtime::kInlineCreateIterResultObject, args,
pos);
}
Expression* ParserBaseTraits<Parser>::NewThrowReferenceError(
MessageTemplate::Template message, int pos) {
return delegate()->NewThrowError(
Runtime::kNewReferenceError, message,
delegate()->ast_value_factory()->empty_string(), pos);
}
Expression* ParserBaseTraits<Parser>::NewThrowSyntaxError(
MessageTemplate::Template message, const AstRawString* arg, int pos) {
return delegate()->NewThrowError(Runtime::kNewSyntaxError, message, arg, pos);
}
auto args = new (zone()) ZoneList<Expression*>(2, zone());
args->Add(value, zone());
args->Add(factory()->NewBooleanLiteral(done, pos), zone());
Expression* ParserBaseTraits<Parser>::NewThrowTypeError(
MessageTemplate::Template message, const AstRawString* arg, int pos) {
return delegate()->NewThrowError(Runtime::kNewTypeError, message, arg, pos);
return factory()->NewCallRuntime(Runtime::kInlineCreateIterResultObject, args,
pos);
}
Expression* Parser::NewThrowError(Runtime::FunctionId id,
......@@ -453,34 +434,6 @@ Expression* Parser::NewThrowError(Runtime::FunctionId id,
return factory()->NewThrow(call_constructor, pos);
}
void ParserBaseTraits<Parser>::ReportMessageAt(
Scanner::Location source_location, MessageTemplate::Template message,
const char* arg, ParseErrorType error_type) {
if (delegate()->stack_overflow()) {
// Suppress the error message (syntax error or such) in the presence of a
// stack overflow. The isolate allows only one pending exception at at time
// and we want to report the stack overflow later.
return;
}
delegate()->pending_error_handler_.ReportMessageAt(source_location.beg_pos,
source_location.end_pos,
message, arg, error_type);
}
void ParserBaseTraits<Parser>::ReportMessageAt(
Scanner::Location source_location, MessageTemplate::Template message,
const AstRawString* arg, ParseErrorType error_type) {
if (delegate()->stack_overflow()) {
// Suppress the error message (syntax error or such) in the presence of a
// stack overflow. The isolate allows only one pending exception at at time
// and we want to report the stack overflow later.
return;
}
delegate()->pending_error_handler_.ReportMessageAt(source_location.beg_pos,
source_location.end_pos,
message, arg, error_type);
}
const AstRawString* ParserBaseTraits<Parser>::GetSymbol(
Scanner* scanner) const {
const AstRawString* result =
......@@ -606,11 +559,6 @@ Expression* ParserBaseTraits<Parser>::GetIterator(Expression* iterable,
return factory->NewCall(prop, args, pos);
}
Literal* ParserBaseTraits<Parser>::GetLiteralTheHole(
int position, AstNodeFactory* factory) const {
return factory->NewTheHoleLiteral(kNoSourcePosition);
}
void Parser::MarkTailPosition(Expression* expression) {
expression->MarkTail();
}
......@@ -3967,7 +3915,8 @@ void ParserBaseTraits<Parser>::ParseArrowFunctionFormalParameterList(
scope_snapshot.Reparent(parameters->scope);
if (parameters->Arity() > Code::kMaxArguments) {
ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList);
delegate()->ReportMessageAt(params_loc,
MessageTemplate::kMalformedArrowFunParamList);
*ok = false;
return;
}
......
......@@ -175,63 +175,6 @@ class ParserBaseTraits<Parser> {
return reinterpret_cast<const Parser*>(this);
}
// Rewrites the following types of unary expressions:
// not <literal> -> true / false
// + <numeric literal> -> <numeric literal>
// - <numeric literal> -> <numeric literal with value negated>
// ! <literal> -> true / false
// The following rewriting rules enable the collection of type feedback
// without any special stub and the multiplication is removed later in
// Crankshaft's canonicalization pass.
// + foo -> foo * 1
// - foo -> foo * (-1)
// ~ foo -> foo ^(~0)
Expression* BuildUnaryExpression(Expression* expression, Token::Value op,
int pos, AstNodeFactory* factory);
Expression* BuildIteratorResult(Expression* value, bool done);
// Generate AST node that throws a ReferenceError with the given type.
Expression* NewThrowReferenceError(MessageTemplate::Template message,
int pos);
// Generate AST node that throws a SyntaxError with the given
// type. The first argument may be null (in the handle sense) in
// which case no arguments are passed to the constructor.
Expression* NewThrowSyntaxError(MessageTemplate::Template message,
const AstRawString* arg, int pos);
// Generate AST node that throws a TypeError with the given
// type. Both arguments must be non-null (in the handle sense).
Expression* NewThrowTypeError(MessageTemplate::Template message,
const AstRawString* arg, int pos);
// Reporting errors.
void ReportMessageAt(Scanner::Location source_location,
MessageTemplate::Template message,
const char* arg = NULL,
ParseErrorType error_type = kSyntaxError);
void ReportMessageAt(Scanner::Location source_location,
MessageTemplate::Template message,
const AstRawString* arg,
ParseErrorType error_type = kSyntaxError);
// "null" return type creators.
static const AstRawString* EmptyIdentifier() { return nullptr; }
static Expression* EmptyExpression() { return nullptr; }
static Literal* EmptyLiteral() { return nullptr; }
static ObjectLiteralProperty* EmptyObjectLiteralProperty() { return nullptr; }
static FunctionLiteral* EmptyFunctionLiteral() { return nullptr; }
// Used in error return values.
static ZoneList<Expression*>* NullExpressionList() { return nullptr; }
// Non-NULL empty string.
V8_INLINE const AstRawString* EmptyIdentifierString() const;
// Odd-ball literal creators.
Literal* GetLiteralTheHole(int position, AstNodeFactory* factory) const;
// Producing data during the recursive descent.
const AstRawString* GetSymbol(Scanner* scanner) const;
const AstRawString* GetNextSymbol(Scanner* scanner) const;
......@@ -939,6 +882,101 @@ class Parser : public ParserBase<Parser> {
bool ShortcutNumericLiteralBinaryExpression(Expression** x, Expression* y,
Token::Value op, int pos);
// Rewrites the following types of unary expressions:
// not <literal> -> true / false
// + <numeric literal> -> <numeric literal>
// - <numeric literal> -> <numeric literal with value negated>
// ! <literal> -> true / false
// The following rewriting rules enable the collection of type feedback
// without any special stub and the multiplication is removed later in
// Crankshaft's canonicalization pass.
// + foo -> foo * 1
// - foo -> foo * (-1)
// ~ foo -> foo ^(~0)
Expression* BuildUnaryExpression(Expression* expression, Token::Value op,
int pos);
Expression* BuildIteratorResult(Expression* value, bool done);
// Generate AST node that throws a ReferenceError with the given type.
V8_INLINE Expression* NewThrowReferenceError(
MessageTemplate::Template message, int pos) {
return NewThrowError(Runtime::kNewReferenceError, message,
ast_value_factory()->empty_string(), pos);
}
// Generate AST node that throws a SyntaxError with the given
// type. The first argument may be null (in the handle sense) in
// which case no arguments are passed to the constructor.
V8_INLINE Expression* NewThrowSyntaxError(MessageTemplate::Template message,
const AstRawString* arg, int pos) {
return NewThrowError(Runtime::kNewSyntaxError, message, arg, pos);
}
// Generate AST node that throws a TypeError with the given
// type. Both arguments must be non-null (in the handle sense).
V8_INLINE Expression* NewThrowTypeError(MessageTemplate::Template message,
const AstRawString* arg, int pos) {
return NewThrowError(Runtime::kNewTypeError, message, arg, pos);
}
// Reporting errors.
V8_INLINE void ReportMessageAt(Scanner::Location source_location,
MessageTemplate::Template message,
const char* arg = NULL,
ParseErrorType error_type = kSyntaxError) {
if (stack_overflow()) {
// Suppress the error message (syntax error or such) in the presence of a
// stack overflow. The isolate allows only one pending exception at at
// time
// and we want to report the stack overflow later.
return;
}
pending_error_handler_.ReportMessageAt(source_location.beg_pos,
source_location.end_pos, message,
arg, error_type);
}
V8_INLINE void ReportMessageAt(Scanner::Location source_location,
MessageTemplate::Template message,
const AstRawString* arg,
ParseErrorType error_type = kSyntaxError) {
if (stack_overflow()) {
// Suppress the error message (syntax error or such) in the presence of a
// stack overflow. The isolate allows only one pending exception at at
// time
// and we want to report the stack overflow later.
return;
}
pending_error_handler_.ReportMessageAt(source_location.beg_pos,
source_location.end_pos, message,
arg, error_type);
}
// "null" return type creators.
V8_INLINE static const AstRawString* EmptyIdentifier() { return nullptr; }
V8_INLINE static Expression* EmptyExpression() { return nullptr; }
V8_INLINE static Literal* EmptyLiteral() { return nullptr; }
V8_INLINE static ObjectLiteralProperty* EmptyObjectLiteralProperty() {
return nullptr;
}
V8_INLINE static FunctionLiteral* EmptyFunctionLiteral() { return nullptr; }
// Used in error return values.
V8_INLINE static ZoneList<Expression*>* NullExpressionList() {
return nullptr;
}
// Non-NULL empty string.
V8_INLINE const AstRawString* EmptyIdentifierString() const {
return ast_value_factory()->empty_string();
}
// Odd-ball literal creators.
V8_INLINE Literal* GetLiteralTheHole(int position) {
return factory()->NewTheHoleLiteral(kNoSourcePosition);
}
// Parser's private field members.
Scanner scanner_;
......@@ -963,10 +1001,6 @@ class Parser : public ParserBase<Parser> {
#endif // DEBUG
};
const AstRawString* ParserBaseTraits<Parser>::EmptyIdentifierString() const {
return delegate()->ast_value_factory()->empty_string();
}
// Support for handling complex values (array and object literals) that
// can be fully handled at compile time.
......
......@@ -41,19 +41,6 @@ namespace internal {
#define DUMMY ) // to make indentation work
#undef DUMMY
void ParserBaseTraits<PreParser>::ReportMessageAt(
Scanner::Location source_location, MessageTemplate::Template message,
const char* arg, ParseErrorType error_type) {
delegate()->log_->LogMessage(source_location.beg_pos, source_location.end_pos,
message, arg, error_type);
}
void ParserBaseTraits<PreParser>::ReportMessageAt(
Scanner::Location source_location, MessageTemplate::Template message,
const AstRawString* arg, ParseErrorType error_type) {
UNREACHABLE();
}
PreParserIdentifier ParserBaseTraits<PreParser>::GetSymbol(
Scanner* scanner) const {
switch (scanner->current_token()) {
......
......@@ -624,75 +624,9 @@ class ParserBaseTraits<PreParser> {
return reinterpret_cast<const PreParser*>(this);
}
PreParserExpression BuildUnaryExpression(PreParserExpression expression,
Token::Value op, int pos,
PreParserFactory* factory) {
return PreParserExpression::Default();
}
PreParserExpression BuildIteratorResult(PreParserExpression value,
bool done) {
return PreParserExpression::Default();
}
PreParserExpression NewThrowReferenceError(MessageTemplate::Template message,
int pos) {
return PreParserExpression::Default();
}
PreParserExpression NewThrowSyntaxError(MessageTemplate::Template message,
PreParserIdentifier arg, int pos) {
return PreParserExpression::Default();
}
PreParserExpression NewThrowTypeError(MessageTemplate::Template message,
PreParserIdentifier arg, int pos) {
return PreParserExpression::Default();
}
// Reporting errors.
void ReportMessageAt(Scanner::Location source_location,
MessageTemplate::Template message,
const char* arg = NULL,
ParseErrorType error_type = kSyntaxError);
void ReportMessageAt(Scanner::Location source_location,
MessageTemplate::Template message,
const AstRawString* arg,
ParseErrorType error_type = kSyntaxError);
// A dummy function, just useful as an argument to CHECK_OK_CUSTOM.
static void Void() {}
// "null" return type creators.
static PreParserIdentifier EmptyIdentifier() {
return PreParserIdentifier::Default();
}
static PreParserExpression EmptyExpression() {
return PreParserExpression::Default();
}
static PreParserExpression EmptyLiteral() {
return PreParserExpression::Default();
}
static PreParserExpression EmptyObjectLiteralProperty() {
return PreParserExpression::Default();
}
static PreParserExpression EmptyFunctionLiteral() {
return PreParserExpression::Default();
}
static PreParserExpressionList NullExpressionList() {
return PreParserExpressionList();
}
PreParserIdentifier EmptyIdentifierString() const {
return PreParserIdentifier::Default();
}
// Odd-ball literal creators.
PreParserExpression GetLiteralTheHole(int position,
PreParserFactory* factory) const {
return PreParserExpression::Default();
}
// Producing data during the recursive descent.
PreParserIdentifier GetSymbol(Scanner* scanner) const;
......@@ -1141,6 +1075,76 @@ class PreParser : public ParserBase<PreParser> {
return false;
}
V8_INLINE PreParserExpression BuildUnaryExpression(
PreParserExpression expression, Token::Value op, int pos) {
return PreParserExpression::Default();
}
V8_INLINE PreParserExpression BuildIteratorResult(PreParserExpression value,
bool done) {
return PreParserExpression::Default();
}
V8_INLINE PreParserExpression
NewThrowReferenceError(MessageTemplate::Template message, int pos) {
return PreParserExpression::Default();
}
V8_INLINE PreParserExpression NewThrowSyntaxError(
MessageTemplate::Template message, PreParserIdentifier arg, int pos) {
return PreParserExpression::Default();
}
V8_INLINE PreParserExpression NewThrowTypeError(
MessageTemplate::Template message, PreParserIdentifier arg, int pos) {
return PreParserExpression::Default();
}
// Reporting errors.
V8_INLINE void ReportMessageAt(Scanner::Location source_location,
MessageTemplate::Template message,
const char* arg = NULL,
ParseErrorType error_type = kSyntaxError) {
log_->LogMessage(source_location.beg_pos, source_location.end_pos, message,
arg, error_type);
}
V8_INLINE void ReportMessageAt(Scanner::Location source_location,
MessageTemplate::Template message,
const AstRawString* arg,
ParseErrorType error_type = kSyntaxError) {
UNREACHABLE();
}
// "null" return type creators.
V8_INLINE static PreParserIdentifier EmptyIdentifier() {
return PreParserIdentifier::Default();
}
V8_INLINE static PreParserExpression EmptyExpression() {
return PreParserExpression::Default();
}
V8_INLINE static PreParserExpression EmptyLiteral() {
return PreParserExpression::Default();
}
V8_INLINE static PreParserExpression EmptyObjectLiteralProperty() {
return PreParserExpression::Default();
}
V8_INLINE static PreParserExpression EmptyFunctionLiteral() {
return PreParserExpression::Default();
}
V8_INLINE static PreParserExpressionList NullExpressionList() {
return PreParserExpressionList();
}
V8_INLINE PreParserIdentifier EmptyIdentifierString() const {
return PreParserIdentifier::Default();
}
// Odd-ball literal creators.
V8_INLINE PreParserExpression GetLiteralTheHole(int position) {
return PreParserExpression::Default();
}
// 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