#include "src/parsing/rewriter.h"
#include <optional>
#include "src/ast/ast.h"
#include "src/ast/scopes.h"
#include "src/logging/runtime-call-stats-scope.h"
#include "src/objects/objects-inl.h"
#include "src/parsing/parse-info.h"
#include "src/parsing/parser.h"
#include "src/zone/zone-list-inl.h"
#define VISIT_AND_RETURN_IF_STACK_OVERFLOW(param) \
Visit(param); \
if (CheckStackOverflow()) return;
#define PROCESS_AND_RETURN_IF_STACK_OVERFLOW(param) \
Process(param); \
if (CheckStackOverflow()) return;
namespace v8::internal {
class Processor final : public AstVisitor<Processor> {
public:
Processor(uintptr_t stack_limit, DeclarationScope* closure_scope,
Variable* result, AstValueFactory* ast_value_factory, Zone* zone)
: result_(result),
replacement_(nullptr),
zone_(zone),
closure_scope_(closure_scope),
factory_(ast_value_factory, zone),
result_assigned_(false),
is_set_(false),
breakable_(false) {
DCHECK_EQ(closure_scope, closure_scope->GetClosureScope());
InitializeAstVisitor(stack_limit);
}
void Process(ZonePtrList<Statement>* statements);
bool result_assigned() const { return result_assigned_; }
Zone* zone() { return zone_; }
DeclarationScope* closure_scope() { return closure_scope_; }
AstNodeFactory* factory() { return &factory_; }
Expression* SetResult(Expression* value) {
result_assigned_ = true;
VariableProxy* result_proxy = factory()->NewVariableProxy(result_);
return factory()->NewAssignment(Token::kAssign, result_proxy, value,
kNoSourcePosition);
}
Statement* AssignUndefinedBefore(Statement* s);
private:
Variable* result_;
Statement* replacement_;
class V8_NODISCARD BreakableScope final {
public:
explicit BreakableScope(Processor* processor, bool breakable = true)
: processor_(processor), previous_(processor->breakable_) {
processor->breakable_ = processor->breakable_ || breakable;
}
~BreakableScope() { processor_->breakable_ = previous_; }
private:
Processor* processor_;
bool previous_;
};
Zone* zone_;
DeclarationScope* closure_scope_;
AstNodeFactory factory_;
#define DEF_VISIT(type) void Visit##type(type* node);
AST_NODE_LIST(DEF_VISIT)
#undef DEF_VISIT
void VisitIterationStatement(IterationStatement* stmt);
DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
bool result_assigned_;
bool is_set_;
bool breakable_;
};
Statement* Processor::AssignUndefinedBefore(Statement* s) {
Expression* undef = factory()->NewUndefinedLiteral(kNoSourcePosition);
Expression* assignment = SetResult(undef);
Block* b = factory()->NewBlock(2, false);
b->statements()->Add(
factory()->NewExpressionStatement(assignment, kNoSourcePosition), zone());
b->statements()->Add(s, zone());
return b;
}
void Processor::Process(ZonePtrList<Statement>* statements) {
for (int i = statements->length() - 1; i >= 0 && (breakable_ || !is_set_);
--i) {
VISIT_AND_RETURN_IF_STACK_OVERFLOW(statements->at(i));
statements->Set(i, replacement_);
}
}
void Processor::VisitBlock(Block* node) {
if (!node->ignore_completion_value()) {
BreakableScope scope(this, node->is_breakable());
PROCESS_AND_RETURN_IF_STACK_OVERFLOW(node->statements());
}
replacement_ = node;
}
void Processor::VisitExpressionStatement(ExpressionStatement* node) {
if (!is_set_) {
node->set_expression(SetResult(node->expression()));
is_set_ = true;
}
replacement_ = node;
}
void Processor::VisitIfStatement(IfStatement* node) {
bool set_after = is_set_;
VISIT_AND_RETURN_IF_STACK_OVERFLOW(node->then_statement());
node->set_then_statement(replacement_);
bool set_in_then = is_set_;
is_set_ = set_after;
VISIT_AND_RETURN_IF_STACK_OVERFLOW(node->else_statement());
node->set_else_statement(replacement_);
replacement_ = set_in_then && is_set_ ? node : AssignUndefinedBefore(node);
is_set_ = true;
}
void Processor::VisitIterationStatement(IterationStatement* node) {
DCHECK(breakable_ || !is_set_);
BreakableScope scope(this);
VISIT_AND_RETURN_IF_STACK_OVERFLOW(node->body());
node->set_body(replacement_);
replacement_ = AssignUndefinedBefore(node);
is_set_ = true;
}
void Processor::VisitDoWhileStatement(DoWhileStatement* node) {
VisitIterationStatement(node);
}
void Processor::VisitWhileStatement(WhileStatement* node) {
VisitIterationStatement(node);
}
void Processor::VisitForStatement(ForStatement* node) {
VisitIterationStatement(node);
}
void Processor::VisitForInStatement(ForInStatement* node) {
VisitIterationStatement(node);
}
void Processor::VisitForOfStatement(ForOfStatement* node) {
VisitIterationStatement(node);
}
void Processor::VisitTryCatchStatement(TryCatchStatement* node) {
bool set_after = is_set_;
VISIT_AND_RETURN_IF_STACK_OVERFLOW(node->try_block());
node->set_try_block(static_cast<Block*>(replacement_));
bool set_in_try = is_set_;
is_set_ = set_after;
VISIT_AND_RETURN_IF_STACK_OVERFLOW(node->catch_block());
node->set_catch_block(static_cast<Block*>(replacement_));
replacement_ = is_set_ && set_in_try ? node : AssignUndefinedBefore(node);
is_set_ = true;
}
void Processor::VisitTryFinallyStatement(TryFinallyStatement* node) {
if (breakable_) {
is_set_ = true;
VISIT_AND_RETURN_IF_STACK_OVERFLOW(node->finally_block());
node->set_finally_block(replacement_->AsBlock());
CHECK_NOT_NULL(closure_scope());
if (is_set_) {
Variable* backup = closure_scope()->NewTemporary(
factory()->ast_value_factory()->dot_result_string());
Expression* backup_proxy = factory()->NewVariableProxy(backup);
Expression* result_proxy = factory()->NewVariableProxy(result_);
Expression* save = factory()->NewAssignment(
Token::kAssign, backup_proxy, result_proxy, kNoSourcePosition);
Expression* restore = factory()->NewAssignment(
Token::kAssign, result_proxy, backup_proxy, kNoSourcePosition);
node->finally_block()->statements()->InsertAt(
0, factory()->NewExpressionStatement(save, kNoSourcePosition),
zone());
node->finally_block()->statements()->Add(
factory()->NewExpressionStatement(restore, kNoSourcePosition),
zone());
} else {
Expression* undef = factory()->NewUndefinedLiteral(kNoSourcePosition);
Expression* assignment = SetResult(undef);
node->finally_block()->statements()->InsertAt(
0, factory()->NewExpressionStatement(assignment, kNoSourcePosition),
zone());
}
is_set_ = false;
}
VISIT_AND_RETURN_IF_STACK_OVERFLOW(node->try_block());
node->set_try_block(replacement_->AsBlock());
replacement_ = is_set_ ? node : AssignUndefinedBefore(node);
is_set_ = true;
}
void Processor::VisitSwitchStatement(SwitchStatement* node) {
DCHECK(breakable_ || !is_set_);
BreakableScope scope(this);
ZonePtrList<CaseClause>* clauses = node->cases();
for (int i = clauses->length() - 1; i >= 0; --i) {
CaseClause* clause = clauses->at(i);
PROCESS_AND_RETURN_IF_STACK_OVERFLOW(clause->statements());
}
replacement_ = AssignUndefinedBefore(node);
is_set_ = true;
}
void Processor::VisitContinueStatement(ContinueStatement* node) {
is_set_ = false;
replacement_ = node;
}
void Processor::VisitBreakStatement(BreakStatement* node) {
is_set_ = false;
replacement_ = node;
}
void Processor::VisitWithStatement(WithStatement* node) {
VISIT_AND_RETURN_IF_STACK_OVERFLOW(node->statement());
node->set_statement(replacement_);
replacement_ = is_set_ ? node : AssignUndefinedBefore(node);
is_set_ = true;
}
void Processor::VisitSloppyBlockFunctionStatement(
SloppyBlockFunctionStatement* node) {
VISIT_AND_RETURN_IF_STACK_OVERFLOW(node->statement());
node->set_statement(replacement_);
replacement_ = node;
}
void Processor::VisitEmptyStatement(EmptyStatement* node) {
replacement_ = node;
}
void Processor::VisitReturnStatement(ReturnStatement* node) {
is_set_ = true;
replacement_ = node;
}
void Processor::VisitDebuggerStatement(DebuggerStatement* node) {
replacement_ = node;
}
void Processor::VisitInitializeClassMembersStatement(
InitializeClassMembersStatement* node) {
replacement_ = node;
}
void Processor::VisitInitializeClassStaticElementsStatement(
InitializeClassStaticElementsStatement* node) {
replacement_ = node;
}
void Processor::VisitAutoAccessorGetterBody(AutoAccessorGetterBody* node) {
replacement_ = node;
}
void Processor::VisitAutoAccessorSetterBody(AutoAccessorSetterBody* node) {
replacement_ = node;
}
#define DEF_VISIT(type) \
void Processor::Visit##type(type* expr) { UNREACHABLE(); }
EXPRESSION_NODE_LIST(DEF_VISIT)
#undef DEF_VISIT
#define DEF_VISIT(type) \
void Processor::Visit##type(type* expr) { UNREACHABLE(); }
DECLARATION_NODE_LIST(DEF_VISIT)
#undef DEF_VISIT
bool Rewriter::Rewrite(ParseInfo* info, bool* out_has_stack_overflow) {
RCS_SCOPE(info->runtime_call_stats(),
RuntimeCallCounterId::kCompileRewriteReturnResult,
RuntimeCallStats::kThreadSpecific);
FunctionLiteral* function = info->literal();
DCHECK_NOT_NULL(function);
Scope* scope = function->scope();
DCHECK_NOT_NULL(scope);
DCHECK_EQ(scope, scope->GetClosureScope());
if (scope->is_repl_mode_scope() ||
!(scope->is_script_scope() || scope->is_eval_scope())) {
return true;
}
ZonePtrList<Statement>* body = function->body();
return RewriteBody(info, scope, body, out_has_stack_overflow).has_value();
}
std::optional<VariableProxy*> Rewriter::RewriteBody(
ParseInfo* info, Scope* scope, ZonePtrList<Statement>* body,
bool* out_has_stack_overflow) {
DisallowGarbageCollection no_gc;
DisallowHandleAllocation no_handles;
DisallowHandleDereference no_deref;
if (!body->is_empty()) {
Variable* result = scope->AsDeclarationScope()->NewTemporary(
info->ast_value_factory()->dot_result_string());
Processor processor(info->stack_limit(), scope->AsDeclarationScope(),
result, info->ast_value_factory(), info->zone());
processor.Process(body);
if (processor.HasStackOverflow()) {
*out_has_stack_overflow = true;
return std::nullopt;
}
if (processor.result_assigned()) {
int pos = kNoSourcePosition;
VariableProxy* result_value =
processor.factory()->NewVariableProxy(result, pos);
if (!info->flags().is_repl_mode()) {
Statement* result_statement;
result_statement =
processor.factory()->NewReturnStatement(result_value, pos);
body->Add(result_statement, info->zone());
}
return result_value;
}
}
return nullptr;
}
#undef PROCESS_AND_RETURN_IF_STACK_OVERFLOW
#undef VISIT_AND_RETURN_IF_STACK_OVERFLOW
}