Commit e474e5ff authored by marja's avatar marja Committed by Commit bot

PreParsing inner functions: Fix declaration-only variables, part 2.

If an inner function only declares a variable but doesn't use it, Parser
and PreParser produced different unresolved variables, and that confused
the pessimistic context allocation.

This is continuation to https://codereview.chromium.org/2388183003/

This CL fixes more complicated declarations (which are not just one
identifier). For this, PreParser needs to accumulate identifiers used
in expressions.

In addition, this CL manifests FLAG_lazy_inner_functions in tests, so that
we get clusterfuzz coverage for it.

BUG=chromium:650969, v8:5501

Review-Url: https://codereview.chromium.org/2400613003
Cr-Commit-Position: refs/heads/master@{#40112}
parent c59cf8cd
......@@ -1018,7 +1018,7 @@ bool Scope::RemoveUnresolved(VariableProxy* var) {
}
bool Scope::RemoveUnresolved(const AstRawString* name) {
if (unresolved_->raw_name() == name) {
if (unresolved_ != nullptr && unresolved_->raw_name() == name) {
VariableProxy* removed = unresolved_;
unresolved_ = unresolved_->next_unresolved();
removed->set_next_unresolved(nullptr);
......@@ -1027,7 +1027,7 @@ bool Scope::RemoveUnresolved(const AstRawString* name) {
VariableProxy* current = unresolved_;
while (current != nullptr) {
VariableProxy* next = current->next_unresolved();
if (next->raw_name() == name) {
if (next != nullptr && next->raw_name() == name) {
current->set_next_unresolved(next->next_unresolved());
next->set_next_unresolved(nullptr);
return true;
......
......@@ -124,6 +124,7 @@ class DiscardableZoneScope {
parser_->zone_ = temp_zone;
if (parser_->reusable_preparser_ != nullptr) {
parser_->reusable_preparser_->zone_ = temp_zone;
parser_->reusable_preparser_->factory()->set_zone(temp_zone);
}
}
}
......@@ -132,6 +133,7 @@ class DiscardableZoneScope {
parser_->zone_ = prev_zone_;
if (parser_->reusable_preparser_ != nullptr) {
parser_->reusable_preparser_->zone_ = prev_zone_;
parser_->reusable_preparser_->factory()->set_zone(prev_zone_);
}
ast_node_factory_scope_.Reset();
}
......
......@@ -228,7 +228,7 @@ PreParserExpression PreParser::ExpressionFromIdentifier(
scope()->NewUnresolved(&factory, name.string_, start_position, end_position,
NORMAL_VARIABLE);
}
return PreParserExpression::FromIdentifier(name);
return PreParserExpression::FromIdentifier(name, zone());
}
void PreParser::DeclareAndInitializeVariables(
......@@ -236,20 +236,23 @@ void PreParser::DeclareAndInitializeVariables(
const DeclarationDescriptor* declaration_descriptor,
const DeclarationParsingResult::Declaration* declaration,
ZoneList<const AstRawString*>* names, bool* ok) {
if (declaration->pattern.string_) {
if (declaration->pattern.identifiers_ != nullptr) {
DCHECK(FLAG_lazy_inner_functions);
/* Mimic what Parser does when declaring variables (see
Parser::PatternRewriter::VisitVariableProxy).
var + no initializer -> RemoveUnresolved
let + no initializer -> RemoveUnresolved
let / const + no initializer -> RemoveUnresolved
var + initializer -> RemoveUnresolved followed by NewUnresolved
let + initializer -> RemoveUnresolved
let / const + initializer -> RemoveUnresolved
*/
if (declaration->initializer.IsEmpty() ||
declaration_descriptor->mode == VariableMode::LET) {
declaration_descriptor->scope->RemoveUnresolved(
declaration->pattern.string_);
(declaration_descriptor->mode == VariableMode::LET ||
declaration_descriptor->mode == VariableMode::CONST)) {
for (auto identifier : *(declaration->pattern.identifiers_)) {
declaration_descriptor->scope->RemoveUnresolved(identifier);
}
}
}
}
......
......@@ -118,27 +118,33 @@ class PreParserIdentifier {
const AstRawString* string_;
friend class PreParserExpression;
friend class PreParser;
friend class PreParserFactory;
};
class PreParserExpression {
public:
PreParserExpression() : code_(TypeField::encode(kEmpty)) {}
PreParserExpression()
: code_(TypeField::encode(kEmpty)), identifiers_(nullptr) {}
static PreParserExpression Empty() { return PreParserExpression(); }
static PreParserExpression Default() {
return PreParserExpression(TypeField::encode(kExpression));
static PreParserExpression Default(
ZoneList<const AstRawString*>* identifiers = nullptr) {
return PreParserExpression(TypeField::encode(kExpression), identifiers);
}
static PreParserExpression Spread(PreParserExpression expression) {
return PreParserExpression(TypeField::encode(kSpreadExpression));
return PreParserExpression(TypeField::encode(kSpreadExpression),
expression.identifiers_);
}
static PreParserExpression FromIdentifier(PreParserIdentifier id) {
return PreParserExpression(TypeField::encode(kIdentifierExpression) |
IdentifierTypeField::encode(id.type_),
id.string_);
static PreParserExpression FromIdentifier(PreParserIdentifier id,
Zone* zone) {
PreParserExpression expression(TypeField::encode(kIdentifierExpression) |
IdentifierTypeField::encode(id.type_));
expression.AddIdentifier(id.string_, zone);
return expression;
}
static PreParserExpression BinaryOperation(PreParserExpression left,
......@@ -152,12 +158,16 @@ class PreParserExpression {
ExpressionTypeField::encode(kAssignment));
}
static PreParserExpression ObjectLiteral() {
return PreParserExpression(TypeField::encode(kObjectLiteralExpression));
static PreParserExpression ObjectLiteral(
ZoneList<const AstRawString*>* identifiers = nullptr) {
return PreParserExpression(TypeField::encode(kObjectLiteralExpression),
identifiers);
}
static PreParserExpression ArrayLiteral() {
return PreParserExpression(TypeField::encode(kArrayLiteralExpression));
static PreParserExpression ArrayLiteral(
ZoneList<const AstRawString*>* identifiers = nullptr) {
return PreParserExpression(TypeField::encode(kArrayLiteralExpression),
identifiers);
}
static PreParserExpression StringLiteral() {
......@@ -344,9 +354,20 @@ class PreParserExpression {
kAssignment
};
explicit PreParserExpression(uint32_t expression_code,
const AstRawString* string = nullptr)
: code_(expression_code), string_(string) {}
explicit PreParserExpression(
uint32_t expression_code,
ZoneList<const AstRawString*>* identifiers = nullptr)
: code_(expression_code), identifiers_(identifiers) {}
void AddIdentifier(const AstRawString* identifier, Zone* zone) {
if (identifier == nullptr) {
return;
}
if (identifiers_ == nullptr) {
identifiers_ = new (zone) ZoneList<const AstRawString*>(1, zone);
}
identifiers_->Add(identifier, zone);
}
// The first three bits are for the Type.
typedef BitField<Type, 0, 3> TypeField;
......@@ -368,31 +389,61 @@ class PreParserExpression {
typedef BitField<bool, TypeField::kNext, 1> HasCoverInitializedNameField;
uint32_t code_;
// Non-nullptr if the expression is one identifier.
const AstRawString* string_;
// If the PreParser is used in the identifier tracking mode,
// PreParserExpression accumulates identifiers in that expression.
ZoneList<const AstRawString*>* identifiers_;
friend class PreParser;
friend class PreParserFactory;
template <typename T>
friend class PreParserList;
};
// The pre-parser doesn't need to build lists of expressions, identifiers, or
// the like.
// the like. If the PreParser is used in identifier tracking mode, it needs to
// build lists of identifiers though.
template <typename T>
class PreParserList {
public:
// These functions make list->Add(some_expression) work (and do nothing).
PreParserList() : length_(0) {}
PreParserList() : length_(0), identifiers_(nullptr) {}
PreParserList* operator->() { return this; }
void Add(T, void*) { ++length_; }
void Add(T, Zone* zone);
int length() const { return length_; }
static PreParserList Null() { return PreParserList(-1); }
bool IsNull() const { return length_ == -1; }
private:
explicit PreParserList(int n) : length_(n) {}
explicit PreParserList(int n) : length_(n), identifiers_(nullptr) {}
int length_;
ZoneList<const AstRawString*>* identifiers_;
friend class PreParser;
friend class PreParserFactory;
};
template <>
inline void PreParserList<PreParserExpression>::Add(
PreParserExpression expression, Zone* zone) {
if (expression.identifiers_ != nullptr) {
DCHECK(FLAG_lazy_inner_functions);
DCHECK(zone != nullptr);
if (identifiers_ == nullptr) {
identifiers_ = new (zone) ZoneList<const AstRawString*>(1, zone);
}
for (auto identifier : (*expression.identifiers_)) {
identifiers_->Add(identifier, zone);
}
}
++length_;
}
template <typename T>
void PreParserList<T>::Add(T, Zone* zone) {
++length_;
}
typedef PreParserList<PreParserExpression> PreParserExpressionList;
class PreParserStatement;
......@@ -480,10 +531,18 @@ class PreParserStatement {
class PreParserFactory {
public:
explicit PreParserFactory(void* unused_value_factory) {}
explicit PreParserFactory(AstValueFactory* ast_value_factory)
: zone_(ast_value_factory->zone()) {}
void set_zone(Zone* zone) { zone_ = zone; }
PreParserExpression NewStringLiteral(PreParserIdentifier identifier,
int pos) {
return PreParserExpression::Default();
// This is needed for object literal property names. Property names are
// normalized to string literals during object literal parsing.
PreParserExpression expression = PreParserExpression::Default();
expression.AddIdentifier(identifier.string_, zone_);
return expression;
}
PreParserExpression NewNumberLiteral(double number,
int pos) {
......@@ -500,7 +559,7 @@ class PreParserFactory {
PreParserExpression NewArrayLiteral(PreParserExpressionList values,
int first_spread_index, int literal_index,
int pos) {
return PreParserExpression::ArrayLiteral();
return PreParserExpression::ArrayLiteral(values.identifiers_);
}
PreParserExpression NewClassLiteralProperty(PreParserExpression key,
PreParserExpression value,
......@@ -513,18 +572,18 @@ class PreParserFactory {
PreParserExpression value,
ObjectLiteralProperty::Kind kind,
bool is_computed_name) {
return PreParserExpression::Default();
return PreParserExpression::Default(value.identifiers_);
}
PreParserExpression NewObjectLiteralProperty(PreParserExpression key,
PreParserExpression value,
bool is_computed_name) {
return PreParserExpression::Default();
return PreParserExpression::Default(value.identifiers_);
}
PreParserExpression NewObjectLiteral(PreParserExpressionList properties,
int literal_index,
int boilerplate_properties,
int pos) {
return PreParserExpression::ObjectLiteral();
return PreParserExpression::ObjectLiteral(properties.identifiers_);
}
PreParserExpression NewVariableProxy(void* variable) {
return PreParserExpression::Default();
......@@ -693,6 +752,9 @@ class PreParserFactory {
static int dummy = 42;
return &dummy;
}
private:
Zone* zone_;
};
......@@ -1408,7 +1470,7 @@ class PreParser : public ParserBase<PreParser> {
V8_INLINE PreParserExpression
ExpressionListToExpression(PreParserExpressionList args) {
return PreParserExpression::Default();
return PreParserExpression::Default(args.identifiers_);
}
V8_INLINE void AddAccessorPrefixToFunctionName(bool is_get,
......
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